clevis-luks-bind 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash -e
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2016 Red Hat, Inc.
  5. # Author: Harald Hoyer <harald@redhat.com>
  6. # Author: Nathaniel McCallum <npmccallum@redhat.com>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. SUMMARY="Binds a LUKSv1 device using the specified policy"
  22. UUID=cb6e8904-81ff-40da-a84a-07ab9ab5715e
  23. function onerr() {
  24. if [ -n "$DEV" -a -n "$SLT" ]; then
  25. luksmeta wipe -f -d "$DEV" -u "$UUID" -s "$SLT"
  26. SLT=
  27. fi
  28. stty echo
  29. exit 1
  30. }
  31. trap 'onerr' ERR HUP INT QUIT PIPE TERM
  32. function usage() {
  33. echo >&2
  34. echo "Usage: clevis luks bind [-f] [-s SLT] [-k KEY] -d DEV PIN CFG" >&2
  35. echo >&2
  36. echo "$SUMMARY": >&2
  37. echo >&2
  38. echo " -f Do not prompt for LUKSMeta initialization" >&2
  39. echo >&2
  40. echo " -d DEV The LUKS device on which to perform binding" >&2
  41. echo >&2
  42. echo " -s SLT The LUKSMeta slot to use for metadata storage" >&2
  43. echo >&2
  44. echo " -k KEY Non-interactively read LUKS password from KEY file" >&2
  45. echo " -k - Non-interactively read LUKS password from standard input" >&2
  46. echo >&2
  47. exit 1
  48. }
  49. if [ $# -eq 1 -a "$1" == "--summary" ]; then
  50. echo "$SUMMARY"
  51. exit 0
  52. fi
  53. while getopts ":hfd:s:k:" o; do
  54. case "$o" in
  55. f) FRC=-f;;
  56. d) DEV=$OPTARG;;
  57. s) SLT=$OPTARG;;
  58. k) KEY=$OPTARG;;
  59. *) usage;;
  60. esac
  61. done
  62. if [ -z "$DEV" ]; then
  63. echo "Did not specify a device!" >&2
  64. usage
  65. fi
  66. if ! PIN=${@:$((OPTIND++)):1} || [ -z "$PIN" ]; then
  67. echo "Did not specify a pin!" >&2
  68. usage
  69. fi
  70. if ! CFG=${@:$((OPTIND++)):1} || [ -z "$CFG" ]; then
  71. echo "Did not specify a pin config!" >&2
  72. usage
  73. fi
  74. if [ -n "$KEY" ]; then
  75. if [ "$KEY" == "-" ]; then
  76. if ! luksmeta test -d $DEV && [ -z "$FRC" ]; then
  77. echo "Cannot use '-k-' without '-f' unless already initialized!" >&2
  78. usage
  79. fi
  80. elif ! [ -f "$KEY" ]; then
  81. echo "Key file '$KEY' not found!" >&2
  82. exit 1
  83. fi
  84. fi
  85. # Generate a key with the same entropy as the LUKS Master Key
  86. dump=`cryptsetup luksDump $DEV`
  87. bits=`sed -r -n 's|MK bits:[ \t]*([0-9]+)|\1|p' <<< "$dump"`
  88. key=`pwmake $bits`
  89. # Encrypt the new key
  90. jwe=`echo -n "$key" | clevis encrypt "$PIN" "$CFG"`
  91. # If necessary, initialize the LUKS volume
  92. if ! luksmeta test -d $DEV; then
  93. luksmeta init -d $DEV $FRC
  94. fi
  95. # Write the JWE into the specified slot. Or, if no slot is given, ...
  96. if [ -n "$SLT" ]; then
  97. if ! echo -n $jwe | luksmeta save -d "$DEV" -u "$UUID" -s $SLT 2>/dev/null; then
  98. echo "Error while saving Clevis metadata in LUKS header!" >&2
  99. false
  100. fi
  101. # ... write the JWE to the first slot unused by both LUKS and LUKSMeta
  102. elif ! SLT=`echo -n $jwe | luksmeta save -d "$DEV" -u "$UUID" 2>/dev/null`; then
  103. echo "Error while saving Clevis metadata in LUKS header!" >&2
  104. false
  105. fi
  106. export DEV
  107. export SLT
  108. # Add the new key to the LUKS slot that matches the LUKSMeta slot
  109. case "$KEY" in
  110. "") read -s -p "Enter existing LUKS password: " old; echo;;
  111. -) old=`cat`;;
  112. *) old=`cat "$KEY"`;;
  113. esac
  114. echo -e "$old\n$key" | cryptsetup luksAddKey -S $SLT $DEV