OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/update_client/client_update_protocol_ecdsa.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/macros.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_piece.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/strings/stringprintf.h" | |
14 #include "crypto/random.h" | |
15 #include "crypto/sha2.h" | |
16 #include "crypto/signature_verifier.h" | |
17 | |
18 namespace update_client { | |
19 | |
20 namespace { | |
21 | |
22 std::vector<uint8_t> SHA256HashStr(const base::StringPiece& str) { | |
23 std::vector<uint8_t> result(crypto::kSHA256Length); | |
24 crypto::SHA256HashString(str, &result.front(), result.size()); | |
25 return result; | |
26 } | |
27 | |
28 std::vector<uint8_t> SHA256HashVec(const std::vector<uint8_t>& vec) { | |
29 if (vec.empty()) | |
30 return SHA256HashStr(base::StringPiece()); | |
31 | |
32 return SHA256HashStr(base::StringPiece( | |
33 reinterpret_cast<const char*>(&vec.front()), vec.size())); | |
34 } | |
35 | |
36 bool ParseETagHeader(const base::StringPiece& etag_header_value_in, | |
37 std::vector<uint8_t>* ecdsa_signature_out, | |
38 std::vector<uint8_t>* request_hash_out) { | |
39 DCHECK(ecdsa_signature_out); | |
40 DCHECK(request_hash_out); | |
41 | |
42 // The ETag value is a UTF-8 string, formatted as "S:H", where: | |
43 // * S is the ECDSA signature in DER-encoded ASN.1 form, converted to hex. | |
44 // * H is the SHA-256 hash of the observed request body, standard hex format. | |
45 // A Weak ETag is formatted as W/"S:H". This function treats it the same as a | |
46 // strong ETag. | |
47 base::StringPiece etag_header_value(etag_header_value_in); | |
48 | |
49 // Remove the weak prefix, then remove the begin and the end quotes. | |
50 const char kWeakETagPrefix[] = "W/"; | |
51 if (etag_header_value.starts_with(kWeakETagPrefix)) | |
52 etag_header_value.remove_prefix(arraysize(kWeakETagPrefix) - 1); | |
53 if (etag_header_value.size() >= 2 && etag_header_value.starts_with("\"") && | |
54 etag_header_value.ends_with("\"")) { | |
55 etag_header_value.remove_prefix(1); | |
56 etag_header_value.remove_suffix(1); | |
57 } | |
58 | |
59 const base::StringPiece::size_type delim_pos = etag_header_value.find(':'); | |
60 if (delim_pos == base::StringPiece::npos || delim_pos == 0 || | |
61 delim_pos == etag_header_value.size() - 1) | |
62 return false; | |
63 | |
64 const base::StringPiece sig_hex = etag_header_value.substr(0, delim_pos); | |
65 const base::StringPiece hash_hex = etag_header_value.substr(delim_pos + 1); | |
66 | |
67 // Decode the ECDSA signature. Don't bother validating the contents of it; | |
68 // the SignatureValidator class will handle the actual DER decoding and | |
69 // ASN.1 parsing. Check for an expected size range only -- valid ECDSA | |
70 // signatures are between 8 and 72 bytes. | |
71 if (!base::HexStringToBytes(sig_hex.as_string(), ecdsa_signature_out)) | |
72 return false; | |
73 if (ecdsa_signature_out->size() < 8 || ecdsa_signature_out->size() > 72) | |
74 return false; | |
75 | |
76 // Decode the SHA-256 hash; it should be exactly 32 bytes, no more or less. | |
77 if (!base::HexStringToBytes(hash_hex.as_string(), request_hash_out)) | |
78 return false; | |
79 if (request_hash_out->size() != crypto::kSHA256Length) | |
80 return false; | |
81 | |
82 return true; | |
83 } | |
84 | |
85 } // namespace | |
86 | |
87 ClientUpdateProtocolEcdsa::ClientUpdateProtocolEcdsa( | |
88 int key_version, | |
89 const base::StringPiece& public_key) | |
90 : pub_key_version_(key_version), | |
91 public_key_(public_key.begin(), public_key.end()) {} | |
92 | |
93 ClientUpdateProtocolEcdsa::~ClientUpdateProtocolEcdsa() {} | |
94 | |
95 scoped_ptr<ClientUpdateProtocolEcdsa> ClientUpdateProtocolEcdsa::Create( | |
96 int key_version, | |
97 const base::StringPiece& public_key) { | |
98 DCHECK_GT(key_version, 0); | |
99 DCHECK(!public_key.empty()); | |
100 | |
101 return make_scoped_ptr( | |
102 new ClientUpdateProtocolEcdsa(key_version, public_key)); | |
103 } | |
104 | |
105 void ClientUpdateProtocolEcdsa::SignRequest( | |
106 const base::StringPiece& request_body, | |
107 std::string* query_params) { | |
108 DCHECK(!request_body.empty()); | |
109 DCHECK(query_params); | |
110 | |
111 // Generate a random nonce to use for freshness, build the cup2key query | |
112 // string, and compute the SHA-256 hash of the request body. Set these | |
113 // two pieces of data aside to use during ValidateResponse(). | |
114 uint32_t nonce = 0; | |
115 crypto::RandBytes(&nonce, sizeof(nonce)); | |
116 request_query_cup2key_ = base::StringPrintf("%d:%u", pub_key_version_, nonce); | |
117 request_hash_ = SHA256HashStr(request_body); | |
118 | |
119 // Return the query string for the user to send with the request. | |
120 std::string request_hash_hex = | |
121 base::HexEncode(&request_hash_.front(), request_hash_.size()); | |
122 request_hash_hex = base::ToLowerASCII(request_hash_hex); | |
123 | |
124 *query_params = base::StringPrintf("cup2key=%s&cup2hreq=%s", | |
125 request_query_cup2key_.c_str(), | |
126 request_hash_hex.c_str()); | |
127 } | |
128 | |
129 bool ClientUpdateProtocolEcdsa::ValidateResponse( | |
130 const base::StringPiece& response_body, | |
131 const base::StringPiece& server_etag) { | |
132 DCHECK(!request_hash_.empty()); | |
133 DCHECK(!request_query_cup2key_.empty()); | |
134 | |
135 if (response_body.empty() || server_etag.empty()) | |
136 return false; | |
137 | |
138 // Break the ETag into its two components (the ECDSA signature, and the | |
139 // hash of the request that the server observed) and decode to byte buffers. | |
140 std::vector<uint8_t> signature; | |
141 std::vector<uint8_t> observed_request_hash; | |
142 if (!ParseETagHeader(server_etag, &signature, &observed_request_hash)) | |
143 return false; | |
144 | |
145 // Check that the server's observed request hash is equal to the original | |
146 // request hash. (This is a quick rejection test; the signature test is | |
147 // authoritative, but slower.) | |
148 DCHECK_EQ(request_hash_.size(), crypto::kSHA256Length); | |
149 if (observed_request_hash.size() != crypto::kSHA256Length) | |
150 return false; | |
151 if (!std::equal(observed_request_hash.begin(), observed_request_hash.end(), | |
152 request_hash_.begin())) | |
153 return false; | |
154 | |
155 // Next, build the buffer that the server will have signed on its end: | |
156 // hash( hash(request) | hash(response) | cup2key_query_string ) | |
157 // When building the client's version of the buffer, it's important to use | |
158 // the original request hash that it attempted to send, and not the observed | |
159 // request hash that the server sent back to us. | |
160 const std::vector<uint8_t> response_hash = SHA256HashStr(response_body); | |
161 | |
162 std::vector<uint8_t> signed_message; | |
163 signed_message.insert(signed_message.end(), request_hash_.begin(), | |
164 request_hash_.end()); | |
165 signed_message.insert(signed_message.end(), response_hash.begin(), | |
166 response_hash.end()); | |
167 signed_message.insert(signed_message.end(), request_query_cup2key_.begin(), | |
168 request_query_cup2key_.end()); | |
169 | |
170 const std::vector<uint8_t> signed_message_hash = | |
171 SHA256HashVec(signed_message); | |
172 | |
173 // Initialize the signature verifier. | |
174 crypto::SignatureVerifier verifier; | |
175 if (!verifier.VerifyInit( | |
176 crypto::SignatureVerifier::ECDSA_SHA256, &signature.front(), | |
177 static_cast<int>(signature.size()), &public_key_.front(), | |
178 static_cast<int>(public_key_.size()))) { | |
179 DVLOG(1) << "Couldn't init SignatureVerifier."; | |
180 return false; | |
181 } | |
182 | |
183 // If the verification fails, that implies one of two outcomes: | |
184 // * The signature was modified | |
185 // * The buffer that the server signed does not match the buffer that the | |
186 // client assembled -- implying that either request body or response body | |
187 // was modified, or a different nonce value was used. | |
188 verifier.VerifyUpdate(&signed_message_hash.front(), | |
189 static_cast<int>(signed_message_hash.size())); | |
190 return verifier.VerifyFinal(); | |
191 } | |
192 | |
193 } // namespace update_client | |
OLD | NEW |