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;
}
});
}
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);
}
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);
}
}
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);
}
}
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);
}
Aggregations