Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly refactor keepass module / replace magic number #935

Merged
merged 1 commit into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-createwalletbackups=<n>", strprintf(_("Number of automatic wallet backups (default: %u)"), nWalletBackups));
strUsage += HelpMessageOpt("-walletbackupsdir=<dir>", _("Specify full path to directory for automatic wallet backups (must exist)"));
strUsage += HelpMessageOpt("-keepass", strprintf(_("Use KeePass 2 integration using KeePassHttp plugin (default: %u)"), 0));
strUsage += HelpMessageOpt("-keepassport=<port>", strprintf(_("Connect to KeePassHttp on port <port> (default: %u)"), 19455));
strUsage += HelpMessageOpt("-keepassport=<port>", strprintf(_("Connect to KeePassHttp on port <port> (default: %u)"), DEFAULT_KEEPASS_HTTP_PORT));
strUsage += HelpMessageOpt("-keepasskey=<key>", _("KeePassHttp key for AES encrypted communication with KeePass"));
strUsage += HelpMessageOpt("-keepassid=<name>", _("KeePassHttp id for the established association"));
strUsage += HelpMessageOpt("-keepassname=<name>", _("Name to construct url for KeePass entry that stores the wallet passphrase"));
Expand Down
45 changes: 22 additions & 23 deletions src/keepass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

#include "keepass.h"

#include <exception>
#include <boost/foreach.hpp>

#include "wallet/crypter.h"
#include "clientversion.h"
#include "random.h"
#include "protocol.h"
#include "random.h"
#include "rpcprotocol.h"

// Necessary to prevent compile errors due to forward declaration of
Expand All @@ -21,6 +18,8 @@
#include "util.h"
#include "utilstrencodings.h"

#include <boost/foreach.hpp>

#include <event2/event.h>
#include <event2/http.h>
#include <event2/buffer.h>
Expand Down Expand Up @@ -97,14 +96,14 @@ CKeePassIntegrator::CKeePassIntegrator()
sKey.clear(); // Prevent LockedPageManagerBase complaints
sUrl.clear(); // Prevent LockedPageManagerBase complaints
bIsActive = false;
nPort = KEEPASS_KEEPASSHTTP_PORT;
nPort = DEFAULT_KEEPASS_HTTP_PORT;
}

