X509Extensions.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // X509Extensions.cs: Handles X.509 extensions.
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // (C) 2004 Novell (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using Mono.Security;
  33. namespace Mono.Security.X509 {
  34. /*
  35. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  36. *
  37. * Note: 1..MAX -> There shouldn't be 0 Extensions in the ASN1 structure
  38. */
  39. #if INSIDE_CORLIB
  40. internal
  41. #else
  42. public
  43. #endif
  44. sealed class X509ExtensionCollection : CollectionBase, IEnumerable {
  45. private bool readOnly;
  46. public X509ExtensionCollection () : base ()
  47. {
  48. }
  49. public X509ExtensionCollection (ASN1 asn1) : this ()
  50. {
  51. readOnly = true;
  52. if (asn1 == null)
  53. return;
  54. if (asn1.Tag != 0x30)
  55. throw new Exception ("Invalid extensions format");
  56. for (int i=0; i < asn1.Count; i++) {
  57. X509Extension extension = new X509Extension (asn1 [i]);
  58. InnerList.Add (extension);
  59. }
  60. }
  61. public int Add (X509Extension extension)
  62. {
  63. if (extension == null)
  64. throw new ArgumentNullException ("extension");
  65. if (readOnly)
  66. throw new NotSupportedException ("Extensions are read only");
  67. return InnerList.Add (extension);
  68. }
  69. public void AddRange (X509Extension[] extension)
  70. {
  71. if (extension == null)
  72. throw new ArgumentNullException ("extension");
  73. if (readOnly)
  74. throw new NotSupportedException ("Extensions are read only");
  75. for (int i = 0; i < extension.Length; i++)
  76. InnerList.Add (extension [i]);
  77. }
  78. public void AddRange (X509ExtensionCollection collection)
  79. {
  80. if (collection == null)
  81. throw new ArgumentNullException ("collection");
  82. if (readOnly)
  83. throw new NotSupportedException ("Extensions are read only");
  84. for (int i = 0; i < collection.InnerList.Count; i++)
  85. InnerList.Add (collection [i]);
  86. }
  87. public bool Contains (X509Extension extension)
  88. {
  89. return (IndexOf (extension) != -1);
  90. }
  91. public bool Contains (string oid)
  92. {
  93. return (IndexOf (oid) != -1);
  94. }
  95. public void CopyTo (X509Extension[] extensions, int index)
  96. {
  97. if (extensions == null)
  98. throw new ArgumentNullException ("extensions");
  99. InnerList.CopyTo (extensions, index);
  100. }
  101. public int IndexOf (X509Extension extension)
  102. {
  103. if (extension == null)
  104. throw new ArgumentNullException ("extension");
  105. for (int i=0; i < InnerList.Count; i++) {
  106. X509Extension ex = (X509Extension) InnerList [i];
  107. if (ex.Equals (extension))
  108. return i;
  109. }
  110. return -1;
  111. }
  112. public int IndexOf (string oid)
  113. {
  114. if (oid == null)
  115. throw new ArgumentNullException ("oid");
  116. for (int i=0; i < InnerList.Count; i++) {
  117. X509Extension ex = (X509Extension) InnerList [i];
  118. if (ex.Oid == oid)
  119. return i;
  120. }
  121. return -1;
  122. }
  123. public void Insert (int index, X509Extension extension)
  124. {
  125. if (extension == null)
  126. throw new ArgumentNullException ("extension");
  127. InnerList.Insert (index, extension);
  128. }
  129. public void Remove (X509Extension extension)
  130. {
  131. if (extension == null)
  132. throw new ArgumentNullException ("extension");
  133. InnerList.Remove (extension);
  134. }
  135. public void Remove (string oid)
  136. {
  137. if (oid == null)
  138. throw new ArgumentNullException ("oid");
  139. int index = IndexOf (oid);
  140. if (index != -1)
  141. InnerList.RemoveAt (index);
  142. }
  143. IEnumerator IEnumerable.GetEnumerator ()
  144. {
  145. return InnerList.GetEnumerator ();
  146. }
  147. public X509Extension this [int index] {
  148. get { return (X509Extension) InnerList [index]; }
  149. }
  150. public X509Extension this [string oid] {
  151. get {
  152. int index = IndexOf (oid);
  153. if (index == -1)
  154. return null;
  155. return (X509Extension) InnerList [index];
  156. }
  157. }
  158. public byte[] GetBytes ()
  159. {
  160. if (InnerList.Count < 1)
  161. return null;
  162. ASN1 sequence = new ASN1 (0x30);
  163. for (int i=0; i < InnerList.Count; i++) {
  164. X509Extension x = (X509Extension) InnerList [i];
  165. sequence.Add (x.ASN1);
  166. }
  167. return sequence.GetBytes ();
  168. }
  169. }
  170. }