connect_test.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <sys/socket.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <openssl/ssl.h>
  5. #include "esp_log.h"
  6. static const char *TAG = "OPENSSL_TEST";
  7. static int open_connection(const char *host, const int port)
  8. {
  9. struct sockaddr_in addr;
  10. struct hostent *h;
  11. int sd;
  12. if ((h = gethostbyname(host)) == NULL) {
  13. ESP_LOGI(TAG, "Failed to get host name %s", host);
  14. return -1;
  15. }
  16. sd = socket(AF_INET, SOCK_STREAM, 0);
  17. bzero(&addr, sizeof(addr));
  18. addr.sin_family = AF_INET;
  19. addr.sin_port = htons(port);
  20. addr.sin_addr.s_addr = *(long*)(h->h_addr);
  21. if (connect(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
  22. return -1;
  23. }
  24. return sd;
  25. }
  26. static SSL_CTX* init_ctx(const char *test_case)
  27. {
  28. extern const unsigned char cacert_pem_start[] asm("_binary_ca_crt_start");
  29. extern const unsigned char cacert_pem_end[] asm("_binary_ca_crt_end");
  30. const unsigned int cacert_pem_bytes = cacert_pem_end - cacert_pem_start;
  31. const SSL_METHOD *method = NULL;
  32. SSL_CTX *ctx = NULL;
  33. if (strcmp(test_case, "CONFIG_TLSV1_1_CONNECT_WRONG_CERT_VERIFY_NONE") == 0) {
  34. method = TLSv1_1_client_method();
  35. ctx = SSL_CTX_new(method); /* Create new context */
  36. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
  37. } else if (strcmp(test_case, "CONFIG_TLSV1_1_CONNECT_WRONG_CERT_VERIFY_PEER") == 0) {
  38. method = TLSv1_1_client_method();
  39. ctx = SSL_CTX_new(method); /* Create new context */
  40. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
  41. } else if (strcmp(test_case, "CONFIG_TLSV1_2_CONNECT_WRONG_CERT_VERIFY_NONE") == 0) {
  42. method = TLSv1_2_client_method();
  43. ctx = SSL_CTX_new(method); /* Create new context */
  44. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
  45. } else if (strcmp(test_case, "CONFIG_TLSV1_2_CONNECT_WRONG_CERT_VERIFY_PEER") == 0) {
  46. method = TLSv1_2_client_method();
  47. ctx = SSL_CTX_new(method); /* Create new context */
  48. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
  49. }
  50. X509 *x = d2i_X509(NULL, cacert_pem_start, cacert_pem_bytes);
  51. if(!x) {
  52. ESP_LOGI(TAG, "Loading certs failed");
  53. goto failed;
  54. }
  55. SSL_CTX_add_client_CA(ctx, x);
  56. return ctx;
  57. failed:
  58. return NULL;
  59. }
  60. static void start_test(const char *host, const int port, const char *test_case)
  61. {
  62. SSL_CTX *ctx = NULL;
  63. SSL *ssl = NULL;
  64. int sockfd;
  65. int ret;
  66. ESP_LOGI(TAG, "Test %s started", test_case);
  67. ctx = init_ctx(test_case);
  68. if (!ctx) {
  69. ESP_LOGI(TAG, "Failed");
  70. goto failed1;
  71. }
  72. ESP_LOGI(TAG, "Trying connect to %s port %d test case %s ...", host, port, test_case);
  73. sockfd = open_connection(host, port);
  74. if(sockfd < 0) {
  75. ESP_LOGI(TAG,"Failed");
  76. goto failed1;
  77. }
  78. ESP_LOGI(TAG, "OK");
  79. ESP_LOGI(TAG, "Create SSL obj");
  80. ssl = SSL_new(ctx);
  81. if (!ssl) {
  82. ESP_LOGI(TAG,"Failed");
  83. goto failed2;
  84. }
  85. ESP_LOGI(TAG, "OK");
  86. SSL_set_fd(ssl, sockfd);
  87. ESP_LOGI(TAG, "SSL verify mode = %d connected to %s port %d ...", SSL_CTX_get_verify_mode(ctx),
  88. host, port);
  89. ret = SSL_connect(ssl);
  90. ESP_LOGI(TAG, "OK");
  91. if (ret <= 0) {
  92. ESP_LOGI(TAG,"SSL Connection Failed");
  93. goto failed3;
  94. }
  95. ESP_LOGI(TAG,"SSL Connection Succeed");
  96. failed3:
  97. SSL_free(ssl);
  98. ssl = NULL;
  99. failed2:
  100. close(sockfd);
  101. sockfd = -1;
  102. failed1:
  103. SSL_CTX_free(ctx);
  104. ctx = NULL;
  105. }
  106. static void scan(char *s, char **test_type, char **host, int *p, char **test_case)
  107. {
  108. const char *delim = " ";
  109. *test_type = strtok(s, delim);
  110. *host = strtok(NULL, delim);
  111. *p = atoi(strtok(NULL, delim));
  112. *test_case = strtok(NULL, delim);
  113. }
  114. void connection_test(char *line)
  115. {
  116. char *test_case;
  117. char *test_type;
  118. char *host;
  119. int port;
  120. scan(line, &test_type, &host, &port, &test_case);
  121. start_test(host, port, test_case);
  122. }