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

Side by Side Diff: net/quic/test_tools/crypto_test_utils_openssl.cc

Issue 300223007: Break ChannelIDSigner into two classes: ChannelIDKey and ChannelIDSource. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed copy-and-paste error Created 6 years, 6 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
« no previous file with comments | « net/quic/test_tools/crypto_test_utils.cc ('k') | net/tools/quic/quic_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/quic/test_tools/crypto_test_utils.h" 5 #include "net/quic/test_tools/crypto_test_utils.h"
6 6
7 #include <openssl/bn.h> 7 #include <openssl/bn.h>
8 #include <openssl/ec.h> 8 #include <openssl/ec.h>
9 #include <openssl/ecdsa.h> 9 #include <openssl/ecdsa.h>
10 #include <openssl/evp.h> 10 #include <openssl/evp.h>
(...skipping 12 matching lines...) Expand all
23 void EvpMdCtxCleanUp(EVP_MD_CTX* ctx) { 23 void EvpMdCtxCleanUp(EVP_MD_CTX* ctx) {
24 (void)EVP_MD_CTX_cleanup(ctx); 24 (void)EVP_MD_CTX_cleanup(ctx);
25 } 25 }
26 26
27 } // namespace anonymous 27 } // namespace anonymous
28 28
29 namespace net { 29 namespace net {
30 30
31 namespace test { 31 namespace test {
32 32
33 class TestChannelIDSigner : public ChannelIDSigner { 33 class TestChannelIDKey : public ChannelIDKey {
wtc 2014/05/28 22:04:29 net/quic/test_tools/crypto_test_utils_nss.cc also
34 public: 34 public:
35 virtual ~TestChannelIDSigner() { } 35 explicit TestChannelIDKey(EVP_PKEY* ecdsa_key) : ecdsa_key_(ecdsa_key) {}
36 virtual ~TestChannelIDKey() { }
36 37
37 // ChannelIDSigner implementation. 38 // ChannelIDKey implementation.
38 39
39 virtual bool Sign(const string& hostname, 40 virtual bool Sign(StringPiece signed_data,
40 StringPiece signed_data, 41 string* out_signature) override {
41 string* out_key,
42 string* out_signature) OVERRIDE {
43 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ecdsa_key(
44 HostnameToKey(hostname));
45
46 *out_key = SerializeKey(ecdsa_key.get());
47 if (out_key->empty()) {
48 return false;
49 }
50
51 EVP_MD_CTX md_ctx; 42 EVP_MD_CTX md_ctx;
52 EVP_MD_CTX_init(&md_ctx); 43 EVP_MD_CTX_init(&md_ctx);
53 crypto::ScopedOpenSSL<EVP_MD_CTX, EvpMdCtxCleanUp> 44 crypto::ScopedOpenSSL<EVP_MD_CTX, EvpMdCtxCleanUp>
54 md_ctx_cleanup(&md_ctx); 45 md_ctx_cleanup(&md_ctx);
55 46
56 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, 47 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
57 ecdsa_key.get()) != 1) { 48 ecdsa_key_.get()) != 1) {
58 return false; 49 return false;
59 } 50 }
60 51
61 EVP_DigestUpdate(&md_ctx, ChannelIDVerifier::kContextStr, 52 EVP_DigestUpdate(&md_ctx, ChannelIDVerifier::kContextStr,
62 strlen(ChannelIDVerifier::kContextStr) + 1); 53 strlen(ChannelIDVerifier::kContextStr) + 1);
63 EVP_DigestUpdate(&md_ctx, ChannelIDVerifier::kClientToServerStr, 54 EVP_DigestUpdate(&md_ctx, ChannelIDVerifier::kClientToServerStr,
64 strlen(ChannelIDVerifier::kClientToServerStr) + 1); 55 strlen(ChannelIDVerifier::kClientToServerStr) + 1);
65 EVP_DigestUpdate(&md_ctx, signed_data.data(), signed_data.size()); 56 EVP_DigestUpdate(&md_ctx, signed_data.data(), signed_data.size());
66 57
67 size_t sig_len; 58 size_t sig_len;
(...skipping 19 matching lines...) Expand all
87 memset(signature.get(), 0, kSignatureLength); 78 memset(signature.get(), 0, kSignatureLength);
88 BN_bn2bin(sig.get()->r, signature.get() + 32 - BN_num_bytes(sig.get()->r)); 79 BN_bn2bin(sig.get()->r, signature.get() + 32 - BN_num_bytes(sig.get()->r));
89 BN_bn2bin(sig.get()->s, signature.get() + 64 - BN_num_bytes(sig.get()->s)); 80 BN_bn2bin(sig.get()->s, signature.get() + 64 - BN_num_bytes(sig.get()->s));
90 81
91 *out_signature = string(reinterpret_cast<char*>(signature.get()), 82 *out_signature = string(reinterpret_cast<char*>(signature.get()),
92 kSignatureLength); 83 kSignatureLength);
93 84
94 return true; 85 return true;
95 } 86 }
96 87
97 virtual string GetKeyForHostname(const string& hostname) OVERRIDE { 88 virtual string SerializeKey() override {
98 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ecdsa_key( 89 // i2d_PublicKey will produce an ANSI X9.62 public key which, for a P-256
99 HostnameToKey(hostname)); 90 // key, is 0x04 (meaning uncompressed) followed by the x and y field
100 return SerializeKey(ecdsa_key.get()); 91 // elements as 32-byte, big-endian numbers.
92 static const int kExpectedKeyLength = 65;
93
94 int len = i2d_PublicKey(ecdsa_key_.get(), NULL);
95 if (len != kExpectedKeyLength) {
96 return "";
97 }
98
99 uint8 buf[kExpectedKeyLength];
100 uint8* derp = buf;
101 i2d_PublicKey(ecdsa_key_.get(), &derp);
102
103 return string(reinterpret_cast<char*>(buf + 1), kExpectedKeyLength - 1);
101 } 104 }
102 105
103 private: 106 private:
107 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ecdsa_key_;
108 };
109
110 class TestChannelIDSource : public ChannelIDSource {
111 public:
112 virtual ~TestChannelIDSource() {}
113
114 // ChannelIDSource implementation.
115
116 virtual bool GetChannelIDKey(
117 const string& hostname,
118 scoped_ptr<ChannelIDKey>* channel_id_key) override {
119 channel_id_key->reset(new TestChannelIDKey(HostnameToKey(hostname)));
120 return true;
121 }
122
123 private:
104 static EVP_PKEY* HostnameToKey(const string& hostname) { 124 static EVP_PKEY* HostnameToKey(const string& hostname) {
105 // In order to generate a deterministic key for a given hostname the 125 // In order to generate a deterministic key for a given hostname the
106 // hostname is hashed with SHA-256 and the resulting digest is treated as a 126 // hostname is hashed with SHA-256 and the resulting digest is treated as a
107 // big-endian number. The most-significant bit is cleared to ensure that 127 // big-endian number. The most-significant bit is cleared to ensure that
108 // the resulting value is less than the order of the group and then it's 128 // the resulting value is less than the order of the group and then it's
109 // taken as a private key. Given the private key, the public key is 129 // taken as a private key. Given the private key, the public key is
110 // calculated with a group multiplication. 130 // calculated with a group multiplication.
111 SHA256_CTX sha256; 131 SHA256_CTX sha256;
112 SHA256_Init(&sha256); 132 SHA256_Init(&sha256);
113 SHA256_Update(&sha256, hostname.data(), hostname.size()); 133 SHA256_Update(&sha256, hostname.data(), hostname.size());
(...skipping 22 matching lines...) Expand all
136 156
137 EC_KEY_set_private_key(ecdsa_key.get(), k.get()); 157 EC_KEY_set_private_key(ecdsa_key.get(), k.get());
138 EC_KEY_set_public_key(ecdsa_key.get(), point.get()); 158 EC_KEY_set_public_key(ecdsa_key.get(), point.get());
139 159
140 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> pkey(EVP_PKEY_new()); 160 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> pkey(EVP_PKEY_new());
141 // EVP_PKEY_set1_EC_KEY takes a reference so no |release| here. 161 // EVP_PKEY_set1_EC_KEY takes a reference so no |release| here.
142 EVP_PKEY_set1_EC_KEY(pkey.get(), ecdsa_key.get()); 162 EVP_PKEY_set1_EC_KEY(pkey.get(), ecdsa_key.get());
143 163
144 return pkey.release(); 164 return pkey.release();
145 } 165 }
146
147 static string SerializeKey(EVP_PKEY* key) {
148 // i2d_PublicKey will produce an ANSI X9.62 public key which, for a P-256
149 // key, is 0x04 (meaning uncompressed) followed by the x and y field
150 // elements as 32-byte, big-endian numbers.
151 static const int kExpectedKeyLength = 65;
152
153 int len = i2d_PublicKey(key, NULL);
154 if (len != kExpectedKeyLength) {
155 return "";
156 }
157
158 uint8 buf[kExpectedKeyLength];
159 uint8* derp = buf;
160 i2d_PublicKey(key, &derp);
161
162 return string(reinterpret_cast<char*>(buf + 1), kExpectedKeyLength - 1);
163 }
164 }; 166 };
165 167
166 // static 168 // static
167 ChannelIDSigner* CryptoTestUtils::ChannelIDSignerForTesting() { 169 ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() {
168 return new TestChannelIDSigner(); 170 return new TestChannelIDSource();
169 } 171 }
170 172
171 } // namespace test 173 } // namespace test
172 174
173 } // namespace net 175 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/test_tools/crypto_test_utils.cc ('k') | net/tools/quic/quic_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698