client-arg.c 2.9 KB

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