1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <openssl/bn.h>
6 #include <openssl/dsa.h>
7 #include <openssl/ecdsa.h>
8 #include <openssl/err.h>
9 #include <openssl/evp.h>
10 #include <openssl/pem.h>
11 #include <openssl/rsa.h>
12 #include <openssl/x509.h>
14 #include "base/android/build_info.h"
15 #include "base/android/jni_android.h"
16 #include "base/android/jni_array.h"
17 #include "base/android/scoped_java_ref.h"
18 #include "base/basictypes.h"
19 #include "base/bind.h"
20 #include "base/callback.h"
21 #include "base/compiler_specific.h"
22 #include "base/file_util.h"
23 #include "base/files/file_path.h"
24 #include "base/memory/scoped_handle.h"
25 #include "base/strings/string_number_conversions.h"
26 #include "base/strings/string_util.h"
27 #include "crypto/openssl_util.h"
28 #include "jni/AndroidKeyStoreTestUtil_jni.h"
29 #include "net/android/keystore.h"
30 #include "net/android/keystore_openssl.h"
31 #include "net/base/test_data_directory.h"
32 #include "testing/gtest/include/gtest/gtest.h"
36 // This source file not only checks that signing with
37 // RawSignDigestWithPrivateKey() works correctly, it also verifies that
38 // the generated signature matches 100% of what OpenSSL generates when
39 // calling RSA_sign(NID_md5_sha1,...), DSA_sign(0, ...) or
40 // ECDSA_sign(0, ...).
42 // That's crucial to ensure that this function can later be used to
43 // implement client certificate support. More specifically, that it is
44 // possible to create a custom EVP_PKEY that uses
45 // RawSignDigestWithPrivateKey() internally to perform RSA/DSA/ECDSA
46 // signing, as invoked by the OpenSSL code at
47 // openssl/ssl/s3_clnt.c:ssl3_send_client_verify().
49 // For more details, read the comments in AndroidKeyStore.java.
51 // Finally, it also checks that using the EVP_PKEY generated with
52 // GetOpenSSLPrivateKeyWrapper() works correctly.
59 typedef crypto::ScopedOpenSSL
<EVP_PKEY
, EVP_PKEY_free
> ScopedEVP_PKEY
;
60 typedef crypto::ScopedOpenSSL
<RSA
, RSA_free
> ScopedRSA
;
61 typedef crypto::ScopedOpenSSL
<DSA
, DSA_free
> ScopedDSA
;
62 typedef crypto::ScopedOpenSSL
<EC_KEY
, EC_KEY_free
> ScopedEC_KEY
;
63 typedef crypto::ScopedOpenSSL
<BIGNUM
, BN_free
> ScopedBIGNUM
;
65 typedef crypto::ScopedOpenSSL
<
66 PKCS8_PRIV_KEY_INFO
, PKCS8_PRIV_KEY_INFO_free
>
67 ScopedPKCS8_PRIV_KEY_INFO
;
69 typedef base::android::ScopedJavaLocalRef
<jobject
> ScopedJava
;
72 JNIEnv
* env
= base::android::AttachCurrentThread();
73 static bool inited
= false;
75 RegisterNativesImpl(env
);
81 // Returns true if running on an Android version older than 4.2
82 bool IsOnAndroidOlderThan_4_2(void) {
83 const int kAndroid42ApiLevel
= 17;
84 int level
= base::android::BuildInfo::GetInstance()->sdk_int();
85 return level
< kAndroid42ApiLevel
;
88 // Implements the callback expected by ERR_print_errors_cb().
89 // used by GetOpenSSLErrorString below.
90 int openssl_print_error_callback(const char* msg
, size_t msglen
, void* u
) {
91 std::string
* result
= reinterpret_cast<std::string
*>(u
);
92 result
->append(msg
, msglen
);
96 // Retrieves the OpenSSL error as a string
97 std::string
GetOpenSSLErrorString(void) {
99 ERR_print_errors_cb(openssl_print_error_callback
, &result
);
103 // Resize a string to |size| bytes of data, then return its data buffer
104 // address cast as an 'unsigned char*', as expected by OpenSSL functions.
105 // |str| the target string.
106 // |size| the number of bytes to write into the string.
107 // Return the string's new buffer in memory, as an 'unsigned char*'
109 unsigned char* OpenSSLWriteInto(std::string
* str
, size_t size
) {
110 return reinterpret_cast<unsigned char*>(WriteInto(str
, size
+ 1));
113 // Load a given private key file into an EVP_PKEY.
114 // |filename| is the key file path.
115 // Returns a new EVP_PKEY on success, NULL on failure.
116 EVP_PKEY
* ImportPrivateKeyFile(const char* filename
) {
117 // Load file in memory.
118 base::FilePath certs_dir
= GetTestCertsDirectory();
119 base::FilePath file_path
= certs_dir
.AppendASCII(filename
);
120 ScopedStdioHandle
handle(base::OpenFile(file_path
, "rb"));
122 LOG(ERROR
) << "Could not open private key file: " << filename
;
125 // Assume it is PEM_encoded. Load it as an EVP_PKEY.
126 EVP_PKEY
* pkey
= PEM_read_PrivateKey(handle
.get(), NULL
, NULL
, NULL
);
128 LOG(ERROR
) << "Could not load public key file: " << filename
129 << ", " << GetOpenSSLErrorString();
135 // Convert a private key into its PKCS#8 encoded representation.
136 // |pkey| is the EVP_PKEY handle for the private key.
137 // |pkcs8| will receive the PKCS#8 bytes.
138 // Returns true on success, false otherwise.
139 bool GetPrivateKeyPkcs8Bytes(const ScopedEVP_PKEY
& pkey
,
140 std::string
* pkcs8
) {
141 // Convert to PKCS#8 object.
142 ScopedPKCS8_PRIV_KEY_INFO
p8_info(EVP_PKEY2PKCS8(pkey
.get()));
143 if (!p8_info
.get()) {
144 LOG(ERROR
) << "Can't get PKCS#8 private key from EVP_PKEY: "
145 << GetOpenSSLErrorString();
150 int len
= i2d_PKCS8_PRIV_KEY_INFO(p8_info
.get(), NULL
);
151 unsigned char* p
= OpenSSLWriteInto(pkcs8
, static_cast<size_t>(len
));
152 i2d_PKCS8_PRIV_KEY_INFO(p8_info
.get(), &p
);
156 bool ImportPrivateKeyFileAsPkcs8(const char* filename
,
157 std::string
* pkcs8
) {
158 ScopedEVP_PKEY
pkey(ImportPrivateKeyFile(filename
));
161 return GetPrivateKeyPkcs8Bytes(pkey
, pkcs8
);
164 // Same as ImportPrivateKey, but for public ones.
165 EVP_PKEY
* ImportPublicKeyFile(const char* filename
) {
166 // Load file as PEM data.
167 base::FilePath certs_dir
= GetTestCertsDirectory();
168 base::FilePath file_path
= certs_dir
.AppendASCII(filename
);
169 ScopedStdioHandle
handle(base::OpenFile(file_path
, "rb"));
171 LOG(ERROR
) << "Could not open public key file: " << filename
;
174 EVP_PKEY
* pkey
= PEM_read_PUBKEY(handle
.get(), NULL
, NULL
, NULL
);
176 LOG(ERROR
) << "Could not load public key file: " << filename
177 << ", " << GetOpenSSLErrorString();
183 // Retrieve a JNI local ref from encoded PKCS#8 data.
184 ScopedJava
GetPKCS8PrivateKeyJava(PrivateKeyType key_type
,
185 const std::string
& pkcs8_key
) {
186 JNIEnv
* env
= InitEnv();
187 base::android::ScopedJavaLocalRef
<jbyteArray
> bytes(
188 base::android::ToJavaByteArray(
190 reinterpret_cast<const uint8
*>(pkcs8_key
.data()),
194 Java_AndroidKeyStoreTestUtil_createPrivateKeyFromPKCS8(
195 env
, key_type
, bytes
.obj()));
200 const char kTestRsaKeyFile
[] = "android-test-key-rsa.pem";
202 // The RSA test hash must be 36 bytes exactly.
203 const char kTestRsaHash
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
205 // Retrieve a JNI local ref for our test RSA key.
206 ScopedJava
GetRSATestKeyJava() {
208 if (!ImportPrivateKeyFileAsPkcs8(kTestRsaKeyFile
, &key
))
210 return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_RSA
, key
);
213 const char kTestDsaKeyFile
[] = "android-test-key-dsa.pem";
214 const char kTestDsaPublicKeyFile
[] = "android-test-key-dsa-public.pem";
216 // The DSA test hash must be 20 bytes exactly.
217 const char kTestDsaHash
[] = "0123456789ABCDEFGHIJ";
219 // Retrieve a JNI local ref for our test DSA key.
220 ScopedJava
GetDSATestKeyJava() {
222 if (!ImportPrivateKeyFileAsPkcs8(kTestDsaKeyFile
, &key
))
224 return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_DSA
, key
);
227 // Call this function to verify that one message signed with our
228 // test DSA private key is correct. Since DSA signing introduces
229 // random elements in the signature, it is not possible to compare
230 // signature bits directly. However, one can use the public key
232 bool VerifyTestDSASignature(const base::StringPiece
& message
,
233 const base::StringPiece
& signature
) {
234 ScopedEVP_PKEY
pkey(ImportPublicKeyFile(kTestDsaPublicKeyFile
));
238 ScopedDSA
pub_key(EVP_PKEY_get1_DSA(pkey
.get()));
239 if (!pub_key
.get()) {
240 LOG(ERROR
) << "Could not get DSA public key: "
241 << GetOpenSSLErrorString();
245 const unsigned char* digest
=
246 reinterpret_cast<const unsigned char*>(message
.data());
247 int digest_len
= static_cast<int>(message
.size());
248 const unsigned char* sigbuf
=
249 reinterpret_cast<const unsigned char*>(signature
.data());
250 int siglen
= static_cast<int>(signature
.size());
252 int ret
= DSA_verify(
253 0, digest
, digest_len
, sigbuf
, siglen
, pub_key
.get());
255 LOG(ERROR
) << "DSA_verify() failed: " << GetOpenSSLErrorString();
261 const char kTestEcdsaKeyFile
[] = "android-test-key-ecdsa.pem";
262 const char kTestEcdsaPublicKeyFile
[] = "android-test-key-ecdsa-public.pem";
264 // The test hash for ECDSA keys must be 20 bytes exactly.
265 const char kTestEcdsaHash
[] = "0123456789ABCDEFGHIJ";
267 // Retrieve a JNI local ref for our test ECDSA key.
268 ScopedJava
GetECDSATestKeyJava() {
270 if (!ImportPrivateKeyFileAsPkcs8(kTestEcdsaKeyFile
, &key
))
272 return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_ECDSA
, key
);
275 // Call this function to verify that one message signed with our
276 // test DSA private key is correct. Since DSA signing introduces
277 // random elements in the signature, it is not possible to compare
278 // signature bits directly. However, one can use the public key
280 bool VerifyTestECDSASignature(const base::StringPiece
& message
,
281 const base::StringPiece
& signature
) {
282 ScopedEVP_PKEY
pkey(ImportPublicKeyFile(kTestEcdsaPublicKeyFile
));
285 ScopedEC_KEY
pub_key(EVP_PKEY_get1_EC_KEY(pkey
.get()));
286 if (!pub_key
.get()) {
287 LOG(ERROR
) << "Could not get ECDSA public key: "
288 << GetOpenSSLErrorString();
292 const unsigned char* digest
=
293 reinterpret_cast<const unsigned char*>(message
.data());
294 int digest_len
= static_cast<int>(message
.size());
295 const unsigned char* sigbuf
=
296 reinterpret_cast<const unsigned char*>(signature
.data());
297 int siglen
= static_cast<int>(signature
.size());
299 int ret
= ECDSA_verify(
300 0, digest
, digest_len
, sigbuf
, siglen
, pub_key
.get());
302 LOG(ERROR
) << "ECDSA_verify() failed: " << GetOpenSSLErrorString();
308 // Sign a message with OpenSSL, return the result as a string.
309 // |message| is the message to be signed.
310 // |openssl_key| is an OpenSSL EVP_PKEY to use.
311 // |result| receives the result.
312 // Returns true on success, false otherwise.
313 bool SignWithOpenSSL(const base::StringPiece
& message
,
314 EVP_PKEY
* openssl_key
,
315 std::string
* result
) {
316 const unsigned char* digest
=
317 reinterpret_cast<const unsigned char*>(message
.data());
318 unsigned int digest_len
= static_cast<unsigned int>(message
.size());
319 std::string signature
;
320 size_t signature_size
;
321 size_t max_signature_size
;
322 int key_type
= EVP_PKEY_id(openssl_key
);
326 ScopedRSA
rsa(EVP_PKEY_get1_RSA(openssl_key
));
328 LOG(ERROR
) << "Could not get RSA from EVP_PKEY: "
329 << GetOpenSSLErrorString();
332 // With RSA, the signature will always be RSA_size() bytes.
333 max_signature_size
= static_cast<size_t>(RSA_size(rsa
.get()));
334 unsigned char* p
= OpenSSLWriteInto(&signature
,
336 unsigned int p_len
= 0;
338 NID_md5_sha1
, digest
, digest_len
, p
, &p_len
, rsa
.get());
340 LOG(ERROR
) << "RSA_sign() failed: " << GetOpenSSLErrorString();
343 signature_size
= static_cast<size_t>(p_len
);
348 ScopedDSA
dsa(EVP_PKEY_get1_DSA(openssl_key
));
350 LOG(ERROR
) << "Could not get DSA from EVP_PKEY: "
351 << GetOpenSSLErrorString();
354 // Note, the actual signature can be smaller than DSA_size()
355 max_signature_size
= static_cast<size_t>(DSA_size(dsa
.get()));
356 unsigned char* p
= OpenSSLWriteInto(&signature
,
358 unsigned int p_len
= 0;
359 // Note: first parameter is ignored by function.
360 int ret
= DSA_sign(0, digest
, digest_len
, p
, &p_len
, dsa
.get());
362 LOG(ERROR
) << "DSA_sign() failed: " << GetOpenSSLErrorString();
365 signature_size
= static_cast<size_t>(p_len
);
370 ScopedEC_KEY
ecdsa(EVP_PKEY_get1_EC_KEY(openssl_key
));
372 LOG(ERROR
) << "Could not get EC_KEY from EVP_PKEY: "
373 << GetOpenSSLErrorString();
376 // Note, the actual signature can be smaller than ECDSA_size()
377 max_signature_size
= ECDSA_size(ecdsa
.get());
378 unsigned char* p
= OpenSSLWriteInto(&signature
,
380 unsigned int p_len
= 0;
381 // Note: first parameter is ignored by function.
382 int ret
= ECDSA_sign(
383 0, digest
, digest_len
, p
, &p_len
, ecdsa
.get());
385 LOG(ERROR
) << "ECDSA_sign() fialed: " << GetOpenSSLErrorString();
388 signature_size
= static_cast<size_t>(p_len
);
392 LOG(WARNING
) << "Invalid OpenSSL key type: " << key_type
;
396 if (signature_size
== 0) {
397 LOG(ERROR
) << "Signature is empty!";
400 if (signature_size
> max_signature_size
) {
401 LOG(ERROR
) << "Signature size mismatch, actual " << signature_size
402 << ", expected <= " << max_signature_size
;
405 signature
.resize(signature_size
);
406 result
->swap(signature
);
410 // Check that a generated signature for a given message matches
411 // OpenSSL output byte-by-byte.
412 // |message| is the input message.
413 // |signature| is the generated signature for the message.
414 // |openssl_key| is a raw EVP_PKEY for the same private key than the
415 // one which was used to generate the signature.
416 // Returns true on success, false otherwise.
417 bool CompareSignatureWithOpenSSL(const base::StringPiece
& message
,
418 const base::StringPiece
& signature
,
419 EVP_PKEY
* openssl_key
) {
420 std::string openssl_signature
;
421 SignWithOpenSSL(message
, openssl_key
, &openssl_signature
);
423 if (signature
.size() != openssl_signature
.size()) {
424 LOG(ERROR
) << "Signature size mismatch, actual "
425 << signature
.size() << ", expected "
426 << openssl_signature
.size();
429 for (size_t n
= 0; n
< signature
.size(); ++n
) {
430 if (openssl_signature
[n
] != signature
[n
]) {
431 LOG(ERROR
) << "Signature byte mismatch at index " << n
432 << "actual " << signature
[n
] << ", expected "
433 << openssl_signature
[n
];
434 LOG(ERROR
) << "Actual signature : "
435 << base::HexEncode(signature
.data(), signature
.size());
436 LOG(ERROR
) << "Expected signature: "
437 << base::HexEncode(openssl_signature
.data(),
438 openssl_signature
.size());
445 // Sign a message with our platform API.
447 // |android_key| is a JNI reference to the platform PrivateKey object.
448 // |openssl_key| is a pointer to an OpenSSL key object for the exact
450 // |message| is a message.
451 // |result| will receive the result.
452 void DoKeySigning(jobject android_key
,
453 EVP_PKEY
* openssl_key
,
454 const base::StringPiece
& message
,
455 std::string
* result
) {
456 // First, get the platform signature.
457 std::vector
<uint8
> android_signature
;
459 RawSignDigestWithPrivateKey(android_key
,
461 &android_signature
));
464 reinterpret_cast<const char*>(&android_signature
[0]),
465 android_signature
.size());
468 // Sign a message with our OpenSSL EVP_PKEY wrapper around platform
471 // |android_key| is a JNI reference to the platform PrivateKey object.
472 // |openssl_key| is a pointer to an OpenSSL key object for the exact
474 // |message| is a message.
475 // |result| will receive the result.
476 void DoKeySigningWithWrapper(EVP_PKEY
* wrapper_key
,
477 EVP_PKEY
* openssl_key
,
478 const base::StringPiece
& message
,
479 std::string
* result
) {
480 // First, get the platform signature.
481 std::string wrapper_signature
;
482 SignWithOpenSSL(message
, wrapper_key
, &wrapper_signature
);
483 ASSERT_NE(0U, wrapper_signature
.size());
486 reinterpret_cast<const char*>(&wrapper_signature
[0]),
487 wrapper_signature
.size());
492 TEST(AndroidKeyStore
,GetRSAKeyModulus
) {
493 crypto::OpenSSLErrStackTracer
err_trace(FROM_HERE
);
496 // Load the test RSA key.
497 ScopedEVP_PKEY
pkey(ImportPrivateKeyFile(kTestRsaKeyFile
));
498 ASSERT_TRUE(pkey
.get());
500 // Convert it to encoded PKCS#8 bytes.
501 std::string pkcs8_data
;
502 ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey
, &pkcs8_data
));
504 // Create platform PrivateKey object from it.
505 ScopedJava key_java
= GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_RSA
,
507 ASSERT_FALSE(key_java
.is_null());
509 // Retrieve the corresponding modulus through JNI
510 std::vector
<uint8
> modulus_java
;
511 ASSERT_TRUE(GetRSAKeyModulus(key_java
.obj(), &modulus_java
));
513 // Create an OpenSSL BIGNUM from it.
516 reinterpret_cast<const unsigned char*>(&modulus_java
[0]),
517 static_cast<int>(modulus_java
.size()),
519 ASSERT_TRUE(bn
.get());
521 // Compare it to the one in the RSA key, they must be identical.
522 ScopedRSA
rsa(EVP_PKEY_get1_RSA(pkey
.get()));
523 ASSERT_TRUE(rsa
.get()) << GetOpenSSLErrorString();
525 ASSERT_EQ(0, BN_cmp(bn
.get(), rsa
.get()->n
));
528 TEST(AndroidKeyStore
,GetDSAKeyParamQ
) {
529 crypto::OpenSSLErrStackTracer
err_trace(FROM_HERE
);
532 // Load the test DSA key.
533 ScopedEVP_PKEY
pkey(ImportPrivateKeyFile(kTestDsaKeyFile
));
534 ASSERT_TRUE(pkey
.get());
536 // Convert it to encoded PKCS#8 bytes.
537 std::string pkcs8_data
;
538 ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey
, &pkcs8_data
));
540 // Create platform PrivateKey object from it.
541 ScopedJava key_java
= GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_DSA
,
543 ASSERT_FALSE(key_java
.is_null());
545 // Retrieve the corresponding Q parameter through JNI
546 std::vector
<uint8
> q_java
;
547 ASSERT_TRUE(GetDSAKeyParamQ(key_java
.obj(), &q_java
));
549 // Create an OpenSSL BIGNUM from it.
552 reinterpret_cast<const unsigned char*>(&q_java
[0]),
553 static_cast<int>(q_java
.size()),
555 ASSERT_TRUE(bn
.get());
557 // Compare it to the one in the RSA key, they must be identical.
558 ScopedDSA
dsa(EVP_PKEY_get1_DSA(pkey
.get()));
559 ASSERT_TRUE(dsa
.get()) << GetOpenSSLErrorString();
561 ASSERT_EQ(0, BN_cmp(bn
.get(), dsa
.get()->q
));
564 TEST(AndroidKeyStore
,GetPrivateKeyTypeRSA
) {
565 crypto::OpenSSLErrStackTracer
err_trace(FROM_HERE
);
567 ScopedJava rsa_key
= GetRSATestKeyJava();
568 ASSERT_FALSE(rsa_key
.is_null());
569 EXPECT_EQ(PRIVATE_KEY_TYPE_RSA
,
570 GetPrivateKeyType(rsa_key
.obj()));
573 TEST(AndroidKeyStore
,SignWithPrivateKeyRSA
) {
574 ScopedJava rsa_key
= GetRSATestKeyJava();
575 ASSERT_FALSE(rsa_key
.is_null());
577 if (IsOnAndroidOlderThan_4_2()) {
578 LOG(INFO
) << "This test can't run on Android < 4.2";
582 ScopedEVP_PKEY
openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile
));
583 ASSERT_TRUE(openssl_key
.get());
585 std::string message
= kTestRsaHash
;
586 ASSERT_EQ(36U, message
.size());
588 std::string signature
;
589 DoKeySigning(rsa_key
.obj(), openssl_key
.get(), message
, &signature
);
591 CompareSignatureWithOpenSSL(message
, signature
, openssl_key
.get()));
595 TEST(AndroidKeyStore
,SignWithWrapperKeyRSA
) {
596 crypto::OpenSSLErrStackTracer
err_tracer(FROM_HERE
);
598 ScopedJava rsa_key
= GetRSATestKeyJava();
599 ASSERT_FALSE(rsa_key
.is_null());
601 ScopedEVP_PKEY
wrapper_key(GetOpenSSLPrivateKeyWrapper(rsa_key
.obj()));
602 ASSERT_TRUE(wrapper_key
.get() != NULL
);
604 ScopedEVP_PKEY
openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile
));
605 ASSERT_TRUE(openssl_key
.get());
607 // Check that RSA_size() works properly on the wrapper key.
608 EXPECT_EQ(EVP_PKEY_size(openssl_key
.get()),
609 EVP_PKEY_size(wrapper_key
.get()));
611 // Message size must be 36 for RSA_sign(NID_md5_sha1,...) to return
613 std::string message
= kTestRsaHash
;
614 ASSERT_EQ(36U, message
.size());
616 std::string signature
;
617 DoKeySigningWithWrapper(wrapper_key
.get(),
622 CompareSignatureWithOpenSSL(message
, signature
, openssl_key
.get()));
625 TEST(AndroidKeyStore
,GetPrivateKeyTypeDSA
) {
626 crypto::OpenSSLErrStackTracer
err_trace(FROM_HERE
);
628 ScopedJava dsa_key
= GetDSATestKeyJava();
629 ASSERT_FALSE(dsa_key
.is_null());
630 EXPECT_EQ(PRIVATE_KEY_TYPE_DSA
,
631 GetPrivateKeyType(dsa_key
.obj()));
634 TEST(AndroidKeyStore
,SignWithPrivateKeyDSA
) {
635 ScopedJava dsa_key
= GetDSATestKeyJava();
636 ASSERT_FALSE(dsa_key
.is_null());
638 ScopedEVP_PKEY
openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile
));
639 ASSERT_TRUE(openssl_key
.get());
641 std::string message
= kTestDsaHash
;
642 ASSERT_EQ(20U, message
.size());
644 std::string signature
;
645 DoKeySigning(dsa_key
.obj(), openssl_key
.get(), message
, &signature
);
646 ASSERT_TRUE(VerifyTestDSASignature(message
, signature
));
649 TEST(AndroidKeyStore
,SignWithWrapperKeyDSA
) {
650 crypto::OpenSSLErrStackTracer
err_tracer(FROM_HERE
);
652 ScopedJava dsa_key
= GetDSATestKeyJava();
653 ASSERT_FALSE(dsa_key
.is_null());
655 ScopedEVP_PKEY
wrapper_key(
656 GetOpenSSLPrivateKeyWrapper(dsa_key
.obj()));
657 ASSERT_TRUE(wrapper_key
.get());
659 ScopedEVP_PKEY
openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile
));
660 ASSERT_TRUE(openssl_key
.get());
662 // Check that DSA_size() works correctly on the wrapper.
663 EXPECT_EQ(EVP_PKEY_size(openssl_key
.get()),
664 EVP_PKEY_size(wrapper_key
.get()));
666 std::string message
= kTestDsaHash
;
667 std::string signature
;
668 DoKeySigningWithWrapper(wrapper_key
.get(),
672 ASSERT_TRUE(VerifyTestDSASignature(message
, signature
));
675 TEST(AndroidKeyStore
,GetPrivateKeyTypeECDSA
) {
676 crypto::OpenSSLErrStackTracer
err_trace(FROM_HERE
);
678 ScopedJava ecdsa_key
= GetECDSATestKeyJava();
679 ASSERT_FALSE(ecdsa_key
.is_null());
680 EXPECT_EQ(PRIVATE_KEY_TYPE_ECDSA
,
681 GetPrivateKeyType(ecdsa_key
.obj()));
684 TEST(AndroidKeyStore
,SignWithPrivateKeyECDSA
) {
685 ScopedJava ecdsa_key
= GetECDSATestKeyJava();
686 ASSERT_FALSE(ecdsa_key
.is_null());
688 ScopedEVP_PKEY
openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile
));
689 ASSERT_TRUE(openssl_key
.get());
691 std::string message
= kTestEcdsaHash
;
692 std::string signature
;
693 DoKeySigning(ecdsa_key
.obj(), openssl_key
.get(), message
, &signature
);
694 ASSERT_TRUE(VerifyTestECDSASignature(message
, signature
));
697 TEST(AndroidKeyStore
, SignWithWrapperKeyECDSA
) {
698 crypto::OpenSSLErrStackTracer
err_tracer(FROM_HERE
);
700 ScopedJava ecdsa_key
= GetECDSATestKeyJava();
701 ASSERT_FALSE(ecdsa_key
.is_null());
703 ScopedEVP_PKEY
wrapper_key(
704 GetOpenSSLPrivateKeyWrapper(ecdsa_key
.obj()));
705 ASSERT_TRUE(wrapper_key
.get());
707 ScopedEVP_PKEY
openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile
));
708 ASSERT_TRUE(openssl_key
.get());
710 // Check that ECDSA size works correctly on the wrapper.
711 EXPECT_EQ(EVP_PKEY_size(openssl_key
.get()),
712 EVP_PKEY_size(wrapper_key
.get()));
714 std::string message
= kTestEcdsaHash
;
715 std::string signature
;
716 DoKeySigningWithWrapper(wrapper_key
.get(),
720 ASSERT_TRUE(VerifyTestECDSASignature(message
, signature
));
723 } // namespace android