2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509.Extensions / PrivateKeyUsagePeriodExtension.cs
1 //
2 // PrivateKeyUsagePeriodExtension.cs: Handles X.509 PrivateKeyUsagePeriod extensions.
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Novell (http://www.novell.com)
8 //
9
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 using System;
32 using System.Globalization;
33 using System.Text;
34
35 using Mono.Security;
36 using Mono.Security.X509;
37
38 namespace Mono.Security.X509.Extensions {
39
40         /*
41          * id-ce-privateKeyUsagePeriod OBJECT IDENTIFIER ::=  { id-ce 16 }
42          * 
43          * PrivateKeyUsagePeriod ::= SEQUENCE {
44          *    notBefore       [0]     GeneralizedTime OPTIONAL,
45          *    notAfter        [1]     GeneralizedTime OPTIONAL 
46          * }
47          */
48         public class PrivateKeyUsagePeriodExtension : X509Extension {
49
50                 private DateTime notBefore;
51                 private DateTime notAfter;
52
53                 public PrivateKeyUsagePeriodExtension () : base () 
54                 {
55                         extnOid = "2.5.29.16";
56                 }
57
58                 public PrivateKeyUsagePeriodExtension (ASN1 asn1) : base (asn1)
59                 {
60                 }
61
62                 public PrivateKeyUsagePeriodExtension (X509Extension extension) : base (extension)
63                 {
64                 }
65
66                 protected override void Decode () 
67                 {
68                         ASN1 sequence = new ASN1 (extnValue.Value);
69                         if (sequence.Tag != 0x30)
70                                 throw new ArgumentException ("Invalid PrivateKeyUsagePeriod extension");
71                         for (int i=0; i < sequence.Count; i++) {
72                                 switch (sequence [i].Tag) {
73                                         case 0x80:
74                                                 notBefore = ASN1Convert.ToDateTime (sequence [i]);
75                                                 break;
76                                         case 0x81:
77                                                 notAfter = ASN1Convert.ToDateTime (sequence [i]);
78                                                 break;
79                                         default:
80                                                 throw new ArgumentException ("Invalid PrivateKeyUsagePeriod extension");
81                                 }
82                         }
83                 }
84
85                 public override string Name {
86                         get { return "Private Key Usage Period"; }
87                 }
88
89                 public override string ToString () 
90                 {
91                         StringBuilder sb = new StringBuilder ();
92                         if (notBefore != DateTime.MinValue) {
93                                 sb.Append ("Not Before: ");
94                                 sb.Append (notBefore.ToString (CultureInfo.CurrentUICulture));
95                                 sb.Append (Environment.NewLine);
96                         }
97                         if (notAfter != DateTime.MinValue) {
98                                 sb.Append ("Not After: ");
99                                 sb.Append (notAfter.ToString (CultureInfo.CurrentUICulture));
100                                 sb.Append (Environment.NewLine);
101                         }
102                         return sb.ToString ();
103                 }
104         }
105 }