Example usage for org.bouncycastle.openpgp PGPSecretKey isMasterKey

List of usage examples for org.bouncycastle.openpgp PGPSecretKey isMasterKey

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPSecretKey isMasterKey.

Prototype

public boolean isMasterKey() 

Source Link

Document

Return true if this is a master key.

Usage

From source file:de.softwareforge.pgpsigner.key.SecretKeyRing.java

License:Apache License

public void load(final String ringFileName) throws IOException, PGPException {
    File ringFile = new File(ringFileName);

    if (!ringFile.exists() || !ringFile.isFile()) {
        throw new IOException("Ring file " + ringFileName + " is not a file!");
    }//from   w  w w. ja v a 2  s . c  o m

    clear();
    setRingFileName(ringFileName);

    PGPSecretKeyRingCollection secretRing = new PGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(new FileInputStream(ringFile)));

    for (Iterator ringIt = secretRing.getKeyRings(); ringIt.hasNext();) {
        PGPSecretKeyRing keyRing = (PGPSecretKeyRing) ringIt.next();

        for (Iterator it = keyRing.getSecretKeys(); it.hasNext();) {
            PGPSecretKey secretKey = (PGPSecretKey) it.next();

            if (secretKey.isMasterKey()) {
                SecretKey secKey = new SecretKey(secretKey);
                keys.put(secKey.getKeyId(), secKey);
            }
        }
    }
}

From source file:dorkbox.util.crypto.CryptoPGP.java

License:Apache License

/**
 * Creates the signature that will be used to PGP sign data
 *
 * @param secretKeys// w ww  . j  a  v a  2  s .  c om
 *                 these are the secret keys
 * @param password
 *                 this is the password to unlock the secret key
 *
 * @return the signature used to sign data
 *
 * @throws PGPException
 */
private static PGPSignatureGenerator createSignature(List<PGPSecretKey> secretKeys, char[] password,
        int signatureType, boolean generateUserIdSubPacket) throws PGPException {

    PGPSecretKey secretKey = null;
    for (int i = 0; i < secretKeys.size(); i++) {
        secretKey = secretKeys.get(i);

        // we ONLY want the signing master key
        if (!secretKey.isSigningKey() || !secretKey.isMasterKey()) {
            secretKey = null;
        }
    }

    if (secretKey == null) {
        throw new PGPException("Secret key is not the signing master key");
    }

    //            System.err.println("Signing key = " + tmpKey.isSigningKey() +", Master key = " + tmpKey.isMasterKey() + ", UserId = " +
    //                               userId );

    if (password == null) {
        password = new char[0];
    }

    PBESecretKeyDecryptor build = new BcPBESecretKeyDecryptorBuilder(digestCalculatorProvider).build(password);

    SecureRandom random = new SecureRandom();
    BcPGPContentSignerBuilder bcPGPContentSignerBuilder = new BcPGPContentSignerBuilder(
            secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1).setSecureRandom(random);

    PGPSignatureGenerator signature = new PGPSignatureGenerator(bcPGPContentSignerBuilder);
    signature.init(signatureType, secretKey.extractPrivateKey(build));

    Iterator userIds = secretKey.getPublicKey().getUserIDs();

    // use the first userId that matches
    if (userIds.hasNext()) {
        if (generateUserIdSubPacket) {
            PGPSignatureSubpacketGenerator subpacketGenerator = new PGPSignatureSubpacketGenerator();
            subpacketGenerator.setSignerUserID(false, (String) userIds.next());
            signature.setHashedSubpackets(subpacketGenerator.generate());
        } else {
            signature.setHashedSubpackets(null);
        }

        return signature;
    } else {
        throw new PGPException("Did not find specified userId");
    }
}

From source file:dorkbox.util.crypto.CryptoPGP.java

License:Apache License

/**
 * Get the first decryption key from the given keyring.
 *//*from  w  w w .j  a  v a2 s.  co m*/
public PGPSecretKey getDecryptionKey(PGPSecretKeyRing keyRing) {
    if (keyRing == null) {
        return null;
    }

    // iterate over the keys on the ring, look for one which is suitable for encryption.
    Iterator keys = keyRing.getSecretKeys();
    PGPSecretKey key;
    while (keys.hasNext()) {
        key = (PGPSecretKey) keys.next();
        if (key.isMasterKey()) {
            return key;
        }
    }

    return null;
}

From source file:org.eclipse.packagedrone.repo.signing.pgp.internal.managed.Entry.java

License:Open Source License

private void processKey(final List<ManagedKey> keys, final PGPSecretKey key) throws Exception {
    final String keyId = String.format("%016X", key.getKeyID());

    @SuppressWarnings("unchecked")
    final Stream<?> s = StreamSupport
            .stream(Spliterators.spliteratorUnknownSize(key.getUserIDs(), Spliterator.ORDERED), false);
    final List<String> users = s.map(Object::toString).collect(Collectors.toList());

    final int bits = key.getPublicKey().getBitStrength();

    final ManagedKey mkey = new ManagedKey(keyId, users, !key.isMasterKey(), bits);
    keys.add(mkey);// w ww .  jav  a 2s. com

    registerKey(key, users);
}

From source file:org.eclipse.packagedrone.repo.signing.pgp.internal.managed.Entry.java

License:Open Source License

protected void registerKey(final PGPSecretKey key, final List<String> users) throws Exception {
    final String keyId = String.format("%016X", key.getKeyID());

    final SigningService service = new ManagedSigningService(key, this.cfg.getPassphrase());
    final Dictionary<String, Object> properties = new Hashtable<>(1);
    properties.put(Constants.SERVICE_PID, "pgp." + keyId);

    final String usersString = users.stream().collect(Collectors.joining("; "));

    if (!users.isEmpty()) {
        properties.put(Constants.SERVICE_DESCRIPTION, String.format("Managed PGP key (%s) %s: %s", keyId,
                !key.isMasterKey() ? "(sub)" : "", usersString));
    } else {//from  w  w  w  .ja v a2  s. c  om
        properties.put(Constants.SERVICE_DESCRIPTION,
                String.format("Managed PGP key (%s) %s", keyId, !key.isMasterKey() ? "(sub)" : ""));
    }

    this.regs.add(this.context.registerService(SigningService.class, service, properties));
}

From source file:org.kontalk.certgen.PGP.java

License:Open Source License

@SuppressWarnings("unchecked")
public static PrivateKey convertPrivateKey(byte[] privateKeyData, String passphrase)
        throws PGPException, IOException {

    PGPDigestCalculatorProvider sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build();
    PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(sha1Calc).setProvider(PGP.PROVIDER)
            .build(passphrase.toCharArray());

    // load the secret key ring
    KeyFingerPrintCalculator fpr = new BcKeyFingerprintCalculator();
    PGPSecretKeyRing secRing = new PGPSecretKeyRing(privateKeyData, fpr);

    // search and decrypt the master (signing key)
    // secret keys
    Iterator<PGPSecretKey> skeys = secRing.getSecretKeys();
    while (skeys.hasNext()) {
        PGPSecretKey key = skeys.next();
        PGPSecretKey sec = secRing.getSecretKey();

        if (key.isMasterKey())
            return convertPrivateKey(sec.extractPrivateKey(decryptor));
    }/* w ww.  j  a  v  a2s. c  o  m*/

    throw new PGPException("no suitable private key found.");
}