Update TLS 1.3 handshake

Working on HelloRetryRequest, NewSessionTicket
This commit is contained in:
Zhi Guan
2026-03-21 18:41:46 +08:00
parent ead4caecb7
commit 0d1acec6df
7 changed files with 2106 additions and 1163 deletions

View File

@@ -1670,6 +1670,11 @@ int tls_seq_num_incr(uint8_t seq_num[8])
return 1;
}
void tls_seq_num_reset(uint8_t seq_num[8])
{
memset(seq_num, 0, 8);
}
int tls_compression_methods_has_null_compression(const uint8_t *meths, size_t methslen)
{
if (!meths || !methslen) {
@@ -2247,8 +2252,6 @@ int tls_ctx_set_signature_algorithms(TLS_CTX *ctx, const int *sig_algs, size_t s
int tls_ctx_init(TLS_CTX *ctx, int protocol, int is_client)
{
const int supported_versions[] = {
TLS_protocol_tls13,
TLS_protocol_tls12,
@@ -2257,11 +2260,13 @@ int tls_ctx_init(TLS_CTX *ctx, int protocol, int is_client)
size_t supported_versions_cnt = sizeof(supported_versions)/sizeof(supported_versions[0]);
const int supported_groups[] = {
TLS_curve_sm2p256v1,
TLS_curve_secp256r1,
TLS_curve_sm2p256v1,
};
size_t supported_groups_cnt = sizeof(supported_groups)/sizeof(supported_groups[0]);
const int signature_algorithms[] = {
TLS_sig_sm2sig_sm3,
TLS_sig_ecdsa_secp256r1_sha256,
@@ -2295,9 +2300,16 @@ int tls_ctx_init(TLS_CTX *ctx, int protocol, int is_client)
return -1;
}
// test HelloRetryRequest
if (!is_client) {
tls_ctx_set_supported_groups(ctx, supported_groups + 1, supported_groups_cnt - 1);
}
ctx->verify_depth = 5;
ctx->new_session_ticket = 1;
return 1;
}
@@ -2567,6 +2579,11 @@ int tls_init(TLS_CONNECT *conn, const TLS_CTX *ctx)
conn->ctx = ctx;
conn->key_exchanges_cnt = 1;
conn->new_session_ticket = ctx->new_session_ticket;
return 1;
}

File diff suppressed because it is too large Load Diff

View File

@@ -321,6 +321,24 @@ const char *tls_signature_scheme_name(int scheme)
return NULL;
}
int tls_signature_scheme_oid(int sig_alg)
{
switch (sig_alg) {
case TLS_sig_sm2sig_sm3: return OID_sm2sign_with_sm3;
case TLS_sig_ecdsa_secp256r1_sha256: return OID_ecdsa_with_sha256;
}
return 0;
}
int tls_signature_scheme_from_oid(int sig_alg_oid)
{
switch (sig_alg_oid) {
case OID_sm2sign_with_sm3: return TLS_sig_sm2sig_sm3;
case OID_ecdsa_with_sha256: return TLS_sig_ecdsa_secp256r1_sha256;
}
return 0;
}
int tls_random_print(FILE *fp, const uint8_t random[32], int format, int indent)
{
time_t gmt_unix_time = 0;
@@ -382,7 +400,7 @@ int tls_extension_print(FILE *fp, int type, const uint8_t *data, size_t datalen,
while (len) {
uint16_t proto;
tls_uint16_from_bytes(&proto, &p, &len);
format_print(fp, format, indent, "%s (0x%04x)\n",
format_print(fp, format, indent, "%s (%04x)\n",
tls_protocol_name(proto), proto);
}
break;
@@ -423,7 +441,7 @@ int tls_extension_print(FILE *fp, int type, const uint8_t *data, size_t datalen,
while (len) {
uint16_t sig_alg;
tls_uint16_from_bytes(&sig_alg, &p, &len);
format_print(fp, format, indent, "%s (0x%04x)\n",
format_print(fp, format, indent, "%s (%04x)\n",
tls_signature_scheme_name(sig_alg), sig_alg);
}
break;
@@ -500,8 +518,8 @@ int tls_client_hello_print(FILE *fp, const uint8_t *data, size_t datalen, int fo
format_print(fp, format, indent, "ClientHello\n"); indent += 4;
if (tls_uint16_from_bytes(&protocol, &data, &datalen) != 1) goto end;
format_print(fp, format, indent, "Version: %s (%d.%d)\n",
tls_protocol_name(protocol), protocol >> 8, protocol & 0xff);
format_print(fp, format, indent, "Version: %s (%04x)\n",
tls_protocol_name(protocol), protocol);
if (tls_array_from_bytes(&random, 32, &data, &datalen) != 1) goto end;
tls_random_print(fp, random, format, indent);
if (tls_uint8array_from_bytes(&session_id, &session_id_len, &data, &datalen) != 1) goto end;
@@ -511,7 +529,7 @@ int tls_client_hello_print(FILE *fp, const uint8_t *data, size_t datalen, int fo
while (cipher_suites_len >= 2) {
uint16_t cipher;
if (tls_uint16_from_bytes(&cipher, &cipher_suites, &cipher_suites_len) != 1) goto end;
format_print(fp, format, indent + 4, "%s (0x%04x)\n",
format_print(fp, format, indent + 4, "%s (%04x)\n",
tls_cipher_suite_name(cipher), cipher);
}
if (cipher_suites_len) {
@@ -586,14 +604,14 @@ int tls_server_hello_print(FILE *fp, const uint8_t *data, size_t datalen, int fo
format_print(fp, format, indent, "ServerHello\n"); indent += 4;
if (tls_uint16_from_bytes(&protocol, &data, &datalen) != 1) goto bad;
format_print(fp, format, indent, "Version: %s (%d.%d)\n",
tls_protocol_name(protocol), protocol >> 8, protocol & 0xff);
format_print(fp, format, indent, "Version: %s (%04x)\n",
tls_protocol_name(protocol), protocol);
if (tls_array_from_bytes(&random, 32, &data, &datalen) != 1) goto bad;
tls_random_print(fp, random, format, indent);
if (tls_uint8array_from_bytes(&session_id, &session_id_len, &data, &datalen) != 1) goto bad;
format_bytes(fp, format, indent, "SessionID", session_id, session_id_len);
if (tls_uint16_from_bytes(&cipher_suite, &data, &datalen) != 1) goto bad;
format_print(fp, format, indent, "CipherSuite: %s (0x%04x)\n",
format_print(fp, format, indent, "CipherSuite: %s (%04x)\n",
tls_cipher_suite_name(cipher_suite), cipher_suite);
if (tls_uint8_from_bytes(&comp_meth, &data, &datalen) != 1) goto bad;
format_print(fp, format, indent, "CompressionMethod: %s (%d)\n",
@@ -673,7 +691,7 @@ int tls_server_key_exchange_ecdhe_print(FILE *fp, const uint8_t *data, size_t da
error_print();
return -1;
}
format_print(fp, format, indent, "SignatureScheme: %s (0x%04x)\n",
format_print(fp, format, indent, "SignatureScheme: %s (%04x)\n",
tls_signature_scheme_name(sig_alg), sig_alg);
if (tls_uint16array_from_bytes(&sig, &siglen, &data, &datalen) != 1) {
error_print();
@@ -990,7 +1008,7 @@ int tls_record_print(FILE *fp, const uint8_t *record, size_t recordlen, int for
protocol = tls_record_protocol(record);
format_print(fp, format, indent, "Record\n"); indent += 4;
format_print(fp, format, indent, "ContentType: %s (%d)\n", tls_record_type_name(record[0]), record[0]);
format_print(fp, format, indent, "Version: %s (%d.%d)\n", tls_protocol_name(protocol), protocol >> 8, protocol & 0xff);
format_print(fp, format, indent, "Version: %s (%04x)\n", tls_protocol_name(protocol), protocol);
format_print(fp, format, indent, "Length: %d\n", tls_record_data_length(record));
data = tls_record_data(record);
@@ -1075,7 +1093,7 @@ int tls_encrypted_record_print(FILE *fp, const uint8_t *record, size_t recordle
protocol = tls_record_protocol(record);
format_print(fp, format, indent, "EncryptedRecord\n"); indent += 4;
format_print(fp, format, indent, "ContentType: %s (%d)\n", tls_record_type_name(record[0]), record[0]);
format_print(fp, format, indent, "Version: %s (%d.%d)\n", tls_protocol_name(protocol), protocol >> 8, protocol & 0xff);
format_print(fp, format, indent, "Version: %s (%04x)\n", tls_protocol_name(protocol), protocol);
format_print(fp, format, indent, "Length: %d\n", tls_record_data_length(record));
format_bytes(fp, format, indent, "EncryptedData", tls_record_data(record), tls_record_data_length(record));

View File

@@ -1594,6 +1594,36 @@ int x509_cert_get_exts(const uint8_t *a, size_t alen, const uint8_t **d, size_t
return 1;
}
int x509_cert_get_signature_algor(const uint8_t *a, size_t alen, int *oid)
{
int inner_alg;
int outer_alg;
if (x509_cert_get_details(a, alen,
NULL, // version
NULL, NULL, // serial
&inner_alg, // signature_algor
NULL, NULL, // issuer
NULL, NULL, // validity
NULL, NULL, // subject
NULL, // subject_public_key
NULL, NULL, // issuer_unique_id
NULL, NULL, // subject_unique_id
NULL, NULL, // extensions
&outer_alg, // signature_algor
NULL, NULL // signature
) != 1) {
error_print();
return -1;
}
if (inner_alg != outer_alg) {
error_print();
return -1;
}
*oid = inner_alg;
return 1;
}
int x509_certs_to_pem(const uint8_t *d, size_t dlen, FILE *fp)
{
const uint8_t *a;