Skip to content

Commit

Permalink
#4 Add example program.
Browse files Browse the repository at this point in the history
  • Loading branch information
harrison314 committed Jan 18, 2024
1 parent be0d016 commit f932e03
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/Test/BouncyHsm.ExampleApp/BouncyHsm.ExampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
<PackageReference Include="Pkcs11Interop" Version="5.1.2" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="Exists('../../x64/Debug/BouncyHsm.Pkcs11Lib.dll')">
<None Include="../../x64/Debug/BouncyHsm.Pkcs11Lib.dll">
<Link>%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


<ItemGroup Condition="Exists('../../../build_linux/BouncyHsm.Pkcs11Lib-x64.so')">
<None Include="../../../build_linux/BouncyHsm.Pkcs11Lib-x64.so">
<Link>%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
72 changes: 70 additions & 2 deletions src/Test/BouncyHsm.ExampleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
using Net.Pkcs11Interop.Common;
using Net.Pkcs11Interop.HighLevelAPI;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;

namespace BouncyHsm.ExampleApp;

public static class Program
{
public const string P11LibPath = "BouncyHsm.Pkcs11Lib.dll";
public static string P11LibPath
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return "BouncyHsm.Pkcs11Lib.dll";
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return "./BouncyHsm.Pkcs11Lib-x64.so";
}

throw new PlatformNotSupportedException();
}
}

public const string UserPin = "123456";

Expand All @@ -20,7 +37,8 @@ public static void Main(string[] args)
Environment.SetEnvironmentVariable("BOUNCY_HSM_CFG_STRING", "Server=127.0.0.1; Port=8765; LogTarget=Console; LogLevel=TRACE; Tag=MyExampleApp;");

//CrateObjectExample();
EncryptAndDecrypt();
//EncryptAndDecrypt();
CreateSesitiveData();
}

private static void CrateObjectExample()
Expand Down Expand Up @@ -93,4 +111,54 @@ private static void EncryptAndDecrypt()
byte[] cipherText = session.Encrypt(mechanism, key, plainText);
byte[] decrypted = session.Decrypt(mechanism, key, cipherText);
}

private static void CreateSesitiveData()
{

Pkcs11InteropFactories factories = new Pkcs11InteropFactories();
using IPkcs11Library library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories,
P11LibPath,
AppType.SingleThreaded);

List<ISlot> slots = library.GetSlotList(SlotsType.WithTokenPresent);
ISlot slot = slots.First();

using ISession session = slot.OpenSession(SessionType.ReadWrite);
session.Login(CKU.CKU_USER, UserPin);


string label = $"AES-{DateTime.UtcNow}-{Random.Shared.Next(100, 999)}";
byte[] ckId = session.GenerateRandom(32);

List<IObjectAttribute> keyAttributes = new List<IObjectAttribute>()
{
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, false),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_PRIVATE, true),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_LABEL, label),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_ID, ckId),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_ENCRYPT, true),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_DECRYPT, true),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_VERIFY, true),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_SENSITIVE, true),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_EXTRACTABLE, false),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_DESTROYABLE, true),
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_VALUE_LEN, (uint)32),
};

using IMechanism keyGenMechanism = session.Factories.MechanismFactory.Create(CKM.CKM_AES_KEY_GEN);
IObjectHandle key = session.GenerateKey(keyGenMechanism, keyAttributes);

List<CKA> listAttr = new List<CKA>()
{
CKA.CKA_LABEL,
CKA.CKA_ID,
CKA.CKA_CLASS,
CKA.CKA_KEY_TYPE,
CKA.CKA_EXTRACTABLE
};

List<IObjectAttribute> attributes = session.GetAttributeValue(key, listAttr);

Console.WriteLine("Key is sensitive {0}", attributes[4].GetValueAsBool());
}
}

0 comments on commit f932e03

Please sign in to comment.