Esempio n. 1
0
        public void _02_EmptyAttributeTest()
        {
            Helpers.CheckPlatform();

            // Create attribute without the value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_CLASS))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_CLASS));
                Assert.IsTrue(attr.GetValueAsByteArray() == null);
            }
        }
Esempio n. 2
0
        public void _03_UintAttributeTest()
        {
            Helpers.CheckPlatform();

            NativeULong value = NativeLongUtils.ConvertFromCKO(CKO.CKO_DATA);

            // Create attribute with NativeULong value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_CLASS, value))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_CLASS));
                Assert.IsTrue(attr.GetValueAsNativeUlong() == value);
            }
        }
Esempio n. 3
0
        public void _07_DateTimeAttributeTest()
        {
            Helpers.CheckPlatform();

            DateTime value = new DateTime(2012, 1, 30, 0, 0, 0, DateTimeKind.Utc);

            // Create attribute with DateTime value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_START_DATE, value))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_START_DATE));
                Assert.IsTrue(attr.GetValueAsDateTime() == value);
            }
        }
Esempio n. 4
0
        public void _04_BoolAttributeTest()
        {
            Helpers.CheckPlatform();

            bool value = true;

            // Create attribute with bool value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_TOKEN, value))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_TOKEN));
                Assert.IsTrue(attr.GetValueAsBool() == value);
            }
        }
Esempio n. 5
0
        public void _10_MechanismArrayAttributeTest()
        {
            Helpers.CheckPlatform();

            List <CKM> originalValue = new List <CKM>();

            originalValue.Add(CKM.CKM_RSA_PKCS);
            originalValue.Add(CKM.CKM_AES_CBC);

            // Create attribute with mechanism array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS));

                List <CKM> recoveredValue = attr.GetValueAsCkmList();
                for (int i = 0; i < recoveredValue.Count; i++)
                {
                    Assert.IsTrue(originalValue[i] == recoveredValue[i]);
                }
            }

            originalValue = null;

            // Create attribute with null mechanism array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS));
                Assert.IsTrue(attr.GetValueAsCkmList() == originalValue);
            }

            originalValue = new List <CKM>();

            // Create attribute with empty mechanism array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS));
                Assert.IsTrue(attr.GetValueAsCkmList() == null);
            }
        }
Esempio n. 6
0
        public void _09_UintArrayAttributeTest()
        {
            Helpers.CheckPlatform();

            List <NativeULong> originalValue = new List <NativeULong>();

            originalValue.Add(333333);
            originalValue.Add(666666);

            // Create attribute with NativeULong array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS));

                List <NativeULong> recoveredValue = attr.GetValueAsNativeULongList();
                for (int i = 0; i < recoveredValue.Count; i++)
                {
                    Assert.IsTrue(originalValue[i] == recoveredValue[i]);
                }
            }

            originalValue = null;

            // Create attribute with null NativeULong array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS));
                Assert.IsTrue(attr.GetValueAsNativeULongList() == originalValue);
            }

            originalValue = new List <NativeULong>();

            // Create attribute with empty NativeULong array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ALLOWED_MECHANISMS));
                Assert.IsTrue(attr.GetValueAsNativeULongList() == null);
            }
        }
Esempio n. 7
0
        public void _06_ByteArrayAttributeTest()
        {
            Helpers.CheckPlatform();

            byte[] value = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };

            // Create attribute with byte array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_ID, value))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ID));
                Assert.IsTrue(ConvertUtils.BytesToBase64String(attr.GetValueAsByteArray()) == ConvertUtils.BytesToBase64String(value));
            }

            value = null;

            // Create attribute with null byte array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_ID, value))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ID));
                Assert.IsTrue(attr.GetValueAsByteArray() == value);
            }
        }
Esempio n. 8
0
        public void _05_StringAttributeTest()
        {
            Helpers.CheckPlatform();

            string value = "Hello world";

            // Create attribute with string value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_LABEL, value))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_LABEL));
                Assert.IsTrue(attr.GetValueAsString() == value);
            }

            value = null;

            // Create attribute with null string value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_LABEL, value))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_LABEL));
                Assert.IsTrue(attr.GetValueAsString() == value);
            }
        }
