Merge pull request #637 from LogosBible/enetdown
[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 #if INSIDE_SYSTEM
49         internal
50 #else
51         public
52 #endif
53         class NetscapeCertTypeExtension : X509Extension {
54
55                 /*
56                  * bit-0 SSL client - this cert is certified for SSL client authentication use 
57                  * bit-1 SSL server - this cert is certified for SSL server authentication use 
58                  * bit-2 S/MIME - this cert is certified for use by clients(New in PR3) 
59                  * bit-3 Object Signing - this cert is certified for signing objects such as Java applets and plugins(New in PR3) 
60                  * bit-4 Reserved - this bit is reserved for future use 
61                  * bit-5 SSL CA - this cert is certified for issuing certs for SSL use 
62                  * bit-6 S/MIME CA - this cert is certified for issuing certs for S/MIME use(New in PR3) 
63                  * bit-7 Object Signing CA - this cert is certified for issuing certs for Object Signing(New in PR3) 
64                  */
65
66                 // note: because nothing is simple in ASN.1 bits are reversed
67                 [Flags]
68                 public enum CertTypes {
69                         SslClient = 0x80,
70                         SslServer = 0x40,
71                         Smime = 0x20,
72                         ObjectSigning = 0x10,
73                         SslCA = 0x04,
74                         SmimeCA = 0x02,
75                         ObjectSigningCA = 0x01
76                 }
77
78                 private int ctbits;
79
80                 public NetscapeCertTypeExtension () : base () 
81                 {
82                         extnOid = "2.16.840.1.113730.1.1";
83                 }
84
85                 public NetscapeCertTypeExtension (ASN1 asn1) : base (asn1) 
86                 {
87                 }
88
89                 public NetscapeCertTypeExtension (X509Extension extension) : base (extension)
90                 {
91                 }
92
93                 protected override void Decode () 
94                 {
95                         ASN1 bitString = new ASN1 (extnValue.Value);
96                         if (bitString.Tag != 0x03)
97                                 throw new ArgumentException ("Invalid NetscapeCertType extension");
98                         int i = 1; // byte zero has the number of unused bits (ASN1's BITSTRING)
99                         while (i < bitString.Value.Length)
100                                 ctbits = (ctbits << 8) + bitString.Value [i++];
101                 }
102
103                 public override string Name {
104                         get { return "NetscapeCertType"; }
105                 }
106
107 /*              public CertType Type {
108                         get { return ctbits; }
109                         set { ctbits = value; }
110                 }*/
111
112                 public bool Support (CertTypes usage) 
113                 {
114                         int x = Convert.ToInt32 (usage, CultureInfo.InvariantCulture);
115                         return ((x & ctbits) == x);
116                 }
117
118                 public override string ToString () 
119                 {
120                         const string separator = " , ";
121                         StringBuilder sb = new StringBuilder ();
122                         if (Support (CertTypes.SslClient))
123                                 sb.Append ("SSL Client Authentication");
124                         if (Support (CertTypes.SslServer)) {
125                                 if (sb.Length > 0)
126                                         sb.Append (separator);
127                                 sb.Append ("SSL Server Authentication");
128                         }
129                         if (Support (CertTypes.Smime)) {
130                                 if (sb.Length > 0)
131                                         sb.Append (separator);
132                                 sb.Append ("SMIME");
133                         }
134                         if (Support (CertTypes.ObjectSigning)) {
135                                 if (sb.Length > 0)
136                                         sb.Append (separator);
137                                 sb.Append ("Object Signing");
138                         }
139                         if (Support (CertTypes.SslCA)) {
140                                 if (sb.Length > 0)
141                                         sb.Append (separator);
142                                 sb.Append ("SSL CA");
143                         }
144                         if (Support (CertTypes.SmimeCA)) {
145                                 if (sb.Length > 0)
146                                         sb.Append (separator);
147                                 sb.Append ("SMIME CA");
148                         }
149                         if (Support (CertTypes.ObjectSigningCA)) {
150                                 if (sb.Length > 0)
151                                         sb.Append (separator);
152                                 sb.Append ("Object Signing CA");
153                         }
154                         sb.Append ("(");
155                         sb.Append (ctbits.ToString ("X2", CultureInfo.InvariantCulture));
156                         sb.Append (")");
157                         sb.Append (Environment.NewLine);
158                         return sb.ToString ();
159                 }
160         }
161 }