clevis-luks-bind 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 LUKS device using the specified policy"
  22. UUID=cb6e8904-81ff-40da-a84a-07ab9ab5715e
  23. function usage() {
  24. echo >&2
  25. echo "Usage: clevis luks bind [-f] [-s SLT] [-k KEY] -d DEV PIN CFG" >&2
  26. echo >&2
  27. echo "$SUMMARY": >&2
  28. echo >&2
  29. echo " -f Do not prompt for LUKSMeta initialization" >&2
  30. echo >&2
  31. echo " -d DEV The LUKS device on which to perform binding" >&2
  32. echo >&2
  33. echo " -s SLT The LUKS slot to use" >&2
  34. echo >&2
  35. echo " -k KEY Non-interactively read LUKS password from KEY file" >&2
  36. echo " -k - Non-interactively read LUKS password from standard input" >&2
  37. echo >&2
  38. exit 1
  39. }
  40. if [ $# -eq 1 -a "$1" == "--summary" ]; then
  41. echo "$SUMMARY"
  42. exit 0
  43. fi
  44. while getopts ":hfd:s:k:" o; do
  45. case "$o" in
  46. f) FRC=-f;;
  47. d) export DEV=$OPTARG;;
  48. s) SLT=$OPTARG;;
  49. k) KEY=$OPTARG;;
  50. *) usage;;
  51. esac
  52. done
  53. if [ -z "$DEV" ]; then
  54. echo "Did not specify a device!" >&2
  55. usage
  56. fi
  57. if ! cryptsetup isLuks "$DEV"; then
  58. echo "$DEV is not a LUKS device!" >&2
  59. exit 1
  60. fi
  61. if ! PIN=${@:$((OPTIND++)):1} || [ -z "$PIN" ]; then
  62. echo "Did not specify a pin!" >&2
  63. usage
  64. fi
  65. if ! CFG=${@:$((OPTIND++)):1} || [ -z "$CFG" ]; then
  66. echo "Did not specify a pin config!" >&2
  67. usage
  68. fi
  69. if [ -n "$KEY" ]; then
  70. if [ "$KEY" == "-" ]; then
  71. if cryptsetup isLuks --type luks1 "$DEV"; then
  72. if ! luksmeta test -d $DEV && [ -z "$FRC" ]; then
  73. echo "Cannot use '-k-' without '-f' unless already initialized!" >&2
  74. usage
  75. fi
  76. fi
  77. elif ! [ -f "$KEY" ]; then
  78. echo "Key file '$KEY' not found!" >&2
  79. exit 1
  80. fi
  81. fi
  82. # Generate a key with the same entropy as the LUKS Master Key
  83. dump=`cryptsetup luksDump $DEV`
  84. if cryptsetup isLuks --type luks1 "$DEV"; then
  85. filt=`sed -rn 's|MK bits:[ \t]*([0-9]+)|\1|p' <<< "$dump"`
  86. else
  87. filt=`sed -rn 's|^\s+Key:\s+([0-9]+) bits\s*$|\1|p' <<< "$dump"`
  88. fi
  89. bits=`sort -n <<<"$filt" | tail -n 1`
  90. export key=`pwmake $bits`
  91. # Encrypt the new key
  92. jwe=`echo -n "$key" | clevis encrypt "$PIN" "$CFG"`
  93. # If necessary, initialize the LUKS volume
  94. if cryptsetup isLuks --type luks1 "$DEV" && ! luksmeta test -d "$DEV"; then
  95. luksmeta init -d "$DEV" $FRC
  96. fi
  97. # Get the old key
  98. case "$KEY" in
  99. "") read -s -p "Enter existing LUKS password: " old; echo;;
  100. -) old=`/bin/cat`;;
  101. *) old=`/bin/cat "$KEY"`;;
  102. esac
  103. # Add the new key
  104. if [ -n "$SLT" ]; then
  105. if ! echo -e "$old\n$key" | cryptsetup luksAddKey --key-slot $SLT $DEV; then
  106. echo "Error while adding new key to LUKS header!" >&2
  107. exit 1
  108. fi
  109. elif ! SLT=`echo -e "$old\n$key" \
  110. | cryptsetup luksAddKey -v $DEV \
  111. | sed -rn 's|^Key slot ([0-9]+) created\.$|\1|p'`; then
  112. echo "Error while adding new key to LUKS header!" >&2
  113. exit 1
  114. fi
  115. if cryptsetup isLuks --type luks1 "$DEV"; then
  116. if ! echo -n $jwe | luksmeta save -d "$DEV" -u "$UUID" -s $SLT 2>/dev/null; then
  117. echo "Error while saving Clevis metadata in LUKSMeta!" >&2
  118. cryptsetup luksRemoveKey "$DEV" <<<"$key"
  119. exit 1
  120. fi
  121. else
  122. jwe=`jose jwe fmt -i- <<<"$jwe"` # Convert to JSON Serialization
  123. tok="{\"type\":\"clevis\",\"keyslots\":[\"$SLT\"],\"jwe\":$jwe}"
  124. if ! cryptsetup token import "$DEV" <<<"$tok" ; then
  125. echo "Error while saving Clevis metadata as a LUKS token!" >&2
  126. cryptsetup luksRemoveKey "$DEV" <<<"$key"
  127. exit 1
  128. fi
  129. fi