Exemplo n.º 1
0
        public DSACng(CngKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (key.AlgorithmGroup != CngAlgorithmGroup.Dsa)
            {
                throw new ArgumentException(SR.GetString(SR.Cryptography_ArgDSARequiresDSAKey), "key");
            }

            LegalKeySizesValue = s_legalKeySizes;
            CngKey cngKey = CngKey.Open(key.Handle, key.IsEphemeral ? CngKeyHandleOpenOptions.EphemeralKey : CngKeyHandleOpenOptions.None);

            Key = cngKey;
        }
Exemplo n.º 2
0
 public ECDsaCng(CngKey key)
 {
     this.m_hashAlgorithm = CngAlgorithm.Sha256;
     if (key == null)
     {
         throw new ArgumentNullException("key");
     }
     if (key.AlgorithmGroup != CngAlgorithmGroup.ECDsa)
     {
         throw new ArgumentException(System.SR.GetString("Cryptography_ArgECDsaRequiresECDsaKey"), "key");
     }
     if (!NCryptNative.NCryptSupported)
     {
         throw new PlatformNotSupportedException(System.SR.GetString("Cryptography_PlatformNotSupported"));
     }
     base.LegalKeySizesValue = s_legalKeySizes;
     new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
     this.Key = CngKey.Open(key.Handle, key.IsEphemeral ? CngKeyHandleOpenOptions.EphemeralKey : CngKeyHandleOpenOptions.None);
     CodeAccessPermission.RevertAssert();
     this.KeySize = this.m_key.KeySize;
 }
Exemplo n.º 3
0
        public ECDiffieHellmanCng(CngKey key)
        {
            Contract.Ensures(LegalKeySizesValue != null);
            Contract.Ensures(m_key != null && m_key.AlgorithmGroup == CngAlgorithmGroup.ECDiffieHellman);

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (key.AlgorithmGroup != CngAlgorithmGroup.ECDiffieHellman)
            {
                throw new ArgumentException(SR.GetString(SR.Cryptography_ArgECDHRequiresECDHKey), "key");
            }

            if (!NCryptNative.NCryptSupported)
            {
                throw new PlatformNotSupportedException(SR.GetString(SR.Cryptography_PlatformNotSupported));
            }

            LegalKeySizesValue = s_legalKeySizes;

            // Make a copy of the key so that we continue to work if it gets disposed before this algorithm
            //
            // This requires an assert for UnmanagedCode since we'll need to access the raw handles of the key
            // and the handle constructor of CngKey.  The assert is safe since ECDiffieHellmanCng will never
            // expose the key handles to calling code (without first demanding UnmanagedCode via the Handle
            // property of CngKey).
            //
            // The bizzare looking disposal of the key.Handle property is intentional - Handle returns a
            // duplicate - without disposing it, we keep the key alive until the GC runs.
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
            using (SafeNCryptKeyHandle importHandle = key.Handle) {
                Key = CngKey.Open(importHandle, key.IsEphemeral ? CngKeyHandleOpenOptions.EphemeralKey : CngKeyHandleOpenOptions.None);
            }
            CodeAccessPermission.RevertAssert();

            KeySize = m_key.KeySize;
        }
        internal ECDiffieHellmanCngPublicKey(CngKey key) : base(key.Export(CngKeyBlobFormat.EccPublicBlob))
        {
            Contract.Requires(key != null && key.AlgorithmGroup == CngAlgorithmGroup.ECDiffieHellman);
            Contract.Ensures(m_format != null);

            m_format = CngKeyBlobFormat.EccPublicBlob;

            //
            // We need to make a copy of the key to prevent the situation where the ECDiffieHellmanCng algorithm
            // object is disposed (this disposing its key) before the ECDiffieHellmanCngPublic key is disposed.
            //
            // Accessing the handle in partial trust is safe because we're not exposing it back out to user code
            //

            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();

            // This looks odd, but .Handle returns a duplicate, so we need to dispose it
            using (SafeNCryptKeyHandle importKey = key.Handle) {
                m_key = CngKey.Open(importKey, key.IsEphemeral ? CngKeyHandleOpenOptions.EphemeralKey : CngKeyHandleOpenOptions.None);
            }

            CodeAccessPermission.RevertAssert();
        }
Exemplo n.º 5
0
 public static CngKey Duplicate(CngKey key)
 {
     return(CngKey.Open(key.HandleNoDuplicate, key.IsEphemeral ? CngKeyHandleOpenOptions.EphemeralKey : CngKeyHandleOpenOptions.None));
 }