2 // KeyAttributesExtension.cs: Handles X.509 *DEPRECATED* KeyAttributes extensions.
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Globalization;
37 using Mono.Security.X509;
39 namespace Mono.Security.X509.Extensions {
40 public class KeyAttributesExtension : X509Extension {
44 private DateTime notBefore;
45 private DateTime notAfter;
47 public KeyAttributesExtension () : base ()
52 public KeyAttributesExtension (ASN1 asn1) : base (asn1)
56 public KeyAttributesExtension (X509Extension extension) : base (extension)
60 protected override void Decode ()
62 ASN1 seq = new ASN1 (extnValue.Value);
64 throw new ArgumentException ("Invalid KeyAttributesExtension extension");
66 // check for KeyIdentifier
69 if (item.Tag == 0x04) {
77 if (item.Tag == 0x03) {
79 int i = 1; // byte zero has the number of unused bits (ASN1's BITSTRING)
80 while (i < item.Value.Length)
81 kubits = (kubits << 8) + item.Value [i++];
84 // check for PrivateKeyValidity
87 if (item.Tag == 0x30) {
93 notBefore = ASN1Convert.ToDateTime (dt);
99 notAfter = ASN1Convert.ToDateTime (dt);
105 public byte[] KeyIdentifier {
109 return (byte[]) keyId.Clone ();
113 public override string Name {
114 get { return "Key Attributes"; }
117 public DateTime NotAfter {
118 get { return notAfter; }
121 public DateTime NotBefore {
122 get { return notBefore; }
125 public bool Support (KeyUsages usage)
127 int x = Convert.ToInt32 (usage, CultureInfo.InvariantCulture);
128 return ((x & kubits) == x);
131 public override string ToString ()
133 StringBuilder sb = new StringBuilder ();
135 sb.Append ("KeyID=");
137 while (x < keyId.Length) {
138 sb.Append (keyId [x].ToString ("X2", CultureInfo.InvariantCulture));
143 sb.Append (Environment.NewLine);
147 sb.Append ("Key Usage=");
148 const string separator = " , ";
149 if (Support (KeyUsages.digitalSignature))
150 sb.Append ("Digital Signature");
151 if (Support (KeyUsages.nonRepudiation)) {
153 sb.Append (separator);
154 sb.Append ("Non-Repudiation");
156 if (Support (KeyUsages.keyEncipherment)) {
158 sb.Append (separator);
159 sb.Append ("Key Encipherment");
161 if (Support (KeyUsages.dataEncipherment)) {
163 sb.Append (separator);
164 sb.Append ("Data Encipherment");
166 if (Support (KeyUsages.keyAgreement)) {
168 sb.Append (separator);
169 sb.Append ("Key Agreement");
171 if (Support (KeyUsages.keyCertSign)) {
173 sb.Append (separator);
174 sb.Append ("Certificate Signing");
176 if (Support (KeyUsages.cRLSign)) {
178 sb.Append (separator);
179 sb.Append ("CRL Signing");
181 if (Support (KeyUsages.encipherOnly)) {
183 sb.Append (separator);
184 sb.Append ("Encipher Only "); // ???
186 if (Support (KeyUsages.decipherOnly)) {
188 sb.Append (separator);
189 sb.Append ("Decipher Only"); // ???
192 sb.Append (kubits.ToString ("X2", CultureInfo.InvariantCulture));
194 sb.Append (Environment.NewLine);
197 if (notBefore != DateTime.MinValue) {
198 sb.Append ("Not Before=");
199 sb.Append (notBefore.ToString (CultureInfo.CurrentUICulture));
200 sb.Append (Environment.NewLine);
202 if (notAfter != DateTime.MinValue) {
203 sb.Append ("Not After=");
204 sb.Append (notAfter.ToString (CultureInfo.CurrentUICulture));
205 sb.Append (Environment.NewLine);
207 return sb.ToString ();