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