Search in sources :

Example 26 with OpenPgpV4Fingerprint

use of org.pgpainless.key.OpenPgpV4Fingerprint in project Smack by igniterealtime.

the class PublicKeysListElementTest method providerTest.

@Test
public void providerTest() throws Exception {
    String expected = "<public-keys-list xmlns='urn:xmpp:openpgp:0'>" + "<pubkey-metadata " + "v4-fingerprint='1357B01865B2503C18453D208CAC2A9678548E35' " + "date='2018-03-01T15:26:12.000+00:00'" + "/>" + "<pubkey-metadata " + "v4-fingerprint='67819B343B2AB70DED9320872C6464AF2A8E4C02' " + "date='1953-05-16T12:00:00.000+00:00'" + "/>" + "</public-keys-list>";
    Date date1 = XmppDateTime.parseDate("2018-03-01T15:26:12.000+00:00");
    Date date2 = XmppDateTime.parseDate("1953-05-16T12:00:00.000+00:00");
    PublicKeysListElement.PubkeyMetadataElement child1 = new PublicKeysListElement.PubkeyMetadataElement(new OpenPgpV4Fingerprint("1357B01865B2503C18453D208CAC2A9678548E35"), date1);
    PublicKeysListElement.PubkeyMetadataElement child2 = new PublicKeysListElement.PubkeyMetadataElement(new OpenPgpV4Fingerprint("67819B343B2AB70DED9320872C6464AF2A8E4C02"), date2);
    PublicKeysListElement element = PublicKeysListElement.builder().addMetadata(child1).addMetadata(child2).build();
    assertXmlSimilar(expected, element.toXML().toString());
    XmlPullParser parser = TestUtils.getParser(expected);
    PublicKeysListElement parsed = PublicKeysListElementProvider.TEST_INSTANCE.parse(parser);
    assertEquals(element.getMetadata(), parsed.getMetadata());
}
Also used : PublicKeysListElement(org.jivesoftware.smackx.ox.element.PublicKeysListElement) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 27 with OpenPgpV4Fingerprint

use of org.pgpainless.key.OpenPgpV4Fingerprint in project Smack by igniterealtime.

the class PublicKeysListElementTest method listBuilderRefusesDuplicatesTest.

@Test
public void listBuilderRefusesDuplicatesTest() {
    PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
    String fp40 = "49545320414c4c2041424f555420444120484558";
    Date oneDate = new Date(12337883234L);
    Date otherDate = new Date(8888348384L);
    // Check if size of metadata is one after insert.
    builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(new OpenPgpV4Fingerprint(fp40), oneDate));
    assertEquals(builder.build().getMetadata().size(), 1);
    // Check if size is still one after inserting element with same fp.
    builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(new OpenPgpV4Fingerprint(fp40), otherDate));
    assertEquals(builder.build().getMetadata().size(), 1);
}
Also used : PublicKeysListElement(org.jivesoftware.smackx.ox.element.PublicKeysListElement) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 28 with OpenPgpV4Fingerprint

use of org.pgpainless.key.OpenPgpV4Fingerprint in project Smack by igniterealtime.

the class SecretKeyBackupHelper method createSecretkeyElement.

/**
 * Create a {@link SecretkeyElement} which contains the secret keys listed in {@code fingerprints} and is encrypted
 * symmetrically using the {@code backupCode}.
 *
 * @param provider {@link OpenPgpProvider} for symmetric encryption.
 * @param owner owner of the secret keys (usually our jid).
 * @param fingerprints set of {@link OpenPgpV4Fingerprint}s of the keys which are going to be backed up.
 * @param backupCode passphrase for symmetric encryption.
 * @return {@link SecretkeyElement}
 *
 * @throws PGPException PGP is brittle
 * @throws IOException IO is dangerous
 * @throws MissingOpenPgpKeyException in case one of the keys whose fingerprint is in {@code fingerprints} is
 * not accessible.
 */
public static SecretkeyElement createSecretkeyElement(OpenPgpProvider provider, BareJid owner, Set<OpenPgpV4Fingerprint> fingerprints, OpenPgpSecretKeyBackupPassphrase backupCode) throws PGPException, IOException, MissingOpenPgpKeyException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (OpenPgpV4Fingerprint fingerprint : fingerprints) {
        PGPSecretKeyRing key = provider.getStore().getSecretKeyRing(owner, fingerprint);
        if (key == null) {
            throw new MissingOpenPgpKeyException(owner, fingerprint);
        }
        byte[] bytes = key.getEncoded();
        buffer.write(bytes);
    }
    return createSecretkeyElement(buffer.toByteArray(), backupCode);
}
Also used : MissingOpenPgpKeyException(org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyException) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing)

Aggregations

OpenPgpV4Fingerprint (org.pgpainless.key.OpenPgpV4Fingerprint)28 Date (java.util.Date)11 PGPSecretKeyRing (org.bouncycastle.openpgp.PGPSecretKeyRing)11 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)8 Test (org.junit.Test)7 Test (org.junit.jupiter.api.Test)5 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 PGPPublicKeyRingCollection (org.bouncycastle.openpgp.PGPPublicKeyRingCollection)3 PainlessOpenPgpProvider (org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider)3 PublicKeysListElement (org.jivesoftware.smackx.ox.element.PublicKeysListElement)3 SecretkeyElement (org.jivesoftware.smackx.ox.element.SecretkeyElement)3 MissingUserIdOnKeyException (org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException)3 OpenPgpStore (org.jivesoftware.smackx.ox.store.definition.OpenPgpStore)3 FileBasedOpenPgpStore (org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore)3 KeyRingInfo (org.pgpainless.key.info.KeyRingInfo)3 UnprotectedKeysProtector (org.pgpainless.key.protection.UnprotectedKeysProtector)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2