2015-03-18 00:06:58 +01:00
|
|
|
// Copyright (c) 2014 The Dash developers
|
2014-12-26 12:53:29 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "keepass.h"
|
|
|
|
|
|
|
|
#include <exception>
|
2015-04-29 19:49:14 +02:00
|
|
|
// #include <openssl/rand.h>
|
2014-12-26 12:53:29 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2015-04-29 19:49:14 +02:00
|
|
|
// #include <boost/asio.hpp>
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
#include "json/json_spirit_writer_template.h"
|
|
|
|
#include "json/json_spirit_reader_template.h"
|
2015-04-03 00:51:08 +02:00
|
|
|
|
|
|
|
#include "crypter.h"
|
|
|
|
#include "clientversion.h"
|
|
|
|
#include "random.h"
|
2014-12-26 12:53:29 +01:00
|
|
|
#include "rpcprotocol.h"
|
2015-04-03 00:51:08 +02:00
|
|
|
|
|
|
|
// Necessary to prevent compile errors due to forward declaration of
|
2014-12-26 12:53:29 +01:00
|
|
|
//CScript in serialize.h (included from crypter.h)
|
2015-04-03 00:51:08 +02:00
|
|
|
#include "script/script.h"
|
|
|
|
#include "script/standard.h"
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "utilstrencodings.h"
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
|
|
|
|
CKeePassIntegrator keePassInt;
|
|
|
|
|
|
|
|
CKeePassIntegrator::CKeePassIntegrator()
|
|
|
|
:sKeyBase64(" "), sKey(" "), sUrl(" ") // Prevent LockedPageManagerBase complaints
|
|
|
|
{
|
|
|
|
sKeyBase64.clear(); // Prevent LockedPageManagerBase complaints
|
|
|
|
sKey.clear(); // Prevent LockedPageManagerBase complaints
|
|
|
|
sUrl.clear(); // Prevent LockedPageManagerBase complaints
|
|
|
|
bIsActive = false;
|
|
|
|
nPort = KEEPASS_KEEPASSHTTP_PORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialze from application context
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::init()
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
bIsActive = GetBoolArg("-keepass", false);
|
|
|
|
nPort = boost::lexical_cast<unsigned int>(GetArg("-keepassport", KEEPASS_KEEPASSHTTP_PORT));
|
|
|
|
sKeyBase64 = SecureString(GetArg("-keepasskey", "").c_str());
|
|
|
|
sKeePassId = GetArg("-keepassid", "");
|
|
|
|
sKeePassEntryName = GetArg("-keepassname", "");
|
|
|
|
// Convert key if available
|
2015-01-18 15:59:41 +01:00
|
|
|
if(sKeyBase64.size() > 0)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
sKey = DecodeBase64Secure(sKeyBase64);
|
|
|
|
}
|
|
|
|
// Construct url if available
|
2015-01-18 15:59:41 +01:00
|
|
|
if(sKeePassEntryName.size() > 0)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
sUrl = SecureString("http://");
|
|
|
|
sUrl += SecureString(sKeePassEntryName.c_str());
|
|
|
|
sUrl += SecureString("/");
|
|
|
|
//sSubmitUrl = "http://";
|
|
|
|
//sSubmitUrl += SecureString(sKeePassEntryName.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::CKeePassRequest::addStrParameter(std::string sName, std::string sValue)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
requestObj.push_back(json_spirit::Pair(sName, sValue));
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::CKeePassRequest::addStrParameter(std::string sName, SecureString sValue)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sCipherValue;
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(!EncryptAES256(sKey, sValue, sIV, sCipherValue))
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("Unable to encrypt Verifier");
|
|
|
|
}
|
|
|
|
|
|
|
|
addStrParameter(sName, EncodeBase64(sCipherValue));
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
std::string CKeePassIntegrator::CKeePassRequest::getJson()
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
return json_spirit::write_string(json_spirit::Value(requestObj), false);
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::CKeePassRequest::init()
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
SecureString sIVSecure = generateRandomKey(KEEPASS_CRYPTO_BLOCK_SIZE);
|
|
|
|
sIV = std::string(&sIVSecure[0], sIVSecure.size());
|
|
|
|
// Generate Nonce, Verifier and RequestType
|
|
|
|
SecureString sNonceBase64Secure = EncodeBase64Secure(sIVSecure);
|
|
|
|
addStrParameter("Nonce", std::string(&sNonceBase64Secure[0], sNonceBase64Secure.size())); // Plain
|
|
|
|
addStrParameter("Verifier", sNonceBase64Secure); // Encoded
|
|
|
|
addStrParameter("RequestType", sType);
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::CKeePassResponse::parseResponse(std::string sResponse)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
json_spirit::Value responseValue;
|
2015-01-18 15:59:41 +01:00
|
|
|
if(!json_spirit::read_string(sResponse, responseValue))
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("Unable to parse KeePassHttp response");
|
|
|
|
}
|
|
|
|
|
|
|
|
responseObj = responseValue.get_obj();
|
|
|
|
|
|
|
|
// retrieve main values
|
|
|
|
bSuccess = json_spirit::find_value(responseObj, "Success").get_bool();
|
|
|
|
sType = getStr("RequestType");
|
|
|
|
sIV = DecodeBase64(getStr("Nonce"));
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
std::string CKeePassIntegrator::CKeePassResponse::getStr(std::string sName)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sValue(json_spirit::find_value(responseObj, sName).get_str());
|
|
|
|
return sValue;
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
SecureString CKeePassIntegrator::CKeePassResponse::getSecureStr(std::string sName)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sValueBase64Encrypted(json_spirit::find_value(responseObj, sName).get_str());
|
|
|
|
SecureString sValue;
|
2015-01-18 15:59:41 +01:00
|
|
|
try
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
sValue = decrypt(sValueBase64Encrypted);
|
2015-01-18 15:59:41 +01:00
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sErrorMessage = "Exception occured while decrypting ";
|
|
|
|
sErrorMessage += sName + ": " + e.what();
|
|
|
|
throw std::runtime_error(sErrorMessage);
|
|
|
|
}
|
|
|
|
return sValue;
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
SecureString CKeePassIntegrator::CKeePassResponse::decrypt(std::string sValueBase64Encrypted)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sValueEncrypted = DecodeBase64(sValueBase64Encrypted);
|
|
|
|
SecureString sValue;
|
2015-01-18 15:59:41 +01:00
|
|
|
if(!DecryptAES256(sKey, sValueEncrypted, sIV, sValue))
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("Unable to decrypt value.");
|
|
|
|
}
|
|
|
|
return sValue;
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
std::vector<CKeePassIntegrator::CKeePassEntry> CKeePassIntegrator::CKeePassResponse::getEntries()
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
std::vector<CKeePassEntry> vEntries;
|
|
|
|
|
|
|
|
json_spirit::Array aEntries = json_spirit::find_value(responseObj, "Entries").get_array();
|
2015-01-18 15:59:41 +01:00
|
|
|
for(json_spirit::Array::iterator it = aEntries.begin(); it != aEntries.end(); ++it)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
SecureString sEntryUuid(decrypt(json_spirit::find_value((*it).get_obj(), "Uuid").get_str().c_str()));
|
|
|
|
SecureString sEntryName(decrypt(json_spirit::find_value((*it).get_obj(), "Name").get_str().c_str()));
|
|
|
|
SecureString sEntryLogin(decrypt(json_spirit::find_value((*it).get_obj(), "Login").get_str().c_str()));
|
|
|
|
SecureString sEntryPassword(decrypt(json_spirit::find_value((*it).get_obj(), "Password").get_str().c_str()));
|
|
|
|
CKeePassEntry entry(sEntryUuid, sEntryUuid, sEntryLogin, sEntryPassword);
|
|
|
|
vEntries.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vEntries;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
SecureString CKeePassIntegrator::generateRandomKey(size_t nSize)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
// Generates random key
|
|
|
|
SecureString key;
|
|
|
|
key.resize(nSize);
|
|
|
|
|
|
|
|
RandAddSeedPerfmon();
|
|
|
|
RAND_bytes((unsigned char *) &key[0], nSize);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct POST body for RPC JSON call
|
|
|
|
std::string CKeePassIntegrator::constructHTTPPost(const std::string& strMsg, const std::map<std::string,std::string>& mapRequestHeaders)
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "POST / HTTP/1.1\r\n"
|
2015-03-19 15:15:08 +01:00
|
|
|
<< "User-Agent: dash-json-rpc/" << FormatFullVersion() << "\r\n"
|
2014-12-26 12:53:29 +01:00
|
|
|
<< "Host: localhost\r\n"
|
|
|
|
<< "Content-Type: application/json\r\n"
|
|
|
|
<< "Content-Length: " << strMsg.size() << "\r\n"
|
|
|
|
<< "Connection: close\r\n"
|
|
|
|
<< "Accept: application/json\r\n";
|
|
|
|
BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, mapRequestHeaders)
|
|
|
|
s << item.first << ": " << item.second << "\r\n";
|
|
|
|
s << "\r\n" << strMsg;
|
|
|
|
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send RPC message to KeePassHttp
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::doHTTPPost(const std::string& sRequest, int& nStatus, std::string& sResponse)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
// Prepare communication
|
|
|
|
boost::asio::io_service io_service;
|
|
|
|
|
|
|
|
// Get a list of endpoints corresponding to the server name.
|
|
|
|
tcp::resolver resolver(io_service);
|
|
|
|
tcp::resolver::query query(KEEPASS_KEEPASSHTTP_HOST, boost::lexical_cast<std::string>(nPort));
|
|
|
|
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
|
|
|
|
tcp::resolver::iterator end;
|
|
|
|
|
|
|
|
// Try each endpoint until we successfully establish a connection.
|
|
|
|
tcp::socket socket(io_service);
|
|
|
|
boost::system::error_code error = boost::asio::error::host_not_found;
|
|
|
|
while (error && endpoint_iterator != end)
|
|
|
|
{
|
|
|
|
socket.close();
|
|
|
|
socket.connect(*endpoint_iterator++, error);
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(error)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw boost::system::system_error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Form the request.
|
|
|
|
std::map<std::string, std::string> mapRequestHeaders;
|
|
|
|
std::string strPost = constructHTTPPost(sRequest, mapRequestHeaders);
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
// Logging of actual post data disabled as to not write passphrase in debug.log. Only enable temporarily when needed
|
2015-07-31 17:46:47 +02:00
|
|
|
//LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - send POST data: %s\n", strPost);
|
|
|
|
LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - send POST data\n");
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
boost::asio::streambuf request;
|
|
|
|
std::ostream request_stream(&request);
|
|
|
|
request_stream << strPost;
|
|
|
|
|
|
|
|
// Send the request.
|
|
|
|
boost::asio::write(socket, request);
|
|
|
|
|
2015-07-31 17:46:47 +02:00
|
|
|
LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - request written\n");
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// Read the response status line. The response streambuf will automatically
|
|
|
|
// grow to accommodate the entire line. The growth may be limited by passing
|
|
|
|
// a maximum size to the streambuf constructor.
|
|
|
|
boost::asio::streambuf response;
|
|
|
|
boost::asio::read_until(socket, response, "\r\n");
|
|
|
|
|
2015-07-31 17:46:47 +02:00
|
|
|
LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - request status line read\n");
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// Receive HTTP reply status
|
|
|
|
int nProto = 0;
|
|
|
|
std::istream response_stream(&response);
|
|
|
|
nStatus = ReadHTTPStatus(response_stream, nProto);
|
|
|
|
|
2015-07-31 17:46:47 +02:00
|
|
|
LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - reading response body start\n");
|
2014-12-26 12:53:29 +01:00
|
|
|
// Read until EOF, writing data to output as we go.
|
|
|
|
while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
|
|
|
|
{
|
2015-01-18 15:59:41 +01:00
|
|
|
if (error != boost::asio::error::eof)
|
|
|
|
{
|
|
|
|
if (error != 0)
|
|
|
|
{ // 0 is success
|
2014-12-26 12:53:29 +01:00
|
|
|
throw boost::system::system_error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 17:46:47 +02:00
|
|
|
LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - reading response body end\n");
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// Receive HTTP reply message headers and body
|
|
|
|
std::map<std::string, std::string> mapHeaders;
|
2015-04-03 00:51:08 +02:00
|
|
|
ReadHTTPMessage(response_stream, mapHeaders, sResponse, nProto, std::numeric_limits<size_t>::max());
|
2015-07-31 17:46:47 +02:00
|
|
|
LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - Processed body\n");
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::rpcTestAssociation(bool bTriggerUnlock)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
CKeePassRequest request(sKey, "test-associate");
|
|
|
|
request.addStrParameter("TriggerUnlock", std::string(bTriggerUnlock ? "true" : "false"));
|
|
|
|
|
|
|
|
int nStatus;
|
|
|
|
std::string sResponse;
|
|
|
|
|
|
|
|
doHTTPPost(request.getJson(), nStatus, sResponse);
|
|
|
|
|
2015-07-31 17:46:47 +02:00
|
|
|
LogPrint("keepass", "CKeePassIntegrator::rpcTestAssociation - send result: status: %d response: %s\n", nStatus, sResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
std::vector<CKeePassIntegrator::CKeePassEntry> CKeePassIntegrator::rpcGetLogins()
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// Convert key format
|
|
|
|
SecureString sKey = DecodeBase64Secure(sKeyBase64);
|
|
|
|
|
|
|
|
CKeePassRequest request(sKey, "get-logins");
|
|
|
|
request.addStrParameter("addStrParameter", std::string("true"));
|
|
|
|
request.addStrParameter("TriggerUnlock", std::string("true"));
|
|
|
|
request.addStrParameter("Id", sKeePassId);
|
|
|
|
request.addStrParameter("Url", sUrl);
|
|
|
|
|
|
|
|
int nStatus;
|
|
|
|
std::string sResponse;
|
|
|
|
|
|
|
|
doHTTPPost(request.getJson(), nStatus, sResponse);
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
// Logging of actual response data disabled as to not write passphrase in debug.log. Only enable temporarily when needed
|
2015-07-31 17:46:47 +02:00
|
|
|
//LogPrint("keepass", "CKeePassIntegrator::rpcGetLogins - send result: status: %d response: %s\n", nStatus, sResponse);
|
|
|
|
LogPrint("keepass", "CKeePassIntegrator::rpcGetLogins - send result: status: %d\n", nStatus);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(nStatus != 200)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sErrorMessage = "Error returned by KeePassHttp: HTTP code ";
|
|
|
|
sErrorMessage += boost::lexical_cast<std::string>(nStatus);
|
|
|
|
sErrorMessage += " - Response: ";
|
|
|
|
sErrorMessage += " response: [";
|
|
|
|
sErrorMessage += sResponse;
|
|
|
|
sErrorMessage += "]";
|
|
|
|
throw std::runtime_error(sErrorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response
|
|
|
|
CKeePassResponse response(sKey, sResponse);
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(!response.getSuccess())
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sErrorMessage = "KeePassHttp returned failure status";
|
|
|
|
throw std::runtime_error(sErrorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.getEntries();
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::rpcSetLogin(const SecureString& strWalletPass, const SecureString& sEntryId)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// Convert key format
|
|
|
|
SecureString sKey = DecodeBase64Secure(sKeyBase64);
|
|
|
|
|
|
|
|
CKeePassRequest request(sKey, "set-login");
|
|
|
|
request.addStrParameter("Id", sKeePassId);
|
|
|
|
request.addStrParameter("Url", sUrl);
|
2015-01-18 15:59:41 +01:00
|
|
|
|
2015-07-31 17:46:47 +02:00
|
|
|
LogPrint("keepass", "CKeePassIntegrator::rpcSetLogin - send Url: %s\n", sUrl);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
//request.addStrParameter("SubmitUrl", sSubmitUrl); // Is used to construct the entry title
|
2015-03-19 15:15:08 +01:00
|
|
|
request.addStrParameter("Login", SecureString("dash"));
|
2014-12-26 12:53:29 +01:00
|
|
|
request.addStrParameter("Password", strWalletPass);
|
2015-01-18 15:59:41 +01:00
|
|
|
if(sEntryId.size() != 0)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
request.addStrParameter("Uuid", sEntryId); // Update existing
|
|
|
|
}
|
|
|
|
|
|
|
|
int nStatus;
|
|
|
|
std::string sResponse;
|
|
|
|
|
|
|
|
doHTTPPost(request.getJson(), nStatus, sResponse);
|
|
|
|
|
|
|
|
|
2015-07-31 17:46:47 +02:00
|
|
|
LogPrint("keepass", "CKeePassIntegrator::rpcSetLogin - send result: status: %d response: %s\n", nStatus, sResponse);
|
2015-01-18 15:59:41 +01:00
|
|
|
|
|
|
|
if(nStatus != 200)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sErrorMessage = "Error returned: HTTP code ";
|
|
|
|
sErrorMessage += boost::lexical_cast<std::string>(nStatus);
|
|
|
|
sErrorMessage += " - Response: ";
|
|
|
|
sErrorMessage += " response: [";
|
|
|
|
sErrorMessage += sResponse;
|
|
|
|
sErrorMessage += "]";
|
|
|
|
throw std::runtime_error(sErrorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response
|
|
|
|
CKeePassResponse response(sKey, sResponse);
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(!response.getSuccess())
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("KeePassHttp returned failure status");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
SecureString CKeePassIntegrator::generateKeePassKey()
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
SecureString sKey = generateRandomKey(KEEPASS_CRYPTO_KEY_SIZE);
|
|
|
|
SecureString sKeyBase64 = EncodeBase64Secure(sKey);
|
|
|
|
return sKeyBase64;
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::rpcAssociate(std::string& sId, SecureString& sKeyBase64)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
sKey = generateRandomKey(KEEPASS_CRYPTO_KEY_SIZE);
|
|
|
|
CKeePassRequest request(sKey, "associate");
|
|
|
|
|
|
|
|
sKeyBase64 = EncodeBase64Secure(sKey);
|
|
|
|
request.addStrParameter("Key", std::string(&sKeyBase64[0], sKeyBase64.size()));
|
|
|
|
|
|
|
|
int nStatus;
|
|
|
|
std::string sResponse;
|
|
|
|
|
|
|
|
doHTTPPost(request.getJson(), nStatus, sResponse);
|
|
|
|
|
2015-07-31 17:46:47 +02:00
|
|
|
LogPrint("keepass", "CKeePassIntegrator::rpcAssociate - send result: status: %d response: %s\n", nStatus, sResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(nStatus != 200)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sErrorMessage = "Error returned: HTTP code ";
|
|
|
|
sErrorMessage += boost::lexical_cast<std::string>(nStatus);
|
|
|
|
sErrorMessage += " - Response: ";
|
|
|
|
sErrorMessage += " response: [";
|
|
|
|
sErrorMessage += sResponse;
|
|
|
|
sErrorMessage += "]";
|
|
|
|
throw std::runtime_error(sErrorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response
|
|
|
|
CKeePassResponse response(sKey, sResponse);
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(!response.getSuccess())
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("KeePassHttp returned failure status");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got here, we were successful. Return the information
|
|
|
|
sId = response.getStr("Id");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve wallet passphrase from KeePass
|
2015-01-18 15:59:41 +01:00
|
|
|
SecureString CKeePassIntegrator::retrievePassphrase()
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// Check we have all required information
|
2015-01-18 15:59:41 +01:00
|
|
|
if(sKey.size() == 0)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("keepasskey parameter is not defined. Please specify the configuration parameter.");
|
|
|
|
}
|
2015-01-18 15:59:41 +01:00
|
|
|
if(sKeePassId.size() == 0)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("keepassid parameter is not defined. Please specify the configuration parameter.");
|
|
|
|
}
|
2015-01-18 15:59:41 +01:00
|
|
|
if(sKeePassEntryName == "")
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("keepassname parameter is not defined. Please specify the configuration parameter.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve matching logins from KeePass
|
|
|
|
std::vector<CKeePassIntegrator::CKeePassEntry> entries = rpcGetLogins();
|
|
|
|
|
|
|
|
// Only accept one unique match
|
2015-01-18 15:59:41 +01:00
|
|
|
if(entries.size() == 0)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("KeePassHttp returned 0 matches, please verify the keepassurl setting.");
|
|
|
|
}
|
2015-01-18 15:59:41 +01:00
|
|
|
if(entries.size() > 1)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("KeePassHttp returned multiple matches, bailing out.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries[0].getPassword();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update wallet passphrase in keepass
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::updatePassphrase(const SecureString& sWalletPassphrase)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
// Check we have all required information
|
2015-01-18 15:59:41 +01:00
|
|
|
if(sKey.size() == 0)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("keepasskey parameter is not defined. Please specify the configuration parameter.");
|
|
|
|
}
|
2015-01-18 15:59:41 +01:00
|
|
|
if(sKeePassId.size() == 0)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("keepassid parameter is not defined. Please specify the configuration parameter.");
|
|
|
|
}
|
2015-01-18 15:59:41 +01:00
|
|
|
if(sKeePassEntryName == "")
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("keepassname parameter is not defined. Please specify the configuration parameter.");
|
|
|
|
}
|
|
|
|
|
|
|
|
SecureString sEntryId("");
|
|
|
|
|
|
|
|
std::string sErrorMessage;
|
|
|
|
|
|
|
|
// Lookup existing entry
|
|
|
|
std::vector<CKeePassIntegrator::CKeePassEntry> vEntries = rpcGetLogins();
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(vEntries.size() > 1)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("KeePassHttp returned multiple matches, bailing out.");
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(vEntries.size() == 1)
|
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
sEntryId = vEntries[0].getUuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update wallet passphrase in KeePass
|
|
|
|
rpcSetLogin(sWalletPassphrase, sEntryId);
|
|
|
|
}
|