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 org.apache.harmony.security.asn1.ASN1Implicit;
024: import org.apache.harmony.security.asn1.ASN1Integer;
025: import org.apache.harmony.security.asn1.ASN1Sequence;
026: import org.apache.harmony.security.asn1.ASN1Type;
027: import org.apache.harmony.security.asn1.BerInputStream;
028:
029: /**
030: * The class encapsulates the ASN.1 DER encoding/decoding work
031: * with the GeneralSubtree structure which is a part of X.509 certificate:
032: * (as specified in RFC 3280 -
033: * Internet X.509 Public Key Infrastructure.
034: * Certificate and Certificate Revocation List (CRL) Profile.
035: * http://www.ietf.org/rfc/rfc3280.txt):
036: *
037: * <pre>
038: *
039: * GeneralSubtree ::= SEQUENCE {
040: * base GeneralName,
041: * minimum [0] BaseDistance DEFAULT 0,
042: * maximum [1] BaseDistance OPTIONAL }
043: *
044: * BaseDistance ::= INTEGER (0..MAX)
045: *
046: * </pre>
047: *
048: * @see org.apache.harmony.security.x509.NameConstraints
049: * @see org.apache.harmony.security.x509.GeneralName
050: */
051: public class GeneralSubtree {
052:
053: // the value of base field of the structure
054: private final GeneralName base;
055: // the value of minimum field of the structure
056: private final int minimum;
057: // the value of maximum field of the structure
058: private final int maximum;
059: // the ASN.1 encoded form of GeneralSubtree
060: private byte[] encoding;
061:
062: /**
063: * TODO
064: * @param base: GeneralName
065: */
066: public GeneralSubtree(GeneralName base) {
067: this (base, 0, -1);
068: }
069:
070: /**
071: * TODO
072: * @param base: GeneralName
073: * @param minimum: int
074: */
075: public GeneralSubtree(GeneralName base, int minimum) {
076: this (base, minimum, -1);
077: }
078:
079: /**
080: * TODO
081: * @param base: GeneralName
082: * @param minimum: int
083: * @param maximum: int
084: */
085: public GeneralSubtree(GeneralName base, int minimum, int maximum) {
086: this .base = base;
087: this .minimum = minimum;
088: this .maximum = maximum;
089: }
090:
091: /**
092: * Returns the value of base field of the structure.
093: * @return base
094: */
095: public GeneralName getBase() {
096: return base;
097: }
098:
099: /**
100: * Returns the value of maximum field of the structure.
101: * @return maximum
102: */
103: public int getMaximum() {
104: return maximum;
105: }
106:
107: /**
108: * Returns the value of minimum field of the structure.
109: * @return minimum
110: */
111: public int getMinimum() {
112: return minimum;
113: }
114:
115: /**
116: * Returns ASN.1 encoded form of this X.509 GeneralSubtree 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("General Subtree: [\n"); //$NON-NLS-1$
132: buffer.append(prefix)
133: .append(" base: ").append(base).append('\n'); //$NON-NLS-1$
134: buffer.append(prefix).append(" minimum: ") //$NON-NLS-1$
135: .append(minimum).append('\n');
136: if (maximum >= 0) {
137: buffer.append(prefix).append(" maximum: ") //$NON-NLS-1$
138: .append(maximum).append('\n');
139: }
140: buffer.append(prefix).append("]\n"); //$NON-NLS-1$
141: }
142:
143: /**
144: * ASN.1 DER X.509 GeneralSubtree encoder/decoder class.
145: */
146: public static final ASN1Sequence ASN1 = new ASN1Sequence(
147: new ASN1Type[] { GeneralName.ASN1,
148: new ASN1Implicit(0, ASN1Integer.getInstance()),
149: new ASN1Implicit(1, ASN1Integer.getInstance()) }) {
150: {
151: setDefault(new byte[] { 0 }, 1); // minimum 0
152: setOptional(2); // maximum optional
153: }
154:
155: protected Object getDecodedObject(BerInputStream in) {
156: Object[] values = (Object[]) in.content;
157: int maximum = -1; // is optional maximum missing?
158: if (values[2] != null) {
159: maximum = ASN1Integer.toIntValue(values[2]); // no!
160: }
161: return new GeneralSubtree((GeneralName) values[0],
162: ASN1Integer.toIntValue(values[1]), maximum);
163: }
164:
165: protected void getValues(Object object, Object[] values) {
166:
167: GeneralSubtree gs = (GeneralSubtree) object;
168:
169: values[0] = gs.base;
170: values[1] = ASN1Integer.fromIntValue(gs.minimum);
171: if (gs.maximum > -1) {
172: values[2] = ASN1Integer.fromIntValue(gs.maximum);
173: }
174: }
175: };
176: }
|