client-arg.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2013-2021 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. int ret = EXIT_FAILURE;
  24. ctx = SSL_CTX_new(TLS_client_method());
  25. cctx = SSL_CONF_CTX_new();
  26. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
  27. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  28. while (*args && **args == '-') {
  29. int rv;
  30. /* Parse standard arguments */
  31. rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
  32. if (rv == -3) {
  33. fprintf(stderr, "Missing argument for %s\n", *args);
  34. goto end;
  35. }
  36. if (rv < 0) {
  37. fprintf(stderr, "Error in command %s\n", *args);
  38. ERR_print_errors_fp(stderr);
  39. goto end;
  40. }
  41. /* If rv > 0 we processed something so proceed to next arg */
  42. if (rv > 0)
  43. continue;
  44. /* Otherwise application specific argument processing */
  45. if (strcmp(*args, "-connect") == 0) {
  46. connect_str = args[1];
  47. if (connect_str == NULL) {
  48. fprintf(stderr, "Missing -connect argument\n");
  49. goto end;
  50. }
  51. args += 2;
  52. nargs -= 2;
  53. continue;
  54. } else {
  55. fprintf(stderr, "Unknown argument %s\n", *args);
  56. goto end;
  57. }
  58. }
  59. if (!SSL_CONF_CTX_finish(cctx)) {
  60. fprintf(stderr, "Finish error\n");
  61. ERR_print_errors_fp(stderr);
  62. goto end;
  63. }
  64. /*
  65. * We'd normally set some stuff like the verify paths and * mode here
  66. * because as things stand this will connect to * any server whose
  67. * certificate is signed by any CA.
  68. */
  69. sbio = BIO_new_ssl_connect(ctx);
  70. BIO_get_ssl(sbio, &ssl);
  71. if (!ssl) {
  72. fprintf(stderr, "Can't locate SSL pointer\n");
  73. goto end;
  74. }
  75. /* We might want to do other things with ssl here */
  76. BIO_set_conn_hostname(sbio, connect_str);
  77. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  78. if (BIO_do_connect(sbio) <= 0) {
  79. fprintf(stderr, "Error connecting to server\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. ret = EXIT_SUCCESS;
  92. end:
  93. SSL_CONF_CTX_free(cctx);
  94. BIO_free_all(sbio);
  95. BIO_free(out);
  96. return ret;
  97. }