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
23package org.apache.harmony.security.x509;
24
25import java.io.IOException;
26import java.math.BigInteger;
27import org.apache.harmony.security.asn1.ASN1Implicit;
28import org.apache.harmony.security.asn1.ASN1Integer;
29import org.apache.harmony.security.asn1.ASN1Sequence;
30import org.apache.harmony.security.asn1.ASN1Type;
31import 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 */
56public final class PolicyConstraints extends ExtensionValue {
57    /** the value of requireExplicitPolicy field of the structure */
58    private final BigInteger requireExplicitPolicy;
59    /** the value of inhibitPolicyMapping field of the structure */
60    private final BigInteger inhibitPolicyMapping;
61    /** the ASN.1 encoded form of PolicyConstraints */
62    private byte[] encoding;
63
64    public PolicyConstraints(BigInteger requireExplicitPolicy,
65            BigInteger inhibitPolicyMapping) {
66        this.requireExplicitPolicy = requireExplicitPolicy;
67        this.inhibitPolicyMapping = inhibitPolicyMapping;
68    }
69
70    public PolicyConstraints(byte[] encoding) throws IOException {
71        super(encoding);
72        PolicyConstraints pc = (PolicyConstraints) ASN1.decode(encoding);
73        this.requireExplicitPolicy = pc.requireExplicitPolicy;
74        this.inhibitPolicyMapping = pc.inhibitPolicyMapping;
75    }
76
77    private PolicyConstraints(BigInteger requireExplicitPolicy,
78                            BigInteger inhibitPolicyMapping, byte[] encoding) {
79        this(requireExplicitPolicy, inhibitPolicyMapping);
80        this.encoding = encoding;
81    }
82
83    /**
84     * Returns ASN.1 encoded form of this X.509 PolicyConstraints value.
85     */
86    @Override public byte[] getEncoded() {
87        if (encoding == null) {
88            encoding = ASN1.encode(this);
89        }
90        return encoding;
91    }
92
93    @Override public void dumpValue(StringBuilder sb, String prefix) {
94        sb.append(prefix).append("PolicyConstraints: [\n");
95        if (requireExplicitPolicy != null) {
96            sb.append(prefix).append("  requireExplicitPolicy: ").append(requireExplicitPolicy).append('\n');
97        }
98        if (inhibitPolicyMapping != null) {
99            sb.append(prefix).append("  inhibitPolicyMapping: ").append(inhibitPolicyMapping).append('\n');
100        }
101        sb.append(prefix).append("]\n");
102    }
103
104    /**
105     * X.509 PolicyConstraints encoder/decoder.
106     */
107    public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
108            new ASN1Implicit(0, ASN1Integer.getInstance()),
109            new ASN1Implicit(1, ASN1Integer.getInstance()) }) {
110        {
111            setOptional(0);
112            setOptional(1);
113        }
114
115        @Override protected Object getDecodedObject(BerInputStream in) {
116            Object[] values = (Object[]) in.content;
117            BigInteger requireExplicitPolicy = null;
118            BigInteger inhibitPolicyMapping = null;
119            if (values[0] != null) {
120                requireExplicitPolicy = new BigInteger((byte[]) values[0]);
121            }
122            if (values[1] != null) {
123                inhibitPolicyMapping = new BigInteger((byte[]) values[1]);
124            }
125            return new PolicyConstraints(
126                requireExplicitPolicy, inhibitPolicyMapping,
127                    in.getEncoded());
128        }
129
130        @Override protected void getValues(Object object, Object[] values) {
131            PolicyConstraints pc = (PolicyConstraints) object;
132            values[0] = pc.requireExplicitPolicy.toByteArray();
133            values[1] = pc.inhibitPolicyMapping.toByteArray();
134        }
135    };
136}
137