client.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * gcc -g -o client client.c -lssl -lcrypt -lcrypto
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #include <malloc.h>
  9. #include <string.h>
  10. #include <sys/socket.h>
  11. #include <resolv.h>
  12. #include <netdb.h>
  13. #include <openssl/ssl.h>
  14. #include <openssl/err.h>
  15. #define FAIL -1
  16. int OpenConnection(const char *hostname, int port)
  17. {
  18. int sd;
  19. struct hostent *host;
  20. struct sockaddr_in addr;
  21. if ((host = gethostbyname(hostname)) == NULL)
  22. {
  23. printf("Eroor: %s\n", hostname);
  24. perror(hostname);
  25. abort();
  26. }
  27. sd = socket(PF_INET, SOCK_STREAM, 0);
  28. sw_memset_zero(&addr, sizeof(addr));
  29. addr.sin_family = AF_INET;
  30. addr.sin_port = htons(port);
  31. addr.sin_addr.s_addr = *(long*) (host->h_addr);
  32. if (connect(sd, (struct sockaddr*) &addr, sizeof(addr)) != 0)
  33. {
  34. close(sd);
  35. perror(hostname);
  36. abort();
  37. }
  38. return sd;
  39. }
  40. SSL_CTX* InitCTX(void)
  41. {
  42. SSL_METHOD *method;
  43. SSL_CTX *ctx;
  44. OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */
  45. SSL_load_error_strings(); /* Bring in and register error messages */
  46. // method = SSLv3_client_method(); /* Create new client-method instance */
  47. method = TLSv1_2_client_method();
  48. ctx = SSL_CTX_new(method); /* Create new context */
  49. if (ctx == NULL)
  50. {
  51. ERR_print_errors_fp(stderr);
  52. printf("Eroor: %s\n", stderr);
  53. abort();
  54. }
  55. return ctx;
  56. }
  57. void ShowCerts(SSL* ssl)
  58. {
  59. X509 *cert;
  60. char *line;
  61. cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */
  62. if (cert != NULL)
  63. {
  64. printf("Server certificates:\n");
  65. line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
  66. printf("Subject: %s\n", line);
  67. free(line);
  68. line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
  69. printf("Issuer: %s\n", line);
  70. free(line);
  71. X509_free(cert);
  72. }
  73. else
  74. printf("No certificates.\n");
  75. }
  76. int main(int count, char *strings[])
  77. {
  78. SSL_CTX *ctx;
  79. int server;
  80. SSL *ssl;
  81. char buf[1024];
  82. int bytes;
  83. char *hostname, *portnum;
  84. if (count != 3)
  85. {
  86. printf("usage: %s <hostname> <portnum>\n", strings[0]);
  87. exit(0);
  88. }
  89. SSL_library_init();
  90. hostname = strings[1];
  91. portnum = strings[2];
  92. ctx = InitCTX();
  93. server = OpenConnection(hostname, atoi(portnum));
  94. ssl = SSL_new(ctx); /* create new SSL connection state */
  95. SSL_set_fd(ssl, server); /* attach the socket descriptor */
  96. TLSv1_2_client_method();
  97. if (SSL_connect(ssl) == FAIL) /* perform the connection */
  98. {
  99. printf("Eroor: %s\n", stderr);
  100. ERR_print_errors_fp(stderr);
  101. }
  102. else
  103. {
  104. char *msg = "HelloWorld";
  105. printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
  106. ShowCerts(ssl); /* get any certs */
  107. SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */
  108. bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */
  109. buf[bytes] = 0;
  110. printf("Received: \"%s\"\n", buf);
  111. SSL_free(ssl); /* release connection state */
  112. }
  113. close(server); /* close socket */
  114. SSL_CTX_free(ctx); /* release context */
  115. return 0;
  116. }