TLS客户端 - 使用根证书, 非阻塞, 向服务器要东西.
开始一个新demo学习时, 要从头配置包含路径, 麻烦. 直接拷贝上一个实现工程, 换掉实现.c方便一些.
换的新demo实现, 要加入库包含和头包含, 麻烦, 做一个公用头文件, 直接include方便一些.
/*!
\file my_openSSL_lib.h
*/
#ifndef __MY_OPENSSL_LIB_H__
#define __MY_OPENSSL_LIB_H__
#ifdef _WIN32
#include <windows.h>
#endif /* #ifdef _WIN32 */
#pragma comment(lib, "libcrypto.lib")
#pragma comment(lib, "libssl.lib")
#include <openssl/applink.c> /*! for OPENSSL_Uplink(00007FF8B7EF0FE8,08): no OPENSSL_Applink */
#ifdef _WIN32
#define MY_SLEEP(x) Sleep(x)
#else
#define MY_SLEEP(x) sleep(x)
#endif /* #ifdef _WIN32 */
#endif /* #ifndef __MY_OPENSSL_LIB_H__ */
/*!
\file sconnect.c
\brief TLS客户端 - 使用根证书, 非阻塞, 向服务器要东西.
*/
/*
* Copyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
/*-
* A minimal program to do SSL to a passed host and port.
* It is actually using non-blocking IO but in a very simple manner
* sconnect host:port - it does a 'GET / HTTP/1.0'
*
* cc -I../../include sconnect.c -L../.. -lssl -lcrypto
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef __unix__
#include <unistd.h>
#endif
#include <string.h>
#include <errno.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include "my_openSSL_lib.h"
#define HOSTPORT "localhost:4433"
#define CAFILE "root.pem"
int main(int argc, char *argv[])
{
const char *hostport = HOSTPORT;
const char *CAfile = CAFILE;
const char *hostname;
// char *cp;
BIO *bio_out = NULL;
char buf[1024 * 10], *p;
SSL_CTX *ssl_ctx = NULL;
SSL *ssl;
BIO *bio_ssl;
int i, len, off, ret = EXIT_FAILURE;
if (argc > 1)
hostport = argv[1];
if (argc > 2)
CAfile = argv[2];
#ifdef WATT32
dbug_init();
sock_init();
#endif
ssl_ctx = SSL_CTX_new(TLS_client_method());
/* Enable trust chain verification */
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL);
/* Lets make a SSL structure */
ssl = SSL_new(ssl_ctx);
SSL_set_connect_state(ssl);
/* Use it inside an SSL BIO */
bio_ssl = BIO_new(BIO_f_ssl());
BIO_set_ssl(bio_ssl, ssl, BIO_CLOSE);
/* Lets use a connect BIO under the SSL BIO */
bio_out = BIO_new(BIO_s_connect());
BIO_set_conn_hostname(bio_out, hostport);
/* The BIO has parsed the host:port and even IPv6 literals in [] */
hostname = BIO_get_conn_hostname(bio_out);
if (!hostname || SSL_set1_host(ssl, hostname) <= 0)
goto err;
/*! https://www.openssl.org/docs/man1.1.1/man3/BIO_set_nbio.html
* sets the non blocking I/O flag to n. If n is zero then blocking I/O is set. If n is 1 then non blocking I/O is set.
*/
BIO_set_nbio(bio_out, 1);
bio_out = BIO_push(bio_ssl, bio_out); /*! append bio_out to bio_ssl, 返回的是链表头 */
/*!
此时的链表头还是bio_ssl, 返回的也是链表头
此时 bio_out == bio_ssl, 指针地址是一样的.
此时操作bio_out的效果, 先经过bio_sll, 再经过原始的bio_out, 达到一个数据流经过不同工序被分别处理的效果.
*/
p = "GET / HTTP/1.0\r\n\r\n";
len = (int)strlen(p);
off = 0;
for (;;) {
i = BIO_write(bio_out, &(p[off]), len);
if (i <= 0) {
/*! BIO_should_retry() 的充实次数为2, 如果第3次还是失败, 就返回false */
if (BIO_should_retry(bio_out)) {
fprintf(stderr, "write DELAY\n");
MY_SLEEP(1);
continue;
} else {
goto err;
}
}
off += i;
len -= i;
if (len <= 0)
break;
}
for (;;) {
i = BIO_read(bio_out, buf, sizeof(buf));
if (i == 0)
break;
if (i < 0) {
if (BIO_should_retry(bio_out)) {
fprintf(stderr, "read DELAY\n");
MY_SLEEP(1);
continue;
}
goto err;
}
fwrite(buf, 1, i, stdout);
}
ret = EXIT_SUCCESS;
goto done;
err:
if (ERR_peek_error() == 0) { /* system call error */
fprintf(stderr, "errno=%d ", errno);
perror("error");
} else {
ERR_print_errors_fp(stderr);
}
done:
BIO_free_all(bio_out); /*! 如果是一个链表头的bio, 用BIO_free_all()来释放 */
SSL_CTX_free(ssl_ctx);
return ret;
}