client-arg.c 3.2 KB

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