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.ASN1GeneralizedTime;
27import org.apache.harmony.security.asn1.ASN1Implicit;
28import org.apache.harmony.security.asn1.ASN1Sequence;
29import org.apache.harmony.security.asn1.ASN1Type;
30import org.apache.harmony.security.asn1.BerInputStream;
31
32/**
33 * The class encapsulates the ASN.1 DER encoding/decoding work
34 * with the following certificate extension (OID: 2.5.29.16)
35 * (as specified in RFC 3280 -
36 *  Internet X.509 Public Key Infrastructure.
37 *  Certificate and Certificate Revocation List (CRL) Profile.
38 *  http://www.ietf.org/rfc/rfc3280.txt):
39 *
40 * <pre>
41 * PrivateKeyUsagePeriod ::= SEQUENCE {
42 *      notBefore       [0]     GeneralizedTime OPTIONAL,
43 *      notAfter        [1]     GeneralizedTime OPTIONAL
44 * }
45 * </pre>
46 */
47public final class PrivateKeyUsagePeriod {
48    /** the value of notBeforeDate field of the structure */
49    private final Date notBeforeDate;
50    /** the value of notAfterDate field of the structure */
51    private final Date notAfterDate;
52    /** the ASN.1 encoded form of PrivateKeyUsagePeriod */
53    private byte[] encoding;
54
55    public PrivateKeyUsagePeriod(Date notBeforeDate, Date notAfterDate) {
56        this(notBeforeDate, notAfterDate, null);
57    }
58
59    private PrivateKeyUsagePeriod(Date notBeforeDate, Date notAfterDate, byte[] encoding) {
60        this.notBeforeDate = notBeforeDate;
61        this.notAfterDate = notAfterDate;
62        this.encoding = encoding;
63    }
64
65    /**
66     * Returns the value of notBefore field of the structure.
67     */
68    public Date getNotBefore() {
69        return notBeforeDate;
70    }
71
72    /**
73     * Returns the value of notAfter field of the structure.
74     */
75    public Date getNotAfter() {
76        return notAfterDate;
77    }
78
79    /**
80     * Returns ASN.1 encoded form of this X.509 PrivateKeyUsagePeriod value.
81     */
82    public byte[] getEncoded() {
83        if (encoding == null) {
84            encoding = ASN1.encode(this);
85        }
86        return encoding;
87    }
88
89    /**
90     * ASN.1 DER X.509 PrivateKeyUsagePeriod encoder/decoder class.
91     */
92    public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
93            new ASN1Implicit(0, ASN1GeneralizedTime.getInstance()),
94            new ASN1Implicit(1, ASN1GeneralizedTime.getInstance()) }) {
95        {
96            setOptional(0);
97            setOptional(1);
98        }
99
100        @Override protected Object getDecodedObject(BerInputStream in) {
101            Object[] values = (Object[])in.content;
102            return new PrivateKeyUsagePeriod((Date) values[0], (Date) values[1], in.getEncoded());
103        }
104
105        @Override protected void getValues(Object object, Object[] values) {
106            PrivateKeyUsagePeriod pkup = (PrivateKeyUsagePeriod) object;
107            values[0] = pkup.notBeforeDate;
108            values[1] = pkup.notAfterDate;
109        }
110    };
111}
112
113