OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/client_update_protocol/ecdsa.h" | 5 #include "components/client_update_protocol/ecdsa.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 Ecdsa::~Ecdsa() {} | 91 Ecdsa::~Ecdsa() {} |
92 | 92 |
93 std::unique_ptr<Ecdsa> Ecdsa::Create(int key_version, | 93 std::unique_ptr<Ecdsa> Ecdsa::Create(int key_version, |
94 const base::StringPiece& public_key) { | 94 const base::StringPiece& public_key) { |
95 DCHECK_GT(key_version, 0); | 95 DCHECK_GT(key_version, 0); |
96 DCHECK(!public_key.empty()); | 96 DCHECK(!public_key.empty()); |
97 | 97 |
98 return base::WrapUnique(new Ecdsa(key_version, public_key)); | 98 return base::WrapUnique(new Ecdsa(key_version, public_key)); |
99 } | 99 } |
100 | 100 |
| 101 void Ecdsa::OverrideNonceForTesting(int key_version, uint32_t nonce) { |
| 102 DCHECK(!request_query_cup2key_.empty()); |
| 103 request_query_cup2key_ = base::StringPrintf("%d:%u", pub_key_version_, nonce); |
| 104 } |
| 105 |
101 void Ecdsa::SignRequest(const base::StringPiece& request_body, | 106 void Ecdsa::SignRequest(const base::StringPiece& request_body, |
102 std::string* query_params) { | 107 std::string* query_params) { |
103 DCHECK(!request_body.empty()); | |
104 DCHECK(query_params); | 108 DCHECK(query_params); |
105 | 109 |
106 // Generate a random nonce to use for freshness, build the cup2key query | 110 // Generate a random nonce to use for freshness, build the cup2key query |
107 // string, and compute the SHA-256 hash of the request body. Set these | 111 // string, and compute the SHA-256 hash of the request body. Set these |
108 // two pieces of data aside to use during ValidateResponse(). | 112 // two pieces of data aside to use during ValidateResponse(). |
109 uint32_t nonce = 0; | 113 uint32_t nonce = 0; |
110 crypto::RandBytes(&nonce, sizeof(nonce)); | 114 crypto::RandBytes(&nonce, sizeof(nonce)); |
111 request_query_cup2key_ = base::StringPrintf("%d:%u", pub_key_version_, nonce); | 115 request_query_cup2key_ = base::StringPrintf("%d:%u", pub_key_version_, nonce); |
112 request_hash_ = SHA256HashStr(request_body); | 116 request_hash_ = SHA256HashStr(request_body); |
113 | 117 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 // * The signature was modified | 182 // * The signature was modified |
179 // * The buffer that the server signed does not match the buffer that the | 183 // * The buffer that the server signed does not match the buffer that the |
180 // client assembled -- implying that either request body or response body | 184 // client assembled -- implying that either request body or response body |
181 // was modified, or a different nonce value was used. | 185 // was modified, or a different nonce value was used. |
182 verifier.VerifyUpdate(&signed_message_hash.front(), | 186 verifier.VerifyUpdate(&signed_message_hash.front(), |
183 static_cast<int>(signed_message_hash.size())); | 187 static_cast<int>(signed_message_hash.size())); |
184 return verifier.VerifyFinal(); | 188 return verifier.VerifyFinal(); |
185 } | 189 } |
186 | 190 |
187 } // namespace client_update_protocol | 191 } // namespace client_update_protocol |
OLD | NEW |