mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-24 14:23:47 +08:00
Update TLS 1.3
This commit is contained in:
137
src/tls.c
137
src/tls.c
@@ -84,13 +84,6 @@ void tls_array_to_bytes(const uint8_t *data, size_t datalen, uint8_t **out, size
|
||||
*outlen += datalen;
|
||||
}
|
||||
|
||||
/*
|
||||
这几个函数要区分data = NULL, datalen = 0 和 data = NULL, datalen != 0的情况
|
||||
前者意味着数据为空,因此输出的就是一个长度
|
||||
后者意味着数据不为空,只是我们不想输出数据,只输出头部的长度,并且更新整个的输出长度。 这种情况应该避免!
|
||||
|
||||
*/
|
||||
|
||||
void tls_uint8array_to_bytes(const uint8_t *data, size_t datalen, uint8_t **out, size_t *outlen)
|
||||
{
|
||||
tls_uint8_to_bytes((uint8_t)datalen, out, outlen);
|
||||
@@ -302,7 +295,7 @@ int tls_cbc_encrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *enc_key,
|
||||
return -1;
|
||||
}
|
||||
if (inlen > (1 << 14)) {
|
||||
error_print_msg("invalid tls record data length %zu\n", inlen);
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if ((((size_t)header[3]) << 8) + header[4] != inlen) {
|
||||
@@ -342,8 +335,6 @@ int tls_cbc_encrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *enc_key,
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// 这个函数应该把所有的输入的dgst都打印出来!这样就可以容易判断出到底是哪个输入错了
|
||||
int tls_cbc_decrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *dec_key,
|
||||
const uint8_t seq_num[8], const uint8_t enced_header[5],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
|
||||
@@ -886,9 +877,7 @@ int tls_record_set_handshake_server_hello(uint8_t *record, size_t *recordlen,
|
||||
return -1;
|
||||
}
|
||||
if (session_id) {
|
||||
if (session_id_len == 0
|
||||
|| session_id_len < TLS_MIN_SESSION_ID_SIZE
|
||||
|| session_id_len > TLS_MAX_SESSION_ID_SIZE) {
|
||||
if (session_id_len > TLS_MAX_SESSION_ID_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -1337,7 +1326,6 @@ int tls_record_set_handshake_finished(uint8_t *record, size_t *recordlen,
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 这个应该改为只支持TLS 1.2的12字节长度判断
|
||||
int tls_record_get_handshake_finished(const uint8_t *record, const uint8_t **verify_data, size_t *verify_data_len)
|
||||
{
|
||||
int type;
|
||||
@@ -1358,7 +1346,7 @@ int tls_record_get_handshake_finished(const uint8_t *record, const uint8_t **ver
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (*verify_data_len != 12 && *verify_data_len != 32) {
|
||||
if (*verify_data_len != 12) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -1500,13 +1488,6 @@ int tls_type_is_in_list(int type, const int *list, size_t list_count)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static const int tlcp_ciphers[] = {
|
||||
TLS_cipher_ecc_sm4_cbc_sm3,
|
||||
TLS_cipher_ecc_sm4_gcm_sm3,
|
||||
@@ -1682,7 +1663,6 @@ int tls_seq_num_incr(uint8_t seq_num[8])
|
||||
seq_num[i]++;
|
||||
if (seq_num[i]) break;
|
||||
}
|
||||
// FIXME: check overflow
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1965,8 +1945,6 @@ int tls_shutdown(TLS_CONNECT *conn)
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ca_names中存储的是什么结构的数据?
|
||||
// 看来这个函数是有问题的,这里面存储的应该是GeneralNames类型的数据,subject是什么结构?
|
||||
int tls_authorities_from_certs(uint8_t *names, size_t *nameslen, size_t maxlen, const uint8_t *certs, size_t certslen)
|
||||
{
|
||||
const uint8_t *cert;
|
||||
@@ -2004,9 +1982,6 @@ int tls_authorities_from_certs(uint8_t *names, size_t *nameslen, size_t maxlen,
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 这个函数在语义上有问题:
|
||||
// 首先我们判断的是证书链,因此函数名上应该是一个cert_chain
|
||||
// 调用这个函数传进来的就不是一个证书链
|
||||
int tls_authorities_issued_certificate(const uint8_t *ca_names, size_t ca_names_len, const uint8_t *certs, size_t certslen)
|
||||
{
|
||||
const uint8_t *cert;
|
||||
@@ -2188,39 +2163,6 @@ int tls_cipher_suites_select(const uint8_t *client_ciphers, size_t client_cipher
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tls_ctx_cleanup(TLS_CTX *ctx)
|
||||
{
|
||||
if (ctx) {
|
||||
gmssl_secure_clear(&ctx->signkey, sizeof(SM2_KEY));
|
||||
gmssl_secure_clear(&ctx->kenckey, sizeof(SM2_KEY));
|
||||
if (ctx->certs) free(ctx->certs);
|
||||
if (ctx->cacerts) free(ctx->cacerts);
|
||||
memset(ctx, 0, sizeof(TLS_CTX));
|
||||
}
|
||||
}
|
||||
|
||||
int tls_ctx_print(FILE *fp, int fmt, int ind, const char *label, const TLS_CTX *ctx)
|
||||
{
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int tls_ctx_init(TLS_CTX *ctx, int protocol, int is_client)
|
||||
@@ -2265,12 +2207,20 @@ int tls_ctx_init(TLS_CTX *ctx, int protocol, int is_client)
|
||||
ctx->key_exchanges_cnt = TLS_DEFAULT_KEY_EXCHANGES_CNT;
|
||||
|
||||
|
||||
|
||||
ctx->change_cipher_spec = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void tls_ctx_cleanup(TLS_CTX *ctx)
|
||||
{
|
||||
if (ctx) {
|
||||
gmssl_secure_clear(&ctx->signkey, sizeof(SM2_KEY));
|
||||
gmssl_secure_clear(&ctx->kenckey, sizeof(SM2_KEY));
|
||||
if (ctx->certs) free(ctx->certs);
|
||||
if (ctx->cacerts) free(ctx->cacerts);
|
||||
memset(ctx, 0, sizeof(TLS_CTX));
|
||||
}
|
||||
}
|
||||
|
||||
int tls_ctx_set_supported_versions(TLS_CTX *ctx, const int *versions, size_t versions_cnt)
|
||||
{
|
||||
size_t i;
|
||||
@@ -2326,24 +2276,6 @@ int tls_ctx_set_cipher_suites(TLS_CTX *ctx, const int *cipher_suites, size_t cip
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
int tls_ctx_set_key_exchange_modes(TLS_CTX *ctx, int modes)
|
||||
{
|
||||
if (!ctx) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (modes & ~(TLS_KE_CERT_DHE|TLS_KE_PSK_DHE|TLS_KE_PSK)) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
ctx->key_exchange_modes = modes;
|
||||
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
// 这个函数不是很好,直接提供的是一个文件名
|
||||
int tls_ctx_set_ca_certificates(TLS_CTX *ctx, const char *cacertsfile, int depth)
|
||||
{
|
||||
@@ -2383,7 +2315,6 @@ int tls_ctx_set_ca_certificates(TLS_CTX *ctx, const char *cacertsfile, int depth
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int tls_ctx_enable_certificate_request(TLS_CTX *ctx, int enable)
|
||||
{
|
||||
if (!ctx) {
|
||||
@@ -2400,7 +2331,6 @@ int tls_ctx_enable_certificate_request(TLS_CTX *ctx, int enable)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int tls_ctx_add_certificate_list_and_key(TLS_CTX *ctx, const char *chainfile,
|
||||
const uint8_t *entity_status_request_ocsp_response, size_t entity_status_request_ocsp_response_len, // optional
|
||||
const uint8_t *entity_signed_certificate_timestamp_list, size_t entity_signed_certificate_timestamp_list_len, // optional
|
||||
@@ -2515,7 +2445,6 @@ int tls_ctx_add_certificate_list_and_key(TLS_CTX *ctx, const char *chainfile,
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int tls_ctx_add_certificate_chain_and_key(TLS_CTX *ctx, const char *chainfile,
|
||||
const char *keyfile, const char *keypass)
|
||||
{
|
||||
@@ -2709,7 +2638,6 @@ int tls_ctx_set_supported_groups(TLS_CTX *ctx, const int *groups, size_t groups_
|
||||
|
||||
|
||||
|
||||
|
||||
int tls_ctx_set_signature_algorithms(TLS_CTX *ctx, const int *sig_algs, size_t sig_algs_cnt)
|
||||
{
|
||||
size_t i;
|
||||
@@ -2750,21 +2678,6 @@ int tls_ctx_set_key_update_seq_num_limit(TLS_CTX *ctx, size_t max_seq_num)
|
||||
}
|
||||
|
||||
|
||||
int tls13_ctx_set_client_hello_key_exchanges_cnt(TLS_CTX *ctx, size_t cnt)
|
||||
{
|
||||
if (!ctx) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (cnt > sizeof(((TLS_CONNECT *)NULL)->key_exchanges)/sizeof(((TLS_CONNECT *)NULL)->key_exchanges[0])) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
ctx->key_exchanges_cnt = cnt;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2773,13 +2686,11 @@ int tls13_ctx_set_client_hello_key_exchanges_cnt(TLS_CTX *ctx, size_t cnt)
|
||||
int tls_init(TLS_CONNECT *conn, TLS_CTX *ctx)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
|
||||
memset(conn, 0, sizeof(*conn));
|
||||
|
||||
|
||||
|
||||
|
||||
//conn->is_client = ctx->is_client;
|
||||
|
||||
conn->protocol = ctx->protocol;
|
||||
|
||||
/*
|
||||
@@ -2835,7 +2746,6 @@ int tls_init(TLS_CONNECT *conn, TLS_CTX *ctx)
|
||||
|
||||
fprintf(stderr, "%s %d: conn->key_exchange_modes = %d\n", __FILE__, __LINE__, conn->key_exchange_modes);
|
||||
|
||||
|
||||
if (conn->key_exchange_modes & (TLS_KE_CERT_DHE|TLS_KE_PSK_DHE)) {
|
||||
conn->key_share = 1;
|
||||
}
|
||||
@@ -2846,7 +2756,6 @@ int tls_init(TLS_CONNECT *conn, TLS_CTX *ctx)
|
||||
conn->early_data = ctx->early_data;
|
||||
conn->max_early_data_size = ctx->max_early_data_size;
|
||||
|
||||
|
||||
// pre_shared_key
|
||||
if (conn->key_exchange_modes & (TLS_KE_PSK_DHE|TLS_KE_PSK)) {
|
||||
conn->pre_shared_key = 1;
|
||||
@@ -2861,18 +2770,6 @@ void tls_cleanup(TLS_CONNECT *conn)
|
||||
gmssl_secure_clear(conn, sizeof(TLS_CONNECT));
|
||||
}
|
||||
|
||||
/*
|
||||
int tls_set_hostname(TLS_CONNECT *conn, const char *hostname)
|
||||
{
|
||||
if (strlen(hostname) > 255) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
conn->hostname = hostname;
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
int tls_set_socket(TLS_CONNECT *conn, tls_socket_t sock)
|
||||
{
|
||||
#ifdef WIN32
|
||||
@@ -2964,7 +2861,6 @@ int tls_uint16array_from_file(uint8_t *arr, size_t *arrlen, size_t maxlen, FILE
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int tls_key_exchange_modes_print(FILE *fp, int fmt, int ind, const char *label, int modes)
|
||||
{
|
||||
int first = 1;
|
||||
@@ -2990,4 +2886,3 @@ int tls_key_exchange_modes_print(FILE *fp, int fmt, int ind, const char *label,
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
262
src/tls13.c
262
src/tls13.c
@@ -1559,7 +1559,6 @@ int tls13_client_supported_versions_print(FILE *fp, int fmt, int ind,
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
struct {
|
||||
ProtocolVersion selected_version;
|
||||
@@ -2091,14 +2090,6 @@ int tls13_key_share_hello_retry_request_print(FILE *fp, int fmt, int ind,
|
||||
}
|
||||
|
||||
|
||||
static const unsigned char TLS13_HELLO_RETRY_REQUEST_RANDOM[32] = {
|
||||
0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
|
||||
0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
|
||||
0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
|
||||
0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C
|
||||
};
|
||||
|
||||
|
||||
// Handshakes
|
||||
// ClientHello
|
||||
// ServerHello
|
||||
@@ -2128,6 +2119,21 @@ opaque Random[32];
|
||||
uint8 CipherSuite[2];
|
||||
*/
|
||||
|
||||
int tls13_ctx_set_client_hello_key_exchanges_cnt(TLS_CTX *ctx, size_t cnt)
|
||||
{
|
||||
if (!ctx) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (cnt > sizeof(((TLS_CONNECT *)NULL)->key_exchanges)/sizeof(((TLS_CONNECT *)NULL)->key_exchanges[0])) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
ctx->key_exchanges_cnt = cnt;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls13_client_hello_print(FILE *fp, int fmt, int ind, const uint8_t *d, size_t dlen)
|
||||
{
|
||||
uint16_t protocol;
|
||||
@@ -2276,6 +2282,14 @@ int tls13_client_hello_print(FILE *fp, int fmt, int ind, const uint8_t *d, size_
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ServerHello.random if HelloRetryReqeust
|
||||
static const unsigned char TLS13_HELLO_RETRY_REQUEST_RANDOM[32] = {
|
||||
0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
|
||||
0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
|
||||
0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
|
||||
0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C
|
||||
};
|
||||
|
||||
/*
|
||||
ServerHello
|
||||
|
||||
@@ -2973,7 +2987,7 @@ int tls13_record_get_handshake_certificate(const uint8_t *record,
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 客户端的证书链中的cert_list 可能为空
|
||||
// client Certificate.certificate_list might be empty/null
|
||||
if (!cert_list) {
|
||||
*cert_chain_len = 0;
|
||||
*entity_status_request_ocsp_response = NULL;
|
||||
@@ -3010,7 +3024,6 @@ int tls13_record_get_handshake_certificate(const uint8_t *record,
|
||||
}
|
||||
// 这里的解析可能是有问题的
|
||||
|
||||
|
||||
x509_cert_to_der(cert, certlen, &cert_chain, cert_chain_len);
|
||||
}
|
||||
|
||||
@@ -3107,9 +3120,6 @@ struct {
|
||||
opaque certificate_request_context<0..2^8-1>;
|
||||
Extension extensions<2..2^16-1>;
|
||||
} CertificateRequest;
|
||||
|
||||
extensiosns:
|
||||
signature_algorithms MUST be specified 这个应该放在代码中
|
||||
*/
|
||||
int tls13_record_set_handshake_certificate_request(uint8_t *record, size_t *recordlen,
|
||||
const uint8_t *request_context, size_t request_context_len,
|
||||
@@ -3123,6 +3133,12 @@ int tls13_record_set_handshake_certificate_request(uint8_t *record, size_t *reco
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (!exts || !extslen) {
|
||||
// signature_algorithms extension must exist
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
data = tls_handshake_data(tls_record_data(record));
|
||||
tls_uint8array_to_bytes(request_context, request_context_len, &data, &datalen);
|
||||
tls_uint16array_to_bytes(exts, extslen, &data, &datalen);
|
||||
@@ -3152,6 +3168,11 @@ int tls13_record_get_handshake_certificate_request(const uint8_t *record,
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (!exts) {
|
||||
// signature_algorithms extension must exist
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -3195,13 +3216,13 @@ int tls13_certificate_request_print(FILE *fp, int fmt, int ind, const uint8_t *d
|
||||
tls13_certificate_authorities_print(fp, fmt, ind, ext_data, ext_datalen);
|
||||
break;
|
||||
case TLS_extension_status_request:
|
||||
//tls13_status_request_print(fp, fmt, ind, ext_data, ext_datalen);
|
||||
//tls13_status_request_print(fp, fmt, ind, ext_data, ext_datalen);
|
||||
break;
|
||||
case TLS_extension_signature_algorithms_cert:
|
||||
tls_signature_algorithms_print(fp, fmt, ind, ext_data, ext_datalen);
|
||||
break;
|
||||
case TLS_extension_client_certificate_type:
|
||||
//tls13_client_certificate_type_print(fp, fmt, ind, ext_data, ext_datalen);
|
||||
//tls13_client_certificate_type_print(fp, fmt, ind, ext_data, ext_datalen);
|
||||
break;
|
||||
default:
|
||||
error_print();
|
||||
@@ -3215,6 +3236,16 @@ int tls13_certificate_request_print(FILE *fp, int fmt, int ind, const uint8_t *d
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls13_ctx_enable_client_certificate_optional(TLS_CTX *ctx, int enable)
|
||||
{
|
||||
if (!ctx) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
ctx->client_certificate_optional = enable ? 1 : 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
CertificateVerify
|
||||
|
||||
@@ -3306,7 +3337,6 @@ struct {
|
||||
} Finished;
|
||||
*/
|
||||
|
||||
|
||||
int tls13_record_set_handshake_finished(uint8_t *record, size_t *recordlen,
|
||||
const uint8_t *verify_data, size_t verify_data_len)
|
||||
{
|
||||
@@ -3367,13 +3397,13 @@ enum {
|
||||
*/
|
||||
|
||||
int tls13_record_set_handshake_key_update(uint8_t *record, size_t *recordlen,
|
||||
int request_update)
|
||||
int req_update)
|
||||
{
|
||||
int type = TLS_handshake_key_update;
|
||||
uint8_t data[1]; // 这个值不太好
|
||||
uint8_t request_update;
|
||||
|
||||
data[0] = request_update ? 1 : 0;
|
||||
if (tls_record_set_handshake(record, recordlen, type, data, sizeof(data)) != 1) {
|
||||
request_update = req_update ? 1 : 0;
|
||||
if (tls_record_set_handshake(record, recordlen, type, &request_update, sizeof(request_update)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -3433,6 +3463,18 @@ int tls13_key_update_print(FILE *fp, int fmt, int ind, const uint8_t *d, size_t
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ChangeCipherSpec
|
||||
|
||||
int tls13_ctx_enable_change_cipher_spec(TLS_CTX *ctx, int enable)
|
||||
{
|
||||
if (!ctx) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
ctx->change_cipher_spec = enable ? 1 : 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int tls13_handshake_print(FILE *fp, int fmt, int ind, const uint8_t *handshake, size_t handshake_len)
|
||||
@@ -3521,12 +3563,7 @@ int tls13_record_print(FILE *fp, int format, int indent, const uint8_t *record,
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 最高字节设置后强制打印记录原始数据
|
||||
if (format >> 24) {
|
||||
format_bytes(fp, format, indent, "Data", data, datalen);
|
||||
fprintf(fp, "\n");
|
||||
return 1;
|
||||
}
|
||||
//format_bytes(fp, format, indent, "RecordRawData", data, datalen);
|
||||
|
||||
switch (record[0]) {
|
||||
case TLS_record_handshake:
|
||||
@@ -3568,13 +3605,6 @@ int tls13_record_print(FILE *fp, int format, int indent, const uint8_t *record,
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
Client Server
|
||||
|
||||
@@ -3623,9 +3653,6 @@ Auth | {CertificateVerify*}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int tls13_init(TLS_CONNECT *conn, TLS_CTX *ctx)
|
||||
{
|
||||
size_t i;
|
||||
@@ -3723,29 +3750,6 @@ int tls13_init(TLS_CONNECT *conn, TLS_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
ClientHello中的很多扩展是和证书有关的
|
||||
如果客户端在ClientHello中已经明确不支持证书认证(没有提供group + sig_alg的组合)
|
||||
那么就不应该在ClientHello中包含这些扩展
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
1. 客户端发送 ClientHello,包含 early_data 扩展
|
||||
2. 客户端设置 cipher_suite 和 early_secret
|
||||
3. 客户端发送 {EarlyData}
|
||||
4. 客户端发送 {EndOfEarlyData}
|
||||
5. 客户端接收 ServerHello
|
||||
6. 客户端设置 handshake_secret
|
||||
7. 客户端接收 {EncryptedExtensions}
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
int tls13_send_client_hello(TLS_CONNECT *conn)
|
||||
{
|
||||
int ret;
|
||||
@@ -6106,9 +6110,6 @@ int tls13_recv_client_certificate_verify(TLS_CONNECT *conn)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// ServerFinished消息之前有可能是什么消息?
|
||||
|
||||
int tls13_recv_server_finished(TLS_CONNECT *conn)
|
||||
{
|
||||
int ret;
|
||||
@@ -6181,8 +6182,6 @@ int tls13_recv_server_finished(TLS_CONNECT *conn)
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 需要验证,当cipher_suite为AES,服务器证书为P-256,客户端证书为SM2的情况
|
||||
|
||||
int tls13_send_client_certificate(TLS_CONNECT *conn)
|
||||
{
|
||||
int ret;
|
||||
@@ -6467,7 +6466,6 @@ int tls13_recv_client_hello(TLS_CONNECT *conn)
|
||||
tls_client_verify_init(&conn->client_verify_ctx);
|
||||
*/
|
||||
|
||||
|
||||
tls_trace("recv ClientHello\n");
|
||||
|
||||
if ((ret = tls_recv_record(conn)) != 1) {
|
||||
@@ -8082,7 +8080,6 @@ int tls13_send_server_finished(TLS_CONNECT *conn)
|
||||
|
||||
tls_trace("send server {Finished}\n");
|
||||
|
||||
|
||||
if (conn->recordlen == 0) {
|
||||
uint8_t verify_data[64];
|
||||
size_t verify_data_len;
|
||||
@@ -8116,38 +8113,9 @@ int tls13_send_server_finished(TLS_CONNECT *conn)
|
||||
}
|
||||
tls_seq_num_incr(conn->server_seq_num);
|
||||
|
||||
// 必须在这里生成client_application_traffic_secret,否则dgst_ctx就变了
|
||||
// 但是必须在ClientFinished之后再更新密钥
|
||||
// 因此application_secrets是可以同时生成的
|
||||
|
||||
|
||||
tls13_generate_application_secrets(conn);
|
||||
|
||||
// Generate client_application_traffic_secret
|
||||
///* 11 */ tls13_derive_secret(conn->master_secret, "c ap traffic", &conn->dgst_ctx, conn->client_application_traffic_secret);
|
||||
// generate server_application_traffic_secret
|
||||
///* 12 */ tls13_derive_secret(conn->master_secret, "s ap traffic", &conn->dgst_ctx, conn->server_application_traffic_secret);
|
||||
|
||||
|
||||
// 这里只生成服务器端的密钥
|
||||
|
||||
tls13_generate_server_application_keys(conn);
|
||||
|
||||
// update server_write_key, server_write_iv, reset server_seq_num
|
||||
/*
|
||||
tls13_hkdf_expand_label(conn->digest, conn->server_application_traffic_secret, "key", NULL, 0, 16, server_write_key);
|
||||
block_cipher_set_encrypt_key(&conn->server_write_key, conn->cipher, server_write_key);
|
||||
tls13_hkdf_expand_label(conn->digest, conn->server_application_traffic_secret, "iv", NULL, 0, 12, conn->server_write_iv);
|
||||
tls_seq_num_reset(conn->server_seq_num);
|
||||
|
||||
format_print(stderr, 0, 0, "update server secrets\n");
|
||||
format_bytes(stderr, 0, 4, "server_application_traffic_secret", conn->server_application_traffic_secret, 32);
|
||||
format_bytes(stderr, 0, 4, "server_write_key", server_write_key, 16);
|
||||
format_bytes(stderr, 0, 4, "server_write_iv", conn->server_write_iv, 12);
|
||||
format_bytes(stderr, 0, 4, "server_seq_num", conn->server_seq_num, 8);
|
||||
format_print(stderr, 0, 0, "\n");
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
if ((ret = tls_send_record(conn)) != 1) {
|
||||
@@ -8374,22 +8342,8 @@ int tls13_recv_client_finished(TLS_CONNECT *conn)
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
tls13_generate_client_application_keys(conn);
|
||||
|
||||
/*
|
||||
// update client_write_key, client_write_iv, reset client_seq_num
|
||||
tls13_hkdf_expand_label(conn->digest, conn->client_application_traffic_secret, "key", NULL, 0, 16, client_write_key);
|
||||
block_cipher_set_encrypt_key(&conn->client_write_key, conn->cipher, client_write_key);
|
||||
tls13_hkdf_expand_label(conn->digest, conn->client_application_traffic_secret, "iv", NULL, 0, 12, conn->client_write_iv);
|
||||
tls_seq_num_reset(conn->client_seq_num);
|
||||
|
||||
format_print(stderr, 0, 0, "update client secrets\n");
|
||||
format_bytes(stderr, 0, 4, "client_write_key", client_write_key, 16);
|
||||
format_bytes(stderr, 0, 4, "client_write_iv", conn->client_write_iv, 12);
|
||||
format_print(stderr, 0, 0, "\n");
|
||||
*/
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -8425,25 +8379,6 @@ int tls13_send_early_data(TLS_CONNECT *conn)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 参数request_update应该根据当前状态设置
|
||||
|
||||
|
||||
/*
|
||||
如果我方是首先发送KeyUpdate一方,那么默认设置要求对方也更新密钥
|
||||
|
||||
|
||||
如果我们接收到对方的要求,并且满足我方的一个最低限度,那么就设置key_update的标志位,下次send的时候就会启动更新
|
||||
|
||||
*/
|
||||
|
||||
int tls13_send_client_key_update(TLS_CONNECT *conn, int request_update)
|
||||
{
|
||||
int ret;
|
||||
@@ -8498,31 +8433,6 @@ int tls13_send_client_key_update(TLS_CONNECT *conn, int request_update)
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
while (conn->recordlen) {
|
||||
tls_ret_t n;
|
||||
|
||||
if ((n = tls_socket_send(conn->sock, conn->record + conn->record_offset, conn->recordlen, 0)) <= 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
return TLS_ERROR_SEND_AGAIN;
|
||||
} else {
|
||||
if (n == 0) {
|
||||
error_puts("TCP connection closed");
|
||||
}
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
conn->recordlen -= n;
|
||||
conn->record_offset += n;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls13_send_server_key_update(TLS_CONNECT *conn, int request_update)
|
||||
@@ -8579,50 +8489,6 @@ int tls13_send_server_key_update(TLS_CONNECT *conn, int request_update)
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Post-Handshake-Auth
|
||||
|
||||
客户端在 ClientHello 中发送post_handshake_auht说明支持PHA
|
||||
|
||||
在握手完成并进行数据通信时,如果客户端请求一个特殊的地址 比如 GET /admin
|
||||
默认未认证的客户端不能访问此地址,因此服务器端如果发现客户端支持PHA,那么就会给客户端发送一个CertificateRequest
|
||||
|
||||
客户端不需要立即响应,但是如果客户端响应
|
||||
则需要发出连续的 Certificate, CertificateVerify, Finished消息
|
||||
|
||||
|
||||
|
||||
这意味着如果客户端开始发送Certificate
|
||||
服务器就必须再次进入到握手的状态机中
|
||||
这意味着SSL再次进入到状态机中
|
||||
|
||||
|
||||
是否意味着每次不论上层调用是read/write,SSL都是按照状态机来处理
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int tls13_do_client_handshake(TLS_CONNECT *conn)
|
||||
{
|
||||
|
||||
@@ -23,12 +23,9 @@
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/tls.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
/*
|
||||
cookie
|
||||
44. cookie
|
||||
* server HelloRetryReqeust
|
||||
* client ClientHello (again)
|
||||
|
||||
|
||||
@@ -419,7 +419,7 @@ int tls_signature_algorithms_print(FILE *fp, int fmt, int ind, const uint8_t *d,
|
||||
}
|
||||
|
||||
|
||||
int tls_enable_signature_algorithms_cert(TLS_CONNECT *conn)
|
||||
int tls_enable_signature_algorithms_cert(TLS_CONNECT *conn, int enable)
|
||||
{
|
||||
if (!conn) {
|
||||
error_print();
|
||||
@@ -429,7 +429,7 @@ int tls_enable_signature_algorithms_cert(TLS_CONNECT *conn)
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
conn->signature_algorithms_cert = 1;
|
||||
conn->signature_algorithms_cert = enable ? 1 : 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
|
||||
/*
|
||||
status_request(5)
|
||||
5. status_request
|
||||
|
||||
ClientHello.status_request
|
||||
ext_data := CertificateStatusRequest;
|
||||
@@ -326,6 +326,7 @@ int tls13_set_client_status_request(TLS_CONNECT *conn,
|
||||
int ocsp_response_verify(const uint8_t *ocsp_response, size_t ocsp_response_len,
|
||||
const uint8_t *ca_certs, size_t ca_certs_len)
|
||||
{
|
||||
error_print();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -334,7 +335,6 @@ int tls_ocsp_response_match_status_request(
|
||||
const uint8_t *responder_id_list, size_t responder_id_list_len,
|
||||
const uint8_t *request_exts, size_t request_exts_len)
|
||||
{
|
||||
error_print();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -23,21 +23,15 @@
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/tls.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
|
||||
|
||||
/*
|
||||
psk_key_exchange_modes
|
||||
45. psk_key_exchange_modes
|
||||
|
||||
enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
|
||||
|
||||
struct {
|
||||
PskKeyExchangeMode ke_modes<1..255>;
|
||||
} PskKeyExchangeModes;
|
||||
|
||||
*/
|
||||
|
||||
const char *tls13_psk_key_exchange_mode_name(int mode)
|
||||
@@ -144,48 +138,6 @@ int tls13_ctx_set_psk_key_exchange_modes(TLS_CTX *ctx, int psk_ke, int psk_dhe_k
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
PSK 功能的关系
|
||||
|
||||
tls13_ctx_set_session_ticket_key
|
||||
1. 服务器设定ticket_key
|
||||
|
||||
2. 服务器设定发送NewSessionTicket,并且设定数量
|
||||
|
||||
3. 服务器 send_new_session_ticket
|
||||
|
||||
4. 客户端 recv_new_session_ticket
|
||||
|
||||
5. 客户端将session保存到文件中
|
||||
|
||||
6. 客户端载入session
|
||||
|
||||
7. 客户端发送 pre_shared_key, 其中 ticket 来自session
|
||||
|
||||
8. 服务器获取 pre_shared_key, 解密ticket,得到PSK
|
||||
|
||||
9. 服务器发送 pre_shared_key, 告知客户端选定的密钥编号
|
||||
|
||||
|
||||
|
||||
基于外部预置PSK
|
||||
|
||||
1. 服务器添加PSK
|
||||
|
||||
2. 客户端添加PSK
|
||||
|
||||
3. 客户端发送pre_shared_key
|
||||
|
||||
4. 服务器用PSK去pre_shared_key查找是否有满足的
|
||||
|
||||
5. 服务器发送pre_shared_key,告知客户端选定的密钥编号
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
session_ticket_key
|
||||
|
||||
@@ -1719,18 +1671,16 @@ int tls13_set_early_data(TLS_CONNECT *conn, const uint8_t *data, size_t datalen)
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 同时影响客户端和服务器吗?
|
||||
int tls13_enable_early_data(TLS_CONNECT *conn, int enable)
|
||||
int tls13_ctx_enable_early_data(TLS_CTX *ctx, int enable)
|
||||
{
|
||||
if (!conn) {
|
||||
if (!ctx) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
conn->early_data = enable ? 1 : 0;
|
||||
ctx->early_data = enable ? 1 : 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int tls13_ctx_set_max_early_data_size(TLS_CTX *ctx, size_t max_early_data_size)
|
||||
{
|
||||
if (!ctx) {
|
||||
@@ -1865,7 +1815,6 @@ int tls13_recv_end_of_early_data(TLS_CONNECT *conn)
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
format_bytes(stderr, 0, 4, "client_write_iv", conn->client_write_iv, 12);
|
||||
|
||||
if (tls13_record_decrypt(&conn->client_write_key, conn->client_write_iv,
|
||||
@@ -1903,5 +1852,3 @@ int tls13_recv_end_of_early_data(TLS_CONNECT *conn)
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -23,18 +23,14 @@
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/tls.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
|
||||
/*
|
||||
18. signed_certificate_timestamp
|
||||
|
||||
|
||||
ClientHello.exts.signed_certificate_timestamp
|
||||
ext_data := empty
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
Certificate.certificate_list.CertificateEntry.exts.signed_certificate_timestamp
|
||||
ext_data := SignedCertificateTimestamp sct_list<0..2^16-1>;
|
||||
struct {
|
||||
@@ -142,33 +138,28 @@ int tls_signed_certificate_timestamp_print(FILE *fp, int fmt, int ind, const uin
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 这个应该改为enable, 服务器也可以设定是否响应这个请求
|
||||
int tls_enable_signed_certificate_timestamp(TLS_CONNECT *conn)
|
||||
int tls_enable_signed_certificate_timestamp(TLS_CONNECT *conn, int enable)
|
||||
{
|
||||
if (!conn) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
conn->signed_certificate_timestamp = 1;
|
||||
conn->signed_certificate_timestamp = enable ? 1 : 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_ctx_enable_signed_certificate_timestamp(TLS_CTX *ctx)
|
||||
int tls_ctx_enable_signed_certificate_timestamp(TLS_CTX *ctx, int enable)
|
||||
{
|
||||
if (!ctx) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
ctx->signed_certificate_timestamp = 1;
|
||||
ctx->signed_certificate_timestamp = enable ? 1 : 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int tls13_signed_certificate_timestamp_verify(const uint8_t *sct_list, size_t sct_list_len)
|
||||
{
|
||||
//error_print();
|
||||
error_print();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -23,12 +23,9 @@
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/tls.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
/*
|
||||
server_name (SNI)
|
||||
0. server_name (SNI)
|
||||
|
||||
ClientHello.server_name
|
||||
ext_data = ServerName server_name_list<1..2^16-1>
|
||||
@@ -111,34 +108,6 @@ int tls_server_name_from_bytes(const uint8_t **host_name, size_t *host_name_len,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_server_name_print(FILE *fp, int fmt, int ind, const uint8_t *ext_data, size_t ext_datalen)
|
||||
{
|
||||
const uint8_t *server_name_list;
|
||||
size_t server_name_list_len;
|
||||
uint8_t name_type;
|
||||
const uint8_t *host_name;
|
||||
size_t host_name_len;
|
||||
|
||||
if (tls_uint16array_from_bytes(&server_name_list, &server_name_list_len, &ext_data, &ext_datalen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
while (server_name_list_len) {
|
||||
if (tls_uint8_from_bytes(&name_type, &server_name_list, &server_name_list_len) != 1
|
||||
|| tls_uint16array_from_bytes(&host_name, &host_name_len, &server_name_list, &server_name_list_len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(fp, fmt, ind, "name_type: %s (%d)\n", name_type == 0 ? "host_name" : "(unknown)", name_type);
|
||||
format_string(fp, fmt, ind, "host_name", host_name, host_name_len); // TODO: print string
|
||||
}
|
||||
if (ext_datalen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_set_server_name(TLS_CONNECT *conn, const uint8_t *host_name, size_t host_name_len)
|
||||
{
|
||||
if (!conn || !host_name || !host_name_len) {
|
||||
@@ -160,4 +129,30 @@ int tls_set_server_name(TLS_CONNECT *conn, const uint8_t *host_name, size_t host
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_server_name_print(FILE *fp, int fmt, int ind, const uint8_t *ext_data, size_t ext_datalen)
|
||||
{
|
||||
const uint8_t *server_name_list;
|
||||
size_t server_name_list_len;
|
||||
uint8_t name_type;
|
||||
const uint8_t *host_name;
|
||||
size_t host_name_len;
|
||||
|
||||
if (tls_uint16array_from_bytes(&server_name_list, &server_name_list_len, &ext_data, &ext_datalen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
while (server_name_list_len) {
|
||||
if (tls_uint8_from_bytes(&name_type, &server_name_list, &server_name_list_len) != 1
|
||||
|| tls_uint16array_from_bytes(&host_name, &host_name_len, &server_name_list, &server_name_list_len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(fp, fmt, ind, "name_type: %s (%d)\n", name_type == 0 ? "host_name" : "(unknown)", name_type);
|
||||
format_string(fp, fmt, ind, "host_name", host_name, host_name_len);
|
||||
}
|
||||
if (ext_datalen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user