0

在用 C 编码的 SSL 服务器中,我试图捕获浏览器标头,服务器在 linux 机器上的端口 443 上运行,还通过在 Windows\System32\drivers\etc\ 的主机文件中添加服务器名称来解析主机名主机 SSL 握手似乎成功,但 SSL_read 失败,请帮助,还请在下面找到服务器信息输出:

ssl = SSL_new(ctx);

    RETURN_NULL(ssl);


    if(SSL_set_fd(ssl, client_s)<0)
        printf("\n error in assigning socket to SSL:");
    else
        printf("\n The socket has been assigned to SSL Structure");

    /* Perform SSL Handshake on the SSL server */
    err = SSL_accept(ssl);
    printf("\n Value of err is %d",err);
    RETURN_ERR(err,"SSL_accept");
    if(err==1)
        printf("\n The ssl connection/Handshake has been successful");
    else
        printf("\n The ssl connection was not successful");

    /* Informational output (optional) */
        printf("\n SSL connection using %s\n", SSL_get_cipher (ssl));


    /*receive the data from the client*/
    //err = SSL_accept(ssl);
    while(i<5)
    {
        err = SSL_read(ssl, in_buf, strlen(in_buf));
        printf("\n value of err is %d",err);
        RETURN_ERR(err,"SSL_read");

        printf("\n The details from the server is\n: %s,\n Bytes Read : %d",in_buf,err);
        if(err<0)
        printf("\n Not Successfully received clients information");
        i++;
     }
     err = SSL_shutdown(ssl);
    /* Terminate communication on a socket */
    err = close(server_s);
    /* Free the SSL structure */
    SSL_free(ssl);
    /* Free the SSL_CTX structure */
    SSL_CTX_free(ctx);

    return(0);
    }

我使用 IE (url: https://myserver.com/test2.jpg ) 得到的上述代码的输出如下:

root@unnidevelubuntu:/programs# gcc -lssl trial11.c -o trial11
root@unnidevelubuntu:/programs# ./trial11

 Available ciphers and digests has been registered :
 New SSL_CTX object created successfully :
Enter PEM pass phrase:

 The key & Certificate has been loaded successfully :
 Server is waiting for a TCP/IP Connection :
 private key matches the certificate public key :server is ready ...

 Connection from 192.168.0.15, port 56969
a new client arrives ...

 The socket has been assigned to SSL Structure
 Value of err is 1
 The ssl connection/Handshake has been successful
 SSL connection using RC4-SHA

 value of err is 0
 The details from the server is
: ,
 Bytes Read : 0
 value of err is 0
 The details from the server is
: ,
 Bytes Read : 0
 value of err is 0
 The details from the server is
: ,
 Bytes Read : 0
 value of err is 0
 The details from the server is
4

1 回答 1

0

上面的问题已经解决了,问题是在SSL_read中使用strlen如下: err = SSL_read(ssl, in_buf, strlen(in_buf)); printf("\n err 的值为 %d",err); RETURN_ERR(err,"SSL_read");

您需要使用 sizeof 而不是 SSL_read,并且程序运行良好,它为您提供来自浏览器的标题。

于 2012-09-24T12:45:36.560 回答