List of usage examples for org.bouncycastle.openpgp PGPSecretKeyRing getEncoded
public byte[] getEncoded() throws IOException
From source file:org.jivesoftware.smackx.ox.OpenPgpStoreTest.java
License:Apache License
@Test public void t02_key_importKeysTest() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, MissingUserIdOnKeyException { // Test for nullity of all possible values. PGPKeyRing keys = openPgpStoreInstance1.generateKeyRing(alice); PGPSecretKeyRing secretKeys = keys.getSecretKeys(); PGPPublicKeyRing publicKeys = keys.getPublicKeys(); assertNotNull(secretKeys);// ww w .ja va2 s .c om assertNotNull(publicKeys); OpenPgpContact cAlice = openPgpStoreInstance1.getOpenPgpContact(alice); assertNull(cAlice.getAnyPublicKeys()); OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(publicKeys); assertEquals(fingerprint, new OpenPgpV4Fingerprint(secretKeys)); assertNull(openPgpStoreInstance1.getPublicKeysOf(alice)); assertNull(openPgpStoreInstance1.getSecretKeysOf(alice)); openPgpStoreInstance1.importPublicKey(alice, publicKeys); assertTrue( Arrays.equals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeysOf(alice).getEncoded())); assertNotNull(openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint)); assertNull(openPgpStoreInstance1.getSecretKeysOf(alice)); cAlice = openPgpStoreInstance1.getOpenPgpContact(alice); assertNotNull(cAlice.getAnyPublicKeys()); // Import keys a second time -> No change expected. openPgpStoreInstance1.importPublicKey(alice, publicKeys); assertTrue( Arrays.equals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeysOf(alice).getEncoded())); openPgpStoreInstance1.importSecretKey(alice, secretKeys); assertTrue( Arrays.equals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getEncoded())); openPgpStoreInstance1.importSecretKey(alice, secretKeys); assertNotNull(openPgpStoreInstance1.getSecretKeysOf(alice)); assertTrue( Arrays.equals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getEncoded())); assertNotNull(openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint)); assertTrue(Arrays.equals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint).getEncoded())); assertTrue(Arrays.equals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint).getEncoded())); // Clean up openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint); openPgpStoreInstance1.deleteSecretKeyRing(alice, fingerprint); }
From source file:org.jivesoftware.smackx.ox.OXSecretKeyBackupIntegrationTest.java
License:Apache License
@SmackIntegrationTest public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException, InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NotLoggedInException, SmackException.FeatureNotSupportedException, MissingUserIdOnKeyException, NoBackupFoundException, InvalidBackupCodeException, PGPException, MissingOpenPgpKeyException { OpenPgpStore beforeStore = new FileBasedOpenPgpStore(beforePath); beforeStore.setKeyRingProtector(new UnprotectedKeysProtector()); PainlessOpenPgpProvider beforeProvider = new PainlessOpenPgpProvider(aliceConnection, beforeStore); openPgpManager = OpenPgpManager.getInstanceFor(aliceConnection); openPgpManager.setOpenPgpProvider(beforeProvider); OpenPgpSelf self = openPgpManager.getOpenPgpSelf(); assertNull(self.getSigningKeyFingerprint()); OpenPgpV4Fingerprint keyFingerprint = openPgpManager.generateAndImportKeyPair(alice); assertEquals(keyFingerprint, self.getSigningKeyFingerprint()); assertTrue(self.getSecretKeys().contains(keyFingerprint.getKeyId())); PGPSecretKeyRing beforeSec = beforeStore.getSecretKeyRing(alice, keyFingerprint); assertNotNull(beforeSec);//from w w w . j a v a2s . c om PGPPublicKeyRing beforePub = beforeStore.getPublicKeyRing(alice, keyFingerprint); assertNotNull(beforePub); openPgpManager.backupSecretKeyToServer(new DisplayBackupCodeCallback() { @Override public void displayBackupCode(String backupCode) { OXSecretKeyBackupIntegrationTest.this.backupCode = backupCode; } }, new SecretKeyBackupSelectionCallback() { @Override public Set<OpenPgpV4Fingerprint> selectKeysToBackup(Set<OpenPgpV4Fingerprint> availableSecretKeys) { return availableSecretKeys; } }); FileBasedOpenPgpStore afterStore = new FileBasedOpenPgpStore(afterPath); afterStore.setKeyRingProtector(new UnprotectedKeysProtector()); PainlessOpenPgpProvider afterProvider = new PainlessOpenPgpProvider(aliceConnection, afterStore); openPgpManager.setOpenPgpProvider(afterProvider); OpenPgpV4Fingerprint fingerprint = openPgpManager .restoreSecretKeyServerBackup(new AskForBackupCodeCallback() { @Override public String askForBackupCode() { return backupCode; } }); assertEquals(keyFingerprint, fingerprint); assertTrue(self.getSecretKeys().contains(keyFingerprint.getKeyId())); assertEquals(keyFingerprint, self.getSigningKeyFingerprint()); PGPSecretKeyRing afterSec = afterStore.getSecretKeyRing(alice, keyFingerprint); assertNotNull(afterSec); assertTrue(Arrays.equals(beforeSec.getEncoded(), afterSec.getEncoded())); PGPPublicKeyRing afterPub = afterStore.getPublicKeyRing(alice, keyFingerprint); assertNotNull(afterPub); assertTrue(Arrays.equals(beforePub.getEncoded(), afterPub.getEncoded())); }
From source file:org.jivesoftware.smackx.ox.SecretKeyBackupHelperTest.java
License:Apache License
@Test public void createAndDecryptSecretKeyElementTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException, MissingOpenPgpKeyException, InvalidBackupCodeException { // Prepare store and provider and so on... FileBasedOpenPgpStore store = new FileBasedOpenPgpStore(basePath); PainlessOpenPgpProvider provider = new PainlessOpenPgpProvider(new DummyConnection(), store); // Generate and import key PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:alice@wonderland.lit"); BareJid jid = JidCreate.bareFrom("alice@wonderland.lit"); provider.getStore().importSecretKey(jid, keyRing.getSecretKeys()); // Create encrypted backup String backupCode = SecretKeyBackupHelper.generateBackupPassword(); SecretkeyElement element = SecretKeyBackupHelper.createSecretkeyElement(provider, jid, Collections.singleton(new OpenPgpV4Fingerprint(keyRing.getSecretKeys())), backupCode); // Decrypt backup and compare PGPSecretKeyRing secretKeyRing = SecretKeyBackupHelper.restoreSecretKeyBackup(element, backupCode); assertTrue(Arrays.equals(keyRing.getSecretKeys().getEncoded(), secretKeyRing.getEncoded())); }
From source file:org.jivesoftware.smackx.ox.util.SecretKeyBackupHelper.java
License:Apache License
/** * 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}//www . j a v a2 s . c om * * @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, String 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); }
From source file:ubicrypt.core.crypto.PGPECTest.java
License:Open Source License
@Test public void serializeKey() throws Exception { final char[] passPhrase = "pass".toCharArray(); final PGPSecretKeyRing secr = PGPEC.createSecretKeyRing(passPhrase); final PGPKeyPair keyPair = PGPEC.extractEncryptKeyPair(secr, passPhrase); final byte[] secbytes = secr.getEncoded(); final InputStream encbody = PGPEC.encrypt(Collections.singletonList(keyPair.getPublicKey()), new ByteArrayInputStream("ciao".getBytes())); final PGPSecretKeyRing secring = PGPEC.readSK(secbytes); assertThat(secbytes).isEqualTo(secring.getEncoded()); final byte[] clearBytes = "ciaoo".getBytes(); final PGPKeyPair privateKey = PGPEC.extractEncryptKeyPair(secring, passPhrase); assertThat(IOUtils.toByteArray(/*from w w w .j a v a 2 s . c o m*/ decrypt(privateKey.getPrivateKey(), encrypt(Collections.singletonList(privateKey.getPublicKey()), new ByteArrayInputStream(clearBytes))))).isEqualTo(clearBytes); assertThat(IOUtils.toByteArray(decrypt(privateKey.getPrivateKey(), encbody))).isEqualTo("ciao".getBytes()); }
From source file:ubicrypt.core.crypto.PGPECTest.java
License:Open Source License
@Test public void signPK() throws Exception { final char[] password = "ciao".toCharArray(); final PGPSecretKeyRing skr = PGPEC.createSecretKeyRing(password); final byte[] encSkr = skr.getEncoded(); final PGPKeyPair keyPair = PGPEC.extractEncryptKeyPair(PGPEC.readSK(new ByteArrayInputStream(encSkr)), "ciao".toCharArray()); final PGPKeyRingGenerator pkgen = PGPEC.keyRingGenerator(); final PGPSecretKeyRing targetSecRing = PGPEC.createSecretKeyRing("g".toCharArray()); final PGPPrivateKey priv = PGPEC.extractSignKey(targetSecRing, "g".toCharArray()); final PGPPublicKeyRing pkr = PGPPublicKeyRing.insertPublicKey(pkgen.generatePublicKeyRing(), PGPEC.signPK(keyPair.getPublicKey(), priv)); final byte[] pkis = pkr.getEncoded(); final List<PGPPublicKey> loadRing = PGPEC.readPKring(new ByteArrayInputStream(pkis)); assertThat(loadRing).hasSize(1);/*w w w . j av a 2 s .c om*/ assertThat(Utils.toStream(loadRing.get(0).getKeySignatures()) .filter(sig -> ((PGPSignature) sig).getKeyID() == priv.getKeyID()).findFirst()).isPresent(); }
From source file:ubicrypt.core.FixPassPhraseInitializer.java
License:Open Source License
private Tuple2<PGPPrivateKey, PGPKeyPair> readOrCreate() { try {//from w w w . j a v a 2 s.c om final Path secFile = securityFile(); final PGPKeyPair keyPair; final PGPSecretKeyRing kring; final PGPPrivateKey signkey; if (secFile.toFile().exists()) { kring = PGPEC.readSK(Files.newInputStream(secFile)); keyPair = PGPEC.extractEncryptKeyPair(kring, password); signkey = PGPEC.extractSignKey(kring, password); } else { kring = PGPEC.createSecretKeyRing(password); keyPair = PGPEC.extractEncryptKeyPair(kring, password); signkey = PGPEC.extractSignKey(kring, password); write(secFile, new ByteArrayInputStream(kring.getEncoded())) .doOnError(err -> log.error(err.getMessage(), err)).subscribe(); } return Tuple.of(signkey, keyPair); } catch (final Exception e) { Throwables.propagate(e); } return null; }