mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-07 12:33:39 +08:00
Remove demos relay on third-party code
This commit is contained in:
@@ -184,9 +184,6 @@ set(demos
|
|||||||
demo_sm9_encrypt
|
demo_sm9_encrypt
|
||||||
demo_sm9_keygen
|
demo_sm9_keygen
|
||||||
demo_sm9_sign
|
demo_sm9_sign
|
||||||
# demo_tlcp_get
|
|
||||||
# demo_tlcp_post
|
|
||||||
# demo_wget
|
|
||||||
demo_zuc
|
demo_zuc
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,91 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <gmssl/tls.h>
|
|
||||||
#include <gmssl/error.h>
|
|
||||||
#define TLS_DEFAULT_VERIFY_DEPTH 4
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int ret = -1;
|
|
||||||
char *prog = argv[0];
|
|
||||||
const int cipher = TLS_cipher_ecc_sm4_cbc_sm3;
|
|
||||||
struct hostent *hp;
|
|
||||||
struct sockaddr_in server;
|
|
||||||
int sock;
|
|
||||||
TLS_CTX ctx;
|
|
||||||
TLS_CONNECT conn;
|
|
||||||
char request[1024];
|
|
||||||
uint8_t buf[16800];
|
|
||||||
char *p;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
//证书和密钥使用/demos/scripts/tlcp_server.sh生成
|
|
||||||
char* cacertfile="rootcacert.pem";
|
|
||||||
char* certfile="clientcert.pem";
|
|
||||||
char* keyfile="clientkey.pem";
|
|
||||||
char *pass = "1234";
|
|
||||||
if(argc < 3)
|
|
||||||
{
|
|
||||||
fprintf(stderr,"usage %s ip port \n",argv[0]);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
server.sin_family = AF_INET;
|
|
||||||
server.sin_port = htons(atoi(argv[2]));
|
|
||||||
server.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
||||||
perror("socket");
|
|
||||||
printf("创建socket错误");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {//去连接服务器
|
|
||||||
perror("connect");
|
|
||||||
printf("socket连接失败");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
memset(&ctx, 0, sizeof(ctx));
|
|
||||||
memset(&conn, 0, sizeof(conn));
|
|
||||||
|
|
||||||
tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_client_mode);
|
|
||||||
tls_ctx_set_cipher_suites(&ctx, &cipher, 1);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tls_init(&conn, &ctx);
|
|
||||||
tls_set_socket(&conn, sock);
|
|
||||||
|
|
||||||
|
|
||||||
if(tls_do_handshake(&conn) == 1)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else {//握手
|
|
||||||
fprintf(stderr, "%s: error\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
end:
|
|
||||||
close(sock);
|
|
||||||
tls_ctx_cleanup(&ctx);
|
|
||||||
tls_cleanup(&conn);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 <unistd.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <gmssl/tls.h>
|
|
||||||
#include <gmssl/error.h>
|
|
||||||
#include "url_parser.h"
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int ret = -1;
|
|
||||||
char *prog = argv[0];
|
|
||||||
const int cipher = TLS_cipher_ecc_sm4_cbc_sm3;
|
|
||||||
URL_COMPONENTS *url;
|
|
||||||
struct hostent *hp;
|
|
||||||
int port = 443;
|
|
||||||
struct sockaddr_in server;
|
|
||||||
int sock;
|
|
||||||
TLS_CTX ctx;
|
|
||||||
TLS_CONNECT conn;
|
|
||||||
char request[1024];
|
|
||||||
uint8_t buf[16800];
|
|
||||||
char *p;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
if (argc != 2) {
|
|
||||||
fprintf(stderr, "example: tlcp_get https://sm2only.ovssl.cn\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(url = parse_url(argv[1]))) {
|
|
||||||
fprintf(stderr, "parse url '%s' failure\n", argv[1]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (!(hp = gethostbyname(url->host))) {
|
|
||||||
herror("tlcp_client: '-host' invalid");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (url->port != -1) {
|
|
||||||
port = url->port;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
|
||||||
server.sin_family = AF_INET;
|
|
||||||
server.sin_port = htons(port);
|
|
||||||
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
||||||
perror("socket");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
|
||||||
perror("connect");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&ctx, 0, sizeof(ctx));
|
|
||||||
memset(&conn, 0, sizeof(conn));
|
|
||||||
|
|
||||||
tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_client_mode);
|
|
||||||
tls_ctx_set_cipher_suites(&ctx, &cipher, 1);
|
|
||||||
tls_init(&conn, &ctx);
|
|
||||||
tls_set_socket(&conn, sock);
|
|
||||||
|
|
||||||
if (tls_do_handshake(&conn) != 1) {
|
|
||||||
fprintf(stderr, "%s: error\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(request, sizeof(request)-1, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",
|
|
||||||
url->path ? url->path : "/",
|
|
||||||
url->host);
|
|
||||||
|
|
||||||
tls_send(&conn, (uint8_t *)request, strlen(request), &len);
|
|
||||||
|
|
||||||
if (tls_recv(&conn, buf, sizeof(buf), &len) != 1) {
|
|
||||||
fprintf(stderr, "recv failure\n");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
buf[len] = 0;
|
|
||||||
|
|
||||||
p = strstr((char *)buf, "\r\n\r\n");
|
|
||||||
if (p) {
|
|
||||||
printf("%s", p + 4);
|
|
||||||
fflush(stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
end:
|
|
||||||
free_url_components(url);
|
|
||||||
close(sock);
|
|
||||||
tls_ctx_cleanup(&ctx);
|
|
||||||
tls_cleanup(&conn);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 <unistd.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <gmssl/tls.h>
|
|
||||||
#include <gmssl/error.h>
|
|
||||||
#include "url_parser.h"
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int ret = -1;
|
|
||||||
char *prog = argv[0];
|
|
||||||
const int cipher = TLS_cipher_ecc_sm4_cbc_sm3;
|
|
||||||
URL_COMPONENTS *url;
|
|
||||||
struct hostent *hp;
|
|
||||||
int port = 443;
|
|
||||||
struct sockaddr_in server;
|
|
||||||
int sock;
|
|
||||||
TLS_CTX ctx;
|
|
||||||
TLS_CONNECT conn;
|
|
||||||
char request[1024];
|
|
||||||
uint8_t buf[16800];
|
|
||||||
char *p;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
if (argc != 2) {
|
|
||||||
fprintf(stderr, "example: echo \"key=word\" | tlcp_post https://sm2only.ovssl.cn\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(url = parse_url(argv[1]))) {
|
|
||||||
fprintf(stderr, "parse url '%s' failure\n", argv[1]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (!(hp = gethostbyname(url->host))) {
|
|
||||||
herror("tlcp_client: '-host' invalid");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (url->port != -1) {
|
|
||||||
port = url->port;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
|
||||||
server.sin_family = AF_INET;
|
|
||||||
server.sin_port = htons(port);
|
|
||||||
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
||||||
perror("socket");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
|
||||||
perror("connect");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&ctx, 0, sizeof(ctx));
|
|
||||||
memset(&conn, 0, sizeof(conn));
|
|
||||||
|
|
||||||
tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_client_mode);
|
|
||||||
tls_ctx_set_cipher_suites(&ctx, &cipher, 1);
|
|
||||||
tls_init(&conn, &ctx);
|
|
||||||
tls_set_socket(&conn, sock);
|
|
||||||
|
|
||||||
if (tls_do_handshake(&conn) != 1) {
|
|
||||||
fprintf(stderr, "%s: error\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(request, sizeof(request)-1, "POST %s HTTP/1.1\r\nHost: %s\r\n\r\n",
|
|
||||||
url->path ? url->path : "/",
|
|
||||||
url->host);
|
|
||||||
|
|
||||||
tls_send(&conn, (uint8_t *)request, strlen(request), &len);
|
|
||||||
|
|
||||||
len = fread(buf, 1, sizeof(buf), stdin);
|
|
||||||
if (len) {
|
|
||||||
tls_send(&conn, buf, len, &len);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_recv(&conn, buf, sizeof(buf), &len) != 1) {
|
|
||||||
fprintf(stderr, "recv failure\n");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
buf[len] = 0;
|
|
||||||
|
|
||||||
p = strstr((char *)buf, "\r\n\r\n");
|
|
||||||
if (p) {
|
|
||||||
printf("%s", p + 4);
|
|
||||||
fflush(stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
end:
|
|
||||||
free_url_components(url);
|
|
||||||
close(sock);
|
|
||||||
tls_ctx_cleanup(&ctx);
|
|
||||||
tls_cleanup(&conn);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <gmssl/mem.h>
|
|
||||||
#include <gmssl/sm2.h>
|
|
||||||
#include <gmssl/tls.h>
|
|
||||||
#include <gmssl/error.h>
|
|
||||||
#include <gmssl/pem.h>
|
|
||||||
|
|
||||||
int main(int argc,char *argv[]){
|
|
||||||
|
|
||||||
int ret = 1;
|
|
||||||
char *prog = argv[0];
|
|
||||||
char *signpass = "1234";
|
|
||||||
char *encpass = "1234";
|
|
||||||
|
|
||||||
int server_ciphers[] = { TLS_cipher_ecc_sm4_cbc_sm3, };
|
|
||||||
uint8_t verify_buf[4096];
|
|
||||||
|
|
||||||
TLS_CTX ctx;
|
|
||||||
TLS_CONNECT conn;
|
|
||||||
char buf[1600] = {0};
|
|
||||||
size_t len = sizeof(buf);
|
|
||||||
|
|
||||||
int sock;
|
|
||||||
struct sockaddr_in server_addr;//服务端地址
|
|
||||||
struct sockaddr_in client_addr;//客户端地址
|
|
||||||
socklen_t client_addrlen;
|
|
||||||
int conn_sock;
|
|
||||||
|
|
||||||
//证书和密钥使用/demos/scripts/tlcp_server.sh生成
|
|
||||||
char* certfile="double_certs.pem";
|
|
||||||
char* signkeyfile="signkey.pem";
|
|
||||||
char* enckeyfile="enckey.pem";
|
|
||||||
char* cacertfile="cacert.pem";
|
|
||||||
|
|
||||||
|
|
||||||
if(argc < 3)
|
|
||||||
{
|
|
||||||
fprintf(stderr,"usage %s ip port \n",argv[0]);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&ctx, 0, sizeof(ctx));
|
|
||||||
memset(&conn, 0, sizeof(conn));
|
|
||||||
|
|
||||||
if (tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_server_mode) != 1
|
|
||||||
|| tls_ctx_set_cipher_suites(&ctx, server_ciphers, sizeof(server_ciphers)/sizeof(int)) != 1
|
|
||||||
|| tls_ctx_set_tlcp_server_certificate_and_keys(&ctx, certfile, signkeyfile, signpass, enckeyfile, encpass) != 1) {
|
|
||||||
error_print();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (cacertfile) {
|
|
||||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
|
||||||
error_print();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
||||||
error_print();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_port = htons(atoi(argv[2]));
|
|
||||||
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
|
|
||||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
|
||||||
error_print();
|
|
||||||
perror("tlcp_accept: bind: ");
|
|
||||||
}
|
|
||||||
|
|
||||||
puts("start listen ...\n");
|
|
||||||
listen(sock, 1);
|
|
||||||
client_addrlen = sizeof(client_addr);
|
|
||||||
|
|
||||||
if ((conn_sock = accept(sock, (struct sockaddr *)&client_addr, &client_addrlen)) < 0) {
|
|
||||||
error_print();
|
|
||||||
}
|
|
||||||
|
|
||||||
puts("socket connected\n");
|
|
||||||
printf("client ip : %s\nport %d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
|
|
||||||
|
|
||||||
|
|
||||||
if (tls_init(&conn, &ctx) != 1
|
|
||||||
|| tls_set_socket(&conn, conn_sock) != 1) {
|
|
||||||
error_print();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
printf("tlcp_init finished\n");
|
|
||||||
if (tls_do_handshake(&conn) == 1) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
error_print();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <gmssl/tls.h>
|
|
||||||
#include <gmssl/error.h>
|
|
||||||
#include "url_parser.h"
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int ret = -1;
|
|
||||||
char *prog = argv[0];
|
|
||||||
const int cipher = TLS_cipher_ecc_sm4_cbc_sm3;
|
|
||||||
URL_COMPONENTS *url;
|
|
||||||
struct hostent *hp;
|
|
||||||
int port = 4430;
|
|
||||||
struct sockaddr_in server;
|
|
||||||
int sock;
|
|
||||||
TLS_CTX ctx;
|
|
||||||
TLS_CONNECT conn;
|
|
||||||
char request[1024];
|
|
||||||
uint8_t buf[16800];
|
|
||||||
char *p;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
if (argc != 2) {
|
|
||||||
fprintf(stderr, "example: https://sm2only.ovssl.cn\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(url = parse_url(argv[1]))) {
|
|
||||||
fprintf(stderr, "parse url '%s' failure\n", argv[1]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (!(hp = gethostbyname(url->host))) {
|
|
||||||
herror("tls12_client: '-host' invalid");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (url->port != -1) {
|
|
||||||
port = url->port;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
|
||||||
server.sin_family = AF_INET;
|
|
||||||
server.sin_port = htons(port);
|
|
||||||
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
||||||
perror("socket");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
|
||||||
perror("connect");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&ctx, 0, sizeof(ctx));
|
|
||||||
memset(&conn, 0, sizeof(conn));
|
|
||||||
|
|
||||||
tls_ctx_init(&ctx, TLS_protocol_tls12, TLS_client_mode);
|
|
||||||
tls_ctx_set_cipher_suites(&ctx, &cipher, 1);
|
|
||||||
tls_init(&conn, &ctx);
|
|
||||||
tls_set_socket(&conn, sock);
|
|
||||||
|
|
||||||
if (tls_do_handshake(&conn) != 1) {
|
|
||||||
fprintf(stderr, "%s: error\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(request, sizeof(request)-1, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",
|
|
||||||
url->path ? url->path : "/",
|
|
||||||
url->host);
|
|
||||||
|
|
||||||
tls_send(&conn, (uint8_t *)request, strlen(request), &len);
|
|
||||||
|
|
||||||
if (tls_recv(&conn, buf, sizeof(buf), &len) != 1) {
|
|
||||||
fprintf(stderr, "recv failure\n");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
buf[len] = 0;
|
|
||||||
|
|
||||||
p = strstr((char *)buf, "\r\n\r\n");
|
|
||||||
if (p) {
|
|
||||||
printf("%s", p + 4);
|
|
||||||
fflush(stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
end:
|
|
||||||
free_url_components(url);
|
|
||||||
close(sock);
|
|
||||||
tls_ctx_cleanup(&ctx);
|
|
||||||
tls_cleanup(&conn);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <gmssl/tls.h>
|
|
||||||
#include <gmssl/error.h>
|
|
||||||
#include "url_parser.h"
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int ret = -1;
|
|
||||||
char *prog = argv[0];
|
|
||||||
const int cipher = TLS_cipher_ecc_sm4_cbc_sm3;
|
|
||||||
URL_COMPONENTS *url;
|
|
||||||
struct hostent *hp;
|
|
||||||
int port = 4430;
|
|
||||||
struct sockaddr_in server;
|
|
||||||
int sock;
|
|
||||||
TLS_CTX ctx;
|
|
||||||
TLS_CONNECT conn;
|
|
||||||
char request[1024];
|
|
||||||
uint8_t buf[16800];
|
|
||||||
char *p;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
if (argc != 2) {
|
|
||||||
fprintf(stderr, "example: example.com\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(url = parse_url(argv[1]))) {
|
|
||||||
fprintf(stderr, "parse url '%s' failure\n", argv[1]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (!(hp = gethostbyname(url->host))) {
|
|
||||||
herror("tls12_client: '-host' invalid");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (url->port != -1) {
|
|
||||||
port = url->port;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
|
||||||
server.sin_family = AF_INET;
|
|
||||||
server.sin_port = htons(port);
|
|
||||||
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
||||||
perror("socket");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
|
||||||
perror("connect");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&ctx, 0, sizeof(ctx));
|
|
||||||
memset(&conn, 0, sizeof(conn));
|
|
||||||
|
|
||||||
tls_ctx_init(&ctx, TLS_protocol_tls12, TLS_client_mode);
|
|
||||||
tls_ctx_set_cipher_suites(&ctx, &cipher, 1);
|
|
||||||
tls_init(&conn, &ctx);
|
|
||||||
tls_set_socket(&conn, sock);
|
|
||||||
|
|
||||||
if (tls_do_handshake(&conn) != 1) {
|
|
||||||
fprintf(stderr, "%s: error\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(request, sizeof(request)-1, "POST %s HTTP/1.1\r\nHost: %s\r\n\r\n",
|
|
||||||
url->path ? url->path : "/",
|
|
||||||
url->host);
|
|
||||||
|
|
||||||
tls_send(&conn, (uint8_t *)request, strlen(request), &len);
|
|
||||||
|
|
||||||
len = fread(buf, 1, sizeof(buf), stdin);
|
|
||||||
if (len) {
|
|
||||||
tls_send(&conn, buf, len, &len);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_recv(&conn, buf, sizeof(buf), &len) != 1) {
|
|
||||||
fprintf(stderr, "recv failure\n");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
buf[len] = 0;
|
|
||||||
|
|
||||||
p = strstr((char *)buf, "\r\n\r\n");
|
|
||||||
if (p) {
|
|
||||||
printf("%s", p + 4);
|
|
||||||
fflush(stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
end:
|
|
||||||
free_url_components(url);
|
|
||||||
close(sock);
|
|
||||||
tls_ctx_cleanup(&ctx);
|
|
||||||
tls_cleanup(&conn);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user