KeyInfoX509Data.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. // (C) 2004 Novell Inc.
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections;
  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. IssuerSerialList = new ArrayList ();
  46. SubjectKeyIdList = new ArrayList ();
  47. SubjectNameList = new ArrayList ();
  48. X509CertificateList = new ArrayList ();
  49. }
  50. public KeyInfoX509Data (byte[] rgbCert) : this ()
  51. {
  52. AddCertificate (new X509Certificate (rgbCert));
  53. }
  54. public KeyInfoX509Data (X509Certificate cert) : this ()
  55. {
  56. AddCertificate (cert);
  57. }
  58. #if NET_2_0
  59. public KeyInfoX509Data (X509Certificate cert, X509IncludeOption includeOption)
  60. {
  61. }
  62. #endif
  63. public ArrayList Certificates {
  64. get { return X509CertificateList.Count != 0 ? X509CertificateList : null; }
  65. }
  66. public byte[] CRL {
  67. get { return x509crl; }
  68. set { x509crl = value; }
  69. }
  70. public ArrayList IssuerSerials {
  71. get { return IssuerSerialList.Count != 0 ? IssuerSerialList : null; }
  72. }
  73. public ArrayList SubjectKeyIds {
  74. get { return SubjectKeyIdList.Count != 0 ? SubjectKeyIdList : null; }
  75. }
  76. public ArrayList SubjectNames {
  77. get { return SubjectNameList.Count != 0 ? SubjectNameList : null; }
  78. }
  79. public void AddCertificate (X509Certificate certificate)
  80. {
  81. X509CertificateList.Add (certificate);
  82. }
  83. public void AddIssuerSerial (string issuerName, string serialNumber)
  84. {
  85. X509IssuerSerial xis = new X509IssuerSerial (issuerName, serialNumber);
  86. IssuerSerialList.Add (xis);
  87. }
  88. public void AddSubjectKeyId (byte[] subjectKeyId)
  89. {
  90. SubjectKeyIdList.Add (subjectKeyId);
  91. }
  92. #if NET_2_0
  93. [MonoTODO]
  94. public void AddSubjectKeyId (string subjectKeyId)
  95. {
  96. throw new NotImplementedException ();
  97. }
  98. #endif
  99. public void AddSubjectName (string subjectName)
  100. {
  101. SubjectNameList.Add (subjectName);
  102. }
  103. public override XmlElement GetXml ()
  104. {
  105. // sanity check
  106. int count = IssuerSerialList.Count + SubjectKeyIdList.Count + SubjectNameList.Count + X509CertificateList.Count;
  107. if ((x509crl == null) && (count == 0))
  108. throw new CryptographicException ("value");
  109. XmlDocument document = new XmlDocument ();
  110. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.X509Data, XmlSignature.NamespaceURI);
  111. // FIXME: hack to match MS implementation
  112. xel.SetAttribute ("xmlns", XmlSignature.NamespaceURI);
  113. // <X509IssuerSerial>
  114. if (IssuerSerialList.Count > 0) {
  115. foreach (X509IssuerSerial iser in IssuerSerialList) {
  116. XmlElement isl = document.CreateElement (XmlSignature.ElementNames.X509IssuerSerial, XmlSignature.NamespaceURI);
  117. XmlElement xin = document.CreateElement (XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
  118. xin.InnerText = iser.IssuerName;
  119. isl.AppendChild (xin);
  120. XmlElement xsn = document.CreateElement (XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
  121. xsn.InnerText = iser.SerialNumber;
  122. isl.AppendChild (xsn);
  123. xel.AppendChild (isl);
  124. }
  125. }
  126. // <X509SKI>
  127. if (SubjectKeyIdList.Count > 0) {
  128. foreach (byte[] skid in SubjectKeyIdList) {
  129. XmlElement ski = document.CreateElement (XmlSignature.ElementNames.X509SKI, XmlSignature.NamespaceURI);
  130. ski.InnerText = Convert.ToBase64String (skid);
  131. xel.AppendChild (ski);
  132. }
  133. }
  134. // <X509SubjectName>
  135. if (SubjectNameList.Count > 0) {
  136. foreach (string subject in SubjectNameList) {
  137. XmlElement sn = document.CreateElement (XmlSignature.ElementNames.X509SubjectName, XmlSignature.NamespaceURI);
  138. sn.InnerText = subject;
  139. xel.AppendChild (sn);
  140. }
  141. }
  142. // <X509Certificate>
  143. if (X509CertificateList.Count > 0) {
  144. foreach (X509Certificate x509 in X509CertificateList) {
  145. XmlElement cert = document.CreateElement (XmlSignature.ElementNames.X509Certificate, XmlSignature.NamespaceURI);
  146. cert.InnerText = Convert.ToBase64String (x509.GetRawCertData ());
  147. xel.AppendChild (cert);
  148. }
  149. }
  150. // only one <X509CRL>
  151. if (x509crl != null) {
  152. XmlElement crl = document.CreateElement (XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
  153. crl.InnerText = Convert.ToBase64String (x509crl);
  154. xel.AppendChild (crl);
  155. }
  156. return xel;
  157. }
  158. public override void LoadXml (XmlElement element)
  159. {
  160. if (element == null)
  161. throw new ArgumentNullException ("element");
  162. IssuerSerialList.Clear ();
  163. SubjectKeyIdList.Clear ();
  164. SubjectNameList.Clear ();
  165. X509CertificateList.Clear ();
  166. x509crl = null;
  167. if ((element.LocalName != XmlSignature.ElementNames.X509Data) || (element.NamespaceURI != XmlSignature.NamespaceURI))
  168. throw new CryptographicException ("element");
  169. XmlElement [] xnl = null;
  170. // <X509IssuerSerial>
  171. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509IssuerSerial);
  172. if (xnl != null) {
  173. for (int i=0; i < xnl.Length; i++) {
  174. XmlElement xel = (XmlElement) xnl[i];
  175. XmlElement issuer = XmlSignature.GetChildElement (xel, XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
  176. XmlElement serial = XmlSignature.GetChildElement (xel, XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
  177. AddIssuerSerial (issuer.InnerText, serial.InnerText);
  178. }
  179. }
  180. // <X509SKI>
  181. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509SKI);
  182. if (xnl != null) {
  183. for (int i=0; i < xnl.Length; i++) {
  184. byte[] skid = Convert.FromBase64String (xnl[i].InnerXml);
  185. AddSubjectKeyId (skid);
  186. }
  187. }
  188. // <X509SubjectName>
  189. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509SubjectName);
  190. if (xnl != null) {
  191. for (int i=0; i < xnl.Length; i++) {
  192. AddSubjectName (xnl[i].InnerXml);
  193. }
  194. }
  195. // <X509Certificate>
  196. xnl = XmlSignature.GetChildElements (element, XmlSignature.ElementNames.X509Certificate);
  197. if (xnl != null) {
  198. for (int i=0; i < xnl.Length; i++) {
  199. byte[] cert = Convert.FromBase64String (xnl[i].InnerXml);
  200. AddCertificate (new X509Certificate (cert));
  201. }
  202. }
  203. // only one <X509CRL>
  204. XmlElement x509el = XmlSignature.GetChildElement (element, XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
  205. if (x509el != null) {
  206. x509crl = Convert.FromBase64String (x509el.InnerXml);
  207. }
  208. }
  209. }
  210. }