// Initialze from application context
void CKeePassIntegrator::init()
{
bIsActive = GetBoolArg("-keepass", false);
nPort = GetArg("-keepassport", KEEPASS_KEEPASSHTTP_PORT);
nPort = GetArg("-keepassport", DEFAULT_KEEPASS_HTTP_PORT);
sKeyBase64 = SecureString(GetArg("-keepasskey", "").c_str());
sKeePassId = GetArg("-keepassid", "");
sKeePassEntryName = GetArg("-keepassname", "");
Expand Down Expand Up @@ -296,7 +295,7 @@ void CKeePassIntegrator::doHTTPPost(const std::string& sRequest, int& nStatus, s

// // 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::query query(KEEPASS_HTTP_HOST, boost::lexical_cast<std::string>(nPort));
// tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
// tcp::resolver::iterator end;

Expand All @@ -319,10 +318,10 @@ void CKeePassIntegrator::doHTTPPost(const std::string& sRequest, int& nStatus, s
throw std::runtime_error("cannot create event_base");

// Synchronously look up hostname
struct evhttp_connection *evcon = evhttp_connection_base_new(base, NULL, KEEPASS_KEEPASSHTTP_HOST, KEEPASS_KEEPASSHTTP_PORT); // TODO RAII
struct evhttp_connection *evcon = evhttp_connection_base_new(base, NULL, KEEPASS_HTTP_HOST, DEFAULT_KEEPASS_HTTP_PORT); // TODO RAII
if (evcon == NULL)
throw std::runtime_error("create connection failed");
evhttp_connection_set_timeout(evcon, KEEPASS_KEEPASSHTTP_CONNECT_TIMEOUT);
evhttp_connection_set_timeout(evcon, KEEPASS_HTTP_CONNECT_TIMEOUT);

// Form the request.
// std::map<std::string, std::string> mapRequestHeaders;
Expand All @@ -337,15 +336,15 @@ void CKeePassIntegrator::doHTTPPost(const std::string& sRequest, int& nStatus, s
assert(output_headers);
// s << "POST / HTTP/1.1\r\n"
evhttp_add_header(output_headers, "User-Agent", ("dash-json-rpc/" + FormatFullVersion()).c_str());
evhttp_add_header(output_headers, "Host", KEEPASS_KEEPASSHTTP_HOST);
evhttp_add_header(output_headers, "Host", KEEPASS_HTTP_HOST);
evhttp_add_header(output_headers, "Accept", "application/json");
evhttp_add_header(output_headers, "Content-Type", "application/json");
// evhttp_add_header(output_headers, "Content-Length", itostr(strMsg.size()).c_str());
evhttp_add_header(output_headers, "Connection", "close");

// Logging of actual post data disabled as to not write passphrase in debug.log. Only enable temporarily when needed
//LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - send POST data: %s\n", strPost);
LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - send POST data\n");
//LogPrint("keepass", "CKeePassIntegrator::doHTTPPost -- send POST data: %s\n", strPost);
LogPrint("keepass", "CKeePassIntegrator::doHTTPPost -- send POST data\n");

// boost::asio::streambuf request;
// std::ostream request_stream(&request);
Expand All @@ -354,15 +353,15 @@ void CKeePassIntegrator::doHTTPPost(const std::string& sRequest, int& nStatus, s
// // Send the request.
// boost::asio::write(socket, request);

// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - request written\n");
// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost -- request written\n");

// // 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");

// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - request status line read\n");
// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost -- request status line read\n");

// // Receive HTTP reply status
// int nProto = 0;
Expand All @@ -386,7 +385,7 @@ void CKeePassIntegrator::doHTTPPost(const std::string& sRequest, int& nStatus, s
evhttp_connection_free(evcon);
event_base_free(base);

// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - reading response body start\n");
// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost -- reading response body start\n");
// // Read until EOF, writing data to output as we go.
// while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
// {
Expand All @@ -398,12 +397,12 @@ void CKeePassIntegrator::doHTTPPost(const std::string& sRequest, int& nStatus, s
// }
// }
// }
// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - reading response body end\n");
// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost -- reading response body end\n");
//
// // Receive HTTP reply message headers and body
// std::map<std::string, std::string> mapHeaders;
// ReadHTTPMessage(response_stream, mapHeaders, sResponse, nProto, std::numeric_limits<size_t>::max());
// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost - Processed body\n");
// LogPrint("keepass", "CKeePassIntegrator::doHTTPPost -- Processed body\n");

nStatus = response.status;
if (response.status == 0)
Expand Down Expand Up @@ -434,7 +433,7 @@ void CKeePassIntegrator::rpcTestAssociation(bool bTriggerUnlock)

doHTTPPost(request.getJson(), nStatus, sResponse);

LogPrint("keepass", "CKeePassIntegrator::rpcTestAssociation - send result: status: %d response: %s\n", nStatus, sResponse);
LogPrint("keepass", "CKeePassIntegrator::rpcTestAssociation -- send result: status: %d response: %s\n", nStatus, sResponse);
}

std::vector<CKeePassIntegrator::CKeePassEntry> CKeePassIntegrator::rpcGetLogins()
Expand All @@ -455,8 +454,8 @@ std::vector<CKeePassIntegrator::CKeePassEntry> CKeePassIntegrator::rpcGetLogins(
doHTTPPost(request.getJson(), nStatus, sResponse);

// Logging of actual response data disabled as to not write passphrase in debug.log. Only enable temporarily when needed
//LogPrint("keepass", "CKeePassIntegrator::rpcGetLogins - send result: status: %d response: %s\n", nStatus, sResponse);
LogPrint("keepass", "CKeePassIntegrator::rpcGetLogins - send result: status: %d\n", nStatus);
//LogPrint("keepass", "CKeePassIntegrator::rpcGetLogins -- send result: status: %d response: %s\n", nStatus, sResponse);
LogPrint("keepass", "CKeePassIntegrator::rpcGetLogins -- send result: status: %d\n", nStatus);

if(nStatus != 200)
{
Expand Down Expand Up @@ -491,7 +490,7 @@ void CKeePassIntegrator::rpcSetLogin(const SecureString& strWalletPass, const Se
request.addStrParameter("Id", sKeePassId);
request.addStrParameter("Url", sUrl);

LogPrint("keepass", "CKeePassIntegrator::rpcSetLogin - send Url: %s\n", sUrl);
LogPrint("keepass", "CKeePassIntegrator::rpcSetLogin -- send Url: %s\n", sUrl);

//request.addStrParameter("SubmitUrl", sSubmitUrl); // Is used to construct the entry title
request.addStrParameter("Login", SecureString("dash"));
Expand All @@ -507,7 +506,7 @@ void CKeePassIntegrator::rpcSetLogin(const SecureString& strWalletPass, const Se
doHTTPPost(request.getJson(), nStatus, sResponse);


LogPrint("keepass", "CKeePassIntegrator::rpcSetLogin - send result: status: %d response: %s\n", nStatus, sResponse);
LogPrint("keepass", "CKeePassIntegrator::rpcSetLogin -- send result: status: %d response: %s\n", nStatus, sResponse);

if(nStatus != 200)
{
Expand Down Expand Up @@ -550,7 +549,7 @@ void CKeePassIntegrator::rpcAssociate(std::string& sId, SecureString& sKeyBase64

doHTTPPost(request.getJson(), nStatus, sResponse);

LogPrint("keepass", "CKeePassIntegrator::rpcAssociate - send result: status: %d response: %s\n", nStatus, sResponse);
LogPrint("keepass", "CKeePassIntegrator::rpcAssociate -- send result: status: %d response: %s\n", nStatus, sResponse);

if(nStatus != 200)
{
Expand Down
17 changes: 7 additions & 10 deletions src/keepass.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
#ifndef _KEEPASS_H_
#define _KEEPASS_H_

#define KEEPASS_CRYPTO_KEY_SIZE 32
#define KEEPASS_CRYPTO_BLOCK_SIZE 16
#define KEEPASS_KEEPASSHTTP_HOST "localhost"
#define KEEPASS_KEEPASSHTTP_PORT 19455
#define KEEPASS_KEEPASSHTTP_CONNECT_TIMEOUT 30

#include <string>
#include <vector>
#include <map>
#include "support/allocators/secure.h"

#include <univalue.h>
#include "support/allocators/secure.h"

static const int KEEPASS_CRYPTO_KEY_SIZE = 32;
static const int KEEPASS_CRYPTO_BLOCK_SIZE = 16;
static const int KEEPASS_HTTP_CONNECT_TIMEOUT = 30;
static const unsigned int DEFAULT_KEEPASS_HTTP_PORT = 19455;
static const char* KEEPASS_HTTP_HOST = "localhost";

class CKeePassIntegrator {

Expand Down