| 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 <keyhi.h> | 7 #include <keyhi.h> |
| 8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
| 9 #include <sechash.h> | 9 #include <sechash.h> |
| 10 | 10 |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "crypto/ec_private_key.h" | 13 #include "crypto/ec_private_key.h" |
| 14 #include "net/quic/crypto/channel_id.h" | 14 #include "net/quic/crypto/channel_id.h" |
| 15 | 15 |
| 16 using base::StringPiece; | 16 using base::StringPiece; |
| 17 using std::string; | 17 using std::string; |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 | 20 |
| 21 namespace test { | 21 namespace test { |
| 22 | 22 |
| 23 // TODO(rtenneti): Implement NSS support ChannelIDSigner. Convert Sign() to be | 23 // TODO(rtenneti): Implement NSS support ChannelIDSigner. Convert Sign() to be |
| 24 // asynchronous using completion callback. After porting TestChannelIDSigner, | 24 // asynchronous using completion callback. After porting TestChannelIDSigner, |
| 25 // implement real ChannelIDSigner. | 25 // implement real ChannelIDSigner. |
| 26 class TestChannelIDSigner : public ChannelIDSigner { | 26 class TestChannelIDSigner : public ChannelIDSigner { |
| 27 public: | 27 public: |
| 28 virtual ~TestChannelIDSigner() { | 28 virtual ~TestChannelIDSigner() { STLDeleteValues(&hostname_to_key_); } |
| 29 STLDeleteValues(&hostname_to_key_); | |
| 30 } | |
| 31 | 29 |
| 32 // ChannelIDSigner implementation. | 30 // ChannelIDSigner implementation. |
| 33 | 31 |
| 34 virtual bool Sign(const string& hostname, | 32 virtual bool Sign(const string& hostname, |
| 35 StringPiece signed_data, | 33 StringPiece signed_data, |
| 36 string* out_key, | 34 string* out_key, |
| 37 string* out_signature) OVERRIDE { | 35 string* out_signature) OVERRIDE { |
| 38 crypto::ECPrivateKey* ecdsa_keypair = HostnameToKey(hostname); | 36 crypto::ECPrivateKey* ecdsa_keypair = HostnameToKey(hostname); |
| 39 if (!ecdsa_keypair) { | 37 if (!ecdsa_keypair) { |
| 40 return false; | 38 return false; |
| 41 } | 39 } |
| 42 | 40 |
| 43 *out_key = SerializeKey(ecdsa_keypair->public_key()); | 41 *out_key = SerializeKey(ecdsa_keypair->public_key()); |
| 44 if (out_key->empty()) { | 42 if (out_key->empty()) { |
| 45 return false; | 43 return false; |
| 46 } | 44 } |
| 47 | 45 |
| 48 unsigned char hash_buf[SHA256_LENGTH]; | 46 unsigned char hash_buf[SHA256_LENGTH]; |
| 49 SECItem hash_item = { siBuffer, hash_buf, sizeof(hash_buf) }; | 47 SECItem hash_item = {siBuffer, hash_buf, sizeof(hash_buf)}; |
| 50 | 48 |
| 51 HASHContext* sha256 = HASH_Create(HASH_AlgSHA256); | 49 HASHContext* sha256 = HASH_Create(HASH_AlgSHA256); |
| 52 if (!sha256) { | 50 if (!sha256) { |
| 53 return false; | 51 return false; |
| 54 } | 52 } |
| 55 HASH_Begin(sha256); | 53 HASH_Begin(sha256); |
| 56 HASH_Update(sha256, | 54 HASH_Update( |
| 57 reinterpret_cast<const unsigned char*>( | 55 sha256, |
| 58 ChannelIDVerifier::kContextStr), | 56 reinterpret_cast<const unsigned char*>(ChannelIDVerifier::kContextStr), |
| 59 strlen(ChannelIDVerifier::kContextStr) + 1); | 57 strlen(ChannelIDVerifier::kContextStr) + 1); |
| 60 HASH_Update(sha256, | 58 HASH_Update(sha256, |
| 61 reinterpret_cast<const unsigned char*>( | 59 reinterpret_cast<const unsigned char*>( |
| 62 ChannelIDVerifier::kClientToServerStr), | 60 ChannelIDVerifier::kClientToServerStr), |
| 63 strlen(ChannelIDVerifier::kClientToServerStr) + 1); | 61 strlen(ChannelIDVerifier::kClientToServerStr) + 1); |
| 64 HASH_Update(sha256, | 62 HASH_Update(sha256, |
| 65 reinterpret_cast<const unsigned char*>(signed_data.data()), | 63 reinterpret_cast<const unsigned char*>(signed_data.data()), |
| 66 signed_data.size()); | 64 signed_data.size()); |
| 67 HASH_End(sha256, hash_buf, &hash_item.len, sizeof(hash_buf)); | 65 HASH_End(sha256, hash_buf, &hash_item.len, sizeof(hash_buf)); |
| 68 HASH_Destroy(sha256); | 66 HASH_Destroy(sha256); |
| 69 | 67 |
| 70 // The signature consists of a pair of 32-byte numbers. | 68 // The signature consists of a pair of 32-byte numbers. |
| 71 static const unsigned int kSignatureLength = 32 * 2; | 69 static const unsigned int kSignatureLength = 32 * 2; |
| 72 string signature; | 70 string signature; |
| 73 SECItem sig_item = { | 71 SECItem sig_item = {siBuffer, reinterpret_cast<unsigned char*>(WriteInto( |
| 74 siBuffer, | 72 &signature, kSignatureLength + 1)), |
| 75 reinterpret_cast<unsigned char*>( | 73 kSignatureLength}; |
| 76 WriteInto(&signature, kSignatureLength + 1)), | |
| 77 kSignatureLength | |
| 78 }; | |
| 79 | 74 |
| 80 if (PK11_Sign(ecdsa_keypair->key(), &sig_item, &hash_item) != SECSuccess) { | 75 if (PK11_Sign(ecdsa_keypair->key(), &sig_item, &hash_item) != SECSuccess) { |
| 81 return false; | 76 return false; |
| 82 } | 77 } |
| 83 *out_signature = signature; | 78 *out_signature = signature; |
| 84 return true; | 79 return true; |
| 85 } | 80 } |
| 86 | 81 |
| 87 virtual string GetKeyForHostname(const string& hostname) OVERRIDE { | 82 virtual string GetKeyForHostname(const string& hostname) OVERRIDE { |
| 88 crypto::ECPrivateKey* ecdsa_keypair = HostnameToKey(hostname); | 83 crypto::ECPrivateKey* ecdsa_keypair = HostnameToKey(hostname); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 }; | 124 }; |
| 130 | 125 |
| 131 // static | 126 // static |
| 132 ChannelIDSigner* CryptoTestUtils::ChannelIDSignerForTesting() { | 127 ChannelIDSigner* CryptoTestUtils::ChannelIDSignerForTesting() { |
| 133 return new TestChannelIDSigner(); | 128 return new TestChannelIDSigner(); |
| 134 } | 129 } |
| 135 | 130 |
| 136 } // namespace test | 131 } // namespace test |
| 137 | 132 |
| 138 } // namespace net | 133 } // namespace net |
| OLD | NEW |