EncryptedXml.cs 8.0 KB

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