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

Side by Side Diff: net/base/keygen_handler_unittest.cc

Issue 1742873002: Switch //net to the new SPKI and PKCS#8 APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@spki-crypto
Patch Set: const_cast Created 4 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/keygen_handler.h" 5 #include "net/base/keygen_handler.h"
6 6
7 #include <openssl/bytestring.h>
8 #include <openssl/evp.h>
9 #include <stdint.h>
10
7 #include <string> 11 #include <string>
8 #include <utility> 12 #include <utility>
9 13
10 #include "base/base64.h" 14 #include "base/base64.h"
11 #include "base/bind.h" 15 #include "base/bind.h"
12 #include "base/location.h" 16 #include "base/location.h"
13 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/strings/string_piece.h"
14 #include "base/synchronization/waitable_event.h" 19 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread_restrictions.h" 20 #include "base/threading/thread_restrictions.h"
16 #include "base/threading/worker_pool.h" 21 #include "base/threading/worker_pool.h"
17 #include "build/build_config.h" 22 #include "build/build_config.h"
23 #include "crypto/scoped_openssl_types.h"
18 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
19 25
20 #if defined(USE_NSS_CERTS) 26 #if defined(USE_NSS_CERTS)
21 #include <private/pprthred.h> // PR_DetachThread 27 #include <private/pprthred.h> // PR_DetachThread
22 #include "crypto/nss_crypto_module_delegate.h" 28 #include "crypto/nss_crypto_module_delegate.h"
23 #include "crypto/scoped_test_nss_db.h" 29 #include "crypto/scoped_test_nss_db.h"
24 #endif 30 #endif
25 31
26 namespace net { 32 namespace net {
27 33
(...skipping 13 matching lines...) Expand all
41 47
42 crypto::ScopedPK11Slot RequestSlot() override { 48 crypto::ScopedPK11Slot RequestSlot() override {
43 return crypto::ScopedPK11Slot(PK11_ReferenceSlot(slot_.get())); 49 return crypto::ScopedPK11Slot(PK11_ReferenceSlot(slot_.get()));
44 } 50 }
45 51
46 private: 52 private:
47 crypto::ScopedPK11Slot slot_; 53 crypto::ScopedPK11Slot slot_;
48 }; 54 };
49 #endif 55 #endif
50 56
57 const char kChallenge[] = "some challenge";
58
51 class KeygenHandlerTest : public ::testing::Test { 59 class KeygenHandlerTest : public ::testing::Test {
52 public: 60 public:
53 KeygenHandlerTest() {} 61 KeygenHandlerTest() {}
54 ~KeygenHandlerTest() override {} 62 ~KeygenHandlerTest() override {}
55 63
56 scoped_ptr<KeygenHandler> CreateKeygenHandler() { 64 scoped_ptr<KeygenHandler> CreateKeygenHandler() {
57 scoped_ptr<KeygenHandler> handler(new KeygenHandler( 65 scoped_ptr<KeygenHandler> handler(
58 768, "some challenge", GURL("http://www.example.com"))); 66 new KeygenHandler(768, kChallenge, GURL("http://www.example.com")));
59 #if defined(USE_NSS_CERTS) 67 #if defined(USE_NSS_CERTS)
60 handler->set_crypto_module_delegate( 68 handler->set_crypto_module_delegate(
61 scoped_ptr<crypto::NSSCryptoModuleDelegate>( 69 scoped_ptr<crypto::NSSCryptoModuleDelegate>(
62 new StubCryptoModuleDelegate(crypto::ScopedPK11Slot( 70 new StubCryptoModuleDelegate(crypto::ScopedPK11Slot(
63 PK11_ReferenceSlot(test_nss_db_.slot()))))); 71 PK11_ReferenceSlot(test_nss_db_.slot())))));
64 #endif 72 #endif
65 return handler; 73 return handler;
66 } 74 }
67 75
68 private: 76 private:
69 #if defined(USE_NSS_CERTS) 77 #if defined(USE_NSS_CERTS)
70 crypto::ScopedTestNSSDB test_nss_db_; 78 crypto::ScopedTestNSSDB test_nss_db_;
71 #endif 79 #endif
72 }; 80 };
73 81
82 base::StringPiece StringPieceFromCBS(const CBS& cbs) {
83 return base::StringPiece(reinterpret_cast<const char*>(CBS_data(&cbs)),
84 CBS_len(&cbs));
85 }
86
74 // Assert that |result| is a valid output for KeygenHandler given challenge 87 // Assert that |result| is a valid output for KeygenHandler given challenge
75 // string of |challenge|. 88 // string of |challenge|.
76 void AssertValidSignedPublicKeyAndChallenge(const std::string& result, 89 void AssertValidSignedPublicKeyAndChallenge(const std::string& result,
77 const std::string& challenge) { 90 const std::string& challenge) {
78 ASSERT_GT(result.length(), 0U);
79
80 // Verify it's valid base64: 91 // Verify it's valid base64:
81 std::string spkac; 92 std::string spkac;
82 ASSERT_TRUE(base::Base64Decode(result, &spkac)); 93 ASSERT_TRUE(base::Base64Decode(result, &spkac));
83 // In lieu of actually parsing and validating the DER data,
84 // just check that it exists and has a reasonable length.
85 // (It's almost always 590 bytes, but the DER encoding of the random key
86 // and signature could sometimes be a few bytes different.)
87 ASSERT_GE(spkac.length(), 200U);
88 ASSERT_LE(spkac.length(), 300U);
89 94
90 // NOTE: 95 // Parse the following structure:
91 // The value of |result| can be validated by prefixing 'SPKAC=' to it
92 // and piping it through
93 // openssl spkac -verify
94 // whose output should look like:
95 // Netscape SPKI:
96 // Public Key Algorithm: rsaEncryption
97 // RSA Public Key: (2048 bit)
98 // Modulus (2048 bit):
99 // 00:b6:cc:14:c9:43:b5:2d:51:65:7e:11:8b:80:9e: .....
100 // Exponent: 65537 (0x10001)
101 // Challenge String: some challenge
102 // Signature Algorithm: md5WithRSAEncryption
103 // 92:f3:cc:ff:0b:d3:d0:4a:3a:4c:ba:ff:d6:38:7f:a5:4b:b5: .....
104 // Signature OK
105 // 96 //
106 // The value of |spkac| can be ASN.1-parsed with: 97 // PublicKeyAndChallenge ::= SEQUENCE {
107 // openssl asn1parse -inform DER 98 // spki SubjectPublicKeyInfo,
99 // challenge IA5STRING
100 // }
101 // SignedPublicKeyAndChallenge ::= SEQUENCE {
102 // publicKeyAndChallenge PublicKeyAndChallenge,
103 // signatureAlgorithm AlgorithmIdentifier,
104 // signature BIT STRING
105 // }
106
107 CBS cbs;
108 CBS_init(&cbs, reinterpret_cast<const uint8_t*>(spkac.data()), spkac.size());
109
110 // The input should consist of a SEQUENCE.
111 CBS child;
112 ASSERT_TRUE(CBS_get_asn1(&cbs, &child, CBS_ASN1_SEQUENCE));
113 ASSERT_EQ(0u, CBS_len(&cbs));
114
115 // Extract the raw PublicKeyAndChallenge.
116 CBS public_key_and_challenge_raw;
117 ASSERT_TRUE(CBS_get_asn1_element(&child, &public_key_and_challenge_raw,
118 CBS_ASN1_SEQUENCE));
119
120 // Parse out the PublicKeyAndChallenge.
121 CBS copy = public_key_and_challenge_raw;
122 CBS public_key_and_challenge;
123 ASSERT_TRUE(
124 CBS_get_asn1(&copy, &public_key_and_challenge, CBS_ASN1_SEQUENCE));
125 ASSERT_EQ(0u, CBS_len(&copy));
126 crypto::ScopedEVP_PKEY key(EVP_parse_public_key(&public_key_and_challenge));
127 ASSERT_TRUE(key);
128 CBS challenge_spkac;
129 ASSERT_TRUE(CBS_get_asn1(&public_key_and_challenge, &challenge_spkac,
130 CBS_ASN1_IA5STRING));
131 ASSERT_EQ(0u, CBS_len(&public_key_and_challenge));
132
133 // The challenge must match.
134 ASSERT_EQ(challenge, StringPieceFromCBS(challenge_spkac));
135
136 // The next element must be the AlgorithmIdentifier for MD5 with RSA.
137 static const uint8_t kMd5WithRsaEncryption[] = {
138 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
139 0xf7, 0x0d, 0x01, 0x01, 0x04, 0x05, 0x00,
140 };
141 CBS algorithm;
142 ASSERT_TRUE(CBS_get_bytes(&child, &algorithm, sizeof(kMd5WithRsaEncryption)));
143 ASSERT_EQ(
144 base::StringPiece(reinterpret_cast<const char*>(kMd5WithRsaEncryption),
145 sizeof(kMd5WithRsaEncryption)),
146 StringPieceFromCBS(algorithm));
147
148 // Finally, parse the signature.
149 CBS signature;
150 ASSERT_TRUE(CBS_get_asn1(&child, &signature, CBS_ASN1_BITSTRING));
151 ASSERT_EQ(0u, CBS_len(&child));
152 uint8_t pad;
153 ASSERT_TRUE(CBS_get_u8(&signature, &pad));
154 ASSERT_EQ(0u, pad);
155
156 // Check the signature.
157 crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create());
158 ASSERT_TRUE(
159 EVP_DigestVerifyInit(ctx.get(), nullptr, EVP_md5(), nullptr, key.get()));
160 ASSERT_TRUE(EVP_DigestVerifyUpdate(ctx.get(),
161 CBS_data(&public_key_and_challenge_raw),
162 CBS_len(&public_key_and_challenge_raw)));
163 ASSERT_TRUE(EVP_DigestVerifyFinal(ctx.get(), CBS_data(&signature),
164 CBS_len(&signature)));
108 } 165 }
109 166
110 TEST_F(KeygenHandlerTest, SmokeTest) { 167 TEST_F(KeygenHandlerTest, SmokeTest) {
111 scoped_ptr<KeygenHandler> handler(CreateKeygenHandler()); 168 scoped_ptr<KeygenHandler> handler(CreateKeygenHandler());
112 handler->set_stores_key(false); // Don't leave the key-pair behind 169 handler->set_stores_key(false); // Don't leave the key-pair behind
113 std::string result = handler->GenKeyAndSignChallenge(); 170 std::string result = handler->GenKeyAndSignChallenge();
114 VLOG(1) << "KeygenHandler produced: " << result; 171 VLOG(1) << "KeygenHandler produced: " << result;
115 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); 172 AssertValidSignedPublicKeyAndChallenge(result, kChallenge);
116 } 173 }
117 174
118 void ConcurrencyTestCallback(const std::string& challenge, 175 void ConcurrencyTestCallback(const std::string& challenge,
119 base::WaitableEvent* event, 176 base::WaitableEvent* event,
120 scoped_ptr<KeygenHandler> handler, 177 scoped_ptr<KeygenHandler> handler,
121 std::string* result) { 178 std::string* result) {
122 // We allow Singleton use on the worker thread here since we use a 179 // We allow Singleton use on the worker thread here since we use a
123 // WaitableEvent to synchronize, so it's safe. 180 // WaitableEvent to synchronize, so it's safe.
124 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; 181 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton;
125 handler->set_stores_key(false); // Don't leave the key-pair behind. 182 handler->set_stores_key(false); // Don't leave the key-pair behind.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 events[i] = NULL; 219 events[i] = NULL;
163 220
164 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; 221 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i];
165 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); 222 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge");
166 } 223 }
167 } 224 }
168 225
169 } // namespace 226 } // namespace
170 227
171 } // namespace net 228 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698