ssl_fp 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 # size of cache, just to help debugging
  9. cache_size=$(stat -c %s "$Cache")
  10. debug -v Cache cache_size
  11. if test "$cache_size" -gt 0;
  12. then
  13. debug "using cache ($cache_size bytes)"
  14. cat "$Cache"
  15. else
  16. debug "building cache"
  17. get_cert | tee "$Cache"
  18. fi
  19. }
  20. get_cert() {
  21. debug heya
  22. </dev/null openssl s_client -connect "$Conn" 2>/dev/null
  23. }
  24. get_fp() {
  25. local fun="$1"
  26. debug -v Conn fun
  27. cached get_cert \
  28. | openssl x509 -noout -"$fun" -fingerprint 2>/dev/null
  29. }
  30. get_fps() {
  31. get_fp md5
  32. get_fp sha1
  33. get_fp sha256
  34. }
  35. main() {
  36. local Conn # connection target (SERVER:PORT)
  37. local Cache # cert cache (for re-computing hashes)
  38. local es # exit status
  39. #shellcheck disable=SC2034
  40. while true; do case "$1" in
  41. *:*) Conn="$1"; shift ;;
  42. -d) SHELLFU_DEBUG=true; shift ;;
  43. "") break ;;
  44. *) usage ;;
  45. esac done
  46. test -n "$Conn" || usage
  47. Cache="$(mktemp -t ssl_fp.cache.XXXXXXXX)"
  48. get_fps; es=$?
  49. rm -f "$Cache"
  50. return "$es"
  51. }
  52. main "$@"