2 // X509Extensions.cs: Handles X.509 extensions.
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
39 namespace Mono.Security.X509 {
41 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
43 * Note: 1..MAX -> There shouldn't be 0 Extensions in the ASN1 structure
50 sealed class X509ExtensionCollection : CollectionBase, IEnumerable {
52 private bool readOnly;
54 public X509ExtensionCollection () : base ()
58 public X509ExtensionCollection (ASN1 asn1) : this ()
64 throw new Exception ("Invalid extensions format");
65 for (int i=0; i < asn1.Count; i++) {
66 X509Extension extension = new X509Extension (asn1 [i]);
67 InnerList.Add (extension);
71 public int Add (X509Extension extension)
73 if (extension == null)
74 throw new ArgumentNullException ("extension");
76 throw new NotSupportedException ("Extensions are read only");
78 return InnerList.Add (extension);
81 public void AddRange (X509Extension[] extension)
83 if (extension == null)
84 throw new ArgumentNullException ("extension");
86 throw new NotSupportedException ("Extensions are read only");
88 for (int i = 0; i < extension.Length; i++)
89 InnerList.Add (extension [i]);
92 public void AddRange (X509ExtensionCollection collection)
94 if (collection == null)
95 throw new ArgumentNullException ("collection");
97 throw new NotSupportedException ("Extensions are read only");
99 for (int i = 0; i < collection.InnerList.Count; i++)
100 InnerList.Add (collection [i]);
103 public bool Contains (X509Extension extension)
105 return (IndexOf (extension) != -1);
108 public bool Contains (string oid)
110 return (IndexOf (oid) != -1);
113 public void CopyTo (X509Extension[] extensions, int index)
115 if (extensions == null)
116 throw new ArgumentNullException ("extensions");
118 InnerList.CopyTo (extensions, index);
121 public int IndexOf (X509Extension extension)
123 if (extension == null)
124 throw new ArgumentNullException ("extension");
126 for (int i=0; i < InnerList.Count; i++) {
127 X509Extension ex = (X509Extension) InnerList [i];
128 if (ex.Equals (extension))
134 public int IndexOf (string oid)
137 throw new ArgumentNullException ("oid");
139 for (int i=0; i < InnerList.Count; i++) {
140 X509Extension ex = (X509Extension) InnerList [i];
147 public void Insert (int index, X509Extension extension)
149 if (extension == null)
150 throw new ArgumentNullException ("extension");
152 InnerList.Insert (index, extension);
155 public void Remove (X509Extension extension)
157 if (extension == null)
158 throw new ArgumentNullException ("extension");
160 InnerList.Remove (extension);
163 public void Remove (string oid)
166 throw new ArgumentNullException ("oid");
168 int index = IndexOf (oid);
170 InnerList.RemoveAt (index);
173 IEnumerator IEnumerable.GetEnumerator ()
175 return InnerList.GetEnumerator ();
178 public X509Extension this [int index] {
179 get { return (X509Extension) InnerList [index]; }
182 public X509Extension this [string oid] {
184 int index = IndexOf (oid);
187 return (X509Extension) InnerList [index];
191 public byte[] GetBytes ()
193 if (InnerList.Count < 1)
195 ASN1 sequence = new ASN1 (0x30);
196 for (int i=0; i < InnerList.Count; i++) {
197 X509Extension x = (X509Extension) InnerList [i];
198 sequence.Add (x.ASN1);
200 return sequence.GetBytes ();