Commit f6ba0594 authored by noamsml@chromium.org's avatar noamsml@chromium.org

Interface plus stub implementation for PrivetV3CryptoProvider

This is a class to handle the session handshake logic as well as encryption with
session key. The stub implementation for session type "empty" does nothing and
passes the data unencrypted.

BUG=380795

Review URL: https://codereview.chromium.org/316873004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@275635 0039d316-1c4b-4281-b951-d872f2087c98
parent 05bfc260
// Copyright 2014 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 "chrome/browser/local_discovery/privetv3_crypto_provider.h"
#include "base/logging.h"
namespace local_discovery {
namespace {
// A stub session type used for development/debugging.
const char kAuthMethodEmpty[] = "empty";
const char kHandshakeStateComplete[] = "complete";
const char kStubVerificationCode[] = "SAMPLE";
}
class PrivetV3CryptoProviderEmpty : public PrivetV3CryptoProvider {
public:
PrivetV3CryptoProviderEmpty();
virtual ~PrivetV3CryptoProviderEmpty();
// PrivetV3CryptoProvider implementation.
virtual HandshakeState GetState() OVERRIDE;
virtual std::string GetAuthMethod() OVERRIDE;
virtual HandshakeState GetNextStep(int* step, std::string* package) OVERRIDE;
virtual HandshakeState SetStepResponse(int step,
const std::string& state,
const std::string& package) OVERRIDE;
virtual std::string GetVerificationCode() OVERRIDE;
virtual HandshakeState AcceptVerificationCode() OVERRIDE;
virtual bool EncryptData(const std::string& input,
std::string* output) OVERRIDE;
private:
HandshakeState state_;
};
scoped_ptr<PrivetV3CryptoProvider> Create(
const std::vector<std::string>& available_auth_methods) {
for (size_t i = 0; i < available_auth_methods.size(); i++) {
if (available_auth_methods[i] == kAuthMethodEmpty) {
return scoped_ptr<PrivetV3CryptoProvider>(
new PrivetV3CryptoProviderEmpty());
}
}
return scoped_ptr<PrivetV3CryptoProvider>();
}
PrivetV3CryptoProviderEmpty::PrivetV3CryptoProviderEmpty()
: state_(IN_PROGRESS) {
}
PrivetV3CryptoProviderEmpty::~PrivetV3CryptoProviderEmpty() {
}
PrivetV3CryptoProvider::HandshakeState PrivetV3CryptoProviderEmpty::GetState() {
return state_;
}
std::string PrivetV3CryptoProviderEmpty::GetAuthMethod() {
return kAuthMethodEmpty;
}
PrivetV3CryptoProvider::HandshakeState PrivetV3CryptoProviderEmpty::GetNextStep(
int* step,
std::string* package) {
DCHECK(state_ == IN_PROGRESS);
*step = 0;
package->clear();
state_ = AWAITING_RESPONSE;
return state_;
}
PrivetV3CryptoProvider::HandshakeState
PrivetV3CryptoProviderEmpty::SetStepResponse(int step,
const std::string& state,
const std::string& package) {
DCHECK(state_ == AWAITING_RESPONSE);
bool success =
(step == 0 && package.empty() && state == kHandshakeStateComplete);
if (success) {
state_ = AWAITING_USER_VERIFICATION;
} else {
state_ = HANDSHAKE_ERROR;
}
return state_;
}
std::string PrivetV3CryptoProviderEmpty::GetVerificationCode() {
DCHECK(state_ == AWAITING_USER_VERIFICATION);
return kStubVerificationCode;
}
PrivetV3CryptoProvider::HandshakeState
PrivetV3CryptoProviderEmpty::AcceptVerificationCode() {
DCHECK(state_ == AWAITING_USER_VERIFICATION);
return (state_ = HANDSHAKE_COMPLETE);
}
bool PrivetV3CryptoProviderEmpty::EncryptData(const std::string& input,
std::string* output) {
*output = input;
return true;
}
} // namespace local_discovery
// Copyright 2014 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.
#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_CRYPTO_PROVIDER_H_
#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_CRYPTO_PROVIDER_H_
#include <string>
#include <vector>
#include "base/memory/scoped_ptr.h"
namespace local_discovery {
class PrivetV3CryptoProvider {
public:
enum HandshakeState {
// Handshake still in progress. Call |GetNextStep| to send next handshake
// step.
IN_PROGRESS,
// Handshake in progress, waiting for response. Call |SetStepResponse| to
// set the step response.
AWAITING_RESPONSE,
// Handshake in progress, need to wait for user verification to
// continue. Call |GetVerificationCode| to get the verification code and
// |AcceptVerificationCode| to signify the code is accepted.
AWAITING_USER_VERIFICATION,
// Handshake complete. Call |EncryptData| to encrypt the data.
HANDSHAKE_COMPLETE,
// Handshake error.
HANDSHAKE_ERROR
};
virtual ~PrivetV3CryptoProvider() {}
static scoped_ptr<PrivetV3CryptoProvider> Create(
const std::vector<std::string>& available_auth_methods);
// Return the current state of the crypto provider.
virtual HandshakeState GetState() = 0;
// Return the authentication method used.
virtual std::string GetAuthMethod() = 0;
// Get the next handshake command. |step| is the step number to send,
// |package| is a base64-encoded package to send with the step. Return
// |true| if a package is generated or |false| in case of an error.
virtual HandshakeState GetNextStep(int* step, std::string* package) = 0;
// Input the response to the handshake command. |step| is the received step
// number, |state| is the received state string, |package| is the received
// base64-encoded package. Return the current handshake state.
virtual HandshakeState SetStepResponse(int step,
const std::string& state,
const std::string& package) = 0;
// Get the verification code to be displayed on the screen.
virtual std::string GetVerificationCode() = 0;
// Signal that the verification code is accepted. Returns the current
// handshake state.
virtual HandshakeState AcceptVerificationCode() = 0;
// Encrypt a string using the session key.
virtual bool EncryptData(const std::string& input, std::string* output) = 0;
};
} // namespace local_discovery
#endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_CRYPTO_PROVIDER_H_
......@@ -990,6 +990,8 @@
'browser/local_discovery/privet_local_printer_lister.cc',
'browser/local_discovery/privet_url_fetcher.cc',
'browser/local_discovery/privet_url_fetcher.h',
'browser/local_discovery/privetv3_crypto_provider.cc',
'browser/local_discovery/privetv3_crypto_provider.h',
'browser/local_discovery/privetv3_session.cc',
'browser/local_discovery/privetv3_session.h',
'browser/local_discovery/privetv3_setup_flow.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment