Example usage for org.bouncycastle.openpgp PGPSignatureGenerator setHashedSubpackets

List of usage examples for org.bouncycastle.openpgp PGPSignatureGenerator setHashedSubpackets

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPSignatureGenerator setHashedSubpackets.

Prototype

public void setHashedSubpackets(PGPSignatureSubpacketVector hashedPcks) 

Source Link

Usage

From source file:org.sufficientlysecure.keychain.pgp.PgpKeyOperation.java

License:Open Source License

private static PGPSignature generateRevocationSignature(PGPSignatureGenerator sGen, Date creationTime,
        PGPPrivateKey masterPrivateKey, PGPPublicKey pKey, String userId)

        throws IOException, PGPException, SignatureException {
    PGPSignatureSubpacketGenerator subHashedPacketsGen = new PGPSignatureSubpacketGenerator();
    // we use the tag NO_REASON since gnupg does not care about the tag while verifying
    // signatures with a revoked key, the warning is the same
    subHashedPacketsGen.setRevocationReason(true, RevocationReasonTags.NO_REASON, "");
    subHashedPacketsGen.setSignatureCreationTime(true, creationTime);
    sGen.setHashedSubpackets(subHashedPacketsGen.generate());
    sGen.init(PGPSignature.CERTIFICATION_REVOCATION, masterPrivateKey);
    return sGen.generateCertification(userId, pKey);
}

From source file:org.sufficientlysecure.keychain.pgp.PgpKeyOperation.java

License:Open Source License

private static PGPSignature generateRevocationSignature(PGPSignatureGenerator sGen, Date creationTime,
        PGPPublicKey masterPublicKey, PGPPrivateKey masterPrivateKey, PGPPublicKey pKey)
        throws IOException, PGPException, SignatureException {

    PGPSignatureSubpacketGenerator subHashedPacketsGen = new PGPSignatureSubpacketGenerator();
    // we use the tag NO_REASON since gnupg does not care about the tag while verifying
    // signatures with a revoked key, the warning is the same
    subHashedPacketsGen.setRevocationReason(true, RevocationReasonTags.NO_REASON, "");
    subHashedPacketsGen.setSignatureCreationTime(true, creationTime);
    sGen.setHashedSubpackets(subHashedPacketsGen.generate());
    // Generate key revocation or subkey revocation, depending on master/subkey-ness
    if (masterPublicKey.getKeyID() == pKey.getKeyID()) {
        sGen.init(PGPSignature.KEY_REVOCATION, masterPrivateKey);
        return sGen.generateCertification(masterPublicKey);
    } else {/*  www. ja v a2 s.  c  o m*/
        sGen.init(PGPSignature.SUBKEY_REVOCATION, masterPrivateKey);
        return sGen.generateCertification(masterPublicKey, pKey);
    }
}

From source file:org.sufficientlysecure.keychain.pgp.PgpKeyOperation.java

License:Open Source License

static PGPSignature generateSubkeyBindingSignature(PGPSignatureGenerator sGen, Date creationTime,
        PGPPublicKey masterPublicKey, PGPPrivateKey masterPrivateKey, PGPSignatureGenerator subSigGen,
        PGPPrivateKey subPrivateKey, PGPPublicKey pKey, int flags, long expiry)
        throws IOException, PGPException, SignatureException {

    PGPSignatureSubpacketGenerator unhashedPacketsGen = new PGPSignatureSubpacketGenerator();

    // If this key can sign, we need a primary key binding signature
    if ((flags & KeyFlags.SIGN_DATA) > 0) {
        // cross-certify signing keys
        PGPSignatureSubpacketGenerator subHashedPacketsGen = new PGPSignatureSubpacketGenerator();
        subHashedPacketsGen.setSignatureCreationTime(false, creationTime);
        subSigGen.init(PGPSignature.PRIMARYKEY_BINDING, subPrivateKey);
        subSigGen.setHashedSubpackets(subHashedPacketsGen.generate());
        PGPSignature certification = subSigGen.generateCertification(masterPublicKey, pKey);
        unhashedPacketsGen.setEmbeddedSignature(true, certification);
    }//from  w w w. j  a  va 2  s.c  o m

    PGPSignatureSubpacketGenerator hashedPacketsGen;
    {
        hashedPacketsGen = new PGPSignatureSubpacketGenerator();
        hashedPacketsGen.setSignatureCreationTime(true, creationTime);
        hashedPacketsGen.setKeyFlags(true, flags);
        if (expiry > 0) {
            hashedPacketsGen.setKeyExpirationTime(true, expiry - pKey.getCreationTime().getTime() / 1000);
        }
    }

    sGen.init(PGPSignature.SUBKEY_BINDING, masterPrivateKey);
    sGen.setHashedSubpackets(hashedPacketsGen.generate());
    sGen.setUnhashedSubpackets(unhashedPacketsGen.generate());

    return sGen.generateCertification(masterPublicKey, pKey);

}

From source file:org.sufficientlysecure.keychain.pgp.UncachedKeyRing.java

License:Open Source License

