SSL_write 默认行为

SSL_write default behavior

目前我正在研究一些基本的 OpenSSL。我对 SSL_write 和 SSL_read 的基本行为感到困惑。在文档中没有清楚地解释数据是如何加密/解密的。我的意思是? SSL_write,一旦设置了 SSL_CTX 并分配了文件描述符,默认加密数据或调用加密函数,还是必须手动完成?我必须明确地调用一些加密功能吗? SSL_read也是这样吗?我需要更深入地了解 SSL_write / read 自动执行的操作和不自动执行的操作,或者如果我遇到问题可以回退到哪些资源。

这是我正在使用的示例。

示例:

    SSL *ssl;

    int client = accept(server, (struct sockaddr*)&addr, &len);  /* accept connection as usual */
    printf("Connection: %s:%d\n",inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
    ssl = SSL_new(ctx);              /* get new SSL state with context */
    SSL_set_fd(ssl, client);      /* set connection socket to SSL state */
    Servlet(ssl);         /* service connection */

Servlet 调用

void Servlet(SSL* ssl) 
{
    char buf[1024];
    char reply[1024];
    int sd, bytes;
    const char* HTMLecho="<html><body><pre>%s</pre></body></html>\n\n";

    if ( SSL_accept(ssl) == FAIL )     /* do SSL-protocol accept */
        ERR_print_errors_fp(stderr);
    else
    {
        ShowCerts(ssl);        /* get any certificates */
        bytes = SSL_read(ssl, buf, sizeof(buf)); /* get request */
        if ( bytes > 0 )
        {
            buf[bytes] = 0;
            printf("Client msg: \"%s\"\n", buf);
            sprintf(reply, HTMLecho, buf);   /* construct reply */
            SSL_write(ssl, reply, strlen(reply)); /* send reply */
        }
        else
            ERR_print_errors_fp(stderr);
    }
    sd = SSL_get_fd(ssl);       /* get socket connection */
    SSL_free(ssl);         /* release SSL state */
    close(sd);          /* close connection */
}

Do I have to call some encryption function explicitly, is the same true for the SSL_read?

没有。加密在 SSL_write 中自动处理,解密在 SSL_read 中自动处理。一旦 TLS 握手完成,两者都使用包含必要加密密钥的 SSL 结构。

... sources where I can fall back to if I'm having issues

这取决于您遇到的问题。 SSL_readSSL_write 都有文档。也有很容易找到的例子。能够使用搜索引擎也有助于找到更多信息。如果事情没有按预期工作,Whosebug 是获得帮助的好地方。