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: */package org.apache.geronimo.crypto.asn1;
017:
018: import java.io.IOException;
019: import java.text.ParseException;
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022: import java.util.SimpleTimeZone;
023:
024: /**
025: * Generalized time object.
026: */
027: public class DERGeneralizedTime extends DERObject {
028: String time;
029:
030: /**
031: * return a generalized time from the passed in object
032: *
033: * @exception IllegalArgumentException if the object cannot be converted.
034: */
035: public static DERGeneralizedTime getInstance(Object obj) {
036: if (obj == null || obj instanceof DERGeneralizedTime) {
037: return (DERGeneralizedTime) obj;
038: }
039:
040: if (obj instanceof ASN1OctetString) {
041: return new DERGeneralizedTime(((ASN1OctetString) obj)
042: .getOctets());
043: }
044:
045: throw new IllegalArgumentException(
046: "illegal object in getInstance: "
047: + obj.getClass().getName());
048: }
049:
050: /**
051: * return a Generalized Time object from a tagged object.
052: *
053: * @param obj the tagged object holding the object we want
054: * @param explicit true if the object is meant to be explicitly
055: * tagged false otherwise.
056: * @exception IllegalArgumentException if the tagged object cannot
057: * be converted.
058: */
059: public static DERGeneralizedTime getInstance(ASN1TaggedObject obj,
060: boolean explicit) {
061: return getInstance(obj.getObject());
062: }
063:
064: /**
065: * The correct format for this is YYYYMMDDHHMMSSZ, or without the Z
066: * for local time, or Z+-HHMM on the end, for difference between local
067: * time and UTC time.
068: * <p>
069: *
070: * @param time the time string.
071: */
072: public DERGeneralizedTime(String time) {
073: this .time = time;
074: }
075:
076: /**
077: * base constructer from a java.util.date object
078: */
079: public DERGeneralizedTime(Date time) {
080: SimpleDateFormat dateF = new SimpleDateFormat(
081: "yyyyMMddHHmmss'Z'");
082:
083: dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
084:
085: this .time = dateF.format(time);
086: }
087:
088: DERGeneralizedTime(byte[] bytes) {
089: //
090: // explicitly convert to characters
091: //
092: char[] dateC = new char[bytes.length];
093:
094: for (int i = 0; i != dateC.length; i++) {
095: dateC[i] = (char) (bytes[i] & 0xff);
096: }
097:
098: this .time = new String(dateC);
099: }
100:
101: /**
102: * return the time - always in the form of
103: * YYYYMMDDhhmmssGMT(+hh:mm|-hh:mm).
104: * <p>
105: * Normally in a certificate we would expect "Z" rather than "GMT",
106: * however adding the "GMT" means we can just use:
107: * <pre>
108: * dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
109: * </pre>
110: * To read in the time and get a date which is compatible with our local
111: * time zone.
112: */
113: public String getTime() {
114: //
115: // standardise the format.
116: //
117: if (time.charAt(time.length() - 1) == 'Z') {
118: return time.substring(0, time.length() - 1) + "GMT+00:00";
119: } else {
120: int signPos = time.length() - 5;
121: char sign = time.charAt(signPos);
122: if (sign == '-' || sign == '+') {
123: return time.substring(0, signPos) + "GMT"
124: + time.substring(signPos, signPos + 3) + ":"
125: + time.substring(signPos + 3);
126: } else {
127: signPos = time.length() - 3;
128: sign = time.charAt(signPos);
129: if (sign == '-' || sign == '+') {
130: return time.substring(0, signPos) + "GMT"
131: + time.substring(signPos) + ":00";
132: }
133: }
134: }
135:
136: return time;
137: }
138:
139: public Date getDate() throws ParseException {
140: SimpleDateFormat dateF;
141:
142: if (time.indexOf('.') == 14) {
143: dateF = new SimpleDateFormat("yyyyMMddHHmmss.SSS'Z'");
144: } else {
145: dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
146: }
147:
148: dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
149:
150: return dateF.parse(time);
151: }
152:
153: private byte[] getOctets() {
154: char[] cs = time.toCharArray();
155: byte[] bs = new byte[cs.length];
156:
157: for (int i = 0; i != cs.length; i++) {
158: bs[i] = (byte) cs[i];
159: }
160:
161: return bs;
162: }
163:
164: void encode(DEROutputStream out) throws IOException {
165: out.writeEncoded(GENERALIZED_TIME, this .getOctets());
166: }
167:
168: public boolean equals(Object o) {
169: if ((o == null) || !(o instanceof DERGeneralizedTime)) {
170: return false;
171: }
172:
173: return time.equals(((DERGeneralizedTime) o).time);
174: }
175:
176: public int hashCode() {
177: return time.hashCode();
178: }
179: }
|