KeyInfoX509Data.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 SECURITY_DEP
  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 (certificate == null)
  112. throw new ArgumentNullException ("certificate");
  113. if (X509CertificateList == null)
  114. X509CertificateList = new ArrayList ();
  115. X509CertificateList.Add (certificate);
  116. }
  117. public void AddIssuerSerial (string issuerName, string serialNumber)
  118. {
  119. if (issuerName == null)
  120. throw new ArgumentException ("issuerName");
  121. if (IssuerSerialList == null)
  122. IssuerSerialList = new ArrayList ();
  123. X509IssuerSerial xis = new X509IssuerSerial (issuerName, serialNumber);
  124. IssuerSerialList.Add (xis);
  125. }
  126. public void AddSubjectKeyId (byte[] subjectKeyId)
  127. {
  128. if (SubjectKeyIdList == null)
  129. SubjectKeyIdList = new ArrayList ();
  130. SubjectKeyIdList.Add (subjectKeyId);
  131. }
  132. [ComVisible (false)]
  133. public void AddSubjectKeyId (string subjectKeyId)
  134. {
  135. if (SubjectKeyIdList == null)
  136. SubjectKeyIdList = new ArrayList ();
  137. byte[] id = null;
  138. if (subjectKeyId != null)
  139. id = Convert.FromBase64String (subjectKeyId);
  140. SubjectKeyIdList.Add (id);
  141. }
  142. public void AddSubjectName (string subjectName)
  143. {
  144. if (SubjectNameList == null)
  145. SubjectNameList = new ArrayList ();
  146. SubjectNameList.Add (subjectName);
  147. }
  148. public override XmlElement GetXml ()
  149. {
  150. XmlDocument document = new XmlDocument ();
  151. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.X509Data, XmlSignature.NamespaceURI);
  152. // FIXME: hack to match MS implementation
  153. xel.SetAttribute ("xmlns", XmlSignature.NamespaceURI);
  154. // <X509IssuerSerial>
  155. if ((IssuerSerialList != null) && (IssuerSerialList.Count > 0)) {
  156. foreach (X509IssuerSerial iser in IssuerSerialList) {
  157. XmlElement isl = document.CreateElement (XmlSignature.ElementNames.X509IssuerSerial, XmlSignature.NamespaceURI);
  158. XmlElement xin = document.CreateElement (XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
  159. xin.InnerText = iser.IssuerName;
  160. isl.AppendChild (xin);
  161. XmlElement xsn = document.CreateElement (XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
  162. xsn.InnerText = iser.SerialNumber;
  163. isl.AppendChild (xsn);
  164. xel.AppendChild (isl);
  165. }
  166. }
  167. // <X509SKI>
  168. if ((SubjectKeyIdList != null) && (SubjectKeyIdList.Count > 0)) {
  169. foreach (byte[] skid in SubjectKeyIdList) {
  170. XmlElement ski = document.CreateElement (XmlSignature.ElementNames.X509SKI, XmlSignature.NamespaceURI);
  171. ski.InnerText = Convert.ToBase64String (skid);
  172. xel.AppendChild (ski);
  173. }
  174. }
  175. // <X509SubjectName>
  176. if ((SubjectNameList != null) && (SubjectNameList.Count > 0)) {
  177. foreach (string subject in SubjectNameList) {
  178. XmlElement sn = document.CreateElement (XmlSignature.ElementNames.X509SubjectName, XmlSignature.NamespaceURI);
  179. sn.InnerText = subject;
  180. xel.AppendChild (sn);
  181. }
  182. }
  183. // <X509Certificate>
  184. if ((X509CertificateList != null) && (X509CertificateList.Count > 0)) {
  185. foreach (X509Certificate x509 in X509CertificateList) {
  186. XmlElement cert = document.CreateElement (XmlSignature.ElementNames.X509Certificate, XmlSignature.NamespaceURI);
  187. cert.InnerText = Convert.ToBase64String (x509.GetRawCertData ());
  188. xel.AppendChild (cert);
  189. }
  190. }
  191. // only one <X509CRL>
  192. if (x509crl != null) {
  193. XmlElement crl = document.CreateElement (XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
  194. crl.InnerText = Convert.ToBase64String (x509crl);
  195. xel.AppendChild (crl);
  196. }
  197. return xel;
  198. }
  199. public override void LoadXml (XmlElement element)
  200. {
  201. if (element == null)
  202. throw new ArgumentNullException ("element");
  203. if (IssuerSerialList != null)
  204. IssuerSerialList.Clear ();
  205. if (SubjectKeyIdList != null)
  206. SubjectKeyIdList.Clear ();
  207. if (SubjectNameList != null)
  208. SubjectNameList.Clear ();
  209. if (X509CertificateList != null)
  210. X509CertificateList.Clear ();
  211. x509crl = null;
  212. if ((element.LocalName != XmlSignature.ElementNames.X509Data) || (element.NamespaceURI != XmlSignature.NamespaceURI))
  213. throw new CryptographicException ("element");
  214. XmlElement [] xnl = null;
  215. // <X509IssuerSerial>
  216. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509IssuerSerial);
  217. if (xnl != null) {
  218. for (int i=0; i < xnl.Length; i++) {
  219. XmlElement xel = (XmlElement) xnl[i];
  220. XmlElement issuer = XmlSignature.GetChildElement (xel, XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
  221. XmlElement serial = XmlSignature.GetChildElement (xel, XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
  222. AddIssuerSerial (issuer.InnerText, serial.InnerText);
  223. }
  224. }
  225. // <X509SKI>
  226. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509SKI);
  227. if (xnl != null) {
  228. for (int i=0; i < xnl.Length; i++) {
  229. byte[] skid = Convert.FromBase64String (xnl[i].InnerXml);
  230. AddSubjectKeyId (skid);
  231. }
  232. }
  233. // <X509SubjectName>
  234. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509SubjectName);
  235. if (xnl != null) {
  236. for (int i=0; i < xnl.Length; i++) {
  237. AddSubjectName (xnl[i].InnerXml);
  238. }
  239. }
  240. // <X509Certificate>
  241. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509Certificate);
  242. if (xnl != null) {
  243. for (int i=0; i < xnl.Length; i++) {
  244. byte[] cert = Convert.FromBase64String (xnl[i].InnerXml);
  245. AddCertificate (new X509Certificate (cert));
  246. }
  247. }
  248. // only one <X509CRL>
  249. XmlElement x509el = XmlSignature.GetChildElement (element, XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
  250. if (x509el != null) {
  251. x509crl = Convert.FromBase64String (x509el.InnerXml);
  252. }
  253. }
  254. }
  255. }