(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Security / Mono.Security.X509 / X509Extensions.cs
blobcb8e15defaaee75c95b782d7ba26fe06c51ce176
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 //
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.
32 using System;
33 using System.Collections;
35 using Mono.Security;
37 namespace Mono.Security.X509 {
39 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
41 * Note: 1..MAX -> There shouldn't be 0 Extensions in the ASN1 structure
43 #if INSIDE_CORLIB
44 internal
45 #else
46 public
47 #endif
48 sealed class X509ExtensionCollection : CollectionBase, IEnumerable {
50 private bool readOnly;
52 public X509ExtensionCollection () : base ()
56 public X509ExtensionCollection (ASN1 asn1) : this ()
58 readOnly = true;
59 if (asn1 == null)
60 return;
61 if (asn1.Tag != 0x30)
62 throw new Exception ("Invalid extensions format");
63 for (int i=0; i < asn1.Count; i++) {
64 X509Extension extension = new X509Extension (asn1 [i]);
65 InnerList.Add (extension);
69 public int Add (X509Extension extension)
71 if (extension == null)
72 throw new ArgumentNullException ("extension");
73 if (readOnly)
74 throw new NotSupportedException ("Extensions are read only");
76 return InnerList.Add (extension);
79 public void AddRange (X509Extension[] extension)
81 if (extension == null)
82 throw new ArgumentNullException ("extension");
83 if (readOnly)
84 throw new NotSupportedException ("Extensions are read only");
86 for (int i = 0; i < extension.Length; i++)
87 InnerList.Add (extension [i]);
90 public void AddRange (X509ExtensionCollection collection)
92 if (collection == null)
93 throw new ArgumentNullException ("collection");
94 if (readOnly)
95 throw new NotSupportedException ("Extensions are read only");
97 for (int i = 0; i < collection.InnerList.Count; i++)
98 InnerList.Add (collection [i]);
101 public bool Contains (X509Extension extension)
103 return (IndexOf (extension) != -1);
106 public bool Contains (string oid)
108 return (IndexOf (oid) != -1);
111 public void CopyTo (X509Extension[] extensions, int index)
113 if (extensions == null)
114 throw new ArgumentNullException ("extensions");
116 InnerList.CopyTo (extensions, index);
119 public int IndexOf (X509Extension extension)
121 if (extension == null)
122 throw new ArgumentNullException ("extension");
124 for (int i=0; i < InnerList.Count; i++) {
125 X509Extension ex = (X509Extension) InnerList [i];
126 if (ex.Equals (extension))
127 return i;
129 return -1;
132 public int IndexOf (string oid)
134 if (oid == null)
135 throw new ArgumentNullException ("oid");
137 for (int i=0; i < InnerList.Count; i++) {
138 X509Extension ex = (X509Extension) InnerList [i];
139 if (ex.Oid == oid)
140 return i;
142 return -1;
145 public void Insert (int index, X509Extension extension)
147 if (extension == null)
148 throw new ArgumentNullException ("extension");
150 InnerList.Insert (index, extension);
153 public void Remove (X509Extension extension)
155 if (extension == null)
156 throw new ArgumentNullException ("extension");
158 InnerList.Remove (extension);
161 public void Remove (string oid)
163 if (oid == null)
164 throw new ArgumentNullException ("oid");
166 int index = IndexOf (oid);
167 if (index != -1)
168 InnerList.RemoveAt (index);
171 IEnumerator IEnumerable.GetEnumerator ()
173 return InnerList.GetEnumerator ();
176 public X509Extension this [int index] {
177 get { return (X509Extension) InnerList [index]; }
180 public X509Extension this [string oid] {
181 get {
182 int index = IndexOf (oid);
183 if (index == -1)
184 return null;
185 return (X509Extension) InnerList [index];
189 public byte[] GetBytes ()
191 if (InnerList.Count < 1)
192 return null;
193 ASN1 sequence = new ASN1 (0x30);
194 for (int i=0; i < InnerList.Count; i++) {
195 X509Extension x = (X509Extension) InnerList [i];
196 sequence.Add (x.ASN1);
198 return sequence.GetBytes ();