Esempio n. 1
0
 public byte[] GetCertificatePublicKey(byte[] certificateData)
 {
     using (var facade = new GostCryptFacade(ProviderParams))
     {
         facade.AcquireContext(null, Constants.CryptVerifycontext);
         using (KeyContext keyContext = facade.ImportSertificate(certificateData))
         {
             return(keyContext.ExportPublicKey());
         }
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Возвращает открытый ключ сертификата
 /// </summary>
 /// <param name="certificateData">данные сертификата</param>
 /// <returns></returns>
 public static byte[] GetCertificatePublicKey(byte[] certificateData)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(null, ProviderName, ProviderType, Constants.CryptVerifycontext);
         using (KeyContext keyContext = container.ImportSertificate(certificateData))
         {
             return(keyContext.ExportPublicKey());
         }
     }
 }
Esempio n. 3
0
        private KeyContext GetUserKey(int keySpec = 0)
        {
            IntPtr keyPiarHandler = IntPtr.Zero;

            if (!CryptoApi.CryptGetUserKey(cspHandler, keySpec, ref keyPiarHandler))
            {
                throw new Win32Exception();
            }

            var keyPairContext = new KeyContext(keyPiarHandler);

            return(keyPairContext);
        }
Esempio n. 4
0
        private KeyContext GenerateRandomKey(KeyNumber keyNumber, int flags = 0)
        {
            IntPtr keyPiarHandler = IntPtr.Zero;

            if (!CryptoApi.CryptGenKey(cspHandler, (int)keyNumber, flags, ref keyPiarHandler))
            {
                throw new Win32Exception();
            }

            var keyPairContext = new KeyContext(keyPiarHandler);

            return(keyPairContext);
        }
Esempio n. 5
0
 /// <summary>
 ///     Проверка подписи.
 /// </summary>
 /// <param name="signature">Подпись.</param>
 /// <param name="data">Данные.</param>
 /// <param name="certificateData">Сертификат.</param>
 /// <returns>True - провека прошла успешно, иначе False.</returns>
 public static bool VerifyCertificate(byte[] signature, byte[] data, byte[] certificateData)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(null, ProviderName, ProviderType, Constants.CryptVerifycontext);
         using (KeyContext keyContext = container.ImportSertificate(certificateData))
         {
             using (HashContext hashContext =
                        container.CreateHash(null, Constants.CpcspHashId, 0))
             {
                 hashContext.AddData(data, 0);
                 return(keyContext.VerifySignature(signature, hashContext, 0));
             }
         }
     }
 }
Esempio n. 6
0
 public bool VerifyCertificate(
     byte[] signature,
     byte[] data,
     byte[] certificateData)
 {
     using (var facade = new GostCryptFacade(ProviderParams))
     {
         facade.AcquireContext(null, Constants.CryptVerifycontext);
         using (KeyContext keyContext = facade.ImportSertificate(certificateData))
         {
             using (HashContext hashContext = facade.CreateHash(null, 0))
             {
                 hashContext.AddData(data, 0);
                 return(keyContext.VerifySignature(signature, hashContext, 0));
             }
         }
     }
 }
Esempio n. 7
0
 public bool VerifySignature(
     byte[] signature,
     byte[] data,
     byte[] publicKey)
 {
     using (var facade = new InfotecsFacade(ProviderParams))
     {
         facade.AcquireContext(null, Constants.CryptVerifycontext);
         using (KeyContext keyContext = facade.ImportKey(null, publicKey, 0))
         {
             using (HashContext hashContext = facade.CreateHash(null, 0))
             {
                 hashContext.AddData(data, 0);
                 return(keyContext.VerifySignature(signature, hashContext, 0));
             }
         }
     }
 }
Esempio n. 8
0
        private HashContext CreateHash(KeyContext keyContext, int algid, int flags)
        {
            IntPtr hashHandler = IntPtr.Zero;
            IntPtr keyHandler  = IntPtr.Zero;

            if (keyContext != null)
            {
                keyHandler = keyContext.Handler;
            }

            if (!CryptoApi.CryptCreateHash(cspHandler, algid, keyHandler, flags, ref hashHandler))
            {
                throw new Win32Exception();
            }

            var hashContext = new HashContext(hashHandler);

            return(hashContext);
        }
Esempio n. 9
0
        private KeyContext ImportKey(KeyContext protectionKeyContext, byte[] keyData, int flags)
        {
            IntPtr protectionKeyHandler = IntPtr.Zero;

            if (protectionKeyContext != null)
            {
                protectionKeyHandler = protectionKeyContext.Handler;
            }

            IntPtr keyHandler = IntPtr.Zero;

            if (!CryptoApi.CryptImportKey(cspHandler, keyData, keyData.Length,
                                          protectionKeyHandler, flags, ref keyHandler))
            {
                throw new Win32Exception();
            }

            var keyContext = new KeyContext(keyHandler);

            return(keyContext);
        }
Esempio n. 10
0
        private byte[] ExportKey(KeyContext protectionKeyContext, int blobType, int falgs)
        {
            IntPtr protectionKeyHandler = IntPtr.Zero;

            if (protectionKeyContext != null)
            {
                protectionKeyHandler = protectionKeyContext.Handler;
            }

            int exportBufferSize = 0;

            if (!CryptoApi.CryptExportKey(handler, protectionKeyHandler, blobType, falgs, null, ref exportBufferSize))
            {
                throw new Win32Exception();
            }

            var result = new byte[exportBufferSize];

            if (!CryptoApi.CryptExportKey(handler, protectionKeyHandler, blobType, falgs, result, ref exportBufferSize))
            {
                throw new Win32Exception();
            }
            return(result);
        }
