Use openssl to write server and client programs

Source: Internet
Author: User

The following is an overview of how to use openssl for programming.

1. Use the same ca to generate two certificates: server. cer and client. cer. Note that when generating server. cer, you must specify that the certificate can be used on the server.

Server code:

 

 
 
  1. # Include "openssl/bio. h"
  2. # Include "openssl/ssl. h"
  3. # Include "openssl/err. h"
  4.  
  5. # Include <cutil. h>
  6.  
  7. # Define EXIT_IF_TRUE (x) if (x )\
  8. Do {\
  9. Fprintf (stderr, "Check '% s' is true \ n", # x );\
  10. ERR_print_errors_fp (stderr );\
  11. Exit (2 );\
  12. } While (0)
  13.  
  14. Int main (int argc, char ** argv)
  15. {
  16. SSL_CTX * ctx;
  17. SSL * ssl;
  18. X509 * client_cert;
  19.  
  20. Char szBuffer [1024];
  21. Int nLen;
  22.  
  23. Struct sockaddr_in addr;
  24. Int len;
  25. Int nListenFd, nAcceptFd;
  26.  
  27. // Initialization
  28. Cutil_init ();
  29. Cutil_log_set_level (LOG_ALL );
  30. Cutil_log_set_stderr (1 );
  31. SSLeay_add_ssl_algorithms ();
  32. OpenSSL_add_all_algorithms ();
  33. SSL_load_error_strings ();
  34. ERR_load_BIO_strings ();
  35.  
  36. // We use SSL V3, V2
  37. EXIT_IF_TRUE (ctx = SSL_CTX_new (SSLv23_method () = NULL );
  38.  
  39. // Verify the certificate of the other party
  40. SSL_CTX_set_verify (ctx, SSL_VERIFY_PEER, NULL );
  41.  
  42. // Load the CA certificate
  43. EXIT_IF_TRUE (! SSL_CTX_load_verify_locations (ctx, "cacert. cer", NULL ));
  44.  
  45. // Load your own certificate
  46. EXIT_IF_TRUE (SSL_CTX_use_certificate_file (ctx, "server. cer", SSL_FILETYPE_PEM) <= 0 );
  47.  
  48. // Load your private key
  49. EXIT_IF_TRUE (SSL_CTX_use_PrivateKey_file (ctx, "server. key", SSL_FILETYPE_PEM) <= 0 );
  50.  
  51. // Determine whether the private key is correct
  52. EXIT_IF_TRUE (! SSL_CTX_check_private_key (ctx ));
  53.  
  54. // Create and wait for connection
  55. NListenFd = cutil_socket_new (SOCK_STREAM );
  56. Cutil_socket_bind (nListenFd, NULL, 8812, 1 );
  57.  
  58. Memset (& addr, 0, sizeof (addr ));
  59. Len = sizeof (addr );
  60. NAcceptFd = accept (nListenFd, (struct sockaddr *) & addr, (size_t *) & len );
  61. Cutil_log_debug ("Accept a connect from [% s: % d] \ n ",
  62. Inet_ntoa (addr. sin_addr), ntohs (addr. sin_port ));
  63.  
  64. // Pay the connection to SSL
  65. EXIT_IF_TRUE (ssl = SSL_new (ctx) = NULL );
  66. SSL_set_fd (ssl, nAcceptFd );
  67. EXIT_IF_TRUE (SSL_accept (ssl )! = 1 );
  68.  
  69. // Perform the operation
  70. Memset (szBuffer, 0, sizeof (szBuffer ));
  71. NLen = SSL_read (ssl, szBuffer, sizeof (szBuffer ));
  72. Fprintf (stderr, "Get Len % d % s OK \ n", nLen, szBuffer );
  73. Strcat (szBuffer, "this is from server ");
  74. SSL_write (ssl, szBuffer, strlen (szBuffer ));
  75.  
  76. // Release resources
  77. SSL_free (ssl );
  78. SSL_CTX_free (ctx );
  79. Close (nAcceptFd );
  80. }
Client code
 
 
  1. # Include "openssl/bio. h"
  2. # Include "openssl/ssl. h"
  3. # Include "openssl/err. h"
  4.  
  5. # Include <cutil. h>
  6.  
  7. # Define EXIT_IF_TRUE (x) if (x )\
  8. Do {\
  9. Fprintf (stderr, "Check '% s' is true \ n", # x );\
  10. ERR_print_errors_fp (stderr );\
  11. Exit (2 );\
  12. } While (0)
  13.  
  14. Int main (int argc, char ** argv)
  15. {
  16. SSL_METHOD * meth;
  17. SSL_CTX * ctx;
  18. SSL * ssl;
  19.  
  20. Int nFd;
  21. Int nLen;
  22. Char szBuffer [1024];
  23.  
  24. // Initialization
  25. Cutil_init ();
  26. Cutil_log_set_level (LOG_ALL );
  27. Cutil_log_set_stderr (1 );
  28. SSLeay_add_ssl_algorithms ();
  29. OpenSSL_add_all_algorithms ();
  30. SSL_load_error_strings ();
  31. ERR_load_BIO_strings ();
  32.  
  33. // We use SSL V3, V2
  34. EXIT_IF_TRUE (ctx = SSL_CTX_new (SSLv23_method () = NULL );
  35.  
  36. // Verify the certificate of the other party
  37. SSL_CTX_set_verify (ctx, SSL_VERIFY_PEER, NULL );
  38.  
  39. // Load the CA certificate
  40. EXIT_IF_TRUE (! SSL_CTX_load_verify_locations (ctx, "cacert. cer", NULL ));
  41.  
  42. // Load your own certificate
  43. EXIT_IF_TRUE (SSL_CTX_use_certificate_file (ctx, "client. cer", SSL_FILETYPE_PEM) <= 0 );
  44.  
  45. // Load your private key
  46. EXIT_IF_TRUE (SSL_CTX_use_PrivateKey_file (ctx, "client. key", SSL_FILETYPE_PEM) <= 0 );
  47.  
  48. // Determine whether the private key is correct
  49. EXIT_IF_TRUE (! SSL_CTX_check_private_key (ctx ));
  50. // Create a connection
  51. NFd = cutil_socket_new (SOCK_STREAM );
  52. If (cutil_socket_connect (nFd, "127.0.0.1", 8812, 30) <0)
  53. {
  54. Cutil_log_error ("failed to connect to the server \ n ");
  55. Return-1;
  56. }
  57.  
  58. // Pay the connection to SSL
  59. EXIT_IF_TRUE (ssl = SSL_new (ctx) = NULL );
  60. SSL_set_fd (ssl, nFd );
  61. EXIT_IF_TRUE (SSL_connect (ssl )! = 1 );
  62.  
  63. // Perform the operation
  64. Sprintf (szBuffer, "this is from client % d", getpid ());
  65. SSL_write (ssl, szBuffer, strlen (szBuffer ));
  66.  
  67. // Release resources
  68. Memset (szBuffer, 0, sizeof (szBuffer ));
  69. NLen = SSL_read (ssl, szBuffer, sizeof (szBuffer ));
  70. Fprintf (stderr, "Get Len % d % s OK \ n", nLen, szBuffer );
  71. SSL_free (ssl );
  72. SSL_CTX_free (ctx );
  73. Close (nFd );
  74. }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.