Search in sources :

Example 1 with SignedSignatureAttributes

use of co.krypt.krypton.pgp.packet.SignedSignatureAttributes 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 SignedSignatureAttributes

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

the class PGPCodesignTest method dataSigning_succeeds.

@Test
public void dataSigning_succeeds() throws Exception {
    final byte[] data = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
    SSHKeyPairI kp1 = KeyManager.loadOrGenerateKeyPair(InstrumentationRegistry.getTargetContext(), KeyType.RSA, "test");
    PGPPublicKey pubkey = new PGPPublicKey(kp1, Collections.singletonList(new UserID("Kevin King", "kevin@krypt.co")));
    UnsignedBinaryDocument unsigned = new UnsignedBinaryDocument(data, kp1, HashAlgorithm.SHA512);
    SignedSignatureAttributes sig = unsigned.sign(kp1.pgpSign(HashAlgorithm.SHA512, SignableUtils.signableBytes(unsigned)));
    byte[] serializedSig = sig.serializedBytes();
    SignedSignatureAttributes parsedSig = SignedSignatureAttributes.parse(new DataInputStream(new ByteArrayInputStream(serializedSig)));
    Assert.assertTrue(parsedSig.attributes.attributes.hashAlgorithm == HashAlgorithm.SHA512);
    Assert.assertTrue(parsedSig.attributes.attributes.pkAlgorithm == PublicKeyAlgorithm.RSA_SIGN_ONLY);
    Assert.assertTrue(parsedSig.attributes.attributes.type == SignatureType.BINARY);
    Assert.assertFalse(parsedSig.attributes.attributes.unhashedSubpackets.issuer.header.type.critical);
}
Also used : UnsignedBinaryDocument(co.krypt.krypton.pgp.codesign.UnsignedBinaryDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) UserID(co.krypt.krypton.pgp.UserID) SignedSignatureAttributes(co.krypt.krypton.pgp.packet.SignedSignatureAttributes) SSHKeyPairI(co.krypt.krypton.crypto.SSHKeyPairI) PGPPublicKey(co.krypt.krypton.pgp.PGPPublicKey) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 3 with SignedSignatureAttributes

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

the class PGPSignatureTest method signedBinaryDocumentHashPrefix_matches.

@Test
public void signedBinaryDocumentHashPrefix_matches() throws Exception {
    byte[] signatureBytes = AsciiArmor.parse(testSig1).data;
    SignedSignatureAttributes sig = SignedSignatureAttributes.parse(new DataInputStream(new ByteArrayInputStream(signatureBytes)));
    UnsignedBinaryDocument doc = new UnsignedBinaryDocument(signedData1.getBytes("UTF-8"), sig.attributes.attributes);
    Assert.assertTrue(SignableUtils.hashPrefix(sig.attributes.attributes.hashAlgorithm, doc) == sig.attributes.hashPrefix);
    ByteArrayOutputStream reserialized = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(reserialized);
    sig.serialize(out);
    out.close();
    Assert.assertTrue(Arrays.equals(reserialized.toByteArray(), signatureBytes));
}
Also used : UnsignedBinaryDocument(co.krypt.krypton.pgp.codesign.UnsignedBinaryDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) SignedSignatureAttributes(co.krypt.krypton.pgp.packet.SignedSignatureAttributes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Aggregations

SignedSignatureAttributes (co.krypt.krypton.pgp.packet.SignedSignatureAttributes)3 UnsignedBinaryDocument (co.krypt.krypton.pgp.codesign.UnsignedBinaryDocument)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 Test (org.junit.Test)2 Pair (android.support.v4.util.Pair)1 SSHKeyPairI (co.krypt.krypton.crypto.SSHKeyPairI)1 PGPPublicKey (co.krypt.krypton.pgp.PGPPublicKey)1 UserID (co.krypt.krypton.pgp.UserID)1 PacketHeader (co.krypt.krypton.pgp.packet.PacketHeader)1 UserIDPacket (co.krypt.krypton.pgp.packet.UserIDPacket)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1