client-conf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2013-2021 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. /* We might want to do other things with ssl here */
  79. BIO_set_conn_hostname(sbio, connect_str);
  80. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  81. if (BIO_do_connect(sbio) <= 0) {
  82. fprintf(stderr, "Error connecting to server\n");
  83. ERR_print_errors_fp(stderr);
  84. goto end;
  85. }
  86. /* Could examine ssl here to get connection info */
  87. BIO_puts(sbio, "GET / HTTP/1.0\n\n");
  88. for (;;) {
  89. len = BIO_read(sbio, tmpbuf, 1024);
  90. if (len <= 0)
  91. break;
  92. BIO_write(out, tmpbuf, len);
  93. }
  94. end:
  95. SSL_CONF_CTX_free(cctx);
  96. BIO_free_all(sbio);
  97. BIO_free(out);
  98. NCONF_free(conf);
  99. return 0;
  100. }