Search in sources :

Example 11 with OpenPgpV4Fingerprint

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

the class OpenPgpStoreTest method t00_store_protectorGetSet.

/*
    Generic
     */
@Test
public void t00_store_protectorGetSet() {
    openPgpStoreInstance1.setKeyRingProtector(new UnprotectedKeysProtector());
    assertNotNull(openPgpStoreInstance1.getKeyRingProtector());
    // TODO: Test method below
    openPgpStoreInstance1.setSecretKeyPassphraseCallback(new SecretKeyPassphraseCallback() {

        @Override
        public Passphrase onPassphraseNeeded(OpenPgpV4Fingerprint fingerprint) {
            return null;
        }
    });
}
Also used : UnprotectedKeysProtector(org.pgpainless.key.protection.UnprotectedKeysProtector) SecretKeyPassphraseCallback(org.jivesoftware.smackx.ox.callback.SecretKeyPassphraseCallback) Passphrase(org.pgpainless.util.Passphrase) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) Test(org.junit.Test)

Example 12 with OpenPgpV4Fingerprint

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

the class OpenPgpStoreTest method t07_multipleKeysTest.

@Test
public void t07_multipleKeysTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
    PGPSecretKeyRing one = openPgpStoreInstance1.generateKeyRing(alice);
    PGPSecretKeyRing two = openPgpStoreInstance1.generateKeyRing(alice);
    OpenPgpV4Fingerprint fingerprint1 = new OpenPgpV4Fingerprint(one);
    OpenPgpV4Fingerprint fingerprint2 = new OpenPgpV4Fingerprint(two);
    openPgpStoreInstance1.importSecretKey(alice, one);
    openPgpStoreInstance1.importSecretKey(alice, two);
    openPgpStoreInstance1.importPublicKey(alice, KeyRingUtils.publicKeyRingFrom(one));
    openPgpStoreInstance1.importPublicKey(alice, KeyRingUtils.publicKeyRingFrom(two));
    assertArrayEquals(one.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint1).getEncoded());
    assertArrayEquals(two.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint2).getEncoded());
    assertArrayEquals(one.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getSecretKeyRing(fingerprint1.getKeyId()).getEncoded());
    assertArrayEquals(KeyRingUtils.publicKeyRingFrom(one).getEncoded(), openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint1).getEncoded());
    // Cleanup
    openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint1);
    openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint2);
    openPgpStoreInstance1.deleteSecretKeyRing(alice, fingerprint1);
    openPgpStoreInstance1.deleteSecretKeyRing(alice, fingerprint2);
}
Also used : OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) Test(org.junit.Test)

Example 13 with OpenPgpV4Fingerprint

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

the class FileBasedOpenPgpMetadataStore method readFingerprintsAndDates.

static Map<OpenPgpV4Fingerprint, Date> readFingerprintsAndDates(File source) throws IOException {
    // TODO: Why do we not throw a FileNotFoundException here?
    if (!source.exists() || source.isDirectory()) {
        return new HashMap<>();
    }
    BufferedReader reader = null;
    try {
        InputStream inputStream = FileUtils.prepareFileInputStream(source);
        InputStreamReader isr = new InputStreamReader(inputStream, Util.UTF8);
        reader = new BufferedReader(isr);
        Map<OpenPgpV4Fingerprint, Date> fingerprintDateMap = new HashMap<>();
        String line;
        int lineNr = 0;
        while ((line = reader.readLine()) != null) {
            lineNr++;
            line = line.trim();
            String[] split = line.split(" ");
            if (split.length != 2) {
                LOGGER.log(Level.FINE, "Skipping invalid line " + lineNr + " in file " + source.getAbsolutePath());
                continue;
            }
            try {
                OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(split[0]);
                Date date = XmppDateTime.parseXEP0082Date(split[1]);
                fingerprintDateMap.put(fingerprint, date);
            } catch (IllegalArgumentException | ParseException e) {
                LOGGER.log(Level.WARNING, "Error parsing fingerprint/date touple in line " + lineNr + " of file " + source.getAbsolutePath(), e);
            }
        }
        return fingerprintDateMap;
    } finally {
        CloseableUtil.maybeClose(reader, LOGGER);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Date(java.util.Date) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) BufferedReader(java.io.BufferedReader) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) ParseException(java.text.ParseException)

Example 14 with OpenPgpV4Fingerprint

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

the class FileBasedOpenPgpTrustStore method readTrust.

@Override
protected Trust readTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException {
    File file = getTrustPath(owner, fingerprint);
    BufferedReader reader = null;
    try {
        InputStream inputStream = FileUtils.prepareFileInputStream(file);
        InputStreamReader isr = new InputStreamReader(inputStream, Util.UTF8);
        reader = new BufferedReader(isr);
        Trust trust = null;
        String line;
        int lineNr = 0;
        while ((line = reader.readLine()) != null) {
            lineNr++;
            try {
                trust = Trust.valueOf(line);
                break;
            } catch (IllegalArgumentException e) {
                LOGGER.log(Level.WARNING, "Skipping invalid trust record in line " + lineNr + " \"" + line + "\" of file " + file.getAbsolutePath());
            }
        }
        return trust != null ? trust : Trust.undecided;
    } catch (IOException e) {
        if (e instanceof FileNotFoundException) {
            return Trust.undecided;
        }
        throw e;
    } finally {
        CloseableUtil.maybeClose(reader, LOGGER);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint)

Example 15 with OpenPgpV4Fingerprint

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

the class AbstractOpenPgpKeyStore method importSecretKey.

@Override
public void importSecretKey(BareJid owner, PGPSecretKeyRing secretKeys) throws IOException, PGPException, MissingUserIdOnKeyException {
    if (!new KeyRingInfo(secretKeys).isUserIdValid("xmpp:" + owner.toString())) {
        throw new MissingUserIdOnKeyException(owner, new OpenPgpV4Fingerprint(secretKeys));
    }
    PGPSecretKeyRingCollection secretKeyRings = getSecretKeysOf(owner);
    try {
        if (secretKeyRings != null) {
            secretKeyRings = PGPSecretKeyRingCollection.addSecretKeyRing(secretKeyRings, secretKeys);
        } else {
            secretKeyRings = new PGPSecretKeyRingCollection(Collections.singleton(secretKeys));
        }
    } catch (IllegalArgumentException e) {
        LOGGER.log(Level.INFO, "Skipping secret key ring " + Long.toHexString(secretKeys.getPublicKey().getKeyID()) + " as it is already in the key ring of " + owner.toString());
    }
    this.secretKeyRingCollections.put(owner, secretKeyRings);
    writeSecretKeysOf(owner, secretKeyRings);
}
Also used : KeyRingInfo(org.pgpainless.key.info.KeyRingInfo) PGPSecretKeyRingCollection(org.bouncycastle.openpgp.PGPSecretKeyRingCollection) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) MissingUserIdOnKeyException(org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException)

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