int _test_config_static_slot2(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 0, 0);
	int rc = 0;
	struct config_st *ycfg;

	unsigned char expected[] = {
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
		0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c,
		0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x20, 0xf0, 0x00,
		0x00, 0xe9, 0xf5
	};

	char *argv[] = {
		"unittest", "-2", "-a303132333435363738393a3b3c3d3e3f",
		NULL
	};
	int argc = 3;

	rc = _test_config(cfg, st, argc, argv);
	_check_success(rc, cfg, expected, __LINE__);

	ykp_free_config(cfg);
	free(st);
}
int _test_config_slot1(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(1, 3, 0);
	int rc = 0;
	struct config_st *ycfg;

	unsigned char expected[] = {
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
		0x00, 0x46, 0xc0
	};

	char *argv[] = {
		"unittest", "-1",
		NULL
	};
	int argc = 2;

	rc = _test_config(cfg, st, argc, argv);
	_check_success(rc, cfg, expected, __LINE__);

	ykp_free_config(cfg);
	free(st);
}
int _test_extended_flags1(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 2, 0);
	int rc = 0;

	/* this matches the python-yubico test case test_challenge_response_hmac_nist */
	unsigned char expected[] = {
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x00,
		0x00, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
		0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c,
		0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x04, 0x40, 0x26, 0x00,
		0x00, 0x98, 0x41, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x03, 0x95, 0x56, 0x00, 0x00, 0x00,
	};

	char *argv[] = {
		"unittest", "-2", "-a303132333435363738393a3b3c3d3e3f40414243",
		"-ochal-resp", "-ochal-hmac", "-ohmac-lt64", "-oserial-api-visible",
		NULL
	};
	int argc = 7;

	rc = _test_config(cfg, st, argc, argv);
	_check_success(rc, cfg, expected, __LINE__);

	ykp_free_config(cfg);
	free(st);
}
int _test_oath_hotp_nist_160_bits(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 1, 0);
	int rc = 0;
	struct config_st *ycfg;

	unsigned char expected[] = {
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x00,
		0x00, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
		0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c,
		0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
		0x00, 0x6a, 0xb9
	};

	char *argv[] = {
		"unittest", "-1", "-a303132333435363738393a3b3c3d3e3f40414243", "-ooath-hotp", "-o-append-cr",
		NULL
	};
	int argc = 5;

	rc = _test_config(cfg, st, argc, argv);
	_check_success(rc, cfg, expected, __LINE__);

	ykp_free_config(cfg);
	free(st);
}
void YubiKeyWriter::deleteConfig(int slot, const QString accCode) {
    bool error = false;
    YK_KEY *yk = NULL;
    YKP_CONFIG *cfg = ykp_alloc();

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

    try {
        if (!(yk = yk_open_first_key())) {
            throw 0;
        }

        unsigned char accessCode[MAX_SIZE];
        size_t accessCodeLen = 0;

        if(accCode.length() > 0) {
            int rc = encodeAccessCode(accCode, accessCode, &accessCodeLen);
            if (rc <= 0) {
                qDebug() << "Invalid access code: " << accCode;
                throw 0;
            }
        }

        // write NULL to delete config
        if (!yk_write_config(yk,
                    NULL, slot,
                    accessCodeLen > 0 ? accessCode : NULL)) {
            qDebug() << "Failed to delete.";
            throw 0;
        }
        emit configWritten(true, NULL);
        qDebug() << "successfully deleted slot " << slot;
        emit diagnostics(QString("Deleted slot %1").arg(slot));
    } catch(...) {
        error = true;
    }

    if (cfg) {
        ykp_free_config(cfg);
    }

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

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

    if(error) {
        qDebug() << "Failed to delete configuration in slot" << slot;
        QString errMsg = reportError();
        emit configWritten(false, errMsg);
    }
}
/*
 * Utility function to parse arguments and just return the result code.
 * The calling function does the assert() to get function name in assert output.
 */
