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.StandardCharsets; 27 import java.text.SimpleDateFormat; 28 import java.util.Locale; 29 import java.util.TimeZone; 30 31 /** 32 * This class represents ASN.1 UTCTime type 33 * 34 * @see http://asn1.elibel.tm.fr/en/standards/index.htm 35 */ 36 public final class ASN1UTCTime extends ASN1Time { 37 38 /** Length for the pattern: YYMMDDhhmm'Z' */ 39 public static final int UTC_HM = 11; 40 41 /** Length for the pattern: YYMMDDhhmmss'Z' */ 42 public static final int UTC_HMS = 13; 43 44 /** Length for the pattern: YYMMDDhhmm('+'/'-')hhmm */ 45 public static final int UTC_LOCAL_HM = 15; 46 47 /** Length for the pattern: YYMMDDhhmmss('+'/'-')hhmm */ 48 public static final int UTC_LOCAL_HMS = 17; 49 50 /** default implementation */ 51 private static final ASN1UTCTime ASN1 = new ASN1UTCTime(); 52 53 /** 54 * Constructs ASN.1 UTCTime type 55 * 56 * The constructor is provided for inheritance purposes 57 * when there is a need to create a custom ASN.1 UTCTime type. 58 * To get a default implementation it is recommended to use 59 * getInstance() method. 60 */ 61 public ASN1UTCTime() { 62 super(TAG_UTCTIME); 63 } 64 65 /** 66 * Returns ASN.1 UTCTime type default implementation 67 * 68 * The default implementation works with encoding 69 * that is represented as Date object. 70 * 71 * @return ASN.1 UTCTime type default implementation 72 */ 73 public static ASN1UTCTime getInstance() { 74 return ASN1; 75 } 76 77 @Override public Object decode(BerInputStream in) throws IOException { 78 in.readUTCTime(); 79 80 if (in.isVerify) { 81 return null; 82 } 83 return getDecodedObject(in); 84 } 85 86 @Override public void encodeContent(BerOutputStream out) { 87 out.encodeUTCTime(); 88 } 89 90 // FIXME support only one format for encoding, do we need others? 91 // 92 // According to X.680 coordinated universal time format: 93 // two digit year, seconds always presented, 94 // no fractional-seconds elements, 'Z' at the end 95 private static final String UTC_PATTERN = "yyMMddHHmmss'Z'"; 96 97 @Override public void setEncodingContent(BerOutputStream out) { 98 SimpleDateFormat sdf = new SimpleDateFormat(UTC_PATTERN, Locale.US); 99 sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 100 out.content = sdf.format(out.content).getBytes(StandardCharsets.UTF_8); 101 out.length = ((byte[]) out.content).length; 102 } 103 } 104