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 Alexander Y. Kleymenov
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.x509;
24
25import java.util.Date;
26import org.apache.harmony.security.asn1.ASN1Sequence;
27import org.apache.harmony.security.asn1.ASN1Type;
28import org.apache.harmony.security.asn1.BerInputStream;
29
30/**
31 * The class encapsulates the ASN.1 DER encoding/decoding work
32 * with Validity structure which is the part of X.509 certificate
33 * (as specified in RFC 3280 -
34 *  Internet X.509 Public Key Infrastructure.
35 *  Certificate and Certificate Revocation List (CRL) Profile.
36 *  http://www.ietf.org/rfc/rfc3280.txt):
37 *
38 * <pre>
39 *  Validity ::= SEQUENCE {
40 *       notBefore      Time,
41 *       notAfter       Time
42 *  }
43 * </pre>
44 */
45public final class Validity {
46    /** the value of notBefore field of the structure */
47    private final Date notBefore;
48    /** the value of notAfter field of the structure */
49    private final Date notAfter;
50    /** the ASN.1 encoded form of Validity */
51    private byte[] encoding;
52
53    public Validity(Date notBefore, Date notAfter) {
54        this.notBefore = notBefore;
55        this.notAfter = notAfter;
56    }
57
58    /**
59     * Returns the value of notBefore field of the structure.
60     */
61    public Date getNotBefore() {
62        return notBefore;
63    }
64
65    /**
66     * Returns the value of notAfter field of the structure.
67     */
68    public Date getNotAfter() {
69        return notAfter;
70    }
71
72    /**
73     * Returns ASN.1 encoded form of this X.509 Validity value.
74     */
75    public byte[] getEncoded() {
76        if (encoding == null) {
77            encoding = ASN1.encode(this);
78        }
79        return encoding;
80    }
81
82    /**
83     * ASN.1 DER X.509 Validity encoder/decoder class.
84     */
85    public static final ASN1Sequence ASN1
86        = new ASN1Sequence(new ASN1Type[] {Time.ASN1, Time.ASN1 }) {
87
88        @Override protected Object getDecodedObject(BerInputStream in) {
89            Object[] values = (Object[]) in.content;
90            return new Validity((Date) values[0], (Date) values[1]);
91        }
92
93        @Override protected void getValues(Object object, Object[] values) {
94            Validity validity = (Validity) object;
95            values[0] = validity.notBefore;
96            values[1] = validity.notAfter;
97        }
98    };
99}
100