int _parse_args_rc(int argc, char *argv[])
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 2, 0);
	int rc = 0;

	rc = _test_config(cfg, st, argc, argv);

	ykp_free_config(cfg);
	free(st);

	return rc;
}
int _test_ndef2_with_neo_beta(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 1, 7);

	char *argv[] = {
		"unittest", "-2", "-nhttps://my.yubico.com/neo/",
	};
	int argc = 2;

	int rc = _test_config(cfg, st, argc, argv);
	assert(rc == 0);
	ykp_free_config(cfg);
	free(st);
}
int _test_slot_two_with_neo_beta(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 1, 7);

	char *argv[] = {
		"unittest", "-2", NULL
	};
	int argc = 2;

	int rc = _test_config(cfg, st, argc, argv);
	assert(rc == 0);
	ykp_free_config(cfg);
	free(st);
}
static void _test_ndef_with_non_neo(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 2, 4);

	char *argv[] = {
		"unittest", "-nhttps://my.yubico.com/neo/",
		NULL
	};
	int argc = 2;

	int rc = _test_config(cfg, st, argc, argv);
	assert(rc == 0);

	ykp_free_config(cfg);
	free(st);
}
static void _test_ndef2_with_neo(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(3, 0, 0);

	char *argv[] = {
		"unittest", "-2", "-nhttps://my.yubico.com/neo/",
		NULL
	};
	int argc = 3;

	int rc = _test_config(cfg, st, argc, argv);
	assert(rc == 1);
	assert(((struct ykp_config_t*)cfg)->command == SLOT_NDEF2);

	ykp_free_config(cfg);
	free(st);
}
int _test_ndef_for_neo_beta(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 1, 7);

	char *argv[] = {
		"unittest", "-nhttps://my.yubico.com/neo/",
		NULL
	};
	int argc = 2;

	int rc = _test_config(cfg, st, argc, argv);
	assert(rc == 1);
	struct config_st *ycfg = (struct config_st *) ykp_core_config(cfg);
	assert(((struct ykp_config_t*)cfg)->command == SLOT_NDEF);

	ykp_free_config(cfg);
	free(st);
}
int _test_too_new_key(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 2, 0);
	int rc = 0;

	char *argv[] = {
		"unittest", "-oticket-first",
		NULL
	};
	int argc = 2;

	rc = _test_config(cfg, st, argc, argv);
	assert(rc == 0);
	assert(ykp_errno == YKP_EYUBIKEYVER);

	ykp_free_config(cfg);
	free(st);
}
int main (void)
{
	YKP_CONFIG *ykp;
	int rc;

	ykp = ykp_create_config ();
	if (!ykp)
	{
		printf ("ykp_create_config returned NULL\n");
		return 1;
	}

	rc = ykp_free_config(ykp);
	if (!rc)
	{
		printf ("ykp_free_config => %d\n", rc);
		return 1;
	}

	return 0;
}
int main (void)
{
	YKP_CONFIG *ykp;
	int rc;

	if (strcmp (YKPERS_VERSION_STRING, ykpers_check_version (NULL)) != 0)
	{
		printf ("version mismatch %s != %s\n",YKPERS_VERSION_STRING,
			ykpers_check_version (NULL));
		return 1;
	}

	if (ykpers_check_version (YKPERS_VERSION_STRING) == NULL)
	{
		printf ("version NULL?\n");
		return 1;
	}

	if (ykpers_check_version ("99.99.99") != NULL)
	{
		printf ("version not NULL?\n");
		return 1;
	}

	ykp = ykp_alloc ();
	if (!ykp)
	{
		printf ("ykp_alloc returned NULL\n");
		return 1;
	}

	rc = ykp_free_config(ykp);
	if (!rc)
	{
		printf ("ykp_free_config => %d\n", rc);
		return 1;
	}

	return 0;
}
static void _test_non_config_args(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 2, 0);
	int rc = 0;

	const char *infname = NULL;
	const char *outfname = NULL;
	bool verbose = false;
	bool dry_run = false;
	char keylocation = 0;
	bool use_access_code = false;
	unsigned char access_code[256];
	bool autocommit = false;
	int exit_code = 0;
	int i;
	int data_format = YKP_FORMAT_LEGACY;

	/* Options */
	char oathid[128] = {0};
	char ndef[128];
	char ndef_type = 0;
	unsigned char usb_mode = 0;
	unsigned char cr_timeout = 0;
	unsigned short autoeject_timeout = 0;
	int num_modes_seen = 0;
	bool zap = false;

	unsigned char scan_map[sizeof(SCAN_MAP)];

	char *argv[] = {
		"unittest", "-1", "-sout", "-iin", "-c313233343536", "-y", "-v",
		NULL
	};
	int argc = 7;

	ykp_errno = 0;

/* getopt reinit (BSD systems use optreset and a different optind value) */
#if defined(__GLIBC__) || defined(_WIN32)
	optind = 0;
#else
	optind = optreset = 1;
