Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <arpa/inet.h>
  7.  
  8. #include <openssl/ssl.h>
  9. #include <event2/event.h>
  10. #include <event2/util.h>
  11. #include <event2/bufferevent.h>
  12. #include <event2/bufferevent_ssl.h>
  13.  
  14. SSL_CTX* init_ssl_ctx(const char *certfile, const char *keyfile, const char *cafile)
  15. {
  16. /* Load encryption & hashing algorithms for the SSL program */
  17. SSL_library_init();
  18.  
  19. /* Load the error strings for SSL & CRYPTO APIs */
  20. SSL_load_error_strings();
  21.  
  22. /* Create an SSL_METHOD structure (choose an SSL/TLS protocol version) */
  23. const SSL_METHOD *meth = SSLv23_client_method();
  24.  
  25. /* Create an SSL_CTX structure */
  26. SSL_CTX *ctx = SSL_CTX_new (meth);
  27. if (NULL == ctx)
  28. {
  29. printf("Could not new SSL_CTX\n");
  30. return NULL;
  31. }
  32.  
  33. /* Load the CA cert file*/
  34. if (SSL_CTX_load_verify_locations(ctx, cafile, NULL) <= 0)
  35. {
  36. printf("Could not load ca cert file\n");
  37. }
  38.  
  39. /* Load the client certificate into the SSL_CTX structure */
  40. if (SSL_CTX_use_certificate_file(ctx, certfile, SSL_FILETYPE_PEM) <= 0)
  41. {
  42. printf("Could not use certificate file\n");
  43. }
  44.  
  45. /* Load the private-key corresponding to the client certificate */
  46. if (SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM) <= 0)
  47. {
  48. printf("Could not use private key file\n");
  49. }
  50.  
  51. /* Check if the client certificate and private-key matches */
  52. if (!SSL_CTX_check_private_key(ctx))
  53. {
  54. printf("Private key does not match certfile\n");
  55. }
  56.  
  57. return ctx;
  58. }
  59.  
  60. static void ssl_read_cb(struct bufferevent *bev, void *arg)
  61. {
  62. char recvbuf[1024] = {'\0'};
  63. if (bufferevent_read(bev, recvbuf, 1024) > 0)
  64. {
  65. printf("Recv from client: %s\n", recvbuf);
  66. }
  67. }
  68.  
  69. static void ssl_event_cb(struct bufferevent *bev, short events, void *arg)
  70. {
  71. if (events & BEV_EVENT_CONNECTED)
  72. {
  73. #define MSG "Hello world"
  74. printf("Client connected!\n");
  75. bufferevent_write(bev, MSG, sizeof(MSG));
  76. }
  77. else if (events & BEV_EVENT_EOF)
  78. {
  79. printf("Client disconnected!\n");
  80. bufferevent_free(bev);
  81. }
  82. else
  83. {
  84. int err = EVUTIL_SOCKET_ERROR();
  85. fprintf(stderr, "Got an error %d on ssl socket: %s\n",
  86. err, evutil_socket_error_to_string(err));
  87. bufferevent_free(bev);
  88. }
  89. }
  90.  
  91. int main ()
  92. {
  93. SSL_CTX *ctx = init_ssl_ctx("./PEMS/client/client.crt",
  94. "./PEMS/client/client.key",
  95. "./PEMS/ca/ca.cert");
  96. if (NULL == ctx)
  97. {
  98. exit(-1);
  99. }
  100.  
  101. SSL* ssl = SSL_new (ctx);
  102. if (NULL == ssl)
  103. {
  104. printf("Could not create new SSL\n");
  105. exit(-1);
  106. }
  107.  
  108. struct event_base *base = event_base_new();
  109. if (!base)
  110. {
  111. fprintf(stderr, "Could not open evnet base\n");
  112. exit(EXIT_FAILURE);
  113. }
  114.  
  115. struct bufferevent *bev = bufferevent_openssl_socket_new(base, -1, ssl,
  116. BUFFEREVENT_SSL_CONNECTING, BEV_OPT_CLOSE_ON_FREE);
  117. if (!bev)
  118. {
  119. fprintf(stderr, "Could not create new ssl bufferevent\n");
  120. exit(EXIT_FAILURE);
  121. }
  122.  
  123. bufferevent_setcb(bev, ssl_read_cb, NULL, ssl_event_cb, NULL);
  124. bufferevent_enable(bev, EV_READ | EV_WRITE);
  125.  
  126. struct sockaddr_in servaddr;
  127. struct sockaddr *sa = (struct sockaddr *)&servaddr;
  128. int socklen = sizeof(servaddr);
  129. if (evutil_parse_sockaddr_port("127.0.0.1:1234", sa, &socklen) == -1)
  130. {
  131. fprintf(stderr, "Could not parse address\n");
  132. exit(EXIT_FAILURE);
  133. }
  134.  
  135. if (bufferevent_socket_connect(bev, sa, sizeof(servaddr)) == -1)
  136. {
  137. int err = EVUTIL_SOCKET_ERROR();
  138. fprintf(stderr, "Could not connect to server: %s\n", evutil_socket_error_to_string(err));
  139. exit(EXIT_FAILURE);
  140. }
  141.  
  142. event_base_dispatch(base);
  143.  
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement