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, Stepan M. Mishura 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 GeneralizedTime type. 33 * 34 * @see http://asn1.elibel.tm.fr/en/standards/index.htm 35 */ 36 public final class ASN1GeneralizedTime extends ASN1Time { 37 38 // default implementation 39 private static final ASN1GeneralizedTime ASN1 = new ASN1GeneralizedTime(); 40 41 /** 42 * Constructs ASN.1 GeneralizedTime type 43 * 44 * The constructor is provided for inheritance purposes 45 * when there is a need to create a custom ASN.1 GeneralizedTime type. 46 * To get a default implementation it is recommended to use 47 * getInstance() method. 48 */ 49 public ASN1GeneralizedTime() { 50 super(TAG_GENERALIZEDTIME); 51 } 52 53 /** 54 * Returns ASN.1 GeneralizedTime type default implementation 55 * 56 * The default implementation works with encoding 57 * that is represented as Date object. 58 * 59 * @return ASN.1 GeneralizedTime type default implementation 60 */ 61 public static ASN1GeneralizedTime getInstance() { 62 return ASN1; 63 } 64 65 public Object decode(BerInputStream in) throws IOException { 66 in.readGeneralizedTime(); 67 68 if (in.isVerify) { 69 return null; 70 } 71 return getDecodedObject(in); 72 } 73 74 public void encodeContent(BerOutputStream out) { 75 out.encodeGeneralizedTime(); 76 } 77 78 // FIXME support only one format for encoding, do we need others? 79 // 80 // According to X.680: 81 // four digit year, seconds always presented 82 // and fractional-seconds elements without 83 // trailing 0's (must be cut later from content) 84 private static final String GEN_PATTERN = "yyyyMMddHHmmss.SSS"; 85 86 public void setEncodingContent(BerOutputStream out) { 87 SimpleDateFormat sdf = new SimpleDateFormat(GEN_PATTERN, Locale.US); 88 sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 89 String temp = sdf.format(out.content); 90 // cut off trailing 0s 91 int nullId; 92 int currLength; 93 while (((nullId = temp.lastIndexOf('0', currLength = temp.length() - 1)) != -1) 94 & (nullId == currLength)) { 95 temp = temp.substring(0, nullId); 96 } 97 // deal with point (cut off if it is last char) 98 if (temp.charAt(currLength) == '.') { 99 temp = temp.substring(0, currLength); 100 } 101 102 out.content = (temp + "Z").getBytes(StandardCharsets.UTF_8); 103 out.length = ((byte[]) out.content).length; 104 } 105 } 106