Esempio n. 9
0
        public void _07_GetObjectAttributes()
        {
            Helpers.CheckPlatform();

            string uri = @"pkcs11:object=foo;type=private;id=%01%02%03";

            Pkcs11Uri pkcs11uri = new Pkcs11Uri(uri);

            List <ObjectAttribute> attributes = null;

            Pkcs11UriUtils.GetObjectAttributes(pkcs11uri, out attributes);

            Assert.IsTrue(attributes != null);
            Assert.IsTrue(attributes.Count == 3);

            Assert.IsTrue(attributes[0].Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_CLASS));
            Assert.IsTrue(attributes[0].GetValueAsNativeUlong() == NativeLongUtils.ConvertFromCKO(CKO.CKO_PRIVATE_KEY));

            Assert.IsTrue(attributes[1].Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_LABEL));
            Assert.IsTrue(attributes[1].GetValueAsString() == "foo");

            Assert.IsTrue(attributes[2].Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_ID));
            Assert.IsTrue(Common.Helpers.ByteArraysMatch(attributes[2].GetValueAsByteArray(), new byte[] { 0x01, 0x02, 0x03 }));
        }
Esempio n. 10
0
        public void _08_AttributeArrayAttributeTest()
        {
            Helpers.CheckPlatform();

            ObjectAttribute nestedAttribute1 = new ObjectAttribute(CKA.CKA_TOKEN, true);
            ObjectAttribute nestedAttribute2 = new ObjectAttribute(CKA.CKA_PRIVATE, true);

            List <ObjectAttribute> originalValue = new List <ObjectAttribute>();

            originalValue.Add(nestedAttribute1);
            originalValue.Add(nestedAttribute2);

            // Create attribute with attribute array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_WRAP_TEMPLATE, originalValue))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_WRAP_TEMPLATE));

                List <ObjectAttribute> recoveredValue = attr.GetValueAsObjectAttributeList();
                Assert.IsTrue(recoveredValue.Count == 2);
                Assert.IsTrue(recoveredValue[0].Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_TOKEN));
                Assert.IsTrue(recoveredValue[0].GetValueAsBool() == true);
                Assert.IsTrue(recoveredValue[1].Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_PRIVATE));
                Assert.IsTrue(recoveredValue[1].GetValueAsBool() == true);
            }

            // There is the same pointer to unmanaged memory in both nestedAttribute1 and recoveredValue[0] instances
            // therefore private low level attribute structure needs to be modified to prevent double free.
            // This special handling is needed only in this synthetic test and should be avoided in real world application.
            CK_ATTRIBUTE ckAttribute1 = (CK_ATTRIBUTE)typeof(ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(nestedAttribute1);

            ckAttribute1.value    = IntPtr.Zero;
            ckAttribute1.valueLen = 0;
            typeof(ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(nestedAttribute1, ckAttribute1);

            // There is the same pointer to unmanaged memory in both nestedAttribute2 and recoveredValue[1] instances
            // therefore private low level attribute structure needs to be modified to prevent double free.
            // This special handling is needed only in this synthetic test and should be avoided in real world application.
            CK_ATTRIBUTE ckAttribute2 = (CK_ATTRIBUTE)typeof(ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(nestedAttribute2);

            ckAttribute2.value    = IntPtr.Zero;
            ckAttribute2.valueLen = 0;
            typeof(ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(nestedAttribute2, ckAttribute2);

            originalValue = null;

            // Create attribute with null attribute array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_WRAP_TEMPLATE, originalValue))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_WRAP_TEMPLATE));
                Assert.IsTrue(attr.GetValueAsObjectAttributeList() == originalValue);
            }

            originalValue = new List <ObjectAttribute>();

            // Create attribute with empty attribute array value
            using (ObjectAttribute attr = new ObjectAttribute(CKA.CKA_WRAP_TEMPLATE, originalValue))
            {
                Assert.IsTrue(attr.Type == NativeLongUtils.ConvertFromCKA(CKA.CKA_WRAP_TEMPLATE));
                Assert.IsTrue(attr.GetValueAsObjectAttributeList() == null);
            }
        }