Copied remotely
[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 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Globalization;
33 using System.Text;
34
35 using Mono.Security;
36 using Mono.Security.X509;
37
38 namespace Mono.Security.X509.Extensions {
39
40         // References:
41         // a.   Netscape Certificate Extensions Navigator 3.0 Version
42         //      http://wp.netscape.com/eng/security/cert-exts.html
43         // b.   Netscape Certificate Extensions Communicator 4.0 Version
44         //      http://wp.netscape.com/eng/security/comm4-cert-exts.html
45         // c.   2.16.840.1.113730.1.1 - Netscape certificate type
46         //      http://www.alvestrand.no/objectid/2.16.840.1.113730.1.1.html
47
48         public class NetscapeCertTypeExtension : X509Extension {
49
50                 /*
51                  * bit-0 SSL client - this cert is certified for SSL client authentication use 
52                  * bit-1 SSL server - this cert is certified for SSL server authentication use 
53                  * bit-2 S/MIME - this cert is certified for use by clients(New in PR3) 
54                  * bit-3 Object Signing - this cert is certified for signing objects such as Java applets and plugins(New in PR3) 
55                  * bit-4 Reserved - this bit is reserved for future use 
56                  * bit-5 SSL CA - this cert is certified for issuing certs for SSL use 
57                  * bit-6 S/MIME CA - this cert is certified for issuing certs for S/MIME use(New in PR3) 
58                  * bit-7 Object Signing CA - this cert is certified for issuing certs for Object Signing(New in PR3) 
59                  */
60
61                 // note: because nothing is simple in ASN.1 bits are reversed
62                 [Flags]
63                 public enum CertTypes {
64                         SslClient = 0x80,
65                         SslServer = 0x40,
66                         Smime = 0x20,
67                         ObjectSigning = 0x10,
68                         SslCA = 0x04,
69                         SmimeCA = 0x02,
70                         ObjectSigningCA = 0x01
71                 }
72
73                 private int ctbits;
74
75                 public NetscapeCertTypeExtension () : base () 
76                 {
77                         extnOid = "2.16.840.1.113730.1.1";
78                 }
79
80                 public NetscapeCertTypeExtension (ASN1 asn1) : base (asn1) 
81                 {
82                 }
83
84                 public NetscapeCertTypeExtension (X509Extension extension) : base (extension)
85                 {
86                 }
87
88                 protected override void Decode () 
89                 {
90                         ASN1 bitString = new ASN1 (extnValue.Value);
91                         if (bitString.Tag != 0x03)
92                                 throw new ArgumentException ("Invalid NetscapeCertType extension");
93                         int i = 1; // byte zero has the number of unused bits (ASN1's BITSTRING)
94                         while (i < bitString.Value.Length)
95                                 ctbits = (ctbits << 8) + bitString.Value [i++];
96                 }
97
98                 public override string Name {
99                         get { return "NetscapeCertType"; }
100                 }
101
102 /*              public CertType Type {
103                         get { return ctbits; }
104                         set { ctbits = value; }
105                 }*/
106
107                 public bool Support (CertTypes usage) 
108                 {
109                         int x = Convert.ToInt32 (usage, CultureInfo.InvariantCulture);
110                         return ((x & ctbits) == x);
111                 }
112
113                 public override string ToString () 
114                 {
115                         const string separator = " , ";
116                         StringBuilder sb = new StringBuilder ();
117                         if (Support (CertTypes.SslClient))
118                                 sb.Append ("SSL Client Authentication");
119                         if (Support (CertTypes.SslServer)) {
120                                 if (sb.Length > 0)
121                                         sb.Append (separator);
122                                 sb.Append ("SSL Server Authentication");
123                         }
124                         if (Support (CertTypes.Smime)) {
125                                 if (sb.Length > 0)
126                                         sb.Append (separator);
127                                 sb.Append ("SMIME");
128                         }
129                         if (Support (CertTypes.ObjectSigning)) {
130                                 if (sb.Length > 0)
131                                         sb.Append (separator);
132                                 sb.Append ("Object Signing");
133                         }
134                         if (Support (CertTypes.SslCA)) {
135                                 if (sb.Length > 0)
136                                         sb.Append (separator);
137                                 sb.Append ("SSL CA");
138                         }
139                         if (Support (CertTypes.SmimeCA)) {
140                                 if (sb.Length > 0)
141                                         sb.Append (separator);
142                                 sb.Append ("SMIME CA");
143                         }
144                         if (Support (CertTypes.ObjectSigningCA)) {
145                                 if (sb.Length > 0)
146                                         sb.Append (separator);
147                                 sb.Append ("Object Signing CA");
148                         }
149                         sb.Append ("(");
150                         sb.Append (ctbits.ToString ("X2", CultureInfo.InvariantCulture));
151                         sb.Append (")");
152                         sb.Append (Environment.NewLine);
153                         return sb.ToString ();
154                 }
155         }
156 }