OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / org / apache / harmony / security / x509 / CertificatePolicies.java
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
23 package org.apache.harmony.security.x509;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.List;
30 import org.apache.harmony.security.asn1.ASN1SequenceOf;
31 import org.apache.harmony.security.asn1.ASN1Type;
32 import org.apache.harmony.security.asn1.BerInputStream;
33
34 /**
35  * The class encapsulates the ASN.1 DER encoding/decoding work
36  * with Certificate Policies structure which is a part of X.509 certificate
37  * (as specified in RFC 3280 -
38  *  Internet X.509 Public Key Infrastructure.
39  *  Certificate and Certificate Revocation List (CRL) Profile.
40  *  http://www.ietf.org/rfc/rfc3280.txt):
41  *
42  * <pre>
43  *   certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
44  * </pre>
45  *
46  */
47
48 public class CertificatePolicies extends ExtensionValue {
49
50     // the values of policyInformation field of the structure
51     private List policyInformations;
52     // the ASN.1 encoded form of CertificatePolicies
53     private byte[] encoding;
54
55     /**
56      * Constructs an object representing the value of CertificatePolicies.
57      */
58     public CertificatePolicies() {}
59
60     /**
61      * TODO
62      * @param   policyInformations: List
63      */
64     public CertificatePolicies(List policyInformations) {
65         this.policyInformations = policyInformations;
66     }
67
68     public static CertificatePolicies decode(byte[] encoding)
69             throws IOException {
70         CertificatePolicies cps = ((CertificatePolicies) ASN1.decode(encoding));
71         cps.encoding = encoding;
72         return cps;
73     }
74
75     //
76     // TODO
77     // @param   policyInformations: List
78     // @param   encoding:   byte[]
79     //
80     private CertificatePolicies(List policyInformations, byte[] encoding) {
81         this.policyInformations = policyInformations;
82         this.encoding = encoding;
83     }
84
85     /**
86      * Returns the values of policyInformation field of the structure.
87      * @return  policyInformations
88      */
89     public List getPolicyInformations() {
90         return new ArrayList(policyInformations);
91     }
92
93     /**
94      * TODO
95      * @param   policyInformation:  PolicyInformation
96      * @return
97      */
98     public CertificatePolicies addPolicyInformation(
99             PolicyInformation policyInformation) {
100         encoding = null;
101         if (policyInformations == null) {
102             policyInformations = new ArrayList();
103         }
104         policyInformations.add(policyInformation);
105         return this;
106     }
107
108     /**
109      * Returns ASN.1 encoded form of this X.509 CertificatePolicies value.
110      * @return a byte array containing ASN.1 encode form.
111      */
112     public byte[] getEncoded() {
113         if (encoding == null) {
114             encoding = ASN1.encode(this);
115         }
116         return encoding;
117     }
118
119     /**
120      * Places the string representation of extension value
121      * into the StringBuffer object.
122      */
123     public void dumpValue(StringBuffer buffer, String prefix) {
124         buffer.append(prefix).append("CertificatePolicies [\n");
125         for (Iterator it=policyInformations.iterator(); it.hasNext();) {
126             buffer.append(prefix);
127             buffer.append("  ");
128             ((PolicyInformation) it.next()).dumpValue(buffer);
129             buffer.append('\n');
130         }
131         buffer.append(prefix).append("]\n");
132     }
133
134     /**
135      * ASN.1 DER X.509 CertificatePolicies encoder/decoder class.
136      */
137     public static final ASN1Type ASN1 =
138         new ASN1SequenceOf(PolicyInformation.ASN1) {
139
140         public Object getDecodedObject(BerInputStream in) {
141             return new CertificatePolicies((List) in.content, in.getEncoded());
142         }
143
144         public Collection getValues(Object object) {
145             CertificatePolicies cps = (CertificatePolicies) object;
146             return cps.policyInformations;
147         }
148     };
149 }
150