keygen-comment.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #    Placed in the Public Domain.
  2. tid="Comment extraction from private key"
  3. S1="secret1"
  4. check_fingerprint()
  5. {
  6. file="$1"
  7. comment="$2"
  8. trace "fingerprinting $file"
  9. if ! ${SSHKEYGEN} -l -E sha256 -f $file > $OBJ/$t-fgp; then
  10. fail "ssh-keygen -l failed for $t-key"
  11. fi
  12. if ! egrep "^([0-9]+) SHA256:(.){43} ${comment} \(.*\)\$" \
  13. $OBJ/$t-fgp > /dev/null 2>&1; then
  14. fail "comment is not correctly recovered for $t-key"
  15. fi
  16. rm -f $OBJ/$t-fgp
  17. }
  18. for fmt in '' RFC4716 PKCS8 PEM; do
  19. for t in $SSH_KEYTYPES; do
  20. trace "generating $t key in '$fmt' format"
  21. rm -f $OBJ/$t-key*
  22. oldfmt=""
  23. case "$fmt" in
  24. PKCS8 | PEM) oldfmt=1 ;;
  25. esac
  26. # Some key types like ssh-ed25519 and *@openssh.com are never
  27. # stored in old formats.
  28. case "$t" in
  29. ssh-ed25519 | *openssh.com) test -z "$oldfmt" || continue ;;
  30. esac
  31. comment="foo bar"
  32. fmtarg=""
  33. test -z "$fmt" || fmtarg="-m $fmt"
  34. ${SSHKEYGEN} $fmtarg -N '' -C "${comment}" \
  35. -t $t -f $OBJ/$t-key > /dev/null 2>&1 ||
  36. fatal "keygen of $t in format $fmt failed"
  37. check_fingerprint $OBJ/$t-key "${comment}"
  38. check_fingerprint $OBJ/$t-key.pub "${comment}"
  39. # Output fingerprint using only private file
  40. trace "fingerprinting $t key using private key file"
  41. rm -f $OBJ/$t-key.pub
  42. if [ ! -z "$oldfmt" ]; then
  43. # Comment cannot be recovered from old format keys.
  44. comment="no comment"
  45. fi
  46. check_fingerprint $OBJ/$t-key "${comment}"
  47. rm -f $OBJ/$t-key*
  48. done
  49. done