OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / org / apache / harmony / security / x509 / PolicyConstraints.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 Vladimir N. Molotkov, Alexander Y. Kleymenov
20 * @version $Revision$
21 */
22
23 package org.apache.harmony.security.x509;
24
25 import java.io.IOException;
26 import java.math.BigInteger;
27 import org.apache.harmony.security.asn1.ASN1Implicit;
28 import org.apache.harmony.security.asn1.ASN1Integer;
29 import org.apache.harmony.security.asn1.ASN1Sequence;
30 import org.apache.harmony.security.asn1.ASN1Type;
31 import org.apache.harmony.security.asn1.BerInputStream;
32
33 /**
34  * The class encapsulates the ASN.1 DER encoding/decoding work
35  * with PolicyConstraints structure which is a part of X.509 certificate
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  *
43  *  PolicyConstraints ::= SEQUENCE {
44  *       requireExplicitPolicy           [0] SkipCerts OPTIONAL,
45  *       inhibitPolicyMapping            [1] SkipCerts OPTIONAL }
46  *
47  *  SkipCerts ::= INTEGER (0..MAX)
48  *
49  * </pre>
50  *
51  * TODO: This class is not fully implemented.
52  *
53  * @see org.apache.harmony.security.x509.GeneralSubtree
54  * @see org.apache.harmony.security.x509.GeneralName
55  */
56 public class PolicyConstraints extends ExtensionValue {
57
58     // the value of requireExplicitPolicy field of the structure
59     private final BigInteger requireExplicitPolicy;
60     // the value of inhibitPolicyMapping field of the structure
61     private final BigInteger inhibitPolicyMapping;
62     // the ASN.1 encoded form of PolicyConstraints;
63     private byte[] encoding;
64
65     /**
66      * TODO
67      */
68     public PolicyConstraints() {
69         this(null, null);
70     }
71
72     /**
73      * TODO
74      * @param   requireExplicitPolicy:  GeneralSubtrees
75      * @param   inhibitPolicyMapping:   GeneralSubtrees
76      */
77     public PolicyConstraints(BigInteger requireExplicitPolicy,
78             BigInteger inhibitPolicyMapping) {
79         this.requireExplicitPolicy = requireExplicitPolicy;
80         this.inhibitPolicyMapping = inhibitPolicyMapping;
81     }
82
83     /**
84      * TODO
85      * @param   requireExplicitPolicy:  GeneralSubtrees
86      * @param   inhibitPolicyMapping:   GeneralSubtrees
87      */
88     public PolicyConstraints(int requireExplicitPolicy,
89             int inhibitPolicyMapping) {
90         this.requireExplicitPolicy = BigInteger.valueOf(requireExplicitPolicy);
91         this.inhibitPolicyMapping = BigInteger.valueOf(inhibitPolicyMapping);
92     }
93
94     public PolicyConstraints(byte[] encoding) throws IOException {
95         super(encoding);
96         PolicyConstraints pc = (PolicyConstraints) ASN1.decode(encoding);
97         this.requireExplicitPolicy = pc.requireExplicitPolicy;
98         this.inhibitPolicyMapping = pc.inhibitPolicyMapping;
99     }
100
101     //
102     // TODO
103     // @param   requireExplicitPolicy:  GeneralSubtrees
104     // @param   inhibitPolicyMapping:   GeneralSubtrees
105     // @param   encoding:   byte[]
106     //
107     private PolicyConstraints(BigInteger requireExplicitPolicy,
108                             BigInteger inhibitPolicyMapping, byte[] encoding) {
109         this(requireExplicitPolicy, inhibitPolicyMapping);
110         this.encoding = encoding;
111     }
112
113     /**
114      * Returns ASN.1 encoded form of this X.509 PolicyConstraints value.
115      * @return a byte array containing ASN.1 encode form.
116      */
117     public byte[] getEncoded() {
118         if (encoding == null) {
119             encoding = ASN1.encode(this);
120         }
121         return encoding;
122     }
123
124     /**
125      * Places the string representation of extension value
126      * into the StringBuffer object.
127      */
128     public void dumpValue(StringBuffer buffer, String prefix) {
129         buffer.append(prefix).append("PolicyConstraints: [\n");
130         if (requireExplicitPolicy != null) {
131             buffer.append(prefix).append("  requireExplicitPolicy: ")
132                 .append(requireExplicitPolicy).append('\n');
133         }
134         if (inhibitPolicyMapping != null) {
135             buffer.append(prefix).append("  inhibitPolicyMapping: ")
136                 .append(inhibitPolicyMapping).append('\n');
137         }
138         buffer.append(prefix).append("]\n");
139     }
140
141     /**
142      * X.509 PolicyConstraints encoder/decoder.
143      */
144     public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
145             new ASN1Implicit(0, ASN1Integer.getInstance()),
146             new ASN1Implicit(1, ASN1Integer.getInstance()) }) {
147         {
148             setOptional(0);
149             setOptional(1);
150         }
151
152         protected Object getDecodedObject(BerInputStream in) {
153             Object[] values = (Object[]) in.content;
154             BigInteger requireExplicitPolicy = null;
155             BigInteger inhibitPolicyMapping = null;
156             if (values[0] != null) {
157                 requireExplicitPolicy = new BigInteger((byte[]) values[0]);
158             }
159             if (values[1] != null) {
160                 inhibitPolicyMapping = new BigInteger((byte[]) values[1]);
161             }
162             return new PolicyConstraints(
163                 requireExplicitPolicy, inhibitPolicyMapping,
164                     in.getEncoded());
165         }
166
167         protected void getValues(Object object, Object[] values) {
168
169             PolicyConstraints pc = (PolicyConstraints) object;
170
171             values[0] = pc.requireExplicitPolicy.toByteArray();
172             values[1] = pc.inhibitPolicyMapping.toByteArray();
173         }
174     };
175 }