333 lines
12 KiB
Java
333 lines
12 KiB
Java
|
package org.bouncycastle.asn1;
|
||
|
|
||
|
import com.google.common.base.Ascii;
|
||
|
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;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class ASN1ObjectIdentifier extends ASN1Primitive {
|
||
|
private static final long LONG_LIMIT = 72057594037927808L;
|
||
|
private static final Map pool = new HashMap();
|
||
|
private byte[] body;
|
||
|
private final String identifier;
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // org.bouncycastle.asn1.ASN1Primitive
|
||
|
public boolean isConstructed() {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public String toString() {
|
||
|
return getId();
|
||
|
}
|
||
|
|
||
|
public boolean on(ASN1ObjectIdentifier aSN1ObjectIdentifier) {
|
||
|
String id = getId();
|
||
|
String id2 = aSN1ObjectIdentifier.getId();
|
||
|
return id.length() > id2.length() && id.charAt(id2.length()) == '.' && id.startsWith(id2);
|
||
|
}
|
||
|
|
||
|
public ASN1ObjectIdentifier intern() {
|
||
|
Map map = pool;
|
||
|
synchronized (map) {
|
||
|
OidHandle oidHandle = new OidHandle(getBody());
|
||
|
ASN1ObjectIdentifier aSN1ObjectIdentifier = (ASN1ObjectIdentifier) map.get(oidHandle);
|
||
|
if (aSN1ObjectIdentifier != null) {
|
||
|
return aSN1ObjectIdentifier;
|
||
|
}
|
||
|
map.put(oidHandle, this);
|
||
|
return this;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.asn1.ASN1Primitive, org.bouncycastle.asn1.ASN1Object
|
||
|
public int hashCode() {
|
||
|
return this.identifier.hashCode();
|
||
|
}
|
||
|
|
||
|
public String getId() {
|
||
|
return this.identifier;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // org.bouncycastle.asn1.ASN1Primitive
|
||
|
public int encodedLength() throws IOException {
|
||
|
int length = getBody().length;
|
||
|
return StreamUtil.calculateBodyLength(length) + 1 + length;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // org.bouncycastle.asn1.ASN1Primitive
|
||
|
public void encode(ASN1OutputStream aSN1OutputStream) throws IOException {
|
||
|
byte[] body = getBody();
|
||
|
aSN1OutputStream.write(6);
|
||
|
aSN1OutputStream.writeLength(body.length);
|
||
|
aSN1OutputStream.write(body);
|
||
|
}
|
||
|
|
||
|
public ASN1ObjectIdentifier branch(String str) {
|
||
|
return new ASN1ObjectIdentifier(this, str);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.asn1.ASN1Primitive
|
||
|
boolean asn1Equals(ASN1Primitive aSN1Primitive) {
|
||
|
if (aSN1Primitive == this) {
|
||
|
return true;
|
||
|
}
|
||
|
if (aSN1Primitive instanceof ASN1ObjectIdentifier) {
|
||
|
return this.identifier.equals(((ASN1ObjectIdentifier) aSN1Primitive).identifier);
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
private void writeField(ByteArrayOutputStream byteArrayOutputStream, BigInteger bigInteger) {
|
||
|
int bitLength = (bigInteger.bitLength() + 6) / 7;
|
||
|
if (bitLength == 0) {
|
||
|
byteArrayOutputStream.write(0);
|
||
|
return;
|
||
|
}
|
||
|
byte[] bArr = new byte[bitLength];
|
||
|
int i = bitLength - 1;
|
||
|
for (int i2 = i; i2 >= 0; i2--) {
|
||
|
bArr[i2] = (byte) ((bigInteger.intValue() & 127) | 128);
|
||
|
bigInteger = bigInteger.shiftRight(7);
|
||
|
}
|
||
|
bArr[i] = (byte) (bArr[i] & Ascii.DEL);
|
||
|
byteArrayOutputStream.write(bArr, 0, bitLength);
|
||
|
}
|
||
|
|
||
|
private void writeField(ByteArrayOutputStream byteArrayOutputStream, long j) {
|
||
|
byte[] bArr = new byte[9];
|
||
|
int i = 8;
|
||
|
bArr[8] = (byte) (((int) j) & 127);
|
||
|
while (j >= 128) {
|
||
|
j >>= 7;
|
||
|
i--;
|
||
|
bArr[i] = (byte) ((((int) j) & 127) | 128);
|
||
|
}
|
||
|
byteArrayOutputStream.write(bArr, i, 9 - i);
|
||
|
}
|
||
|
|
||
|
private static boolean isValidIdentifier(String str) {
|
||
|
char charAt;
|
||
|
if (str.length() < 3 || str.charAt(1) != '.' || (charAt = str.charAt(0)) < '0' || charAt > '2') {
|
||
|
return false;
|
||
|
}
|
||
|
return isValidBranchID(str, 2);
|
||
|
}
|
||
|
|
||
|
/* JADX WARN: Code restructure failed: missing block: B:12:0x001a, code lost:
|
||
|
|
||
|
if (r3 != '.') goto L17;
|
||
|
*/
|
||
|
/*
|
||
|
Code decompiled incorrectly, please refer to instructions dump.
|
||
|
To view partially-correct add '--show-bad-code' argument
|
||
|
*/
|
||
|
private static boolean isValidBranchID(java.lang.String r5, int r6) {
|
||
|
/*
|
||
|
int r0 = r5.length()
|
||
|
L4:
|
||
|
r1 = 0
|
||
|
r2 = r1
|
||
|
L6:
|
||
|
int r0 = r0 + (-1)
|
||
|
if (r0 < r6) goto L1f
|
||
|
char r3 = r5.charAt(r0)
|
||
|
r4 = 48
|
||
|
if (r4 > r3) goto L18
|
||
|
r4 = 57
|
||
|
if (r3 > r4) goto L18
|
||
|
r2 = 1
|
||
|
goto L6
|
||
|
L18:
|
||
|
r4 = 46
|
||
|
if (r3 != r4) goto L1e
|
||
|
if (r2 != 0) goto L4
|
||
|
L1e:
|
||
|
return r1
|
||
|
L1f:
|
||
|
return r2
|
||
|
*/
|
||
|
throw new UnsupportedOperationException("Method not decompiled: org.bouncycastle.asn1.ASN1ObjectIdentifier.isValidBranchID(java.lang.String, int):boolean");
|
||
|
}
|
||
|
|
||
|
public static ASN1ObjectIdentifier getInstance(ASN1TaggedObject aSN1TaggedObject, boolean z) {
|
||
|
ASN1Primitive object = aSN1TaggedObject.getObject();
|
||
|
return (z || (object instanceof ASN1ObjectIdentifier)) ? getInstance(object) : fromOctetString(ASN1OctetString.getInstance(aSN1TaggedObject.getObject()).getOctets());
|
||
|
}
|
||
|
|
||
|
public static ASN1ObjectIdentifier getInstance(Object obj) {
|
||
|
if (obj == null || (obj instanceof ASN1ObjectIdentifier)) {
|
||
|
return (ASN1ObjectIdentifier) obj;
|
||
|
}
|
||
|
if (obj instanceof ASN1Encodable) {
|
||
|
ASN1Encodable aSN1Encodable = (ASN1Encodable) obj;
|
||
|
if (aSN1Encodable.toASN1Primitive() instanceof ASN1ObjectIdentifier) {
|
||
|
return (ASN1ObjectIdentifier) aSN1Encodable.toASN1Primitive();
|
||
|
}
|
||
|
}
|
||
|
if (!(obj instanceof byte[])) {
|
||
|
StringBuilder sb = new StringBuilder("illegal object in getInstance: ");
|
||
|
sb.append(obj.getClass().getName());
|
||
|
throw new IllegalArgumentException(sb.toString());
|
||
|
}
|
||
|
try {
|
||
|
return (ASN1ObjectIdentifier) fromByteArray((byte[]) obj);
|
||
|
} catch (IOException e) {
|
||
|
StringBuilder sb2 = new StringBuilder("failed to construct object identifier from byte[]: ");
|
||
|
sb2.append(e.getMessage());
|
||
|
throw new IllegalArgumentException(sb2.toString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private byte[] getBody() {
|
||
|
byte[] bArr;
|
||
|
synchronized (this) {
|
||
|
if (this.body == null) {
|
||
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||
|
doOutput(byteArrayOutputStream);
|
||
|
this.body = byteArrayOutputStream.toByteArray();
|
||
|
}
|
||
|
bArr = this.body;
|
||
|
}
|
||
|
return bArr;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public static ASN1ObjectIdentifier fromOctetString(byte[] bArr) {
|
||
|
ASN1ObjectIdentifier aSN1ObjectIdentifier;
|
||
|
OidHandle oidHandle = new OidHandle(bArr);
|
||
|
Map map = pool;
|
||
|
synchronized (map) {
|
||
|
aSN1ObjectIdentifier = (ASN1ObjectIdentifier) map.get(oidHandle);
|
||
|
}
|
||
|
return aSN1ObjectIdentifier != null ? aSN1ObjectIdentifier : new ASN1ObjectIdentifier(bArr);
|
||
|
}
|
||
|
|
||
|
private void doOutput(ByteArrayOutputStream byteArrayOutputStream) {
|
||
|
OIDTokenizer oIDTokenizer = new OIDTokenizer(this.identifier);
|
||
|
int parseInt = Integer.parseInt(oIDTokenizer.nextToken()) * 40;
|
||
|
String nextToken = oIDTokenizer.nextToken();
|
||
|
if (nextToken.length() <= 18) {
|
||
|
writeField(byteArrayOutputStream, parseInt + Long.parseLong(nextToken));
|
||
|
} else {
|
||
|
writeField(byteArrayOutputStream, new BigInteger(nextToken).add(BigInteger.valueOf(parseInt)));
|
||
|
}
|
||
|
while (oIDTokenizer.hasMoreTokens()) {
|
||
|
String nextToken2 = oIDTokenizer.nextToken();
|
||
|
if (nextToken2.length() <= 18) {
|
||
|
writeField(byteArrayOutputStream, Long.parseLong(nextToken2));
|
||
|
} else {
|
||
|
writeField(byteArrayOutputStream, new BigInteger(nextToken2));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* loaded from: classes6.dex */
|
||
|
public static class OidHandle {
|
||
|
private final byte[] enc;
|
||
|
private int key;
|
||
|
|
||
|
public int hashCode() {
|
||
|
return this.key;
|
||
|
}
|
||
|
|
||
|
public boolean equals(Object obj) {
|
||
|
if (obj instanceof OidHandle) {
|
||
|
return Arrays.areEqual(this.enc, ((OidHandle) obj).enc);
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
OidHandle(byte[] bArr) {
|
||
|
this.key = Arrays.hashCode(bArr);
|
||
|
this.enc = bArr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public ASN1ObjectIdentifier(byte[] bArr) {
|
||
|
StringBuffer stringBuffer = new StringBuffer();
|
||
|
boolean z = true;
|
||
|
long j = 0;
|
||
|
BigInteger bigInteger = null;
|
||
|
for (int i = 0; i != bArr.length; i++) {
|
||
|
byte b = bArr[i];
|
||
|
if (j <= LONG_LIMIT) {
|
||
|
long j2 = j + (b & Ascii.DEL);
|
||
|
if ((b & 128) == 0) {
|
||
|
if (z) {
|
||
|
if (j2 < 40) {
|
||
|
stringBuffer.append('0');
|
||
|
} else if (j2 < 80) {
|
||
|
stringBuffer.append('1');
|
||
|
j2 -= 40;
|
||
|
} else {
|
||
|
stringBuffer.append('2');
|
||
|
j2 -= 80;
|
||
|
}
|
||
|
z = false;
|
||
|
}
|
||
|
stringBuffer.append('.');
|
||
|
stringBuffer.append(j2);
|
||
|
j = 0;
|
||
|
} else {
|
||
|
j = j2 << 7;
|
||
|
}
|
||
|
} else {
|
||
|
BigInteger or = (bigInteger == null ? BigInteger.valueOf(j) : bigInteger).or(BigInteger.valueOf(b & Ascii.DEL));
|
||
|
if ((b & 128) == 0) {
|
||
|
if (z) {
|
||
|
stringBuffer.append('2');
|
||
|
or = or.subtract(BigInteger.valueOf(80L));
|
||
|
z = false;
|
||
|
}
|
||
|
stringBuffer.append('.');
|
||
|
stringBuffer.append(or);
|
||
|
bigInteger = null;
|
||
|
j = 0;
|
||
|
} else {
|
||
|
bigInteger = or.shiftLeft(7);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
this.identifier = stringBuffer.toString();
|
||
|
this.body = Arrays.clone(bArr);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public ASN1ObjectIdentifier(ASN1ObjectIdentifier aSN1ObjectIdentifier, String str) {
|
||
|
if (!isValidBranchID(str, 0)) {
|
||
|
StringBuilder sb = new StringBuilder("string ");
|
||
|
sb.append(str);
|
||
|
sb.append(" not a valid OID branch");
|
||
|
throw new IllegalArgumentException(sb.toString());
|
||
|
}
|
||
|
StringBuilder sb2 = new StringBuilder();
|
||
|
sb2.append(aSN1ObjectIdentifier.getId());
|
||
|
sb2.append(".");
|
||
|
sb2.append(str);
|
||
|
this.identifier = sb2.toString();
|
||
|
}
|
||
|
|
||
|
public ASN1ObjectIdentifier(String str) {
|
||
|
if (str == null) {
|
||
|
throw new IllegalArgumentException("'identifier' cannot be null");
|
||
|
}
|
||
|
if (isValidIdentifier(str)) {
|
||
|
this.identifier = str;
|
||
|
return;
|
||
|
}
|
||
|
StringBuilder sb = new StringBuilder("string ");
|
||
|
sb.append(str);
|
||
|
sb.append(" not an OID");
|
||
|
throw new IllegalArgumentException(sb.toString());
|
||
|
}
|
||
|
}
|