* FileSystemInfo.cs: corrected COM visibility of UTC properties
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509.Extensions / KeyUsageExtension.cs
1 //
2 // KeyUsageExtension.cs: Handles X.509 KeyUsage extensions.
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.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-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }
20          * 
21          * KeyUsage ::= BIT STRING {
22          *      digitalSignature        (0),
23          *      nonRepudiation          (1),
24          *      keyEncipherment         (2),
25          *      dataEncipherment        (3),
26          *      keyAgreement            (4),
27          *      keyCertSign             (5),
28          *      cRLSign                 (6),
29          *      encipherOnly            (7),
30          *      decipherOnly            (8) 
31          * }
32          */
33         // note: because nothing is simple in ASN.1 bits are reversed
34         [Flags]
35         public enum KeyUsage {
36                 digitalSignature = 0x80,
37                 nonRepudiation = 0x40,
38                 keyEncipherment = 0x20,
39                 dataEncipherment = 0x10,
40                 keyAgreement = 0x08,
41                 keyCertSign = 0x04,
42                 cRLSign = 0x02,
43                 encipherOnly = 0x01,
44                 decipherOnly = 0x800,
45                 none = 0x0
46         }
47
48         public class KeyUsageExtension : X509Extension {
49
50                 private int kubits;
51
52                 public KeyUsageExtension (ASN1 asn1) : base (asn1) {}
53
54                 public KeyUsageExtension (X509Extension extension) : base (extension) {}
55
56                 protected override void Decode () 
57                 {
58                         ASN1 bitString = new ASN1 (extnValue.Value);
59                         if (bitString.Tag != 0x03)
60                                 throw new ArgumentException ("Invalid KeyUsage extension");
61                         int i = 1; // byte zero has the number of unused bits (ASN1's BITSTRING)
62                         while (i < bitString.Value.Length)
63                                 kubits = (kubits << 8) + bitString.Value [i++];
64                 }
65
66                 public override string Name {
67                         get { return "Key Usage"; }
68                 }
69
70                 public bool Support (KeyUsage usage) 
71                 {
72                         int x = Convert.ToInt32 (usage);
73                         return ((x & kubits) == x);
74                 }
75
76                 public override string ToString () 
77                 {
78                         const string separator = " , ";
79                         StringBuilder sb = new StringBuilder ();
80                         if (Support (KeyUsage.digitalSignature))
81                                 sb.Append ("Digital Signature");
82                         if (Support (KeyUsage.nonRepudiation)) {
83                                 if (sb.Length > 0)
84                                         sb.Append (separator);
85                                 sb.Append ("Non-Repudiation");
86                         }
87                         if (Support (KeyUsage.keyEncipherment)) {
88                                 if (sb.Length > 0)
89                                         sb.Append (separator);
90                                 sb.Append ("Key Encipherment");
91                         }
92                         if (Support (KeyUsage.dataEncipherment)) {
93                                 if (sb.Length > 0)
94                                         sb.Append (separator);
95                                 sb.Append ("Data Encipherment");
96                         }
97                         if (Support (KeyUsage.keyAgreement)) {
98                                 if (sb.Length > 0)
99                                         sb.Append (separator);
100                                 sb.Append ("Key Agreement");            
101                         }
102                         if (Support (KeyUsage.keyCertSign)) {
103                                 if (sb.Length > 0)
104                                         sb.Append (separator);
105                                 sb.Append ("Certificate Signing");
106                         }
107                         if (Support (KeyUsage.cRLSign)) {
108                                 if (sb.Length > 0)
109                                         sb.Append (separator);
110                                 sb.Append ("CRL Signing");
111                         }
112                         if (Support (KeyUsage.encipherOnly)) {
113                                 if (sb.Length > 0)
114                                         sb.Append (separator);
115                                 sb.Append ("Encipher Only ");   // ???
116                         }
117                         if (Support (KeyUsage.decipherOnly)) {
118                                 if (sb.Length > 0)
119                                         sb.Append (separator);
120                                 sb.Append ("Decipher Only");    // ???
121                         }
122                         sb.Append ("(");
123                         sb.Append (kubits.ToString ("X2"));
124                         sb.Append (")");
125                         sb.Append (Environment.NewLine);
126                         return sb.ToString ();
127                 }
128         }
129 }