1 // ==++== 2 // 3 // Copyright (c) Microsoft Corporation. All rights reserved. 4 // 5 // ==--== 6 // <OWNER>Microsoft</OWNER> 7 // 8 9 // 10 // SignatureDescription.cs 11 // 12 13 namespace System.Security.Cryptography { 14 using System.Security.Util; 15 using System.Diagnostics.Contracts; 16 17 [System.Runtime.InteropServices.ComVisible(true)] 18 public class SignatureDescription { 19 private String _strKey; 20 private String _strDigest; 21 private String _strFormatter; 22 private String _strDeformatter; 23 24 // 25 // public constructors 26 // 27 SignatureDescription()28 public SignatureDescription() { 29 } 30 SignatureDescription(SecurityElement el)31 public SignatureDescription(SecurityElement el) { 32 if (el == null) throw new ArgumentNullException("el"); 33 Contract.EndContractBlock(); 34 _strKey = el.SearchForTextOfTag("Key"); 35 _strDigest = el.SearchForTextOfTag("Digest"); 36 _strFormatter = el.SearchForTextOfTag("Formatter"); 37 _strDeformatter = el.SearchForTextOfTag("Deformatter"); 38 } 39 40 // 41 // property methods 42 // 43 44 public String KeyAlgorithm { 45 get { return _strKey; } 46 set { _strKey = value; } 47 } 48 public String DigestAlgorithm { 49 get { return _strDigest; } 50 set { _strDigest = value; } 51 } 52 public String FormatterAlgorithm { 53 get { return _strFormatter; } 54 set { _strFormatter = value; } 55 } 56 public String DeformatterAlgorithm { 57 get {return _strDeformatter; } 58 set {_strDeformatter = value; } 59 } 60 61 // 62 // public methods 63 // 64 CreateDeformatter(AsymmetricAlgorithm key)65 public virtual AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key) { 66 AsymmetricSignatureDeformatter item; 67 68 item = (AsymmetricSignatureDeformatter) CryptoConfig.CreateFromName(_strDeformatter); 69 item.SetKey(key); 70 return item; 71 } 72 CreateFormatter(AsymmetricAlgorithm key)73 public virtual AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key) { 74 AsymmetricSignatureFormatter item; 75 76 item = (AsymmetricSignatureFormatter) CryptoConfig.CreateFromName(_strFormatter); 77 item.SetKey(key); 78 return item; 79 } 80 CreateDigest()81 public virtual HashAlgorithm CreateDigest() { 82 return (HashAlgorithm) CryptoConfig.CreateFromName(_strDigest); 83 } 84 } 85 86 internal abstract class RSAPKCS1SignatureDescription : SignatureDescription { RSAPKCS1SignatureDescription(string hashAlgorithm, string digestAlgorithm)87 protected RSAPKCS1SignatureDescription(string hashAlgorithm, string digestAlgorithm) { 88 KeyAlgorithm = "System.Security.Cryptography.RSA"; 89 DigestAlgorithm = digestAlgorithm; 90 FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter"; 91 DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter"; 92 _hashAlgorithm = hashAlgorithm; 93 } 94 CreateDeformatter(AsymmetricAlgorithm key)95 public sealed override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key) { 96 AsymmetricSignatureDeformatter item = base.CreateDeformatter(key); 97 item.SetHashAlgorithm(_hashAlgorithm); 98 return item; 99 } 100 CreateFormatter(AsymmetricAlgorithm key)101 public sealed override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key) { 102 AsymmetricSignatureFormatter item = base.CreateFormatter(key); 103 item.SetHashAlgorithm(_hashAlgorithm); 104 return item; 105 } 106 107 private string _hashAlgorithm; 108 } 109 110 internal class RSAPKCS1SHA1SignatureDescription : RSAPKCS1SignatureDescription { RSAPKCS1SHA1SignatureDescription()111 public RSAPKCS1SHA1SignatureDescription() 112 : base("SHA1", "System.Security.Cryptography.SHA1Cng") { 113 } 114 } 115 116 internal class RSAPKCS1SHA256SignatureDescription : RSAPKCS1SignatureDescription { RSAPKCS1SHA256SignatureDescription()117 public RSAPKCS1SHA256SignatureDescription() 118 : base("SHA256", "System.Security.Cryptography.SHA256Cng") { 119 } 120 } 121 122 internal class RSAPKCS1SHA384SignatureDescription : RSAPKCS1SignatureDescription { RSAPKCS1SHA384SignatureDescription()123 public RSAPKCS1SHA384SignatureDescription() 124 : base("SHA384", "System.Security.Cryptography.SHA384Cng") { 125 } 126 } 127 128 internal class RSAPKCS1SHA512SignatureDescription : RSAPKCS1SignatureDescription { RSAPKCS1SHA512SignatureDescription()129 public RSAPKCS1SHA512SignatureDescription() 130 : base("SHA512", "System.Security.Cryptography.SHA512Cng") { 131 } 132 } 133 134 internal class DSASignatureDescription : SignatureDescription { DSASignatureDescription()135 public DSASignatureDescription() { 136 KeyAlgorithm = "System.Security.Cryptography.DSACryptoServiceProvider"; 137 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider"; 138 FormatterAlgorithm = "System.Security.Cryptography.DSASignatureFormatter"; 139 DeformatterAlgorithm = "System.Security.Cryptography.DSASignatureDeformatter"; 140 } 141 } 142 } 143