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>
40 #include "libzfs_impl.h"
41 #include "zfeature_common.h"
44 * User keys are used to decrypt the master encryption keys of a dataset. This
45 * indirection allows a user to change his / her access key without having to
46 * re-encrypt the entire dataset. User keys can be provided in one of several
47 * ways. Raw keys are simply given to the kernel as is. Similarly, hex keys
48 * are converted to binary and passed into the kernel. Password based keys are
49 * a bit more complicated. Passwords alone do not provide suitable entropy for
50 * encryption and may be too short or too long to be used. In order to derive
51 * a more appropriate key we use a PBKDF2 function. This function is designed
52 * to take a (relatively) long time to calculate in order to discourage
53 * attackers from guessing from a list of common passwords. PBKDF2 requires
54 * 2 additional parameters. The first is the number of iterations to run, which
55 * will ultimately determine how long it takes to derive the resulting key from
56 * the password. The second parameter is a salt that is randomly generated for
57 * each dataset. The salt is used to "tweak" PBKDF2 such that a group of
58 * attackers cannot reasonably generate a table of commonly known passwords to
59 * their output keys and expect it work for all past and future PBKDF2 users.
60 * We store the salt as a hidden property of the dataset (although it is
61 * technically ok if the salt is known to the attacker).
64 #define MIN_PASSPHRASE_LEN 8
65 #define MAX_PASSPHRASE_LEN 512
66 #define MAX_KEY_PROMPT_ATTEMPTS 3
68 static int caught_interrupt
;
70 static int get_key_material_file(libzfs_handle_t
*, const char *, const char *,
71 zfs_keyformat_t
, boolean_t
, uint8_t **, size_t *);
72 static int get_key_material_https(libzfs_handle_t
*, const char *, const char *,
73 zfs_keyformat_t
, boolean_t
, uint8_t **, size_t *);
75 static zfs_uri_handler_t uri_handlers
[] = {
76 { "file", get_key_material_file
},
77 { "https", get_key_material_https
},
78 { "http", get_key_material_https
},
83 pkcs11_get_urandom(uint8_t *buf
, size_t bytes
)
86 ssize_t bytes_read
= 0;
88 rand
= open("/dev/urandom", O_RDONLY
| O_CLOEXEC
);
93 while (bytes_read
< bytes
) {
94 ssize_t rc
= read(rand
, buf
+ bytes_read
, bytes
- bytes_read
);
106 zfs_prop_parse_keylocation(libzfs_handle_t
*restrict hdl
, const char *str
,
107 zfs_keylocation_t
*restrict locp
, char **restrict schemep
)
109 *locp
= ZFS_KEYLOCATION_NONE
;
112 if (strcmp("prompt", str
) == 0) {
113 *locp
= ZFS_KEYLOCATION_PROMPT
;
117 regmatch_t pmatch
[2];
119 if (regexec(&hdl
->libzfs_urire
, str
, ARRAY_SIZE(pmatch
),
123 if (pmatch
[1].rm_so
== -1) {
124 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
129 scheme_len
= pmatch
[1].rm_eo
- pmatch
[1].rm_so
;
131 *schemep
= calloc(1, scheme_len
+ 1);
132 if (*schemep
== NULL
) {
136 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
141 (void) memcpy(*schemep
, str
+ pmatch
[1].rm_so
, scheme_len
);
142 *locp
= ZFS_KEYLOCATION_URI
;
146 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Invalid keylocation"));
151 hex_key_to_raw(char *hex
, int hexlen
, uint8_t *out
)
156 for (i
= 0; i
< hexlen
; i
+= 2) {
157 if (!isxdigit(hex
[i
]) || !isxdigit(hex
[i
+ 1])) {
162 ret
= sscanf(&hex
[i
], "%02x", &c
);
179 catch_signal(int sig
)
181 caught_interrupt
= sig
;
185 get_format_prompt_string(zfs_keyformat_t format
)
188 case ZFS_KEYFORMAT_RAW
:
190 case ZFS_KEYFORMAT_HEX
:
192 case ZFS_KEYFORMAT_PASSPHRASE
:
193 return ("passphrase");
195 /* shouldn't happen */
200 /* do basic validation of the key material */
202 validate_key(libzfs_handle_t
*hdl
, zfs_keyformat_t keyformat
,
203 const char *key
, size_t keylen
, boolean_t do_verify
)
206 case ZFS_KEYFORMAT_RAW
:
207 /* verify the key length is correct */
208 if (keylen
< WRAPPING_KEY_LEN
) {
209 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
210 "Raw key too short (expected %u)."),
215 if (keylen
> WRAPPING_KEY_LEN
) {
216 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
217 "Raw key too long (expected %u)."),
222 case ZFS_KEYFORMAT_HEX
:
223 /* verify the key length is correct */
224 if (keylen
< WRAPPING_KEY_LEN
* 2) {
225 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
226 "Hex key too short (expected %u)."),
227 WRAPPING_KEY_LEN
* 2);
231 if (keylen
> WRAPPING_KEY_LEN
* 2) {
232 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
233 "Hex key too long (expected %u)."),
234 WRAPPING_KEY_LEN
* 2);
238 /* check for invalid hex digits */
239 for (size_t i
= 0; i
< WRAPPING_KEY_LEN
* 2; i
++) {
240 if (!isxdigit(key
[i
])) {
241 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
242 "Invalid hex character detected."));
247 case ZFS_KEYFORMAT_PASSPHRASE
:
249 * Verify the length is within bounds when setting a new key,
250 * but not when loading an existing key.
254 if (keylen
> MAX_PASSPHRASE_LEN
) {
255 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
256 "Passphrase too long (max %u)."),
261 if (keylen
< MIN_PASSPHRASE_LEN
) {
262 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
263 "Passphrase too short (min %u)."),
269 /* can't happen, checked above */
277 libzfs_getpassphrase(zfs_keyformat_t keyformat
, boolean_t is_reenter
,
278 boolean_t new_key
, const char *fsname
,
279 char **restrict res
, size_t *restrict reslen
)
285 struct termios old_term
, new_term
;
286 struct sigaction act
, osigint
, osigtstp
;
292 * handle SIGINT and ignore SIGSTP. This is necessary to
293 * restore the state of the terminal.
295 caught_interrupt
= 0;
297 (void) sigemptyset(&act
.sa_mask
);
298 act
.sa_handler
= catch_signal
;
300 (void) sigaction(SIGINT
, &act
, &osigint
);
301 act
.sa_handler
= SIG_IGN
;
302 (void) sigaction(SIGTSTP
, &act
, &osigtstp
);
304 (void) printf("%s %s%s",
305 is_reenter
? "Re-enter" : "Enter",
306 new_key
? "new " : "",
307 get_format_prompt_string(keyformat
));
309 (void) printf(" for '%s'", fsname
);
310 (void) fputc(':', stdout
);
311 (void) fflush(stdout
);
313 /* disable the terminal echo for key input */
314 (void) tcgetattr(fileno(f
), &old_term
);
317 new_term
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
319 ret
= tcsetattr(fileno(f
), TCSAFLUSH
, &new_term
);
326 bytes
= getline(res
, &buflen
, f
);
333 /* trim the ending newline if it exists */
334 if (bytes
> 0 && (*res
)[bytes
- 1] == '\n') {
335 (*res
)[bytes
- 1] = '\0';
342 /* reset the terminal */
343 (void) tcsetattr(fileno(f
), TCSAFLUSH
, &old_term
);
344 (void) sigaction(SIGINT
, &osigint
, NULL
);
345 (void) sigaction(SIGTSTP
, &osigtstp
, NULL
);
347 /* if we caught a signal, re-throw it now */
348 if (caught_interrupt
!= 0)
349 (void) kill(getpid(), caught_interrupt
);
351 /* print the newline that was not echo'd */
358 get_key_interactive(libzfs_handle_t
*restrict hdl
, const char *fsname
,
359 zfs_keyformat_t keyformat
, boolean_t confirm_key
, boolean_t newkey
,
360 uint8_t **restrict outbuf
, size_t *restrict len_out
)
362 char *buf
= NULL
, *buf2
= NULL
;
363 size_t buflen
= 0, buf2len
= 0;
366 ASSERT(isatty(fileno(stdin
)));
368 /* raw keys cannot be entered on the terminal */
369 if (keyformat
== ZFS_KEYFORMAT_RAW
) {
371 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
372 "Cannot enter raw keys on the terminal"));
376 /* prompt for the key */
377 if ((ret
= libzfs_getpassphrase(keyformat
, B_FALSE
, newkey
, fsname
,
378 &buf
, &buflen
)) != 0) {
388 if ((ret
= validate_key(hdl
, keyformat
, buf
, buflen
, confirm_key
)) !=
394 ret
= libzfs_getpassphrase(keyformat
, B_TRUE
, newkey
, fsname
, &buf2
,
400 buflen
= buf2len
= 0;
404 if (buflen
!= buf2len
|| strcmp(buf
, buf2
) != 0) {
410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
411 "Provided keys do not match."));
417 *outbuf
= (uint8_t *)buf
;
423 get_key_material_raw(FILE *fd
, zfs_keyformat_t keyformat
,
424 uint8_t **buf
, size_t *len_out
)
431 /* read the key material */
432 if (keyformat
!= ZFS_KEYFORMAT_RAW
) {
435 bytes
= getline((char **)buf
, &buflen
, fd
);
442 /* trim the ending newline if it exists */
443 if (bytes
> 0 && (*buf
)[bytes
- 1] == '\n') {
444 (*buf
)[bytes
- 1] = '\0';
453 * Raw keys may have newline characters in them and so can't
454 * use getline(). Here we attempt to read 33 bytes so that we
455 * can properly check the key length (the file should only have
458 *buf
= malloc((WRAPPING_KEY_LEN
+ 1) * sizeof (uint8_t));
464 n
= fread(*buf
, 1, WRAPPING_KEY_LEN
+ 1, fd
);
465 if (n
== 0 || ferror(fd
)) {
466 /* size errors are handled by the calling function */
481 get_key_material_file(libzfs_handle_t
*hdl
, const char *uri
,
482 const char *fsname
, zfs_keyformat_t keyformat
, boolean_t newkey
,
483 uint8_t **restrict buf
, size_t *restrict len_out
)
485 (void) fsname
, (void) newkey
;
492 if ((f
= fopen(uri
+ 7, "re")) == NULL
) {
495 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
496 "Failed to open key material file: %s"), strerror(ret
));
500 ret
= get_key_material_raw(f
, keyformat
, buf
, len_out
);
508 get_key_material_https(libzfs_handle_t
*hdl
, const char *uri
,
509 const char *fsname
, zfs_keyformat_t keyformat
, boolean_t newkey
,
510 uint8_t **restrict buf
, size_t *restrict len_out
)
512 (void) fsname
, (void) newkey
;
515 boolean_t is_http
= strncmp(uri
, "http:", strlen("http:")) == 0;
517 if (strlen(uri
) < (is_http
? 7 : 8)) {
523 #define LOAD_FUNCTION(func) \
524 __typeof__(func) *func = dlsym(hdl->libfetch, #func);
526 if (hdl
->libfetch
== NULL
)
527 hdl
->libfetch
= dlopen(LIBFETCH_SONAME
, RTLD_LAZY
);
529 if (hdl
->libfetch
== NULL
) {
530 hdl
->libfetch
= (void *)-1;
531 char *err
= dlerror();
533 hdl
->libfetch_load_error
= strdup(err
);
536 if (hdl
->libfetch
== (void *)-1) {
538 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
539 "Couldn't load %s: %s"),
540 LIBFETCH_SONAME
, hdl
->libfetch_load_error
?: "(?)");
545 #if LIBFETCH_IS_FETCH
546 LOAD_FUNCTION(fetchGetURL
);
547 char *fetchLastErrString
= dlsym(hdl
->libfetch
, "fetchLastErrString");
549 ok
= fetchGetURL
&& fetchLastErrString
;
550 #elif LIBFETCH_IS_LIBCURL
551 LOAD_FUNCTION(curl_easy_init
);
552 LOAD_FUNCTION(curl_easy_setopt
);
553 LOAD_FUNCTION(curl_easy_perform
);
554 LOAD_FUNCTION(curl_easy_cleanup
);
555 LOAD_FUNCTION(curl_easy_strerror
);
556 LOAD_FUNCTION(curl_easy_getinfo
);
558 ok
= curl_easy_init
&& curl_easy_setopt
&& curl_easy_perform
&&
559 curl_easy_cleanup
&& curl_easy_strerror
&& curl_easy_getinfo
;
562 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
563 "keylocation=%s back-end %s missing symbols."),
564 is_http
? "http://" : "https://", LIBFETCH_SONAME
);
570 #if LIBFETCH_IS_FETCH
571 key
= fetchGetURL(uri
, "");
573 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
574 "Couldn't GET %s: %s"),
575 uri
, fetchLastErrString
);
578 #elif LIBFETCH_IS_LIBCURL
579 CURL
*curl
= curl_easy_init();
587 kfd
= open(getenv("TMPDIR") ?: "/tmp",
588 O_RDWR
| O_TMPFILE
| O_EXCL
| O_CLOEXEC
, 0600);
595 "%s/libzfs-XXXXXXXX.https", getenv("TMPDIR") ?: "/tmp") == -1) {
597 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s"),
602 kfd
= mkostemps(path
, strlen(".https"), O_CLOEXEC
);
605 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
606 "Couldn't create temporary file %s: %s"),
607 path
, strerror(ret
));
615 if ((key
= fdopen(kfd
, "r+")) == NULL
) {
618 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
619 "Couldn't reopen temporary file: %s"), strerror(ret
));
623 char errbuf
[CURL_ERROR_SIZE
] = "";
624 char *cainfo
= getenv("SSL_CA_CERT_FILE"); /* matches fetch(3) */
625 char *capath
= getenv("SSL_CA_CERT_PATH"); /* matches fetch(3) */
626 char *clcert
= getenv("SSL_CLIENT_CERT_FILE"); /* matches fetch(3) */
627 char *clkey
= getenv("SSL_CLIENT_KEY_FILE"); /* matches fetch(3) */
628 (void) curl_easy_setopt(curl
, CURLOPT_URL
, uri
);
629 (void) curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, 1L);
630 (void) curl_easy_setopt(curl
, CURLOPT_TIMEOUT_MS
, 30000L);
631 (void) curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, key
);
632 (void) curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, errbuf
);
634 (void) curl_easy_setopt(curl
, CURLOPT_CAINFO
, cainfo
);
636 (void) curl_easy_setopt(curl
, CURLOPT_CAPATH
, capath
);
638 (void) curl_easy_setopt(curl
, CURLOPT_SSLCERT
, clcert
);
640 (void) curl_easy_setopt(curl
, CURLOPT_SSLKEY
, clkey
);
642 CURLcode res
= curl_easy_perform(curl
);
644 if (res
!= CURLE_OK
) {
645 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
646 "Failed to connect to %s: %s"),
647 uri
, strlen(errbuf
) ? errbuf
: curl_easy_strerror(res
));
651 (void) curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &resp
);
653 if (resp
< 200 || resp
>= 300) {
654 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
655 "Couldn't GET %s: %ld"),
662 curl_easy_cleanup(curl
);
664 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
665 "No keylocation=%s back-end."), is_http
? "http://" : "https://");
671 ret
= get_key_material_raw(key
, keyformat
, buf
, len_out
);
680 * Attempts to fetch key material, no matter where it might live. The key
681 * material is allocated and returned in km_out. *can_retry_out will be set
682 * to B_TRUE if the user is providing the key material interactively, allowing
683 * for re-entry attempts.
686 get_key_material(libzfs_handle_t
*hdl
, boolean_t do_verify
, boolean_t newkey
,
687 zfs_keyformat_t keyformat
, const char *keylocation
, const char *fsname
,
688 uint8_t **km_out
, size_t *kmlen_out
, boolean_t
*can_retry_out
)
691 zfs_keylocation_t keyloc
= ZFS_KEYLOCATION_NONE
;
694 char *uri_scheme
= NULL
;
695 zfs_uri_handler_t
*handler
= NULL
;
696 boolean_t can_retry
= B_FALSE
;
698 /* verify and parse the keylocation */
699 ret
= zfs_prop_parse_keylocation(hdl
, keylocation
, &keyloc
,
704 /* open the appropriate file descriptor */
706 case ZFS_KEYLOCATION_PROMPT
:
707 if (isatty(fileno(stdin
))) {
708 can_retry
= keyformat
!= ZFS_KEYFORMAT_RAW
;
709 ret
= get_key_interactive(hdl
, fsname
, keyformat
,
710 do_verify
, newkey
, &km
, &kmlen
);
712 /* fetch the key material into the buffer */
713 ret
= get_key_material_raw(stdin
, keyformat
, &km
,
721 case ZFS_KEYLOCATION_URI
:
724 for (handler
= uri_handlers
; handler
->zuh_scheme
!= NULL
;
726 if (strcmp(handler
->zuh_scheme
, uri_scheme
) != 0)
729 if ((ret
= handler
->zuh_handler(hdl
, keylocation
,
730 fsname
, keyformat
, newkey
, &km
, &kmlen
)) != 0)
736 if (ret
== ENOTSUP
) {
737 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
738 "URI scheme is not supported"));
745 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
746 "Invalid keylocation."));
750 if ((ret
= validate_key(hdl
, keyformat
, (const char *)km
, kmlen
,
756 if (can_retry_out
!= NULL
)
757 *can_retry_out
= can_retry
;
768 if (can_retry_out
!= NULL
)
769 *can_retry_out
= can_retry
;
776 derive_key(libzfs_handle_t
*hdl
, zfs_keyformat_t format
, uint64_t iters
,
777 uint8_t *key_material
, uint64_t salt
,
785 key
= zfs_alloc(hdl
, WRAPPING_KEY_LEN
);
788 case ZFS_KEYFORMAT_RAW
:
789 memcpy(key
, key_material
, WRAPPING_KEY_LEN
);
791 case ZFS_KEYFORMAT_HEX
:
792 ret
= hex_key_to_raw((char *)key_material
,
793 WRAPPING_KEY_LEN
* 2, key
);
795 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
796 "Invalid hex key provided."));
800 case ZFS_KEYFORMAT_PASSPHRASE
:
803 ret
= PKCS5_PBKDF2_HMAC_SHA1((char *)key_material
,
804 strlen((char *)key_material
), ((uint8_t *)&salt
),
805 sizeof (uint64_t), iters
, WRAPPING_KEY_LEN
, key
);
808 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
809 "Failed to generate key from passphrase."));
829 encryption_feature_is_enabled(zpool_handle_t
*zph
)
832 uint64_t feat_refcount
;
834 /* check that features can be enabled */
835 if (zpool_get_prop_int(zph
, ZPOOL_PROP_VERSION
, NULL
)
836 < SPA_VERSION_FEATURES
)
839 /* check for crypto feature */
840 features
= zpool_get_features(zph
);
841 if (!features
|| nvlist_lookup_uint64(features
,
842 spa_feature_table
[SPA_FEATURE_ENCRYPTION
].fi_guid
,
843 &feat_refcount
) != 0)
850 populate_create_encryption_params_nvlists(libzfs_handle_t
*hdl
,
851 zfs_handle_t
*zhp
, boolean_t newkey
, zfs_keyformat_t keyformat
,
852 const char *keylocation
, nvlist_t
*props
, uint8_t **wkeydata
,
856 uint64_t iters
= 0, salt
= 0;
857 uint8_t *key_material
= NULL
;
858 size_t key_material_len
= 0;
859 uint8_t *key_data
= NULL
;
860 const char *fsname
= (zhp
) ? zfs_get_name(zhp
) : NULL
;
862 /* get key material from keyformat and keylocation */
863 ret
= get_key_material(hdl
, B_TRUE
, newkey
, keyformat
, keylocation
,
864 fsname
, &key_material
, &key_material_len
, NULL
);
868 /* passphrase formats require a salt and pbkdf2 iters property */
869 if (keyformat
== ZFS_KEYFORMAT_PASSPHRASE
) {
870 /* always generate a new salt */
871 ret
= pkcs11_get_urandom((uint8_t *)&salt
, sizeof (uint64_t));
872 if (ret
!= sizeof (uint64_t)) {
873 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
874 "Failed to generate salt."));
878 ret
= nvlist_add_uint64(props
,
879 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), salt
);
881 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
882 "Failed to add salt to properties."));
887 * If not otherwise specified, use the default number of
888 * pbkdf2 iterations. If specified, we have already checked
889 * that the given value is greater than MIN_PBKDF2_ITERATIONS
890 * during zfs_valid_proplist().
892 ret
= nvlist_lookup_uint64(props
,
893 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), &iters
);
895 iters
= DEFAULT_PBKDF2_ITERATIONS
;
896 ret
= nvlist_add_uint64(props
,
897 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), iters
);
900 } else if (ret
!= 0) {
901 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
902 "Failed to get pbkdf2 iterations."));
906 /* check that pbkdf2iters was not specified by the user */
907 ret
= nvlist_lookup_uint64(props
,
908 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), &iters
);
911 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
912 "Cannot specify pbkdf2iters with a non-passphrase "
918 /* derive a key from the key material */
919 ret
= derive_key(hdl
, keyformat
, iters
, key_material
, salt
, &key_data
);
925 *wkeydata
= key_data
;
926 *wkeylen
= WRAPPING_KEY_LEN
;
930 if (key_material
!= NULL
)
932 if (key_data
!= NULL
)
941 proplist_has_encryption_props(nvlist_t
*props
)
947 ret
= nvlist_lookup_uint64(props
,
948 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), &intval
);
949 if (ret
== 0 && intval
!= ZIO_CRYPT_OFF
)
952 ret
= nvlist_lookup_string(props
,
953 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), &strval
);
954 if (ret
== 0 && strcmp(strval
, "none") != 0)
957 ret
= nvlist_lookup_uint64(props
,
958 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), &intval
);
962 ret
= nvlist_lookup_uint64(props
,
963 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), &intval
);
971 zfs_crypto_get_encryption_root(zfs_handle_t
*zhp
, boolean_t
*is_encroot
,
975 char prop_encroot
[MAXNAMELEN
];
977 /* if the dataset isn't encrypted, just return */
978 if (zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) == ZIO_CRYPT_OFF
) {
979 *is_encroot
= B_FALSE
;
985 ret
= zfs_prop_get(zhp
, ZFS_PROP_ENCRYPTION_ROOT
, prop_encroot
,
986 sizeof (prop_encroot
), NULL
, NULL
, 0, B_TRUE
);
988 *is_encroot
= B_FALSE
;
994 *is_encroot
= strcmp(prop_encroot
, zfs_get_name(zhp
)) == 0;
996 strcpy(buf
, prop_encroot
);
1002 zfs_crypto_create(libzfs_handle_t
*hdl
, char *parent_name
, nvlist_t
*props
,
1003 nvlist_t
*pool_props
, boolean_t stdin_available
, uint8_t **wkeydata_out
,
1004 uint_t
*wkeylen_out
)
1007 char errbuf
[ERRBUFLEN
];
1008 uint64_t crypt
= ZIO_CRYPT_INHERIT
, pcrypt
= ZIO_CRYPT_INHERIT
;
1009 uint64_t keyformat
= ZFS_KEYFORMAT_NONE
;
1010 char *keylocation
= NULL
;
1011 zfs_handle_t
*pzhp
= NULL
;
1012 uint8_t *wkeydata
= NULL
;
1014 boolean_t local_crypt
= B_TRUE
;
1016 (void) snprintf(errbuf
, sizeof (errbuf
),
1017 dgettext(TEXT_DOMAIN
, "Encryption create error"));
1019 /* lookup crypt from props */
1020 ret
= nvlist_lookup_uint64(props
,
1021 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), &crypt
);
1023 local_crypt
= B_FALSE
;
1025 /* lookup key location and format from props */
1026 (void) nvlist_lookup_uint64(props
,
1027 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), &keyformat
);
1028 (void) nvlist_lookup_string(props
,
1029 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), &keylocation
);
1031 if (parent_name
!= NULL
) {
1032 /* get a reference to parent dataset */
1033 pzhp
= make_dataset_handle(hdl
, parent_name
);
1036 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1037 "Failed to lookup parent."));
1041 /* Lookup parent's crypt */
1042 pcrypt
= zfs_prop_get_int(pzhp
, ZFS_PROP_ENCRYPTION
);
1044 /* Params require the encryption feature */
1045 if (!encryption_feature_is_enabled(pzhp
->zpool_hdl
)) {
1046 if (proplist_has_encryption_props(props
)) {
1048 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1049 "Encryption feature not enabled."));
1058 * special case for root dataset where encryption feature
1059 * feature won't be on disk yet
1061 if (!nvlist_exists(pool_props
, "feature@encryption")) {
1062 if (proplist_has_encryption_props(props
)) {
1064 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1065 "Encryption feature not enabled."));
1073 pcrypt
= ZIO_CRYPT_OFF
;
1076 /* Get the inherited encryption property if we don't have it locally */
1081 * At this point crypt should be the actual encryption value. If
1082 * encryption is off just verify that no encryption properties have
1083 * been specified and return.
1085 if (crypt
== ZIO_CRYPT_OFF
) {
1086 if (proplist_has_encryption_props(props
)) {
1088 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1089 "Encryption must be turned on to set encryption "
1099 * If we have a parent crypt it is valid to specify encryption alone.
1100 * This will result in a child that is encrypted with the chosen
1101 * encryption suite that will also inherit the parent's key. If
1102 * the parent is not encrypted we need an encryption suite provided.
1104 if (pcrypt
== ZIO_CRYPT_OFF
&& keylocation
== NULL
&&
1105 keyformat
== ZFS_KEYFORMAT_NONE
) {
1107 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1108 "Keyformat required for new encryption root."));
1113 * Specifying a keylocation implies this will be a new encryption root.
1114 * Check that a keyformat is also specified.
1116 if (keylocation
!= NULL
&& keyformat
== ZFS_KEYFORMAT_NONE
) {
1118 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1119 "Keyformat required for new encryption root."));
1123 /* default to prompt if no keylocation is specified */
1124 if (keyformat
!= ZFS_KEYFORMAT_NONE
&& keylocation
== NULL
) {
1125 keylocation
= (char *)"prompt";
1126 ret
= nvlist_add_string(props
,
1127 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), keylocation
);
1133 * If a local key is provided, this dataset will be a new
1134 * encryption root. Populate the encryption params.
1136 if (keylocation
!= NULL
) {
1138 * 'zfs recv -o keylocation=prompt' won't work because stdin
1139 * is being used by the send stream, so we disallow it.
1141 if (!stdin_available
&& strcmp(keylocation
, "prompt") == 0) {
1143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Cannot use "
1144 "'prompt' keylocation because stdin is in use."));
1148 ret
= populate_create_encryption_params_nvlists(hdl
, NULL
,
1149 B_TRUE
, keyformat
, keylocation
, props
, &wkeydata
,
1158 *wkeydata_out
= wkeydata
;
1159 *wkeylen_out
= wkeylen
;
1165 if (wkeydata
!= NULL
)
1168 *wkeydata_out
= NULL
;
1174 zfs_crypto_clone_check(libzfs_handle_t
*hdl
, zfs_handle_t
*origin_zhp
,
1175 char *parent_name
, nvlist_t
*props
)
1177 (void) origin_zhp
, (void) parent_name
;
1178 char errbuf
[ERRBUFLEN
];
1180 (void) snprintf(errbuf
, sizeof (errbuf
),
1181 dgettext(TEXT_DOMAIN
, "Encryption clone error"));
1184 * No encryption properties should be specified. They will all be
1185 * inherited from the origin dataset.
1187 if (nvlist_exists(props
, zfs_prop_to_name(ZFS_PROP_KEYFORMAT
)) ||
1188 nvlist_exists(props
, zfs_prop_to_name(ZFS_PROP_KEYLOCATION
)) ||
1189 nvlist_exists(props
, zfs_prop_to_name(ZFS_PROP_ENCRYPTION
)) ||
1190 nvlist_exists(props
, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
))) {
1191 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1192 "Encryption properties must inherit from origin dataset."));
1199 typedef struct loadkeys_cbdata
{
1200 uint64_t cb_numfailed
;
1201 uint64_t cb_numattempted
;
1205 load_keys_cb(zfs_handle_t
*zhp
, void *arg
)
1208 boolean_t is_encroot
;
1209 loadkey_cbdata_t
*cb
= arg
;
1210 uint64_t keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
1212 /* only attempt to load keys for encryption roots */
1213 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
1214 if (ret
!= 0 || !is_encroot
)
1217 /* don't attempt to load already loaded keys */
1218 if (keystatus
== ZFS_KEYSTATUS_AVAILABLE
)
1221 /* Attempt to load the key. Record status in cb. */
1222 cb
->cb_numattempted
++;
1224 ret
= zfs_crypto_load_key(zhp
, B_FALSE
, NULL
);
1229 (void) zfs_iter_filesystems(zhp
, 0, load_keys_cb
, cb
);
1232 /* always return 0, since this function is best effort */
1237 * This function is best effort. It attempts to load all the keys for the given
1238 * filesystem and all of its children.
1241 zfs_crypto_attempt_load_keys(libzfs_handle_t
*hdl
, const char *fsname
)
1244 zfs_handle_t
*zhp
= NULL
;
1245 loadkey_cbdata_t cb
= { 0 };
1247 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1253 ret
= load_keys_cb(zfs_handle_dup(zhp
), &cb
);
1257 (void) printf(gettext("%llu / %llu keys successfully loaded\n"),
1258 (u_longlong_t
)(cb
.cb_numattempted
- cb
.cb_numfailed
),
1259 (u_longlong_t
)cb
.cb_numattempted
);
1261 if (cb
.cb_numfailed
!= 0) {
1276 zfs_crypto_load_key(zfs_handle_t
*zhp
, boolean_t noop
,
1277 const char *alt_keylocation
)
1279 int ret
, attempts
= 0;
1280 char errbuf
[ERRBUFLEN
];
1281 uint64_t keystatus
, iters
= 0, salt
= 0;
1282 uint64_t keyformat
= ZFS_KEYFORMAT_NONE
;
1283 char prop_keylocation
[MAXNAMELEN
];
1284 char prop_encroot
[MAXNAMELEN
];
1285 const char *keylocation
= NULL
;
1286 uint8_t *key_material
= NULL
, *key_data
= NULL
;
1287 size_t key_material_len
;
1288 boolean_t is_encroot
, can_retry
= B_FALSE
, correctible
= B_FALSE
;
1290 (void) snprintf(errbuf
, sizeof (errbuf
),
1291 dgettext(TEXT_DOMAIN
, "Key load error"));
1293 /* check that encryption is enabled for the pool */
1294 if (!encryption_feature_is_enabled(zhp
->zpool_hdl
)) {
1295 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1296 "Encryption feature not enabled."));
1301 /* Fetch the keyformat. Check that the dataset is encrypted. */
1302 keyformat
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYFORMAT
);
1303 if (keyformat
== ZFS_KEYFORMAT_NONE
) {
1304 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1305 "'%s' is not encrypted."), zfs_get_name(zhp
));
1311 * Fetch the key location. Check that we are working with an
1314 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, prop_encroot
);
1316 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1317 "Failed to get encryption root for '%s'."),
1320 } else if (!is_encroot
) {
1321 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1322 "Keys must be loaded for encryption root of '%s' (%s)."),
1323 zfs_get_name(zhp
), prop_encroot
);
1329 * if the caller has elected to override the keylocation property
1332 if (alt_keylocation
!= NULL
) {
1333 keylocation
= alt_keylocation
;
1335 ret
= zfs_prop_get(zhp
, ZFS_PROP_KEYLOCATION
, prop_keylocation
,
1336 sizeof (prop_keylocation
), NULL
, NULL
, 0, B_TRUE
);
1338 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1339 "Failed to get keylocation for '%s'."),
1344 keylocation
= prop_keylocation
;
1347 /* check that the key is unloaded unless this is a noop */
1349 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
1350 if (keystatus
== ZFS_KEYSTATUS_AVAILABLE
) {
1351 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1352 "Key already loaded for '%s'."), zfs_get_name(zhp
));
1358 /* passphrase formats require a salt and pbkdf2_iters property */
1359 if (keyformat
== ZFS_KEYFORMAT_PASSPHRASE
) {
1360 salt
= zfs_prop_get_int(zhp
, ZFS_PROP_PBKDF2_SALT
);
1361 iters
= zfs_prop_get_int(zhp
, ZFS_PROP_PBKDF2_ITERS
);
1365 /* fetching and deriving the key are correctable errors. set the flag */
1366 correctible
= B_TRUE
;
1368 /* get key material from key format and location */
1369 ret
= get_key_material(zhp
->zfs_hdl
, B_FALSE
, B_FALSE
, keyformat
,
1370 keylocation
, zfs_get_name(zhp
), &key_material
, &key_material_len
,
1375 /* derive a key from the key material */
1376 ret
= derive_key(zhp
->zfs_hdl
, keyformat
, iters
, key_material
, salt
,
1381 correctible
= B_FALSE
;
1383 /* pass the wrapping key and noop flag to the ioctl */
1384 ret
= lzc_load_key(zhp
->zfs_name
, noop
, key_data
, WRAPPING_KEY_LEN
);
1388 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1389 "Permission denied."));
1392 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1393 "Invalid parameters provided for dataset %s."),
1397 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1398 "Key already loaded for '%s'."), zfs_get_name(zhp
));
1401 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1402 "'%s' is busy."), zfs_get_name(zhp
));
1405 correctible
= B_TRUE
;
1406 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1407 "Incorrect key provided for '%s'."),
1420 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1421 if (key_material
!= NULL
) {
1423 key_material
= NULL
;
1425 if (key_data
!= NULL
) {
1431 * Here we decide if it is ok to allow the user to retry entering their
1432 * key. The can_retry flag will be set if the user is entering their
1433 * key from an interactive prompt. The correctable flag will only be
1434 * set if an error that occurred could be corrected by retrying. Both
1435 * flags are needed to allow the user to attempt key entry again
1438 if (can_retry
&& correctible
&& attempts
< MAX_KEY_PROMPT_ATTEMPTS
)
1445 zfs_crypto_unload_key(zfs_handle_t
*zhp
)
1448 char errbuf
[ERRBUFLEN
];
1449 char prop_encroot
[MAXNAMELEN
];
1450 uint64_t keystatus
, keyformat
;
1451 boolean_t is_encroot
;
1453 (void) snprintf(errbuf
, sizeof (errbuf
),
1454 dgettext(TEXT_DOMAIN
, "Key unload error"));
1456 /* check that encryption is enabled for the pool */
1457 if (!encryption_feature_is_enabled(zhp
->zpool_hdl
)) {
1458 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1459 "Encryption feature not enabled."));
1464 /* Fetch the keyformat. Check that the dataset is encrypted. */
1465 keyformat
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYFORMAT
);
1466 if (keyformat
== ZFS_KEYFORMAT_NONE
) {
1467 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1468 "'%s' is not encrypted."), zfs_get_name(zhp
));
1474 * Fetch the key location. Check that we are working with an
1477 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, prop_encroot
);
1479 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1480 "Failed to get encryption root for '%s'."),
1483 } else if (!is_encroot
) {
1484 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1485 "Keys must be unloaded for encryption root of '%s' (%s)."),
1486 zfs_get_name(zhp
), prop_encroot
);
1491 /* check that the key is loaded */
1492 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
1493 if (keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
) {
1494 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1495 "Key already unloaded for '%s'."), zfs_get_name(zhp
));
1500 /* call the ioctl */
1501 ret
= lzc_unload_key(zhp
->zfs_name
);
1506 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1507 "Permission denied."));
1510 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1511 "Key already unloaded for '%s'."),
1515 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1516 "'%s' is busy."), zfs_get_name(zhp
));
1519 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1525 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1530 zfs_crypto_verify_rewrap_nvlist(zfs_handle_t
*zhp
, nvlist_t
*props
,
1531 nvlist_t
**props_out
, char *errbuf
)
1534 nvpair_t
*elem
= NULL
;
1536 nvlist_t
*new_props
= NULL
;
1538 new_props
= fnvlist_alloc();
1541 * loop through all provided properties, we should only have
1542 * keyformat, keylocation and pbkdf2iters. The actual validation of
1543 * values is done by zfs_valid_proplist().
1545 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
1546 const char *propname
= nvpair_name(elem
);
1547 prop
= zfs_name_to_prop(propname
);
1550 case ZFS_PROP_PBKDF2_ITERS
:
1551 case ZFS_PROP_KEYFORMAT
:
1552 case ZFS_PROP_KEYLOCATION
:
1556 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1557 "Only keyformat, keylocation and pbkdf2iters may "
1558 "be set with this command."));
1563 new_props
= zfs_valid_proplist(zhp
->zfs_hdl
, zhp
->zfs_type
, props
,
1564 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), NULL
, zhp
->zpool_hdl
,
1566 if (new_props
== NULL
) {
1571 *props_out
= new_props
;
1575 nvlist_free(new_props
);
1581 zfs_crypto_rewrap(zfs_handle_t
*zhp
, nvlist_t
*raw_props
, boolean_t inheritkey
)
1584 char errbuf
[ERRBUFLEN
];
1585 boolean_t is_encroot
;
1586 nvlist_t
*props
= NULL
;
1587 uint8_t *wkeydata
= NULL
;
1589 dcp_cmd_t cmd
= (inheritkey
) ? DCP_CMD_INHERIT
: DCP_CMD_NEW_KEY
;
1590 uint64_t crypt
, pcrypt
, keystatus
, pkeystatus
;
1591 uint64_t keyformat
= ZFS_KEYFORMAT_NONE
;
1592 zfs_handle_t
*pzhp
= NULL
;
1593 char *keylocation
= NULL
;
1594 char origin_name
[MAXNAMELEN
];
1595 char prop_keylocation
[MAXNAMELEN
];
1596 char parent_name
[ZFS_MAX_DATASET_NAME_LEN
];
1598 (void) snprintf(errbuf
, sizeof (errbuf
),
1599 dgettext(TEXT_DOMAIN
, "Key change error"));
1601 /* check that encryption is enabled for the pool */
1602 if (!encryption_feature_is_enabled(zhp
->zpool_hdl
)) {
1603 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1604 "Encryption feature not enabled."));
1609 /* get crypt from dataset */
1610 crypt
= zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1611 if (crypt
== ZIO_CRYPT_OFF
) {
1612 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1613 "Dataset not encrypted."));
1618 /* get the encryption root of the dataset */
1619 ret
= zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
1621 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1622 "Failed to get encryption root for '%s'."),
1627 /* Clones use their origin's key and cannot rewrap it */
1628 ret
= zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, origin_name
,
1629 sizeof (origin_name
), NULL
, NULL
, 0, B_TRUE
);
1630 if (ret
== 0 && strcmp(origin_name
, "") != 0) {
1631 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1632 "Keys cannot be changed on clones."));
1638 * If the user wants to use the inheritkey variant of this function
1639 * we don't need to collect any crypto arguments.
1642 /* validate the provided properties */
1643 ret
= zfs_crypto_verify_rewrap_nvlist(zhp
, raw_props
, &props
,
1649 * Load keyformat and keylocation from the nvlist. Fetch from
1650 * the dataset properties if not specified.
1652 (void) nvlist_lookup_uint64(props
,
1653 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), &keyformat
);
1654 (void) nvlist_lookup_string(props
,
1655 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), &keylocation
);
1659 * If this is already an encryption root, just keep
1660 * any properties not set by the user.
1662 if (keyformat
== ZFS_KEYFORMAT_NONE
) {
1663 keyformat
= zfs_prop_get_int(zhp
,
1664 ZFS_PROP_KEYFORMAT
);
1665 ret
= nvlist_add_uint64(props
,
1666 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
),
1669 zfs_error_aux(zhp
->zfs_hdl
,
1670 dgettext(TEXT_DOMAIN
, "Failed to "
1671 "get existing keyformat "
1677 if (keylocation
== NULL
) {
1678 ret
= zfs_prop_get(zhp
, ZFS_PROP_KEYLOCATION
,
1679 prop_keylocation
, sizeof (prop_keylocation
),
1680 NULL
, NULL
, 0, B_TRUE
);
1682 zfs_error_aux(zhp
->zfs_hdl
,
1683 dgettext(TEXT_DOMAIN
, "Failed to "
1684 "get existing keylocation "
1689 keylocation
= prop_keylocation
;
1692 /* need a new key for non-encryption roots */
1693 if (keyformat
== ZFS_KEYFORMAT_NONE
) {
1695 zfs_error_aux(zhp
->zfs_hdl
,
1696 dgettext(TEXT_DOMAIN
, "Keyformat required "
1697 "for new encryption root."));
1701 /* default to prompt if no keylocation is specified */
1702 if (keylocation
== NULL
) {
1703 keylocation
= (char *)"prompt";
1704 ret
= nvlist_add_string(props
,
1705 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1712 /* fetch the new wrapping key and associated properties */
1713 ret
= populate_create_encryption_params_nvlists(zhp
->zfs_hdl
,
1714 zhp
, B_TRUE
, keyformat
, keylocation
, props
, &wkeydata
,
1719 /* check that zhp is an encryption root */
1721 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1722 "Key inheritting can only be performed on "
1723 "encryption roots."));
1728 /* get the parent's name */
1729 ret
= zfs_parent_name(zhp
, parent_name
, sizeof (parent_name
));
1731 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1732 "Root dataset cannot inherit key."));
1737 /* get a handle to the parent */
1738 pzhp
= make_dataset_handle(zhp
->zfs_hdl
, parent_name
);
1740 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1741 "Failed to lookup parent."));
1746 /* parent must be encrypted */
1747 pcrypt
= zfs_prop_get_int(pzhp
, ZFS_PROP_ENCRYPTION
);
1748 if (pcrypt
== ZIO_CRYPT_OFF
) {
1749 zfs_error_aux(pzhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1750 "Parent must be encrypted."));
1755 /* check that the parent's key is loaded */
1756 pkeystatus
= zfs_prop_get_int(pzhp
, ZFS_PROP_KEYSTATUS
);
1757 if (pkeystatus
== ZFS_KEYSTATUS_UNAVAILABLE
) {
1758 zfs_error_aux(pzhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1759 "Parent key must be loaded."));
1765 /* check that the key is loaded */
1766 keystatus
= zfs_prop_get_int(zhp
, ZFS_PROP_KEYSTATUS
);
1767 if (keystatus
== ZFS_KEYSTATUS_UNAVAILABLE
) {
1768 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1769 "Key must be loaded."));
1774 /* call the ioctl */
1775 ret
= lzc_change_key(zhp
->zfs_name
, cmd
, props
, wkeydata
, wkeylen
);
1779 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1780 "Permission denied."));
1783 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1784 "Invalid properties for key change."));
1787 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1788 "Key is not currently loaded."));
1791 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1798 if (wkeydata
!= NULL
)
1808 if (wkeydata
!= NULL
)
1811 zfs_error(zhp
->zfs_hdl
, EZFS_CRYPTOFAILED
, errbuf
);