2 // ExtendedKeyUsageExtension.cs: Handles X.509 ExtendedKeyUsage extensions.
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
35 using Mono.Security.X509;
37 namespace Mono.Security.X509.Extensions {
40 * id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 }
42 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
44 * KeyPurposeId ::= OBJECT IDENTIFIER
48 class ExtendedKeyUsageExtension : X509Extension {
50 private ArrayList keyPurpose;
52 public ExtendedKeyUsageExtension () : base ()
54 extnOid = "2.5.29.37";
55 keyPurpose = new ArrayList ();
58 public ExtendedKeyUsageExtension (ASN1 asn1) : base (asn1)
62 public ExtendedKeyUsageExtension (X509Extension extension) : base (extension)
66 protected override void Decode ()
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]));
77 protected override void Encode ()
79 ASN1 seq = new ASN1 (0x30);
80 foreach (string oid in keyPurpose) {
81 seq.Add (ASN1Convert.FromOid (oid));
84 extnValue = new ASN1 (0x04);
88 public ArrayList KeyPurpose {
89 get { return keyPurpose; }
92 public override string Name {
93 get { return "Extended Key Usage"; }
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 ()
104 StringBuilder sb = new StringBuilder ();
105 foreach (string s in keyPurpose) {
107 case "1.3.6.1.5.5.7.3.1":
108 sb.Append ("Server Authentication");
110 case "1.3.6.1.5.5.7.3.2":
111 sb.Append ("Client Authentication");
113 case "1.3.6.1.5.5.7.3.3":
114 sb.Append ("Code Signing");
116 case "1.3.6.1.5.5.7.3.4":
117 sb.Append ("Email Protection");
119 case "1.3.6.1.5.5.7.3.8":
120 sb.Append ("Time Stamping");
122 case "1.3.6.1.5.5.7.3.9":
123 sb.Append ("OCSP Signing");
126 sb.Append ("unknown");
129 sb.AppendFormat (" ({0}){1}", s, Environment.NewLine);
131 return sb.ToString ();