Search in sources :

Example 36 with KeyPair

use of java.security.KeyPair in project android_frameworks_base by AOSPA.

the class AndroidKeyPairGeneratorTest method testKeyPairGenerator_GenerateKeyPair_EC_P521_Unencrypted_Success.

public void testKeyPairGenerator_GenerateKeyPair_EC_P521_Unencrypted_Success() throws Exception {
    mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()).setAlias(TEST_ALIAS_1).setKeyType("EC").setKeySize(521).setSubject(TEST_DN_1).setSerialNumber(TEST_SERIAL_1).setStartDate(NOW).setEndDate(NOW_PLUS_10_YEARS).build());
    final KeyPair pair = mGenerator.generateKeyPair();
    assertNotNull("The KeyPair returned should not be null", pair);
    assertKeyPairCorrect(pair, TEST_ALIAS_1, "EC", 521, null, TEST_DN_1, TEST_SERIAL_1, NOW, NOW_PLUS_10_YEARS);
}
Also used : KeyPairGeneratorSpec(android.security.KeyPairGeneratorSpec) KeyPair(java.security.KeyPair)

Example 37 with KeyPair

use of java.security.KeyPair in project android_frameworks_base by AOSPA.

the class AndroidKeyPairGeneratorTest method testKeyPairGenerator_Legacy_GenerateKeyPair_EC_Unencrypted_Success.

public void testKeyPairGenerator_Legacy_GenerateKeyPair_EC_Unencrypted_Success() throws Exception {
    mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()).setAlias(TEST_ALIAS_1).setKeyType("EC").setSubject(TEST_DN_1).setSerialNumber(TEST_SERIAL_1).setStartDate(NOW).setEndDate(NOW_PLUS_10_YEARS).build());
    final KeyPair pair = mGenerator.generateKeyPair();
    assertNotNull("The KeyPair returned should not be null", pair);
    assertKeyPairCorrect(pair, TEST_ALIAS_1, "EC", 256, null, TEST_DN_1, TEST_SERIAL_1, NOW, NOW_PLUS_10_YEARS);
}
Also used : KeyPair(java.security.KeyPair)

Example 38 with KeyPair

use of java.security.KeyPair in project android_frameworks_base by AOSPA.

the class AndroidKeyPairGeneratorTest method testKeyPairGenerator_GenerateKeyPair_RSA_Unencrypted_Success.

public void testKeyPairGenerator_GenerateKeyPair_RSA_Unencrypted_Success() throws Exception {
    mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()).setAlias(TEST_ALIAS_1).setSubject(TEST_DN_1).setSerialNumber(TEST_SERIAL_1).setStartDate(NOW).setEndDate(NOW_PLUS_10_YEARS).build());
    final KeyPair pair = mGenerator.generateKeyPair();
    assertNotNull("The KeyPair returned should not be null", pair);
    assertKeyPairCorrect(pair, TEST_ALIAS_1, "RSA", 2048, null, TEST_DN_1, TEST_SERIAL_1, NOW, NOW_PLUS_10_YEARS);
}
Also used : KeyPairGeneratorSpec(android.security.KeyPairGeneratorSpec) KeyPair(java.security.KeyPair)

Example 39 with KeyPair

use of java.security.KeyPair in project android_frameworks_base by AOSPA.

the class AndroidKeyStoreKeyPairGeneratorSpi method generateKeyPair.

@Override
public KeyPair generateKeyPair() {
    if (mKeyStore == null || mSpec == null) {
        throw new IllegalStateException("Not initialized");
    }
    final int flags = (mEncryptionAtRestRequired) ? KeyStore.FLAG_ENCRYPTED : 0;
    if (((flags & KeyStore.FLAG_ENCRYPTED) != 0) && (mKeyStore.state() != KeyStore.State.UNLOCKED)) {
        throw new IllegalStateException("Encryption at rest using secure lock screen credential requested for key pair" + ", but the user has not yet entered the credential");
    }
    byte[] additionalEntropy = KeyStoreCryptoOperationUtils.getRandomBytesToMixIntoKeystoreRng(mRng, (mKeySizeBits + 7) / 8);
    Credentials.deleteAllTypesForAlias(mKeyStore, mEntryAlias, mEntryUid);
    final String privateKeyAlias = Credentials.USER_PRIVATE_KEY + mEntryAlias;
    boolean success = false;
    try {
        generateKeystoreKeyPair(privateKeyAlias, constructKeyGenerationArguments(), additionalEntropy, flags);
        KeyPair keyPair = loadKeystoreKeyPair(privateKeyAlias);
        storeCertificateChain(flags, createCertificateChain(privateKeyAlias, keyPair));
        success = true;
        return keyPair;
    } finally {
        if (!success) {
            Credentials.deleteAllTypesForAlias(mKeyStore, mEntryAlias, mEntryUid);
        }
    }
}
Also used : KeyPair(java.security.KeyPair) DERBitString(com.android.org.bouncycastle.asn1.DERBitString)

Example 40 with KeyPair

use of java.security.KeyPair in project GNS by MobilityFirst.

the class KeyPairUtils method copyKeyPair.

/**
   * A useful tool when you want to use an existing guid with another gns server.
   *
   * @param sourceGNSName
   * @param username
   * @param destGNSName
   */
public static void copyKeyPair(String sourceGNSName, String username, String destGNSName) {
    GuidEntry entry = getGuidEntry(sourceGNSName, username);
    saveKeyPair(destGNSName, entry.getEntityName(), entry.getGuid(), new KeyPair(entry.getPublicKey(), entry.getPrivateKey()));
}
Also used : KeyPair(java.security.KeyPair)

Aggregations

KeyPair (java.security.KeyPair)903 KeyPairGenerator (java.security.KeyPairGenerator)345 Test (org.junit.Test)235 PrivateKey (java.security.PrivateKey)189 X509Certificate (java.security.cert.X509Certificate)185 PublicKey (java.security.PublicKey)167 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)127 IOException (java.io.IOException)121 BigInteger (java.math.BigInteger)87 Date (java.util.Date)78 SecureRandom (java.security.SecureRandom)75 KeyStore (java.security.KeyStore)74 GeneralSecurityException (java.security.GeneralSecurityException)63 RSAPublicKey (java.security.interfaces.RSAPublicKey)55 X500Principal (javax.security.auth.x500.X500Principal)53 File (java.io.File)52 KeyFactory (java.security.KeyFactory)52 ECPrivateKey (java.security.interfaces.ECPrivateKey)52 ECPublicKey (java.security.interfaces.ECPublicKey)52 InvalidKeyException (java.security.InvalidKeyException)40