2016-11-25 2 views
0

Ich mag erstellen verschlüsselten Chat-Server mit OpenSSL in C erstellen haben so, den Code aus genommen: http://simplestcodings.blogspot.in/2010/08/secure-server-client-using-openssl-in-c.htmlWie Chat-Server mit OpenSSL in c

Ich mag diesen Code zu gleichzeitigem Chat-Server konvertieren, aber wie kann ich mach das?

wenn ich versuche Client mit dem Server zu verbinden, es ist nur zeigt:

auf Server-Seite:

Connection: 127.0.0.1:34902 
No certificates. 
Client msg: "Hello???" 

auf Client-Seite:

Connected with AES256-GCM-SHA384 encryption 
Server certificates: 
Subject: /C=IN/ST=UK/L=DDN/O=UPES/OU=IT/CN=KAVIN/[email protected] 
Issuer: /C=IN/ST=UK/L=DDN/O=UPES/OU=IT/CN=KAVIN/[email protected] 
Received: "<html><body><pre>Hello???</pre></body></html> 

" 

Ich möchte tun verschlüsselter Chat zwischen Client und Server?

server.c

#include <stdio.h> 
#include <errno.h> 
#include <unistd.h> 
#include <malloc.h> 
#include <string.h> 
#include <sys/socket.h> 
#include <resolv.h> 
#include <netdb.h> 
#include <openssl/ssl.h> 
#include <openssl/err.h> 

#define FAIL -1 

int OpenConnection(const char *hostname, int port) 
{ int sd; 
    struct hostent *host; 
    struct sockaddr_in addr; 

    if ((host = gethostbyname(hostname)) == NULL) 
    { 
     perror(hostname); 
     abort(); 
    } 
    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(); 
    } 
    return sd; 
} 

SSL_CTX* InitCTX(void) 
{ SSL_METHOD *method; 
    SSL_CTX *ctx; 

    OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */ 
    SSL_load_error_strings(); /* Bring in and register error messages */ 
    method = TLSv1_2_client_method(); /* Create new client-method instance */ 
    ctx = SSL_CTX_new(method); /* Create new context */ 
    if (ctx == NULL) 
    { 
     ERR_print_errors_fp(stderr); 
     abort(); 
    } 
    return ctx; 
} 

void ShowCerts(SSL* ssl) 
{ X509 *cert; 
    char *line; 

    cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */ 
    if (cert != NULL) 
    { 
     printf("Server certificates:\n"); 
     line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); 
     printf("Subject: %s\n", line); 
     free(line);  /* free the malloc'ed string */ 
     line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); 
     printf("Issuer: %s\n", line); 
     free(line);  /* free the malloc'ed string */ 
     X509_free(cert);  /* free the malloc'ed certificate copy */ 
    } 
    else 
     printf("Info: No client certificates configured.\n"); 
} 

int main(int count, char *strings[]) 
{ SSL_CTX *ctx; 
    int server; 
    SSL *ssl; 
    char buf[1024]; 
    int bytes; 
    char *hostname, *portnum; 

    if (count != 3) 
    { 
     printf("usage: %s <hostname> <portnum>\n", strings[0]); 
     exit(0); 
    } 
    SSL_library_init(); 
    hostname=strings[1]; 
    portnum=strings[2]; 

    ctx = InitCTX(); 
    server = OpenConnection(hostname, atoi(portnum)); 
    ssl = SSL_new(ctx);  /* create new SSL connection state */ 
    SSL_set_fd(ssl, server); /* attach the socket descriptor */ 
    if (SSL_connect(ssl) == FAIL) /* perform the connection */ 
     ERR_print_errors_fp(stderr); 
    else 

    { char *msg = "Hello???"; 

     printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); 
     ShowCerts(ssl); 
     /* get any certs */ 
    while(1){ 
     SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */ 
     bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */ 
     buf[bytes] = 0; 
     printf("Received: \"%s\"\n", buf); 
     SSL_free(ssl);  /* release connection state */ 

} 
    close(server);   /* close socket */ 
    SSL_CTX_free(ctx);  /* release context */ 

    return 0; 
} 

client.c

#include <stdio.h> 
#include <errno.h> 
#include <unistd.h> 
#include <malloc.h> 
#include <string.h> 
#include <sys/socket.h> 
#include <resolv.h> 
#include <netdb.h> 
#include <openssl/ssl.h> 
#include <openssl/err.h> 

#define FAIL -1 

