client-conf.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2013-2016 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. #include <string.h>
  10. #include <openssl/err.h>
  11. #include <openssl/ssl.h>
  12. #include <openssl/conf.h>
  13. int main(int argc, char **argv)
  14. {
  15. BIO *sbio = NULL, *out = NULL;
  16. int i, len, rv;
  17. char tmpbuf[1024];
  18. SSL_CTX *ctx = NULL;
  19. SSL_CONF_CTX *cctx = NULL;
  20. SSL *ssl = NULL;
  21. CONF *conf = NULL;
  22. STACK_OF(CONF_VALUE) *sect = NULL;
  23. CONF_VALUE *cnf;
  24. const char *connect_str = "localhost:4433";
  25. long errline = -1;
  26. conf = NCONF_new(NULL);
  27. if (NCONF_load(conf, "connect.cnf", &errline) <= 0) {
  28. if (errline <= 0)
  29. fprintf(stderr, "Error processing config file\n");
  30. else
  31. fprintf(stderr, "Error on line %ld\n", errline);
  32. goto end;
  33. }
  34. sect = NCONF_get_section(conf, "default");
  35. if (sect == NULL) {
  36. fprintf(stderr, "Error retrieving default section\n");
  37. goto end;
  38. }
  39. ctx = SSL_CTX_new(TLS_client_method());
  40. cctx = SSL_CONF_CTX_new();
  41. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
  42. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
  43. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  44. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  45. cnf = sk_CONF_VALUE_value(sect, i);
  46. rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
  47. if (rv > 0)
  48. continue;
  49. if (rv != -2) {
  50. fprintf(stderr, "Error processing %s = %s\n",
  51. cnf->name, cnf->value);
  52. ERR_print_errors_fp(stderr);
  53. goto end;
  54. }
  55. if (strcmp(cnf->name, "Connect") == 0) {
  56. connect_str = cnf->value;
  57. } else {
  58. fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
  59. goto end;
  60. }
  61. }
  62. if (!SSL_CONF_CTX_finish(cctx)) {
  63. fprintf(stderr, "Finish error\n");
  64. ERR_print_errors_fp(stderr);
  65. goto end;
  66. }
  67. /*
  68. * We'd normally set some stuff like the verify paths and * mode here
  69. * because as things stand this will connect to * any server whose
  70. * certificate is signed by any CA.
  71. */
  72. sbio = BIO_new_ssl_connect(ctx);
  73. BIO_get_ssl(sbio, &ssl);
  74. if (!ssl) {
  75. fprintf(stderr, "Can't locate SSL pointer\n");
  76. goto end;
  77. }
  78. /* Don't want any retries */
  79. SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
  80. /* We might want to do other things with ssl here */
  81. BIO_set_conn_hostname(sbio, connect_str);
  82. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  83. if (BIO_do_connect(sbio) <= 0) {
  84. fprintf(stderr, "Error connecting to server\n");
  85. ERR_print_errors_fp(stderr);
  86. goto end;
  87. }
  88. if (BIO_do_handshake(sbio) <= 0) {
  89. fprintf(stderr, "Error establishing SSL connection\n");
  90. ERR_print_errors_fp(stderr);
  91. goto end;
  92. }
  93. /* Could examine ssl here to get connection info */
  94. BIO_puts(sbio, "GET / HTTP/1.0\n\n");
  95. for (;;) {
  96. len = BIO_read(sbio, tmpbuf, 1024);
  97. if (len <= 0)
  98. break;
  99. BIO_write(out, tmpbuf, len);
  100. }
  101. end:
  102. SSL_CONF_CTX_free(cctx);
  103. BIO_free_all(sbio);
  104. BIO_free(out);
  105. NCONF_free(conf);
  106. return 0;
  107. }