Add Windows and Visual Studio support

This commit is contained in:
Zhi Guan
2022-10-03 11:36:03 +08:00
parent 75155a4c37
commit 5e38788659
45 changed files with 1147 additions and 67 deletions

View File

@@ -1,4 +1,4 @@
/*
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may

View File

@@ -192,10 +192,12 @@ int main(int argc, char **argv)
return tls13_client_main(argc, argv);
} else if (!strcmp(*argv, "tls13_server")) {
return tls13_server_main(argc, argv);
#ifndef WIN32
} else if (!strcmp(*argv, "sdfutil")) {
return sdfutil_main(argc, argv);
} else if (!strcmp(*argv, "skfutil")) {
return skfutil_main(argc, argv);
#endif
} else {
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
fprintf(stderr, "usage: %s %s\n", prog, options);

View File

@@ -33,7 +33,7 @@ int sm2sign_main(int argc, char **argv)
SM2_KEY key;
SM2_SIGN_CTX sign_ctx;
uint8_t buf[4096];
ssize_t len;
size_t len;
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
size_t siglen;

View File

@@ -36,7 +36,7 @@ int sm2verify_main(int argc, char **argv)
uint8_t cert[1024];
size_t certlen;
uint8_t buf[4096];
ssize_t len;
size_t len;
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
size_t siglen;
int vr;

View File

@@ -34,7 +34,7 @@ int sm3_main(int argc, char **argv)
SM3_CTX sm3_ctx;
uint8_t dgst[32];
uint8_t buf[4096];
ssize_t len;
size_t len;
int i;
argc--;

View File

@@ -33,7 +33,7 @@ int sm9sign_main(int argc, char **argv)
SM9_SIGN_KEY key;
SM9_SIGN_CTX ctx;
uint8_t buf[4096];
ssize_t len;
size_t len;
uint8_t sig[SM9_SIGNATURE_SIZE];
size_t siglen;

View File

@@ -31,9 +31,9 @@ int sm9verify_main(int argc, char **argv)
SM9_SIGN_MASTER_KEY mpk;
SM9_SIGN_CTX ctx;
uint8_t buf[4096];
ssize_t len;
size_t len;
uint8_t sig[SM9_SIGNATURE_SIZE];
ssize_t siglen;
size_t siglen;
argc--;
argv++;

View File

@@ -1,4 +1,4 @@
/*
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
@@ -12,12 +12,18 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
// FIMXE: socket related headers should be moved to tls.h
#include <winsock2.h>
#else
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <gmssl/tls.h>
#include <gmssl/error.h>
@@ -95,7 +101,7 @@ bad:
return -1;
}
if (!(hp = gethostbyname(host))) {
herror("tlcp_client: '-host' invalid");
//herror("tlcp_client: '-host' invalid");
goto end;
}
@@ -149,7 +155,7 @@ bad:
FD_ZERO(&fds);
FD_SET(conn.sock, &fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(fileno(stdin), &fds); //FD_SET(STDIN_FILENO, &fds);
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
fprintf(stderr, "%s: select failed\n", prog);
@@ -172,7 +178,7 @@ bad:
}
}
if (FD_ISSET(STDIN_FILENO, &fds)) {
if (FD_ISSET(fileno(stdin), &fds)) {
fprintf(stderr, "recv from stdin\n");
memset(send_buf, 0, sizeof(send_buf));

View File

@@ -12,11 +12,15 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <gmssl/mem.h>
#include <gmssl/sm2.h>
#include <gmssl/tls.h>
@@ -38,7 +42,6 @@ int tlcp_server_main(int argc , char **argv)
char *cacertfile = NULL;
int server_ciphers[] = { TLS_cipher_ecc_sm4_cbc_sm3, };
uint8_t verify_buf[4096];
TLS_CTX ctx;
TLS_CONNECT conn;
@@ -48,7 +51,11 @@ int tlcp_server_main(int argc , char **argv)
int sock;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
#ifdef WIN32
int client_addrlen;
#else
socklen_t client_addrlen;
#endif
int conn_sock;
@@ -189,7 +196,11 @@ restart:
if (tls_send(&conn, (uint8_t *)buf, len, &sentlen) != 1) {
fprintf(stderr, "%s: send failure, close connection\n", prog);
#ifdef WIN32
closesocket(conn.sock);
#else
close(conn.sock);
#endif
goto end;
}
}

View File

@@ -1,4 +1,4 @@
/*
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
@@ -12,6 +12,9 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <netdb.h>
@@ -19,6 +22,9 @@
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <gmssl/tls.h>
#include <gmssl/error.h>
@@ -52,7 +58,6 @@ int tls12_client_main(int argc, char *argv[])
char buf[1024] = {0};
size_t len = sizeof(buf);
char send_buf[1024] = {0};
size_t send_len;
argc--;
argv++;
@@ -98,7 +103,7 @@ bad:
return -1;
}
if (!(hp = gethostbyname(host))) {
herror("tls12_client: '-host' invalid");
//herror("tls12_client: '-host' invalid"); // herror() not in winsock2, use WSAGetLastError() instead
goto end;
}
@@ -111,11 +116,11 @@ bad:
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "%s: open socket error : %s\n", prog, strerror(errno));
//fprintf(stderr, "%s: open socket error : %s\n", prog, strerror(errno)); //FIXME: WIN32 use WSAGetLastError()
goto end;
}
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
fprintf(stderr, "%s: connect error : %s\n", prog, strerror(errno));
//fprintf(stderr, "%s: connect error : %s\n", prog, strerror(errno)); //
goto end;
}
@@ -150,7 +155,7 @@ bad:
FD_ZERO(&fds);
FD_SET(conn.sock, &fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(fileno(stdin), &fds);
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
fprintf(stderr, "%s: select failed\n", prog);
@@ -173,7 +178,7 @@ bad:
}
}
if (FD_ISSET(STDIN_FILENO, &fds)) {
if (FD_ISSET(fileno(stdin), &fds)) {
memset(send_buf, 0, sizeof(send_buf));
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
@@ -193,7 +198,11 @@ bad:
end:
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
tls_ctx_cleanup(&ctx);
tls_cleanup(&conn);
return 0;

View File

@@ -12,11 +12,15 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <gmssl/mem.h>
#include <gmssl/sm2.h>
#include <gmssl/tls.h>
@@ -46,7 +50,11 @@ int tls12_server_main(int argc , char **argv)
int sock;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
#ifdef WIN32
int client_addrlen;
#else
socklen_t client_addrlen;
#endif
int conn_sock;

View File

@@ -1,4 +1,4 @@
/*
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
@@ -12,13 +12,16 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <gmssl/tls.h>
#include <gmssl/error.h>
@@ -98,7 +101,7 @@ bad:
return -1;
}
if (!(hp = gethostbyname(host))) {
herror("tls13_client: '-host' invalid");
//herror("tls13_client: '-host' invalid");
goto end;
}
@@ -149,7 +152,7 @@ bad:
FD_ZERO(&fds);
FD_SET(conn.sock, &fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(fileno(stdin), &fds);
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
fprintf(stderr, "%s: select failed\n", prog);
@@ -172,7 +175,7 @@ bad:
}
}
if (FD_ISSET(STDIN_FILENO, &fds)) {
if (FD_ISSET(fileno(stdin), &fds)) {
memset(send_buf, 0, sizeof(send_buf));
if (!fgets(send_buf, sizeof(send_buf), stdin)) {

View File

@@ -12,11 +12,15 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <gmssl/mem.h>
#include <gmssl/sm2.h>
#include <gmssl/tls.h>
@@ -46,7 +50,11 @@ int tls13_server_main(int argc , char **argv)
int sock;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
#ifdef WIN32
int client_addrlen;
#else
socklen_t client_addrlen;
#endif
int conn_sock;