Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 CRYPTO_CUP_H_ | |
| 6 #define CRYPTO_CUP_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/strings/string_piece.h" | |
| 13 #include "crypto/crypto_export.h" | |
| 14 | |
| 15 #if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | |
| 16 #include "crypto/scoped_nss_types.h" | |
| 17 #endif | |
| 18 | |
| 19 namespace crypto { | |
| 20 | |
| 21 // Client Update Protocol, or CUP, is used by Google Update (Omaha) servers to | |
| 22 // ensure freshness and authenticity of update checks over HTTP, without the | |
| 23 // overhead of HTTPS -- namely, no PKI, no guarantee of privacy, and no request | |
| 24 // replay protection (since update checks are idempotent). | |
| 25 // | |
| 26 // http://omaha.googlecode.com/svn/wiki/cup.html | |
| 27 // | |
| 28 // This implementation does not persist client proofs by design. | |
| 29 class CRYPTO_EXPORT ClientUpdateProtocol { | |
| 30 public: | |
| 31 ~ClientUpdateProtocol(); | |
| 32 | |
| 33 // Initializes this instance of CUP with a versioned public key. |key_version| | |
| 34 // must be non-negative. |public_key| is expected to be a DER-encoded ASN.1 | |
| 35 // Subject Public Key Info. Returns NULL on failure. | |
| 36 static ClientUpdateProtocol* Create(int key_version, | |
| 37 const base::StringPiece& public_key); | |
| 38 | |
| 39 // Returns a versioned encrypted secret (v|w) in a URL-safe Base64 encoding. | |
| 40 // Add to your URL before calling SignRequest(). | |
| 41 std::string GetVersionedSecret() const; | |
| 42 | |
| 43 // Generates freshness/authentication data for an outgoing update check. | |
| 44 // |url| contains the the URL that the request will be sent to; it should | |
| 45 // include GetVersionedSecret() in its query string. This needs to be | |
| 46 // formatted in the way that the Omaha server expects: omit the scheme and | |
| 47 // any port number. (e.g. "//tools.google.com/service/update2?w=1:abcdef") | |
| 48 // |request_body| contains the body of the update check request in UTF-8. | |
| 49 // | |
| 50 // On success, returns true, and |cp_out| receives a Base64-encoded client | |
| 51 // proof, which should be sent in the If-Match HTTP header. On failure, | |
| 52 // returns false, and |cp_out| is not modified. | |
| 53 // | |
| 54 // This method will store internal state in this instance used by calls to | |
| 55 // ValidateResponse(); if you need to have multiple update checks in flight, | |
| 56 // initialize a separate CUP instance for each one. | |
| 57 bool SignRequest(const base::StringPiece& url, | |
| 58 const base::StringPiece& request_body, | |
| 59 std::string* client_proof_out); | |
|
wtc
2013/05/30 21:38:56
Nit: it is uncommon to encode _in and _out in para
Ryan Myers (chromium)
2013/05/30 21:55:11
Done.
| |
| 60 | |
| 61 // Validates a response to a update check request that was previously signed | |
| 62 // with SignRequest(). Returns true on success, false if it cannot validate. | |
| 63 // Uses internal state from the previous call to SignRequest(). | |
| 64 bool ValidateResponse(const base::StringPiece& response_body, | |
| 65 const base::StringPiece& cookie_in, | |
| 66 const base::StringPiece& server_proof_in); | |
|
wtc
2013/05/30 21:38:56
|cookie_in| and |server_proof_in| need to be docum
Ryan Myers (chromium)
2013/05/30 21:55:11
Done.
| |
| 67 | |
| 68 private: | |
| 69 friend class CupUnitTest; | |
| 70 | |
| 71 // Constructor is private -- use one of the Create* methods above. | |
| 72 explicit ClientUpdateProtocol(int key_version); | |
| 73 | |
| 74 // Decodes |public_key| into the appropriate internal structures. Returns | |
| 75 // the length of the public key (modulus) in bytes, or 0 on failure. | |
| 76 size_t LoadPublicKey(const base::StringPiece& public_key); | |
| 77 | |
| 78 // Helper function for BuildSharedKey() -- encrypts |key_source| (r) using | |
| 79 // the loaded public key, filling out |encrypted_key_source_| (w). | |
| 80 // Returns true on success. | |
| 81 bool EncryptKeySource(const std::vector<uint8>& key_source); | |
| 82 | |
| 83 // Generates a key source (r) and uses that to fill out |shared_key_| (sk') | |
| 84 // and encrypted_key_source_ (w). If |opt_key_source| is non-NULL, the key | |
| 85 // source is read from that location instead of being randomly generated. | |
| 86 // (Used for unit testing.) Returns true on success. | |
| 87 bool BuildSharedKey(size_t public_key_length, const uint8* opt_key_source); | |
| 88 | |
| 89 // The server keeps multiple private keys; a version must be sent so that | |
| 90 // the right private key is used to decode the versioned secret. (The CUP | |
| 91 // specification calls this "v".) | |
| 92 int pub_key_version_; | |
| 93 | |
| 94 // Holds the shared key, which is used to generate an HMAC signature for both | |
| 95 // the update check request and the update response. The client builds it | |
| 96 // locally, but sends the server an encrypted copy of the key source to | |
| 97 // synthesize it on its own. (The CUP specification calls this "sk'".) | |
| 98 std::vector<uint8> shared_key_; | |
| 99 | |
| 100 // Holds the original contents of key_source_ that have been encrypted with | |
| 101 // the server's public key. The client sends this, along with the version of | |
| 102 // the keypair that was used, to the server. The server decrypts it using its | |
| 103 // private key to get the contents of key_source_, from which it recreates the | |
| 104 // shared key. (The CUP specification calls this "w".) | |
| 105 std::vector<uint8> encrypted_key_source_; | |
| 106 | |
| 107 // Holds the hash of the update check request, the URL that it was sent to, | |
| 108 // and the versioned secret. This is filled out by a successful call to | |
| 109 // SignRequest(), and used by ValidateResponse() to confirm that the server | |
| 110 // has successfully decoded the versioned secret and signed the response using | |
| 111 // the same shared key as our own. (The CUP specification calls this "hw".) | |
| 112 std::vector<uint8> client_challenge_hash_; | |
| 113 | |
| 114 #if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | |
| 115 // The public key used to encrypt the key source. (The CUP specification | |
| 116 // calls this "pk[v]".) | |
| 117 ScopedSECKEYPublicKey public_key_; | |
| 118 #endif | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(ClientUpdateProtocol); | |
| 121 }; | |
| 122 | |
| 123 } // namespace crypto | |
| 124 | |
| 125 #endif // CRYPTO_CUP_H_ | |
| 126 | |
| OLD | NEW |