void TorController::add_onion_cb(TorControlConnection& conn, const TorControlReply& reply) { if (reply.code == 250) { LogPrint("tor", "tor: ADD_ONION successful\n"); for (const std::string &s : reply.lines) { std::map<std::string,std::string> m = ParseTorReplyMapping(s); std::map<std::string,std::string>::iterator i; if ((i = m.find("ServiceID")) != m.end()) service_id = i->second; if ((i = m.find("PrivateKey")) != m.end()) private_key = i->second; } service = CService(service_id+".onion", GetListenPort()); LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString()); if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) { LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile()); } else { LogPrintf("tor: Error writing service private key to %s\n", GetPrivateKeyFile()); } AddLocal(service, LOCAL_MANUAL); // ... onion requested - keep connection open } else if (reply.code == 510) { // 510 Unrecognized command LogPrintf("tor: Add onion failed with unrecognized command (You probably need to upgrade Tor)\n"); } else { LogPrintf("tor: Add onion failed; error code %d\n", reply.code); } }
void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlReply& reply) { if (reply.code == 250) { LogPrint(BCLog::TOR, "tor: ADD_ONION successful\n"); BOOST_FOREACH(const std::string &s, reply.lines) { std::map<std::string,std::string> m = ParseTorReplyMapping(s); std::map<std::string,std::string>::iterator i; if ((i = m.find("ServiceID")) != m.end()) service_id = i->second; if ((i = m.find("PrivateKey")) != m.end()) private_key = i->second; } if (service_id.empty()) { LogPrintf("tor: Error parsing ADD_ONION parameters:\n"); for (const std::string &s : reply.lines) { LogPrintf(" %s\n", SanitizeString(s)); } return; } service = LookupNumeric(std::string(service_id+".onion").c_str(), GetListenPort()); LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString()); if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) { LogPrint(BCLog::TOR, "tor: Cached service private key to %s\n", GetPrivateKeyFile().string()); } else { LogPrintf("tor: Error writing service private key to %s\n", GetPrivateKeyFile().string()); } AddLocal(service, LOCAL_MANUAL); // ... onion requested - keep connection open } else if (reply.code == 510) { // 510 Unrecognized command
TorController::TorController(struct event_base* base, const std::string& target): base(base), target(target), conn(base), reconnect(true), reconnect_ev(0), reconnect_timeout(RECONNECT_TIMEOUT_START) { // Start connection attempts immediately if (!conn.Connect(target, boost::bind(&TorController::connected_cb, this, _1), boost::bind(&TorController::disconnected_cb, this, _1) )) { LogPrintf("tor: Initiating connection to Tor control port %s failed\n", target); } // Read service private key if cached std::pair<bool,std::string> pkf = ReadBinaryFile(GetPrivateKeyFile()); if (pkf.first) { LogPrint("tor", "tor: Reading cached private key from %s\n", GetPrivateKeyFile()); private_key = pkf.second; } }
TorController::TorController(struct event_base* _base, const std::string& _target): base(_base), target(_target), conn(base), reconnect(true), reconnect_ev(0), reconnect_timeout(RECONNECT_TIMEOUT_START) { reconnect_ev = event_new(base, -1, 0, reconnect_cb, this); if (!reconnect_ev) LogPrintf("tor: Failed to create event for reconnection: out of memory?\n"); // Start connection attempts immediately if (!conn.Connect(_target, boost::bind(&TorController::connected_cb, this, _1), boost::bind(&TorController::disconnected_cb, this, _1) )) { LogPrintf("tor: Initiating connection to Tor control port %s failed\n", _target); } // Read service private key if cached std::pair<bool,std::string> pkf = ReadBinaryFile(GetPrivateKeyFile()); if (pkf.first) { LogPrint(BCLog::TOR, "tor: Reading cached private key from %s\n", GetPrivateKeyFile().string()); private_key = pkf.second; } }
int CDSAKeyGenerator::Generate() //Generate a DSA key with pre-determined length { unsigned char* pbSeed = NULL; DSA* pDSAParams = NULL; FILE* fp = NULL; LPSTR pbPassword = NULL; const _TCHAR* pPrivKeyFile = NULL; int retVal = FAIL; int retFunc = FAIL; pPrivKeyFile = GetPrivateKeyFile(); if(!pPrivKeyFile) { PrintErrorInfo("Bad parameter error!", EGeneric, constparams); return 0; } OPENSSL_add_all_algorithms_conf(); ERR_load_crypto_strings(); int dwKeyLength = 0; dwKeyLength = GetKeyLength(); try { retVal = GenerateSeed(dwKeyLength, &pbSeed); if(retVal != SUCCESS) { throw EMSCrypto; } //Generate DSA params (p,q and g) _tprintf(_T("\nGenerating DSA key .")); pDSAParams = DSA_generate_parameters(dwKeyLength, pbSeed, dwKeyLength, NULL, NULL, DSAKeyStatus, NULL); if(!pDSAParams) { PrintErrorInfo("Error generating DSA key params!", EOPENSSL, constparams); throw EOPENSSL; } //Generate DSA key retVal = DSA_generate_key(pDSAParams); if(!retVal) { PrintErrorInfo("DSA key generation failed!", EOPENSSL, constparams); throw EOPENSSL; } _tprintf(_T("Generated!\n")); //Create a key file fp = _tfopen(pPrivKeyFile, _T("w")); if(!fp) { PrintErrorInfo("Error creating key file!", EGeneric, constparams); throw EOPENSSL; } //Write generated DSA key to the key file if(m_bPassword) { DWORD len = 0; len = _tcslen(GetPassword()); pbPassword = MakeMBCSString(GetPassword(), CP_UTF8, len); retVal = PEM_write_DSAPrivateKey(fp, pDSAParams, EVP_des_ede3_cbc(), (unsigned char *) pbPassword, len, NULL, NULL); delete pbPassword; } else if(m_bAsk) { retVal = PEM_write_DSAPrivateKey(fp, pDSAParams, EVP_des_ede3_cbc(), NULL, 0, NULL, NULL); } else { _tprintf(_T("\n")); retVal = PEM_write_DSAPrivateKey(fp, pDSAParams, NULL , NULL, 0, NULL, NULL); } if(!retVal) { PrintErrorInfo("Error writing to key file", EOPENSSL, constparams); throw EOPENSSL; } //Free variables DSA_free(pDSAParams); fclose(fp); SYMBIAN_FREE_MEM(pbSeed); //Get command prompt handle HANDLE hndl = 0; hndl = GetStdHandle(STD_OUTPUT_HANDLE); _tprintf(_T("\nCreated key: ")); DWORD bytesWritten; WriteConsole(hndl, pPrivKeyFile, wcslen(pPrivKeyFile), &bytesWritten, NULL); retFunc = SUCCESS; } catch (...) { //Delete dsa params if(pDSAParams) { DSA_free(pDSAParams); } if (fp) { fclose(fp); } SYMBIAN_FREE_MEM(pbSeed); } return retFunc; }