001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.crypto.asn1.x509;
019
020 import java.text.ParsePosition;
021 import java.text.SimpleDateFormat;
022 import java.util.Date;
023 import java.util.SimpleTimeZone;
024
025 import org.apache.geronimo.crypto.asn1.ASN1Choice;
026 import org.apache.geronimo.crypto.asn1.ASN1Encodable;
027 import org.apache.geronimo.crypto.asn1.ASN1TaggedObject;
028 import org.apache.geronimo.crypto.asn1.DERGeneralizedTime;
029 import org.apache.geronimo.crypto.asn1.DERObject;
030 import org.apache.geronimo.crypto.asn1.DERUTCTime;
031
032 public class Time
033 extends ASN1Encodable
034 implements ASN1Choice
035 {
036 DERObject time;
037
038 public static Time getInstance(
039 ASN1TaggedObject obj,
040 boolean explicit)
041 {
042 return getInstance(obj.getObject()); // must be explicitly tagged
043 }
044
045 public Time(
046 DERObject time)
047 {
048 if (!(time instanceof DERUTCTime)
049 && !(time instanceof DERGeneralizedTime))
050 {
051 throw new IllegalArgumentException("unknown object passed to Time");
052 }
053
054 this.time = time;
055 }
056
057 /**
058 * creates a time object from a given date - if the date is between 1950
059 * and 2049 a UTCTime object is generated, otherwise a GeneralizedTime
060 * is used.
061 */
062 public Time(
063 Date date)
064 {
065 SimpleTimeZone tz = new SimpleTimeZone(0, "Z");
066 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss");
067
068 dateF.setTimeZone(tz);
069
070 String d = dateF.format(date) + "Z";
071 int year = Integer.parseInt(d.substring(0, 4));
072
073 if (year < 1950 || year > 2049)
074 {
075 time = new DERGeneralizedTime(d);
076 }
077 else
078 {
079 time = new DERUTCTime(d.substring(2));
080 }
081 }
082
083 public static Time getInstance(
084 Object obj)
085 {
086 if (obj instanceof Time)
087 {
088 return (Time)obj;
089 }
090 else if (obj instanceof DERUTCTime)
091 {
092 return new Time((DERUTCTime)obj);
093 }
094 else if (obj instanceof DERGeneralizedTime)
095 {
096 return new Time((DERGeneralizedTime)obj);
097 }
098
099 throw new IllegalArgumentException("unknown object in factory");
100 }
101
102 public String getTime()
103 {
104 if (time instanceof DERUTCTime)
105 {
106 return ((DERUTCTime)time).getAdjustedTime();
107 }
108 else
109 {
110 return ((DERGeneralizedTime)time).getTime();
111 }
112 }
113
114 public Date getDate()
115 {
116 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
117
118 return dateF.parse(this.getTime(), new ParsePosition(0));
119 }
120
121 /**
122 * Produce an object suitable for an ASN1OutputStream.
123 * <pre>
124 * Time ::= CHOICE {
125 * utcTime UTCTime,
126 * generalTime GeneralizedTime }
127 * </pre>
128 */
129 public DERObject toASN1Object()
130 {
131 return time;
132 }
133 }