Search in sources :

Example 1 with PacketHeader

use of co.krypt.krypton.pgp.packet.PacketHeader in project krypton-android by kryptco.

the class CertifiedPublicKey method parse.

public static CertifiedPublicKey parse(DataInputStream in) throws InvalidPacketTagException, UnsupportedOldPacketLengthTypeException, UnsupportedNewFormatException, UnsupportedPublicKeyAlgorithmException, UnsupportedPublicKeyVersionException, InvalidEd25519PublicKeyFormatException, IOException, InvalidUTF8Exception, DuplicateSubpacketException, NoSuchAlgorithmException, UnsupportedHashAlgorithmException, InvalidSubpacketLengthException, UnsupportedCriticalSubpacketTypeException, UnsupportedSignatureVersionException {
    PublicKeyPacket publicKeyPacket = null;
    boolean lastPacketUserIDOrSignature = false;
    List<Pair<UserIDPacket, List<SignedSignatureAttributes>>> identities = new LinkedList<>();
    while (true) {
        try {
            PacketHeader header = PacketHeader.parse(in);
            Log.d("PGP", "found packet with type " + header.tag.packetType.toString());
            switch(header.tag.packetType) {
                case SIGNATURE:
                    SignedSignatureAttributes signaturePacket = SignedSignatureAttributes.parse(header, in);
                    if (lastPacketUserIDOrSignature && identities.size() > 0) {
                        identities.get(identities.size() - 1).second.add(signaturePacket);
                    }
                    break;
                case PUBLIC_KEY:
                    if (publicKeyPacket != null) {
                        // only accept first public key packet
                        in.skip(header.length.bodyLength);
                        continue;
                    }
                    publicKeyPacket = PublicKeyPacket.parse(header, in);
                    break;
                case USER_ID:
                    identities.add(new Pair<UserIDPacket, List<SignedSignatureAttributes>>(UserIDPacket.parse(header, in), new LinkedList<SignedSignatureAttributes>()));
                    break;
                default:
                    in.skip(header.length.bodyLength);
                    break;
            }
            lastPacketUserIDOrSignature = header.tag.packetType == PacketType.USER_ID || header.tag.packetType == PacketType.SIGNATURE;
        } catch (EOFException e) {
            break;
        }
    }
    return new CertifiedPublicKey(publicKeyPacket, identities);
}
Also used : SignedSignatureAttributes(co.krypt.krypton.pgp.packet.SignedSignatureAttributes) EOFException(java.io.EOFException) PacketHeader(co.krypt.krypton.pgp.packet.PacketHeader) List(java.util.List) LinkedList(java.util.LinkedList) UserIDPacket(co.krypt.krypton.pgp.packet.UserIDPacket) LinkedList(java.util.LinkedList) Pair(android.support.v4.util.Pair)

Example 2 with PacketHeader

use of co.krypt.krypton.pgp.packet.PacketHeader in project krypton-android by kryptco.

the class PGPPacketTest method invalidUserIDPacket_throwsIO.

@Test(expected = InvalidUTF8Exception.class)
public void invalidUserIDPacket_throwsIO() throws Exception {
    PacketHeader header = new PacketHeader(PacketTag.parse((byte) (PacketTag.LEADING_ONE | OldPacketLengthType.ONE_OCTET.getValue() | (PacketType.USER_ID.getValue() << 2))), 2);
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(new byte[] { (byte) 0xc3, 0x28 }));
    UserIDPacket.parse(header, in);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PacketHeader(co.krypt.krypton.pgp.packet.PacketHeader) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Aggregations

PacketHeader (co.krypt.krypton.pgp.packet.PacketHeader)2 Pair (android.support.v4.util.Pair)1 SignedSignatureAttributes (co.krypt.krypton.pgp.packet.SignedSignatureAttributes)1 UserIDPacket (co.krypt.krypton.pgp.packet.UserIDPacket)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Test (org.junit.Test)1