oa8sign 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env sh
  2. # vim: filetype=sh:tabstop=4:tw=80
  3. set -e > /dev/null 2>&1
  4. set -u > /dev/null 2>&1
  5. me=oa8sign
  6. vers=1.1.1
  7. osl=openssl
  8. a85=ascii85
  9. # shellcheck disable=SC2097,SC2098
  10. if ! PATH="$(command -p env -i \
  11. getconf PATH)" \
  12. command -p env -i TMPDIR="${TMPDIR:-}" PATH="${PATH:-}" \
  13. rm -f \
  14. "$(command -p env -i TMPDIR="${TMPDIR:-}" mktemp)"; then
  15. printf '%s\n' \
  16. "Error: Misbehaving shell or environment; aborting" >&2
  17. exit 1
  18. fi
  19. cleanup() {
  20. command -p env -i \
  21. rm -f \
  22. "${tmpfile:-}" ||
  23. true
  24. }
  25. filename=${1:-}
  26. privatekey=${2:-}
  27. exitstatus=""
  28. if ! command -p env \
  29. "${osl:?}" version > /dev/null 2>&1; then
  30. exitstatus="${?:?Error: exitstatus undefined}"
  31. if [ "${exitstatus:-}" -eq 126 ]; then
  32. printf '%s\n' \
  33. "Error: ${osl:?} could not be executed" >&2
  34. exit 1
  35. elif [ "${exitstatus:-}" -eq 127 ]; then
  36. printf '%s\n' \
  37. "Error: ${osl:?} could not be found" >&2
  38. exit 1
  39. else
  40. printf '%s\n' \
  41. "Error: ${osl:?} failure ${exitstatus:-}" >&2
  42. exit 1
  43. fi
  44. fi
  45. exitstatus=""
  46. if ! command -p env \
  47. "${a85:?}" -h > /dev/null 2>&1; then
  48. exitstatus="${?:?Error: exitstatus undefined}"
  49. if [ "${exitstatus:-}" -eq 126 ]; then
  50. printf '%s\n' \
  51. "Error: ${a85:?} could not be executed" >&2
  52. exit 1
  53. elif [ "${exitstatus:-}" -eq 127 ]; then
  54. printf '%s\n' \
  55. "Error: ${a85:?} could not be found" >&2
  56. printf '\t%s\n' \
  57. 'Suggestion: "gem install Ascii85"' >&2
  58. exit 1
  59. else
  60. printf '%s\n' \
  61. "Error: ${a85:?} failure ${exitstatus:-}" >&2
  62. exit 1
  63. fi
  64. fi
  65. if [ "${#}" -lt 2 ]; then
  66. printf '%s\n' \
  67. "*** ${me:?Error: me undefined} (v${vers:?Error: vers undefined})"
  68. printf '\t%s\n' \
  69. "Usage: ${me:?} <file> <private.oa8>"
  70. exit 1
  71. fi
  72. if [ ! -f "${filename:?Error: filename undefined}" ]; then
  73. printf '%s\n' \
  74. "Error: \"${filename:?}\" not found" >&2
  75. exit 1
  76. fi
  77. if [ -f "${filename:?Error: filename undefined}.oa8" ]; then
  78. printf '%s\n' \
  79. "Error: \"${filename:?Error: filename undefined}.oa8\" exists" >&2
  80. exit 1
  81. fi
  82. if [ ! -f "${privatekey:?Error: privatekey undefined}" ]; then
  83. printf '%s\n' \
  84. "Error: Private key \"${privatekey:?}\" not found" >&2
  85. exit 1
  86. fi
  87. tmpfile=""
  88. tmpfile=$(command -p env -i TMPDIR="${TMPDIR:-}" \
  89. mktemp) ||
  90. {
  91. printf '%s\n' \
  92. "Error: mktemp failure" >&2
  93. exit 1
  94. }
  95. exitstatus=""
  96. command -p env \
  97. "${osl:?}" dgst -sha3-512 \
  98. -sign "${privatekey:?Error: privatekey undefined}" \
  99. -out "${tmpfile:?Error: tmpfile undefined}" \
  100. "${filename:?Error: filename undefined}" ||
  101. {
  102. exitstatus="${?:?Error: exitstatus undefined}"
  103. if [ "${exitstatus:-}" -eq 126 ]; then
  104. printf '%s\n' \
  105. "Error: ${osl:?} could not be executed" >&2
  106. cleanup
  107. exit 1
  108. elif [ "${exitstatus:-}" -eq 127 ]; then
  109. printf '%s\n' \
  110. "Error: ${osl:?} could not be found" >&2
  111. cleanup
  112. exit 1
  113. else
  114. printf '%s\n' \
  115. "Error: ${osl:?} failure ${exitstatus:-}" >&2
  116. cleanup
  117. exit 1
  118. fi
  119. }
  120. exitstatus=""
  121. printf \
  122. '****** BEGIN OA8 SIGNATURE ******\n%s\n****** END OA8 SIGNATURE ******\n' \
  123. "$(command -p env \
  124. "${a85:?}" -w 61 \
  125. < "${tmpfile:?Error: filename undefined}")" \
  126. > "${filename:?Error: filename undefined}.oa8" ||
  127. {
  128. exitstatus="${?:?Error: exitstatus undefined}"
  129. if [ "${exitstatus:-}" -eq 126 ]; then
  130. printf '%s\n' \
  131. "Error: ${a85:?} could not be executed" >&2
  132. cleanup
  133. exit 1
  134. elif [ "${exitstatus:-}" -eq 127 ]; then
  135. printf '%s\n' \
  136. "Error: ${a85:?} could not be found" >&2
  137. cleanup
  138. exit 1
  139. else
  140. printf '%s\n' \
  141. "Error: ${me:?} failure ${exitstatus:-}" >&2
  142. cleanup
  143. exit 2
  144. fi
  145. }
  146. cleanup
  147. # Local Variables:
  148. # mode: sh
  149. # sh-shell: sh
  150. # sh-indentation: 4
  151. # sh-basic-offset: 4
  152. # tab-width: 4
  153. # End: