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

📄 crypto_internal.c

📁 IEEE 802.11a/b/g 服务器端AP
💻 C
📖 第 1 页 / 共 2 页
字号:
	default:		break;	}	os_free(ctx);}/* Dummy structures; these are just typecast to struct crypto_rsa_key */struct crypto_public_key;struct crypto_private_key;struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len){	return (struct crypto_public_key *)		crypto_rsa_import_public_key(key, len);}static struct crypto_private_key *crypto_pkcs8_key_import(const u8 *buf, size_t len){	struct asn1_hdr hdr;	const u8 *pos, *end;	struct bignum *zero;	struct asn1_oid oid;	char obuf[80];	/* PKCS #8, Chapter 6 */	/* PrivateKeyInfo ::= SEQUENCE */	if (asn1_get_next(buf, len, &hdr) < 0 ||	    hdr.class != ASN1_CLASS_UNIVERSAL ||	    hdr.tag != ASN1_TAG_SEQUENCE) {		wpa_printf(MSG_DEBUG, "PKCS #8: Does not start with PKCS #8 "			   "header (SEQUENCE); assume PKCS #8 not used");		return NULL;	}	pos = hdr.payload;	end = pos + hdr.length;	/* version Version (Version ::= INTEGER) */	if (asn1_get_next(pos, end - pos, &hdr) < 0 ||	    hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {		wpa_printf(MSG_DEBUG, "PKCS #8: Expected INTEGER - found "			   "class %d tag 0x%x; assume PKCS #8 not used",			   hdr.class, hdr.tag);		return NULL;	}	zero = bignum_init();	if (zero == NULL)		return NULL;	if (bignum_set_unsigned_bin(zero, hdr.payload, hdr.length) < 0) {		wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse INTEGER");		bignum_deinit(zero);		return NULL;	}	pos = hdr.payload + hdr.length;	if (bignum_cmp_d(zero, 0) != 0) {		wpa_printf(MSG_DEBUG, "PKCS #8: Expected zero INTEGER in the "			   "beginning of private key; not found; assume "			   "PKCS #8 not used");		bignum_deinit(zero);		return NULL;	}	bignum_deinit(zero);	/* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier	 * (PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier) */	if (asn1_get_next(pos, len, &hdr) < 0 ||	    hdr.class != ASN1_CLASS_UNIVERSAL ||	    hdr.tag != ASN1_TAG_SEQUENCE) {		wpa_printf(MSG_DEBUG, "PKCS #8: Expected SEQUENCE "			   "(AlgorithmIdentifier) - found class %d tag 0x%x; "			   "assume PKCS #8 not used",			   hdr.class, hdr.tag);		return NULL;	}	if (asn1_get_oid(hdr.payload, hdr.length, &oid, &pos)) {		wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse OID "			   "(algorithm); assume PKCS #8 not used");		return NULL;	}	asn1_oid_to_str(&oid, obuf, sizeof(obuf));	wpa_printf(MSG_DEBUG, "PKCS #8: algorithm=%s", obuf);	if (oid.len != 7 ||	    oid.oid[0] != 1 /* iso */ ||	    oid.oid[1] != 2 /* member-body */ ||	    oid.oid[2] != 840 /* us */ ||	    oid.oid[3] != 113549 /* rsadsi */ ||	    oid.oid[4] != 1 /* pkcs */ ||	    oid.oid[5] != 1 /* pkcs-1 */ ||	    oid.oid[6] != 1 /* rsaEncryption */) {		wpa_printf(MSG_DEBUG, "PKCS #8: Unsupported private key "			   "algorithm %s", obuf);		return NULL;	}	pos = hdr.payload + hdr.length;	/* privateKey PrivateKey (PrivateKey ::= OCTET STRING) */	if (asn1_get_next(pos, end - pos, &hdr) < 0 ||	    hdr.class != ASN1_CLASS_UNIVERSAL ||	    hdr.tag != ASN1_TAG_OCTETSTRING) {		wpa_printf(MSG_DEBUG, "PKCS #8: Expected OCTETSTRING "			   "(privateKey) - found class %d tag 0x%x",			   hdr.class, hdr.tag);		return NULL;	}	wpa_printf(MSG_DEBUG, "PKCS #8: Try to parse RSAPrivateKey");	return (struct crypto_private_key *)		crypto_rsa_import_private_key(hdr.payload, hdr.length);}struct crypto_private_key * crypto_private_key_import(const u8 *key,						      size_t len){	struct crypto_private_key *res;	/* First, check for possible PKCS #8 encoding */	res = crypto_pkcs8_key_import(key, len);	if (res)		return res;	/* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */	wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "		   "key");	return (struct crypto_private_key *)		crypto_rsa_import_private_key(key, len);}struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,						       size_t len){	/* No X.509 support in crypto_internal.c */	return NULL;}static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,					   const u8 *in, size_t inlen,					   u8 *out, size_t *outlen){	size_t ps_len;	u8 *pos;	/*	 * PKCS #1 v1.5, 8.1:	 *	 * EB = 00 || BT || PS || 00 || D	 * BT = 00 or 01 for private-key operation; 02 for public-key operation	 * PS = k-3-||D||; at least eight octets	 * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)	 * k = length of modulus in octets (modlen)	 */	if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {		wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "			   "lengths (modlen=%lu outlen=%lu inlen=%lu)",			   __func__, (unsigned long) modlen,			   (unsigned long) *outlen,			   (unsigned long) inlen);		return -1;	}	pos = out;	*pos++ = 0x00;	*pos++ = block_type; /* BT */	ps_len = modlen - inlen - 3;	switch (block_type) {	case 0:		os_memset(pos, 0x00, ps_len);		pos += ps_len;		break;	case 1:		os_memset(pos, 0xff, ps_len);		pos += ps_len;		break;	case 2:		if (os_get_random(pos, ps_len) < 0) {			wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "				   "random data for PS", __func__);			return -1;		}		while (ps_len--) {			if (*pos == 0x00)				*pos = 0x01;			pos++;		}		break;	default:		wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "			   "%d", __func__, block_type);		return -1;	}	*pos++ = 0x00;	os_memcpy(pos, in, inlen); /* D */	return 0;}static int crypto_rsa_encrypt_pkcs1(int block_type, struct crypto_rsa_key *key,				    int use_private,				    const u8 *in, size_t inlen,				    u8 *out, size_t *outlen){	size_t modlen;	modlen = crypto_rsa_get_modulus_len(key);	if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,					    out, outlen) < 0)		return -1;	return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);}int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,					const u8 *in, size_t inlen,					u8 *out, size_t *outlen){	return crypto_rsa_encrypt_pkcs1(2, (struct crypto_rsa_key *) key,					0, in, inlen, out, outlen);}int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,					 const u8 *in, size_t inlen,					 u8 *out, size_t *outlen){	struct crypto_rsa_key *rkey = (struct crypto_rsa_key *) key;	int res;	u8 *pos, *end;	res = crypto_rsa_exptmod(in, inlen, out, outlen, rkey, 1);	if (res)		return res;	if (*outlen < 2 || out[0] != 0 || out[1] != 2)		return -1;	/* Skip PS (pseudorandom non-zero octets) */	pos = out + 2;	end = out + *outlen;	while (*pos && pos < end)		pos++;	if (pos == end)		return -1;	pos++;	*outlen -= pos - out;	/* Strip PKCS #1 header */	os_memmove(out, pos, *outlen);	return 0;}int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,				  const u8 *in, size_t inlen,				  u8 *out, size_t *outlen){	return crypto_rsa_encrypt_pkcs1(1, (struct crypto_rsa_key *) key,					1, in, inlen, out, outlen);}void crypto_public_key_free(struct crypto_public_key *key){	crypto_rsa_free((struct crypto_rsa_key *) key);}void crypto_private_key_free(struct crypto_private_key *key){	crypto_rsa_free((struct crypto_rsa_key *) key);}int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,				    const u8 *crypt, size_t crypt_len,				    u8 *plain, size_t *plain_len){	size_t len;	u8 *pos;	len = *plain_len;	if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len,			       (struct crypto_rsa_key *) key, 0) < 0)		return -1;	/*	 * PKCS #1 v1.5, 8.1:	 *	 * EB = 00 || BT || PS || 00 || D	 * BT = 00 or 01	 * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01)	 * k = length of modulus in octets	 */	if (len < 3 + 8 + 16 /* min hash len */ ||	    plain[0] != 0x00 || (plain[1] != 0x00 && plain[1] != 0x01)) {		wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "			   "structure");		return -1;	}	pos = plain + 3;	if (plain[1] == 0x00) {		/* BT = 00 */		if (plain[2] != 0x00) {			wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "				   "PS (BT=00)");			return -1;		}		while (pos + 1 < plain + len && *pos == 0x00 && pos[1] == 0x00)			pos++;	} else {		/* BT = 01 */		if (plain[2] != 0xff) {			wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "				   "PS (BT=01)");			return -1;		}		while (pos < plain + len && *pos == 0xff)			pos++;	}	if (pos - plain - 2 < 8) {		/* PKCS #1 v1.5, 8.1: At least eight octets long PS */		wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "			   "padding");		return -1;	}	if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {		wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "			   "structure (2)");		return -1;	}	pos++;	len -= pos - plain;	/* Strip PKCS #1 header */	os_memmove(plain, pos, len);	*plain_len = len;	return 0;}int crypto_global_init(void){	return 0;}void crypto_global_deinit(void){}#ifdef EAP_FASTint crypto_mod_exp(const u8 *base, size_t base_len,		   const u8 *power, size_t power_len,		   const u8 *modulus, size_t modulus_len,		   u8 *result, size_t *result_len){	struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result;	int ret = -1;	bn_base = bignum_init();	bn_exp = bignum_init();	bn_modulus = bignum_init();	bn_result = bignum_init();	if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||	    bn_result == NULL)		goto error;	if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 ||	    bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 ||	    bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0)		goto error;	if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0)		goto error;	ret = bignum_get_unsigned_bin(bn_result, result, result_len);error:	bignum_deinit(bn_base);	bignum_deinit(bn_exp);	bignum_deinit(bn_modulus);	bignum_deinit(bn_result);	return ret;}#endif /* EAP_FAST */#endif /* CONFIG_TLS_INTERNAL */#endif /* EAP_TLS_FUNCS */

⌨️ 快捷键说明

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