Example usage for org.bouncycastle.openpgp PGPPublicKeyRing getEncoded

List of usage examples for org.bouncycastle.openpgp PGPPublicKeyRing getEncoded

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPPublicKeyRing getEncoded.

Prototype

public byte[] getEncoded() throws IOException 

Source Link

Usage

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);//  w w  w  . j  a  v a 2 s . c o  m
    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  ww  w  .ja  v a2  s  . 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.kontalk.certgen.X509Bridge.java

License:Open Source License

public static X509Certificate createCertificate(PGPPublicKeyRing publicKeyRing, PGPPrivateKey privateKey,
        String subjectAltName)/*w ww  . j a v  a 2 s .c o  m*/
        throws InvalidKeyException, IllegalStateException, NoSuchAlgorithmException, SignatureException,
        CertificateException, NoSuchProviderException, PGPException, IOException, OperatorCreationException {

    X500NameBuilder x500NameBuilder = new X500NameBuilder();

    /*
     * The X.509 Name to be the subject DN is prepared.
     * The CN is extracted from the Secret Key user ID.
     */

    x500NameBuilder.addRDN(BCStyle.O, DN_COMMON_PART_O);

    PGPPublicKey publicKey = publicKeyRing.getPublicKey();

    for (@SuppressWarnings("unchecked")
    Iterator<Object> it = publicKey.getUserIDs(); it.hasNext();) {
        Object attrib = it.next();
        x500NameBuilder.addRDN(BCStyle.CN, attrib.toString());
    }

    X500Name x509name = x500NameBuilder.build();

    /*
     * To check the signature from the certificate on the recipient side,
     * the creation time needs to be embedded in the certificate.
     * It seems natural to make this creation time be the "not-before"
     * date of the X.509 certificate.
     * Unlimited PGP keys have a validity of 0 second. In this case,
     * the "not-after" date will be the same as the not-before date.
     * This is something that needs to be checked by the service
     * receiving this certificate.
     */
    Date creationTime = publicKey.getCreationTime();
    Date validTo = null;
    if (publicKey.getValidSeconds() > 0)
        validTo = new Date(creationTime.getTime() + 1000L * publicKey.getValidSeconds());

    return createCertificate(PGP.convertPublicKey(publicKey), PGP.convertPrivateKey(privateKey), x509name,
            creationTime, validTo, subjectAltName, publicKeyRing.getEncoded());
}

From source file:org.kontalk.xmppserver.KontalkKeyring.java

License:Open Source License

public byte[] exportKey(String fingerprint) throws IOException, PGPException {
    PGPPublicKeyRing pk = keyring.getKey(fingerprint);
    return (pk != null) ? pk.getEncoded() : null;
}

From source file:org.kontalk.xmppserver.pgp.PGPLocalKeyring.java

License:Open Source License

private PGPPublicKeyRing importKey(PGPPublicKeyRing keyring) throws IOException, PGPException {
    String fpr = PGPUtils.getFingerprint(keyring);
    PGPPublicKeyRing newring;
    PGPPublicKeyRing oldring = getKey(fpr);
    if (oldring != null) {
        newring = PGPUtils.merge(oldring, keyring);
    } else {/*w  ww . j a  va  2  s. com*/
        newring = keyring;
    }

    db.set(fingerprintKey(fpr), newring.getEncoded());
    return newring;
}

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);/* w  ww  .  j  a va 2  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: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);//from w  ww.  j a  v a 2  s  .co m
    assertThat(Utils.toStream(loadRing.get(0).getKeySignatures())
            .filter(sig -> ((PGPSignature) sig).getKeyID() == priv.getKeyID()).findFirst()).isPresent();
}