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

📄 tlsv1_common.c

📁 WLAN无线网络管理的最新程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * wpa_supplicant/hostapd: TLSv1 common routines * Copyright (c) 2006, Jouni Malinen <j@w1.fi> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Alternatively, this software may be distributed under the terms of BSD * license. * * See README and COPYING for more details. */#include "includes.h"#include "common.h"#include "md5.h"#include "sha1.h"#include "crypto.h"#include "x509v3.h"#include "tlsv1_common.h"/* * TODO: * RFC 2246 Section 9: Mandatory to implement TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA * Add support for commonly used cipher suites; don't bother with exportable * suites. */ static const struct tls_cipher_suite tls_cipher_suites[] = {	{ TLS_NULL_WITH_NULL_NULL, TLS_KEY_X_NULL, TLS_CIPHER_NULL,	  TLS_HASH_NULL },	{ TLS_RSA_WITH_RC4_128_MD5, TLS_KEY_X_RSA, TLS_CIPHER_RC4_128,	  TLS_HASH_MD5 },	{ TLS_RSA_WITH_RC4_128_SHA, TLS_KEY_X_RSA, TLS_CIPHER_RC4_128,	  TLS_HASH_SHA },	{ TLS_RSA_WITH_DES_CBC_SHA, TLS_KEY_X_RSA, TLS_CIPHER_DES_CBC,	  TLS_HASH_SHA },	{ TLS_RSA_WITH_3DES_EDE_CBC_SHA, TLS_KEY_X_RSA,	  TLS_CIPHER_3DES_EDE_CBC, TLS_HASH_SHA }, 	{ TLS_DH_anon_WITH_RC4_128_MD5, TLS_KEY_X_DH_anon,	  TLS_CIPHER_RC4_128, TLS_HASH_MD5 }, 	{ TLS_DH_anon_WITH_DES_CBC_SHA, TLS_KEY_X_DH_anon,	  TLS_CIPHER_DES_CBC, TLS_HASH_SHA }, 	{ TLS_DH_anon_WITH_3DES_EDE_CBC_SHA, TLS_KEY_X_DH_anon,	  TLS_CIPHER_3DES_EDE_CBC, TLS_HASH_SHA },	{ TLS_RSA_WITH_AES_128_CBC_SHA, TLS_KEY_X_RSA, TLS_CIPHER_AES_128_CBC,	  TLS_HASH_SHA },	{ TLS_DH_anon_WITH_AES_128_CBC_SHA, TLS_KEY_X_DH_anon,	  TLS_CIPHER_AES_128_CBC, TLS_HASH_SHA },	{ TLS_RSA_WITH_AES_256_CBC_SHA, TLS_KEY_X_RSA, TLS_CIPHER_AES_256_CBC,	  TLS_HASH_SHA },	{ TLS_DH_anon_WITH_AES_256_CBC_SHA, TLS_KEY_X_DH_anon,	  TLS_CIPHER_AES_256_CBC, TLS_HASH_SHA }};#define NUM_ELEMS(a) (sizeof(a) / sizeof((a)[0]))#define NUM_TLS_CIPHER_SUITES NUM_ELEMS(tls_cipher_suites)static const struct tls_cipher_data tls_ciphers[] = {	{ TLS_CIPHER_NULL,         TLS_CIPHER_STREAM,  0,  0,  0,	  CRYPTO_CIPHER_NULL },	{ TLS_CIPHER_IDEA_CBC,     TLS_CIPHER_BLOCK,  16, 16,  8,	  CRYPTO_CIPHER_NULL },	{ TLS_CIPHER_RC2_CBC_40,   TLS_CIPHER_BLOCK,   5, 16,  0,	  CRYPTO_CIPHER_ALG_RC2 },	{ TLS_CIPHER_RC4_40,       TLS_CIPHER_STREAM,  5, 16,  0,	  CRYPTO_CIPHER_ALG_RC4 },	{ TLS_CIPHER_RC4_128,      TLS_CIPHER_STREAM, 16, 16,  0,	  CRYPTO_CIPHER_ALG_RC4 },	{ TLS_CIPHER_DES40_CBC,    TLS_CIPHER_BLOCK,   5,  8,  8,	  CRYPTO_CIPHER_ALG_DES },	{ TLS_CIPHER_DES_CBC,      TLS_CIPHER_BLOCK,   8,  8,  8,	  CRYPTO_CIPHER_ALG_DES },	{ TLS_CIPHER_3DES_EDE_CBC, TLS_CIPHER_BLOCK,  24, 24,  8,	  CRYPTO_CIPHER_ALG_3DES },	{ TLS_CIPHER_AES_128_CBC,  TLS_CIPHER_BLOCK,  16, 16, 16,	  CRYPTO_CIPHER_ALG_AES },	{ TLS_CIPHER_AES_256_CBC,  TLS_CIPHER_BLOCK,  32, 32, 16,	  CRYPTO_CIPHER_ALG_AES }};#define NUM_TLS_CIPHER_DATA NUM_ELEMS(tls_ciphers)/** * tls_get_cipher_suite - Get TLS cipher suite * @suite: Cipher suite identifier * Returns: Pointer to the cipher data or %NULL if not found */const struct tls_cipher_suite * tls_get_cipher_suite(u16 suite){	size_t i;	for (i = 0; i < NUM_TLS_CIPHER_SUITES; i++)		if (tls_cipher_suites[i].suite == suite)			return &tls_cipher_suites[i];	return NULL;}static const struct tls_cipher_data * tls_get_cipher_data(tls_cipher cipher){	size_t i;	for (i = 0; i < NUM_TLS_CIPHER_DATA; i++)		if (tls_ciphers[i].cipher == cipher)			return &tls_ciphers[i];	return NULL;}/** * tls_parse_cert - Parse DER encoded X.509 certificate and get public key * @buf: ASN.1 DER encoded certificate * @len: Length of the buffer * @pk: Buffer for returning the allocated public key * Returns: 0 on success, -1 on failure * * This functions parses an ASN.1 DER encoded X.509 certificate and retrieves * the public key from it. The caller is responsible for freeing the public key * by calling crypto_public_key_free(). */int tls_parse_cert(const u8 *buf, size_t len, struct crypto_public_key **pk){	struct x509_certificate *cert;	wpa_hexdump(MSG_MSGDUMP, "TLSv1: Parse ASN.1 DER certificate",		    buf, len);	*pk = crypto_public_key_from_cert(buf, len);	if (*pk)		return 0;	cert = x509_certificate_parse(buf, len);	if (cert == NULL) {		wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse X.509 "			   "certificate");		return -1;	}	/* TODO	 * verify key usage (must allow encryption)	 *	 * All certificate profiles, key and cryptographic formats are	 * defined by the IETF PKIX working group [PKIX]. When a key	 * usage extension is present, the digitalSignature bit must be	 * set for the key to be eligible for signing, as described	 * above, and the keyEncipherment bit must be present to allow	 * encryption, as described above. The keyAgreement bit must be	 * set on Diffie-Hellman certificates. (PKIX: RFC 3280)	 */	*pk = crypto_public_key_import(cert->public_key, cert->public_key_len);	x509_certificate_free(cert);	if (*pk == NULL) {		wpa_printf(MSG_ERROR, "TLSv1: Failed to import "			   "server public key");		return -1;	}	return 0;}/** * tlsv1_record_set_cipher_suite - TLS record layer: Set cipher suite * @rl: Pointer to TLS record layer data * @cipher_suite: New cipher suite * Returns: 0 on success, -1 on failure * * This function is used to prepare TLS record layer for cipher suite change. * tlsv1_record_change_write_cipher() and * tlsv1_record_change_read_cipher() functions can then be used to change the * currently used ciphers. */int tlsv1_record_set_cipher_suite(struct tlsv1_record_layer *rl,				  u16 cipher_suite){	const struct tls_cipher_suite *suite;	const struct tls_cipher_data *data;	wpa_printf(MSG_DEBUG, "TLSv1: Selected cipher suite: 0x%04x",		   cipher_suite);	rl->cipher_suite = cipher_suite;	suite = tls_get_cipher_suite(cipher_suite);	if (suite == NULL)		return -1;	if (suite->hash == TLS_HASH_MD5) {		rl->hash_alg = CRYPTO_HASH_ALG_HMAC_MD5;		rl->hash_size = MD5_MAC_LEN;	} else if (suite->hash == TLS_HASH_SHA) {		rl->hash_alg = CRYPTO_HASH_ALG_HMAC_SHA1;		rl->hash_size = SHA1_MAC_LEN;	}	data = tls_get_cipher_data(suite->cipher);	if (data == NULL)		return -1;	rl->key_material_len = data->key_material;	rl->iv_size = data->block_size;	rl->cipher_alg = data->alg;	return 0;}/** * tlsv1_record_change_write_cipher - TLS record layer: Change write cipher * @rl: Pointer to TLS record layer data * Returns: 0 on success (cipher changed), -1 on failure * * This function changes TLS record layer to use the new cipher suite * configured with tlsv1_record_set_cipher_suite() for writing. */int tlsv1_record_change_write_cipher(struct tlsv1_record_layer *rl){	wpa_printf(MSG_DEBUG, "TLSv1: Record Layer - New write cipher suite "		   "0x%04x", rl->cipher_suite);	rl->write_cipher_suite = rl->cipher_suite;	os_memset(rl->write_seq_num, 0, TLS_SEQ_NUM_LEN);	if (rl->write_cbc) {		crypto_cipher_deinit(rl->write_cbc);		rl->write_cbc = NULL;	}	if (rl->cipher_alg != CRYPTO_CIPHER_NULL) {		rl->write_cbc = crypto_cipher_init(rl->cipher_alg,						   rl->write_iv, rl->write_key,						   rl->key_material_len);		if (rl->write_cbc == NULL) {			wpa_printf(MSG_DEBUG, "TLSv1: Failed to initialize "				   "cipher");			return -1;		}	}	return 0;}/** * tlsv1_record_change_read_cipher - TLS record layer: Change read cipher * @rl: Pointer to TLS record layer data * Returns: 0 on success (cipher changed), -1 on failure * * This function changes TLS record layer to use the new cipher suite * configured with tlsv1_record_set_cipher_suite() for reading. */int tlsv1_record_change_read_cipher(struct tlsv1_record_layer *rl){	wpa_printf(MSG_DEBUG, "TLSv1: Record Layer - New read cipher suite "		   "0x%04x", rl->cipher_suite);	rl->read_cipher_suite = rl->cipher_suite;	os_memset(rl->read_seq_num, 0, TLS_SEQ_NUM_LEN);	if (rl->read_cbc) {		crypto_cipher_deinit(rl->read_cbc);		rl->read_cbc = NULL;	}	if (rl->cipher_alg != CRYPTO_CIPHER_NULL) {		rl->read_cbc = crypto_cipher_init(rl->cipher_alg,						  rl->read_iv, rl->read_key,						  rl->key_material_len);		if (rl->read_cbc == NULL) {			wpa_printf(MSG_DEBUG, "TLSv1: Failed to initialize "				   "cipher");			return -1;		}	}	return 0;

⌨️ 快捷键说明

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