KeyInfoX509Data.cs 7.5 KB

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