2020-01-17 15:42:55 +01:00
|
|
|
// Copyright (c) 2014-2020 The Dash Core 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.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <keepass.h>
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <wallet/crypter.h>
|
|
|
|
#include <clientversion.h>
|
|
|
|
#include <protocol.h>
|
|
|
|
#include <random.h>
|
|
|
|
#include <rpc/protocol.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)
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <script/script.h>
|
|
|
|
#include <script/standard.h>
|
2015-04-03 00:51:08 +02:00
|
|
|
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/system.h>
|
|
|
|
#include <util/strencodings.h>
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-02-02 16:28:56 +01:00
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/http.h>
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/keyvalq_struct.h>
|
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/buffer.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <support/cleanse.h> // for OPENSSL_cleanse()
|
2016-02-02 16:28:56 +01:00
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
const char* CKeePassIntegrator::KEEPASS_HTTP_HOST = "localhost";
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
CKeePassIntegrator keePassInt;
|
|
|
|
|
2016-02-02 16:28:56 +01:00
|
|
|
// Base64 decoding with secure memory allocation
|
2016-08-23 13:27:04 +02:00
|
|
|
SecureString DecodeBase64Secure(const SecureString& sInput)
|
2016-02-02 16:28:56 +01:00
|
|
|
{
|
|
|
|
SecureString output;
|
|
|
|
|
|
|
|
// Init openssl BIO with base64 filter and memory input
|
|
|
|
BIO *b64, *mem;
|
|
|
|
b64 = BIO_new(BIO_f_base64());
|
|
|
|
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer
|
2020-01-05 03:00:36 +01:00
|
|
|
mem = BIO_new_mem_buf((void *) sInput.data(), sInput.size());
|
2016-02-02 16:28:56 +01:00
|
|
|
BIO_push(b64, mem);
|
|
|
|
|
|
|
|
// Prepare buffer to receive decoded data
|
2016-08-23 13:27:04 +02:00
|
|
|
if(sInput.size() % 4 != 0) {
|
2016-02-02 16:28:56 +01:00
|
|
|
throw std::runtime_error("Input length should be a multiple of 4");
|
|
|
|
}
|
2016-08-23 13:27:04 +02:00
|
|
|
size_t nMaxLen = sInput.size() / 4 * 3; // upper bound, guaranteed divisible by 4
|
2016-02-02 16:28:56 +01:00
|
|
|
output.resize(nMaxLen);
|
|
|
|
|
|
|
|
// Decode the string
|
|
|
|
size_t nLen;
|
2020-01-05 03:00:36 +01:00
|
|
|
nLen = BIO_read(b64, (void *) output.data(), sInput.size());
|
2016-02-02 16:28:56 +01:00
|
|
|
output.resize(nLen);
|
|
|
|
|
|
|
|
// Free memory
|
|
|
|
BIO_free_all(b64);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base64 encoding with secure memory allocation
|
2016-08-23 13:27:04 +02:00
|
|
|
SecureString EncodeBase64Secure(const SecureString& sInput)
|
2016-02-02 16:28:56 +01:00
|
|
|
{
|
|
|
|
// Init openssl BIO with base64 filter and memory output
|
|
|
|
BIO *b64, *mem;
|
|
|
|
b64 = BIO_new(BIO_f_base64());
|
|
|
|
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); // No newlines in output
|
|
|
|
mem = BIO_new(BIO_s_mem());
|
|
|
|
BIO_push(b64, mem);
|
|
|
|
|
|
|
|
// Decode the string
|
2020-01-05 03:00:36 +01:00
|
|
|
BIO_write(b64, sInput.data(), sInput.size());
|
2016-02-02 16:28:56 +01:00
|
|
|
(void) BIO_flush(b64);
|
|
|
|
|
|
|
|
// Create output variable from buffer mem ptr
|
|
|
|
BUF_MEM *bptr;
|
|
|
|
BIO_get_mem_ptr(b64, &bptr);
|
|
|
|
SecureString output(bptr->data, bptr->length);
|
|
|
|
|
|
|
|
// Cleanse secure data buffer from memory
|
|
|
|
memory_cleanse((void *) bptr->data, bptr->length);
|
|
|
|
|
|
|
|
// Free memory
|
|
|
|
BIO_free_all(b64);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2014-12-26 12:53:29 +01:00
|
|
|
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;
|
2016-08-12 07:39:43 +02:00
|
|
|
nPort = DEFAULT_KEEPASS_HTTP_PORT;
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2021-07-17 21:15:21 +02:00
|
|
|
// Initialize from application context
|
2015-01-18 15:59:41 +01:00
|
|
|
void CKeePassIntegrator::init()
|
|
|
|
{
|
2019-06-24 18:44:27 +02:00
|
|
|
bIsActive = gArgs.GetBoolArg("-keepass", false);
|
|
|
|
nPort = gArgs.GetArg("-keepassport", DEFAULT_KEEPASS_HTTP_PORT);
|
|
|
|
sKeyBase64 = SecureString(gArgs.GetArg("-keepasskey", "").c_str());
|
|
|
|
strKeePassId = gArgs.GetArg("-keepassid", "");
|
|
|
|
strKeePassEntryName = gArgs.GetArg("-keepassname", "");
|
2014-12-26 12:53:29 +01:00
|
|
|
// 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
|
2016-08-23 13:27:04 +02:00
|
|
|
if(strKeePassEntryName.size() > 0)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
sUrl = SecureString("http://");
|
2016-08-23 13:27:04 +02:00
|
|
|
sUrl += SecureString(strKeePassEntryName.c_str());
|
2014-12-26 12:53:29 +01:00
|
|
|
sUrl += SecureString("/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
void CKeePassIntegrator::CKeePassRequest::addStrParameter(const std::string& strName, const std::string& strValue)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2020-06-18 11:17:23 +02:00
|
|
|
requestObj.pushKV(strName, strValue);
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
void CKeePassIntegrator::CKeePassRequest::addStrParameter(const std::string& strName, const SecureString& sValue)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
std::string sCipherValue;
|
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
if(!EncryptAES256(sKey, sValue, strIV, sCipherValue))
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("Unable to encrypt Verifier");
|
|
|
|
}
|
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
addStrParameter(strName, EncodeBase64(sCipherValue));
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
std::string CKeePassIntegrator::CKeePassRequest::getJson()
|
|
|
|
{
|
2016-02-02 16:28:56 +01:00
|
|
|
return requestObj.write();
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2020-01-05 03:00:36 +01:00
|
|
|
strIV = std::string(sIVSecure.data(), sIVSecure.size());
|
2014-12-26 12:53:29 +01:00
|
|
|
// Generate Nonce, Verifier and RequestType
|
|
|
|
SecureString sNonceBase64Secure = EncodeBase64Secure(sIVSecure);
|
2020-01-05 03:00:36 +01:00
|
|
|
addStrParameter("Nonce", std::string(sNonceBase64Secure.data(), sNonceBase64Secure.size())); // Plain
|
2014-12-26 12:53:29 +01:00
|
|
|
addStrParameter("Verifier", sNonceBase64Secure); // Encoded
|
2016-08-23 13:27:04 +02:00
|
|
|
addStrParameter("RequestType", strType);
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
void CKeePassIntegrator::CKeePassResponse::parseResponse(const std::string& strResponse)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2016-02-02 16:28:56 +01:00
|
|
|
UniValue responseValue;
|
2016-08-23 13:27:04 +02:00
|
|
|
if(!responseValue.read(strResponse))
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("Unable to parse KeePassHttp response");
|
|
|
|
}
|
|
|
|
|
2016-02-02 16:28:56 +01:00
|
|
|
responseObj = responseValue;
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// retrieve main values
|
2016-02-02 16:28:56 +01:00
|
|
|
bSuccess = responseObj["Success"].get_bool();
|
2016-08-23 13:27:04 +02:00
|
|
|
strType = getStr("RequestType");
|
|
|
|
strIV = DecodeBase64(getStr("Nonce"));
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
std::string CKeePassIntegrator::CKeePassResponse::getStr(const std::string& strName)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
return responseObj[strName].get_str();
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
SecureString CKeePassIntegrator::CKeePassResponse::getSecureStr(const std::string& strName)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strValueBase64Encrypted(responseObj[strName].get_str());
|
2014-12-26 12:53:29 +01:00
|
|
|
SecureString sValue;
|
2015-01-18 15:59:41 +01:00
|
|
|
try
|
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
sValue = decrypt(strValueBase64Encrypted);
|
2015-01-18 15:59:41 +01:00
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
2021-07-17 21:15:21 +02:00
|
|
|
std::string strError = "Exception occurred while decrypting ";
|
2016-08-23 13:27:04 +02:00
|
|
|
strError += strName + ": " + e.what();
|
|
|
|
throw std::runtime_error(strError);
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
return sValue;
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
SecureString CKeePassIntegrator::CKeePassResponse::decrypt(const std::string& strValueBase64Encrypted)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strValueEncrypted = DecodeBase64(strValueBase64Encrypted);
|
2014-12-26 12:53:29 +01:00
|
|
|
SecureString sValue;
|
2016-08-23 13:27:04 +02:00
|
|
|
if(!DecryptAES256(sKey, strValueEncrypted, strIV, sValue))
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
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;
|
|
|
|
|
2016-02-02 16:28:56 +01:00
|
|
|
UniValue aEntries = responseObj["Entries"].get_array();
|
|
|
|
for(size_t i = 0; i < aEntries.size(); i++)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2016-02-02 16:28:56 +01:00
|
|
|
SecureString sEntryUuid(decrypt(aEntries[i]["Uuid"].get_str().c_str()));
|
|
|
|
SecureString sEntryName(decrypt(aEntries[i]["Name"].get_str().c_str()));
|
|
|
|
SecureString sEntryLogin(decrypt(aEntries[i]["Login"].get_str().c_str()));
|
|
|
|
SecureString sEntryPassword(decrypt(aEntries[i]["Password"].get_str().c_str()));
|
|
|
|
CKeePassEntry entry(sEntryUuid, sEntryName, sEntryLogin, sEntryPassword);
|
2014-12-26 12:53:29 +01:00
|
|
|
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
|
2016-08-23 13:27:04 +02:00
|
|
|
SecureString sKey;
|
|
|
|
sKey.resize(nSize);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2020-01-05 03:00:36 +01:00
|
|
|
GetStrongRandBytes((unsigned char *) sKey.data(), nSize);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
return sKey;
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct POST body for RPC JSON call
|
|
|
|
std::string CKeePassIntegrator::constructHTTPPost(const std::string& strMsg, const std::map<std::string,std::string>& mapRequestHeaders)
|
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
std::ostringstream streamOut;
|
|
|
|
streamOut << "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";
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& item : mapRequestHeaders)
|
2016-08-23 13:27:04 +02:00
|
|
|
streamOut << item.first << ": " << item.second << "\r\n";
|
|
|
|
streamOut << "\r\n" << strMsg;
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
return streamOut.str();
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 16:28:56 +01:00
|
|
|
/** Reply structure for request_done to fill in */
|
|
|
|
struct HTTPReply
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
int nStatus;
|
|
|
|
std::string strBody;
|
2016-02-02 16:28:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void http_request_done(struct evhttp_request *req, void *ctx)
|
|
|
|
{
|
|
|
|
HTTPReply *reply = static_cast<HTTPReply*>(ctx);
|
|
|
|
|
2019-08-06 05:08:33 +02:00
|
|
|
if (req == nullptr) {
|
|
|
|
/* If req is nullptr, it means an error occurred while connecting, but
|
2016-02-02 16:28:56 +01:00
|
|
|
* I'm not sure how to find out which one. We also don't really care.
|
|
|
|
*/
|
2016-08-23 13:27:04 +02:00
|
|
|
reply->nStatus = 0;
|
2016-02-02 16:28:56 +01:00
|
|
|
return;
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
reply->nStatus = evhttp_request_get_response_code(req);
|
2016-02-02 16:28:56 +01:00
|
|
|
|
|
|
|
struct evbuffer *buf = evhttp_request_get_input_buffer(req);
|
|
|
|
if (buf)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2016-02-02 16:28:56 +01:00
|
|
|
size_t size = evbuffer_get_length(buf);
|
|
|
|
const char *data = (const char*)evbuffer_pullup(buf, size);
|
|
|
|
if (data)
|
2016-08-23 13:27:04 +02:00
|
|
|
reply->strBody = std::string(data, size);
|
2016-02-02 16:28:56 +01:00
|
|
|
evbuffer_drain(buf, size);
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
2016-02-02 16:28:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send RPC message to KeePassHttp
|
2018-02-12 13:49:00 +01:00
|
|
|
void CKeePassIntegrator::doHTTPPost(const std::string& sRequest, int& nStatusRet, std::string& strResponseRet)
|
2016-02-02 16:28:56 +01:00
|
|
|
{
|
|
|
|
// Create event base
|
|
|
|
struct event_base *base = event_base_new(); // TODO RAII
|
|
|
|
if (!base)
|
|
|
|
throw std::runtime_error("cannot create event_base");
|
|
|
|
|
|
|
|
// Synchronously look up hostname
|
2019-08-06 05:08:33 +02:00
|
|
|
struct evhttp_connection *evcon = evhttp_connection_base_new(base, nullptr, KEEPASS_HTTP_HOST, DEFAULT_KEEPASS_HTTP_PORT); // TODO RAII
|
|
|
|
if (evcon == nullptr)
|
2016-02-02 16:28:56 +01:00
|
|
|
throw std::runtime_error("create connection failed");
|
2016-08-12 07:39:43 +02:00
|
|
|
evhttp_connection_set_timeout(evcon, KEEPASS_HTTP_CONNECT_TIMEOUT);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-02-02 16:28:56 +01:00
|
|
|
HTTPReply response;
|
|
|
|
struct evhttp_request *req = evhttp_request_new(http_request_done, (void*)&response); // TODO RAII
|
2019-08-06 05:08:33 +02:00
|
|
|
if (req == nullptr)
|
2016-02-02 16:28:56 +01:00
|
|
|
throw std::runtime_error("create http request failed");
|
|
|
|
|
|
|
|
struct evkeyvalq *output_headers = evhttp_request_get_output_headers(req);
|
|
|
|
assert(output_headers);
|
|
|
|
evhttp_add_header(output_headers, "User-Agent", ("dash-json-rpc/" + FormatFullVersion()).c_str());
|
2016-08-12 07:39:43 +02:00
|
|
|
evhttp_add_header(output_headers, "Host", KEEPASS_HTTP_HOST);
|
2016-02-02 16:28:56 +01:00
|
|
|
evhttp_add_header(output_headers, "Accept", "application/json");
|
|
|
|
evhttp_add_header(output_headers, "Content-Type", "application/json");
|
|
|
|
evhttp_add_header(output_headers, "Connection", "close");
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::KEEPASS, "CKeePassIntegrator::doHTTPPost -- send POST data\n");
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-02-02 16:28:56 +01:00
|
|
|
struct evbuffer * output_buffer = evhttp_request_get_output_buffer(req);
|
|
|
|
assert(output_buffer);
|
|
|
|
evbuffer_add(output_buffer, sRequest.data(), sRequest.size());
|
|
|
|
|
|
|
|
int r = evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/");
|
|
|
|
if (r != 0) {
|
|
|
|
evhttp_connection_free(evcon);
|
|
|
|
event_base_free(base);
|
|
|
|
throw std::runtime_error("send http request failed");
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 16:28:56 +01:00
|
|
|
event_base_dispatch(base);
|
|
|
|
evhttp_connection_free(evcon);
|
|
|
|
event_base_free(base);
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
nStatusRet = response.nStatus;
|
2016-08-23 13:27:04 +02:00
|
|
|
if (response.nStatus == 0)
|
2016-02-02 16:28:56 +01:00
|
|
|
throw std::runtime_error("couldn't connect to server");
|
2016-08-23 13:27:04 +02:00
|
|
|
else if (response.nStatus >= 400 && response.nStatus != HTTP_BAD_REQUEST && response.nStatus != HTTP_NOT_FOUND && response.nStatus != HTTP_INTERNAL_SERVER_ERROR)
|
|
|
|
throw std::runtime_error(strprintf("server returned HTTP error %d", response.nStatus));
|
|
|
|
else if (response.strBody.empty())
|
2016-02-02 16:28:56 +01:00
|
|
|
throw std::runtime_error("no response from server");
|
|
|
|
|
|
|
|
// Parse reply
|
|
|
|
UniValue valReply(UniValue::VSTR);
|
2016-08-23 13:27:04 +02:00
|
|
|
if (!valReply.read(response.strBody))
|
2016-02-02 16:28:56 +01:00
|
|
|
throw std::runtime_error("couldn't parse reply from server");
|
|
|
|
const UniValue& reply = valReply.get_obj();
|
|
|
|
if (reply.empty())
|
|
|
|
throw std::runtime_error("expected reply to have result, error and id properties");
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
strResponseRet = valReply.get_str();
|
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;
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strResponse;
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
doHTTPPost(request.getJson(), nStatus, strResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::KEEPASS, "CKeePassIntegrator::rpcTestAssociation -- send result: status: %d response: %s\n", nStatus, strResponse);
|
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"));
|
2016-08-23 13:27:04 +02:00
|
|
|
request.addStrParameter("Id", strKeePassId);
|
2014-12-26 12:53:29 +01:00
|
|
|
request.addStrParameter("Url", sUrl);
|
|
|
|
|
|
|
|
int nStatus;
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strResponse;
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
doHTTPPost(request.getJson(), nStatus, strResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::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)
|
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strError = "Error returned by KeePassHttp: HTTP code ";
|
|
|
|
strError += itostr(nStatus);
|
|
|
|
strError += " - Response: ";
|
|
|
|
strError += " response: [";
|
|
|
|
strError += strResponse;
|
|
|
|
strError += "]";
|
|
|
|
throw std::runtime_error(strError);
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response
|
2016-08-23 13:27:04 +02:00
|
|
|
CKeePassResponse response(sKey, strResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(!response.getSuccess())
|
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strError = "KeePassHttp returned failure status";
|
|
|
|
throw std::runtime_error(strError);
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.getEntries();
|
|
|
|
}
|
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
void CKeePassIntegrator::rpcSetLogin(const SecureString& sWalletPass, const SecureString& sEntryId)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// Convert key format
|
|
|
|
SecureString sKey = DecodeBase64Secure(sKeyBase64);
|
|
|
|
|
|
|
|
CKeePassRequest request(sKey, "set-login");
|
2016-08-23 13:27:04 +02:00
|
|
|
request.addStrParameter("Id", strKeePassId);
|
2014-12-26 12:53:29 +01:00
|
|
|
request.addStrParameter("Url", sUrl);
|
2015-01-18 15:59:41 +01:00
|
|
|
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::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"));
|
2016-08-23 13:27:04 +02:00
|
|
|
request.addStrParameter("Password", sWalletPass);
|
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;
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strResponse;
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
doHTTPPost(request.getJson(), nStatus, strResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::KEEPASS, "CKeePassIntegrator::rpcSetLogin -- send result: status: %d response: %s\n", nStatus, strResponse);
|
2015-01-18 15:59:41 +01:00
|
|
|
|
|
|
|
if(nStatus != 200)
|
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strError = "Error returned: HTTP code ";
|
|
|
|
strError += itostr(nStatus);
|
|
|
|
strError += " - Response: ";
|
|
|
|
strError += " response: [";
|
|
|
|
strError += strResponse;
|
|
|
|
strError += "]";
|
|
|
|
throw std::runtime_error(strError);
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response
|
2016-08-23 13:27:04 +02:00
|
|
|
CKeePassResponse response(sKey, strResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
void CKeePassIntegrator::rpcAssociate(std::string& strIdRet, SecureString& sKeyBase64Ret)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
sKey = generateRandomKey(KEEPASS_CRYPTO_KEY_SIZE);
|
|
|
|
CKeePassRequest request(sKey, "associate");
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
sKeyBase64Ret = EncodeBase64Secure(sKey);
|
2020-01-05 03:00:36 +01:00
|
|
|
request.addStrParameter("Key", std::string(sKeyBase64Ret.data(), sKeyBase64Ret.size()));
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
int nStatus;
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strResponse;
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
doHTTPPost(request.getJson(), nStatus, strResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::KEEPASS, "CKeePassIntegrator::rpcAssociate -- send result: status: %d response: %s\n", nStatus, strResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2015-01-18 15:59:41 +01:00
|
|
|
if(nStatus != 200)
|
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strError = "Error returned: HTTP code ";
|
|
|
|
strError += itostr(nStatus);
|
|
|
|
strError += " - Response: ";
|
|
|
|
strError += " response: [";
|
|
|
|
strError += strResponse;
|
|
|
|
strError += "]";
|
|
|
|
throw std::runtime_error(strError);
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response
|
2016-08-23 13:27:04 +02:00
|
|
|
CKeePassResponse response(sKey, strResponse);
|
2014-12-26 12:53:29 +01:00
|
|
|
|
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
|
2018-02-12 13:49:00 +01:00
|
|
|
strIdRet = response.getStr("Id");
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.");
|
|
|
|
}
|
2016-08-23 13:27:04 +02:00
|
|
|
if(strKeePassId.size() == 0)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("keepassid parameter is not defined. Please specify the configuration parameter.");
|
|
|
|
}
|
2016-08-23 13:27:04 +02:00
|
|
|
if(strKeePassEntryName == "")
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
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
|
2016-08-23 13:27:04 +02:00
|
|
|
std::vector<CKeePassIntegrator::CKeePassEntry> vecEntries = rpcGetLogins();
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// Only accept one unique match
|
2016-08-23 13:27:04 +02:00
|
|
|
if(vecEntries.size() == 0)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("KeePassHttp returned 0 matches, please verify the keepassurl setting.");
|
|
|
|
}
|
2016-08-23 13:27:04 +02:00
|
|
|
if(vecEntries.size() > 1)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("KeePassHttp returned multiple matches, bailing out.");
|
|
|
|
}
|
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
return vecEntries[0].getPassword();
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.");
|
|
|
|
}
|
2016-08-23 13:27:04 +02:00
|
|
|
if(strKeePassId.size() == 0)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("keepassid parameter is not defined. Please specify the configuration parameter.");
|
|
|
|
}
|
2016-08-23 13:27:04 +02:00
|
|
|
if(strKeePassEntryName == "")
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("keepassname parameter is not defined. Please specify the configuration parameter.");
|
|
|
|
}
|
|
|
|
|
|
|
|
SecureString sEntryId("");
|
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
std::string strError;
|
2014-12-26 12:53:29 +01:00
|
|
|
|
|
|
|
// Lookup existing entry
|
2016-08-23 13:27:04 +02:00
|
|
|
std::vector<CKeePassIntegrator::CKeePassEntry> vecEntries = rpcGetLogins();
|
2014-12-26 12:53:29 +01:00
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
if(vecEntries.size() > 1)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2014-12-26 12:53:29 +01:00
|
|
|
throw std::runtime_error("KeePassHttp returned multiple matches, bailing out.");
|
|
|
|
}
|
|
|
|
|
2016-08-23 13:27:04 +02:00
|
|
|
if(vecEntries.size() == 1)
|
2015-01-18 15:59:41 +01:00
|
|
|
{
|
2016-08-23 13:27:04 +02:00
|
|
|
sEntryId = vecEntries[0].getUuid();
|
2014-12-26 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update wallet passphrase in KeePass
|
|
|
|
rpcSetLogin(sWalletPassphrase, sEntryId);
|
|
|
|
}
|