Related configuration, etc. please see an article about server side articles: http://blog.csdn.net/pingd/article/details/47805349
1.Client Terminal Source:
Openssl_client.c
#include <stdio.h> #include <string.h> #include <errno.h> #include <sys/socket.h> #include < resolv.h> #include <stdlib.h> #include <netinet/in.h> #include <arpa/inet.h> #include < unistd.h> #include <openssl/ssl.h> #include <openssl/err.h> #define MAXBUF 1024void showcerts (SSL * SSL) { X509 *cert; Char *line; Cert = Ssl_get_peer_certificate (SSL); if (cert! = NULL) {printf ("Digital cert info:\n"); line = X509_name_oneline (X509_get_subject_name (cert), 0, 0); printf ("Cert:%s\n", line); (line); line = X509_name_oneline (X509_get_issuer_name (cert), 0, 0); printf ("Owner:%s\n", line); (line); X509_free (CERT); } else printf ("No cert info\n");} int main (int argc, char **argv) {int sockfd, Len; struct sockaddr_in dest; Char buffer[maxbuf + 1]; Ssl_ctx *ctx; SSL *ssl; if (argc! = 3) {printf ("usage:%s [server_ip] [server_port]\n", argv[0]); Exit (-1); }/* SSL library initialization, see SSL-SERVER.C Code */Ssl_library_init (); Openssl_add_all_algorithms (); Ssl_load_error_strings (); CTX = Ssl_ctx_new (Sslv23_client_method ()); if (CTX = = NULL) {ERR_PRINT_ERRORS_FP (stdout); Exit (-1); }/* Create a socket for TCP communication */if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0) {perror ("socket"); Exit (errno); } printf ("Socket created\n"); /* Initialize the address and port information for the server side (each other) */bzero (&dest, sizeof (dest)); dest.sin_family = af_inet; Dest.sin_port = htons (Atoi (argv[2])); if (Inet_aton (argv[1], (struct in_addr *) &dest.sin_addr.s_addr) = = 0) {perror (argv[1]); Exit (errno); } printf ("Address created\n"); /* Connect Server */if (Connect (SOCKFD, (struct sockaddr *) &dest, sizeof (dest))! = 0) {perror ("connect"); Exit (errno); } printf ("Server connected\n"); /* Generate a new SSL */SSL = Ssl_new (CTX) based on CTX; SSL_SET_FD (SSL, SOCKFD);/* Establish an SSL connection */if (ssl_connect (SSL) = =-1) err_print_errors_fp (stderr); else {printf ("Connected with%s encryption\n", Ssl_get_cipher (SSL)); Showcerts (SSL); */* receive messages from each other, receive up to maxbuf bytes */bzero (buffer, maxbuf + 1); /* Message received from server */len = ssl_read (SSL, buffer, maxbuf); if (len > 0) printf ("recv message '%s ' ok.total size: '%d ' \ n", buffer, Len); else {printf ("recv Message Error!error code: '%d ', error info: '%s ' \ n", errno, Strerror (errno)); Goto finish; } bzero (buffer, maxbuf + 1); strcpy (buffer, "from Client->server"); /* Send message to server */len = ssl_write (SSL, buffer, strlen (buffer)); if (Len < 0) printf ("Send Message '%s ' Error!error code: '%d ', error info: '%s ' \ n", buffer, errno, stre Rror (errno)); else printf ("Send Message '%s ' ok,total size: '%d ' \ n", buffer, Len); Finish:/* Close connection */Ssl_shutdown (SSL); Ssl_free (SSL); Close (SOCKFD); Ssl_ctx_free (CTX); return 0;}
2. Compiling
3. Running
usage:openssl_client [SERVER_IP] [Server_port]
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux OpenSSL programming client side