Chromium Code Reviews| Index: net/android/keystore_unittest.cc |
| diff --git a/net/android/keystore_unittest.cc b/net/android/keystore_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..369d31053686b79a9b8fef2445f8dfdead24ad4c |
| --- /dev/null |
| +++ b/net/android/keystore_unittest.cc |
| @@ -0,0 +1,675 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <openssl/bio.h> |
| +#include <openssl/bn.h> |
| +#include <openssl/dsa.h> |
| +#include <openssl/ecdsa.h> |
| +#include <openssl/err.h> |
| +#include <openssl/evp.h> |
| +#include <openssl/pem.h> |
| +#include <openssl/rsa.h> |
| +#include <openssl/x509.h> |
| + |
| +#include "base/android/build_info.h" |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_array.h" |
| +#include "base/android/scoped_java_ref.h" |
| +#include "base/basictypes.h" |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "crypto/openssl_util.h" |
| +#include "jni/AndroidKeyStoreTestUtil_jni.h" |
| +#include "net/android/keystore.h" |
| +#include "net/android/keystore_openssl.h" |
| +#include "net/base/test_data_directory.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using base::android::AttachCurrentThread; |
| +using base::android::ScopedJavaLocalRef; |
| +using base::android::ToJavaByteArray; |
| + |
| +// Technical note: |
| +// |
| +// This source file not only checks that signing with |
| +// RawSignDigestWithPrivateKey() works correctly, it also verifies that |
| +// the generated signature matches 100% of what OpenSSL generates when |
| +// calling RSA_sign(NID_md5_sha1,...), DSA_sign(0, ...) or |
| +// ECDSA_sign(0, ...). |
| +// |
| +// That's crucial to ensure that this function can later be used to |
| +// implement client certificate support. More specifically, that it is |
| +// possible to create a custom EVP_PKEY that uses |
| +// RawSignDigestWithPrivateKey() internally to perform RSA/DSA/ECDSA |
| +// signing, as invoked by the OpenSSL code at |
| +// openssl/ssl/s3_clnt.c:ssl3_send_client_verify(). |
| +// |
| +// For more details, read the comments in AndroidKeyStore.java. |
| +// |
| +// Finally, it also checks that using the EVP_PKEY generated with |
| +// GetOpenSSLPrivateKeyWrapper() works correctly. |
| + |
| +namespace net { |
| +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<BIO, BIO_free_all> ScopedBIO; |
| +typedef crypto::ScopedOpenSSL<BIGNUM, BN_free> ScopedBIGNUM; |
| + |
| +typedef crypto::ScopedOpenSSL< |
| + PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> |
| + ScopedPKCS8_PRIV_KEY_INFO; |
|
Ryan Sleevi
2013/02/04 22:53:27
style: Doesn't this fit on the same line?
digit1
2013/02/05 14:31:58
No, it doesn't fit in 80 columns.
|
| + |
| +JNIEnv* InitEnv() { |
| + JNIEnv* env = AttachCurrentThread(); |
| + static bool inited = false; |
| + if (!inited) { |
| + RegisterNativesImpl(env); |
| + inited = true; |
| + } |
| + return env; |
| +} |
| + |
| +// Returns true if running on an Android version older than 4.2 |
| +bool IsOnAndroidOlderThan_4_2(void) { |
| + const int kAndroid42ApiLevel = 17; |
| + int level = base::android::BuildInfo::GetInstance()->sdk_int(); |
| + return level < kAndroid42ApiLevel; |
| +} |
| + |
| +// Implements the callback expected by ERR_print_errors_cb(). |
| +// used by GetOpenSSLErrorString below. |
| +int openssl_print_error_callback(const char* msg, size_t msglen, void* u) { |
| + std::string* result = reinterpret_cast<std::string*>(u); |
| + result->append(msg, msglen); |
| + return 1; |
| +} |
| + |
| +// Retrieves the OpenSSL error as a string |
| +std::string GetOpenSSLErrorString(void) { |
| + std::string result; |
| + ERR_print_errors_cb(openssl_print_error_callback, &result); |
| + return result; |
| +} |
| + |
| +// Load a given private key file into an EVP_PKEY. |
| +// |filename| is the key file name. |
| +// |pkey| is a scoped OpenSSL EVP_PKEY reference that will be reset to |
| +// the private key. |
| +void ImportPrivateKeyFile(const char* filename, |
| + ScopedEVP_PKEY& pkey) { |
| + // Load file in memory. |
| + FilePath certs_dir = GetTestCertsDirectory(); |
| + FilePath file_path = certs_dir.AppendASCII(filename); |
| + std::string data; |
| + ASSERT_TRUE(file_util::ReadFileToString(file_path, &data)) |
| + << "Could not load private key file: " << filename; |
| + |
| + // Assume it is PEM_encoded. Load it as an EVP_PKEY. |
| + ScopedBIO bio( |
| + BIO_new_mem_buf( |
| + const_cast<char*>(data.data()), static_cast<int>(data.size()))); |
| + ASSERT_TRUE(bio.get()) << GetOpenSSLErrorString(); |
|
Ryan Sleevi
2013/02/04 22:53:27
Why do you copy it into a BIO, when PEM_read_Priva
digit1
2013/02/05 14:31:58
Done.
|
| + |
| + pkey.reset(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL)); |
| + ASSERT_TRUE(pkey.get()) << GetOpenSSLErrorString(); |
| +} |
| + |
| +// Convert a private key into its PKCS#8 encoded representation. |
| +// |pkey| is the EVP_PKEY handle for the private key. |
| +// |pkcs8| will receive the PKCS#8 bytes. |
| +void GetPrivateKeyPkcs8Bytes(const ScopedEVP_PKEY& pkey, |
| + std::string* pkcs8) { |
| + // Convert to PKCS#8 object. |
| + ScopedPKCS8_PRIV_KEY_INFO p8_info(EVP_PKEY2PKCS8(pkey.get())); |
| + ASSERT_TRUE(p8_info.get()) << GetOpenSSLErrorString(); |
| + |
| + // Then convert it |
| + int len = i2d_PKCS8_PRIV_KEY_INFO(p8_info.get(), NULL); |
| + std::vector<uint8> bytes; |
| + bytes.resize(len); |
| + unsigned char* p = reinterpret_cast<unsigned char*>(&bytes[0]); |
| + i2d_PKCS8_PRIV_KEY_INFO(p8_info.get(), &p); |
| + pkcs8->assign( |
| + reinterpret_cast<const char*>(&bytes[0]), |
|
Ryan Sleevi
2013/02/04 22:53:27
Why do you not use |p| here?
Further, why do you
digit1
2013/02/05 14:31:58
Because i2d_PKCS8_PRIV_KEY_INFO only works with an
|
| + bytes.size()); |
| +} |
| + |
| +void ImportPrivateKeyFileAsPkcs8(const char* filename, |
| + std::string* pkcs8) { |
| + ScopedEVP_PKEY pkey(NULL); |
| + ImportPrivateKeyFile(filename, pkey); |
| + GetPrivateKeyPkcs8Bytes(pkey, pkcs8); |
|
Ryan Sleevi
2013/02/04 22:53:27
BUG: Any one of these can fail their ASSERTs, but
digit1
2013/02/05 14:31:58
You can't use ASSERT in a function that doesn't re
|
| +} |
| + |
| +// Same as ImportPrivateKey, but for public ones. |
| +void ImportPublicKeyFile(const char* filename, |
| + ScopedEVP_PKEY& pkey) { |
| + // Load file as PEM data. |
| + FilePath certs_dir = GetTestCertsDirectory(); |
| + FilePath file_path = certs_dir.AppendASCII(filename); |
| + std::string data; |
| + ASSERT_TRUE(file_util::ReadFileToString(file_path, &data)) |
| + << "Could not load public key file: " << filename; |
| + |
| + ScopedBIO bio( |
| + BIO_new_mem_buf( |
| + const_cast<char*>(data.data()), static_cast<int>(data.size()))); |
|
Ryan Sleevi
2013/02/04 22:53:27
same comments RE: BIO and ASSERT
|
| + ASSERT_TRUE(bio.get()) << GetOpenSSLErrorString(); |
| + |
| + pkey.reset(PEM_read_bio_PUBKEY(bio.get(), NULL, NULL, NULL)); |
| + ASSERT_TRUE(pkey.get()) << GetOpenSSLErrorString(); |
| +} |
| + |
| +// Retrieve a JNI local ref from encoded PKCS#8 data. |
| +ScopedJavaLocalRef<jobject> GetPKCS8PrivateKeyJava( |
| + PrivateKeyType key_type, |
| + const std::string& pkcs8_key) { |
| + JNIEnv* env = InitEnv(); |
| + ScopedJavaLocalRef<jbyteArray> bytes( |
| + ToJavaByteArray(env, |
| + reinterpret_cast<const uint8*>(pkcs8_key.data()), |
| + pkcs8_key.size())); |
| + |
| + ScopedJavaLocalRef<jobject> key( |
| + Java_AndroidKeyStoreTestUtil_createPrivateKeyFromPKCS8( |
| + env, key_type, bytes.obj())); |
| + |
| + return key; |
| +} |
| + |
| +// Create an OpenSSL EVP_PKEY object from a PKCS#8 in-memory content. |
| +EVP_PKEY* GetOpenSSLPKCS8PrivateKey(int type, const std::string& key) { |
| + const unsigned char* p = |
| + reinterpret_cast<const unsigned char*>(key.data()); |
| + long p_length = static_cast<long>(key.size()); |
| + return d2i_PrivateKey(type, NULL, &p, p_length); |
| +} |
| + |
| +const char kTestRsaKeyFile[] = "android-test-key-rsa.pem"; |
| + |
| +// Retrieve a JNI local ref for our test RSA key. |
| +ScopedJavaLocalRef<jobject> GetRSATestKeyJava() { |
| + std::string key; |
| + ImportPrivateKeyFileAsPkcs8(kTestRsaKeyFile, &key); |
|
Ryan Sleevi
2013/02/04 22:53:27
same comments here re: ASSERT failures propagating
|
| + return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_RSA, key); |
| +} |
| + |
| +// Retrieve the OpenSSL object for our test RSA key. |
| +EVP_PKEY* GetRSATestKeyOpenSSL() { |
| + ScopedEVP_PKEY pkey(NULL); |
| + ImportPrivateKeyFile(kTestRsaKeyFile, pkey); |
| + return pkey.release(); |
| +} |
| + |
| +const char kTestDsaKeyFile[] = "android-test-key-dsa.pem"; |
| +const char kTestDsaPublicKeyFile[] = "android-test-key-dsa-public.pem"; |
| + |
| +// Retrieve a JNI local ref for our test DSA key. |
| +ScopedJavaLocalRef<jobject> GetDSATestKeyJava() { |
| + std::string key; |
| + ImportPrivateKeyFileAsPkcs8(kTestDsaKeyFile, &key); |
| + return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_DSA, key); |
| +} |
| + |
| +// Retrieve the OpenSSL object for our test DSA key. |
| +EVP_PKEY* GetDSATestKeyOpenSSL() { |
| + ScopedEVP_PKEY pkey; |
| + ImportPrivateKeyFile(kTestDsaKeyFile, pkey); |
| + return pkey.release(); |
| +} |
| + |
| +// Call this function to verify that one message signed with our |
| +// test DSA private key is correct. Since DSA signing introduces |
| +// random elements in the signature, it is not possible to compare |
| +// signature bits directly. However, one can use the public key |
| +// to do the check. |
| +void VerifyTestDSASignature(const base::StringPiece& message, |
| + const base::StringPiece& signature) { |
| + ScopedEVP_PKEY pkey(NULL); |
|
Ryan Sleevi
2013/02/04 22:53:27
nit: do not explicitly NULL initialize like this (
digit1
2013/02/05 14:31:58
Done.
|
| + ImportPublicKeyFile(kTestDsaPublicKeyFile, pkey); |
| + ScopedDSA pub_key(EVP_PKEY_get1_DSA(pkey.get())); |
| + ASSERT_TRUE(pub_key.get() != NULL); |
| + |
| + const unsigned char* digest = |
| + reinterpret_cast<const unsigned char*>(message.data()); |
| + int digest_len = static_cast<int>(message.size()); |
| + const unsigned char* sigbuf = |
| + reinterpret_cast<const unsigned char*>(signature.data()); |
| + int siglen = static_cast<int>(signature.size()); |
| + |
| + ASSERT_EQ( |
| + 1, DSA_verify(0, digest, digest_len, sigbuf, siglen, pub_key.get())); |
| +} |
| + |
| +const char kTestEcdsaKeyFile[] = "android-test-key-ecdsa.pem"; |
| +const char kTestEcdsaPublicKeyFile[] = "android-test-key-ecdsa-public.pem"; |
| + |
| +// The test hash for ECDSA keys must be 20 bytes. |
| +const char kTestEcdsaHash[] = "0123456789ABCDEFGHIJ"; |
|
Ryan Sleevi
2013/02/04 22:53:27
style: Place these constants at the beginning (lin
|
| + |
| +// Retrieve a JNI local ref for our test ECDSA key. |
| +ScopedJavaLocalRef<jobject> GetECDSATestKeyJava() { |
| + std::string key; |
| + ImportPrivateKeyFileAsPkcs8(kTestEcdsaKeyFile, &key); |
| + return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_ECDSA, key); |
| +} |
| + |
| +// Retrieve the OpenSSL object for our test ECDSA key. |
| +EVP_PKEY* GetECDSATestKeyOpenSSL() { |
| + ScopedEVP_PKEY pkey(NULL); |
| + ImportPrivateKeyFile(kTestEcdsaKeyFile, pkey); |
| + return pkey.release(); |
| +} |
| + |
| +// Call this function to verify that one message signed with our |
| +// test DSA private key is correct. Since DSA signing introduces |
| +// random elements in the signature, it is not possible to compare |
| +// signature bits directly. However, one can use the public key |
| +// to do the check. |
| +void VerifyTestECDSASignature(const base::StringPiece& message, |
| + const base::StringPiece& signature) { |
| + ScopedEVP_PKEY pkey; |
| + ImportPublicKeyFile(kTestEcdsaPublicKeyFile, pkey); |
| + ScopedEC_KEY pub_key(EVP_PKEY_get1_EC_KEY(pkey.get())); |
| + ASSERT_TRUE(pub_key.get()); |
| + |
| + const unsigned char* digest = |
| + reinterpret_cast<const unsigned char*>(message.data()); |
| + int digest_len = static_cast<int>(message.size()); |
| + const unsigned char* sigbuf = |
| + reinterpret_cast<const unsigned char*>(signature.data()); |
| + int siglen = static_cast<int>(signature.size()); |
| + |
| + ASSERT_EQ( |
| + 1, ECDSA_verify( |
| + 0, digest, digest_len, sigbuf, siglen, pub_key.get())); |
| +} |
| + |
| +// Sign a message with OpenSSL, return the result as a string. |
| +// |message| is the message to be signed. |
| +// |openssl_key| is an OpenSSL EVP_PKEY to use. |
| +// |result| receives the result. |
| +void SignWithOpenSSL(const base::StringPiece& message, |
| + EVP_PKEY* openssl_key, |
| + std::string* result) { |
| + const unsigned char* digest = |
| + reinterpret_cast<const unsigned char*>(message.data()); |
| + unsigned int digest_len = static_cast<unsigned int>(message.size()); |
| + // Calling size functions like "RSA_size()" doesn't work at all |
| + // with custom RSA_METHOD implementations. That's probably a bug |
| + // in OpenSSL, so instead just use a very large buffer. |
| + // Note that the code in ssl/s3_clnt.c does something similar. |
| + std::vector<uint8> openssl_signature; |
| + int key_type = EVP_PKEY_id(openssl_key); |
| + switch (key_type) { |
| + case EVP_PKEY_RSA: |
| + { |
| + ScopedRSA rsa(EVP_PKEY_get1_RSA(openssl_key)); |
| + ASSERT_TRUE(rsa.get()); |
| + // With RSA, the signature will always be RSA_size() bytes. |
| + openssl_signature.resize(static_cast<size_t>(RSA_size(rsa.get()))); |
| + unsigned char* p = |
| + reinterpret_cast<unsigned char*>(&openssl_signature[0]); |
| + unsigned int p_len = 0; |
| + int ret = RSA_sign( |
| + NID_md5_sha1, digest, digest_len, p, &p_len, rsa.get()); |
| + ASSERT_EQ(1, ret) << GetOpenSSLErrorString(); |
| + ASSERT_EQ(static_cast<size_t>(p_len), openssl_signature.size()); |
| + } |
| + break; |
| + case EVP_PKEY_DSA: |
| + { |
| + ScopedDSA dsa(EVP_PKEY_get1_DSA(openssl_key)); |
| + ASSERT_TRUE(dsa.get()); |
| + // Note, the actual signature can be smaller than DSA_size() |
| + openssl_signature.resize( |
| + static_cast<size_t>(DSA_size(dsa.get()))); |
| + unsigned char* p = |
| + reinterpret_cast<unsigned char*>(&openssl_signature[0]); |
| + unsigned int p_len = 0; |
| + // Note: first parameter is ignored by function. |
| + int ret = DSA_sign(0, digest, digest_len, p, &p_len, dsa.get()); |
| + ASSERT_EQ(1, ret) << GetOpenSSLErrorString(); |
| + ASSERT_LT(0U, p_len); |
| + ASSERT_LE(static_cast<size_t>(p_len), openssl_signature.size()); |
| + openssl_signature.resize(static_cast<size_t>(p_len)); |
| + } |
| + break; |
| + case EVP_PKEY_EC: |
| + { |
| + ScopedEC_KEY ecdsa(EVP_PKEY_get1_EC_KEY(openssl_key)); |
| + ASSERT_TRUE(ecdsa.get()); |
| + // Note, the actual signature can be smaller than ECDSA_size() |
| + openssl_signature.resize(ECDSA_size(ecdsa.get())); |
| + unsigned char* p = |
| + reinterpret_cast<unsigned char*>(&openssl_signature[0]); |
| + unsigned int p_len = 0; |
| + // Note: first parameter is ignored by function. |
| + int ret = ECDSA_sign( |
| + 0, digest, digest_len, p, &p_len, ecdsa.get()); |
| + ASSERT_EQ(1, ret) << GetOpenSSLErrorString(); |
| + ASSERT_LT(0U, p_len); |
| + ASSERT_LE(static_cast<size_t>(p_len), openssl_signature.size()); |
| + openssl_signature.resize(static_cast<size_t>(p_len)); |
| + } |
| + break; |
| + default: |
| + LOG(WARNING) << "Invalid OpenSSL key type: " << key_type; |
| + return; |
| + } |
| + result->assign(reinterpret_cast<const char*>(&openssl_signature[0]), |
| + openssl_signature.size()); |
| +} |
| + |
| +// Check that a generated signature for a given message matches |
| +// OpenSSL output byte-by-byte. |
| +// |message| is the input message. |
| +// |signature| is the generated signature for the message. |
| +// |openssl_key| is a raw EVP_PKEY for the same private key than the |
| +// one which was used to generate the signature. |
| +void CompareSignatureWithOpenSSL(const base::StringPiece& message, |
| + const base::StringPiece& signature, |
| + EVP_PKEY* openssl_key) { |
| + std::string openssl_signature; |
| + SignWithOpenSSL(message, openssl_key, &openssl_signature); |
| + |
| + ASSERT_EQ(signature.size(), openssl_signature.size()); |
| + for (size_t n = 0; n < signature.size(); ++n) |
| + ASSERT_EQ(openssl_signature[n], signature[n]); |
|
Ryan Sleevi
2013/02/04 22:53:27
Use SCOPED_TRACE here, as otherwise the gtest outp
digit1
2013/02/05 14:31:58
I've changed the code to output this through LOG(E
|
| + |
| + // All good, just exit. |
| +} |
| + |
| +// Sign a message with our platform API. |
| +// |
| +// |android_key| is a JNI reference to the platform PrivateKey object. |
| +// |openssl_key| is a pointer to an OpenSSL key object for the exact |
| +// same key content. |
| +// |message| is a message. |
| +// |result| will receive the result. |
| +void DoKeySigning(jobject android_key, |
| + EVP_PKEY* openssl_key, |
| + const base::StringPiece& message, |
| + std::string* result) { |
| + // First, get the platform signature. |
| + std::vector<uint8> android_signature; |
| + ASSERT_TRUE( |
| + RawSignDigestWithPrivateKey(android_key, |
| + message, |
| + &android_signature)); |
| + |
| + result->assign( |
| + reinterpret_cast<const char*>(&android_signature[0]), |
| + android_signature.size()); |
| +} |
| + |
| +// Sign a message with our OpenSSL EVP_PKEY wrapper around platform |
| +// APIS. |
| +// |
| +// |android_key| is a JNI reference to the platform PrivateKey object. |
| +// |openssl_key| is a pointer to an OpenSSL key object for the exact |
| +// same key content. |
| +// |message| is a message. |
| +// |result| will receive the result. |
| +void DoKeySigningWithWrapper(EVP_PKEY* wrapper_key, |
| + EVP_PKEY* openssl_key, |
| + const base::StringPiece& message, |
| + std::string* result) { |
| + // First, get the platform signature. |
| + std::string wrapper_signature; |
| + SignWithOpenSSL(message, wrapper_key, &wrapper_signature); |
| + ASSERT_NE(0U, wrapper_signature.size()); |
| + |
| + result->assign( |
| + reinterpret_cast<const char*>(&wrapper_signature[0]), |
| + wrapper_signature.size()); |
| +} |
| + |
| +} // namespace |
| + |
| +TEST(AndroidKeyStore,GetRSAKeyModulus) { |
| + crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); |
| + InitEnv(); |
| + |
| + // Load the test RSA key. |
| + ScopedEVP_PKEY pkey(NULL); |
| + ImportPrivateKeyFile(kTestRsaKeyFile, pkey); |
| + |
| + // Convert it to encoded PKCS#8 bytes. |
| + std::string pkcs8_data; |
| + GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data); |
| + |
| + // Create platform PrivateKey object from it. |
| + ScopedJavaLocalRef<jobject> key_java = GetPKCS8PrivateKeyJava( |
| + PRIVATE_KEY_TYPE_RSA, pkcs8_data); |
| + ASSERT_FALSE(key_java.is_null()); |
| + |
| + // Retrieve the corresponding modulus through JNI |
| + std::vector<uint8> modulus_java; |
| + 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)); |
| + 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())); |
| + ASSERT_TRUE(rsa.get()) << GetOpenSSLErrorString(); |
| + |
| + ASSERT_EQ(0, BN_cmp(bn.get(), rsa.get()->n)); |
| +} |
| + |
| +TEST(AndroidKeyStore,GetDSAKeyParamQ) { |
| + crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); |
| + InitEnv(); |
| + |
| + // Load the test DSA key. |
| + ScopedEVP_PKEY pkey(NULL); |
| + ImportPrivateKeyFile(kTestDsaKeyFile, pkey); |
| + |
| + // Convert it to encoded PKCS#8 bytes. |
| + std::string pkcs8_data; |
| + GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data); |
| + |
| + // Create platform PrivateKey object from it. |
| + ScopedJavaLocalRef<jobject> key_java = GetPKCS8PrivateKeyJava( |
| + PRIVATE_KEY_TYPE_DSA, pkcs8_data); |
| + ASSERT_FALSE(key_java.is_null()); |
| + |
| + // Retrieve the corresponding Q parameter through JNI |
| + std::vector<uint8> q_java; |
| + 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)); |
| + 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())); |
| + ASSERT_TRUE(dsa.get()) << GetOpenSSLErrorString(); |
| + |
| + ASSERT_EQ(0, BN_cmp(bn.get(), dsa.get()->q)); |
| +} |
| + |
| +TEST(AndroidKeyStore,GetPrivateKeyTypeRSA) { |
| + crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); |
| + |
| + ScopedJavaLocalRef<jobject> rsa_key = GetRSATestKeyJava(); |
| + ASSERT_FALSE(rsa_key.is_null()); |
| + EXPECT_EQ(PRIVATE_KEY_TYPE_RSA, |
| + GetPrivateKeyType(rsa_key.obj())); |
| +} |
| + |
| +TEST(AndroidKeyStore,SignWithPrivateKeyRSA) { |
| + ScopedJavaLocalRef<jobject> rsa_key = GetRSATestKeyJava(); |
| + ASSERT_FALSE(rsa_key.is_null()); |
| + |
| + if (IsOnAndroidOlderThan_4_2()) { |
| + LOG(INFO) << "This test can't run on Android < 4.2"; |
| + return; |
| + } |
| + |
| + ScopedEVP_PKEY openssl_key; |
| + ImportPrivateKeyFile(kTestRsaKeyFile, openssl_key); |
| + |
| + std::string message = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| + ASSERT_EQ(36U, message.size()); |
| + |
| + std::string signature; |
| + DoKeySigning(rsa_key.obj(), openssl_key.get(), message, &signature); |
| + CompareSignatureWithOpenSSL(message, signature, openssl_key.get()); |
| + // All good. |
| +} |
| + |
| +TEST(AndroidKeyStore,SignWithWrapperKeyRSA) { |
| + crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| + |
| + ScopedJavaLocalRef<jobject> rsa_key = GetRSATestKeyJava(); |
| + ASSERT_FALSE(rsa_key.is_null()); |
| + |
| + ScopedEVP_PKEY wrapper_key(GetOpenSSLPrivateKeyWrapper(rsa_key.obj())); |
| + ASSERT_TRUE(wrapper_key.get() != NULL); |
| + |
| + ScopedEVP_PKEY openssl_key; |
| + ImportPrivateKeyFile(kTestRsaKeyFile, openssl_key); |
| + |
| + // Check that RSA_size() works properly on the wrapper key. |
| + EXPECT_EQ(EVP_PKEY_size(openssl_key.get()), |
| + EVP_PKEY_size(wrapper_key.get())); |
| + |
| + // Message size must be 36 for RSA_sign(NID_md5_sha1,...) to return |
| + // without an error. |
| + std::string message = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| + ASSERT_EQ(36U, message.size()); |
| + |
| + std::string signature; |
| + DoKeySigningWithWrapper(wrapper_key.get(), |
| + openssl_key.get(), |
| + message, |
| + &signature); |
| + CompareSignatureWithOpenSSL(message, signature, openssl_key.get()); |
| + // All good. |
| +} |
| + |
| +TEST(AndroidKeyStore,GetPrivateKeyTypeDSA) { |
| + crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); |
| + |
| + ScopedJavaLocalRef<jobject> dsa_key = GetDSATestKeyJava(); |
| + ASSERT_FALSE(dsa_key.is_null()); |
| + EXPECT_EQ(PRIVATE_KEY_TYPE_DSA, |
| + GetPrivateKeyType(dsa_key.obj())); |
| +} |
| + |
| +TEST(AndroidKeyStore,SignWithPrivateKeyDSA) { |
| + ScopedJavaLocalRef<jobject> dsa_key = GetDSATestKeyJava(); |
| + ASSERT_FALSE(dsa_key.is_null()); |
| + |
| + ScopedEVP_PKEY openssl_key; |
| + ImportPrivateKeyFile(kTestDsaKeyFile, openssl_key); |
| + |
| + std::string message = "0123456789ABCDEFGHIJ"; |
| + ASSERT_EQ(20U, message.size()); |
| + |
| + std::string signature; |
| + DoKeySigning(dsa_key.obj(), openssl_key.get(), message, &signature); |
| + VerifyTestDSASignature(message, signature); |
| + // All good. |
| +} |
| + |
| +TEST(AndroidKeyStore,SignWithWrapperKeyDSA) { |
| + crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| + |
| + ScopedJavaLocalRef<jobject> dsa_key = GetDSATestKeyJava(); |
| + ASSERT_FALSE(dsa_key.is_null()); |
| + |
| + ScopedEVP_PKEY wrapper_key( |
| + GetOpenSSLPrivateKeyWrapper(dsa_key.obj())); |
| + ASSERT_TRUE(wrapper_key.get() != NULL); |
| + |
| + ScopedEVP_PKEY openssl_key; |
| + ImportPrivateKeyFile(kTestDsaKeyFile, openssl_key); |
| + |
| + // Check that DSA_size() works correctly on the wrapper. |
| + EXPECT_EQ(EVP_PKEY_size(openssl_key.get()), |
| + EVP_PKEY_size(wrapper_key.get())); |
| + |
| + std::string message = "0123456789ABCDEFGHIJ"; |
| + std::string signature; |
| + DoKeySigningWithWrapper(wrapper_key.get(), |
| + openssl_key.get(), |
| + message, |
| + &signature); |
| + VerifyTestDSASignature(message, signature); |
| + // All good. |
| +} |
| + |
| +TEST(AndroidKeyStore,GetPrivateKeyTypeECDSA) { |
| + crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); |
| + |
| + ScopedJavaLocalRef<jobject> ecdsa_key = GetECDSATestKeyJava(); |
| + ASSERT_FALSE(ecdsa_key.is_null()); |
| + EXPECT_EQ(PRIVATE_KEY_TYPE_ECDSA, |
| + GetPrivateKeyType(ecdsa_key.obj())); |
| +} |
| + |
| +TEST(AndroidKeyStore,SignWithPrivateKeyECDSA) { |
| + ScopedJavaLocalRef<jobject> ecdsa_key = GetECDSATestKeyJava(); |
| + ASSERT_FALSE(ecdsa_key.is_null()); |
| + |
| + ScopedEVP_PKEY openssl_key; |
| + ImportPrivateKeyFile(kTestEcdsaKeyFile, openssl_key); |
| + |
| + std::string message = "0123456789ABCDEFGHIJ"; |
| + std::string signature; |
| + DoKeySigning(ecdsa_key.obj(), openssl_key.get(), message, &signature); |
| + VerifyTestECDSASignature(message, signature); |
| + // All good. |
| +} |
| + |
| +TEST(AndroidKeyStore, SignWithWrapperKeyECDSA) { |
| + crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| + |
| + ScopedJavaLocalRef<jobject> ecdsa_key = GetECDSATestKeyJava(); |
| + ASSERT_FALSE(ecdsa_key.is_null()); |
| + |
| + ScopedEVP_PKEY wrapper_key( |
| + GetOpenSSLPrivateKeyWrapper(ecdsa_key.obj())); |
| + ASSERT_TRUE(wrapper_key.get() != NULL); |
| + |
| + ScopedEVP_PKEY openssl_key; |
| + ImportPrivateKeyFile(kTestEcdsaKeyFile, openssl_key); |
| + |
| + // Check that ECDSA size works correctly on the wrapper. |
| + EXPECT_EQ(EVP_PKEY_size(openssl_key.get()), |
| + EVP_PKEY_size(wrapper_key.get())); |
| + |
| + std::string message = "0123456789ABCDEFGHIJ"; |
| + std::string signature; |
| + DoKeySigningWithWrapper(wrapper_key.get(), |
| + openssl_key.get(), |
| + message, |
| + &signature); |
| + VerifyTestECDSASignature(message, signature); |
| + // All good. |
| +} |
| + |
| +} // namespace android |
| +} // namespace net |