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, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17
18package org.apache.harmony.security.pkcs10;
19
20import org.apache.harmony.security.asn1.ASN1BitString;
21import org.apache.harmony.security.asn1.ASN1Sequence;
22import org.apache.harmony.security.asn1.ASN1Type;
23import org.apache.harmony.security.asn1.BerInputStream;
24import org.apache.harmony.security.asn1.BitString;
25import org.apache.harmony.security.x509.AlgorithmIdentifier;
26
27/**
28 * The class implements the ASN.1 DER encoding and decoding of the PKCS#10
29 * Certificate Signing Request (CSR). Its ASN notation is as follows:
30 *
31 * CertificationRequest ::= SEQUENCE {
32 *   certificationRequestInfo CertificationRequestInfo,
33 *   signatureAlgorithm SignatureAlgorithmIdentifier,
34 *   signature Signature
35 * }
36 *
37 * SignatureAlgorithmIdentifier ::= AlgorithmIdentifier
38 *
39 * Signature ::= BIT STRING
40 */
41public final class CertificationRequest {
42
43    /** the value of certificationRequestInfo field of the structure */
44    private CertificationRequestInfo info;
45
46    /** the value of signatureAlgorithm field of the structure */
47    private AlgorithmIdentifier algId;
48
49    /** the value of signature field of the structure */
50    private byte[] signature;
51
52    /** the ASN.1 encoded form of CertificationRequest */
53    private byte[] encoding;
54
55    public CertificationRequest(CertificationRequestInfo info,
56            AlgorithmIdentifier algId, byte[] signature) {
57        this.info = info;
58        this.algId = algId;
59        this.signature = signature.clone();
60    }
61
62    private CertificationRequest(CertificationRequestInfo info,
63            AlgorithmIdentifier algId, byte[] signature, byte[] encoding) {
64        this(info, algId, signature);
65        this.encoding = encoding;
66    }
67
68    public CertificationRequestInfo getInfo() {
69        return info;
70    }
71
72    public byte[] getSignature() {
73        byte[] result = new byte[signature.length];
74        System.arraycopy(signature, 0, result, 0, signature.length);
75        return result;
76    }
77
78    /**
79     * Returns ASN.1 encoded form of this CertificationRequest value.
80     * @return a byte array containing ASN.1 encode form.
81     */
82    public byte[] getEncoded() {
83        if (encoding == null) {
84            encoding = CertificationRequest.ASN1.encode(this);
85        }
86        return encoding;
87    }
88
89    public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
90            CertificationRequestInfo.ASN1,  // info
91            AlgorithmIdentifier.ASN1,       // signatureAlgorithm
92            ASN1BitString.getInstance() })  // signature
93    {
94
95        public Object getDecodedObject(BerInputStream in) {
96            Object[] values = (Object[]) in.content;
97            return new CertificationRequest(
98                    (CertificationRequestInfo) values[0],
99                    (AlgorithmIdentifier) values[1],
100                    ((BitString) values[2]).bytes,
101                    in.getEncoded());
102        }
103
104        protected void getValues(Object object, Object[] values) {
105            CertificationRequest certReq = (CertificationRequest) object;
106            values[0] = certReq.info;
107            values[1] = certReq.algId;
108            values[2] = new BitString(certReq.signature, 0);
109        }
110    };
111}
112
113