Do not expose the classes when they are compiled inside corlib as helpers.
[mono.git] / mcs / class / corlib / Mono.Security.X509 / X509Extensions.cs
1 //
2 // X509Extensions.cs: Handles X.509 extensions.
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.Collections;
12
13 using Mono.Security;
14
15 namespace Mono.Security.X509 {
16         /*
17          * Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension
18          * 
19          * Note: 1..MAX -> There shouldn't be 0 Extensions in the ASN1 structure
20          */
21 #if INSIDE_CORLIB
22         internal
23 #else
24         public
25 #endif
26         class X509Extensions : ICollection, IEnumerable {
27
28                 private ArrayList extensions;
29                 private bool readOnly;
30
31                 public X509Extensions ()
32                 {
33                         extensions = new ArrayList ();
34                 }
35
36                 public X509Extensions (ASN1 asn1) : this ()
37                 {
38                         readOnly = true;
39                         if (asn1 == null)
40                                 return;
41                         if (asn1.Tag != 0x30)
42                                 throw new Exception ("Invalid extensions format");
43                         for (int i=0; i < asn1.Count; i++) {
44                                 X509Extension extension = new X509Extension (asn1 [i]);
45                                 extensions.Add (extension);
46                         }
47                 }
48
49                 // ICollection
50                 public int Count {
51                         get { return extensions.Count; }
52                 }
53
54                 // ICollection
55                 public bool IsSynchronized {
56                         get { return extensions.IsSynchronized; }
57                 }
58
59                 // ICollection
60                 public object SyncRoot {
61                         get { return extensions.SyncRoot; }
62                 }
63
64                 // ICollection
65                 public void CopyTo (Array array, int index) 
66                 {
67                         extensions.CopyTo (array, index);
68                 }
69
70                 // IEnumerable
71                 public IEnumerator GetEnumerator () 
72                 {
73                         return extensions.GetEnumerator ();
74                 }
75
76                 public X509Extension this [int index] {
77                         get { return (X509Extension) extensions [index]; }
78                 }
79
80                 public X509Extension this [string index] {
81                         get { 
82                                 for (int i=0; i < extensions.Count; i++) {
83                                         X509Extension extension = (X509Extension) extensions [i];
84                                         if (extension.OID == index)
85                                                 return extension;
86                                 }
87                                 return null; 
88                         }
89                 }
90
91                 public void Add (X509Extension extension) 
92                 {
93                         if (readOnly)
94                                 throw new NotSupportedException ("Extensions are read only");
95                         extensions.Add (extension);
96                 }
97
98                 public byte[] GetBytes () 
99                 {
100                         if (extensions.Count < 1)
101                                 return null;
102                         ASN1 sequence = new ASN1 (0x30);
103                         for (int i=0; i < extensions.Count; i++) {
104                                 X509Extension x = (X509Extension) extensions [i];
105                                 sequence.Add (x.ASN1);
106                         }
107                         return sequence.GetBytes ();
108                 }
109         }
110 }