package org.bouncycastle.openpgp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.HashUtils;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.bcpg.MPInteger;
import org.bouncycastle.bcpg.Packet;
import org.bouncycastle.bcpg.SignaturePacket;
import org.bouncycastle.bcpg.SignatureSubpacket;
import org.bouncycastle.bcpg.TrustPacket;
import org.bouncycastle.bcpg.sig.IssuerFingerprint;
import org.bouncycastle.bcpg.sig.IssuerKeyID;
import org.bouncycastle.bcpg.sig.RevocationReason;
import org.bouncycastle.openpgp.operator.PGPContentVerifier;
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilder;
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.BigIntegers;
import org.bouncycastle.util.Strings;

/* JADX INFO: loaded from: classes3.dex */
public class PGPSignature extends PGPDefaultSignatureGenerator {
    public static final int BINARY_DOCUMENT = 0;
    public static final int CANONICAL_TEXT_DOCUMENT = 1;
    public static final int CASUAL_CERTIFICATION = 18;
    public static final int CERTIFICATION_REVOCATION = 48;
    public static final int DEFAULT_CERTIFICATION = 16;
    public static final int DIRECT_KEY = 31;
    public static final int KEY_REVOCATION = 32;
    public static final int NO_CERTIFICATION = 17;
    public static final int POSITIVE_CERTIFICATION = 19;
    public static final int PRIMARYKEY_BINDING = 25;
    public static final int STAND_ALONE = 2;
    public static final int SUBKEY_BINDING = 24;
    public static final int SUBKEY_REVOCATION = 40;
    public static final int THIRD_PARTY_CONFIRMATION = 80;
    public static final int TIMESTAMP = 64;
    final SignaturePacket sigPck;
    private final TrustPacket trustPck;
    private volatile PGPContentVerifier verifier;

    public PGPSignature(BCPGInputStream bCPGInputStream) throws IOException, PGPException {
        this(cast(bCPGInputStream.readPacket()));
    }

    PGPSignature(SignaturePacket signaturePacket) {
        this(signaturePacket, null);
    }

    PGPSignature(SignaturePacket signaturePacket, TrustPacket trustPacket) {
        super(signaturePacket.getVersion());
        this.sigPck = signaturePacket;
        this.sigType = this.sigPck.getSignatureType();
        this.trustPck = trustPacket;
    }

    PGPSignature(PGPSignature pGPSignature) {
        super(pGPSignature.getVersion());
        this.sigPck = pGPSignature.sigPck;
        this.sigType = pGPSignature.sigType;
        this.trustPck = pGPSignature.trustPck;
    }

    private void addTrailer() {
        try {
            this.sigOut.write(this.sigPck.getSignatureTrailer());
            this.sigOut.close();
        } catch (IOException e) {
            throw new PGPRuntimeOperationException(e.getMessage(), e);
        }
    }

    private static SignaturePacket cast(Packet packet) throws IOException {
        if (packet instanceof SignaturePacket) {
            return (SignaturePacket) packet;
        }
        throw new IOException("unexpected packet in stream: " + packet);
    }

    private void checkSaltSize() throws PGPException {
        int v6SignatureSaltSizeInBytes;
        if (getVersion() == 6 && (v6SignatureSaltSizeInBytes = HashUtils.getV6SignatureSaltSizeInBytes(getHashAlgorithm())) != this.sigPck.getSalt().length) {
            throw new PGPException("RFC9580 defines the salt size for " + PGPUtil.getDigestName(getHashAlgorithm()) + " as " + v6SignatureSaltSizeInBytes + " octets, but signature has " + this.sigPck.getSalt().length + " octets.");
        }
    }

    private PGPSignatureSubpacketVector createSubpacketVector(SignatureSubpacket[] signatureSubpacketArr) {
        if (signatureSubpacketArr != null) {
            return new PGPSignatureSubpacketVector(signatureSubpacketArr);
        }
        return null;
    }

