openssl3.2 - 官方demo學(xué)習(xí) - sconnect.c
概述
TLS客戶端 - 使用根證書, 非阻塞, 向服務(wù)器要東西.文章來源:http://www.zghlxwxcb.cn/news/detail-790860.html
筆記
開始一個新demo學(xué)習(xí)時, 要從頭配置包含路徑, 麻煩. 直接拷貝上一個實現(xiàn)工程, 換掉實現(xiàn).c方便一些.
換的新demo實現(xiàn), 要加入庫包含和頭包含, 麻煩, 做一個公用頭文件, 直接include方便一些.文章來源地址http://www.zghlxwxcb.cn/news/detail-790860.html
/*!
\file my_openSSL_lib.h
*/
#ifndef __MY_OPENSSL_LIB_H__
#define __MY_OPENSSL_LIB_H__
#ifdef _WIN32
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib") // for select()
#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客戶端 - 使用根證書, 非阻塞, 向服務(wù)器要東西.
*/
/*
* 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的效果, 先經(jīng)過bio_sll, 再經(jīng)過原始的bio_out, 達(dá)到一個數(shù)據(jù)流經(jīng)過不同工序被分別處理的效果.
*/
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() 的充實次數(shù)為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;
}
END
到了這里,關(guān)于openssl3.2 - 官方demo學(xué)習(xí) - sconnect.c的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!