File: password_sharing_invitation_helper.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (160 lines) | stat: -rw-r--r-- 6,560 bytes parent folder | download | duplicates (6)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/test/integration/password_sharing_invitation_helper.h"

#include <memory>
#include <optional>
#include <vector>

#include "base/containers/span.h"
#include "base/uuid.h"
#include "components/sync/engine/nigori/cross_user_sharing_public_key.h"
#include "components/sync/nigori/cryptographer_impl.h"

namespace password_sharing_helper {

namespace {
constexpr char kSignonRealm[] = "signon_realm";
constexpr char kOrigin[] = "http://abc.com/";
constexpr char kUsernameElement[] = "username_element";
constexpr char kPasswordElement[] = "password_element";
constexpr char kPasswordDisplayName[] = "password_display_name";
constexpr char kPasswordAvatarUrl[] = "http://avatar.url/";

constexpr char kSenderEmail[] = "sender@gmail.com";
constexpr char kSenderDisplayName[] = "Sender Name";
constexpr char kSenderProfileImageUrl[] = "http://sender.url/image";

constexpr int kDefaultKeyVersion = 0;

sync_pb::CrossUserSharingPublicKey PublicKeyToProto(
    const syncer::CrossUserSharingPublicKey& public_key) {
  sync_pb::CrossUserSharingPublicKey proto;
  auto raw_public_key = public_key.GetRawPublicKey();
  proto.set_x25519_public_key(
      std::string(raw_public_key.begin(), raw_public_key.end()));
  proto.set_version(kDefaultKeyVersion);
  return proto;
}

std::unique_ptr<syncer::CryptographerImpl> InitializeCryptographer(
    const syncer::CrossUserSharingPublicPrivateKeyPair& key_pair) {
  std::unique_ptr<syncer::CryptographerImpl> cryptographer =
      syncer::CryptographerImpl::CreateEmpty();

  // Clone `key_pair` since the cryptographer requires it to be moved.
  std::optional<syncer::CrossUserSharingPublicPrivateKeyPair> key_pair_copy =
      syncer::CrossUserSharingPublicPrivateKeyPair::CreateByImport(
          key_pair.GetRawPrivateKey());
  CHECK(key_pair_copy);
  cryptographer->SetKeyPair(std::move(key_pair_copy.value()),
                            kDefaultKeyVersion);
  cryptographer->SelectDefaultCrossUserSharingKey(kDefaultKeyVersion);

  return cryptographer;
}

// Encrypts the invitation data to simulate the sending client.
std::vector<uint8_t> EncryptInvitationData(
    const sync_pb::PasswordSharingInvitationData& unencrypted_password_data,
    const sync_pb::CrossUserSharingPublicKey& recipient_public_key,
    const syncer::CrossUserSharingPublicPrivateKeyPair& sender_key_pair) {
  std::unique_ptr<syncer::CryptographerImpl> sender_cryptographer =
      InitializeCryptographer(sender_key_pair);

  std::string serialized_data;
  bool success = unencrypted_password_data.SerializeToString(&serialized_data);
  CHECK(success);

  std::optional<std::vector<uint8_t>> result =
      sender_cryptographer->AuthEncryptForCrossUserSharing(
          base::as_byte_span(serialized_data),
          base::as_byte_span(recipient_public_key.x25519_public_key()));
  CHECK(result);

  return result.value();
}
}  // namespace

sync_pb::PasswordSharingInvitationData DecryptInvitationData(
    const std::string& encrypted_data,
    const sync_pb::CrossUserSharingPublicKey& sender_public_key,
    const syncer::CrossUserSharingPublicPrivateKeyPair& recipient_key_pair) {
  std::unique_ptr<syncer::CryptographerImpl> recipient_cryptographer =
      InitializeCryptographer(recipient_key_pair);

  std::optional<std::vector<uint8_t>> decrypted_data =
      recipient_cryptographer->AuthDecryptForCrossUserSharing(
          base::as_byte_span(encrypted_data),
          base::as_byte_span(sender_public_key.x25519_public_key()),
          kDefaultKeyVersion);
  CHECK(decrypted_data);

  sync_pb::PasswordSharingInvitationData invitation_data;
  CHECK(invitation_data.ParseFromArray(decrypted_data->data(),
                                       decrypted_data->size()));
  return invitation_data;
}

sync_pb::IncomingPasswordSharingInvitationSpecifics
CreateEncryptedIncomingInvitationSpecifics(
    const sync_pb::PasswordSharingInvitationData& invitation_data,
    const sync_pb::UserDisplayInfo& sender_display_info,
    const sync_pb::CrossUserSharingPublicKey& recipient_public_key,
    const syncer::CrossUserSharingPublicPrivateKeyPair& sender_key_pair) {
  sync_pb::IncomingPasswordSharingInvitationSpecifics specifics;
  std::vector<uint8_t> encrypted_password = EncryptInvitationData(
      invitation_data, recipient_public_key, sender_key_pair);
  specifics.set_encrypted_password_sharing_invitation_data(
      std::string(encrypted_password.begin(), encrypted_password.end()));
  specifics.set_guid(base::Uuid::GenerateRandomV4().AsLowercaseString());
  specifics.set_recipient_key_version(recipient_public_key.version());

  std::optional<syncer::CrossUserSharingPublicKey> sender_public_key =
      syncer::CrossUserSharingPublicKey::CreateByImport(
          sender_key_pair.GetRawPublicKey());
  CHECK(sender_public_key);

  sync_pb::UserInfo* sender_info = specifics.mutable_sender_info();
  sender_info->mutable_cross_user_sharing_public_key()->CopyFrom(
      PublicKeyToProto(sender_public_key.value()));
  sender_info->mutable_user_display_info()->CopyFrom(sender_display_info);

  return specifics;
}

sync_pb::PasswordSharingInvitationData CreateDefaultIncomingInvitation(
    const std::string& username_value,
    const std::string& password_value) {
  sync_pb::PasswordSharingInvitationData password_invitation_data;
  sync_pb::PasswordSharingInvitationData::PasswordGroupData*
      password_group_data =
          password_invitation_data.mutable_password_group_data();

  password_group_data->set_username_value(username_value);
  password_group_data->set_password_value(password_value);

  sync_pb::PasswordSharingInvitationData::PasswordGroupElementData*
      element_data = password_group_data->add_element_data();

  element_data->set_signon_realm(kSignonRealm);
  element_data->set_origin(kOrigin);
  element_data->set_username_element(kUsernameElement);
  element_data->set_password_element(kPasswordElement);
  element_data->set_display_name(kPasswordDisplayName);
  element_data->set_avatar_url(kPasswordAvatarUrl);

  return password_invitation_data;
}

sync_pb::UserDisplayInfo CreateDefaultSenderDisplayInfo() {
  sync_pb::UserDisplayInfo sender_display_info;
  sender_display_info.set_email(kSenderEmail);
  sender_display_info.set_display_name(kSenderDisplayName);
  sender_display_info.set_profile_image_url(kSenderProfileImageUrl);
  return sender_display_info;
}

}  // namespace password_sharing_helper