Merge remote-tracking branch 'upstream/master'
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / X509KeyUsageExtension.cs
1 //
2 // System.Security.Cryptography.X509Certificates.X509KeyUsageExtension
3 //
4 // Authors:
5 //      Tim Coleman (tim@timcoleman.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) Tim Coleman, 2004
9 // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
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 #if SECURITY_DEP
32
33 extern alias MonoSecurity;
34
35 using System.Text;
36
37 using MonoSecurity::Mono.Security;
38
39 namespace System.Security.Cryptography.X509Certificates {
40
41         public sealed class X509KeyUsageExtension : X509Extension {
42
43                 internal const string oid = "2.5.29.15";
44                 internal const string friendlyName = "Key Usage";
45
46                 internal const X509KeyUsageFlags all = X509KeyUsageFlags.EncipherOnly | X509KeyUsageFlags.CrlSign | 
47                         X509KeyUsageFlags.KeyCertSign | X509KeyUsageFlags.KeyAgreement | X509KeyUsageFlags.DataEncipherment |
48                         X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.NonRepudiation | 
49                         X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.DecipherOnly;
50
51                 private X509KeyUsageFlags _keyUsages;
52                 private AsnDecodeStatus _status;
53
54                 // constructors
55
56                 public X509KeyUsageExtension ()
57                 {
58                         _oid = new Oid (oid, friendlyName);
59                 }
60
61                 public X509KeyUsageExtension (AsnEncodedData encodedKeyUsage, bool critical)
62                 {
63                         // ignore the Oid provided by encodedKeyUsage (our rules!)
64                         _oid = new Oid (oid, friendlyName);
65                         _raw = encodedKeyUsage.RawData;
66                         base.Critical = critical;
67                         _status = Decode (this.RawData);
68                 }
69
70                 public X509KeyUsageExtension (X509KeyUsageFlags keyUsages, bool critical)
71                 {
72                         _oid = new Oid (oid, friendlyName);
73                         base.Critical = critical;
74                         _keyUsages = GetValidFlags (keyUsages);
75                         RawData = Encode ();
76                 }
77
78                 // properties
79
80                 public X509KeyUsageFlags KeyUsages {
81                         get {
82                                 switch (_status) {
83                                 case AsnDecodeStatus.Ok:
84                                 case AsnDecodeStatus.InformationNotAvailable:
85                                         return _keyUsages;
86                                 default:
87                                         throw new CryptographicException ("Badly encoded extension.");
88                                 }
89                         }
90                 }
91
92                 // methods
93
94                 public override void CopyFrom (AsnEncodedData encodedData)
95                 {
96                         if (encodedData == null)
97                                 throw new ArgumentNullException ("encodedData");
98
99                         X509Extension ex = (encodedData as X509Extension);
100                         if (ex == null)
101                                 throw new ArgumentException (Locale.GetText ("Wrong type."), "encodedData");
102
103                         if (ex._oid == null)
104                                 _oid = new Oid (oid, friendlyName);
105                         else 
106                                 _oid = new Oid (ex._oid);
107
108                         RawData = ex.RawData;
109                         base.Critical = ex.Critical;
110                         // and we deal with the rest later
111                         _status = Decode (this.RawData);
112                 }
113
114                 // internal
115
116                 internal X509KeyUsageFlags GetValidFlags (X509KeyUsageFlags flags)
117                 {
118                         if ((flags & all) != flags)
119                                 return (X509KeyUsageFlags) 0;
120                         return flags;
121                 }
122
123                 internal AsnDecodeStatus Decode (byte[] extension)
124                 {
125                         if ((extension == null) || (extension.Length == 0))
126                                 return AsnDecodeStatus.BadAsn;
127                         if (extension [0] != 0x03)
128                                 return AsnDecodeStatus.BadTag;
129                         if (extension.Length < 3)
130                                 return AsnDecodeStatus.BadLength;
131                         if (extension.Length < 4)
132                                 return AsnDecodeStatus.InformationNotAvailable;
133
134                         try {
135                                 ASN1 ex = new ASN1 (extension);
136                                 int kubits = 0;
137                                 int i = 1; // byte zero has the number of unused bits (ASN1's BITSTRING)
138                                 while (i < ex.Value.Length)
139                                         kubits = (kubits << 8) + ex.Value [i++];
140
141                                 _keyUsages = GetValidFlags ((X509KeyUsageFlags)kubits);
142                         }
143                         catch {
144                                 return AsnDecodeStatus.BadAsn;
145                         }
146
147                         return AsnDecodeStatus.Ok;
148                 }
149
150                 internal byte[] Encode ()
151                 {
152                         ASN1 ex = null;
153                         int kubits = (int)_keyUsages;
154                         byte empty = 0;
155
156                         if (kubits == 0) {
157                                 ex = new ASN1 (0x03, new byte[] { empty });
158                         } else {
159                                 // count empty bits (applicable to first byte only)
160                                 int ku = ((kubits < Byte.MaxValue) ? kubits : (kubits >> 8));
161                                 while (((ku & 0x01) == 0x00) && (empty < 8)) {
162                                         empty++;
163                                         ku >>= 1;
164                                 }
165
166                                 if (kubits <= Byte.MaxValue) {
167                                         ex = new ASN1 (0x03, new byte[] { empty, (byte)kubits });
168                                 } else {
169                                         ex = new ASN1 (0x03, new byte[] { empty, (byte)kubits, (byte)(kubits >> 8) });
170                                 }
171                         }
172
173                         return ex.GetBytes ();
174                 }
175
176                 internal override string ToString (bool multiLine)
177                 {
178                         switch (_status) {
179                         case AsnDecodeStatus.BadAsn:
180                                 return String.Empty;
181                         case AsnDecodeStatus.BadTag:
182                         case AsnDecodeStatus.BadLength:
183                                 return FormatUnkownData (_raw);
184                         case AsnDecodeStatus.InformationNotAvailable:
185                                 return "Information Not Available";
186                         }
187
188                         if (_oid.Value != oid)
189                                 return String.Format ("Unknown Key Usage ({0})", _oid.Value);
190                         if (_keyUsages == 0)
191                                 return "Information Not Available";
192
193                         StringBuilder sb = new StringBuilder ();
194
195                         if ((_keyUsages & X509KeyUsageFlags.DigitalSignature) != 0) {
196                                 sb.Append ("Digital Signature");
197                         }
198                         if ((_keyUsages & X509KeyUsageFlags.NonRepudiation) != 0) {
199                                 if (sb.Length > 0)
200                                         sb.Append (", ");
201                                 sb.Append ("Non-Repudiation");
202                         }
203                         if ((_keyUsages & X509KeyUsageFlags.KeyEncipherment) != 0) {
204                                 if (sb.Length > 0)
205                                         sb.Append (", ");
206                                 sb.Append ("Key Encipherment");
207                         }
208                         if ((_keyUsages & X509KeyUsageFlags.DataEncipherment) != 0) {
209                                 if (sb.Length > 0)
210                                         sb.Append (", ");
211                                 sb.Append ("Data Encipherment");
212                         }
213                         if ((_keyUsages & X509KeyUsageFlags.KeyAgreement) != 0) {
214                                 if (sb.Length > 0)
215                                         sb.Append (", ");
216                                 sb.Append ("Key Agreement");
217                         }
218                         if ((_keyUsages & X509KeyUsageFlags.KeyCertSign) != 0) {
219                                 if (sb.Length > 0)
220                                         sb.Append (", ");
221                                 sb.Append ("Certificate Signing");
222                         }
223                         if ((_keyUsages & X509KeyUsageFlags.CrlSign) != 0) {
224                                 if (sb.Length > 0)
225                                         sb.Append (", ");
226                                 sb.Append ("Off-line CRL Signing, CRL Signing");
227                         }
228                         if ((_keyUsages & X509KeyUsageFlags.EncipherOnly) != 0) {
229                                 if (sb.Length > 0)
230                                         sb.Append (", ");
231                                 sb.Append ("Encipher Only");
232                         }
233                         if ((_keyUsages & X509KeyUsageFlags.DecipherOnly) != 0) {
234                                 if (sb.Length > 0)
235                                         sb.Append (", ");
236                                 sb.Append ("Decipher Only");
237                         }
238
239                         int ku = (int)_keyUsages;
240                         sb.Append (" (");
241                         sb.Append (((byte)ku).ToString ("x2"));
242                         if (ku > Byte.MaxValue) {
243                                 sb.Append (" ");
244                                 sb.Append (((byte)(ku >> 8)).ToString ("x2"));
245                         }
246                         sb.Append (")");
247
248                         if (multiLine)
249                                 sb.Append (Environment.NewLine);
250
251                         return sb.ToString ();
252                 }
253         }
254 }
255
256 #endif