OLD | NEW |
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> |
11 #include <openssl/obj_mac.h> | 11 #include <openssl/obj_mac.h> |
12 #include <openssl/sha.h> | 12 #include <openssl/sha.h> |
13 | 13 |
14 #include "crypto/openssl_util.h" | 14 #include "crypto/openssl_util.h" |
15 #include "crypto/scoped_openssl_types.h" | 15 #include "crypto/scoped_openssl_types.h" |
16 #include "crypto/secure_hash.h" | 16 #include "crypto/secure_hash.h" |
17 #include "net/quic/crypto/channel_id.h" | 17 #include "net/quic/crypto/channel_id.h" |
18 | 18 |
19 using base::StringPiece; | 19 using base::StringPiece; |
20 using std::string; | 20 using std::string; |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 namespace test { | 24 namespace test { |
25 | 25 |
26 class TestChannelIDKey : public ChannelIDKey { | 26 class TestChannelIDKey : public ChannelIDKey { |
27 public: | 27 public: |
28 explicit TestChannelIDKey(EVP_PKEY* ecdsa_key) : ecdsa_key_(ecdsa_key) {} | 28 explicit TestChannelIDKey(EVP_PKEY* ecdsa_key) : ecdsa_key_(ecdsa_key) {} |
29 virtual ~TestChannelIDKey() override {} | 29 ~TestChannelIDKey() override {} |
30 | 30 |
31 // ChannelIDKey implementation. | 31 // ChannelIDKey implementation. |
32 | 32 |
33 virtual bool Sign(StringPiece signed_data, | 33 bool Sign(StringPiece signed_data, string* out_signature) const override { |
34 string* out_signature) const override { | |
35 crypto::ScopedEVP_MD_CTX md_ctx(EVP_MD_CTX_create()); | 34 crypto::ScopedEVP_MD_CTX md_ctx(EVP_MD_CTX_create()); |
36 if (!md_ctx || | 35 if (!md_ctx || |
37 EVP_DigestSignInit(md_ctx.get(), nullptr, EVP_sha256(), nullptr, | 36 EVP_DigestSignInit(md_ctx.get(), nullptr, EVP_sha256(), nullptr, |
38 ecdsa_key_.get()) != 1) { | 37 ecdsa_key_.get()) != 1) { |
39 return false; | 38 return false; |
40 } | 39 } |
41 | 40 |
42 EVP_DigestUpdate(md_ctx.get(), ChannelIDVerifier::kContextStr, | 41 EVP_DigestUpdate(md_ctx.get(), ChannelIDVerifier::kContextStr, |
43 strlen(ChannelIDVerifier::kContextStr) + 1); | 42 strlen(ChannelIDVerifier::kContextStr) + 1); |
44 EVP_DigestUpdate(md_ctx.get(), ChannelIDVerifier::kClientToServerStr, | 43 EVP_DigestUpdate(md_ctx.get(), ChannelIDVerifier::kClientToServerStr, |
(...skipping 23 matching lines...) Expand all Loading... |
68 memset(signature.get(), 0, kSignatureLength); | 67 memset(signature.get(), 0, kSignatureLength); |
69 BN_bn2bin(sig.get()->r, signature.get() + 32 - BN_num_bytes(sig.get()->r)); | 68 BN_bn2bin(sig.get()->r, signature.get() + 32 - BN_num_bytes(sig.get()->r)); |
70 BN_bn2bin(sig.get()->s, signature.get() + 64 - BN_num_bytes(sig.get()->s)); | 69 BN_bn2bin(sig.get()->s, signature.get() + 64 - BN_num_bytes(sig.get()->s)); |
71 | 70 |
72 *out_signature = string(reinterpret_cast<char*>(signature.get()), | 71 *out_signature = string(reinterpret_cast<char*>(signature.get()), |
73 kSignatureLength); | 72 kSignatureLength); |
74 | 73 |
75 return true; | 74 return true; |
76 } | 75 } |
77 | 76 |
78 virtual string SerializeKey() const override { | 77 string SerializeKey() const override { |
79 // i2d_PublicKey will produce an ANSI X9.62 public key which, for a P-256 | 78 // i2d_PublicKey will produce an ANSI X9.62 public key which, for a P-256 |
80 // key, is 0x04 (meaning uncompressed) followed by the x and y field | 79 // key, is 0x04 (meaning uncompressed) followed by the x and y field |
81 // elements as 32-byte, big-endian numbers. | 80 // elements as 32-byte, big-endian numbers. |
82 static const int kExpectedKeyLength = 65; | 81 static const int kExpectedKeyLength = 65; |
83 | 82 |
84 int len = i2d_PublicKey(ecdsa_key_.get(), nullptr); | 83 int len = i2d_PublicKey(ecdsa_key_.get(), nullptr); |
85 if (len != kExpectedKeyLength) { | 84 if (len != kExpectedKeyLength) { |
86 return ""; | 85 return ""; |
87 } | 86 } |
88 | 87 |
89 uint8 buf[kExpectedKeyLength]; | 88 uint8 buf[kExpectedKeyLength]; |
90 uint8* derp = buf; | 89 uint8* derp = buf; |
91 i2d_PublicKey(ecdsa_key_.get(), &derp); | 90 i2d_PublicKey(ecdsa_key_.get(), &derp); |
92 | 91 |
93 return string(reinterpret_cast<char*>(buf + 1), kExpectedKeyLength - 1); | 92 return string(reinterpret_cast<char*>(buf + 1), kExpectedKeyLength - 1); |
94 } | 93 } |
95 | 94 |
96 private: | 95 private: |
97 crypto::ScopedEVP_PKEY ecdsa_key_; | 96 crypto::ScopedEVP_PKEY ecdsa_key_; |
98 }; | 97 }; |
99 | 98 |
100 class TestChannelIDSource : public ChannelIDSource { | 99 class TestChannelIDSource : public ChannelIDSource { |
101 public: | 100 public: |
102 virtual ~TestChannelIDSource() {} | 101 ~TestChannelIDSource() override {} |
103 | 102 |
104 // ChannelIDSource implementation. | 103 // ChannelIDSource implementation. |
105 | 104 |
106 virtual QuicAsyncStatus GetChannelIDKey( | 105 QuicAsyncStatus GetChannelIDKey( |
107 const string& hostname, | 106 const string& hostname, |
108 scoped_ptr<ChannelIDKey>* channel_id_key, | 107 scoped_ptr<ChannelIDKey>* channel_id_key, |
109 ChannelIDSourceCallback* /*callback*/) override { | 108 ChannelIDSourceCallback* /*callback*/) override { |
110 channel_id_key->reset(new TestChannelIDKey(HostnameToKey(hostname))); | 109 channel_id_key->reset(new TestChannelIDKey(HostnameToKey(hostname))); |
111 return QUIC_SUCCESS; | 110 return QUIC_SUCCESS; |
112 } | 111 } |
113 | 112 |
114 private: | 113 private: |
115 static EVP_PKEY* HostnameToKey(const string& hostname) { | 114 static EVP_PKEY* HostnameToKey(const string& hostname) { |
116 // In order to generate a deterministic key for a given hostname the | 115 // In order to generate a deterministic key for a given hostname the |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 }; | 157 }; |
159 | 158 |
160 // static | 159 // static |
161 ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() { | 160 ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() { |
162 return new TestChannelIDSource(); | 161 return new TestChannelIDSource(); |
163 } | 162 } |
164 | 163 |
165 } // namespace test | 164 } // namespace test |
166 | 165 |
167 } // namespace net | 166 } // namespace net |
OLD | NEW |