2004-02-20 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509.Extensions / AuthorityKeyIdentifierExtension.cs
1 //
2 // AuthorityKeyIdentifierExtension.cs: Handles X.509 AuthorityKeyIdentifier extensions.
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Novell (http://www.novell.com)
8 //
9
10 using System;
11 using System.Text;
12
13 using Mono.Security;
14 using Mono.Security.X509;
15
16 namespace Mono.Security.X509.Extensions {
17
18         /*
19          * id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::=  { id-ce 35 }
20          * 
21          * AuthorityKeyIdentifier ::= SEQUENCE {
22          *    keyIdentifier             [0] KeyIdentifier           OPTIONAL,
23          *    authorityCertIssuer       [1] GeneralNames            OPTIONAL,
24          *    authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }
25          * 
26          * KeyIdentifier ::= OCTET STRING
27          */
28
29         public class AuthorityKeyIdentifierExtension : X509Extension {
30
31                 private byte[] aki;
32
33                 public AuthorityKeyIdentifierExtension () : base () 
34                 {
35                         extnOid = "2.5.29.35";
36                 }
37
38                 public AuthorityKeyIdentifierExtension (ASN1 asn1) : base (asn1) {}
39
40                 public AuthorityKeyIdentifierExtension (X509Extension extension) : base (extension) {}
41
42                 protected override void Decode () 
43                 {
44                         ASN1 sequence = new ASN1 (extnValue.Value);
45                         if (sequence.Tag != 0x30)
46                                 throw new ArgumentException ("Invalid AuthorityKeyIdentifier extension");
47                         for (int i=0; i < sequence.Count; i++) {
48                                 ASN1 el = sequence [i];
49                                 switch (el.Tag) {
50                                         case 0x80:
51                                                 aki = el.Value;
52                                                 break;
53                                         case 0x81:
54                                         case 0x82:
55                                         default:
56                                                 throw new ArgumentException ("Invalid AuthorityKeyIdentifier extension");
57                                 }
58                         }
59                 }
60
61                 public override string Name {
62                         get { return "Authority Key Identifier"; }
63                 }
64
65                 public override string ToString () 
66                 {
67                         StringBuilder sb = new StringBuilder ();
68                         if (aki != null) {
69                                 // [0] KeyIdentifier
70                                 int x = 0;
71                                 sb.Append ("KeyID=");
72                                 while (x < aki.Length) {
73                                         sb.Append (aki [x].ToString ("X2"));
74                                         if (x % 2 == 1)
75                                                 sb.Append (" ");
76                                         x++;
77                                 }
78                                 // [1] GeneralNames
79                                 // TODO
80                                 // [2] CertificateSerialNumber
81                                 // TODO
82                         }
83                         return sb.ToString ();
84                 }
85         }
86 }