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