home *** CD-ROM | disk | FTP | other *** search
- package sun.security.util;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
-
- public class DerValue {
- public byte tag;
- protected DerInputBuffer buffer;
- public DerInputStream data;
- private int length;
- public static final byte tag_Integer = 2;
- public static final byte tag_BitString = 3;
- public static final byte tag_OctetString = 4;
- public static final byte tag_Null = 5;
- public static final byte tag_ObjectId = 6;
- public static final byte tag_PrintableString = 19;
- public static final byte tag_IA5String = 22;
- public static final byte tag_UtcTime = 23;
- public static final byte tag_Sequence = 48;
- public static final byte tag_SequenceOf = 48;
- public static final byte tag_Set = 49;
- public static final byte tag_SetOf = 49;
-
- boolean isUniversal() {
- return (this.tag & 192) == 0;
- }
-
- boolean isApplication() {
- return (this.tag & 192) == 64;
- }
-
- public boolean isContextSpecific() {
- return (this.tag & 192) == 128;
- }
-
- boolean isPrivate() {
- return (this.tag & 192) == 192;
- }
-
- public boolean isConstructed() {
- return (this.tag & 32) == 32;
- }
-
- public DerValue(String var1) throws IOException {
- this.tag = 19;
- this.length = var1.length();
- byte[] var3 = new byte[this.length];
-
- for(int var2 = 0; var2 < this.length; ++var2) {
- var3[var2] = (byte)var1.charAt(var2);
- }
-
- this.buffer = new DerInputBuffer(var3);
- this.data = new DerInputStream(this.buffer);
- this.data.mark(Integer.MAX_VALUE);
- }
-
- public DerValue(byte var1, byte[] var2) {
- this.tag = var1;
- this.buffer = new DerInputBuffer(var2);
- this.length = var2.length;
- this.data = new DerInputStream(this.buffer);
- this.data.mark(Integer.MAX_VALUE);
- }
-
- DerValue(DerInputBuffer var1) throws IOException {
- this.tag = (byte)((ByteArrayInputStream)var1).read();
- this.length = DerInputStream.getLength(var1);
- this.buffer = var1.dup();
- this.buffer.truncate(this.length);
- this.data = new DerInputStream(this.buffer);
- ((ByteArrayInputStream)var1).skip((long)this.length);
- }
-
- public DerValue(byte[] var1) throws IOException {
- this.init(true, new ByteArrayInputStream(var1));
- }
-
- public DerValue(byte[] var1, int var2, int var3) throws IOException {
- this.init(true, new ByteArrayInputStream(var1, var2, var3));
- }
-
- public DerValue(InputStream var1) throws IOException {
- this.init(false, var1);
- }
-
- private void init(boolean var1, InputStream var2) throws IOException {
- this.tag = (byte)var2.read();
- this.length = DerInputStream.getLength(var2);
- if (this.length != 0) {
- if (var1 && var2.available() != this.length) {
- throw new IOException("extra DER value data (constructor)");
- } else {
- byte[] var3 = new byte[this.length];
- DataInputStream var4 = new DataInputStream(var2);
- var4.readFully(var3);
- this.buffer = new DerInputBuffer(var3);
- this.data = new DerInputStream(this.buffer);
- }
- }
- }
-
- public void emit(DerOutputStream var1) throws IOException {
- ((ByteArrayOutputStream)var1).write(this.tag);
- var1.putLength(this.length);
- if (this.length > 0) {
- byte[] var2 = new byte[this.length];
- this.buffer.reset();
- if (this.buffer.read(var2) != this.length) {
- throw new IOException("short DER value read (emit)");
- }
-
- ((OutputStream)var1).write(var2);
- }
-
- }
-
- public ObjectIdentifier getOID() throws IOException {
- if (this.tag != 6) {
- throw new IOException("DerValue.getOID, not an OID " + this.tag);
- } else {
- return new ObjectIdentifier(this.buffer);
- }
- }
-
- public byte[] getOctetString() throws IOException {
- if (this.tag != 4) {
- throw new IOException("DerValue.getOctetString, not an Octet String: " + this.tag);
- } else {
- byte[] var1 = new byte[this.length];
- if (this.buffer.read(var1) != this.length) {
- throw new IOException("short read on DerValue buffer");
- } else {
- return var1;
- }
- }
- }
-
- public BigInt getInteger() throws IOException {
- if (this.tag != 2) {
- throw new IOException("DerValue.getInteger, not an int " + this.tag);
- } else {
- return this.buffer.getUnsigned(this.data.available());
- }
- }
-
- public byte[] getBitString() throws IOException {
- if (this.tag != 3) {
- throw new IOException("DerValue.getBitString, not a bit string " + this.tag);
- } else {
- return this.buffer.getBitString();
- }
- }
-
- public String getPrintableString() throws IOException {
- if (this.tag != 19) {
- throw new IOException("DerValue.getPrintableString, not a string " + this.tag);
- } else {
- StringBuffer var1 = new StringBuffer(this.length);
-
- try {
- int var2 = this.length;
- this.data.reset();
-
- while(var2-- > 0) {
- var1.append((char)this.data.getByte());
- }
- } catch (IOException var3) {
- return null;
- }
-
- return new String(var1);
- }
- }
-
- public String getIA5String() throws IOException {
- if (this.tag != 22) {
- throw new IOException("DerValue.getPrintableString, not a string " + this.tag);
- } else {
- StringBuffer var1 = new StringBuffer(this.length);
-
- try {
- int var2 = this.length;
- this.data.reset();
-
- while(var2-- > 0) {
- var1.append((char)this.data.getByte());
- }
- } catch (IOException var3) {
- return null;
- }
-
- return new String(var1);
- }
- }
-
- public boolean equals(Object var1) {
- return var1 instanceof DerValue ? this.equals((DerValue)var1) : false;
- }
-
- public boolean equals(DerValue var1) {
- this.data.reset();
- var1.data.reset();
- if (this == var1) {
- return true;
- } else {
- return this.tag != var1.tag ? false : this.buffer.equals(var1.buffer);
- }
- }
-
- public String toString() {
- try {
- if (this.tag == 19) {
- return "\"" + this.getPrintableString() + "\"";
- } else if (this.tag == 5) {
- return "[DerValue, null]";
- } else {
- return this.tag == 6 ? "OID." + this.getOID() : "[DerValue, tag = " + this.tag + ", length = " + this.length + "]";
- }
- } catch (IOException var1) {
- throw new IllegalArgumentException("misformatted DER value");
- }
- }
-
- public byte[] toByteArray() throws IOException {
- DerOutputStream var1 = new DerOutputStream();
- this.emit(var1);
- this.data.reset();
- return ((ByteArrayOutputStream)var1).toByteArray();
- }
-
- public DerInputStream toDerInputStream() throws IOException {
- if (this.tag != 48 && this.tag != 49) {
- throw new IOException("toDerInputStream rejects tag type " + this.tag);
- } else {
- return new DerInputStream(this.buffer);
- }
- }
- }
-