diff options
Diffstat (limited to 'net/android/keystore_unittest.cc')
-rw-r--r-- | net/android/keystore_unittest.cc | 76 |
1 files changed, 35 insertions, 41 deletions
diff --git a/net/android/keystore_unittest.cc b/net/android/keystore_unittest.cc index ff20435..076693f 100644 --- a/net/android/keystore_unittest.cc +++ b/net/android/keystore_unittest.cc @@ -25,6 +25,7 @@ #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" #include "crypto/openssl_util.h" +#include "crypto/scoped_openssl_types.h" #include "jni/AndroidKeyStoreTestUtil_jni.h" #include "net/android/keystore.h" #include "net/android/keystore_openssl.h" @@ -56,15 +57,9 @@ namespace android { namespace { -typedef crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ScopedEVP_PKEY; -typedef crypto::ScopedOpenSSL<RSA, RSA_free> ScopedRSA; -typedef crypto::ScopedOpenSSL<DSA, DSA_free> ScopedDSA; -typedef crypto::ScopedOpenSSL<EC_KEY, EC_KEY_free> ScopedEC_KEY; -typedef crypto::ScopedOpenSSL<BIGNUM, BN_free> ScopedBIGNUM; - -typedef crypto::ScopedOpenSSL< - PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> - ScopedPKCS8_PRIV_KEY_INFO; +typedef crypto::ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, + PKCS8_PRIV_KEY_INFO_free>::Type + ScopedPKCS8_PRIV_KEY_INFO; typedef base::android::ScopedJavaLocalRef<jobject> ScopedJava; @@ -136,7 +131,7 @@ EVP_PKEY* ImportPrivateKeyFile(const char* filename) { // |pkey| is the EVP_PKEY handle for the private key. // |pkcs8| will receive the PKCS#8 bytes. // Returns true on success, false otherwise. -bool GetPrivateKeyPkcs8Bytes(const ScopedEVP_PKEY& pkey, +bool GetPrivateKeyPkcs8Bytes(const crypto::ScopedEVP_PKEY& pkey, std::string* pkcs8) { // Convert to PKCS#8 object. ScopedPKCS8_PRIV_KEY_INFO p8_info(EVP_PKEY2PKCS8(pkey.get())); @@ -155,7 +150,7 @@ bool GetPrivateKeyPkcs8Bytes(const ScopedEVP_PKEY& pkey, bool ImportPrivateKeyFileAsPkcs8(const char* filename, std::string* pkcs8) { - ScopedEVP_PKEY pkey(ImportPrivateKeyFile(filename)); + crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(filename)); if (!pkey.get()) return false; return GetPrivateKeyPkcs8Bytes(pkey, pkcs8); @@ -231,11 +226,11 @@ ScopedJava GetDSATestKeyJava() { // to do the check. bool VerifyTestDSASignature(const base::StringPiece& message, const base::StringPiece& signature) { - ScopedEVP_PKEY pkey(ImportPublicKeyFile(kTestDsaPublicKeyFile)); + crypto::ScopedEVP_PKEY pkey(ImportPublicKeyFile(kTestDsaPublicKeyFile)); if (!pkey.get()) return false; - ScopedDSA pub_key(EVP_PKEY_get1_DSA(pkey.get())); + crypto::ScopedDSA pub_key(EVP_PKEY_get1_DSA(pkey.get())); if (!pub_key.get()) { LOG(ERROR) << "Could not get DSA public key: " << GetOpenSSLErrorString(); @@ -279,10 +274,10 @@ ScopedJava GetECDSATestKeyJava() { // to do the check. bool VerifyTestECDSASignature(const base::StringPiece& message, const base::StringPiece& signature) { - ScopedEVP_PKEY pkey(ImportPublicKeyFile(kTestEcdsaPublicKeyFile)); + crypto::ScopedEVP_PKEY pkey(ImportPublicKeyFile(kTestEcdsaPublicKeyFile)); if (!pkey.get()) return false; - ScopedEC_KEY pub_key(EVP_PKEY_get1_EC_KEY(pkey.get())); + crypto::ScopedEC_KEY pub_key(EVP_PKEY_get1_EC_KEY(pkey.get())); if (!pub_key.get()) { LOG(ERROR) << "Could not get ECDSA public key: " << GetOpenSSLErrorString(); @@ -323,7 +318,7 @@ bool SignWithOpenSSL(const base::StringPiece& message, switch (key_type) { case EVP_PKEY_RSA: { - ScopedRSA rsa(EVP_PKEY_get1_RSA(openssl_key)); + crypto::ScopedRSA rsa(EVP_PKEY_get1_RSA(openssl_key)); if (!rsa.get()) { LOG(ERROR) << "Could not get RSA from EVP_PKEY: " << GetOpenSSLErrorString(); @@ -345,7 +340,7 @@ bool SignWithOpenSSL(const base::StringPiece& message, } case EVP_PKEY_DSA: { - ScopedDSA dsa(EVP_PKEY_get1_DSA(openssl_key)); + crypto::ScopedDSA dsa(EVP_PKEY_get1_DSA(openssl_key)); if (!dsa.get()) { LOG(ERROR) << "Could not get DSA from EVP_PKEY: " << GetOpenSSLErrorString(); @@ -367,7 +362,7 @@ bool SignWithOpenSSL(const base::StringPiece& message, } case EVP_PKEY_EC: { - ScopedEC_KEY ecdsa(EVP_PKEY_get1_EC_KEY(openssl_key)); + crypto::ScopedEC_KEY ecdsa(EVP_PKEY_get1_EC_KEY(openssl_key)); if (!ecdsa.get()) { LOG(ERROR) << "Could not get EC_KEY from EVP_PKEY: " << GetOpenSSLErrorString(); @@ -494,7 +489,7 @@ TEST(AndroidKeyStore,GetRSAKeyModulus) { InitEnv(); // Load the test RSA key. - ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestRsaKeyFile)); + crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestRsaKeyFile)); ASSERT_TRUE(pkey.get()); // Convert it to encoded PKCS#8 bytes. @@ -511,15 +506,14 @@ TEST(AndroidKeyStore,GetRSAKeyModulus) { ASSERT_TRUE(GetRSAKeyModulus(key_java.obj(), &modulus_java)); // Create an OpenSSL BIGNUM from it. - ScopedBIGNUM bn( - BN_bin2bn( - reinterpret_cast<const unsigned char*>(&modulus_java[0]), - static_cast<int>(modulus_java.size()), - NULL)); + crypto::ScopedBIGNUM bn( + BN_bin2bn(reinterpret_cast<const unsigned char*>(&modulus_java[0]), + static_cast<int>(modulus_java.size()), + NULL)); ASSERT_TRUE(bn.get()); // Compare it to the one in the RSA key, they must be identical. - ScopedRSA rsa(EVP_PKEY_get1_RSA(pkey.get())); + crypto::ScopedRSA rsa(EVP_PKEY_get1_RSA(pkey.get())); ASSERT_TRUE(rsa.get()) << GetOpenSSLErrorString(); ASSERT_EQ(0, BN_cmp(bn.get(), rsa.get()->n)); @@ -530,7 +524,7 @@ TEST(AndroidKeyStore,GetDSAKeyParamQ) { InitEnv(); // Load the test DSA key. - ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestDsaKeyFile)); + crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestDsaKeyFile)); ASSERT_TRUE(pkey.get()); // Convert it to encoded PKCS#8 bytes. @@ -547,15 +541,14 @@ TEST(AndroidKeyStore,GetDSAKeyParamQ) { ASSERT_TRUE(GetDSAKeyParamQ(key_java.obj(), &q_java)); // Create an OpenSSL BIGNUM from it. - ScopedBIGNUM bn( - BN_bin2bn( - reinterpret_cast<const unsigned char*>(&q_java[0]), - static_cast<int>(q_java.size()), - NULL)); + crypto::ScopedBIGNUM bn( + BN_bin2bn(reinterpret_cast<const unsigned char*>(&q_java[0]), + static_cast<int>(q_java.size()), + NULL)); ASSERT_TRUE(bn.get()); // Compare it to the one in the RSA key, they must be identical. - ScopedDSA dsa(EVP_PKEY_get1_DSA(pkey.get())); + crypto::ScopedDSA dsa(EVP_PKEY_get1_DSA(pkey.get())); ASSERT_TRUE(dsa.get()) << GetOpenSSLErrorString(); ASSERT_EQ(0, BN_cmp(bn.get(), dsa.get()->q)); @@ -579,7 +572,7 @@ TEST(AndroidKeyStore,SignWithPrivateKeyRSA) { return; } - ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile)); + crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile)); ASSERT_TRUE(openssl_key.get()); std::string message = kTestRsaHash; @@ -598,10 +591,11 @@ TEST(AndroidKeyStore,SignWithWrapperKeyRSA) { ScopedJava rsa_key = GetRSATestKeyJava(); ASSERT_FALSE(rsa_key.is_null()); - ScopedEVP_PKEY wrapper_key(GetOpenSSLPrivateKeyWrapper(rsa_key.obj())); + crypto::ScopedEVP_PKEY wrapper_key( + GetOpenSSLPrivateKeyWrapper(rsa_key.obj())); ASSERT_TRUE(wrapper_key.get() != NULL); - ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile)); + crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile)); ASSERT_TRUE(openssl_key.get()); // Check that RSA_size() works properly on the wrapper key. @@ -635,7 +629,7 @@ TEST(AndroidKeyStore,SignWithPrivateKeyDSA) { ScopedJava dsa_key = GetDSATestKeyJava(); ASSERT_FALSE(dsa_key.is_null()); - ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile)); + crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile)); ASSERT_TRUE(openssl_key.get()); std::string message = kTestDsaHash; @@ -652,11 +646,11 @@ TEST(AndroidKeyStore,SignWithWrapperKeyDSA) { ScopedJava dsa_key = GetDSATestKeyJava(); ASSERT_FALSE(dsa_key.is_null()); - ScopedEVP_PKEY wrapper_key( + crypto::ScopedEVP_PKEY wrapper_key( GetOpenSSLPrivateKeyWrapper(dsa_key.obj())); ASSERT_TRUE(wrapper_key.get()); - ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile)); + crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile)); ASSERT_TRUE(openssl_key.get()); // Check that DSA_size() works correctly on the wrapper. @@ -685,7 +679,7 @@ TEST(AndroidKeyStore,SignWithPrivateKeyECDSA) { ScopedJava ecdsa_key = GetECDSATestKeyJava(); ASSERT_FALSE(ecdsa_key.is_null()); - ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile)); + crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile)); ASSERT_TRUE(openssl_key.get()); std::string message = kTestEcdsaHash; @@ -700,11 +694,11 @@ TEST(AndroidKeyStore, SignWithWrapperKeyECDSA) { ScopedJava ecdsa_key = GetECDSATestKeyJava(); ASSERT_FALSE(ecdsa_key.is_null()); - ScopedEVP_PKEY wrapper_key( + crypto::ScopedEVP_PKEY wrapper_key( GetOpenSSLPrivateKeyWrapper(ecdsa_key.obj())); ASSERT_TRUE(wrapper_key.get()); - ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile)); + crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile)); ASSERT_TRUE(openssl_key.get()); // Check that ECDSA size works correctly on the wrapper. |