package org.bouncycastle.asn1.x509; import java.io.IOException; import java.util.StringTokenizer; import org.bouncycastle.asn1.ASN1Choice; import org.bouncycastle.asn1.ASN1Encodable; import org.bouncycastle.asn1.ASN1Object; import org.bouncycastle.asn1.ASN1ObjectIdentifier; import org.bouncycastle.asn1.ASN1OctetString; import org.bouncycastle.asn1.ASN1Primitive; import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1.ASN1TaggedObject; import org.bouncycastle.asn1.DERIA5String; import org.bouncycastle.asn1.DEROctetString; import org.bouncycastle.asn1.DERTaggedObject; import org.bouncycastle.asn1.x500.X500Name; import org.bouncycastle.util.IPAddress; /* loaded from: classes6.dex */ public class GeneralName extends ASN1Object implements ASN1Choice { public static final int dNSName = 2; public static final int directoryName = 4; public static final int ediPartyName = 5; public static final int iPAddress = 7; public static final int otherName = 0; public static final int registeredID = 8; public static final int rfc822Name = 1; public static final int uniformResourceIdentifier = 6; public static final int x400Address = 3; private ASN1Encodable obj; private int tag; public String toString() { String string; Object x500Name; StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(this.tag); stringBuffer.append(": "); int i = this.tag; if (i != 1 && i != 2) { if (i == 4) { x500Name = X500Name.getInstance(this.obj); } else if (i != 6) { x500Name = this.obj; } string = x500Name.toString(); stringBuffer.append(string); return stringBuffer.toString(); } string = DERIA5String.getInstance(this.obj).getString(); stringBuffer.append(string); return stringBuffer.toString(); } @Override // org.bouncycastle.asn1.ASN1Object, org.bouncycastle.asn1.ASN1Encodable public ASN1Primitive toASN1Primitive() { return this.tag == 4 ? new DERTaggedObject(true, this.tag, this.obj) : new DERTaggedObject(false, this.tag, this.obj); } public int getTagNo() { return this.tag; } public ASN1Encodable getName() { return this.obj; } private byte[] toGeneralNameEncoding(String str) { if (IPAddress.isValidIPv6WithNetmask(str) || IPAddress.isValidIPv6(str)) { int indexOf = str.indexOf(47); if (indexOf < 0) { byte[] bArr = new byte[16]; copyInts(parseIPv6(str), bArr, 0); return bArr; } byte[] bArr2 = new byte[32]; copyInts(parseIPv6(str.substring(0, indexOf)), bArr2, 0); String substring = str.substring(indexOf + 1); copyInts(substring.indexOf(58) > 0 ? parseIPv6(substring) : parseMask(substring), bArr2, 16); return bArr2; } if (!IPAddress.isValidIPv4WithNetmask(str) && !IPAddress.isValidIPv4(str)) { return null; } int indexOf2 = str.indexOf(47); if (indexOf2 < 0) { byte[] bArr3 = new byte[4]; parseIPv4(str, bArr3, 0); return bArr3; } byte[] bArr4 = new byte[8]; parseIPv4(str.substring(0, indexOf2), bArr4, 0); String substring2 = str.substring(indexOf2 + 1); if (substring2.indexOf(46) > 0) { parseIPv4(substring2, bArr4, 4); } else { parseIPv4Mask(substring2, bArr4, 4); } return bArr4; } private int[] parseMask(String str) { int[] iArr = new int[8]; int parseInt = Integer.parseInt(str); for (int i = 0; i != parseInt; i++) { int i2 = i / 16; iArr[i2] = iArr[i2] | (1 << (15 - (i % 16))); } return iArr; } private int[] parseIPv6(String str) { StringTokenizer stringTokenizer = new StringTokenizer(str, ":", true); int[] iArr = new int[8]; if (str.charAt(0) == ':' && str.charAt(1) == ':') { stringTokenizer.nextToken(); } int i = -1; int i2 = 0; while (stringTokenizer.hasMoreTokens()) { String nextToken = stringTokenizer.nextToken(); if (nextToken.equals(":")) { iArr[i2] = 0; i = i2; i2++; } else if (nextToken.indexOf(46) < 0) { iArr[i2] = Integer.parseInt(nextToken, 16); if (stringTokenizer.hasMoreTokens()) { stringTokenizer.nextToken(); } i2++; } else { StringTokenizer stringTokenizer2 = new StringTokenizer(nextToken, "."); iArr[i2] = (Integer.parseInt(stringTokenizer2.nextToken()) << 8) | Integer.parseInt(stringTokenizer2.nextToken()); iArr[i2 + 1] = Integer.parseInt(stringTokenizer2.nextToken()) | (Integer.parseInt(stringTokenizer2.nextToken()) << 8); i2 += 2; } } if (i2 != 8) { int i3 = i2 - i; int i4 = 8 - i3; System.arraycopy(iArr, i, iArr, i4, i3); while (i != i4) { iArr[i] = 0; i++; } } return iArr; } private void parseIPv4Mask(String str, byte[] bArr, int i) { int parseInt = Integer.parseInt(str); for (int i2 = 0; i2 != parseInt; i2++) { int i3 = (i2 / 8) + i; bArr[i3] = (byte) (bArr[i3] | (1 << (7 - (i2 % 8)))); } } private void parseIPv4(String str, byte[] bArr, int i) { StringTokenizer stringTokenizer = new StringTokenizer(str, "./"); int i2 = 0; while (stringTokenizer.hasMoreTokens()) { bArr[i2 + i] = (byte) Integer.parseInt(stringTokenizer.nextToken()); i2++; } } public static GeneralName getInstance(ASN1TaggedObject aSN1TaggedObject, boolean z) { return getInstance(ASN1TaggedObject.getInstance(aSN1TaggedObject, true)); } public static GeneralName getInstance(Object obj) { if (obj == null || (obj instanceof GeneralName)) { return (GeneralName) obj; } if (obj instanceof ASN1TaggedObject) { ASN1TaggedObject aSN1TaggedObject = (ASN1TaggedObject) obj; int tagNo = aSN1TaggedObject.getTagNo(); switch (tagNo) { case 0: return new GeneralName(tagNo, ASN1Sequence.getInstance(aSN1TaggedObject, false)); case 1: return new GeneralName(tagNo, DERIA5String.getInstance(aSN1TaggedObject, false)); case 2: return new GeneralName(tagNo, DERIA5String.getInstance(aSN1TaggedObject, false)); case 3: throw new IllegalArgumentException("unknown tag: ".concat(String.valueOf(tagNo))); case 4: return new GeneralName(tagNo, X500Name.getInstance(aSN1TaggedObject, true)); case 5: return new GeneralName(tagNo, ASN1Sequence.getInstance(aSN1TaggedObject, false)); case 6: return new GeneralName(tagNo, DERIA5String.getInstance(aSN1TaggedObject, false)); case 7: return new GeneralName(tagNo, ASN1OctetString.getInstance(aSN1TaggedObject, false)); case 8: return new GeneralName(tagNo, ASN1ObjectIdentifier.getInstance(aSN1TaggedObject, false)); } } if (obj instanceof byte[]) { try { return getInstance(ASN1Primitive.fromByteArray((byte[]) obj)); } catch (IOException unused) { throw new IllegalArgumentException("unable to parse encoded general name"); } } StringBuilder sb = new StringBuilder("unknown object in getInstance: "); sb.append(obj.getClass().getName()); throw new IllegalArgumentException(sb.toString()); } private void copyInts(int[] iArr, byte[] bArr, int i) { for (int i2 = 0; i2 != iArr.length; i2++) { int i3 = i2 << 1; int i4 = iArr[i2]; bArr[i3 + i] = (byte) (i4 >> 8); bArr[i3 + 1 + i] = (byte) i4; } } public GeneralName(X509Name x509Name) { this.obj = X500Name.getInstance(x509Name); this.tag = 4; } public GeneralName(X500Name x500Name) { this.obj = x500Name; this.tag = 4; } public GeneralName(int i, ASN1Encodable aSN1Encodable) { this.obj = aSN1Encodable; this.tag = i; } public GeneralName(int i, String str) { ASN1Encodable dERIA5String; this.tag = i; if (i == 1 || i == 2 || i == 6) { dERIA5String = new DERIA5String(str); } else if (i == 8) { dERIA5String = new ASN1ObjectIdentifier(str); } else { if (i != 4) { if (i != 7) { throw new IllegalArgumentException("can't process String for tag: ".concat(String.valueOf(i))); } byte[] generalNameEncoding = toGeneralNameEncoding(str); if (generalNameEncoding == null) { throw new IllegalArgumentException("IP Address is invalid"); } this.obj = new DEROctetString(generalNameEncoding); return; } dERIA5String = new X500Name(str); } this.obj = dERIA5String; } }