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: package org.apache.harmony.security.x509;
019:
020: import java.io.IOException;
021: import java.math.BigInteger;
022: import org.apache.harmony.security.asn1.ASN1Boolean;
023: import org.apache.harmony.security.asn1.ASN1Integer;
024: import org.apache.harmony.security.asn1.ASN1Type;
025: import org.apache.harmony.security.asn1.ASN1Sequence;
026: import org.apache.harmony.security.asn1.BerInputStream;
027:
028: /**
029: * Basic Constraints Extension (OID == 2.5.29.19).
030: *
031: * The ASN.1 definition for Basic Constraints Extension is:
032: *
033: * <pre>
034: * id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 }
035: *
036: * BasicConstraints ::= SEQUENCE {
037: * cA BOOLEAN DEFAULT FALSE,
038: * pathLenConstraint INTEGER (0..MAX) OPTIONAL
039: * }
040: * </pre>
041: * (as specified in RFC 3280)
042: */
043: public class BasicConstraints extends ExtensionValue {
044:
045: // is CA
046: private boolean cA = false;
047: // path len constraint
048: private int pathLenConstraint = Integer.MAX_VALUE;
049:
050: // Constructor for creating the extension without
051: // encoding provided
052: /**
053: * Creates the extension object on the base of the values of
054: * fields of the structure..
055: */
056: public BasicConstraints(boolean cA, int pathLenConstraint) {
057: this .cA = cA;
058: this .pathLenConstraint = pathLenConstraint;
059: }
060:
061: /**
062: * Creates the extension object on the base of its encoded form.
063: */
064: public BasicConstraints(byte[] encoding) throws IOException {
065: super (encoding);
066: Object[] values = (Object[]) ASN1.decode(encoding);
067: cA = ((Boolean) values[0]).booleanValue();
068: if (values[1] != null) {
069: pathLenConstraint = new BigInteger((byte[]) values[1])
070: .intValue();
071: }
072: }
073:
074: public boolean getCA() {
075: return cA;
076: }
077:
078: public int getPathLenConstraint() {
079: return pathLenConstraint;
080: }
081:
082: /**
083: * Returns the encoded form of the object.
084: */
085: public byte[] getEncoded() {
086: if (encoding == null) {
087: encoding = ASN1.encode(new Object[] { Boolean.valueOf(cA),
088: BigInteger.valueOf(pathLenConstraint) });
089: }
090: return encoding;
091: }
092:
093: /**
094: * Places the string representation of extension value
095: * into the StringBuffer object.
096: */
097: public void dumpValue(StringBuffer buffer, String prefix) {
098: buffer
099: .append(prefix)
100: .append("BasicConstraints [\n").append(prefix) //$NON-NLS-1$
101: .append(" CA: ").append(cA) //$NON-NLS-1$
102: .append("\n ").append(prefix).append("pathLenConstraint: ") //$NON-NLS-1$ //$NON-NLS-2$
103: .append(pathLenConstraint).append('\n').append(prefix)
104: .append("]\n"); //$NON-NLS-1$
105: }
106:
107: /**
108: * ASN.1 Encoder/Decoder.
109: */
110: public static final ASN1Type ASN1 = new ASN1Sequence(
111: new ASN1Type[] { ASN1Boolean.getInstance(),
112: ASN1Integer.getInstance() }) {
113: {
114: setDefault(Boolean.FALSE, 0);
115: setOptional(1);
116: }
117:
118: public Object getDecodedObject(BerInputStream in)
119: throws IOException {
120: return in.content;
121: }
122:
123: protected void getValues(Object object, Object[] values) {
124: Object[] vals = (Object[]) object;
125: values[0] = (Boolean) vals[0];
126: values[1] = ((BigInteger) vals[1]).toByteArray();
127: }
128:
129: };
130: }
|