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> |
(...skipping 20 matching lines...) Expand all Loading... |
31 namespace test { | 31 namespace test { |
32 | 32 |
33 class TestChannelIDKey : public ChannelIDKey { | 33 class TestChannelIDKey : public ChannelIDKey { |
34 public: | 34 public: |
35 explicit TestChannelIDKey(EVP_PKEY* ecdsa_key) : ecdsa_key_(ecdsa_key) {} | 35 explicit TestChannelIDKey(EVP_PKEY* ecdsa_key) : ecdsa_key_(ecdsa_key) {} |
36 virtual ~TestChannelIDKey() { } | 36 virtual ~TestChannelIDKey() { } |
37 | 37 |
38 // ChannelIDKey implementation. | 38 // ChannelIDKey implementation. |
39 | 39 |
40 virtual bool Sign(StringPiece signed_data, | 40 virtual bool Sign(StringPiece signed_data, |
41 string* out_signature) OVERRIDE { | 41 string* out_signature) const OVERRIDE { |
42 EVP_MD_CTX md_ctx; | 42 EVP_MD_CTX md_ctx; |
43 EVP_MD_CTX_init(&md_ctx); | 43 EVP_MD_CTX_init(&md_ctx); |
44 crypto::ScopedOpenSSL<EVP_MD_CTX, EvpMdCtxCleanUp> | 44 crypto::ScopedOpenSSL<EVP_MD_CTX, EvpMdCtxCleanUp> |
45 md_ctx_cleanup(&md_ctx); | 45 md_ctx_cleanup(&md_ctx); |
46 | 46 |
47 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, | 47 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, |
48 ecdsa_key_.get()) != 1) { | 48 ecdsa_key_.get()) != 1) { |
49 return false; | 49 return false; |
50 } | 50 } |
51 | 51 |
(...skipping 26 matching lines...) Expand all Loading... |
78 memset(signature.get(), 0, kSignatureLength); | 78 memset(signature.get(), 0, kSignatureLength); |
79 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)); |
80 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)); |
81 | 81 |
82 *out_signature = string(reinterpret_cast<char*>(signature.get()), | 82 *out_signature = string(reinterpret_cast<char*>(signature.get()), |
83 kSignatureLength); | 83 kSignatureLength); |
84 | 84 |
85 return true; | 85 return true; |
86 } | 86 } |
87 | 87 |
88 virtual string SerializeKey() OVERRIDE { | 88 virtual string SerializeKey() const OVERRIDE { |
89 // i2d_PublicKey will produce an ANSI X9.62 public key which, for a P-256 | 89 // i2d_PublicKey will produce an ANSI X9.62 public key which, for a P-256 |
90 // key, is 0x04 (meaning uncompressed) followed by the x and y field | 90 // key, is 0x04 (meaning uncompressed) followed by the x and y field |
91 // elements as 32-byte, big-endian numbers. | 91 // elements as 32-byte, big-endian numbers. |
92 static const int kExpectedKeyLength = 65; | 92 static const int kExpectedKeyLength = 65; |
93 | 93 |
94 int len = i2d_PublicKey(ecdsa_key_.get(), NULL); | 94 int len = i2d_PublicKey(ecdsa_key_.get(), NULL); |
95 if (len != kExpectedKeyLength) { | 95 if (len != kExpectedKeyLength) { |
96 return ""; | 96 return ""; |
97 } | 97 } |
98 | 98 |
99 uint8 buf[kExpectedKeyLength]; | 99 uint8 buf[kExpectedKeyLength]; |
100 uint8* derp = buf; | 100 uint8* derp = buf; |
101 i2d_PublicKey(ecdsa_key_.get(), &derp); | 101 i2d_PublicKey(ecdsa_key_.get(), &derp); |
102 | 102 |
103 return string(reinterpret_cast<char*>(buf + 1), kExpectedKeyLength - 1); | 103 return string(reinterpret_cast<char*>(buf + 1), kExpectedKeyLength - 1); |
104 } | 104 } |
105 | 105 |
106 private: | 106 private: |
107 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ecdsa_key_; | 107 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ecdsa_key_; |
108 }; | 108 }; |
109 | 109 |
110 class TestChannelIDSource : public ChannelIDSource { | 110 class TestChannelIDSource : public ChannelIDSource { |
111 public: | 111 public: |
112 virtual ~TestChannelIDSource() {} | 112 virtual ~TestChannelIDSource() {} |
113 | 113 |
114 // ChannelIDSource implementation. | 114 // ChannelIDSource implementation. |
115 | 115 |
116 virtual bool GetChannelIDKey( | 116 virtual QuicAsyncStatus GetChannelIDKey( |
117 const string& hostname, | 117 const string& hostname, |
118 scoped_ptr<ChannelIDKey>* channel_id_key) OVERRIDE { | 118 scoped_ptr<ChannelIDKey>* channel_id_key, |
| 119 ChannelIDSourceCallback* /*callback*/) OVERRIDE { |
119 channel_id_key->reset(new TestChannelIDKey(HostnameToKey(hostname))); | 120 channel_id_key->reset(new TestChannelIDKey(HostnameToKey(hostname))); |
120 return true; | 121 return QUIC_SUCCESS; |
121 } | 122 } |
122 | 123 |
123 private: | 124 private: |
124 static EVP_PKEY* HostnameToKey(const string& hostname) { | 125 static EVP_PKEY* HostnameToKey(const string& hostname) { |
125 // In order to generate a deterministic key for a given hostname the | 126 // In order to generate a deterministic key for a given hostname the |
126 // hostname is hashed with SHA-256 and the resulting digest is treated as a | 127 // hostname is hashed with SHA-256 and the resulting digest is treated as a |
127 // big-endian number. The most-significant bit is cleared to ensure that | 128 // big-endian number. The most-significant bit is cleared to ensure that |
128 // the resulting value is less than the order of the group and then it's | 129 // the resulting value is less than the order of the group and then it's |
129 // taken as a private key. Given the private key, the public key is | 130 // taken as a private key. Given the private key, the public key is |
130 // calculated with a group multiplication. | 131 // calculated with a group multiplication. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 }; | 167 }; |
167 | 168 |
168 // static | 169 // static |
169 ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() { | 170 ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() { |
170 return new TestChannelIDSource(); | 171 return new TestChannelIDSource(); |
171 } | 172 } |
172 | 173 |
173 } // namespace test | 174 } // namespace test |
174 | 175 |
175 } // namespace net | 176 } // namespace net |
OLD | NEW |