Exemplo n.º 1
0
int yk_write_config(YK_KEY *yk, YK_CONFIG *cfg, int confnum,
		    unsigned char *acc_code)
{
	uint8_t command;
	switch(confnum) {
	case 1:
		command = SLOT_CONFIG;
		break;
	case 2:
		command = SLOT_CONFIG2;
		break;
	default:
		yk_errno = YK_EINVALIDCMD;
		return 0;
	}
	if(!yk_write_command(yk, cfg, command, acc_code)) {
		return 0;
	}
	return 1;
}
void YubiKeyWriter::writeConfig(YubiKeyConfig *ykConfig) {

    YubiKeyFinder::getInstance()->stop();

    YK_KEY *yk = 0;
    YK_STATUS *ykst = YubiKeyFinder::getInstance()->status();
    YKP_CONFIG *cfg = ykp_alloc();

    bool error = false;

    qDebug() << "-------------------------";
    qDebug() << "Starting write config";
    qDebug() << "-------------------------";

    try {
        if (!(yk = yk_open_first_key())) {
            throw 0;
        } else if (!yk_get_status(yk, ykst)) {
            throw 0;
        }

        ykp_configure_version(cfg, ykst);

        qDebug() << "writer:configuration slot:" << ykConfig->configSlot();

        bool useAccessCode;
        unsigned char accessCode[MAX_SIZE];

        if(!assembleConfig(ykConfig, cfg, &useAccessCode, accessCode)) {
            throw 0;
        }

        //Log configuration...
        qDebug() << "-------------------------";
        qDebug() << "Config data to be written to key configuration...";

        char conf_buf[1024];
        ykp_export_config(cfg, conf_buf, 1024, YKP_FORMAT_LEGACY);
        qDebug() << conf_buf;

        qDebug() << "-------------------------";

        // Write configuration...
        if (!yk_write_command(yk,
                              ykp_core_config(cfg), ykp_command(cfg),
                              useAccessCode ? accessCode : NULL)) {
            qDebug() << "Failure";
            throw 0;
        }
        qDebug() << "Success... config written";
        emit diagnostics(QString("Successfully wrote config to slot %1").arg(ykp_config_num(cfg)));

        YubiKeyLogger::logConfig(ykConfig);
        emit configWritten(true, NULL);
    }
    catch(...) {
        error = true;
    }

    if (cfg) {
        ykp_free_config(cfg);
    }

    if (yk && !yk_close_key(yk)) {
        error = true;
    }

    YubiKeyFinder::getInstance()->start();

    if(error) {
        qDebug() << "Config not written";
        QString errMsg = reportError();
        emit configWritten(false, errMsg);
    }

    qDebug() << "-------------------------";
    qDebug() << "Stopping write config";
    qDebug() << "-------------------------";
}
Exemplo n.º 3
0
///
/// Set the ChallengeResponse key on specified slot
///
__declspec(dllexport) int SetChallengeResponse(uint32_t slot, const unsigned char* secret, uint32_t keysize, BOOL userpress, unsigned char* access_code)
{
	YK_CONFIG config;
	uint8_t command;
	YK_KEY* key;
	int result = 0;

	memset(&config, 0, sizeof(config));

	if (slot == 1)
	{
		command = SLOT_CONFIG;
	}
	else if (slot == 2)
	{
		command = SLOT_CONFIG2;
	}
	else
	{
		// invalid slot
		return ERROR_INVALID_SLOT;
	}

	if (keysize == KEY_SIZE_OATH)
	{
		// put first 16 in key and last 4 in uid
		memcpy(config.key, secret, KEY_SIZE);
		memcpy(config.uid, secret + KEY_SIZE, keysize - KEY_SIZE);

		config.tktFlags |= TKTFLAG_CHAL_RESP;
		config.cfgFlags |= CFGFLAG_CHAL_HMAC;
		config.cfgFlags |= CFGFLAG_HMAC_LT64;
	}
	else if (keysize == KEY_SIZE)
	{
		memcpy(config.key, secret, KEY_SIZE);

		config.tktFlags |= TKTFLAG_CHAL_RESP;
		config.cfgFlags |= CFGFLAG_CHAL_YUBICO;
	}
	else
	{
		// invalid key size
		return ERROR_INVALID_KEYSIZE;
	}

	if (userpress)
	{
		config.cfgFlags |= CFGFLAG_CHAL_BTN_TRIG;
	}

	key = yk_open_first_key();
	if (key == NULL)
	{
		// cannot open
		_lastError = yk_errno;
		return ERROR_CANNOT_OPEN_KEY;
	}

	if (!yk_write_command(key, &config, command, access_code ? access_code : NULL))
	{
		result = ERROR_WRITING_SLOT;
		_lastError = yk_errno;
	}
	yk_close_key(key);

	return result;
}