コード例 #1
0
ファイル: pkcs15-cache.c プロジェクト: BradPID/OpenSC
static int generate_cache_filename(struct sc_pkcs15_card *p15card,
				   const sc_path_t *path,
				   char *buf, size_t bufsize)
{
	char dir[PATH_MAX];
        char pathname[SC_MAX_PATH_SIZE*2+1];
	int  r;
        const u8 *pathptr;
        size_t i, pathlen;

	if (path->type != SC_PATH_TYPE_PATH)
                return SC_ERROR_INVALID_ARGUMENTS;
	assert(path->len <= SC_MAX_PATH_SIZE);
	r = sc_get_cache_dir(p15card->card->ctx, dir, sizeof(dir));
	if (r)
		return r;
	pathptr = path->value;
	pathlen = path->len;
	if (pathlen > 2 && memcmp(pathptr, "\x3F\x00", 2) == 0) {
                pathptr += 2;
		pathlen -= 2;
	}
	for (i = 0; i < pathlen; i++)
		sprintf(pathname + 2*i, "%02X", pathptr[i]);
	if (p15card->tokeninfo->serial_number != NULL) {
		char *last_update = sc_pkcs15_get_lastupdate(p15card);
		if (last_update != NULL)
			r = snprintf(buf, bufsize, "%s/%s_%s_%s", dir, p15card->tokeninfo->serial_number,
					last_update, pathname);
		else
			r = snprintf(buf, bufsize, "%s/%s_DATE_%s", dir,
					p15card->tokeninfo->serial_number, pathname);
		if (r < 0)
			return SC_ERROR_BUFFER_TOO_SMALL;
	} else
		return SC_ERROR_INVALID_ARGUMENTS;
        return SC_SUCCESS;
}
コード例 #2
0
ファイル: pkcs15-cache.c プロジェクト: PhoenixBaymax/OpenSC
static int generate_cache_filename(struct sc_pkcs15_card *p15card,
				   const sc_path_t *path,
				   char *buf, size_t bufsize)
{
	char dir[PATH_MAX];
	char *last_update = NULL;
	int  r;
	unsigned u;

	if (p15card->tokeninfo->serial_number == NULL
			&& (p15card->card->uid.len == 0
				|| p15card->card->uid.value[0] == RANDOM_UID_INDICATOR))
		return SC_ERROR_INVALID_ARGUMENTS;

	assert(path->len <= SC_MAX_PATH_SIZE);
	r = sc_get_cache_dir(p15card->card->ctx, dir, sizeof(dir));
	if (r)
		return r;
	snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "/");

	last_update = sc_pkcs15_get_lastupdate(p15card);
	if (!last_update)
		last_update = "NODATE";

	if (p15card->tokeninfo->serial_number) {
		snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir),
				"%s_%s", p15card->tokeninfo->serial_number,
				last_update);
	} else {
		snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir),
				"uid-%s_%s", sc_dump_hex(
					p15card->card->uid.value,
					p15card->card->uid.len), last_update);
	}

	if (path->aid.len &&
		(path->type == SC_PATH_TYPE_FILE_ID || path->type == SC_PATH_TYPE_PATH))   {
		snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "_");
		for (u = 0; u < path->aid.len; u++)
			snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir),
					"%02X",  path->aid.value[u]);
	}
	else if (path->type != SC_PATH_TYPE_PATH)  {
		return SC_ERROR_INVALID_ARGUMENTS;
	}

	if (path->len)   {
		size_t offs = 0;

		if (path->len > 2 && memcmp(path->value, "\x3F\x00", 2) == 0)
			offs = 2;
		snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "_");
		for (u = 0; u < path->len - offs; u++)
			snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir),
					"%02X",  path->value[u + offs]);
	}

	if (!buf || bufsize < strlen(dir))
		return SC_ERROR_BUFFER_TOO_SMALL;
	strcpy(buf, dir);

	return SC_SUCCESS;
}