⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pkcs11-tool.c

📁 读写Smart卡加解密接口的程序
💻 C
📖 第 1 页 / 共 5 页
字号:
			info.cryptokiVersion.major,			info.cryptokiVersion.minor);	printf("Manufacturer     %s\n",			p11_utf8_to_local(info.manufacturerID,				sizeof(info.manufacturerID)));	printf("Library          %s (ver %u.%u)\n",			p11_utf8_to_local(info.libraryDescription,				sizeof(info.libraryDescription)),			info.libraryVersion.major,			info.libraryVersion.minor);}voidlist_slots(void){	CK_SLOT_INFO	info;	CK_ULONG	n;	CK_RV		rv;	if (!p11_num_slots) {		printf("No slots found\n");		return;	}	printf("Available slots:\n");	for (n = 0; n < p11_num_slots; n++) {		printf("Slot %-2u          ", (unsigned int) p11_slots[n]);		rv = p11->C_GetSlotInfo(p11_slots[n], &info);		if (rv != CKR_OK) {			printf("(GetSlotInfo failed, error %u)\n", (unsigned int) rv);			continue;		}		if (opt_quiet && !(info.flags & CKF_TOKEN_PRESENT)) {			printf("(empty)\n");			continue;		}		printf("%s\n", p11_utf8_to_local(info.slotDescription,					sizeof(info.slotDescription)));		if (!opt_quiet) {			printf("  manufacturer:  %s\n", p11_utf8_to_local(info.manufacturerID,						sizeof(info.manufacturerID)));			printf("  hardware ver:  %u.%u\n",						info.hardwareVersion.major,						info.hardwareVersion.minor);			printf("  firmware ver:  %u.%u\n",						info.firmwareVersion.major,						info.firmwareVersion.minor);			printf("  flags:         %s\n", p11_slot_info_flags(info.flags));		}		if (info.flags & CKF_TOKEN_PRESENT)			show_token(p11_slots[n]);	}}voidshow_token(CK_SLOT_ID slot){	CK_TOKEN_INFO	info;	get_token_info(slot, &info);	if (!(info.flags & CKF_TOKEN_INITIALIZED) && opt_quiet) {		printf("  token state:   uninitialized\n");		return;	}	printf("  token label:   %s\n",			p11_utf8_to_local(info.label,				sizeof(info.label)));	printf("  token manuf:   %s\n",			p11_utf8_to_local(info.manufacturerID,				sizeof(info.manufacturerID)));	printf("  token model:   %s\n",			p11_utf8_to_local(info.model,				sizeof(info.model)));	printf("  token flags:   %s\n",			p11_token_info_flags(info.flags));}voidlist_mechs(CK_SLOT_ID slot){	CK_MECHANISM_TYPE	*mechs = NULL;	CK_ULONG		n, num_mechs = 0;	CK_RV			rv;	get_mechanisms(slot, &mechs, &num_mechs);	printf("Supported mechanisms:\n");	for (n = 0; n < num_mechs; n++) {		CK_MECHANISM_INFO info;		printf("  %s", p11_mechanism_to_name(mechs[n]));		rv = p11->C_GetMechanismInfo(slot, mechs[n], &info);		if (rv == CKR_OK) {			if (info.flags & CKF_DIGEST)				printf(", digest");			if (info.flags & CKF_SIGN)				printf(", sign");			if (info.flags & CKF_VERIFY)				printf(", verify");			if (info.flags & CKF_WRAP)				printf(", wrap");			if (info.flags & CKF_UNWRAP)				printf(", unwrap");			if (info.flags & CKF_ENCRYPT)				printf(", encrypt");			if (info.flags & CKF_DECRYPT)				printf(", decrypt");			if (info.flags & CKF_GENERATE_KEY_PAIR)				printf(", keypairgen");			info.flags &= ~(CKF_DIGEST|CKF_SIGN|CKF_VERIFY|CKF_HW|CKF_UNWRAP|CKF_ENCRYPT|CKF_DECRYPT|CKF_GENERATE_KEY_PAIR);			if (info.flags)				printf(", other flags=0x%x", (unsigned int) info.flags);		}		printf("\n");	}}voidlist_objects(CK_SESSION_HANDLE sess){	CK_OBJECT_HANDLE object;	CK_ULONG count;	CK_RV rv;	rv = p11->C_FindObjectsInit(sess, NULL, 0);	if (rv != CKR_OK)		p11_fatal("C_FindObjectsInit", rv);	while (1) {		rv = p11->C_FindObjects(sess, &object, 1, &count);		if (rv != CKR_OK)			p11_fatal("C_FindObjects", rv);		if (count == 0)			break;		show_object(sess, object);	}	p11->C_FindObjectsFinal(sess);}intchange_pin(CK_SLOT_ID slot, CK_SESSION_HANDLE sess){	char old_buf[21], *old_pin = NULL;	char new_buf[21], *new_pin = NULL;	CK_TOKEN_INFO	info;	CK_RV rv;	get_token_info(slot, &info);	if (!(info.flags & CKF_PROTECTED_AUTHENTICATION_PATH)) {		old_pin = getpass("Please enter the current PIN: ");		if (!old_pin || !*old_pin || strlen(old_pin) > 20)			return 1;		strcpy(old_buf, old_pin);		old_pin = old_buf;		new_pin = getpass("Please enter the new PIN: ");		if (!new_pin || !*new_pin || strlen(new_pin) > 20)			return 1;		strcpy(new_buf, new_pin);		new_pin = getpass("Please enter the new PIN again: ");		if (!new_pin || !*new_pin || strcmp(new_buf, new_pin) != 0) {			printf("  different new PINs, exiting\n");			return -1;		}	}	rv = p11->C_SetPIN(sess,		(CK_UTF8CHAR *) old_pin, old_pin == NULL ? 0 : strlen(old_pin),		(CK_UTF8CHAR *) new_pin, new_pin == NULL ? 0 : strlen(new_pin));	if (rv != CKR_OK)		p11_fatal("C_SetPIN", rv);	printf("PIN successfully changed\n");	return 0;}voidsign_data(CK_SLOT_ID slot, CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key){	unsigned char	buffer[512];	CK_MECHANISM	mech;	CK_RV		rv;	CK_ULONG	sig_len;	int		fd, r;	if (opt_mechanism == NO_MECHANISM) {		opt_mechanism = find_mechanism(slot, CKF_SIGN|CKF_HW, 1);		printf("Using signature algorithm %s\n",				p11_mechanism_to_name(opt_mechanism));	}	memset(&mech, 0, sizeof(mech));	mech.mechanism = opt_mechanism;	rv = p11->C_SignInit(session, &mech, key);	if (rv != CKR_OK)		p11_fatal("C_SignInit", rv);	if (opt_input == NULL)		fd = 0;	else if ((fd = open(opt_input, O_RDONLY|O_BINARY)) < 0)		fatal("Cannot open %s: %m", opt_input);	while ((r = read(fd, buffer, sizeof(buffer))) > 0) {		rv = p11->C_SignUpdate(session, buffer, r);		if (rv != CKR_OK)			p11_fatal("C_SignUpdate", rv);	}	if (rv < 0)		fatal("failed to read from %s: %m",				opt_input? opt_input : "<stdin>");	if (fd != 0)		close(fd);	sig_len = sizeof(buffer);	rv = p11->C_SignFinal(session, buffer, &sig_len);	if (rv != CKR_OK)		p11_fatal("C_SignFinal", rv);	if (opt_output == NULL)		fd = 1;	else if ((fd = open(opt_output, O_CREAT|O_TRUNC|O_WRONLY, 0666)) < 0)		fatal("failed to open %s: %m", opt_output);	r = write(fd, buffer, sig_len);	if (r < 0)		fatal("Failed to write to %s: %m", opt_output);	if (fd != 1)		close(fd);}voidhash_data(CK_SLOT_ID slot, CK_SESSION_HANDLE session){	unsigned char	buffer[64];	CK_MECHANISM	mech;	CK_RV		rv;	CK_ULONG	hash_len;	int		fd, r;	if (opt_mechanism == NO_MECHANISM) {		opt_mechanism = find_mechanism(slot, CKF_DIGEST, 1);		printf("Using digest algorithm %s\n",				p11_mechanism_to_name(opt_mechanism));	}	memset(&mech, 0, sizeof(mech));	mech.mechanism = opt_mechanism;	rv = p11->C_DigestInit(session, &mech);	if (rv != CKR_OK)		p11_fatal("C_DigestInit", rv);	if (opt_input == NULL)		fd = 0;	else if ((fd = open(opt_input, O_RDONLY)) < 0)		fatal("Cannot open %s: %m", opt_input);	while ((r = read(fd, buffer, sizeof(buffer))) > 0) {		rv = p11->C_DigestUpdate(session, buffer, r);		if (rv != CKR_OK)			p11_fatal("C_DigestUpdate", rv);	}	if (rv < 0)		fatal("failed to read from %s: %m",				opt_input? opt_input : "<stdin>");	if (fd != 0)		close(fd);	hash_len = sizeof(buffer);	rv = p11->C_DigestFinal(session, buffer, &hash_len);	if (rv != CKR_OK)		p11_fatal("C_DigestFinal", rv);	if (opt_output == NULL)		fd = 1;	else if ((fd = open(opt_output, O_CREAT|O_TRUNC|O_WRONLY, 0666)) < 0)		fatal("failed to open %s: %m", opt_output);	r = write(fd, buffer, hash_len);	if (r < 0)		fatal("Failed to write to %s: %m", opt_output);	if (fd != 1)		close(fd);}#define FILL_ATTR(attr, typ, val, len) {(attr).type=(typ); (attr).pValue=(val); (attr).ulValueLen=len;}intgen_keypair(CK_SLOT_ID slot, CK_SESSION_HANDLE session,	CK_OBJECT_HANDLE *hPublicKey, CK_OBJECT_HANDLE *hPrivateKey){	CK_MECHANISM mechanism = {CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0};	CK_ULONG modulusBits = 768;	CK_BYTE publicExponent[] = { 3 };	CK_BBOOL _true = TRUE;	CK_OBJECT_CLASS pubkey_class = CKO_PUBLIC_KEY;	CK_OBJECT_CLASS privkey_class = CKO_PRIVATE_KEY;	CK_ATTRIBUTE publicKeyTemplate[20] = {		{CKA_CLASS, &pubkey_class, sizeof(pubkey_class)},		{CKA_ENCRYPT, &_true, sizeof(_true)},		{CKA_VERIFY, &_true, sizeof(_true)},		{CKA_WRAP, &_true, sizeof(_true)},		{CKA_MODULUS_BITS, &modulusBits, sizeof(modulusBits)},		{CKA_PUBLIC_EXPONENT, publicExponent, sizeof(publicExponent)}	};	int n_pubkey_attr = 6;	CK_ATTRIBUTE privateKeyTemplate[20] = {		{CKA_CLASS, &privkey_class, sizeof(privkey_class)},		{CKA_TOKEN, &_true, sizeof(_true)},		{CKA_PRIVATE, &_true, sizeof(_true)},		{CKA_SENSITIVE, &_true, sizeof(_true)},		{CKA_DECRYPT, &_true, sizeof(_true)},		{CKA_SIGN, &_true, sizeof(_true)},		{CKA_UNWRAP, &_true, sizeof(_true)}	};	int n_privkey_attr = 7;	CK_RV rv;	if (opt_object_label != NULL) {		FILL_ATTR(publicKeyTemplate[n_pubkey_attr], CKA_LABEL,			opt_object_label, strlen(opt_object_label));		FILL_ATTR(privateKeyTemplate[n_privkey_attr], CKA_LABEL,			opt_object_label, strlen(opt_object_label));		n_pubkey_attr++;		n_privkey_attr++;			}	if (opt_object_id_len != 0) {		FILL_ATTR(publicKeyTemplate[n_pubkey_attr], CKA_ID,			opt_object_id, opt_object_id_len);		FILL_ATTR(privateKeyTemplate[n_privkey_attr], CKA_ID,			opt_object_id, opt_object_id_len);		n_pubkey_attr++;		n_privkey_attr++;	}	rv = p11->C_GenerateKeyPair(session, &mechanism,		publicKeyTemplate, n_pubkey_attr,		privateKeyTemplate, n_privkey_attr,		hPublicKey, hPrivateKey);	if (rv != CKR_OK)		p11_fatal("C_GenerateKeyPair", rv);	printf("Key pair generated:\n");	show_object(session, *hPrivateKey);	show_object(session, *hPublicKey);	return 1;}/* Currently only for certificates (-type cert) */intwrite_object(CK_SLOT_ID slot, CK_SESSION_HANDLE session){	CK_BBOOL _true = TRUE;	unsigned char contents[5000];	int contents_len;	FILE *f;	CK_OBJECT_HANDLE cert_obj, pubkey_obj, privkey_obj;	CK_ATTRIBUTE cert_templ[20], pubkey_templ[20], privkey_templ[20];	int n_cert_attr = 0, n_pubkey_attr = 0, n_privkey_attr = 0;	CK_RV rv;	f = fopen(opt_file_to_write, "rb");	if (f == NULL)		fatal("Couldn't open file \"%s\"\n", opt_file_to_write);	contents_len = fread(contents, 1, sizeof(contents), f);	if (contents_len < 0)		fatal("Couldn't read from file \"%s\"\n", opt_file_to_write);	fclose(f);	if (opt_object_class == CKO_CERTIFICATE) {		CK_OBJECT_CLASS clazz = CKO_CERTIFICATE;		CK_CERTIFICATE_TYPE cert_type = CKC_X_509;		FILL_ATTR(cert_templ[0], CKA_TOKEN, &_true, sizeof(_true));		FILL_ATTR(cert_templ[1], CKA_VALUE, contents, contents_len);		FILL_ATTR(cert_templ[2], CKA_CLASS, &clazz, sizeof(clazz));		FILL_ATTR(cert_templ[3], CKA_CERTIFICATE_TYPE, &cert_type, sizeof(cert_type));		n_cert_attr = 4;		if (opt_object_label != NULL) {			FILL_ATTR(cert_templ[n_cert_attr], CKA_LABEL,				opt_object_label, strlen(opt_object_label));			n_cert_attr++;		}		if (opt_object_id_len != 0) {			FILL_ATTR(cert_templ[n_cert_attr], CKA_ID,				opt_object_id, opt_object_id_len);			n_cert_attr++;		}	}	else		fatal("Writing of a \"%s\" type not (yet) supported\n", opt_object_class_str);	if (n_cert_attr) {		rv = p11->C_CreateObject(session, cert_templ, n_cert_attr, &cert_obj);		if (rv != CKR_OK)			p11_fatal("C_CreateObject", rv);				printf("Generated certificate:\n");		show_object(session, cert_obj);	}	if (n_pubkey_attr) {		rv = p11->C_CreateObject(session, pubkey_templ, n_pubkey_attr, &pubkey_obj);		if (rv != CKR_OK)			p11_fatal("C_CreateObject", rv);				printf("Generated public key:\n");		show_object(session, pubkey_obj);	}	if (n_privkey_attr) {		rv = p11->C_CreateObject(session, privkey_templ, n_privkey_attr, &privkey_obj);		if (rv != CKR_OK)			p11_fatal("C_CreateObject", rv);				printf("Generated private key:\n");		show_object(session, privkey_obj);	}	return 1;}voidset_id_attr(CK_SLOT_ID slot, CK_SESSION_HANDLE session){	CK_OBJECT_HANDLE obj;	CK_ATTRIBUTE templ[] = {{CKA_ID, new_object_id, new_object_id_len}};	CK_RV rv;	if (!find_object(session, opt_object_class, &obj, opt_object_id, opt_object_id_len, 0)) {		printf("set_id(): coudn't find the object\n");		return;	}	rv = p11->C_SetAttributeValue(session, obj, templ, 1);	if (rv != CKR_OK)		p11_fatal("C_SetAttributeValue", rv);	printf("Result:");	show_object(session, obj);}CK_SLOT_IDfind_slot_by_label(const char *label){	CK_TOKEN_INFO	info;	CK_ULONG	n, len;	CK_RV		rv;	if (!p11_num_slots)		return NO_SLOT;	len = strlen(label);	for (n = 0; n < p11_num_slots; n++) {		const char	*token_label;		rv = p11->C_GetTokenInfo(n, &info);		if (rv != CKR_OK)			continue;		token_label = p11_utf8_to_local(info.label, sizeof(info.label));		if (!strncmp(label, token_label, len))			return n;	}	return NO_SLOT;}intfind_object(CK_SESSION_HANDLE sess, CK_OBJECT_CLASS cls,		CK_OBJECT_HANDLE_PTR ret,		const unsigned char *id, size_t id_len, int obj_index){	CK_ATTRIBUTE attrs[2];	unsigned int nattrs = 0;	CK_ULONG count;	CK_RV rv;	int i;	attrs[0].type = CKA_CLASS;	attrs[0].pValue = &cls;	attrs[0].ulValueLen = sizeof(cls);	nattrs++;	if (id) {               attrs[nattrs].type = CKA_ID;               attrs[nattrs].pValue = (void *) id;               attrs[nattrs].ulValueLen = id_len;               nattrs++;	}	rv = p11->C_FindObjectsInit(sess, attrs, nattrs);	if (rv != CKR_OK)		p11_fatal("C_FindObjectsInit", rv);	for (i = 0; i < obj_index; i++) {		rv = p11->C_FindObjects(sess, ret, 1, &count);		if (rv != CKR_OK)			p11_fatal("C_FindObjects", rv);		if (count == 0)			goto done;	}	rv = p11->C_FindObjects(sess, ret, 1, &count);	if (rv != CKR_OK)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -