Merge pull request #555 from jack-pappas/sigaltstack-patch
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / X509EnhancedKeyUsageExtension.cs
1 //
2 // System.Security.Cryptography.X509EnhancedKeyUsageExtension
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if SECURITY_DEP
30
31 extern alias MonoSecurity;
32
33 using System.Text;
34
35 using MonoSecurity::Mono.Security;
36
37 namespace System.Security.Cryptography.X509Certificates {
38
39         public sealed class X509EnhancedKeyUsageExtension : X509Extension {
40
41                 internal const string oid = "2.5.29.37";
42                 internal const string friendlyName = "Enhanced Key Usage";
43
44                 private OidCollection _enhKeyUsage;
45                 private AsnDecodeStatus _status;
46
47                 // constructors
48
49                 public X509EnhancedKeyUsageExtension ()
50                 {
51                         _oid = new Oid (oid, friendlyName);
52                 }
53
54                 public X509EnhancedKeyUsageExtension (AsnEncodedData encodedEnhancedKeyUsages, bool critical)
55                 {
56                         // ignore the Oid provided by encodedKeyUsage (our rules!)
57                         _oid = new Oid (oid, friendlyName);
58                         _raw = encodedEnhancedKeyUsages.RawData;
59                         base.Critical = critical;
60                         _status = Decode (this.RawData);
61                 }
62
63                 public X509EnhancedKeyUsageExtension (OidCollection enhancedKeyUsages, bool critical)
64                 {
65                         if (enhancedKeyUsages == null)
66                                 throw new ArgumentNullException ("enhancedKeyUsages");
67
68                         _oid = new Oid (oid, friendlyName);
69                         base.Critical = critical;
70                         _enhKeyUsage = enhancedKeyUsages.ReadOnlyCopy ();
71                         RawData = Encode ();
72                 }
73
74                 // properties
75
76                 public OidCollection EnhancedKeyUsages {
77                         get {
78                                 switch (_status) {
79                                 case AsnDecodeStatus.Ok:
80                                 case AsnDecodeStatus.InformationNotAvailable:
81                                         if (_enhKeyUsage == null)
82                                                 _enhKeyUsage = new OidCollection ();
83                                         _enhKeyUsage.ReadOnly = true;
84                                         return _enhKeyUsage;
85                                 default:
86                                         throw new CryptographicException ("Badly encoded extension.");
87                                 }
88                         }
89                 }
90
91                 // methods
92
93                 public override void CopyFrom (AsnEncodedData asnEncodedData) 
94                 {
95                         if (asnEncodedData == null)
96                                 throw new ArgumentNullException ("encodedData");
97
98                         X509Extension ex = (asnEncodedData as X509Extension);
99                         if (ex == null)
100                                 throw new ArgumentException (Locale.GetText ("Wrong type."), "asnEncodedData");
101
102                         if (ex._oid == null)
103                                 _oid = new Oid (oid, friendlyName);
104                         else 
105                                 _oid = new Oid (ex._oid);
106
107                         RawData = ex.RawData;
108                         base.Critical = ex.Critical;
109                         // and we deal with the rest later
110                         _status = Decode (this.RawData);
111                 }
112
113                 // internal
114
115                 internal AsnDecodeStatus Decode (byte[] extension)
116                 {
117                         if ((extension == null) || (extension.Length == 0))
118                                 return AsnDecodeStatus.BadAsn;
119                         if (extension [0] != 0x30)
120                                 return AsnDecodeStatus.BadTag;
121
122                         if (_enhKeyUsage == null)
123                                 _enhKeyUsage = new OidCollection ();
124
125                         try {
126                                 ASN1 ex = new ASN1 (extension);
127                                 if (ex.Tag != 0x30)
128                                         throw new CryptographicException (Locale.GetText ("Invalid ASN.1 Tag"));
129                                 for (int i=0; i < ex.Count; i++) {
130                                         _enhKeyUsage.Add (new Oid (ASN1Convert.ToOid (ex [i])));
131                                 }
132                         }
133                         catch {
134                                 return AsnDecodeStatus.BadAsn;
135                         }
136
137                         return AsnDecodeStatus.Ok;
138                 }
139
140                 internal byte[] Encode ()
141                 {
142                         ASN1 ex = new ASN1 (0x30);
143                         foreach (Oid oid in _enhKeyUsage) {
144                                 ex.Add (ASN1Convert.FromOid (oid.Value));
145                         }
146                         return ex.GetBytes ();
147                 }
148
149                 internal override string ToString (bool multiLine)
150                 {
151                         switch (_status) {
152                         case AsnDecodeStatus.BadAsn:
153                                 return String.Empty;
154                         case AsnDecodeStatus.BadTag:
155                         case AsnDecodeStatus.BadLength:
156                                 return FormatUnkownData (_raw);
157                         case AsnDecodeStatus.InformationNotAvailable:
158                                 return "Information Not Available";
159                         }
160
161                         if (_oid.Value != oid)
162                                 return String.Format ("Unknown Key Usage ({0})", _oid.Value);
163                         if (_enhKeyUsage.Count == 0)
164                                 return "Information Not Available";
165
166                         StringBuilder sb = new StringBuilder ();
167
168                         for (int i=0; i < _enhKeyUsage.Count; i++) {
169                                 Oid o = _enhKeyUsage [i];
170                                 switch (o.Value) {
171                                 case "1.3.6.1.5.5.7.3.1":
172                                         sb.Append ("Server Authentication (");
173                                         break;
174                                 default:
175                                         sb.Append ("Unknown Key Usage (");
176                                         break;
177                                 }
178                                 sb.Append (o.Value);
179                                 sb.Append (")");
180
181                                 if (multiLine)
182                                         sb.Append (Environment.NewLine);
183                                 else if (i != (_enhKeyUsage.Count - 1))
184                                         sb.Append (", ");
185                         }
186
187                         return sb.ToString ();
188                 }
189         }
190 }
191
192 #endif