KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > security > cert > extensions > PrivateKeyUsagePeriod


1
2 package com.ca.commons.security.cert.extensions;
3
4 import com.ca.commons.security.asn1.*;
5
6 import java.util.Date JavaDoc;
7 import java.text.SimpleDateFormat JavaDoc;
8
9 /**
10  * <pre>
11  * PrivateKeyUsagePeriod ::= SEQUENCE {
12  * notBefore [0] GeneralizedTime OPTIONAL,
13  * notAfter [1] GeneralizedTime OPTIONAL }
14  * </pre>
15  *
16  * @author vbui
17  */

18 public class PrivateKeyUsagePeriod implements V3Extension
19 {
20     String JavaDoc value = null;
21
22     public void init(ASN1Object asn1object) throws Exception JavaDoc
23     {
24         if (!asn1object.isASN1Type(ASN1Type.SEQUENCE))
25             throw new Exception JavaDoc("Wrong ASN.1 type for PrivateKeyUsagePeriod");
26
27         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
28         SimpleDateFormat JavaDoc printer = new SimpleDateFormat JavaDoc("MMM d, yyyy h:mm:ss a");
29
30         String JavaDoc dateString = null;
31
32         for (int i = 0; i < asn1object.size(); i++)
33         {
34             ASN1Object nextComp = (ASN1Object) asn1object.getComponent(i).getValue();
35
36             if (nextComp.isASN1Type(ASN1Type.OCTET_STRING) && i == 0)
37             {
38                 String JavaDoc notBefore = new String JavaDoc((byte[])nextComp.getValue());
39                 try
40                 {
41                     Date JavaDoc hotdate = formatter.parse(notBefore);
42                     value = "Not Before: " + printer.format(hotdate);
43                 }
44                 catch (Exception JavaDoc ex)
45                 {
46                     ex.printStackTrace();
47                     value = "Not Before: invalid date";
48                 }
49             }
50             else if (nextComp.isASN1Type(ASN1Type.OCTET_STRING) && i == 1)
51             {
52                 String JavaDoc notAfter = new String JavaDoc((byte[])nextComp.getValue());
53                 try
54                 {
55                     Date JavaDoc hotdate = formatter.parse(notAfter);
56                     value = value + "\nNot After: " + printer.format(hotdate);
57                 }
58                 catch (Exception JavaDoc ex)
59                 {
60                     ex.printStackTrace();
61                     value = value + "\nNot After: invalid date";
62                 }
63             }
64         }
65     }
66
67     public String JavaDoc toString()
68     {
69         return value;
70     }
71 }
72
73
Popular Tags