using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using Mono.Security.Cryptography; namespace System.Security.Cryptography { /// Performs asymmetric encryption and decryption using the implementation of the algorithm provided by the cryptographic service provider (CSP). This class cannot be inherited. // Token: 0x0200051C RID: 1308 [ComVisible(true)] public sealed class RSACryptoServiceProvider : RSA, ICspAsymmetricAlgorithm { /// Initializes a new instance of the class using the default key. /// The cryptographic service provider (CSP) cannot be acquired. // Token: 0x0600300E RID: 12302 RVA: 0x0009D5D4 File Offset: 0x0009B7D4 public RSACryptoServiceProvider() { this.Common(1024, null); } /// Initializes a new instance of the class with the specified parameters. /// The parameters to be passed to the cryptographic service provider (CSP). /// The CSP cannot be acquired. // Token: 0x0600300F RID: 12303 RVA: 0x0009D5F0 File Offset: 0x0009B7F0 public RSACryptoServiceProvider(CspParameters parameters) { this.Common(1024, parameters); } /// Initializes a new instance of the class with the specified key size. /// The size of the key to use in bits. /// The cryptographic service provider (CSP) cannot be acquired. // Token: 0x06003010 RID: 12304 RVA: 0x0009D60C File Offset: 0x0009B80C public RSACryptoServiceProvider(int dwKeySize) { this.Common(dwKeySize, null); } /// Initializes a new instance of the class with the specified key size and parameters. /// The size of the key to use in bits. /// The parameters to be passed to the cryptographic service provider (CSP). /// The CSP cannot be acquired.-or- The key cannot be created. // Token: 0x06003011 RID: 12305 RVA: 0x0009D624 File Offset: 0x0009B824 public RSACryptoServiceProvider(int dwKeySize, CspParameters parameters) { this.Common(dwKeySize, parameters); } // Token: 0x06003013 RID: 12307 RVA: 0x0009D640 File Offset: 0x0009B840 private void Common(int dwKeySize, CspParameters p) { this.LegalKeySizesValue = new KeySizes[1]; this.LegalKeySizesValue[0] = new KeySizes(384, 16384, 8); base.KeySize = dwKeySize; this.rsa = new RSAManaged(this.KeySize); this.rsa.KeyGenerated += this.OnKeyGenerated; this.persistKey = p != null; if (p == null) { p = new CspParameters(1); if (RSACryptoServiceProvider.useMachineKeyStore) { p.Flags |= CspProviderFlags.UseMachineKeyStore; } this.store = new KeyPairPersistence(p); } else { this.store = new KeyPairPersistence(p); this.store.Load(); if (this.store.KeyValue != null) { this.persisted = true; this.FromXmlString(this.store.KeyValue); } } } /// Gets or sets a value indicating whether the key should be persisted in the computer's key store instead of the user profile store. /// true if the key should be persisted in the computer key store; otherwise, false. // Token: 0x17000975 RID: 2421 // (get) Token: 0x06003014 RID: 12308 RVA: 0x0009D724 File Offset: 0x0009B924 // (set) Token: 0x06003015 RID: 12309 RVA: 0x0009D72C File Offset: 0x0009B92C public static bool UseMachineKeyStore { get { return RSACryptoServiceProvider.useMachineKeyStore; } set { RSACryptoServiceProvider.useMachineKeyStore = value; } } // Token: 0x06003016 RID: 12310 RVA: 0x0009D734 File Offset: 0x0009B934 ~RSACryptoServiceProvider() { this.Dispose(false); } /// Gets the name of the key exchange algorithm available with this implementation of . /// The name of the key exchange algorithm if it exists; otherwise, null. // Token: 0x17000976 RID: 2422 // (get) Token: 0x06003017 RID: 12311 RVA: 0x0009D770 File Offset: 0x0009B970 public override string KeyExchangeAlgorithm { get { return "RSA-PKCS1-KeyEx"; } } /// Gets the size of the current key. /// The size of the key in bits. // Token: 0x17000977 RID: 2423 // (get) Token: 0x06003018 RID: 12312 RVA: 0x0009D778 File Offset: 0x0009B978 public override int KeySize { get { if (this.rsa == null) { return this.KeySizeValue; } return this.rsa.KeySize; } } /// Gets or sets a value indicating whether the key should be persisted in the cryptographic service provider (CSP). /// true if the key should be persisted in the CSP; otherwise, false. // Token: 0x17000978 RID: 2424 // (get) Token: 0x06003019 RID: 12313 RVA: 0x0009D798 File Offset: 0x0009B998 // (set) Token: 0x0600301A RID: 12314 RVA: 0x0009D7A0 File Offset: 0x0009B9A0 public bool PersistKeyInCsp { get { return this.persistKey; } set { this.persistKey = value; if (this.persistKey) { this.OnKeyGenerated(this.rsa, null); } } } /// Gets a value that indicates whether the object contains only a public key. /// true if the object contains only a public key; otherwise, false. // Token: 0x17000979 RID: 2425 // (get) Token: 0x0600301B RID: 12315 RVA: 0x0009D7C4 File Offset: 0x0009B9C4 [ComVisible(false)] public bool PublicOnly { get { return this.rsa.PublicOnly; } } /// Gets the name of the signature algorithm available with this implementation of . /// The name of the signature algorithm. // Token: 0x1700097A RID: 2426 // (get) Token: 0x0600301C RID: 12316 RVA: 0x0009D7D4 File Offset: 0x0009B9D4 public override string SignatureAlgorithm { get { return "http://www.w3.org/2000/09/xmldsig#rsa-sha1"; } } /// Decrypts data with the algorithm. /// The decrypted data, which is the original plain text before encryption. /// The data to be decrypted. /// true to perform direct decryption using OAEP padding (only available on a computer running Microsoft Windows XP or later); otherwise, false to use PKCS#1 v1.5 padding. /// The cryptographic service provider (CSP) cannot be acquired.-or- The parameter is true and the length of the parameter is greater than .-or- The parameter is true and OAEP is not supported. /// /// is null. /// /// /// // Token: 0x0600301D RID: 12317 RVA: 0x0009D7DC File Offset: 0x0009B9DC public byte[] Decrypt(byte[] rgb, bool fOAEP) { if (this.m_disposed) { throw new ObjectDisposedException("rsa"); } AsymmetricKeyExchangeDeformatter asymmetricKeyExchangeDeformatter; if (fOAEP) { asymmetricKeyExchangeDeformatter = new RSAOAEPKeyExchangeDeformatter(this.rsa); } else { asymmetricKeyExchangeDeformatter = new RSAPKCS1KeyExchangeDeformatter(this.rsa); } return asymmetricKeyExchangeDeformatter.DecryptKeyExchange(rgb); } /// This method is not supported in the current version. /// The decrypted data, which is the original plain text before encryption. /// The data to be decrypted. /// This method is not supported in the current version. // Token: 0x0600301E RID: 12318 RVA: 0x0009D82C File Offset: 0x0009BA2C public override byte[] DecryptValue(byte[] rgb) { if (!this.rsa.IsCrtPossible) { throw new CryptographicException("Incomplete private key - missing CRT."); } return this.rsa.DecryptValue(rgb); } /// Encrypts data with the algorithm. /// The encrypted data. /// The data to be encrypted. /// true to perform direct encryption using Optimal Asymmetric Encryption Padding (OAEP), which is only available on a computer running Microsoft Windows XP or later; false to use PKCS#1 v1.5 padding. /// The cryptographic service provider (CSP) cannot be acquired.-or- The length of the parameter is greater than the maximum allowed length.-or- The parameter is true and OAEP padding is not supported. /// /// is null. /// /// /// // Token: 0x0600301F RID: 12319 RVA: 0x0009D858 File Offset: 0x0009BA58 public byte[] Encrypt(byte[] rgb, bool fOAEP) { AsymmetricKeyExchangeFormatter asymmetricKeyExchangeFormatter; if (fOAEP) { asymmetricKeyExchangeFormatter = new RSAOAEPKeyExchangeFormatter(this.rsa); } else { asymmetricKeyExchangeFormatter = new RSAPKCS1KeyExchangeFormatter(this.rsa); } return asymmetricKeyExchangeFormatter.CreateKeyExchange(rgb); } /// This method is not supported in the current version. /// The encrypted data. /// The data to be encrypted. /// This method is not supported in the current version. // Token: 0x06003020 RID: 12320 RVA: 0x0009D894 File Offset: 0x0009BA94 public override byte[] EncryptValue(byte[] rgb) { return this.rsa.EncryptValue(rgb); } /// Exports the . /// The parameters for . /// true to include private parameters; otherwise, false. /// The key cannot be exported. /// /// /// // Token: 0x06003021 RID: 12321 RVA: 0x0009D8A4 File Offset: 0x0009BAA4 public override RSAParameters ExportParameters(bool includePrivateParameters) { if (includePrivateParameters && !this.privateKeyExportable) { throw new CryptographicException("cannot export private key"); } return this.rsa.ExportParameters(includePrivateParameters); } /// Imports the specified . /// The parameters for . /// The cryptographic service provider (CSP) cannot be acquired.-or- The parameter has missing fields. /// /// /// // Token: 0x06003022 RID: 12322 RVA: 0x0009D8DC File Offset: 0x0009BADC public override void ImportParameters(RSAParameters parameters) { this.rsa.ImportParameters(parameters); } // Token: 0x06003023 RID: 12323 RVA: 0x0009D8EC File Offset: 0x0009BAEC private HashAlgorithm GetHash(object halg) { if (halg == null) { throw new ArgumentNullException("halg"); } HashAlgorithm hashAlgorithm; if (halg is string) { hashAlgorithm = HashAlgorithm.Create((string)halg); } else if (halg is HashAlgorithm) { hashAlgorithm = (HashAlgorithm)halg; } else { if (!(halg is Type)) { throw new ArgumentException("halg"); } hashAlgorithm = (HashAlgorithm)Activator.CreateInstance((Type)halg); } return hashAlgorithm; } /// Computes the hash value of the specified byte array using the specified hash algorithm, and signs the resulting hash value. /// The signature for the specified data. /// The input data for which to compute the hash. /// The hash algorithm to use to create the hash value. /// The parameter is null. /// The parameter is not a valid type. /// /// /// // Token: 0x06003024 RID: 12324 RVA: 0x0009D96C File Offset: 0x0009BB6C public byte[] SignData(byte[] buffer, object halg) { if (buffer == null) { throw new ArgumentNullException("buffer"); } return this.SignData(buffer, 0, buffer.Length, halg); } /// Computes the hash value of the specified input stream using the specified hash algorithm, and signs the resulting hash value. /// The signature for the specified data. /// The input data for which to compute the hash. /// The hash algorithm to use to create the hash value. /// The parameter is null. /// The parameter is not a valid type. /// /// /// // Token: 0x06003025 RID: 12325 RVA: 0x0009D98C File Offset: 0x0009BB8C public byte[] SignData(Stream inputStream, object halg) { HashAlgorithm hash = this.GetHash(halg); byte[] array = hash.ComputeHash(inputStream); return PKCS1.Sign_v15(this, hash, array); } /// Computes the hash value of a subset of the specified byte array using the specified hash algorithm, and signs the resulting hash value. /// The signature for the specified data. /// The input data for which to compute the hash. /// The offset into the array from which to begin using data. /// The number of bytes in the array to use as data. /// The hash algorithm to use to create the hash value. /// The parameter is null. /// The parameter is not a valid type. /// /// /// // Token: 0x06003026 RID: 12326 RVA: 0x0009D9B4 File Offset: 0x0009BBB4 public byte[] SignData(byte[] buffer, int offset, int count, object halg) { HashAlgorithm hash = this.GetHash(halg); byte[] array = hash.ComputeHash(buffer, offset, count); return PKCS1.Sign_v15(this, hash, array); } // Token: 0x06003027 RID: 12327 RVA: 0x0009D9DC File Offset: 0x0009BBDC private string GetHashNameFromOID(string oid) { if (oid != null) { if (RSACryptoServiceProvider.<>f__switch$map29 == null) { RSACryptoServiceProvider.<>f__switch$map29 = new Dictionary(2) { { "1.3.14.3.2.26", 0 }, { "1.2.840.113549.2.5", 1 } }; } int num; if (RSACryptoServiceProvider.<>f__switch$map29.TryGetValue(oid, out num)) { if (num == 0) { return "SHA1"; } if (num == 1) { return "MD5"; } } } throw new NotSupportedException(oid + " is an unsupported hash algorithm for RSA signing"); } /// Computes the signature for the specified hash value by encrypting it with the private key. /// The signature for the specified hash value. /// The hash value of the data to be signed. /// The hash algorithm identifier (OID) used to create the hash value of the data. /// The parameter is null. /// The cryptographic service provider (CSP) cannot be acquired.-or- There is no private key. /// /// /// // Token: 0x06003028 RID: 12328 RVA: 0x0009DA60 File Offset: 0x0009BC60 public byte[] SignHash(byte[] rgbHash, string str) { if (rgbHash == null) { throw new ArgumentNullException("rgbHash"); } string text = ((str != null) ? this.GetHashNameFromOID(str) : "SHA1"); HashAlgorithm hashAlgorithm = HashAlgorithm.Create(text); return PKCS1.Sign_v15(this, hashAlgorithm, rgbHash); } /// Verifies that a digital signature is valid by determining the hash value in the signature using the provided public key and comparing it to the hash value of the provided data. /// true if the signature is valid; otherwise, false. /// The data that was signed. /// The name of the hash algorithm used to create the hash value of the data. /// The signature data to use for verification. /// The parameter is null. /// The parameter is not a valid type. /// /// /// // Token: 0x06003029 RID: 12329 RVA: 0x0009DAA8 File Offset: 0x0009BCA8 public bool VerifyData(byte[] buffer, object halg, byte[] signature) { if (buffer == null) { throw new ArgumentNullException("buffer"); } if (signature == null) { throw new ArgumentNullException("signature"); } HashAlgorithm hash = this.GetHash(halg); byte[] array = hash.ComputeHash(buffer); return PKCS1.Verify_v15(this, hash, array, signature); } /// Verifies that a digital signature is valid by determining the hash value in the signature using the provided public key and comparing it to the provided hash value. /// true if the signature is valid; otherwise, false. /// The hash value of the signed data. /// The hash algorithm identifier (OID) used to create the hash value of the data. /// The signature data to use for verification. /// The parameter is null.-or- The parameter is null. /// The cryptographic service provider (CSP) cannot be acquired.-or- The signature cannot be verified. /// /// /// // Token: 0x0600302A RID: 12330 RVA: 0x0009DAF0 File Offset: 0x0009BCF0 public bool VerifyHash(byte[] rgbHash, string str, byte[] rgbSignature) { if (rgbHash == null) { throw new ArgumentNullException("rgbHash"); } if (rgbSignature == null) { throw new ArgumentNullException("rgbSignature"); } string text = ((str != null) ? this.GetHashNameFromOID(str) : "SHA1"); HashAlgorithm hashAlgorithm = HashAlgorithm.Create(text); return PKCS1.Verify_v15(this, hashAlgorithm, rgbHash, rgbSignature); } // Token: 0x0600302B RID: 12331 RVA: 0x0009DB48 File Offset: 0x0009BD48 protected override void Dispose(bool disposing) { if (!this.m_disposed) { if (this.persisted && !this.persistKey) { this.store.Remove(); } if (this.rsa != null) { this.rsa.Clear(); } this.m_disposed = true; } } // Token: 0x0600302C RID: 12332 RVA: 0x0009DBA0 File Offset: 0x0009BDA0 private void OnKeyGenerated(object sender, EventArgs e) { if (this.persistKey && !this.persisted) { this.store.KeyValue = this.ToXmlString(!this.rsa.PublicOnly); this.store.Save(); this.persisted = true; } } /// Gets a object that describes additional information about a cryptographic key pair. /// A object that describes additional information about a cryptographic key pair. // Token: 0x1700097B RID: 2427 // (get) Token: 0x0600302D RID: 12333 RVA: 0x0009DBF4 File Offset: 0x0009BDF4 [MonoTODO("Always return null")] [ComVisible(false)] public CspKeyContainerInfo CspKeyContainerInfo { get { return null; } } /// Exports a blob containing the key information associated with an object. /// A byte array containing the key information associated with an object. /// true to include the private key; otherwise, false. /// /// /// // Token: 0x0600302E RID: 12334 RVA: 0x0009DBF8 File Offset: 0x0009BDF8 [ComVisible(false)] public byte[] ExportCspBlob(bool includePrivateParameters) { byte[] array; if (includePrivateParameters) { array = CryptoConvert.ToCapiPrivateKeyBlob(this); } else { array = CryptoConvert.ToCapiPublicKeyBlob(this); } array[5] = 164; return array; } /// Imports a blob that represents RSA key information. /// A byte array that represents an RSA key blob. /// /// /// /// // Token: 0x0600302F RID: 12335 RVA: 0x0009DC2C File Offset: 0x0009BE2C [ComVisible(false)] public void ImportCspBlob(byte[] keyBlob) { if (keyBlob == null) { throw new ArgumentNullException("keyBlob"); } RSA rsa = CryptoConvert.FromCapiKeyBlob(keyBlob); if (rsa is RSACryptoServiceProvider) { RSAParameters rsaparameters = rsa.ExportParameters(!(rsa as RSACryptoServiceProvider).PublicOnly); this.ImportParameters(rsaparameters); } else { try { RSAParameters rsaparameters2 = rsa.ExportParameters(true); this.ImportParameters(rsaparameters2); } catch { RSAParameters rsaparameters3 = rsa.ExportParameters(false); this.ImportParameters(rsaparameters3); } } } // Token: 0x04001392 RID: 5010 private const int PROV_RSA_FULL = 1; // Token: 0x04001393 RID: 5011 private KeyPairPersistence store; // Token: 0x04001394 RID: 5012 private bool persistKey; // Token: 0x04001395 RID: 5013 private bool persisted; // Token: 0x04001396 RID: 5014 private bool privateKeyExportable = true; // Token: 0x04001397 RID: 5015 private bool m_disposed; // Token: 0x04001398 RID: 5016 private RSAManaged rsa; // Token: 0x04001399 RID: 5017 private static bool useMachineKeyStore; } }