1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 * @author Vladimir N. Molotkov 20 * @version $Revision$ 21 */ 22 23 package org.apache.harmony.security.asn1; 24 25 import java.io.IOException; 26 import java.nio.charset.Charsets; 27 import java.text.SimpleDateFormat; 28 import java.util.TimeZone; 29 30 /** 31 * This class represents ASN.1 UTCTime type 32 * 33 * @see http://asn1.elibel.tm.fr/en/standards/index.htm 34 */ 35 public final class ASN1UTCTime extends ASN1Time { 36 37 /** Length for the pattern: YYMMDDhhmm'Z' */ 38 public static final int UTC_HM = 11; 39 40 /** Length for the pattern: YYMMDDhhmmss'Z' */ 41 public static final int UTC_HMS = 13; 42 43 /** Length for the pattern: YYMMDDhhmm('+'/'-')hhmm */ 44 public static final int UTC_LOCAL_HM = 15; 45 46 /** Length for the pattern: YYMMDDhhmmss('+'/'-')hhmm */ 47 public static final int UTC_LOCAL_HMS = 17; 48 49 /** default implementation */ 50 private static final ASN1UTCTime ASN1 = new ASN1UTCTime(); 51 52 /** 53 * Constructs ASN.1 UTCTime type 54 * 55 * The constructor is provided for inheritance purposes 56 * when there is a need to create a custom ASN.1 UTCTime type. 57 * To get a default implementation it is recommended to use 58 * getInstance() method. 59 */ 60 public ASN1UTCTime() { 61 super(TAG_UTCTIME); 62 } 63 64 /** 65 * Returns ASN.1 UTCTime type default implementation 66 * 67 * The default implementation works with encoding 68 * that is represented as Date object. 69 * 70 * @return ASN.1 UTCTime type default implementation 71 */ 72 public static ASN1UTCTime getInstance() { 73 return ASN1; 74 } 75 76 @Override public Object decode(BerInputStream in) throws IOException { 77 in.readUTCTime(); 78 79 if (in.isVerify) { 80 return null; 81 } 82 return getDecodedObject(in); 83 } 84 85 @Override public void encodeContent(BerOutputStream out) { 86 out.encodeUTCTime(); 87 } 88 89 // FIXME support only one format for encoding, do we need others? 90 // 91 // According to X.680 coordinated universal time format: 92 // two digit year, seconds always presented, 93 // no fractional-seconds elements, 'Z' at the end 94 private static final String UTC_PATTERN = "yyMMddHHmmss'Z'"; 95 96 @Override public void setEncodingContent(BerOutputStream out) { 97 SimpleDateFormat sdf = new SimpleDateFormat(UTC_PATTERN); 98 sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 99 out.content = sdf.format(out.content).getBytes(Charsets.UTF_8); 100 out.length = ((byte[]) out.content).length; 101 } 102 } 103