1// Copyright 2014 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#include "chrome/browser/extensions/api/networking_private/crypto_verify_impl.h"
6
7#include "base/base64.h"
8#include "chrome/browser/extensions/api/networking_private/networking_private_credentials_getter.h"
9#include "chrome/browser/extensions/api/networking_private/networking_private_service_client.h"
10#include "chrome/common/extensions/api/networking_private/networking_private_crypto.h"
11
12namespace {
13
14bool VerifyCredentials(const CryptoVerifyImpl::Credentials& credentials) {
15  return networking_private_crypto::VerifyCredentials(credentials.certificate,
16                                                      credentials.signed_data,
17                                                      credentials.unsigned_data,
18                                                      credentials.device_bssid);
19}
20
21}  // namespace
22
23using extensions::NetworkingPrivateServiceClient;
24using extensions::NetworkingPrivateCredentialsGetter;
25
26NetworkingPrivateServiceClient::CryptoVerify*
27NetworkingPrivateServiceClient::CryptoVerify::Create() {
28  return new CryptoVerifyImpl();
29}
30
31CryptoVerifyImpl::CryptoVerifyImpl() {
32}
33
34CryptoVerifyImpl::~CryptoVerifyImpl() {
35}
36
37void CryptoVerifyImpl::VerifyDestination(const Credentials& credentials,
38                                         bool* verified,
39                                         std::string* error) {
40  *verified = VerifyCredentials(credentials);
41}
42
43void CryptoVerifyImpl::VerifyAndEncryptCredentials(
44    const std::string& network_guid,
45    const Credentials& credentials,
46    const VerifyAndEncryptCredentialsCallback& callback) {
47  if (!VerifyCredentials(credentials)) {
48    callback.Run("", "VerifyError");
49    return;
50  }
51
52  scoped_ptr<NetworkingPrivateCredentialsGetter> credentials_getter(
53      NetworkingPrivateCredentialsGetter::Create());
54
55  // Start getting credentials. On Windows |callback| will be called
56  // asynchronously on a different thread after |credentials_getter|
57  // is deleted.
58  credentials_getter->Start(network_guid, credentials.public_key, callback);
59}
60
61void CryptoVerifyImpl::VerifyAndEncryptData(
62    const Credentials& credentials,
63    const std::string& data,
64    std::string* base64_encoded_ciphertext,
65    std::string* error) {
66  if (!VerifyCredentials(credentials)) {
67    *error = "VerifyError";
68    return;
69  }
70
71  std::vector<uint8> public_key_data(credentials.public_key.begin(),
72                                     credentials.public_key.end());
73  std::vector<uint8> ciphertext;
74  if (!networking_private_crypto::EncryptByteString(
75          public_key_data, data, &ciphertext)) {
76    *error = "EncryptError";
77    return;
78  }
79
80  base::Base64Encode(std::string(ciphertext.begin(), ciphertext.end()),
81                     base64_encoded_ciphertext);
82}
83