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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Vladimir N. Molotkov, Alexander Y. Kleymenov
23 package org.apache.harmony.security.x509;
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;
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):
43 * PolicyConstraints ::= SEQUENCE {
44 * requireExplicitPolicy [0] SkipCerts OPTIONAL,
45 * inhibitPolicyMapping [1] SkipCerts OPTIONAL }
47 * SkipCerts ::= INTEGER (0..MAX)
51 * TODO: This class is not fully implemented.
53 * @see org.apache.harmony.security.x509.GeneralSubtree
54 * @see org.apache.harmony.security.x509.GeneralName
56 public class PolicyConstraints extends ExtensionValue {
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;
68 public PolicyConstraints() {
74 * @param requireExplicitPolicy: GeneralSubtrees
75 * @param inhibitPolicyMapping: GeneralSubtrees
77 public PolicyConstraints(BigInteger requireExplicitPolicy,
78 BigInteger inhibitPolicyMapping) {
79 this.requireExplicitPolicy = requireExplicitPolicy;
80 this.inhibitPolicyMapping = inhibitPolicyMapping;
85 * @param requireExplicitPolicy: GeneralSubtrees
86 * @param inhibitPolicyMapping: GeneralSubtrees
88 public PolicyConstraints(int requireExplicitPolicy,
89 int inhibitPolicyMapping) {
90 this.requireExplicitPolicy = BigInteger.valueOf(requireExplicitPolicy);
91 this.inhibitPolicyMapping = BigInteger.valueOf(inhibitPolicyMapping);
94 public PolicyConstraints(byte[] encoding) throws IOException {
96 PolicyConstraints pc = (PolicyConstraints) ASN1.decode(encoding);
97 this.requireExplicitPolicy = pc.requireExplicitPolicy;
98 this.inhibitPolicyMapping = pc.inhibitPolicyMapping;
103 // @param requireExplicitPolicy: GeneralSubtrees
104 // @param inhibitPolicyMapping: GeneralSubtrees
105 // @param encoding: byte[]
107 private PolicyConstraints(BigInteger requireExplicitPolicy,
108 BigInteger inhibitPolicyMapping, byte[] encoding) {
109 this(requireExplicitPolicy, inhibitPolicyMapping);
110 this.encoding = encoding;
114 * Returns ASN.1 encoded form of this X.509 PolicyConstraints value.
115 * @return a byte array containing ASN.1 encode form.
117 public byte[] getEncoded() {
118 if (encoding == null) {
119 encoding = ASN1.encode(this);
125 * Places the string representation of extension value
126 * into the StringBuffer object.
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');
134 if (inhibitPolicyMapping != null) {
135 buffer.append(prefix).append(" inhibitPolicyMapping: ")
136 .append(inhibitPolicyMapping).append('\n');
138 buffer.append(prefix).append("]\n");
142 * X.509 PolicyConstraints encoder/decoder.
144 public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
145 new ASN1Implicit(0, ASN1Integer.getInstance()),
146 new ASN1Implicit(1, ASN1Integer.getInstance()) }) {
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]);
159 if (values[1] != null) {
160 inhibitPolicyMapping = new BigInteger((byte[]) values[1]);
162 return new PolicyConstraints(
163 requireExplicitPolicy, inhibitPolicyMapping,
167 protected void getValues(Object object, Object[] values) {
169 PolicyConstraints pc = (PolicyConstraints) object;
171 values[0] = pc.requireExplicitPolicy.toByteArray();
172 values[1] = pc.inhibitPolicyMapping.toByteArray();