what-the-bank/sources/org/bouncycastle/asn1/x500/style/IETFUtils.java

400 lines
16 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.asn1.x500.style;
import com.google.common.primitives.UnsignedBytes;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1String;
import org.bouncycastle.asn1.DERUniversalString;
import org.bouncycastle.asn1.x500.AttributeTypeAndValue;
import org.bouncycastle.asn1.x500.RDN;
import org.bouncycastle.asn1.x500.X500NameBuilder;
import org.bouncycastle.asn1.x500.X500NameStyle;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;
/* loaded from: classes6.dex */
public class IETFUtils {
private static int convertHex(char c) {
return ('0' > c || c > '9') ? ('a' > c || c > 'f') ? c - '7' : c - 'W' : c - '0';
}
private static boolean isHexDigit(char c) {
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
}
public static String valueToString(ASN1Encodable aSN1Encodable) {
StringBuffer stringBuffer = new StringBuffer();
if (!(aSN1Encodable instanceof ASN1String) || (aSN1Encodable instanceof DERUniversalString)) {
try {
StringBuilder sb = new StringBuilder("#");
sb.append(bytesToString(Hex.encode(aSN1Encodable.toASN1Primitive().getEncoded(ASN1Encoding.DER))));
stringBuffer.append(sb.toString());
} catch (IOException unused) {
throw new IllegalArgumentException("Other value has no encoded form");
}
} else {
String string = ((ASN1String) aSN1Encodable).getString();
if (string.length() > 0 && string.charAt(0) == '#') {
string = "\\".concat(String.valueOf(string));
}
stringBuffer.append(string);
}
int length = stringBuffer.length();
int i = (stringBuffer.length() >= 2 && stringBuffer.charAt(0) == '\\' && stringBuffer.charAt(1) == '#') ? 2 : 0;
while (i != length) {
if (stringBuffer.charAt(i) == ',' || stringBuffer.charAt(i) == '\"' || stringBuffer.charAt(i) == '\\' || stringBuffer.charAt(i) == '+' || stringBuffer.charAt(i) == '=' || stringBuffer.charAt(i) == '<' || stringBuffer.charAt(i) == '>' || stringBuffer.charAt(i) == ';') {
stringBuffer.insert(i, "\\");
i++;
length++;
}
i++;
}
if (stringBuffer.length() > 0) {
for (int i2 = 0; stringBuffer.length() > i2 && stringBuffer.charAt(i2) == ' '; i2 += 2) {
stringBuffer.insert(i2, "\\");
}
}
for (int length2 = stringBuffer.length() - 1; length2 >= 0 && stringBuffer.charAt(length2) == ' '; length2--) {
stringBuffer.insert(length2, '\\');
}
return stringBuffer.toString();
}
public static ASN1Encodable valueFromHexString(String str, int i) throws IOException {
int length = (str.length() - i) / 2;
byte[] bArr = new byte[length];
for (int i2 = 0; i2 != length; i2++) {
int i3 = (i2 << 1) + i;
char charAt = str.charAt(i3);
bArr[i2] = (byte) (convertHex(str.charAt(i3 + 1)) | (convertHex(charAt) << 4));
}
return ASN1Primitive.fromByteArray(bArr);
}
private static String unescape(String str) {
int i;
if (str.length() == 0 || (str.indexOf(92) < 0 && str.indexOf(34) < 0)) {
return str.trim();
}
char[] charArray = str.toCharArray();
StringBuffer stringBuffer = new StringBuffer(str.length());
if (charArray[0] == '\\' && charArray[1] == '#') {
stringBuffer.append("\\#");
i = 2;
} else {
i = 0;
}
boolean z = false;
int i2 = 0;
boolean z2 = false;
char c = 0;
boolean z3 = false;
while (i != charArray.length) {
char c2 = charArray[i];
if (c2 != ' ') {
z3 = true;
}
if (c2 != '\"') {
if (c2 == '\\' && !z && !z2) {
i2 = stringBuffer.length();
z = true;
} else if (c2 != ' ' || z || z3) {
if (z && isHexDigit(c2)) {
if (c != 0) {
stringBuffer.append((char) ((convertHex(c) << 4) + convertHex(c2)));
z = false;
c = 0;
} else {
c = c2;
}
}
}
i++;
} else if (!z) {
z2 = !z2;
z = false;
i++;
}
stringBuffer.append(c2);
z = false;
i++;
}
if (stringBuffer.length() > 0) {
while (stringBuffer.charAt(stringBuffer.length() - 1) == ' ' && i2 != stringBuffer.length() - 1) {
stringBuffer.setLength(stringBuffer.length() - 1);
}
}
return stringBuffer.toString();
}
private static String[] toValueArray(Vector vector) {
int size = vector.size();
String[] strArr = new String[size];
for (int i = 0; i != size; i++) {
strArr[i] = (String) vector.elementAt(i);
}
return strArr;
}
private static ASN1ObjectIdentifier[] toOIDArray(Vector vector) {
int size = vector.size();
ASN1ObjectIdentifier[] aSN1ObjectIdentifierArr = new ASN1ObjectIdentifier[size];
for (int i = 0; i != size; i++) {
aSN1ObjectIdentifierArr[i] = (ASN1ObjectIdentifier) vector.elementAt(i);
}
return aSN1ObjectIdentifierArr;
}
public static String stripInternalSpaces(String str) {
StringBuffer stringBuffer = new StringBuffer();
if (str.length() != 0) {
char charAt = str.charAt(0);
stringBuffer.append(charAt);
int i = 1;
while (i < str.length()) {
char charAt2 = str.charAt(i);
if (charAt != ' ' || charAt2 != ' ') {
stringBuffer.append(charAt2);
}
i++;
charAt = charAt2;
}
}
return stringBuffer.toString();
}
public static RDN[] rDNsFromString(String str, X500NameStyle x500NameStyle) {
X500NameTokenizer x500NameTokenizer = new X500NameTokenizer(str);
X500NameBuilder x500NameBuilder = new X500NameBuilder(x500NameStyle);
while (x500NameTokenizer.hasMoreTokens()) {
String nextToken = x500NameTokenizer.nextToken();
if (nextToken.indexOf(43) > 0) {
X500NameTokenizer x500NameTokenizer2 = new X500NameTokenizer(nextToken, '+');
X500NameTokenizer x500NameTokenizer3 = new X500NameTokenizer(x500NameTokenizer2.nextToken(), '=');
String nextToken2 = x500NameTokenizer3.nextToken();
if (!x500NameTokenizer3.hasMoreTokens()) {
throw new IllegalArgumentException("badly formatted directory string");
}
String nextToken3 = x500NameTokenizer3.nextToken();
ASN1ObjectIdentifier attrNameToOID = x500NameStyle.attrNameToOID(nextToken2.trim());
if (x500NameTokenizer2.hasMoreTokens()) {
Vector vector = new Vector();
Vector vector2 = new Vector();
while (true) {
vector.addElement(attrNameToOID);
vector2.addElement(unescape(nextToken3));
if (!x500NameTokenizer2.hasMoreTokens()) {
x500NameBuilder.addMultiValuedRDN(toOIDArray(vector), toValueArray(vector2));
break;
}
X500NameTokenizer x500NameTokenizer4 = new X500NameTokenizer(x500NameTokenizer2.nextToken(), '=');
String nextToken4 = x500NameTokenizer4.nextToken();
if (!x500NameTokenizer4.hasMoreTokens()) {
throw new IllegalArgumentException("badly formatted directory string");
}
nextToken3 = x500NameTokenizer4.nextToken();
attrNameToOID = x500NameStyle.attrNameToOID(nextToken4.trim());
}
} else {
x500NameBuilder.addRDN(attrNameToOID, unescape(nextToken3));
}
} else {
X500NameTokenizer x500NameTokenizer5 = new X500NameTokenizer(nextToken, '=');
String nextToken5 = x500NameTokenizer5.nextToken();
if (!x500NameTokenizer5.hasMoreTokens()) {
throw new IllegalArgumentException("badly formatted directory string");
}
x500NameBuilder.addRDN(x500NameStyle.attrNameToOID(nextToken5.trim()), unescape(x500NameTokenizer5.nextToken()));
}
}
return x500NameBuilder.build().getRDNs();
}
public static boolean rDNAreEqual(RDN rdn, RDN rdn2) {
if (!rdn.isMultiValued()) {
if (rdn2.isMultiValued()) {
return false;
}
return atvAreEqual(rdn.getFirst(), rdn2.getFirst());
}
if (!rdn2.isMultiValued()) {
return false;
}
AttributeTypeAndValue[] typesAndValues = rdn.getTypesAndValues();
AttributeTypeAndValue[] typesAndValues2 = rdn2.getTypesAndValues();
if (typesAndValues.length != typesAndValues2.length) {
return false;
}
for (int i = 0; i != typesAndValues.length; i++) {
if (!atvAreEqual(typesAndValues[i], typesAndValues2[i])) {
return false;
}
}
return true;
}
public static String[] findAttrNamesForOID(ASN1ObjectIdentifier aSN1ObjectIdentifier, Hashtable hashtable) {
Enumeration elements = hashtable.elements();
int i = 0;
int i2 = 0;
while (elements.hasMoreElements()) {
if (aSN1ObjectIdentifier.equals(elements.nextElement())) {
i2++;
}
}
String[] strArr = new String[i2];
Enumeration keys = hashtable.keys();
while (keys.hasMoreElements()) {
String str = (String) keys.nextElement();
if (aSN1ObjectIdentifier.equals(hashtable.get(str))) {
strArr[i] = str;
i++;
}
}
return strArr;
}
private static ASN1Primitive decodeObject(String str) {
try {
return ASN1Primitive.fromByteArray(Hex.decode(str.substring(1)));
} catch (IOException e) {
throw new IllegalStateException("unknown encoding in name: ".concat(String.valueOf(e)));
}
}
public static ASN1ObjectIdentifier decodeAttrName(String str, Hashtable hashtable) {
if (Strings.toUpperCase(str).startsWith("OID.")) {
return new ASN1ObjectIdentifier(str.substring(4));
}
if (str.charAt(0) >= '0' && str.charAt(0) <= '9') {
return new ASN1ObjectIdentifier(str);
}
ASN1ObjectIdentifier aSN1ObjectIdentifier = (ASN1ObjectIdentifier) hashtable.get(Strings.toLowerCase(str));
if (aSN1ObjectIdentifier != null) {
return aSN1ObjectIdentifier;
}
StringBuilder sb = new StringBuilder("Unknown object id - ");
sb.append(str);
sb.append(" - passed to distinguished name");
throw new IllegalArgumentException(sb.toString());
}
/* JADX WARN: Code restructure failed: missing block: B:29:0x0066, code lost:
if (r0 >= (r6.length() - 1)) goto L30;
*/
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct add '--show-bad-code' argument
*/
public static java.lang.String canonicalize(java.lang.String r6) {
/*
java.lang.String r6 = org.bouncycastle.util.Strings.toLowerCase(r6)
int r0 = r6.length()
r1 = 0
if (r0 <= 0) goto L25
char r0 = r6.charAt(r1)
r2 = 35
if (r0 != r2) goto L25
org.bouncycastle.asn1.ASN1Primitive r0 = decodeObject(r6)
boolean r2 = r0 instanceof org.bouncycastle.asn1.ASN1String
if (r2 == 0) goto L25
org.bouncycastle.asn1.ASN1String r0 = (org.bouncycastle.asn1.ASN1String) r0
java.lang.String r6 = r0.getString()
java.lang.String r6 = org.bouncycastle.util.Strings.toLowerCase(r6)
L25:
int r0 = r6.length()
r2 = 1
if (r0 <= r2) goto L6d
L2c:
int r0 = r1 + 1
int r3 = r6.length()
r4 = 32
r5 = 92
if (r0 >= r3) goto L47
char r3 = r6.charAt(r1)
if (r3 != r5) goto L47
char r0 = r6.charAt(r0)
if (r0 != r4) goto L47
int r1 = r1 + 2
goto L2c
L47:
int r0 = r6.length()
int r0 = r0 - r2
L4c:
int r3 = r0 + (-1)
if (r3 <= 0) goto L5f
char r3 = r6.charAt(r3)
if (r3 != r5) goto L5f
char r3 = r6.charAt(r0)
if (r3 != r4) goto L5f
int r0 = r0 + (-2)
goto L4c
L5f:
if (r1 > 0) goto L68
int r3 = r6.length()
int r3 = r3 - r2
if (r0 >= r3) goto L6d
L68:
int r0 = r0 + r2
java.lang.String r6 = r6.substring(r1, r0)
L6d:
java.lang.String r6 = stripInternalSpaces(r6)
return r6
*/
throw new UnsupportedOperationException("Method not decompiled: org.bouncycastle.asn1.x500.style.IETFUtils.canonicalize(java.lang.String):java.lang.String");
}
private static String bytesToString(byte[] bArr) {
int length = bArr.length;
char[] cArr = new char[length];
for (int i = 0; i != length; i++) {
cArr[i] = (char) (bArr[i] & UnsignedBytes.MAX_VALUE);
}
return new String(cArr);
}
private static boolean atvAreEqual(AttributeTypeAndValue attributeTypeAndValue, AttributeTypeAndValue attributeTypeAndValue2) {
if (attributeTypeAndValue == attributeTypeAndValue2) {
return true;
}
return attributeTypeAndValue != null && attributeTypeAndValue2 != null && attributeTypeAndValue.getType().equals(attributeTypeAndValue2.getType()) && canonicalize(valueToString(attributeTypeAndValue.getValue())).equals(canonicalize(valueToString(attributeTypeAndValue2.getValue())));
}
public static void appendTypeAndValue(StringBuffer stringBuffer, AttributeTypeAndValue attributeTypeAndValue, Hashtable hashtable) {
String str = (String) hashtable.get(attributeTypeAndValue.getType());
if (str == null) {
str = attributeTypeAndValue.getType().getId();
}
stringBuffer.append(str);
stringBuffer.append('=');
stringBuffer.append(valueToString(attributeTypeAndValue.getValue()));
}
public static void appendRDN(StringBuffer stringBuffer, RDN rdn, Hashtable hashtable) {
if (!rdn.isMultiValued()) {
if (rdn.getFirst() != null) {
appendTypeAndValue(stringBuffer, rdn.getFirst(), hashtable);
return;
}
return;
}
AttributeTypeAndValue[] typesAndValues = rdn.getTypesAndValues();
boolean z = true;
for (int i = 0; i != typesAndValues.length; i++) {
if (z) {
z = false;
} else {
stringBuffer.append('+');
}
appendTypeAndValue(stringBuffer, typesAndValues[i], hashtable);
}
}
}