一些神秘地不起作用的OpenSSL源代码

Some OpenSSL source that mysteriously doesn't work

本文关键字:OpenSSL 源代码 不起作用      更新时间:2023-10-16

这让我爬上了墙。我找不出这个源为什么不打开套接字。这很简单,但不起作用。有人能帮我做这个吗?感谢您的考虑!BTW:我在屏幕上没有得到任何文本,它用BIO_do_accept()函数阻塞。

#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
#include <iostream>
#include <process.h>
using namespace std;
int main()  {
    SSL_load_error_strings();
    SSL_library_init();
    OpenSSL_add_all_algorithms();
    BIO *abio, *cbio, *cbio2;
    ERR_load_crypto_strings();
    abio = BIO_new_accept("4444");
    /* First call to BIO_accept() sets up accept BIO */
    if(BIO_do_accept(abio) <= 0) {
        fprintf(stderr, "Error setting up acceptn");
        ERR_print_errors_fp(stderr);
        exit(0);
    }
    /* Wait for incoming connection */
    if(BIO_do_accept(abio) <= 0) {
        fprintf(stderr, "Error accepting connectionn");
        ERR_print_errors_fp(stderr);
        exit(0);
    }
    fprintf(stderr, "Connection 1 establishedn");
    /* Retrieve BIO for connection */
    cbio = BIO_pop(abio);
    BIO_puts(cbio, "Connection 1: Sending out Data on initial connectionn");
    fprintf(stderr, "Sent out data on connection 1n");
}

我刚刚测试了这个(在cygwin上,安装了gcc 4.5.3和openssl-devel 1.0.1)

您在聊天中发布的代码使用编译

g++ -std=c++0x ./test.cpp -lssl -lcrypto  -o test

生成的代码显然不起作用,因为该代码引用了server.crtserver.key:

openssl genrsa -out server.key 1024
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

使用未受保护的密钥创建自签名证书(您可以使用genrsa -des3向密钥添加密码短语)。

现在,我可以正确地测试它:

test& # in the background
openssl s_client -connect localhost:12120

这使您能够使用一种支持SSL的telnet客户端,并且工作得很好。