sconnect.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <unistd.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <openssl/err.h>
  22. #include <openssl/ssl.h>
  23. #define HOSTPORT "localhost:4433"
  24. #define CAFILE "root.pem"
  25. int main(int argc, char *argv[])
  26. {
  27. const char *hostport = HOSTPORT;
  28. const char *CAfile = CAFILE;
  29. const char *hostname;
  30. BIO *out = NULL;
  31. char buf[1024 * 10], *p;
  32. SSL_CTX *ssl_ctx = NULL;
  33. SSL *ssl;
  34. BIO *ssl_bio;
  35. int i, len, off, ret = EXIT_FAILURE;
  36. if (argc > 1)
  37. hostport = argv[1];
  38. if (argc > 2)
  39. CAfile = argv[2];
  40. #ifdef WATT32
  41. dbug_init();
  42. sock_init();
  43. #endif
  44. ssl_ctx = SSL_CTX_new(TLS_client_method());
  45. /* Enable trust chain verification */
  46. SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
  47. SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL);
  48. /* Lets make a SSL structure */
  49. ssl = SSL_new(ssl_ctx);
  50. SSL_set_connect_state(ssl);
  51. /* Use it inside an SSL BIO */
  52. ssl_bio = BIO_new(BIO_f_ssl());
  53. BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
  54. /* Lets use a connect BIO under the SSL BIO */
  55. out = BIO_new(BIO_s_connect());
  56. BIO_set_conn_hostname(out, hostport);
  57. /* The BIO has parsed the host:port and even IPv6 literals in [] */
  58. hostname = BIO_get_conn_hostname(out);
  59. if (!hostname || SSL_set1_host(ssl, hostname) <= 0) {
  60. BIO_free(ssl_bio);
  61. goto err;
  62. }
  63. BIO_set_nbio(out, 1);
  64. out = BIO_push(ssl_bio, out);
  65. p = "GET / HTTP/1.0\r\n\r\n";
  66. len = strlen(p);
  67. off = 0;
  68. for (;;) {
  69. i = BIO_write(out, &(p[off]), len);
  70. if (i <= 0) {
  71. if (BIO_should_retry(out)) {
  72. fprintf(stderr, "write DELAY\n");
  73. sleep(1);
  74. continue;
  75. } else {
  76. goto err;
  77. }
  78. }
  79. off += i;
  80. len -= i;
  81. if (len <= 0)
  82. break;
  83. }
  84. for (;;) {
  85. i = BIO_read(out, buf, sizeof(buf));
  86. if (i == 0)
  87. break;
  88. if (i < 0) {
  89. if (BIO_should_retry(out)) {
  90. fprintf(stderr, "read DELAY\n");
  91. sleep(1);
  92. continue;
  93. }
  94. goto err;
  95. }
  96. fwrite(buf, 1, i, stdout);
  97. }
  98. ret = EXIT_SUCCESS;
  99. goto done;
  100. err:
  101. if (ERR_peek_error() == 0) { /* system call error */
  102. fprintf(stderr, "errno=%d ", errno);
  103. perror("error");
  104. } else {
  105. ERR_print_errors_fp(stderr);
  106. }
  107. done:
  108. BIO_free_all(out);
  109. SSL_CTX_free(ssl_ctx);
  110. return ret;
  111. }