2012-09-17 167 views
0

我已经在网上找到了几个教程编写了一套简单的SSL客户端/服务器程序 - 而且这些工作正常。我无法得到我的头是客户端的东西(见下文)尝试使用SSL连接

它看起来好像从客户端连接到SSL服务器的代码,盲目接受它提供的证书,然后用它来加密/解密发送和接收数据的数据。

是否应该没有客户端验证服务器证书的使用?我的意思是我可以用任何新的服务器端证书来更换/交换服务器端证书,并让它连接起来没有那么多润色剂?这是一种安全的连接方式? (还是我 - 我怀疑 - 缺少的东西)

非常感谢

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 

回答

2

你是对的。连接是安全的(从窃听),具有完整性(防止注入,截断和修改)和身份验证(同行是他说他在证书中的人),所有这些都是由交通工具自动为您完成的,但它仍然缺乏授权(这是我想与之交谈的人,以及此人在我的申请中的确切位置)。本质上,只有应用程序可以这样做,所以取决于您的对等证书,获得其SubjectDN,将其与某个本地用户数据库相关联,检查是否存在,检查其角色等。

2

A “安全连接” 可以看作是由两件​​事情:

1认证:请务必连接到合作伙伴(此处:服务器),预计连接到

2加密:让数据(由连接传输)再次受到保护

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

关于如何将服务器证书的验证添加到您的客户端T是在传输过程中阅读,你可能喜欢看看这个SO回答:https://stackoverflow.com/a/12245023/694576(你可以在第一个地方完成......; - >)

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

+0

它被视为四件事:看我的答案。 – EJP

+0

感谢您的链接:)所以我会更好地实施TLS而不是SSL来确保在可预见的未来有一个很好的安全连接? – FiniteRed

+0

它只是名称。 'TLS 1.0'与'SSL 3.1'相同。 'TLS'是'SSL'的继承者。只要阅读我链接的维基百科文章,你就会看到...... ;-) @FiniteRed – alk