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

这篇具有很好参考价值的文章主要介绍了openssl3.2 - 官方demo学习 - sconnect.c。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

概述

TLS客户端 - 使用根证书, 非阻塞, 向服务器要东西.

笔记

开始一个新demo学习时, 要从头配置包含路径, 麻烦. 直接拷贝上一个实现工程, 换掉实现.c方便一些.
换的新demo实现, 要加入库包含和头包含, 麻烦, 做一个公用头文件, 直接include方便一些.文章来源地址https://www.toymoban.com/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客户端 - 使用根证书, 非阻塞, 向服务器要东西.
*/

/*
 * 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

到了这里,关于openssl3.2 - 官方demo学习 - sconnect.c的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • openssl3.2 - 官方demo学习 - mac - gmac.c

    使用GMAC算法, 设置参数(指定加密算法 e.g. AES-128-GCM, 设置iv) 用key执行初始化, 然后对明文生成MAC数据 官方注释给出建议, key, iv最好不要硬编码出现在程序中

    2024年01月16日
    浏览(34)
  • openssl3.2 - 官方demo学习 - kdf - scrypt.c

    设置 kdf-SCRYPT算法的参数, 取key

    2024年01月16日
    浏览(32)
  • openssl3.2 - 官方demo学习 - smime - smdec.c

    从pem证书中得到x509*和私钥, 用私钥和证书解密MIME格式的PKCS7密文, 并保存解密后的明文 MIME的数据操作, 都是PKCS7相关的

    2024年01月18日
    浏览(31)
  • openssl3.2 - 官方demo学习 - smime - smenc.c

    读取X509证书, 用PKCS7加密明文(证书 + 明文 + 3DES_CBC), 保存为MIME格式的密文 openssl API的命名含义 BIO_new_file “new” a “file”, return a “BIO” object PEM_read_bio_X509() Read a certificate in PEM format from a BIO data format is “PEM”, “read” from “bio”, return a object type is “X509”

    2024年01月20日
    浏览(38)
  • openssl3.2 - 官方demo学习 - mac - siphash.c

    MAC算法为 SIPHASH, 设置参数(C-rounds, D-rounds, 也可以不设置, 有默认值) 用key初始化MAC算法, 算明文的MAC值

    2024年01月19日
    浏览(41)
  • openssl3.2 - 官方demo学习 - smime - smver.c

    对于签名文件(不管是单独签名, 还是联合签名), 都要用顶层证书进行验签(靠近根CA的证书) 读证书文件, 得到x509*, 添加到证书容器 读取签名密文, 得到pkcs7*和密文的bio 进行pkcs7验签, 并将验签得到的签名的明文写到文件.

    2024年01月19日
    浏览(34)
  • openssl3.2 - 官方demo学习 - kdf - hkdf.c

    设置摘要算法HKDF的参数, 然后取key

    2024年01月17日
    浏览(33)
  • openssl3.2 - 官方demo学习 - cipher - aesgcm.c

    AES-256-GCM 在这个实验中验证了EVP_CIPHER_fetch()中算法名称字符串的来源定位. 在工程中配置环境变量PATH, 且合并环境. 这样就不用将openSSL的DLL和配置文件拷贝到工程里面了. 提交代码时, 就能节省很多空间. 配置工程调试时, 也顺畅多了.

    2024年01月22日
    浏览(38)
  • openssl3.2 - 官方demo学习 - smime - smsign.c

    从证书中得到X509*和私钥指针 用证书和私钥对铭文进行签名, 得到签名后的pkcs7指针 将pkcs7指向的bio_in, 写为MIME格式的签名密文 BIO_reset() 可以将一个bio恢复到刚打开的状态(应该就是将文件指针重新指向文件头部), 一般用于只读打开的场景 经常用于多个对象要操作同一个bio的场

    2024年01月19日
    浏览(33)
  • openssl3.2 - 官方demo学习 - keyexch - x25519.c

    官方程序中演示了私钥2种key交换的情况: 产生X25519的key对(私钥/公钥), 并交换公钥给对方, 并分别产生会话密钥, 使双方都能持有相同的会话密钥 产生X25519的key对(私钥/公钥)时, 产生私钥时, 可以随机产生. 这个私钥是为会话准备的, 然后根据会话私钥产生会话公钥. 然后交换公

    2024年01月16日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包