[xbuild] Actually delete common files (CopyLocal) during Clean.
[mono.git] / mcs / class / corlib / 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 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34 using System;
35 using System.Collections;
36
37 using Mono.Security;
38
39 namespace Mono.Security.X509 {
40         /*
41          * Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension
42          * 
43          * Note: 1..MAX -> There shouldn't be 0 Extensions in the ASN1 structure
44          */
45 #if INSIDE_CORLIB
46         internal
47 #else
48         public 
49 #endif
50         sealed class X509ExtensionCollection : CollectionBase, IEnumerable {
51
52                 private bool readOnly;
53
54                 public X509ExtensionCollection () : base ()
55                 {
56                 }
57
58                 public X509ExtensionCollection (ASN1 asn1) : this ()
59                 {
60                         readOnly = true;
61                         if (asn1 == null)
62                                 return;
63                         if (asn1.Tag != 0x30)
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);
68                         }
69                 }
70
71                 public int Add (X509Extension extension) 
72                 {
73                         if (extension == null)
74                                 throw new ArgumentNullException ("extension");
75                         if (readOnly)
76                                 throw new NotSupportedException ("Extensions are read only");
77                 
78                         return InnerList.Add (extension);
79                 }
80
81                 public void AddRange (X509Extension[] extension) 
82                 {
83                         if (extension == null)
84                                 throw new ArgumentNullException ("extension");
85                         if (readOnly)
86                                 throw new NotSupportedException ("Extensions are read only");
87
88                         for (int i = 0; i < extension.Length; i++) 
89                                 InnerList.Add (extension [i]);
90                 }
91         
92                 public void AddRange (X509ExtensionCollection collection) 
93                 {
94                         if (collection == null)
95                                 throw new ArgumentNullException ("collection");
96                         if (readOnly)
97                                 throw new NotSupportedException ("Extensions are read only");
98
99                         for (int i = 0; i < collection.InnerList.Count; i++) 
100                                 InnerList.Add (collection [i]);
101                 }
102
103                 public bool Contains (X509Extension extension) 
104                 {
105                         return (IndexOf (extension) != -1);
106                 }
107
108                 public bool Contains (string oid) 
109                 {
110                         return (IndexOf (oid) != -1);
111                 }
112
113                 public void CopyTo (X509Extension[] extensions, int index) 
114                 {
115                         if (extensions == null)
116                                 throw new ArgumentNullException ("extensions");
117
118                         InnerList.CopyTo (extensions, index);
119                 }
120
121                 public int IndexOf (X509Extension extension) 
122                 {
123                         if (extension == null)
124                                 throw new ArgumentNullException ("extension");
125
126                         for (int i=0; i < InnerList.Count; i++) {
127                                 X509Extension ex = (X509Extension) InnerList [i];
128                                 if (ex.Equals (extension))
129                                         return i;
130                         }
131                         return -1;
132                 }
133
134                 public int IndexOf (string oid) 
135                 {
136                         if (oid == null)
137                                 throw new ArgumentNullException ("oid");
138
139                         for (int i=0; i < InnerList.Count; i++) {
140                                 X509Extension ex = (X509Extension) InnerList [i];
141                                 if (ex.Oid == oid)
142                                         return i;
143                         }
144                         return -1;
145                 }
146
147                 public void Insert (int index, X509Extension extension) 
148                 {
149                         if (extension == null)
150                                 throw new ArgumentNullException ("extension");
151
152                         InnerList.Insert (index, extension);
153                 }
154
155                 public void Remove (X509Extension extension) 
156                 {
157                         if (extension == null)
158                                 throw new ArgumentNullException ("extension");
159
160                         InnerList.Remove (extension);
161                 }
162
163                 public void Remove (string oid) 
164                 {
165                         if (oid == null)
166                                 throw new ArgumentNullException ("oid");
167
168                         int index = IndexOf (oid);
169                         if (index != -1)
170                                 InnerList.RemoveAt (index);
171                 }
172
173                 IEnumerator IEnumerable.GetEnumerator () 
174                 {
175                         return InnerList.GetEnumerator ();
176                 }
177
178                 public X509Extension this [int index] {
179                         get { return (X509Extension) InnerList [index]; }
180                 }
181
182                 public X509Extension this [string oid] {
183                         get {
184                                 int index = IndexOf (oid);
185                                 if (index == -1)
186                                         return null;
187                                 return (X509Extension) InnerList [index];
188                         }
189                 }
190
191                 public byte[] GetBytes () 
192                 {
193                         if (InnerList.Count < 1)
194                                 return null;
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);
199                         }
200                         return sequence.GetBytes ();
201                 }
202         }
203 }