mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-19 19:33:38 +08:00
Update TLS12
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/digest.h>
|
||||
#include <gmssl/hmac.h>
|
||||
#include <gmssl/block_cipher.h>
|
||||
#include <gmssl/socket.h>
|
||||
#include <gmssl/x509_key.h>
|
||||
@@ -1057,15 +1058,21 @@ typedef struct {
|
||||
|
||||
|
||||
// transcript hash
|
||||
SM3_CTX sm3_ctx;
|
||||
//SM3_CTX sm3_ctx;
|
||||
DIGEST_CTX dgst_ctx;
|
||||
|
||||
|
||||
// secrets
|
||||
SM3_HMAC_CTX client_write_mac_ctx;
|
||||
SM3_HMAC_CTX server_write_mac_ctx;
|
||||
HMAC_CTX client_write_mac_ctx;
|
||||
HMAC_CTX server_write_mac_ctx;
|
||||
|
||||
SM4_KEY client_write_enc_key;
|
||||
SM4_KEY server_write_enc_key;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
uint8_t client_seq_num[8];
|
||||
uint8_t server_seq_num[8];
|
||||
|
||||
|
||||
@@ -1909,7 +1909,7 @@ int tlcp_do_connect(TLS_CONNECT *conn)
|
||||
// 应该把protocol_version的初始化放在这里
|
||||
|
||||
conn->state = TLS_state_client_hello;
|
||||
sm3_init(&conn->sm3_ctx);
|
||||
//sm3_init(&conn->sm3_ctx);
|
||||
|
||||
while (1) {
|
||||
|
||||
@@ -1946,7 +1946,7 @@ int tlcp_do_accept(TLS_CONNECT *conn)
|
||||
|
||||
conn->state = TLS_state_client_hello;
|
||||
|
||||
sm3_init(&conn->sm3_ctx);
|
||||
//sm3_init(&conn->sm3_ctx);
|
||||
|
||||
while (1) {
|
||||
|
||||
|
||||
@@ -2732,7 +2732,7 @@ int tls_init(TLS_CONNECT *conn, TLS_CTX *ctx)
|
||||
}
|
||||
|
||||
if (ctx->protocol == TLS_protocol_tlcp) {
|
||||
sm3_init(&conn->sm3_ctx);
|
||||
//sm3_init(&conn->sm3_ctx);
|
||||
}
|
||||
|
||||
|
||||
|
||||
961
src/tls12.c
961
src/tls12.c
File diff suppressed because it is too large
Load Diff
@@ -8804,7 +8804,7 @@ int tls13_do_connect(TLS_CONNECT *conn)
|
||||
// 应该把protocol_version的初始化放在这里
|
||||
|
||||
conn->state = TLS_state_client_hello;
|
||||
sm3_init(&conn->sm3_ctx);
|
||||
//sm3_init(&conn->sm3_ctx);
|
||||
|
||||
while (1) {
|
||||
|
||||
@@ -8843,7 +8843,7 @@ int tls13_do_accept(TLS_CONNECT *conn)
|
||||
|
||||
conn->state = TLS_state_client_hello;
|
||||
|
||||
sm3_init(&conn->sm3_ctx);
|
||||
//sm3_init(&conn->sm3_ctx);
|
||||
|
||||
fprintf(stderr, "tls13_do_accept\n");
|
||||
|
||||
|
||||
@@ -626,10 +626,10 @@ int tls_client_hello_print(FILE *fp, const uint8_t *data, size_t datalen, int fo
|
||||
}
|
||||
if (datalen > 0) {
|
||||
if (tls_uint16array_from_bytes(&exts, &exts_len, &data, &datalen) != 1) goto end;
|
||||
format_print(fp, format, indent, "Extensions\n");
|
||||
indent += 4;
|
||||
tls_extensions_print(fp, exts, exts_len, format, indent);
|
||||
}
|
||||
// 打印扩展
|
||||
|
||||
/*
|
||||
while (exts_len > 0) {
|
||||
uint16_t ext_type;
|
||||
const uint8_t *ext_data;
|
||||
@@ -642,10 +642,9 @@ int tls_client_hello_print(FILE *fp, const uint8_t *data, size_t datalen, int fo
|
||||
}
|
||||
|
||||
format_print(fp, format, indent, "%s (%d)\n", tls_extension_name(ext_type), ext_type);
|
||||
indent += 4;
|
||||
|
||||
tls_extensions_print(fp, exts, exts_len, format, indent);
|
||||
}
|
||||
*/
|
||||
|
||||
if (datalen > 0) {
|
||||
error_print();
|
||||
@@ -700,9 +699,7 @@ int tls_server_hello_print(FILE *fp, const uint8_t *data, size_t datalen, int fo
|
||||
tls_compression_method_name(comp_meth), comp_meth);
|
||||
if (datalen > 0) {
|
||||
if (tls_uint16array_from_bytes(&exts, &exts_len, &data, &datalen) != 1) goto bad;
|
||||
//format_bytes(fp, format, indent, "Extensions : ", exts, exts_len); // FIXME: extensions_print
|
||||
//tls_extensions_print(fp, exts, exts_len, format, indent);
|
||||
//tls13_extensions_print(fp, format, indent, TLS_handshake_server_hello, exts, exts_len);
|
||||
tls_extensions_print(fp, exts, exts_len, format, indent);
|
||||
}
|
||||
return 1;
|
||||
bad:
|
||||
|
||||
@@ -28,5 +28,5 @@
|
||||
" cat cacert.pem >> certs.pem\n"
|
||||
"\n"
|
||||
" gmssl tls12_server -port 4430 -cert certs.pem -key signkey.pem -pass 1234\n"
|
||||
" gmssl tls12_client -host 127.0.0.1 -port 4430 -cacert rootcacert.pem\n"
|
||||
" gmssl tls12_client -host 127.0.0.1 -port 4430 -cipher_suite TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 -supported_group prime256v1 -sig_alg ecdsa_secp256r1_sha256 -cacert rootcacert.pem\n"
|
||||
|
||||
|
||||
@@ -24,10 +24,16 @@ static const char *help =
|
||||
"Options\n"
|
||||
"\n"
|
||||
" -port num Listening port number, 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"
|
||||
" -cert file Server's certificate chain in PEM format\n"
|
||||
" -key file Server's encrypted private key in PEM format\n"
|
||||
" -pass str Password to decrypt private key\n"
|
||||
" -cert_request Client certificate request\n"
|
||||
" -cacert file CA certificate for client certificate verification\n"
|
||||
" -verify_depth num Certificate verification depth\n"
|
||||
" -client_cert_optional Allow client send empty Certificate\n"
|
||||
"\n"
|
||||
#include "tls12_help.h"
|
||||
"\n";
|
||||
@@ -38,10 +44,31 @@ int tls12_server_main(int argc , char **argv)
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
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 *certfiles[4];
|
||||
size_t certfiles_cnt = 0;
|
||||
char *keyfiles[sizeof(certfiles)/sizeof(certfiles[0])];
|
||||
size_t keyfiles_cnt = 0;
|
||||
char *passes[sizeof(certfiles)/sizeof(certfiles[0])];
|
||||
size_t passes_cnt = 0;
|
||||
|
||||
/*
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
*/
|
||||
|
||||
int cert_request = 0;
|
||||
char *cacertfile = NULL;
|
||||
int verify_depth = TLS_DEFAULT_VERIFY_DEPTH;
|
||||
int client_cert_optional = 0;
|
||||
|
||||
int server_ciphers[] = { TLS_cipher_ecdhe_sm4_cbc_sm3, };
|
||||
|
||||
@@ -54,6 +81,8 @@ int tls12_server_main(int argc , char **argv)
|
||||
struct sockaddr_in server_addr;
|
||||
struct sockaddr_in client_addr;
|
||||
|
||||
size_t i;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
@@ -70,6 +99,51 @@ int tls12_server_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, "-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);
|
||||
@@ -79,9 +153,45 @@ int tls12_server_main(int argc , char **argv)
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
*/
|
||||
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (certfiles_cnt >= sizeof(certfiles)/sizeof(certfiles[0])) {
|
||||
fprintf(stderr, "%s: too many -cert options\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
certfiles[certfiles_cnt++] = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (keyfiles_cnt >= sizeof(keyfiles)/sizeof(keyfiles[0])) {
|
||||
fprintf(stderr, "%s: too many -key options\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
keyfiles[keyfiles_cnt++] = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (passes_cnt >= sizeof(passes)/sizeof(passes[0])) {
|
||||
fprintf(stderr, "%s: too many -pass options\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
passes[passes_cnt++] = *(++argv);
|
||||
|
||||
|
||||
} else if (!strcmp(*argv, "-cert_request")) {
|
||||
cert_request = 1;
|
||||
} 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, "-client_cert_optional")) {
|
||||
client_cert_optional = 1;
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
@@ -92,39 +202,102 @@ bad:
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
if (!certfile) {
|
||||
|
||||
if (!certfiles_cnt) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!keyfile) {
|
||||
if (!keyfiles_cnt) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!pass) {
|
||||
if (!passes_cnt) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
if (tls_socket_lib_init() != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls12, TLS_server_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, server_ciphers, sizeof(server_ciphers)/sizeof(int)) != 1
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls12, TLS_server_mode) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_ctx_set_cipher_suites(&ctx, cipher_suites, cipher_suites_cnt) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
// supported_groups
|
||||
if (supported_groups_cnt > 0) {
|
||||
if (tls_ctx_set_supported_groups(&ctx, supported_groups, supported_groups_cnt) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
// signature_algorithms
|
||||
if (sig_algs_cnt > 0) {
|
||||
if (tls_ctx_set_signature_algorithms(&ctx, sig_algs, sig_algs_cnt) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (certfiles_cnt != keyfiles_cnt || keyfiles_cnt != passes_cnt) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
// Certificate
|
||||
for (i = 0; i < certfiles_cnt; i++) {
|
||||
if (tls_ctx_add_certificate_chain_and_key(&ctx, certfiles[i], keyfiles[i], passes[i]) != 1) {
|
||||
error_print();
|
||||
goto end;;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if (tls_ctx_set_cipher_suites(&ctx, server_ciphers, sizeof(server_ciphers)/sizeof(int)) != 1
|
||||
|| tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
*/
|
||||
|
||||
// CertificateRequest
|
||||
if (cert_request) {
|
||||
if (!cacertfile) {
|
||||
fprintf(stderr, "%s: -cacert required by -cert_request\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, verify_depth) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (tls_ctx_enable_certificate_request(&ctx, 1) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (client_cert_optional) {
|
||||
if (tls13_ctx_enable_client_certificate_optional(&ctx, 1) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Socket
|
||||
|
||||
|
||||
Reference in New Issue
Block a user