jose-jwe-enc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash -ex
  2. WRAP=`jose alg -k wrap`
  3. ENCR=`jose alg -k encr`
  4. tmpdir=`mktemp -d 2>/dev/null || mktemp -d -t jose`
  5. function onexit() {
  6. rm -rf $tmpdir
  7. }
  8. trap exit ERR
  9. trap onexit EXIT
  10. jwk=$tmpdir/jwk
  11. jwe=$tmpdir/jwe
  12. function jqopt() {
  13. if ! which jq >/dev/null 2>&1; then
  14. echo "$3"
  15. else
  16. jq -r "if $2 | type | . == \"string\" then $2 else error(\"\") end" < $1
  17. fi
  18. }
  19. function jqbopt() {
  20. if ! which jq >/dev/null 2>&1; then
  21. echo "$4"
  22. else
  23. jq -r "if $2 | type | . == \"string\" then $2 else error(\"\") end" < $1 \
  24. | jose b64 dec -i- \
  25. | jq -r "if $3 | type | . == \"string\" then $3 else error(\"\") end"
  26. fi
  27. }
  28. for msg in "hi" "this is a longer message that is more than one block"; do
  29. for w in $WRAP; do
  30. [ $w == "dir" ] && continue
  31. jose jwk gen -i "{\"alg\":\"$w\"}" -o $jwk
  32. echo -n "$msg" | jose jwe enc -I- -k $jwk -o $jwe
  33. [ "`jqopt $jwe .header.alg $w`" == "$w" ]
  34. [ "`jose jwe dec -i $jwe -k $jwk -O-`" == "$msg" ]
  35. for e in $ENCR; do
  36. echo -n "$msg" | jose jwe enc \
  37. -i "{\"protected\":{\"enc\":\"$e\"}}" -I- \
  38. -k $jwk -o $jwe
  39. [ "`jqopt $jwe .header.alg $w`" == "$w" ]
  40. [ "`jqbopt $jwe .protected .enc $e`" == "$e" ]
  41. [ "`jose jwe dec -i $jwe -k $jwk -O-`" == "$msg" ]
  42. done
  43. done
  44. for e in $ENCR; do
  45. jose jwk gen -i "{\"alg\":\"$e\"}" -o $jwk
  46. echo -n "$msg" | jose jwe enc \
  47. -i "{\"protected\":{\"alg\":\"dir\"}}" -I- \
  48. -k $jwk -o $jwe
  49. [ "`jqbopt $jwe .protected .alg dir`" == "dir" ]
  50. [ "`jqbopt $jwe .protected .enc $e`" == "$e" ]
  51. [ "`jose jwe dec -i $jwe -k $jwk -O-`" == "$msg" ]
  52. echo -n "$msg" | jose jwe enc -I- -k $jwk -o $jwe
  53. [ "`jqopt $jwe .header.alg dir`" == "dir" ]
  54. [ "`jqbopt $jwe .protected .enc $e`" == "$e" ]
  55. [ "`jose jwe dec -i $jwe -k $jwk -O-`" == "$msg" ]
  56. done
  57. for tmpl in '{"kty":"oct","bytes":32}' '{"kty":"RSA","bits":2048}' '{"kty":"EC","crv":"P-256"}'; do
  58. jose jwk gen -i "$tmpl" -o $jwk
  59. echo -n "$msg" | jose jwe enc -I- -k $jwk -o $jwe
  60. [ "`jose jwe dec -i $jwe -k $jwk -O-`" == "$msg" ]
  61. done
  62. done