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>
33 #include <sys/param.h>
36 #elif LIBFETCH_IS_LIBCURL
37 #include <curl/curl.h>
41 #include "libzfs_impl.h"
42 #include "zfeature_common.h"
45 * User keys are used to decrypt the master encryption keys of a dataset. This
46 * indirection allows a user to change his / her access key without having to
47 * re-encrypt the entire dataset. User keys can be provided in one of several
48 * ways. Raw keys are simply given to the kernel as is. Similarly, hex keys
49 * are converted to binary and passed into the kernel. Password based keys are
50 * a bit more complicated. Passwords alone do not provide suitable entropy for
51 * encryption and may be too short or too long to be used. In order to derive
52 * a more appropriate key we use a PBKDF2 function. This function is designed
53 * to take a (relatively) long time to calculate in order to discourage
54 * attackers from guessing from a list of common passwords. PBKDF2 requires
55 * 2 additional parameters. The first is the number of iterations to run, which
56 * will ultimately determine how long it takes to derive the resulting key from
57 * the password. The second parameter is a salt that is randomly generated for
58 * each dataset. The salt is used to "tweak" PBKDF2 such that a group of
59 * attackers cannot reasonably generate a table of commonly known passwords to
60 * their output keys and expect it work for all past and future PBKDF2 users.
61 * We store the salt as a hidden property of the dataset (although it is
62 * technically ok if the salt is known to the attacker).
65 #define MIN_PASSPHRASE_LEN 8
66 #define MAX_PASSPHRASE_LEN 512
67 #define MAX_KEY_PROMPT_ATTEMPTS 3
69 static int caught_interrupt
;
71 static int get_key_material_file(libzfs_handle_t
*, const char *, const char *,
72 zfs_keyformat_t
, boolean_t
, uint8_t **, size_t *);
73 static int get_key_material_https(libzfs_handle_t
*, const char *, const char *,
74 zfs_keyformat_t
, boolean_t
, uint8_t **, size_t *);
76 static zfs_uri_handler_t uri_handlers
[] = {
77 { "file", get_key_material_file
},
78 { "https", get_key_material_https
},
79 { "http", get_key_material_https
},
84 pkcs11_get_urandom(uint8_t *buf
, size_t bytes
)
87 ssize_t bytes_read
= 0;
89 rand
= open("/dev/urandom", O_RDONLY
| O_CLOEXEC
);
94 while (bytes_read
< bytes
) {
95 ssize_t rc
= read(rand
, buf
+ bytes_read
, bytes
- bytes_read
);
107 zfs_prop_parse_keylocation(libzfs_handle_t
*restrict hdl
, const char *str
,
108 zfs_keylocation_t
*restrict locp
, char **restrict schemep
)
110 *locp
= ZFS_KEYLOCATION_NONE
;
113 if (strcmp("prompt", str
) == 0) {
114 *locp
= ZFS_KEYLOCATION_PROMPT
;
118 regmatch_t pmatch
[2];
120 if (regexec(&hdl
->libzfs_urire
, str
, ARRAY_SIZE(pmatch
),
124 if (pmatch
[1].rm_so
== -1) {
125 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
130 scheme_len
= pmatch
[1].rm_eo
- pmatch
[1].rm_so
;
132 *schemep
= calloc(1, scheme_len
+ 1);
133 if (*schemep
== NULL
) {
137 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
142 (void) memcpy(*schemep
, str
+ pmatch
[1].rm_so
, scheme_len
);
143 *locp
= ZFS_KEYLOCATION_URI
;
147 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Invalid keylocation"));
152 hex_key_to_raw(char *hex
, int hexlen
, uint8_t *out
)
157 for (i
= 0; i
< hexlen
; i
+= 2) {
158 if (!isxdigit(hex
[i
]) || !isxdigit(hex
[i
+ 1])) {
163 ret
= sscanf(&hex
[i
], "%02x", &c
);
180 catch_signal(int sig
)
182 caught_interrupt
= sig
;
186 get_format_prompt_string(zfs_keyformat_t format
)
189 case ZFS_KEYFORMAT_RAW
:
191 case ZFS_KEYFORMAT_HEX
:
193 case ZFS_KEYFORMAT_PASSPHRASE
:
194 return ("passphrase");
196 /* shouldn't happen */
201 /* do basic validation of the key material */
203 validate_key(libzfs_handle_t
*hdl
, zfs_keyformat_t keyformat
,
204 const char *key
, size_t keylen
, boolean_t do_verify
)
207 case ZFS_KEYFORMAT_RAW
:
208 /* verify the key length is correct */
209 if (keylen
< WRAPPING_KEY_LEN
) {
210 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
211 "Raw key too short (expected %u)."),
216 if (keylen
> WRAPPING_KEY_LEN
) {
217 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
218 "Raw key too long (expected %u)."),
223 case ZFS_KEYFORMAT_HEX
:
224 /* verify the key length is correct */
225 if (keylen
< WRAPPING_KEY_LEN
* 2) {
226 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
227 "Hex key too short (expected %u)."),
228 WRAPPING_KEY_LEN
* 2);
232 if (keylen
> WRAPPING_KEY_LEN
* 2) {
233 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
234 "Hex key too long (expected %u)."),
235 WRAPPING_KEY_LEN
* 2);
239 /* check for invalid hex digits */
240 for (size_t i
= 0; i
< WRAPPING_KEY_LEN
* 2; i
++) {
241 if (!isxdigit(key
[i
])) {
242 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
243 "Invalid hex character detected."));
248 case ZFS_KEYFORMAT_PASSPHRASE
:
250 * Verify the length is within bounds when setting a new key,
251 * but not when loading an existing key.
255 if (keylen
> MAX_PASSPHRASE_LEN
) {
256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
257 "Passphrase too long (max %u)."),
262 if (keylen
< MIN_PASSPHRASE_LEN
) {
263 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
264 "Passphrase too short (min %u)."),
270 /* can't happen, checked above */
278 libzfs_getpassphrase(zfs_keyformat_t keyformat
, boolean_t is_reenter
,
279 boolean_t new_key
, const char *fsname
,
280 char **restrict res
, size_t *restrict reslen
)
286 struct termios old_term
, new_term
;
287 struct sigaction act
, osigint
, osigtstp
;
293 * handle SIGINT and ignore SIGSTP. This is necessary to
294 * restore the state of the terminal.
296 caught_interrupt
= 0;
298 (void) sigemptyset(&act
.sa_mask
);
299 act
.sa_handler
= catch_signal
;
301 (void) sigaction(SIGINT
, &act
, &osigint
);
302 act
.sa_handler
= SIG_IGN
;
303 (void) sigaction(SIGTSTP
, &act
, &osigtstp
);
305 (void) printf("%s %s%s",
306 is_reenter
? "Re-enter" : "Enter",
307 new_key
? "new " : "",
308 get_format_prompt_string(keyformat
));
310 (void) printf(" for '%s'", fsname
);
311 (void) fputc(':', stdout
);
312 (void) fflush(stdout
);
314 /* disable the terminal echo for key input */
315 (void) tcgetattr(fileno(f
), &old_term
);
318 new_term
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
320 ret
= tcsetattr(fileno(f
), TCSAFLUSH
, &new_term
);
327 bytes
= getline(res
, &buflen
, f
);
334 /* trim the ending newline if it exists */
335 if (bytes
> 0 && (*res
)[bytes
- 1] == '\n') {
336 (*res
)[bytes
- 1] = '\0';
343 /* reset the terminal */
344 (void) tcsetattr(fileno(f
), TCSAFLUSH
, &old_term
);
345 (void) sigaction(SIGINT
, &osigint
, NULL
);
346 (void) sigaction(SIGTSTP
, &osigtstp
, NULL
);
348 /* if we caught a signal, re-throw it now */
349 if (caught_interrupt
!= 0)
350 (void) kill(getpid(), caught_interrupt
);
352 /* print the newline that was not echo'd */
359 get_key_interactive(libzfs_handle_t
*restrict hdl
, const char *fsname
,
360 zfs_keyformat_t keyformat
, boolean_t confirm_key
, boolean_t newkey
,
361 uint8_t **restrict outbuf
, size_t *restrict len_out
)
363 char *buf
= NULL
, *buf2
= NULL
;
364 size_t buflen
= 0, buf2len
= 0;
367 ASSERT(isatty(fileno(stdin
)));
369 /* raw keys cannot be entered on the terminal */
370 if (keyformat
== ZFS_KEYFORMAT_RAW
) {
372 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
373 "Cannot enter raw keys on the terminal"));
377 /* prompt for the key */
378 if ((ret
= libzfs_getpassphrase(keyformat
, B_FALSE
, newkey
, fsname
,
379 &buf
, &buflen
)) != 0) {
389 if ((ret
= validate_key(hdl
, keyformat
, buf
, buflen
, confirm_key
)) !=
395 ret
= libzfs_getpassphrase(keyformat
, B_TRUE
, newkey
, fsname
, &buf2
,
401 buflen
= buf2len
= 0;
405 if (buflen
!= buf2len
|| strcmp(buf
, buf2
) != 0) {
411 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
412 "Provided keys do not match."));
418 *outbuf
= (uint8_t *)buf
;
424 get_key_material_raw(FILE *fd
, zfs_keyformat_t keyformat
,
425 uint8_t **buf
, size_t *len_out
)
432 /* read the key material */
433 if (keyformat
!= ZFS_KEYFORMAT_RAW
) {
436 bytes
= getline((char **)buf
, &buflen
, fd
);
443 /* trim the ending newline if it exists */
444 if (bytes
> 0 && (*buf
)[bytes
- 1] == '\n') {
445 (*buf
)[bytes
- 1] = '\0';
454 * Raw keys may have newline characters in them and so can't
455 * use getline(). Here we attempt to read 33 bytes so that we
456 * can properly check the key length (the file should only have
459 *buf
= malloc((WRAPPING_KEY_LEN
+ 1) * sizeof (uint8_t));
465 n
= fread(*buf
, 1, WRAPPING_KEY_LEN
+ 1, fd
);
466 if (n
== 0 || ferror(fd
)) {
467 /* size errors are handled by the calling function */
482 get_key_material_file(libzfs_handle_t
*hdl
, const char *uri
,
483 const char *fsname
, zfs_keyformat_t keyformat
, boolean_t newkey
,
484 uint8_t **restrict buf
, size_t *restrict len_out
)
486 (void) fsname
, (void) newkey
;
493 if ((f
= fopen(uri
+ 7, "re")) == NULL
) {
496 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
497 "Failed to open key material file: %s"), zfs_strerror(ret
));
501 ret
= get_key_material_raw(f
, keyformat
, buf
, len_out
);
509 get_key_material_https(libzfs_handle_t
*hdl
, const char *uri
,
510 const char *fsname
, zfs_keyformat_t keyformat
, boolean_t newkey
,
511 uint8_t **restrict buf
, size_t *restrict len_out
)
513 (void) fsname
, (void) newkey
;
516 boolean_t is_http
= strncmp(uri
, "http:", strlen("http:")) == 0;
518 if (strlen(uri
) < (is_http
? 7 : 8)) {
524 #define LOAD_FUNCTION(func) \
525 __typeof__(func) *func = dlsym(hdl->libfetch, #func);
527 if (hdl
->libfetch
== NULL
)
528 hdl
->libfetch
= dlopen(LIBFETCH_SONAME
, RTLD_LAZY
);
530 if (hdl
->libfetch
== NULL
) {
531 hdl
->libfetch
= (void *)-1;
532 char *err
= dlerror();
534 hdl
->libfetch_load_error
= strdup(err
);
537 if (hdl
->libfetch
== (void *)-1) {
539 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
540 "Couldn't load %s: %s"),
541 LIBFETCH_SONAME
, hdl
->libfetch_load_error
?: "(?)");
546 #if LIBFETCH_IS_FETCH
547 LOAD_FUNCTION(fetchGetURL
);
548 char *fetchLastErrString
= dlsym(hdl
->libfetch
, "fetchLastErrString");
550 ok
= fetchGetURL
&& fetchLastErrString
;
551 #elif LIBFETCH_IS_LIBCURL
552 LOAD_FUNCTION(curl_easy_init
);
553 LOAD_FUNCTION(curl_easy_setopt
);
554 LOAD_FUNCTION(curl_easy_perform
);
555 LOAD_FUNCTION(curl_easy_cleanup
);
556 LOAD_FUNCTION(curl_easy_strerror
);
557 LOAD_FUNCTION(curl_easy_getinfo
);
559 ok
= curl_easy_init
&& curl_easy_setopt
&& curl_easy_perform
&&
560 curl_easy_cleanup
&& curl_easy_strerror
&& curl_easy_getinfo
;
563 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
564 "keylocation=%s back-end %s missing symbols."),
565 is_http
? "http://" : "https://", LIBFETCH_SONAME
);
571 #if LIBFETCH_IS_FETCH
572 key
= fetchGetURL(uri
, "");
574 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
575 "Couldn't GET %s: %s"),
576 uri
, fetchLastErrString
);
579 #elif LIBFETCH_IS_LIBCURL
580 CURL
*curl
= curl_easy_init();
588 kfd
= open(getenv("TMPDIR") ?: "/tmp",
589 O_RDWR
| O_TMPFILE
| O_EXCL
| O_CLOEXEC
, 0600);
596 "%s/libzfs-XXXXXXXX.https", getenv("TMPDIR") ?: "/tmp") == -1) {
598 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s"),
603 kfd
= mkostemps(path
, strlen(".https"), O_CLOEXEC
);
606 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
607 "Couldn't create temporary file %s: %s"),
608 path
, zfs_strerror(ret
));
616 if ((key
= fdopen(kfd
, "r+")) == NULL
) {
619 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
620 "Couldn't reopen temporary file: %s"), zfs_strerror(ret
));
624 char errbuf
[CURL_ERROR_SIZE
] = "";
625 char *cainfo
= getenv("SSL_CA_CERT_FILE"); /* matches fetch(3) */
626 char *capath
= getenv("SSL_CA_CERT_PATH"); /* matches fetch(3) */
627 char *clcert
= getenv("SSL_CLIENT_CERT_FILE"); /* matches fetch(3) */
628 char *clkey
= getenv("SSL_CLIENT_KEY_FILE"); /* matches fetch(3) */
629 (void) curl_easy_setopt(curl
, CURLOPT_URL
, uri
);
630 (void) curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, 1L);
631 (void) curl_easy_setopt(curl
, CURLOPT_TIMEOUT_MS
, 30000L);
632 (void) curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, key
);
633 (void) curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, errbuf
);
635 (void) curl_easy_setopt(curl
, CURLOPT_CAINFO
, cainfo
);
637 (void) curl_easy_setopt(curl
, CURLOPT_CAPATH
, capath
);
639 (void) curl_easy_setopt(curl
, CURLOPT_SSLCERT
, clcert
);
641 (void) curl_easy_setopt(curl
, CURLOPT_SSLKEY
, clkey
);
643 CURLcode res
= curl_easy_perform(curl
);
645 if (res
!= CURLE_OK
) {
646 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
647 "Failed to connect to %s: %s"),
648 uri
, strlen(errbuf
) ? errbuf
: curl_easy_strerror(res
));
652 (void) curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &resp
);
654 if (resp
< 200 || resp
>= 300) {
655 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
656 "Couldn't GET %s: %ld"),
663 curl_easy_cleanup(curl
);
665 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
666 "No keylocation=%s back-end."), is_http
? "http://" : "https://");
672 ret
= get_key_material_raw(key
, keyformat
, buf
, len_out
);
681 * Attempts to fetch key material, no matter where it might live. The key
682 * material is allocated and returned in km_out. *can_retry_out will be set
683 * to B_TRUE if the user is providing the key material interactively, allowing
684 * for re-entry attempts.
687 get_key_material(libzfs_handle_t
*hdl
, boolean_t do_verify
, boolean_t newkey
,
688 zfs_keyformat_t keyformat
, const char *keylocation
, const char *fsname
,
689 uint8_t **km_out
, size_t *kmlen_out
, boolean_t
*can_retry_out
)
692 zfs_keylocation_t keyloc
= ZFS_KEYLOCATION_NONE
;
695 char *uri_scheme
= NULL
;
696 zfs_uri_handler_t
*handler
= NULL
;
697 boolean_t can_retry
= B_FALSE
;
699 /* verify and parse the keylocation */
700 ret
= zfs_prop_parse_keylocation(hdl
, keylocation
, &keyloc
,
705 /* open the appropriate file descriptor */
707 case ZFS_KEYLOCATION_PROMPT
:
708 if (isatty(fileno(stdin
))) {
709 can_retry
= keyformat
!= ZFS_KEYFORMAT_RAW
;
710 ret
= get_key_interactive(hdl
, fsname
, keyformat
,
711 do_verify
, newkey
, &km
, &kmlen
);
713 /* fetch the key material into the buffer */
714 ret
= get_key_material_raw(stdin
, keyformat
, &km
,
722 case ZFS_KEYLOCATION_URI
:
725 for (handler
= uri_handlers
; handler
->zuh_scheme
!= NULL
;
727 if (strcmp(handler
->zuh_scheme
, uri_scheme
) != 0)
730 if ((ret
= handler
->zuh_handler(hdl
, keylocation
,
731 fsname
, keyformat
, newkey
, &km
, &kmlen
)) != 0)
737 if (ret
== ENOTSUP
) {
738 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
739 "URI scheme is not supported"));
746 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
747 "Invalid keylocation."));
751 if ((ret
= validate_key(hdl
, keyformat
, (const char *)km
, kmlen
,
757 if (can_retry_out
!= NULL
)
758 *can_retry_out
= can_retry
;
769 if (can_retry_out
!= NULL
)
770 *can_retry_out
= can_retry
;
777 derive_key(libzfs_handle_t
*hdl
, zfs_keyformat_t format
, uint64_t iters
,
778 uint8_t *key_material
, uint64_t salt
,
786 key
= zfs_alloc(hdl
, WRAPPING_KEY_LEN
);
789 case ZFS_KEYFORMAT_RAW
:
790 memcpy(key
, key_material
, WRAPPING_KEY_LEN
);
792 case ZFS_KEYFORMAT_HEX
:
793 ret
= hex_key_to_raw((char *)key_material
,
794 WRAPPING_KEY_LEN
* 2, key
);
796 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
797 "Invalid hex key provided."));
801 case ZFS_KEYFORMAT_PASSPHRASE
:
804 ret
= PKCS5_PBKDF2_HMAC_SHA1((char *)key_material
,
805 strlen((char *)key_material
), ((uint8_t *)&salt
),
806 sizeof (uint64_t), iters
, WRAPPING_KEY_LEN
, key
);
809 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
810 "Failed to generate key from passphrase."));
830 encryption_feature_is_enabled(zpool_handle_t
*zph
)
833 uint64_t feat_refcount
;
835 /* check that features can be enabled */
836 if (zpool_get_prop_int(zph
, ZPOOL_PROP_VERSION
, NULL
)
837 < SPA_VERSION_FEATURES
)
840 /* check for crypto feature */
841 features
= zpool_get_features(zph
);
842 if (!features
|| nvlist_lookup_uint64(features
,
843 spa_feature_table
[SPA_FEATURE_ENCRYPTION
].fi_guid
,
844 &feat_refcount
) != 0)
851 populate_create_encryption_params_nvlists(libzfs_handle_t
*hdl
,
852 zfs_handle_t
*zhp
, boolean_t newkey
, zfs_keyformat_t keyformat
,
853 const char *keylocation
, nvlist_t
*props
, uint8_t **wkeydata
,
857 uint64_t iters
= 0, salt
= 0;
858 uint8_t *key_material
= NULL
;
859 size_t key_material_len
= 0;
860 uint8_t *key_data
= NULL
;
861 const char *fsname
= (zhp
) ? zfs_get_name(zhp
) : NULL
;
863 /* get key material from keyformat and keylocation */
864 ret
= get_key_material(hdl
, B_TRUE
, newkey
, keyformat
, keylocation
,
865 fsname
, &key_material
, &key_material_len
, NULL
);
869 /* passphrase formats require a salt and pbkdf2 iters property */
870 if (keyformat
== ZFS_KEYFORMAT_PASSPHRASE
) {
871 /* always generate a new salt */
872 ret
= pkcs11_get_urandom((uint8_t *)&salt
, sizeof (uint64_t));
873 if (ret
!= sizeof (uint64_t)) {
874 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
875 "Failed to generate salt."));
879 ret
= nvlist_add_uint64(props
,
880 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), salt
);
882 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
883 "Failed to add salt to properties."));
888 * If not otherwise specified, use the default number of
889 * pbkdf2 iterations. If specified, we have already checked
890 * that the given value is greater than MIN_PBKDF2_ITERATIONS
891 * during zfs_valid_proplist().
893 ret
= nvlist_lookup_uint64(props
,
894 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), &iters
);
896 iters
= DEFAULT_PBKDF2_ITERATIONS
;
897 ret
= nvlist_add_uint64(props
,
898 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), iters
);
901 } else if (ret
!= 0) {
902 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
903 "Failed to get pbkdf2 iterations."));
907 /* check that pbkdf2iters was not specified by the user */
908 ret
= nvlist_lookup_uint64(props
,
909 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), &iters
);
912 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
913 "Cannot specify pbkdf2iters with a non-passphrase "
919 /* derive a key from the key material */
920 ret
= derive_key(hdl
, keyformat
, iters
, key_material
, salt
, &key_data
);
926 *wkeydata
= key_data
;
927 *wkeylen
= WRAPPING_KEY_LEN
;
931 if (key_material
!= NULL
)
933 if (key_data
!= NULL
)
942 proplist_has_encryption_props(nvlist_t
*props
)
948 ret
= nvlist_lookup_uint64(props
,
949 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), &intval
);
950 if (ret
== 0 && intval
!= ZIO_CRYPT_OFF
)
953 ret
= nvlist_lookup_string(props
,
954 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), &strval
);
955 if (ret
== 0 && strcmp(strval
, "none") != 0)
958 ret
= nvlist_lookup_uint64(props
,
959 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), &intval
);
963 ret
= nvlist_lookup_uint64(props
,
964 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), &intval
);
972 zfs_crypto_get_encryption_root(zfs_handle_t
*zhp
, boolean_t
*is_encroot
,
976 char prop_encroot
[MAXNAMELEN
];
978 /* if the dataset isn't encrypted, just return */
979 if (zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) == ZIO_CRYPT_OFF
) {
980 *is_encroot
= B_FALSE
;
986 ret
= zfs_prop_get(zhp
, ZFS_PROP_ENCRYPTION_ROOT
, prop_encroot
,
987 sizeof (prop_encroot
), NULL
, NULL
, 0, B_TRUE
);
989 *is_encroot
= B_FALSE
;
995 *is_encroot
= strcmp(prop_encroot
, zfs_get_name(zhp
)) == 0;
997 strcpy(buf
, prop_encroot
);
1003 zfs_crypto_create(libzfs_handle_t
*hdl
, char *parent_name
, nvlist_t
*props
,
1004 nvlist_t
*pool_props
, boolean_t stdin_available
, uint8_t **wkeydata_out
,
1005 uint_t
*wkeylen_out
)
1008 char errbuf
[ERRBUFLEN
];
1009 uint64_t crypt
= ZIO_CRYPT_INHERIT
, pcrypt
= ZIO_CRYPT_INHERIT
;
1010 uint64_t keyformat
= ZFS_KEYFORMAT_NONE
;
1011 const char *keylocation
= NULL
;
1012 zfs_handle_t
*pzhp
= NULL
;
1013 uint8_t *wkeydata
= NULL
;
1015 boolean_t local_crypt
= B_TRUE
;
1017 (void) snprintf(errbuf
, sizeof (errbuf
),
1018 dgettext(TEXT_DOMAIN
, "Encryption create error"));
1020 /* lookup crypt from props */
1021 ret
= nvlist_lookup_uint64(props
,
1022 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), &crypt
);
1024 local_crypt
= B_FALSE
;
1026 /* lookup key location and format from props */
1027 (void) nvlist_lookup_uint64(props
,
1028 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), &keyformat
);
1029 (void) nvlist_lookup_string(props
,
1030 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), &keylocation
);
1032 if (parent_name
!= NULL
) {
1033 /* get a reference to parent dataset */
1034 pzhp
= make_dataset_handle(hdl
, parent_name
);
1037 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1038 "Failed to lookup parent."));
1042 /* Lookup parent's crypt */
1043 pcrypt
= zfs_prop_get_int(pzhp
, ZFS_PROP_ENCRYPTION
);
1045 /* Params require the encryption feature */
1046 if (!encryption_feature_is_enabled(pzhp
->zpool_hdl
)) {
1047 if (proplist_has_encryption_props(props
)) {
1049 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1050 "Encryption feature not enabled."));
1059 * special case for root dataset where encryption feature
1060 * feature won't be on disk yet
1062 if (!nvlist_exists(pool_props
, "feature@encryption")) {
1063 if (proplist_has_encryption_props(props
)) {
1065 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1066 "Encryption feature not enabled."));
1074 pcrypt
= ZIO_CRYPT_OFF
;
1077 /* Get the inherited encryption property if we don't have it locally */
1082 * At this point crypt should be the actual encryption value. If
1083 * encryption is off just verify that no encryption properties have
1084 * been specified and return.
1086 if (crypt
== ZIO_CRYPT_OFF
) {
1087 if (proplist_has_encryption_props(props
)) {
1089 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1090 "Encryption must be turned on to set encryption "
1100 * If we have a parent crypt it is valid to specify encryption alone.
1101 * This will result in a child that is encrypted with the chosen
1102 * encryption suite that will also inherit the parent's key. If
1103 * the parent is not encrypted we need an encryption suite provided.
1105 if (pcrypt
== ZIO_CRYPT_OFF
&& keylocation
== NULL
&&
1106 keyformat
== ZFS_KEYFORMAT_NONE
) {
1108 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1109 "Keyformat required for new encryption root."));
1114 * Specifying a keylocation implies this will be a new encryption root.
1115 * Check that a keyformat is also specified.
1117 if (keylocation
!= NULL
&& keyformat
== ZFS_KEYFORMAT_NONE
) {
1119 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1120 "Keyformat required for new encryption root."));
1124 /* default to prompt if no keylocation is specified */
1125 if (keyformat
!= ZFS_KEYFORMAT_NONE
&& keylocation
== NULL
) {
1126 keylocation
= (char *)"prompt";
1127 ret
= nvlist_add_string(props
,
1128 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), keylocation
);
1134 * If a local key is provided, this dataset will be a new
1135 * encryption root. Populate the encryption params.
1137 if (keylocation
!= NULL
) {
1139 * 'zfs recv -o keylocation=prompt' won't work because stdin
1140 * is being used by the send stream, so we disallow it.
1142 if (!stdin_available
&& strcmp(keylocation
, "prompt") == 0) {
1144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Cannot use "
1145 "'prompt' keylocation because stdin is in use."));
1149 ret
= populate_create_encryption_params_nvlists(hdl
, NULL
,
1150 B_TRUE
, keyformat
, keylocation
, props
, &wkeydata
,
1159 *wkeydata_out
= wkeydata
;
1160 *wkeylen_out
= wkeylen
;
1166 if (wkeydata
!= NULL
)
1169 *wkeydata_out
= NULL
;
1175 zfs_crypto_clone_check(libzfs_handle_t
*hdl
, zfs_handle_t
*origin_zhp
,
1176 char *parent_name
, nvlist_t
*props
)
1178 (void) origin_zhp
, (void) parent_name
;
1179 char errbuf
[ERRBUFLEN
];
1181 (void) snprintf(errbuf
, sizeof (errbuf
),
1182 dgettext(TEXT_DOMAIN
, "Encryption clone error"));
1185 * No encryption properties should be specified. They will all be
1186 * inherited from the origin dataset.
1188 if (nvlist_exists(props
, zfs_prop_to_name(ZFS_PROP_KEYFORMAT
)) ||
1189 nvlist_exists(props
, zfs_prop_to_name(ZFS_PROP_KEYLOCATION
)) ||
1190 nvlist_exists(props
, zfs_prop_to_name(ZFS_PROP_ENCRYPTION
)) ||
1191 nvlist_exists(props
, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
))) {
1192 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1193 "Encryption properties must inherit from origin dataset."));
1200 typedef struct loadkeys_cbdata
{
1201 uint64_t cb_numfailed
;
1202 uint64_t cb_numattempted
;
1206 load_keys_cb(zfs_handle_t
*zhp
, void *arg
)
1209 boolean_t is_encroot
;
1210 loadkey_cbdata_t
*cb
= arg
;
1211 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
1213 /* only attempt to load keys for encryption roots */
1214 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
1215 if (ret
!= 0 || !is_encroot
)
1218 /* don't attempt to load already loaded keys */
1219 if (keystatus
== ZFS_KEYSTATUS_AVAILABLE
)
1222 /* Attempt to load the key. Record status in cb. */
1223 cb
->cb_numattempted
++;
1225 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
1230 (void) zfs_iter_filesystems_v2(zhp
, 0, load_keys_cb
, cb
);
1233 /* always return 0, since this function is best effort */
1238 * This function is best effort. It attempts to load all the keys for the given
1239 * filesystem and all of its children.
1242 zfs_crypto_attempt_load_keys(libzfs_handle_t
*hdl
, const char *fsname
)
1245 zfs_handle_t
*zhp
= NULL
;
1246 loadkey_cbdata_t cb
= { 0 };
1248 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1254 ret
= load_keys_cb(zfs_handle_dup(zhp
), &cb
);
1258 (void) printf(gettext("%llu / %llu keys successfully loaded\n"),
1259 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
1260 (u_longlong_t
)cb
.cb_numattempted
);
1262 if (cb
.cb_numfailed
!= 0) {
1277 zfs_crypto_load_key(zfs_handle_t
*zhp
, boolean_t noop
,
1278 const char *alt_keylocation
)
1280 int ret
, attempts
= 0;
1281 char errbuf
[ERRBUFLEN
];
1282 uint64_t keystatus
, iters
= 0, salt
= 0;
1283 uint64_t keyformat
= ZFS_KEYFORMAT_NONE
;
1284 char prop_keylocation
[MAXNAMELEN
];
1285 char prop_encroot
[MAXNAMELEN
];
1286 const char *keylocation
= NULL
;
1287 uint8_t *key_material
= NULL
, *key_data
= NULL
;
1288 size_t key_material_len
;
1289 boolean_t is_encroot
, can_retry
= B_FALSE
, correctible
= B_FALSE
;
1291 (void) snprintf(errbuf
, sizeof (errbuf
),
1292 dgettext(TEXT_DOMAIN
, "Key load error"));
1294 /* check that encryption is enabled for the pool */
1295 if (!encryption_feature_is_enabled(zhp
->zpool_hdl
)) {
1296 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1297 "Encryption feature not enabled."));
1302 /* Fetch the keyformat. Check that the dataset is encrypted. */
1303 keyformat
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYFORMAT
);
1304 if (keyformat
== ZFS_KEYFORMAT_NONE
) {
1305 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1306 "'%s' is not encrypted."), zfs_get_name(zhp
));
1312 * Fetch the key location. Check that we are working with an
1315 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, prop_encroot
);
1317 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1318 "Failed to get encryption root for '%s'."),
1321 } else if (!is_encroot
) {
1322 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1323 "Keys must be loaded for encryption root of '%s' (%s)."),
1324 zfs_get_name(zhp
), prop_encroot
);
1330 * if the caller has elected to override the keylocation property
1333 if (alt_keylocation
!= NULL
) {
1334 keylocation
= alt_keylocation
;
1336 ret
= zfs_prop_get(zhp
, ZFS_PROP_KEYLOCATION
, prop_keylocation
,
1337 sizeof (prop_keylocation
), NULL
, NULL
, 0, B_TRUE
);
1339 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1340 "Failed to get keylocation for '%s'."),
1345 keylocation
= prop_keylocation
;
1348 /* check that the key is unloaded unless this is a noop */
1350 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
1351 if (keystatus
== ZFS_KEYSTATUS_AVAILABLE
) {
1352 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1353 "Key already loaded for '%s'."), zfs_get_name(zhp
));
1359 /* passphrase formats require a salt and pbkdf2_iters property */
1360 if (keyformat
== ZFS_KEYFORMAT_PASSPHRASE
) {
1361 salt
= zfs_prop_get_int(zhp
, ZFS_PROP_PBKDF2_SALT
);
1362 iters
= zfs_prop_get_int(zhp
, ZFS_PROP_PBKDF2_ITERS
);
1366 /* fetching and deriving the key are correctable errors. set the flag */
1367 correctible
= B_TRUE
;
1369 /* get key material from key format and location */
1370 ret
= get_key_material(zhp
->zfs_hdl
, B_FALSE
, B_FALSE
, keyformat
,
1371 keylocation
, zfs_get_name(zhp
), &key_material
, &key_material_len
,
1376 /* derive a key from the key material */
1377 ret
= derive_key(zhp
->zfs_hdl
, keyformat
, iters
, key_material
, salt
,
1382 correctible
= B_FALSE
;
1384 /* pass the wrapping key and noop flag to the ioctl */
1385 ret
= lzc_load_key(zhp
->zfs_name
, noop
, key_data
, WRAPPING_KEY_LEN
);
1389 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1390 "Permission denied."));
1393 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1394 "Invalid parameters provided for dataset %s."),
1398 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1399 "Key already loaded for '%s'."), zfs_get_name(zhp
));
1402 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1403 "'%s' is busy."), zfs_get_name(zhp
));
1406 correctible
= B_TRUE
;
1407 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1408 "Incorrect key provided for '%s'."),
1411 case ZFS_ERR_CRYPTO_NOTSUP
:
1412 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1413 "'%s' uses an unsupported encryption suite."),
1426 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1427 if (key_material
!= NULL
) {
1429 key_material
= NULL
;
1431 if (key_data
!= NULL
) {
1437 * Here we decide if it is ok to allow the user to retry entering their
1438 * key. The can_retry flag will be set if the user is entering their
1439 * key from an interactive prompt. The correctable flag will only be
1440 * set if an error that occurred could be corrected by retrying. Both
1441 * flags are needed to allow the user to attempt key entry again
1444 if (can_retry
&& correctible
&& attempts
< MAX_KEY_PROMPT_ATTEMPTS
)
1451 zfs_crypto_unload_key(zfs_handle_t
*zhp
)
1454 char errbuf
[ERRBUFLEN
];
1455 char prop_encroot
[MAXNAMELEN
];
1456 uint64_t keystatus
, keyformat
;
1457 boolean_t is_encroot
;
1459 (void) snprintf(errbuf
, sizeof (errbuf
),
1460 dgettext(TEXT_DOMAIN
, "Key unload error"));
1462 /* check that encryption is enabled for the pool */
1463 if (!encryption_feature_is_enabled(zhp
->zpool_hdl
)) {
1464 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1465 "Encryption feature not enabled."));
1470 /* Fetch the keyformat. Check that the dataset is encrypted. */
1471 keyformat
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYFORMAT
);
1472 if (keyformat
== ZFS_KEYFORMAT_NONE
) {
1473 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1474 "'%s' is not encrypted."), zfs_get_name(zhp
));
1480 * Fetch the key location. Check that we are working with an
1483 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, prop_encroot
);
1485 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1486 "Failed to get encryption root for '%s'."),
1489 } else if (!is_encroot
) {
1490 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1491 "Keys must be unloaded for encryption root of '%s' (%s)."),
1492 zfs_get_name(zhp
), prop_encroot
);
1497 /* check that the key is loaded */
1498 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
1499 if (keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
) {
1500 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1501 "Key already unloaded for '%s'."), zfs_get_name(zhp
));
1506 /* call the ioctl */
1507 ret
= lzc_unload_key(zhp
->zfs_name
);
1512 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1513 "Permission denied."));
1516 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1517 "Key already unloaded for '%s'."),
1521 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1522 "'%s' is busy."), zfs_get_name(zhp
));
1525 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1531 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1536 zfs_crypto_verify_rewrap_nvlist(zfs_handle_t
*zhp
, nvlist_t
*props
,
1537 nvlist_t
**props_out
, char *errbuf
)
1540 nvpair_t
*elem
= NULL
;
1542 nvlist_t
*new_props
= NULL
;
1544 new_props
= fnvlist_alloc();
1547 * loop through all provided properties, we should only have
1548 * keyformat, keylocation and pbkdf2iters. The actual validation of
1549 * values is done by zfs_valid_proplist().
1551 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
1552 const char *propname
= nvpair_name(elem
);
1553 prop
= zfs_name_to_prop(propname
);
1556 case ZFS_PROP_PBKDF2_ITERS
:
1557 case ZFS_PROP_KEYFORMAT
:
1558 case ZFS_PROP_KEYLOCATION
:
1562 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1563 "Only keyformat, keylocation and pbkdf2iters may "
1564 "be set with this command."));
1569 new_props
= zfs_valid_proplist(zhp
->zfs_hdl
, zhp
->zfs_type
, props
,
1570 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), NULL
, zhp
->zpool_hdl
,
1572 if (new_props
== NULL
) {
1577 *props_out
= new_props
;
1581 nvlist_free(new_props
);
1587 zfs_crypto_rewrap(zfs_handle_t
*zhp
, nvlist_t
*raw_props
, boolean_t inheritkey
)
1590 char errbuf
[ERRBUFLEN
];
1591 boolean_t is_encroot
;
1592 nvlist_t
*props
= NULL
;
1593 uint8_t *wkeydata
= NULL
;
1595 dcp_cmd_t cmd
= (inheritkey
) ? DCP_CMD_INHERIT
: DCP_CMD_NEW_KEY
;
1596 uint64_t crypt
, pcrypt
, keystatus
, pkeystatus
;
1597 uint64_t keyformat
= ZFS_KEYFORMAT_NONE
;
1598 zfs_handle_t
*pzhp
= NULL
;
1599 const char *keylocation
= NULL
;
1600 char origin_name
[MAXNAMELEN
];
1601 char prop_keylocation
[MAXNAMELEN
];
1602 char parent_name
[ZFS_MAX_DATASET_NAME_LEN
];
1604 (void) snprintf(errbuf
, sizeof (errbuf
),
1605 dgettext(TEXT_DOMAIN
, "Key change error"));
1607 /* check that encryption is enabled for the pool */
1608 if (!encryption_feature_is_enabled(zhp
->zpool_hdl
)) {
1609 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1610 "Encryption feature not enabled."));
1615 /* get crypt from dataset */
1616 crypt
= zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1617 if (crypt
== ZIO_CRYPT_OFF
) {
1618 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1619 "Dataset not encrypted."));
1624 /* get the encryption root of the dataset */
1625 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
1627 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1628 "Failed to get encryption root for '%s'."),
1633 /* Clones use their origin's key and cannot rewrap it */
1634 ret
= zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, origin_name
,
1635 sizeof (origin_name
), NULL
, NULL
, 0, B_TRUE
);
1636 if (ret
== 0 && strcmp(origin_name
, "") != 0) {
1637 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1638 "Keys cannot be changed on clones."));
1644 * If the user wants to use the inheritkey variant of this function
1645 * we don't need to collect any crypto arguments.
1648 /* validate the provided properties */
1649 ret
= zfs_crypto_verify_rewrap_nvlist(zhp
, raw_props
, &props
,
1655 * Load keyformat and keylocation from the nvlist. Fetch from
1656 * the dataset properties if not specified.
1658 (void) nvlist_lookup_uint64(props
,
1659 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), &keyformat
);
1660 (void) nvlist_lookup_string(props
,
1661 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), &keylocation
);
1665 * If this is already an encryption root, just keep
1666 * any properties not set by the user.
1668 if (keyformat
== ZFS_KEYFORMAT_NONE
) {
1669 keyformat
= zfs_prop_get_int(zhp
,
1670 ZFS_PROP_KEYFORMAT
);
1671 ret
= nvlist_add_uint64(props
,
1672 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
),
1675 zfs_error_aux(zhp
->zfs_hdl
,
1676 dgettext(TEXT_DOMAIN
, "Failed to "
1677 "get existing keyformat "
1683 if (keylocation
== NULL
) {
1684 ret
= zfs_prop_get(zhp
, ZFS_PROP_KEYLOCATION
,
1685 prop_keylocation
, sizeof (prop_keylocation
),
1686 NULL
, NULL
, 0, B_TRUE
);
1688 zfs_error_aux(zhp
->zfs_hdl
,
1689 dgettext(TEXT_DOMAIN
, "Failed to "
1690 "get existing keylocation "
1695 keylocation
= prop_keylocation
;
1698 /* need a new key for non-encryption roots */
1699 if (keyformat
== ZFS_KEYFORMAT_NONE
) {
1701 zfs_error_aux(zhp
->zfs_hdl
,
1702 dgettext(TEXT_DOMAIN
, "Keyformat required "
1703 "for new encryption root."));
1707 /* default to prompt if no keylocation is specified */
1708 if (keylocation
== NULL
) {
1709 keylocation
= "prompt";
1710 ret
= nvlist_add_string(props
,
1711 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1718 /* fetch the new wrapping key and associated properties */
1719 ret
= populate_create_encryption_params_nvlists(zhp
->zfs_hdl
,
1720 zhp
, B_TRUE
, keyformat
, keylocation
, props
, &wkeydata
,
1725 /* check that zhp is an encryption root */
1727 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1728 "Key inheritting can only be performed on "
1729 "encryption roots."));
1734 /* get the parent's name */
1735 ret
= zfs_parent_name(zhp
, parent_name
, sizeof (parent_name
));
1737 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1738 "Root dataset cannot inherit key."));
1743 /* get a handle to the parent */
1744 pzhp
= make_dataset_handle(zhp
->zfs_hdl
, parent_name
);
1746 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1747 "Failed to lookup parent."));
1752 /* parent must be encrypted */
1753 pcrypt
= zfs_prop_get_int(pzhp
, ZFS_PROP_ENCRYPTION
);
1754 if (pcrypt
== ZIO_CRYPT_OFF
) {
1755 zfs_error_aux(pzhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1756 "Parent must be encrypted."));
1761 /* check that the parent's key is loaded */
1762 pkeystatus
= zfs_prop_get_int(pzhp
, ZFS_PROP_KEYSTATUS
);
1763 if (pkeystatus
== ZFS_KEYSTATUS_UNAVAILABLE
) {
1764 zfs_error_aux(pzhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1765 "Parent key must be loaded."));
1771 /* check that the key is loaded */
1772 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
1773 if (keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
) {
1774 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1775 "Key must be loaded."));
1780 /* call the ioctl */
1781 ret
= lzc_change_key(zhp
->zfs_name
, cmd
, props
, wkeydata
, wkeylen
);
1785 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1786 "Permission denied."));
1789 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1790 "Invalid properties for key change."));
1793 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1794 "Key is not currently loaded."));
1797 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1804 if (wkeydata
!= NULL
)
1814 if (wkeydata
!= NULL
)
1817 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);