Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509.Extensions / ExtendedKeyUsageExtension.cs
1 //
2 // ExtendedKeyUsageExtension.cs: Handles X.509 ExtendedKeyUsage extensions.
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections;
32 using System.Text;
33
34 using Mono.Security;
35 using Mono.Security.X509;
36
37 namespace Mono.Security.X509.Extensions {
38
39         /*
40          * id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 }
41          * 
42          * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
43          * 
44          * KeyPurposeId ::= OBJECT IDENTIFIER
45          */
46
47         public 
48         class ExtendedKeyUsageExtension : X509Extension {
49
50                 private ArrayList keyPurpose;
51
52                 public ExtendedKeyUsageExtension () : base () 
53                 {
54                         extnOid = "2.5.29.37";
55                         keyPurpose = new ArrayList ();
56                 }
57
58                 public ExtendedKeyUsageExtension (ASN1 asn1) : base (asn1)
59                 {
60                 }
61
62                 public ExtendedKeyUsageExtension (X509Extension extension) : base (extension)
63                 {
64                 }
65
66                 protected override void Decode () 
67                 {
68                         keyPurpose = new ArrayList ();
69                         ASN1 sequence = new ASN1 (extnValue.Value);
70                         if (sequence.Tag != 0x30)
71                                 throw new ArgumentException ("Invalid ExtendedKeyUsage extension");
72                         // for every policy OID
73                         for (int i=0; i < sequence.Count; i++)
74                                 keyPurpose.Add (ASN1Convert.ToOid (sequence [i]));
75                 }
76
77                 protected override void Encode () 
78                 {
79                         ASN1 seq = new ASN1 (0x30);
80                         foreach (string oid in keyPurpose) {
81                                 seq.Add (ASN1Convert.FromOid (oid));
82                         }
83
84                         extnValue = new ASN1 (0x04);
85                         extnValue.Add (seq);
86                 }
87
88                 public ArrayList KeyPurpose {
89                         get { return keyPurpose; }
90                 }
91
92                 public override string Name {
93                         get { return "Extended Key Usage"; }
94                 }
95
96                 // serverAuth           1.3.6.1.5.5.7.3.1
97                 // clientAuth           1.3.6.1.5.5.7.3.2
98                 // codeSigning          1.3.6.1.5.5.7.3.3
99                 // emailProtection      1.3.6.1.5.5.7.3.4
100                 // timeStamping         1.3.6.1.5.5.7.3.8
101                 // OCSPSigning          1.3.6.1.5.5.7.3.9
102                 public override string ToString () 
103                 {
104                         StringBuilder sb = new StringBuilder ();
105                         foreach (string s in keyPurpose) {
106                                 switch (s) {
107                                         case "1.3.6.1.5.5.7.3.1":
108                                                 sb.Append ("Server Authentication");
109                                                 break;
110                                         case "1.3.6.1.5.5.7.3.2":
111                                                 sb.Append ("Client Authentication");
112                                                 break;
113                                         case "1.3.6.1.5.5.7.3.3":
114                                                 sb.Append ("Code Signing");
115                                                 break;
116                                         case "1.3.6.1.5.5.7.3.4":
117                                                 sb.Append ("Email Protection");
118                                                 break;
119                                         case "1.3.6.1.5.5.7.3.8":
120                                                 sb.Append ("Time Stamping");
121                                                 break;
122                                         case "1.3.6.1.5.5.7.3.9":
123                                                 sb.Append ("OCSP Signing");
124                                                 break;
125                                         default:
126                                                 sb.Append ("unknown");
127                                                 break;
128                                 }
129                                 sb.AppendFormat (" ({0}){1}", s, Environment.NewLine);
130                         }
131                         return sb.ToString ();
132                 }
133         }
134 }