client-conf.c 3.1 KB

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