package org.bouncycastle.openpgp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.SecretKeyPacket;
import org.bouncycastle.gpg.SExprParser;
import org.bouncycastle.gpg.SExpression;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.openpgp.operator.PBEProtectionRemoverFactory;
import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider;

/* JADX INFO: loaded from: classes3.dex */
public class OpenedPGPKeyData {
    private final List<PGPExtendedKeyHeader> headerList;
    private final SExpression keyExpression;

    /* JADX INFO: loaded from: classes2.dex */
    public static class Builder {
        private List<PGPExtendedKeyHeader> headerList = new ArrayList();
        private SExpression keyExpression;

        public void add(PGPExtendedKeyHeader pGPExtendedKeyHeader) {
            this.headerList.add(pGPExtendedKeyHeader);
        }

        public OpenedPGPKeyData build() {
            return new OpenedPGPKeyData(this.headerList, this.keyExpression);
        }

        public Builder setHeaderList(List<PGPExtendedKeyHeader> list) {
            this.headerList = list;
            return this;
        }

        public Builder setKeyExpression(SExpression sExpression) {
            this.keyExpression = sExpression;
            return this;
        }
    }

    public OpenedPGPKeyData(List<PGPExtendedKeyHeader> list, SExpression sExpression) {
        this.headerList = Collections.unmodifiableList(list);
        this.keyExpression = sExpression;
    }

    public static Builder builder() {
        return new Builder();
    }

    public List<PGPExtendedKeyHeader> getHeaderList() {
        return this.headerList;
    }

    public ExtendedPGPSecretKey getKeyData(PGPPublicKey pGPPublicKey, PGPDigestCalculatorProvider pGPDigestCalculatorProvider, PBEProtectionRemoverFactory pBEProtectionRemoverFactory, KeyFingerPrintCalculator keyFingerPrintCalculator, int i) throws IOException, PGPException {
        int protectionType = SExprParser.getProtectionType(this.keyExpression.getString(0));
        ArrayList arrayList = new ArrayList();
        if (protectionType != 1 && protectionType != 2 && protectionType != 3) {
            return null;
        }
        SExpression expression = getKeyExpression().getExpression(1);
        String string = expression.getString(0);
        PublicKeyAlgorithmTags[] pGPSecretKey = SExprParser.getPGPSecretKey(pBEProtectionRemoverFactory, keyFingerPrintCalculator, pGPPublicKey, i, protectionType, expression, string, pGPDigestCalculatorProvider);
        if (string.equals("rsa")) {
            for (Object obj : expression.filterOut("rsa", "e", "n", "d", "p", "q", "u", "protected").getValues()) {
                arrayList.add(obj instanceof SExpression ? ((SExpression) obj).toAttribute() : PGPExtendedKeyAttribute.builder().addAttribute(obj).build());
            }
        }
        return new ExtendedPGPSecretKey(this.headerList, arrayList, (SecretKeyPacket) pGPSecretKey[0], (PGPPublicKey) pGPSecretKey[1]);
    }

    public SExpression getKeyExpression() {
        return this.keyExpression;
    }

    public String getKeyType() {
        return null;
    }
}
