EncryptedXml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // EncryptedXml.cs - EncryptedXml implementation for XML Encryption
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2004
  8. #if NET_1_2
  9. using System.Collections;
  10. using System.IO;
  11. using System.Security.Cryptography;
  12. using System.Security.Policy;
  13. using System.Text;
  14. using System.Xml;
  15. namespace System.Security.Cryptography.Xml {
  16. public class EncryptedXml {
  17. #region Fields
  18. public const string XmlEncAES128KeyWrapUrl = XmlEncNamespaceUrl + "kw-aes128";
  19. public const string XmlEncAES128Url = XmlEncNamespaceUrl + "aes128-cbc";
  20. public const string XmlEncAES192KeyWrapUrl = XmlEncNamespaceUrl + "kw-aes192";
  21. public const string XmlEncAES192Url = XmlEncNamespaceUrl + "aes192-cbc";
  22. public const string XmlEncAES256KeyWrapUrl = XmlEncNamespaceUrl + "kw-aes256";
  23. public const string XmlEncAES256Url = XmlEncNamespaceUrl + "aes256-cbc";
  24. public const string XmlEncDESUrl = XmlEncNamespaceUrl + "des-cbc";
  25. public const string XmlEncElementContentUrl = XmlEncNamespaceUrl + "ElementContent";
  26. public const string XmlEncElementUrl = XmlEncNamespaceUrl + "element";
  27. public const string XmlEncEncryptedKeyUrl = XmlEncNamespaceUrl + "EncryptedKey";
  28. public const string XmlEncNamespaceUrl = "http://www.w3.org/2001/04/xmlenc#";
  29. public const string XmlEncRSA1_5Url = XmlEncNamespaceUrl + "rsa-1_5";
  30. public const string XmlEncRSAOAEPUrl = XmlEncNamespaceUrl + "rsa-oaep-mgf1p";
  31. public const string XmlEncSHA256Url = XmlEncNamespaceUrl + "sha256";
  32. public const string XmlEncSHA512Url = XmlEncNamespaceUrl + "sha512";
  33. public const string XmlEncTripleDESKeyWrapUrl = XmlEncNamespaceUrl + "kw-tripledes";
  34. public const string XmlEncTripleDESUrl = XmlEncNamespaceUrl + "tripledes-cbc";
  35. Evidence documentEvidence;
  36. Encoding encoding = Encoding.UTF8;
  37. Hashtable keyNameMapping = new Hashtable ();
  38. CipherMode mode = CipherMode.CBC;
  39. PaddingMode padding = PaddingMode.ISO10126;
  40. string recipient;
  41. XmlResolver resolver;
  42. #endregion // Fields
  43. #region Constructors
  44. [MonoTODO]
  45. public EncryptedXml ()
  46. {
  47. }
  48. [MonoTODO]
  49. public EncryptedXml (XmlDocument document)
  50. {
  51. }
  52. [MonoTODO]
  53. public EncryptedXml (XmlDocument document, Evidence evidence)
  54. {
  55. DocumentEvidence = evidence;
  56. }
  57. #endregion // Constructors
  58. #region Properties
  59. public Evidence DocumentEvidence {
  60. get { return documentEvidence; }
  61. set { documentEvidence = value; }
  62. }
  63. public Encoding Encoding {
  64. get { return encoding; }
  65. set { encoding = value; }
  66. }
  67. public CipherMode Mode {
  68. get { return mode; }
  69. set { mode = value; }
  70. }
  71. public PaddingMode Padding {
  72. get { return padding; }
  73. set { padding = value; }
  74. }
  75. public string Recipient {
  76. get { return recipient; }
  77. set { recipient = value; }
  78. }
  79. public XmlResolver Resolver {
  80. get { return resolver; }
  81. set { resolver = value; }
  82. }
  83. #endregion // Properties
  84. #region Methods
  85. public void AddKeyNameMapping (string keyName, object keyObject)
  86. {
  87. keyNameMapping [keyName] = keyObject;
  88. }
  89. public void ClearKeyNameMappings ()
  90. {
  91. keyNameMapping.Clear ();
  92. }
  93. public byte[] DecryptData (EncryptedData encryptedData, SymmetricAlgorithm symAlg)
  94. {
  95. return Transform (encryptedData.CipherData.CipherValue, symAlg.CreateDecryptor ());
  96. }
  97. [MonoTODO]
  98. public void DecryptDocument ()
  99. {
  100. throw new NotImplementedException ();
  101. }
  102. [MonoTODO]
  103. public virtual byte[] DecryptEncryptedKey (EncryptedKey encryptedKey)
  104. {
  105. throw new NotImplementedException ();
  106. }
  107. [MonoTODO]
  108. public static byte[] DecryptKey (byte[] keyData, SymmetricAlgorithm symAlg)
  109. {
  110. if (symAlg is TripleDES)
  111. return SymmetricKeyWrap.TripleDESKeyWrapDecrypt (symAlg.Key, keyData);
  112. if (symAlg is Rijndael)
  113. return SymmetricKeyWrap.TripleDESKeyWrapDecrypt (symAlg.Key, keyData);
  114. throw new CryptographicException ("The specified cryptographic transform is not supported.");
  115. }
  116. [MonoTODO]
  117. public static byte[] DecryptKey (byte[] keyData, RSA rsa, bool fOAEP)
  118. {
  119. throw new NotImplementedException ();
  120. }
  121. public byte[] EncryptData (XmlElement inputElement, SymmetricAlgorithm symAlg, bool content)
  122. {
  123. if (content)
  124. return Transform (Encoding.GetBytes (inputElement.InnerXml), symAlg.CreateEncryptor ());
  125. else
  126. return Transform (Encoding.GetBytes (inputElement.OuterXml), symAlg.CreateEncryptor ());
  127. }
  128. [MonoTODO ("Do we need to support more algorithms?")]
  129. public static byte[] EncryptKey (byte[] keyData, SymmetricAlgorithm symAlg)
  130. {
  131. if (symAlg is TripleDES)
  132. return SymmetricKeyWrap.TripleDESKeyWrapEncrypt (symAlg.Key, keyData);
  133. if (symAlg is Rijndael)
  134. return SymmetricKeyWrap.AESKeyWrapEncrypt (symAlg.Key, keyData);
  135. throw new CryptographicException ("The specified cryptographic transform is not supported.");
  136. }
  137. [MonoTODO ("Not sure what this is for.")]
  138. public static byte[] EncryptKey (byte[] keyData, RSA rsa, bool fOAEP)
  139. {
  140. throw new NotImplementedException ();
  141. }
  142. private static SymmetricAlgorithm GetAlgorithm (string symAlgUri)
  143. {
  144. SymmetricAlgorithm symAlg = null;
  145. switch (symAlgUri) {
  146. case XmlEncAES128Url:
  147. symAlg = SymmetricAlgorithm.Create ("Rijndael");
  148. symAlg.KeySize = 128;
  149. break;
  150. case XmlEncAES192Url:
  151. symAlg = SymmetricAlgorithm.Create ("Rijndael");
  152. symAlg.KeySize = 192;
  153. break;
  154. case XmlEncAES256Url:
  155. symAlg = SymmetricAlgorithm.Create ("Rijndael");
  156. symAlg.KeySize = 256;
  157. break;
  158. case XmlEncDESUrl:
  159. symAlg = SymmetricAlgorithm.Create ("DES");
  160. break;
  161. case XmlEncTripleDESUrl:
  162. symAlg = SymmetricAlgorithm.Create ("TripleDES");
  163. break;
  164. default:
  165. throw new ArgumentException ("symAlgUri");
  166. }
  167. return symAlg;
  168. }
  169. [MonoTODO]
  170. public virtual byte[] GetDecryptionIV (EncryptedData encryptedData, string symAlgUri)
  171. {
  172. SymmetricAlgorithm symAlg = GetAlgorithm (symAlgUri);
  173. throw new NotImplementedException ();
  174. }
  175. [MonoTODO]
  176. public virtual SymmetricAlgorithm GetDecryptionKey (EncryptedData encryptedData, string symAlgUri)
  177. {
  178. SymmetricAlgorithm symAlg = GetAlgorithm (symAlgUri);
  179. throw new NotImplementedException ();
  180. }
  181. public virtual XmlElement GetIdElement (XmlDocument document, string idValue)
  182. {
  183. // this works only if there's a DTD or XSD available to define the ID
  184. XmlElement xel = document.GetElementById (idValue);
  185. if (xel == null) {
  186. // search an "undefined" ID
  187. xel = (XmlElement) document.SelectSingleNode ("//*[@Id='" + idValue + "']");
  188. }
  189. return xel;
  190. }
  191. [MonoTODO]
  192. public static void ReplaceElement (XmlElement inputElement, EncryptedData encryptedData, bool content)
  193. {
  194. throw new NotImplementedException ();
  195. }
  196. private byte[] Transform (byte[] data, ICryptoTransform transform)
  197. {
  198. MemoryStream output = new MemoryStream ();
  199. CryptoStream crypto = new CryptoStream (output, transform, CryptoStreamMode.Write);
  200. crypto.Write (data, 0, data.Length);
  201. crypto.Close ();
  202. output.Close ();
  203. return output.ToArray ();
  204. }
  205. #endregion // Methods
  206. }
  207. }
  208. #endif