Exemple #1
0
bool Wallet::walletIsAuthenticatedWithPassphrase() {
    // try to read existing keys if they exist...

    // FIXME: initialize OpenSSL elsewhere soon
    initialize();

    // this should always be false if we don't have a passphrase
    // cached yet
    if (!_passphrase || _passphrase->isEmpty()) {
        return false;
    }
    if (_publicKeys.count() > 0) {
        // we _must_ be authenticated if the publicKeys are there
        DependencyManager::get<WalletScriptingInterface>()->setWalletStatus((uint)WalletStatus::WALLET_STATUS_READY);
        return true;
    }

    // otherwise, we have a passphrase but no keys, so we have to check
    auto publicKey = readPublicKey(keyFilePath().toStdString().c_str());

    if (publicKey.size() > 0) {
        if (auto key = readPrivateKey(keyFilePath().toStdString().c_str())) {
            EC_KEY_free(key);

            // be sure to add the public key so we don't do this over and over
            _publicKeys.push_back(publicKey.toBase64());
            DependencyManager::get<WalletScriptingInterface>()->setWalletStatus((uint)WalletStatus::WALLET_STATUS_READY);
            return true;
        }
    }

    return false;
}
Exemple #2
0
bool Wallet::walletIsAuthenticatedWithPassphrase() {
    // try to read existing keys if they exist...

    // FIXME: initialize OpenSSL elsewhere soon
    initialize();
    qCDebug(commerce) << "walletIsAuthenticatedWithPassphrase: checking" << (!_passphrase || !_passphrase->isEmpty());

    // this should always be false if we don't have a passphrase
    // cached yet
    if (!_passphrase || _passphrase->isEmpty()) {
        if (!getKeyFilePath().isEmpty()) { // If file exists, then it is an old school file that has not been lockered. Must get user's passphrase.
            qCDebug(commerce) << "walletIsAuthenticatedWithPassphrase: No passphrase, but there is an existing wallet.";
            return false;
        } else {
            qCDebug(commerce) << "walletIsAuthenticatedWithPassphrase: New setup.";
            setPassphrase("ACCOUNT"); // Going forward, consider this an account-based client.
        }
    }
    if (_publicKeys.count() > 0) {
        // we _must_ be authenticated if the publicKeys are there
        DependencyManager::get<WalletScriptingInterface>()->setWalletStatus((uint)WalletStatus::WALLET_STATUS_READY);
        qCDebug(commerce) << "walletIsAuthenticatedWithPassphrase: wallet was ready";
        return true;
    }

    // otherwise, we have a passphrase but no keys, so we have to check
    auto publicKey = readPublicKey(keyFilePath());

    if (publicKey.size() > 0) {
        if (auto key = readPrivateKey(keyFilePath())) {
            EC_KEY_free(key);

            // be sure to add the public key so we don't do this over and over
            _publicKeys.push_back(publicKey.toBase64());

            if (*_passphrase != "ACCOUNT") {
                changePassphrase("ACCOUNT"); // Rewrites with salt and constant, and will be lockered that way.
            }
            qCDebug(commerce) << "walletIsAuthenticatedWithPassphrase: wallet now ready";
            return true;
        }
    }
    qCDebug(commerce) << "walletIsAuthenticatedWithPassphrase: wallet not ready";
    return false;
}
void UpdaterDialog::httpDone(bool error)
{
	timeOutTimer->stop();
	if(error)
	{
		if(isVisible())
		{
			if(httpGet->errorString()=="Request aborted")return;
			downloadError();
			return;
		}
		if(feedbackMessage)
			QMessageBox::information(0,"Qt Bitcoin Trader",julyTr("UPDATE_ERROR","Cannot check for update. Network error: %1").arg(httpGet->errorString()));
		exitSlot();
		return;
	}

	if(stateUpdate==0)
	{
		QByteArray dataReceived(httpGet->readAll().replace("\r",""));
		if(dataReceived.size()>10245)exitSlot();
		QMap<QString,QString>versionsMap;
		QStringList dataList=QString(dataReceived).split("\n");
		for(int n=0;n<dataList.count();n++)
		{
			QString varData=dataList.at(n);
			int splitPos=varData.indexOf('=');
			if(splitPos>-1)
			{
				QString varName=varData.left(splitPos);
				varData.remove(0,splitPos+1);
				versionsMap[varName]=varData;
			}
		}

QString os="Src";
bool canAutoUpdate=false;
#ifdef Q_OS_MAC
		os="Mac";
		canAutoUpdate=true;
#endif
#ifdef Q_OS_WIN
		os="Win32";
		canAutoUpdate=true;
#endif
		updateVersion=versionsMap.value(os+"Ver");
		updateSignature=versionsMap.value(os+"Signature").toAscii();
		if(!updateSignature.isEmpty())updateSignature=QByteArray::fromBase64(updateSignature);
		updateChangeLog=versionsMap.value(os+"ChangeLog");
		updateLink=versionsMap.value(os+"Bin");
		if(updateVersion.toDouble()<=appVerReal)
		{
			if(feedbackMessage)
				QMessageBox::information(0,"Qt Bitcoin Trader",julyTr("UP_TO_DATE","Your version of Qt Bitcoin Trader is up to date."));
			exitSlot();
			return;
		}
		stateUpdate=1;
		ui.autoUpdateGroupBox->setVisible(canAutoUpdate);
		ui.changeLogText->setHtml(updateChangeLog);
		ui.versionLabel->setText("v"+updateVersion);

#ifdef Q_OS_WIN
		if(QtWin::isCompositionEnabled())
			QtWin::extendFrameIntoClientArea(this);
#endif

#ifdef GENERATE_LANGUAGE_FILE
		julyTranslator->loadMapFromUi(this);
		julyTranslator->saveToFile("LanguageDefault.lng");
#endif
		julyTranslator->translateUi(this);
		ui.iconLabel->setPixmap(QPixmap(":/Resources/QtBitcoinTrader.png"));
		QSize minSizeHint=minimumSizeHint();
		if(mainWindow.isValidSize(&minSizeHint))setFixedSize(minimumSizeHint());
		show();
	}
	else
	if(stateUpdate==1)
	{
		QByteArray binData=httpGet->readAll();
		QByteArray fileSha1=QCryptographicHash::hash(binData,QCryptographicHash::Sha1);
		QFile readPublicKey(":/Resources/Public.key");
		if(!readPublicKey.open(QIODevice::ReadOnly)){QMessageBox::critical(this,windowTitle(),"Public.key is missing");return;}
		QByteArray publicKey=readPublicKey.readAll();
		QByteArray decrypted=JulyRSA::getSignature(updateSignature,publicKey);
		if(decrypted==fileSha1)
		{
			QString curBin=QApplication::applicationFilePath();
			QString updBin=curBin+".upd";
			QString bkpBin=curBin+".bkp";
			if(QFile::exists(updBin))QFile::remove(updBin);
			if(QFile::exists(bkpBin))QFile::remove(bkpBin);
			if(QFile::exists(updBin)||QFile::exists(bkpBin)){downloadError();return;}
			{
				QFile wrFile(updBin);
				if(wrFile.open(QIODevice::WriteOnly|QIODevice::Truncate))
				{
					wrFile.write(binData);
					wrFile.close();
				}else {downloadError();return;}
			}
			QByteArray fileData;
			{
			QFile opFile(updBin);
			if(opFile.open(QIODevice::ReadOnly))fileData=opFile.readAll();
			opFile.close();
			}
			if(QCryptographicHash::hash(fileData,QCryptographicHash::Sha1)!=fileSha1){downloadError();return;}
			QFile::rename(curBin,bkpBin);
			if(!QFile::exists(bkpBin)){downloadError();return;}
			QFile::rename(updBin,curBin);
			if(!QFile::exists(curBin)){QMessageBox::critical(this,windowTitle(),"Critical error. Please reinstall application. Download it from http://sourceforge.net/projects/bitcointrader/<br>File not exists: "+curBin+"\n"+updBin);downloadError();return;}
#ifdef Q_OS_MAC
            QFile(curBin).setPermissions(QFile(bkpBin).permissions());
#endif
            QMessageBox::information(this,windowTitle(),julyTr("UPDATED_SUCCESSFULLY","Application updated successfully. Please restart application to apply changes."));
			exitSlot();
		}
	}
}
int main( void ){
    // int i2d_RSAPublicKey(RSA *a, unsigned char **pp);
    // int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp);
    const char* keyStr =
        "-----BEGIN PUBLIC KEY-----\n"
        "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAws5ZlcsFQv8oh+f5YDE/\n"
        "Dqro+tyQxcRpw8Ykjo/Vxq/x7rFgCZch7IUWfImTXEiYbePVApgcCFS/yMBJpaG9\n"
        "mWYbYDmpQEMrYEAdo7dB0A6NS/DFvdlTmhUxe2YBqeP7U+s5pZ1nekhVD1vCkJro\n"
        "P8Z8pwOZ4kDo1pWDcguL8j0c0a5JeO24sBtBxak3lDOlTdIrc6ulJ/cNrhzIhbmu\n"
        "QUTwImsmOH/SYHHKhMctPAU26CRai8NmhIucNx+0LYhikaJXgfdyHD/a7RdSqMHy\n"
        "QWqRjvEyk7DJOEojSEF8OlES24qoyMTNRUIndrQc2u96oQToQh9sjg6S0g8TlWc0\n"
        "BwIDAQAB\n"
        "-----END PUBLIC KEY-----"
    ;
    EVP_PKEY* evpKey = readPublicKey( keyStr );
    RSA* rsaKey = EVP_PKEY_get1_RSA( evpKey );

    unsigned char keyBinaryBuffer[ 10240 ] = { 0 };
    unsigned char* keyBinaryBufferPointer = keyBinaryBuffer;
    int keyLength = i2d_RSA_PUBKEY( rsaKey, (unsigned char**)&keyBinaryBufferPointer );

    std::cout << "DER Format (" << keyLength << "):" << std::hex << std::setfill( '0' ) << std::endl;
    for( int i = 0; i < keyLength; ++i ){
        if( i && (i % 10 == 0) ){
            std::cout << std::endl;
        }
        std::cout << "0x" << std::setw( 2 ) << (int)keyBinaryBuffer[ i ] << ", ";
    }
    std::cout << std::endl;

    const unsigned char keyBinary[] = {
        0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
        0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,
        0x01, 0x01, 0x00, 0xc2, 0xce, 0x59, 0x95, 0xcb, 0x05, 0x42,
        0xff, 0x28, 0x87, 0xe7, 0xf9, 0x60, 0x31, 0x3f, 0x0e, 0xaa,
        0xe8, 0xfa, 0xdc, 0x90, 0xc5, 0xc4, 0x69, 0xc3, 0xc6, 0x24,
        0x8e, 0x8f, 0xd5, 0xc6, 0xaf, 0xf1, 0xee, 0xb1, 0x60, 0x09,
        0x97, 0x21, 0xec, 0x85, 0x16, 0x7c, 0x89, 0x93, 0x5c, 0x48,
        0x98, 0x6d, 0xe3, 0xd5, 0x02, 0x98, 0x1c, 0x08, 0x54, 0xbf,
        0xc8, 0xc0, 0x49, 0xa5, 0xa1, 0xbd, 0x99, 0x66, 0x1b, 0x60,
        0x39, 0xa9, 0x40, 0x43, 0x2b, 0x60, 0x40, 0x1d, 0xa3, 0xb7,
        0x41, 0xd0, 0x0e, 0x8d, 0x4b, 0xf0, 0xc5, 0xbd, 0xd9, 0x53,
        0x9a, 0x15, 0x31, 0x7b, 0x66, 0x01, 0xa9, 0xe3, 0xfb, 0x53,
        0xeb, 0x39, 0xa5, 0x9d, 0x67, 0x7a, 0x48, 0x55, 0x0f, 0x5b,
        0xc2, 0x90, 0x9a, 0xe8, 0x3f, 0xc6, 0x7c, 0xa7, 0x03, 0x99,
        0xe2, 0x40, 0xe8, 0xd6, 0x95, 0x83, 0x72, 0x0b, 0x8b, 0xf2,
        0x3d, 0x1c, 0xd1, 0xae, 0x49, 0x78, 0xed, 0xb8, 0xb0, 0x1b,
        0x41, 0xc5, 0xa9, 0x37, 0x94, 0x33, 0xa5, 0x4d, 0xd2, 0x2b,
        0x73, 0xab, 0xa5, 0x27, 0xf7, 0x0d, 0xae, 0x1c, 0xc8, 0x85,
        0xb9, 0xae, 0x41, 0x44, 0xf0, 0x22, 0x6b, 0x26, 0x38, 0x7f,
        0xd2, 0x60, 0x71, 0xca, 0x84, 0xc7, 0x2d, 0x3c, 0x05, 0x36,
        0xe8, 0x24, 0x5a, 0x8b, 0xc3, 0x66, 0x84, 0x8b, 0x9c, 0x37,
        0x1f, 0xb4, 0x2d, 0x88, 0x62, 0x91, 0xa2, 0x57, 0x81, 0xf7,
        0x72, 0x1c, 0x3f, 0xda, 0xed, 0x17, 0x52, 0xa8, 0xc1, 0xf2,
        0x41, 0x6a, 0x91, 0x8e, 0xf1, 0x32, 0x93, 0xb0, 0xc9, 0x38,
        0x4a, 0x23, 0x48, 0x41, 0x7c, 0x3a, 0x51, 0x12, 0xdb, 0x8a,
        0xa8, 0xc8, 0xc4, 0xcd, 0x45, 0x42, 0x27, 0x76, 0xb4, 0x1c,
        0xda, 0xef, 0x7a, 0xa1, 0x04, 0xe8, 0x42, 0x1f, 0x6c, 0x8e,
        0x0e, 0x92, 0xd2, 0x0f, 0x13, 0x95, 0x67, 0x34, 0x07, 0x02,
        0x03, 0x01, 0x00, 0x01
    };
    const unsigned char* keyBinaryPointer = keyBinary;

    RSA* reloadedKey = nullptr;
    d2i_RSA_PUBKEY( &reloadedKey, &keyBinaryPointer, (long)sizeof( keyBinary ) );
    EVP_PKEY* reloadedEVPKey = EVP_PKEY_new();
    EVP_PKEY_assign_RSA( reloadedEVPKey, reloadedKey );
    char keyStrBuffer[ 10240 ] = { 0 };
    writePublicKey( reloadedEVPKey, keyStrBuffer, 10240 );
    std::cout << "PEM Format:" << std::endl;
    std::cout << keyStrBuffer << std::endl;
}