package io.netty.handler.ssl.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.base64.Base64;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Date;

/* JADX INFO: loaded from: classes.dex */
public final class SelfSignedCertificate {
    private final X509Certificate cert;
    private final File certificate;
    private final PrivateKey key;
    private final File privateKey;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) SelfSignedCertificate.class);
    private static final Date DEFAULT_NOT_BEFORE = new Date(SystemPropertyUtil.getLong("io.netty.selfSignedCertificate.defaultNotBefore", System.currentTimeMillis() - 31536000000L));
    private static final Date DEFAULT_NOT_AFTER = new Date(SystemPropertyUtil.getLong("io.netty.selfSignedCertificate.defaultNotAfter", 253402300799000L));

    public SelfSignedCertificate() throws CertificateException {
        this(DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER);
    }

    static String[] newSelfSignedCertificate(String str, PrivateKey privateKey, X509Certificate x509Certificate) throws IOException, CertificateEncodingException {
        ByteBuf byteBufWrappedBuffer = Unpooled.wrappedBuffer(privateKey.getEncoded());
        try {
            try {
                String str2 = "-----BEGIN PRIVATE KEY-----\n" + Base64.encode(byteBufWrappedBuffer, true).toString(CharsetUtil.US_ASCII) + "\n-----END PRIVATE KEY-----\n";
                byteBufWrappedBuffer.release();
                File fileCreateTempFile = File.createTempFile("keyutil_" + str + '_', ".key");
                fileCreateTempFile.deleteOnExit();
                FileOutputStream fileOutputStream = new FileOutputStream(fileCreateTempFile);
                try {
                    fileOutputStream.write(str2.getBytes(CharsetUtil.US_ASCII));
                    fileOutputStream.close();
                    byteBufWrappedBuffer = Unpooled.wrappedBuffer(x509Certificate.getEncoded());
                    try {
                        try {
                            String str3 = "-----BEGIN CERTIFICATE-----\n" + Base64.encode(byteBufWrappedBuffer, true).toString(CharsetUtil.US_ASCII) + "\n-----END CERTIFICATE-----\n";
                            byteBufWrappedBuffer.release();
                            File fileCreateTempFile2 = File.createTempFile("keyutil_" + str + '_', ".crt");
                            fileCreateTempFile2.deleteOnExit();
                            FileOutputStream fileOutputStream2 = new FileOutputStream(fileCreateTempFile2);
                            try {
                                fileOutputStream2.write(str3.getBytes(CharsetUtil.US_ASCII));
                                fileOutputStream2.close();
                                return new String[]{fileCreateTempFile2.getPath(), fileCreateTempFile.getPath()};
                            } catch (Throwable th) {
                                safeClose(fileCreateTempFile2, fileOutputStream2);
                                safeDelete(fileCreateTempFile2);
                                safeDelete(fileCreateTempFile);
                                throw th;
                            }
                        } finally {
                        }
                    } finally {
                    }
                } catch (Throwable th2) {
                    safeClose(fileCreateTempFile, fileOutputStream);
                    safeDelete(fileCreateTempFile);
                    throw th2;
                }
            } finally {
            }
        } finally {
        }
    }

    private static void safeClose(File file, OutputStream outputStream) {
        try {
            outputStream.close();
        } catch (IOException e2) {
            logger.warn("Failed to close a file: " + file, (Throwable) e2);
        }
    }

    private static void safeDelete(File file) {
        if (file.delete()) {
            return;
        }
        logger.warn("Failed to delete a file: " + file);
    }

    public X509Certificate cert() {
        return this.cert;
    }

    public File certificate() {
        return this.certificate;
    }

    public void delete() {
        safeDelete(this.certificate);
        safeDelete(this.privateKey);
    }

    public PrivateKey key() {
        return this.key;
    }

    public File privateKey() {
        return this.privateKey;
    }

    public SelfSignedCertificate(Date date, Date date2) throws CertificateException {
        this("example.com", date, date2);
    }

    public SelfSignedCertificate(String str) throws CertificateException {
        this(str, DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER);
    }

    public SelfSignedCertificate(String str, Date date, Date date2) throws CertificateException {
        this(str, ThreadLocalInsecureRandom.current(), 1024, date, date2);
    }

    public SelfSignedCertificate(String str, SecureRandom secureRandom, int i2) throws CertificateException {
        this(str, secureRandom, i2, DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:41:0x0083 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Type inference failed for: r7v10, types: [java.io.FileInputStream] */
    /* JADX WARN: Type inference failed for: r7v5, types: [java.io.File] */
    /* JADX WARN: Type inference failed for: r7v7 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public SelfSignedCertificate(java.lang.String r6, java.security.SecureRandom r7, int r8, java.util.Date r9, java.util.Date r10) throws java.lang.Throwable {
        /*
            r5 = this;
            java.lang.String r0 = "Failed to close a file: "
            r5.<init>()
            java.lang.String r1 = "RSA"
            java.security.KeyPairGenerator r1 = java.security.KeyPairGenerator.getInstance(r1)     // Catch: java.security.NoSuchAlgorithmException -> Laf
            r1.initialize(r8, r7)     // Catch: java.security.NoSuchAlgorithmException -> Laf
            java.security.KeyPair r8 = r1.generateKeyPair()     // Catch: java.security.NoSuchAlgorithmException -> Laf
            java.lang.String[] r6 = io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator.generate(r6, r8, r7, r9, r10)     // Catch: java.lang.Throwable -> L17
            goto L23
        L17:
            r1 = move-exception
            io.netty.util.internal.logging.InternalLogger r2 = io.netty.handler.ssl.util.SelfSignedCertificate.logger
            java.lang.String r3 = "Failed to generate a self-signed X.509 certificate using sun.security.x509:"
            r2.debug(r3, r1)
            java.lang.String[] r6 = io.netty.handler.ssl.util.BouncyCastleSelfSignedCertGenerator.generate(r6, r8, r7, r9, r10)     // Catch: java.lang.Throwable -> L9f
        L23:
            java.io.File r7 = new java.io.File
            r9 = 0
            r9 = r6[r9]
            r7.<init>(r9)
            r5.certificate = r7
            java.io.File r7 = new java.io.File
            r9 = 1
            r6 = r6[r9]
            r7.<init>(r6)
            r5.privateKey = r7
            java.security.PrivateKey r6 = r8.getPrivate()
            r5.key = r6
            r6 = 0
            java.io.FileInputStream r7 = new java.io.FileInputStream     // Catch: java.lang.Throwable -> L71 java.lang.Exception -> L76
            java.io.File r8 = r5.certificate     // Catch: java.lang.Throwable -> L71 java.lang.Exception -> L76
            r7.<init>(r8)     // Catch: java.lang.Throwable -> L71 java.lang.Exception -> L76
            java.lang.String r6 = "X509"
            java.security.cert.CertificateFactory r6 = java.security.cert.CertificateFactory.getInstance(r6)     // Catch: java.lang.Exception -> L6f java.lang.Throwable -> L80
            java.security.cert.Certificate r6 = r6.generateCertificate(r7)     // Catch: java.lang.Exception -> L6f java.lang.Throwable -> L80
            java.security.cert.X509Certificate r6 = (java.security.cert.X509Certificate) r6     // Catch: java.lang.Exception -> L6f java.lang.Throwable -> L80
            r5.cert = r6     // Catch: java.lang.Exception -> L6f java.lang.Throwable -> L80
            r7.close()     // Catch: java.io.IOException -> L57
            goto L6e
        L57:
            r6 = move-exception
            io.netty.util.internal.logging.InternalLogger r7 = io.netty.handler.ssl.util.SelfSignedCertificate.logger
            java.lang.StringBuilder r8 = new java.lang.StringBuilder
            r8.<init>()
            r8.append(r0)
            java.io.File r9 = r5.certificate
            r8.append(r9)
            java.lang.String r8 = r8.toString()
            r7.warn(r8, r6)
        L6e:
            return
        L6f:
            r6 = move-exception
            goto L7a
        L71:
            r7 = move-exception
            r4 = r7
            r7 = r6
            r6 = r4
            goto L81
        L76:
            r7 = move-exception
            r4 = r7
            r7 = r6
            r6 = r4
        L7a:
            java.security.cert.CertificateEncodingException r8 = new java.security.cert.CertificateEncodingException     // Catch: java.lang.Throwable -> L80
            r8.<init>(r6)     // Catch: java.lang.Throwable -> L80
            throw r8     // Catch: java.lang.Throwable -> L80
        L80:
            r6 = move-exception
        L81:
            if (r7 == 0) goto L9e
            r7.close()     // Catch: java.io.IOException -> L87
            goto L9e
        L87:
            r7 = move-exception
            io.netty.util.internal.logging.InternalLogger r8 = io.netty.handler.ssl.util.SelfSignedCertificate.logger
            java.lang.StringBuilder r9 = new java.lang.StringBuilder
            r9.<init>()
            r9.append(r0)
            java.io.File r10 = r5.certificate
            r9.append(r10)
            java.lang.String r9 = r9.toString()
            r8.warn(r9, r7)
        L9e:
            throw r6
        L9f:
            r6 = move-exception
            io.netty.util.internal.logging.InternalLogger r7 = io.netty.handler.ssl.util.SelfSignedCertificate.logger
            java.lang.String r8 = "Failed to generate a self-signed X.509 certificate using Bouncy Castle:"
            r7.debug(r8, r6)
            java.security.cert.CertificateException r6 = new java.security.cert.CertificateException
            java.lang.String r7 = "No provider succeeded to generate a self-signed certificate. See debug log for the root cause."
            r6.<init>(r7)
            throw r6
        Laf:
            r6 = move-exception
            java.lang.Error r7 = new java.lang.Error
            r7.<init>(r6)
            throw r7
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.ssl.util.SelfSignedCertificate.<init>(java.lang.String, java.security.SecureRandom, int, java.util.Date, java.util.Date):void");
    }
}
