KeyInfoX509Data.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. AddCertificate (new X509Certificate (rgbCert));
  49. }
  50. public KeyInfoX509Data (X509Certificate cert)
  51. {
  52. AddCertificate (cert);
  53. }
  54. #if NET_2_0
  55. public KeyInfoX509Data (X509Certificate cert, X509IncludeOption includeOption)
  56. {
  57. if (cert == null)
  58. throw new ArgumentNullException ("cert");
  59. switch (includeOption) {
  60. case X509IncludeOption.None:
  61. case X509IncludeOption.EndCertOnly:
  62. AddCertificate (cert);
  63. break;
  64. case X509IncludeOption.ExcludeRoot:
  65. AddCertificatesChainFrom (cert, false);
  66. break;
  67. case X509IncludeOption.WholeChain:
  68. AddCertificatesChainFrom (cert, true);
  69. break;
  70. }
  71. }
  72. // this gets complicated because we must:
  73. // 1. build the chain using a X509Certificate2 class;
  74. // 2. test for root using the Mono.Security.X509.X509Certificate class;
  75. // 3. add the certificates as X509Certificate instances;
  76. private void AddCertificatesChainFrom (X509Certificate cert, bool root)
  77. {
  78. X509Chain chain = new X509Chain ();
  79. chain.Build (new X509Certificate2 (cert));
  80. foreach (X509ChainElement ce in chain.ChainElements) {
  81. byte[] rawdata = ce.Certificate.RawData;
  82. if (!root) {
  83. // exclude root
  84. Mono.Security.X509.X509Certificate mx = new Mono.Security.X509.X509Certificate (rawdata);
  85. if (mx.IsSelfSigned)
  86. rawdata = null;
  87. }
  88. if (rawdata != null)
  89. AddCertificate (new X509Certificate (rawdata));
  90. }
  91. }
  92. #endif
  93. public ArrayList Certificates {
  94. get { return X509CertificateList; }
  95. }
  96. public byte[] CRL {
  97. get { return x509crl; }
  98. set { x509crl = value; }
  99. }
  100. public ArrayList IssuerSerials {
  101. get { return IssuerSerialList; }
  102. }
  103. public ArrayList SubjectKeyIds {
  104. get { return SubjectKeyIdList; }
  105. }
  106. public ArrayList SubjectNames {
  107. get { return SubjectNameList; }
  108. }
  109. public void AddCertificate (X509Certificate certificate)
  110. {
  111. #if NET_2_0
  112. if (certificate == null)
  113. throw new ArgumentNullException ("certificate");
  114. #endif
  115. if (X509CertificateList == null)
  116. X509CertificateList = new ArrayList ();
  117. X509CertificateList.Add (certificate);
  118. }
  119. public void AddIssuerSerial (string issuerName, string serialNumber)
  120. {
  121. #if NET_2_0
  122. if (issuerName == null)
  123. throw new ArgumentException ("issuerName");
  124. #endif
  125. if (IssuerSerialList == null)
  126. IssuerSerialList = new ArrayList ();
  127. X509IssuerSerial xis = new X509IssuerSerial (issuerName, serialNumber);
  128. IssuerSerialList.Add (xis);
  129. }
  130. public void AddSubjectKeyId (byte[] subjectKeyId)
  131. {
  132. if (SubjectKeyIdList == null)
  133. SubjectKeyIdList = new ArrayList ();
  134. SubjectKeyIdList.Add (subjectKeyId);
  135. }
  136. #if NET_2_0
  137. [ComVisible (false)]
  138. public void AddSubjectKeyId (string subjectKeyId)
  139. {
  140. if (SubjectKeyIdList == null)
  141. SubjectKeyIdList = new ArrayList ();
  142. byte[] id = null;
  143. if (subjectKeyId != null)
  144. id = Convert.FromBase64String (subjectKeyId);
  145. SubjectKeyIdList.Add (id);
  146. }
  147. #endif
  148. public void AddSubjectName (string subjectName)
  149. {
  150. if (SubjectNameList == null)
  151. SubjectNameList = new ArrayList ();
  152. SubjectNameList.Add (subjectName);
  153. }
  154. public override XmlElement GetXml ()
  155. {
  156. #if !NET_2_0
  157. // sanity check
  158. int count = 0;
  159. if (IssuerSerialList != null)
  160. count += IssuerSerialList.Count;
  161. if (SubjectKeyIdList != null)
  162. count += SubjectKeyIdList.Count;
  163. if (SubjectNameList != null)
  164. count += SubjectNameList.Count;
  165. if (X509CertificateList != null)
  166. count += X509CertificateList.Count;
  167. if ((x509crl == null) && (count == 0))
  168. throw new CryptographicException ("value");
  169. #endif
  170. XmlDocument document = new XmlDocument ();
  171. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.X509Data, XmlSignature.NamespaceURI);
  172. // FIXME: hack to match MS implementation
  173. xel.SetAttribute ("xmlns", XmlSignature.NamespaceURI);
  174. // <X509IssuerSerial>
  175. if ((IssuerSerialList != null) && (IssuerSerialList.Count > 0)) {
  176. foreach (X509IssuerSerial iser in IssuerSerialList) {
  177. XmlElement isl = document.CreateElement (XmlSignature.ElementNames.X509IssuerSerial, XmlSignature.NamespaceURI);
  178. XmlElement xin = document.CreateElement (XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
  179. xin.InnerText = iser.IssuerName;
  180. isl.AppendChild (xin);
  181. XmlElement xsn = document.CreateElement (XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
  182. xsn.InnerText = iser.SerialNumber;
  183. isl.AppendChild (xsn);
  184. xel.AppendChild (isl);
  185. }
  186. }
  187. // <X509SKI>
  188. if ((SubjectKeyIdList != null) && (SubjectKeyIdList.Count > 0)) {
  189. foreach (byte[] skid in SubjectKeyIdList) {
  190. XmlElement ski = document.CreateElement (XmlSignature.ElementNames.X509SKI, XmlSignature.NamespaceURI);
  191. ski.InnerText = Convert.ToBase64String (skid);
  192. xel.AppendChild (ski);
  193. }
  194. }
  195. // <X509SubjectName>
  196. if ((SubjectNameList != null) && (SubjectNameList.Count > 0)) {
  197. foreach (string subject in SubjectNameList) {
  198. XmlElement sn = document.CreateElement (XmlSignature.ElementNames.X509SubjectName, XmlSignature.NamespaceURI);
  199. sn.InnerText = subject;
  200. xel.AppendChild (sn);
  201. }
  202. }
  203. // <X509Certificate>
  204. if ((X509CertificateList != null) && (X509CertificateList.Count > 0)) {
  205. foreach (X509Certificate x509 in X509CertificateList) {
  206. XmlElement cert = document.CreateElement (XmlSignature.ElementNames.X509Certificate, XmlSignature.NamespaceURI);
  207. cert.InnerText = Convert.ToBase64String (x509.GetRawCertData ());
  208. xel.AppendChild (cert);
  209. }
  210. }
  211. // only one <X509CRL>
  212. if (x509crl != null) {
  213. XmlElement crl = document.CreateElement (XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
  214. crl.InnerText = Convert.ToBase64String (x509crl);
  215. xel.AppendChild (crl);
  216. }
  217. return xel;
  218. }
  219. public override void LoadXml (XmlElement element)
  220. {
  221. if (element == null)
  222. throw new ArgumentNullException ("element");
  223. if (IssuerSerialList != null)
  224. IssuerSerialList.Clear ();
  225. if (SubjectKeyIdList != null)
  226. SubjectKeyIdList.Clear ();
  227. if (SubjectNameList != null)
  228. SubjectNameList.Clear ();
  229. if (X509CertificateList != null)
  230. X509CertificateList.Clear ();
  231. x509crl = null;
  232. if ((element.LocalName != XmlSignature.ElementNames.X509Data) || (element.NamespaceURI != XmlSignature.NamespaceURI))
  233. throw new CryptographicException ("element");
  234. XmlElement [] xnl = null;
  235. // <X509IssuerSerial>
  236. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509IssuerSerial);
  237. if (xnl != null) {
  238. for (int i=0; i < xnl.Length; i++) {
  239. XmlElement xel = (XmlElement) xnl[i];
  240. XmlElement issuer = XmlSignature.GetChildElement (xel, XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
  241. XmlElement serial = XmlSignature.GetChildElement (xel, XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
  242. AddIssuerSerial (issuer.InnerText, serial.InnerText);
  243. }
  244. }
  245. // <X509SKI>
  246. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509SKI);
  247. if (xnl != null) {
  248. for (int i=0; i < xnl.Length; i++) {
  249. byte[] skid = Convert.FromBase64String (xnl[i].InnerXml);
  250. AddSubjectKeyId (skid);
  251. }
  252. }
  253. // <X509SubjectName>
  254. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509SubjectName);
  255. if (xnl != null) {
  256. for (int i=0; i < xnl.Length; i++) {
  257. AddSubjectName (xnl[i].InnerXml);
  258. }
  259. }
  260. // <X509Certificate>
  261. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509Certificate);
  262. if (xnl != null) {
  263. for (int i=0; i < xnl.Length; i++) {
  264. byte[] cert = Convert.FromBase64String (xnl[i].InnerXml);
  265. AddCertificate (new X509Certificate (cert));
  266. }
  267. }
  268. // only one <X509CRL>
  269. XmlElement x509el = XmlSignature.GetChildElement (element, XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
  270. if (x509el != null) {
  271. x509crl = Convert.FromBase64String (x509el.InnerXml);
  272. }
  273. }
  274. }
  275. }