* FileSystemInfo.cs: corrected COM visibility of UTC properties
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509.Extensions / NetscapeCertTypeExtension.cs
1 //
2 // NetscapeCertTypeExtension.cs: Handles Netscape CertType 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         // References:
19         // a.   Netscape Certificate Extensions Navigator 3.0 Version
20         //      http://wp.netscape.com/eng/security/cert-exts.html
21         // b.   Netscape Certificate Extensions Communicator 4.0 Version
22         //      http://wp.netscape.com/eng/security/comm4-cert-exts.html
23         // c.   2.16.840.1.113730.1.1 - Netscape certificate type
24         //      http://www.alvestrand.no/objectid/2.16.840.1.113730.1.1.html
25
26         public class NetscapeCertTypeExtension : X509Extension {
27
28                 /*
29                  * bit-0 SSL client - this cert is certified for SSL client authentication use 
30                  * bit-1 SSL server - this cert is certified for SSL server authentication use 
31                  * bit-2 S/MIME - this cert is certified for use by clients(New in PR3) 
32                  * bit-3 Object Signing - this cert is certified for signing objects such as Java applets and plugins(New in PR3) 
33                  * bit-4 Reserved - this bit is reserved for future use 
34                  * bit-5 SSL CA - this cert is certified for issuing certs for SSL use 
35                  * bit-6 S/MIME CA - this cert is certified for issuing certs for S/MIME use(New in PR3) 
36                  * bit-7 Object Signing CA - this cert is certified for issuing certs for Object Signing(New in PR3) 
37                  */
38
39                 // note: because nothing is simple in ASN.1 bits are reversed
40                 [Flags]
41                 public enum CertType {
42                         SslClient = 0x80,
43                         SslServer = 0x40,
44                         Smime = 0x20,
45                         ObjectSigning = 0x10,
46                         SslCa = 0x04,
47                         SmimeCa = 0x02,
48                         ObjectSigningCA = 0x01
49                 }
50
51                 private int ctbits;
52
53                 public NetscapeCertTypeExtension () : base () 
54                 {
55                         extnOid = "2.16.840.1.113730.1.1";
56                 }
57
58                 public NetscapeCertTypeExtension (ASN1 asn1) : base (asn1) {}
59
60                 public NetscapeCertTypeExtension (X509Extension extension) : base (extension) {}
61
62                 protected override void Decode () 
63                 {
64                         ASN1 bitString = new ASN1 (extnValue.Value);
65                         if (bitString.Tag != 0x03)
66                                 throw new ArgumentException ("Invalid NetscapeCertType extension");
67                         int i = 1; // byte zero has the number of unused bits (ASN1's BITSTRING)
68                         while (i < bitString.Value.Length)
69                                 ctbits = (ctbits << 8) + bitString.Value [i++];
70                 }
71
72                 public override string Name {
73                         get { return "NetscapeCertType"; }
74                 }
75
76 /*              public CertType Type {
77                         get { return ctbits; }
78                         set { ctbits = value; }
79                 }*/
80
81                 public bool Support (CertType usage) 
82                 {
83                         int x = Convert.ToInt32 (usage);
84                         return ((x & ctbits) == x);
85                 }
86
87                 public override string ToString () 
88                 {
89                         const string separator = " , ";
90                         StringBuilder sb = new StringBuilder ();
91                         if (Support (CertType.SslClient))
92                                 sb.Append ("SSL Client Authentication");
93                         if (Support (CertType.SslServer)) {
94                                 if (sb.Length > 0)
95                                         sb.Append (separator);
96                                 sb.Append ("SSL Server Authentication");
97                         }
98                         if (Support (CertType.Smime)) {
99                                 if (sb.Length > 0)
100                                         sb.Append (separator);
101                                 sb.Append ("SMIME");
102                         }
103                         if (Support (CertType.ObjectSigning)) {
104                                 if (sb.Length > 0)
105                                         sb.Append (separator);
106                                 sb.Append ("Object Signing");
107                         }
108                         if (Support (CertType.SslCa)) {
109                                 if (sb.Length > 0)
110                                         sb.Append (separator);
111                                 sb.Append ("SSL CA");
112                         }
113                         if (Support (CertType.SmimeCa)) {
114                                 if (sb.Length > 0)
115                                         sb.Append (separator);
116                                 sb.Append ("SMIME CA");
117                         }
118                         if (Support (CertType.ObjectSigningCA)) {
119                                 if (sb.Length > 0)
120                                         sb.Append (separator);
121                                 sb.Append ("Object Signing CA");
122                         }
123                         sb.Append ("(");
124                         sb.Append (ctbits.ToString ("X2"));
125                         sb.Append (")");
126                         sb.Append (Environment.NewLine);
127                         return sb.ToString ();
128                 }
129         }
130 }