2 // System.Security.Cryptography.X509EnhancedKeyUsageExtension
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 #if NET_2_0 && SECURITY_DEP
35 namespace System
.Security
.Cryptography
.X509Certificates
{
37 public sealed class X509EnhancedKeyUsageExtension
: X509Extension
{
39 internal const string oid
= "2.5.29.37";
40 internal const string friendlyName
= "Enhanced Key Usage";
42 private OidCollection _enhKeyUsage
;
43 private AsnDecodeStatus _status
;
47 public X509EnhancedKeyUsageExtension ()
49 _oid
= new Oid (oid
, friendlyName
);
52 public X509EnhancedKeyUsageExtension (AsnEncodedData encodedEnhancedKeyUsages
, bool critical
)
54 // ignore the Oid provided by encodedKeyUsage (our rules!)
55 _oid
= new Oid (oid
, friendlyName
);
56 _raw
= encodedEnhancedKeyUsages
.RawData
;
57 base.Critical
= critical
;
58 _status
= Decode (this.RawData
);
61 public X509EnhancedKeyUsageExtension (OidCollection enhancedKeyUsages
, bool critical
)
63 if (enhancedKeyUsages
== null)
64 throw new ArgumentNullException ("enhancedKeyUsages");
66 _oid
= new Oid (oid
, friendlyName
);
67 base.Critical
= critical
;
68 _enhKeyUsage
= enhancedKeyUsages
.ReadOnlyCopy ();
74 public OidCollection EnhancedKeyUsages
{
77 case AsnDecodeStatus
.Ok
:
78 case AsnDecodeStatus
.InformationNotAvailable
:
79 if (_enhKeyUsage
== null)
80 _enhKeyUsage
= new OidCollection ();
81 _enhKeyUsage
.ReadOnly
= true;
84 throw new CryptographicException ("Badly encoded extension.");
91 public override void CopyFrom (AsnEncodedData asnEncodedData
)
93 if (asnEncodedData
== null)
94 throw new ArgumentNullException ("encodedData");
96 X509Extension ex
= (asnEncodedData
as X509Extension
);
98 throw new ArgumentException (Locale
.GetText ("Wrong type."), "asnEncodedData");
101 _oid
= new Oid (oid
, friendlyName
);
103 _oid
= new Oid (ex
._oid
);
105 RawData
= ex
.RawData
;
106 base.Critical
= ex
.Critical
;
107 // and we deal with the rest later
108 _status
= Decode (this.RawData
);
113 internal AsnDecodeStatus
Decode (byte[] extension
)
115 if ((extension
== null) || (extension
.Length
== 0))
116 return AsnDecodeStatus
.BadAsn
;
117 if (extension
[0] != 0x30)
118 return AsnDecodeStatus
.BadTag
;
120 if (_enhKeyUsage
== null)
121 _enhKeyUsage
= new OidCollection ();
124 ASN1 ex
= new ASN1 (extension
);
126 throw new CryptographicException (Locale
.GetText ("Invalid ASN.1 Tag"));
127 for (int i
=0; i
< ex
.Count
; i
++) {
128 _enhKeyUsage
.Add (new Oid (ASN1Convert
.ToOid (ex
[i
])));
132 return AsnDecodeStatus
.BadAsn
;
135 return AsnDecodeStatus
.Ok
;
138 internal byte[] Encode ()
140 ASN1 ex
= new ASN1 (0x30);
141 foreach (Oid oid
in _enhKeyUsage
) {
142 ex
.Add (ASN1Convert
.FromOid (oid
.Value
));
144 return ex
.GetBytes ();
147 internal override string ToString (bool multiLine
)
150 case AsnDecodeStatus
.BadAsn
:
152 case AsnDecodeStatus
.BadTag
:
153 case AsnDecodeStatus
.BadLength
:
154 return FormatUnkownData (_raw
);
155 case AsnDecodeStatus
.InformationNotAvailable
:
156 return "Information Not Available";
159 if (_oid
.Value
!= oid
)
160 return String
.Format ("Unknown Key Usage ({0})", _oid
.Value
);
161 if (_enhKeyUsage
.Count
== 0)
162 return "Information Not Available";
164 StringBuilder sb
= new StringBuilder ();
166 for (int i
=0; i
< _enhKeyUsage
.Count
; i
++) {
167 Oid o
= _enhKeyUsage
[i
];
169 case "1.3.6.1.5.5.7.3.1":
170 sb
.Append ("Server Authentication (");
173 sb
.Append ("Unknown Key Usage (");
180 sb
.Append (Environment
.NewLine
);
181 else if (i
!= (_enhKeyUsage
.Count
- 1))
185 return sb
.ToString ();