Esempio n. 11
0
        private KeyContext GenerateRandomKey(KeyNumber keyNumber, int flags = 0)
        {
            IntPtr keyPiarHandler = IntPtr.Zero;
            if (!CryptoApi.CryptGenKey(cspHandler, (int)keyNumber, flags, ref keyPiarHandler))
            {
                throw new Win32Exception();
            }

            var keyPairContext = new KeyContext(keyPiarHandler);
            return keyPairContext;
        }
Esempio n. 12
0
        private HashContext CreateHash(KeyContext keyContext, int algid, int flags)
        {
            IntPtr hashHandler = IntPtr.Zero;
            IntPtr keyHandler = IntPtr.Zero;

            if (keyContext != null)
            {
                keyHandler = keyContext.Handler;
            }

            if (!CryptoApi.CryptCreateHash(cspHandler, algid, keyHandler, flags, ref hashHandler))
            {
                throw new Win32Exception();
            }

            var hashContext = new HashContext(hashHandler);
            return hashContext;
        }
Esempio n. 13
0
 /// <summary>
 ///     Экспорт открытого ключа.
 /// </summary>
 /// <param name="context">Контекст ключей.</param>
 /// <returns>Открытый ключ.</returns>
 public byte[] ExportPublicKey(KeyContext context = null)
 {
     byte[] result = ExportKey(context, Constants.PublicKeyBlob, 0);
     return(result);
 }
Esempio n. 14
0
        private byte[] ExportKey(KeyContext protectionKeyContext, int blobType, int falgs)
        {
            IntPtr protectionKeyHandler = IntPtr.Zero;

            if (protectionKeyContext != null)
            {
                protectionKeyHandler = protectionKeyContext.Handler;
            }

            int exportBufferSize = 0;
            if (!CryptoApi.CryptExportKey(handler, protectionKeyHandler, blobType, falgs, null, ref exportBufferSize))
            {
                throw new Win32Exception();
            }

            var result = new byte[exportBufferSize];
            if (!CryptoApi.CryptExportKey(handler, protectionKeyHandler, blobType, falgs, result, ref exportBufferSize))
            {
                throw new Win32Exception();
            }
            return result;
        }
Esempio n. 15
0
 /// <summary>
 ///     Экспорт открытого ключа.
 /// </summary>
 /// <param name="context">Контекст ключей.</param>
 /// <returns>Открытый ключ.</returns>
 public byte[] ExportPublicKey(KeyContext context = null)
 {
     byte[] result = ExportKey(context, Constants.PublicKeyBlob, 0);
     return result;
 }
Esempio n. 16
0
        private KeyContext GetUserKey(int keySpec = 0)
        {
            IntPtr keyPiarHandler = IntPtr.Zero;
            if (!CryptoApi.CryptGetUserKey(cspHandler, keySpec, ref keyPiarHandler))
            {
                throw new Win32Exception();
            }

            var keyPairContext = new KeyContext(keyPiarHandler);
            return keyPairContext;
        }
Esempio n. 17
0
        private KeyContext ImportKey(KeyContext protectionKeyContext, byte[] keyData, int keyDataLength, int flags)
        {
            IntPtr protectionKeyHandler = IntPtr.Zero;

            if (protectionKeyContext != null)
            {
                protectionKeyHandler = protectionKeyContext.Handler;
            }

            IntPtr keyHandler = IntPtr.Zero;
            if (
                !CryptoApi.CryptImportKey(
                    cspHandler, keyData, keyDataLength, protectionKeyHandler, flags, ref keyHandler))
            {
                throw new Win32Exception();
            }

            var keyContext = new KeyContext(keyHandler);
            return keyContext;
        }
        private KeyContext ImportSertificate(byte[] certificateData)
        {
            // создаём объект сертификата
            var hCertContext = CryptoApi.CertCreateCertificateContext(
                Constants.MyEncodingType, certificateData, certificateData.Length);

            //Получаем указатель на SubjectPublicKeyInfo
            var certContextStruct = (Constants.CertContext)
                Marshal.PtrToStructure(hCertContext, typeof(Constants.CertContext));
            var pCertInfo = certContextStruct.pCertInfo;

            // магия. для x32 и x64 сборок структуры разных размеров
            var certInfoStruct = (Constants.CertInfo)Marshal.PtrToStructure(pCertInfo, typeof(Constants.CertInfo));
            IntPtr subjectPublicKeyInfo = Marshal.AllocHGlobal(Marshal.SizeOf(certInfoStruct.SubjectPublicKeyInfo));
            Marshal.StructureToPtr(certInfoStruct.SubjectPublicKeyInfo, subjectPublicKeyInfo, false);

            IntPtr keyHandler = IntPtr.Zero;
            if (!CryptoApi.CryptImportPublicKeyInfo(cspHandler, Constants.MyEncodingType,
                subjectPublicKeyInfo, ref keyHandler))
            {
                throw new Win32Exception();
            }

            var keyContext = new KeyContext(keyHandler);
            return keyContext;
        }