Example usage for org.bouncycastle.bcpg S2K getProtectionMode

List of usage examples for org.bouncycastle.bcpg S2K getProtectionMode

Introduction

In this page you can find the example usage for org.bouncycastle.bcpg S2K getProtectionMode.

Prototype

public int getProtectionMode() 

Source Link

Document

Gets the protection mode - only if GNU_DUMMY_S2K

Usage

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

License:Open Source License

/** This method returns the SecretKeyType for this secret key, testing for an empty
 * passphrase in the process./*from ww  w.  ja v  a  2s .co m*/
 *
 * This method can potentially take a LONG time (i.e. seconds), so it should only
 * ever be called by {@link ProviderHelper} for the purpose of caching its output
 * in the database.
 */
public SecretKeyType getSecretKeyTypeSuperExpensive() {
    S2K s2k = mSecretKey.getS2K();
    if (s2k != null && s2k.getType() == S2K.GNU_DUMMY_S2K) {
        // divert to card is special
        if (s2k.getProtectionMode() == S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD) {
            return SecretKeyType.DIVERT_TO_CARD;
        }
        // no matter the exact protection mode, it's some kind of dummy key
        return SecretKeyType.GNU_DUMMY;
    }

    try {
        PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
                .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray());
        // If this doesn't throw
        mSecretKey.extractPrivateKey(keyDecryptor);
        // It means the passphrase is empty
        return SecretKeyType.PASSPHRASE_EMPTY;
    } catch (PGPException e) {
        HashMap<String, String> notation = getRing().getLocalNotationData();
        if (notation.containsKey("unlock.pin@sufficientlysecure.org")
                && "1".equals(notation.get("unlock.pin@sufficientlysecure.org"))) {
            return SecretKeyType.PIN;
        }
        // Otherwise, it's just a regular ol' passphrase
        return SecretKeyType.PASSPHRASE;
    }
}

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

License:Open Source License

/**
 * Returns true on right passphrase/*ww  w  .j a v a2 s  .  c o m*/
 */
public boolean unlock(final Passphrase passphrase) throws PgpGeneralException {
    // handle keys on OpenPGP cards like they were unlocked
    S2K s2k = mSecretKey.getS2K();
    if (s2k != null && s2k.getType() == S2K.GNU_DUMMY_S2K
            && s2k.getProtectionMode() == S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD) {
        mPrivateKeyState = PRIVATE_KEY_STATE_DIVERT_TO_CARD;
        return true;
    }

    // try to extract keys using the passphrase
    try {

        int keyEncryptionAlgorithm = mSecretKey.getKeyEncryptionAlgorithm();
        if (keyEncryptionAlgorithm == SymmetricKeyAlgorithmTags.NULL) {
            mPrivateKey = mSecretKey.extractPrivateKey(null);
            mPrivateKeyState = PRIVATE_KEY_STATE_UNLOCKED;
            return true;
        }

        byte[] sessionKey;
        sessionKey = passphrase.getCachedSessionKeyForParameters(keyEncryptionAlgorithm, s2k);
        if (sessionKey == null) {
            PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
                    .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(passphrase.getCharArray());
            // this operation is EXPENSIVE, so we cache its result in the passed Passphrase object!
            sessionKey = keyDecryptor.makeKeyFromPassPhrase(keyEncryptionAlgorithm, s2k);
            passphrase.addCachedSessionKeyForParameters(keyEncryptionAlgorithm, s2k, sessionKey);
        }

        PBESecretKeyDecryptor keyDecryptor = new SessionKeySecretKeyDecryptorBuilder()
                .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(sessionKey);
        mPrivateKey = mSecretKey.extractPrivateKey(keyDecryptor);
        mPrivateKeyState = PRIVATE_KEY_STATE_UNLOCKED;
    } catch (PGPException e) {
        return false;
    }
    if (mPrivateKey == null) {
        throw new PgpGeneralException("error extracting key");
    }
    return true;
}

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

License:Open Source License

static PGPSignatureGenerator getSignatureGenerator(PGPSecretKey secretKey, CryptoInputParcel cryptoInput) {

    S2K s2k = secretKey.getS2K();
    boolean isDivertToCard = s2k != null && s2k.getType() == S2K.GNU_DUMMY_S2K
            && s2k.getProtectionMode() == S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD;

    return getSignatureGenerator(secretKey.getPublicKey(), cryptoInput, isDivertToCard);
}

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

License:Open Source License

private static boolean isDummy(PGPSecretKey secretKey) {
    S2K s2k = secretKey.getS2K();
    return s2k != null && s2k.getType() == S2K.GNU_DUMMY_S2K
            && s2k.getProtectionMode() != S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD;
}

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

License:Open Source License

private static boolean isDivertToCard(PGPSecretKey secretKey) {
    S2K s2k = secretKey.getS2K();
    return s2k != null && s2k.getType() == S2K.GNU_DUMMY_S2K
            && s2k.getProtectionMode() == S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD;
}