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