Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Unified Diff: net/android/keystore_unittest.cc

Issue 11571059: Add net/android/keystore.h (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: First real implementation - RSA only Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..e615c4f1a26573fd61da7caa59ddb713fc7757fb
--- /dev/null
+++ b/net/android/keystore_unittest.cc
@@ -0,0 +1,420 @@
+// 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/dsa.h>
+#include <openssl/ecdsa.h>
+#include <openssl/err.h>
+#include <openssl/evp.h>
+#include <openssl/rsa.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 "crypto/openssl_util.h"
+#include "jni/AndroidKeyStoreTestUtil_jni.h"
+#include "net/android/keystore.h"
+#include "net/android/keystore_openssl.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 SignWithPrivateKey()
+// 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 SignWithPrivateKey()
+// internally to perform RSA/DSA/ECDSA signing, as invoked by the
+// OpenSSL code at ssl/s3_clnt.c:ssl3_send_client_verify().
+//
+// Finally, it also checks that using the EVP_PKEY generated with
+// GetOpenSSLPrivateKeyWrapper() works too as well.
+//
+
+namespace net {
+namespace android {
+
+namespace {
+
+JNIEnv* InitEnv() {
+ JNIEnv* env = AttachCurrentThread();
+ static bool inited = false;
+ if (!inited) {
+ RegisterNativesImpl(env);
+ inited = true;
+ }
+ return env;
+}
+
+// Implements the callback expected by ERR_print_errors_cb().
+// used by GetOpenSSLErrorString.
+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;
+}
+
+// Retrieve OpenSSL error as a string
+std::string GetOpenSSLErrorString(void) {
+ std::string result;
+ ERR_print_errors_cb(openssl_print_error_callback, &result);
+ return result;
+}
+
+// Retrieve a JNI local ref from encoded PKCS#8 data.
+ScopedJavaLocalRef<jobject> GetPKCS8PrivateKey(
+ SSLClientCertType key_type,
+ const unsigned char* pkcs8_bytes,
+ size_t pkcs8_size) {
+ JNIEnv* env = InitEnv();
+ ScopedJavaLocalRef<jbyteArray> bytes(
+ ToJavaByteArray(env,
+ reinterpret_cast<const uint8*>(pkcs8_bytes),
+ pkcs8_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 unsigned char* key_bytes,
+ size_t key_size) {
+ const unsigned char* p = key_bytes;
+ long p_length = static_cast<long>(key_size);
+ return d2i_PrivateKey(type, NULL, &p, p_length);
+}
+
+// A simple test RSA 2048-bits key, in PKCS#8 format, the following was
+// generated with:
+//
+// openssl genrsa -out key.pem 2048
+// openssl pkcs8 -topk8 -inform PEM -outform DER -in key.pem key.pkcs8
+// xxd -i key.pkcs8
+//
+const unsigned char test_rsa_key_pkcs8[] = {
+ 0x30, 0x82, 0x04, 0xbd, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
+ 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
+ 0x04, 0xa7, 0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01,
+ 0x01, 0x00, 0xc0, 0x41, 0x18, 0x86, 0x1c, 0xe3, 0x60, 0xd7, 0xec, 0x38,
+ 0x70, 0xd0, 0x35, 0x64, 0x6e, 0x0e, 0x4f, 0xaf, 0x3f, 0x05, 0x57, 0x05,
+ 0xc3, 0xe9, 0x8d, 0xc3, 0x92, 0x14, 0xa1, 0xea, 0xab, 0xe3, 0x5f, 0x61,
+ 0x70, 0x9a, 0xd7, 0x43, 0x2f, 0xc2, 0x31, 0xd7, 0x8c, 0x65, 0x89, 0xea,
+ 0x8b, 0xdb, 0xa7, 0x9f, 0x9e, 0xba, 0xea, 0x48, 0x7d, 0x71, 0x10, 0x11,
+ 0x86, 0x9d, 0x55, 0x68, 0xd4, 0x31, 0xa0, 0x33, 0x06, 0x5f, 0x0f, 0x43,
+ 0xe5, 0x52, 0x46, 0x8d, 0xc1, 0x26, 0xb2, 0x08, 0xd6, 0xa5, 0x9f, 0x85,
+ 0x24, 0xb0, 0x4d, 0x7e, 0x2a, 0x7b, 0x4e, 0xcd, 0x54, 0xe5, 0x11, 0xf8,
+ 0x39, 0xf5, 0x13, 0x5c, 0xb4, 0xff, 0x07, 0x82, 0xaf, 0xdc, 0x05, 0x27,
+ 0x35, 0x5a, 0x48, 0xc9, 0xa0, 0x3b, 0xe5, 0x03, 0x33, 0x32, 0x9a, 0x60,
+ 0xe7, 0x0d, 0xce, 0x23, 0xb7, 0xbf, 0x6e, 0x9e, 0x14, 0x6f, 0xe0, 0x5f,
+ 0x36, 0x98, 0xd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 0xe5, 0x9f, 0xe6, 0x45, 0x8d, 0xc0, 0xfe, 0x5d, 0x6f, 0x4d,
+ 0xc4, 0xa5, 0x43, 0x7b, 0x0a, 0xed, 0xa2, 0x8a, 0x9c, 0x0c, 0x95, 0xd4,
+ 0x23, 0x8d, 0x34, 0x56, 0xfd, 0x02, 0x81, 0x81, 0x00, 0xe2, 0x05, 0xb1,
+ 0xe7, 0x6b, 0xf1, 0xa9, 0xa5, 0xe1, 0x48, 0xb5, 0xe0, 0x76, 0x95, 0xa0,
+ 0x7e, 0xb0, 0xa6, 0x32, 0x2d, 0x16, 0x07, 0xe9, 0x30, 0x90, 0xf4, 0xfe,
+ 0x54, 0x98, 0x7c, 0xdc, 0xf5, 0x67, 0x87, 0xa1, 0x1c, 0x18, 0x49, 0x06,
+ 0x4a, 0xaf, 0xbb, 0xbb, 0xb1, 0x0b, 0xcc, 0x3d, 0x89, 0x2a, 0x70, 0x18,
+ 0x39, 0x92, 0x6f, 0xc4, 0xcb, 0x76, 0xe6, 0x1e, 0xd5, 0x72, 0xc1, 0x98,
+ 0x75, 0x5c, 0xdb, 0x15, 0x01, 0x1f, 0xb7, 0xcd, 0xc2, 0x88, 0xc5, 0x2c,
+ 0x5d, 0x1d, 0x76, 0x1c, 0xd5, 0xcd, 0xb5, 0xa0, 0x18, 0xd7, 0x0d, 0x53,
+ 0x43, 0xb7, 0x31, 0x51, 0x30, 0x27, 0xf5, 0x60, 0x8c, 0x8e, 0x17, 0x44,
+ 0xac, 0xeb, 0xac, 0xde, 0x39, 0x28, 0x78, 0x02, 0x95, 0xeb, 0x2f, 0xaa,
+ 0xd8, 0x26, 0x76, 0xbd, 0xeb, 0x0e, 0x3f, 0x99, 0xe6, 0xb6, 0xf5, 0xcf,
+ 0xd3, 0xe2, 0xf4, 0xa2, 0xab
+};
+
+// Retrieve a JNI local ref for our test RSA key.
+ScopedJavaLocalRef<jobject> GetRSATestKey() {
+ return GetPKCS8PrivateKey(CLIENT_CERT_RSA_SIGN,
+ test_rsa_key_pkcs8,
+ sizeof(test_rsa_key_pkcs8));
+}
+
+// Retrieve the OpenSSL object for our test RSA key.
+EVP_PKEY* GetRSATestKeyOpenSSL() {
+ return GetOpenSSLPKCS8PrivateKey(EVP_PKEY_RSA,
+ test_rsa_key_pkcs8,
+ sizeof(test_rsa_key_pkcs8));
+}
+
+// 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.
+ unsigned char openssl_signature[8192];
+ unsigned int openssl_signature_len = 0;
+ int key_type = EVP_PKEY_id(openssl_key);
+ switch (key_type) {
+ case EVP_PKEY_RSA:
+ {
+ RSA* rsa = EVP_PKEY_get1_RSA(openssl_key);
+ ASSERT_TRUE(rsa != NULL);
+ int ret = RSA_sign(NID_md5_sha1,
+ digest,
+ digest_len,
+ openssl_signature,
+ &openssl_signature_len,
+ rsa);
+ ASSERT_EQ(1, ret) << GetOpenSSLErrorString();
+ RSA_free(rsa);
+ }
+ break;
+ case EVP_PKEY_DSA:
+ {
+ DSA* dsa = EVP_PKEY_get1_DSA(openssl_key);
+ ASSERT_TRUE(dsa != NULL);
+ int ret = DSA_sign(0, // ignored by the function
+ digest,
+ digest_len,
+ openssl_signature,
+ &openssl_signature_len,
+ dsa);
+ ASSERT_EQ(1, ret) << GetOpenSSLErrorString();
+ DSA_free(dsa);
+ }
+ break;
+ case EVP_PKEY_EC:
+ {
+ EC_KEY* ecdsa = EVP_PKEY_get1_EC_KEY(openssl_key);
+ ASSERT_TRUE(ecdsa != NULL);
+ int ret = ECDSA_sign(0, // ignored by the function
+ digest,
+ digest_len,
+ openssl_signature,
+ &openssl_signature_len,
+ ecdsa);
+ ASSERT_EQ(1, ret) << GetOpenSSLErrorString();
+ EC_KEY_free(ecdsa);
+ }
+ break;
+ default:
+ LOG(WARNING) << "Invalid OpenSSL key type: " + key_type;
+ return;
+ }
+ result->assign(reinterpret_cast<const char*>(openssl_signature),
+ static_cast<size_t>(openssl_signature_len));
+}
+
+// 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]);
+
+ // All good, just exit.
+}
+
+// Check that signing a message with our platform API generates
+// a result that is identical to calling OpenSSL's RSA_sign() function.
+// This is a hard requirement for client certificate support.
+//
+// |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.
+void CheckKeySigning(jobject rsa_key,
+ EVP_PKEY* openssl_key,
+ const base::StringPiece& message) {
+ // First, get the platform signature.
+ std::vector<uint8> android_signature;
+ ASSERT_TRUE(
+ SignWithPrivateKey(rsa_key,
+ message,
+ &android_signature));
+
+ base::StringPiece signature(
+ reinterpret_cast<const char*>(&android_signature[0]),
+ android_signature.size());
+
+ CompareSignatureWithOpenSSL(message, signature, openssl_key);
+}
+
+// Check that signing a message with our platform API generates
+// a result that is identical to calling OpenSSL's RSA_sign() function.
+// This is a hard requirement for client certificate support.
+//
+// |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.
+void CheckKeySigningWithWrapper(EVP_PKEY* wrapper_key,
+ EVP_PKEY* openssl_key,
+ const base::StringPiece& message) {
+ // First, get the signature platform signature.
+ std::string wrapper_signature;
+ SignWithOpenSSL(message, wrapper_key, &wrapper_signature);
+ ASSERT_NE(0U, wrapper_signature.size());
+
+ base::StringPiece signature(
+ reinterpret_cast<const char*>(&wrapper_signature[0]),
+ wrapper_signature.size());
+
+ CompareSignatureWithOpenSSL(message, signature, openssl_key);
+}
+
+} // namespace
+
+TEST(AndroidKeyStore,GetPrivateKeySigningTypeRSA) {
+ crypto::OpenSSLErrStackTracer err_trace(FROM_HERE);
+
+ ScopedJavaLocalRef<jobject> rsa_key = GetRSATestKey();
+ ASSERT_FALSE(rsa_key.is_null());
+ EXPECT_EQ(CLIENT_CERT_RSA_SIGN,
+ GetPrivateKeySigningType(rsa_key.obj()));
+}
+
+TEST(AndroidKeyStore,SignWithPrivateKeyRSA) {
+ ScopedJavaLocalRef<jobject> rsa_key = GetRSATestKey();
+ ASSERT_FALSE(rsa_key.is_null());
+
+ crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> openssl_key(
+ GetRSATestKeyOpenSSL());
+ ASSERT_TRUE(openssl_key.get() != NULL);
+
+ // Message size must be 36 for RSA_sign(NID_md5_sha1,...) to return
+ // without an error.
+ // See third_party/openssl/openssl/crypto/rsa/rsa_sign.c
+ std::string message = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ ASSERT_EQ(36U, message.size());
+
+ CheckKeySigning(rsa_key.obj(), openssl_key.get(), message);
+ // All good.
+}
+
+TEST(AndroidKeyStore,SignWithWrapperKeyRSA) {
+ crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
+
+ ScopedJavaLocalRef<jobject> rsa_key = GetRSATestKey();
+ ASSERT_FALSE(rsa_key.is_null());
+
+ crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> wrapper_key(
+ GetOpenSSLPrivateKeyWrapper(rsa_key.obj()));
+ ASSERT_TRUE(wrapper_key.get() != NULL);
+
+ crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> openssl_key(
+ GetRSATestKeyOpenSSL());
+ ASSERT_TRUE(openssl_key.get() != NULL);
+
+ // 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());
+
+ CheckKeySigningWithWrapper(wrapper_key.get(),
+ openssl_key.get(),
+ message);
+ // All good.
+}
+
+// TODO(digit): Add DSA + ECDSA tests.
+
+} // namespace android
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698