sconnect.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* NOCW */
  2. /* demos/bio/sconnect.c */
  3. /*-
  4. * A minimal program to do SSL to a passed host and port.
  5. * It is actually using non-blocking IO but in a very simple manner
  6. * sconnect host:port - it does a 'GET / HTTP/1.0'
  7. *
  8. * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include <openssl/err.h>
  15. #include <openssl/ssl.h>
  16. #define HOSTPORT "localhost:4433"
  17. #define CAFILE "root.pem"
  18. extern int errno;
  19. int main(argc, argv)
  20. int argc;
  21. char *argv[];
  22. {
  23. const char *hostport = HOSTPORT;
  24. const char *CAfile = CAFILE;
  25. char *hostname;
  26. char *cp;
  27. BIO *out = NULL;
  28. char buf[1024 * 10], *p;
  29. SSL_CTX *ssl_ctx = NULL;
  30. SSL *ssl;
  31. BIO *ssl_bio;
  32. int i, len, off, ret = 1;
  33. if (argc > 1)
  34. hostport = argv[1];
  35. if (argc > 2)
  36. CAfile = argv[2];
  37. hostname = OPENSSL_strdup(hostport);
  38. if ((cp = strchr(hostname, ':')) != NULL)
  39. *cp = 0;
  40. #ifdef WATT32
  41. dbug_init();
  42. sock_init();
  43. #endif
  44. /* Lets get nice error messages */
  45. SSL_load_error_strings();
  46. /* Setup all the global SSL stuff */
  47. OpenSSL_add_ssl_algorithms();
  48. ssl_ctx = SSL_CTX_new(TLS_client_method());
  49. /* Enable trust chain verification */
  50. SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
  51. SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL);
  52. /* Lets make a SSL structure */
  53. ssl = SSL_new(ssl_ctx);
  54. SSL_set_connect_state(ssl);
  55. /* Enable peername verification */
  56. if (SSL_set1_host(ssl, hostname) <= 0)
  57. goto err;
  58. /* Use it inside an SSL BIO */
  59. ssl_bio = BIO_new(BIO_f_ssl());
  60. BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
  61. /* Lets use a connect BIO under the SSL BIO */
  62. out = BIO_new(BIO_s_connect());
  63. BIO_set_conn_hostname(out, hostport);
  64. BIO_set_nbio(out, 1);
  65. out = BIO_push(ssl_bio, out);
  66. p = "GET / HTTP/1.0\r\n\r\n";
  67. len = strlen(p);
  68. off = 0;
  69. for (;;) {
  70. i = BIO_write(out, &(p[off]), len);
  71. if (i <= 0) {
  72. if (BIO_should_retry(out)) {
  73. fprintf(stderr, "write DELAY\n");
  74. sleep(1);
  75. continue;
  76. } else {
  77. goto err;
  78. }
  79. }
  80. off += i;
  81. len -= i;
  82. if (len <= 0)
  83. break;
  84. }
  85. for (;;) {
  86. i = BIO_read(out, buf, sizeof(buf));
  87. if (i == 0)
  88. break;
  89. if (i < 0) {
  90. if (BIO_should_retry(out)) {
  91. fprintf(stderr, "read DELAY\n");
  92. sleep(1);
  93. continue;
  94. }
  95. goto err;
  96. }
  97. fwrite(buf, 1, i, stdout);
  98. }
  99. ret = 1;
  100. goto done;
  101. err:
  102. if (ERR_peek_error() == 0) { /* system call error */
  103. fprintf(stderr, "errno=%d ", errno);
  104. perror("error");
  105. } else
  106. ERR_print_errors_fp(stderr);
  107. done:
  108. BIO_free_all(out);
  109. SSL_CTX_free(ssl_ctx);
  110. return (ret == 1);
  111. }