summaryrefslogtreecommitdiffstats
path: root/remoting/host/support_access_verifier.cc
blob: 30ad662d03ed9459ca84f17df5cf059de14e6d8c (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
// 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/support_access_verifier.h"

#include <stdlib.h>
#include <vector>
#include <iostream>

#include "base/logging.h"
#include "base/rand_util.h"
#include "base/string_util.h"
#include "remoting/host/host_config.h"
#include "remoting/protocol/auth_token_utils.h"

namespace remoting {

namespace {
// 5 characters long from 34-letter alphabet gives 4.5M possible
// host secrets with uniform distribution, which should be enough
// for short-term passwords.
const int kHostSecretLength = 5;

// The following set includes 10 digits and Latin alphabet except I
// and O. I and O are not used to avoid confusion with 1 and 0.
const char kHostSecretAlphabet[] = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";

// Generates cryptographically strong random number in the range [0, max).
int CryptoRandomInt(int max) {
  uint32 random_int32;
  base::RandBytes(&random_int32, sizeof(random_int32));
  return random_int32 % max;
}

std::string GenerateRandomHostSecret() {
  std::vector<char> result;
  int alphabet_size = strlen(kHostSecretAlphabet);
  result.resize(kHostSecretLength);
  for (int i = 0; i < kHostSecretLength; ++i) {
    result[i] = kHostSecretAlphabet[CryptoRandomInt(alphabet_size)];
  }
  return std::string(result.begin(), result.end());
}

}  // namespace

SupportAccessVerifier::SupportAccessVerifier()
    : initialized_(false) {
}

SupportAccessVerifier::~SupportAccessVerifier() { }

bool SupportAccessVerifier::Init() {
  host_secret_ = GenerateRandomHostSecret();
  initialized_ = true;
  return true;
}

bool SupportAccessVerifier::VerifyPermissions(
    const std::string& client_jid,
    const std::string& encoded_access_token) {
  CHECK(initialized_);
  if (support_id_.empty())
    return false;
  // TODO(jamiewalch): This needs to compose the support id with the host
  // secret before verification.
  std::string access_code = support_id_ + "-" + host_secret_;
  return protocol::VerifySupportAuthToken(
      client_jid, access_code, encoded_access_token);
}

void SupportAccessVerifier::OnMe2MomHostRegistered(
    bool successful, const std::string& support_id) {
  if (successful) {
    // TODO(jamiewalch): cout is only needed so that remoting_simple_host can
    // report the support id to the user. Find a better way (issue 82651).
    std::cout << "Support id: "
              << support_id << std::endl;
    support_id_ = support_id;
  } else {
    LOG(ERROR) << "Failed to register support host";
  }
}

}  // namespace remoting