Merge pull request #5032 from kumpera/profiler-reorder-heap-events
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509 / X509Extensions.cs
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 //
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
32 using System;
33 using System.Collections;
34
35 using Mono.Security;
36
37 namespace Mono.Security.X509 {
38         /*
39          * Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension
40          * 
41          * Note: 1..MAX -> There shouldn't be 0 Extensions in the ASN1 structure
42          */
43 #if INSIDE_CORLIB
44         internal
45 #else
46         public 
47 #endif
48         sealed class X509ExtensionCollection : CollectionBase, IEnumerable {
49
50                 private bool readOnly;
51
52                 public X509ExtensionCollection () : base ()
53                 {
54                 }
55
56                 public X509ExtensionCollection (ASN1 asn1) : this ()
57                 {
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);
66                         }
67                 }
68
69                 public int Add (X509Extension extension) 
70                 {
71                         if (extension == null)
72                                 throw new ArgumentNullException ("extension");
73                         if (readOnly)
74                                 throw new NotSupportedException ("Extensions are read only");
75                 
76                         return InnerList.Add (extension);
77                 }
78
79                 public void AddRange (X509Extension[] extension) 
80                 {
81                         if (extension == null)
82                                 throw new ArgumentNullException ("extension");
83                         if (readOnly)
84                                 throw new NotSupportedException ("Extensions are read only");
85
86                         for (int i = 0; i < extension.Length; i++) 
87                                 InnerList.Add (extension [i]);
88                 }
89         
90                 public void AddRange (X509ExtensionCollection collection) 
91                 {
92                         if (collection == null)
93                                 throw new ArgumentNullException ("collection");
94                         if (readOnly)
95                                 throw new NotSupportedException ("Extensions are read only");
96
97                         for (int i = 0; i < collection.InnerList.Count; i++) 
98                                 InnerList.Add (collection [i]);
99                 }
100
101                 public bool Contains (X509Extension extension) 
102                 {
103                         return (IndexOf (extension) != -1);
104                 }
105
106                 public bool Contains (string oid) 
107                 {
108                         return (IndexOf (oid) != -1);
109                 }
110
111                 public void CopyTo (X509Extension[] extensions, int index) 
112                 {
113                         if (extensions == null)
114                                 throw new ArgumentNullException ("extensions");
115
116                         InnerList.CopyTo (extensions, index);
117                 }
118
119                 public int IndexOf (X509Extension extension) 
120                 {
121                         if (extension == null)
122                                 throw new ArgumentNullException ("extension");
123
124                         for (int i=0; i < InnerList.Count; i++) {
125                                 X509Extension ex = (X509Extension) InnerList [i];
126                                 if (ex.Equals (extension))
127                                         return i;
128                         }
129                         return -1;
130                 }
131
132                 public int IndexOf (string oid) 
133                 {
134                         if (oid == null)
135                                 throw new ArgumentNullException ("oid");
136
137                         for (int i=0; i < InnerList.Count; i++) {
138                                 X509Extension ex = (X509Extension) InnerList [i];
139                                 if (ex.Oid == oid)
140                                         return i;
141                         }
142                         return -1;
143                 }
144
145                 public void Insert (int index, X509Extension extension) 
146                 {
147                         if (extension == null)
148                                 throw new ArgumentNullException ("extension");
149
150                         InnerList.Insert (index, extension);
151                 }
152
153                 public void Remove (X509Extension extension) 
154                 {
155                         if (extension == null)
156                                 throw new ArgumentNullException ("extension");
157
158                         InnerList.Remove (extension);
159                 }
160
161                 public void Remove (string oid) 
162                 {
163                         if (oid == null)
164                                 throw new ArgumentNullException ("oid");
165
166                         int index = IndexOf (oid);
167                         if (index != -1)
168                                 InnerList.RemoveAt (index);
169                 }
170
171                 IEnumerator IEnumerable.GetEnumerator () 
172                 {
173                         return InnerList.GetEnumerator ();
174                 }
175
176                 public X509Extension this [int index] {
177                         get { return (X509Extension) InnerList [index]; }
178                 }
179
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];
186                         }
187                 }
188
189                 public byte[] GetBytes () 
190                 {
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);
197                         }
198                         return sequence.GetBytes ();
199                 }
200         }
201 }