    private List<KeyIdentifier> extractKeyIdentifiers(SignatureSubpacket[] signatureSubpacketArr) {
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i != signatureSubpacketArr.length; i++) {
            SignatureSubpacket signatureSubpacket = signatureSubpacketArr[i];
            if (signatureSubpacket instanceof IssuerFingerprint) {
                arrayList.add(new KeyIdentifier(((IssuerFingerprint) signatureSubpacket).getFingerprint()));
            }
            if (signatureSubpacket instanceof IssuerKeyID) {
                arrayList.add(new KeyIdentifier(((IssuerKeyID) signatureSubpacket).getKeyID()));
            }
        }
        return arrayList;
    }

    public static boolean isCertification(int i) {
        return 16 == i || 17 == i || 18 == i || 19 == i;
    }

    public static boolean isRevocation(int i) {
        return 32 == i || 48 == i || 40 == i;
    }

    public static boolean isSignatureEncodingEqual(PGPSignature pGPSignature, PGPSignature pGPSignature2) {
        return Arrays.areEqual(pGPSignature.sigPck.getSignatureBytes(), pGPSignature2.sigPck.getSignatureBytes());
    }

    public static PGPSignature join(PGPSignature pGPSignature, PGPSignature pGPSignature2) throws PGPException {
        if (pGPSignature.getVersion() < 4) {
            return pGPSignature;
        }
        if (pGPSignature.getVersion() != pGPSignature2.getVersion() || !isSignatureEncodingEqual(pGPSignature, pGPSignature2)) {
            throw new IllegalArgumentException("These are different signatures.");
        }
        SignatureSubpacket[] signatureSubpacketArr = pGPSignature.getUnhashedSubPackets().packets;
        SignatureSubpacket[] signatureSubpacketArr2 = pGPSignature2.getUnhashedSubPackets().packets;
        ArrayList arrayList = new ArrayList(java.util.Arrays.asList(signatureSubpacketArr));
        for (int i = 0; i != signatureSubpacketArr2.length; i++) {
            SignatureSubpacket signatureSubpacket = signatureSubpacketArr2[i];
            int i2 = 0;
            while (true) {
                if (i2 == signatureSubpacketArr.length) {
                    arrayList.add(signatureSubpacket);
                    break;
                }
                if (signatureSubpacket.equals(signatureSubpacketArr[i2])) {
                    break;
                }
                i2++;
            }
        }
        return new PGPSignature(SignaturePacket.copyOfWith(pGPSignature.sigPck, (SignatureSubpacket[]) arrayList.toArray(new SignatureSubpacket[0])));
    }

    private void updateWithSalt() throws PGPException {
        if (getVersion() == 6) {
            try {
                this.sigOut.write(this.sigPck.getSalt());
            } catch (IOException e) {
                throw new PGPException("Could not update with salt.", e);
            }
        }
    }

    PGPContentVerifierBuilder createVerifierProvider(PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider) throws PGPException {
        return pGPContentVerifierBuilderProvider.get(this.sigPck.getKeyAlgorithm(), this.sigPck.getHashAlgorithm());
    }

    boolean doVerifyCertification(PGPPublicKey pGPPublicKey) throws PGPException {
        updateWithPublicKey(pGPPublicKey);
        addTrailer();
        return this.verifier.verify(getSignature());
    }

    boolean doVerifyCertification(PGPPublicKey pGPPublicKey, PGPPublicKey pGPPublicKey2) throws PGPException {
        updateWithPublicKey(pGPPublicKey);
        updateWithPublicKey(pGPPublicKey2);
        addTrailer();
        return this.verifier.verify(getSignature());
    }

    boolean doVerifyCertification(PGPUserAttributeSubpacketVector pGPUserAttributeSubpacketVector, PGPPublicKey pGPPublicKey) throws PGPException {
        updateWithPublicKey(pGPPublicKey);
        getAttributesHash(pGPUserAttributeSubpacketVector);
        addTrailer();
        return this.verifier.verify(getSignature());
    }

    boolean doVerifyCertification(byte[] bArr, PGPPublicKey pGPPublicKey) throws PGPException {
        updateWithPublicKey(pGPPublicKey);
        updateWithIdData(180, bArr);
        addTrailer();
        return this.verifier.verify(getSignature());
    }

    public void encode(OutputStream outputStream) throws IOException {
        encode(outputStream, false);
    }

    public void encode(OutputStream outputStream, boolean z) throws IOException {
        if (!z || (getHashedSubPackets().isExportable() && getUnhashedSubPackets().isExportable())) {
            BCPGOutputStream bCPGOutputStreamWrap = BCPGOutputStream.wrap(outputStream);
            bCPGOutputStreamWrap.writePacket(this.sigPck);
            if (z || this.trustPck == null) {
                return;
            }
            bCPGOutputStreamWrap.writePacket(this.trustPck);
        }
    }

    public Date getCreationTime() {
        return new Date(this.sigPck.getCreationTime());
    }

    public byte[] getDigestPrefix() {
        return this.sigPck.getFingerPrint();
    }

    public byte[] getEncoded() throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        encode(byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    public byte[] getEncoded(boolean z) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        encode(byteArrayOutputStream, z);
        return byteArrayOutputStream.toByteArray();
    }

    public int getHashAlgorithm() {
        return this.sigPck.getHashAlgorithm();
    }

    public List<KeyIdentifier> getHashedKeyIdentifiers() {
        return extractKeyIdentifiers(this.sigPck.getHashedSubPackets());
    }

    public PGPSignatureSubpacketVector getHashedSubPackets() {
        return createSubpacketVector(this.sigPck.getHashedSubPackets());
    }

    public int getKeyAlgorithm() {
        return this.sigPck.getKeyAlgorithm();
    }

    public long getKeyID() {
        return this.sigPck.getKeyID();
    }

    public List<KeyIdentifier> getKeyIdentifiers() {
        ArrayList arrayList = new ArrayList();
        if (getVersion() <= 3) {
            arrayList.add(new KeyIdentifier(getKeyID()));
        } else {
            arrayList.addAll(getHashedKeyIdentifiers());
            arrayList.addAll(getUnhashedKeyIdentifiers());
        }
        return arrayList;
    }

    byte[] getSalt() {
        return this.sigPck.getSalt();
    }

    public byte[] getSignature() throws PGPException {
        byte[] bArr;
        MPInteger[] signature = this.sigPck.getSignature();
        if (signature == null) {
            return this.sigPck.getSignatureBytes();
        }
        if (signature.length == 1) {
            return BigIntegers.asUnsignedByteArray(signature[0].getValue());
        }
        if (getKeyAlgorithm() != 22) {
            try {
                ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector();
                aSN1EncodableVector.add(new ASN1Integer(signature[0].getValue()));
                aSN1EncodableVector.add(new ASN1Integer(signature[1].getValue()));
                return new DERSequence(aSN1EncodableVector).getEncoded();
            } catch (IOException e) {
                throw new PGPException("exception encoding DSA sig.", e);
            }
        }
        byte[] bArrAsUnsignedByteArray = BigIntegers.asUnsignedByteArray(signature[0].getValue());
        byte[] bArrAsUnsignedByteArray2 = BigIntegers.asUnsignedByteArray(signature[1].getValue());
        if (bArrAsUnsignedByteArray.length + bArrAsUnsignedByteArray2.length > 64) {
            if (bArrAsUnsignedByteArray.length > 57 || bArrAsUnsignedByteArray2.length > 114) {
                throw new PGPException("Malformed Ed448 signature encoding (too long).");
            }
            bArr = new byte[114];
            System.arraycopy(bArrAsUnsignedByteArray, 0, bArr, 57 - bArrAsUnsignedByteArray.length, bArrAsUnsignedByteArray.length);
            System.arraycopy(bArrAsUnsignedByteArray2, 0, bArr, 114 - bArrAsUnsignedByteArray2.length, bArrAsUnsignedByteArray2.length);
        } else {
            if (bArrAsUnsignedByteArray.length > 32 || bArrAsUnsignedByteArray2.length > 64) {
                throw new PGPException("Malformed Ed25519 signature encoding (too long).");
            }
            bArr = new byte[64];
            System.arraycopy(bArrAsUnsignedByteArray, 0, bArr, 32 - bArrAsUnsignedByteArray.length, bArrAsUnsignedByteArray.length);
            System.arraycopy(bArrAsUnsignedByteArray2, 0, bArr, 64 - bArrAsUnsignedByteArray2.length, bArrAsUnsignedByteArray2.length);
        }
        return bArr;
    }

    public byte[] getSignatureTrailer() {
        return this.sigPck.getSignatureTrailer();
    }

    public int getSignatureType() {
        return this.sigPck.getSignatureType();
    }

    public List<KeyIdentifier> getUnhashedKeyIdentifiers() {
        return extractKeyIdentifiers(this.sigPck.getUnhashedSubPackets());
    }

    public PGPSignatureSubpacketVector getUnhashedSubPackets() {
        return createSubpacketVector(this.sigPck.getUnhashedSubPackets());
    }

    public int getVersion() {
        return this.sigPck.getVersion();
    }

    public boolean hasKeyIdentifier(KeyIdentifier keyIdentifier) {
        Iterator<KeyIdentifier> it = getKeyIdentifiers().iterator();
        while (it.hasNext()) {
            if (it.next().matchesExplicit(keyIdentifier)) {
                return true;
            }
        }
        return false;
    }

    public boolean hasSubpackets() {
        return (this.sigPck.getHashedSubPackets() == null && this.sigPck.getUnhashedSubPackets() == null) ? false : true;
    }

    void init(PGPContentVerifier pGPContentVerifier) throws PGPException {
        this.verifier = pGPContentVerifier;
        this.lastb = (byte) 0;
        this.sigOut = pGPContentVerifier.getOutputStream();
        checkSaltSize();
        updateWithSalt();
    }

    public void init(PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider, PGPPublicKey pGPPublicKey) throws PGPException {
        if (this.sigType == 255) {
            throw new PGPException("Illegal signature type 0xFF provided.");
        }
        if (getVersion() == 6 && pGPPublicKey.getVersion() != 6) {
            throw new PGPException("MUST NOT verify v6 signature with non-v6 key.");
        }
        if (getVersion() == 4 && pGPPublicKey.getVersion() != 4) {
            throw new PGPException("MUST NOT verify v4 signature with non-v4 key.");
        }
        init(createVerifierProvider(pGPContentVerifierBuilderProvider).build(pGPPublicKey));
    }

    public boolean isCertification() {
        return isCertification(getSignatureType());
    }

    public boolean isHardRevocation() {
        if (!isRevocation(getSignatureType())) {
            return false;
        }
        if (!hasSubpackets()) {
            return true;
        }
        RevocationReason revocationReason = getHashedSubPackets() != null ? getHashedSubPackets().getRevocationReason() : null;
        if (revocationReason == null) {
            return true;
        }
        byte revocationReason2 = revocationReason.getRevocationReason();
        if (revocationReason2 < 100 || revocationReason2 > 110) {
            return (revocationReason2 == 1 || revocationReason2 == 3 || revocationReason2 == 32) ? false : true;
        }
        return true;
    }

    @Override // org.bouncycastle.openpgp.PGPDefaultSignatureGenerator
    public /* bridge */ /* synthetic */ void update(byte b) {
        super.update(b);
    }

    @Override // org.bouncycastle.openpgp.PGPDefaultSignatureGenerator
    public /* bridge */ /* synthetic */ void update(byte[] bArr) {
        super.update(bArr);
    }

    @Override // org.bouncycastle.openpgp.PGPDefaultSignatureGenerator
    public /* bridge */ /* synthetic */ void update(byte[] bArr, int i, int i2) {
        super.update(bArr, i, i2);
    }

    public boolean verify() throws PGPException {
        try {
            this.sigOut.write(getSignatureTrailer());
            this.sigOut.close();
            return this.verifier.verify(getSignature());
        } catch (IOException e) {
            throw new PGPException(e.getMessage(), e);
        }
    }

    public boolean verifyCertification(String str, PGPPublicKey pGPPublicKey) throws PGPException {
        return verifyCertification(Strings.toUTF8ByteArray(str), pGPPublicKey);
    }

    public boolean verifyCertification(PGPPublicKey pGPPublicKey) throws PGPException {
        if (this.verifier == null) {
            throw new PGPException("PGPSignature not initialised - call init().");
        }
        if (getSignatureType() == 32 || getSignatureType() == 31) {
            return doVerifyCertification(pGPPublicKey);
        }
        throw new PGPException("signature is not a key signature");
    }

    public boolean verifyCertification(PGPPublicKey pGPPublicKey, PGPPublicKey pGPPublicKey2) throws PGPException {
        if (this.verifier == null) {
            throw new PGPException("PGPSignature not initialised - call init().");
        }
        if (24 == this.sigType || 25 == this.sigType || 40 == this.sigType) {
            return doVerifyCertification(pGPPublicKey, pGPPublicKey2);
        }
        throw new PGPException("signature is not a key binding signature.");
    }

    public boolean verifyCertification(PGPUserAttributeSubpacketVector pGPUserAttributeSubpacketVector, PGPPublicKey pGPPublicKey) throws PGPException {
        if (this.verifier == null) {
            throw new PGPException("PGPSignature not initialised - call init().");
        }
        if (isCertification(this.sigType) || 48 == this.sigType) {
            return doVerifyCertification(pGPUserAttributeSubpacketVector, pGPPublicKey);
        }
        throw new PGPException("signature is neither a certification signature nor a certification revocation.");
    }

    public boolean verifyCertification(byte[] bArr, PGPPublicKey pGPPublicKey) throws PGPException {
        if (this.verifier == null) {
            throw new PGPException("PGPSignature not initialised - call init().");
        }
        if (isCertification(this.sigType) || 48 == this.sigType) {
            return doVerifyCertification(bArr, pGPPublicKey);
        }
        throw new PGPException("signature is neither a certification signature nor a certification revocation.");
    }
}
