Example usage for org.bouncycastle.openpgp PGPSecretKey isSigningKey

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

Introduction

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

Prototype

public boolean isSigningKey() 

Source Link

Document

Return true if this key has an algorithm type that makes it suitable to use for signing.

Usage

From source file:alpha.offsync.security.OpenPGPSecurityUtility.java

License:Apache License

/**
 * Gets the correct signing key from local secret keyring using the supplied
 * key information./*w w w .  j  a  v a2  s . c om*/
 * 
 * @param keyInfo
 *            the supplied key information
 * @return the correct signing key
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws PGPException
 *             thrown if an error is encountered
 */
public PGPSecretKey getSignKey(final String keyInfo) throws IOException, PGPException {
    final PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(new FileInputStream(this.secretKeyRing)));

    final Iterator keyRingIter = pgpSec.getKeyRings();
    while (keyRingIter.hasNext()) {
        final PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();

        final Iterator keyIter = keyRing.getSecretKeys();
        while (keyIter.hasNext()) {
            final PGPSecretKey key = (PGPSecretKey) keyIter.next();

            final Iterator idIter = key.getUserIDs();
            while (idIter.hasNext()) {
                final String userID = idIter.next().toString();
                if (userID.contains(keyInfo) && key.isSigningKey())
                    return key;
            }

        }
    }

    return null;
}

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

/**
 * Finds the first key in secretKeyRings which is capable of signing and which corresponds with a key in keyRing.
 * @param keyRing//from www  .ja  v  a 2s  .  c o  m
 * @param secretKeyRings
 * @return
 * @throws PGPException 
 */
public PGPSecretKey findFirstSigningKey(PGPPublicKeyRing keyRing, PGPSecretKeyRingCollection secretKeyRings)
        throws PGPException {
    @SuppressWarnings("unchecked")
    Iterator<PGPPublicKey> kIt = keyRing.getPublicKeys();

    PGPSecretKey retval = null;
    while (retval == null && kIt.hasNext()) {
        PGPPublicKey k = kIt.next();
        PGPSecretKey sk = secretKeyRings.getSecretKey(k.getKeyID());
        if (sk.isSigningKey()) {
            retval = sk;
        }
    }

    if (retval == null) {
        throw new PGPException("No signing key found");
    }

    return retval;
}

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

/**
 * Load a secret key ring collection from keyIn and find the secret key corresponding to
 * keyID if it exists.//from w ww .j av  a 2s  . co  m
 *
 * @param keyIn input stream representing a key ring collection.
 * @param keyID keyID we want.
 * @param signing indicates whether looking for a signing key.
 * @return
 * @throws IOException
 * @throws PGPException
 * @throws NoSuchProviderException
 */
public PGPSecretKey findSecretKey(PGPSecretKeyRingCollection secRing, long keyID, boolean signing)
        throws IOException, PGPException, NoSuchProviderException {
    PGPSecretKey pgpSecKey = secRing.getSecretKey(keyID);

    if (pgpSecKey != null) {
        if (signing && !pgpSecKey.isSigningKey()) {
            throw new PGPException("Key is not a signing key");
        }
    } else {
        throw new PGPException("Can't find secret key in key ring");
    }

    return pgpSecKey;
}

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

/**
 * A simple routine that opens a key ring file and finds the first available
 * key suitable for signature generation.
 * /*from  w ww  .j av  a  2s  . c  o m*/
 * @param in
 * @return
 * @throws IOException
 * @throws PGPException
 */
private static PGPSecretKey findSigningKey(PGPSecretKeyRingCollection secRing)
        throws IOException, PGPException {
    //
    // We just loop through the collection till we find a key suitable for encryption.
    //
    PGPSecretKey key = null;

    @SuppressWarnings("unchecked")
    Iterator<PGPSecretKeyRing> rIt = secRing.getKeyRings();

    while (key == null && rIt.hasNext()) {
        PGPSecretKeyRing kRing = rIt.next();
        @SuppressWarnings("unchecked")
        Iterator<PGPSecretKey> kIt = kRing.getSecretKeys();

        while (key == null && kIt.hasNext()) {
            PGPSecretKey k = (PGPSecretKey) kIt.next();
            if (k.isSigningKey()) {
                key = k;
            }
        }
    }

    if (key == null) {
        throw new PGPException("Can't find a signing key in the key ring");
    }

    return key;
}

From source file:com.github.sannies.nexusaptplugin.sign.PGPSigner.java

License:Apache License

/**
 * Returns the secret key matching the specified identifier.
 * /*  w ww .j  a  v a 2 s.c o  m*/
 * @param input the input stream containing the keyring collection
 * @param keyId the 4 bytes identifier of the key
 */
