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: */package org.apache.geronimo.crypto.asn1;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.util.Enumeration;
021: import java.util.Vector;
022:
023: public class BERConstructedOctetString extends DEROctetString {
024: /**
025: * convert a vector of octet strings into a single byte string
026: */
027: static private byte[] toBytes(Vector octs) {
028: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
029:
030: for (int i = 0; i != octs.size(); i++) {
031: try {
032: DEROctetString o = (DEROctetString) octs.elementAt(i);
033:
034: bOut.write(o.getOctets());
035: } catch (ClassCastException e) {
036: throw new IllegalArgumentException(
037: octs.elementAt(i).getClass().getName()
038: + " found in input should only contain DEROctetString",
039: e);
040: } catch (IOException e) {
041: throw new IllegalArgumentException(
042: "exception converting octets " + e.getMessage(),
043: e);
044: }
045: }
046:
047: return bOut.toByteArray();
048: }
049:
050: private Vector octs;
051:
052: /**
053: * @param string the octets making up the octet string.
054: */
055: public BERConstructedOctetString(byte[] string) {
056: super (string);
057: }
058:
059: public BERConstructedOctetString(Vector octs) {
060: super (toBytes(octs));
061:
062: this .octs = octs;
063: }
064:
065: public BERConstructedOctetString(DERObject obj) {
066: super (obj);
067: }
068:
069: public BERConstructedOctetString(DEREncodable obj) {
070: super (obj.getDERObject());
071: }
072:
073: public byte[] getOctets() {
074: return string;
075: }
076:
077: /**
078: * return the DER octets that make up this string.
079: */
080: public Enumeration getObjects() {
081: if (octs == null) {
082: return generateOcts().elements();
083: }
084:
085: return octs.elements();
086: }
087:
088: private Vector generateOcts() {
089: int start = 0;
090: int end = 0;
091: Vector vec = new Vector();
092:
093: while ((end + 1) < string.length) {
094: if (string[end] == 0 && string[end + 1] == 0) {
095: byte[] nStr = new byte[end - start + 1];
096:
097: System.arraycopy(string, start, nStr, 0, nStr.length);
098:
099: vec.addElement(new DEROctetString(nStr));
100: start = end + 1;
101: }
102: end++;
103: }
104:
105: byte[] nStr = new byte[string.length - start];
106:
107: System.arraycopy(string, start, nStr, 0, nStr.length);
108:
109: vec.addElement(new DEROctetString(nStr));
110:
111: return vec;
112: }
113:
114: public void encode(DEROutputStream out) throws IOException {
115: if (out instanceof ASN1OutputStream
116: || out instanceof BEROutputStream) {
117: out.write(CONSTRUCTED | OCTET_STRING);
118:
119: out.write(0x80);
120:
121: //
122: // write out the octet array
123: //
124: if (octs != null) {
125: for (int i = 0; i != octs.size(); i++) {
126: out.writeObject(octs.elementAt(i));
127: }
128: } else {
129: int start = 0;
130: int end = 0;
131:
132: while ((end + 1) < string.length) {
133: if (string[end] == 0 && string[end + 1] == 0) {
134: byte[] nStr = new byte[end - start + 1];
135:
136: System.arraycopy(string, start, nStr, 0,
137: nStr.length);
138:
139: out.writeObject(new DEROctetString(nStr));
140: start = end + 1;
141: }
142: end++;
143: }
144:
145: byte[] nStr = new byte[string.length - start];
146:
147: System.arraycopy(string, start, nStr, 0, nStr.length);
148:
149: out.writeObject(new DEROctetString(nStr));
150: }
151:
152: out.write(0x00);
153: out.write(0x00);
154: } else {
155: super.encode(out);
156: }
157: }
158: }
|