Exemple #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());
         }
     }
 }
Exemple #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());
         }
     }
 }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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));
             }
         }
     }
 }
Exemple #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));
             }
         }
     }
 }
Exemple #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));
             }
         }
     }
 }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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);
        }
        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;
        }
        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;
        }
Exemple #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);
 }
Exemple #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;
        }
Exemple #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;
 }
        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;
        }
        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;
        }