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, Stepan M. Mishura
020: * @version $Revision$
021: */package org.apache.harmony.security.asn1;
022:
023: import java.io.IOException;
024:
025: /**
026: * This class represents ASN.1 Object Identifier type.
027: *
028: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
029: */
030:
031: public class ASN1Oid extends ASN1Primitive {
032:
033: // default implementation
034: private static final ASN1Oid ASN1 = new ASN1Oid();
035:
036: /**
037: * Constructs ASN.1 Object Identifier type
038: *
039: * The constructor is provided for inheritance purposes
040: * when there is a need to create a custom ASN.1 Object Identifier type.
041: * To get a default implementation it is recommended to use
042: * getInstance() method.
043: */
044: public ASN1Oid() {
045: super (TAG_OID);
046: }
047:
048: /**
049: * Returns ASN.1 Object Identifier type default implementation
050: *
051: * The default implementation works with encoding
052: * that is represented as array of integers.
053: *
054: * @return ASN.1 Object Identifier type default implementation
055: */
056: public static ASN1Oid getInstance() {
057: return ASN1;
058: }
059:
060: //
061: //
062: // Decode
063: //
064: //
065:
066: public Object decode(BerInputStream in) throws IOException {
067: in.readOID();
068:
069: if (in.isVerify) {
070: return null;
071: }
072: return getDecodedObject(in);
073: }
074:
075: /**
076: * Extracts array of integers from BER input stream.
077: *
078: * @param in - BER input stream
079: * @return array of integers
080: */
081: public Object getDecodedObject(BerInputStream in)
082: throws IOException {
083: // Allocate and decode
084: int oidElement = in.oidElement;
085: int[] oid = new int[oidElement];
086: for (int id = 1, i = 0; id < oid.length; id++, i++) {
087: int octet = in.buffer[in.contentOffset + i];
088: oidElement = octet & 0x7F;
089: while ((octet & 0x80) != 0) {
090: i++;
091: octet = in.buffer[in.contentOffset + i];
092: oidElement = oidElement << 7 | (octet & 0x7f);
093: }
094: oid[id] = oidElement;
095: }
096: // Special handling for the first packed OID element
097: if (oid[1] > 79) {
098: oid[0] = 2;
099: oid[1] = oid[1] - 80;
100: } else {
101: oid[0] = oid[1] / 40;
102: oid[1] = oid[1] % 40;
103: }
104: return oid;
105: }
106:
107: //
108: //
109: // Encode
110: //
111: //
112:
113: public void encodeContent(BerOutputStream out) {
114: out.encodeOID();
115: }
116:
117: public void setEncodingContent(BerOutputStream out) {
118: int[] oid = (int[]) out.content;
119:
120: int length = 0;
121:
122: // first subidentifier
123: int elem = oid[0] * 40 + oid[1];
124: if (elem == 0) {
125: length = 1;
126: } else {
127: for (; elem > 0; elem = elem >> 7) {
128: length++;
129: }
130: }
131:
132: // the rest of subidentifiers
133: for (int i = 2; i < oid.length; i++) {
134: if (oid[i] == 0) {
135: length++;
136: continue;
137: }
138: for (elem = oid[i]; elem > 0; elem = elem >> 7) {
139: length++;
140: }
141: }
142: out.length = length;
143: }
144:
145: //
146: //
147: // OID encoder/decoder for mapping to string
148: //
149: //
150:
151: private final static ASN1Oid STRING_OID = new ASN1Oid() {
152:
153: public Object getDecodedObject(BerInputStream in)
154: throws IOException {
155:
156: StringBuffer buf = new StringBuffer();
157:
158: int element;
159:
160: //Special handling for the first packed OID element
161: int octet = in.buffer[in.contentOffset];
162: element = octet & 0x7F;
163:
164: int index = 0;
165: while ((octet & 0x80) != 0) {
166: index++;
167: octet = in.buffer[in.contentOffset + index];
168: element = element << 7 | (octet & 0x7F);
169: }
170:
171: if (element > 79) {
172: buf.append('2');
173: buf.append('.');
174: buf.append(element - 80);
175: } else {
176: buf.append(element / 40);
177: buf.append('.');
178: buf.append(element % 40);
179: }
180:
181: // the rest of subidentifiers
182: for (int j = 2; j < in.oidElement; j++) {
183: buf.append('.');
184:
185: index++;
186: octet = in.buffer[in.contentOffset + index];
187: element = octet & 0x7F;
188:
189: while ((octet & 0x80) != 0) {
190: index++;
191: octet = in.buffer[in.contentOffset + index];
192: element = element << 7 | (octet & 0x7f);
193: }
194:
195: buf.append(element);
196: }
197:
198: return buf.toString();
199: }
200:
201: public void setEncodingContent(BerOutputStream out) {
202:
203: //FIXME this is a stub for a while
204: out.content = ObjectIdentifier
205: .toIntArray((String) out.content);
206:
207: super .setEncodingContent(out);
208: }
209: };
210:
211: /**
212: * Returns ASN.1 Object Identifier type implementation
213: *
214: * This implementation works with encoding
215: * that is mapped to java.lang.String object.
216: *
217: * @return ASN.1 Object Identifier type implementation
218: */
219: public static ASN1Oid getInstanceForString() {
220: return STRING_OID;
221: }
222: }
|