int OpenConnection(const char *hostname, int port) 
{ int sd; 
    struct hostent *host; 
    struct sockaddr_in addr; 

    if ((host = gethostbyname(hostname)) == NULL) 
    { 
     perror(hostname); 
     abort(); 
    } 
    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(); 
    } 
    return sd; 
} 

SSL_CTX* InitCTX(void) 
{ SSL_METHOD *method; 
    SSL_CTX *ctx; 

    OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */ 
    SSL_load_error_strings(); /* Bring in and register error messages */ 
    method = TLSv1_2_client_method(); /* Create new client-method instance */ 
    ctx = SSL_CTX_new(method); /* Create new context */ 
    if (ctx == NULL) 
    { 
     ERR_print_errors_fp(stderr); 
     abort(); 
    } 
    return ctx; 
} 

void ShowCerts(SSL* ssl) 
{ X509 *cert; 
    char *line; 

    cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */ 
    if (cert != NULL) 
    { 
     printf("Server certificates:\n"); 
     line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); 
     printf("Subject: %s\n", line); 
     free(line);  /* free the malloc'ed string */ 
     line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); 
     printf("Issuer: %s\n", line); 
     free(line);  /* free the malloc'ed string */ 
     X509_free(cert);  /* free the malloc'ed certificate copy */ 
    } 
    else 
     printf("Info: No client certificates configured.\n"); 
} 

int main(int count, char *strings[]) 
{ SSL_CTX *ctx; 
    int server; 
    SSL *ssl; 
    char buf[1024]; 
    int bytes; 
    char *hostname, *portnum; 

    if (count != 3) 
    { 
     printf("usage: %s <hostname> <portnum>\n", strings[0]); 
     exit(0); 
    } 
    SSL_library_init(); 
    hostname=strings[1]; 
    portnum=strings[2]; 

    ctx = InitCTX(); 
    server = OpenConnection(hostname, atoi(portnum)); 
    ssl = SSL_new(ctx);  /* create new SSL connection state */ 
    SSL_set_fd(ssl, server); /* attach the socket descriptor */ 
    if (SSL_connect(ssl) == FAIL) /* perform the connection */ 
     ERR_print_errors_fp(stderr); 
    else 

    { char *msg = "Hello???"; 

     printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); 
     ShowCerts(ssl); 
     /* get any certs */ 
    while(1){ 
     SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */ 
     bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */ 
     buf[bytes] = 0; 
     printf("Received: \"%s\"\n", buf); 
     SSL_free(ssl);  /* release connection state */ 

} 
    close(server);   /* close socket */ 
    SSL_CTX_free(ctx);  /* release context */ 

    return 0; 
} 

Bitte beachten Sie die Änderungen im Code Kommentar Code zu verschlüsselten Chat-Server

Antwort

2

Wie Sie bereits für die Umwandlung sind SSL-Funktionalität , Ihr Chat-Programm ist tatsächlich gesichert. Aber von Ihrem Post aus sieht es so aus, als wollten Sie Ihre sichere Verbindung verbessern. Für die Verschlüsselung und Entschlüsselung der Umsetzung können Sie versuchen,

// Encryption

AES_KEY encKey; 
AES_set_encrypt_key(key, 128, &encKey); 
AES_encrypt(text, out, &encKey); 

// Decryption

AES_KEY decKey; 
AES_set_decrypt_key(key,128,&decKey); 
AES_decrypt(out, text, &decKey); 

Hinweis: key sollte

+0

Eigentlich möchte ich senden und empfangen gemeinsam sein Nachrichten zwischen Cilent und Server, so dass es wie eine verschlüsselte Chat-App funktioniert Da im obigen Code ich nicht senden und empfangen kann s es ist nur zu sagen ("Hallo") – kavin

+2

Die Logik besteht darin, "Hallo" Nachricht zu verschlüsseln und dann die verschlüsselte Nachricht zu senden. Jetzt auf der anderen Seite mit dem gemeinsamen Schlüssel entschlüsseln Sie die Nachricht. Wenn irgendein Betrug/Mann in mittleren Angriffen hereinkommt, wird eine verschlüsselte Nachricht geliefert. Es wird unmöglich sein, die Nachricht ohne gemeinsamen Schlüssel zu entschlüsseln. Dies sind die Funktionen für die Verschlüsselung und Entschlüsselung. Sie können es in Ihrem Code verwenden. –

+0

Dies wirft die Frage auf: Wie werden die Schlüssel zwischen den beiden Geräten geteilt und wie wird das Gerät/die Person authentifiziert? – zaph