Search in sources :

Example 1 with KeyInfo

use of android.security.keystore.KeyInfo in project krypton-android by kryptco.

the class RSASSHKeyPair method isKeyStoredInSecureHardware.

public boolean isKeyStoredInSecureHardware() {
    try {
        KeyInfo keyInfo;
        KeyFactory factory = KeyFactory.getInstance(keyPair.getPrivate().getAlgorithm(), "AndroidKeyStore");
        keyInfo = factory.getKeySpec(keyPair.getPrivate(), KeyInfo.class);
        return keyInfo.isInsideSecureHardware();
    } catch (InvalidKeySpecException e) {
        // Not an Android KeyStore key.
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return false;
}
Also used : KeyInfo(android.security.keystore.KeyInfo) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) KeyFactory(java.security.KeyFactory)

Example 2 with KeyInfo

use of android.security.keystore.KeyInfo in project krypton-android by kryptco.

the class RSASSHKeyPair method getSignerAndPrepareData.

public Pair<Signature, byte[]> getSignerAndPrepareData(String digest, byte[] data) throws CryptoException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    KeyFactory factory = KeyFactory.getInstance(keyPair.getPrivate().getAlgorithm(), "AndroidKeyStore");
    KeyInfo keyInfo;
    keyInfo = factory.getKeySpec(keyPair.getPrivate(), KeyInfo.class);
    Signature signer;
    if (Arrays.asList(keyInfo.getDigests()).contains(digest)) {
        switch(digest) {
            case KeyProperties.DIGEST_SHA1:
                signer = Signature.getInstance("SHA1withRSA");
                break;
            case KeyProperties.DIGEST_SHA256:
                signer = Signature.getInstance("SHA256withRSA");
                break;
            case KeyProperties.DIGEST_SHA512:
                signer = Signature.getInstance("SHA512withRSA");
                break;
            default:
                throw new CryptoException("Unsupported digest: " + digest);
        }
    } else {
        // fall back to NONEwithRSA for backwards compatibility
        signer = Signature.getInstance("NONEwithRSA");
        switch(digest) {
            case KeyProperties.DIGEST_SHA1:
                data = SHA1.digestPrependingOID(data);
                break;
            case KeyProperties.DIGEST_SHA256:
                data = SHA256.digestPrependingOID(data);
                break;
            case KeyProperties.DIGEST_SHA512:
                data = SHA512.digestPrependingOID(data);
                break;
            default:
                throw new CryptoException("Unsupported digest: " + digest);
        }
    }
    return new Pair<>(signer, data);
}
Also used : KeyInfo(android.security.keystore.KeyInfo) Signature(java.security.Signature) RSASignature(co.krypt.krypton.pgp.packet.RSASignature) CryptoException(co.krypt.krypton.exception.CryptoException) KeyFactory(java.security.KeyFactory) KeyPair(java.security.KeyPair) Pair(android.support.v4.util.Pair)

Example 3 with KeyInfo

use of android.security.keystore.KeyInfo in project krypton-android by kryptco.

the class RSAKeyManager method logKeyInfo.

public void logKeyInfo(PrivateKey sk) {
    try {
        KeyInfo keyInfo;
        KeyFactory factory = KeyFactory.getInstance(sk.getAlgorithm(), "AndroidKeyStore");
        keyInfo = factory.getKeySpec(sk, KeyInfo.class);
        Log.i(LOG_TAG, String.valueOf(keyInfo.isInsideSecureHardware()));
        Log.i(LOG_TAG, String.valueOf(keyInfo.isUserAuthenticationRequired()));
        Log.i(LOG_TAG, String.valueOf(keyInfo.isUserAuthenticationRequirementEnforcedBySecureHardware()));
    } catch (InvalidKeySpecException e) {
        // Not an Android KeyStore key.
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
}
Also used : KeyInfo(android.security.keystore.KeyInfo) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) KeyFactory(java.security.KeyFactory)

Aggregations

KeyInfo (android.security.keystore.KeyInfo)3 KeyFactory (java.security.KeyFactory)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 Pair (android.support.v4.util.Pair)1 CryptoException (co.krypt.krypton.exception.CryptoException)1 RSASignature (co.krypt.krypton.pgp.packet.RSASignature)1 KeyPair (java.security.KeyPair)1 Signature (java.security.Signature)1