mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-11-12 23:56:18 +08:00
Update cmake
This commit is contained in:
64
src/tlcp.c
64
src/tlcp.c
@@ -50,21 +50,6 @@ int tlcp_record_print(FILE *fp, int fmt, int ind, const uint8_t *record, size_t
|
||||
record, recordlen);
|
||||
}
|
||||
|
||||
static int tlcp_cipher_suite_get(int cipher_suite, const BLOCK_CIPHER **cipher, const DIGEST **digest)
|
||||
{
|
||||
switch (cipher_suite) {
|
||||
case TLS_cipher_ecc_sm4_cbc_sm3:
|
||||
case TLS_cipher_ecc_sm4_gcm_sm3:
|
||||
*cipher = BLOCK_CIPHER_sm4();
|
||||
*digest = DIGEST_sm3();
|
||||
break;
|
||||
default:
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tlcp_record_encrypt(int cipher_suite,
|
||||
const HMAC_CTX *hmac_ctx, const BLOCK_CIPHER_KEY *key, const uint8_t fixed_iv[4],
|
||||
const uint8_t seq_num[8], const uint8_t *in, size_t inlen,
|
||||
@@ -387,18 +372,11 @@ struct {
|
||||
opaque RSAEncryptedPreMasterSecret<0..2^16-1>;
|
||||
} exchange_keys;
|
||||
} ClientKeyExchange;
|
||||
|
||||
这里比较特殊的是需要打印一个SM2Ciphertext,也许直接打印二进制就可以了
|
||||
*/
|
||||
|
||||
|
||||
// Handshakes
|
||||
|
||||
// 一般来说digest_init是在cipher_suite定下来之后才能设置
|
||||
// 但是TLCP中的SM3是确定的,因此可以在ClientHello, ServerHello中设置
|
||||
|
||||
|
||||
// 这个一定是要不同的
|
||||
int tlcp_send_client_hello(TLS_CONNECT *conn)
|
||||
{
|
||||
int ret;
|
||||
@@ -413,13 +391,16 @@ int tlcp_send_client_hello(TLS_CONNECT *conn)
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (tlcp_cipher_suite_get(conn->ctx->cipher_suites[0], &conn->cipher, &conn->digest) != 1
|
||||
|| digest_init(&conn->dgst_ctx, conn->digest) != 1) {
|
||||
|
||||
// TLCP only use sm4 and sm3
|
||||
conn->cipher = BLOCK_CIPHER_sm4();
|
||||
conn->digest = DIGEST_sm3();
|
||||
|
||||
if (digest_init(&conn->dgst_ctx, conn->digest) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
tls_record_set_protocol(conn->record, TLS_protocol_tlcp);
|
||||
|
||||
if (tls_random_generate(conn->client_random) != 1) {
|
||||
@@ -508,15 +489,19 @@ int tlcp_send_client_hello(TLS_CONNECT *conn)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(conn->verbose) tls_trace("send ClientHello\n");
|
||||
if (conn->verbose)
|
||||
if (conn->verbose) {
|
||||
tls_trace("send ClientHello\n");
|
||||
tlcp_record_print(stderr, 0, 0, conn->record, conn->recordlen);
|
||||
}
|
||||
|
||||
if (digest_update(&conn->dgst_ctx, conn->record + 5, conn->recordlen - 5) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if(conn->verbose) tls_handshake_digest_print(stderr, 0, 0, "ClientHello", &conn->dgst_ctx);
|
||||
if(conn->verbose)
|
||||
tls_handshake_digest_print(stderr, 0, 0, "ClientHello", &conn->dgst_ctx);
|
||||
|
||||
|
||||
|
||||
if (conn->client_certificate_verify) {
|
||||
sm2_sign_update(&conn->sign_ctx, conn->record + 5, conn->recordlen - 5);
|
||||
@@ -593,11 +578,6 @@ int tlcp_recv_server_hello(TLS_CONNECT *conn)
|
||||
return -1;
|
||||
}
|
||||
conn->cipher_suite = cipher_suite;
|
||||
if (tlcp_cipher_suite_get(conn->cipher_suite, &conn->cipher, &conn->digest) != 1) {
|
||||
error_print();
|
||||
tls_send_alert(conn, TLS_alert_internal_error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (extslen) {
|
||||
int ext_type;
|
||||
@@ -1479,9 +1459,14 @@ int tlcp_recv_client_hello(TLS_CONNECT *conn)
|
||||
const uint8_t *host_name = NULL;
|
||||
size_t host_name_len = 0;
|
||||
|
||||
// TLCP only use sm4 and sm3
|
||||
conn->cipher = BLOCK_CIPHER_sm4();
|
||||
conn->digest = DIGEST_sm3();
|
||||
|
||||
|
||||
|
||||
if (digest_init(&conn->dgst_ctx, conn->digest) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 这个判断应该改为一个函数
|
||||
/*
|
||||
@@ -1648,12 +1633,6 @@ int tlcp_recv_client_hello(TLS_CONNECT *conn)
|
||||
}
|
||||
}
|
||||
|
||||
if (tlcp_cipher_suite_get(conn->cipher_suite, &conn->cipher, &conn->digest) != 1) {
|
||||
error_print();
|
||||
tls_send_alert(conn, TLS_alert_internal_error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (supported_groups) {
|
||||
int common_group;
|
||||
size_t common_groups_cnt = 0;
|
||||
@@ -1748,8 +1727,7 @@ int tlcp_recv_client_hello(TLS_CONNECT *conn)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (digest_init(&conn->dgst_ctx, conn->digest) != 1
|
||||
|| digest_update(&conn->dgst_ctx, conn->record + 5, conn->recordlen - 5) != 1) {
|
||||
if (digest_update(&conn->dgst_ctx, conn->record + 5, conn->recordlen - 5) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
106
src/tls13.c
106
src/tls13.c
@@ -646,6 +646,7 @@ int tls13_generate_early_keys(TLS_CONNECT *conn)
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
conn->cipher_suite = conn->psk_cipher_suites[0];
|
||||
client_write_key_len = conn->cipher->key_size;
|
||||
|
||||
if (digest_init(&conn->dgst_ctx, conn->digest) != 1
|
||||
@@ -692,8 +693,8 @@ int tls13_generate_early_keys(TLS_CONNECT *conn)
|
||||
int tls13_generate_handshake_secrets(TLS_CONNECT *conn)
|
||||
{
|
||||
const uint8_t zeros[32] = {0};
|
||||
uint8_t ecdhe_shared_secret[32];
|
||||
size_t ecdhe_shared_secret_len;
|
||||
uint8_t ecdhe_shared_secret[32] = {0};
|
||||
size_t ecdhe_shared_secret_len = sizeof(ecdhe_shared_secret);
|
||||
uint8_t derived_secret[32];
|
||||
DIGEST_CTX null_dgst_ctx;
|
||||
|
||||
@@ -2714,7 +2715,8 @@ int tls13_record_get_handshake_hello_retry_request(uint8_t *record,
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (type != TLS_handshake_hello_retry_request) {
|
||||
if (type != TLS_handshake_hello_retry_request
|
||||
&& type != TLS_handshake_server_hello) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -4285,8 +4287,9 @@ int tls13_send_client_hello(TLS_CONNECT *conn)
|
||||
// generate binders and output final pre_shared_key ext
|
||||
if (tls13_psk_binders_generate(
|
||||
conn->psk_cipher_suites, conn->psk_cipher_suites_cnt,
|
||||
conn->psk_identity_types,
|
||||
conn->psk_keys, conn->psk_keys_len,
|
||||
conn->record + 5, conn->recordlen - 5,
|
||||
conn->record + 5, conn->recordlen - 5 - 2 - binderslen,
|
||||
binders, &binderslen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
@@ -4425,7 +4428,8 @@ int tls13_recv_hello_retry_request(TLS_CONNECT *conn)
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (handshake_type != TLS_handshake_hello_retry_request) {
|
||||
if (handshake_type != TLS_handshake_hello_retry_request
|
||||
&& handshake_type != TLS_handshake_server_hello) {
|
||||
if(conn->verbose) tls_trace(" no HelloRetryRequest\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -4440,6 +4444,10 @@ int tls13_recv_hello_retry_request(TLS_CONNECT *conn)
|
||||
tls13_send_alert(conn, TLS_alert_decode_error);
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(random, TLS13_HELLO_RETRY_REQUEST_RANDOM, 32) != 0) {
|
||||
if(conn->verbose) tls_trace(" no HelloRetryRequest\n");
|
||||
return 0;
|
||||
}
|
||||
conn->hello_retry_request = 1;
|
||||
|
||||
|
||||
@@ -4731,12 +4739,6 @@ int tls13_send_client_hello_again(TLS_CONNECT *conn)
|
||||
// record_version
|
||||
tls_record_set_protocol(conn->record, TLS_protocol_tls1);
|
||||
|
||||
// client_random
|
||||
if (tls13_random_generate(conn->client_random) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Extensions
|
||||
|
||||
// keep ClientHello extensions except
|
||||
@@ -4915,8 +4917,9 @@ int tls13_send_client_hello_again(TLS_CONNECT *conn)
|
||||
// generate binders and output final pre_shared_key ext
|
||||
if (tls13_psk_binders_generate(
|
||||
conn->psk_cipher_suites, conn->psk_cipher_suites_cnt,
|
||||
conn->psk_identity_types,
|
||||
conn->psk_keys, conn->psk_keys_len,
|
||||
conn->record + 5, conn->recordlen - 5,
|
||||
conn->record + 5, conn->recordlen - 5 - 2 - binderslen,
|
||||
binders, &binderslen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
@@ -6681,9 +6684,6 @@ int tls13_send_client_finished(TLS_CONNECT *conn)
|
||||
tls13_compute_verify_data(conn->client_handshake_traffic_secret, &conn->dgst_ctx,
|
||||
verify_data, &verify_data_len);
|
||||
|
||||
memset(&conn->dgst_ctx, 0, sizeof(conn->dgst_ctx));
|
||||
|
||||
|
||||
if (tls_record_set_handshake_finished(conn->plain_record, &conn->plain_recordlen,
|
||||
verify_data, verify_data_len) != 1) {
|
||||
error_print();
|
||||
@@ -6716,6 +6716,10 @@ int tls13_send_client_finished(TLS_CONNECT *conn)
|
||||
|
||||
|
||||
tls13_generate_client_application_keys(conn);
|
||||
if (digest_update(&conn->dgst_ctx, conn->plain_record + 5, conn->plain_recordlen - 5) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -6838,7 +6842,8 @@ int tls13_recv_client_hello(TLS_CONNECT *conn)
|
||||
}
|
||||
tls13_record_print(stderr, 0, 0, conn->record, conn->recordlen);
|
||||
|
||||
if (tls_record_protocol(record) != TLS_protocol_tls1) {
|
||||
if (tls_record_protocol(record) != TLS_protocol_tls1
|
||||
&& tls_record_protocol(record) != TLS_protocol_tls12) {
|
||||
error_print();
|
||||
tls13_send_alert(conn, TLS_alert_protocol_version);
|
||||
return -1;
|
||||
@@ -7465,17 +7470,15 @@ int tls13_recv_client_hello(TLS_CONNECT *conn)
|
||||
* key_exchange_group
|
||||
* [key_exchanges_cnt]
|
||||
*/
|
||||
if (common_key_exchange_modes & TLS_KE_PSK_DHE) {
|
||||
if (conn->selected_psk_identity && conn->key_exchange_group) {
|
||||
if ((common_key_exchange_modes & TLS_KE_PSK_DHE) && conn->selected_psk_identity) {
|
||||
if (conn->key_exchange_group) {
|
||||
conn->key_exchange_modes = TLS_KE_PSK_DHE;
|
||||
if (!conn->peer_key_exchange_len) {
|
||||
conn->hello_retry_request = 1;
|
||||
}
|
||||
}
|
||||
} else if (common_key_exchange_modes & TLS_KE_PSK) {
|
||||
if (conn->selected_psk_identity) {
|
||||
conn->key_exchange_modes = TLS_KE_PSK;
|
||||
}
|
||||
} else if ((common_key_exchange_modes & TLS_KE_PSK) && conn->selected_psk_identity) {
|
||||
conn->key_exchange_modes = TLS_KE_PSK;
|
||||
} else if (common_key_exchange_modes & TLS_KE_CERT_DHE) {
|
||||
if (conn->cert_chain && conn->key_exchange_group) {
|
||||
conn->key_exchange_modes = TLS_KE_CERT_DHE;
|
||||
@@ -7690,7 +7693,8 @@ int tls13_recv_client_hello_again(TLS_CONNECT *conn)
|
||||
}
|
||||
tls13_record_print(stderr, 0, 0, conn->record, conn->recordlen);
|
||||
|
||||
if (tls_record_protocol(record) != TLS_protocol_tls12) {
|
||||
if (tls_record_protocol(record) != TLS_protocol_tls1
|
||||
&& tls_record_protocol(record) != TLS_protocol_tls12) {
|
||||
error_print();
|
||||
tls13_send_alert(conn, TLS_alert_protocol_version);
|
||||
return -1;
|
||||
@@ -8026,7 +8030,7 @@ int tls13_send_server_hello(TLS_CONNECT *conn)
|
||||
}
|
||||
|
||||
// pre_shared_key
|
||||
if (conn->key_exchange_modes & (TLS_KE_PSK_DHE|TLS_KE_PSK)) {
|
||||
if (conn->selected_psk_identity) {
|
||||
if (tls13_server_pre_shared_key_ext_to_bytes(
|
||||
conn->selected_psk_identity, &pexts, &extslen) != 1) {
|
||||
error_print();
|
||||
@@ -8730,8 +8734,6 @@ int tls13_recv_client_finished(TLS_CONNECT *conn)
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memset(&conn->dgst_ctx, 0, sizeof(conn->dgst_ctx));
|
||||
|
||||
|
||||
// 当客户端发送了Certificate的时候,这个验证是不对的
|
||||
// 服务器多发送了CertificateRequest
|
||||
@@ -8745,14 +8747,16 @@ int tls13_recv_client_finished(TLS_CONNECT *conn)
|
||||
}
|
||||
|
||||
tls13_generate_client_application_keys(conn);
|
||||
if (digest_update(&conn->dgst_ctx, conn->plain_record + 5, conn->plain_recordlen - 5) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls13_send_early_data(TLS_CONNECT *conn)
|
||||
{
|
||||
size_t sentlen;
|
||||
|
||||
if(conn->verbose) tls_trace("send EarlyData\n");
|
||||
|
||||
if (!conn->early_data) {
|
||||
@@ -8766,13 +8770,48 @@ int tls13_send_early_data(TLS_CONNECT *conn)
|
||||
|
||||
while (conn->early_data_offset < conn->early_data_len) {
|
||||
int ret;
|
||||
if ((ret = tls13_send(conn, conn->early_data_buf + conn->early_data_offset,
|
||||
conn->early_data_len - conn->early_data_offset, &sentlen)) <= 0) {
|
||||
if (conn->recordlen == 0) {
|
||||
uint8_t plain_record[TLS_MAX_RECORD_SIZE];
|
||||
size_t plain_recordlen;
|
||||
size_t datalen;
|
||||
size_t padding_len;
|
||||
|
||||
datalen = conn->early_data_len - conn->early_data_offset;
|
||||
if (datalen > TLS_MAX_PLAINTEXT_SIZE) {
|
||||
datalen = TLS_MAX_PLAINTEXT_SIZE;
|
||||
}
|
||||
|
||||
tls13_padding_len_rand(&padding_len);
|
||||
if (tls_record_set_application_data(plain_record, &plain_recordlen,
|
||||
conn->early_data_buf + conn->early_data_offset, datalen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (tls13_record_encrypt(conn->cipher_suite, &conn->client_write_key, conn->client_write_iv,
|
||||
conn->client_seq_num, plain_record, plain_recordlen, padding_len,
|
||||
conn->record, &conn->recordlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
tls_seq_num_incr(conn->client_seq_num);
|
||||
|
||||
conn->record_offset = 0;
|
||||
conn->sentlen = datalen;
|
||||
|
||||
tls13_record_print(stderr, 0, 0, conn->record, conn->recordlen);
|
||||
}
|
||||
|
||||
if ((ret = tls_send_record(conn)) != 1) {
|
||||
if (ret == TLS_ERROR_SEND_AGAIN) {
|
||||
return ret;
|
||||
}
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
conn->early_data_offset += sentlen;
|
||||
|
||||
conn->early_data_offset += conn->sentlen;
|
||||
conn->record_offset = 0;
|
||||
conn->recordlen = 0;
|
||||
}
|
||||
|
||||
conn->record_offset = 0;
|
||||
@@ -9033,6 +9072,7 @@ int tls13_do_client_handshake(TLS_CONNECT *conn)
|
||||
int tls13_do_server_handshake(TLS_CONNECT *conn)
|
||||
{
|
||||
int ret;
|
||||
int state = conn->handshake_state;
|
||||
int next_state;
|
||||
|
||||
switch (conn->handshake_state) {
|
||||
@@ -9164,7 +9204,9 @@ int tls13_do_server_handshake(TLS_CONNECT *conn)
|
||||
|
||||
conn->handshake_state = next_state;
|
||||
|
||||
tls_clean_record(conn);
|
||||
if (!(state == TLS_state_client_change_cipher_spec && ret == 0)) {
|
||||
tls_clean_record(conn);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -629,6 +629,7 @@ int tls13_add_pre_shared_key_from_session_file(TLS_CONNECT *conn, FILE *fp)
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
conn->psk_identity_types[conn->psk_cipher_suites_cnt - 1] = TLS13_PSK_IDENTITY_RESUMPTION;
|
||||
|
||||
conn->pre_shared_key = 1;
|
||||
return 1;
|
||||
@@ -681,8 +682,6 @@ int tls13_send_new_session_ticket(TLS_CONNECT *conn)
|
||||
|
||||
// generate resumption_master_secret
|
||||
|
||||
digest_init(&conn->dgst_ctx, conn->digest);
|
||||
|
||||
/* [14] */ tls13_derive_secret(conn->master_secret, "res master", &conn->dgst_ctx, resumption_master_secret);
|
||||
|
||||
// pre_shared_key = HKDF-Expand-Label(resumption_master_secret, "resumption", ticket_nonce, Hash.length)
|
||||
@@ -808,8 +807,6 @@ int tls13_process_new_session_ticket(TLS_CONNECT *conn)
|
||||
size_t dgstlen = 32;
|
||||
uint8_t pre_shared_key[32];
|
||||
|
||||
digest_init(&conn->dgst_ctx, conn->digest);
|
||||
|
||||
// generate resumption_master_secret
|
||||
/* [14] */ tls13_derive_secret(conn->master_secret, "res master", &conn->dgst_ctx, resumption_master_secret);
|
||||
|
||||
@@ -1000,6 +997,7 @@ int tls13_psk_binders_generate_empty(const int *psk_cipher_suites, size_t psk_ci
|
||||
|
||||
int tls13_psk_binders_generate(
|
||||
const int *psk_cipher_suites, size_t psk_cipher_suites_cnt,
|
||||
const uint8_t *psk_identity_types,
|
||||
const uint8_t *psk_keys, size_t psk_keys_len,
|
||||
const uint8_t *truncated_client_hello, size_t truncated_client_hello_len,
|
||||
uint8_t *binders, size_t *binders_len)
|
||||
@@ -1028,11 +1026,16 @@ int tls13_psk_binders_generate(
|
||||
const DIGEST *digest;
|
||||
const uint8_t *psk_key;
|
||||
size_t psk_key_len;
|
||||
const char *binder_label = "ext binder";
|
||||
|
||||
if (tls13_cipher_suite_get(psk_cipher_suites[i], &cipher, &digest) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (psk_identity_types
|
||||
&& psk_identity_types[i] == TLS13_PSK_IDENTITY_RESUMPTION) {
|
||||
binder_label = "res binder";
|
||||
}
|
||||
|
||||
if (digest->digest_size != sizeof(secret)) {
|
||||
error_print();
|
||||
@@ -1058,7 +1061,7 @@ int tls13_psk_binders_generate(
|
||||
// [1]
|
||||
tls13_hkdf_extract(digest, zeros, psk_key, early_secret);
|
||||
// [2]
|
||||
tls13_derive_secret(early_secret, "res binder", &null_dgst_ctx, binder_key);
|
||||
tls13_derive_secret(early_secret, binder_label, &null_dgst_ctx, binder_key);
|
||||
|
||||
tls13_compute_verify_data(binder_key, &dgst_ctx, binder, &binderlen);
|
||||
|
||||
@@ -1069,10 +1072,10 @@ int tls13_psk_binders_generate(
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls13_psk_binder_verify(const DIGEST *digest,
|
||||
static int tls13_psk_binder_verify_with_label(const DIGEST *digest,
|
||||
const uint8_t *pre_shared_key, size_t pre_shared_key_len,
|
||||
const DIGEST_CTX *truncated_client_hello_dgst_ctx,
|
||||
const uint8_t *binder, size_t binderlen)
|
||||
const uint8_t *binder, size_t binderlen, const char *binder_label)
|
||||
{
|
||||
uint8_t secret[32] = {0};
|
||||
uint8_t *zeros = secret;
|
||||
@@ -1090,7 +1093,7 @@ int tls13_psk_binder_verify(const DIGEST *digest,
|
||||
// [1]
|
||||
tls13_hkdf_extract(digest, zeros, pre_shared_key, early_secret);
|
||||
// [2]
|
||||
tls13_derive_secret(early_secret, "res binder", &null_dgst_ctx, binder_key);
|
||||
tls13_derive_secret(early_secret, binder_label, &null_dgst_ctx, binder_key);
|
||||
|
||||
tls13_compute_verify_data(binder_key, truncated_client_hello_dgst_ctx, local_binder, &local_binder_len);
|
||||
|
||||
@@ -1101,6 +1104,15 @@ int tls13_psk_binder_verify(const DIGEST *digest,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls13_psk_binder_verify(const DIGEST *digest,
|
||||
const uint8_t *pre_shared_key, size_t pre_shared_key_len,
|
||||
const DIGEST_CTX *truncated_client_hello_dgst_ctx,
|
||||
const uint8_t *binder, size_t binderlen)
|
||||
{
|
||||
return tls13_psk_binder_verify_with_label(digest, pre_shared_key, pre_shared_key_len,
|
||||
truncated_client_hello_dgst_ctx, binder, binderlen, "res binder");
|
||||
}
|
||||
|
||||
int tls13_client_pre_shared_key_ext_to_bytes(const uint8_t *identities, size_t identitieslen,
|
||||
const uint8_t *binders, size_t binderslen, uint8_t **out, size_t *outlen)
|
||||
{
|
||||
@@ -1288,7 +1300,9 @@ int tls13_add_pre_shared_key(TLS_CONNECT *conn,
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
conn->psk_cipher_suites[conn->psk_cipher_suites_cnt++] = psk_cipher_suite;
|
||||
conn->psk_cipher_suites[conn->psk_cipher_suites_cnt] = psk_cipher_suite;
|
||||
conn->psk_identity_types[conn->psk_cipher_suites_cnt] = TLS13_PSK_IDENTITY_EXTERNAL;
|
||||
conn->psk_cipher_suites_cnt++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -1343,8 +1357,8 @@ int tls13_process_client_pre_shared_key_external(TLS_CONNECT *conn,
|
||||
size_t identitieslen;
|
||||
const uint8_t *binders;
|
||||
size_t binderslen;
|
||||
const uint8_t *truncated_binders;
|
||||
size_t truncated_binderslen;
|
||||
const uint8_t *truncated_client_hello;
|
||||
size_t truncated_client_hello_len;
|
||||
size_t i;
|
||||
|
||||
if (!conn || !ext_data || !ext_datalen) {
|
||||
@@ -1364,21 +1378,12 @@ int tls13_process_client_pre_shared_key_external(TLS_CONNECT *conn,
|
||||
return -1;
|
||||
}
|
||||
|
||||
// truncate client_hello => plain_record
|
||||
memcpy(conn->plain_record, conn->record, conn->recordlen);
|
||||
conn->plain_recordlen = conn->recordlen;
|
||||
truncated_binders = conn->plain_record + (binders - conn->record);
|
||||
truncated_binderslen = binderslen;
|
||||
while (truncated_binderslen) {
|
||||
const uint8_t *truncated_binder;
|
||||
size_t truncated_binderlen;
|
||||
if (tls_uint8array_from_bytes(&truncated_binder, &truncated_binderlen,
|
||||
&truncated_binders, &truncated_binderslen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memset((uint8_t *)truncated_binder, 0, truncated_binderlen);
|
||||
if (binders < conn->record + TLS_RECORD_HEADER_SIZE + 2) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
truncated_client_hello = conn->record + TLS_RECORD_HEADER_SIZE;
|
||||
truncated_client_hello_len = (size_t)((binders - 2) - truncated_client_hello);
|
||||
|
||||
// search psk
|
||||
for (i = 0; identitieslen; i++) {
|
||||
@@ -1423,12 +1428,12 @@ int tls13_process_client_pre_shared_key_external(TLS_CONNECT *conn,
|
||||
|
||||
// verify binder
|
||||
if (digest_init(&dgst_ctx, conn->digest) != 1
|
||||
|| digest_update(&dgst_ctx, conn->plain_record + 5, conn->plain_recordlen - 5) != 1) {
|
||||
|| digest_update(&dgst_ctx, truncated_client_hello, truncated_client_hello_len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if ((ret = tls13_psk_binder_verify(conn->digest, matched_psk, matched_psk_len,
|
||||
&dgst_ctx, binder, binderlen)) < 0) {
|
||||
if ((ret = tls13_psk_binder_verify_with_label(conn->digest, matched_psk, matched_psk_len,
|
||||
&dgst_ctx, binder, binderlen, "ext binder")) < 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
} else if (ret == 0) {
|
||||
@@ -1456,8 +1461,8 @@ int tls13_process_client_pre_shared_key_from_ticket(TLS_CONNECT *conn,
|
||||
size_t identitieslen;
|
||||
const uint8_t *binders;
|
||||
size_t binderslen;
|
||||
const uint8_t *truncated_binders;
|
||||
size_t truncated_binderslen;
|
||||
const uint8_t *truncated_client_hello;
|
||||
size_t truncated_client_hello_len;
|
||||
size_t i;
|
||||
|
||||
if (!conn || !ext_data || !ext_datalen) {
|
||||
@@ -1485,21 +1490,12 @@ int tls13_process_client_pre_shared_key_from_ticket(TLS_CONNECT *conn,
|
||||
return -1;
|
||||
}
|
||||
|
||||
// truncate client_hello
|
||||
memcpy(conn->plain_record, conn->record, conn->recordlen);
|
||||
conn->plain_recordlen = conn->recordlen;
|
||||
truncated_binders = conn->plain_record + (binders - conn->record);
|
||||
truncated_binderslen = binderslen;
|
||||
while (truncated_binderslen) {
|
||||
const uint8_t *truncated_binder;
|
||||
size_t truncated_binderlen;
|
||||
if (tls_uint8array_from_bytes(&truncated_binder, &truncated_binderlen,
|
||||
&truncated_binders, &truncated_binderslen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memset((uint8_t *)truncated_binder, 0, truncated_binderlen);
|
||||
if (binders < conn->record + TLS_RECORD_HEADER_SIZE + 2) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
truncated_client_hello = conn->record + TLS_RECORD_HEADER_SIZE;
|
||||
truncated_client_hello_len = (size_t)((binders - 2) - truncated_client_hello);
|
||||
|
||||
// search psk
|
||||
for (i = 0; identitieslen; i++) {
|
||||
@@ -1552,7 +1548,7 @@ int tls13_process_client_pre_shared_key_from_ticket(TLS_CONNECT *conn,
|
||||
|
||||
// verify binder
|
||||
if (digest_init(&dgst_ctx, conn->digest) != 1
|
||||
|| digest_update(&dgst_ctx, conn->plain_record + 5, conn->plain_recordlen - 5) != 1) {
|
||||
|| digest_update(&dgst_ctx, truncated_client_hello, truncated_client_hello_len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user