KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > DERGeneralizedTime


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1;
21
22 import java.io.IOException JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.SimpleTimeZone JavaDoc;
27
28 /**
29  * Generalized time object.
30  */

31 public class DERGeneralizedTime
32     extends DERObject
33 {
34     String JavaDoc time;
35
36     /**
37      * return a generalized time from the passed in object
38      *
39      * @exception IllegalArgumentException if the object cannot be converted.
40      */

41     public static DERGeneralizedTime getInstance(
42         Object JavaDoc obj)
43     {
44         if (obj == null || obj instanceof DERGeneralizedTime)
45         {
46             return (DERGeneralizedTime)obj;
47         }
48
49         if (obj instanceof ASN1OctetString)
50         {
51             return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets());
52         }
53
54         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
55     }
56
57     /**
58      * return a Generalized Time object from a tagged object.
59      *
60      * @param obj the tagged object holding the object we want
61      * @param explicit true if the object is meant to be explicitly
62      * tagged false otherwise.
63      * @exception IllegalArgumentException if the tagged object cannot
64      * be converted.
65      */

66     public static DERGeneralizedTime getInstance(
67         ASN1TaggedObject obj,
68         boolean explicit)
69     {
70         return getInstance(obj.getObject());
71     }
72
73     /**
74      * The correct format for this is YYYYMMDDHHMMSSZ, or without the Z
75      * for local time, or Z+-HHMM on the end, for difference between local
76      * time and UTC time.
77      * <p>
78      *
79      * @param time the time string.
80      */

81     public DERGeneralizedTime(
82         String JavaDoc time)
83     {
84         this.time = time;
85     }
86
87     /**
88      * base constructer from a java.util.date object
89      */

90     public DERGeneralizedTime(
91         Date JavaDoc time)
92     {
93         SimpleDateFormat JavaDoc dateF = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss'Z'");
94
95         dateF.setTimeZone(new SimpleTimeZone JavaDoc(0,"Z"));
96
97         this.time = dateF.format(time);
98     }
99
100     DERGeneralizedTime(
101         byte[] bytes)
102     {
103         //
104
// explicitly convert to characters
105
//
106
char[] dateC = new char[bytes.length];
107
108         for (int i = 0; i != dateC.length; i++)
109         {
110             dateC[i] = (char)(bytes[i] & 0xff);
111         }
112
113         this.time = new String JavaDoc(dateC);
114     }
115
116     /**
117      * return the time - always in the form of
118      * YYYYMMDDhhmmssGMT(+hh:mm|-hh:mm).
119      * <p>
120      * Normally in a certificate we would expect "Z" rather than "GMT",
121      * however adding the "GMT" means we can just use:
122      * <pre>
123      * dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
124      * </pre>
125      * To read in the time and get a date which is compatible with our local
126      * time zone.
127      */

128     public String JavaDoc getTime()
129     {
130         //
131
// standardise the format.
132
//
133
if (time.charAt(time.length() - 1) == 'Z')
134         {
135             return time.substring(0, time.length() - 1) + "GMT+00:00";
136         }
137         else
138         {
139             int signPos = time.length() - 5;
140             char sign = time.charAt(signPos);
141             if (sign == '-' || sign == '+')
142             {
143                 return time.substring(0, signPos)
144                     + "GMT"
145                     + time.substring(signPos, signPos + 3)
146                     + ":"
147                     + time.substring(signPos + 3);
148             }
149             else
150             {
151                 signPos = time.length() - 3;
152                 sign = time.charAt(signPos);
153                 if (sign == '-' || sign == '+')
154                 {
155                     return time.substring(0, signPos)
156                         + "GMT"
157                         + time.substring(signPos)
158                         + ":00";
159                 }
160             }
161         }
162
163         return time;
164     }
165
166     public Date JavaDoc getDate()
167         throws ParseException JavaDoc
168     {
169         SimpleDateFormat JavaDoc dateF = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss'Z'");
170
171         dateF.setTimeZone(new SimpleTimeZone JavaDoc(0, "Z"));
172
173         return dateF.parse(time);
174     }
175
176     private byte[] getOctets()
177     {
178         char[] cs = time.toCharArray();
179         byte[] bs = new byte[cs.length];
180
181         for (int i = 0; i != cs.length; i++)
182         {
183             bs[i] = (byte)cs[i];
184         }
185
186         return bs;
187     }
188
189
190     void encode(
191         DEROutputStream out)
192         throws IOException JavaDoc
193     {
194         out.writeEncoded(GENERALIZED_TIME, this.getOctets());
195     }
196
197     public boolean equals(
198         Object JavaDoc o)
199     {
200         if ((o == null) || !(o instanceof DERGeneralizedTime))
201         {
202             return false;
203         }
204
205         return time.equals(((DERGeneralizedTime)o).time);
206     }
207 }
208
Popular Tags