Exemple #1
0
        public MFTestResults DsaTest_ImportRsaKeyAndUseWithDsaShouldFail()
        {
            bool testResult = false;

            try
            {
                using (Session session = new Session("", MechanismType.DSA))
                    using (CryptoKey obj = CryptokiObject.CreateObject(session, m_privateRsaKey) as CryptoKey)
                    {
                        string dataToSign = "This is a simple message to be encrypted";

                        byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(dataToSign);

                        using (DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(obj))
                        {
                            byte[] signature = dsa.SignData(data);

                            dsa.VerifyData(data, signature);
                        }
                    }
            }
            catch (ArgumentException)
            {
                testResult = true;
            }
            catch
            {
                testResult = false;
            }

            return(testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }
Exemple #2
0
        /// <summary>
        /// Creates a CryptoKey in the specfied session context with the specified key attribute template.
        /// </summary>
        /// <param name="session">The Cryptoki session context.</param>
        /// <param name="keyTemplate">The Cryptoki attribute template that specifies key properties.</param>
        /// <returns></returns>
        public static CryptoKey LoadKey(Session session, CryptokiAttribute[] keyTemplate)
        {
            CryptoKey key = CryptokiObject.CreateObject(session, keyTemplate) as CryptoKey;

            key.m_keyType = KeyType.INVALID;

            return(key);
        }
Exemple #3
0
        internal bool Init(int sslMode, int sslVerify, IntPtr certificate, int cert_len, IntPtr szCertPwd, ref int sslContextHandle)
        {
            SslData data = new SslData();

            data.m_sslVersion = (SslProtocols.None);

            if ((sslMode & 0x18) != 0)
            {
                data.m_sslVersion = SslProtocols.Default;
            }
            else if ((sslMode & 0x10) != 0)
            {
                data.m_sslVersion = SslProtocols.Tls;
            }
            else if ((sslMode & 0x8) != 0)
            {
                data.m_sslVersion = SslProtocols.Ssl3;
            }
            else if ((sslMode & 0x4) != 0)
            {
                data.m_sslVersion = SslProtocols.Ssl2;
            }

            data.m_sslVerify = (SslVerification)sslVerify;
            if (certificate != IntPtr.Zero)
            {
                if (cert_len == 4)
                {
                    SessionData ctx = ((SessionDriver)this.Hal.Session).GetSessionCtx(sslContextHandle);

                    if (ctx == null)
                    {
                        return(false);
                    }

                    CryptokiObject obj = ctx.ObjectCtx.GetObject(Marshal.ReadInt32(certificate));

                    if (obj == null || obj.Type != CryptokiObjectType.Cert)
                    {
                        return(false);
                    }

                    data.m_cert = obj.Data as X509Certificate2;
                }
                else
                {
                    data.m_cert = CreateCert(certificate, cert_len, Marshal.PtrToStringAnsi(szCertPwd));
                }
            }

            lock (this)
            {
                _sslDataCollection[_sslDataCollectionIndex] = data;

                sslContextHandle = _sslDataCollectionIndex;

                System.Threading.Interlocked.Increment(ref _sslDataCollectionIndex);
            }

            return(true);
        }