X509AsymmetricSecurityKey.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // X509AsymmetricSecurityKey.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Xml;
  31. using System.IdentityModel.Policy;
  32. using System.Security.Cryptography;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.Security.Cryptography.Xml;
  35. namespace System.IdentityModel.Tokens
  36. {
  37. public class X509AsymmetricSecurityKey : AsymmetricSecurityKey
  38. {
  39. public X509AsymmetricSecurityKey (X509Certificate2 certificate)
  40. {
  41. if (certificate == null)
  42. throw new ArgumentNullException ("certificate");
  43. cert = certificate;
  44. }
  45. X509Certificate2 cert;
  46. // AsymmetricSecurityKey implementation
  47. public override AsymmetricAlgorithm GetAsymmetricAlgorithm (
  48. string algorithm, bool privateKey)
  49. {
  50. if (algorithm == null)
  51. throw new ArgumentNullException ("algorithm");
  52. if (privateKey && !cert.HasPrivateKey)
  53. throw new NotSupportedException ("The certificate does not contain a private key.");
  54. AsymmetricAlgorithm alg = privateKey ?
  55. cert.PrivateKey : cert.PublicKey.Key;
  56. switch (algorithm) {
  57. // case SignedXml.XmlDsigDSAUrl:
  58. // if (alg is DSA)
  59. // return alg;
  60. // throw new NotSupportedException (String.Format ("The certificate does not contain DSA private key while '{0}' requires it.", algorithm));
  61. case EncryptedXml.XmlEncRSA15Url:
  62. case EncryptedXml.XmlEncRSAOAEPUrl:
  63. case SignedXml.XmlDsigRSASHA1Url:
  64. case SecurityAlgorithms.RsaSha256Signature:
  65. if (alg is RSA)
  66. return alg;
  67. throw new NotSupportedException (String.Format ("The certificate does not contain RSA private key while '{0}' requires it.", algorithm));
  68. }
  69. throw new NotSupportedException (String.Format ("The asymmetric algorithm '{0}' is not supported.", algorithm));
  70. }
  71. public override HashAlgorithm GetHashAlgorithmForSignature (
  72. string algorithm)
  73. {
  74. if (algorithm == null)
  75. throw new ArgumentNullException ("algorithm");
  76. switch (algorithm) {
  77. //case SignedXml.XmlDsigDSAUrl: // it is documented as supported, but it isn't in reality and it wouldn't be possible.
  78. case SignedXml.XmlDsigRSASHA1Url:
  79. return new SHA1Managed ();
  80. case SecurityAlgorithms.RsaSha256Signature:
  81. return new SHA256Managed ();
  82. default:
  83. throw new NotSupportedException (String.Format ("'{0}' Hash algorithm is not supported in this security key.", algorithm));
  84. }
  85. }
  86. public override AsymmetricSignatureDeformatter GetSignatureDeformatter (string algorithm)
  87. {
  88. switch (algorithm) {
  89. //case SignedXml.XmlDsigDSAUrl:
  90. // DSA dsa = (cert.PublicKey.Key as DSA);
  91. // if (dsa == null) {
  92. // throw new NotSupportedException (String.Format ("The certificate does not contain DSA public key while '{0}' requires it.", algorithm));
  93. // }
  94. // else {
  95. // return new DSASignatureDeformatter(dsa);
  96. // }
  97. case SignedXml.XmlDsigRSASHA1Url:
  98. case SecurityAlgorithms.RsaSha256Signature:
  99. RSA rsa = (cert.PublicKey.Key as RSA);
  100. if (rsa == null) {
  101. throw new NotSupportedException (String.Format ("The certificate does not contain RSA public key while '{0}' requires it.", algorithm));
  102. }
  103. else {
  104. return new RSAPKCS1SignatureDeformatter (rsa);
  105. }
  106. default:
  107. throw new NotSupportedException (String.Format ("'{0}' Hash algorithm is not supported in this security key.", algorithm));
  108. }
  109. }
  110. public override AsymmetricSignatureFormatter GetSignatureFormatter (string algorithm)
  111. {
  112. switch (algorithm) {
  113. //case SignedXml.XmlDsigDSAUrl:
  114. // DSA dsa = (cert.PrivateKey as DSA);
  115. // if (dsa == null) {
  116. // throw new NotSupportedException (String.Format ("The certificate does not contain DSA private key while '{0}' requires it.", algorithm));
  117. // }
  118. // else {
  119. // return new DSASignatureFormatter(dsa);
  120. // }
  121. case SignedXml.XmlDsigRSASHA1Url:
  122. case SecurityAlgorithms.RsaSha256Signature:
  123. RSA rsa = (cert.PrivateKey as RSA);
  124. if (rsa == null) {
  125. throw new NotSupportedException (String.Format ("The certificate does not contain RSA private key while '{0}' requires it.", algorithm));
  126. }
  127. else {
  128. return new RSAPKCS1SignatureFormatter (rsa);
  129. }
  130. default:
  131. throw new NotSupportedException (String.Format ("'{0}' Hash algorithm is not supported in this security key.", algorithm));
  132. }
  133. }
  134. public override bool HasPrivateKey ()
  135. {
  136. return cert.HasPrivateKey;
  137. }
  138. // SecurityKey implementation
  139. public override int KeySize {
  140. get { return cert.PublicKey.Key.KeySize; }
  141. }
  142. public override byte [] DecryptKey (string algorithm, byte [] keyData)
  143. {
  144. if (algorithm == null)
  145. throw new ArgumentNullException ("algorithm");
  146. if (keyData == null)
  147. throw new ArgumentNullException ("keyData");
  148. if (!HasPrivateKey ())
  149. throw new NotSupportedException ("This X509 certificate does not contain private key.");
  150. if (cert.PrivateKey.KeyExchangeAlgorithm == null)
  151. throw new NotSupportedException ("The exchange algorithm of the X509 certificate private key is null");
  152. switch (algorithm) {
  153. case EncryptedXml.XmlEncRSA15Url:
  154. case EncryptedXml.XmlEncRSAOAEPUrl:
  155. break;
  156. default:
  157. throw new NotSupportedException (String.Format ("This X509 security key does not support specified algorithm '{0}'", algorithm));
  158. }
  159. bool useOAEP =
  160. algorithm == EncryptedXml.XmlEncRSAOAEPUrl;
  161. return EncryptedXml.DecryptKey (keyData, cert.PrivateKey as RSA, useOAEP);
  162. }
  163. public override byte [] EncryptKey (string algorithm, byte [] keyData)
  164. {
  165. if (algorithm == null)
  166. throw new ArgumentNullException ("algorithm");
  167. if (keyData == null)
  168. throw new ArgumentNullException ("keyData");
  169. switch (algorithm) {
  170. case EncryptedXml.XmlEncRSA15Url:
  171. case EncryptedXml.XmlEncRSAOAEPUrl:
  172. break;
  173. default:
  174. throw new NotSupportedException (String.Format ("This X509 security key does not support specified algorithm '{0}'", algorithm));
  175. }
  176. bool useOAEP =
  177. algorithm == EncryptedXml.XmlEncRSAOAEPUrl;
  178. return EncryptedXml.EncryptKey (keyData, cert.PublicKey.Key as RSA, useOAEP);
  179. }
  180. public override bool IsAsymmetricAlgorithm (string algorithm)
  181. {
  182. return GetAlgorithmSupportType (algorithm) == AlgorithmSupportType.Asymmetric;
  183. }
  184. public override bool IsSupportedAlgorithm (string algorithm)
  185. {
  186. switch (algorithm) {
  187. case SecurityAlgorithms.RsaV15KeyWrap:
  188. case SecurityAlgorithms.RsaOaepKeyWrap:
  189. case SecurityAlgorithms.RsaSha1Signature:
  190. case SecurityAlgorithms.RsaSha256Signature:
  191. return true;
  192. default:
  193. return false;
  194. }
  195. }
  196. public override bool IsSymmetricAlgorithm (string algorithm)
  197. {
  198. return GetAlgorithmSupportType (algorithm) == AlgorithmSupportType.Symmetric;
  199. }
  200. }
  201. }