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/secure_hash.h" | 15 #include "crypto/secure_hash.h" |
16 #include "net/quic/crypto/channel_id.h" | 16 #include "net/quic/crypto/channel_id.h" |
17 | 17 |
18 using base::StringPiece; | 18 using base::StringPiece; |
19 using std::string; | 19 using std::string; |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
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 TestChannelIDSigner : public ChannelIDSigner { |
34 public: | 34 public: |
35 virtual ~TestChannelIDSigner() { } | 35 virtual ~TestChannelIDSigner() {} |
36 | 36 |
37 // ChannelIDSigner implementation. | 37 // ChannelIDSigner implementation. |
38 | 38 |
39 virtual bool Sign(const string& hostname, | 39 virtual bool Sign(const string& hostname, |
40 StringPiece signed_data, | 40 StringPiece signed_data, |
41 string* out_key, | 41 string* out_key, |
42 string* out_signature) OVERRIDE { | 42 string* out_signature) OVERRIDE { |
43 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ecdsa_key( | 43 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ecdsa_key( |
44 HostnameToKey(hostname)); | 44 HostnameToKey(hostname)); |
45 | 45 |
46 *out_key = SerializeKey(ecdsa_key.get()); | 46 *out_key = SerializeKey(ecdsa_key.get()); |
47 if (out_key->empty()) { | 47 if (out_key->empty()) { |
48 return false; | 48 return false; |
49 } | 49 } |
50 | 50 |
51 EVP_MD_CTX md_ctx; | 51 EVP_MD_CTX md_ctx; |
52 EVP_MD_CTX_init(&md_ctx); | 52 EVP_MD_CTX_init(&md_ctx); |
53 crypto::ScopedOpenSSL<EVP_MD_CTX, EvpMdCtxCleanUp> | 53 crypto::ScopedOpenSSL<EVP_MD_CTX, EvpMdCtxCleanUp> md_ctx_cleanup(&md_ctx); |
54 md_ctx_cleanup(&md_ctx); | |
55 | 54 |
56 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, | 55 if (EVP_DigestSignInit( |
57 ecdsa_key.get()) != 1) { | 56 &md_ctx, NULL, EVP_sha256(), NULL, ecdsa_key.get()) != 1) { |
58 return false; | 57 return false; |
59 } | 58 } |
60 | 59 |
61 EVP_DigestUpdate(&md_ctx, ChannelIDVerifier::kContextStr, | 60 EVP_DigestUpdate(&md_ctx, |
| 61 ChannelIDVerifier::kContextStr, |
62 strlen(ChannelIDVerifier::kContextStr) + 1); | 62 strlen(ChannelIDVerifier::kContextStr) + 1); |
63 EVP_DigestUpdate(&md_ctx, ChannelIDVerifier::kClientToServerStr, | 63 EVP_DigestUpdate(&md_ctx, |
| 64 ChannelIDVerifier::kClientToServerStr, |
64 strlen(ChannelIDVerifier::kClientToServerStr) + 1); | 65 strlen(ChannelIDVerifier::kClientToServerStr) + 1); |
65 EVP_DigestUpdate(&md_ctx, signed_data.data(), signed_data.size()); | 66 EVP_DigestUpdate(&md_ctx, signed_data.data(), signed_data.size()); |
66 | 67 |
67 size_t sig_len; | 68 size_t sig_len; |
68 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) { | 69 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) { |
69 return false; | 70 return false; |
70 } | 71 } |
71 | 72 |
72 scoped_ptr<uint8[]> der_sig(new uint8[sig_len]); | 73 scoped_ptr<uint8[]> der_sig(new uint8[sig_len]); |
73 if (!EVP_DigestSignFinal(&md_ctx, der_sig.get(), &sig_len)) { | 74 if (!EVP_DigestSignFinal(&md_ctx, der_sig.get(), &sig_len)) { |
74 return false; | 75 return false; |
75 } | 76 } |
76 | 77 |
77 uint8* derp = der_sig.get(); | 78 uint8* derp = der_sig.get(); |
78 crypto::ScopedOpenSSL<ECDSA_SIG, ECDSA_SIG_free> sig( | 79 crypto::ScopedOpenSSL<ECDSA_SIG, ECDSA_SIG_free> sig( |
79 d2i_ECDSA_SIG(NULL, const_cast<const uint8**>(&derp), sig_len)); | 80 d2i_ECDSA_SIG(NULL, const_cast<const uint8**>(&derp), sig_len)); |
80 if (sig.get() == NULL) { | 81 if (sig.get() == NULL) { |
81 return false; | 82 return false; |
82 } | 83 } |
83 | 84 |
84 // The signature consists of a pair of 32-byte numbers. | 85 // The signature consists of a pair of 32-byte numbers. |
85 static const size_t kSignatureLength = 32 * 2; | 86 static const size_t kSignatureLength = 32 * 2; |
86 scoped_ptr<uint8[]> signature(new uint8[kSignatureLength]); | 87 scoped_ptr<uint8[]> signature(new uint8[kSignatureLength]); |
87 memset(signature.get(), 0, kSignatureLength); | 88 memset(signature.get(), 0, kSignatureLength); |
88 BN_bn2bin(sig.get()->r, signature.get() + 32 - BN_num_bytes(sig.get()->r)); | 89 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)); | 90 BN_bn2bin(sig.get()->s, signature.get() + 64 - BN_num_bytes(sig.get()->s)); |
90 | 91 |
91 *out_signature = string(reinterpret_cast<char*>(signature.get()), | 92 *out_signature = |
92 kSignatureLength); | 93 string(reinterpret_cast<char*>(signature.get()), kSignatureLength); |
93 | 94 |
94 return true; | 95 return true; |
95 } | 96 } |
96 | 97 |
97 virtual string GetKeyForHostname(const string& hostname) OVERRIDE { | 98 virtual string GetKeyForHostname(const string& hostname) OVERRIDE { |
98 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ecdsa_key( | 99 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ecdsa_key( |
99 HostnameToKey(hostname)); | 100 HostnameToKey(hostname)); |
100 return SerializeKey(ecdsa_key.get()); | 101 return SerializeKey(ecdsa_key.get()); |
101 } | 102 } |
102 | 103 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 }; | 165 }; |
165 | 166 |
166 // static | 167 // static |
167 ChannelIDSigner* CryptoTestUtils::ChannelIDSignerForTesting() { | 168 ChannelIDSigner* CryptoTestUtils::ChannelIDSignerForTesting() { |
168 return new TestChannelIDSigner(); | 169 return new TestChannelIDSigner(); |
169 } | 170 } |
170 | 171 |
171 } // namespace test | 172 } // namespace test |
172 | 173 |
173 } // namespace net | 174 } // namespace net |
OLD | NEW |