2
0

client-conf.c 3.2 KB

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