@VisibleForTesting
public static UncachedKeyRing forTestingOnlyAddDummyLocalSignature(UncachedKeyRing uncachedKeyRing,
        String passphrase) throws Exception {
    PGPSecretKeyRing sKR = (PGPSecretKeyRing) uncachedKeyRing.mRing;

    PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
            .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(passphrase.toCharArray());
    PGPPrivateKey masterPrivateKey = sKR.getSecretKey().extractPrivateKey(keyDecryptor);
    PGPPublicKey masterPublicKey = uncachedKeyRing.mRing.getPublicKey();

    // add packet with "pin" notation data
    PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder(
            masterPrivateKey.getPublicKeyPacket().getAlgorithm(),
            PgpSecurityConstants.SECRET_KEY_BINDING_SIGNATURE_HASH_ALGO)
                    .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);
    PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder);
    { // set subpackets
        PGPSignatureSubpacketGenerator hashedPacketsGen = new PGPSignatureSubpacketGenerator();
        hashedPacketsGen.setExportable(false, false);
        hashedPacketsGen.setNotationData(false, true, "dummynotationdata", "some data");
        sGen.setHashedSubpackets(hashedPacketsGen.generate());
    }/*from w  w w . j a  va2  s  .co  m*/
    sGen.init(PGPSignature.DIRECT_KEY, masterPrivateKey);
    PGPSignature emptySig = sGen.generateCertification(masterPublicKey);

    masterPublicKey = PGPPublicKey.addCertification(masterPublicKey, emptySig);
    sKR = PGPSecretKeyRing.insertSecretKey(sKR,
            PGPSecretKey.replacePublicKey(sKR.getSecretKey(), masterPublicKey));

    return new UncachedKeyRing(sKR);
}

From source file:org.sufficientlysecure.keychain.pgp.UncachedKeyringCanonicalizeTest.java

License:Open Source License

private static PGPSignature forgeSignature(PGPSecretKey key, int type,
        PGPSignatureSubpacketGenerator subpackets, PGPPublicKey publicKey) throws Exception {

    PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
            .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray());
    PGPPrivateKey privateKey = key.extractPrivateKey(keyDecryptor);

    PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder(publicKey.getAlgorithm(),
            PGPUtil.SHA1).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);

    PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder);
    sGen.setHashedSubpackets(subpackets.generate());
    sGen.init(type, privateKey);//from  www.j  av  a 2  s  .  c  om
    return sGen.generateCertification(publicKey);

}

From source file:org.sufficientlysecure.keychain.pgp.UncachedKeyringCanonicalizeTest.java

License:Open Source License

private static PGPSignature forgeSignature(PGPSecretKey key, int type,
        PGPSignatureSubpacketGenerator subpackets, String userId, PGPPublicKey publicKey) throws Exception {

    PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
            .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray());
    PGPPrivateKey privateKey = key.extractPrivateKey(keyDecryptor);

    PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder(publicKey.getAlgorithm(),
            PGPUtil.SHA1).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);

    PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder);
    sGen.setHashedSubpackets(subpackets.generate());
    sGen.init(type, privateKey);/*  w  w  w.  ja  v a 2 s . c o m*/
    return sGen.generateCertification(userId, publicKey);

}

From source file:org.sufficientlysecure.keychain.pgp.UncachedKeyringCanonicalizeTest.java

License:Open Source License

private static PGPSignature forgeSignature(PGPSecretKey key, int type,
        PGPSignatureSubpacketGenerator subpackets, PGPPublicKey publicKey, PGPPublicKey signedKey)
        throws Exception {

    PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
            .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray());
    PGPPrivateKey privateKey = key.extractPrivateKey(keyDecryptor);

    PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder(publicKey.getAlgorithm(),
            PGPUtil.SHA1).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);

    PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder);
    sGen.setHashedSubpackets(subpackets.generate());
    sGen.init(type, privateKey);/* www .j  a  va2 s  . c  o  m*/
    return sGen.generateCertification(publicKey, signedKey);

}

From source file:org.sufficientlysecure.keychain.pgp.UncachedKeyringCanonicalizeTest.java

License:Open Source License

private static PGPSignature forgeSignature(PGPSecretKey key, int type,
        PGPSignatureSubpacketGenerator hashedSubs, PGPSignatureSubpacketGenerator unhashedSubs,
        PGPPublicKey publicKey, PGPPublicKey signedKey) throws Exception {

    PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
            .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray());
    PGPPrivateKey privateKey = key.extractPrivateKey(keyDecryptor);

    PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder(publicKey.getAlgorithm(),
            PGPUtil.SHA1).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);

    PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder);
    sGen.setHashedSubpackets(hashedSubs.generate());
    sGen.setUnhashedSubpackets(unhashedSubs.generate());
    sGen.init(type, privateKey);/*from  w w w  . ja  v  a 2 s  .c o m*/
    return sGen.generateCertification(publicKey, signedKey);

}

From source file:ubicrypt.core.crypto.PGPEC.java

License:Open Source License

public static PGPPublicKey signPK(final PGPPublicKey pk, final PGPPrivateKey priv) {
    final PGPSignatureGenerator sGen = new PGPSignatureGenerator(
            new JcaPGPContentSignerBuilder(PGPPublicKey.ECDSA, PGPUtil.SHA256).setProvider("BC"));

    try {//from  ww w . jav a2s. c  o  m
        sGen.init(PGPSignature.DIRECT_KEY, priv);
    } catch (final PGPException e) {
        Throwables.propagate(e);
    }

    final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

    final PGPSignatureSubpacketVector packetVector = spGen.generate();

    sGen.setHashedSubpackets(packetVector);

    try {
        return PGPPublicKey.addCertification(pk, sGen.generateCertification("id", pk));
    } catch (final PGPException e) {
        Throwables.propagate(e);
    }
    return null;
}