blob: 24e4171ba8e59c39060dec4d4a29121460189c18 [file] [log] [blame]
mabbb61b52a2016-03-17 23:37:231// 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#ifndef COMPONENTS_CLIENT_UPDATE_PROTOCOL_ECDSA_H_
6#define COMPONENTS_CLIENT_UPDATE_PROTOCOL_ECDSA_H_
7
8#include <stdint.h>
9
dchenga0ee5fb2016-04-26 02:46:5510#include <memory>
mabbb61b52a2016-03-17 23:37:2311#include <string>
12#include <vector>
13
mabbb61b52a2016-03-17 23:37:2314#include "base/strings/string_piece.h"
15
16namespace client_update_protocol {
17
18// Client Update Protocol v2, or CUP-ECDSA, is used by Google Update (Omaha)
19// servers to ensure freshness and authenticity of server responses over HTTP,
20// without the overhead of HTTPS -- namely, no PKI, no guarantee of privacy, and
21// no request replay protection.
22//
23// CUP-ECDSA relies on a single signing operation using ECDSA with SHA-256,
24// instead of the original CUP which used HMAC-SHA1 with a random signing key
25// encrypted using RSA.
26//
27// Each |Ecdsa| object represents a single network ping in flight -- a call to
28// SignRequest() generates internal state that will be used by
29// ValidateResponse().
30class Ecdsa {
31 public:
32 ~Ecdsa();
33
34 // Initializes this instance of CUP-ECDSA with a versioned public key.
35 // |key_version| must be non-negative. |public_key| is expected to be a
36 // DER-encoded ASN.1 SubjectPublicKeyInfo containing an ECDSA public key.
37 // Returns a NULL pointer on failure.
dchenga0ee5fb2016-04-26 02:46:5538 static std::unique_ptr<Ecdsa> Create(int key_version,
39 const base::StringPiece& public_key);
mabbb61b52a2016-03-17 23:37:2340
41 // Generates freshness/authentication data for an outgoing ping.
42 // |request_body| contains the body of the ping in UTF-8. On return,
43 // |query_params| contains a set of query parameters (in UTF-8) to be appended
44 // to the URL.
45 //
46 // This method will store internal state in this instance used by calls to
47 // ValidateResponse(); if you need to have multiple pings in flight,
48 // initialize a separate CUP-ECDSA instance for each one.
49 void SignRequest(const base::StringPiece& request_body,
50 std::string* query_params);
51
52 // Validates a response given to a ping previously signed with
53 // SignRequest(). |response_body| contains the body of the response in
mab2f07cb92016-05-10 20:55:5154 // UTF-8. |signature| contains the ECDSA signature and observed request
55 // hash. Returns true if the response is valid and the observed request hash
56 // matches the sent hash. This method uses internal state that is set by a
57 // prior SignRequest() call.
mabbb61b52a2016-03-17 23:37:2358 bool ValidateResponse(const base::StringPiece& response_body,
mab2f07cb92016-05-10 20:55:5159 const base::StringPiece& signature);
60
61 // Sets the key and nonce that were used to generate a signature that is baked
62 // into a unit test.
63 void OverrideNonceForTesting(int key_version, uint32_t nonce);
mabbb61b52a2016-03-17 23:37:2364
65 private:
mabbb61b52a2016-03-17 23:37:2366 Ecdsa(int key_version, const base::StringPiece& public_key);
67
68 // The server keeps multiple signing keys; a version must be sent so that
69 // the correct signing key is used to sign the assembled message.
70 const int pub_key_version_;
71
72 // The ECDSA public key to use for verifying response signatures.
73 const std::vector<uint8_t> public_key_;
74
75 // The SHA-256 hash of the XML request. This is modified on each call to
76 // SignRequest(), and checked by ValidateResponse().
77 std::vector<uint8_t> request_hash_;
78
79 // The query string containing key version and nonce in UTF-8 form. This is
80 // modified on each call to SignRequest(), and checked by ValidateResponse().
81 std::string request_query_cup2key_;
82
83 DISALLOW_IMPLICIT_CONSTRUCTORS(Ecdsa);
84};
85
86} // namespace client_update_protocol
87
88#endif // COMPONENTS_CLIENT_UPDATE_PROTOCOL_ECDSA_H_