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 // 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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
33 using System
.Collections
;
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
48 sealed class X509ExtensionCollection
: CollectionBase
, IEnumerable
{
50 private bool readOnly
;
52 public X509ExtensionCollection () : base ()
56 public X509ExtensionCollection (ASN1 asn1
) : this ()
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");
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");
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");
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
))
132 public int IndexOf (string oid
)
135 throw new ArgumentNullException ("oid");
137 for (int i
=0; i
< InnerList
.Count
; i
++) {
138 X509Extension ex
= (X509Extension
) InnerList
[i
];
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
)
164 throw new ArgumentNullException ("oid");
166 int index
= IndexOf (oid
);
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
] {
182 int index
= IndexOf (oid
);
185 return (X509Extension
) InnerList
[index
];
189 public byte[] GetBytes ()
191 if (InnerList
.Count
< 1)
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 ();