summaryrefslogtreecommitdiff
path: root/src/main/cpp/include/securegcm/d2d_connection_context_v1.h
blob: 098e65433f913c188e9e065d9e0eabd6168a6eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SECURITY_CRYPTAUTH_LIB_SECUREGCM_D2D_CONNECTION_CONTEXT_V1_H_
#define SECURITY_CRYPTAUTH_LIB_SECUREGCM_D2D_CONNECTION_CONTEXT_V1_H_

#include <memory>
#include <string>

#include "securemessage/crypto_ops.h"

namespace securegcm {

// The full context of a secure connection. This class has methods to encode and
// decode messages that are to be sent to another device.
//
// This class should be kept compatible with the Java implementation in
// java/com/google/security/cryptauth/lib/securegcm/D2DConnectionContextV1.java
class D2DConnectionContextV1 {
 public:
  D2DConnectionContextV1(const securemessage::CryptoOps::SecretKey& encode_key,
                         const securemessage::CryptoOps::SecretKey& decode_key,
                         uint32_t encode_sequence_number,
                         uint32_t decode_sequence_number);

  // Once the initiator and responder have negotiated a secret key, use this
  // method to encrypt and sign |payload|. Both initiator and responder devices
  // can use this message.
  //
  // On failure, nullptr is returned.
  std::unique_ptr<string> EncodeMessageToPeer(const string& payload);

  // Once the initiator and responder have negotiated a secret key, use this
  // method to decrypt and verify a |message| received from the other device.
  // Both initiator and responder devices can use this message.
  //
  // On failure, nullptr is returned.
  std::unique_ptr<string> DecodeMessageFromPeer(const string& message);

  // Returns a cryptographic digest (SHA256) of the session keys prepended by
  // the SHA256 hash of the ASCII string "D2D".
  //
  // On failure, nullptr is returned.
  std::unique_ptr<string> GetSessionUnique();

  // Creates a saved session that can be later used for resumption. Note,
  // this must be stored in a secure location.
  std::unique_ptr<string> SaveSession();

  // Parse a saved session info and attempt to construct a resumed context.
  //
  // The session info passed to this method should be one that was generated
  // by |SaveSession|.
  //
  // On failure, nullptr is returned.
  static std::unique_ptr<D2DConnectionContextV1> FromSavedSession(
      const string& savedSessionInfo);

 private:
  // The key used to encode payloads.
  const securemessage::CryptoOps::SecretKey encode_key_;

  // The key used to decode received messages.
  const securemessage::CryptoOps::SecretKey decode_key_;

  // The current sequence number for encoding.
  uint32_t encode_sequence_number_;

  // The current sequence number for decoding.
  uint32_t decode_sequence_number_;

  // A friend to access private variables for testing.
  friend class D2DConnectionContextV1Peer;
};

}  // namespace securegcm

#endif  // SECURITY_CRYPTAUTH_LIB_SECUREGCM_D2D_CONNECTION_CONTEXT_V1_H_