2003-03-15 Sebastien Pouliot <spouliot@videotron.ca>
[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         public class X509Extensions : ICollection, IEnumerable {
22
23                 private ArrayList extensions;
24                 private bool readOnly;
25
26                 public X509Extensions ()
27                 {
28                         extensions = new ArrayList ();
29                 }
30
31                 public X509Extensions (ASN1 asn1) : this ()
32                 {
33                         readOnly = true;
34                         if (asn1 == null)
35                                 return;
36                         if (asn1.Tag != 0x30)
37                                 throw new Exception ("Invalid extensions format");
38                         for (int i=0; i < asn1.Count; i++) {
39                                 X509Extension extension = new X509Extension (asn1 [i]);
40                                 extensions.Add (extension);
41                         }
42                 }
43
44                 // ICollection
45                 public int Count {
46                         get { return extensions.Count; }
47                 }
48
49                 // ICollection
50                 public bool IsSynchronized {
51                         get { return extensions.IsSynchronized; }
52                 }
53
54                 // ICollection
55                 public object SyncRoot {
56                         get { return extensions.SyncRoot; }
57                 }
58
59                 // ICollection
60                 public void CopyTo (Array array, int index) 
61                 {
62                         extensions.CopyTo (array, index);
63                 }
64
65                 // IEnumerable
66                 public IEnumerator GetEnumerator () 
67                 {
68                         return extensions.GetEnumerator ();
69                 }
70
71                 public X509Extension this [int index] {
72                         get { return (X509Extension) extensions [index]; }
73                 }
74
75                 public X509Extension this [string index] {
76                         get { 
77                                 for (int i=0; i < extensions.Count; i++) {
78                                         X509Extension extension = (X509Extension) extensions [i];
79                                         if (extension.OID == index)
80                                                 return extension;
81                                 }
82                                 return null; 
83                         }
84                 }
85
86                 public void Add (X509Extension extension) 
87                 {
88                         if (readOnly)
89                                 throw new NotSupportedException ("Extensions are read only");
90                         extensions.Add (extension);
91                 }
92
93                 public byte[] GetBytes () 
94                 {
95                         if (extensions.Count < 1)
96                                 return null;
97                         ASN1 sequence = new ASN1 (0x30);
98                         for (int i=0; i < extensions.Count; i++) {
99                                 X509Extension x = (X509Extension) extensions [i];
100                                 sequence.Add (x.ASN1);
101                         }
102                         return sequence.GetBytes ();
103                 }
104         }
105 }