public void Open()
 {
     ThrowIfDisposed();
     if (!Win32Native.CryptAcquireContext(out handle, ContainerName, ProviderName, ProviderType, Flags))
     {
         Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero();
     }
 }
 internal void DestroyKey(CryptKey key)
 {
     ThrowIfDisposedOrNotOpen();
     if (!Win32Native.CryptDestroyKey(key.Handle))
     {
         Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero();
     }
 }
        private Win32Native.SystemTime ToSystemTime(DateTime dateTime)
        {
            long fileTime   = dateTime.ToFileTime();
            var  systemTime = new Win32Native.SystemTime();

            if (!Win32Native.FileTimeToSystemTime(ref fileTime, systemTime))
            {
                Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero();
            }
            return(systemTime);
        }
 protected override void CleanUp(bool viaDispose)
 {
     if (handle != IntPtr.Zero)
     {
         if (!Win32Native.CryptReleaseContext(handle, 0))
         {
             if (viaDispose)
             {
                 Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero();
             }
         }
     }
 }
        public KeyExchangeKey GenerateKeyExchangeKey(bool exportable, int keyBitLength)
        {
            ThrowIfDisposedOrNotOpen();

            uint flags = (exportable ? 1U : 0U) | ((uint)keyBitLength) << 16;

            IntPtr keyHandle;
            bool   result = Win32Native.CryptGenKey(handle, (int)KeyType.Exchange, flags, out keyHandle);

            if (!result)
            {
                Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero();
            }

            return(new KeyExchangeKey(this, keyHandle));
        }
Esempio n. 6
0
        public X509Certificate2 CreateSelfSignedCertificate(SelfSignedCertProperties properties)
        {
            ThrowIfDisposedOrNotOpen();

            GenerateKeyExchangeKey(properties.IsPrivateKeyExportable, properties.KeyBitLength);
            //GenerateSignatureKey(properties.IsPrivateKeyExportable, properties.KeyBitLength);

            byte[]   asnName       = properties.Name.RawData;
            GCHandle asnNameHandle = GCHandle.Alloc(asnName, GCHandleType.Pinned);

            var kpi = new Win32Native.CryptKeyProviderInformation
            {
                ContainerName = this.ContainerName,
                KeySpec       = (int)KeyType.Exchange,
                ProviderType  = 1, // default RSA provider
            };

            IntPtr certContext = Win32Native.CertCreateSelfSignCertificate(
                handle,
                new Win32Native.CryptoApiBlob(asnName.Length, asnNameHandle.AddrOfPinnedObject()),
                0, kpi, IntPtr.Zero,
                ToSystemTime(properties.ValidFrom),
                ToSystemTime(properties.ValidTo),
                IntPtr.Zero);

            asnNameHandle.Free();

            if (IntPtr.Zero == certContext)
            {
                Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero();
            }

            X509Certificate2 cert = new X509Certificate2(certContext); // dups the context (increasing it's refcount)

            if (!Win32Native.CertFreeCertificateContext(certContext))
            {
                Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero();
            }

            return(cert);
        }