001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vladimir N. Molotkov, Alexander Y. Kleymenov
020: * @version $Revision$
021: */package org.apache.harmony.security.x509;
022:
023: import java.io.IOException;
024: import java.math.BigInteger;
025:
026: import org.apache.harmony.security.asn1.ASN1Implicit;
027: import org.apache.harmony.security.asn1.ASN1Integer;
028: import org.apache.harmony.security.asn1.ASN1Sequence;
029: import org.apache.harmony.security.asn1.ASN1Type;
030: import org.apache.harmony.security.asn1.BerInputStream;
031:
032: /**
033: * The class encapsulates the ASN.1 DER encoding/decoding work
034: * with PolicyConstraints structure which is a part of X.509 certificate
035: * (as specified in RFC 3280 -
036: * Internet X.509 Public Key Infrastructure.
037: * Certificate and Certificate Revocation List (CRL) Profile.
038: * http://www.ietf.org/rfc/rfc3280.txt):
039: *
040: * <pre>
041: *
042: * PolicyConstraints ::= SEQUENCE {
043: * requireExplicitPolicy [0] SkipCerts OPTIONAL,
044: * inhibitPolicyMapping [1] SkipCerts OPTIONAL }
045: *
046: * SkipCerts ::= INTEGER (0..MAX)
047: *
048: * </pre>
049: *
050: * TODO: This class is not fully implemented.
051: *
052: * @see org.apache.harmony.security.x509.GeneralSubtree
053: * @see org.apache.harmony.security.x509.GeneralName
054: */
055: public class PolicyConstraints extends ExtensionValue {
056:
057: // the value of requireExplicitPolicy field of the structure
058: private final BigInteger requireExplicitPolicy;
059: // the value of inhibitPolicyMapping field of the structure
060: private final BigInteger inhibitPolicyMapping;
061: // the ASN.1 encoded form of PolicyConstraints;
062: private byte[] encoding;
063:
064: /**
065: * TODO
066: */
067: public PolicyConstraints() {
068: this (null, null);
069: }
070:
071: /**
072: * TODO
073: * @param requireExplicitPolicy: GeneralSubtrees
074: * @param inhibitPolicyMapping: GeneralSubtrees
075: */
076: public PolicyConstraints(BigInteger requireExplicitPolicy,
077: BigInteger inhibitPolicyMapping) {
078: this .requireExplicitPolicy = requireExplicitPolicy;
079: this .inhibitPolicyMapping = inhibitPolicyMapping;
080: }
081:
082: /**
083: * TODO
084: * @param requireExplicitPolicy: GeneralSubtrees
085: * @param inhibitPolicyMapping: GeneralSubtrees
086: */
087: public PolicyConstraints(int requireExplicitPolicy,
088: int inhibitPolicyMapping) {
089: this .requireExplicitPolicy = BigInteger
090: .valueOf(requireExplicitPolicy);
091: this .inhibitPolicyMapping = BigInteger
092: .valueOf(inhibitPolicyMapping);
093: }
094:
095: public PolicyConstraints(byte[] encoding) throws IOException {
096: super (encoding);
097: PolicyConstraints pc = (PolicyConstraints) ASN1
098: .decode(encoding);
099: this .requireExplicitPolicy = pc.requireExplicitPolicy;
100: this .inhibitPolicyMapping = pc.inhibitPolicyMapping;
101: }
102:
103: //
104: // TODO
105: // @param requireExplicitPolicy: GeneralSubtrees
106: // @param inhibitPolicyMapping: GeneralSubtrees
107: // @param encoding: byte[]
108: //
109: private PolicyConstraints(BigInteger requireExplicitPolicy,
110: BigInteger inhibitPolicyMapping, byte[] encoding) {
111: this (requireExplicitPolicy, inhibitPolicyMapping);
112: this .encoding = encoding;
113: }
114:
115: /**
116: * Returns ASN.1 encoded form of this X.509 PolicyConstraints value.
117: * @return a byte array containing ASN.1 encode form.
118: */
119: public byte[] getEncoded() {
120: if (encoding == null) {
121: encoding = ASN1.encode(this );
122: }
123: return encoding;
124: }
125:
126: /**
127: * Places the string representation of extension value
128: * into the StringBuffer object.
129: */
130: public void dumpValue(StringBuffer buffer, String prefix) {
131: buffer.append(prefix).append("PolicyConstraints: [\n"); //$NON-NLS-1$
132: if (requireExplicitPolicy != null) {
133: buffer.append(prefix).append(" requireExplicitPolicy: ") //$NON-NLS-1$
134: .append(requireExplicitPolicy).append('\n');
135: }
136: if (inhibitPolicyMapping != null) {
137: buffer.append(prefix).append(" inhibitPolicyMapping: ") //$NON-NLS-1$
138: .append(inhibitPolicyMapping).append('\n');
139: }
140: buffer.append(prefix).append("]\n"); //$NON-NLS-1$
141: }
142:
143: /**
144: * X.509 PolicyConstraints encoder/decoder.
145: */
146: public static final ASN1Sequence ASN1 = new ASN1Sequence(
147: new ASN1Type[] {
148: new ASN1Implicit(0, ASN1Integer.getInstance()),
149: new ASN1Implicit(1, ASN1Integer.getInstance()) }) {
150: {
151: setOptional(0);
152: setOptional(1);
153: }
154:
155: protected Object getDecodedObject(BerInputStream in) {
156: Object[] values = (Object[]) in.content;
157: BigInteger requireExplicitPolicy = null;
158: BigInteger inhibitPolicyMapping = null;
159: if (values[0] != null) {
160: requireExplicitPolicy = new BigInteger(
161: (byte[]) values[0]);
162: }
163: if (values[1] != null) {
164: inhibitPolicyMapping = new BigInteger(
165: (byte[]) values[1]);
166: }
167: return new PolicyConstraints(requireExplicitPolicy,
168: inhibitPolicyMapping, in.getEncoded());
169: }
170:
171: protected void getValues(Object object, Object[] values) {
172:
173: PolicyConstraints pc = (PolicyConstraints) object;
174:
175: values[0] = pc.requireExplicitPolicy.toByteArray();
176: values[1] = pc.inhibitPolicyMapping.toByteArray();
177: }
178: };
179: }
|