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