4 * This file and its contents are supplied under the terms of the
5 * Common Development and Distribution License ("CDDL"), version 1.0.
6 * You may only use this file in accordance with the terms of version
9 * A full copy of the text of the CDDL should have accompanied this
10 * source. A copy of the CDDL is also available via the Internet at
11 * http://www.illumos.org/license/CDDL.
17 * Copyright (c) 2017, Datto, Inc. All rights reserved.
18 * Copyright 2020 Joyent, Inc.
21 #include <sys/zfs_context.h>
22 #include <sys/fs/zfs.h>
23 #include <sys/dsl_crypt.h>
28 #include <openssl/evp.h>
30 #include "libzfs_impl.h"
31 #include "zfeature_common.h"
34 * User keys are used to decrypt the master encryption keys of a dataset. This
35 * indirection allows a user to change his / her access key without having to
36 * re-encrypt the entire dataset. User keys can be provided in one of several
37 * ways. Raw keys are simply given to the kernel as is. Similarly, hex keys
38 * are converted to binary and passed into the kernel. Password based keys are
39 * a bit more complicated. Passwords alone do not provide suitable entropy for
40 * encryption and may be too short or too long to be used. In order to derive
41 * a more appropriate key we use a PBKDF2 function. This function is designed
42 * to take a (relatively) long time to calculate in order to discourage
43 * attackers from guessing from a list of common passwords. PBKDF2 requires
44 * 2 additional parameters. The first is the number of iterations to run, which
45 * will ultimately determine how long it takes to derive the resulting key from
46 * the password. The second parameter is a salt that is randomly generated for
47 * each dataset. The salt is used to "tweak" PBKDF2 such that a group of
48 * attackers cannot reasonably generate a table of commonly known passwords to
49 * their output keys and expect it work for all past and future PBKDF2 users.
50 * We store the salt as a hidden property of the dataset (although it is
51 * technically ok if the salt is known to the attacker).
54 typedef enum key_locator {
60 #define MIN_PASSPHRASE_LEN 8
61 #define MAX_PASSPHRASE_LEN 512
62 #define MAX_KEY_PROMPT_ATTEMPTS 3
64 static int caught_interrupt;
66 static int get_key_material_file(libzfs_handle_t *, const char *, const char *,
67 zfs_keyformat_t, boolean_t, uint8_t **, size_t *);
69 static zfs_uri_handler_t uri_handlers[] = {
70 { "file", get_key_material_file },
75 pkcs11_get_urandom(uint8_t *buf, size_t bytes)
78 ssize_t bytes_read = 0;
80 rand = open("/dev/urandom", O_RDONLY);
85 while (bytes_read < bytes) {
86 ssize_t rc = read(rand, buf + bytes_read, bytes - bytes_read);
98 zfs_prop_parse_keylocation(libzfs_handle_t *restrict hdl, const char *str,
99 zfs_keylocation_t *restrict locp, char **restrict schemep)
101 *locp = ZFS_KEYLOCATION_NONE;
104 if (strcmp("prompt", str) == 0) {
105 *locp = ZFS_KEYLOCATION_PROMPT;
109 regmatch_t pmatch[2];
111 if (regexec(&hdl->libzfs_urire, str, ARRAY_SIZE(pmatch),
115 if (pmatch[1].rm_so == -1) {
116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
121 scheme_len = pmatch[1].rm_eo - pmatch[1].rm_so;
123 *schemep = calloc(1, scheme_len + 1);
124 if (*schemep == NULL) {
128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133 (void) memcpy(*schemep, str + pmatch[1].rm_so, scheme_len);
134 *locp = ZFS_KEYLOCATION_URI;
138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Invalid keylocation"));
143 hex_key_to_raw(char *hex, int hexlen, uint8_t *out)
148 for (i = 0; i < hexlen; i += 2) {
149 if (!isxdigit(hex[i]) || !isxdigit(hex[i + 1])) {
154 ret = sscanf(&hex[i], "%02x", &c);
171 catch_signal(int sig)
173 caught_interrupt = sig;
177 get_format_prompt_string(zfs_keyformat_t format)
180 case ZFS_KEYFORMAT_RAW:
182 case ZFS_KEYFORMAT_HEX:
184 case ZFS_KEYFORMAT_PASSPHRASE:
185 return ("passphrase");
187 /* shouldn't happen */
192 /* do basic validation of the key material */
194 validate_key(libzfs_handle_t *hdl, zfs_keyformat_t keyformat,
195 const char *key, size_t keylen)
198 case ZFS_KEYFORMAT_RAW:
199 /* verify the key length is correct */
200 if (keylen < WRAPPING_KEY_LEN) {
201 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
202 "Raw key too short (expected %u)."),
207 if (keylen > WRAPPING_KEY_LEN) {
208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
209 "Raw key too long (expected %u)."),
214 case ZFS_KEYFORMAT_HEX:
215 /* verify the key length is correct */
216 if (keylen < WRAPPING_KEY_LEN * 2) {
217 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
218 "Hex key too short (expected %u)."),
219 WRAPPING_KEY_LEN * 2);
223 if (keylen > WRAPPING_KEY_LEN * 2) {
224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
225 "Hex key too long (expected %u)."),
226 WRAPPING_KEY_LEN * 2);
230 /* check for invalid hex digits */
231 for (size_t i = 0; i < WRAPPING_KEY_LEN * 2; i++) {
232 if (!isxdigit(key[i])) {
233 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
234 "Invalid hex character detected."));
239 case ZFS_KEYFORMAT_PASSPHRASE:
240 /* verify the length is within bounds */
241 if (keylen > MAX_PASSPHRASE_LEN) {
242 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
243 "Passphrase too long (max %u)."),
248 if (keylen < MIN_PASSPHRASE_LEN) {
249 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
250 "Passphrase too short (min %u)."),
256 /* can't happen, checked above */
264 libzfs_getpassphrase(zfs_keyformat_t keyformat, boolean_t is_reenter,
265 boolean_t new_key, const char *fsname,
266 char **restrict res, size_t *restrict reslen)
272 struct termios old_term, new_term;
273 struct sigaction act, osigint, osigtstp;
279 * handle SIGINT and ignore SIGSTP. This is necessary to
280 * restore the state of the terminal.
282 caught_interrupt = 0;
284 (void) sigemptyset(&act.sa_mask);
285 act.sa_handler = catch_signal;
287 (void) sigaction(SIGINT, &act, &osigint);
288 act.sa_handler = SIG_IGN;
289 (void) sigaction(SIGTSTP, &act, &osigtstp);
291 (void) printf("%s %s%s",
292 is_reenter ? "Re-enter" : "Enter",
293 new_key ? "new " : "",
294 get_format_prompt_string(keyformat));
296 (void) printf(" for '%s'", fsname);
297 (void) fputc(':', stdout);
298 (void) fflush(stdout);
300 /* disable the terminal echo for key input */
301 (void) tcgetattr(fileno(f), &old_term);
304 new_term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
306 ret = tcsetattr(fileno(f), TCSAFLUSH, &new_term);
313 bytes = getline(res, &buflen, f);
320 /* trim the ending newline if it exists */
321 if (bytes > 0 && (*res)[bytes - 1] == '\n') {
322 (*res)[bytes - 1] = '\0';
329 /* reset the terminal */
330 (void) tcsetattr(fileno(f), TCSAFLUSH, &old_term);
331 (void) sigaction(SIGINT, &osigint, NULL);
332 (void) sigaction(SIGTSTP, &osigtstp, NULL);
334 /* if we caught a signal, re-throw it now */
335 if (caught_interrupt != 0)
336 (void) kill(getpid(), caught_interrupt);
338 /* print the newline that was not echo'd */
345 get_key_interactive(libzfs_handle_t *restrict hdl, const char *fsname,
346 zfs_keyformat_t keyformat, boolean_t confirm_key, boolean_t newkey,
347 uint8_t **restrict outbuf, size_t *restrict len_out)
349 char *buf = NULL, *buf2 = NULL;
350 size_t buflen = 0, buf2len = 0;
353 ASSERT(isatty(fileno(stdin)));
355 /* raw keys cannot be entered on the terminal */
356 if (keyformat == ZFS_KEYFORMAT_RAW) {
358 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
359 "Cannot enter raw keys on the terminal"));
363 /* prompt for the key */
364 if ((ret = libzfs_getpassphrase(keyformat, B_FALSE, newkey, fsname,
365 &buf, &buflen)) != 0) {
375 if ((ret = validate_key(hdl, keyformat, buf, buflen)) != 0) {
380 ret = libzfs_getpassphrase(keyformat, B_TRUE, newkey, fsname, &buf2,
386 buflen = buf2len = 0;
390 if (buflen != buf2len || strcmp(buf, buf2) != 0) {
396 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
397 "Provided keys do not match."));
403 *outbuf = (uint8_t *)buf;
409 get_key_material_raw(FILE *fd, zfs_keyformat_t keyformat,
410 uint8_t **buf, size_t *len_out)
417 /* read the key material */
418 if (keyformat != ZFS_KEYFORMAT_RAW) {
421 bytes = getline((char **)buf, &buflen, fd);
428 /* trim the ending newline if it exists */
429 if (bytes > 0 && (*buf)[bytes - 1] == '\n') {
430 (*buf)[bytes - 1] = '\0';
439 * Raw keys may have newline characters in them and so can't
440 * use getline(). Here we attempt to read 33 bytes so that we
441 * can properly check the key length (the file should only have
444 *buf = malloc((WRAPPING_KEY_LEN + 1) * sizeof (uint8_t));
450 n = fread(*buf, 1, WRAPPING_KEY_LEN + 1, fd);
451 if (n == 0 || ferror(fd)) {
452 /* size errors are handled by the calling function */
467 get_key_material_file(libzfs_handle_t *hdl, const char *uri,
468 const char *fsname, zfs_keyformat_t keyformat, boolean_t newkey,
469 uint8_t **restrict buf, size_t *restrict len_out)
477 if ((f = fopen(uri + 7, "r")) == NULL) {
480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
481 "Failed to open key material file"));
485 ret = get_key_material_raw(f, keyformat, buf, len_out);
493 * Attempts to fetch key material, no matter where it might live. The key
494 * material is allocated and returned in km_out. *can_retry_out will be set
495 * to B_TRUE if the user is providing the key material interactively, allowing
496 * for re-entry attempts.
499 get_key_material(libzfs_handle_t *hdl, boolean_t do_verify, boolean_t newkey,
500 zfs_keyformat_t keyformat, char *keylocation, const char *fsname,
501 uint8_t **km_out, size_t *kmlen_out, boolean_t *can_retry_out)
504 zfs_keylocation_t keyloc = ZFS_KEYLOCATION_NONE;
507 char *uri_scheme = NULL;
508 zfs_uri_handler_t *handler = NULL;
509 boolean_t can_retry = B_FALSE;
511 /* verify and parse the keylocation */
512 ret = zfs_prop_parse_keylocation(hdl, keylocation, &keyloc,
517 /* open the appropriate file descriptor */
519 case ZFS_KEYLOCATION_PROMPT:
520 if (isatty(fileno(stdin))) {
522 ret = get_key_interactive(hdl, fsname, keyformat,
523 do_verify, newkey, &km, &kmlen);
525 /* fetch the key material into the buffer */
526 ret = get_key_material_raw(stdin, keyformat, &km,
534 case ZFS_KEYLOCATION_URI:
535 for (handler = uri_handlers; handler->zuh_scheme != NULL;
537 if (strcmp(handler->zuh_scheme, uri_scheme) != 0)
540 if ((ret = handler->zuh_handler(hdl, keylocation,
541 fsname, keyformat, newkey, &km, &kmlen)) != 0)
548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
549 "URI scheme is not supported"));
554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 "Invalid keylocation."));
559 if ((ret = validate_key(hdl, keyformat, (const char *)km, kmlen)) != 0)
564 if (can_retry_out != NULL)
565 *can_retry_out = can_retry;
576 if (can_retry_out != NULL)
577 *can_retry_out = can_retry;
584 derive_key(libzfs_handle_t *hdl, zfs_keyformat_t format, uint64_t iters,
585 uint8_t *key_material, size_t key_material_len, uint64_t salt,
593 key = zfs_alloc(hdl, WRAPPING_KEY_LEN);
598 case ZFS_KEYFORMAT_RAW:
599 bcopy(key_material, key, WRAPPING_KEY_LEN);
601 case ZFS_KEYFORMAT_HEX:
602 ret = hex_key_to_raw((char *)key_material,
603 WRAPPING_KEY_LEN * 2, key);
605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
606 "Invalid hex key provided."));
610 case ZFS_KEYFORMAT_PASSPHRASE:
613 ret = PKCS5_PBKDF2_HMAC_SHA1((char *)key_material,
614 strlen((char *)key_material), ((uint8_t *)&salt),
615 sizeof (uint64_t), iters, WRAPPING_KEY_LEN, key);
618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
619 "Failed to generate key from passphrase."));
639 encryption_feature_is_enabled(zpool_handle_t *zph)
642 uint64_t feat_refcount;
644 /* check that features can be enabled */
645 if (zpool_get_prop_int(zph, ZPOOL_PROP_VERSION, NULL)
646 < SPA_VERSION_FEATURES)
649 /* check for crypto feature */
650 features = zpool_get_features(zph);
651 if (!features || nvlist_lookup_uint64(features,
652 spa_feature_table[SPA_FEATURE_ENCRYPTION].fi_guid,
653 &feat_refcount) != 0)
660 populate_create_encryption_params_nvlists(libzfs_handle_t *hdl,
661 zfs_handle_t *zhp, boolean_t newkey, zfs_keyformat_t keyformat,
662 char *keylocation, nvlist_t *props, uint8_t **wkeydata, uint_t *wkeylen)
665 uint64_t iters = 0, salt = 0;
666 uint8_t *key_material = NULL;
667 size_t key_material_len = 0;
668 uint8_t *key_data = NULL;
669 const char *fsname = (zhp) ? zfs_get_name(zhp) : NULL;
671 /* get key material from keyformat and keylocation */
672 ret = get_key_material(hdl, B_TRUE, newkey, keyformat, keylocation,
673 fsname, &key_material, &key_material_len, NULL);
677 /* passphrase formats require a salt and pbkdf2 iters property */
678 if (keyformat == ZFS_KEYFORMAT_PASSPHRASE) {
679 /* always generate a new salt */
680 ret = pkcs11_get_urandom((uint8_t *)&salt, sizeof (uint64_t));
681 if (ret != sizeof (uint64_t)) {
682 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
683 "Failed to generate salt."));
687 ret = nvlist_add_uint64(props,
688 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), salt);
690 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
691 "Failed to add salt to properties."));
696 * If not otherwise specified, use the default number of
697 * pbkdf2 iterations. If specified, we have already checked
698 * that the given value is greater than MIN_PBKDF2_ITERATIONS
699 * during zfs_valid_proplist().
701 ret = nvlist_lookup_uint64(props,
702 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);
704 iters = DEFAULT_PBKDF2_ITERATIONS;
705 ret = nvlist_add_uint64(props,
706 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), iters);
709 } else if (ret != 0) {
710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
711 "Failed to get pbkdf2 iterations."));
715 /* check that pbkdf2iters was not specified by the user */
716 ret = nvlist_lookup_uint64(props,
717 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);
720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
721 "Cannot specify pbkdf2iters with a non-passphrase "
727 /* derive a key from the key material */
728 ret = derive_key(hdl, keyformat, iters, key_material, key_material_len,
735 *wkeydata = key_data;
736 *wkeylen = WRAPPING_KEY_LEN;
740 if (key_material != NULL)
742 if (key_data != NULL)
751 proplist_has_encryption_props(nvlist_t *props)
757 ret = nvlist_lookup_uint64(props,
758 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &intval);
759 if (ret == 0 && intval != ZIO_CRYPT_OFF)
762 ret = nvlist_lookup_string(props,
763 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &strval);
764 if (ret == 0 && strcmp(strval, "none") != 0)
767 ret = nvlist_lookup_uint64(props,
768 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &intval);
772 ret = nvlist_lookup_uint64(props,
773 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &intval);
781 zfs_crypto_get_encryption_root(zfs_handle_t *zhp, boolean_t *is_encroot,
785 char prop_encroot[MAXNAMELEN];
787 /* if the dataset isn't encrypted, just return */
788 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) == ZIO_CRYPT_OFF) {
789 *is_encroot = B_FALSE;
795 ret = zfs_prop_get(zhp, ZFS_PROP_ENCRYPTION_ROOT, prop_encroot,
796 sizeof (prop_encroot), NULL, NULL, 0, B_TRUE);
798 *is_encroot = B_FALSE;
804 *is_encroot = strcmp(prop_encroot, zfs_get_name(zhp)) == 0;
806 strcpy(buf, prop_encroot);
812 zfs_crypto_create(libzfs_handle_t *hdl, char *parent_name, nvlist_t *props,
813 nvlist_t *pool_props, boolean_t stdin_available, uint8_t **wkeydata_out,
818 uint64_t crypt = ZIO_CRYPT_INHERIT, pcrypt = ZIO_CRYPT_INHERIT;
819 uint64_t keyformat = ZFS_KEYFORMAT_NONE;
820 char *keylocation = NULL;
821 zfs_handle_t *pzhp = NULL;
822 uint8_t *wkeydata = NULL;
824 boolean_t local_crypt = B_TRUE;
826 (void) snprintf(errbuf, sizeof (errbuf),
827 dgettext(TEXT_DOMAIN, "Encryption create error"));
829 /* lookup crypt from props */
830 ret = nvlist_lookup_uint64(props,
831 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &crypt);
833 local_crypt = B_FALSE;
835 /* lookup key location and format from props */
836 (void) nvlist_lookup_uint64(props,
837 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);
838 (void) nvlist_lookup_string(props,
839 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
841 if (parent_name != NULL) {
842 /* get a reference to parent dataset */
843 pzhp = make_dataset_handle(hdl, parent_name);
846 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
847 "Failed to lookup parent."));
851 /* Lookup parent's crypt */
852 pcrypt = zfs_prop_get_int(pzhp, ZFS_PROP_ENCRYPTION);
854 /* Params require the encryption feature */
855 if (!encryption_feature_is_enabled(pzhp->zpool_hdl)) {
856 if (proplist_has_encryption_props(props)) {
858 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
859 "Encryption feature not enabled."));
868 * special case for root dataset where encryption feature
869 * feature won't be on disk yet
871 if (!nvlist_exists(pool_props, "feature@encryption")) {
872 if (proplist_has_encryption_props(props)) {
874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
875 "Encryption feature not enabled."));
883 pcrypt = ZIO_CRYPT_OFF;
886 /* Get the inherited encryption property if we don't have it locally */
891 * At this point crypt should be the actual encryption value. If
892 * encryption is off just verify that no encryption properties have
893 * been specified and return.
895 if (crypt == ZIO_CRYPT_OFF) {
896 if (proplist_has_encryption_props(props)) {
898 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
899 "Encryption must be turned on to set encryption "
909 * If we have a parent crypt it is valid to specify encryption alone.
910 * This will result in a child that is encrypted with the chosen
911 * encryption suite that will also inherit the parent's key. If
912 * the parent is not encrypted we need an encryption suite provided.
914 if (pcrypt == ZIO_CRYPT_OFF && keylocation == NULL &&
915 keyformat == ZFS_KEYFORMAT_NONE) {
917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
918 "Keyformat required for new encryption root."));
923 * Specifying a keylocation implies this will be a new encryption root.
924 * Check that a keyformat is also specified.
926 if (keylocation != NULL && keyformat == ZFS_KEYFORMAT_NONE) {
928 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
929 "Keyformat required for new encryption root."));
933 /* default to prompt if no keylocation is specified */
934 if (keyformat != ZFS_KEYFORMAT_NONE && keylocation == NULL) {
935 keylocation = "prompt";
936 ret = nvlist_add_string(props,
937 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), keylocation);
943 * If a local key is provided, this dataset will be a new
944 * encryption root. Populate the encryption params.
946 if (keylocation != NULL) {
948 * 'zfs recv -o keylocation=prompt' won't work because stdin
949 * is being used by the send stream, so we disallow it.
951 if (!stdin_available && strcmp(keylocation, "prompt") == 0) {
953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Cannot use "
954 "'prompt' keylocation because stdin is in use."));
958 ret = populate_create_encryption_params_nvlists(hdl, NULL,
959 B_FALSE, keyformat, keylocation, props, &wkeydata,
968 *wkeydata_out = wkeydata;
969 *wkeylen_out = wkeylen;
975 if (wkeydata != NULL)
978 *wkeydata_out = NULL;
984 zfs_crypto_clone_check(libzfs_handle_t *hdl, zfs_handle_t *origin_zhp,
985 char *parent_name, nvlist_t *props)
989 (void) snprintf(errbuf, sizeof (errbuf),
990 dgettext(TEXT_DOMAIN, "Encryption clone error"));
993 * No encryption properties should be specified. They will all be
994 * inherited from the origin dataset.
996 if (nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_KEYFORMAT)) ||
997 nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_KEYLOCATION)) ||
998 nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_ENCRYPTION)) ||
999 nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS))) {
1000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1001 "Encryption properties must inherit from origin dataset."));
1008 typedef struct loadkeys_cbdata {
1009 uint64_t cb_numfailed;
1010 uint64_t cb_numattempted;
1014 load_keys_cb(zfs_handle_t *zhp, void *arg)
1017 boolean_t is_encroot;
1018 loadkey_cbdata_t *cb = arg;
1019 uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1021 /* only attempt to load keys for encryption roots */
1022 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
1023 if (ret != 0 || !is_encroot)
1026 /* don't attempt to load already loaded keys */
1027 if (keystatus == ZFS_KEYSTATUS_AVAILABLE)
1030 /* Attempt to load the key. Record status in cb. */
1031 cb->cb_numattempted++;
1033 ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
1038 (void) zfs_iter_filesystems(zhp, load_keys_cb, cb);
1041 /* always return 0, since this function is best effort */
1046 * This function is best effort. It attempts to load all the keys for the given
1047 * filesystem and all of its children.
1050 zfs_crypto_attempt_load_keys(libzfs_handle_t *hdl, char *fsname)
1053 zfs_handle_t *zhp = NULL;
1054 loadkey_cbdata_t cb = { 0 };
1056 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1062 ret = load_keys_cb(zfs_handle_dup(zhp), &cb);
1066 (void) printf(gettext("%llu / %llu keys successfully loaded\n"),
1067 (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
1068 (u_longlong_t)cb.cb_numattempted);
1070 if (cb.cb_numfailed != 0) {
1085 zfs_crypto_load_key(zfs_handle_t *zhp, boolean_t noop, char *alt_keylocation)
1087 int ret, attempts = 0;
1089 uint64_t keystatus, iters = 0, salt = 0;
1090 uint64_t keyformat = ZFS_KEYFORMAT_NONE;
1091 char prop_keylocation[MAXNAMELEN];
1092 char prop_encroot[MAXNAMELEN];
1093 char *keylocation = NULL;
1094 uint8_t *key_material = NULL, *key_data = NULL;
1095 size_t key_material_len;
1096 boolean_t is_encroot, can_retry = B_FALSE, correctible = B_FALSE;
1098 (void) snprintf(errbuf, sizeof (errbuf),
1099 dgettext(TEXT_DOMAIN, "Key load error"));
1101 /* check that encryption is enabled for the pool */
1102 if (!encryption_feature_is_enabled(zhp->zpool_hdl)) {
1103 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1104 "Encryption feature not enabled."));
1109 /* Fetch the keyformat. Check that the dataset is encrypted. */
1110 keyformat = zfs_prop_get_int(zhp, ZFS_PROP_KEYFORMAT);
1111 if (keyformat == ZFS_KEYFORMAT_NONE) {
1112 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1113 "'%s' is not encrypted."), zfs_get_name(zhp));
1119 * Fetch the key location. Check that we are working with an
1122 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, prop_encroot);
1124 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1125 "Failed to get encryption root for '%s'."),
1128 } else if (!is_encroot) {
1129 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1130 "Keys must be loaded for encryption root of '%s' (%s)."),
1131 zfs_get_name(zhp), prop_encroot);
1137 * if the caller has elected to override the keylocation property
1140 if (alt_keylocation != NULL) {
1141 keylocation = alt_keylocation;
1143 ret = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION, prop_keylocation,
1144 sizeof (prop_keylocation), NULL, NULL, 0, B_TRUE);
1146 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1147 "Failed to get keylocation for '%s'."),
1152 keylocation = prop_keylocation;
1155 /* check that the key is unloaded unless this is a noop */
1157 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1158 if (keystatus == ZFS_KEYSTATUS_AVAILABLE) {
1159 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1160 "Key already loaded for '%s'."), zfs_get_name(zhp));
1166 /* passphrase formats require a salt and pbkdf2_iters property */
1167 if (keyformat == ZFS_KEYFORMAT_PASSPHRASE) {
1168 salt = zfs_prop_get_int(zhp, ZFS_PROP_PBKDF2_SALT);
1169 iters = zfs_prop_get_int(zhp, ZFS_PROP_PBKDF2_ITERS);
1173 /* fetching and deriving the key are correctable errors. set the flag */
1174 correctible = B_TRUE;
1176 /* get key material from key format and location */
1177 ret = get_key_material(zhp->zfs_hdl, B_FALSE, B_FALSE, keyformat,
1178 keylocation, zfs_get_name(zhp), &key_material, &key_material_len,
1183 /* derive a key from the key material */
1184 ret = derive_key(zhp->zfs_hdl, keyformat, iters, key_material,
1185 key_material_len, salt, &key_data);
1189 correctible = B_FALSE;
1191 /* pass the wrapping key and noop flag to the ioctl */
1192 ret = lzc_load_key(zhp->zfs_name, noop, key_data, WRAPPING_KEY_LEN);
1196 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1197 "Permission denied."));
1200 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1201 "Invalid parameters provided for dataset %s."),
1205 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1206 "Key already loaded for '%s'."), zfs_get_name(zhp));
1209 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1210 "'%s' is busy."), zfs_get_name(zhp));
1213 correctible = B_TRUE;
1214 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1215 "Incorrect key provided for '%s'."),
1228 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1229 if (key_material != NULL) {
1231 key_material = NULL;
1233 if (key_data != NULL) {
1239 * Here we decide if it is ok to allow the user to retry entering their
1240 * key. The can_retry flag will be set if the user is entering their
1241 * key from an interactive prompt. The correctable flag will only be
1242 * set if an error that occurred could be corrected by retrying. Both
1243 * flags are needed to allow the user to attempt key entry again
1246 if (can_retry && correctible && attempts < MAX_KEY_PROMPT_ATTEMPTS)
1253 zfs_crypto_unload_key(zfs_handle_t *zhp)
1257 char prop_encroot[MAXNAMELEN];
1258 uint64_t keystatus, keyformat;
1259 boolean_t is_encroot;
1261 (void) snprintf(errbuf, sizeof (errbuf),
1262 dgettext(TEXT_DOMAIN, "Key unload error"));
1264 /* check that encryption is enabled for the pool */
1265 if (!encryption_feature_is_enabled(zhp->zpool_hdl)) {
1266 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1267 "Encryption feature not enabled."));
1272 /* Fetch the keyformat. Check that the dataset is encrypted. */
1273 keyformat = zfs_prop_get_int(zhp, ZFS_PROP_KEYFORMAT);
1274 if (keyformat == ZFS_KEYFORMAT_NONE) {
1275 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1276 "'%s' is not encrypted."), zfs_get_name(zhp));
1282 * Fetch the key location. Check that we are working with an
1285 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, prop_encroot);
1287 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1288 "Failed to get encryption root for '%s'."),
1291 } else if (!is_encroot) {
1292 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1293 "Keys must be unloaded for encryption root of '%s' (%s)."),
1294 zfs_get_name(zhp), prop_encroot);
1299 /* check that the key is loaded */
1300 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1301 if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
1302 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1303 "Key already unloaded for '%s'."), zfs_get_name(zhp));
1308 /* call the ioctl */
1309 ret = lzc_unload_key(zhp->zfs_name);
1314 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1315 "Permission denied."));
1318 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1319 "Key already unloaded for '%s'."),
1323 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1324 "'%s' is busy."), zfs_get_name(zhp));
1327 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1333 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1338 zfs_crypto_verify_rewrap_nvlist(zfs_handle_t *zhp, nvlist_t *props,
1339 nvlist_t **props_out, char *errbuf)
1342 nvpair_t *elem = NULL;
1344 nvlist_t *new_props = NULL;
1346 new_props = fnvlist_alloc();
1349 * loop through all provided properties, we should only have
1350 * keyformat, keylocation and pbkdf2iters. The actual validation of
1351 * values is done by zfs_valid_proplist().
1353 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
1354 const char *propname = nvpair_name(elem);
1355 prop = zfs_name_to_prop(propname);
1358 case ZFS_PROP_PBKDF2_ITERS:
1359 case ZFS_PROP_KEYFORMAT:
1360 case ZFS_PROP_KEYLOCATION:
1364 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1365 "Only keyformat, keylocation and pbkdf2iters may "
1366 "be set with this command."));
1371 new_props = zfs_valid_proplist(zhp->zfs_hdl, zhp->zfs_type, props,
1372 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), NULL, zhp->zpool_hdl,
1374 if (new_props == NULL) {
1379 *props_out = new_props;
1383 nvlist_free(new_props);
1389 zfs_crypto_rewrap(zfs_handle_t *zhp, nvlist_t *raw_props, boolean_t inheritkey)
1393 boolean_t is_encroot;
1394 nvlist_t *props = NULL;
1395 uint8_t *wkeydata = NULL;
1397 dcp_cmd_t cmd = (inheritkey) ? DCP_CMD_INHERIT : DCP_CMD_NEW_KEY;
1398 uint64_t crypt, pcrypt, keystatus, pkeystatus;
1399 uint64_t keyformat = ZFS_KEYFORMAT_NONE;
1400 zfs_handle_t *pzhp = NULL;
1401 char *keylocation = NULL;
1402 char origin_name[MAXNAMELEN];
1403 char prop_keylocation[MAXNAMELEN];
1404 char parent_name[ZFS_MAX_DATASET_NAME_LEN];
1406 (void) snprintf(errbuf, sizeof (errbuf),
1407 dgettext(TEXT_DOMAIN, "Key change error"));
1409 /* check that encryption is enabled for the pool */
1410 if (!encryption_feature_is_enabled(zhp->zpool_hdl)) {
1411 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1412 "Encryption feature not enabled."));
1417 /* get crypt from dataset */
1418 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1419 if (crypt == ZIO_CRYPT_OFF) {
1420 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1421 "Dataset not encrypted."));
1426 /* get the encryption root of the dataset */
1427 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
1429 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1430 "Failed to get encryption root for '%s'."),
1435 /* Clones use their origin's key and cannot rewrap it */
1436 ret = zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin_name,
1437 sizeof (origin_name), NULL, NULL, 0, B_TRUE);
1438 if (ret == 0 && strcmp(origin_name, "") != 0) {
1439 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1440 "Keys cannot be changed on clones."));
1446 * If the user wants to use the inheritkey variant of this function
1447 * we don't need to collect any crypto arguments.
1450 /* validate the provided properties */
1451 ret = zfs_crypto_verify_rewrap_nvlist(zhp, raw_props, &props,
1457 * Load keyformat and keylocation from the nvlist. Fetch from
1458 * the dataset properties if not specified.
1460 (void) nvlist_lookup_uint64(props,
1461 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);
1462 (void) nvlist_lookup_string(props,
1463 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
1467 * If this is already an encryption root, just keep
1468 * any properties not set by the user.
1470 if (keyformat == ZFS_KEYFORMAT_NONE) {
1471 keyformat = zfs_prop_get_int(zhp,
1472 ZFS_PROP_KEYFORMAT);
1473 ret = nvlist_add_uint64(props,
1474 zfs_prop_to_name(ZFS_PROP_KEYFORMAT),
1477 zfs_error_aux(zhp->zfs_hdl,
1478 dgettext(TEXT_DOMAIN, "Failed to "
1479 "get existing keyformat "
1485 if (keylocation == NULL) {
1486 ret = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
1487 prop_keylocation, sizeof (prop_keylocation),
1488 NULL, NULL, 0, B_TRUE);
1490 zfs_error_aux(zhp->zfs_hdl,
1491 dgettext(TEXT_DOMAIN, "Failed to "
1492 "get existing keylocation "
1497 keylocation = prop_keylocation;
1500 /* need a new key for non-encryption roots */
1501 if (keyformat == ZFS_KEYFORMAT_NONE) {
1503 zfs_error_aux(zhp->zfs_hdl,
1504 dgettext(TEXT_DOMAIN, "Keyformat required "
1505 "for new encryption root."));
1509 /* default to prompt if no keylocation is specified */
1510 if (keylocation == NULL) {
1511 keylocation = "prompt";
1512 ret = nvlist_add_string(props,
1513 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1520 /* fetch the new wrapping key and associated properties */
1521 ret = populate_create_encryption_params_nvlists(zhp->zfs_hdl,
1522 zhp, B_TRUE, keyformat, keylocation, props, &wkeydata,
1527 /* check that zhp is an encryption root */
1529 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1530 "Key inheritting can only be performed on "
1531 "encryption roots."));
1536 /* get the parent's name */
1537 ret = zfs_parent_name(zhp, parent_name, sizeof (parent_name));
1539 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1540 "Root dataset cannot inherit key."));
1545 /* get a handle to the parent */
1546 pzhp = make_dataset_handle(zhp->zfs_hdl, parent_name);
1548 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1549 "Failed to lookup parent."));
1554 /* parent must be encrypted */
1555 pcrypt = zfs_prop_get_int(pzhp, ZFS_PROP_ENCRYPTION);
1556 if (pcrypt == ZIO_CRYPT_OFF) {
1557 zfs_error_aux(pzhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1558 "Parent must be encrypted."));
1563 /* check that the parent's key is loaded */
1564 pkeystatus = zfs_prop_get_int(pzhp, ZFS_PROP_KEYSTATUS);
1565 if (pkeystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
1566 zfs_error_aux(pzhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1567 "Parent key must be loaded."));
1573 /* check that the key is loaded */
1574 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1575 if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
1576 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1577 "Key must be loaded."));
1582 /* call the ioctl */
1583 ret = lzc_change_key(zhp->zfs_name, cmd, props, wkeydata, wkeylen);
1587 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1588 "Permission denied."));
1591 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1592 "Invalid properties for key change."));
1595 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1596 "Key is not currently loaded."));
1599 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1606 if (wkeydata != NULL)
1616 if (wkeydata != NULL)
1619 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);