blob: 977fce64867bd6554395ea45e2bf28a5e482550d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
package org.bouncycastle.asn1.cms;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
import org.bouncycastle.asn1.ASN1Choice;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.DERGeneralizedTime;
import org.bouncycastle.asn1.DERUTCTime;
/**
* <a href="http://tools.ietf.org/html/rfc5652#section-11.3">RFC 5652</a>:
* Dual-mode timestamp format producing either UTCTIme or GeneralizedTime.
* <p>
* <pre>
* Time ::= CHOICE {
* utcTime UTCTime,
* generalTime GeneralizedTime }
* </pre>
* <p>
* This has a constructor using java.util.Date for input which generates
* a {@link org.bouncycastle.asn1.DERUTCTime DERUTCTime} object if the
* supplied datetime is in range 1950-01-01-00:00:00 UTC until 2049-12-31-23:59:60 UTC.
* If the datetime value is outside that range, the generated object will be
* {@link org.bouncycastle.asn1.DERGeneralizedTime DERGeneralizedTime}.
*/
public class Time
extends ASN1Object
implements ASN1Choice
{
ASN1Primitive time;
public static Time getInstance(
ASN1TaggedObject obj,
boolean explicit)
{
return getInstance(obj.getObject());
}
/**
* @deprecated use getInstance()
*/
public Time(
ASN1Primitive time)
{
if (!(time instanceof DERUTCTime)
&& !(time instanceof DERGeneralizedTime))
{
throw new IllegalArgumentException("unknown object passed to Time");
}
this.time = time;
}
/**
* Create a time object from a given date - if the year is in between 1950
* and 2049 a UTCTime object is generated, otherwise a GeneralizedTime
* is used.
*/
public Time(
Date date)
{
SimpleTimeZone tz = new SimpleTimeZone(0, "Z");
SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss");
dateF.setTimeZone(tz);
String d = dateF.format(date) + "Z";
int year = Integer.parseInt(d.substring(0, 4));
if (year < 1950 || year > 2049)
{
time = new DERGeneralizedTime(d);
}
else
{
time = new DERUTCTime(d.substring(2));
}
}
/**
* Return a Time object from the given object.
* <p>
* Accepted inputs:
* <ul>
* <li> null → null
* <li> {@link Time} object
* <li> {@link org.bouncycastle.asn1.DERUTCTime DERUTCTime} object
* <li> {@link org.bouncycastle.asn1.DERGeneralizedTime DERGeneralizedTime} object
* </ul>
*
* @param obj the object we want converted.
* @exception IllegalArgumentException if the object cannot be converted.
*/
public static Time getInstance(
Object obj)
{
if (obj == null || obj instanceof Time)
{
return (Time)obj;
}
else if (obj instanceof DERUTCTime)
{
return new Time((DERUTCTime)obj);
}
else if (obj instanceof DERGeneralizedTime)
{
return new Time((DERGeneralizedTime)obj);
}
throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
}
/**
* Get the date+tine as a String in full form century format.
*/
public String getTime()
{
if (time instanceof DERUTCTime)
{
return ((DERUTCTime)time).getAdjustedTime();
}
else
{
return ((DERGeneralizedTime)time).getTime();
}
}
/**
* Get java.util.Date version of date+time.
*/
public Date getDate()
{
try
{
if (time instanceof DERUTCTime)
{
return ((DERUTCTime)time).getAdjustedDate();
}
else
{
return ((DERGeneralizedTime)time).getDate();
}
}
catch (ParseException e)
{ // this should never happen
throw new IllegalStateException("invalid date string: " + e.getMessage());
}
}
/**
* Produce an object suitable for an ASN1OutputStream.
*/
public ASN1Primitive toASN1Primitive()
{
return time;
}
}
|