public void _02_UintAttributeTest() { Helpers.CheckPlatform(); NativeULong originalValue = NativeLongUtils.ConvertFromCKO(CKO.CKO_DATA); // Create attribute with NativeULong value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_CLASS, originalValue); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_CLASS)); Assert.IsTrue(attr.value != IntPtr.Zero); Assert.IsTrue(attr.valueLen == NativeLongUtils.ConvertFromInt32(UnmanagedMemory.SizeOf(typeof(NativeULong)))); NativeULong recoveredValue = 0; // Read the value of attribute CkaUtils.ConvertValue(ref attr, out recoveredValue); Assert.IsTrue(originalValue == recoveredValue); // Free attribute value UnmanagedMemory.Free(ref attr.value); attr.valueLen = 0; Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_CLASS)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }
public void _07_GetObjectAttributes() { Helpers.CheckPlatform(); string uri = @"pkcs11:object=foo;type=private;id=%01%02%03"; Pkcs11Uri pkcs11uri = new Pkcs11Uri(uri); CK_ATTRIBUTE[] attributes = null; Pkcs11UriUtils.GetObjectAttributes(pkcs11uri, out attributes); Assert.IsTrue(attributes != null); Assert.IsTrue(attributes.Length == 3); Assert.IsTrue(attributes[0].type == NativeLongUtils.ConvertFromCKA(CKA.CKA_CLASS)); NativeULong ckaClass = 0; CkaUtils.ConvertValue(ref attributes[0], out ckaClass); Assert.IsTrue(ckaClass == NativeLongUtils.ConvertFromCKO(CKO.CKO_PRIVATE_KEY)); Assert.IsTrue(attributes[1].type == NativeLongUtils.ConvertFromCKA(CKA.CKA_LABEL)); string ckaLabel = null; CkaUtils.ConvertValue(ref attributes[1], out ckaLabel); Assert.IsTrue(ckaLabel == "foo"); Assert.IsTrue(attributes[2].type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ID)); byte[] ckaId = null; CkaUtils.ConvertValue(ref attributes[2], out ckaId); Assert.IsTrue(Common.Helpers.ByteArraysMatch(ckaId, new byte[] { 0x01, 0x02, 0x03 })); }
public void _03_BoolAttributeTest() { Helpers.CheckPlatform(); bool originalValue = true; // Create attribute with bool value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_TOKEN, originalValue); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_TOKEN)); Assert.IsTrue(attr.value != IntPtr.Zero); Assert.IsTrue(attr.valueLen == 1); bool recoveredValue = false; // Read the value of attribute CkaUtils.ConvertValue(ref attr, out recoveredValue); Assert.IsTrue(originalValue == recoveredValue); // Free attribute value UnmanagedMemory.Free(ref attr.value); attr.valueLen = 0; Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_TOKEN)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }
public void _10_CustomAttributeTest() { Helpers.CheckPlatform(); byte[] originalValue = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; // Create attribute without the value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_VALUE); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_VALUE)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); // Allocate unmanaged memory for attribute value.. attr.value = UnmanagedMemory.Allocate(originalValue.Length); // ..then set the value of attribute.. UnmanagedMemory.Write(attr.value, originalValue); // ..and finally set the length of attribute value. attr.valueLen = NativeLongUtils.ConvertFromInt32(originalValue.Length); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_VALUE)); Assert.IsTrue(attr.value != IntPtr.Zero); Assert.IsTrue(attr.valueLen == NativeLongUtils.ConvertFromInt32(originalValue.Length)); // Read the value of attribute byte[] recoveredValue = UnmanagedMemory.Read(attr.value, NativeLongUtils.ConvertToInt32(attr.valueLen)); Assert.IsTrue(ConvertUtils.BytesToBase64String(originalValue) == ConvertUtils.BytesToBase64String(recoveredValue)); // Free attribute value UnmanagedMemory.Free(ref attr.value); attr.valueLen = 0; Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_VALUE)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }
public void _06_DateTimeAttributeTest() { Helpers.CheckPlatform(); DateTime originalValue = new DateTime(2012, 1, 30, 0, 0, 0, DateTimeKind.Utc); // Create attribute with DateTime value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_START_DATE, originalValue); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_START_DATE)); Assert.IsTrue(attr.value != IntPtr.Zero); Assert.IsTrue(attr.valueLen == 8); DateTime?recoveredValue = null; // Read the value of attribute CkaUtils.ConvertValue(ref attr, out recoveredValue); Assert.IsTrue(originalValue == recoveredValue); // Free attribute value UnmanagedMemory.Free(ref attr.value); attr.valueLen = 0; Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_START_DATE)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }
public void _05_ByteArrayAttributeTest() { Helpers.CheckPlatform(); byte[] originalValue = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; // Create attribute with byte array value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_ID, originalValue); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ID)); Assert.IsTrue(attr.value != IntPtr.Zero); Assert.IsTrue(attr.valueLen == NativeLongUtils.ConvertFromInt32(originalValue.Length)); byte[] recoveredValue = null; // Read the value of attribute CkaUtils.ConvertValue(ref attr, out recoveredValue); Assert.IsTrue(ConvertUtils.BytesToBase64String(originalValue) == ConvertUtils.BytesToBase64String(recoveredValue)); // Free attribute value UnmanagedMemory.Free(ref attr.value); attr.valueLen = 0; Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ID)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); // Create attribute with null byte array value attr = CkaUtils.CreateAttribute(CKA.CKA_ID, (byte[])null); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ID)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }
public void _04_StringAttributeTest() { Helpers.CheckPlatform(); string originalValue = "Hello world"; // Create attribute with string value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_LABEL, originalValue); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_LABEL)); Assert.IsTrue(attr.value != IntPtr.Zero); Assert.IsTrue(attr.valueLen == NativeLongUtils.ConvertFromInt32(originalValue.Length)); string recoveredValue = null; // Read the value of attribute CkaUtils.ConvertValue(ref attr, out recoveredValue); Assert.IsTrue(originalValue == recoveredValue); // Free attribute value UnmanagedMemory.Free(ref attr.value); attr.valueLen = 0; Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_LABEL)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); // Create attribute with null string value attr = CkaUtils.CreateAttribute(CKA.CKA_LABEL, (string)null); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_LABEL)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }
public void _01_EmptyAttributeTest() { Helpers.CheckPlatform(); // Create attribute without the value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_CLASS); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_CLASS)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }
public void _09_MechanismArrayAttributeTest() { Helpers.CheckPlatform(); CKM[] originalValue = new CKM[2]; originalValue[0] = CKM.CKM_RSA_PKCS; originalValue[1] = CKM.CKM_AES_CBC; // Create attribute with mechanism array value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS)); Assert.IsTrue(attr.value != IntPtr.Zero); Assert.IsTrue(attr.valueLen == NativeLongUtils.ConvertFromInt32(UnmanagedMemory.SizeOf(typeof(NativeULong)) * originalValue.Length)); CKM[] recoveredValue = null; // Read the value of attribute CkaUtils.ConvertValue(ref attr, out recoveredValue); Assert.IsTrue(originalValue.Length == recoveredValue.Length); for (int i = 0; i < recoveredValue.Length; i++) { Assert.IsTrue(originalValue[i] == recoveredValue[i]); } // Free attribute value UnmanagedMemory.Free(ref attr.value); attr.valueLen = 0; Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); // Create attribute with null mechanism array value attr = CkaUtils.CreateAttribute(CKA.CKA_ALLOWED_MECHANISMS, (CKM[])null); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); // Create attribute with empty mechanism array value attr = CkaUtils.CreateAttribute(CKA.CKA_ALLOWED_MECHANISMS, new CKM[0]); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }
/// <summary> /// Checks whether object attributes match PKCS#11 URI /// </summary> /// <param name="pkcs11Uri">PKCS#11 URI</param> /// <param name="objectAttributes">Object attributes</param> /// <returns>True if object attributes match PKCS#11 URI</returns> public static bool Matches(Pkcs11Uri pkcs11Uri, List <ObjectAttribute> objectAttributes) { if (pkcs11Uri == null) { throw new ArgumentNullException("pkcs11Uri"); } if (objectAttributes == null) { throw new ArgumentNullException("objectAttributes"); } NativeULong ckaClassType = NativeLongUtils.ConvertFromCKA(CKA.CKA_CLASS); CKO? ckaClassValue = null; bool ckaClassFound = false; NativeULong ckaLabelType = NativeLongUtils.ConvertFromCKA(CKA.CKA_LABEL); string ckaLabelValue = null; bool ckaLabelFound = false; NativeULong ckaIdType = NativeLongUtils.ConvertFromCKA(CKA.CKA_ID); byte[] ckaIdValue = null; bool ckaIdFound = false; foreach (ObjectAttribute objectAttribute in objectAttributes) { if (objectAttribute == null) { continue; } if (objectAttribute.Type == ckaClassType) { ckaClassValue = NativeLongUtils.ConvertToCKO(objectAttribute.GetValueAsNativeUlong()); ckaClassFound = true; } else if (objectAttribute.Type == ckaLabelType) { ckaLabelValue = objectAttribute.GetValueAsString(); ckaLabelFound = true; } else if (objectAttribute.Type == ckaIdType) { ckaIdValue = objectAttribute.GetValueAsByteArray(); ckaIdFound = true; } if (ckaClassFound && ckaLabelFound && ckaIdFound) { break; } } if ((!ckaClassFound) && (pkcs11Uri.Type != null)) { throw new Pkcs11UriException("CKA_CLASS attribute is not present in the list of object attributes"); } if ((!ckaLabelFound) && (pkcs11Uri.Object != null)) { throw new Pkcs11UriException("CKA_LABEL attribute is not present in the list of object attributes"); } if ((!ckaIdFound) && (pkcs11Uri.Id != null)) { throw new Pkcs11UriException("CKA_ID attribute is not present in the list of object attributes"); } return(Pkcs11UriSharedUtils.Matches(pkcs11Uri, ckaClassValue, ckaLabelValue, ckaIdValue)); }
public void _07_AttributeArrayAttributeTest() { Helpers.CheckPlatform(); CK_ATTRIBUTE[] originalValue = new CK_ATTRIBUTE[2]; originalValue[0] = CkaUtils.CreateAttribute(CKA.CKA_TOKEN, true); originalValue[1] = CkaUtils.CreateAttribute(CKA.CKA_PRIVATE, true); // Create attribute with attribute array value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_WRAP_TEMPLATE, originalValue); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_WRAP_TEMPLATE)); Assert.IsTrue(attr.value != IntPtr.Zero); Assert.IsTrue(attr.valueLen == NativeLongUtils.ConvertFromInt32(UnmanagedMemory.SizeOf(typeof(CK_ATTRIBUTE)) * originalValue.Length)); CK_ATTRIBUTE[] recoveredValue = null; // Read the value of attribute CkaUtils.ConvertValue(ref attr, out recoveredValue); Assert.IsTrue(originalValue.Length == recoveredValue.Length); for (int i = 0; i < recoveredValue.Length; i++) { Assert.IsTrue(originalValue[i].type == recoveredValue[i].type); Assert.IsTrue(originalValue[i].valueLen == recoveredValue[i].valueLen); bool originalBool = false; // Read the value of nested attribute CkaUtils.ConvertValue(ref originalValue[i], out originalBool); bool recoveredBool = true; // Read the value of nested attribute CkaUtils.ConvertValue(ref recoveredValue[i], out recoveredBool); Assert.IsTrue(originalBool == recoveredBool); // In this example there is the same pointer to unmanaged memory // in both originalValue and recoveredValue array therefore it // needs to be freed only once. Assert.IsTrue(originalValue[i].value == recoveredValue[i].value); // Free value of nested attributes UnmanagedMemory.Free(ref originalValue[i].value); originalValue[i].valueLen = 0; recoveredValue[i].value = IntPtr.Zero; recoveredValue[i].valueLen = 0; } // Free attribute value UnmanagedMemory.Free(ref attr.value); attr.valueLen = 0; Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_WRAP_TEMPLATE)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); // Create attribute with null attribute array value attr = CkaUtils.CreateAttribute(CKA.CKA_WRAP_TEMPLATE, (CK_ATTRIBUTE[])null); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_WRAP_TEMPLATE)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); // Create attribute with empty attribute array value attr = CkaUtils.CreateAttribute(CKA.CKA_WRAP_TEMPLATE, new CK_ATTRIBUTE[0]); Assert.IsTrue(attr.type == NativeLongUtils.ConvertFromCKA(CKA.CKA_WRAP_TEMPLATE)); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }