| 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 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // callback. | 24 // callback. |
| 25 class TestChannelIDKey : public ChannelIDKey { | 25 class TestChannelIDKey : public ChannelIDKey { |
| 26 public: | 26 public: |
| 27 explicit TestChannelIDKey(crypto::ECPrivateKey* ecdsa_keypair) | 27 explicit TestChannelIDKey(crypto::ECPrivateKey* ecdsa_keypair) |
| 28 : ecdsa_keypair_(ecdsa_keypair) {} | 28 : ecdsa_keypair_(ecdsa_keypair) {} |
| 29 virtual ~TestChannelIDKey() {} | 29 virtual ~TestChannelIDKey() {} |
| 30 | 30 |
| 31 // ChannelIDKey implementation. | 31 // ChannelIDKey implementation. |
| 32 | 32 |
| 33 virtual bool Sign(StringPiece signed_data, | 33 virtual bool Sign(StringPiece signed_data, |
| 34 string* out_signature) OVERRIDE { | 34 string* out_signature) const OVERRIDE { |
| 35 unsigned char hash_buf[SHA256_LENGTH]; | 35 unsigned char hash_buf[SHA256_LENGTH]; |
| 36 SECItem hash_item = { siBuffer, hash_buf, sizeof(hash_buf) }; | 36 SECItem hash_item = { siBuffer, hash_buf, sizeof(hash_buf) }; |
| 37 | 37 |
| 38 HASHContext* sha256 = HASH_Create(HASH_AlgSHA256); | 38 HASHContext* sha256 = HASH_Create(HASH_AlgSHA256); |
| 39 if (!sha256) { | 39 if (!sha256) { |
| 40 return false; | 40 return false; |
| 41 } | 41 } |
| 42 HASH_Begin(sha256); | 42 HASH_Begin(sha256); |
| 43 HASH_Update(sha256, | 43 HASH_Update(sha256, |
| 44 reinterpret_cast<const unsigned char*>( | 44 reinterpret_cast<const unsigned char*>( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 64 kSignatureLength | 64 kSignatureLength |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 if (PK11_Sign(ecdsa_keypair_->key(), &sig_item, &hash_item) != SECSuccess) { | 67 if (PK11_Sign(ecdsa_keypair_->key(), &sig_item, &hash_item) != SECSuccess) { |
| 68 return false; | 68 return false; |
| 69 } | 69 } |
| 70 *out_signature = signature; | 70 *out_signature = signature; |
| 71 return true; | 71 return true; |
| 72 } | 72 } |
| 73 | 73 |
| 74 virtual string SerializeKey() OVERRIDE { | 74 virtual string SerializeKey() const OVERRIDE { |
| 75 static const unsigned int kExpectedKeyLength = 65; | 75 const SECKEYPublicKey* public_key = ecdsa_keypair_->public_key(); |
| 76 | 76 |
| 77 const SECKEYPublicKey* public_key = ecdsa_keypair_->public_key(); | |
| 78 // public_key->u.ec.publicValue is an ANSI X9.62 public key which, for | 77 // public_key->u.ec.publicValue is an ANSI X9.62 public key which, for |
| 79 // a P-256 key, is 0x04 (meaning uncompressed) followed by the x and y field | 78 // a P-256 key, is 0x04 (meaning uncompressed) followed by the x and y field |
| 80 // elements as 32-byte, big-endian numbers. | 79 // elements as 32-byte, big-endian numbers. |
| 80 static const unsigned int kExpectedKeyLength = 65; |
| 81 | 81 |
| 82 const unsigned char* const data = public_key->u.ec.publicValue.data; | 82 const unsigned char* const data = public_key->u.ec.publicValue.data; |
| 83 const unsigned int len = public_key->u.ec.publicValue.len; | 83 const unsigned int len = public_key->u.ec.publicValue.len; |
| 84 if (len != kExpectedKeyLength || data[0] != 0x04) { | 84 if (len != kExpectedKeyLength || data[0] != 0x04) { |
| 85 return ""; | 85 return ""; |
| 86 } | 86 } |
| 87 | 87 |
| 88 string key(reinterpret_cast<const char*>(data + 1), kExpectedKeyLength - 1); | 88 string key(reinterpret_cast<const char*>(data + 1), kExpectedKeyLength - 1); |
| 89 return key; | 89 return key; |
| 90 } | 90 } |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 crypto::ECPrivateKey* ecdsa_keypair_; | 93 crypto::ECPrivateKey* ecdsa_keypair_; |
| 94 }; | 94 }; |
| 95 | 95 |
| 96 class TestChannelIDSource : public ChannelIDSource { | 96 class TestChannelIDSource : public ChannelIDSource { |
| 97 public: | 97 public: |
| 98 virtual ~TestChannelIDSource() { | 98 virtual ~TestChannelIDSource() { |
| 99 STLDeleteValues(&hostname_to_key_); | 99 STLDeleteValues(&hostname_to_key_); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // ChannelIDSource implementation. | 102 // ChannelIDSource implementation. |
| 103 | 103 |
| 104 virtual bool GetChannelIDKey( | 104 virtual QuicAsyncStatus GetChannelIDKey( |
| 105 const string& hostname, | 105 const string& hostname, |
| 106 scoped_ptr<ChannelIDKey>* channel_id_key) OVERRIDE { | 106 scoped_ptr<ChannelIDKey>* channel_id_key, |
| 107 ChannelIDSourceCallback* /*callback*/) OVERRIDE { |
| 107 channel_id_key->reset(new TestChannelIDKey(HostnameToKey(hostname))); | 108 channel_id_key->reset(new TestChannelIDKey(HostnameToKey(hostname))); |
| 108 return true; | 109 return QUIC_SUCCESS; |
| 109 } | 110 } |
| 110 | 111 |
| 111 private: | 112 private: |
| 112 typedef std::map<string, crypto::ECPrivateKey*> HostnameToKeyMap; | 113 typedef std::map<string, crypto::ECPrivateKey*> HostnameToKeyMap; |
| 113 | 114 |
| 114 crypto::ECPrivateKey* HostnameToKey(const string& hostname) { | 115 crypto::ECPrivateKey* HostnameToKey(const string& hostname) { |
| 115 HostnameToKeyMap::const_iterator it = hostname_to_key_.find(hostname); | 116 HostnameToKeyMap::const_iterator it = hostname_to_key_.find(hostname); |
| 116 if (it != hostname_to_key_.end()) { | 117 if (it != hostname_to_key_.end()) { |
| 117 return it->second; | 118 return it->second; |
| 118 } | 119 } |
| 119 | 120 |
| 120 crypto::ECPrivateKey* keypair = crypto::ECPrivateKey::Create(); | 121 crypto::ECPrivateKey* keypair = crypto::ECPrivateKey::Create(); |
| 121 if (!keypair) { | 122 if (!keypair) { |
| 122 return NULL; | 123 return NULL; |
| 123 } | 124 } |
| 124 hostname_to_key_[hostname] = keypair; | 125 hostname_to_key_[hostname] = keypair; |
| 125 return keypair; | 126 return keypair; |
| 126 } | 127 } |
| 127 | 128 |
| 128 | |
| 129 HostnameToKeyMap hostname_to_key_; | 129 HostnameToKeyMap hostname_to_key_; |
| 130 }; | 130 }; |
| 131 | 131 |
| 132 // static | 132 // static |
| 133 ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() { | 133 ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() { |
| 134 return new TestChannelIDSource(); | 134 return new TestChannelIDSource(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 } // namespace test | 137 } // namespace test |
| 138 | 138 |
| 139 } // namespace net | 139 } // namespace net |
| OLD | NEW |