3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
10 // RSAPKCS1SignatureDeformatter.cs
14 using System.Diagnostics.Contracts;
15 using System.Security.Cryptography.X509Certificates;
17 namespace System.Security.Cryptography {
18 [System.Runtime.InteropServices.ComVisible(true)]
19 public class RSAPKCS1SignatureDeformatter : AsymmetricSignatureDeformatter {
21 // This class provides the PKCS#1 v1.5 signature format processing during
22 // the verification process (i.e. decrypting the object). The class has
23 // some special code for dealing with the CSP based RSA keys as the
24 // formatting and verification is done within the CSP rather than in
28 private RSA _rsaKey; // RSA Key value to do decrypt operation
29 private String _strOID; // OID value for the HASH algorithm
32 // public constructors
35 public RSAPKCS1SignatureDeformatter() {}
36 public RSAPKCS1SignatureDeformatter(AsymmetricAlgorithm key) {
38 throw new ArgumentNullException("key");
39 Contract.EndContractBlock();
47 public override void SetKey(AsymmetricAlgorithm key) {
49 throw new ArgumentNullException("key");
50 Contract.EndContractBlock();
54 public override void SetHashAlgorithm(String strName) {
55 _strOID = CryptoConfig.MapNameToOID(strName, OidGroup.HashAlgorithm);
58 [System.Security.SecuritySafeCritical] // auto-generated
59 public override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature) {
61 throw new ArgumentNullException("rgbHash");
62 if (rgbSignature == null)
63 throw new ArgumentNullException("rgbSignature");
64 Contract.EndContractBlock();
67 throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingOID"));
69 throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));
71 // Two cases here -- if we are talking to the CSP version or if we are talking to some other RSA provider.
72 if (_rsaKey is RSACryptoServiceProvider) {
73 int calgHash = X509Utils.GetAlgIdFromOid(_strOID, OidGroup.HashAlgorithm);
74 return ((RSACryptoServiceProvider)_rsaKey).VerifyHash(rgbHash, calgHash, rgbSignature);
77 byte[] pad = Utils.RsaPkcs1Padding(_rsaKey, CryptoConfig.EncodeOID(_strOID), rgbHash);
78 // Apply the public key to the signature data to get back the padded buffer actually signed.
79 // Compare the two buffers to see if they match; ignoring any leading zeros
80 return Utils.CompareBigIntArrays(_rsaKey.EncryptValue(rgbSignature), pad);