1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
// 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/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_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#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"
#include "net/base/test_data_directory.h"
#include "testing/gtest/include/gtest/gtest.h"
// 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<PKCS8_PRIV_KEY_INFO,
PKCS8_PRIV_KEY_INFO_free>::Type
ScopedPKCS8_PRIV_KEY_INFO;
typedef base::android::ScopedJavaLocalRef<jobject> ScopedJava;
JNIEnv* InitEnv() {
JNIEnv* env = base::android::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;
}
// Resize a string to |size| bytes of data, then return its data buffer
// address cast as an 'unsigned char*', as expected by OpenSSL functions.
// |str| the target string.
// |size| the number of bytes to write into the string.
// Return the string's new buffer in memory, as an 'unsigned char*'
// pointer.
unsigned char* OpenSSLWriteInto(std::string* str, size_t size) {
return reinterpret_cast<unsigned char*>(WriteInto(str, size + 1));
}
// Load a given private key file into an EVP_PKEY.
// |filename| is the key file path.
// Returns a new EVP_PKEY on success, NULL on failure.
EVP_PKEY* ImportPrivateKeyFile(const char* filename) {
// Load file in memory.
base::FilePath certs_dir = GetTestCertsDirectory();
base::FilePath file_path = certs_dir.AppendASCII(filename);
base::ScopedFILE handle(base::OpenFile(file_path, "rb"));
if (!handle.get()) {
LOG(ERROR) << "Could not open private key file: " << filename;
return NULL;
}
// Assume it is PEM_encoded. Load it as an EVP_PKEY.
EVP_PKEY* pkey = PEM_read_PrivateKey(handle.get(), NULL, NULL, NULL);
if (!pkey) {
LOG(ERROR) << "Could not load public key file: " << filename
<< ", " << GetOpenSSLErrorString();
return NULL;
}
return pkey;
}
// 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.
// Returns true on success, false otherwise.
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()));
if (!p8_info.get()) {
LOG(ERROR) << "Can't get PKCS#8 private key from EVP_PKEY: "
<< GetOpenSSLErrorString();
return false;
}
// Then convert it
int len = i2d_PKCS8_PRIV_KEY_INFO(p8_info.get(), NULL);
unsigned char* p = OpenSSLWriteInto(pkcs8, static_cast<size_t>(len));
i2d_PKCS8_PRIV_KEY_INFO(p8_info.get(), &p);
return true;
}
bool ImportPrivateKeyFileAsPkcs8(const char* filename,
std::string* pkcs8) {
crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(filename));
if (!pkey.get())
return false;
return GetPrivateKeyPkcs8Bytes(pkey, pkcs8);
}
// Same as ImportPrivateKey, but for public ones.
EVP_PKEY* ImportPublicKeyFile(const char* filename) {
// Load file as PEM data.
base::FilePath certs_dir = GetTestCertsDirectory();
base::FilePath file_path = certs_dir.AppendASCII(filename);
base::ScopedFILE handle(base::OpenFile(file_path, "rb"));
if (!handle.get()) {
LOG(ERROR) << "Could not open public key file: " << filename;
return NULL;
}
EVP_PKEY* pkey = PEM_read_PUBKEY(handle.get(), NULL, NULL, NULL);
if (!pkey) {
LOG(ERROR) << "Could not load public key file: " << filename
<< ", " << GetOpenSSLErrorString();
return NULL;
}
return pkey;
}
// Retrieve a JNI local ref from encoded PKCS#8 data.
ScopedJava GetPKCS8PrivateKeyJava(PrivateKeyType key_type,
const std::string& pkcs8_key) {
JNIEnv* env = InitEnv();
base::android::ScopedJavaLocalRef<jbyteArray> bytes(
base::android::ToJavaByteArray(
env,
reinterpret_cast<const uint8*>(pkcs8_key.data()),
pkcs8_key.size()));
ScopedJava key(
Java_AndroidKeyStoreTestUtil_createPrivateKeyFromPKCS8(
env, key_type, bytes.obj()));
return key;
}
const char kTestRsaKeyFile[] = "android-test-key-rsa.pem";
// The RSA test hash must be 36 bytes exactly.
const char kTestRsaHash[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Retrieve a JNI local ref for our test RSA key.
ScopedJava GetRSATestKeyJava() {
std::string key;
if (!ImportPrivateKeyFileAsPkcs8(kTestRsaKeyFile, &key))
return ScopedJava();
return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_RSA, key);
}
const char kTestDsaKeyFile[] = "android-test-key-dsa.pem";
const char kTestDsaPublicKeyFile[] = "android-test-key-dsa-public.pem";
// The DSA test hash must be 20 bytes exactly.
const char kTestDsaHash[] = "0123456789ABCDEFGHIJ";
// Retrieve a JNI local ref for our test DSA key.
ScopedJava GetDSATestKeyJava() {
std::string key;
if (!ImportPrivateKeyFileAsPkcs8(kTestDsaKeyFile, &key))
return ScopedJava();
return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_DSA, key);
}
// 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.
bool VerifyTestDSASignature(const base::StringPiece& message,
const base::StringPiece& signature) {
crypto::ScopedEVP_PKEY pkey(ImportPublicKeyFile(kTestDsaPublicKeyFile));
if (!pkey.get())
return false;
crypto::ScopedDSA pub_key(EVP_PKEY_get1_DSA(pkey.get()));
if (!pub_key.get()) {
LOG(ERROR) << "Could not get DSA public key: "
<< GetOpenSSLErrorString();
return false;
}
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());
int ret = DSA_verify(
0, digest, digest_len, sigbuf, siglen, pub_key.get());
if (ret != 1) {
LOG(ERROR) << "DSA_verify() failed: " << GetOpenSSLErrorString();
return false;
}
return true;
}
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 exactly.
const char kTestEcdsaHash[] = "0123456789ABCDEFGHIJ";
// Retrieve a JNI local ref for our test ECDSA key.
ScopedJava GetECDSATestKeyJava() {
std::string key;
if (!ImportPrivateKeyFileAsPkcs8(kTestEcdsaKeyFile, &key))
return ScopedJava();
return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_ECDSA, key);
}
// 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.
bool VerifyTestECDSASignature(const base::StringPiece& message,
const base::StringPiece& signature) {
crypto::ScopedEVP_PKEY pkey(ImportPublicKeyFile(kTestEcdsaPublicKeyFile));
if (!pkey.get())
return false;
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();
return false;
}
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());
int ret = ECDSA_verify(
0, digest, digest_len, sigbuf, siglen, pub_key.get());
if (ret != 1) {
LOG(ERROR) << "ECDSA_verify() failed: " << GetOpenSSLErrorString();
return false;
}
return true;
}
// 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.
// Returns true on success, false otherwise.
bool 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());
std::string signature;
size_t signature_size;
size_t max_signature_size;
int key_type = EVP_PKEY_id(openssl_key);
switch (key_type) {
case EVP_PKEY_RSA:
{
crypto::ScopedRSA rsa(EVP_PKEY_get1_RSA(openssl_key));
if (!rsa.get()) {
LOG(ERROR) << "Could not get RSA from EVP_PKEY: "
<< GetOpenSSLErrorString();
return false;
}
// With RSA, the signature will always be RSA_size() bytes.
max_signature_size = static_cast<size_t>(RSA_size(rsa.get()));
unsigned char* p = OpenSSLWriteInto(&signature,
max_signature_size);
unsigned int p_len = 0;
int ret = RSA_sign(
NID_md5_sha1, digest, digest_len, p, &p_len, rsa.get());
if (ret != 1) {
LOG(ERROR) << "RSA_sign() failed: " << GetOpenSSLErrorString();
return false;
}
signature_size = static_cast<size_t>(p_len);
break;
}
case EVP_PKEY_DSA:
{
crypto::ScopedDSA dsa(EVP_PKEY_get1_DSA(openssl_key));
if (!dsa.get()) {
LOG(ERROR) << "Could not get DSA from EVP_PKEY: "
<< GetOpenSSLErrorString();
return false;
}
// Note, the actual signature can be smaller than DSA_size()
max_signature_size = static_cast<size_t>(DSA_size(dsa.get()));
unsigned char* p = OpenSSLWriteInto(&signature,
max_signature_size);
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());
if (ret != 1) {
LOG(ERROR) << "DSA_sign() failed: " << GetOpenSSLErrorString();
return false;
}
signature_size = static_cast<size_t>(p_len);
break;
}
case EVP_PKEY_EC:
{
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();
return false;
}
// Note, the actual signature can be smaller than ECDSA_size()
max_signature_size = ECDSA_size(ecdsa.get());
unsigned char* p = OpenSSLWriteInto(&signature,
max_signature_size);
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());
if (ret != 1) {
LOG(ERROR) << "ECDSA_sign() fialed: " << GetOpenSSLErrorString();
return false;
}
signature_size = static_cast<size_t>(p_len);
break;
}
default:
LOG(WARNING) << "Invalid OpenSSL key type: " << key_type;
return false;
}
if (signature_size == 0) {
LOG(ERROR) << "Signature is empty!";
return false;
}
if (signature_size > max_signature_size) {
LOG(ERROR) << "Signature size mismatch, actual " << signature_size
<< ", expected <= " << max_signature_size;
return false;
}
signature.resize(signature_size);
result->swap(signature);
return true;
}
// 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.
// Returns true on success, false otherwise.
bool CompareSignatureWithOpenSSL(const base::StringPiece& message,
const base::StringPiece& signature,
EVP_PKEY* openssl_key) {
std::string openssl_signature;
SignWithOpenSSL(message, openssl_key, &openssl_signature);
if (signature.size() != openssl_signature.size()) {
LOG(ERROR) << "Signature size mismatch, actual "
<< signature.size() << ", expected "
<< openssl_signature.size();
return false;
}
for (size_t n = 0; n < signature.size(); ++n) {
if (openssl_signature[n] != signature[n]) {
LOG(ERROR) << "Signature byte mismatch at index " << n
<< "actual " << signature[n] << ", expected "
<< openssl_signature[n];
LOG(ERROR) << "Actual signature : "
<< base::HexEncode(signature.data(), signature.size());
LOG(ERROR) << "Expected signature: "
<< base::HexEncode(openssl_signature.data(),
openssl_signature.size());
return false;
}
}
return true;
}
// 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.
crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestRsaKeyFile));
ASSERT_TRUE(pkey.get());
// Convert it to encoded PKCS#8 bytes.
std::string pkcs8_data;
ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data));
// Create platform PrivateKey object from it.
ScopedJava 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.
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.
crypto::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.
crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestDsaKeyFile));
ASSERT_TRUE(pkey.get());
// Convert it to encoded PKCS#8 bytes.
std::string pkcs8_data;
ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data));
// Create platform PrivateKey object from it.
ScopedJava 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.
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.
crypto::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);
ScopedJava rsa_key = GetRSATestKeyJava();
ASSERT_FALSE(rsa_key.is_null());
EXPECT_EQ(PRIVATE_KEY_TYPE_RSA,
GetPrivateKeyType(rsa_key.obj()));
}
TEST(AndroidKeyStore,SignWithPrivateKeyRSA) {
ScopedJava 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;
}
crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile));
ASSERT_TRUE(openssl_key.get());
std::string message = kTestRsaHash;
ASSERT_EQ(36U, message.size());
std::string signature;
DoKeySigning(rsa_key.obj(), openssl_key.get(), message, &signature);
ASSERT_TRUE(
CompareSignatureWithOpenSSL(message, signature, openssl_key.get()));
// All good.
}
TEST(AndroidKeyStore,SignWithWrapperKeyRSA) {
crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
ScopedJava rsa_key = GetRSATestKeyJava();
ASSERT_FALSE(rsa_key.is_null());
crypto::ScopedEVP_PKEY wrapper_key(
GetOpenSSLPrivateKeyWrapper(rsa_key.obj()));
ASSERT_TRUE(wrapper_key.get() != NULL);
crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile));
ASSERT_TRUE(openssl_key.get());
// 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 = kTestRsaHash;
ASSERT_EQ(36U, message.size());
std::string signature;
DoKeySigningWithWrapper(wrapper_key.get(),
openssl_key.get(),
message,
&signature);
ASSERT_TRUE(
CompareSignatureWithOpenSSL(message, signature, openssl_key.get()));
}
TEST(AndroidKeyStore,GetPrivateKeyTypeDSA) {
crypto::OpenSSLErrStackTracer err_trace(FROM_HERE);
ScopedJava dsa_key = GetDSATestKeyJava();
ASSERT_FALSE(dsa_key.is_null());
EXPECT_EQ(PRIVATE_KEY_TYPE_DSA,
GetPrivateKeyType(dsa_key.obj()));
}
TEST(AndroidKeyStore,SignWithPrivateKeyDSA) {
ScopedJava dsa_key = GetDSATestKeyJava();
ASSERT_FALSE(dsa_key.is_null());
crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile));
ASSERT_TRUE(openssl_key.get());
std::string message = kTestDsaHash;
ASSERT_EQ(20U, message.size());
std::string signature;
DoKeySigning(dsa_key.obj(), openssl_key.get(), message, &signature);
ASSERT_TRUE(VerifyTestDSASignature(message, signature));
}
TEST(AndroidKeyStore,SignWithWrapperKeyDSA) {
crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
ScopedJava dsa_key = GetDSATestKeyJava();
ASSERT_FALSE(dsa_key.is_null());
crypto::ScopedEVP_PKEY wrapper_key(
GetOpenSSLPrivateKeyWrapper(dsa_key.obj()));
ASSERT_TRUE(wrapper_key.get());
crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile));
ASSERT_TRUE(openssl_key.get());
// 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 = kTestDsaHash;
std::string signature;
DoKeySigningWithWrapper(wrapper_key.get(),
openssl_key.get(),
message,
&signature);
ASSERT_TRUE(VerifyTestDSASignature(message, signature));
}
TEST(AndroidKeyStore,GetPrivateKeyTypeECDSA) {
crypto::OpenSSLErrStackTracer err_trace(FROM_HERE);
ScopedJava ecdsa_key = GetECDSATestKeyJava();
ASSERT_FALSE(ecdsa_key.is_null());
EXPECT_EQ(PRIVATE_KEY_TYPE_ECDSA,
GetPrivateKeyType(ecdsa_key.obj()));
}
TEST(AndroidKeyStore,SignWithPrivateKeyECDSA) {
ScopedJava ecdsa_key = GetECDSATestKeyJava();
ASSERT_FALSE(ecdsa_key.is_null());
crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile));
ASSERT_TRUE(openssl_key.get());
std::string message = kTestEcdsaHash;
std::string signature;
DoKeySigning(ecdsa_key.obj(), openssl_key.get(), message, &signature);
ASSERT_TRUE(VerifyTestECDSASignature(message, signature));
}
TEST(AndroidKeyStore, SignWithWrapperKeyECDSA) {
crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
ScopedJava ecdsa_key = GetECDSATestKeyJava();
ASSERT_FALSE(ecdsa_key.is_null());
crypto::ScopedEVP_PKEY wrapper_key(
GetOpenSSLPrivateKeyWrapper(ecdsa_key.obj()));
ASSERT_TRUE(wrapper_key.get());
crypto::ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile));
ASSERT_TRUE(openssl_key.get());
// 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 = kTestEcdsaHash;
std::string signature;
DoKeySigningWithWrapper(wrapper_key.get(),
openssl_key.get(),
message,
&signature);
ASSERT_TRUE(VerifyTestECDSASignature(message, signature));
}
} // namespace android
} // namespace net
|