Files
GmSSL/tools/tlcp_client.c
Zhi Guan 9f2bfb5ad0 Add more options to TLS
add `-get` and `-quiet` option to `tlcp_client`
2024-02-05 17:01:09 +08:00

294 lines
6.7 KiB
C

/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/tls.h>
#include <gmssl/x509.h>
#include <gmssl/error.h>
#define TIMEOUT_SECONDS 1
static int client_ciphers[] = { TLS_cipher_ecc_sm4_cbc_sm3, };
static const char *options = "-host str [-port num] [-cacert file] [-cert file -key file -pass str]"
" -quiet"
" -get [path]"
" [-outcerts file]";
int tlcp_client_main(int argc, char *argv[])
{
int ret = -1;
char *prog = argv[0];
char *host = NULL;
int port = 443;
char *cacertfile = NULL;
char *certfile = NULL;
char *keyfile = NULL;
char *pass = NULL;
char *get = NULL;
char *outcertsfile = NULL;
int quiet = 0;
struct hostent *hp;
struct sockaddr_in server;
tls_socket_t sock;
int sock_inited = 0;
TLS_CTX ctx;
TLS_CONNECT conn;
char buf[1024] = {0};
size_t len = sizeof(buf);
char send_buf[1024] = {0};
size_t sentlen;
argc--;
argv++;
if (argc < 1) {
fprintf(stderr, "usage: %s %s\n", prog, options);
return 1;
}
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
printf("usage: %s %s\n", prog, options);
return 0;
} else if (!strcmp(*argv, "-host")) {
if (--argc < 1) goto bad;
host = *(++argv);
} else if (!strcmp(*argv, "-port")) {
if (--argc < 1) goto bad;
port = atoi(*(++argv));
} else if (!strcmp(*argv, "-cacert")) {
if (--argc < 1) goto bad;
cacertfile = *(++argv);
} else if (!strcmp(*argv, "-cert")) {
if (--argc < 1) goto bad;
certfile = *(++argv);
} else if (!strcmp(*argv, "-key")) {
if (--argc < 1) goto bad;
keyfile = *(++argv);
} else if (!strcmp(*argv, "-pass")) {
if (--argc < 1) goto bad;
pass = *(++argv);
} else if (!strcmp(*argv, "-get")) {
if (--argc < 1) goto bad;
get = *(++argv);
} else if (!strcmp(*argv, "-outcerts")) {
if (--argc < 1) goto bad;
outcertsfile = *(++argv);
} else if (!strcmp(*argv, "-quiet")) {
quiet = 1;
} else {
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
return 1;
bad:
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
return 0;
}
argc--;
argv++;
}
if (!host) {
fprintf(stderr, "%s: '-in' option required\n", prog);
return -1;
}
if (tls_socket_lib_init() != 1) {
error_print();
return -1;
}
if (!(hp = gethostbyname(host))) {
//herror("tlcp_client: '-host' invalid");
goto end;
}
memset(&ctx, 0, sizeof(ctx));
memset(&conn, 0, sizeof(conn));
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
server.sin_family = AF_INET;
server.sin_port = htons(port);
if (tls_socket_create(&sock, AF_INET, SOCK_STREAM, 0) != 1) {
fprintf(stderr, "%s: open socket error\n", prog);
goto end;
}
sock_inited = 1;
if (tls_socket_connect(sock, &server) != 1) {
fprintf(stderr, "%s: socket connect error\n", prog);
goto end;
}
if (tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_client_mode) != 1
|| 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 (cacertfile) {
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
fprintf(stderr, "%s: context init error\n", prog);
goto end;
}
}
if (certfile) {
if (tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
fprintf(stderr, "%s: context init error\n", prog);
goto end;
}
}
if (quiet || get) {
ctx.quiet = 1;
}
if (tls_init(&conn, &ctx) != 1
|| tls_set_socket(&conn, sock) != 1
|| tls_do_handshake(&conn) != 1) {
fprintf(stderr, "%s: error\n", prog);
goto end;
}
if (outcertsfile) {
FILE *outcertsfp;
if (!(outcertsfp = fopen(outcertsfile, "wb"))) {
fprintf(stderr, "%s: open '%s' failure\n", prog, outcertsfile);
perror("fopen");
goto end;
}
if (x509_certs_to_pem(conn.server_certs, conn.server_certs_len, outcertsfp) != 1) {
fprintf(stderr, "%s: x509_certs_to_pem error\n", prog);
fclose(outcertsfp);
goto end;
}
fclose(outcertsfp);
}
if (get) {
struct timeval timeout;
timeout.tv_sec = TIMEOUT_SECONDS;
timeout.tv_usec = 0;
snprintf(buf, sizeof(buf), "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", get, host);
if (tls_send(&conn, (uint8_t *)buf, strlen(buf), &len) != 1) {
fprintf(stderr, "%s: send error\n", prog);
goto end;
}
if (setsockopt(conn.sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)) != 0) {
perror("setsockopt");
fprintf(stderr, "%s: set socket timeout error\n", prog);
goto end;
}
for (;;) {
int ret;
if ((ret = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
if (ret == 0) {
fprintf(stderr, "%s: TLCP connection is closed by remote host\n", prog);
} else if (ret != -EAGAIN) {
fprintf(stderr, "%s: recv error\n", prog);
}
break;
}
fwrite(buf, 1, len, stdout);
fflush(stdout);
}
tls_shutdown(&conn);
goto end;
}
for (;;) {
fd_set fds;
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
if (feof(stdin)) {
tls_shutdown(&conn);
goto end;
} else {
continue;
}
}
if (tls_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
fprintf(stderr, "%s: send error\n", prog);
goto end;
}
FD_ZERO(&fds);
FD_SET(conn.sock, &fds);
#ifdef WIN32
#else
FD_SET(fileno(stdin), &fds); //FD_SET(STDIN_FILENO, &fds); // NOT allowed in winsock2 !!!
#endif
if (select((int)(conn.sock + 1), // WinSock2 select() ignore this arg
&fds, NULL, NULL, NULL) < 0) {
fprintf(stderr, "%s: select failed\n", prog);
#ifdef WIN32
fprintf(stderr, "WSAGetLastError = %u\n", WSAGetLastError());
#endif
goto end;
}
if (FD_ISSET(conn.sock, &fds)) {
for (;;) {
memset(buf, 0, sizeof(buf));
if (tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len) != 1) {
goto end;
}
fwrite(buf, 1, len, stdout);
fflush(stdout);
// 应该调整tls_recv 逻辑、API或者其他方式
if (conn.datalen == 0) {
break;
}
}
}
#ifdef WIN32
#else
if (FD_ISSET(fileno(stdin), &fds)) {
fprintf(stderr, "recv from stdin\n");
memset(send_buf, 0, sizeof(send_buf));
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
if (feof(stdin)) {
tls_shutdown(&conn);
goto end;
} else {
continue;
}
}
if (tls_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
fprintf(stderr, "%s: send error\n", prog);
goto end;
}
}
#endif
fprintf(stderr, "end of this round\n");
}
end:
if (sock_inited) tls_socket_close(sock);
tls_ctx_cleanup(&ctx);
tls_cleanup(&conn);
return 0;
}