Java Code Examples for org.bouncycastle.openpgp.PGPPublicKeyRing#getPublicKeys()
The following examples show how to use
org.bouncycastle.openpgp.PGPPublicKeyRing#getPublicKeys() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PGPKeyUtil.java From peer-os with Apache License 2.0 | 6 votes |
public static PGPPublicKey readPublicKey( PGPPublicKeyRing keyRing ) throws PGPException { try { Iterator keyIter = keyRing.getPublicKeys(); while ( keyIter.hasNext() ) { PGPPublicKey key = ( PGPPublicKey ) keyIter.next(); if ( key.isEncryptionKey() ) { return key; } } } catch ( Exception e ) { LOG.error( e.getMessage() ); } return null; }
Example 2
Source File: OpenPGPKeyBasedEncryptor.java From localization_nifi with Apache License 2.0 | 5 votes |
@SuppressWarnings("rawtypes") public static PGPPublicKey getPublicKey(String userId, String publicKeyringFile) throws IOException, PGPException { // TODO: Reevaluate the mechanism for executing this task as performance can suffer here and only a specific key needs to be validated // Read in from the public keyring file try (FileInputStream keyInputStream = new FileInputStream(publicKeyringFile)) { // Form the PublicKeyRing collection (1.53 way with fingerprint calculator) PGPPublicKeyRingCollection pgpPublicKeyRingCollection = new PGPPublicKeyRingCollection(keyInputStream, new BcKeyFingerprintCalculator()); // Iterate over all public keyrings Iterator<PGPPublicKeyRing> iter = pgpPublicKeyRingCollection.getKeyRings(); PGPPublicKeyRing keyRing; while (iter.hasNext()) { keyRing = iter.next(); // Iterate over each public key in this keyring Iterator<PGPPublicKey> keyIter = keyRing.getPublicKeys(); while (keyIter.hasNext()) { PGPPublicKey publicKey = keyIter.next(); // Iterate over each userId attached to the public key Iterator userIdIterator = publicKey.getUserIDs(); while (userIdIterator.hasNext()) { String id = (String) userIdIterator.next(); if (userId.equalsIgnoreCase(id)) { return publicKey; } } } } } // If this point is reached, no public key could be extracted with the given userId throw new PGPException("Could not find a public key with the given userId"); }
Example 3
Source File: PGPKeyHelper.java From peer-os with Apache License 2.0 | 5 votes |
public static PGPPublicKey readPublicKey( InputStream is ) throws IOException, PGPException { PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( is ), new JcaKeyFingerprintCalculator() ); Iterator keyRingIter = pgpPub.getKeyRings(); while ( keyRingIter.hasNext() ) { PGPPublicKeyRing keyRing = ( PGPPublicKeyRing ) keyRingIter.next(); Iterator keyIter = keyRing.getPublicKeys(); while ( keyIter.hasNext() ) { PGPPublicKey key = ( PGPPublicKey ) keyIter.next(); if ( key.isEncryptionKey() ) { return key; } } } throw new IllegalArgumentException( "Can't find encryption key in key ring." ); }
Example 4
Source File: PGPEncryptionUtilTest.java From peer-os with Apache License 2.0 | 5 votes |
@Test public void testExtractingContentFromClearSign() { PGPPublicKey key = null; try { InputStream in = findFile( PLUGIN_PRIVATE_KEY ); in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream( in ); JcaPGPPublicKeyRingCollection pgpPub = new JcaPGPPublicKeyRingCollection( in ); in.close(); Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings(); while ( key == null && rIt.hasNext() ) { PGPPublicKeyRing kRing = rIt.next(); Iterator<PGPPublicKey> kIt = kRing.getPublicKeys(); while ( key == null && kIt.hasNext() ) { PGPPublicKey k = kIt.next(); if ( k.isEncryptionKey() ) { key = k; } } } } catch ( Exception e ) { e.printStackTrace(); } }
Example 5
Source File: PgpHelper.java From nomulus with Apache License 2.0 | 5 votes |
/** * Return appropriate key or subkey for given task from public key. * * <p>Weirder older PGP public keys will actually have multiple keys. The main key will usually * be sign-only in such situations. So you've gotta go digging in through the key packets and * make sure you get the one that's valid for encryption, or whatever you want to do. */ public static Optional<PGPPublicKey> lookupPublicSubkey( PGPPublicKeyRing ring, KeyRequirement want) { Iterator<PGPPublicKey> keys = ring.getPublicKeys(); while (keys.hasNext()) { PGPPublicKey key = keys.next(); switch (want) { case ENCRYPT: if (key.isEncryptionKey()) { return Optional.of(key); } break; case SIGN: if (isSigningKey(key)) { return Optional.of(key); } break; case ENCRYPT_SIGN: if (key.isEncryptionKey() && isSigningKey(key)) { return Optional.of(key); } break; default: throw new AssertionError(); } } return Optional.empty(); }
Example 6
Source File: OpenPGPKeyBasedEncryptor.java From nifi with Apache License 2.0 | 5 votes |
@SuppressWarnings("rawtypes") public static PGPPublicKey getPublicKey(String userId, String publicKeyringFile) throws IOException, PGPException { // TODO: Reevaluate the mechanism for executing this task as performance can suffer here and only a specific key needs to be validated // Read in from the public keyring file try (FileInputStream keyInputStream = new FileInputStream(publicKeyringFile)) { // Form the PublicKeyRing collection (1.53 way with fingerprint calculator) PGPPublicKeyRingCollection pgpPublicKeyRingCollection = new PGPPublicKeyRingCollection(keyInputStream, new BcKeyFingerprintCalculator()); // Iterate over all public keyrings Iterator<PGPPublicKeyRing> iter = pgpPublicKeyRingCollection.getKeyRings(); PGPPublicKeyRing keyRing; while (iter.hasNext()) { keyRing = iter.next(); // Iterate over each public key in this keyring Iterator<PGPPublicKey> keyIter = keyRing.getPublicKeys(); while (keyIter.hasNext()) { PGPPublicKey publicKey = keyIter.next(); // Iterate over each userId attached to the public key Iterator userIdIterator = publicKey.getUserIDs(); while (userIdIterator.hasNext()) { String id = (String) userIdIterator.next(); if (userId.equalsIgnoreCase(id)) { return publicKey; } } } } } // If this point is reached, no public key could be extracted with the given userId throw new PGPException("Could not find a public key with the given userId"); }
Example 7
Source File: ExpirationExtractor.java From keywhiz with Apache License 2.0 | 4 votes |
@Nullable public static Instant expirationFromOpenPGP(byte[] content) { JcaPGPPublicKeyRingCollection collection; try { collection = new JcaPGPPublicKeyRingCollection(new ByteArrayInputStream(content)); } catch (IOException | PGPException e) { // Unable to parse logger.info("Failed to parse OpenPGP keyring", e); return null; } Instant earliest = null; // Iterate over all key rings in file Iterator rings = collection.getKeyRings(); while (rings.hasNext()) { Object ringItem = rings.next(); if (ringItem instanceof PGPPublicKeyRing) { PGPPublicKeyRing ring = (PGPPublicKeyRing) ringItem; // Iterate over all keys in ring Iterator keys = ring.getPublicKeys(); while (keys.hasNext()) { Object keyItem = keys.next(); if (keyItem instanceof PGPPublicKey) { PGPPublicKey key = (PGPPublicKey) keyItem; // Get validity for key (zero means no expiry) long validSeconds = key.getValidSeconds(); if (validSeconds > 0) { Instant expiry = key.getCreationTime().toInstant().plusSeconds(validSeconds); if (earliest == null || expiry.isBefore(earliest)) { earliest = expiry; } } } } } } return earliest; }
Example 8
Source File: PGPUtils.java From desktopclient-java with GNU General Public License v3.0 | 4 votes |
/** * Read a public key from key ring byte data. */ public static Optional<PGPCoderKey> readPublicKey(byte[] publicKeyring) { PGPPublicKey encryptKey = null; PGPPublicKey signKey = null; // for legacy keyring PGPPublicKey authKey = null; String uid = null; String fp = null; PGPPublicKeyRing keyRing = keyRingOrNull(publicKeyring); if (keyRing == null) return Optional.empty(); Iterator<PGPPublicKey> keyIter = keyRing.getPublicKeys(); while (keyIter.hasNext()) { PGPPublicKey key = keyIter.next(); if (key.isMasterKey()) { authKey = key; fp = Hex.toHexString(key.getFingerprint()); Iterator<?> uidIt = key.getUserIDs(); if (uidIt.hasNext()) uid = (String) uidIt.next(); // TODO if more than one UID? } else if (isSigningKey(key)) { signKey = key; } else if (key.isEncryptionKey()) { encryptKey = key; } } // legacy: auth key is actually signing key if (signKey == null && authKey != null) { LOGGER.info("loading legacy public key, uid: "+uid); signKey = authKey; } if (encryptKey == null || signKey == null || uid == null) { LOGGER.warning("can't find public keys in key ring, uid: "+uid); return Optional.empty(); } return Optional.of(new PGPCoderKey(encryptKey, signKey, uid, fp, publicKeyring)); }