123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. . "$(shellfu-get path)" || exit 3
  3. shellfu import pretty
  4. usage() {
  5. mkusage "[-d] SERVER:PORT"
  6. }
  7. cached() {
  8. local cache_size=$(stat -c %s "$Cache")
  9. debug -v Cache cache_size
  10. if test "$cache_size" -gt 0;
  11. then
  12. debug "using cache ($cache_size bytes)"
  13. cat "$Cache"
  14. else
  15. debug "building cache"
  16. get_cert | tee "$Cache"
  17. fi
  18. }
  19. get_cert() {
  20. debug heya
  21. </dev/null openssl s_client -connect "$Conn" 2>/dev/null
  22. }
  23. get_fp() {
  24. local fun="$1"
  25. debug -v Conn fun
  26. cached get_cert \
  27. | openssl x509 -noout -"$fun" -fingerprint 2>/dev/null
  28. }
  29. get_fps() {
  30. get_fp md5
  31. get_fp sha1
  32. get_fp sha256
  33. }
  34. main() {
  35. local Conn
  36. local Cache
  37. local es
  38. #shellcheck disable=SC2034
  39. while true; do case "$1" in
  40. *:*) Conn="$1"; shift ;;
  41. -d) SHELLFU_DEBUG=true; shift ;;
  42. "") break ;;
  43. *) usage ;;
  44. esac done
  45. test -n "$Conn" || usage
  46. Cache="$(mktemp -t ssl_fp.cache.XXXXXXXX)"
  47. get_fps; es=$?
  48. rm -f "$Cache"
  49. return "$es"
  50. }
  51. main "$@"