调用BIO_free_all()时是否释放了SSL_CTX对象?

时间:2016-09-18 23:09:59

标签: c openssl free

许多客户端/服务器示例显示使用SSL_CTX来初始化SSL端,但不显示正在释放的ctx变量。阅读文档,目前尚不清楚上下文是否属于链条的一部分,我会说不是因为它不是BIO对象,但我不太清楚该怎么想。

以下是man BIO_new_ssl输出中的代码示例。我们可以看到,他们在开始时使用以下内容分配上下文:

ctx = SSL_CTX_new(SSLv23_server_method());

但我在任何地方都看不到任何SSL_CTX_free(ctx)。但是,它的上下文附加到sbio,它也附加到bbioacpt(是的,它是一个很大的混乱,它们会为不同的东西重用相同的变量!)他们打电话给BIO_free_all(acpt)。那个调用也会释放ctx变量指针吗?或者这只是例子中的遗漏? (即如果我打电话给自由,我最终会得到双free()错误吗?)

    BIO *sbio, *bbio, *acpt, *out;
    int len;
    char tmpbuf[1024];
    SSL_CTX *ctx;
    SSL *ssl;

    ERR_load_crypto_strings();
    ERR_load_SSL_strings();
    OpenSSL_add_all_algorithms();

    /* Might seed PRNG here */

    ctx = SSL_CTX_new(SSLv23_server_method());

    if (!SSL_CTX_use_certificate_file(ctx,"server.pem",SSL_FILETYPE_PEM)
           || !SSL_CTX_use_PrivateKey_file(ctx,"server.pem",SSL_FILETYPE_PEM)
           || !SSL_CTX_check_private_key(ctx)) {

           fprintf(stderr, "Error setting up SSL_CTX\n");
           ERR_print_errors_fp(stderr);
           return 0;
    }

    /* Might do other things here like setting verify locations and
     * DH and/or RSA temporary key callbacks
     */

    /* New SSL BIO setup as server */
    sbio=BIO_new_ssl(ctx,0);

    BIO_get_ssl(sbio, &ssl);

    if(!ssl) {
      fprintf(stderr, "Can't locate SSL pointer\n");
      /* whatever ... */
    }

    /* Don't want any retries */
    SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

    /* Create the buffering BIO */

    bbio = BIO_new(BIO_f_buffer());

    /* Add to chain */
    sbio = BIO_push(bbio, sbio);

    acpt=BIO_new_accept("4433");

    /* By doing this when a new connection is established
     * we automatically have sbio inserted into it. The
     * BIO chain is now 'swallowed' by the accept BIO and
     * will be freed when the accept BIO is freed.
     */

    BIO_set_accept_bios(acpt,sbio);

    out = BIO_new_fp(stdout, BIO_NOCLOSE);

    /* Setup accept BIO */
    if(BIO_do_accept(acpt) <= 0) {
           fprintf(stderr, "Error setting up accept BIO\n");
           ERR_print_errors_fp(stderr);
           return 0;
    }

    /* Now wait for incoming connection */
    if(BIO_do_accept(acpt) <= 0) {
           fprintf(stderr, "Error in connection\n");
           ERR_print_errors_fp(stderr);
           return 0;
    }

    /* We only want one connection so remove and free
     * accept BIO
     */

    sbio = BIO_pop(acpt);

    BIO_free_all(acpt);

    if(BIO_do_handshake(sbio) <= 0) {
           fprintf(stderr, "Error in SSL handshake\n");
           ERR_print_errors_fp(stderr);
           return 0;
    }

    BIO_puts(sbio, "HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n");
    BIO_puts(sbio, "\r\nConnection Established\r\nRequest headers:\r\n");
    BIO_puts(sbio, "--------------------------------------------------\r\n");

    for(;;) {
           len = BIO_gets(sbio, tmpbuf, 1024);
           if(len <= 0) break;
           BIO_write(sbio, tmpbuf, len);
           BIO_write(out, tmpbuf, len);
           /* Look for blank line signifying end of headers*/
           if((tmpbuf[0] == '\r') || (tmpbuf[0] == '\n')) break;
    }

    BIO_puts(sbio, "--------------------------------------------------\r\n");
    BIO_puts(sbio, "\r\n");

    /* Since there is a buffering BIO present we had better flush it */
    BIO_flush(sbio);

    BIO_free_all(sbio);

0 个答案:

没有答案