ssl_client.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (C) 2017 Denys Vlasenko
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  5. */
  6. //config:config SSL_CLIENT
  7. //config: bool "ssl_client (23 kb)"
  8. //config: default y
  9. //config: select TLS
  10. //config: help
  11. //config: This tool pipes data to/from a socket, TLS-encrypting it.
  12. //applet:IF_SSL_CLIENT(APPLET(ssl_client, BB_DIR_USR_BIN, BB_SUID_DROP))
  13. //kbuild:lib-$(CONFIG_SSL_CLIENT) += ssl_client.o
  14. //usage:#define ssl_client_trivial_usage
  15. //usage: "-s FD [-r FD] [-n SNI]"
  16. //usage:#define ssl_client_full_usage ""
  17. #include "libbb.h"
  18. int ssl_client_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  19. int ssl_client_main(int argc UNUSED_PARAM, char **argv)
  20. {
  21. tls_state_t *tls;
  22. const char *sni = NULL;
  23. int opt;
  24. // INIT_G();
  25. tls = new_tls_state();
  26. opt = getopt32(argv, "s:+r:+n:", &tls->ofd, &tls->ifd, &sni);
  27. if (!(opt & 2)) {
  28. /* -r N defaults to -s N */
  29. tls->ifd = tls->ofd;
  30. }
  31. if (!(opt & 3)) {
  32. if (!argv[1])
  33. bb_show_usage();
  34. /* Undocumented debug feature: without -s and -r, takes HOST arg and connects to it */
  35. //
  36. // Talk to kernel.org:
  37. // printf "GET / HTTP/1.1\r\nHost: kernel.org\r\n\r\n" | ./busybox ssl_client kernel.org
  38. if (!sni)
  39. sni = argv[1];
  40. tls->ifd = tls->ofd = create_and_connect_stream_or_die(argv[1], 443);
  41. }
  42. tls_handshake(tls, sni);
  43. tls_run_copy_loop(tls);
  44. return EXIT_SUCCESS;
  45. }