CertificationRequestInfo.java revision f33eae7e84eb6d3b0f4e86b59605bb3de73009f3
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 java.util.List;
21
22import javax.security.auth.x500.X500Principal;
23
24import org.apache.harmony.security.asn1.ASN1Implicit;
25import org.apache.harmony.security.asn1.ASN1Integer;
26import org.apache.harmony.security.asn1.ASN1Sequence;
27import org.apache.harmony.security.asn1.ASN1SetOf;
28import org.apache.harmony.security.asn1.ASN1Type;
29import org.apache.harmony.security.asn1.BerInputStream;
30import org.apache.harmony.security.x501.AttributeTypeAndValue;
31import org.apache.harmony.security.x501.Name;
32import org.apache.harmony.security.x509.SubjectPublicKeyInfo;
33
34/**
35   CertificationRequestInfo ::= SEQUENCE {
36     version Version,
37     subject Name,
38     subjectPublicKeyInfo SubjectPublicKeyInfo,
39     attributes [0] IMPLICIT Attributes }
40
41   Version ::= INTEGER
42
43   Attributes ::= SET OF Attribute
44*/
45
46public class CertificationRequestInfo {
47    // version
48    private int version;
49
50    // the value of subject field of the structure
51    private Name subject;
52
53    // the value of subjectPublicKeyInfo field of the structure
54    private SubjectPublicKeyInfo subjectPublicKeyInfo;
55
56    // the value of attributes field of the structure
57    private List attributes;
58
59    // the ASN.1 encoded form of CertificationRequestInfo
60    private byte [] encoding;
61
62    public CertificationRequestInfo(int version, Name subject,
63            SubjectPublicKeyInfo subjectPublicKeyInfo, List attributes) {
64        this.version = version;
65        this.subject = subject;
66        this.subjectPublicKeyInfo = subjectPublicKeyInfo;
67        this.attributes = attributes;
68    }
69
70    // private constructor with encoding given
71    private CertificationRequestInfo(int version, Name subject,
72            SubjectPublicKeyInfo subjectPublicKeyInfo, List attributes, byte [] encoding) {
73        this(version, subject, subjectPublicKeyInfo, attributes);
74        this.encoding = encoding;
75    }
76
77    /**
78     * @return Returns the attributes.
79     */
80    public List getAttributes() {
81        return attributes;
82    }
83
84    /**
85     * @return Returns the subject.
86     */
87    public Name getSubject() {
88        return subject;
89    }
90
91    /**
92     * @return Returns the subjectPublicKeyInfo.
93     */
94    public SubjectPublicKeyInfo getSubjectPublicKeyInfo() {
95        return subjectPublicKeyInfo;
96    }
97
98    /**
99     * @return Returns the version.
100     */
101    public int getVersion() {
102        return version;
103    }
104
105    /**
106     * Returns ASN.1 encoded form of this CertificationRequestInfo.
107     * @return a byte array containing ASN.1 encode form.
108     */
109    public byte[] getEncoded() {
110        if (encoding == null) {
111            encoding = ASN1.encode(this);
112        }
113        return encoding;
114    }
115
116
117    public String toString() {
118        StringBuilder res = new StringBuilder();
119        res.append("-- CertificationRequestInfo:");
120        res.append("\n version: ");
121        res.append(version);
122        res.append("\n subject: ");
123        res.append(subject.getName(X500Principal.CANONICAL));
124        res.append("\n subjectPublicKeyInfo: ");
125        res.append("\n\t algorithm: "
126                + subjectPublicKeyInfo.getAlgorithmIdentifier().getAlgorithm());
127        res.append("\n\t public key: " + subjectPublicKeyInfo.getPublicKey());
128        res.append("\n attributes: ");
129        if (attributes != null) {
130            res.append(attributes.toString());
131        } else {
132            res.append("none");
133        }
134        res.append("\n-- CertificationRequestInfo End\n");
135        return res.toString();
136    }
137
138    public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
139            ASN1Integer.getInstance(),              // version
140            Name.ASN1,                              // subject
141            SubjectPublicKeyInfo.ASN1,              // subjectPublicKeyInfo
142            new ASN1Implicit(0, new ASN1SetOf(
143                    AttributeTypeAndValue.ASN1))    // attributes
144            }) {
145
146        protected Object getDecodedObject(BerInputStream in) {
147            Object[] values = (Object[]) in.content;
148            return new CertificationRequestInfo(
149                    ASN1Integer.toIntValue(values[0]),
150                    (Name) values[1],
151                    (SubjectPublicKeyInfo) values[2],
152                    (List) values[3],
153                    in.getEncoded());
154        }
155
156        protected void getValues(Object object, Object[] values) {
157            CertificationRequestInfo certReqInfo = (CertificationRequestInfo) object;
158
159            values[0] = ASN1Integer.fromIntValue(certReqInfo.version);
160            values[1] = certReqInfo.subject;
161            values[2] = certReqInfo.subjectPublicKeyInfo;
162            values[3] = certReqInfo.attributes;
163        }
164    };
165
166}
167
168