#endif

	/* copy version number from st into cfg */
  ykp_configure_version(cfg, st);
	//assert(ykp_configure_for(cfg, 1, st) == 1);

	/* call args_to_config from ykpers-args.c with a fake set of program arguments */
	rc = args_to_config(argc, argv, cfg, oathid,
			    &infname, &outfname,
			    &data_format, &autocommit,
			    st, &verbose, &dry_run,
			    access_code, &use_access_code,
			    &keylocation, &ndef_type, ndef, &usb_mode, &zap,
			    scan_map, &cr_timeout, &autoeject_timeout, &num_modes_seen,
			    &exit_code);
	assert(rc == 1);
	i = strcmp(infname, "in"); assert(i == 0);
	i = strcmp(outfname, "out"); assert(i == 0);
	i = memcmp(access_code, "123456", 6); assert(i == 0);
	assert(autocommit == true);
	assert(verbose == true);

	ykp_free_config(cfg);
	free(st);
}
void YubiKeyWriter::exportConfig(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;
        }

        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;
        }

        m_filename = QFileDialog::getSaveFileName(NULL, tr("Export File"), m_filename, tr("Yubico cfg format (*.ycfg)"), NULL);

        char data[1024];
        int len = ykp_export_config(cfg, data, 1024, YKP_FORMAT_YCFG);
        if(!len) {
            throw 0;
        }

        QFile file(m_filename);
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
            throw 0;
        }
        if(!file.write(data, len)) {
            throw 0;
        }
        file.close();

        QSettings settings;
        settings.setValue(SG_EXPORT_FILENAME, m_filename);

        emit configWritten(true, NULL);
        emit diagnostics(QString("Exported config to file %1").arg(m_filename));
    }
    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 exported";
        QString errMsg = reportError();
        emit configWritten(false, errMsg);
    }
}
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() << "-------------------------";
}
Example #18
0
int main(int argc, char** argv) {

	char showmessage = 1;
	if((argc == 2) && (strcmp(argv[1], "-y") == 0)) showmessage = 0;
	if(showmessage == 1) {
		puts("--------------------------------------------");
		puts("Hi! You're going to crack the access code of");
		puts("a Yubikey. As soon as the appropriate code  ");
		puts("is found, the AES key will be set to zeros.");
		puts("Brute forcing the code can take a very long ");
		puts("time, and with long I mean like more than a ");
		puts("year.");
		puts("(By the way you can bypass this message by  ");
		puts("passing the -y option to the program.) ");
		puts("--------------------------------------------");
		puts("Type \"start\" to continue.");

		char acknowledge[256];
		fgets(acknowledge, 256, stdin);
		if(strcmp(acknowledge, "start\n") != 0) {
			puts("Quitting.");
			return EXIT_SUCCESS;
		}
	} 

	yk = 0;
	unsigned char access_code[6];
	const char* aeshash="00000000000000000000000000000000";
	YKP_CONFIG *cfg = ykp_create_config();
	YK_STATUS *st = ykds_alloc();

	if(!yk_init()) {
		fputs("Failed to init Yubikey.\n", stderr);
		return EXIT_FAILURE;
	}
	if(!(yk = yk_open_first_key())) {
		fputs("No Yubikey found.\n", stderr);
		return EXIT_FAILURE;
	}
	if(!yk_get_status(yk,st)) {
		fputs("Failed to get status of the Yubikey.\n", stderr);
		return EXIT_FAILURE;
	}

	printf("Found Yubikey. Version: %d.%d.%d Touch level: %d\n",
		ykds_version_major(st),
		ykds_version_minor(st),
		ykds_version_build(st),
		ykds_touch_level(st));

	if(!ykp_configure_for(cfg, 1, st)) {
		printf("Can't set configuration to 1.\n");
		return EXIT_FAILURE;
	}
	if(ykp_AES_key_from_hex(cfg, aeshash)) {
		fputs("Bad AES key. WTF did you do to my source?", stderr);
		return EXIT_FAILURE;
	}

	coreconfig = ykp_core_config(cfg);
	coreconfignum = ykp_config_num(cfg);
	bruteforce(access_code, 5);

	if(st) free(st);
	if(!yk_close_key(yk)) {
		fputs("Can't close Yubikey! What the hell are you doing over there?", stderr);
		return EXIT_FAILURE;
	}
	if(!yk_release()) {
		fputs("Can't release Yubikey.", stderr);
		return EXIT_FAILURE;
	}

	if(cfg) ykp_free_config(cfg);

	return EXIT_SUCCESS;
}
int _test_non_config_args(void)
{
	YKP_CONFIG *cfg = ykp_alloc();
	YK_STATUS *st = _test_init_st(2, 2, 0);
	int rc = 0;

	const char *infname = NULL;
	const char *outfname = NULL;
	bool verbose = false;
	bool aesviahash = false;
	bool use_access_code = false;
	unsigned char access_code[256];
	YK_KEY *yk = 0;
	bool autocommit = false;
	int exit_code = 0;
	int i;

	/* Options */
	char *salt = NULL;
	char ndef[128];
	char ndef_type = NULL;
	unsigned char usb_mode = -1;
	bool zap = false;

	unsigned char scan_map[sizeof(SCAN_MAP)];

	char *argv[] = {
		"unittest", "-1", "-sout", "-iin", "-c313233343536", "-y", "-v",
		NULL
	};
	int argc = 7;

	ykp_errno = 0;

/* getopt reinit (BSD systems use optreset and a different optind value) */
#if defined(__GLIBC__) || defined(_WIN32)
	optind = 0;
#else
	optind = optreset = 1;
#endif

	/* copy version number from st into cfg */
  ykp_configure_version(cfg, st);
	//assert(ykp_configure_for(cfg, 1, st) == 1);

	/* call args_to_config from ykpers-args.c with a fake set of program arguments */
	rc = args_to_config(argc, argv, cfg, yk,
			    &infname, &outfname,
			    &autocommit, salt,
			    st, &verbose,
			    access_code, &use_access_code,
			    &aesviahash, &ndef_type, ndef, &usb_mode, &zap,
			    scan_map, &exit_code);
	assert(rc == 1);
	i = strcmp(infname, "in"); assert(i == 0);
	i = strcmp(outfname, "out"); assert(i == 0);
	i = strcmp(access_code, "123456"); assert(i == 0);
	assert(autocommit == true);
	assert(verbose == true);

	ykp_free_config(cfg);
	free(st);
}