cli.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* cli.cpp - Minimal ssleay client for Unix
  2. 30.9.1996, Sampo Kellomaki <sampo@iki.fi> */
  3. /* mangled to work with SSLeay-0.9.0b and OpenSSL 0.9.2b
  4. Simplified to be even more minimal
  5. 12/98 - 4/99 Wade Scholine <wades@mail.cybg.com> */
  6. #include <stdio.h>
  7. #include <memory.h>
  8. #include <errno.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13. #include <netdb.h>
  14. #include <openssl/crypto.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/pem.h>
  17. #include <openssl/ssl.h>
  18. #include <openssl/err.h>
  19. #define CHK_NULL(x) if ((x)==NULL) exit (1)
  20. #define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); }
  21. #define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); }
  22. void main ()
  23. {
  24. int err;
  25. int sd;
  26. struct sockaddr_in sa;
  27. SSL_CTX* ctx;
  28. SSL* ssl;
  29. X509* server_cert;
  30. char* str;
  31. char buf [4096];
  32. SSL_METHOD *meth;
  33. SSLeay_add_ssl_algorithms();
  34. meth = SSLv2_client_method();
  35. SSL_load_error_strings();
  36. ctx = SSL_CTX_new (meth); CHK_NULL(ctx);
  37. CHK_SSL(err);
  38. /* ----------------------------------------------- */
  39. /* Create a socket and connect to server using normal socket calls. */
  40. sd = socket (AF_INET, SOCK_STREAM, 0); CHK_ERR(sd, "socket");
  41. memset (&sa, '\0', sizeof(sa));
  42. sa.sin_family = AF_INET;
  43. sa.sin_addr.s_addr = inet_addr ("127.0.0.1"); /* Server IP */
  44. sa.sin_port = htons (1111); /* Server Port number */
  45. err = connect(sd, (struct sockaddr*) &sa,
  46. sizeof(sa)); CHK_ERR(err, "connect");
  47. /* ----------------------------------------------- */
  48. /* Now we have TCP conncetion. Start SSL negotiation. */
  49. ssl = SSL_new (ctx); CHK_NULL(ssl);
  50. SSL_set_fd (ssl, sd);
  51. err = SSL_connect (ssl); CHK_SSL(err);
  52. /* Following two steps are optional and not required for
  53. data exchange to be successful. */
  54. /* Get the cipher - opt */
  55. printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
  56. /* Get server's certificate (note: beware of dynamic allocation) - opt */
  57. server_cert = SSL_get_peer_certificate (ssl); CHK_NULL(server_cert);
  58. printf ("Server certificate:\n");
  59. str = X509_NAME_oneline (X509_get_subject_name (server_cert),0,0);
  60. CHK_NULL(str);
  61. printf ("\t subject: %s\n", str);
  62. OPENSSL_free (str);
  63. str = X509_NAME_oneline (X509_get_issuer_name (server_cert),0,0);
  64. CHK_NULL(str);
  65. printf ("\t issuer: %s\n", str);
  66. OPENSSL_free (str);
  67. /* We could do all sorts of certificate verification stuff here before
  68. deallocating the certificate. */
  69. X509_free (server_cert);
  70. /* --------------------------------------------------- */
  71. /* DATA EXCHANGE - Send a message and receive a reply. */
  72. err = SSL_write (ssl, "Hello World!", strlen("Hello World!")); CHK_SSL(err);
  73. err = SSL_read (ssl, buf, sizeof(buf) - 1); CHK_SSL(err);
  74. buf[err] = '\0';
  75. printf ("Got %d chars:'%s'\n", err, buf);
  76. SSL_shutdown (ssl); /* send SSL/TLS close_notify */
  77. /* Clean up. */
  78. close (sd);
  79. SSL_free (ssl);
  80. SSL_CTX_free (ctx);
  81. }
  82. /* EOF - cli.cpp */