New test.
[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 class ExtendedKeyUsageExtension : X509Extension {
48
49                 private ArrayList keyPurpose;
50
51                 public ExtendedKeyUsageExtension () : base () 
52                 {
53                         extnOid = "2.5.29.37";
54                         keyPurpose = new ArrayList ();
55                 }
56
57                 public ExtendedKeyUsageExtension (ASN1 asn1) : base (asn1)
58                 {
59                 }
60
61                 public ExtendedKeyUsageExtension (X509Extension extension) : base (extension)
62                 {
63                 }
64
65                 protected override void Decode () 
66                 {
67                         keyPurpose = new ArrayList ();
68                         ASN1 sequence = new ASN1 (extnValue.Value);
69                         if (sequence.Tag != 0x30)
70                                 throw new ArgumentException ("Invalid ExtendedKeyUsage extension");
71                         // for every policy OID
72                         for (int i=0; i < sequence.Count; i++)
73                                 keyPurpose.Add (ASN1Convert.ToOid (sequence [i]));
74                 }
75
76                 protected override void Encode () 
77                 {
78                         ASN1 seq = new ASN1 (0x30);
79                         foreach (string oid in keyPurpose) {
80                                 seq.Add (ASN1Convert.FromOid (oid));
81                         }
82
83                         extnValue = new ASN1 (0x04);
84                         extnValue.Add (seq);
85                 }
86
87                 public ArrayList KeyPurpose {
88                         get { return keyPurpose; }
89                 }
90
91                 public override string Name {
92                         get { return "Extended Key Usage"; }
93                 }
94
95                 // serverAuth           1.3.6.1.5.5.7.3.1
96                 // clientAuth           1.3.6.1.5.5.7.3.2
97                 // codeSigning          1.3.6.1.5.5.7.3.3
98                 // emailProtection      1.3.6.1.5.5.7.3.4
99                 // timeStamping         1.3.6.1.5.5.7.3.8
100                 // OCSPSigning          1.3.6.1.5.5.7.3.9
101                 public override string ToString () 
102                 {
103                         StringBuilder sb = new StringBuilder ();
104                         foreach (string s in keyPurpose) {
105                                 switch (s) {
106                                         case "1.3.6.1.5.5.7.3.1":
107                                                 sb.Append ("Server Authentication");
108                                                 break;
109                                         case "1.3.6.1.5.5.7.3.2":
110                                                 sb.Append ("Client Authentication");
111                                                 break;
112                                         case "1.3.6.1.5.5.7.3.3":
113                                                 sb.Append ("Code Signing");
114                                                 break;
115                                         case "1.3.6.1.5.5.7.3.4":
116                                                 sb.Append ("Email Protection");
117                                                 break;
118                                         case "1.3.6.1.5.5.7.3.8":
119                                                 sb.Append ("Time Stamping");
120                                                 break;
121                                         case "1.3.6.1.5.5.7.3.9":
122                                                 sb.Append ("OCSP Signing");
123                                                 break;
124                                         default:
125                                                 sb.Append ("unknown");
126                                                 break;
127                                 }
128                                 sb.AppendFormat (" ({0}){1}", s, Environment.NewLine);
129                         }
130                         return sb.ToString ();
131                 }
132         }
133 }