如何初始化OpenSSL BIO对象?

时间:2012-07-15 21:39:15

标签: c++ openssl

我有一个tcp服务器,它检测传入的SSL连接(请参阅here),然后执行以下操作:

BIO* initialize(SSL_CTX *context, int socket){
    BIO *bio = NULL;
    SSL *ssl = SSL_new(context);
    SSL_set_fd(ssl, socket);
    if (SSL_accept(ssl) == -1){
        return NULL; //error
    }
    //what do I do here??
    bio = BIO_new_ssl(context, 1); //this seems wrong...
    return bio;
}

我不知道如何创建BIO对象,文档真的很混乱。任何帮助表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:1)

这是我的一个老学生项目的摘录(大约2006年)。 我希望它能够对这个问题有所了解。 我不使用BIO_new_ssl()而是使用SSL_set_bio()。

SSL_CTX *ctx = setup_server_ctx("root.pem", NULL);
SSL_CTX *ssl = SSL_new(ctx);
if (NULL == ssl) {
   fprintf(stderr, "Error creating SSL context.\n")
   goto err;
}
BIO *acc = BIO_new_accept(port);

if (BIO_do_accept(acc) <= 0) {
   fprintf(stderr, "Error accepting connection.\n");
   goto err;
}

BIO *client = BIO_pop(acc);
SSL_set_bio(ssl, client, client);

if (0 >= SSL_accept(ssl)) {
    fprintf(stderr, "Error accepting SSL connection\n");
    goto end;
}   

SSL_write(ssl, SOME_MESSAGE, strlen(SOME_MESSAGE));
char buf[BUF_SIZE + 1]= {0};
int ret = SSL_read(ssl, buf, BUF_SIZE);
if (ret <= 0) { 
   break;
}   

/* do some more stuff */

SSL_get_shutdown(ssl);