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 #define MIN_PASSPHRASE_LEN 8
55 #define MAX_PASSPHRASE_LEN 512
56 #define MAX_KEY_PROMPT_ATTEMPTS 3
58 static int caught_interrupt;
60 static int get_key_material_file(libzfs_handle_t *, const char *, const char *,
61 zfs_keyformat_t, boolean_t, uint8_t **, size_t *);
63 static zfs_uri_handler_t uri_handlers[] = {
64 { "file", get_key_material_file },
69 pkcs11_get_urandom(uint8_t *buf, size_t bytes)
72 ssize_t bytes_read = 0;
74 rand = open("/dev/urandom", O_RDONLY);
79 while (bytes_read < bytes) {
80 ssize_t rc = read(rand, buf + bytes_read, bytes - bytes_read);
92 zfs_prop_parse_keylocation(libzfs_handle_t *restrict hdl, const char *str,
93 zfs_keylocation_t *restrict locp, char **restrict schemep)
95 *locp = ZFS_KEYLOCATION_NONE;
98 if (strcmp("prompt", str) == 0) {
99 *locp = ZFS_KEYLOCATION_PROMPT;
103 regmatch_t pmatch[2];
105 if (regexec(&hdl->libzfs_urire, str, ARRAY_SIZE(pmatch),
109 if (pmatch[1].rm_so == -1) {
110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
115 scheme_len = pmatch[1].rm_eo - pmatch[1].rm_so;
117 *schemep = calloc(1, scheme_len + 1);
118 if (*schemep == NULL) {
122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
127 (void) memcpy(*schemep, str + pmatch[1].rm_so, scheme_len);
128 *locp = ZFS_KEYLOCATION_URI;
132 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Invalid keylocation"));
137 hex_key_to_raw(char *hex, int hexlen, uint8_t *out)
142 for (i = 0; i < hexlen; i += 2) {
143 if (!isxdigit(hex[i]) || !isxdigit(hex[i + 1])) {
148 ret = sscanf(&hex[i], "%02x", &c);
165 catch_signal(int sig)
167 caught_interrupt = sig;
171 get_format_prompt_string(zfs_keyformat_t format)
174 case ZFS_KEYFORMAT_RAW:
176 case ZFS_KEYFORMAT_HEX:
178 case ZFS_KEYFORMAT_PASSPHRASE:
179 return ("passphrase");
181 /* shouldn't happen */
186 /* do basic validation of the key material */
188 validate_key(libzfs_handle_t *hdl, zfs_keyformat_t keyformat,
189 const char *key, size_t keylen)
192 case ZFS_KEYFORMAT_RAW:
193 /* verify the key length is correct */
194 if (keylen < WRAPPING_KEY_LEN) {
195 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
196 "Raw key too short (expected %u)."),
201 if (keylen > WRAPPING_KEY_LEN) {
202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
203 "Raw key too long (expected %u)."),
208 case ZFS_KEYFORMAT_HEX:
209 /* verify the key length is correct */
210 if (keylen < WRAPPING_KEY_LEN * 2) {
211 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
212 "Hex key too short (expected %u)."),
213 WRAPPING_KEY_LEN * 2);
217 if (keylen > WRAPPING_KEY_LEN * 2) {
218 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
219 "Hex key too long (expected %u)."),
220 WRAPPING_KEY_LEN * 2);
224 /* check for invalid hex digits */
225 for (size_t i = 0; i < WRAPPING_KEY_LEN * 2; i++) {
226 if (!isxdigit(key[i])) {
227 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
228 "Invalid hex character detected."));
233 case ZFS_KEYFORMAT_PASSPHRASE:
234 /* verify the length is within bounds */
235 if (keylen > MAX_PASSPHRASE_LEN) {
236 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
237 "Passphrase too long (max %u)."),
242 if (keylen < MIN_PASSPHRASE_LEN) {
243 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
244 "Passphrase too short (min %u)."),
250 /* can't happen, checked above */
258 libzfs_getpassphrase(zfs_keyformat_t keyformat, boolean_t is_reenter,
259 boolean_t new_key, const char *fsname,
260 char **restrict res, size_t *restrict reslen)
266 struct termios old_term, new_term;
267 struct sigaction act, osigint, osigtstp;
273 * handle SIGINT and ignore SIGSTP. This is necessary to
274 * restore the state of the terminal.
276 caught_interrupt = 0;
278 (void) sigemptyset(&act.sa_mask);
279 act.sa_handler = catch_signal;
281 (void) sigaction(SIGINT, &act, &osigint);
282 act.sa_handler = SIG_IGN;
283 (void) sigaction(SIGTSTP, &act, &osigtstp);
285 (void) printf("%s %s%s",
286 is_reenter ? "Re-enter" : "Enter",
287 new_key ? "new " : "",
288 get_format_prompt_string(keyformat));
290 (void) printf(" for '%s'", fsname);
291 (void) fputc(':', stdout);
292 (void) fflush(stdout);
294 /* disable the terminal echo for key input */
295 (void) tcgetattr(fileno(f), &old_term);
298 new_term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
300 ret = tcsetattr(fileno(f), TCSAFLUSH, &new_term);
307 bytes = getline(res, &buflen, f);
314 /* trim the ending newline if it exists */
315 if (bytes > 0 && (*res)[bytes - 1] == '\n') {
316 (*res)[bytes - 1] = '\0';
323 /* reset the terminal */
324 (void) tcsetattr(fileno(f), TCSAFLUSH, &old_term);
325 (void) sigaction(SIGINT, &osigint, NULL);
326 (void) sigaction(SIGTSTP, &osigtstp, NULL);
328 /* if we caught a signal, re-throw it now */
329 if (caught_interrupt != 0)
330 (void) kill(getpid(), caught_interrupt);
332 /* print the newline that was not echo'd */
339 get_key_interactive(libzfs_handle_t *restrict hdl, const char *fsname,
340 zfs_keyformat_t keyformat, boolean_t confirm_key, boolean_t newkey,
341 uint8_t **restrict outbuf, size_t *restrict len_out)
343 char *buf = NULL, *buf2 = NULL;
344 size_t buflen = 0, buf2len = 0;
347 ASSERT(isatty(fileno(stdin)));
349 /* raw keys cannot be entered on the terminal */
350 if (keyformat == ZFS_KEYFORMAT_RAW) {
352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
353 "Cannot enter raw keys on the terminal"));
357 /* prompt for the key */
358 if ((ret = libzfs_getpassphrase(keyformat, B_FALSE, newkey, fsname,
359 &buf, &buflen)) != 0) {
369 if ((ret = validate_key(hdl, keyformat, buf, buflen)) != 0) {
374 ret = libzfs_getpassphrase(keyformat, B_TRUE, newkey, fsname, &buf2,
380 buflen = buf2len = 0;
384 if (buflen != buf2len || strcmp(buf, buf2) != 0) {
390 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
391 "Provided keys do not match."));
397 *outbuf = (uint8_t *)buf;
403 get_key_material_raw(FILE *fd, zfs_keyformat_t keyformat,
404 uint8_t **buf, size_t *len_out)
411 /* read the key material */
412 if (keyformat != ZFS_KEYFORMAT_RAW) {
415 bytes = getline((char **)buf, &buflen, fd);
422 /* trim the ending newline if it exists */
423 if (bytes > 0 && (*buf)[bytes - 1] == '\n') {
424 (*buf)[bytes - 1] = '\0';
433 * Raw keys may have newline characters in them and so can't
434 * use getline(). Here we attempt to read 33 bytes so that we
435 * can properly check the key length (the file should only have
438 *buf = malloc((WRAPPING_KEY_LEN + 1) * sizeof (uint8_t));
444 n = fread(*buf, 1, WRAPPING_KEY_LEN + 1, fd);
445 if (n == 0 || ferror(fd)) {
446 /* size errors are handled by the calling function */
461 get_key_material_file(libzfs_handle_t *hdl, const char *uri,
462 const char *fsname, zfs_keyformat_t keyformat, boolean_t newkey,
463 uint8_t **restrict buf, size_t *restrict len_out)
471 if ((f = fopen(uri + 7, "r")) == NULL) {
474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
475 "Failed to open key material file"));
479 ret = get_key_material_raw(f, keyformat, buf, len_out);
487 * Attempts to fetch key material, no matter where it might live. The key
488 * material is allocated and returned in km_out. *can_retry_out will be set
489 * to B_TRUE if the user is providing the key material interactively, allowing
490 * for re-entry attempts.
493 get_key_material(libzfs_handle_t *hdl, boolean_t do_verify, boolean_t newkey,
494 zfs_keyformat_t keyformat, char *keylocation, const char *fsname,
495 uint8_t **km_out, size_t *kmlen_out, boolean_t *can_retry_out)
498 zfs_keylocation_t keyloc = ZFS_KEYLOCATION_NONE;
501 char *uri_scheme = NULL;
502 zfs_uri_handler_t *handler = NULL;
503 boolean_t can_retry = B_FALSE;
505 /* verify and parse the keylocation */
506 ret = zfs_prop_parse_keylocation(hdl, keylocation, &keyloc,
511 /* open the appropriate file descriptor */
513 case ZFS_KEYLOCATION_PROMPT:
514 if (isatty(fileno(stdin))) {
516 ret = get_key_interactive(hdl, fsname, keyformat,
517 do_verify, newkey, &km, &kmlen);
519 /* fetch the key material into the buffer */
520 ret = get_key_material_raw(stdin, keyformat, &km,
528 case ZFS_KEYLOCATION_URI:
531 for (handler = uri_handlers; handler->zuh_scheme != NULL;
533 if (strcmp(handler->zuh_scheme, uri_scheme) != 0)
536 if ((ret = handler->zuh_handler(hdl, keylocation,
537 fsname, keyformat, newkey, &km, &kmlen)) != 0)
543 if (ret == ENOTSUP) {
544 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
545 "URI scheme is not supported"));
552 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
553 "Invalid keylocation."));
557 if ((ret = validate_key(hdl, keyformat, (const char *)km, kmlen)) != 0)
562 if (can_retry_out != NULL)
563 *can_retry_out = can_retry;
574 if (can_retry_out != NULL)
575 *can_retry_out = can_retry;
582 derive_key(libzfs_handle_t *hdl, zfs_keyformat_t format, uint64_t iters,
583 uint8_t *key_material, size_t key_material_len, uint64_t salt,
591 key = zfs_alloc(hdl, WRAPPING_KEY_LEN);
596 case ZFS_KEYFORMAT_RAW:
597 bcopy(key_material, key, WRAPPING_KEY_LEN);
599 case ZFS_KEYFORMAT_HEX:
600 ret = hex_key_to_raw((char *)key_material,
601 WRAPPING_KEY_LEN * 2, key);
603 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
604 "Invalid hex key provided."));
608 case ZFS_KEYFORMAT_PASSPHRASE:
611 ret = PKCS5_PBKDF2_HMAC_SHA1((char *)key_material,
612 strlen((char *)key_material), ((uint8_t *)&salt),
613 sizeof (uint64_t), iters, WRAPPING_KEY_LEN, key);
616 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
617 "Failed to generate key from passphrase."));
637 encryption_feature_is_enabled(zpool_handle_t *zph)
640 uint64_t feat_refcount;
642 /* check that features can be enabled */
643 if (zpool_get_prop_int(zph, ZPOOL_PROP_VERSION, NULL)
644 < SPA_VERSION_FEATURES)
647 /* check for crypto feature */
648 features = zpool_get_features(zph);
649 if (!features || nvlist_lookup_uint64(features,
650 spa_feature_table[SPA_FEATURE_ENCRYPTION].fi_guid,
651 &feat_refcount) != 0)
658 populate_create_encryption_params_nvlists(libzfs_handle_t *hdl,
659 zfs_handle_t *zhp, boolean_t newkey, zfs_keyformat_t keyformat,
660 char *keylocation, nvlist_t *props, uint8_t **wkeydata, uint_t *wkeylen)
663 uint64_t iters = 0, salt = 0;
664 uint8_t *key_material = NULL;
665 size_t key_material_len = 0;
666 uint8_t *key_data = NULL;
667 const char *fsname = (zhp) ? zfs_get_name(zhp) : NULL;
669 /* get key material from keyformat and keylocation */
670 ret = get_key_material(hdl, B_TRUE, newkey, keyformat, keylocation,
671 fsname, &key_material, &key_material_len, NULL);
675 /* passphrase formats require a salt and pbkdf2 iters property */
676 if (keyformat == ZFS_KEYFORMAT_PASSPHRASE) {
677 /* always generate a new salt */
678 ret = pkcs11_get_urandom((uint8_t *)&salt, sizeof (uint64_t));
679 if (ret != sizeof (uint64_t)) {
680 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
681 "Failed to generate salt."));
685 ret = nvlist_add_uint64(props,
686 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), salt);
688 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
689 "Failed to add salt to properties."));
694 * If not otherwise specified, use the default number of
695 * pbkdf2 iterations. If specified, we have already checked
696 * that the given value is greater than MIN_PBKDF2_ITERATIONS
697 * during zfs_valid_proplist().
699 ret = nvlist_lookup_uint64(props,
700 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);
702 iters = DEFAULT_PBKDF2_ITERATIONS;
703 ret = nvlist_add_uint64(props,
704 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), iters);
707 } else if (ret != 0) {
708 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
709 "Failed to get pbkdf2 iterations."));
713 /* check that pbkdf2iters was not specified by the user */
714 ret = nvlist_lookup_uint64(props,
715 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);
718 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
719 "Cannot specify pbkdf2iters with a non-passphrase "
725 /* derive a key from the key material */
726 ret = derive_key(hdl, keyformat, iters, key_material, key_material_len,
733 *wkeydata = key_data;
734 *wkeylen = WRAPPING_KEY_LEN;
738 if (key_material != NULL)
740 if (key_data != NULL)
749 proplist_has_encryption_props(nvlist_t *props)
755 ret = nvlist_lookup_uint64(props,
756 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &intval);
757 if (ret == 0 && intval != ZIO_CRYPT_OFF)
760 ret = nvlist_lookup_string(props,
761 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &strval);
762 if (ret == 0 && strcmp(strval, "none") != 0)
765 ret = nvlist_lookup_uint64(props,
766 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &intval);
770 ret = nvlist_lookup_uint64(props,
771 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &intval);
779 zfs_crypto_get_encryption_root(zfs_handle_t *zhp, boolean_t *is_encroot,
783 char prop_encroot[MAXNAMELEN];
785 /* if the dataset isn't encrypted, just return */
786 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) == ZIO_CRYPT_OFF) {
787 *is_encroot = B_FALSE;
793 ret = zfs_prop_get(zhp, ZFS_PROP_ENCRYPTION_ROOT, prop_encroot,
794 sizeof (prop_encroot), NULL, NULL, 0, B_TRUE);
796 *is_encroot = B_FALSE;
802 *is_encroot = strcmp(prop_encroot, zfs_get_name(zhp)) == 0;
804 strcpy(buf, prop_encroot);
810 zfs_crypto_create(libzfs_handle_t *hdl, char *parent_name, nvlist_t *props,
811 nvlist_t *pool_props, boolean_t stdin_available, uint8_t **wkeydata_out,
816 uint64_t crypt = ZIO_CRYPT_INHERIT, pcrypt = ZIO_CRYPT_INHERIT;
817 uint64_t keyformat = ZFS_KEYFORMAT_NONE;
818 char *keylocation = NULL;
819 zfs_handle_t *pzhp = NULL;
820 uint8_t *wkeydata = NULL;
822 boolean_t local_crypt = B_TRUE;
824 (void) snprintf(errbuf, sizeof (errbuf),
825 dgettext(TEXT_DOMAIN, "Encryption create error"));
827 /* lookup crypt from props */
828 ret = nvlist_lookup_uint64(props,
829 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &crypt);
831 local_crypt = B_FALSE;
833 /* lookup key location and format from props */
834 (void) nvlist_lookup_uint64(props,
835 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);
836 (void) nvlist_lookup_string(props,
837 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
839 if (parent_name != NULL) {
840 /* get a reference to parent dataset */
841 pzhp = make_dataset_handle(hdl, parent_name);
844 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
845 "Failed to lookup parent."));
849 /* Lookup parent's crypt */
850 pcrypt = zfs_prop_get_int(pzhp, ZFS_PROP_ENCRYPTION);
852 /* Params require the encryption feature */
853 if (!encryption_feature_is_enabled(pzhp->zpool_hdl)) {
854 if (proplist_has_encryption_props(props)) {
856 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
857 "Encryption feature not enabled."));
866 * special case for root dataset where encryption feature
867 * feature won't be on disk yet
869 if (!nvlist_exists(pool_props, "feature@encryption")) {
870 if (proplist_has_encryption_props(props)) {
872 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
873 "Encryption feature not enabled."));
881 pcrypt = ZIO_CRYPT_OFF;
884 /* Get the inherited encryption property if we don't have it locally */
889 * At this point crypt should be the actual encryption value. If
890 * encryption is off just verify that no encryption properties have
891 * been specified and return.
893 if (crypt == ZIO_CRYPT_OFF) {
894 if (proplist_has_encryption_props(props)) {
896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
897 "Encryption must be turned on to set encryption "
907 * If we have a parent crypt it is valid to specify encryption alone.
908 * This will result in a child that is encrypted with the chosen
909 * encryption suite that will also inherit the parent's key. If
910 * the parent is not encrypted we need an encryption suite provided.
912 if (pcrypt == ZIO_CRYPT_OFF && keylocation == NULL &&
913 keyformat == ZFS_KEYFORMAT_NONE) {
915 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
916 "Keyformat required for new encryption root."));
921 * Specifying a keylocation implies this will be a new encryption root.
922 * Check that a keyformat is also specified.
924 if (keylocation != NULL && keyformat == ZFS_KEYFORMAT_NONE) {
926 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
927 "Keyformat required for new encryption root."));
931 /* default to prompt if no keylocation is specified */
932 if (keyformat != ZFS_KEYFORMAT_NONE && keylocation == NULL) {
933 keylocation = "prompt";
934 ret = nvlist_add_string(props,
935 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), keylocation);
941 * If a local key is provided, this dataset will be a new
942 * encryption root. Populate the encryption params.
944 if (keylocation != NULL) {
946 * 'zfs recv -o keylocation=prompt' won't work because stdin
947 * is being used by the send stream, so we disallow it.
949 if (!stdin_available && strcmp(keylocation, "prompt") == 0) {
951 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Cannot use "
952 "'prompt' keylocation because stdin is in use."));
956 ret = populate_create_encryption_params_nvlists(hdl, NULL,
957 B_FALSE, keyformat, keylocation, props, &wkeydata,
966 *wkeydata_out = wkeydata;
967 *wkeylen_out = wkeylen;
973 if (wkeydata != NULL)
976 *wkeydata_out = NULL;
982 zfs_crypto_clone_check(libzfs_handle_t *hdl, zfs_handle_t *origin_zhp,
983 char *parent_name, nvlist_t *props)
987 (void) snprintf(errbuf, sizeof (errbuf),
988 dgettext(TEXT_DOMAIN, "Encryption clone error"));
991 * No encryption properties should be specified. They will all be
992 * inherited from the origin dataset.
994 if (nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_KEYFORMAT)) ||
995 nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_KEYLOCATION)) ||
996 nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_ENCRYPTION)) ||
997 nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS))) {
998 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
999 "Encryption properties must inherit from origin dataset."));
1006 typedef struct loadkeys_cbdata {
1007 uint64_t cb_numfailed;
1008 uint64_t cb_numattempted;
1012 load_keys_cb(zfs_handle_t *zhp, void *arg)
1015 boolean_t is_encroot;
1016 loadkey_cbdata_t *cb = arg;
1017 uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1019 /* only attempt to load keys for encryption roots */
1020 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
1021 if (ret != 0 || !is_encroot)
1024 /* don't attempt to load already loaded keys */
1025 if (keystatus == ZFS_KEYSTATUS_AVAILABLE)
1028 /* Attempt to load the key. Record status in cb. */
1029 cb->cb_numattempted++;
1031 ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
1036 (void) zfs_iter_filesystems(zhp, load_keys_cb, cb);
1039 /* always return 0, since this function is best effort */
1044 * This function is best effort. It attempts to load all the keys for the given
1045 * filesystem and all of its children.
1048 zfs_crypto_attempt_load_keys(libzfs_handle_t *hdl, char *fsname)
1051 zfs_handle_t *zhp = NULL;
1052 loadkey_cbdata_t cb = { 0 };
1054 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1060 ret = load_keys_cb(zfs_handle_dup(zhp), &cb);
1064 (void) printf(gettext("%llu / %llu keys successfully loaded\n"),
1065 (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
1066 (u_longlong_t)cb.cb_numattempted);
1068 if (cb.cb_numfailed != 0) {
1083 zfs_crypto_load_key(zfs_handle_t *zhp, boolean_t noop, char *alt_keylocation)
1085 int ret, attempts = 0;
1087 uint64_t keystatus, iters = 0, salt = 0;
1088 uint64_t keyformat = ZFS_KEYFORMAT_NONE;
1089 char prop_keylocation[MAXNAMELEN];
1090 char prop_encroot[MAXNAMELEN];
1091 char *keylocation = NULL;
1092 uint8_t *key_material = NULL, *key_data = NULL;
1093 size_t key_material_len;
1094 boolean_t is_encroot, can_retry = B_FALSE, correctible = B_FALSE;
1096 (void) snprintf(errbuf, sizeof (errbuf),
1097 dgettext(TEXT_DOMAIN, "Key load error"));
1099 /* check that encryption is enabled for the pool */
1100 if (!encryption_feature_is_enabled(zhp->zpool_hdl)) {
1101 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1102 "Encryption feature not enabled."));
1107 /* Fetch the keyformat. Check that the dataset is encrypted. */
1108 keyformat = zfs_prop_get_int(zhp, ZFS_PROP_KEYFORMAT);
1109 if (keyformat == ZFS_KEYFORMAT_NONE) {
1110 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1111 "'%s' is not encrypted."), zfs_get_name(zhp));
1117 * Fetch the key location. Check that we are working with an
1120 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, prop_encroot);
1122 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1123 "Failed to get encryption root for '%s'."),
1126 } else if (!is_encroot) {
1127 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1128 "Keys must be loaded for encryption root of '%s' (%s)."),
1129 zfs_get_name(zhp), prop_encroot);
1135 * if the caller has elected to override the keylocation property
1138 if (alt_keylocation != NULL) {
1139 keylocation = alt_keylocation;
1141 ret = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION, prop_keylocation,
1142 sizeof (prop_keylocation), NULL, NULL, 0, B_TRUE);
1144 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1145 "Failed to get keylocation for '%s'."),
1150 keylocation = prop_keylocation;
1153 /* check that the key is unloaded unless this is a noop */
1155 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1156 if (keystatus == ZFS_KEYSTATUS_AVAILABLE) {
1157 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1158 "Key already loaded for '%s'."), zfs_get_name(zhp));
1164 /* passphrase formats require a salt and pbkdf2_iters property */
1165 if (keyformat == ZFS_KEYFORMAT_PASSPHRASE) {
1166 salt = zfs_prop_get_int(zhp, ZFS_PROP_PBKDF2_SALT);
1167 iters = zfs_prop_get_int(zhp, ZFS_PROP_PBKDF2_ITERS);
1171 /* fetching and deriving the key are correctable errors. set the flag */
1172 correctible = B_TRUE;
1174 /* get key material from key format and location */
1175 ret = get_key_material(zhp->zfs_hdl, B_FALSE, B_FALSE, keyformat,
1176 keylocation, zfs_get_name(zhp), &key_material, &key_material_len,
1181 /* derive a key from the key material */
1182 ret = derive_key(zhp->zfs_hdl, keyformat, iters, key_material,
1183 key_material_len, salt, &key_data);
1187 correctible = B_FALSE;
1189 /* pass the wrapping key and noop flag to the ioctl */
1190 ret = lzc_load_key(zhp->zfs_name, noop, key_data, WRAPPING_KEY_LEN);
1194 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1195 "Permission denied."));
1198 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1199 "Invalid parameters provided for dataset %s."),
1203 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1204 "Key already loaded for '%s'."), zfs_get_name(zhp));
1207 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1208 "'%s' is busy."), zfs_get_name(zhp));
1211 correctible = B_TRUE;
1212 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1213 "Incorrect key provided for '%s'."),
1226 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1227 if (key_material != NULL) {
1229 key_material = NULL;
1231 if (key_data != NULL) {
1237 * Here we decide if it is ok to allow the user to retry entering their
1238 * key. The can_retry flag will be set if the user is entering their
1239 * key from an interactive prompt. The correctable flag will only be
1240 * set if an error that occurred could be corrected by retrying. Both
1241 * flags are needed to allow the user to attempt key entry again
1244 if (can_retry && correctible && attempts < MAX_KEY_PROMPT_ATTEMPTS)
1251 zfs_crypto_unload_key(zfs_handle_t *zhp)
1255 char prop_encroot[MAXNAMELEN];
1256 uint64_t keystatus, keyformat;
1257 boolean_t is_encroot;
1259 (void) snprintf(errbuf, sizeof (errbuf),
1260 dgettext(TEXT_DOMAIN, "Key unload error"));
1262 /* check that encryption is enabled for the pool */
1263 if (!encryption_feature_is_enabled(zhp->zpool_hdl)) {
1264 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1265 "Encryption feature not enabled."));
1270 /* Fetch the keyformat. Check that the dataset is encrypted. */
1271 keyformat = zfs_prop_get_int(zhp, ZFS_PROP_KEYFORMAT);
1272 if (keyformat == ZFS_KEYFORMAT_NONE) {
1273 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1274 "'%s' is not encrypted."), zfs_get_name(zhp));
1280 * Fetch the key location. Check that we are working with an
1283 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, prop_encroot);
1285 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1286 "Failed to get encryption root for '%s'."),
1289 } else if (!is_encroot) {
1290 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1291 "Keys must be unloaded for encryption root of '%s' (%s)."),
1292 zfs_get_name(zhp), prop_encroot);
1297 /* check that the key is loaded */
1298 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1299 if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
1300 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1301 "Key already unloaded for '%s'."), zfs_get_name(zhp));
1306 /* call the ioctl */
1307 ret = lzc_unload_key(zhp->zfs_name);
1312 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1313 "Permission denied."));
1316 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1317 "Key already unloaded for '%s'."),
1321 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1322 "'%s' is busy."), zfs_get_name(zhp));
1325 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1331 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1336 zfs_crypto_verify_rewrap_nvlist(zfs_handle_t *zhp, nvlist_t *props,
1337 nvlist_t **props_out, char *errbuf)
1340 nvpair_t *elem = NULL;
1342 nvlist_t *new_props = NULL;
1344 new_props = fnvlist_alloc();
1347 * loop through all provided properties, we should only have
1348 * keyformat, keylocation and pbkdf2iters. The actual validation of
1349 * values is done by zfs_valid_proplist().
1351 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
1352 const char *propname = nvpair_name(elem);
1353 prop = zfs_name_to_prop(propname);
1356 case ZFS_PROP_PBKDF2_ITERS:
1357 case ZFS_PROP_KEYFORMAT:
1358 case ZFS_PROP_KEYLOCATION:
1362 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1363 "Only keyformat, keylocation and pbkdf2iters may "
1364 "be set with this command."));
1369 new_props = zfs_valid_proplist(zhp->zfs_hdl, zhp->zfs_type, props,
1370 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), NULL, zhp->zpool_hdl,
1372 if (new_props == NULL) {
1377 *props_out = new_props;
1381 nvlist_free(new_props);
1387 zfs_crypto_rewrap(zfs_handle_t *zhp, nvlist_t *raw_props, boolean_t inheritkey)
1391 boolean_t is_encroot;
1392 nvlist_t *props = NULL;
1393 uint8_t *wkeydata = NULL;
1395 dcp_cmd_t cmd = (inheritkey) ? DCP_CMD_INHERIT : DCP_CMD_NEW_KEY;
1396 uint64_t crypt, pcrypt, keystatus, pkeystatus;
1397 uint64_t keyformat = ZFS_KEYFORMAT_NONE;
1398 zfs_handle_t *pzhp = NULL;
1399 char *keylocation = NULL;
1400 char origin_name[MAXNAMELEN];
1401 char prop_keylocation[MAXNAMELEN];
1402 char parent_name[ZFS_MAX_DATASET_NAME_LEN];
1404 (void) snprintf(errbuf, sizeof (errbuf),
1405 dgettext(TEXT_DOMAIN, "Key change error"));
1407 /* check that encryption is enabled for the pool */
1408 if (!encryption_feature_is_enabled(zhp->zpool_hdl)) {
1409 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1410 "Encryption feature not enabled."));
1415 /* get crypt from dataset */
1416 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1417 if (crypt == ZIO_CRYPT_OFF) {
1418 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1419 "Dataset not encrypted."));
1424 /* get the encryption root of the dataset */
1425 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
1427 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1428 "Failed to get encryption root for '%s'."),
1433 /* Clones use their origin's key and cannot rewrap it */
1434 ret = zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin_name,
1435 sizeof (origin_name), NULL, NULL, 0, B_TRUE);
1436 if (ret == 0 && strcmp(origin_name, "") != 0) {
1437 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1438 "Keys cannot be changed on clones."));
1444 * If the user wants to use the inheritkey variant of this function
1445 * we don't need to collect any crypto arguments.
1448 /* validate the provided properties */
1449 ret = zfs_crypto_verify_rewrap_nvlist(zhp, raw_props, &props,
1455 * Load keyformat and keylocation from the nvlist. Fetch from
1456 * the dataset properties if not specified.
1458 (void) nvlist_lookup_uint64(props,
1459 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);
1460 (void) nvlist_lookup_string(props,
1461 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
1465 * If this is already an encryption root, just keep
1466 * any properties not set by the user.
1468 if (keyformat == ZFS_KEYFORMAT_NONE) {
1469 keyformat = zfs_prop_get_int(zhp,
1470 ZFS_PROP_KEYFORMAT);
1471 ret = nvlist_add_uint64(props,
1472 zfs_prop_to_name(ZFS_PROP_KEYFORMAT),
1475 zfs_error_aux(zhp->zfs_hdl,
1476 dgettext(TEXT_DOMAIN, "Failed to "
1477 "get existing keyformat "
1483 if (keylocation == NULL) {
1484 ret = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
1485 prop_keylocation, sizeof (prop_keylocation),
1486 NULL, NULL, 0, B_TRUE);
1488 zfs_error_aux(zhp->zfs_hdl,
1489 dgettext(TEXT_DOMAIN, "Failed to "
1490 "get existing keylocation "
1495 keylocation = prop_keylocation;
1498 /* need a new key for non-encryption roots */
1499 if (keyformat == ZFS_KEYFORMAT_NONE) {
1501 zfs_error_aux(zhp->zfs_hdl,
1502 dgettext(TEXT_DOMAIN, "Keyformat required "
1503 "for new encryption root."));
1507 /* default to prompt if no keylocation is specified */
1508 if (keylocation == NULL) {
1509 keylocation = "prompt";
1510 ret = nvlist_add_string(props,
1511 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1518 /* fetch the new wrapping key and associated properties */
1519 ret = populate_create_encryption_params_nvlists(zhp->zfs_hdl,
1520 zhp, B_TRUE, keyformat, keylocation, props, &wkeydata,
1525 /* check that zhp is an encryption root */
1527 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1528 "Key inheritting can only be performed on "
1529 "encryption roots."));
1534 /* get the parent's name */
1535 ret = zfs_parent_name(zhp, parent_name, sizeof (parent_name));
1537 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1538 "Root dataset cannot inherit key."));
1543 /* get a handle to the parent */
1544 pzhp = make_dataset_handle(zhp->zfs_hdl, parent_name);
1546 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1547 "Failed to lookup parent."));
1552 /* parent must be encrypted */
1553 pcrypt = zfs_prop_get_int(pzhp, ZFS_PROP_ENCRYPTION);
1554 if (pcrypt == ZIO_CRYPT_OFF) {
1555 zfs_error_aux(pzhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1556 "Parent must be encrypted."));
1561 /* check that the parent's key is loaded */
1562 pkeystatus = zfs_prop_get_int(pzhp, ZFS_PROP_KEYSTATUS);
1563 if (pkeystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
1564 zfs_error_aux(pzhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1565 "Parent key must be loaded."));
1571 /* check that the key is loaded */
1572 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1573 if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
1574 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1575 "Key must be loaded."));
1580 /* call the ioctl */
1581 ret = lzc_change_key(zhp->zfs_name, cmd, props, wkeydata, wkeylen);
1585 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1586 "Permission denied."));
1589 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1590 "Invalid properties for key change."));
1593 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1594 "Key is not currently loaded."));
1597 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1604 if (wkeydata != NULL)
1614 if (wkeydata != NULL)
1617 zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);