package o; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.security.NoSuchProviderException; import java.security.cert.CertPath; import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import javax.security.auth.x500.X500Principal; import org.bouncycastle.asn1.ASN1Encoding; import org.spongycastle.asn1.ASN1Integer; /* renamed from: o.gqG, reason: case insensitive filesystem */ /* loaded from: classes6.dex */ public final class C15427gqG extends CertPath { static final List c; private List d; private final gqQ e; static { ArrayList arrayList = new ArrayList(); arrayList.add("PkiPath"); arrayList.add("PEM"); arrayList.add("PKCS7"); c = Collections.unmodifiableList(arrayList); } private static List b(List list) { int i; if (list.size() < 2) { return list; } X500Principal issuerX500Principal = ((X509Certificate) list.get(0)).getIssuerX500Principal(); for (int i2 = 1; i2 != list.size(); i2++) { if (issuerX500Principal.equals(((X509Certificate) list.get(i2)).getSubjectX500Principal())) { issuerX500Principal = ((X509Certificate) list.get(i2)).getIssuerX500Principal(); } else { ArrayList arrayList = new ArrayList(list.size()); ArrayList arrayList2 = new ArrayList(list); for (int i3 = 0; i3 < list.size(); i3++) { X509Certificate x509Certificate = (X509Certificate) list.get(i3); X500Principal subjectX500Principal = x509Certificate.getSubjectX500Principal(); while (true) { if (i != list.size()) { i = ((X509Certificate) list.get(i)).getIssuerX500Principal().equals(subjectX500Principal) ? 0 : i + 1; } else { arrayList.add(x509Certificate); list.remove(i3); break; } } } if (arrayList.size() > 1) { return arrayList2; } for (int i4 = 0; i4 != arrayList.size(); i4++) { X500Principal issuerX500Principal2 = ((X509Certificate) arrayList.get(i4)).getIssuerX500Principal(); int i5 = 0; while (true) { if (i5 < list.size()) { X509Certificate x509Certificate2 = (X509Certificate) list.get(i5); if (issuerX500Principal2.equals(x509Certificate2.getSubjectX500Principal())) { arrayList.add(x509Certificate2); list.remove(i5); break; } i5++; } } } return list.size() > 0 ? arrayList2 : arrayList; } } return list; } /* JADX INFO: Access modifiers changed from: package-private */ public C15427gqG(List list) { super("X.509"); this.e = new gqR(); this.d = b(new ArrayList(list)); } /* JADX INFO: Access modifiers changed from: package-private */ public C15427gqG(InputStream inputStream, String str) throws CertificateException { super("X.509"); gqR gqr = new gqR(); this.e = gqr; try { if (str.equalsIgnoreCase("PkiPath")) { AbstractC15315gnE b = new C15348gnx(inputStream).b(); if (!(b instanceof AbstractC15317gnH)) { throw new CertificateException("input stream does not contain a ASN1 SEQUENCE while reading PkiPath encoded data to load CertPath"); } Enumeration b2 = ((AbstractC15317gnH) b).b(); this.d = new ArrayList(); CertificateFactory c2 = gqr.c("X.509"); while (b2.hasMoreElements()) { this.d.add(0, c2.generateCertificate(new ByteArrayInputStream(((InterfaceC15342gnr) b2.nextElement()).i().c(ASN1Encoding.DER)))); } } else { if (!str.equalsIgnoreCase("PKCS7") && !str.equalsIgnoreCase("PEM")) { StringBuilder sb = new StringBuilder("unsupported encoding: "); sb.append(str); throw new CertificateException(sb.toString()); } BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); this.d = new ArrayList(); CertificateFactory c3 = gqr.c("X.509"); while (true) { Certificate generateCertificate = c3.generateCertificate(bufferedInputStream); if (generateCertificate == null) { break; } else { this.d.add(generateCertificate); } } } this.d = b(this.d); } catch (IOException e) { StringBuilder sb2 = new StringBuilder("IOException throw while decoding CertPath:\n"); sb2.append(e.toString()); throw new CertificateException(sb2.toString()); } catch (NoSuchProviderException e2) { StringBuilder sb3 = new StringBuilder("BouncyCastle provider not found while trying to get a CertificateFactory:\n"); sb3.append(e2.toString()); throw new CertificateException(sb3.toString()); } } @Override // java.security.cert.CertPath public final Iterator getEncodings() { return c.iterator(); } @Override // java.security.cert.CertPath public final byte[] getEncoded() throws CertificateEncodingException { Iterator encodings = getEncodings(); if (!encodings.hasNext()) { return null; } String next = encodings.next(); if (next instanceof String) { return getEncoded(next); } return null; } @Override // java.security.cert.CertPath public final byte[] getEncoded(String str) throws CertificateEncodingException { char[] cArr; int i; if (str.equalsIgnoreCase("PkiPath")) { C15343gns c15343gns = new C15343gns(); List list = this.d; ListIterator listIterator = list.listIterator(list.size()); while (listIterator.hasPrevious()) { c15343gns.a.addElement(c((X509Certificate) listIterator.previous())); } return d(new C15385goy(c15343gns)); } if (str.equalsIgnoreCase("PKCS7")) { C15402gpe c15402gpe = new C15402gpe(InterfaceC15405gph.b, null); C15343gns c15343gns2 = new C15343gns(); for (int i2 = 0; i2 != this.d.size(); i2++) { c15343gns2.a.addElement(c((X509Certificate) this.d.get(i2))); } return d(new C15402gpe(InterfaceC15405gph.j, new C15409gpl(new ASN1Integer(1L), new C15384gox(), c15402gpe, new C15384gox(c15343gns2), new C15384gox()))); } if (str.equalsIgnoreCase("PEM")) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); C15506gsb c15506gsb = new C15506gsb(new OutputStreamWriter(byteArrayOutputStream)); for (int i3 = 0; i3 != this.d.size(); i3++) { try { C15471grX b = new C15471grX("CERTIFICATE", ((X509Certificate) this.d.get(i3)).getEncoded()).b(); String str2 = b.c; StringBuilder sb = new StringBuilder("-----BEGIN "); sb.append(str2); sb.append("-----"); c15506gsb.write(sb.toString()); c15506gsb.newLine(); if (!b.b.isEmpty()) { for (C15470grW c15470grW : b.b) { c15506gsb.write(c15470grW.a); c15506gsb.write(": "); c15506gsb.write(c15470grW.b); c15506gsb.newLine(); } c15506gsb.newLine(); } byte[] d = C15469grO.d(b.a); for (int i4 = 0; i4 < d.length; i4 += c15506gsb.e.length) { int i5 = 0; while (true) { cArr = c15506gsb.e; if (i5 != cArr.length && (i = i4 + i5) < d.length) { cArr[i5] = (char) d[i]; i5++; } } c15506gsb.write(cArr, 0, i5); c15506gsb.newLine(); } String str3 = b.c; StringBuilder sb2 = new StringBuilder("-----END "); sb2.append(str3); sb2.append("-----"); c15506gsb.write(sb2.toString()); c15506gsb.newLine(); } catch (Exception unused) { throw new CertificateEncodingException("can't encode certificate for PEM encoded path"); } } c15506gsb.close(); return byteArrayOutputStream.toByteArray(); } throw new CertificateEncodingException("unsupported encoding: ".concat(String.valueOf(str))); } @Override // java.security.cert.CertPath public final List getCertificates() { return Collections.unmodifiableList(new ArrayList(this.d)); } private static AbstractC15315gnE c(X509Certificate x509Certificate) throws CertificateEncodingException { try { return new C15348gnx(x509Certificate.getEncoded()).b(); } catch (Exception e) { StringBuilder sb = new StringBuilder("Exception while encoding certificate: "); sb.append(e.toString()); throw new CertificateEncodingException(sb.toString()); } } private static byte[] d(InterfaceC15342gnr interfaceC15342gnr) throws CertificateEncodingException { try { return interfaceC15342gnr.i().c(ASN1Encoding.DER); } catch (IOException e) { throw new CertificateEncodingException("Exception thrown: ".concat(String.valueOf(e))); } } }