private PGPSecretKey getSecretKey(InputStream input, String keyId) throws IOException, PGPException {
    PGPSecretKeyRingCollection keyrings = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));

    Iterator rIt = keyrings.getKeyRings();

    while (rIt.hasNext()) {
        PGPSecretKeyRing kRing = (PGPSecretKeyRing) rIt.next();
        Iterator kIt = kRing.getSecretKeys();

        while (kIt.hasNext()) {
            PGPSecretKey key = (PGPSecretKey) kIt.next();

            if (key.isSigningKey()
                    && Long.toHexString(key.getKeyID() & 0xFFFFFFFFL).equals(keyId.toLowerCase())) {
                return key;
            }
        }
    }

    return null;
}

From source file:com.navnorth.learningregistry.LRSigner.java

License:Apache License

/**
 * Reads private key from the provided InputStream
 *
 * @param input InputStream of the private key
 * @return PGPSecretKey//from  w ww. j a  va  2 s.  com
 * @throws LRException NO_KEY error if the key cannot be obtained from the input stream
 */
private PGPSecretKey readSecretKey(InputStream input) throws LRException {
    PGPSecretKeyRingCollection pgpSec;

    try {
        pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));
    } catch (Exception e) {
        throw new LRException(LRException.NO_KEY);
    }

    java.util.Iterator keyRingIter = pgpSec.getKeyRings();
    while (keyRingIter.hasNext()) {
        PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();

        java.util.Iterator keyIter = keyRing.getSecretKeys();
        while (keyIter.hasNext()) {
            PGPSecretKey key = (PGPSecretKey) keyIter.next();

            if (key.isSigningKey()) {
                return key;
            }
        }
    }

    throw new LRException(LRException.NO_KEY);
}

From source file:com.verhas.licensor.License.java

License:Open Source License

private boolean keyIsAppropriate(String userId, String keyUserId, PGPSecretKey k) {
    return k.isSigningKey() && (userId == null || userId.equals(keyUserId));
}

From source file:com.zwitserloot.ivyplusplus.mavencentral.CreateDetachedSignatures_.java

License:Open Source License

PGPSecretKey getSigningKey(InputStream keyData, String streamName)
        throws IOException, PGPException, SigningException {
    PGPSecretKeyRingCollection keyrings_ = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyData));
    Iterator<?> keyrings = keyrings_.getKeyRings();
    while (keyrings.hasNext()) {
        PGPSecretKeyRing keys_ = (PGPSecretKeyRing) keyrings.next();
        Iterator<?> keys = keys_.getSecretKeys();
        while (keys.hasNext()) {
            PGPSecretKey key = (PGPSecretKey) keys.next();
            if (key.isSigningKey())
                return key;
        }/*from   w ww .j a va  2  s .c om*/
    }

    throw new SigningException("No signing key found in keyring: " + streamName);
}

From source file:crypttools.PGPCryptoBC.java

License:Open Source License

/**
* <p>Return the first suitable key for encryption in the key ring
* collection. For this case we only expect there to be one key
* available for signing.</p>//w  w  w .  j  a  va2  s.com
* 
* @param input - the input stream of the key PGP Key Ring
* @return the first suitable PGP Secret Key found for signing
* @throws IOException
* @throws PGPException
*/
@SuppressWarnings("unchecked")
private static PGPSecretKey readSecretKey(InputStream input) throws IOException, PGPException {
    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));
    Iterator<PGPSecretKeyRing> iter = pgpSec.getKeyRings();
    PGPSecretKey secKey = null;

    while (iter.hasNext() && secKey == null) {
        PGPSecretKeyRing keyRing = iter.next();
        Iterator<PGPSecretKey> keyIter = keyRing.getSecretKeys();

        while (keyIter.hasNext()) {
            PGPSecretKey key = keyIter.next();
            if (key.isSigningKey()) {
                secKey = key;
                break;
            }
        }
    }

    if (secKey != null) {
        return secKey;
    } else {
        throw new IllegalArgumentException("Can't find signing key in key ring.");
    }
}

From source file:de.dentrassi.pm.signing.pgp.PgpHelper.java

License:Open Source License

public static PGPSecretKey loadSecretKey(final InputStream input, final String keyId)
        throws IOException, PGPException {
    final long keyIdNum = Long.parseUnsignedLong(keyId, 16);

    final BcPGPSecretKeyRingCollection keyrings = new BcPGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(input));

    final Iterator<?> keyRingIter = keyrings.getKeyRings();
    while (keyRingIter.hasNext()) {
        final PGPSecretKeyRing secretKeyRing = (PGPSecretKeyRing) keyRingIter.next();

        final Iterator<?> secretKeyIterator = secretKeyRing.getSecretKeys();
        while (secretKeyIterator.hasNext()) {
            final PGPSecretKey key = (PGPSecretKey) secretKeyIterator.next();

            if (!key.isSigningKey()) {
                continue;
            }//from  www .jav  a2  s . c o m

            final long shortId = key.getKeyID() & 0xFFFFFFFFL;

            if (key.getKeyID() != keyIdNum && shortId != keyIdNum) {
                continue;
            }

            return key;
        }
    }

    return null;
}