package org.bouncycastle.bcpg;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.io.Streams;

/* JADX INFO: loaded from: classes3.dex */
public class PublicKeyEncSessionPacket extends ContainedPacket implements PublicKeyAlgorithmTags {
    public static final int VERSION_3 = 3;
    public static final int VERSION_6 = 6;
    private int algorithm;
    private byte[][] data;
    private byte[] keyFingerprint;
    private long keyID;
    private int keyVersion;
    private int version;

    public PublicKeyEncSessionPacket(int i, byte[] bArr, int i2, byte[][] bArr2) {
        super(1);
        this.version = 6;
        this.keyVersion = i;
        this.keyFingerprint = Arrays.clone(bArr);
        this.algorithm = i2;
        this.data = new byte[bArr2.length][];
        for (int i3 = 0; i3 < bArr2.length; i3++) {
            this.data[i3] = Arrays.clone(bArr2[i3]);
        }
    }

    public PublicKeyEncSessionPacket(long j, int i, byte[][] bArr) {
        super(1);
        this.version = 3;
        this.keyID = j;
        this.algorithm = i;
        this.data = new byte[bArr.length][];
        for (int i2 = 0; i2 != bArr.length; i2++) {
            this.data[i2] = Arrays.clone(bArr[i2]);
        }
    }

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

    /* JADX WARN: Code duplicated, block: B:22:0x005b  */
    /* JADX WARN: Code duplicated, block: B:24:0x0063  */
    /* JADX WARN: Code duplicated, block: B:25:0x0070  */
    /* JADX WARN: Code duplicated, block: B:26:0x0090  */
    PublicKeyEncSessionPacket(BCPGInputStream bCPGInputStream, boolean z) throws IOException {
        long keyID;
        super(1, z);
        this.version = bCPGInputStream.read();
        if (this.version != 3) {
            if (this.version != 6) {
                throw new UnsupportedPacketVersionException("Unsupported PGP public key encrypted session key packet version encountered: " + this.version);
            }
            int i = bCPGInputStream.read();
            if (i < 0) {
                throw new MalformedPacketException("Key Info Length cannot be negative: " + i);
            }
            if (i == 0) {
                this.keyVersion = 0;
                this.keyFingerprint = new byte[0];
                keyID = 0;
            } else {
                this.keyVersion = bCPGInputStream.read();
                this.keyFingerprint = new byte[i - 1];
                bCPGInputStream.readFully(this.keyFingerprint);
                try {
                    int i2 = this.keyVersion;
                    byte[] bArr = this.keyFingerprint;
                    this.keyID = i2 == 4 ? FingerprintUtil.keyIdFromV4Fingerprint(bArr) : FingerprintUtil.keyIdFromV6Fingerprint(bArr);
                } catch (IllegalArgumentException e) {
                    throw new MalformedPacketException("Malformed fingerprint encoding.", e);
                }
            }
            this.algorithm = bCPGInputStream.read();
            switch (this.algorithm) {
                case 1:
                case 2:
                    this.data = new byte[1][];
                    this.data[0] = new MPInteger(bCPGInputStream).getEncoded();
                    return;
                case 16:
                case 20:
                    this.data = new byte[2][];
                    this.data[0] = new MPInteger(bCPGInputStream).getEncoded();
                    this.data[1] = new MPInteger(bCPGInputStream).getEncoded();
                    return;
                case 18:
                case 25:
                case 26:
                    this.data = new byte[1][];
                    this.data[0] = Streams.readAll(bCPGInputStream);
                    return;
                default:
                    throw new IOException("unknown PGP public key algorithm encountered");
            }
        }
        keyID = StreamUtil.readKeyID(bCPGInputStream);
        this.keyID = keyID;
        this.algorithm = bCPGInputStream.read();
        switch (this.algorithm) {
            case 1:
            case 2:
                this.data = new byte[1][];
                this.data[0] = new MPInteger(bCPGInputStream).getEncoded();
                return;
            case 16:
            case 20:
                this.data = new byte[2][];
                this.data[0] = new MPInteger(bCPGInputStream).getEncoded();
                this.data[1] = new MPInteger(bCPGInputStream).getEncoded();
                return;
            case 18:
            case 25:
            case 26:
                this.data = new byte[1][];
                this.data[0] = Streams.readAll(bCPGInputStream);
                return;
            default:
                throw new IOException("unknown PGP public key algorithm encountered");
        }
    }

    public static PublicKeyEncSessionPacket createV3PKESKPacket(long j, int i, byte[][] bArr) {
        return new PublicKeyEncSessionPacket(j, i, bArr);
    }

    public static PublicKeyEncSessionPacket createV6PKESKPacket(int i, byte[] bArr, int i2, byte[][] bArr2) {
        return new PublicKeyEncSessionPacket(i, bArr, i2, bArr2);
    }

    @Override // org.bouncycastle.bcpg.ContainedPacket
    public void encode(BCPGOutputStream bCPGOutputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BCPGOutputStream bCPGOutputStream2 = new BCPGOutputStream(byteArrayOutputStream);
        bCPGOutputStream2.write(this.version);
        if (this.version == 3) {
            StreamUtil.writeKeyID(bCPGOutputStream2, this.keyID);
        } else if (this.version == 6) {
            if (this.keyFingerprint.length != 0) {
                bCPGOutputStream2.write(this.keyFingerprint.length + 1);
                bCPGOutputStream2.write(this.keyVersion);
                bCPGOutputStream2.write(this.keyFingerprint);
            } else {
                bCPGOutputStream2.write(0);
            }
        }
        bCPGOutputStream2.write(this.algorithm);
        for (int i = 0; i != this.data.length; i++) {
            bCPGOutputStream2.write(this.data[i]);
        }
        bCPGOutputStream2.close();
        bCPGOutputStream.writePacket(hasNewPacketFormat(), 1, byteArrayOutputStream.toByteArray());
    }

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

    public byte[][] getEncSessionKey() {
        return this.data;
    }

    public byte[] getKeyFingerprint() {
        return Arrays.clone(this.keyFingerprint);
    }

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

    public int getKeyVersion() {
        return this.keyVersion;
    }

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