openssl3.2 - 官方dmeo学习 - sconnect.c

openssl3.2 - 官方dmeo学习 - sconnect.c

概述

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;
}

END

相关推荐

  1. openssl3.2 - 官方dmeo学习 - sconnect.c

    2024-01-12 07:58:02       53 阅读
  2. openssl3.2 - 官方demo学习 - cipher - aesccm.c

    2024-01-12 07:58:02       42 阅读
  3. openssl3.2 - 官方demo学习 - server-conf.c

    2024-01-12 07:58:02       53 阅读
  4. openssl3.2 - 官方demo学习 - client-conf.c

    2024-01-12 07:58:02       56 阅读
  5. openssl3.2 - 官方demo学习 - cipher - ariacbc.c

    2024-01-12 07:58:02       49 阅读
  6. openssl3.2 - 官方demo学习 - mac - gmac.c

    2024-01-12 07:58:02       59 阅读
  7. openssl3.2 - 官方demo学习 - mac - siphash.c

    2024-01-12 07:58:02       56 阅读
  8. openssl3.2 - 官方demo学习 - cms - cms_enc.c

    2024-01-12 07:58:02       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-12 07:58:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-12 07:58:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-12 07:58:02       87 阅读
  4. Python语言-面向对象

    2024-01-12 07:58:02       96 阅读

热门阅读

  1. 本地由LeetCode输入构建二叉树(C++版)

    2024-01-12 07:58:02       51 阅读
  2. 探索计算机网络:应用层的魅力

    2024-01-12 07:58:02       55 阅读
  3. 计算机网络层之ICMP与IGMP

    2024-01-12 07:58:02       56 阅读
  4. 决策树回归(Decision Tree Regression)

    2024-01-12 07:58:02       59 阅读
  5. 视觉SLAM十四讲|【五】相机与IMU时间戳同步

    2024-01-12 07:58:02       52 阅读
  6. 自然语言处理(NLP)技术

    2024-01-12 07:58:02       51 阅读
  7. 使用Sqoop将Hive数据导出到TiDB

    2024-01-12 07:58:02       53 阅读
  8. k8s存储卷和数据卷下

    2024-01-12 07:58:02       46 阅读