Search in sources :

Example 1 with PGPPublicKey

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

the class MeStorage method loadWithUserID.

public Profile loadWithUserID(@Nullable UserID userID) {
    synchronized (lock) {
        String meJSON = preferences.getString("ME", null);
        if (meJSON == null) {
            Log.i(TAG, "no profile found");
            return null;
        }
        Profile me = JSON.fromJson(meJSON, Profile.class);
        if (me == null) {
            Log.i(TAG, "no profile found");
            return null;
        }
        try {
            SSHKeyPairI kp = getOrLoadKeyPair(context);
            if (kp != null) {
                me.sshWirePublicKey = kp.publicKeySSHWireFormat();
                if (userID != null) {
                    try {
                        List<UserID> userIDs = getUserIDs();
                        // keep USER_ID_LIMIT most recent UserIDs
                        if (userIDs.remove(userID)) {
                            userIDs.add(userID);
                        } else {
                            if (userIDs.size() >= USER_ID_LIMIT) {
                                userIDs.remove(0);
                            }
                            userIDs.add(userID);
                            PGPPublicKey pgpPublicKey = PGPManager.publicKeyWithIdentities(kp, userIDs);
                            me.pgpPublicKey = pgpPublicKey.serializedBytes();
                            if (userIDs.size() == USER_ID_LIMIT) {
                                // detect abuse of exporting PGP userIDs
                                Notifications.notifyPGPKeyExport(context, pgpPublicKey);
                            }
                        }
                        set(me, userIDs);
                    } catch (PGPException | IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (InvalidKeyException | IOException | CryptoException e) {
            e.printStackTrace();
        }
        try {
            me.teamCheckpoint = TeamDataProvider.getTeamCheckpoint(context).success;
        } catch (Native.NotLinked notLinked) {
            notLinked.printStackTrace();
        }
        return me;
    }
}
Also used : SSHKeyPairI(co.krypt.krypton.crypto.SSHKeyPairI) PGPPublicKey(co.krypt.krypton.pgp.PGPPublicKey) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) Profile(co.krypt.krypton.protocol.Profile) PGPException(co.krypt.krypton.pgp.PGPException) Native(co.krypt.krypton.team.Native) UserID(co.krypt.krypton.pgp.UserID) CryptoException(co.krypt.krypton.exception.CryptoException)

Example 2 with PGPPublicKey

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

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

the class PGPublicKeySelfCertificationTest method keySigning_succeeds.

@Test
public void keySigning_succeeds() throws Exception {
    for (KeyType keyType : new KeyType[] { KeyType.RSA, KeyType.Ed25519 }) {
        SSHKeyPairI kp1 = KeyManager.loadOrGenerateKeyPair(InstrumentationRegistry.getTargetContext(), keyType, "test");
        PGPPublicKey pubkey = new PGPPublicKey(kp1, Collections.singletonList(new UserID("Kevin King", "kevin@krypt.co")));
        CertifiedPublicKey parsedPubkey = CertifiedPublicKey.parse(new DataInputStream(new ByteArrayInputStream(pubkey.serializedBytes())));
        Assert.assertTrue(pubkey.signedIdentities.size() == parsedPubkey.identities.size());
        Assert.assertFalse(pubkey.signedIdentities.get(0).signature.attributes.attributes.unhashedSubpackets.issuer.header.type.critical);
    }
}
Also used : KeyType(co.krypt.krypton.crypto.KeyType) ByteArrayInputStream(java.io.ByteArrayInputStream) UserID(co.krypt.krypton.pgp.UserID) CertifiedPublicKey(co.krypt.krypton.pgp.publickey.CertifiedPublicKey) SSHKeyPairI(co.krypt.krypton.crypto.SSHKeyPairI) PGPPublicKey(co.krypt.krypton.pgp.PGPPublicKey) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Aggregations

SSHKeyPairI (co.krypt.krypton.crypto.SSHKeyPairI)3 PGPPublicKey (co.krypt.krypton.pgp.PGPPublicKey)3 UserID (co.krypt.krypton.pgp.UserID)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 Test (org.junit.Test)2 KeyType (co.krypt.krypton.crypto.KeyType)1 CryptoException (co.krypt.krypton.exception.CryptoException)1 PGPException (co.krypt.krypton.pgp.PGPException)1 UnsignedBinaryDocument (co.krypt.krypton.pgp.codesign.UnsignedBinaryDocument)1 SignedSignatureAttributes (co.krypt.krypton.pgp.packet.SignedSignatureAttributes)1 CertifiedPublicKey (co.krypt.krypton.pgp.publickey.CertifiedPublicKey)1 Profile (co.krypt.krypton.protocol.Profile)1 Native (co.krypt.krypton.team.Native)1 IOException (java.io.IOException)1 InvalidKeyException (java.security.InvalidKeyException)1