KeyInfoX509Data.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // KeyInfoX509Data.cs - KeyInfoX509Data implementation for XML Signature
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  6. // Atsushi Enomoto ([email protected])
  7. // Tim Coleman ([email protected])
  8. //
  9. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  10. // Copyright (C) Tim Coleman, 2004
  11. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Collections;
  33. using System.Runtime.InteropServices;
  34. using System.Security.Cryptography.X509Certificates;
  35. using System.Xml;
  36. namespace System.Security.Cryptography.Xml {
  37. public class KeyInfoX509Data : KeyInfoClause {
  38. private byte[] x509crl;
  39. private ArrayList IssuerSerialList;
  40. private ArrayList SubjectKeyIdList;
  41. private ArrayList SubjectNameList;
  42. private ArrayList X509CertificateList;
  43. public KeyInfoX509Data ()
  44. {
  45. }
  46. public KeyInfoX509Data (byte[] rgbCert)
  47. {
  48. #if NET_2_0
  49. if (rgbCert == null)
  50. throw new ArgumentException ("rgbCert");
  51. #endif
  52. AddCertificate (new X509Certificate (rgbCert));
  53. }
  54. public KeyInfoX509Data (X509Certificate cert)
  55. {
  56. AddCertificate (cert);
  57. }
  58. #if NET_2_0
  59. public KeyInfoX509Data (X509Certificate cert, X509IncludeOption includeOption)
  60. {
  61. if (cert == null)
  62. throw new ArgumentNullException ("cert");
  63. switch (includeOption) {
  64. case X509IncludeOption.None:
  65. case X509IncludeOption.EndCertOnly:
  66. AddCertificate (cert);
  67. break;
  68. case X509IncludeOption.ExcludeRoot:
  69. AddCertificatesChainFrom (cert, false);
  70. break;
  71. case X509IncludeOption.WholeChain:
  72. AddCertificatesChainFrom (cert, true);
  73. break;
  74. }
  75. }
  76. // this gets complicated because we must:
  77. // 1. build the chain using a X509Certificate2 class;
  78. // 2. test for root using the Mono.Security.X509.X509Certificate class;
  79. // 3. add the certificates as X509Certificate instances;
  80. private void AddCertificatesChainFrom (X509Certificate cert, bool root)
  81. {
  82. X509Chain chain = new X509Chain ();
  83. chain.Build (new X509Certificate2 (cert));
  84. foreach (X509ChainElement ce in chain.ChainElements) {
  85. byte[] rawdata = ce.Certificate.RawData;
  86. if (!root) {
  87. // exclude root
  88. Mono.Security.X509.X509Certificate mx = new Mono.Security.X509.X509Certificate (rawdata);
  89. if (mx.IsSelfSigned)
  90. rawdata = null;
  91. }
  92. if (rawdata != null)
  93. AddCertificate (new X509Certificate (rawdata));
  94. }
  95. }
  96. #endif
  97. public ArrayList Certificates {
  98. get { return X509CertificateList; }
  99. }
  100. public byte[] CRL {
  101. get { return x509crl; }
  102. set { x509crl = value; }
  103. }
  104. public ArrayList IssuerSerials {
  105. get { return IssuerSerialList; }
  106. }
  107. public ArrayList SubjectKeyIds {
  108. get { return SubjectKeyIdList; }
  109. }
  110. public ArrayList SubjectNames {
  111. get { return SubjectNameList; }
  112. }
  113. public void AddCertificate (X509Certificate certificate)
  114. {
  115. #if NET_2_0
  116. if (certificate == null)
  117. throw new ArgumentNullException ("certificate");
  118. #endif
  119. if (X509CertificateList == null)
  120. X509CertificateList = new ArrayList ();
  121. X509CertificateList.Add (certificate);
  122. }
  123. public void AddIssuerSerial (string issuerName, string serialNumber)
  124. {
  125. #if NET_2_0
  126. if (issuerName == null)
  127. throw new ArgumentException ("issuerName");
  128. #endif
  129. if (IssuerSerialList == null)
  130. IssuerSerialList = new ArrayList ();
  131. X509IssuerSerial xis = new X509IssuerSerial (issuerName, serialNumber);
  132. IssuerSerialList.Add (xis);
  133. }
  134. public void AddSubjectKeyId (byte[] subjectKeyId)
  135. {
  136. if (SubjectKeyIdList == null)
  137. SubjectKeyIdList = new ArrayList ();
  138. SubjectKeyIdList.Add (subjectKeyId);
  139. }
  140. #if NET_2_0
  141. [ComVisible (false)]
  142. public void AddSubjectKeyId (string subjectKeyId)
  143. {
  144. if (SubjectKeyIdList == null)
  145. SubjectKeyIdList = new ArrayList ();
  146. byte[] id = null;
  147. if (subjectKeyId != null)
  148. id = Convert.FromBase64String (subjectKeyId);
  149. SubjectKeyIdList.Add (id);
  150. }
  151. #endif
  152. public void AddSubjectName (string subjectName)
  153. {
  154. if (SubjectNameList == null)
  155. SubjectNameList = new ArrayList ();
  156. SubjectNameList.Add (subjectName);
  157. }
  158. public override XmlElement GetXml ()
  159. {
  160. #if !NET_2_0
  161. // sanity check
  162. int count = 0;
  163. if (IssuerSerialList != null)
  164. count += IssuerSerialList.Count;
  165. if (SubjectKeyIdList != null)
  166. count += SubjectKeyIdList.Count;
  167. if (SubjectNameList != null)
  168. count += SubjectNameList.Count;
  169. if (X509CertificateList != null)
  170. count += X509CertificateList.Count;
  171. if ((x509crl == null) && (count == 0))
  172. throw new CryptographicException ("value");
  173. #endif
  174. XmlDocument document = new XmlDocument ();
  175. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.X509Data, XmlSignature.NamespaceURI);
  176. // FIXME: hack to match MS implementation
  177. xel.SetAttribute ("xmlns", XmlSignature.NamespaceURI);
  178. // <X509IssuerSerial>
  179. if ((IssuerSerialList != null) && (IssuerSerialList.Count > 0)) {
  180. foreach (X509IssuerSerial iser in IssuerSerialList) {
  181. XmlElement isl = document.CreateElement (XmlSignature.ElementNames.X509IssuerSerial, XmlSignature.NamespaceURI);
  182. XmlElement xin = document.CreateElement (XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
  183. xin.InnerText = iser.IssuerName;
  184. isl.AppendChild (xin);
  185. XmlElement xsn = document.CreateElement (XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
  186. xsn.InnerText = iser.SerialNumber;
  187. isl.AppendChild (xsn);
  188. xel.AppendChild (isl);
  189. }
  190. }
  191. // <X509SKI>
  192. if ((SubjectKeyIdList != null) && (SubjectKeyIdList.Count > 0)) {
  193. foreach (byte[] skid in SubjectKeyIdList) {
  194. XmlElement ski = document.CreateElement (XmlSignature.ElementNames.X509SKI, XmlSignature.NamespaceURI);
  195. ski.InnerText = Convert.ToBase64String (skid);
  196. xel.AppendChild (ski);
  197. }
  198. }
  199. // <X509SubjectName>
  200. if ((SubjectNameList != null) && (SubjectNameList.Count > 0)) {
  201. foreach (string subject in SubjectNameList) {
  202. XmlElement sn = document.CreateElement (XmlSignature.ElementNames.X509SubjectName, XmlSignature.NamespaceURI);
  203. sn.InnerText = subject;
  204. xel.AppendChild (sn);
  205. }
  206. }
  207. // <X509Certificate>
  208. if ((X509CertificateList != null) && (X509CertificateList.Count > 0)) {
  209. foreach (X509Certificate x509 in X509CertificateList) {
  210. XmlElement cert = document.CreateElement (XmlSignature.ElementNames.X509Certificate, XmlSignature.NamespaceURI);
  211. cert.InnerText = Convert.ToBase64String (x509.GetRawCertData ());
  212. xel.AppendChild (cert);
  213. }
  214. }
  215. // only one <X509CRL>
  216. if (x509crl != null) {
  217. XmlElement crl = document.CreateElement (XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
  218. crl.InnerText = Convert.ToBase64String (x509crl);
  219. xel.AppendChild (crl);
  220. }
  221. return xel;
  222. }
  223. public override void LoadXml (XmlElement element)
  224. {
  225. if (element == null)
  226. throw new ArgumentNullException ("element");
  227. if (IssuerSerialList != null)
  228. IssuerSerialList.Clear ();
  229. if (SubjectKeyIdList != null)
  230. SubjectKeyIdList.Clear ();
  231. if (SubjectNameList != null)
  232. SubjectNameList.Clear ();
  233. if (X509CertificateList != null)
  234. X509CertificateList.Clear ();
  235. x509crl = null;
  236. if ((element.LocalName != XmlSignature.ElementNames.X509Data) || (element.NamespaceURI != XmlSignature.NamespaceURI))
  237. throw new CryptographicException ("element");
  238. XmlElement [] xnl = null;
  239. // <X509IssuerSerial>
  240. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509IssuerSerial);
  241. if (xnl != null) {
  242. for (int i=0; i < xnl.Length; i++) {
  243. XmlElement xel = (XmlElement) xnl[i];
  244. XmlElement issuer = XmlSignature.GetChildElement (xel, XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
  245. XmlElement serial = XmlSignature.GetChildElement (xel, XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
  246. AddIssuerSerial (issuer.InnerText, serial.InnerText);
  247. }
  248. }
  249. // <X509SKI>
  250. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509SKI);
  251. if (xnl != null) {
  252. for (int i=0; i < xnl.Length; i++) {
  253. byte[] skid = Convert.FromBase64String (xnl[i].InnerXml);
  254. AddSubjectKeyId (skid);
  255. }
  256. }
  257. // <X509SubjectName>
  258. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509SubjectName);
  259. if (xnl != null) {
  260. for (int i=0; i < xnl.Length; i++) {
  261. AddSubjectName (xnl[i].InnerXml);
  262. }
  263. }
  264. // <X509Certificate>
  265. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509Certificate);
  266. if (xnl != null) {
  267. for (int i=0; i < xnl.Length; i++) {
  268. byte[] cert = Convert.FromBase64String (xnl[i].InnerXml);
  269. AddCertificate (new X509Certificate (cert));
  270. }
  271. }
  272. // only one <X509CRL>
  273. XmlElement x509el = XmlSignature.GetChildElement (element, XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
  274. if (x509el != null) {
  275. x509crl = Convert.FromBase64String (x509el.InnerXml);
  276. }
  277. }
  278. }
  279. }