client-arg.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <openssl/err.h>
  2. #include <openssl/ssl.h>
  3. int main(int argc, char **argv)
  4. {
  5. BIO *sbio = NULL, *out = NULL;
  6. int len;
  7. char tmpbuf[1024];
  8. SSL_CTX *ctx;
  9. SSL_CONF_CTX *cctx;
  10. SSL *ssl;
  11. char **args = argv + 1;
  12. const char *connect_str = "localhost:4433";
  13. int nargs = argc - 1;
  14. ERR_load_crypto_strings();
  15. ERR_load_SSL_strings();
  16. SSL_library_init();
  17. ctx = SSL_CTX_new(SSLv23_client_method());
  18. cctx = SSL_CONF_CTX_new();
  19. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
  20. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  21. while (*args && **args == '-') {
  22. int rv;
  23. /* Parse standard arguments */
  24. rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
  25. if (rv == -3) {
  26. fprintf(stderr, "Missing argument for %s\n", *args);
  27. goto end;
  28. }
  29. if (rv < 0) {
  30. fprintf(stderr, "Error in command %s\n", *args);
  31. ERR_print_errors_fp(stderr);
  32. goto end;
  33. }
  34. /* If rv > 0 we processed something so proceed to next arg */
  35. if (rv > 0)
  36. continue;
  37. /* Otherwise application specific argument processing */
  38. if (!strcmp(*args, "-connect")) {
  39. connect_str = args[1];
  40. if (connect_str == NULL) {
  41. fprintf(stderr, "Missing -connect argument\n");
  42. goto end;
  43. }
  44. args += 2;
  45. nargs -= 2;
  46. continue;
  47. } else {
  48. fprintf(stderr, "Unknown argument %s\n", *args);
  49. goto end;
  50. }
  51. }
  52. if (!SSL_CONF_CTX_finish(cctx)) {
  53. fprintf(stderr, "Finish error\n");
  54. ERR_print_errors_fp(stderr);
  55. goto err;
  56. }
  57. /*
  58. * We'd normally set some stuff like the verify paths and * mode here
  59. * because as things stand this will connect to * any server whose
  60. * certificate is signed by any CA.
  61. */
  62. sbio = BIO_new_ssl_connect(ctx);
  63. BIO_get_ssl(sbio, &ssl);
  64. if (!ssl) {
  65. fprintf(stderr, "Can't locate SSL pointer\n");
  66. goto end;
  67. }
  68. /* Don't want any retries */
  69. SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
  70. /* We might want to do other things with ssl here */
  71. BIO_set_conn_hostname(sbio, connect_str);
  72. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  73. if (BIO_do_connect(sbio) <= 0) {
  74. fprintf(stderr, "Error connecting to server\n");
  75. ERR_print_errors_fp(stderr);
  76. goto end;
  77. }
  78. if (BIO_do_handshake(sbio) <= 0) {
  79. fprintf(stderr, "Error establishing SSL connection\n");
  80. ERR_print_errors_fp(stderr);
  81. goto end;
  82. }
  83. /* Could examine ssl here to get connection info */
  84. BIO_puts(sbio, "GET / HTTP/1.0\n\n");
  85. for (;;) {
  86. len = BIO_read(sbio, tmpbuf, 1024);
  87. if (len <= 0)
  88. break;
  89. BIO_write(out, tmpbuf, len);
  90. }
  91. end:
  92. SSL_CONF_CTX_free(cctx);
  93. BIO_free_all(sbio);
  94. BIO_free(out);
  95. return 0;
  96. }