KickJava   Java API By Example, From Geeks To Geeks.

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


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.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.SimpleTimeZone JavaDoc;
26
27 /**
28  * UTC time object.
29  */

30 public class DERUTCTime
31     extends DERObject
32 {
33     String JavaDoc time;
34
35     /**
36      * return an UTC Time from the passed in object.
37      *
38      * @exception IllegalArgumentException if the object cannot be converted.
39      */

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

65     public static DERUTCTime getInstance(
66         ASN1TaggedObject obj,
67         boolean explicit)
68     {
69         return getInstance(obj.getObject());
70     }
71
72     /**
73      * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
74      * never encoded. When you're creating one of these objects from scratch, that's
75      * what you want to use, otherwise we'll try to deal with whatever gets read from
76      * the input stream... (this is why the input format is different from the getTime()
77      * method output).
78      * <p>
79      *
80      * @param time the time string.
81      */

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

91     public DERUTCTime(
92         Date JavaDoc time)
93     {
94         SimpleDateFormat JavaDoc dateF = new SimpleDateFormat JavaDoc("yyMMddHHmmss'Z'");
95
96         dateF.setTimeZone(new SimpleTimeZone JavaDoc(0,"Z"));
97
98         this.time = dateF.format(time);
99     }
100
101     DERUTCTime(
102         byte[] bytes)
103     {
104         //
105
// explicitly convert to characters
106
//
107
char[] dateC = new char[bytes.length];
108
109         for (int i = 0; i != dateC.length; i++)
110         {
111             dateC[i] = (char)(bytes[i] & 0xff);
112         }
113
114         this.time = new String JavaDoc(dateC);
115     }
116
117     /**
118      * return the time - always in the form of
119      * YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
120      * <p>
121      * Normally in a certificate we would expect "Z" rather than "GMT",
122      * however adding the "GMT" means we can just use:
123      * <pre>
124      * dateF = new SimpleDateFormat("yyMMddHHmmssz");
125      * </pre>
126      * To read in the time and get a date which is compatible with our local
127      * time zone.
128      * <p>
129      * <b>Note:</b> In some cases, due to the local date processing, this
130      * may lead to unexpected results. If you want to stick the normal
131      * convention of 1950 to 2049 use the getAdjustedTime() method.
132      */

133     public String JavaDoc getTime()
134     {
135         //
136
// standardise the format.
137
//
138
if (time.length() == 11)
139         {
140             return time.substring(0, 10) + "00GMT+00:00";
141         }
142         else if (time.length() == 13)
143         {
144             return time.substring(0, 12) + "GMT+00:00";
145         }
146         else if (time.length() == 17)
147         {
148             return time.substring(0, 12) + "GMT" + time.substring(12, 15) + ":" + time.substring(15, 17);
149         }
150
151         return time;
152     }
153
154     /**
155      * return the time as an adjusted date with a 4 digit year. This goes
156      * in the range of 1950 - 2049.
157      */

158     public String JavaDoc getAdjustedTime()
159     {
160         String JavaDoc d = this.getTime();
161
162         if (d.charAt(0) < '5')
163         {
164             return "20" + d;
165         }
166         else
167         {
168             return "19" + d;
169         }
170     }
171
172     private byte[] getOctets()
173     {
174         char[] cs = time.toCharArray();
175         byte[] bs = new byte[cs.length];
176
177         for (int i = 0; i != cs.length; i++)
178         {
179             bs[i] = (byte)cs[i];
180         }
181
182         return bs;
183     }
184
185     void encode(
186         DEROutputStream out)
187         throws IOException JavaDoc
188     {
189         out.writeEncoded(UTC_TIME, this.getOctets());
190     }
191
192     public boolean equals(
193         Object JavaDoc o)
194     {
195         if ((o == null) || !(o instanceof DERUTCTime))
196         {
197             return false;
198         }
199
200         return time.equals(((DERUTCTime)o).time);
201     }
202 }
203
Popular Tags