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.SimpleDateFormat;
020: import java.util.Date;
021: import java.util.SimpleTimeZone;
022:
023: /**
024: * UTC time object.
025: */
026: public class DERUTCTime extends DERObject {
027: String time;
028:
029: /**
030: * return an UTC Time from the passed in object.
031: *
032: * @exception IllegalArgumentException if the object cannot be converted.
033: */
034: public static DERUTCTime getInstance(Object obj) {
035: if (obj == null || obj instanceof DERUTCTime) {
036: return (DERUTCTime) obj;
037: }
038:
039: if (obj instanceof ASN1OctetString) {
040: return new DERUTCTime(((ASN1OctetString) obj).getOctets());
041: }
042:
043: throw new IllegalArgumentException(
044: "illegal object in getInstance: "
045: + obj.getClass().getName());
046: }
047:
048: /**
049: * return an UTC Time from a tagged object.
050: *
051: * @param obj the tagged object holding the object we want
052: * @param explicit true if the object is meant to be explicitly
053: * tagged false otherwise.
054: * @exception IllegalArgumentException if the tagged object cannot
055: * be converted.
056: */
057: public static DERUTCTime getInstance(ASN1TaggedObject obj,
058: boolean explicit) {
059: return getInstance(obj.getObject());
060: }
061:
062: /**
063: * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
064: * never encoded. When you're creating one of these objects from scratch, that's
065: * what you want to use, otherwise we'll try to deal with whatever gets read from
066: * the input stream... (this is why the input format is different from the getTime()
067: * method output).
068: * <p>
069: *
070: * @param time the time string.
071: */
072: public DERUTCTime(String time) {
073: this .time = time;
074: }
075:
076: /**
077: * base constructer from a java.util.date object
078: */
079: public DERUTCTime(Date time) {
080: SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmss'Z'");
081:
082: dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
083:
084: this .time = dateF.format(time);
085: }
086:
087: DERUTCTime(byte[] bytes) {
088: //
089: // explicitly convert to characters
090: //
091: char[] dateC = new char[bytes.length];
092:
093: for (int i = 0; i != dateC.length; i++) {
094: dateC[i] = (char) (bytes[i] & 0xff);
095: }
096:
097: this .time = new String(dateC);
098: }
099:
100: /**
101: * return the time - always in the form of
102: * YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
103: * <p>
104: * Normally in a certificate we would expect "Z" rather than "GMT",
105: * however adding the "GMT" means we can just use:
106: * <pre>
107: * dateF = new SimpleDateFormat("yyMMddHHmmssz");
108: * </pre>
109: * To read in the time and get a date which is compatible with our local
110: * time zone.
111: * <p>
112: * <b>Note:</b> In some cases, due to the local date processing, this
113: * may lead to unexpected results. If you want to stick the normal
114: * convention of 1950 to 2049 use the getAdjustedTime() method.
115: */
116: public String getTime() {
117: //
118: // standardise the format.
119: //
120: if (time.length() == 11) {
121: return time.substring(0, 10) + "00GMT+00:00";
122: } else if (time.length() == 13) {
123: return time.substring(0, 12) + "GMT+00:00";
124: } else if (time.length() == 17) {
125: return time.substring(0, 12) + "GMT"
126: + time.substring(12, 15) + ":"
127: + time.substring(15, 17);
128: }
129:
130: return time;
131: }
132:
133: /**
134: * return the time as an adjusted date with a 4 digit year. This goes
135: * in the range of 1950 - 2049.
136: */
137: public String getAdjustedTime() {
138: String d = this .getTime();
139:
140: if (d.charAt(0) < '5') {
141: return "20" + d;
142: } else {
143: return "19" + d;
144: }
145: }
146:
147: private byte[] getOctets() {
148: char[] cs = time.toCharArray();
149: byte[] bs = new byte[cs.length];
150:
151: for (int i = 0; i != cs.length; i++) {
152: bs[i] = (byte) cs[i];
153: }
154:
155: return bs;
156: }
157:
158: void encode(DEROutputStream out) throws IOException {
159: out.writeEncoded(UTC_TIME, this .getOctets());
160: }
161:
162: public boolean equals(Object o) {
163: if ((o == null) || !(o instanceof DERUTCTime)) {
164: return false;
165: }
166:
167: return time.equals(((DERUTCTime) o).time);
168: }
169:
170: public int hashCode() {
171: return time.hashCode();
172: }
173: }
|