1

我已经从网上找到的一些教程中编写了一组简单的 SSL 客户端/服务器程序 - 这些工作正常。我无法理解的是事物的客户端(见下文)

从客户端连接到 SSL 服务器的代码来看,它似乎盲目地接受了它提供的证书,然后使用它来加密/解密发送到和从该对发送的数据。

不应该有客户端验证服务器证书以供使用吗?我的意思是我可以用任何新的证书更改/交换服务器端证书,并让它连接起来而不需要太多的wimper?这是一种安全的连接方法吗?(或者我 - 我怀疑 - 错过了什么)

非常感谢

FR

void LoadCertificates(SSL_CTX* ctx, char* CertFile, char* KeyFile);
int OpenConnection(const char *hostname, int port);
void ShowCerts(SSL* ssl);
SSL_CTX* InitCTX(void);


int main(int count, char *strings[]) {


    char *hostname, *portnum;
    char buf[1024];
    SSL_CTX *ctx;
    SSL *ssl;
    int server;
    int bytes;


    if ( count != 3 ) {


        printf("usage: %s <hostname> <portnum>\n", strings[0]);
        exit(0);


    } // if


    hostname=strings[1];
    portnum=strings[2];


    printf("\nSSL Client 0.1\n~~~~~~~~~~~~~~~\n\n");


    // Init. the SSL lib
    SSL_library_init();
    ctx = InitCTX();


    printf("Client SSL lib init complete\n");


    // Open the connection as normal
    server = OpenConnection(hostname, atoi(portnum));


    // Create new SSL connection state
    ssl = SSL_new(ctx);


    // Attach the socket descriptor
    SSL_set_fd(ssl, server);


    // Perform the connection
    if ( SSL_connect(ssl) != FAIL ) {


        char *msg = "Here is some data";


        printf("Connected with %s encryption\n", SSL_get_cipher(ssl));

        // Print any certs
        ShowCerts(ssl);


        // Encrypt & send message */
        SSL_write(ssl, msg, strlen(msg));


        // Get reply & decrypt
        bytes = SSL_read(ssl, buf, sizeof(buf));


        buf[bytes] = 0;
        printf("Received: '%s'\n\n", buf);


        // Release connection state
        SSL_free(ssl);


    } // if


    else ERR_print_errors_fp(stderr);


    // Close socket
    close(server);


    // Release context
    SSL_CTX_free(ctx);
    return 0;


} // main


SSL_CTX* InitCTX(void) {


    SSL_METHOD const *method;
    SSL_CTX *ctx;


    // Load cryptos, et.al.
    OpenSSL_add_all_algorithms();


    // Bring in and register error messages
    SSL_load_error_strings();


    // Create new client-method instance
    method = SSLv3_client_method();


    // Create new context
    ctx = SSL_CTX_new(method);


    if ( ctx == NULL ) {


        ERR_print_errors_fp(stderr);
        abort();


    } // if


    return ctx;


} //InitCTX


int OpenConnection(const char *hostname, int port) {


    int sd;
    struct hostent *host;
    struct sockaddr_in addr;


    if ( (host = gethostbyname(hostname)) == NULL ) {


        perror(hostname);
        abort();


    } // if


    sd = socket(PF_INET, SOCK_STREAM, 0);
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = *(long*)(host->h_addr);


    if ( connect(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) {


        close(sd);
        perror(hostname);
        abort();


    } // if


    return sd;


} // OpenConnection


void ShowCerts(SSL* ssl) {


    X509 *cert;
    char *line;


    cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */


    if ( cert != NULL ) {


        printf("\nServer certificate:\n");
        line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
        printf("Subject: %s\n", line);


        // Free the malloc'ed string
        free(line);
        line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
        printf("Issuer: %s\n", line);


        // Free the malloc'ed string
        free(line);


        // Free the malloc'ed certificate copy
        X509_free(cert);


    } // if


    else printf("No certificates.\n");


} // ShowCerts
4

2 回答 2

2

“安全连接”可以被视为由两件事组成:

1 身份验证:确保连接到期望连接的伙伴(这里:服务器)

2 加密:保护数据(由连接传输)在传输过程中不被读取

从服务器收到的证书可以用来完成1。证书不一定参与2。

关于如何将服务器证书的验证添加到您的客户端,您可能想查看这个 SO 答案:https ://stackoverflow.com/a/12245023/694576 (无论如何您都可以首先完成... ;->)

有关 TLS(SSL 的继任者和前 SSL 3.x)的详细信息,请参见此处:http ://en.wikipedia.org/wiki/Transport_Layer_Security

于 2012-09-17T20:24:56.350 回答
2

你是对的。连接是安全的(不被窃听),具有完整性(防止注入、截断和修改)和身份验证(对等方是他在证书中说的那个人),所有这些都是由传输自动为您完成的,但它仍然缺乏授权(这是我想与之交谈的人吗?这个人在我的应用程序中究竟被允许做什么)。本质上,只有应用程序才能做到这一点,因此您可以获取对等证书、获取其 SubjectDN、将其与某个本地用户数据库相关联、检查是否存在、检查其角色等。

于 2012-09-17T21:53:43.070 回答