List of usage examples for org.bouncycastle.openpgp PGPKeyPair getKeyID
public long getKeyID()
From source file:google.registry.keyring.api.InMemoryKeyring.java
License:Open Source License
public InMemoryKeyring(PGPKeyPair rdeStagingKey, PGPKeyPair rdeSigningKey, PGPPublicKey rdeReceiverKey, PGPKeyPair brdaSigningKey, PGPPublicKey brdaEncryptionKey, String rdeSshClientPublicKey, String rdeSshClientPrivateKey, String icannReportingPassword, String marksdbDnlLogin, String marksdbLordnPassword, String marksdbSmdrlLogin, String jsonCredential, String braintreePrivateKey) { checkArgument(PgpHelper.isSigningKey(rdeSigningKey.getPublicKey()), "RDE signing key must support signing: %s", rdeSigningKey.getKeyID()); checkArgument(rdeStagingKey.getPublicKey().isEncryptionKey(), "staging key must support encryption: %s", rdeStagingKey.getKeyID());/*from ww w.ja v a2 s . c o m*/ checkArgument(rdeReceiverKey.isEncryptionKey(), "receiver key must support encryption: %s", rdeReceiverKey.getKeyID()); checkArgument(PgpHelper.isSigningKey(brdaSigningKey.getPublicKey()), "BRDA signing key must support signing: %s", brdaSigningKey.getKeyID()); checkArgument(brdaEncryptionKey.isEncryptionKey(), "encryption key must support encryption: %s", brdaEncryptionKey.getKeyID()); this.rdeStagingKey = rdeStagingKey; this.rdeSigningKey = rdeSigningKey; this.rdeReceiverKey = rdeReceiverKey; this.brdaSigningKey = brdaSigningKey; this.brdaEncryptionKey = brdaEncryptionKey; this.rdeSshClientPublicKey = checkNotNull(rdeSshClientPublicKey, "rdeSshClientPublicKey"); this.rdeSshClientPrivateKey = checkNotNull(rdeSshClientPrivateKey, "rdeSshClientPrivateKey"); this.icannReportingPassword = checkNotNull(icannReportingPassword, "icannReportingPassword"); this.marksdbDnlLogin = checkNotNull(marksdbDnlLogin, "marksdbDnlLogin"); this.marksdbLordnPassword = checkNotNull(marksdbLordnPassword, "marksdbLordnPassword"); this.marksdbSmdrlLogin = checkNotNull(marksdbSmdrlLogin, "marksdbSmdrlLogin"); this.jsonCredential = checkNotNull(jsonCredential, "jsonCredential"); this.braintreePrivateKey = checkNotNull(braintreePrivateKey, "braintreePrivateKey"); }
From source file:google.registry.keyring.kms.KmsKeyringTest.java
License:Open Source License
@Test public void test_getRdeSigningKey() throws Exception { saveKeyPairSecret("rde-signing-public", "rde-signing-private"); PGPKeyPair rdeSigningKey = keyring.getRdeSigningKey(); assertThat(rdeSigningKey.getKeyID()).isEqualTo(KmsTestHelper.getPublicKeyring().getPublicKey().getKeyID()); }
From source file:google.registry.keyring.kms.KmsKeyringTest.java
License:Open Source License
@Test public void test_getBrdaSigningKey() throws Exception { saveKeyPairSecret("brda-signing-public", "brda-signing-private"); PGPKeyPair brdaSigningKey = keyring.getBrdaSigningKey(); assertThat(brdaSigningKey.getKeyID()) .isEqualTo(KmsTestHelper.getPrivateKeyring().getPublicKey().getKeyID()); }
From source file:google.registry.rde.GhostrydeTest.java
License:Open Source License
@Test @Ignore("Intentionally corrupting a PGP key is easier said than done >_>") public void testFailure_keyCorruption() throws Exception { RdeKeyringModule keyringModule = new RdeKeyringModule(); byte[] data = "Fanatics have their dreams, wherewith they weave.".getBytes(UTF_8); DateTime mtime = DateTime.parse("1984-12-18T00:30:00Z"); PGPKeyPair rsa = keyringModule.get("rde-unittest@registry.test", ENCRYPT); PGPPublicKey publicKey = rsa.getPublicKey(); // Make the last byte of the private key off by one. muahahaha byte[] keyData = rsa.getPrivateKey().getPrivateKeyDataPacket().getEncoded(); keyData[keyData.length - 1]++;/*from w w w. j a v a2 s . com*/ PGPPrivateKey privateKey = new PGPPrivateKey(rsa.getKeyID(), rsa.getPrivateKey().getPublicKeyPacket(), rsa.getPrivateKey().getPrivateKeyDataPacket()); Ghostryde ghost = new Ghostryde(1024); ByteArrayOutputStream bsOut = new ByteArrayOutputStream(); try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey); Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor); OutputStream output = ghost.openOutput(kompressor, "lol", mtime)) { output.write(data); } ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray()); try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) { ByteStreams.copy(decryptor, ByteStreams.nullOutputStream()); } }
From source file:ubicrypt.core.crypto.PGPECTest.java
License:Open Source License
@Test public void serializePKring() throws Exception { final PGPKeyPair sign = PGPEC.masterKey(); final PGPKeyPair enc = PGPEC.encryptionKey(); final char[] passPhrase = "g".toCharArray(); final PGPKeyPair newKeyPair = PGPEC.extractEncryptKeyPair(PGPEC.createSecretKeyRing(passPhrase), passPhrase);/*from www. ja v a2 s .c o m*/ final PGPKeyPair newKeyPair2 = PGPEC.extractEncryptKeyPair(PGPEC.createSecretKeyRing(passPhrase), passPhrase); final PGPPublicKeyRing kring = PGPPublicKeyRing .insertPublicKey(PGPEC.keyRingGenerator(sign).generatePublicKeyRing(), newKeyPair.getPublicKey()); List<PGPPublicKey> pks = PGPEC.readPKring(new ByteArrayInputStream(kring.getEncoded())); assertThat(pks).hasSize(1); assertThat(pks.get(0).getKeyID()).isEqualTo(newKeyPair.getKeyID()); pks = PGPEC.readPKring(new ByteArrayInputStream( PGPPublicKeyRing.insertPublicKey(kring, newKeyPair2.getPublicKey()).getEncoded())); assertThat(pks).hasSize(2); assertThat(pks.stream().map(PGPPublicKey::getKeyID).collect(Collectors.toList())) .contains(newKeyPair.getKeyID(), newKeyPair2.getKeyID()); }
From source file:uk.co.platosys.dinigma.LockSmith.java
License:Open Source License
/** * * @param keyDirectory the directory in which the private key is to be saved. This could be on a removable drive. * @param lockDirectory the directory in which the Lock(the public key) is to be saved. * @param userName//from w w w . java 2s .co m * @param passPhrase * @return The key_id of the signing key. * @throws MinigmaException */ public static long createLockset(File keyDirectory, File lockDirectory, String userName, char[] passPhrase) throws MinigmaException, DuplicateNameException { String filename; File lockFile; File keyFile; //test that parameters have been set: if (keyDirectory == null) { throw new MinigmaException("Locksmith - key directory is null"); } if (!keyDirectory.isDirectory()) { throw new MinigmaException("Locksmith: " + keyDirectory.toString() + " is not a directory"); } if (!keyDirectory.canWrite()) { throw new MinigmaException("Locksmith: can't write to " + keyDirectory.toString()); } if (lockDirectory == null) { throw new MinigmaException("Locksmith - lock directory is null"); } if (!lockDirectory.isDirectory()) { throw new MinigmaException("Locksmith: " + keyDirectory.toString() + " is not a directory"); } if (!lockDirectory.canWrite()) { throw new MinigmaException("Locksmith: can't write to " + keyDirectory.toString()); } try { if (Security.getProvider(PROVIDER) == null) { Security.addProvider(new BouncyCastleProvider()); } } catch (Exception e) { throw new MinigmaException("Locksmith: problem adding security provider", e); } // KeyPairGenerator generator; KeyPair dsaKeyPair; KeyPair elgKeyPair; BigInteger g; BigInteger p; File lockFolder; File keyFolder; PGPKeyPair pgpSigKeyPair; PGPKeyPair pgpEncKeyPair; PGPKeyRingGenerator pgpKeyRingGenerator; PGPPublicKeyRing pgpPublicKeyRing; PGPSecretKeyRing pgpSecretKeyRing; try { //the DSA key for signing generator = KeyPairGenerator.getInstance(SIGNATURE_ALGORITHM, PROVIDER); generator.initialize(1024); dsaKeyPair = generator.generateKeyPair(); } catch (Exception e) { throw new MinigmaException("Locksmith: failed to generate dsa key pair", e); } try { //the strong ElGamal key for encrypting generator = KeyPairGenerator.getInstance(ASYMMETRIC_ALGORITHM, PROVIDER); g = new BigInteger( "153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16); p = new BigInteger( "9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16); AlgorithmParameterSpec elGamalParameters = new ElGamalParameterSpec(p, g); generator.initialize(elGamalParameters); elgKeyPair = generator.generateKeyPair(); } catch (Exception e) { throw new MinigmaException("Locksmith: failed to generate elgamal key pair", e); } try { filename = FileTools.removeFunnyCharacters(userName); lockFolder = new File(lockDirectory, Minigma.LOCK_DIRNAME); if (!lockFolder.exists()) { if (!lockFolder.mkdirs()) { throw new MinigmaException("Can't create lock folder"); } } lockFile = new File(lockFolder, filename); if (lockFile.exists()) { throw new DuplicateNameException("lockfile with name " + lockFile.getName() + " already exists"); } keyFolder = new File(keyDirectory, Minigma.KEY_DIRNAME); if (!keyFolder.exists()) { if (!keyFolder.mkdirs()) { throw new MinigmaException("Can't create key folder"); } } keyFile = new File(keyFolder, filename); if (lockFile.exists()) { throw new DuplicateNameException("keyfile with name " + keyFile.getName() + " already exists"); } } catch (Exception exc) { throw new MinigmaException("Locksmith: error setting up key files", exc); } try { pgpSigKeyPair = new JcaPGPKeyPair(PGPPublicKey.DSA, dsaKeyPair, new Date()); } catch (Exception e) { throw new MinigmaException("Locksmith: failed to generate pgp-dsa key pair", e); } try { pgpEncKeyPair = new JcaPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKeyPair, new Date()); } catch (Exception e) { throw new MinigmaException("Locksmith: failed to generate pgp-elgamal key pair", e); } PGPDigestCalculator pgpDigestCalculator = null; PGPContentSignerBuilder pgpContentSignerBuilder = null; PBESecretKeyEncryptor pbeSecretKeyEncryptor = null; try { pgpDigestCalculator = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1); pgpContentSignerBuilder = new JcaPGPContentSignerBuilder(SIGNATURE_ALGORITHM_TAG, HashAlgorithmTags.SHA512); pbeSecretKeyEncryptor = new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, pgpDigestCalculator).setProvider(PROVIDER).build(passPhrase); } catch (Exception e) { throw new MinigmaException("failed to initialise KRG components", e); } try { pgpKeyRingGenerator = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, //certification level pgpSigKeyPair, //master key userName, // id pgpDigestCalculator, //PGPDigestCalculator null, //PGPSignatureSubpacketsVector hashed packets null, //PGPSignatureSubpacketsVector unhashed packets pgpContentSignerBuilder, //PGPContentSignerBuilder pbeSecretKeyEncryptor//PBESecretKeyEncryptor ); } catch (PGPException e) { throw new MinigmaException("Locksmith: failed to create PGP-keyring generator", e); } try { pgpKeyRingGenerator.addSubKey(pgpEncKeyPair); } catch (Exception e) { throw new MinigmaException("Locksmith: failed to add elgamal subkey to ring", e); } try { ArmoredOutputStream secOut = new ArmoredOutputStream(new FileOutputStream(keyFile)); pgpSecretKeyRing = pgpKeyRingGenerator.generateSecretKeyRing(); pgpSecretKeyRing.encode(secOut); secOut.close(); } catch (Exception e) { throw new MinigmaException("Locksmith: failed to encode secret key output", e); } try { ArmoredOutputStream pubOut = new ArmoredOutputStream(new FileOutputStream(lockFile)); pgpPublicKeyRing = pgpKeyRingGenerator.generatePublicKeyRing(); pgpPublicKeyRing.encode(pubOut); pubOut.close(); } catch (Exception e) { throw new MinigmaException("Locksmith: failed to encode pubring output", e); } return pgpSigKeyPair.getKeyID(); }