sconnect.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 1998-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*-
  10. * A minimal program to do SSL to a passed host and port.
  11. * It is actually using non-blocking IO but in a very simple manner
  12. * sconnect host:port - it does a 'GET / HTTP/1.0'
  13. *
  14. * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <openssl/err.h>
  21. #include <openssl/ssl.h>
  22. #if !defined(OPENSSL_SYS_WINDOWS)
  23. #include <unistd.h>
  24. #else
  25. #include <windows.h>
  26. # define sleep(x) Sleep(x*1000)
  27. #endif
  28. #define HOSTPORT "localhost:4433"
  29. #define CAFILE "root.pem"
  30. int main(int argc, char *argv[])
  31. {
  32. const char *hostport = HOSTPORT;
  33. const char *CAfile = CAFILE;
  34. const char *hostname;
  35. BIO *out = NULL;
  36. char buf[1024 * 10], *p;
  37. SSL_CTX *ssl_ctx = NULL;
  38. SSL *ssl;
  39. BIO *ssl_bio;
  40. int i, len, off, ret = EXIT_FAILURE;
  41. if (argc > 1)
  42. hostport = argv[1];
  43. if (argc > 2)
  44. CAfile = argv[2];
  45. #ifdef WATT32
  46. dbug_init();
  47. sock_init();
  48. #endif
  49. ssl_ctx = SSL_CTX_new(TLS_client_method());
  50. /* Enable trust chain verification */
  51. SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
  52. if (!SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL))
  53. goto err;
  54. /* Let's make an SSL structure */
  55. ssl = SSL_new(ssl_ctx);
  56. SSL_set_connect_state(ssl);
  57. /* Use it inside an SSL BIO */
  58. ssl_bio = BIO_new(BIO_f_ssl());
  59. BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
  60. /* Lets use a connect BIO under the SSL BIO */
  61. out = BIO_new(BIO_s_connect());
  62. BIO_set_conn_hostname(out, hostport);
  63. /* The BIO has parsed the host:port and even IPv6 literals in [] */
  64. hostname = BIO_get_conn_hostname(out);
  65. if (!hostname || SSL_set1_host(ssl, hostname) <= 0) {
  66. BIO_free(ssl_bio);
  67. goto err;
  68. }
  69. BIO_set_nbio(out, 1);
  70. out = BIO_push(ssl_bio, out);
  71. p = "GET / HTTP/1.0\r\n\r\n";
  72. len = strlen(p);
  73. off = 0;
  74. for (;;) {
  75. i = BIO_write(out, &(p[off]), len);
  76. if (i <= 0) {
  77. if (BIO_should_retry(out)) {
  78. fprintf(stderr, "write DELAY\n");
  79. sleep(1);
  80. continue;
  81. } else {
  82. goto err;
  83. }
  84. }
  85. off += i;
  86. len -= i;
  87. if (len <= 0)
  88. break;
  89. }
  90. for (;;) {
  91. i = BIO_read(out, buf, sizeof(buf));
  92. if (i == 0)
  93. break;
  94. if (i < 0) {
  95. if (BIO_should_retry(out)) {
  96. fprintf(stderr, "read DELAY\n");
  97. sleep(1);
  98. continue;
  99. }
  100. goto err;
  101. }
  102. fwrite(buf, 1, i, stdout);
  103. }
  104. ret = EXIT_SUCCESS;
  105. goto done;
  106. err:
  107. if (ERR_peek_error() == 0) { /* system call error */
  108. fprintf(stderr, "errno=%d ", errno);
  109. perror("error");
  110. } else {
  111. ERR_print_errors_fp(stderr);
  112. }
  113. done:
  114. BIO_free_all(out);
  115. SSL_CTX_free(ssl_ctx);
  116. return ret;
  117. }