Author: erodriguez Date: Thu Jan 20 12:01:38 2005 New Revision: 125809 URL: http://svn.apache.org/viewcvs?view=rev&rev=125809 Log: Apache DER codecs for Kerberos. Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/ASN1InputStream.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/ASN1OutputStream.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERConstructedOctetString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERNull.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERSequence.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERSet.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERTaggedObject.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERApplicationSpecific.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBMPString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBitString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBoolean.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEREncodable.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEREnumerated.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERGeneralString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERGeneralizedTime.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERIA5String.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERInteger.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERNull.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERNumericString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERObject.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERObjectIdentifier.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEROctetString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERPrintableString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERSequence.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERSet.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERTaggedObject.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERTeletexString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUTCTime.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUTF8String.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUniversalString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUnknownTag.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERVisibleString.java incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/OIDTokenizer.java
Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/ASN1InputStream.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/ASN1InputStream.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/ASN1InputStream.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,461 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.EOFException; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Vector; + +/** + * General purpose ASN.1 decoder. + */ +public class ASN1InputStream extends FilterInputStream +{ + private boolean EOF_FOUND = false; + + private DERObject END_OF_STREAM = new DERObject( 0, null ) { + public void encode( ASN1OutputStream out ) + throws IOException + { + throw new IOException("End of stream."); + } + public int hashCode() + { + return 0; + } + public boolean equals( Object o ) + { + return o == this; + } + }; + + public ASN1InputStream( InputStream is ) + { + super( is ); + } + + public ASN1InputStream( byte[] input ) + { + super( new ByteArrayInputStream( input ) ); + } + + protected int readLength() + throws IOException + { + int length = read(); + if ( length < 0 ) + { + throw new IOException( "EOF found when length expected." ); + } + + // Indefinite-length encoding. + if ( length == 0x80 ) + { + return -1; + } + + if ( length > 127 ) + { + int size = length & 0x7f; + + if ( size > 4 ) + { + throw new IOException( "DER length more than 4 bytes." ); + } + + length = 0; + for ( int i = 0; i < size; i++ ) + { + int next = read(); + + if ( next < 0 ) + { + throw new IOException( "EOF found reading length." ); + } + + length = ( length << 8 ) + next; + } + + if ( length < 0 ) + { + throw new IOException( "Corrupted steam - negative length found." ); + } + } + + return length; + } + + protected void readFully( byte[] bytes ) + throws IOException + { + int left = bytes.length; + int len; + + if ( left == 0 ) + { + return; + } + + while ( ( len = read( bytes, bytes.length - left, left ) ) > 0 ) + { + if ( ( left -= len ) == 0 ) + { + return; + } + } + + if ( left != 0 ) + { + throw new EOFException( "EOF encountered in middle of object." ); + } + } + + /** + * Build an object given its tag and a byte stream. + */ + protected DEREncodable buildObject( int tag, byte[] bytes ) + throws IOException + { + if ( ( tag & DERObject.APPLICATION ) != 0 ) + { + return new DERApplicationSpecific( tag, bytes ); + } + + switch ( tag ) + { + case DERObject.NULL: + return new DERNull(); + case DERObject.SEQUENCE | DERObject.CONSTRUCTED: + ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); + ASN1InputStream ais = new ASN1InputStream( bytes ); + + DERSequence sequence = new DERSequence(); + + DEREncodable obj = ais.readObject(); + + while ( obj != null ) + { + sequence.add( obj ); + obj = ais.readObject(); + } + + return sequence; + case DERObject.SET | DERObject.CONSTRUCTED: + bais = new ByteArrayInputStream( bytes ); + ais = new ASN1InputStream( bais ); + DERSet set = new DERSet(); + + obj = ais.readObject(); + + while ( obj != null ) + { + set.add( obj ); + obj = ais.readObject(); + } + + return set; + case DERObject.BOOLEAN: + return new DERBoolean( bytes ); + case DERObject.INTEGER: + return new DERInteger( bytes ); + case DERObject.ENUMERATED: + return new DEREnumerated( bytes ); + case DERObject.OBJECT_IDENTIFIER: + return new DERObjectIdentifier( bytes ); + case DERObject.BIT_STRING: + return new DERBitString( bytes ); + case DERObject.NUMERIC_STRING: + return new DERNumericString( bytes ); + case DERObject.UTF8_STRING: + return new DERUTF8String( bytes ); + case DERObject.PRINTABLE_STRING: + return new DERPrintableString( bytes ); + case DERObject.IA5_STRING: + return new DERIA5String( bytes ); + case DERObject.T61_STRING: + return new DERTeletexString( bytes ); + case DERObject.VISIBLE_STRING: + return new DERVisibleString( bytes ); + case DERObject.GENERAL_STRING: + return new DERGeneralString( bytes ); + case DERObject.UNIVERSAL_STRING: + return new DERUniversalString( bytes ); + case DERObject.BMP_STRING: + return new DERBMPString( bytes ); + case DERObject.OCTET_STRING: + return new DEROctetString( bytes ); + case DERObject.UTC_TIME: + return new DERUTCTime( bytes ); + case DERObject.GENERALIZED_TIME: + return new DERGeneralizedTime( bytes ); + default: + // Tag number is bottom 5 bits. + if ( ( tag & DERObject.TAGGED ) != 0 ) + { + int tagNo = tag & 0x1f; + + if ( tagNo == 0x1f ) + { + int idx = 0; + + tagNo = 0; + + while ( ( bytes[ idx ] & 0x80 ) != 0 ) + { + tagNo |= ( bytes[ idx++ ] & 0x7f ); + tagNo <<= 7; + } + + tagNo |= ( bytes[ idx ] & 0x7f ); + + byte[] tmp = bytes; + + bytes = new byte[ tmp.length - ( idx + 1 ) ]; + System.arraycopy( tmp, idx + 1, bytes, 0, bytes.length ); + } + + // Empty tag. + if ( bytes.length == 0 ) + { + if ( ( tag & DERObject.CONSTRUCTED ) == 0) + { + return new DERTaggedObject( tagNo, new DERNull() ); + } + + return new DERTaggedObject( false, tagNo, new DERSequence() ); + } + + // Simple type - implicit, return an octet string. + if ( ( tag & DERObject.CONSTRUCTED ) == 0 ) + { + return new DERTaggedObject( false, tagNo, new DEROctetString( bytes ) ); + } + + bais = new ByteArrayInputStream( bytes ); + ais = new ASN1InputStream( bais ); + + DEREncodable encodable = ais.readObject(); + + // Explicitly tagged - if it isn't we'd have to tell from the context. + if ( ais.available() == 0 ) + { + return new DERTaggedObject( tagNo, encodable ); + } + + // Another implicit object, create a sequence. + DERSequence derSequence = new DERSequence(); + + while ( encodable != null ) + { + derSequence.add( encodable ); + encodable = ais.readObject(); + } + + return new DERTaggedObject( false, tagNo, derSequence ); + } + + return new DERUnknownTag( tag, bytes ); + } + } + + /** + * Read a string of bytes representing an indefinite length object. + */ + private byte[] readIndefiniteLengthFully() + throws IOException + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + int b, b1; + + b1 = read(); + + while ( ( b = read() ) >= 0 ) + { + if ( b1 == 0 && b == 0 ) + { + break; + } + + baos.write( b1 ); + b1 = b; + } + + return baos.toByteArray(); + } + + private BERConstructedOctetString buildConstructedOctetString() + throws IOException + { + Vector octets = new Vector(); + + for (;;) + { + DEREncodable encodable = readObject(); + + if ( encodable == END_OF_STREAM ) + { + break; + } + + octets.addElement( encodable ); + } + + return new BERConstructedOctetString( octets ); + } + + public DEREncodable readObject() + throws IOException + { + int tag = read(); + if ( tag == -1 ) + { + if ( EOF_FOUND ) + { + throw new EOFException( "Attempt to read past end of file." ); + } + + EOF_FOUND = true; + + return null; + } + + int length = readLength(); + + // Indefinite length method. + if ( length < 0 ) + { + switch ( tag ) + { + case DERObject.NULL: + return new BERNull(); + case DERObject.SEQUENCE | DERObject.CONSTRUCTED: + BERSequence sequence = new BERSequence(); + + for (;;) + { + DEREncodable obj = readObject(); + + if ( obj == END_OF_STREAM ) + { + break; + } + + sequence.add( obj ); + } + return sequence; + case DERObject.SET | DERObject.CONSTRUCTED: + BERSet set = new BERSet(); + + for (;;) + { + DEREncodable obj = readObject(); + + if ( obj == END_OF_STREAM ) + { + break; + } + + set.add( obj ); + } + return set; + case DERObject.OCTET_STRING | DERObject.CONSTRUCTED: + return buildConstructedOctetString(); + default: + // Tag number is bottom 5 bits. + if ( ( tag & DERObject.TAGGED ) != 0 ) + { + int tagNo = tag & 0x1f; + + if ( tagNo == 0x1f ) + { + int b = read(); + + tagNo = 0; + + while ( ( b >= 0 ) && ( ( b & 0x80 ) != 0 ) ) + { + tagNo |= ( b & 0x7f ); + tagNo <<= 7; + b = read(); + } + + tagNo |= ( b & 0x7f ); + } + + // Simple type - implicit, return an octet string. + if ( ( tag & DERObject.CONSTRUCTED ) == 0 ) + { + byte[] bytes = readIndefiniteLengthFully(); + + return new BERTaggedObject( false, tagNo, new DEROctetString( bytes ) ); + } + + // Either constructed or explicitly tagged + DEREncodable dObj = readObject(); + + // Empty tag! + if ( dObj == END_OF_STREAM ) + { + return new DERTaggedObject( tagNo ); + } + + DEREncodable next = readObject(); + + // Explicitly tagged. + if ( next == END_OF_STREAM ) + { + return new BERTaggedObject( tagNo, dObj ); + } + + // Another implicit object, create a sequence. + BERSequence berSequence = new BERSequence(); + + berSequence.add( dObj ); + + do + { + berSequence.add( next ); + next = readObject(); + } + while ( next != END_OF_STREAM ); + + return new BERTaggedObject( false, tagNo, berSequence ); + } + + throw new IOException( "Unknown BER object encountered." ); + } + } + + // End of contents marker. + if ( tag == 0 && length == 0 ) + { + return END_OF_STREAM; + } + + byte[] bytes = new byte[ length ]; + + readFully( bytes ); + + return buildObject( tag, bytes ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/ASN1OutputStream.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/ASN1OutputStream.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/ASN1OutputStream.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,90 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + + +public class ASN1OutputStream extends FilterOutputStream +{ + public ASN1OutputStream( OutputStream os ) + { + super( os ); + } + + private void writeLength( int length ) + throws IOException + { + if ( length > 127 ) + { + int size = 1; + int val = length; + + while ( ( val >>>= 8 ) != 0 ) + { + size++; + } + + write( (byte)( size | 0x80 ) ); + + for ( int i = (size - 1) * 8; i >= 0; i -= 8 ) + { + write( (byte)( length >> i ) ); + } + } + else + { + write( (byte)length ); + } + } + + void writeEncoded( int tag, byte[] bytes ) + throws IOException + { + write( tag ); + writeLength( bytes.length ); + write( bytes ); + } + + public void writeObject( Object obj ) + throws IOException + { + if ( obj == null ) + { + writeNull(); + } + else if ( obj instanceof DEREncodable ) + { + ( (DEREncodable)obj ).encode( this ); + } + else + { + throw new IOException( "Object not DEREncodable." ); + } + } + + protected void writeNull() + throws IOException + { + write( DERObject.NULL ); + write( DERObject.TERMINATOR ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERConstructedOctetString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERConstructedOctetString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERConstructedOctetString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,159 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Vector; + + +public class BERConstructedOctetString extends DEROctetString +{ + private Vector octets; + + /** + * @param string the octets making up the octet string. + */ + public BERConstructedOctetString( byte[] string ) + { + super( string ); + } + + public BERConstructedOctetString( Vector octets ) + { + super( toBytes( octets ) ); + + this.octets = octets; + } + + /** + * Convert a vector of octet strings into a single byte string. + */ + static private byte[] toBytes( Vector octs ) + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + for ( int i = 0; i != octs.size(); i++ ) + { + try + { + DEROctetString o = (DEROctetString)octs.elementAt( i ); + + baos.write( o.getOctets() ); + } + catch (ClassCastException e) + { + throw new IllegalArgumentException( octs.elementAt( i ).getClass().getName() + " found in input should only contain DEROctetString." ); + } + catch (IOException e) + { + throw new IllegalArgumentException( "Exception converting octets " + e.toString() ); + } + } + + return baos.toByteArray(); + } + + /** + * @return Enumeration the DER octets that make up this string. + */ + public Enumeration getObjects() + { + if ( octets == null ) + { + return generateOcts().elements(); + } + + return octets.elements(); + } + + private Vector generateOcts() + { + int start = 0; + int end = 0; + Vector vector = new Vector(); + + while ( ( end + 1 ) < value.length ) + { + if ( value[ end ] == 0 && value[ end + 1 ] == 0 ) + { + byte[] nStr = new byte[ end - start + 1 ]; + + System.arraycopy( value, start, nStr, 0, nStr.length ); + + vector.addElement( new DEROctetString( nStr ) ); + start = end + 1; + } + end++; + } + + byte[] nStr = new byte[ value.length - start ]; + + System.arraycopy( value, start, nStr, 0, nStr.length ); + + vector.addElement( new DEROctetString( nStr ) ); + + return vector; + } + + public void encode( ASN1OutputStream out ) + throws IOException + { + out.write( CONSTRUCTED | OCTET_STRING ); + + out.write( DERObject.TAGGED ); + + if ( octets != null ) + { + for ( int i = 0; i != octets.size(); i++ ) + { + out.writeObject( octets.elementAt( i ) ); + } + } + else + { + int start = 0; + int end = 0; + + while ( ( end + 1 ) < value.length ) + { + if ( value[ end ] == 0 && value[ end + 1 ] == 0 ) + { + byte[] newString = new byte[ end - start + 1 ]; + + System.arraycopy( value, start, newString, 0, newString.length ); + + out.writeObject( new DEROctetString( newString ) ); + start = end + 1; + } + end++; + } + + byte[] newString = new byte[ value.length - start ]; + + System.arraycopy( value, start, newString, 0, newString.length ); + + out.writeObject( new DEROctetString( newString ) ); + } + + out.write( TERMINATOR ); + out.write( TERMINATOR ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERNull.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERNull.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERNull.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,33 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.IOException; + +/** + * A BER NULL object. + */ +public class BERNull extends DERNull +{ + public void encode( ASN1OutputStream out ) + throws IOException + { + out.write( NULL ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERSequence.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERSequence.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERSequence.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.IOException; +import java.util.Enumeration; + +public class BERSequence extends DERSequence +{ + public void encode( ASN1OutputStream out ) + throws IOException + { + out.write( DERObject.SEQUENCE | DERObject.CONSTRUCTED); + out.write( DERObject.TAGGED ); + + Enumeration e = getObjects(); + while ( e.hasMoreElements() ) + { + out.writeObject( e.nextElement() ); + } + + out.write( DERObject.TERMINATOR ); + out.write( DERObject.TERMINATOR ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERSet.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERSet.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERSet.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,42 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.IOException; +import java.util.Enumeration; + + +public class BERSet extends DERSet +{ + public void encode( ASN1OutputStream out ) + throws IOException + { + out.write( DERObject.SET | DERObject.CONSTRUCTED ); + out.write( DERObject.TAGGED ); + + Enumeration e = getObjects(); + while ( e.hasMoreElements() ) + { + out.writeObject( e.nextElement() ); + } + + out.write( DERObject.TERMINATOR ); + out.write( DERObject.TERMINATOR ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERTaggedObject.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERTaggedObject.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/BERTaggedObject.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,102 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.IOException; +import java.util.Enumeration; + +/** + * BER TaggedObject + */ +public class BERTaggedObject extends DERTaggedObject +{ + /** + * @param tag the tag number for this object. + * @param obj the tagged object. + */ + public BERTaggedObject( int tag, DEREncodable obj ) + { + super( tag, obj ); + } + + /** + * @param true if an explicitly tagged object. + * @param tag the tag number for this object. + * @param obj the tagged object. + */ + public BERTaggedObject( boolean explicit, int tag, DEREncodable obj ) + { + super( explicit, tag, obj ); + } + + public void encode( ASN1OutputStream out ) + throws IOException + { + out.write( DERObject.CONSTRUCTED | DERObject.TAGGED | tag ); + out.write( DERObject.TAGGED ); + + if ( !empty ) + { + if ( !explicit ) + { + if ( obj instanceof DEROctetString ) + { + Enumeration e; + + if ( obj instanceof BERConstructedOctetString ) + { + e = ( (BERConstructedOctetString)obj ).getObjects(); + } + else + { + DEROctetString octs = (DEROctetString)obj; + BERConstructedOctetString berO = new BERConstructedOctetString( octs.getOctets() ); + + e = berO.getObjects(); + } + + while ( e.hasMoreElements() ) + { + out.writeObject( e.nextElement() ); + } + } + else if ( obj instanceof DERSequence ) + { + Enumeration e = ( (DERSequence)obj ).getObjects(); + + while ( e.hasMoreElements() ) + { + out.writeObject( e.nextElement() ); + } + } + else + { + throw new RuntimeException( "Not implemented: " + obj.getClass().getName() ); + } + } + else + { + out.writeObject( obj ); + } + } + + out.write( DERObject.TERMINATOR ); + out.write( DERObject.TERMINATOR ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERApplicationSpecific.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERApplicationSpecific.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERApplicationSpecific.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,72 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * DER Application Specific object. + */ +public class DERApplicationSpecific extends DERObject +{ + private int tag; + + /** + * Basic DERObject constructor. + */ + public DERApplicationSpecific( int tag, byte[] value ) + { + super( tag, value ); + this.tag = tag; + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERApplicationSpecific valueOf( int tag, DEREncodable object ) + throws IOException + { + tag = tag | CONSTRUCTED; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ASN1OutputStream aos = new ASN1OutputStream( baos ); + + aos.writeObject( object ); + + return new DERApplicationSpecific( tag, baos.toByteArray() ); + } + + public int getApplicationTag() + { + return tag & 0x1F; + } + + public DEREncodable getObject() + throws IOException + { + return new ASN1InputStream( new ByteArrayInputStream( getOctets() ) ).readObject(); + } + + public void encode( ASN1OutputStream out ) throws IOException + { + out.writeEncoded( APPLICATION | tag, value ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBMPString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBMPString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBMPString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,46 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * DER BMPString object. + */ +public class DERBMPString extends DERString +{ + /** + * Basic DERObject constructor. + */ + public DERBMPString( byte[] value ) + { + super( BMP_STRING, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERBMPString valueOf( String string ) + { + return new DERBMPString( stringToByteArray( string ) ); + } + + public String getString() + { + return byteArrayToString( value ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBitString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBitString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBitString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,51 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.IOException; + +/** + * DER Bit String object. + */ +public class DERBitString extends DERObject +{ + /** + * Basic DERObject constructor. + */ + public DERBitString( byte[] value ) + { + super( BIT_STRING, value ); + } + + public byte[] getOctets() + { + return value; + } + + public void encode( ASN1OutputStream out ) + throws IOException + { + byte[] bytes = new byte[ value.length + 1 ]; + + bytes[0] = (byte)0x00; + System.arraycopy( value, 0, bytes, 1, bytes.length - 1 ); + + out.writeEncoded( BIT_STRING, bytes ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBoolean.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBoolean.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERBoolean.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,56 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * DER Boolean object. + */ +public class DERBoolean extends DERObject +{ + private static final byte[] trueArray = { (byte)0x01 }; + private static final byte[] falseArray = { (byte)0x00 }; + + public static final DERBoolean TRUE = new DERBoolean( trueArray ); + public static final DERBoolean FALSE = new DERBoolean( falseArray ); + + /** + * Basic DERObject constructor. + */ + public DERBoolean( byte[] value ) + { + super( BOOLEAN, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERBoolean valueOf( boolean value ) + { + return ( value ? TRUE : FALSE ); + } + + /** + * Lazy accessor + * @return boolean value + */ + public boolean isTrue() + { + return value[ 0 ] == (byte)0x01; + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEREncodable.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEREncodable.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEREncodable.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,29 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.IOException; + +/** + * Interface for encodable DER objects. + */ +public interface DEREncodable +{ + void encode( ASN1OutputStream out ) throws IOException; +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEREnumerated.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEREnumerated.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEREnumerated.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,73 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + + +/** + * DER Enumerated object. + */ +public class DEREnumerated extends DERObject +{ + /** + * Basic DERObject constructor. + */ + public DEREnumerated( byte[] value ) + { + super( ENUMERATED, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DEREnumerated valueOf( int integer ) + { + return new DEREnumerated( intToOctet( integer ) ); + } + + /** + * Lazy accessor + * @return integer value + */ + public int intValue() + { + return octetToInt( value ); + } + + private static int octetToInt( byte[] bytes ) + { + int result = 0; + + for ( int ii = 0; ii < Math.min( 4, bytes.length ); ii++ ) + { + result += bytes[ ii ] * ( 16 ^ ii ); + } + return result; + } + + private static byte[] intToOctet( int integer ) + { + byte[] result = new byte[ 4 ]; + + for ( int ii = 0, shift = 24; ii < 4; ii++, shift -= 8 ) + { + result[ ii ] = (byte) ( 0xFF & ( integer >> shift ) ); + } + return result; + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERGeneralString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERGeneralString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERGeneralString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,39 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + + +public class DERGeneralString extends DERString +{ + /** + * Basic DERObject constructor. + */ + DERGeneralString( byte[] value ) + { + super( GENERAL_STRING, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERGeneralString valueOf( String string ) + { + return new DERGeneralString( stringToByteArray( string ) ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERGeneralizedTime.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERGeneralizedTime.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERGeneralizedTime.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,71 @@ + /* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +/** + * DER Generalized time object. + */ +public class DERGeneralizedTime extends DERString +{ + private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" ); + private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss'Z'" ); + + static + { + dateFormat.setTimeZone( UTC_TIME_ZONE ); + } + + /** + * Basic DERObject constructor. + */ + DERGeneralizedTime( byte[] value ) + { + super( GENERALIZED_TIME, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERGeneralizedTime valueOf( Date date ) + { + String dateString = dateFormat.format( date ); + + byte[] bytes = stringToByteArray( dateString ); + + return new DERGeneralizedTime( bytes ); + } + + /** + * Lazy accessor + * @return Date representation of this DER Generalized Time + * @throws ParseException + */ + public Date getDate() + throws ParseException + { + String string = byteArrayToString( value ); + + return dateFormat.parse( string ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERIA5String.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERIA5String.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERIA5String.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * DER IA5String object. + */ +public class DERIA5String extends DERString +{ + /** + * Basic DERObject constructor. + */ + DERIA5String( byte[] value ) + { + super( IA5_STRING, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERIA5String valueOf( String string ) + { + return new DERIA5String( stringToByteArray( string ) ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERInteger.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERInteger.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERInteger.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,62 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.math.BigInteger; + +/** + * DER Integer object. + */ +public class DERInteger extends DERObject +{ + /** + * Basic DERObject constructor. + */ + DERInteger( byte[] value ) + { + super( INTEGER, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERInteger valueOf( int integer ) + { + return new DERInteger( intToOctet( integer ) ); + } + + /** + * Lazy accessor + * @return integer value + */ + public int intValue() + { + return octetToInt( value ); + } + + private static int octetToInt( byte[] bytes ) + { + return new BigInteger( bytes ).intValue(); + } + + private static byte[] intToOctet( int integer ) + { + return BigInteger.valueOf( integer ).toByteArray(); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERNull.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERNull.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERNull.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,32 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * A NULL object. + */ +public class DERNull extends DERObject +{ + private static final byte[] zeroBytes = new byte[ 0 ]; + + public DERNull() + { + super( NULL, zeroBytes ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERNumericString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERNumericString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERNumericString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * DER NumericString - a string of ASCII numeric characters { 0,1,2,3,4,5,6,7,8,9 }. + */ +public class DERNumericString extends DERString +{ + /** + * Basic DERObject constructor. + */ + DERNumericString( byte[] value ) + { + super( NUMERIC_STRING, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERNumericString valueOf( String string ) + { + return new DERNumericString( stringToByteArray( string )); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERObject.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERObject.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERObject.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,123 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.IOException; +import java.util.Arrays; + +/** + * DER object. + */ +public abstract class DERObject implements DEREncodable +{ + static final int TERMINATOR = 0x00; + static final int BOOLEAN = 0x01; + static final int INTEGER = 0x02; + static final int BIT_STRING = 0x03; + static final int OCTET_STRING = 0x04; + static final int NULL = 0x05; + static final int OBJECT_IDENTIFIER = 0x06; + static final int EXTERNAL = 0x08; + static final int ENUMERATED = 0x0a; + static final int SEQUENCE = 0x10; + static final int SET = 0x11; + static final int NUMERIC_STRING = 0x12; + static final int PRINTABLE_STRING = 0x13; + static final int T61_STRING = 0x14; + static final int VIDEOTEX_STRING = 0x15; + static final int IA5_STRING = 0x16; + static final int UTC_TIME = 0x17; + static final int GENERALIZED_TIME = 0x18; + static final int GRAPHIC_STRING = 0x19; + static final int VISIBLE_STRING = 0x1a; + static final int GENERAL_STRING = 0x1b; + static final int UNIVERSAL_STRING = 0x1c; + static final int BMP_STRING = 0x1e; + static final int UTF8_STRING = 0x0c; + static final int CONSTRUCTED = 0x20; + static final int APPLICATION = 0x40; + static final int TAGGED = 0x80; + + protected int tag; + protected byte[] value; + + /** + * Basic DERObject constructor. + */ + protected DERObject( int tag, byte[] value ) + { + this.tag = tag; + this.value = value; + } + + public void encode( ASN1OutputStream out ) + throws IOException + { + out.writeEncoded( tag, value ); + } + + byte[] getOctets() + { + return value; + } + + /** + * Fast rotate left and XOR hashcode generator. + * @return a hash code for the byte array backing this object. + */ + public int hashCode() + { + int hash = 0; + int len = value.length; + + for ( int i = 0; i < len; i++ ) + { + // rotate left and xor + hash <<= 1; + if ( hash < 0 ) + { + hash |= 1; + } + hash ^= value[ i ]; + } + + return hash; + } + + /** + * Two DERObjects are equal if their underlying byte arrays are equal. + * @return true if the two DERObject underlying byte arrays are equal. + */ + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + + if ( !( o instanceof DERObject ) ) + { + return false; + } + + DERObject that = (DERObject)o; + + return Arrays.equals( this.value, that.value ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERObjectIdentifier.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERObjectIdentifier.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERObjectIdentifier.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,131 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; + + +public class DERObjectIdentifier extends DERObject +{ + String identifier; + + DERObjectIdentifier( byte[] bytes ) + { + super( OBJECT_IDENTIFIER, bytes ); + + StringBuffer objId = new StringBuffer(); + long value = 0; + boolean first = true; + + for ( int i = 0; i != bytes.length; i++ ) + { + int b = bytes[ i ] & 0xff; + + value = value * 128 + ( b & 0x7f ); + if ( ( b & 0x80 ) == 0 ) // end of number reached + { + if ( first ) + { + switch ( (int)value / 40 ) + { + case 0: + objId.append( '0' ); + break; + case 1: + objId.append( '1' ); + value -= 40; + break; + default: + objId.append( '2' ); + value -= 80; + } + first = false; + } + + objId.append( '.' ); + objId.append( Long.toString( value ) ); + value = 0; + } + } + + this.identifier = objId.toString(); + } + + private void writeField( OutputStream out, long fieldValue ) + throws IOException + { + if ( fieldValue >= ( 1 << 7 ) ) + { + if ( fieldValue >= ( 1 << 14 ) ) + { + if ( fieldValue >= ( 1 << 21 ) ) + { + if ( fieldValue >= ( 1 << 28 ) ) + { + if ( fieldValue >= ( 1 << 35 ) ) + { + if ( fieldValue >= ( 1 << 42 ) ) + { + if ( fieldValue >= ( 1 << 49 ) ) + { + if ( fieldValue >= ( 1 << 56 ) ) + { + out.write( (int)( fieldValue >> 56 ) | 0x80 ); + } + out.write( (int)( fieldValue >> 49 ) | 0x80 ); + } + out.write( (int)( fieldValue >> 42 ) | 0x80 ); + } + out.write( (int)( fieldValue >> 35 ) | 0x80 ); + } + out.write( (int)( fieldValue >> 28 ) | 0x80 ); + } + out.write( (int)( fieldValue >> 21 ) | 0x80 ); + } + out.write( (int)( fieldValue >> 14 ) | 0x80 ); + } + out.write( (int)( fieldValue >> 7 ) | 0x80 ); + } + out.write( (int)fieldValue & 0x7f ); + } + + public void encode( ASN1OutputStream out ) + throws IOException + { + OIDTokenizer tok = new OIDTokenizer( identifier ); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ASN1OutputStream aos = new ASN1OutputStream( baos ); + + writeField( baos, Integer.parseInt( tok.nextToken() ) * 40 + + Integer.parseInt( tok.nextToken() ) ); + + while ( tok.hasMoreTokens() ) + { + writeField( baos, Long.parseLong( tok.nextToken() ) ); + } + + aos.close(); + + byte[] bytes = baos.toByteArray(); + + out.writeEncoded( OBJECT_IDENTIFIER, bytes ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEROctetString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEROctetString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DEROctetString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,38 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * DER Octet String object. + */ +public class DEROctetString extends DERObject +{ + /** + * Basic DERObject constructor. + */ + public DEROctetString( byte[] value ) + { + super( OCTET_STRING, value ); + } + + public byte[] getOctets() + { + return value; + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERPrintableString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERPrintableString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERPrintableString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * DER PrintableString object. + */ +public class DERPrintableString extends DERString +{ + /** + * Basic DERObject constructor. + */ + public DERPrintableString( byte[] value ) + { + super( PRINTABLE_STRING, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERPrintableString valueOf( String string ) + { + return new DERPrintableString( stringToByteArray( string ) ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERSequence.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERSequence.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERSequence.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,76 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Vector; + + +public class DERSequence implements DEREncodable +{ + private Vector v = new Vector(); + + public void add( DEREncodable obj ) + { + v.addElement( obj ); + } + + public Enumeration getObjects() + { + return v.elements(); + } + + public DEREncodable get( int i ) + { + return (DEREncodable)v.elementAt( i ); + } + + public int size() + { + return v.size(); + } + + /** + * As DER requires the constructed, definite-length model to + * be used for structured types, this varies slightly from the + * ASN.1 descriptions given. Rather than just outputing SEQUENCE, + * we also have to specify CONSTRUCTED, and the objects length. + */ + public void encode( ASN1OutputStream out ) + throws IOException + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ASN1OutputStream aos = new ASN1OutputStream( baos ); + + Enumeration e = getObjects(); + + while ( e.hasMoreElements() ) + { + aos.writeObject( e.nextElement() ); + } + + aos.close(); + + byte[] bytes = baos.toByteArray(); + + out.writeEncoded( DERObject.SEQUENCE | DERObject.CONSTRUCTED, bytes ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERSet.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERSet.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERSet.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,72 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Vector; + +/** + * A DER encoded set object + */ +public class DERSet implements DEREncodable +{ + protected Vector set = new Vector(); + + public Enumeration getObjects() + { + return set.elements(); + } + + public DEREncodable getObjectAt( int index ) + { + return (DEREncodable)set.elementAt( index ); + } + + public int size() + { + return set.size(); + } + + public void add( DEREncodable obj ) + { + set.addElement( obj ); + } + + public void encode( ASN1OutputStream out ) + throws IOException + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ASN1OutputStream aos = new ASN1OutputStream( baos ); + + Enumeration e = getObjects(); + + while ( e.hasMoreElements() ) + { + aos.writeObject( e.nextElement() ); + } + + aos.close(); + + byte[] bytes = baos.toByteArray(); + + out.writeEncoded( DERObject.SET | DERObject.CONSTRUCTED, bytes ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,77 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * Interface for DER string objects. + */ +public abstract class DERString extends DERObject +{ + /** + * Basic DERObject constructor. + */ + DERString( int tag, byte[] value ) + { + super( tag, value ); + } + + /** + * Lazy accessor. + * @return underlying byte array converted to a String + */ + public String getString() + { + return byteArrayToString( value ); + } + + /** + * Utility method for converting byte arrays to Strings. + * @param bytes + * @return String + */ + protected static String byteArrayToString( byte[] bytes ) + { + char[] characters = new char[ bytes.length ]; + + for ( int ii = 0; ii < characters.length; ii++ ) + { + characters[ ii ] = (char)( bytes[ ii ] & 0xff ); + } + + return new String( characters ); + } + + /** + * Utility method for converting Strings to bytes. + * @param string + * @return bytes + */ + protected static byte[] stringToByteArray( String string ) + { + char[] characters = string.toCharArray(); + byte[] bytes = new byte[ characters.length ]; + + for ( int ii = 0; ii < characters.length; ii++ ) + { + bytes[ ii ] = (byte)characters[ ii ]; + } + + return bytes; + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERTaggedObject.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERTaggedObject.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERTaggedObject.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,125 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * DER TaggedObject + */ +public class DERTaggedObject implements DEREncodable +{ + protected int tag; + protected boolean empty = false; + protected boolean explicit = true; + protected DEREncodable obj = null; + + /** + * @param tag the tag number for this object. + * @param obj the tagged object. + */ + public DERTaggedObject( int tag, DEREncodable obj ) + { + this.explicit = true; + this.tag = tag; + this.obj = obj; + } + + /** + * @param explicit true if an explicitly tagged object. + * @param tagNo the tag number for this object. + * @param obj the tagged object. + */ + public DERTaggedObject( boolean explicit, int tag, DEREncodable obj ) + { + this.explicit = explicit; + this.tag = tag; + this.obj = obj; + } + + /** + * create an implicitly tagged object that contains a zero + * length sequence. + */ + public DERTaggedObject( int tag ) + { + this( false, tag, new DERSequence() ); + } + + public int getTagNo() + { + return tag; + } + + /** + * return whatever was following the tag. + * <p> + * Note: tagged objects are generally context dependent if you're + * trying to extract a tagged object you should be going via the + * appropriate getInstance method. + */ + public DEREncodable getObject() + { + if ( obj != null ) + { + return obj; + } + + return null; + } + + public void encode( ASN1OutputStream out ) + throws IOException + { + if ( !empty ) + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ASN1OutputStream aos = new ASN1OutputStream( baos ); + + aos.writeObject( obj ); + aos.close(); + + byte[] bytes = baos.toByteArray(); + + if ( explicit ) + { + out.writeEncoded( DERObject.CONSTRUCTED | DERObject.TAGGED | tag, bytes ); + } + else + { + // need to mark constructed types + if ( ( bytes[ 0 ] & DERObject.CONSTRUCTED ) != 0 ) + { + bytes[ 0 ] = (byte)( DERObject.CONSTRUCTED | DERObject.TAGGED | tag ); + } + else + { + bytes[ 0 ] = (byte)( DERObject.TAGGED | tag ); + } + + out.write( bytes ); + } + } + else + { + out.writeEncoded( DERObject.CONSTRUCTED | DERObject.TAGGED | tag, new byte[ 0 ] ); + } + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERTeletexString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERTeletexString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERTeletexString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * DER Teletex String + */ +public class DERTeletexString extends DERString +{ + /** + * Basic DERObject constructor. + */ + public DERTeletexString( byte[] value ) + { + super( T61_STRING, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERTeletexString valueOf( String string ) + { + return new DERTeletexString( stringToByteArray( string ) ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUTCTime.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUTCTime.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUTCTime.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,71 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +/** + * DER UTC time object. + */ +public class DERUTCTime extends DERString +{ + private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" ); + private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyMMddHHmmss'Z'" ); + + static + { + dateFormat.setTimeZone( UTC_TIME_ZONE ); + } + + /** + * Basic DERObject constructor. + */ + public DERUTCTime( byte[] value ) + { + super( UTC_TIME, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERUTCTime valueOf( Date date ) + { + String dateString = dateFormat.format( date ); + + byte[] bytes = stringToByteArray( dateString ); + + return new DERUTCTime( bytes ); + } + + /** + * Lazy accessor + * @return Date representation of this DER UTC Time + * @throws ParseException + */ + public Date getDate() + throws ParseException + { + String string = byteArrayToString( value ); + + return dateFormat.parse( string ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUTF8String.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUTF8String.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUTF8String.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * DER UTF8String object. + */ +public class DERUTF8String extends DERString +{ + /** + * Basic DERObject constructor. + */ + public DERUTF8String( byte[] value ) + { + super( UTF8_STRING, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERUTF8String valueOf( String string ) + { + return new DERUTF8String( stringToByteArray( string ) ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUniversalString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUniversalString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUniversalString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,65 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * DER UniversalString object. + */ +public class DERUniversalString extends DERString +{ + private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + + /** + * Basic DERObject constructor. + */ + public DERUniversalString( byte[] value ) + { + super( UNIVERSAL_STRING, value ); + } + + public String getString() + { + StringBuffer buf = new StringBuffer( "#" ); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ASN1OutputStream aos = new ASN1OutputStream( baos ); + + try + { + aos.writeObject( this ); + } + catch (IOException e) + { + throw new RuntimeException( "Internal error encoding BitString." ); + } + + byte[] string = baos.toByteArray(); + + for ( int i = 0; i < string.length; i++ ) + { + buf.append( table[ ( string[ i ] >>> 4 ) % 0xf ] ); + buf.append( table[ string[ i ] & 0xf ] ); + } + + return buf.toString(); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUnknownTag.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUnknownTag.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERUnknownTag.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,33 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * Placeholder for unrecognized tags. + */ +public class DERUnknownTag extends DERObject +{ + /** + * Basic DERObject constructor. + */ + public DERUnknownTag( int tag, byte[] value ) + { + super( tag, value ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERVisibleString.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERVisibleString.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/DERVisibleString.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * DER VisibleString object. + */ +public class DERVisibleString extends DERString +{ + /** + * Basic DERObject constructor. + */ + public DERVisibleString( byte[] value ) + { + super( VISIBLE_STRING, value ); + } + + /** + * Static factory method, type-conversion operator. + */ + public static DERVisibleString valueOf( String string ) + { + return new DERVisibleString( stringToByteArray( string ) ); + } +} + Added: incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/OIDTokenizer.java Url: http://svn.apache.org/viewcvs/incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/OIDTokenizer.java?view=auto&rev=125809 ============================================================================== --- (empty file) +++ incubator/directory/asn1/trunk/der/src/java/org/apache/asn1/der/OIDTokenizer.java Thu Jan 20 12:01:38 2005 @@ -0,0 +1,65 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.asn1.der; + +/** + * class for breaking up an OID into it's component tokens, ala + * java.util.StringTokenizer. We need this class as some of the + * lightweight Java environment don't support classes like + * StringTokenizer. + */ +public class OIDTokenizer +{ + private String oid; + private int index; + + public OIDTokenizer( String oid ) + { + this.oid = oid; + this.index = 0; + } + + public boolean hasMoreTokens() + { + return ( index != -1 ); + } + + public String nextToken() + { + if ( index == -1 ) + { + return null; + } + + String token; + int end = oid.indexOf( '.', index ); + + if ( end == -1 ) + { + token = oid.substring( index ); + index = -1; + return token; + } + + token = oid.substring( index, end ); + + index = end + 1; + return token; + } +} +
