summaryrefslogtreecommitdiffstats
path: root/remoting/host/host_key_pair.cc
blob: ad3d59b1a2f7034f8411012536695baf30adda65 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/host_key_pair.h"

#include <limits>
#include <string>
#include <vector>

#include "base/base64.h"
#include "base/crypto/rsa_private_key.h"
#include "base/crypto/signature_creator.h"
#include "base/logging.h"
#include "base/rand_util.h"
#include "base/task.h"
#include "base/time.h"
#include "net/base/x509_certificate.h"
#include "remoting/host/host_config.h"

namespace remoting {

HostKeyPair::HostKeyPair() { }

HostKeyPair::~HostKeyPair() { }

void HostKeyPair::Generate() {
  key_.reset(base::RSAPrivateKey::Create(2048));
}

bool HostKeyPair::LoadFromString(const std::string& key_base64) {
  std::string key_str;
  if (!base::Base64Decode(key_base64, &key_str)) {
    LOG(ERROR) << "Failed to decode private key.";
    return false;
  }

  std::vector<uint8> key_buf(key_str.begin(), key_str.end());
  key_.reset(base::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf));
  if (key_.get() == NULL) {
    LOG(ERROR) << "Invalid private key.";
    return false;
  }

  return true;
}

bool HostKeyPair::Load(HostConfig* host_config) {
  std::string key_base64;
  if (!host_config->GetString(kPrivateKeyConfigPath, &key_base64)) {
    LOG(ERROR) << "Private key wasn't found in the config file.";
    return false;
  }
  return LoadFromString(key_base64);
}

void HostKeyPair::Save(MutableHostConfig* host_config) {
  // Check that the key initialized.
  DCHECK(key_.get() != NULL);

  std::vector<uint8> key_buf;
  key_->ExportPrivateKey(&key_buf);
  std::string key_str(key_buf.begin(), key_buf.end());
  std::string key_base64;
  base::Base64Encode(key_str, &key_base64);
  host_config->SetString(kPrivateKeyConfigPath, key_base64);
}

std::string HostKeyPair::GetPublicKey() const {
  std::vector<uint8> public_key;
  key_->ExportPublicKey(&public_key);
  std::string public_key_str(public_key.begin(), public_key.end());
  std::string public_key_base64;
  base::Base64Encode(public_key_str, &public_key_base64);
  return public_key_base64;
}

std::string HostKeyPair::GetSignature(const std::string& message) const {
  scoped_ptr<base::SignatureCreator> signature_creator(
      base::SignatureCreator::Create(key_.get()));
  signature_creator->Update(reinterpret_cast<const uint8*>(message.c_str()),
                            message.length());
  std::vector<uint8> signature_buf;
  signature_creator->Final(&signature_buf);
  std::string signature_str(signature_buf.begin(), signature_buf.end());
  std::string signature_base64;
  base::Base64Encode(signature_str, &signature_base64);
  return signature_base64;
}

base::RSAPrivateKey* HostKeyPair::CopyPrivateKey() const {
  std::vector<uint8> key_bytes;
  CHECK(key_->ExportPrivateKey(&key_bytes));
  return base::RSAPrivateKey::CreateFromPrivateKeyInfo(key_bytes);
}

net::X509Certificate* HostKeyPair::GenerateCertificate() const {
  return net::X509Certificate::CreateSelfSigned(
      key_.get(), "CN=chromoting",
      base::RandInt(1, std::numeric_limits<int>::max()),
      base::TimeDelta::FromDays(1));
}

}  // namespace remoting