package org.bouncycastle.bcpg;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import org.bouncycastle.asn1.cmc.BodyPartID;
import org.bouncycastle.util.Pack;

/* JADX INFO: loaded from: classes.dex */
public class PublicKeyPacket extends ContainedPacket implements PublicKeyAlgorithmTags {
    public static final int LIBREPGP_5 = 5;
    public static final int MAX_LEN = 2097152;
    public static final int VERSION_3 = 3;
    public static final int VERSION_4 = 4;
    public static final int VERSION_6 = 6;
    private int algorithm;
    private BCPGKey key;
    private long time;
    private int validDays;
    private int version;

    PublicKeyPacket(int i, int i2, int i3, Date date, BCPGKey bCPGKey) {
        super(i);
        this.version = i2;
        this.time = date.getTime() / 1000;
        this.algorithm = i3;
        this.key = bCPGKey;
    }

    public PublicKeyPacket(int i, int i2, Date date, BCPGKey bCPGKey) {
        this(6, i, i2, date, bCPGKey);
    }

    @Deprecated
    public PublicKeyPacket(int i, Date date, BCPGKey bCPGKey) {
        this(4, i, date, bCPGKey);
    }

    PublicKeyPacket(int i, BCPGInputStream bCPGInputStream) throws IOException {
        this(i, bCPGInputStream, false);
    }

    PublicKeyPacket(int i, BCPGInputStream bCPGInputStream, boolean z) throws IOException {
        long j;
        super(i, z);
        this.version = bCPGInputStream.read();
        if (this.version < 2 || this.version > 6) {
            throw new UnsupportedPacketVersionException("Unsupported Public Key Packet version encountered: " + this.version);
        }
        this.time = ((long) StreamUtil.read4OctetLength(bCPGInputStream)) & BodyPartID.bodyIdMax;
        if (this.version == 2 || this.version == 3) {
            this.validDays = StreamUtil.read2OctetLength(bCPGInputStream);
        }
        this.algorithm = (byte) bCPGInputStream.read();
        if (this.version == 5 || this.version == 6) {
            j = StreamUtil.read4OctetLength(bCPGInputStream);
            if (j < 0) {
                throw new MalformedPacketException("Octet length cannot be negative.");
            }
        } else {
            j = -1;
        }
        parseKey(bCPGInputStream, this.algorithm, j);
    }

    PublicKeyPacket(BCPGInputStream bCPGInputStream) throws IOException {
        this(bCPGInputStream, false);
    }

    PublicKeyPacket(BCPGInputStream bCPGInputStream, boolean z) throws IOException {
        this(6, bCPGInputStream, z);
    }

    public static long getKeyID(PublicKeyPacket publicKeyPacket, byte[] bArr) {
        if (publicKeyPacket.version <= 3) {
            return ((RSAPublicBCPGKey) publicKeyPacket.key).getModulus().longValue();
        }
        if (publicKeyPacket.version == 4) {
            return Pack.bigEndianToLong(bArr, bArr.length - 8);
        }
        if (publicKeyPacket.version == 5 || publicKeyPacket.version == 6) {
            return Pack.bigEndianToLong(bArr, 0);
        }
        return 0L;
    }

    private void parseKey(BCPGInputStream bCPGInputStream, int i, long j) throws IOException {
        BCPGKey rSAPublicBCPGKey;
        try {
            switch (i) {
                case 1:
                case 2:
                case 3:
                    rSAPublicBCPGKey = new RSAPublicBCPGKey(bCPGInputStream);
                    break;
                case 16:
                case 20:
                    rSAPublicBCPGKey = new ElGamalPublicBCPGKey(bCPGInputStream);
                    break;
                case 17:
                    rSAPublicBCPGKey = new DSAPublicBCPGKey(bCPGInputStream);
                    break;
                case 18:
                    rSAPublicBCPGKey = new ECDHPublicBCPGKey(bCPGInputStream);
                    break;
                case 19:
                    rSAPublicBCPGKey = new ECDSAPublicBCPGKey(bCPGInputStream);
                    break;
                case 22:
                    rSAPublicBCPGKey = new EdDSAPublicBCPGKey(bCPGInputStream);
                    break;
                case 25:
                    rSAPublicBCPGKey = new X25519PublicBCPGKey(bCPGInputStream);
                    break;
                case 26:
                    rSAPublicBCPGKey = new X448PublicBCPGKey(bCPGInputStream);
                    break;
                case 27:
                    rSAPublicBCPGKey = new Ed25519PublicBCPGKey(bCPGInputStream);
                    break;
                case 28:
                    rSAPublicBCPGKey = new Ed448PublicBCPGKey(bCPGInputStream);
                    break;
                default:
                    if (this.version != 6 && this.version != 5) {
                        throw new IOException("unknown PGP public key algorithm encountered: " + this.algorithm);
                    }
                    rSAPublicBCPGKey = new UnknownBCPGKey((int) j, bCPGInputStream);
                    break;
            }
            this.key = rSAPublicBCPGKey;
        } catch (RuntimeException e) {
            throw new MalformedPacketException("Malformed PGP key.", e);
        }
    }

    @Override // org.bouncycastle.bcpg.ContainedPacket
    public void encode(BCPGOutputStream bCPGOutputStream) throws IOException {
        bCPGOutputStream.writePacket(hasNewPacketFormat(), getPacketTag(), getEncodedContents());
    }

    public int getAlgorithm() {
        return this.algorithm;
    }

    public byte[] getEncodedContents() throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BCPGOutputStream bCPGOutputStream = new BCPGOutputStream(byteArrayOutputStream);
        bCPGOutputStream.write(this.version);
        StreamUtil.writeSeconds(bCPGOutputStream, this.time);
        if (this.version <= 3) {
            StreamUtil.write2OctetLength(bCPGOutputStream, this.validDays);
        }
        bCPGOutputStream.write(this.algorithm);
        if (this.version == 6 || this.version == 5) {
            StreamUtil.write4OctetLength(bCPGOutputStream, this.key.getEncoded().length);
        }
        bCPGOutputStream.writeObject((BCPGObject) this.key);
        bCPGOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    }

    public BCPGKey getKey() {
        return this.key;
    }

    public Date getTime() {
        return new Date(this.time * 1000);
    }

    public int getValidDays() {
        return this.validDays;
    }

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