OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_GCM_DRIVER_CRYPTO_MESSAGE_PAYLOAD_PARSER_H_ |
| 6 #define COMPONENTS_GCM_DRIVER_CRYPTO_MESSAGE_PAYLOAD_PARSER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/strings/string_piece.h" |
| 13 |
| 14 namespace gcm { |
| 15 |
| 16 // Parses and validates the binary message payload included in messages that |
| 17 // are encrypted per draft-ietf-webpush-encryption-08: |
| 18 // |
| 19 // https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-08#section
-2.1 |
| 20 // |
| 21 // In summary, such messages start with a binary header block that includes the |
| 22 // parameters needed to decrypt the content, other than the key. All content |
| 23 // following this binary header is considered the ciphertext. |
| 24 // |
| 25 // +-----------+--------+-----------+-----------------+ |
| 26 // | salt (16) | rs (4) | idlen (1) | public_key (65) | |
| 27 // +-----------+--------+-----------+-----------------+ |
| 28 // |
| 29 // Specific to Web Push encryption, the `public_key` parameter of this header |
| 30 // must be set to the ECDH public key of the sender. This is a point on the |
| 31 // P-256 elliptic curve in uncompressed form, 65 bytes long starting with 0x04. |
| 32 // |
| 33 // https://tools.ietf.org/html/draft-ietf-webpush-encryption-08#section-3.1 |
| 34 class MessagePayloadParser { |
| 35 public: |
| 36 explicit MessagePayloadParser(base::StringPiece message); |
| 37 ~MessagePayloadParser(); |
| 38 |
| 39 // Returns whether the parser represents a valid message. |
| 40 bool IsValid() const { return is_valid_; } |
| 41 |
| 42 // Returns the 16-byte long salt for the message. Must only be called after |
| 43 // validity of the message has been verified. |
| 44 const std::string& salt() const { |
| 45 CHECK(is_valid_); |
| 46 return salt_; |
| 47 } |
| 48 |
| 49 // Returns the record size for the message. Must only be called after validity |
| 50 // of the message has been verified. |
| 51 uint32_t record_size() const { |
| 52 CHECK(is_valid_); |
| 53 return record_size_; |
| 54 } |
| 55 |
| 56 // Returns the sender's ECDH public key for the message. This will be a point |
| 57 // on the P-256 elliptic curve in uncompressed form. Must only be called after |
| 58 // validity of the message has been verified. |
| 59 const std::string& public_key() const { |
| 60 CHECK(is_valid_); |
| 61 return public_key_; |
| 62 } |
| 63 |
| 64 // Returns the ciphertext for the message. This will be at least the size of |
| 65 // a single record, which is 18 octets. Must only be called after validity of |
| 66 // the message has been verified. |
| 67 const std::string& ciphertext() const { |
| 68 CHECK(is_valid_); |
| 69 return ciphertext_; |
| 70 } |
| 71 |
| 72 private: |
| 73 bool is_valid_ = false; |
| 74 |
| 75 std::string salt_; |
| 76 uint32_t record_size_ = 0; |
| 77 std::string public_key_; |
| 78 std::string ciphertext_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(MessagePayloadParser); |
| 81 }; |
| 82 |
| 83 } // namespace gcm |
| 84 |
| 85 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_MESSAGE_PAYLOAD_PARSER_H_ |
OLD | NEW |