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

            byte[]        parameter = new byte[16];
            System.Random rng       = new Random();
            rng.NextBytes(parameter);

            // Create mechanism with the byte array parameter
            Mechanism mechanism = new Mechanism(CKM.CKM_AES_CBC, parameter);

            Assert.IsTrue(mechanism.Type == NativeLongUtils.ConvertFromCKM(CKM.CKM_AES_CBC));

            // We access private Mechanism member here just for the testing purposes
            CK_MECHANISM ckMechanism = (CK_MECHANISM)typeof(Mechanism).GetField("_ckMechanism", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(mechanism);

            Assert.IsTrue(ckMechanism.Mechanism == NativeLongUtils.ConvertFromCKM(CKM.CKM_AES_CBC));
            Assert.IsTrue(ckMechanism.Parameter != IntPtr.Zero);
            Assert.IsTrue(ckMechanism.ParameterLen == NativeLongUtils.ConvertFromInt32(parameter.Length));

            parameter = null;

            // Create mechanism with null byte array parameter
            mechanism = new Mechanism(CKM.CKM_AES_CBC, parameter);
            Assert.IsTrue(mechanism.Type == NativeLongUtils.ConvertFromCKM(CKM.CKM_AES_CBC));

            // We access private Mechanism member here just for the testing purposes
            ckMechanism = (CK_MECHANISM)typeof(Mechanism).GetField("_ckMechanism", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(mechanism);
            Assert.IsTrue(ckMechanism.Mechanism == NativeLongUtils.ConvertFromCKM(CKM.CKM_AES_CBC));
            Assert.IsTrue(ckMechanism.Parameter == IntPtr.Zero);
            Assert.IsTrue(ckMechanism.ParameterLen == 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);
            }
        }
        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);
            }
        }
        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);
            }
        }
        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. 6
0
        public void _02_EmptyParameterTest()
        {
            Helpers.CheckPlatform();

            // Create mechanism without the parameter
            Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS);

            Assert.IsTrue(mechanism.Type == NativeLongUtils.ConvertFromCKM(CKM.CKM_RSA_PKCS));

            // We access private Mechanism member just for the testing purposes
            CK_MECHANISM ckMechanism = (CK_MECHANISM)typeof(Mechanism).GetField("_ckMechanism", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(mechanism);

            Assert.IsTrue(ckMechanism.Mechanism == NativeLongUtils.ConvertFromCKM(CKM.CKM_RSA_PKCS));
            Assert.IsTrue(ckMechanism.Parameter == IntPtr.Zero);
            Assert.IsTrue(ckMechanism.ParameterLen == 0);
        }
Esempio n. 7
0
        public void _03_EncryptAndDecryptSinglePartOaepTest()
        {
            Helpers.CheckPlatform();

            using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType))
            {
                // Find first slot with token present
                Slot slot = Helpers.GetUsableSlot(pkcs11);

                // Open RW session
                using (Session session = slot.OpenSession(SessionType.ReadWrite))
                {
                    // Login as normal user
                    session.Login(CKU.CKU_USER, Settings.NormalUserPin);

                    // Generate key pair
                    ObjectHandle publicKey  = null;
                    ObjectHandle privateKey = null;
                    Helpers.GenerateKeyPair(session, out publicKey, out privateKey);

                    // Specify mechanism parameters
                    CkRsaPkcsOaepParams mechanismParams = new CkRsaPkcsOaepParams(NativeLongUtils.ConvertFromCKM(CKM.CKM_SHA_1), NativeLongUtils.ConvertFromCKG(CKG.CKG_MGF1_SHA1), NativeLongUtils.ConvertFromUInt32(CKZ.CKZ_DATA_SPECIFIED), null);

                    // Specify encryption mechanism with parameters
                    Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS_OAEP, mechanismParams);

                    byte[] sourceData = ConvertUtils.Utf8StringToBytes("Hello world");

                    // Encrypt data
                    byte[] encryptedData = session.Encrypt(mechanism, publicKey, sourceData);

                    // Do something interesting with encrypted data

                    // Decrypt data
                    byte[] decryptedData = session.Decrypt(mechanism, privateKey, encryptedData);

                    // Do something interesting with decrypted data
                    Assert.IsTrue(ConvertUtils.BytesToBase64String(sourceData) == ConvertUtils.BytesToBase64String(decryptedData));

                    session.DestroyObject(privateKey);
                    session.DestroyObject(publicKey);
                    session.Logout();
                }
            }
        }
        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);
            }
        }
        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);
            }
        }
        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);
            }
        }
        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);
            }
        }
        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. 13
0
        public void _04_ObjectParameterTest()
        {
            Helpers.CheckPlatform();

            byte[]        data = new byte[24];
            System.Random rng  = new Random();
            rng.NextBytes(data);

            // Specify mechanism parameters
            CkKeyDerivationStringData parameter = new CkKeyDerivationStringData(data);

            // Create mechanism with the object as parameter
            Mechanism mechanism = new Mechanism(CKM.CKM_XOR_BASE_AND_DATA, parameter);

            Assert.IsTrue(mechanism.Type == NativeLongUtils.ConvertFromCKM(CKM.CKM_XOR_BASE_AND_DATA));

            // We access private Mechanism member here just for the testing purposes
            CK_MECHANISM ckMechanism = (CK_MECHANISM)typeof(Mechanism).GetField("_ckMechanism", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(mechanism);

            Assert.IsTrue(ckMechanism.Mechanism == NativeLongUtils.ConvertFromCKM(CKM.CKM_XOR_BASE_AND_DATA));
            Assert.IsTrue(ckMechanism.Parameter != IntPtr.Zero);
            Assert.IsTrue(NativeLongUtils.ConvertToInt32(ckMechanism.ParameterLen) == UnmanagedMemory.SizeOf(typeof(CK_KEY_DERIVATION_STRING_DATA)));
        }
        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);
            }
        }