first commit
This commit is contained in:
commit
4d332ef662
27586 changed files with 3281783 additions and 0 deletions
|
|
@ -0,0 +1,11 @@
|
|||
package org.bouncycastle;
|
||||
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class LICENSE {
|
||||
public static String licenseText = "Copyright (c) 2000-2015 The Legion of the Bouncy Castle Inc. (http://www.bouncycastle.org) " + Strings.lineSeparator() + Strings.lineSeparator() + "Permission is hereby granted, free of charge, to any person obtaining a copy of this software " + Strings.lineSeparator() + "and associated documentation files (the \"Software\"), to deal in the Software without restriction, " + Strings.lineSeparator() + "including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, " + Strings.lineSeparator() + "and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so," + Strings.lineSeparator() + "subject to the following conditions:" + Strings.lineSeparator() + Strings.lineSeparator() + "The above copyright notice and this permission notice shall be included in all copies or substantial" + Strings.lineSeparator() + "portions of the Software." + Strings.lineSeparator() + Strings.lineSeparator() + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED," + Strings.lineSeparator() + "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR" + Strings.lineSeparator() + "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE" + Strings.lineSeparator() + "LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR" + Strings.lineSeparator() + "OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER" + Strings.lineSeparator() + "DEALINGS IN THE SOFTWARE.";
|
||||
|
||||
public static void main(String[] paramArrayOfString) {
|
||||
System.out.println(licenseText);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
|
||||
public abstract class ASN1ApplicationSpecific extends ASN1Primitive {
|
||||
protected final boolean isConstructed;
|
||||
|
||||
protected final int tag;
|
||||
|
||||
protected final byte[] octets;
|
||||
|
||||
ASN1ApplicationSpecific(boolean paramBoolean, int paramInt, byte[] paramArrayOfbyte) {
|
||||
this.isConstructed = paramBoolean;
|
||||
this.tag = paramInt;
|
||||
this.octets = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public static ASN1ApplicationSpecific getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1ApplicationSpecific)
|
||||
return (ASN1ApplicationSpecific)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return getInstance(ASN1Primitive.fromByteArray((byte[])paramObject));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("Failed to construct object from byte[]: " + e.getMessage());
|
||||
}
|
||||
throw new IllegalArgumentException("unknown object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
protected static int getLengthOfHeader(byte[] paramArrayOfbyte) {
|
||||
int i = paramArrayOfbyte[1] & 0xFF;
|
||||
if (i == 128)
|
||||
return 2;
|
||||
if (i > 127) {
|
||||
int j = i & 0x7F;
|
||||
if (j > 4)
|
||||
throw new IllegalStateException("DER length more than 4 bytes: " + j);
|
||||
return j + 2;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
public boolean isConstructed() {
|
||||
return this.isConstructed;
|
||||
}
|
||||
|
||||
public byte[] getContents() {
|
||||
return this.octets;
|
||||
}
|
||||
|
||||
public int getApplicationTag() {
|
||||
return this.tag;
|
||||
}
|
||||
|
||||
public ASN1Primitive getObject() throws IOException {
|
||||
return new ASN1InputStream(getContents()).readObject();
|
||||
}
|
||||
|
||||
public ASN1Primitive getObject(int paramInt) throws IOException {
|
||||
if (paramInt >= 31)
|
||||
throw new IOException("unsupported tag number");
|
||||
byte[] arrayOfByte1 = getEncoded();
|
||||
byte[] arrayOfByte2 = replaceTagNumber(paramInt, arrayOfByte1);
|
||||
if ((arrayOfByte1[0] & 0x20) != 0)
|
||||
arrayOfByte2[0] = (byte)(arrayOfByte2[0] | 0x20);
|
||||
return new ASN1InputStream(arrayOfByte2).readObject();
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
return StreamUtil.calculateTagLength(this.tag) + StreamUtil.calculateBodyLength(this.octets.length) + this.octets.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
int i = 64;
|
||||
if (this.isConstructed)
|
||||
i |= 0x20;
|
||||
paramASN1OutputStream.writeEncoded(i, this.tag, this.octets);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof ASN1ApplicationSpecific))
|
||||
return false;
|
||||
ASN1ApplicationSpecific aSN1ApplicationSpecific = (ASN1ApplicationSpecific)paramASN1Primitive;
|
||||
return (this.isConstructed == aSN1ApplicationSpecific.isConstructed && this.tag == aSN1ApplicationSpecific.tag && Arrays.areEqual(this.octets, aSN1ApplicationSpecific.octets));
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return (this.isConstructed ? 1 : 0) ^ this.tag ^ Arrays.hashCode(this.octets);
|
||||
}
|
||||
|
||||
private byte[] replaceTagNumber(int paramInt, byte[] paramArrayOfbyte) throws IOException {
|
||||
int i = paramArrayOfbyte[0] & 0x1F;
|
||||
int j = 1;
|
||||
if (i == 31) {
|
||||
i = 0;
|
||||
int k = paramArrayOfbyte[j++] & 0xFF;
|
||||
if ((k & 0x7F) == 0)
|
||||
throw new ASN1ParsingException("corrupted stream - invalid high tag number found");
|
||||
while (k >= 0 && (k & 0x80) != 0) {
|
||||
i |= k & 0x7F;
|
||||
i <<= 7;
|
||||
k = paramArrayOfbyte[j++] & 0xFF;
|
||||
}
|
||||
}
|
||||
byte[] arrayOfByte = new byte[paramArrayOfbyte.length - j + 1];
|
||||
System.arraycopy(paramArrayOfbyte, j, arrayOfByte, 1, arrayOfByte.length - 1);
|
||||
arrayOfByte[0] = (byte)paramInt;
|
||||
return arrayOfByte;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface ASN1ApplicationSpecificParser extends ASN1Encodable, InMemoryRepresentable {
|
||||
ASN1Encodable readObject() throws IOException;
|
||||
}
|
||||
|
|
@ -0,0 +1,152 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
|
||||
public abstract class ASN1BitString extends ASN1Primitive implements ASN1String {
|
||||
private static final char[] table = new char[] {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F' };
|
||||
|
||||
protected final byte[] data;
|
||||
|
||||
protected final int padBits;
|
||||
|
||||
protected static int getPadBits(int paramInt) {
|
||||
int i = 0;
|
||||
int j;
|
||||
for (j = 3; j >= 0; j--) {
|
||||
if (j != 0) {
|
||||
if (paramInt >> j * 8 != 0) {
|
||||
i = paramInt >> j * 8 & 0xFF;
|
||||
break;
|
||||
}
|
||||
} else if (paramInt != 0) {
|
||||
i = paramInt & 0xFF;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == 0)
|
||||
return 0;
|
||||
for (j = 1; ((i <<= 1) & 0xFF) != 0; j++);
|
||||
return 8 - j;
|
||||
}
|
||||
|
||||
protected static byte[] getBytes(int paramInt) {
|
||||
if (paramInt == 0)
|
||||
return new byte[0];
|
||||
int i = 4;
|
||||
for (int j = 3; j >= 1 && (paramInt & 255 << j * 8) == 0; j--)
|
||||
i--;
|
||||
byte[] arrayOfByte = new byte[i];
|
||||
for (int k = 0; k < i; k++)
|
||||
arrayOfByte[k] = (byte)(paramInt >> k * 8 & 0xFF);
|
||||
return arrayOfByte;
|
||||
}
|
||||
|
||||
public ASN1BitString(byte[] paramArrayOfbyte, int paramInt) {
|
||||
if (paramArrayOfbyte == null)
|
||||
throw new NullPointerException("data cannot be null");
|
||||
if (paramArrayOfbyte.length == 0 && paramInt != 0)
|
||||
throw new IllegalArgumentException("zero length data with non-zero pad bits");
|
||||
if (paramInt > 7 || paramInt < 0)
|
||||
throw new IllegalArgumentException("pad bits cannot be greater than 7 or less than 0");
|
||||
this.data = Arrays.clone(paramArrayOfbyte);
|
||||
this.padBits = paramInt;
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
StringBuffer stringBuffer = new StringBuffer("#");
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
ASN1OutputStream aSN1OutputStream = new ASN1OutputStream(byteArrayOutputStream);
|
||||
try {
|
||||
aSN1OutputStream.writeObject(this);
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException("Internal error encoding BitString: " + e.getMessage(), e);
|
||||
}
|
||||
byte[] arrayOfByte = byteArrayOutputStream.toByteArray();
|
||||
for (int i = 0; i != arrayOfByte.length; i++) {
|
||||
stringBuffer.append(table[arrayOfByte[i] >>> 4 & 0xF]);
|
||||
stringBuffer.append(table[arrayOfByte[i] & 0xF]);
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
|
||||
public int intValue() {
|
||||
int i = 0;
|
||||
byte[] arrayOfByte = this.data;
|
||||
if (this.padBits > 0 && this.data.length <= 4)
|
||||
arrayOfByte = derForm(this.data, this.padBits);
|
||||
for (int j = 0; j != arrayOfByte.length && j != 4; j++)
|
||||
i |= (arrayOfByte[j] & 0xFF) << 8 * j;
|
||||
return i;
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
if (this.padBits != 0)
|
||||
throw new IllegalStateException("attempt to get non-octet aligned data from BIT STRING");
|
||||
return Arrays.clone(this.data);
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return derForm(this.data, this.padBits);
|
||||
}
|
||||
|
||||
public int getPadBits() {
|
||||
return this.padBits;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.padBits ^ Arrays.hashCode(getBytes());
|
||||
}
|
||||
|
||||
protected boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof ASN1BitString))
|
||||
return false;
|
||||
ASN1BitString aSN1BitString = (ASN1BitString)paramASN1Primitive;
|
||||
return (this.padBits == aSN1BitString.padBits && Arrays.areEqual(getBytes(), aSN1BitString.getBytes()));
|
||||
}
|
||||
|
||||
protected static byte[] derForm(byte[] paramArrayOfbyte, int paramInt) {
|
||||
byte[] arrayOfByte = Arrays.clone(paramArrayOfbyte);
|
||||
if (paramInt > 0)
|
||||
arrayOfByte[paramArrayOfbyte.length - 1] = (byte)(arrayOfByte[paramArrayOfbyte.length - 1] & 255 << paramInt);
|
||||
return arrayOfByte;
|
||||
}
|
||||
|
||||
static ASN1BitString fromInputStream(int paramInt, InputStream paramInputStream) throws IOException {
|
||||
if (paramInt < 1)
|
||||
throw new IllegalArgumentException("truncated BIT STRING detected");
|
||||
int i = paramInputStream.read();
|
||||
byte[] arrayOfByte = new byte[paramInt - 1];
|
||||
if (arrayOfByte.length != 0) {
|
||||
if (Streams.readFully(paramInputStream, arrayOfByte) != arrayOfByte.length)
|
||||
throw new EOFException("EOF encountered in middle of BIT STRING");
|
||||
if (i > 0 && i < 8 && arrayOfByte[arrayOfByte.length - 1] != (byte)(arrayOfByte[arrayOfByte.length - 1] & 255 << i))
|
||||
return new DLBitString(arrayOfByte, i);
|
||||
}
|
||||
return new DERBitString(arrayOfByte, i);
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() {
|
||||
return toASN1Primitive();
|
||||
}
|
||||
|
||||
ASN1Primitive toDERObject() {
|
||||
return new DERBitString(this.data, this.padBits);
|
||||
}
|
||||
|
||||
ASN1Primitive toDLObject() {
|
||||
return new DLBitString(this.data, this.padBits);
|
||||
}
|
||||
|
||||
abstract void encode(ASN1OutputStream paramASN1OutputStream) throws IOException;
|
||||
}
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
|
||||
public class ASN1Boolean extends ASN1Primitive {
|
||||
private static final byte[] TRUE_VALUE = new byte[] { -1 };
|
||||
|
||||
private static final byte[] FALSE_VALUE = new byte[] { 0 };
|
||||
|
||||
private final byte[] value;
|
||||
|
||||
public static final ASN1Boolean FALSE = new ASN1Boolean(false);
|
||||
|
||||
public static final ASN1Boolean TRUE = new ASN1Boolean(true);
|
||||
|
||||
public static ASN1Boolean getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1Boolean)
|
||||
return (ASN1Boolean)paramObject;
|
||||
if (paramObject instanceof byte[]) {
|
||||
byte[] arrayOfByte = (byte[])paramObject;
|
||||
try {
|
||||
return (ASN1Boolean)fromByteArray(arrayOfByte);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("failed to construct boolean from byte[]: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static ASN1Boolean getInstance(boolean paramBoolean) {
|
||||
return paramBoolean ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
public static ASN1Boolean getInstance(int paramInt) {
|
||||
return (paramInt != 0) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
public static ASN1Boolean getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof ASN1Boolean) ? getInstance(aSN1Primitive) : fromOctetString(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
ASN1Boolean(byte[] paramArrayOfbyte) {
|
||||
if (paramArrayOfbyte.length != 1)
|
||||
throw new IllegalArgumentException("byte value should have 1 byte in it");
|
||||
if (paramArrayOfbyte[0] == 0) {
|
||||
this.value = FALSE_VALUE;
|
||||
} else if ((paramArrayOfbyte[0] & 0xFF) == 255) {
|
||||
this.value = TRUE_VALUE;
|
||||
} else {
|
||||
this.value = Arrays.clone(paramArrayOfbyte);
|
||||
}
|
||||
}
|
||||
|
||||
public ASN1Boolean(boolean paramBoolean) {
|
||||
this.value = paramBoolean ? TRUE_VALUE : FALSE_VALUE;
|
||||
}
|
||||
|
||||
public boolean isTrue() {
|
||||
return (this.value[0] != 0);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(1, this.value);
|
||||
}
|
||||
|
||||
protected boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
return (paramASN1Primitive instanceof ASN1Boolean) ? ((this.value[0] == ((ASN1Boolean)paramASN1Primitive).value[0])) : false;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.value[0];
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return (this.value[0] != 0) ? "TRUE" : "FALSE";
|
||||
}
|
||||
|
||||
static ASN1Boolean fromOctetString(byte[] paramArrayOfbyte) {
|
||||
if (paramArrayOfbyte.length != 1)
|
||||
throw new IllegalArgumentException("BOOLEAN value should have 1 byte in it");
|
||||
return (paramArrayOfbyte[0] == 0) ? FALSE : (((paramArrayOfbyte[0] & 0xFF) == 255) ? TRUE : new ASN1Boolean(paramArrayOfbyte));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public interface ASN1Choice {}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public interface ASN1Encodable {
|
||||
ASN1Primitive toASN1Primitive();
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Vector;
|
||||
|
||||
public class ASN1EncodableVector {
|
||||
private final Vector v = new Vector();
|
||||
|
||||
public void add(ASN1Encodable paramASN1Encodable) {
|
||||
this.v.addElement(paramASN1Encodable);
|
||||
}
|
||||
|
||||
public void addAll(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
Enumeration enumeration = paramASN1EncodableVector.v.elements();
|
||||
while (enumeration.hasMoreElements())
|
||||
this.v.addElement(enumeration.nextElement());
|
||||
}
|
||||
|
||||
public ASN1Encodable get(int paramInt) {
|
||||
return this.v.elementAt(paramInt);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.v.size();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public interface ASN1Encoding {
|
||||
public static final String DER = "DER";
|
||||
|
||||
public static final String DL = "DL";
|
||||
|
||||
public static final String BER = "BER";
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
|
||||
public class ASN1Enumerated extends ASN1Primitive {
|
||||
private final byte[] bytes;
|
||||
|
||||
private static ASN1Enumerated[] cache = new ASN1Enumerated[12];
|
||||
|
||||
public static ASN1Enumerated getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1Enumerated)
|
||||
return (ASN1Enumerated)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (ASN1Enumerated)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static ASN1Enumerated getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof ASN1Enumerated) ? getInstance(aSN1Primitive) : fromOctetString(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
public ASN1Enumerated(int paramInt) {
|
||||
this.bytes = BigInteger.valueOf((long)paramInt).toByteArray();
|
||||
}
|
||||
|
||||
public ASN1Enumerated(BigInteger paramBigInteger) {
|
||||
this.bytes = paramBigInteger.toByteArray();
|
||||
}
|
||||
|
||||
public ASN1Enumerated(byte[] paramArrayOfbyte) {
|
||||
this.bytes = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public BigInteger getValue() {
|
||||
return new BigInteger(this.bytes);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.bytes.length) + this.bytes.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(10, this.bytes);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof ASN1Enumerated))
|
||||
return false;
|
||||
ASN1Enumerated aSN1Enumerated = (ASN1Enumerated)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.bytes, aSN1Enumerated.bytes);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.bytes);
|
||||
}
|
||||
|
||||
static ASN1Enumerated fromOctetString(byte[] paramArrayOfbyte) {
|
||||
if (paramArrayOfbyte.length > 1)
|
||||
return new ASN1Enumerated(Arrays.clone(paramArrayOfbyte));
|
||||
if (paramArrayOfbyte.length == 0)
|
||||
throw new IllegalArgumentException("ENUMERATED has zero length");
|
||||
int i = paramArrayOfbyte[0] & 0xFF;
|
||||
if (i >= cache.length)
|
||||
return new ASN1Enumerated(Arrays.clone(paramArrayOfbyte));
|
||||
ASN1Enumerated aSN1Enumerated = cache[i];
|
||||
if (aSN1Enumerated == null)
|
||||
aSN1Enumerated = cache[i] = new ASN1Enumerated(Arrays.clone(paramArrayOfbyte));
|
||||
return aSN1Enumerated;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ASN1Exception extends IOException {
|
||||
private Throwable cause;
|
||||
|
||||
ASN1Exception(String paramString) {
|
||||
super(paramString);
|
||||
}
|
||||
|
||||
ASN1Exception(String paramString, Throwable paramThrowable) {
|
||||
super(paramString);
|
||||
this.cause = paramThrowable;
|
||||
}
|
||||
|
||||
public Throwable getCause() {
|
||||
return this.cause;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,173 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.SimpleTimeZone;
|
||||
import java.util.TimeZone;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class ASN1GeneralizedTime extends ASN1Primitive {
|
||||
private byte[] time;
|
||||
|
||||
public static ASN1GeneralizedTime getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1GeneralizedTime)
|
||||
return (ASN1GeneralizedTime)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (ASN1GeneralizedTime)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static ASN1GeneralizedTime getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof ASN1GeneralizedTime) ? getInstance(aSN1Primitive) : new ASN1GeneralizedTime(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
public ASN1GeneralizedTime(String paramString) {
|
||||
this.time = Strings.toByteArray(paramString);
|
||||
try {
|
||||
getDate();
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("invalid date string: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public ASN1GeneralizedTime(Date paramDate) {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
|
||||
simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
|
||||
this.time = Strings.toByteArray(simpleDateFormat.format(paramDate));
|
||||
}
|
||||
|
||||
public ASN1GeneralizedTime(Date paramDate, Locale paramLocale) {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss'Z'", paramLocale);
|
||||
simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
|
||||
this.time = Strings.toByteArray(simpleDateFormat.format(paramDate));
|
||||
}
|
||||
|
||||
ASN1GeneralizedTime(byte[] paramArrayOfbyte) {
|
||||
this.time = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public String getTimeString() {
|
||||
return Strings.fromByteArray(this.time);
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
String str = Strings.fromByteArray(this.time);
|
||||
if (str.charAt(str.length() - 1) == 'Z')
|
||||
return str.substring(0, str.length() - 1) + "GMT+00:00";
|
||||
int i = str.length() - 5;
|
||||
char c = str.charAt(i);
|
||||
if (c == '-' || c == '+')
|
||||
return str.substring(0, i) + "GMT" + str.substring(i, i + 3) + ":" + str.substring(i + 3);
|
||||
i = str.length() - 3;
|
||||
c = str.charAt(i);
|
||||
return (c == '-' || c == '+') ? (str.substring(0, i) + "GMT" + str.substring(i) + ":00") : (str + calculateGMTOffset());
|
||||
}
|
||||
|
||||
private String calculateGMTOffset() {
|
||||
String str = "+";
|
||||
TimeZone timeZone = TimeZone.getDefault();
|
||||
int i = timeZone.getRawOffset();
|
||||
if (i < 0) {
|
||||
str = "-";
|
||||
i = -i;
|
||||
}
|
||||
int j = i / 3600000;
|
||||
int k = (i - j * 60 * 60 * 1000) / 60000;
|
||||
try {
|
||||
if (timeZone.useDaylightTime() && timeZone.inDaylightTime(getDate()))
|
||||
j += str.equals("+") ? 1 : -1;
|
||||
} catch (ParseException e) {}
|
||||
return "GMT" + str + convert(j) + ":" + convert(k);
|
||||
}
|
||||
|
||||
private String convert(int paramInt) {
|
||||
return (paramInt < 10) ? ("0" + paramInt) : Integer.toString(paramInt);
|
||||
}
|
||||
|
||||
public Date getDate() throws ParseException {
|
||||
SimpleDateFormat simpleDateFormat;
|
||||
String str1 = Strings.fromByteArray(this.time);
|
||||
String str2 = str1;
|
||||
if (str1.endsWith("Z")) {
|
||||
if (hasFractionalSeconds()) {
|
||||
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss.SSS'Z'");
|
||||
} else {
|
||||
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
|
||||
}
|
||||
simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
|
||||
} else if (str1.indexOf('-') > 0 || str1.indexOf('+') > 0) {
|
||||
str2 = getTime();
|
||||
if (hasFractionalSeconds()) {
|
||||
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss.SSSz");
|
||||
} else {
|
||||
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssz");
|
||||
}
|
||||
simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
|
||||
} else {
|
||||
if (hasFractionalSeconds()) {
|
||||
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
|
||||
} else {
|
||||
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
}
|
||||
simpleDateFormat.setTimeZone(new SimpleTimeZone(0, TimeZone.getDefault().getID()));
|
||||
}
|
||||
if (hasFractionalSeconds()) {
|
||||
String str = str2.substring(14);
|
||||
int i;
|
||||
for (i = 1; i < str.length(); i++) {
|
||||
char c = str.charAt(i);
|
||||
if ('0' > c || c > '9')
|
||||
break;
|
||||
}
|
||||
if (i - 1 > 3) {
|
||||
str = str.substring(0, 4) + str.substring(i);
|
||||
str2 = str2.substring(0, 14) + str;
|
||||
} else if (i - 1 == 1) {
|
||||
str = str.substring(0, i) + "00" + str.substring(i);
|
||||
str2 = str2.substring(0, 14) + str;
|
||||
} else if (i - 1 == 2) {
|
||||
str = str.substring(0, i) + "0" + str.substring(i);
|
||||
str2 = str2.substring(0, 14) + str;
|
||||
}
|
||||
}
|
||||
return simpleDateFormat.parse(str2);
|
||||
}
|
||||
|
||||
private boolean hasFractionalSeconds() {
|
||||
for (int i = 0; i != this.time.length; i++) {
|
||||
if (this.time[i] == 46 && i == 14)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
int i = this.time.length;
|
||||
return 1 + StreamUtil.calculateBodyLength(i) + i;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(24, this.time);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
return !(paramASN1Primitive instanceof ASN1GeneralizedTime) ? false : Arrays.areEqual(this.time, ((ASN1GeneralizedTime)paramASN1Primitive).time);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.time);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
public abstract class ASN1Generator {
|
||||
protected OutputStream _out;
|
||||
|
||||
public ASN1Generator(OutputStream paramOutputStream) {
|
||||
this._out = paramOutputStream;
|
||||
}
|
||||
|
||||
public abstract OutputStream getRawOutputStream();
|
||||
}
|
||||
|
|
@ -0,0 +1,254 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
|
||||
public class ASN1InputStream extends FilterInputStream implements BERTags {
|
||||
private final int limit;
|
||||
|
||||
private final boolean lazyEvaluate;
|
||||
|
||||
private final byte[][] tmpBuffers;
|
||||
|
||||
public ASN1InputStream(InputStream paramInputStream) {
|
||||
this(paramInputStream, StreamUtil.findLimit(paramInputStream));
|
||||
}
|
||||
|
||||
public ASN1InputStream(byte[] paramArrayOfbyte) {
|
||||
this((InputStream)new ByteArrayInputStream(paramArrayOfbyte), paramArrayOfbyte.length);
|
||||
}
|
||||
|
||||
public ASN1InputStream(byte[] paramArrayOfbyte, boolean paramBoolean) {
|
||||
this(new ByteArrayInputStream(paramArrayOfbyte), paramArrayOfbyte.length, paramBoolean);
|
||||
}
|
||||
|
||||
public ASN1InputStream(InputStream paramInputStream, int paramInt) {
|
||||
this(paramInputStream, paramInt, false);
|
||||
}
|
||||
|
||||
public ASN1InputStream(InputStream paramInputStream, boolean paramBoolean) {
|
||||
this(paramInputStream, StreamUtil.findLimit(paramInputStream), paramBoolean);
|
||||
}
|
||||
|
||||
public ASN1InputStream(InputStream paramInputStream, int paramInt, boolean paramBoolean) {
|
||||
super(paramInputStream);
|
||||
this.limit = paramInt;
|
||||
this.lazyEvaluate = paramBoolean;
|
||||
this.tmpBuffers = new byte[11][];
|
||||
}
|
||||
|
||||
int getLimit() {
|
||||
return this.limit;
|
||||
}
|
||||
|
||||
protected int readLength() throws IOException {
|
||||
return readLength(this, this.limit);
|
||||
}
|
||||
|
||||
protected void readFully(byte[] paramArrayOfbyte) throws IOException {
|
||||
if (Streams.readFully(this, paramArrayOfbyte) != paramArrayOfbyte.length)
|
||||
throw new EOFException("EOF encountered in middle of object");
|
||||
}
|
||||
|
||||
protected ASN1Primitive buildObject(int paramInt1, int paramInt2, int paramInt3) throws IOException {
|
||||
boolean bool = ((paramInt1 & 0x20) != 0);
|
||||
DefiniteLengthInputStream definiteLengthInputStream = new DefiniteLengthInputStream(this, paramInt3);
|
||||
if ((paramInt1 & 0x40) != 0)
|
||||
return new DERApplicationSpecific(bool, paramInt2, definiteLengthInputStream.toByteArray());
|
||||
if ((paramInt1 & 0x80) != 0)
|
||||
return new ASN1StreamParser(definiteLengthInputStream).readTaggedObject(bool, paramInt2);
|
||||
if (bool) {
|
||||
ASN1EncodableVector aSN1EncodableVector;
|
||||
ASN1OctetString[] arrayOfASN1OctetString;
|
||||
int i;
|
||||
switch (paramInt2) {
|
||||
case 4:
|
||||
aSN1EncodableVector = buildDEREncodableVector(definiteLengthInputStream);
|
||||
arrayOfASN1OctetString = new ASN1OctetString[aSN1EncodableVector.size()];
|
||||
for (i = 0; i != arrayOfASN1OctetString.length; i++)
|
||||
arrayOfASN1OctetString[i] = (ASN1OctetString)aSN1EncodableVector.get(i);
|
||||
return new BEROctetString(arrayOfASN1OctetString);
|
||||
case 16:
|
||||
return this.lazyEvaluate ? new LazyEncodedSequence(definiteLengthInputStream.toByteArray()) : DERFactory.createSequence(buildDEREncodableVector(definiteLengthInputStream));
|
||||
case 17:
|
||||
return DERFactory.createSet(buildDEREncodableVector(definiteLengthInputStream));
|
||||
case 8:
|
||||
return new DERExternal(buildDEREncodableVector(definiteLengthInputStream));
|
||||
}
|
||||
throw new IOException("unknown tag " + paramInt2 + " encountered");
|
||||
}
|
||||
return createPrimitiveDERObject(paramInt2, definiteLengthInputStream, this.tmpBuffers);
|
||||
}
|
||||
|
||||
ASN1EncodableVector buildEncodableVector() throws IOException {
|
||||
ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector();
|
||||
ASN1Primitive aSN1Primitive;
|
||||
while ((aSN1Primitive = readObject()) != null)
|
||||
aSN1EncodableVector.add(aSN1Primitive);
|
||||
return aSN1EncodableVector;
|
||||
}
|
||||
|
||||
ASN1EncodableVector buildDEREncodableVector(DefiniteLengthInputStream paramDefiniteLengthInputStream) throws IOException {
|
||||
return new ASN1InputStream(paramDefiniteLengthInputStream).buildEncodableVector();
|
||||
}
|
||||
|
||||
public ASN1Primitive readObject() throws IOException {
|
||||
int i = read();
|
||||
if (i <= 0) {
|
||||
if (i == 0)
|
||||
throw new IOException("unexpected end-of-contents marker");
|
||||
return null;
|
||||
}
|
||||
int j = readTagNumber(this, i);
|
||||
boolean bool = ((i & 0x20) != 0) ? true : false;
|
||||
int k = readLength();
|
||||
if (k < 0) {
|
||||
if (!bool)
|
||||
throw new IOException("indefinite-length primitive encoding encountered");
|
||||
IndefiniteLengthInputStream indefiniteLengthInputStream = new IndefiniteLengthInputStream(this, this.limit);
|
||||
ASN1StreamParser aSN1StreamParser = new ASN1StreamParser(indefiniteLengthInputStream, this.limit);
|
||||
if ((i & 0x40) != 0)
|
||||
return new BERApplicationSpecificParser(j, aSN1StreamParser).getLoadedObject();
|
||||
if ((i & 0x80) != 0)
|
||||
return new BERTaggedObjectParser(true, j, aSN1StreamParser).getLoadedObject();
|
||||
switch (j) {
|
||||
case 4:
|
||||
return new BEROctetStringParser(aSN1StreamParser).getLoadedObject();
|
||||
case 16:
|
||||
return new BERSequenceParser(aSN1StreamParser).getLoadedObject();
|
||||
case 17:
|
||||
return new BERSetParser(aSN1StreamParser).getLoadedObject();
|
||||
case 8:
|
||||
return new DERExternalParser(aSN1StreamParser).getLoadedObject();
|
||||
}
|
||||
throw new IOException("unknown BER object encountered");
|
||||
}
|
||||
try {
|
||||
return buildObject(i, j, k);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ASN1Exception("corrupted stream detected", e);
|
||||
}
|
||||
}
|
||||
|
||||
static int readTagNumber(InputStream paramInputStream, int paramInt) throws IOException {
|
||||
int i = paramInt & 0x1F;
|
||||
if (i == 31) {
|
||||
i = 0;
|
||||
int j = paramInputStream.read();
|
||||
if ((j & 0x7F) == 0)
|
||||
throw new IOException("corrupted stream - invalid high tag number found");
|
||||
while (j >= 0 && (j & 0x80) != 0) {
|
||||
i |= j & 0x7F;
|
||||
i <<= 7;
|
||||
j = paramInputStream.read();
|
||||
}
|
||||
if (j < 0)
|
||||
throw new EOFException("EOF found inside tag value.");
|
||||
i |= j & 0x7F;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static int readLength(InputStream paramInputStream, int paramInt) throws IOException {
|
||||
int i = paramInputStream.read();
|
||||
if (i < 0)
|
||||
throw new EOFException("EOF found when length expected");
|
||||
if (i == 128)
|
||||
return -1;
|
||||
if (i > 127) {
|
||||
int j = i & 0x7F;
|
||||
if (j > 4)
|
||||
throw new IOException("DER length more than 4 bytes: " + j);
|
||||
i = 0;
|
||||
for (int k = 0; k < j; k++) {
|
||||
int m = paramInputStream.read();
|
||||
if (m < 0)
|
||||
throw new EOFException("EOF found reading length");
|
||||
i = (i << 8) + m;
|
||||
}
|
||||
if (i < 0)
|
||||
throw new IOException("corrupted stream - negative length found");
|
||||
if (i >= paramInt)
|
||||
throw new IOException("corrupted stream - out of bounds length found");
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
private static byte[] getBuffer(DefiniteLengthInputStream paramDefiniteLengthInputStream, byte[][] paramArrayOfbyte) throws IOException {
|
||||
int i = paramDefiniteLengthInputStream.getRemaining();
|
||||
if (paramDefiniteLengthInputStream.getRemaining() < paramArrayOfbyte.length) {
|
||||
byte[] arrayOfByte = paramArrayOfbyte[i];
|
||||
if (arrayOfByte == null)
|
||||
arrayOfByte = paramArrayOfbyte[i] = new byte[i];
|
||||
Streams.readFully(paramDefiniteLengthInputStream, arrayOfByte);
|
||||
return arrayOfByte;
|
||||
}
|
||||
return paramDefiniteLengthInputStream.toByteArray();
|
||||
}
|
||||
|
||||
private static char[] getBMPCharBuffer(DefiniteLengthInputStream paramDefiniteLengthInputStream) throws IOException {
|
||||
int i = paramDefiniteLengthInputStream.getRemaining() / 2;
|
||||
char[] arrayOfChar = new char[i];
|
||||
int j = 0;
|
||||
while (j < i) {
|
||||
int k = paramDefiniteLengthInputStream.read();
|
||||
if (k < 0)
|
||||
break;
|
||||
int m = paramDefiniteLengthInputStream.read();
|
||||
if (m < 0)
|
||||
break;
|
||||
arrayOfChar[j++] = (char)(k << 8 | m & 0xFF);
|
||||
}
|
||||
return arrayOfChar;
|
||||
}
|
||||
|
||||
static ASN1Primitive createPrimitiveDERObject(int paramInt, DefiniteLengthInputStream paramDefiniteLengthInputStream, byte[][] paramArrayOfbyte) throws IOException {
|
||||
switch (paramInt) {
|
||||
case 3:
|
||||
return ASN1BitString.fromInputStream(paramDefiniteLengthInputStream.getRemaining(), paramDefiniteLengthInputStream);
|
||||
case 30:
|
||||
return new DERBMPString(getBMPCharBuffer(paramDefiniteLengthInputStream));
|
||||
case 1:
|
||||
return ASN1Boolean.fromOctetString(getBuffer(paramDefiniteLengthInputStream, paramArrayOfbyte));
|
||||
case 10:
|
||||
return ASN1Enumerated.fromOctetString(getBuffer(paramDefiniteLengthInputStream, paramArrayOfbyte));
|
||||
case 24:
|
||||
return new ASN1GeneralizedTime(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 27:
|
||||
return new DERGeneralString(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 22:
|
||||
return new DERIA5String(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 2:
|
||||
return new ASN1Integer(paramDefiniteLengthInputStream.toByteArray(), false);
|
||||
case 5:
|
||||
return DERNull.INSTANCE;
|
||||
case 18:
|
||||
return new DERNumericString(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 6:
|
||||
return ASN1ObjectIdentifier.fromOctetString(getBuffer(paramDefiniteLengthInputStream, paramArrayOfbyte));
|
||||
case 4:
|
||||
return new DEROctetString(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 19:
|
||||
return new DERPrintableString(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 20:
|
||||
return new DERT61String(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 28:
|
||||
return new DERUniversalString(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 23:
|
||||
return new ASN1UTCTime(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 12:
|
||||
return new DERUTF8String(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 26:
|
||||
return new DERVisibleString(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 25:
|
||||
return new DERGraphicString(paramDefiniteLengthInputStream.toByteArray());
|
||||
case 21:
|
||||
return new DERVideotexString(paramDefiniteLengthInputStream.toByteArray());
|
||||
}
|
||||
throw new IOException("unknown tag " + paramInt + " encountered");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
|
||||
public class ASN1Integer extends ASN1Primitive {
|
||||
private final byte[] bytes;
|
||||
|
||||
public static ASN1Integer getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1Integer)
|
||||
return (ASN1Integer)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (ASN1Integer)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static ASN1Integer getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof ASN1Integer) ? getInstance(aSN1Primitive) : new ASN1Integer(ASN1OctetString.getInstance(paramASN1TaggedObject.getObject()).getOctets());
|
||||
}
|
||||
|
||||
public ASN1Integer(long paramLong) {
|
||||
this.bytes = BigInteger.valueOf(paramLong).toByteArray();
|
||||
}
|
||||
|
||||
public ASN1Integer(BigInteger paramBigInteger) {
|
||||
this.bytes = paramBigInteger.toByteArray();
|
||||
}
|
||||
|
||||
public ASN1Integer(byte[] paramArrayOfbyte) {
|
||||
this(paramArrayOfbyte, true);
|
||||
}
|
||||
|
||||
ASN1Integer(byte[] paramArrayOfbyte, boolean paramBoolean) {
|
||||
this.bytes = paramBoolean ? Arrays.clone(paramArrayOfbyte) : paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public BigInteger getValue() {
|
||||
return new BigInteger(this.bytes);
|
||||
}
|
||||
|
||||
public BigInteger getPositiveValue() {
|
||||
return new BigInteger(1, this.bytes);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.bytes.length) + this.bytes.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(2, this.bytes);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int i = 0;
|
||||
for (int j = 0; j != this.bytes.length; j++)
|
||||
i ^= (this.bytes[j] & 0xFF) << j % 4;
|
||||
return i;
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof ASN1Integer))
|
||||
return false;
|
||||
ASN1Integer aSN1Integer = (ASN1Integer)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.bytes, aSN1Integer.bytes);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getValue().toString();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class ASN1Null extends ASN1Primitive {
|
||||
public static ASN1Null getInstance(Object paramObject) {
|
||||
if (paramObject instanceof ASN1Null)
|
||||
return (ASN1Null)paramObject;
|
||||
if (paramObject != null)
|
||||
try {
|
||||
return getInstance(ASN1Primitive.fromByteArray((byte[])paramObject));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("failed to construct NULL from byte[]: " + e.getMessage());
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException("unknown object in getInstance(): " + paramObject.getClass().getName());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
return !!(paramASN1Primitive instanceof ASN1Null);
|
||||
}
|
||||
|
||||
abstract void encode(ASN1OutputStream paramASN1OutputStream) throws IOException;
|
||||
|
||||
public String toString() {
|
||||
return "NULL";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Encodable;
|
||||
|
||||
public abstract class ASN1Object implements ASN1Encodable, Encodable {
|
||||
public byte[] getEncoded() throws IOException {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
ASN1OutputStream aSN1OutputStream = new ASN1OutputStream(byteArrayOutputStream);
|
||||
aSN1OutputStream.writeObject(this);
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
public byte[] getEncoded(String paramString) throws IOException {
|
||||
if (paramString.equals("DER")) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
DEROutputStream dEROutputStream = new DEROutputStream(byteArrayOutputStream);
|
||||
dEROutputStream.writeObject(this);
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
if (paramString.equals("DL")) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
DLOutputStream dLOutputStream = new DLOutputStream(byteArrayOutputStream);
|
||||
dLOutputStream.writeObject(this);
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
return getEncoded();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return toASN1Primitive().hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object paramObject) {
|
||||
if (this == paramObject)
|
||||
return true;
|
||||
if (!(paramObject instanceof ASN1Encodable))
|
||||
return false;
|
||||
ASN1Encodable aSN1Encodable = (ASN1Encodable)paramObject;
|
||||
return toASN1Primitive().equals(aSN1Encodable.toASN1Primitive());
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Object() {
|
||||
return toASN1Primitive();
|
||||
}
|
||||
|
||||
protected static boolean hasEncodedTagValue(Object paramObject, int paramInt) {
|
||||
return (paramObject instanceof byte[] && ((byte[])paramObject)[0] == paramInt);
|
||||
}
|
||||
|
||||
public abstract ASN1Primitive toASN1Primitive();
|
||||
}
|
||||
|
|
@ -0,0 +1,268 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
|
||||
public class ASN1ObjectIdentifier extends ASN1Primitive {
|
||||
private final String identifier;
|
||||
|
||||
private byte[] body;
|
||||
|
||||
private static final long LONG_LIMIT = 72057594037927808L;
|
||||
|
||||
private static final Map pool = new HashMap();
|
||||
|
||||
public static ASN1ObjectIdentifier getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1ObjectIdentifier)
|
||||
return (ASN1ObjectIdentifier)paramObject;
|
||||
if (paramObject instanceof ASN1Encodable && ((ASN1Encodable)paramObject).toASN1Primitive() instanceof ASN1ObjectIdentifier)
|
||||
return (ASN1ObjectIdentifier)((ASN1Encodable)paramObject).toASN1Primitive();
|
||||
if (paramObject instanceof byte[]) {
|
||||
byte[] arrayOfByte = (byte[])paramObject;
|
||||
try {
|
||||
return (ASN1ObjectIdentifier)fromByteArray(arrayOfByte);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("failed to construct object identifier from byte[]: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static ASN1ObjectIdentifier getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof ASN1ObjectIdentifier) ? getInstance(aSN1Primitive) : fromOctetString(ASN1OctetString.getInstance(paramASN1TaggedObject.getObject()).getOctets());
|
||||
}
|
||||
|
||||
ASN1ObjectIdentifier(byte[] paramArrayOfbyte) {
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
long l = 0L;
|
||||
BigInteger bigInteger = null;
|
||||
boolean bool = true;
|
||||
for (int i = 0; i != paramArrayOfbyte.length; i++) {
|
||||
int j = paramArrayOfbyte[i] & 0xFF;
|
||||
if (l <= 72057594037927808L) {
|
||||
l += (long)(j & 0x7F);
|
||||
if ((j & 0x80) == 0) {
|
||||
if (bool) {
|
||||
if (l < 40L) {
|
||||
stringBuffer.append('0');
|
||||
} else if (l < 80L) {
|
||||
stringBuffer.append('1');
|
||||
l -= 40L;
|
||||
} else {
|
||||
stringBuffer.append('2');
|
||||
l -= 80L;
|
||||
}
|
||||
bool = false;
|
||||
}
|
||||
stringBuffer.append('.');
|
||||
stringBuffer.append(l);
|
||||
l = 0L;
|
||||
} else {
|
||||
l <<= 7L;
|
||||
}
|
||||
} else {
|
||||
if (bigInteger == null)
|
||||
bigInteger = BigInteger.valueOf(l);
|
||||
bigInteger = bigInteger.or(BigInteger.valueOf((long)(j & 0x7F)));
|
||||
if ((j & 0x80) == 0) {
|
||||
if (bool) {
|
||||
stringBuffer.append('2');
|
||||
bigInteger = bigInteger.subtract(BigInteger.valueOf(80L));
|
||||
bool = false;
|
||||
}
|
||||
stringBuffer.append('.');
|
||||
stringBuffer.append(bigInteger);
|
||||
bigInteger = null;
|
||||
l = 0L;
|
||||
} else {
|
||||
bigInteger = bigInteger.shiftLeft(7);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.identifier = stringBuffer.toString();
|
||||
this.body = Arrays.clone(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public ASN1ObjectIdentifier(String paramString) {
|
||||
if (paramString == null)
|
||||
throw new IllegalArgumentException("'identifier' cannot be null");
|
||||
if (!isValidIdentifier(paramString))
|
||||
throw new IllegalArgumentException("string " + paramString + " not an OID");
|
||||
this.identifier = paramString;
|
||||
}
|
||||
|
||||
ASN1ObjectIdentifier(ASN1ObjectIdentifier paramASN1ObjectIdentifier, String paramString) {
|
||||
if (!isValidBranchID(paramString, 0))
|
||||
throw new IllegalArgumentException("string " + paramString + " not a valid OID branch");
|
||||
this.identifier = paramASN1ObjectIdentifier.getId() + "." + paramString;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.identifier;
|
||||
}
|
||||
|
||||
public ASN1ObjectIdentifier branch(String paramString) {
|
||||
return new ASN1ObjectIdentifier(this, paramString);
|
||||
}
|
||||
|
||||
public boolean on(ASN1ObjectIdentifier paramASN1ObjectIdentifier) {
|
||||
String str1 = getId();
|
||||
String str2 = paramASN1ObjectIdentifier.getId();
|
||||
return (str1.length() > str2.length() && str1.charAt(str2.length()) == '.' && str1.startsWith(str2));
|
||||
}
|
||||
|
||||
private void writeField(ByteArrayOutputStream paramByteArrayOutputStream, long paramLong) {
|
||||
byte[] arrayOfByte = new byte[9];
|
||||
int i = 8;
|
||||
arrayOfByte[i] = (byte)((int)paramLong & 0x7F);
|
||||
while (paramLong >= 128L) {
|
||||
paramLong >>= 7L;
|
||||
arrayOfByte[--i] = (byte)((int)paramLong & 0x7F | 0x80);
|
||||
}
|
||||
paramByteArrayOutputStream.write(arrayOfByte, i, 9 - i);
|
||||
}
|
||||
|
||||
private void writeField(ByteArrayOutputStream paramByteArrayOutputStream, BigInteger paramBigInteger) {
|
||||
int i = (paramBigInteger.bitLength() + 6) / 7;
|
||||
if (i == 0) {
|
||||
paramByteArrayOutputStream.write(0);
|
||||
} else {
|
||||
BigInteger bigInteger = paramBigInteger;
|
||||
byte[] arrayOfByte = new byte[i];
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
arrayOfByte[j] = (byte)(bigInteger.intValue() & 0x7F | 0x80);
|
||||
bigInteger = bigInteger.shiftRight(7);
|
||||
}
|
||||
arrayOfByte[i - 1] = (byte)(arrayOfByte[i - 1] & Byte.MAX_VALUE);
|
||||
paramByteArrayOutputStream.write(arrayOfByte, 0, arrayOfByte.length);
|
||||
}
|
||||
}
|
||||
|
||||
private void doOutput(ByteArrayOutputStream paramByteArrayOutputStream) {
|
||||
OIDTokenizer oIDTokenizer = new OIDTokenizer(this.identifier);
|
||||
int i = Integer.parseInt(oIDTokenizer.nextToken()) * 40;
|
||||
String str = oIDTokenizer.nextToken();
|
||||
if (str.length() <= 18) {
|
||||
writeField(paramByteArrayOutputStream, (long)i + Long.parseLong(str));
|
||||
} else {
|
||||
writeField(paramByteArrayOutputStream, new BigInteger(str).add(BigInteger.valueOf((long)i)));
|
||||
}
|
||||
while (oIDTokenizer.hasMoreTokens()) {
|
||||
String str1 = oIDTokenizer.nextToken();
|
||||
if (str1.length() <= 18) {
|
||||
writeField(paramByteArrayOutputStream, Long.parseLong(str1));
|
||||
continue;
|
||||
}
|
||||
writeField(paramByteArrayOutputStream, new BigInteger(str1));
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized byte[] getBody() {
|
||||
if (this.body == null) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
doOutput(byteArrayOutputStream);
|
||||
this.body = byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
return this.body;
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
int i = (getBody()).length;
|
||||
return 1 + StreamUtil.calculateBodyLength(i) + i;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
byte[] arrayOfByte = getBody();
|
||||
paramASN1OutputStream.write(6);
|
||||
paramASN1OutputStream.writeLength(arrayOfByte.length);
|
||||
paramASN1OutputStream.write(arrayOfByte);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.identifier.hashCode();
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
return (paramASN1Primitive == this) ? true : (!(paramASN1Primitive instanceof ASN1ObjectIdentifier) ? false : this.identifier.equals(((ASN1ObjectIdentifier)paramASN1Primitive).identifier));
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getId();
|
||||
}
|
||||
|
||||
private static boolean isValidBranchID(String paramString, int paramInt) {
|
||||
boolean bool = false;
|
||||
int i = paramString.length();
|
||||
while (--i >= paramInt) {
|
||||
char c = paramString.charAt(i);
|
||||
if ('0' <= c && c <= '9') {
|
||||
bool = true;
|
||||
continue;
|
||||
}
|
||||
if (c == '.') {
|
||||
if (!bool)
|
||||
return false;
|
||||
bool = false;
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return bool;
|
||||
}
|
||||
|
||||
private static boolean isValidIdentifier(String paramString) {
|
||||
if (paramString.length() < 3 || paramString.charAt(1) != '.')
|
||||
return false;
|
||||
char c = paramString.charAt(0);
|
||||
return (c < '0' || c > '2') ? false : isValidBranchID(paramString, 2);
|
||||
}
|
||||
|
||||
public ASN1ObjectIdentifier intern() {
|
||||
synchronized (pool) {
|
||||
OidHandle oidHandle = new OidHandle(getBody());
|
||||
ASN1ObjectIdentifier aSN1ObjectIdentifier = (ASN1ObjectIdentifier)pool.get(oidHandle);
|
||||
if (aSN1ObjectIdentifier != null)
|
||||
return aSN1ObjectIdentifier;
|
||||
pool.put(oidHandle, this);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
static ASN1ObjectIdentifier fromOctetString(byte[] paramArrayOfbyte) {
|
||||
OidHandle oidHandle = new OidHandle(paramArrayOfbyte);
|
||||
synchronized (pool) {
|
||||
ASN1ObjectIdentifier aSN1ObjectIdentifier = (ASN1ObjectIdentifier)pool.get(oidHandle);
|
||||
if (aSN1ObjectIdentifier != null)
|
||||
return aSN1ObjectIdentifier;
|
||||
}
|
||||
return new ASN1ObjectIdentifier(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
private static class OidHandle {
|
||||
private int key;
|
||||
|
||||
private final byte[] enc;
|
||||
|
||||
OidHandle(byte[] param1ArrayOfbyte) {
|
||||
this.key = Arrays.hashCode(param1ArrayOfbyte);
|
||||
this.enc = param1ArrayOfbyte;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
public boolean equals(Object param1Object) {
|
||||
return (param1Object instanceof OidHandle) ? Arrays.areEqual(this.enc, ((OidHandle)param1Object).enc) : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
|
||||
public abstract class ASN1OctetString extends ASN1Primitive implements ASN1OctetStringParser {
|
||||
byte[] string;
|
||||
|
||||
public static ASN1OctetString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof ASN1OctetString) ? getInstance(aSN1Primitive) : BEROctetString.fromSequence(ASN1Sequence.getInstance(aSN1Primitive));
|
||||
}
|
||||
|
||||
public static ASN1OctetString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1OctetString)
|
||||
return (ASN1OctetString)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return getInstance(ASN1Primitive.fromByteArray((byte[])paramObject));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("failed to construct OCTET STRING from byte[]: " + e.getMessage());
|
||||
}
|
||||
if (paramObject instanceof ASN1Encodable) {
|
||||
ASN1Primitive aSN1Primitive = ((ASN1Encodable)paramObject).toASN1Primitive();
|
||||
if (aSN1Primitive instanceof ASN1OctetString)
|
||||
return (ASN1OctetString)aSN1Primitive;
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public ASN1OctetString(byte[] paramArrayOfbyte) {
|
||||
if (paramArrayOfbyte == null)
|
||||
throw new NullPointerException("string cannot be null");
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public InputStream getOctetStream() {
|
||||
return new ByteArrayInputStream(this.string);
|
||||
}
|
||||
|
||||
public ASN1OctetStringParser parser() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return this.string;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(getOctets());
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof ASN1OctetString))
|
||||
return false;
|
||||
ASN1OctetString aSN1OctetString = (ASN1OctetString)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.string, aSN1OctetString.string);
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() {
|
||||
return toASN1Primitive();
|
||||
}
|
||||
|
||||
ASN1Primitive toDERObject() {
|
||||
return new DEROctetString(this.string);
|
||||
}
|
||||
|
||||
ASN1Primitive toDLObject() {
|
||||
return new DEROctetString(this.string);
|
||||
}
|
||||
|
||||
abstract void encode(ASN1OutputStream paramASN1OutputStream) throws IOException;
|
||||
|
||||
public String toString() {
|
||||
return "#" + new String(Hex.encode(this.string));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public interface ASN1OctetStringParser extends ASN1Encodable, InMemoryRepresentable {
|
||||
InputStream getOctetStream();
|
||||
}
|
||||
|
|
@ -0,0 +1,126 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class ASN1OutputStream {
|
||||
private OutputStream os;
|
||||
|
||||
public ASN1OutputStream(OutputStream paramOutputStream) {
|
||||
this.os = paramOutputStream;
|
||||
}
|
||||
|
||||
void writeLength(int paramInt) throws IOException {
|
||||
if (paramInt > 127) {
|
||||
int i = 1;
|
||||
int j = paramInt;
|
||||
while ((j >>>= 8) != 0)
|
||||
i++;
|
||||
write((byte)(i | 0x80));
|
||||
for (int k = (i - 1) * 8; k >= 0; k -= 8)
|
||||
write((byte)(paramInt >> k));
|
||||
} else {
|
||||
write((byte)paramInt);
|
||||
}
|
||||
}
|
||||
|
||||
void write(int paramInt) throws IOException {
|
||||
this.os.write(paramInt);
|
||||
}
|
||||
|
||||
void write(byte[] paramArrayOfbyte) throws IOException {
|
||||
this.os.write(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
void write(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) throws IOException {
|
||||
this.os.write(paramArrayOfbyte, paramInt1, paramInt2);
|
||||
}
|
||||
|
||||
void writeEncoded(int paramInt, byte[] paramArrayOfbyte) throws IOException {
|
||||
write(paramInt);
|
||||
writeLength(paramArrayOfbyte.length);
|
||||
write(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
void writeTag(int paramInt1, int paramInt2) throws IOException {
|
||||
if (paramInt2 < 31) {
|
||||
write(paramInt1 | paramInt2);
|
||||
} else {
|
||||
write(paramInt1 | 0x1F);
|
||||
if (paramInt2 < 128) {
|
||||
write(paramInt2);
|
||||
} else {
|
||||
byte[] arrayOfByte = new byte[5];
|
||||
int i = arrayOfByte.length;
|
||||
arrayOfByte[--i] = (byte)(paramInt2 & 0x7F);
|
||||
while (true) {
|
||||
paramInt2 >>= 7;
|
||||
arrayOfByte[--i] = (byte)(paramInt2 & 0x7F | 0x80);
|
||||
if (paramInt2 <= 127) {
|
||||
write(arrayOfByte, i, arrayOfByte.length - i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void writeEncoded(int paramInt1, int paramInt2, byte[] paramArrayOfbyte) throws IOException {
|
||||
writeTag(paramInt1, paramInt2);
|
||||
writeLength(paramArrayOfbyte.length);
|
||||
write(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
protected void writeNull() throws IOException {
|
||||
this.os.write(5);
|
||||
this.os.write(0);
|
||||
}
|
||||
|
||||
public void writeObject(ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
if (paramASN1Encodable != null) {
|
||||
paramASN1Encodable.toASN1Primitive().encode(this);
|
||||
} else {
|
||||
throw new IOException("null object detected");
|
||||
}
|
||||
}
|
||||
|
||||
void writeImplicitObject(ASN1Primitive paramASN1Primitive) throws IOException {
|
||||
if (paramASN1Primitive != null) {
|
||||
paramASN1Primitive.encode(new ImplicitOutputStream(this.os));
|
||||
} else {
|
||||
throw new IOException("null object detected");
|
||||
}
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
this.os.close();
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
this.os.flush();
|
||||
}
|
||||
|
||||
ASN1OutputStream getDERSubStream() {
|
||||
return new DEROutputStream(this.os);
|
||||
}
|
||||
|
||||
ASN1OutputStream getDLSubStream() {
|
||||
return new DLOutputStream(this.os);
|
||||
}
|
||||
|
||||
private class ImplicitOutputStream extends ASN1OutputStream {
|
||||
private boolean first = true;
|
||||
|
||||
public ImplicitOutputStream(OutputStream param1OutputStream) {
|
||||
super(param1OutputStream);
|
||||
}
|
||||
|
||||
public void write(int param1Int) throws IOException {
|
||||
if (this.first) {
|
||||
this.first = false;
|
||||
} else {
|
||||
super.write(param1Int);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public class ASN1ParsingException extends IllegalStateException {
|
||||
private Throwable cause;
|
||||
|
||||
public ASN1ParsingException(String paramString) {
|
||||
super(paramString);
|
||||
}
|
||||
|
||||
public ASN1ParsingException(String paramString, Throwable paramThrowable) {
|
||||
super(paramString);
|
||||
this.cause = paramThrowable;
|
||||
}
|
||||
|
||||
public Throwable getCause() {
|
||||
return this.cause;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class ASN1Primitive extends ASN1Object {
|
||||
public static ASN1Primitive fromByteArray(byte[] paramArrayOfbyte) throws IOException {
|
||||
ASN1InputStream aSN1InputStream = new ASN1InputStream(paramArrayOfbyte);
|
||||
try {
|
||||
ASN1Primitive aSN1Primitive = aSN1InputStream.readObject();
|
||||
if (aSN1InputStream.available() != 0)
|
||||
throw new IOException("Extra data detected in stream");
|
||||
return aSN1Primitive;
|
||||
} catch (ClassCastException e) {
|
||||
throw new IOException("cannot recognise object in stream");
|
||||
}
|
||||
}
|
||||
|
||||
public final boolean equals(Object paramObject) {
|
||||
return (this == paramObject) ? true : ((paramObject instanceof ASN1Encodable && asn1Equals(((ASN1Encodable)paramObject).toASN1Primitive())));
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
return this;
|
||||
}
|
||||
|
||||
ASN1Primitive toDERObject() {
|
||||
return this;
|
||||
}
|
||||
|
||||
ASN1Primitive toDLObject() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public abstract int hashCode();
|
||||
|
||||
abstract boolean isConstructed();
|
||||
|
||||
abstract int encodedLength() throws IOException;
|
||||
|
||||
abstract void encode(ASN1OutputStream paramASN1OutputStream) throws IOException;
|
||||
|
||||
abstract boolean asn1Equals(ASN1Primitive paramASN1Primitive);
|
||||
}
|
||||
|
|
@ -0,0 +1,164 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Iterable;
|
||||
|
||||
public abstract class ASN1Sequence extends ASN1Primitive implements Iterable<ASN1Encodable> {
|
||||
protected Vector seq = new Vector();
|
||||
|
||||
public static ASN1Sequence getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1Sequence)
|
||||
return (ASN1Sequence)paramObject;
|
||||
if (paramObject instanceof ASN1SequenceParser)
|
||||
return getInstance(((ASN1SequenceParser)paramObject).toASN1Primitive());
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return getInstance(fromByteArray((byte[])paramObject));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("failed to construct sequence from byte[]: " + e.getMessage());
|
||||
}
|
||||
if (paramObject instanceof ASN1Encodable) {
|
||||
ASN1Primitive aSN1Primitive = ((ASN1Encodable)paramObject).toASN1Primitive();
|
||||
if (aSN1Primitive instanceof ASN1Sequence)
|
||||
return (ASN1Sequence)aSN1Primitive;
|
||||
}
|
||||
throw new IllegalArgumentException("unknown object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static ASN1Sequence getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
if (paramBoolean) {
|
||||
if (!paramASN1TaggedObject.isExplicit())
|
||||
throw new IllegalArgumentException("object implicit - explicit expected.");
|
||||
return getInstance(paramASN1TaggedObject.getObject().toASN1Primitive());
|
||||
}
|
||||
if (paramASN1TaggedObject.isExplicit())
|
||||
return (paramASN1TaggedObject instanceof BERTaggedObject) ? new BERSequence(paramASN1TaggedObject.getObject()) : new DLSequence(paramASN1TaggedObject.getObject());
|
||||
if (paramASN1TaggedObject.getObject() instanceof ASN1Sequence)
|
||||
return (ASN1Sequence)paramASN1TaggedObject.getObject();
|
||||
throw new IllegalArgumentException("unknown object in getInstance: " + paramASN1TaggedObject.getClass().getName());
|
||||
}
|
||||
|
||||
protected ASN1Sequence() {}
|
||||
|
||||
protected ASN1Sequence(ASN1Encodable paramASN1Encodable) {
|
||||
this.seq.addElement(paramASN1Encodable);
|
||||
}
|
||||
|
||||
protected ASN1Sequence(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
for (int i = 0; i != paramASN1EncodableVector.size(); i++)
|
||||
this.seq.addElement(paramASN1EncodableVector.get(i));
|
||||
}
|
||||
|
||||
protected ASN1Sequence(ASN1Encodable[] paramArrayOfASN1Encodable) {
|
||||
for (int i = 0; i != paramArrayOfASN1Encodable.length; i++)
|
||||
this.seq.addElement(paramArrayOfASN1Encodable[i]);
|
||||
}
|
||||
|
||||
public ASN1Encodable[] toArray() {
|
||||
ASN1Encodable[] arrayOfASN1Encodable = new ASN1Encodable[size()];
|
||||
for (int i = 0; i != size(); i++)
|
||||
arrayOfASN1Encodable[i] = getObjectAt(i);
|
||||
return arrayOfASN1Encodable;
|
||||
}
|
||||
|
||||
public Enumeration getObjects() {
|
||||
return this.seq.elements();
|
||||
}
|
||||
|
||||
public ASN1SequenceParser parser() {
|
||||
final ASN1Sequence outer = this;
|
||||
return new ASN1SequenceParser() {
|
||||
private final int max = ASN1Sequence.this.size();
|
||||
|
||||
private int index;
|
||||
|
||||
public ASN1Encodable readObject() throws IOException {
|
||||
if (this.index == this.max)
|
||||
return null;
|
||||
ASN1Encodable aSN1Encodable = ASN1Sequence.this.getObjectAt(this.index++);
|
||||
return (aSN1Encodable instanceof ASN1Sequence) ? ((ASN1Sequence)aSN1Encodable).parser() : ((aSN1Encodable instanceof ASN1Set) ? ((ASN1Set)aSN1Encodable).parser() : aSN1Encodable);
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() {
|
||||
return outer;
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
return outer;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public ASN1Encodable getObjectAt(int paramInt) {
|
||||
return this.seq.elementAt(paramInt);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.seq.size();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
ASN1Encodable aSN1Encodable;
|
||||
Enumeration enumeration = getObjects();
|
||||
int i;
|
||||
for (i = size(); enumeration.hasMoreElements(); i ^= aSN1Encodable.hashCode()) {
|
||||
aSN1Encodable = getNext(enumeration);
|
||||
i *= 17;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof ASN1Sequence))
|
||||
return false;
|
||||
ASN1Sequence aSN1Sequence = (ASN1Sequence)paramASN1Primitive;
|
||||
if (size() != aSN1Sequence.size())
|
||||
return false;
|
||||
Enumeration enumeration1 = getObjects();
|
||||
Enumeration enumeration2 = aSN1Sequence.getObjects();
|
||||
while (enumeration1.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable1 = getNext(enumeration1);
|
||||
ASN1Encodable aSN1Encodable2 = getNext(enumeration2);
|
||||
ASN1Primitive aSN1Primitive1 = aSN1Encodable1.toASN1Primitive();
|
||||
ASN1Primitive aSN1Primitive2 = aSN1Encodable2.toASN1Primitive();
|
||||
if (aSN1Primitive1 == aSN1Primitive2 || aSN1Primitive1.equals(aSN1Primitive2))
|
||||
continue;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private ASN1Encodable getNext(Enumeration paramEnumeration) {
|
||||
return paramEnumeration.nextElement();
|
||||
}
|
||||
|
||||
ASN1Primitive toDERObject() {
|
||||
DERSequence dERSequence = new DERSequence();
|
||||
dERSequence.seq = this.seq;
|
||||
return dERSequence;
|
||||
}
|
||||
|
||||
ASN1Primitive toDLObject() {
|
||||
DLSequence dLSequence = new DLSequence();
|
||||
dLSequence.seq = this.seq;
|
||||
return dLSequence;
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
abstract void encode(ASN1OutputStream paramASN1OutputStream) throws IOException;
|
||||
|
||||
public String toString() {
|
||||
return this.seq.toString();
|
||||
}
|
||||
|
||||
public Iterator<ASN1Encodable> iterator() {
|
||||
return new Arrays.Iterator<ASN1Encodable>(toArray());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface ASN1SequenceParser extends ASN1Encodable, InMemoryRepresentable {
|
||||
ASN1Encodable readObject() throws IOException;
|
||||
}
|
||||
|
|
@ -0,0 +1,230 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Iterable;
|
||||
|
||||
public abstract class ASN1Set extends ASN1Primitive implements Iterable<ASN1Encodable> {
|
||||
private Vector set = new Vector();
|
||||
|
||||
private boolean isSorted = false;
|
||||
|
||||
public static ASN1Set getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1Set)
|
||||
return (ASN1Set)paramObject;
|
||||
if (paramObject instanceof ASN1SetParser)
|
||||
return getInstance(((ASN1SetParser)paramObject).toASN1Primitive());
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return getInstance(ASN1Primitive.fromByteArray((byte[])paramObject));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("failed to construct set from byte[]: " + e.getMessage());
|
||||
}
|
||||
if (paramObject instanceof ASN1Encodable) {
|
||||
ASN1Primitive aSN1Primitive = ((ASN1Encodable)paramObject).toASN1Primitive();
|
||||
if (aSN1Primitive instanceof ASN1Set)
|
||||
return (ASN1Set)aSN1Primitive;
|
||||
}
|
||||
throw new IllegalArgumentException("unknown object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static ASN1Set getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
if (paramBoolean) {
|
||||
if (!paramASN1TaggedObject.isExplicit())
|
||||
throw new IllegalArgumentException("object implicit - explicit expected.");
|
||||
return (ASN1Set)paramASN1TaggedObject.getObject();
|
||||
}
|
||||
if (paramASN1TaggedObject.isExplicit())
|
||||
return (paramASN1TaggedObject instanceof BERTaggedObject) ? new BERSet(paramASN1TaggedObject.getObject()) : new DLSet(paramASN1TaggedObject.getObject());
|
||||
if (paramASN1TaggedObject.getObject() instanceof ASN1Set)
|
||||
return (ASN1Set)paramASN1TaggedObject.getObject();
|
||||
if (paramASN1TaggedObject.getObject() instanceof ASN1Sequence) {
|
||||
ASN1Sequence aSN1Sequence = (ASN1Sequence)paramASN1TaggedObject.getObject();
|
||||
return (paramASN1TaggedObject instanceof BERTaggedObject) ? new BERSet(aSN1Sequence.toArray()) : new DLSet(aSN1Sequence.toArray());
|
||||
}
|
||||
throw new IllegalArgumentException("unknown object in getInstance: " + paramASN1TaggedObject.getClass().getName());
|
||||
}
|
||||
|
||||
protected ASN1Set() {}
|
||||
|
||||
protected ASN1Set(ASN1Encodable paramASN1Encodable) {
|
||||
this.set.addElement(paramASN1Encodable);
|
||||
}
|
||||
|
||||
protected ASN1Set(ASN1EncodableVector paramASN1EncodableVector, boolean paramBoolean) {
|
||||
for (int i = 0; i != paramASN1EncodableVector.size(); i++)
|
||||
this.set.addElement(paramASN1EncodableVector.get(i));
|
||||
if (paramBoolean)
|
||||
sort();
|
||||
}
|
||||
|
||||
protected ASN1Set(ASN1Encodable[] paramArrayOfASN1Encodable, boolean paramBoolean) {
|
||||
for (int i = 0; i != paramArrayOfASN1Encodable.length; i++)
|
||||
this.set.addElement(paramArrayOfASN1Encodable[i]);
|
||||
if (paramBoolean)
|
||||
sort();
|
||||
}
|
||||
|
||||
public Enumeration getObjects() {
|
||||
return this.set.elements();
|
||||
}
|
||||
|
||||
public ASN1Encodable getObjectAt(int paramInt) {
|
||||
return this.set.elementAt(paramInt);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.set.size();
|
||||
}
|
||||
|
||||
public ASN1Encodable[] toArray() {
|
||||
ASN1Encodable[] arrayOfASN1Encodable = new ASN1Encodable[size()];
|
||||
for (int i = 0; i != size(); i++)
|
||||
arrayOfASN1Encodable[i] = getObjectAt(i);
|
||||
return arrayOfASN1Encodable;
|
||||
}
|
||||
|
||||
public ASN1SetParser parser() {
|
||||
final ASN1Set outer = this;
|
||||
return new ASN1SetParser() {
|
||||
private final int max = ASN1Set.this.size();
|
||||
|
||||
private int index;
|
||||
|
||||
public ASN1Encodable readObject() throws IOException {
|
||||
if (this.index == this.max)
|
||||
return null;
|
||||
ASN1Encodable aSN1Encodable = ASN1Set.this.getObjectAt(this.index++);
|
||||
return (aSN1Encodable instanceof ASN1Sequence) ? ((ASN1Sequence)aSN1Encodable).parser() : ((aSN1Encodable instanceof ASN1Set) ? ((ASN1Set)aSN1Encodable).parser() : aSN1Encodable);
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() {
|
||||
return outer;
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
return outer;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
ASN1Encodable aSN1Encodable;
|
||||
Enumeration enumeration = getObjects();
|
||||
int i;
|
||||
for (i = size(); enumeration.hasMoreElements(); i ^= aSN1Encodable.hashCode()) {
|
||||
aSN1Encodable = getNext(enumeration);
|
||||
i *= 17;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
ASN1Primitive toDERObject() {
|
||||
if (this.isSorted) {
|
||||
DERSet dERSet1 = new DERSet();
|
||||
dERSet1.set = this.set;
|
||||
return dERSet1;
|
||||
}
|
||||
Vector vector = new Vector();
|
||||
for (int i = 0; i != this.set.size(); i++)
|
||||
vector.addElement(this.set.elementAt(i));
|
||||
DERSet dERSet = new DERSet();
|
||||
dERSet.set = vector;
|
||||
dERSet.sort();
|
||||
return dERSet;
|
||||
}
|
||||
|
||||
ASN1Primitive toDLObject() {
|
||||
DLSet dLSet = new DLSet();
|
||||
dLSet.set = this.set;
|
||||
return dLSet;
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof ASN1Set))
|
||||
return false;
|
||||
ASN1Set aSN1Set = (ASN1Set)paramASN1Primitive;
|
||||
if (size() != aSN1Set.size())
|
||||
return false;
|
||||
Enumeration enumeration1 = getObjects();
|
||||
Enumeration enumeration2 = aSN1Set.getObjects();
|
||||
while (enumeration1.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable1 = getNext(enumeration1);
|
||||
ASN1Encodable aSN1Encodable2 = getNext(enumeration2);
|
||||
ASN1Primitive aSN1Primitive1 = aSN1Encodable1.toASN1Primitive();
|
||||
ASN1Primitive aSN1Primitive2 = aSN1Encodable2.toASN1Primitive();
|
||||
if (aSN1Primitive1 == aSN1Primitive2 || aSN1Primitive1.equals(aSN1Primitive2))
|
||||
continue;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private ASN1Encodable getNext(Enumeration paramEnumeration) {
|
||||
ASN1Encodable aSN1Encodable = paramEnumeration.nextElement();
|
||||
return (aSN1Encodable == null) ? DERNull.INSTANCE : aSN1Encodable;
|
||||
}
|
||||
|
||||
private boolean lessThanOrEqual(byte[] paramArrayOfbyte1, byte[] paramArrayOfbyte2) {
|
||||
int i = Math.min(paramArrayOfbyte1.length, paramArrayOfbyte2.length);
|
||||
for (int j = 0; j != i; j++) {
|
||||
if (paramArrayOfbyte1[j] != paramArrayOfbyte2[j])
|
||||
return ((paramArrayOfbyte1[j] & 0xFF) < (paramArrayOfbyte2[j] & 0xFF));
|
||||
}
|
||||
return (i == paramArrayOfbyte1.length);
|
||||
}
|
||||
|
||||
private byte[] getDEREncoded(ASN1Encodable paramASN1Encodable) {
|
||||
try {
|
||||
return paramASN1Encodable.toASN1Primitive().getEncoded("DER");
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("cannot encode object added to SET");
|
||||
}
|
||||
}
|
||||
|
||||
protected void sort() {
|
||||
if (!this.isSorted) {
|
||||
this.isSorted = true;
|
||||
if (this.set.size() > 1) {
|
||||
int j;
|
||||
boolean bool = true;
|
||||
for (int i = this.set.size() - 1; bool; i = j) {
|
||||
int k = 0;
|
||||
j = 0;
|
||||
byte[] arrayOfByte = getDEREncoded(this.set.elementAt(0));
|
||||
bool = false;
|
||||
while (k != i) {
|
||||
byte[] arrayOfByte1 = getDEREncoded(this.set.elementAt(k + 1));
|
||||
if (lessThanOrEqual(arrayOfByte, arrayOfByte1)) {
|
||||
arrayOfByte = arrayOfByte1;
|
||||
} else {
|
||||
Object object = this.set.elementAt(k);
|
||||
this.set.setElementAt(this.set.elementAt(k + 1), k);
|
||||
this.set.setElementAt(object, k + 1);
|
||||
bool = true;
|
||||
j = k;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
abstract void encode(ASN1OutputStream paramASN1OutputStream) throws IOException;
|
||||
|
||||
public String toString() {
|
||||
return this.set.toString();
|
||||
}
|
||||
|
||||
public Iterator<ASN1Encodable> iterator() {
|
||||
return new Arrays.Iterator<ASN1Encodable>(toArray());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface ASN1SetParser extends ASN1Encodable, InMemoryRepresentable {
|
||||
ASN1Encodable readObject() throws IOException;
|
||||
}
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ASN1StreamParser {
|
||||
private final InputStream _in;
|
||||
|
||||
private final int _limit;
|
||||
|
||||
private final byte[][] tmpBuffers;
|
||||
|
||||
public ASN1StreamParser(InputStream paramInputStream) {
|
||||
this(paramInputStream, StreamUtil.findLimit(paramInputStream));
|
||||
}
|
||||
|
||||
public ASN1StreamParser(InputStream paramInputStream, int paramInt) {
|
||||
this._in = paramInputStream;
|
||||
this._limit = paramInt;
|
||||
this.tmpBuffers = new byte[11][];
|
||||
}
|
||||
|
||||
public ASN1StreamParser(byte[] paramArrayOfbyte) {
|
||||
this(new ByteArrayInputStream(paramArrayOfbyte), paramArrayOfbyte.length);
|
||||
}
|
||||
|
||||
ASN1Encodable readIndef(int paramInt) throws IOException {
|
||||
switch (paramInt) {
|
||||
case 8:
|
||||
return new DERExternalParser(this);
|
||||
case 4:
|
||||
return new BEROctetStringParser(this);
|
||||
case 16:
|
||||
return new BERSequenceParser(this);
|
||||
case 17:
|
||||
return new BERSetParser(this);
|
||||
}
|
||||
throw new ASN1Exception("unknown BER object encountered: 0x" + Integer.toHexString(paramInt));
|
||||
}
|
||||
|
||||
ASN1Encodable readImplicit(boolean paramBoolean, int paramInt) throws IOException {
|
||||
if (this._in instanceof IndefiniteLengthInputStream) {
|
||||
if (!paramBoolean)
|
||||
throw new IOException("indefinite-length primitive encoding encountered");
|
||||
return readIndef(paramInt);
|
||||
}
|
||||
if (paramBoolean) {
|
||||
switch (paramInt) {
|
||||
case 17:
|
||||
return new DERSetParser(this);
|
||||
case 16:
|
||||
return new DERSequenceParser(this);
|
||||
case 4:
|
||||
return new BEROctetStringParser(this);
|
||||
}
|
||||
} else {
|
||||
switch (paramInt) {
|
||||
case 17:
|
||||
throw new ASN1Exception("sequences must use constructed encoding (see X.690 8.9.1/8.10.1)");
|
||||
case 16:
|
||||
throw new ASN1Exception("sets must use constructed encoding (see X.690 8.11.1/8.12.1)");
|
||||
case 4:
|
||||
return new DEROctetStringParser((DefiniteLengthInputStream)this._in);
|
||||
}
|
||||
}
|
||||
throw new ASN1Exception("implicit tagging not implemented");
|
||||
}
|
||||
|
||||
ASN1Primitive readTaggedObject(boolean paramBoolean, int paramInt) throws IOException {
|
||||
if (!paramBoolean) {
|
||||
DefiniteLengthInputStream definiteLengthInputStream = (DefiniteLengthInputStream)this._in;
|
||||
return new DERTaggedObject(false, paramInt, new DEROctetString(definiteLengthInputStream.toByteArray()));
|
||||
}
|
||||
ASN1EncodableVector aSN1EncodableVector = readVector();
|
||||
return (this._in instanceof IndefiniteLengthInputStream) ? ((aSN1EncodableVector.size() == 1) ? new BERTaggedObject(true, paramInt, aSN1EncodableVector.get(0)) : new BERTaggedObject(false, paramInt, BERFactory.createSequence(aSN1EncodableVector))) : ((aSN1EncodableVector.size() == 1) ? new DERTaggedObject(true, paramInt, aSN1EncodableVector.get(0)) : new DERTaggedObject(false, paramInt, DERFactory.createSequence(aSN1EncodableVector)));
|
||||
}
|
||||
|
||||
public ASN1Encodable readObject() throws IOException {
|
||||
int i = this._in.read();
|
||||
if (i == -1)
|
||||
return null;
|
||||
set00Check(false);
|
||||
int j = ASN1InputStream.readTagNumber(this._in, i);
|
||||
boolean bool = ((i & 0x20) != 0);
|
||||
int k = ASN1InputStream.readLength(this._in, this._limit);
|
||||
if (k < 0) {
|
||||
if (!bool)
|
||||
throw new IOException("indefinite-length primitive encoding encountered");
|
||||
IndefiniteLengthInputStream indefiniteLengthInputStream = new IndefiniteLengthInputStream(this._in, this._limit);
|
||||
ASN1StreamParser aSN1StreamParser = new ASN1StreamParser(indefiniteLengthInputStream, this._limit);
|
||||
return ((i & 0x40) != 0) ? new BERApplicationSpecificParser(j, aSN1StreamParser) : (((i & 0x80) != 0) ? new BERTaggedObjectParser(true, j, aSN1StreamParser) : aSN1StreamParser.readIndef(j));
|
||||
}
|
||||
DefiniteLengthInputStream definiteLengthInputStream = new DefiniteLengthInputStream(this._in, k);
|
||||
if ((i & 0x40) != 0)
|
||||
return new DERApplicationSpecific(bool, j, definiteLengthInputStream.toByteArray());
|
||||
if ((i & 0x80) != 0)
|
||||
return new BERTaggedObjectParser(bool, j, new ASN1StreamParser(definiteLengthInputStream));
|
||||
if (bool) {
|
||||
switch (j) {
|
||||
case 4:
|
||||
return new BEROctetStringParser(new ASN1StreamParser(definiteLengthInputStream));
|
||||
case 16:
|
||||
return new DERSequenceParser(new ASN1StreamParser(definiteLengthInputStream));
|
||||
case 17:
|
||||
return new DERSetParser(new ASN1StreamParser(definiteLengthInputStream));
|
||||
case 8:
|
||||
return new DERExternalParser(new ASN1StreamParser(definiteLengthInputStream));
|
||||
}
|
||||
throw new IOException("unknown tag " + j + " encountered");
|
||||
}
|
||||
switch (j) {
|
||||
case 4:
|
||||
return new DEROctetStringParser(definiteLengthInputStream);
|
||||
}
|
||||
try {
|
||||
return ASN1InputStream.createPrimitiveDERObject(j, definiteLengthInputStream, this.tmpBuffers);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ASN1Exception("corrupted stream detected", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void set00Check(boolean paramBoolean) {
|
||||
if (this._in instanceof IndefiniteLengthInputStream)
|
||||
((IndefiniteLengthInputStream)this._in).setEofOn00(paramBoolean);
|
||||
}
|
||||
|
||||
ASN1EncodableVector readVector() throws IOException {
|
||||
ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector();
|
||||
ASN1Encodable aSN1Encodable;
|
||||
while ((aSN1Encodable = readObject()) != null) {
|
||||
if (aSN1Encodable instanceof InMemoryRepresentable) {
|
||||
aSN1EncodableVector.add(((InMemoryRepresentable)aSN1Encodable).getLoadedObject());
|
||||
continue;
|
||||
}
|
||||
aSN1EncodableVector.add(aSN1Encodable.toASN1Primitive());
|
||||
}
|
||||
return aSN1EncodableVector;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public interface ASN1String {
|
||||
String getString();
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class ASN1TaggedObject extends ASN1Primitive implements ASN1TaggedObjectParser {
|
||||
int tagNo;
|
||||
|
||||
boolean empty = false;
|
||||
|
||||
boolean explicit = true;
|
||||
|
||||
ASN1Encodable obj = null;
|
||||
|
||||
public static ASN1TaggedObject getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
if (paramBoolean)
|
||||
return (ASN1TaggedObject)paramASN1TaggedObject.getObject();
|
||||
throw new IllegalArgumentException("implicitly tagged tagged object");
|
||||
}
|
||||
|
||||
public static ASN1TaggedObject getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1TaggedObject)
|
||||
return (ASN1TaggedObject)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return getInstance(fromByteArray((byte[])paramObject));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("failed to construct tagged object from byte[]: " + e.getMessage());
|
||||
}
|
||||
throw new IllegalArgumentException("unknown object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public ASN1TaggedObject(boolean paramBoolean, int paramInt, ASN1Encodable paramASN1Encodable) {
|
||||
if (paramASN1Encodable instanceof ASN1Choice) {
|
||||
this.explicit = true;
|
||||
} else {
|
||||
this.explicit = paramBoolean;
|
||||
}
|
||||
this.tagNo = paramInt;
|
||||
if (this.explicit) {
|
||||
this.obj = paramASN1Encodable;
|
||||
} else {
|
||||
ASN1Primitive aSN1Primitive = paramASN1Encodable.toASN1Primitive();
|
||||
if (aSN1Primitive instanceof ASN1Set)
|
||||
Object object = null;
|
||||
this.obj = paramASN1Encodable;
|
||||
}
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof ASN1TaggedObject))
|
||||
return false;
|
||||
ASN1TaggedObject aSN1TaggedObject = (ASN1TaggedObject)paramASN1Primitive;
|
||||
if (this.tagNo != aSN1TaggedObject.tagNo || this.empty != aSN1TaggedObject.empty || this.explicit != aSN1TaggedObject.explicit)
|
||||
return false;
|
||||
if (this.obj == null) {
|
||||
if (aSN1TaggedObject.obj != null)
|
||||
return false;
|
||||
} else if (!this.obj.toASN1Primitive().equals(aSN1TaggedObject.obj.toASN1Primitive())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int i = this.tagNo;
|
||||
if (this.obj != null)
|
||||
i ^= this.obj.hashCode();
|
||||
return i;
|
||||
}
|
||||
|
||||
public int getTagNo() {
|
||||
return this.tagNo;
|
||||
}
|
||||
|
||||
public boolean isExplicit() {
|
||||
return this.explicit;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.empty;
|
||||
}
|
||||
|
||||
public ASN1Primitive getObject() {
|
||||
return (this.obj != null) ? this.obj.toASN1Primitive() : null;
|
||||
}
|
||||
|
||||
public ASN1Encodable getObjectParser(int paramInt, boolean paramBoolean) throws IOException {
|
||||
switch (paramInt) {
|
||||
case 17:
|
||||
return ASN1Set.getInstance(this, paramBoolean).parser();
|
||||
case 16:
|
||||
return ASN1Sequence.getInstance(this, paramBoolean).parser();
|
||||
case 4:
|
||||
return ASN1OctetString.getInstance(this, paramBoolean).parser();
|
||||
}
|
||||
if (paramBoolean)
|
||||
return getObject();
|
||||
throw new ASN1Exception("implicit tagging not implemented for tag: " + paramInt);
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() {
|
||||
return toASN1Primitive();
|
||||
}
|
||||
|
||||
ASN1Primitive toDERObject() {
|
||||
return new DERTaggedObject(this.explicit, this.tagNo, this.obj);
|
||||
}
|
||||
|
||||
ASN1Primitive toDLObject() {
|
||||
return new DLTaggedObject(this.explicit, this.tagNo, this.obj);
|
||||
}
|
||||
|
||||
abstract void encode(ASN1OutputStream paramASN1OutputStream) throws IOException;
|
||||
|
||||
public String toString() {
|
||||
return "[" + this.tagNo + "]" + this.obj;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface ASN1TaggedObjectParser extends ASN1Encodable, InMemoryRepresentable {
|
||||
int getTagNo();
|
||||
|
||||
ASN1Encodable getObjectParser(int paramInt, boolean paramBoolean) throws IOException;
|
||||
}
|
||||
|
|
@ -0,0 +1,114 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.SimpleTimeZone;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class ASN1UTCTime extends ASN1Primitive {
|
||||
private byte[] time;
|
||||
|
||||
public static ASN1UTCTime getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof ASN1UTCTime)
|
||||
return (ASN1UTCTime)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (ASN1UTCTime)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static ASN1UTCTime getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof ASN1UTCTime) ? getInstance(aSN1Primitive) : new ASN1UTCTime(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
public ASN1UTCTime(String paramString) {
|
||||
this.time = Strings.toByteArray(paramString);
|
||||
try {
|
||||
getDate();
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("invalid date string: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public ASN1UTCTime(Date paramDate) {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss'Z'");
|
||||
simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
|
||||
this.time = Strings.toByteArray(simpleDateFormat.format(paramDate));
|
||||
}
|
||||
|
||||
public ASN1UTCTime(Date paramDate, Locale paramLocale) {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss'Z'", paramLocale);
|
||||
simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
|
||||
this.time = Strings.toByteArray(simpleDateFormat.format(paramDate));
|
||||
}
|
||||
|
||||
ASN1UTCTime(byte[] paramArrayOfbyte) {
|
||||
this.time = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public Date getDate() throws ParseException {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmssz");
|
||||
return simpleDateFormat.parse(getTime());
|
||||
}
|
||||
|
||||
public Date getAdjustedDate() throws ParseException {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssz");
|
||||
simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
|
||||
return simpleDateFormat.parse(getAdjustedTime());
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
String str1 = Strings.fromByteArray(this.time);
|
||||
if (str1.indexOf('-') < 0 && str1.indexOf('+') < 0)
|
||||
return (str1.length() == 11) ? (str1.substring(0, 10) + "00GMT+00:00") : (str1.substring(0, 12) + "GMT+00:00");
|
||||
int i = str1.indexOf('-');
|
||||
if (i < 0)
|
||||
i = str1.indexOf('+');
|
||||
String str2 = str1;
|
||||
if (i == str1.length() - 3)
|
||||
str2 = str2 + "00";
|
||||
return (i == 10) ? (str2.substring(0, 10) + "00GMT" + str2.substring(10, 13) + ":" + str2.substring(13, 15)) : (str2.substring(0, 12) + "GMT" + str2.substring(12, 15) + ":" + str2.substring(15, 17));
|
||||
}
|
||||
|
||||
public String getAdjustedTime() {
|
||||
String str = getTime();
|
||||
return (str.charAt(0) < '5') ? ("20" + str) : ("19" + str);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
int i = this.time.length;
|
||||
return 1 + StreamUtil.calculateBodyLength(i) + i;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.write(23);
|
||||
int i = this.time.length;
|
||||
paramASN1OutputStream.writeLength(i);
|
||||
for (int j = 0; j != i; j++)
|
||||
paramASN1OutputStream.write(this.time[j]);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
return !(paramASN1Primitive instanceof ASN1UTCTime) ? false : Arrays.areEqual(this.time, ((ASN1UTCTime)paramASN1Primitive).time);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.time);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return Strings.fromByteArray(this.time);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class BERApplicationSpecific extends ASN1ApplicationSpecific {
|
||||
BERApplicationSpecific(boolean paramBoolean, int paramInt, byte[] paramArrayOfbyte) {
|
||||
super(paramBoolean, paramInt, paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public BERApplicationSpecific(int paramInt, ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
this(true, paramInt, paramASN1Encodable);
|
||||
}
|
||||
|
||||
public BERApplicationSpecific(boolean paramBoolean, int paramInt, ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
super((paramBoolean || paramASN1Encodable.toASN1Primitive().isConstructed()), paramInt, getEncoding(paramBoolean, paramASN1Encodable));
|
||||
}
|
||||
|
||||
private static byte[] getEncoding(boolean paramBoolean, ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
byte[] arrayOfByte1 = paramASN1Encodable.toASN1Primitive().getEncoded("BER");
|
||||
if (paramBoolean)
|
||||
return arrayOfByte1;
|
||||
int i = getLengthOfHeader(arrayOfByte1);
|
||||
byte[] arrayOfByte2 = new byte[arrayOfByte1.length - i];
|
||||
System.arraycopy(arrayOfByte1, i, arrayOfByte2, 0, arrayOfByte2.length);
|
||||
return arrayOfByte2;
|
||||
}
|
||||
|
||||
public BERApplicationSpecific(int paramInt, ASN1EncodableVector paramASN1EncodableVector) {
|
||||
super(true, paramInt, getEncodedVector(paramASN1EncodableVector));
|
||||
}
|
||||
|
||||
private static byte[] getEncodedVector(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
for (int i = 0; i != paramASN1EncodableVector.size(); i++) {
|
||||
try {
|
||||
byteArrayOutputStream.write(((ASN1Object)paramASN1EncodableVector.get(i)).getEncoded("BER"));
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException("malformed object: " + e, e);
|
||||
}
|
||||
}
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
int i = 64;
|
||||
if (this.isConstructed)
|
||||
i |= 0x20;
|
||||
paramASN1OutputStream.writeTag(i, this.tag);
|
||||
paramASN1OutputStream.write(128);
|
||||
paramASN1OutputStream.write(this.octets);
|
||||
paramASN1OutputStream.write(0);
|
||||
paramASN1OutputStream.write(0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BERApplicationSpecificParser implements ASN1ApplicationSpecificParser {
|
||||
private final int tag;
|
||||
|
||||
private final ASN1StreamParser parser;
|
||||
|
||||
BERApplicationSpecificParser(int paramInt, ASN1StreamParser paramASN1StreamParser) {
|
||||
this.tag = paramInt;
|
||||
this.parser = paramASN1StreamParser;
|
||||
}
|
||||
|
||||
public ASN1Encodable readObject() throws IOException {
|
||||
return this.parser.readObject();
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() throws IOException {
|
||||
return new BERApplicationSpecific(this.tag, this.parser.readVector());
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
try {
|
||||
return getLoadedObject();
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Vector;
|
||||
|
||||
public class BERConstructedOctetString extends BEROctetString {
|
||||
private static final int MAX_LENGTH = 1000;
|
||||
|
||||
private Vector octs;
|
||||
|
||||
private static byte[] toBytes(Vector paramVector) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
for (int i = 0; i != paramVector.size(); i++) {
|
||||
try {
|
||||
DEROctetString dEROctetString = paramVector.elementAt(i);
|
||||
byteArrayOutputStream.write(dEROctetString.getOctets());
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException(paramVector.elementAt(i).getClass().getName() + " found in input should only contain DEROctetString");
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("exception converting octets " + e.toString());
|
||||
}
|
||||
}
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
public BERConstructedOctetString(byte[] paramArrayOfbyte) {
|
||||
super(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public BERConstructedOctetString(Vector paramVector) {
|
||||
super(toBytes(paramVector));
|
||||
this.octs = paramVector;
|
||||
}
|
||||
|
||||
public BERConstructedOctetString(ASN1Primitive paramASN1Primitive) {
|
||||
super(toByteArray(paramASN1Primitive));
|
||||
}
|
||||
|
||||
private static byte[] toByteArray(ASN1Primitive paramASN1Primitive) {
|
||||
try {
|
||||
return paramASN1Primitive.getEncoded();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("Unable to encode object");
|
||||
}
|
||||
}
|
||||
|
||||
public BERConstructedOctetString(ASN1Encodable paramASN1Encodable) {
|
||||
this(paramASN1Encodable.toASN1Primitive());
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return this.string;
|
||||
}
|
||||
|
||||
public Enumeration getObjects() {
|
||||
return (this.octs == null) ? generateOcts().elements() : this.octs.elements();
|
||||
}
|
||||
|
||||
private Vector generateOcts() {
|
||||
Vector<DEROctetString> vector = new Vector();
|
||||
for (int i = 0; i < this.string.length; i += 1000) {
|
||||
int j;
|
||||
if (i + 1000 > this.string.length) {
|
||||
j = this.string.length;
|
||||
} else {
|
||||
j = i + 1000;
|
||||
}
|
||||
byte[] arrayOfByte = new byte[j - i];
|
||||
System.arraycopy(this.string, i, arrayOfByte, 0, arrayOfByte.length);
|
||||
vector.addElement(new DEROctetString(arrayOfByte));
|
||||
}
|
||||
return vector;
|
||||
}
|
||||
|
||||
public static BEROctetString fromSequence(ASN1Sequence paramASN1Sequence) {
|
||||
Vector vector = new Vector();
|
||||
Enumeration enumeration = paramASN1Sequence.getObjects();
|
||||
while (enumeration.hasMoreElements())
|
||||
vector.addElement(enumeration.nextElement());
|
||||
return new BERConstructedOctetString(vector);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
class BERFactory {
|
||||
static final BERSequence EMPTY_SEQUENCE = new BERSequence();
|
||||
|
||||
static final BERSet EMPTY_SET = new BERSet();
|
||||
|
||||
static BERSequence createSequence(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
return (paramASN1EncodableVector.size() < 1) ? EMPTY_SEQUENCE : new BERSequence(paramASN1EncodableVector);
|
||||
}
|
||||
|
||||
static BERSet createSet(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
return (paramASN1EncodableVector.size() < 1) ? EMPTY_SET : new BERSet(paramASN1EncodableVector);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class BERGenerator extends ASN1Generator {
|
||||
private boolean _tagged = false;
|
||||
|
||||
private boolean _isExplicit;
|
||||
|
||||
private int _tagNo;
|
||||
|
||||
protected BERGenerator(OutputStream paramOutputStream) {
|
||||
super(paramOutputStream);
|
||||
}
|
||||
|
||||
protected BERGenerator(OutputStream paramOutputStream, int paramInt, boolean paramBoolean) {
|
||||
super(paramOutputStream);
|
||||
this._tagged = true;
|
||||
this._isExplicit = paramBoolean;
|
||||
this._tagNo = paramInt;
|
||||
}
|
||||
|
||||
public OutputStream getRawOutputStream() {
|
||||
return this._out;
|
||||
}
|
||||
|
||||
private void writeHdr(int paramInt) throws IOException {
|
||||
this._out.write(paramInt);
|
||||
this._out.write(128);
|
||||
}
|
||||
|
||||
protected void writeBERHeader(int paramInt) throws IOException {
|
||||
if (this._tagged) {
|
||||
int i = this._tagNo | 0x80;
|
||||
if (this._isExplicit) {
|
||||
writeHdr(i | 0x20);
|
||||
writeHdr(paramInt);
|
||||
} else if ((paramInt & 0x20) != 0) {
|
||||
writeHdr(i | 0x20);
|
||||
} else {
|
||||
writeHdr(i);
|
||||
}
|
||||
} else {
|
||||
writeHdr(paramInt);
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeBEREnd() throws IOException {
|
||||
this._out.write(0);
|
||||
this._out.write(0);
|
||||
if (this._tagged && this._isExplicit) {
|
||||
this._out.write(0);
|
||||
this._out.write(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Vector;
|
||||
|
||||
public class BEROctetString extends ASN1OctetString {
|
||||
private static final int MAX_LENGTH = 1000;
|
||||
|
||||
private ASN1OctetString[] octs;
|
||||
|
||||
private static byte[] toBytes(ASN1OctetString[] paramArrayOfASN1OctetString) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
for (int i = 0; i != paramArrayOfASN1OctetString.length; i++) {
|
||||
try {
|
||||
DEROctetString dEROctetString = (DEROctetString)paramArrayOfASN1OctetString[i];
|
||||
byteArrayOutputStream.write(dEROctetString.getOctets());
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException(paramArrayOfASN1OctetString[i].getClass().getName() + " found in input should only contain DEROctetString");
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("exception converting octets " + e.toString());
|
||||
}
|
||||
}
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
public BEROctetString(byte[] paramArrayOfbyte) {
|
||||
super(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public BEROctetString(ASN1OctetString[] paramArrayOfASN1OctetString) {
|
||||
super(toBytes(paramArrayOfASN1OctetString));
|
||||
this.octs = paramArrayOfASN1OctetString;
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return this.string;
|
||||
}
|
||||
|
||||
public Enumeration getObjects() {
|
||||
return (this.octs == null) ? generateOcts().elements() : new Enumeration() {
|
||||
int counter = 0;
|
||||
|
||||
public boolean hasMoreElements() {
|
||||
return (this.counter < BEROctetString.this.octs.length);
|
||||
}
|
||||
|
||||
public Object nextElement() {
|
||||
return BEROctetString.this.octs[this.counter++];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Vector generateOcts() {
|
||||
Vector<DEROctetString> vector = new Vector();
|
||||
for (int i = 0; i < this.string.length; i += 1000) {
|
||||
int j;
|
||||
if (i + 1000 > this.string.length) {
|
||||
j = this.string.length;
|
||||
} else {
|
||||
j = i + 1000;
|
||||
}
|
||||
byte[] arrayOfByte = new byte[j - i];
|
||||
System.arraycopy(this.string, i, arrayOfByte, 0, arrayOfByte.length);
|
||||
vector.addElement(new DEROctetString(arrayOfByte));
|
||||
}
|
||||
return vector;
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
int i = 0;
|
||||
Enumeration<ASN1Encodable> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements())
|
||||
i += enumeration.nextElement().toASN1Primitive().encodedLength();
|
||||
return 2 + i + 2;
|
||||
}
|
||||
|
||||
public void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.write(36);
|
||||
paramASN1OutputStream.write(128);
|
||||
Enumeration<ASN1Encodable> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements())
|
||||
paramASN1OutputStream.writeObject(enumeration.nextElement());
|
||||
paramASN1OutputStream.write(0);
|
||||
paramASN1OutputStream.write(0);
|
||||
}
|
||||
|
||||
static BEROctetString fromSequence(ASN1Sequence paramASN1Sequence) {
|
||||
ASN1OctetString[] arrayOfASN1OctetString = new ASN1OctetString[paramASN1Sequence.size()];
|
||||
Enumeration<ASN1OctetString> enumeration = paramASN1Sequence.getObjects();
|
||||
int i = 0;
|
||||
while (enumeration.hasMoreElements())
|
||||
arrayOfASN1OctetString[i++] = enumeration.nextElement();
|
||||
return new BEROctetString(arrayOfASN1OctetString);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class BEROctetStringGenerator extends BERGenerator {
|
||||
public BEROctetStringGenerator(OutputStream paramOutputStream) throws IOException {
|
||||
super(paramOutputStream);
|
||||
writeBERHeader(36);
|
||||
}
|
||||
|
||||
public BEROctetStringGenerator(OutputStream paramOutputStream, int paramInt, boolean paramBoolean) throws IOException {
|
||||
super(paramOutputStream, paramInt, paramBoolean);
|
||||
writeBERHeader(36);
|
||||
}
|
||||
|
||||
public OutputStream getOctetOutputStream() {
|
||||
return getOctetOutputStream(new byte[1000]);
|
||||
}
|
||||
|
||||
public OutputStream getOctetOutputStream(byte[] paramArrayOfbyte) {
|
||||
return new BufferedBEROctetStream(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
private class BufferedBEROctetStream extends OutputStream {
|
||||
private byte[] _buf;
|
||||
|
||||
private int _off;
|
||||
|
||||
private DEROutputStream _derOut;
|
||||
|
||||
BufferedBEROctetStream(byte[] param1ArrayOfbyte) {
|
||||
this._buf = param1ArrayOfbyte;
|
||||
this._off = 0;
|
||||
this._derOut = new DEROutputStream(BEROctetStringGenerator.this._out);
|
||||
}
|
||||
|
||||
public void write(int param1Int) throws IOException {
|
||||
this._buf[this._off++] = (byte)param1Int;
|
||||
if (this._off == this._buf.length) {
|
||||
DEROctetString.encode(this._derOut, this._buf);
|
||||
this._off = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void write(byte[] param1ArrayOfbyte, int param1Int1, int param1Int2) throws IOException {
|
||||
while (param1Int2 > 0) {
|
||||
int i = Math.min(param1Int2, this._buf.length - this._off);
|
||||
System.arraycopy(param1ArrayOfbyte, param1Int1, this._buf, this._off, i);
|
||||
this._off += i;
|
||||
if (this._off < this._buf.length)
|
||||
break;
|
||||
DEROctetString.encode(this._derOut, this._buf);
|
||||
this._off = 0;
|
||||
param1Int1 += i;
|
||||
param1Int2 -= i;
|
||||
}
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
if (this._off != 0) {
|
||||
byte[] arrayOfByte = new byte[this._off];
|
||||
System.arraycopy(this._buf, 0, arrayOfByte, 0, this._off);
|
||||
DEROctetString.encode(this._derOut, arrayOfByte);
|
||||
}
|
||||
BEROctetStringGenerator.this.writeBEREnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
|
||||
public class BEROctetStringParser implements ASN1OctetStringParser {
|
||||
private ASN1StreamParser _parser;
|
||||
|
||||
BEROctetStringParser(ASN1StreamParser paramASN1StreamParser) {
|
||||
this._parser = paramASN1StreamParser;
|
||||
}
|
||||
|
||||
public InputStream getOctetStream() {
|
||||
return new ConstructedOctetStream(this._parser);
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() throws IOException {
|
||||
return new BEROctetString(Streams.readAll(getOctetStream()));
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
try {
|
||||
return getLoadedObject();
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException("IOException converting stream to byte array: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class BEROutputStream extends DEROutputStream {
|
||||
public BEROutputStream(OutputStream paramOutputStream) {
|
||||
super(paramOutputStream);
|
||||
}
|
||||
|
||||
public void writeObject(Object paramObject) throws IOException {
|
||||
if (paramObject == null) {
|
||||
writeNull();
|
||||
} else if (paramObject instanceof ASN1Primitive) {
|
||||
((ASN1Primitive)paramObject).encode(this);
|
||||
} else if (paramObject instanceof ASN1Encodable) {
|
||||
((ASN1Encodable)paramObject).toASN1Primitive().encode(this);
|
||||
} else {
|
||||
throw new IOException("object not BEREncodable");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class BERSequence extends ASN1Sequence {
|
||||
public BERSequence() {}
|
||||
|
||||
public BERSequence(ASN1Encodable paramASN1Encodable) {
|
||||
super(paramASN1Encodable);
|
||||
}
|
||||
|
||||
public BERSequence(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
super(paramASN1EncodableVector);
|
||||
}
|
||||
|
||||
public BERSequence(ASN1Encodable[] paramArrayOfASN1Encodable) {
|
||||
super(paramArrayOfASN1Encodable);
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
int i = 0;
|
||||
Enumeration<ASN1Encodable> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements())
|
||||
i += enumeration.nextElement().toASN1Primitive().encodedLength();
|
||||
return 2 + i + 2;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.write(48);
|
||||
paramASN1OutputStream.write(128);
|
||||
Enumeration<ASN1Encodable> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements())
|
||||
paramASN1OutputStream.writeObject(enumeration.nextElement());
|
||||
paramASN1OutputStream.write(0);
|
||||
paramASN1OutputStream.write(0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class BERSequenceGenerator extends BERGenerator {
|
||||
public BERSequenceGenerator(OutputStream paramOutputStream) throws IOException {
|
||||
super(paramOutputStream);
|
||||
writeBERHeader(48);
|
||||
}
|
||||
|
||||
public BERSequenceGenerator(OutputStream paramOutputStream, int paramInt, boolean paramBoolean) throws IOException {
|
||||
super(paramOutputStream, paramInt, paramBoolean);
|
||||
writeBERHeader(48);
|
||||
}
|
||||
|
||||
public void addObject(ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
paramASN1Encodable.toASN1Primitive().encode(new BEROutputStream(this._out));
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
writeBEREnd();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BERSequenceParser implements ASN1SequenceParser {
|
||||
private ASN1StreamParser _parser;
|
||||
|
||||
BERSequenceParser(ASN1StreamParser paramASN1StreamParser) {
|
||||
this._parser = paramASN1StreamParser;
|
||||
}
|
||||
|
||||
public ASN1Encodable readObject() throws IOException {
|
||||
return this._parser.readObject();
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() throws IOException {
|
||||
return new BERSequence(this._parser.readVector());
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
try {
|
||||
return getLoadedObject();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class BERSet extends ASN1Set {
|
||||
public BERSet() {}
|
||||
|
||||
public BERSet(ASN1Encodable paramASN1Encodable) {
|
||||
super(paramASN1Encodable);
|
||||
}
|
||||
|
||||
public BERSet(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
super(paramASN1EncodableVector, false);
|
||||
}
|
||||
|
||||
public BERSet(ASN1Encodable[] paramArrayOfASN1Encodable) {
|
||||
super(paramArrayOfASN1Encodable, false);
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
int i = 0;
|
||||
Enumeration<ASN1Encodable> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements())
|
||||
i += enumeration.nextElement().toASN1Primitive().encodedLength();
|
||||
return 2 + i + 2;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.write(49);
|
||||
paramASN1OutputStream.write(128);
|
||||
Enumeration<ASN1Encodable> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements())
|
||||
paramASN1OutputStream.writeObject(enumeration.nextElement());
|
||||
paramASN1OutputStream.write(0);
|
||||
paramASN1OutputStream.write(0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BERSetParser implements ASN1SetParser {
|
||||
private ASN1StreamParser _parser;
|
||||
|
||||
BERSetParser(ASN1StreamParser paramASN1StreamParser) {
|
||||
this._parser = paramASN1StreamParser;
|
||||
}
|
||||
|
||||
public ASN1Encodable readObject() throws IOException {
|
||||
return this._parser.readObject();
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() throws IOException {
|
||||
return new BERSet(this._parser.readVector());
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
try {
|
||||
return getLoadedObject();
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class BERTaggedObject extends ASN1TaggedObject {
|
||||
public BERTaggedObject(int paramInt, ASN1Encodable paramASN1Encodable) {
|
||||
super(true, paramInt, paramASN1Encodable);
|
||||
}
|
||||
|
||||
public BERTaggedObject(boolean paramBoolean, int paramInt, ASN1Encodable paramASN1Encodable) {
|
||||
super(paramBoolean, paramInt, paramASN1Encodable);
|
||||
}
|
||||
|
||||
public BERTaggedObject(int paramInt) {
|
||||
super(false, paramInt, new BERSequence());
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
if (!this.empty) {
|
||||
if (this.explicit)
|
||||
return true;
|
||||
ASN1Primitive aSN1Primitive = this.obj.toASN1Primitive().toDERObject();
|
||||
return aSN1Primitive.isConstructed();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
if (!this.empty) {
|
||||
ASN1Primitive aSN1Primitive = this.obj.toASN1Primitive();
|
||||
int i = aSN1Primitive.encodedLength();
|
||||
return this.explicit ? (StreamUtil.calculateTagLength(this.tagNo) + StreamUtil.calculateBodyLength(i) + i) : (StreamUtil.calculateTagLength(this.tagNo) + --i);
|
||||
}
|
||||
return StreamUtil.calculateTagLength(this.tagNo) + 1;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeTag(160, this.tagNo);
|
||||
paramASN1OutputStream.write(128);
|
||||
if (!this.empty)
|
||||
if (!this.explicit) {
|
||||
Enumeration<ASN1Encodable> enumeration;
|
||||
if (this.obj instanceof ASN1OctetString) {
|
||||
if (this.obj instanceof BEROctetString) {
|
||||
enumeration = ((BEROctetString)this.obj).getObjects();
|
||||
} else {
|
||||
ASN1OctetString aSN1OctetString = (ASN1OctetString)this.obj;
|
||||
BEROctetString bEROctetString = new BEROctetString(aSN1OctetString.getOctets());
|
||||
enumeration = bEROctetString.getObjects();
|
||||
}
|
||||
} else if (this.obj instanceof ASN1Sequence) {
|
||||
enumeration = ((ASN1Sequence)this.obj).getObjects();
|
||||
} else if (this.obj instanceof ASN1Set) {
|
||||
enumeration = ((ASN1Set)this.obj).getObjects();
|
||||
} else {
|
||||
throw new RuntimeException("not implemented: " + this.obj.getClass().getName());
|
||||
}
|
||||
while (enumeration.hasMoreElements())
|
||||
paramASN1OutputStream.writeObject(enumeration.nextElement());
|
||||
} else {
|
||||
paramASN1OutputStream.writeObject(this.obj);
|
||||
}
|
||||
paramASN1OutputStream.write(0);
|
||||
paramASN1OutputStream.write(0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BERTaggedObjectParser implements ASN1TaggedObjectParser {
|
||||
private boolean _constructed;
|
||||
|
||||
private int _tagNumber;
|
||||
|
||||
private ASN1StreamParser _parser;
|
||||
|
||||
BERTaggedObjectParser(boolean paramBoolean, int paramInt, ASN1StreamParser paramASN1StreamParser) {
|
||||
this._constructed = paramBoolean;
|
||||
this._tagNumber = paramInt;
|
||||
this._parser = paramASN1StreamParser;
|
||||
}
|
||||
|
||||
public boolean isConstructed() {
|
||||
return this._constructed;
|
||||
}
|
||||
|
||||
public int getTagNo() {
|
||||
return this._tagNumber;
|
||||
}
|
||||
|
||||
public ASN1Encodable getObjectParser(int paramInt, boolean paramBoolean) throws IOException {
|
||||
if (paramBoolean) {
|
||||
if (!this._constructed)
|
||||
throw new IOException("Explicit tags must be constructed (see X.690 8.14.2)");
|
||||
return this._parser.readObject();
|
||||
}
|
||||
return this._parser.readImplicit(this._constructed, paramInt);
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() throws IOException {
|
||||
return this._parser.readTaggedObject(this._constructed, this._tagNumber);
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
try {
|
||||
return getLoadedObject();
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public interface BERTags {
|
||||
public static final int BOOLEAN = 1;
|
||||
|
||||
public static final int INTEGER = 2;
|
||||
|
||||
public static final int BIT_STRING = 3;
|
||||
|
||||
public static final int OCTET_STRING = 4;
|
||||
|
||||
public static final int NULL = 5;
|
||||
|
||||
public static final int OBJECT_IDENTIFIER = 6;
|
||||
|
||||
public static final int EXTERNAL = 8;
|
||||
|
||||
public static final int ENUMERATED = 10;
|
||||
|
||||
public static final int SEQUENCE = 16;
|
||||
|
||||
public static final int SEQUENCE_OF = 16;
|
||||
|
||||
public static final int SET = 17;
|
||||
|
||||
public static final int SET_OF = 17;
|
||||
|
||||
public static final int NUMERIC_STRING = 18;
|
||||
|
||||
public static final int PRINTABLE_STRING = 19;
|
||||
|
||||
public static final int T61_STRING = 20;
|
||||
|
||||
public static final int VIDEOTEX_STRING = 21;
|
||||
|
||||
public static final int IA5_STRING = 22;
|
||||
|
||||
public static final int UTC_TIME = 23;
|
||||
|
||||
public static final int GENERALIZED_TIME = 24;
|
||||
|
||||
public static final int GRAPHIC_STRING = 25;
|
||||
|
||||
public static final int VISIBLE_STRING = 26;
|
||||
|
||||
public static final int GENERAL_STRING = 27;
|
||||
|
||||
public static final int UNIVERSAL_STRING = 28;
|
||||
|
||||
public static final int BMP_STRING = 30;
|
||||
|
||||
public static final int UTF8_STRING = 12;
|
||||
|
||||
public static final int CONSTRUCTED = 32;
|
||||
|
||||
public static final int APPLICATION = 64;
|
||||
|
||||
public static final int TAGGED = 128;
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
class ConstructedOctetStream extends InputStream {
|
||||
private final ASN1StreamParser _parser;
|
||||
|
||||
private boolean _first = true;
|
||||
|
||||
private InputStream _currentStream;
|
||||
|
||||
ConstructedOctetStream(ASN1StreamParser paramASN1StreamParser) {
|
||||
this._parser = paramASN1StreamParser;
|
||||
}
|
||||
|
||||
public int read(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) throws IOException {
|
||||
if (this._currentStream == null) {
|
||||
if (!this._first)
|
||||
return -1;
|
||||
ASN1OctetStringParser aSN1OctetStringParser = (ASN1OctetStringParser)this._parser.readObject();
|
||||
if (aSN1OctetStringParser == null)
|
||||
return -1;
|
||||
this._first = false;
|
||||
this._currentStream = aSN1OctetStringParser.getOctetStream();
|
||||
}
|
||||
int i = 0;
|
||||
while (true) {
|
||||
int j = this._currentStream.read(paramArrayOfbyte, paramInt1 + i, paramInt2 - i);
|
||||
if (j >= 0) {
|
||||
i += j;
|
||||
if (i == paramInt2)
|
||||
return i;
|
||||
continue;
|
||||
}
|
||||
ASN1OctetStringParser aSN1OctetStringParser = (ASN1OctetStringParser)this._parser.readObject();
|
||||
if (aSN1OctetStringParser == null) {
|
||||
this._currentStream = null;
|
||||
return (i < 1) ? -1 : i;
|
||||
}
|
||||
this._currentStream = aSN1OctetStringParser.getOctetStream();
|
||||
}
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
if (this._currentStream == null) {
|
||||
if (!this._first)
|
||||
return -1;
|
||||
ASN1OctetStringParser aSN1OctetStringParser = (ASN1OctetStringParser)this._parser.readObject();
|
||||
if (aSN1OctetStringParser == null)
|
||||
return -1;
|
||||
this._first = false;
|
||||
this._currentStream = aSN1OctetStringParser.getOctetStream();
|
||||
}
|
||||
while (true) {
|
||||
int i = this._currentStream.read();
|
||||
if (i >= 0)
|
||||
return i;
|
||||
ASN1OctetStringParser aSN1OctetStringParser = (ASN1OctetStringParser)this._parser.readObject();
|
||||
if (aSN1OctetStringParser == null) {
|
||||
this._currentStream = null;
|
||||
return -1;
|
||||
}
|
||||
this._currentStream = aSN1OctetStringParser.getOctetStream();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DERApplicationSpecific extends ASN1ApplicationSpecific {
|
||||
DERApplicationSpecific(boolean paramBoolean, int paramInt, byte[] paramArrayOfbyte) {
|
||||
super(paramBoolean, paramInt, paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public DERApplicationSpecific(int paramInt, byte[] paramArrayOfbyte) {
|
||||
this(false, paramInt, paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public DERApplicationSpecific(int paramInt, ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
this(true, paramInt, paramASN1Encodable);
|
||||
}
|
||||
|
||||
public DERApplicationSpecific(boolean paramBoolean, int paramInt, ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
super((paramBoolean || paramASN1Encodable.toASN1Primitive().isConstructed()), paramInt, getEncoding(paramBoolean, paramASN1Encodable));
|
||||
}
|
||||
|
||||
private static byte[] getEncoding(boolean paramBoolean, ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
byte[] arrayOfByte1 = paramASN1Encodable.toASN1Primitive().getEncoded("DER");
|
||||
if (paramBoolean)
|
||||
return arrayOfByte1;
|
||||
int i = getLengthOfHeader(arrayOfByte1);
|
||||
byte[] arrayOfByte2 = new byte[arrayOfByte1.length - i];
|
||||
System.arraycopy(arrayOfByte1, i, arrayOfByte2, 0, arrayOfByte2.length);
|
||||
return arrayOfByte2;
|
||||
}
|
||||
|
||||
public DERApplicationSpecific(int paramInt, ASN1EncodableVector paramASN1EncodableVector) {
|
||||
super(true, paramInt, getEncodedVector(paramASN1EncodableVector));
|
||||
}
|
||||
|
||||
private static byte[] getEncodedVector(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
for (int i = 0; i != paramASN1EncodableVector.size(); i++) {
|
||||
try {
|
||||
byteArrayOutputStream.write(((ASN1Object)paramASN1EncodableVector.get(i)).getEncoded("DER"));
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException("malformed object: " + e, e);
|
||||
}
|
||||
}
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
int i = 64;
|
||||
if (this.isConstructed)
|
||||
i |= 0x20;
|
||||
paramASN1OutputStream.writeEncoded(i, this.tag, this.octets);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
|
||||
public class DERBMPString extends ASN1Primitive implements ASN1String {
|
||||
private final char[] string;
|
||||
|
||||
public static DERBMPString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERBMPString)
|
||||
return (DERBMPString)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERBMPString)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERBMPString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERBMPString) ? getInstance(aSN1Primitive) : new DERBMPString(ASN1OctetString.getInstance(aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
DERBMPString(byte[] paramArrayOfbyte) {
|
||||
char[] arrayOfChar = new char[paramArrayOfbyte.length / 2];
|
||||
for (int i = 0; i != arrayOfChar.length; i++)
|
||||
arrayOfChar[i] = (char)(paramArrayOfbyte[2 * i] << 8 | paramArrayOfbyte[2 * i + 1] & 0xFF);
|
||||
this.string = arrayOfChar;
|
||||
}
|
||||
|
||||
DERBMPString(char[] paramArrayOfchar) {
|
||||
this.string = paramArrayOfchar;
|
||||
}
|
||||
|
||||
public DERBMPString(String paramString) {
|
||||
this.string = paramString.toCharArray();
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return new String(this.string);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
|
||||
protected boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof DERBMPString))
|
||||
return false;
|
||||
DERBMPString dERBMPString = (DERBMPString)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.string, dERBMPString.string);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length * 2) + this.string.length * 2;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.write(30);
|
||||
paramASN1OutputStream.writeLength(this.string.length * 2);
|
||||
for (int i = 0; i != this.string.length; i++) {
|
||||
char c = this.string[i];
|
||||
paramASN1OutputStream.write((byte)(c >> 8));
|
||||
paramASN1OutputStream.write((byte)c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DERBitString extends ASN1BitString {
|
||||
public static DERBitString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERBitString)
|
||||
return (DERBitString)paramObject;
|
||||
if (paramObject instanceof DLBitString)
|
||||
return new DERBitString(((DLBitString)paramObject).data, ((DLBitString)paramObject).padBits);
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERBitString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERBitString) ? getInstance(aSN1Primitive) : fromOctetString(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
protected DERBitString(byte paramByte, int paramInt) {
|
||||
this(toByteArray(paramByte), paramInt);
|
||||
}
|
||||
|
||||
private static byte[] toByteArray(byte paramByte) {
|
||||
byte[] arrayOfByte = new byte[1];
|
||||
arrayOfByte[0] = paramByte;
|
||||
return arrayOfByte;
|
||||
}
|
||||
|
||||
public DERBitString(byte[] paramArrayOfbyte, int paramInt) {
|
||||
super(paramArrayOfbyte, paramInt);
|
||||
}
|
||||
|
||||
public DERBitString(byte[] paramArrayOfbyte) {
|
||||
this(paramArrayOfbyte, 0);
|
||||
}
|
||||
|
||||
public DERBitString(int paramInt) {
|
||||
super(getBytes(paramInt), getPadBits(paramInt));
|
||||
}
|
||||
|
||||
public DERBitString(ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
super(paramASN1Encodable.toASN1Primitive().getEncoded("DER"), 0);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.data.length + 1) + this.data.length + 1;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
byte[] arrayOfByte1 = derForm(this.data, this.padBits);
|
||||
byte[] arrayOfByte2 = new byte[arrayOfByte1.length + 1];
|
||||
arrayOfByte2[0] = (byte)getPadBits();
|
||||
System.arraycopy(arrayOfByte1, 0, arrayOfByte2, 1, arrayOfByte2.length - 1);
|
||||
paramASN1OutputStream.writeEncoded(3, arrayOfByte2);
|
||||
}
|
||||
|
||||
static DERBitString fromOctetString(byte[] paramArrayOfbyte) {
|
||||
if (paramArrayOfbyte.length < 1)
|
||||
throw new IllegalArgumentException("truncated BIT STRING detected");
|
||||
byte b = paramArrayOfbyte[0];
|
||||
byte[] arrayOfByte = new byte[paramArrayOfbyte.length - 1];
|
||||
if (arrayOfByte.length != 0)
|
||||
System.arraycopy(paramArrayOfbyte, 1, arrayOfByte, 0, paramArrayOfbyte.length - 1);
|
||||
return new DERBitString(arrayOfByte, b);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public class DERBoolean extends ASN1Boolean {
|
||||
public DERBoolean(boolean paramBoolean) {
|
||||
super(paramBoolean);
|
||||
}
|
||||
|
||||
DERBoolean(byte[] paramArrayOfbyte) {
|
||||
super(paramArrayOfbyte);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public class DEREncodableVector extends ASN1EncodableVector {}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class DEREnumerated extends ASN1Enumerated {
|
||||
DEREnumerated(byte[] paramArrayOfbyte) {
|
||||
super(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public DEREnumerated(BigInteger paramBigInteger) {
|
||||
super(paramBigInteger);
|
||||
}
|
||||
|
||||
public DEREnumerated(int paramInt) {
|
||||
super(paramInt);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,142 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DERExternal extends ASN1Primitive {
|
||||
private ASN1ObjectIdentifier directReference;
|
||||
|
||||
private ASN1Integer indirectReference;
|
||||
|
||||
private ASN1Primitive dataValueDescriptor;
|
||||
|
||||
private int encoding;
|
||||
|
||||
private ASN1Primitive externalContent;
|
||||
|
||||
public DERExternal(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
int i = 0;
|
||||
ASN1Primitive aSN1Primitive = getObjFromVector(paramASN1EncodableVector, i);
|
||||
if (aSN1Primitive instanceof ASN1ObjectIdentifier) {
|
||||
this.directReference = (ASN1ObjectIdentifier)aSN1Primitive;
|
||||
aSN1Primitive = getObjFromVector(paramASN1EncodableVector, ++i);
|
||||
}
|
||||
if (aSN1Primitive instanceof ASN1Integer) {
|
||||
this.indirectReference = (ASN1Integer)aSN1Primitive;
|
||||
aSN1Primitive = getObjFromVector(paramASN1EncodableVector, ++i);
|
||||
}
|
||||
if (!(aSN1Primitive instanceof ASN1TaggedObject)) {
|
||||
this.dataValueDescriptor = aSN1Primitive;
|
||||
aSN1Primitive = getObjFromVector(paramASN1EncodableVector, ++i);
|
||||
}
|
||||
if (paramASN1EncodableVector.size() != i + 1)
|
||||
throw new IllegalArgumentException("input vector too large");
|
||||
if (!(aSN1Primitive instanceof ASN1TaggedObject))
|
||||
throw new IllegalArgumentException("No tagged object found in vector. Structure doesn't seem to be of type External");
|
||||
ASN1TaggedObject aSN1TaggedObject = (ASN1TaggedObject)aSN1Primitive;
|
||||
setEncoding(aSN1TaggedObject.getTagNo());
|
||||
this.externalContent = aSN1TaggedObject.getObject();
|
||||
}
|
||||
|
||||
private ASN1Primitive getObjFromVector(ASN1EncodableVector paramASN1EncodableVector, int paramInt) {
|
||||
if (paramASN1EncodableVector.size() <= paramInt)
|
||||
throw new IllegalArgumentException("too few objects in input vector");
|
||||
return paramASN1EncodableVector.get(paramInt).toASN1Primitive();
|
||||
}
|
||||
|
||||
public DERExternal(ASN1ObjectIdentifier paramASN1ObjectIdentifier, ASN1Integer paramASN1Integer, ASN1Primitive paramASN1Primitive, DERTaggedObject paramDERTaggedObject) {
|
||||
this(paramASN1ObjectIdentifier, paramASN1Integer, paramASN1Primitive, paramDERTaggedObject.getTagNo(), paramDERTaggedObject.toASN1Primitive());
|
||||
}
|
||||
|
||||
public DERExternal(ASN1ObjectIdentifier paramASN1ObjectIdentifier, ASN1Integer paramASN1Integer, ASN1Primitive paramASN1Primitive1, int paramInt, ASN1Primitive paramASN1Primitive2) {
|
||||
setDirectReference(paramASN1ObjectIdentifier);
|
||||
setIndirectReference(paramASN1Integer);
|
||||
setDataValueDescriptor(paramASN1Primitive1);
|
||||
setEncoding(paramInt);
|
||||
setExternalContent(paramASN1Primitive2.toASN1Primitive());
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int i = 0;
|
||||
if (this.directReference != null)
|
||||
i = this.directReference.hashCode();
|
||||
if (this.indirectReference != null)
|
||||
i ^= this.indirectReference.hashCode();
|
||||
if (this.dataValueDescriptor != null)
|
||||
i ^= this.dataValueDescriptor.hashCode();
|
||||
i ^= this.externalContent.hashCode();
|
||||
return i;
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
return (getEncoded()).length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
if (this.directReference != null)
|
||||
byteArrayOutputStream.write(this.directReference.getEncoded("DER"));
|
||||
if (this.indirectReference != null)
|
||||
byteArrayOutputStream.write(this.indirectReference.getEncoded("DER"));
|
||||
if (this.dataValueDescriptor != null)
|
||||
byteArrayOutputStream.write(this.dataValueDescriptor.getEncoded("DER"));
|
||||
DERTaggedObject dERTaggedObject = new DERTaggedObject(true, this.encoding, this.externalContent);
|
||||
byteArrayOutputStream.write(dERTaggedObject.getEncoded("DER"));
|
||||
paramASN1OutputStream.writeEncoded(32, 8, byteArrayOutputStream.toByteArray());
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof DERExternal))
|
||||
return false;
|
||||
if (this == paramASN1Primitive)
|
||||
return true;
|
||||
DERExternal dERExternal = (DERExternal)paramASN1Primitive;
|
||||
return (this.directReference != null && (dERExternal.directReference == null || !dERExternal.directReference.equals(this.directReference))) ? false : ((this.indirectReference != null && (dERExternal.indirectReference == null || !dERExternal.indirectReference.equals(this.indirectReference))) ? false : ((this.dataValueDescriptor != null && (dERExternal.dataValueDescriptor == null || !dERExternal.dataValueDescriptor.equals(this.dataValueDescriptor))) ? false : this.externalContent.equals(dERExternal.externalContent)));
|
||||
}
|
||||
|
||||
public ASN1Primitive getDataValueDescriptor() {
|
||||
return this.dataValueDescriptor;
|
||||
}
|
||||
|
||||
public ASN1ObjectIdentifier getDirectReference() {
|
||||
return this.directReference;
|
||||
}
|
||||
|
||||
public int getEncoding() {
|
||||
return this.encoding;
|
||||
}
|
||||
|
||||
public ASN1Primitive getExternalContent() {
|
||||
return this.externalContent;
|
||||
}
|
||||
|
||||
public ASN1Integer getIndirectReference() {
|
||||
return this.indirectReference;
|
||||
}
|
||||
|
||||
private void setDataValueDescriptor(ASN1Primitive paramASN1Primitive) {
|
||||
this.dataValueDescriptor = paramASN1Primitive;
|
||||
}
|
||||
|
||||
private void setDirectReference(ASN1ObjectIdentifier paramASN1ObjectIdentifier) {
|
||||
this.directReference = paramASN1ObjectIdentifier;
|
||||
}
|
||||
|
||||
private void setEncoding(int paramInt) {
|
||||
if (paramInt < 0 || paramInt > 2)
|
||||
throw new IllegalArgumentException("invalid encoding value: " + paramInt);
|
||||
this.encoding = paramInt;
|
||||
}
|
||||
|
||||
private void setExternalContent(ASN1Primitive paramASN1Primitive) {
|
||||
this.externalContent = paramASN1Primitive;
|
||||
}
|
||||
|
||||
private void setIndirectReference(ASN1Integer paramASN1Integer) {
|
||||
this.indirectReference = paramASN1Integer;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DERExternalParser implements ASN1Encodable, InMemoryRepresentable {
|
||||
private ASN1StreamParser _parser;
|
||||
|
||||
public DERExternalParser(ASN1StreamParser paramASN1StreamParser) {
|
||||
this._parser = paramASN1StreamParser;
|
||||
}
|
||||
|
||||
public ASN1Encodable readObject() throws IOException {
|
||||
return this._parser.readObject();
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() throws IOException {
|
||||
try {
|
||||
return new DERExternal(this._parser.readVector());
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ASN1Exception(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
try {
|
||||
return getLoadedObject();
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException("unable to get DER object", e);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ASN1ParsingException("unable to get DER object", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
class DERFactory {
|
||||
static final ASN1Sequence EMPTY_SEQUENCE = new DERSequence();
|
||||
|
||||
static final ASN1Set EMPTY_SET = new DERSet();
|
||||
|
||||
static ASN1Sequence createSequence(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
return (paramASN1EncodableVector.size() < 1) ? EMPTY_SEQUENCE : new DLSequence(paramASN1EncodableVector);
|
||||
}
|
||||
|
||||
static ASN1Set createSet(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
return (paramASN1EncodableVector.size() < 1) ? EMPTY_SET : new DLSet(paramASN1EncodableVector);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERGeneralString extends ASN1Primitive implements ASN1String {
|
||||
private final byte[] string;
|
||||
|
||||
public static DERGeneralString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERGeneralString)
|
||||
return (DERGeneralString)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERGeneralString)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERGeneralString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERGeneralString) ? getInstance(aSN1Primitive) : new DERGeneralString(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
DERGeneralString(byte[] paramArrayOfbyte) {
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public DERGeneralString(String paramString) {
|
||||
this.string = Strings.toByteArray(paramString);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromByteArray(this.string);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return Arrays.clone(this.string);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(27, this.string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof DERGeneralString))
|
||||
return false;
|
||||
DERGeneralString dERGeneralString = (DERGeneralString)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.string, dERGeneralString.string);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class DERGeneralizedTime extends ASN1GeneralizedTime {
|
||||
DERGeneralizedTime(byte[] paramArrayOfbyte) {
|
||||
super(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public DERGeneralizedTime(Date paramDate) {
|
||||
super(paramDate);
|
||||
}
|
||||
|
||||
public DERGeneralizedTime(String paramString) {
|
||||
super(paramString);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public abstract class DERGenerator extends ASN1Generator {
|
||||
private boolean _tagged = false;
|
||||
|
||||
private boolean _isExplicit;
|
||||
|
||||
private int _tagNo;
|
||||
|
||||
protected DERGenerator(OutputStream paramOutputStream) {
|
||||
super(paramOutputStream);
|
||||
}
|
||||
|
||||
public DERGenerator(OutputStream paramOutputStream, int paramInt, boolean paramBoolean) {
|
||||
super(paramOutputStream);
|
||||
this._tagged = true;
|
||||
this._isExplicit = paramBoolean;
|
||||
this._tagNo = paramInt;
|
||||
}
|
||||
|
||||
private void writeLength(OutputStream paramOutputStream, int paramInt) throws IOException {
|
||||
if (paramInt > 127) {
|
||||
int i = 1;
|
||||
int j = paramInt;
|
||||
while ((j >>>= 8) != 0)
|
||||
i++;
|
||||
paramOutputStream.write((byte)(i | 0x80));
|
||||
for (int k = (i - 1) * 8; k >= 0; k -= 8)
|
||||
paramOutputStream.write((byte)(paramInt >> k));
|
||||
} else {
|
||||
paramOutputStream.write((byte)paramInt);
|
||||
}
|
||||
}
|
||||
|
||||
void writeDEREncoded(OutputStream paramOutputStream, int paramInt, byte[] paramArrayOfbyte) throws IOException {
|
||||
paramOutputStream.write(paramInt);
|
||||
writeLength(paramOutputStream, paramArrayOfbyte.length);
|
||||
paramOutputStream.write(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
void writeDEREncoded(int paramInt, byte[] paramArrayOfbyte) throws IOException {
|
||||
if (this._tagged) {
|
||||
int i = this._tagNo | 0x80;
|
||||
if (this._isExplicit) {
|
||||
int j = this._tagNo | 0x20 | 0x80;
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
writeDEREncoded(byteArrayOutputStream, paramInt, paramArrayOfbyte);
|
||||
writeDEREncoded(this._out, j, byteArrayOutputStream.toByteArray());
|
||||
} else if ((paramInt & 0x20) != 0) {
|
||||
writeDEREncoded(this._out, i | 0x20, paramArrayOfbyte);
|
||||
} else {
|
||||
writeDEREncoded(this._out, i, paramArrayOfbyte);
|
||||
}
|
||||
} else {
|
||||
writeDEREncoded(this._out, paramInt, paramArrayOfbyte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERGraphicString extends ASN1Primitive implements ASN1String {
|
||||
private final byte[] string;
|
||||
|
||||
public static DERGraphicString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERGraphicString)
|
||||
return (DERGraphicString)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERGraphicString)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERGraphicString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERGraphicString) ? getInstance(aSN1Primitive) : new DERGraphicString(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
public DERGraphicString(byte[] paramArrayOfbyte) {
|
||||
this.string = Arrays.clone(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return Arrays.clone(this.string);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(25, this.string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof DERGraphicString))
|
||||
return false;
|
||||
DERGraphicString dERGraphicString = (DERGraphicString)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.string, dERGraphicString.string);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromByteArray(this.string);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERIA5String extends ASN1Primitive implements ASN1String {
|
||||
private final byte[] string;
|
||||
|
||||
public static DERIA5String getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERIA5String)
|
||||
return (DERIA5String)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERIA5String)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERIA5String getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERIA5String) ? getInstance(aSN1Primitive) : new DERIA5String(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
DERIA5String(byte[] paramArrayOfbyte) {
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public DERIA5String(String paramString) {
|
||||
this(paramString, false);
|
||||
}
|
||||
|
||||
public DERIA5String(String paramString, boolean paramBoolean) {
|
||||
if (paramString == null)
|
||||
throw new NullPointerException("string cannot be null");
|
||||
if (paramBoolean && !isIA5String(paramString))
|
||||
throw new IllegalArgumentException("string contains illegal characters");
|
||||
this.string = Strings.toByteArray(paramString);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromByteArray(this.string);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return Arrays.clone(this.string);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(22, this.string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof DERIA5String))
|
||||
return false;
|
||||
DERIA5String dERIA5String = (DERIA5String)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.string, dERIA5String.string);
|
||||
}
|
||||
|
||||
public static boolean isIA5String(String paramString) {
|
||||
for (int i = paramString.length() - 1; i >= 0; i--) {
|
||||
char c = paramString.charAt(i);
|
||||
if (c > '\u007F')
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class DERInteger extends ASN1Integer {
|
||||
public DERInteger(byte[] paramArrayOfbyte) {
|
||||
super(paramArrayOfbyte, true);
|
||||
}
|
||||
|
||||
public DERInteger(BigInteger paramBigInteger) {
|
||||
super(paramBigInteger);
|
||||
}
|
||||
|
||||
public DERInteger(long paramLong) {
|
||||
super(paramLong);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DERNull extends ASN1Null {
|
||||
public static final DERNull INSTANCE = new DERNull();
|
||||
|
||||
private static final byte[] zeroBytes = new byte[0];
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(5, zeroBytes);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERNumericString extends ASN1Primitive implements ASN1String {
|
||||
private final byte[] string;
|
||||
|
||||
public static DERNumericString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERNumericString)
|
||||
return (DERNumericString)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERNumericString)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERNumericString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERNumericString) ? getInstance(aSN1Primitive) : new DERNumericString(ASN1OctetString.getInstance(aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
DERNumericString(byte[] paramArrayOfbyte) {
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public DERNumericString(String paramString) {
|
||||
this(paramString, false);
|
||||
}
|
||||
|
||||
public DERNumericString(String paramString, boolean paramBoolean) {
|
||||
if (paramBoolean && !isNumericString(paramString))
|
||||
throw new IllegalArgumentException("string contains illegal characters");
|
||||
this.string = Strings.toByteArray(paramString);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromByteArray(this.string);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return Arrays.clone(this.string);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(18, this.string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof DERNumericString))
|
||||
return false;
|
||||
DERNumericString dERNumericString = (DERNumericString)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.string, dERNumericString.string);
|
||||
}
|
||||
|
||||
public static boolean isNumericString(String paramString) {
|
||||
int i = paramString.length() - 1;
|
||||
while (i >= 0) {
|
||||
char c = paramString.charAt(i);
|
||||
if (c > '\u007F')
|
||||
return false;
|
||||
if (('0' <= c && c <= '9') || c == ' ') {
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public class DERObjectIdentifier extends ASN1ObjectIdentifier {
|
||||
public DERObjectIdentifier(String paramString) {
|
||||
super(paramString);
|
||||
}
|
||||
|
||||
DERObjectIdentifier(byte[] paramArrayOfbyte) {
|
||||
super(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
DERObjectIdentifier(ASN1ObjectIdentifier paramASN1ObjectIdentifier, String paramString) {
|
||||
super(paramASN1ObjectIdentifier, paramString);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DEROctetString extends ASN1OctetString {
|
||||
public DEROctetString(byte[] paramArrayOfbyte) {
|
||||
super(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public DEROctetString(ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
super(paramASN1Encodable.toASN1Primitive().getEncoded("DER"));
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(4, this.string);
|
||||
}
|
||||
|
||||
static void encode(DEROutputStream paramDEROutputStream, byte[] paramArrayOfbyte) throws IOException {
|
||||
paramDEROutputStream.writeEncoded(4, paramArrayOfbyte);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class DEROctetStringParser implements ASN1OctetStringParser {
|
||||
private DefiniteLengthInputStream stream;
|
||||
|
||||
DEROctetStringParser(DefiniteLengthInputStream paramDefiniteLengthInputStream) {
|
||||
this.stream = paramDefiniteLengthInputStream;
|
||||
}
|
||||
|
||||
public InputStream getOctetStream() {
|
||||
return this.stream;
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() throws IOException {
|
||||
return new DEROctetString(this.stream.toByteArray());
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
try {
|
||||
return getLoadedObject();
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException("IOException converting stream to byte array: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class DEROutputStream extends ASN1OutputStream {
|
||||
public DEROutputStream(OutputStream paramOutputStream) {
|
||||
super(paramOutputStream);
|
||||
}
|
||||
|
||||
public void writeObject(ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
if (paramASN1Encodable != null) {
|
||||
paramASN1Encodable.toASN1Primitive().toDERObject().encode(this);
|
||||
} else {
|
||||
throw new IOException("null object detected");
|
||||
}
|
||||
}
|
||||
|
||||
ASN1OutputStream getDERSubStream() {
|
||||
return this;
|
||||
}
|
||||
|
||||
ASN1OutputStream getDLSubStream() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERPrintableString extends ASN1Primitive implements ASN1String {
|
||||
private final byte[] string;
|
||||
|
||||
public static DERPrintableString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERPrintableString)
|
||||
return (DERPrintableString)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERPrintableString)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERPrintableString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERPrintableString) ? getInstance(aSN1Primitive) : new DERPrintableString(ASN1OctetString.getInstance(aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
DERPrintableString(byte[] paramArrayOfbyte) {
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public DERPrintableString(String paramString) {
|
||||
this(paramString, false);
|
||||
}
|
||||
|
||||
public DERPrintableString(String paramString, boolean paramBoolean) {
|
||||
if (paramBoolean && !isPrintableString(paramString))
|
||||
throw new IllegalArgumentException("string contains illegal characters");
|
||||
this.string = Strings.toByteArray(paramString);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromByteArray(this.string);
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return Arrays.clone(this.string);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(19, this.string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof DERPrintableString))
|
||||
return false;
|
||||
DERPrintableString dERPrintableString = (DERPrintableString)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.string, dERPrintableString.string);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
public static boolean isPrintableString(String paramString) {
|
||||
for (int i = paramString.length() - 1; i >= 0; i--) {
|
||||
char c = paramString.charAt(i);
|
||||
if (c > '\u007F')
|
||||
return false;
|
||||
if (('a' > c || c > 'z') && ('A' > c || c > 'Z') && ('0' > c || c > '9'))
|
||||
switch (c) {
|
||||
case ' ':
|
||||
case '\'':
|
||||
case '(':
|
||||
case ')':
|
||||
case '+':
|
||||
case ',':
|
||||
case '-':
|
||||
case '.':
|
||||
case '/':
|
||||
case ':':
|
||||
case '=':
|
||||
case '?':
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class DERSequence extends ASN1Sequence {
|
||||
private int bodyLength = -1;
|
||||
|
||||
public DERSequence() {}
|
||||
|
||||
public DERSequence(ASN1Encodable paramASN1Encodable) {
|
||||
super(paramASN1Encodable);
|
||||
}
|
||||
|
||||
public DERSequence(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
super(paramASN1EncodableVector);
|
||||
}
|
||||
|
||||
public DERSequence(ASN1Encodable[] paramArrayOfASN1Encodable) {
|
||||
super(paramArrayOfASN1Encodable);
|
||||
}
|
||||
|
||||
private int getBodyLength() throws IOException {
|
||||
if (this.bodyLength < 0) {
|
||||
int i = 0;
|
||||
Enumeration<Object> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable = enumeration.nextElement();
|
||||
i += ((ASN1Encodable)aSN1Encodable).toASN1Primitive().toDERObject().encodedLength();
|
||||
}
|
||||
this.bodyLength = i;
|
||||
}
|
||||
return this.bodyLength;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
int i = getBodyLength();
|
||||
return 1 + StreamUtil.calculateBodyLength(i) + i;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
ASN1OutputStream aSN1OutputStream = paramASN1OutputStream.getDERSubStream();
|
||||
int i = getBodyLength();
|
||||
paramASN1OutputStream.write(48);
|
||||
paramASN1OutputStream.writeLength(i);
|
||||
Enumeration<Object> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable = enumeration.nextElement();
|
||||
aSN1OutputStream.writeObject(aSN1Encodable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class DERSequenceGenerator extends DERGenerator {
|
||||
private final ByteArrayOutputStream _bOut = new ByteArrayOutputStream();
|
||||
|
||||
public DERSequenceGenerator(OutputStream paramOutputStream) throws IOException {
|
||||
super(paramOutputStream);
|
||||
}
|
||||
|
||||
public DERSequenceGenerator(OutputStream paramOutputStream, int paramInt, boolean paramBoolean) throws IOException {
|
||||
super(paramOutputStream, paramInt, paramBoolean);
|
||||
}
|
||||
|
||||
public void addObject(ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
paramASN1Encodable.toASN1Primitive().encode(new DEROutputStream(this._bOut));
|
||||
}
|
||||
|
||||
public OutputStream getRawOutputStream() {
|
||||
return this._bOut;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
writeDEREncoded(48, this._bOut.toByteArray());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DERSequenceParser implements ASN1SequenceParser {
|
||||
private ASN1StreamParser _parser;
|
||||
|
||||
DERSequenceParser(ASN1StreamParser paramASN1StreamParser) {
|
||||
this._parser = paramASN1StreamParser;
|
||||
}
|
||||
|
||||
public ASN1Encodable readObject() throws IOException {
|
||||
return this._parser.readObject();
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() throws IOException {
|
||||
return new DERSequence(this._parser.readVector());
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
try {
|
||||
return getLoadedObject();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class DERSet extends ASN1Set {
|
||||
private int bodyLength = -1;
|
||||
|
||||
public DERSet() {}
|
||||
|
||||
public DERSet(ASN1Encodable paramASN1Encodable) {
|
||||
super(paramASN1Encodable);
|
||||
}
|
||||
|
||||
public DERSet(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
super(paramASN1EncodableVector, true);
|
||||
}
|
||||
|
||||
public DERSet(ASN1Encodable[] paramArrayOfASN1Encodable) {
|
||||
super(paramArrayOfASN1Encodable, true);
|
||||
}
|
||||
|
||||
DERSet(ASN1EncodableVector paramASN1EncodableVector, boolean paramBoolean) {
|
||||
super(paramASN1EncodableVector, paramBoolean);
|
||||
}
|
||||
|
||||
private int getBodyLength() throws IOException {
|
||||
if (this.bodyLength < 0) {
|
||||
int i = 0;
|
||||
Enumeration<Object> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable = enumeration.nextElement();
|
||||
i += ((ASN1Encodable)aSN1Encodable).toASN1Primitive().toDERObject().encodedLength();
|
||||
}
|
||||
this.bodyLength = i;
|
||||
}
|
||||
return this.bodyLength;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
int i = getBodyLength();
|
||||
return 1 + StreamUtil.calculateBodyLength(i) + i;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
ASN1OutputStream aSN1OutputStream = paramASN1OutputStream.getDERSubStream();
|
||||
int i = getBodyLength();
|
||||
paramASN1OutputStream.write(49);
|
||||
paramASN1OutputStream.writeLength(i);
|
||||
Enumeration<Object> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable = enumeration.nextElement();
|
||||
aSN1OutputStream.writeObject(aSN1Encodable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DERSetParser implements ASN1SetParser {
|
||||
private ASN1StreamParser _parser;
|
||||
|
||||
DERSetParser(ASN1StreamParser paramASN1StreamParser) {
|
||||
this._parser = paramASN1StreamParser;
|
||||
}
|
||||
|
||||
public ASN1Encodable readObject() throws IOException {
|
||||
return this._parser.readObject();
|
||||
}
|
||||
|
||||
public ASN1Primitive getLoadedObject() throws IOException {
|
||||
return new DERSet(this._parser.readVector(), false);
|
||||
}
|
||||
|
||||
public ASN1Primitive toASN1Primitive() {
|
||||
try {
|
||||
return getLoadedObject();
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERT61String extends ASN1Primitive implements ASN1String {
|
||||
private final byte[] string;
|
||||
|
||||
public static DERT61String getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERT61String)
|
||||
return (DERT61String)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERT61String)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERT61String getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERT61String) ? getInstance(aSN1Primitive) : new DERT61String(ASN1OctetString.getInstance(aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
public DERT61String(byte[] paramArrayOfbyte) {
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public DERT61String(String paramString) {
|
||||
this(Strings.toByteArray(paramString));
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromByteArray(this.string);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(20, this.string);
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return Arrays.clone(this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
return !(paramASN1Primitive instanceof DERT61String) ? false : Arrays.areEqual(this.string, ((DERT61String)paramASN1Primitive).string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERT61UTF8String extends ASN1Primitive implements ASN1String {
|
||||
private byte[] string;
|
||||
|
||||
public static DERT61UTF8String getInstance(Object paramObject) {
|
||||
if (paramObject instanceof DERT61String)
|
||||
return new DERT61UTF8String(((DERT61String)paramObject).getOctets());
|
||||
if (paramObject == null || paramObject instanceof DERT61UTF8String)
|
||||
return (DERT61UTF8String)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return new DERT61UTF8String(((DERT61String)fromByteArray((byte[])paramObject)).getOctets());
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERT61UTF8String getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERT61String || aSN1Primitive instanceof DERT61UTF8String) ? getInstance(aSN1Primitive) : new DERT61UTF8String(ASN1OctetString.getInstance(aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
public DERT61UTF8String(byte[] paramArrayOfbyte) {
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public DERT61UTF8String(String paramString) {
|
||||
this(Strings.toUTF8ByteArray(paramString));
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromUTF8ByteArray(this.string);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(20, this.string);
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return Arrays.clone(this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
return !(paramASN1Primitive instanceof DERT61UTF8String) ? false : Arrays.areEqual(this.string, ((DERT61UTF8String)paramASN1Primitive).string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DERTaggedObject extends ASN1TaggedObject {
|
||||
private static final byte[] ZERO_BYTES = new byte[0];
|
||||
|
||||
public DERTaggedObject(boolean paramBoolean, int paramInt, ASN1Encodable paramASN1Encodable) {
|
||||
super(paramBoolean, paramInt, paramASN1Encodable);
|
||||
}
|
||||
|
||||
public DERTaggedObject(int paramInt, ASN1Encodable paramASN1Encodable) {
|
||||
super(true, paramInt, paramASN1Encodable);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
if (!this.empty) {
|
||||
if (this.explicit)
|
||||
return true;
|
||||
ASN1Primitive aSN1Primitive = this.obj.toASN1Primitive().toDERObject();
|
||||
return aSN1Primitive.isConstructed();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
if (!this.empty) {
|
||||
ASN1Primitive aSN1Primitive = this.obj.toASN1Primitive().toDERObject();
|
||||
int i = aSN1Primitive.encodedLength();
|
||||
return this.explicit ? (StreamUtil.calculateTagLength(this.tagNo) + StreamUtil.calculateBodyLength(i) + i) : (StreamUtil.calculateTagLength(this.tagNo) + --i);
|
||||
}
|
||||
return StreamUtil.calculateTagLength(this.tagNo) + 1;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
if (!this.empty) {
|
||||
ASN1Primitive aSN1Primitive = this.obj.toASN1Primitive().toDERObject();
|
||||
if (this.explicit) {
|
||||
paramASN1OutputStream.writeTag(160, this.tagNo);
|
||||
paramASN1OutputStream.writeLength(aSN1Primitive.encodedLength());
|
||||
paramASN1OutputStream.writeObject(aSN1Primitive);
|
||||
} else {
|
||||
int i;
|
||||
if (aSN1Primitive.isConstructed()) {
|
||||
i = 160;
|
||||
} else {
|
||||
i = 128;
|
||||
}
|
||||
paramASN1OutputStream.writeTag(i, this.tagNo);
|
||||
paramASN1OutputStream.writeImplicitObject(aSN1Primitive);
|
||||
}
|
||||
} else {
|
||||
paramASN1OutputStream.writeEncoded(160, this.tagNo, ZERO_BYTES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public interface DERTags extends BERTags {}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class DERUTCTime extends ASN1UTCTime {
|
||||
DERUTCTime(byte[] paramArrayOfbyte) {
|
||||
super(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public DERUTCTime(Date paramDate) {
|
||||
super(paramDate);
|
||||
}
|
||||
|
||||
public DERUTCTime(String paramString) {
|
||||
super(paramString);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERUTF8String extends ASN1Primitive implements ASN1String {
|
||||
private final byte[] string;
|
||||
|
||||
public static DERUTF8String getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERUTF8String)
|
||||
return (DERUTF8String)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERUTF8String)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERUTF8String getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERUTF8String) ? getInstance(aSN1Primitive) : new DERUTF8String(ASN1OctetString.getInstance(aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
DERUTF8String(byte[] paramArrayOfbyte) {
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public DERUTF8String(String paramString) {
|
||||
this.string = Strings.toUTF8ByteArray(paramString);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromUTF8ByteArray(this.string);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof DERUTF8String))
|
||||
return false;
|
||||
DERUTF8String dERUTF8String = (DERUTF8String)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.string, dERUTF8String.string);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(12, this.string);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
|
||||
public class DERUniversalString extends ASN1Primitive implements ASN1String {
|
||||
private static final char[] table = new char[] {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F' };
|
||||
|
||||
private final byte[] string;
|
||||
|
||||
public static DERUniversalString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERUniversalString)
|
||||
return (DERUniversalString)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERUniversalString)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERUniversalString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERUniversalString) ? getInstance(aSN1Primitive) : new DERUniversalString(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
public DERUniversalString(byte[] paramArrayOfbyte) {
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
StringBuffer stringBuffer = new StringBuffer("#");
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
ASN1OutputStream aSN1OutputStream = new ASN1OutputStream(byteArrayOutputStream);
|
||||
try {
|
||||
aSN1OutputStream.writeObject(this);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("internal error encoding BitString");
|
||||
}
|
||||
byte[] arrayOfByte = byteArrayOutputStream.toByteArray();
|
||||
for (int i = 0; i != arrayOfByte.length; i++) {
|
||||
stringBuffer.append(table[arrayOfByte[i] >>> 4 & 0xF]);
|
||||
stringBuffer.append(table[arrayOfByte[i] & 0xF]);
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return this.string;
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(28, getOctets());
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
return !(paramASN1Primitive instanceof DERUniversalString) ? false : Arrays.areEqual(this.string, ((DERUniversalString)paramASN1Primitive).string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERVideotexString extends ASN1Primitive implements ASN1String {
|
||||
private final byte[] string;
|
||||
|
||||
public static DERVideotexString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERVideotexString)
|
||||
return (DERVideotexString)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERVideotexString)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERVideotexString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERVideotexString) ? getInstance(aSN1Primitive) : new DERVideotexString(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
public DERVideotexString(byte[] paramArrayOfbyte) {
|
||||
this.string = Arrays.clone(paramArrayOfbyte);
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return Arrays.clone(this.string);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(21, this.string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
if (!(paramASN1Primitive instanceof DERVideotexString))
|
||||
return false;
|
||||
DERVideotexString dERVideotexString = (DERVideotexString)paramASN1Primitive;
|
||||
return Arrays.areEqual(this.string, dERVideotexString.string);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromByteArray(this.string);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bouncycastle.util.Strings;
|
||||
|
||||
public class DERVisibleString extends ASN1Primitive implements ASN1String {
|
||||
private final byte[] string;
|
||||
|
||||
public static DERVisibleString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DERVisibleString)
|
||||
return (DERVisibleString)paramObject;
|
||||
if (paramObject instanceof byte[])
|
||||
try {
|
||||
return (DERVisibleString)fromByteArray((byte[])paramObject);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
|
||||
}
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static DERVisibleString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DERVisibleString) ? getInstance(aSN1Primitive) : new DERVisibleString(ASN1OctetString.getInstance(aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
DERVisibleString(byte[] paramArrayOfbyte) {
|
||||
this.string = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
public DERVisibleString(String paramString) {
|
||||
this.string = Strings.toByteArray(paramString);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return Strings.fromByteArray(this.string);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
public byte[] getOctets() {
|
||||
return Arrays.clone(this.string);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.string.length) + this.string.length;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
paramASN1OutputStream.writeEncoded(26, this.string);
|
||||
}
|
||||
|
||||
boolean asn1Equals(ASN1Primitive paramASN1Primitive) {
|
||||
return !(paramASN1Primitive instanceof DERVisibleString) ? false : Arrays.areEqual(this.string, ((DERVisibleString)paramASN1Primitive).string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.string);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DLBitString extends ASN1BitString {
|
||||
public static ASN1BitString getInstance(Object paramObject) {
|
||||
if (paramObject == null || paramObject instanceof DLBitString)
|
||||
return (DLBitString)paramObject;
|
||||
if (paramObject instanceof DERBitString)
|
||||
return (DERBitString)paramObject;
|
||||
throw new IllegalArgumentException("illegal object in getInstance: " + paramObject.getClass().getName());
|
||||
}
|
||||
|
||||
public static ASN1BitString getInstance(ASN1TaggedObject paramASN1TaggedObject, boolean paramBoolean) {
|
||||
ASN1Primitive aSN1Primitive = paramASN1TaggedObject.getObject();
|
||||
return (paramBoolean || aSN1Primitive instanceof DLBitString) ? getInstance(aSN1Primitive) : fromOctetString(((ASN1OctetString)aSN1Primitive).getOctets());
|
||||
}
|
||||
|
||||
protected DLBitString(byte paramByte, int paramInt) {
|
||||
this(toByteArray(paramByte), paramInt);
|
||||
}
|
||||
|
||||
private static byte[] toByteArray(byte paramByte) {
|
||||
byte[] arrayOfByte = new byte[1];
|
||||
arrayOfByte[0] = paramByte;
|
||||
return arrayOfByte;
|
||||
}
|
||||
|
||||
public DLBitString(byte[] paramArrayOfbyte, int paramInt) {
|
||||
super(paramArrayOfbyte, paramInt);
|
||||
}
|
||||
|
||||
public DLBitString(byte[] paramArrayOfbyte) {
|
||||
this(paramArrayOfbyte, 0);
|
||||
}
|
||||
|
||||
public DLBitString(int paramInt) {
|
||||
super(getBytes(paramInt), getPadBits(paramInt));
|
||||
}
|
||||
|
||||
public DLBitString(ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
super(paramASN1Encodable.toASN1Primitive().getEncoded("DER"), 0);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int encodedLength() {
|
||||
return 1 + StreamUtil.calculateBodyLength(this.data.length + 1) + this.data.length + 1;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
byte[] arrayOfByte1 = this.data;
|
||||
byte[] arrayOfByte2 = new byte[arrayOfByte1.length + 1];
|
||||
arrayOfByte2[0] = (byte)getPadBits();
|
||||
System.arraycopy(arrayOfByte1, 0, arrayOfByte2, 1, arrayOfByte2.length - 1);
|
||||
paramASN1OutputStream.writeEncoded(3, arrayOfByte2);
|
||||
}
|
||||
|
||||
static DLBitString fromOctetString(byte[] paramArrayOfbyte) {
|
||||
if (paramArrayOfbyte.length < 1)
|
||||
throw new IllegalArgumentException("truncated BIT STRING detected");
|
||||
byte b = paramArrayOfbyte[0];
|
||||
byte[] arrayOfByte = new byte[paramArrayOfbyte.length - 1];
|
||||
if (arrayOfByte.length != 0)
|
||||
System.arraycopy(paramArrayOfbyte, 1, arrayOfByte, 0, paramArrayOfbyte.length - 1);
|
||||
return new DLBitString(arrayOfByte, b);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class DLOutputStream extends ASN1OutputStream {
|
||||
public DLOutputStream(OutputStream paramOutputStream) {
|
||||
super(paramOutputStream);
|
||||
}
|
||||
|
||||
public void writeObject(ASN1Encodable paramASN1Encodable) throws IOException {
|
||||
if (paramASN1Encodable != null) {
|
||||
paramASN1Encodable.toASN1Primitive().toDLObject().encode(this);
|
||||
} else {
|
||||
throw new IOException("null object detected");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class DLSequence extends ASN1Sequence {
|
||||
private int bodyLength = -1;
|
||||
|
||||
public DLSequence() {}
|
||||
|
||||
public DLSequence(ASN1Encodable paramASN1Encodable) {
|
||||
super(paramASN1Encodable);
|
||||
}
|
||||
|
||||
public DLSequence(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
super(paramASN1EncodableVector);
|
||||
}
|
||||
|
||||
public DLSequence(ASN1Encodable[] paramArrayOfASN1Encodable) {
|
||||
super(paramArrayOfASN1Encodable);
|
||||
}
|
||||
|
||||
private int getBodyLength() throws IOException {
|
||||
if (this.bodyLength < 0) {
|
||||
int i = 0;
|
||||
Enumeration<Object> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable = enumeration.nextElement();
|
||||
i += ((ASN1Encodable)aSN1Encodable).toASN1Primitive().toDLObject().encodedLength();
|
||||
}
|
||||
this.bodyLength = i;
|
||||
}
|
||||
return this.bodyLength;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
int i = getBodyLength();
|
||||
return 1 + StreamUtil.calculateBodyLength(i) + i;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
ASN1OutputStream aSN1OutputStream = paramASN1OutputStream.getDLSubStream();
|
||||
int i = getBodyLength();
|
||||
paramASN1OutputStream.write(48);
|
||||
paramASN1OutputStream.writeLength(i);
|
||||
Enumeration<Object> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable = enumeration.nextElement();
|
||||
aSN1OutputStream.writeObject(aSN1Encodable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class DLSet extends ASN1Set {
|
||||
private int bodyLength = -1;
|
||||
|
||||
public DLSet() {}
|
||||
|
||||
public DLSet(ASN1Encodable paramASN1Encodable) {
|
||||
super(paramASN1Encodable);
|
||||
}
|
||||
|
||||
public DLSet(ASN1EncodableVector paramASN1EncodableVector) {
|
||||
super(paramASN1EncodableVector, false);
|
||||
}
|
||||
|
||||
public DLSet(ASN1Encodable[] paramArrayOfASN1Encodable) {
|
||||
super(paramArrayOfASN1Encodable, false);
|
||||
}
|
||||
|
||||
private int getBodyLength() throws IOException {
|
||||
if (this.bodyLength < 0) {
|
||||
int i = 0;
|
||||
Enumeration<Object> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable = enumeration.nextElement();
|
||||
i += ((ASN1Encodable)aSN1Encodable).toASN1Primitive().toDLObject().encodedLength();
|
||||
}
|
||||
this.bodyLength = i;
|
||||
}
|
||||
return this.bodyLength;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
int i = getBodyLength();
|
||||
return 1 + StreamUtil.calculateBodyLength(i) + i;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
ASN1OutputStream aSN1OutputStream = paramASN1OutputStream.getDLSubStream();
|
||||
int i = getBodyLength();
|
||||
paramASN1OutputStream.write(49);
|
||||
paramASN1OutputStream.writeLength(i);
|
||||
Enumeration<Object> enumeration = getObjects();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
ASN1Encodable aSN1Encodable = enumeration.nextElement();
|
||||
aSN1OutputStream.writeObject(aSN1Encodable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DLTaggedObject extends ASN1TaggedObject {
|
||||
private static final byte[] ZERO_BYTES = new byte[0];
|
||||
|
||||
public DLTaggedObject(boolean paramBoolean, int paramInt, ASN1Encodable paramASN1Encodable) {
|
||||
super(paramBoolean, paramInt, paramASN1Encodable);
|
||||
}
|
||||
|
||||
boolean isConstructed() {
|
||||
if (!this.empty) {
|
||||
if (this.explicit)
|
||||
return true;
|
||||
ASN1Primitive aSN1Primitive = this.obj.toASN1Primitive().toDLObject();
|
||||
return aSN1Primitive.isConstructed();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
if (!this.empty) {
|
||||
int i = this.obj.toASN1Primitive().toDLObject().encodedLength();
|
||||
return this.explicit ? (StreamUtil.calculateTagLength(this.tagNo) + StreamUtil.calculateBodyLength(i) + i) : (StreamUtil.calculateTagLength(this.tagNo) + --i);
|
||||
}
|
||||
return StreamUtil.calculateTagLength(this.tagNo) + 1;
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
if (!this.empty) {
|
||||
ASN1Primitive aSN1Primitive = this.obj.toASN1Primitive().toDLObject();
|
||||
if (this.explicit) {
|
||||
paramASN1OutputStream.writeTag(160, this.tagNo);
|
||||
paramASN1OutputStream.writeLength(aSN1Primitive.encodedLength());
|
||||
paramASN1OutputStream.writeObject(aSN1Primitive);
|
||||
} else {
|
||||
int i;
|
||||
if (aSN1Primitive.isConstructed()) {
|
||||
i = 160;
|
||||
} else {
|
||||
i = 128;
|
||||
}
|
||||
paramASN1OutputStream.writeTag(i, this.tagNo);
|
||||
paramASN1OutputStream.writeImplicitObject(aSN1Primitive);
|
||||
}
|
||||
} else {
|
||||
paramASN1OutputStream.writeEncoded(160, this.tagNo, ZERO_BYTES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
|
||||
class DefiniteLengthInputStream extends LimitedInputStream {
|
||||
private static final byte[] EMPTY_BYTES = new byte[0];
|
||||
|
||||
private final int _originalLength;
|
||||
|
||||
private int _remaining;
|
||||
|
||||
DefiniteLengthInputStream(InputStream paramInputStream, int paramInt) {
|
||||
super(paramInputStream, paramInt);
|
||||
if (paramInt < 0)
|
||||
throw new IllegalArgumentException("negative lengths not allowed");
|
||||
this._originalLength = paramInt;
|
||||
this._remaining = paramInt;
|
||||
if (paramInt == 0)
|
||||
setParentEofDetect(true);
|
||||
}
|
||||
|
||||
int getRemaining() {
|
||||
return this._remaining;
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
if (this._remaining == 0)
|
||||
return -1;
|
||||
int i = this._in.read();
|
||||
if (i < 0)
|
||||
throw new EOFException("DEF length " + this._originalLength + " object truncated by " + this._remaining);
|
||||
if (--this._remaining == 0)
|
||||
setParentEofDetect(true);
|
||||
return i;
|
||||
}
|
||||
|
||||
public int read(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) throws IOException {
|
||||
if (this._remaining == 0)
|
||||
return -1;
|
||||
int i = Math.min(paramInt2, this._remaining);
|
||||
int j = this._in.read(paramArrayOfbyte, paramInt1, i);
|
||||
if (j < 0)
|
||||
throw new EOFException("DEF length " + this._originalLength + " object truncated by " + this._remaining);
|
||||
if ((this._remaining -= j) == 0)
|
||||
setParentEofDetect(true);
|
||||
return j;
|
||||
}
|
||||
|
||||
byte[] toByteArray() throws IOException {
|
||||
if (this._remaining == 0)
|
||||
return EMPTY_BYTES;
|
||||
byte[] arrayOfByte = new byte[this._remaining];
|
||||
if ((this._remaining -= Streams.readFully(this._in, arrayOfByte)) != 0)
|
||||
throw new EOFException("DEF length " + this._originalLength + " object truncated by " + this._remaining);
|
||||
setParentEofDetect(true);
|
||||
return arrayOfByte;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface InMemoryRepresentable {
|
||||
ASN1Primitive getLoadedObject() throws IOException;
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
class IndefiniteLengthInputStream extends LimitedInputStream {
|
||||
private int _b1;
|
||||
|
||||
private int _b2;
|
||||
|
||||
private boolean _eofReached = false;
|
||||
|
||||
private boolean _eofOn00 = true;
|
||||
|
||||
IndefiniteLengthInputStream(InputStream paramInputStream, int paramInt) throws IOException {
|
||||
super(paramInputStream, paramInt);
|
||||
this._b1 = paramInputStream.read();
|
||||
this._b2 = paramInputStream.read();
|
||||
if (this._b2 < 0)
|
||||
throw new EOFException();
|
||||
checkForEof();
|
||||
}
|
||||
|
||||
void setEofOn00(boolean paramBoolean) {
|
||||
this._eofOn00 = paramBoolean;
|
||||
checkForEof();
|
||||
}
|
||||
|
||||
private boolean checkForEof() {
|
||||
if (!this._eofReached && this._eofOn00 && this._b1 == 0 && this._b2 == 0) {
|
||||
this._eofReached = true;
|
||||
setParentEofDetect(true);
|
||||
}
|
||||
return this._eofReached;
|
||||
}
|
||||
|
||||
public int read(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) throws IOException {
|
||||
if (this._eofOn00 || paramInt2 < 3)
|
||||
return super.read(paramArrayOfbyte, paramInt1, paramInt2);
|
||||
if (this._eofReached)
|
||||
return -1;
|
||||
int i = this._in.read(paramArrayOfbyte, paramInt1 + 2, paramInt2 - 2);
|
||||
if (i < 0)
|
||||
throw new EOFException();
|
||||
paramArrayOfbyte[paramInt1] = (byte)this._b1;
|
||||
paramArrayOfbyte[paramInt1 + 1] = (byte)this._b2;
|
||||
this._b1 = this._in.read();
|
||||
this._b2 = this._in.read();
|
||||
if (this._b2 < 0)
|
||||
throw new EOFException();
|
||||
return i + 2;
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
if (checkForEof())
|
||||
return -1;
|
||||
int i = this._in.read();
|
||||
if (i < 0)
|
||||
throw new EOFException();
|
||||
int j = this._b1;
|
||||
this._b1 = this._b2;
|
||||
this._b2 = i;
|
||||
return j;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
class LazyConstructionEnumeration implements Enumeration {
|
||||
private ASN1InputStream aIn;
|
||||
|
||||
private Object nextObj;
|
||||
|
||||
public LazyConstructionEnumeration(byte[] paramArrayOfbyte) {
|
||||
this.aIn = new ASN1InputStream(paramArrayOfbyte, true);
|
||||
this.nextObj = readObject();
|
||||
}
|
||||
|
||||
public boolean hasMoreElements() {
|
||||
return (this.nextObj != null);
|
||||
}
|
||||
|
||||
public Object nextElement() {
|
||||
Object object = this.nextObj;
|
||||
this.nextObj = readObject();
|
||||
return object;
|
||||
}
|
||||
|
||||
private Object readObject() {
|
||||
try {
|
||||
return this.aIn.readObject();
|
||||
} catch (IOException e) {
|
||||
throw new ASN1ParsingException("malformed DER construction: " + e, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
class LazyEncodedSequence extends ASN1Sequence {
|
||||
private byte[] encoded;
|
||||
|
||||
LazyEncodedSequence(byte[] paramArrayOfbyte) throws IOException {
|
||||
this.encoded = paramArrayOfbyte;
|
||||
}
|
||||
|
||||
private void parse() {
|
||||
LazyConstructionEnumeration lazyConstructionEnumeration = new LazyConstructionEnumeration(this.encoded);
|
||||
while (lazyConstructionEnumeration.hasMoreElements())
|
||||
this.seq.addElement(lazyConstructionEnumeration.nextElement());
|
||||
this.encoded = null;
|
||||
}
|
||||
|
||||
public synchronized ASN1Encodable getObjectAt(int paramInt) {
|
||||
if (this.encoded != null)
|
||||
parse();
|
||||
return super.getObjectAt(paramInt);
|
||||
}
|
||||
|
||||
public synchronized Enumeration getObjects() {
|
||||
return (this.encoded == null) ? super.getObjects() : new LazyConstructionEnumeration(this.encoded);
|
||||
}
|
||||
|
||||
public synchronized int size() {
|
||||
if (this.encoded != null)
|
||||
parse();
|
||||
return super.size();
|
||||
}
|
||||
|
||||
ASN1Primitive toDERObject() {
|
||||
if (this.encoded != null)
|
||||
parse();
|
||||
return super.toDERObject();
|
||||
}
|
||||
|
||||
ASN1Primitive toDLObject() {
|
||||
if (this.encoded != null)
|
||||
parse();
|
||||
return super.toDLObject();
|
||||
}
|
||||
|
||||
int encodedLength() throws IOException {
|
||||
return (this.encoded != null) ? (1 + StreamUtil.calculateBodyLength(this.encoded.length) + this.encoded.length) : super.toDLObject().encodedLength();
|
||||
}
|
||||
|
||||
void encode(ASN1OutputStream paramASN1OutputStream) throws IOException {
|
||||
if (this.encoded != null) {
|
||||
paramASN1OutputStream.writeEncoded(48, this.encoded);
|
||||
} else {
|
||||
super.toDLObject().encode(paramASN1OutputStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
abstract class LimitedInputStream extends InputStream {
|
||||
protected final InputStream _in;
|
||||
|
||||
private int _limit;
|
||||
|
||||
LimitedInputStream(InputStream paramInputStream, int paramInt) {
|
||||
this._in = paramInputStream;
|
||||
this._limit = paramInt;
|
||||
}
|
||||
|
||||
int getRemaining() {
|
||||
return this._limit;
|
||||
}
|
||||
|
||||
protected void setParentEofDetect(boolean paramBoolean) {
|
||||
if (this._in instanceof IndefiniteLengthInputStream)
|
||||
((IndefiniteLengthInputStream)this._in).setEofOn00(paramBoolean);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
public class OIDTokenizer {
|
||||
private String oid;
|
||||
|
||||
private int index;
|
||||
|
||||
public OIDTokenizer(String paramString) {
|
||||
this.oid = paramString;
|
||||
this.index = 0;
|
||||
}
|
||||
|
||||
public boolean hasMoreTokens() {
|
||||
return (this.index != -1);
|
||||
}
|
||||
|
||||
public String nextToken() {
|
||||
if (this.index == -1)
|
||||
return null;
|
||||
int i = this.oid.indexOf('.', this.index);
|
||||
if (i == -1) {
|
||||
String str1 = this.oid.substring(this.index);
|
||||
this.index = -1;
|
||||
return str1;
|
||||
}
|
||||
String str = this.oid.substring(this.index, i);
|
||||
this.index = i + 1;
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package org.bouncycastle.asn1;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
class StreamUtil {
|
||||
private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory();
|
||||
|
||||
static int findLimit(InputStream paramInputStream) {
|
||||
if (paramInputStream instanceof LimitedInputStream)
|
||||
return ((LimitedInputStream)paramInputStream).getRemaining();
|
||||
if (paramInputStream instanceof ASN1InputStream)
|
||||
return ((ASN1InputStream)paramInputStream).getLimit();
|
||||
if (paramInputStream instanceof ByteArrayInputStream)
|
||||
return ((ByteArrayInputStream)paramInputStream).available();
|
||||
if (paramInputStream instanceof FileInputStream)
|
||||
try {
|
||||
FileChannel fileChannel = ((FileInputStream)paramInputStream).getChannel();
|
||||
long l = (fileChannel != null) ? fileChannel.size() : Integer.MAX_VALUE;
|
||||
if (l < Integer.MAX_VALUE)
|
||||
return (int)l;
|
||||
} catch (IOException e) {}
|
||||
return (MAX_MEMORY > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)MAX_MEMORY;
|
||||
}
|
||||
|
||||
static int calculateBodyLength(int paramInt) {
|
||||
int i = 1;
|
||||
if (paramInt > 127) {
|
||||
int j = 1;
|
||||
int k = paramInt;
|
||||
while ((k >>>= 8) != 0)
|
||||
j++;
|
||||
for (int m = (j - 1) * 8; m >= 0; m -= 8)
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static int calculateTagLength(int paramInt) throws IOException {
|
||||
int i = 1;
|
||||
if (paramInt >= 31)
|
||||
if (paramInt < 128) {
|
||||
i++;
|
||||
} else {
|
||||
byte[] arrayOfByte = new byte[5];
|
||||
int j = arrayOfByte.length;
|
||||
arrayOfByte[--j] = (byte)(paramInt & 0x7F);
|
||||
while (true) {
|
||||
paramInt >>= 7;
|
||||
arrayOfByte[--j] = (byte)(paramInt & 0x7F | 0x80);
|
||||
if (paramInt <= 127) {
|
||||
i += arrayOfByte.length - j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
package org.bouncycastle.asn1.anssi;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
||||
import org.bouncycastle.asn1.x9.X9ECParameters;
|
||||
import org.bouncycastle.asn1.x9.X9ECParametersHolder;
|
||||
import org.bouncycastle.asn1.x9.X9ECPoint;
|
||||
import org.bouncycastle.math.ec.ECCurve;
|
||||
import org.bouncycastle.util.Strings;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
|
||||
public class ANSSINamedCurves {
|
||||
static X9ECParametersHolder FRP256v1 = new X9ECParametersHolder() {
|
||||
protected X9ECParameters createParameters() {
|
||||
BigInteger bigInteger1 = ANSSINamedCurves.fromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C03");
|
||||
BigInteger bigInteger2 = ANSSINamedCurves.fromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C00");
|
||||
BigInteger bigInteger3 = ANSSINamedCurves.fromHex("EE353FCA5428A9300D4ABA754A44C00FDFEC0C9AE4B1A1803075ED967B7BB73F");
|
||||
byte[] arrayOfByte = null;
|
||||
BigInteger bigInteger4 = ANSSINamedCurves.fromHex("F1FD178C0B3AD58F10126DE8CE42435B53DC67E140D2BF941FFDD459C6D655E1");
|
||||
BigInteger bigInteger5 = BigInteger.valueOf(1L);
|
||||
ECCurve eCCurve = ANSSINamedCurves.configureCurve(new ECCurve.Fp(bigInteger1, bigInteger2, bigInteger3, bigInteger4, bigInteger5));
|
||||
X9ECPoint x9ECPoint = new X9ECPoint(eCCurve, Hex.decode("04B6B3D4C356C139EB31183D4749D423958C27D2DCAF98B70164C97A2DD98F5CFF6142E0F7C8B204911F9271F0F3ECEF8C2701C307E8E4C9E183115A1554062CFB"));
|
||||
return new X9ECParameters(eCCurve, x9ECPoint, bigInteger4, bigInteger5, arrayOfByte);
|
||||
}
|
||||
};
|
||||
|
||||
static final Hashtable objIds = new Hashtable();
|
||||
|
||||
static final Hashtable curves = new Hashtable();
|
||||
|
||||
static final Hashtable names = new Hashtable();
|
||||
|
||||
private static ECCurve configureCurve(ECCurve paramECCurve) {
|
||||
return paramECCurve;
|
||||
}
|
||||
|
||||
private static BigInteger fromHex(String paramString) {
|
||||
return new BigInteger(1, Hex.decode(paramString));
|
||||
}
|
||||
|
||||
static void defineCurve(String paramString, ASN1ObjectIdentifier paramASN1ObjectIdentifier, X9ECParametersHolder paramX9ECParametersHolder) {
|
||||
objIds.put(paramString.toLowerCase(), paramASN1ObjectIdentifier);
|
||||
names.put(paramASN1ObjectIdentifier, paramString);
|
||||
curves.put(paramASN1ObjectIdentifier, paramX9ECParametersHolder);
|
||||
}
|
||||
|
||||
public static X9ECParameters getByName(String paramString) {
|
||||
ASN1ObjectIdentifier aSN1ObjectIdentifier = getOID(paramString);
|
||||
return (aSN1ObjectIdentifier == null) ? null : getByOID(aSN1ObjectIdentifier);
|
||||
}
|
||||
|
||||
public static X9ECParameters getByOID(ASN1ObjectIdentifier paramASN1ObjectIdentifier) {
|
||||
X9ECParametersHolder x9ECParametersHolder = (X9ECParametersHolder)curves.get(paramASN1ObjectIdentifier);
|
||||
return (x9ECParametersHolder == null) ? null : x9ECParametersHolder.getParameters();
|
||||
}
|
||||
|
||||
public static ASN1ObjectIdentifier getOID(String paramString) {
|
||||
return (ASN1ObjectIdentifier)objIds.get(Strings.toLowerCase(paramString));
|
||||
}
|
||||
|
||||
public static String getName(ASN1ObjectIdentifier paramASN1ObjectIdentifier) {
|
||||
return (String)names.get(paramASN1ObjectIdentifier);
|
||||
}
|
||||
|
||||
public static Enumeration getNames() {
|
||||
return names.elements();
|
||||
}
|
||||
|
||||
static {
|
||||
defineCurve("FRP256v1", ANSSIObjectIdentifiers.FRP256v1, FRP256v1);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue