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.Charsets; 27 import java.text.SimpleDateFormat; 28 import java.util.TimeZone; 29 30 /** 31 * This class represents ASN.1 GeneralizedTime type. 32 * 33 * @see http://asn1.elibel.tm.fr/en/standards/index.htm 34 */ 35 36 public 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 // 66 // 67 // Decode 68 // 69 // 70 71 public Object decode(BerInputStream in) throws IOException { 72 in.readGeneralizedTime(); 73 74 if (in.isVerify) { 75 return null; 76 } 77 return getDecodedObject(in); 78 } 79 80 // 81 // 82 // Encode 83 // 84 // 85 86 public void encodeContent(BerOutputStream out) { 87 out.encodeGeneralizedTime(); 88 } 89 90 // FIXME support only one format for encoding, do we need others? 91 // 92 // According to X.680: 93 // four digit year, seconds always presented 94 // and fractional-seconds elements without 95 // trailing 0's (must be cut later from content) 96 private final static String GEN_PATTERN = "yyyyMMddHHmmss.SSS"; 97 98 public void setEncodingContent(BerOutputStream out) { 99 100 SimpleDateFormat sdf = new SimpleDateFormat(GEN_PATTERN); 101 sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 102 String temp = sdf.format(out.content); 103 // cut off trailing 0s 104 int nullId; 105 int currLength; 106 while (((nullId = temp.lastIndexOf('0', currLength = temp.length() - 1)) != -1) 107 & (nullId == currLength)) { 108 temp = temp.substring(0, nullId); 109 } 110 // deal with point (cut off if it is last char) 111 if (temp.charAt(currLength) == '.') { 112 temp = temp.substring(0, currLength); 113 } 114 115 out.content = (temp + "Z").getBytes(Charsets.UTF_8); 116 out.length = ((byte[]) out.content).length; 117 } 118 } 119