/// <summary> /// Initializes an instance of <see cref="MD4CryptoServiceProvider"/>. /// </summary> /// <exception cref="ObjectDisposedException">The MD4CryptoServiceProvider instance has been disposed.</exception> public override void Initialize() { if (m_Disposed) { throw new ObjectDisposedException(this.GetType().FullName); } if (m_Hash != 0) { SspiProvider.CryptDestroyHash(m_Hash); } SspiProvider.CryptCreateHash(m_Provider, SecurityConstants.CALG_MD4, 0, 0, out m_Hash); }
public bool VerifySignature(Certificate cert, byte[] signature, byte[] hash) { int provider = 0; int hashptr = 0; int pubKey = 0; try { if (SspiProvider.CryptAcquireContext(ref provider, IntPtr.Zero, null, SecurityConstants.PROV_RSA_FULL, 0) == 0) { if (Marshal.GetLastWin32Error() == SecurityConstants.NTE_BAD_KEYSET) { SspiProvider.CryptAcquireContext(ref provider, IntPtr.Zero, null, SecurityConstants.PROV_RSA_FULL, SecurityConstants.CRYPT_NEWKEYSET); } } if (provider == 0) { throw new CryptographicException("Unable to acquire a cryptographic context."); } if (SspiProvider.CryptCreateHash(provider, SecurityConstants.CALG_SSL3_SHAMD5, 0, 0, out hashptr) == 0) { throw new CryptographicException("Unable to create the SHA-MD5 hash."); } if (SspiProvider.CryptSetHashParam(hashptr, SecurityConstants.HP_HASHVAL, hash, 0) == 0) { throw new CryptographicException("Unable to set the value of the SHA-MD5 hash."); } CertificateInfo ci = cert.GetCertificateInfo(); CERT_PUBLIC_KEY_INFO pki = new CERT_PUBLIC_KEY_INFO(ci); if (SspiProvider.CryptImportPublicKeyInfo(provider, SecurityConstants.X509_ASN_ENCODING | SecurityConstants.PKCS_7_ASN_ENCODING, ref pki, out pubKey) == 0) { throw new CryptographicException("Unable to get a handle to the public key of the specified certificate."); } byte[] sign_rev = new byte[signature.Length]; Array.Copy(signature, 0, sign_rev, 0, signature.Length); Array.Reverse(sign_rev); return(SspiProvider.CryptVerifySignature(hashptr, sign_rev, sign_rev.Length, pubKey, IntPtr.Zero, 0) != 0); } finally { if (pubKey != 0) { SspiProvider.CryptDestroyKey(pubKey); } if (hashptr != 0) { SspiProvider.CryptDestroyHash(hashptr); } if (provider != 0) { SspiProvider.CryptReleaseContext(provider, 0); } } }
protected override void Dispose(bool disposing) { if (!m_Disposed) { if (m_Hash != 0) { SspiProvider.CryptDestroyHash(m_Hash); m_Hash = 0; } try { GC.SuppressFinalize(this); } catch {} m_Disposed = true; } }
public byte[] CreateSignature(Certificate cert, byte[] hash) { int flags = 0, mustFree = 0, provider = 0, keySpec = 0, hashptr = 0, size = 0; try { if (!Environment.UserInteractive) { flags = SecurityConstants.CRYPT_ACQUIRE_SILENT_FLAG; } if (SspiProvider.CryptAcquireCertificatePrivateKey(cert.Handle, flags, IntPtr.Zero, ref provider, ref keySpec, ref mustFree) == 0) { throw new SslException(AlertDescription.InternalError, "Could not acquire private key."); } if (SspiProvider.CryptCreateHash(provider, SecurityConstants.CALG_SSL3_SHAMD5, 0, 0, out hashptr) == 0) { throw new CryptographicException("Unable to create the SHA-MD5 hash."); } if (SspiProvider.CryptSetHashParam(hashptr, SecurityConstants.HP_HASHVAL, hash, 0) == 0) { throw new CryptographicException("Unable to set the value of the SHA-MD5 hash."); } SspiProvider.CryptSignHash(hashptr, keySpec, IntPtr.Zero, 0, null, ref size); if (size == 0) { throw new CryptographicException("Unable to sign the data."); } byte[] buffer = new byte[size]; if (SspiProvider.CryptSignHash(hashptr, keySpec, IntPtr.Zero, 0, buffer, ref size) == 0) { throw new CryptographicException("Unable to sign the data."); } Array.Reverse(buffer); return(buffer); } finally { if (hashptr != 0) { SspiProvider.CryptDestroyHash(hashptr); } if (mustFree != 0 && provider != 0) { SspiProvider.CryptReleaseContext(provider, 0); } } }
public override void Initialize() { if (m_Disposed) { throw new ObjectDisposedException(this.GetType().FullName); } if (m_Hash != 0) { SspiProvider.CryptDestroyHash(m_Hash); } int type = SecurityConstants.CALG_SHA1; m_Size = 20; if (m_Type == HashType.MD5) { type = SecurityConstants.CALG_MD5; m_Size = 16; } SspiProvider.CryptCreateHash(m_Provider, type, 0, 0, out m_Hash); }
/// <summary> /// Releases the unmanaged resources used by the <see cref="MD4CryptoServiceProvider"/> and optionally releases the managed resources. /// </summary> /// <param name="disposing"><b>true</b> to release both managed and unmanaged resources; <b>false</b> to release only unmanaged resources.</param> protected override void Dispose(bool disposing) { if (!m_Disposed) { if (m_Hash != 0) { SspiProvider.CryptDestroyHash(m_Hash); m_Hash = 0; } /* if (m_Provider != 0) { * SspiProvider.CryptReleaseContext(m_Provider, 0); * m_Provider = 0; * }*/ try { GC.SuppressFinalize(this); } catch {} m_Disposed = true; } }