mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-19 11:23:38 +08:00
Update TLS1.2
This commit is contained in:
@@ -200,7 +200,7 @@ typedef enum {
|
||||
TLS_extension_signature_algorithms = 13,
|
||||
TLS_extension_use_srtp = 14,
|
||||
TLS_extension_heartbeat = 15,
|
||||
TLS_extension_application_layer_protocol_negotiation= 16,
|
||||
TLS_extension_application_layer_protocol_negotiation = 16,
|
||||
TLS_extension_status_request_v2 = 17,
|
||||
TLS_extension_signed_certificate_timestamp = 18,
|
||||
TLS_extension_client_certificate_type = 19,
|
||||
@@ -240,6 +240,7 @@ typedef enum {
|
||||
TLS_extension_external_session_id = 56,
|
||||
TLS_extension_quic_transport_parameters = 57,
|
||||
TLS_extension_ticket_request = 58,
|
||||
TLS_extension_client_id = 66, // TLCP (GM/T 0024-2023) only
|
||||
TLS_extension_renegotiation_info = 65281,
|
||||
} TLS_EXTENSION_TYPE;
|
||||
|
||||
@@ -751,6 +752,7 @@ typedef struct {
|
||||
int is_client;
|
||||
|
||||
int quiet;
|
||||
int verbose;
|
||||
|
||||
int protocol;
|
||||
|
||||
@@ -813,6 +815,9 @@ typedef struct {
|
||||
// 0. server_name (SNI)
|
||||
// server_name is connection only
|
||||
|
||||
// 3. trusted_ca_keys
|
||||
int trusted_ca_keys;
|
||||
|
||||
// 5. status_request
|
||||
int status_request; // if send in ClientHello, CertificateRequest
|
||||
// list of (uint24array)CertificateEntry.extensions.status_request.response
|
||||
@@ -828,6 +833,9 @@ typedef struct {
|
||||
int signature_algorithms[2];
|
||||
size_t signature_algorithms_cnt;
|
||||
|
||||
// 16. application_layer_protocol_negotiation
|
||||
int application_layer_protocol_negotiation;
|
||||
|
||||
// 18. signed_certificate_timestamp
|
||||
int signed_certificate_timestamp;
|
||||
uint8_t signed_certificate_timestamp_lists[512]; // list of uint16array
|
||||
@@ -874,6 +882,9 @@ typedef struct {
|
||||
// 51. key_share
|
||||
size_t key_exchanges_cnt;
|
||||
|
||||
// 66. client_id (TLCP only)
|
||||
int client_id;
|
||||
|
||||
} TLS_CTX;
|
||||
|
||||
|
||||
@@ -898,6 +909,9 @@ int tls_ctx_add_certificate_list_and_key(TLS_CTX *ctx, const char *chainfile,
|
||||
const char *keyfile, const char *keypass);
|
||||
|
||||
|
||||
int tls_ctx_enable_verbose(TLS_CTX *ctx, int enable);
|
||||
|
||||
|
||||
// KeyUpdate
|
||||
#define TLS13_DEFAULT_KEY_UPDATE_SEQ_NUM_LIMIT (1 << 20)
|
||||
int tls13_ctx_enable_key_update(TLS_CTX *ctx, int enable);
|
||||
@@ -1116,6 +1130,9 @@ typedef struct {
|
||||
size_t host_name_len;
|
||||
// EncryptedExtensions.server_name (emtpy)
|
||||
|
||||
// 3. trusted_ca_keys
|
||||
int trusted_ca_keys;
|
||||
|
||||
// 5. status_request
|
||||
int status_request;
|
||||
// ClientHello.status_request
|
||||
@@ -1140,6 +1157,9 @@ typedef struct {
|
||||
int signature_algorithms[2];
|
||||
size_t signature_algorithms_cnt;
|
||||
|
||||
// 16. application_layer_protocol_negotiation
|
||||
int application_layer_protocol_negotiation;
|
||||
|
||||
// 18. signed_certificate_timestamp
|
||||
int signed_certificate_timestamp;
|
||||
// ClientHello.signed_certificate_timestamp (empty)
|
||||
@@ -1216,6 +1236,9 @@ typedef struct {
|
||||
uint8_t peer_key_exchange[65]; //这个似乎应该替换掉
|
||||
size_t peer_key_exchange_len;
|
||||
|
||||
// 66. client_id (TLCP only)
|
||||
int client_id;
|
||||
|
||||
} TLS_CONNECT;
|
||||
|
||||
|
||||
@@ -1356,6 +1379,10 @@ int tls13_ctx_enable_change_cipher_spec(TLS_CTX *ctx, int enable);
|
||||
|
||||
int tls_generate_keys(TLS_CONNECT *conn);
|
||||
|
||||
int tls_compute_verify_data(const uint8_t master_secret[48],
|
||||
const char *label, const DIGEST_CTX *dgst_ctx, uint8_t verify_data[12]);
|
||||
|
||||
|
||||
int tls13_update_client_application_keys(TLS_CONNECT *conn);
|
||||
int tls13_update_server_application_keys(TLS_CONNECT *conn);
|
||||
|
||||
@@ -1980,6 +2007,8 @@ int tls13_key_share_server_hello_print(FILE *fp, int fmt, int ind,
|
||||
|
||||
int tls13_ctx_set_max_key_exchanges(TLS_CTX *ctx, size_t cnt);
|
||||
|
||||
int tls_handshake_digest_print(FILE *fp, int fmt, int ind, const char *label, const DIGEST_CTX *dgst_ctx);
|
||||
|
||||
#define TLS_DEFAULT_KEY_EXCHANGES_CNT 1
|
||||
|
||||
|
||||
|
||||
1515
src/tlcp.c
1515
src/tlcp.c
File diff suppressed because it is too large
Load Diff
80
src/tls.c
80
src/tls.c
@@ -1042,6 +1042,7 @@ int tls_record_set_handshake_certificate(uint8_t *record, size_t *recordlen,
|
||||
return 1;
|
||||
}
|
||||
|
||||
// FIXME: 这个函数没有提供缓冲区的长度限制
|
||||
int tls_record_get_handshake_certificate(const uint8_t *record, uint8_t *certs, size_t *certslen)
|
||||
{
|
||||
int type;
|
||||
@@ -2307,6 +2308,16 @@ int tls_ctx_set_ca_certificates(TLS_CTX *ctx, const char *cacertsfile, int depth
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_ctx_enable_verbose(TLS_CTX *ctx, int enable)
|
||||
{
|
||||
if (!ctx) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
ctx->verbose = enable ? 1 : 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_ctx_enable_certificate_request(TLS_CTX *ctx, int enable)
|
||||
{
|
||||
if (!ctx) {
|
||||
@@ -2677,12 +2688,6 @@ int tls_init(TLS_CONNECT *conn, TLS_CTX *ctx)
|
||||
conn->is_client = ctx->is_client; // TODO: remove conn->is_client
|
||||
conn->protocol = ctx->protocol;
|
||||
|
||||
/*
|
||||
for (i = 0; i < ctx->cipher_suites_cnt; i++) {
|
||||
conn->cipher_suites[i] = ctx->cipher_suites[i];
|
||||
}
|
||||
conn->cipher_suites_cnt = ctx->cipher_suites_cnt;
|
||||
*/
|
||||
|
||||
if (ctx->certslen > TLS_MAX_CERTIFICATES_SIZE) {
|
||||
error_print();
|
||||
@@ -2696,14 +2701,6 @@ int tls_init(TLS_CONNECT *conn, TLS_CTX *ctx)
|
||||
conn->server_certs_len = ctx->certslen;
|
||||
}
|
||||
|
||||
/*
|
||||
if (ctx->cacertslen > TLS_MAX_CERTIFICATES_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memcpy(conn->ca_certs, ctx->cacerts, ctx->cacertslen);
|
||||
conn->ca_certs_len = ctx->cacertslen;
|
||||
*/
|
||||
|
||||
conn->sign_key = ctx->signkey;
|
||||
conn->kenc_key = ctx->kenckey;
|
||||
@@ -2733,6 +2730,10 @@ int tls_init(TLS_CONNECT *conn, TLS_CTX *ctx)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (ctx->protocol == TLS_protocol_tlcp) {
|
||||
sm3_init(&conn->sm3_ctx);
|
||||
}
|
||||
|
||||
|
||||
conn->signed_certificate_timestamp = ctx->signed_certificate_timestamp;
|
||||
@@ -2869,3 +2870,54 @@ int tls_key_exchange_modes_print(FILE *fp, int fmt, int ind, const char *label,
|
||||
fprintf(fp, "\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int tls_handshake_digest_print(FILE *fp, int fmt, int ind, const char *label, const DIGEST_CTX *dgst_ctx)
|
||||
{
|
||||
DIGEST_CTX tmp_ctx;
|
||||
uint8_t dgst[64];
|
||||
size_t dgstlen;
|
||||
|
||||
tmp_ctx = *dgst_ctx;
|
||||
|
||||
if (digest_finish(&tmp_ctx, dgst, &dgstlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
format_print(fp, fmt, ind, "transcript_hash ");
|
||||
format_bytes(fp, 0, 0, label, dgst, dgstlen);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_compute_verify_data(const uint8_t master_secret[48],
|
||||
const char *label, const DIGEST_CTX *dgst_ctx, uint8_t verify_data[12])
|
||||
{
|
||||
const size_t master_secret_len = 48;
|
||||
const size_t verify_data_len = 12;
|
||||
DIGEST_CTX tmp_ctx;
|
||||
uint8_t dgst[64];
|
||||
size_t dgstlen;
|
||||
|
||||
if (!master_secret || !dgst_ctx || !verify_data) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
tmp_ctx = *dgst_ctx;
|
||||
|
||||
if (digest_finish(&tmp_ctx, dgst, &dgstlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (tls_prf(master_secret, master_secret_len,
|
||||
label, // "client finished" or "server finished",
|
||||
dgst, dgstlen, NULL, 0,
|
||||
verify_data_len, verify_data) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
10
src/tls13.c
10
src/tls13.c
@@ -372,18 +372,8 @@ int tls13_derive_secret(const uint8_t secret[32], const char *label, const DIGES
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_handshake_digest_print(FILE *fp, int fmt, int ind, const char *label, const DIGEST_CTX *dgst_ctx)
|
||||
{
|
||||
DIGEST_CTX tmp_ctx;
|
||||
|
||||
tmp_ctx = *dgst_ctx;
|
||||
|
||||
//digest_finish(&tmp_ctx, dgst, &dgstlen);
|
||||
//format_print(fp, fmt, ind, "Transcript-Hash ");
|
||||
//format_bytes(fp, 0, 0, label, dgst, dgstlen);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
@@ -19,8 +19,6 @@
|
||||
|
||||
#define TIMEOUT_SECONDS 1
|
||||
|
||||
static int client_ciphers[] = { TLS_cipher_ecc_sm4_cbc_sm3, };
|
||||
|
||||
static const char *usage =
|
||||
"-host str [-port num] [-cacert file]"
|
||||
" [-cert file -key file -pass str]"
|
||||
@@ -33,12 +31,19 @@ static const char *help =
|
||||
"\n"
|
||||
" -host str Domain name or IP address of remote host\n"
|
||||
" -port num Port number of remote host, default 443\n"
|
||||
" -cipher_suite str Supported cipher suites, may appear multiple times, higher priority first\n"
|
||||
" -supported_group str Supported elliptic curves, may appear multiple times, higher priority first\n"
|
||||
" -sig_alg str Supported signature algorithms\n"
|
||||
" -cacert file Trusted CA certificate(s) in PEM format\n"
|
||||
" -verify_depth num Certificate verification depth\n"
|
||||
" -cert file Client certificate(s) in PEM format\n"
|
||||
" -key file Private key of client certificate\n"
|
||||
" -pass password Password of encrypted private key\n"
|
||||
" -client_cert_optional Allow client send empty Certificate\n"
|
||||
" -get path Send a GET request with given path of URI\n"
|
||||
" -outcerts file Save server certificates to a PEM file\n"
|
||||
" -server_name Send server_name (SNI) request\n"
|
||||
" -status_request Send status_request (OCSP Stapling) request\n"
|
||||
" -quiet Without printing any status message\n"
|
||||
"\n"
|
||||
#include "tlcp_help.h"
|
||||
@@ -51,10 +56,19 @@ int tlcp_client_main(int argc, char *argv[])
|
||||
char *prog = argv[0];
|
||||
char *host = NULL;
|
||||
int port = 443;
|
||||
|
||||
int cipher_suites[4];
|
||||
size_t cipher_suites_cnt = 0;
|
||||
int supported_groups[4];
|
||||
size_t supported_groups_cnt = 0;
|
||||
int sig_algs[4];
|
||||
size_t sig_algs_cnt = 0;
|
||||
char *cacertfile = NULL;
|
||||
int verify_depth = TLS_DEFAULT_VERIFY_DEPTH;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
int client_cert_optional = 0;
|
||||
char *get = NULL;
|
||||
char *outcertsfile = NULL;
|
||||
int quiet = 0;
|
||||
@@ -86,9 +100,59 @@ int tlcp_client_main(int argc, char *argv[])
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cipher_suite")) {
|
||||
char *cipher_suite_name;
|
||||
int cipher_suite;
|
||||
if (cipher_suites_cnt >= sizeof(cipher_suites)/sizeof(cipher_suites[0])) {
|
||||
fprintf(stderr, "%s: too many -cipher_suite options\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
cipher_suite_name = *(++argv);
|
||||
if ((cipher_suite = tls_cipher_suite_from_name(cipher_suite_name)) == 0) {
|
||||
fprintf(stderr, "%s: invalid -cipher_suite '%s' value\n", prog, cipher_suite_name);
|
||||
return -1;
|
||||
}
|
||||
cipher_suites[cipher_suites_cnt] = cipher_suite;
|
||||
cipher_suites_cnt++;
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-verify_depth")) {
|
||||
if (--argc < 1) goto bad;
|
||||
verify_depth = atoi(*(++argv));
|
||||
if (verify_depth < 1) {
|
||||
fprintf(stderr, "%s: invalid -verify_depth value '%d'\n", prog, verify_depth);
|
||||
return -1;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-supported_group")) {
|
||||
char *supported_group_name;
|
||||
int supported_group;
|
||||
if (supported_groups_cnt >= sizeof(supported_groups)/sizeof(supported_groups[0])) {
|
||||
fprintf(stderr, "%s: too many -supported_group options\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
supported_group_name = *(++argv);
|
||||
if ((supported_group = tls_named_curve_from_name(supported_group_name)) == 0) {
|
||||
fprintf(stderr, "%s: -supported_group '%s' not supported\n", prog, supported_group_name);
|
||||
return -1;
|
||||
}
|
||||
supported_groups[supported_groups_cnt++] = supported_group;
|
||||
} else if (!strcmp(*argv, "-sig_alg")) {
|
||||
char *sig_alg_name;
|
||||
int sig_alg;
|
||||
if (sig_algs_cnt >= sizeof(sig_algs)/sizeof(sig_algs[0])) {
|
||||
fprintf(stderr, "%s: too many -sig_alg options\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
sig_alg_name = *(++argv);
|
||||
if ((sig_alg = tls_signature_scheme_from_name(sig_alg_name)) == 0) {
|
||||
fprintf(stderr, "%s: -sig_alg '%s' not supported\n", prog, sig_alg_name);
|
||||
return -1;
|
||||
}
|
||||
sig_algs[sig_algs_cnt++] = sig_alg;
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
@@ -98,6 +162,8 @@ int tlcp_client_main(int argc, char *argv[])
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-client_cert_optional")) {
|
||||
client_cert_optional = 1;
|
||||
} else if (!strcmp(*argv, "-get")) {
|
||||
if (--argc < 1) goto bad;
|
||||
get = *(++argv);
|
||||
@@ -117,72 +183,101 @@ bad:
|
||||
argv++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!host) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
fprintf(stderr, "%s: option '-host' missing\n", prog);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_client_mode) != 1) {
|
||||
error_print();
|
||||
if (!cipher_suites_cnt) {
|
||||
fprintf(stderr, "%s: option '-cipher_suite' missing\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (tls_ctx_set_cipher_suites(&ctx, client_ciphers, sizeof(client_ciphers)/sizeof(client_ciphers[0])) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_socket_lib_init() != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_socket_create(&sock, AF_INET, SOCK_STREAM, 0) != 1) {
|
||||
fprintf(stderr, "%s: open socket error\n", prog);
|
||||
goto end;
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_client_mode) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(hp = gethostbyname(host))) {
|
||||
fprintf(stderr, "%s: invalid hostname '%s'\n", prog, host);
|
||||
goto end;
|
||||
}
|
||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(port);
|
||||
|
||||
if (tls_socket_connect(sock, &server) != 1) {
|
||||
fprintf(stderr, "%s: socket connect error\n", prog);
|
||||
if (tls_ctx_set_cipher_suites(&ctx, cipher_suites, cipher_suites_cnt) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, verify_depth) != 1) {
|
||||
fprintf(stderr, "%s: failed to load CA certificate\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (supported_groups_cnt > 0) {
|
||||
if (tls_ctx_set_supported_groups(&ctx, supported_groups, supported_groups_cnt) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (sig_algs_cnt > 0) {
|
||||
if (tls_ctx_set_signature_algorithms(&ctx, sig_algs, sig_algs_cnt) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (certfile) {
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: option '-key' should be assigned with '-cert'\n", prog);
|
||||
fprintf(stderr, "%s: option '-key' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: option '-pass' should be assigned with '-pass'\n", prog);
|
||||
fprintf(stderr, "%s: option '-pass' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
fprintf(stderr, "%s: failed to load client certificate\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (quiet) {
|
||||
ctx.quiet = 1;
|
||||
// quiet/verbose
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, sock) != 1
|
||||
|| tls_do_handshake(&conn) != 1) {
|
||||
if (tls_socket_create(&sock, AF_INET, SOCK_STREAM, 0) != 1) {
|
||||
fprintf(stderr, "%s: faild to open socket\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(hp = gethostbyname(host))) {
|
||||
fprintf(stderr, "%s: failed to parse host name\n", prog, host);
|
||||
goto end;
|
||||
}
|
||||
|
||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(port);
|
||||
|
||||
if (tls_socket_connect(sock, &server) != 1) {
|
||||
fprintf(stderr, "%s: failed to connect socket\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_set_socket(&conn, sock) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_do_handshake(&conn) != 1) {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user