1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.text.ParseException;
4c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.text.SimpleDateFormat;
5c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.Date;
6c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.SimpleTimeZone;
7c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
84c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Choice;
94c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Object;
104c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Primitive;
114c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1TaggedObject;
124c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.DERGeneralizedTime;
134c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.DERUTCTime;
144c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class Time
164c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Object
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements ASN1Choice
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
194c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    ASN1Primitive time;
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static Time getInstance(
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return getInstance(obj.getObject()); // must be explicitly tagged
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public Time(
294c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1Primitive   time)
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!(time instanceof DERUTCTime)
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            && !(time instanceof DERGeneralizedTime))
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new IllegalArgumentException("unknown object passed to Time");
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.time = time;
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * creates a time object from a given date - if the date is between 1950
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * and 2049 a UTCTime object is generated, otherwise a GeneralizedTime
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * is used.
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public Time(
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Date    date)
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        SimpleTimeZone      tz = new SimpleTimeZone(0, "Z");
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        SimpleDateFormat    dateF = new SimpleDateFormat("yyyyMMddHHmmss");
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        dateF.setTimeZone(tz);
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        String  d = dateF.format(date) + "Z";
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int     year = Integer.parseInt(d.substring(0, 4));
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (year < 1950 || year > 2049)
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            time = new DERGeneralizedTime(d);
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            time = new DERUTCTime(d.substring(2));
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static Time getInstance(
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
696e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        if (obj == null || obj instanceof Time)
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (Time)obj;
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else if (obj instanceof DERUTCTime)
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new Time((DERUTCTime)obj);
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else if (obj instanceof DERGeneralizedTime)
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new Time((DERGeneralizedTime)obj);
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
82c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String getTime()
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (time instanceof DERUTCTime)
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return ((DERUTCTime)time).getAdjustedTime();
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return ((DERGeneralizedTime)time).getTime();
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public Date getDate()
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
99c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        try
100c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
101c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (time instanceof DERUTCTime)
102c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
103c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return ((DERUTCTime)time).getAdjustedDate();
104c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
105c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            else
106c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
107c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return ((DERGeneralizedTime)time).getDate();
108c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
109c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
110c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        catch (ParseException e)
111c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {         // this should never happen
112c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            throw new IllegalStateException("invalid date string: " + e.getMessage());
113c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Produce an object suitable for an ASN1OutputStream.
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * <pre>
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Time ::= CHOICE {
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *             utcTime        UTCTime,
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *             generalTime    GeneralizedTime }
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * </pre>
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
1244c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Primitive toASN1Primitive()
125b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return time;
127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
128c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
129c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public String toString()
130c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
131c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return getTime();
132c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
133b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
134