1eb633035STom Caputi /*
2eb633035STom Caputi * CDDL HEADER START
3eb633035STom Caputi *
4eb633035STom Caputi * This file and its contents are supplied under the terms of the
5eb633035STom Caputi * Common Development and Distribution License ("CDDL"), version 1.0.
6eb633035STom Caputi * You may only use this file in accordance with the terms of version
7eb633035STom Caputi * 1.0 of the CDDL.
8eb633035STom Caputi *
9eb633035STom Caputi * A full copy of the text of the CDDL should have accompanied this
10eb633035STom Caputi * source. A copy of the CDDL is also available via the Internet at
11eb633035STom Caputi * http://www.illumos.org/license/CDDL.
12eb633035STom Caputi *
13eb633035STom Caputi * CDDL HEADER END
14eb633035STom Caputi */
15eb633035STom Caputi
16eb633035STom Caputi /*
17eb633035STom Caputi * Copyright (c) 2017, Datto, Inc. All rights reserved.
18*6c24238bSJason King * Copyright 2020 Joyent, Inc.
19eb633035STom Caputi */
20eb633035STom Caputi
21eb633035STom Caputi #include <string.h>
22eb633035STom Caputi #include <strings.h>
23eb633035STom Caputi #include <ctype.h>
24eb633035STom Caputi #include <unistd.h>
25eb633035STom Caputi #include <sys/zfs_context.h>
26eb633035STom Caputi #include <sys/fs/zfs.h>
27eb633035STom Caputi #include <sys/dsl_crypt.h>
28eb633035STom Caputi #ifdef sun
294a3c0cdaSJason King #include <stdlib.h>
304a3c0cdaSJason King #include <security/cryptoki.h>
314a3c0cdaSJason King #include <cryptoutil.h> /* for pkcs11_strerror */
32eb633035STom Caputi #else
33eb633035STom Caputi #include <sys/crypto/icp.h>
34eb633035STom Caputi #endif
35eb633035STom Caputi #include <libintl.h>
36eb633035STom Caputi #include <termios.h>
37eb633035STom Caputi #include <signal.h>
38eb633035STom Caputi #include <errno.h>
39eb633035STom Caputi #include <libzfs.h>
40eb633035STom Caputi #include "libzfs_impl.h"
41eb633035STom Caputi #include "zfeature_common.h"
42eb633035STom Caputi
43eb633035STom Caputi /*
44eb633035STom Caputi * User keys are used to decrypt the master encryption keys of a dataset. This
45eb633035STom Caputi * indirection allows a user to change his / her access key without having to
46eb633035STom Caputi * re-encrypt the entire dataset. User keys can be provided in one of several
47eb633035STom Caputi * ways. Raw keys are simply given to the kernel as is. Similarly, hex keys
48eb633035STom Caputi * are converted to binary and passed into the kernel. Password based keys are
49eb633035STom Caputi * a bit more complicated. Passwords alone do not provide suitable entropy for
50eb633035STom Caputi * encryption and may be too short or too long to be used. In order to derive
51eb633035STom Caputi * a more appropriate key we use a PBKDF2 function. This function is designed
52eb633035STom Caputi * to take a (relatively) long time to calculate in order to discourage
53eb633035STom Caputi * attackers from guessing from a list of common passwords. PBKDF2 requires
54eb633035STom Caputi * 2 additional parameters. The first is the number of iterations to run, which
55eb633035STom Caputi * will ultimately determine how long it takes to derive the resulting key from
56eb633035STom Caputi * the password. The second parameter is a salt that is randomly generated for
57eb633035STom Caputi * each dataset. The salt is used to "tweak" PBKDF2 such that a group of
58eb633035STom Caputi * attackers cannot reasonably generate a table of commonly known passwords to
59eb633035STom Caputi * their output keys and expect it work for all past and future PBKDF2 users.
60eb633035STom Caputi * We store the salt as a hidden property of the dataset (although it is
61eb633035STom Caputi * technically ok if the salt is known to the attacker).
62eb633035STom Caputi */
63eb633035STom Caputi
64eb633035STom Caputi typedef enum key_locator {
65eb633035STom Caputi KEY_LOCATOR_NONE,
66eb633035STom Caputi KEY_LOCATOR_PROMPT,
67eb633035STom Caputi KEY_LOCATOR_URI
68eb633035STom Caputi } key_locator_t;
69eb633035STom Caputi
70eb633035STom Caputi #define MIN_PASSPHRASE_LEN 8
71eb633035STom Caputi #define MAX_PASSPHRASE_LEN 512
72eb633035STom Caputi #define MAX_KEY_PROMPT_ATTEMPTS 3
73eb633035STom Caputi
74eb633035STom Caputi static int caught_interrupt;
75eb633035STom Caputi
76*6c24238bSJason King static int get_key_material_file(libzfs_handle_t *, const char *, const char *,
77*6c24238bSJason King zfs_keyformat_t, boolean_t, uint8_t **, size_t *);
78*6c24238bSJason King
79*6c24238bSJason King static zfs_uri_handler_t uri_handlers[] = {
80*6c24238bSJason King { "file", get_key_material_file },
81*6c24238bSJason King { NULL, NULL }
82*6c24238bSJason King };
83*6c24238bSJason King
84*6c24238bSJason King static int
zfs_prop_parse_keylocation(libzfs_handle_t * restrict hdl,const char * str,zfs_keylocation_t * restrict locp,char ** restrict schemep)85*6c24238bSJason King zfs_prop_parse_keylocation(libzfs_handle_t *restrict hdl, const char *str,
86*6c24238bSJason King zfs_keylocation_t *restrict locp, char **restrict schemep)
87eb633035STom Caputi {
88*6c24238bSJason King int ret;
89*6c24238bSJason King
90*6c24238bSJason King *locp = ZFS_KEYLOCATION_NONE;
91*6c24238bSJason King *schemep = NULL;
92eb633035STom Caputi
93*6c24238bSJason King if (strcmp("prompt", str) == 0) {
94*6c24238bSJason King *locp = ZFS_KEYLOCATION_PROMPT;
95*6c24238bSJason King return (0);
96*6c24238bSJason King }
97*6c24238bSJason King
98*6c24238bSJason King regmatch_t pmatch[URI_NMATCH];
99*6c24238bSJason King regmatch_t *smatch = &pmatch[URI_SCHEMESUBEXP];
100*6c24238bSJason King
101*6c24238bSJason King ret = regexec(&hdl->libzfs_urire, str, ARRAY_SIZE(pmatch), pmatch, 0);
102*6c24238bSJason King switch (ret) {
103*6c24238bSJason King case 0:
104*6c24238bSJason King break;
105*6c24238bSJason King case REG_ESPACE:
106*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Out of memory"));
107*6c24238bSJason King return (ENOMEM);
108*6c24238bSJason King case REG_NOMATCH:
109*6c24238bSJason King goto invalid;
110*6c24238bSJason King default:
111*6c24238bSJason King /*
112*6c24238bSJason King * Any other errors from regexec are a programming bug,
113*6c24238bSJason King * so consider them a fatal error.
114*6c24238bSJason King */
115*6c24238bSJason King (void) fprintf(stderr, "regexec failed: %d\n", ret);
116*6c24238bSJason King abort();
117*6c24238bSJason King }
118*6c24238bSJason King
119*6c24238bSJason King if (smatch->rm_so == -1)
120*6c24238bSJason King goto invalid;
121*6c24238bSJason King
122*6c24238bSJason King *schemep = strndup(str + smatch->rm_so, smatch->rm_eo - smatch->rm_so);
123*6c24238bSJason King if (*schemep == NULL) {
124*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Out of memory"));
125*6c24238bSJason King return (ENOMEM);
126*6c24238bSJason King }
127*6c24238bSJason King
128*6c24238bSJason King *locp = ZFS_KEYLOCATION_URI;
129*6c24238bSJason King return (0);
130*6c24238bSJason King
131*6c24238bSJason King invalid:
132*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Invalid keylocation"));
133*6c24238bSJason King return (EINVAL);
134eb633035STom Caputi }
135eb633035STom Caputi
136eb633035STom Caputi static int
hex_key_to_raw(char * hex,int hexlen,uint8_t * out)137eb633035STom Caputi hex_key_to_raw(char *hex, int hexlen, uint8_t *out)
138eb633035STom Caputi {
139eb633035STom Caputi int ret, i;
140eb633035STom Caputi unsigned int c;
141eb633035STom Caputi
142eb633035STom Caputi for (i = 0; i < hexlen; i += 2) {
143eb633035STom Caputi if (!isxdigit(hex[i]) || !isxdigit(hex[i + 1])) {
144eb633035STom Caputi ret = EINVAL;
145eb633035STom Caputi goto error;
146eb633035STom Caputi }
147eb633035STom Caputi
148eb633035STom Caputi ret = sscanf(&hex[i], "%02x", &c);
149eb633035STom Caputi if (ret != 1) {
150eb633035STom Caputi ret = EINVAL;
151eb633035STom Caputi goto error;
152eb633035STom Caputi }
153eb633035STom Caputi
154eb633035STom Caputi out[i / 2] = c;
155eb633035STom Caputi }
156eb633035STom Caputi
157eb633035STom Caputi return (0);
158eb633035STom Caputi
159eb633035STom Caputi error:
160eb633035STom Caputi return (ret);
161eb633035STom Caputi }
162eb633035STom Caputi
163eb633035STom Caputi
164eb633035STom Caputi static void
catch_signal(int sig)165eb633035STom Caputi catch_signal(int sig)
166eb633035STom Caputi {
167eb633035STom Caputi caught_interrupt = sig;
168eb633035STom Caputi }
169eb633035STom Caputi
170eb633035STom Caputi static char *
get_format_prompt_string(zfs_keyformat_t format)171eb633035STom Caputi get_format_prompt_string(zfs_keyformat_t format)
172eb633035STom Caputi {
173eb633035STom Caputi switch (format) {
174eb633035STom Caputi case ZFS_KEYFORMAT_RAW:
175eb633035STom Caputi return ("raw key");
176eb633035STom Caputi case ZFS_KEYFORMAT_HEX:
177eb633035STom Caputi return ("hex key");
178eb633035STom Caputi case ZFS_KEYFORMAT_PASSPHRASE:
179eb633035STom Caputi return ("passphrase");
180eb633035STom Caputi default:
181eb633035STom Caputi /* shouldn't happen */
182eb633035STom Caputi return (NULL);
183eb633035STom Caputi }
184eb633035STom Caputi }
185eb633035STom Caputi
186*6c24238bSJason King /* do basic validation of the key material */
187eb633035STom Caputi static int
validate_key(libzfs_handle_t * hdl,zfs_keyformat_t keyformat,const char * key,size_t keylen)188*6c24238bSJason King validate_key(libzfs_handle_t *hdl, zfs_keyformat_t keyformat,
189*6c24238bSJason King const char *key, size_t keylen)
190eb633035STom Caputi {
191*6c24238bSJason King switch (keyformat) {
192*6c24238bSJason King case ZFS_KEYFORMAT_RAW:
193*6c24238bSJason King /* verify the key length is correct */
194*6c24238bSJason King if (keylen < WRAPPING_KEY_LEN) {
195*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
196*6c24238bSJason King "Raw key too short (expected %u)."),
197*6c24238bSJason King WRAPPING_KEY_LEN);
198*6c24238bSJason King return (EINVAL);
199*6c24238bSJason King }
200*6c24238bSJason King
201*6c24238bSJason King if (keylen > WRAPPING_KEY_LEN) {
202*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
203*6c24238bSJason King "Raw key too long (expected %u)."),
204*6c24238bSJason King WRAPPING_KEY_LEN);
205*6c24238bSJason King return (EINVAL);
206*6c24238bSJason King }
207*6c24238bSJason King break;
208*6c24238bSJason King case ZFS_KEYFORMAT_HEX:
209*6c24238bSJason King /* verify the key length is correct */
210*6c24238bSJason King if (keylen < WRAPPING_KEY_LEN * 2) {
211*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
212*6c24238bSJason King "Hex key too short (expected %u)."),
213*6c24238bSJason King WRAPPING_KEY_LEN * 2);
214*6c24238bSJason King return (EINVAL);
215*6c24238bSJason King }
216*6c24238bSJason King
217*6c24238bSJason King if (keylen > WRAPPING_KEY_LEN * 2) {
218*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
219*6c24238bSJason King "Hex key too long (expected %u)."),
220*6c24238bSJason King WRAPPING_KEY_LEN * 2);
221*6c24238bSJason King return (EINVAL);
222*6c24238bSJason King }
223*6c24238bSJason King
224*6c24238bSJason King /* check for invalid hex digits */
225*6c24238bSJason King for (size_t i = 0; i < WRAPPING_KEY_LEN * 2; i++) {
226*6c24238bSJason King if (!isxdigit(key[i])) {
227*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
228*6c24238bSJason King "Invalid hex character detected."));
229*6c24238bSJason King return (EINVAL);
230*6c24238bSJason King }
231*6c24238bSJason King }
232*6c24238bSJason King break;
233*6c24238bSJason King case ZFS_KEYFORMAT_PASSPHRASE:
234*6c24238bSJason King /* verify the length is within bounds */
235*6c24238bSJason King if (keylen > MAX_PASSPHRASE_LEN) {
236*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
237*6c24238bSJason King "Passphrase too long (max %u)."),
238*6c24238bSJason King MAX_PASSPHRASE_LEN);
239*6c24238bSJason King return (EINVAL);
240*6c24238bSJason King }
241*6c24238bSJason King
242*6c24238bSJason King if (keylen < MIN_PASSPHRASE_LEN) {
243*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
244*6c24238bSJason King "Passphrase too short (min %u)."),
245*6c24238bSJason King MIN_PASSPHRASE_LEN);
246*6c24238bSJason King return (EINVAL);
247*6c24238bSJason King }
248*6c24238bSJason King break;
249*6c24238bSJason King default:
250*6c24238bSJason King /* can't happen, checked above */
251*6c24238bSJason King break;
252*6c24238bSJason King }
253*6c24238bSJason King
254*6c24238bSJason King return (0);
255*6c24238bSJason King }
256*6c24238bSJason King
257*6c24238bSJason King static int
libzfs_getpassphrase(zfs_keyformat_t keyformat,boolean_t is_reenter,boolean_t new_key,const char * fsname,char ** restrict res,size_t * restrict reslen)258*6c24238bSJason King libzfs_getpassphrase(zfs_keyformat_t keyformat, boolean_t is_reenter,
259*6c24238bSJason King boolean_t new_key, const char *fsname,
260*6c24238bSJason King char **restrict res, size_t *restrict reslen)
261*6c24238bSJason King {
262*6c24238bSJason King FILE *f = stdin;
263eb633035STom Caputi size_t buflen = 0;
264*6c24238bSJason King ssize_t bytes;
265*6c24238bSJason King int ret = 0;
266eb633035STom Caputi struct termios old_term, new_term;
267eb633035STom Caputi struct sigaction act, osigint, osigtstp;
268eb633035STom Caputi
269*6c24238bSJason King *res = NULL;
270*6c24238bSJason King *reslen = 0;
271*6c24238bSJason King
272*6c24238bSJason King /*
273*6c24238bSJason King * handle SIGINT and ignore SIGSTP. This is necessary to
274*6c24238bSJason King * restore the state of the terminal.
275*6c24238bSJason King */
276*6c24238bSJason King caught_interrupt = 0;
277*6c24238bSJason King act.sa_flags = 0;
278*6c24238bSJason King (void) sigemptyset(&act.sa_mask);
279*6c24238bSJason King act.sa_handler = catch_signal;
280*6c24238bSJason King
281*6c24238bSJason King (void) sigaction(SIGINT, &act, &osigint);
282*6c24238bSJason King act.sa_handler = SIG_IGN;
283*6c24238bSJason King (void) sigaction(SIGTSTP, &act, &osigtstp);
284*6c24238bSJason King
285*6c24238bSJason King (void) printf("%s %s%s",
286*6c24238bSJason King is_reenter ? "Re-enter" : "Enter",
287*6c24238bSJason King new_key ? "new " : "",
288*6c24238bSJason King get_format_prompt_string(keyformat));
289*6c24238bSJason King if (fsname != NULL)
290*6c24238bSJason King (void) printf(" for '%s'", fsname);
291*6c24238bSJason King (void) fputc(':', stdout);
292*6c24238bSJason King (void) fflush(stdout);
293*6c24238bSJason King
294*6c24238bSJason King /* disable the terminal echo for key input */
295*6c24238bSJason King (void) tcgetattr(fileno(f), &old_term);
296*6c24238bSJason King
297*6c24238bSJason King new_term = old_term;
298*6c24238bSJason King new_term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
299*6c24238bSJason King
300*6c24238bSJason King ret = tcsetattr(fileno(f), TCSAFLUSH, &new_term);
301*6c24238bSJason King if (ret != 0) {
302*6c24238bSJason King ret = errno;
303*6c24238bSJason King errno = 0;
304*6c24238bSJason King goto out;
305*6c24238bSJason King }
306eb633035STom Caputi
307*6c24238bSJason King bytes = getline(res, &buflen, f);
308*6c24238bSJason King if (bytes < 0) {
309*6c24238bSJason King ret = errno;
310*6c24238bSJason King errno = 0;
311*6c24238bSJason King goto out;
312*6c24238bSJason King }
313eb633035STom Caputi
314*6c24238bSJason King /* trim the ending newline if it exists */
315*6c24238bSJason King if (bytes > 0 && (*res)[bytes - 1] == '\n') {
316*6c24238bSJason King (*res)[bytes - 1] = '\0';
317*6c24238bSJason King bytes--;
318*6c24238bSJason King }
319eb633035STom Caputi
320*6c24238bSJason King *reslen = bytes;
321eb633035STom Caputi
322*6c24238bSJason King out:
323*6c24238bSJason King /* reset the teminal */
324*6c24238bSJason King (void) tcsetattr(fileno(f), TCSAFLUSH, &old_term);
325*6c24238bSJason King (void) sigaction(SIGINT, &osigint, NULL);
326*6c24238bSJason King (void) sigaction(SIGTSTP, &osigtstp, NULL);
327eb633035STom Caputi
328*6c24238bSJason King /* if we caught a signal, re-throw it now */
329*6c24238bSJason King if (caught_interrupt != 0)
330*6c24238bSJason King (void) kill(getpid(), caught_interrupt);
331*6c24238bSJason King
332*6c24238bSJason King /* print the newline that was not echo'd */
333*6c24238bSJason King (void) printf("\n");
334*6c24238bSJason King
335*6c24238bSJason King return (ret);
336*6c24238bSJason King }
337*6c24238bSJason King
338*6c24238bSJason King static int
get_key_interactive(libzfs_handle_t * restrict hdl,const char * fsname,zfs_keyformat_t keyformat,boolean_t confirm_key,boolean_t newkey,uint8_t ** restrict outbuf,size_t * restrict len_out)339*6c24238bSJason King get_key_interactive(libzfs_handle_t *restrict hdl, const char *fsname,
340*6c24238bSJason King zfs_keyformat_t keyformat, boolean_t confirm_key, boolean_t newkey,
341*6c24238bSJason King uint8_t **restrict outbuf, size_t *restrict len_out)
342*6c24238bSJason King {
343*6c24238bSJason King char *buf = NULL, *buf2 = NULL;
344*6c24238bSJason King size_t buflen = 0, buf2len = 0;
345*6c24238bSJason King int ret = 0;
346*6c24238bSJason King
347*6c24238bSJason King ASSERT(isatty(fileno(stdin)));
348*6c24238bSJason King
349*6c24238bSJason King /* raw keys cannot be entered on the terminal */
350*6c24238bSJason King if (keyformat == ZFS_KEYFORMAT_RAW) {
351*6c24238bSJason King ret = EINVAL;
352*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
353*6c24238bSJason King "Cannot enter raw keys on the terminal"));
354*6c24238bSJason King goto out;
355*6c24238bSJason King }
356*6c24238bSJason King
357*6c24238bSJason King /* prompt for the key */
358*6c24238bSJason King if ((ret = libzfs_getpassphrase(keyformat, B_FALSE, newkey, fsname,
359*6c24238bSJason King &buf, &buflen)) != 0) {
360*6c24238bSJason King freezero(buf, buflen);
361*6c24238bSJason King buf = NULL;
362*6c24238bSJason King buflen = 0;
363*6c24238bSJason King goto out;
364*6c24238bSJason King }
365*6c24238bSJason King
366*6c24238bSJason King if (!confirm_key)
367*6c24238bSJason King goto out;
368*6c24238bSJason King
369*6c24238bSJason King if ((ret = validate_key(hdl, keyformat, buf, buflen)) != 0) {
370*6c24238bSJason King freezero(buf, buflen);
371*6c24238bSJason King return (ret);
372*6c24238bSJason King }
373*6c24238bSJason King
374*6c24238bSJason King ret = libzfs_getpassphrase(keyformat, B_TRUE, newkey, fsname, &buf2,
375*6c24238bSJason King &buf2len);
376*6c24238bSJason King if (ret != 0) {
377*6c24238bSJason King freezero(buf, buflen);
378*6c24238bSJason King freezero(buf2, buf2len);
379*6c24238bSJason King buf = buf2 = NULL;
380*6c24238bSJason King buflen = buf2len = 0;
381*6c24238bSJason King goto out;
382*6c24238bSJason King }
383*6c24238bSJason King
384*6c24238bSJason King if (buflen != buf2len || strcmp(buf, buf2) != 0) {
385*6c24238bSJason King freezero(buf, buflen);
386*6c24238bSJason King buf = NULL;
387*6c24238bSJason King buflen = 0;
388*6c24238bSJason King
389*6c24238bSJason King ret = EINVAL;
390*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
391*6c24238bSJason King "Provided keys do not match."));
392eb633035STom Caputi }
393eb633035STom Caputi
394*6c24238bSJason King freezero(buf2, buf2len);
395*6c24238bSJason King
396*6c24238bSJason King out:
397*6c24238bSJason King *outbuf = (uint8_t *)buf;
398*6c24238bSJason King *len_out = buflen;
399*6c24238bSJason King return (ret);
400*6c24238bSJason King }
401*6c24238bSJason King
402*6c24238bSJason King static int
get_key_material_raw(FILE * fd,zfs_keyformat_t keyformat,uint8_t ** buf,size_t * len_out)403*6c24238bSJason King get_key_material_raw(FILE *fd, zfs_keyformat_t keyformat,
404*6c24238bSJason King uint8_t **buf, size_t *len_out)
405*6c24238bSJason King {
406*6c24238bSJason King int ret = 0;
407*6c24238bSJason King size_t buflen = 0;
408*6c24238bSJason King
409*6c24238bSJason King *len_out = 0;
410*6c24238bSJason King
411eb633035STom Caputi /* read the key material */
412eb633035STom Caputi if (keyformat != ZFS_KEYFORMAT_RAW) {
413*6c24238bSJason King ssize_t bytes;
414*6c24238bSJason King
415eb633035STom Caputi bytes = getline((char **)buf, &buflen, fd);
416eb633035STom Caputi if (bytes < 0) {
417eb633035STom Caputi ret = errno;
418eb633035STom Caputi errno = 0;
419eb633035STom Caputi goto out;
420eb633035STom Caputi }
421eb633035STom Caputi
422eb633035STom Caputi /* trim the ending newline if it exists */
423*6c24238bSJason King if (bytes > 0 && (*buf)[bytes - 1] == '\n') {
424eb633035STom Caputi (*buf)[bytes - 1] = '\0';
425eb633035STom Caputi bytes--;
426eb633035STom Caputi }
427*6c24238bSJason King
428*6c24238bSJason King *len_out = bytes;
429eb633035STom Caputi } else {
430*6c24238bSJason King size_t n;
431*6c24238bSJason King
432eb633035STom Caputi /*
433eb633035STom Caputi * Raw keys may have newline characters in them and so can't
434eb633035STom Caputi * use getline(). Here we attempt to read 33 bytes so that we
435eb633035STom Caputi * can properly check the key length (the file should only have
436eb633035STom Caputi * 32 bytes).
437eb633035STom Caputi */
438*6c24238bSJason King *buf = malloc((WRAPPING_KEY_LEN + 1) * sizeof (uint8_t));
439eb633035STom Caputi if (*buf == NULL) {
440eb633035STom Caputi ret = ENOMEM;
441eb633035STom Caputi goto out;
442eb633035STom Caputi }
443eb633035STom Caputi
444*6c24238bSJason King n = fread(*buf, 1, WRAPPING_KEY_LEN + 1, fd);
445*6c24238bSJason King if (n == 0 || ferror(fd)) {
446eb633035STom Caputi /* size errors are handled by the calling function */
447eb633035STom Caputi free(*buf);
448eb633035STom Caputi *buf = NULL;
449eb633035STom Caputi ret = errno;
450eb633035STom Caputi errno = 0;
451eb633035STom Caputi goto out;
452eb633035STom Caputi }
453eb633035STom Caputi
454*6c24238bSJason King *len_out = n;
455*6c24238bSJason King }
456eb633035STom Caputi
457eb633035STom Caputi out:
458*6c24238bSJason King return (ret);
459*6c24238bSJason King }
460eb633035STom Caputi
461*6c24238bSJason King static int
get_key_material_file(libzfs_handle_t * hdl,const char * uri,const char * fsname,zfs_keyformat_t keyformat,boolean_t newkey,uint8_t ** restrict buf,size_t * restrict len_out)462*6c24238bSJason King get_key_material_file(libzfs_handle_t *hdl, const char *uri,
463*6c24238bSJason King const char *fsname, zfs_keyformat_t keyformat, boolean_t newkey,
464*6c24238bSJason King uint8_t **restrict buf, size_t *restrict len_out)
465*6c24238bSJason King {
466*6c24238bSJason King const char *path;
467*6c24238bSJason King FILE *f = NULL;
468*6c24238bSJason King int ret = 0;
469*6c24238bSJason King
470*6c24238bSJason King /*
471*6c24238bSJason King * get_key_material() should guarantee we're only called for a file
472*6c24238bSJason King * URI.
473*6c24238bSJason King */
474*6c24238bSJason King VERIFY0(strncmp(uri, "file://", 7));
475*6c24238bSJason King path = uri + 7;
476*6c24238bSJason King
477*6c24238bSJason King if ((f = fopen(path, "r")) == NULL) {
478*6c24238bSJason King ret = errno;
479*6c24238bSJason King errno = 0;
480*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
481*6c24238bSJason King "Failed to open key material file"));
482*6c24238bSJason King return (ret);
483eb633035STom Caputi }
484eb633035STom Caputi
485*6c24238bSJason King ret = get_key_material_raw(f, keyformat, buf, len_out);
486*6c24238bSJason King
487*6c24238bSJason King (void) fclose(f);
488eb633035STom Caputi
489*6c24238bSJason King return (ret);
490eb633035STom Caputi }
491eb633035STom Caputi
492eb633035STom Caputi /*
493eb633035STom Caputi * Attempts to fetch key material, no matter where it might live. The key
494eb633035STom Caputi * material is allocated and returned in km_out. *can_retry_out will be set
495eb633035STom Caputi * to B_TRUE if the user is providing the key material interactively, allowing
496eb633035STom Caputi * for re-entry attempts.
497eb633035STom Caputi */
498eb633035STom Caputi static int
get_key_material(libzfs_handle_t * hdl,boolean_t do_verify,boolean_t newkey,zfs_keyformat_t keyformat,char * keylocation,const char * fsname,uint8_t ** km_out,size_t * kmlen_out,boolean_t * can_retry_out)499eb633035STom Caputi get_key_material(libzfs_handle_t *hdl, boolean_t do_verify, boolean_t newkey,
500eb633035STom Caputi zfs_keyformat_t keyformat, char *keylocation, const char *fsname,
501eb633035STom Caputi uint8_t **km_out, size_t *kmlen_out, boolean_t *can_retry_out)
502eb633035STom Caputi {
503*6c24238bSJason King int ret;
504eb633035STom Caputi zfs_keylocation_t keyloc = ZFS_KEYLOCATION_NONE;
505*6c24238bSJason King uint8_t *km = NULL;
506*6c24238bSJason King size_t kmlen = 0;
507*6c24238bSJason King char *scheme = NULL;
508*6c24238bSJason King zfs_uri_handler_t *handler = NULL;
509eb633035STom Caputi boolean_t can_retry = B_FALSE;
510eb633035STom Caputi
511eb633035STom Caputi /* verify and parse the keylocation */
512*6c24238bSJason King ret = zfs_prop_parse_keylocation(hdl, keylocation, &keyloc, &scheme);
513*6c24238bSJason King if (ret != 0)
514*6c24238bSJason King goto error;
515eb633035STom Caputi
516eb633035STom Caputi /* open the appropriate file descriptor */
517eb633035STom Caputi switch (keyloc) {
518eb633035STom Caputi case ZFS_KEYLOCATION_PROMPT:
519*6c24238bSJason King if (isatty(fileno(stdin))) {
520eb633035STom Caputi can_retry = B_TRUE;
521eb633035STom Caputi
522*6c24238bSJason King ret = get_key_interactive(hdl, fsname, keyformat,
523*6c24238bSJason King do_verify, newkey, &km, &kmlen);
524*6c24238bSJason King } else {
525*6c24238bSJason King /* fetch the key material into the buffer */
526*6c24238bSJason King ret = get_key_material_raw(stdin, keyformat, &km,
527*6c24238bSJason King &kmlen);
528eb633035STom Caputi }
529*6c24238bSJason King if (ret != 0)
530*6c24238bSJason King goto error;
531eb633035STom Caputi break;
532eb633035STom Caputi case ZFS_KEYLOCATION_URI:
533*6c24238bSJason King for (handler = uri_handlers; handler->zuh_scheme != NULL;
534*6c24238bSJason King handler++) {
535*6c24238bSJason King if (strcmp(handler->zuh_scheme, scheme) != 0)
536*6c24238bSJason King continue;
537*6c24238bSJason King if ((ret = handler->zuh_handler(hdl, keylocation,
538*6c24238bSJason King fsname, keyformat, newkey, &km, &kmlen)) != 0)
539*6c24238bSJason King goto error;
540eb633035STom Caputi }
541*6c24238bSJason King ret = ENOTSUP;
542*6c24238bSJason King zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
543*6c24238bSJason King "URI scheme is not supported"));
544eb633035STom Caputi break;
545eb633035STom Caputi default:
546eb633035STom Caputi ret = EINVAL;
547eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
548eb633035STom Caputi "Invalid keylocation."));
549eb633035STom Caputi goto error;
550eb633035STom Caputi }
551eb633035STom Caputi
552*6c24238bSJason King if ((ret = validate_key(hdl, keyformat, (const char *)km, kmlen)) != 0)
553eb633035STom Caputi goto error;
554eb633035STom Caputi
555eb633035STom Caputi *km_out = km;
556eb633035STom Caputi *kmlen_out = kmlen;
557eb633035STom Caputi if (can_retry_out != NULL)
558eb633035STom Caputi *can_retry_out = can_retry;
559eb633035STom Caputi
560*6c24238bSJason King free(scheme);
561eb633035STom Caputi return (0);
562eb633035STom Caputi
563eb633035STom Caputi error:
564*6c24238bSJason King freezero(km, kmlen);
565*6c24238bSJason King free(scheme);
566eb633035STom Caputi
567eb633035STom Caputi *km_out = NULL;
568eb633035STom Caputi *kmlen_out = 0;
569eb633035STom Caputi
570eb633035STom Caputi return (ret);
571eb633035STom Caputi }
572eb633035STom Caputi
573eb633035STom Caputi /* This needs to be fixed to be compatible with other platforms */
574eb633035STom Caputi
575eb633035STom Caputi static int
pbkdf2(uint8_t * passphrase,size_t passphraselen,uint8_t * salt,size_t saltlen,uint64_t iterations,uint8_t * output,size_t outputlen)576eb633035STom Caputi pbkdf2(uint8_t *passphrase, size_t passphraselen, uint8_t *salt,
577eb633035STom Caputi size_t saltlen, uint64_t iterations, uint8_t *output,
578eb633035STom Caputi size_t outputlen)
579eb633035STom Caputi {
580eb633035STom Caputi int ret = 0;
581eb633035STom Caputi CK_SESSION_HANDLE session;
582eb633035STom Caputi char *tmpkeydata = NULL;
583eb633035STom Caputi size_t tmpkeydatalen = 0;
584eb633035STom Caputi CK_OBJECT_HANDLE obj;
585eb633035STom Caputi
586eb633035STom Caputi /* initialize output */
587eb633035STom Caputi (void) memset(output, 0, outputlen);
588eb633035STom Caputi
589eb633035STom Caputi ret = SUNW_C_GetMechSession(CKM_PKCS5_PBKD2, &session);
590eb633035STom Caputi if (ret) {
591eb633035STom Caputi (void) fprintf(stderr, "failed to connect to pkcs5: %s\n",
592eb633035STom Caputi pkcs11_strerror(ret));
593eb633035STom Caputi return (ret);
594eb633035STom Caputi }
595eb633035STom Caputi
596eb633035STom Caputi ret = pkcs11_PasswdToPBKD2Object(session, (char *)passphrase,
597eb633035STom Caputi passphraselen, salt, saltlen, iterations, CKK_AES, outputlen, 0,
598eb633035STom Caputi &obj);
599eb633035STom Caputi
600eb633035STom Caputi if (ret == CKR_OK)
601eb633035STom Caputi ret = pkcs11_ObjectToKey(session, obj, (void **)&tmpkeydata,
602eb633035STom Caputi &tmpkeydatalen, B_TRUE);
603eb633035STom Caputi
604eb633035STom Caputi (void) C_CloseSession(session);
605eb633035STom Caputi if (ret) {
606eb633035STom Caputi (void) fprintf(stderr, "unable to generate key: %s\n",
607eb633035STom Caputi pkcs11_strerror(ret));
608eb633035STom Caputi return (ret);
609eb633035STom Caputi }
610eb633035STom Caputi
611eb633035STom Caputi /*
612eb633035STom Caputi * Because it allocates an area for the passphrase, we copy it out
613eb633035STom Caputi * then zero the original
614eb633035STom Caputi */
615eb633035STom Caputi (void) memcpy(output, tmpkeydata, tmpkeydatalen);
616eb633035STom Caputi (void) memset(tmpkeydata, 0, tmpkeydatalen);
617eb633035STom Caputi free(tmpkeydata);
618eb633035STom Caputi
619eb633035STom Caputi return (ret);
620eb633035STom Caputi }
621eb633035STom Caputi
622eb633035STom Caputi /* ARGSUSED */
623eb633035STom Caputi static int
derive_key(libzfs_handle_t * hdl,zfs_keyformat_t format,uint64_t iters,uint8_t * key_material,size_t key_material_len,uint64_t salt,uint8_t ** key_out)624eb633035STom Caputi derive_key(libzfs_handle_t *hdl, zfs_keyformat_t format, uint64_t iters,
625eb633035STom Caputi uint8_t *key_material, size_t key_material_len, uint64_t salt,
626eb633035STom Caputi uint8_t **key_out)
627eb633035STom Caputi {
628eb633035STom Caputi int ret;
629eb633035STom Caputi uint8_t *key;
630eb633035STom Caputi
631eb633035STom Caputi *key_out = NULL;
632eb633035STom Caputi
633eb633035STom Caputi key = zfs_alloc(hdl, WRAPPING_KEY_LEN);
634eb633035STom Caputi if (!key)
635eb633035STom Caputi return (ENOMEM);
636eb633035STom Caputi
637eb633035STom Caputi switch (format) {
638eb633035STom Caputi case ZFS_KEYFORMAT_RAW:
639eb633035STom Caputi bcopy(key_material, key, WRAPPING_KEY_LEN);
640eb633035STom Caputi break;
641eb633035STom Caputi case ZFS_KEYFORMAT_HEX:
642eb633035STom Caputi ret = hex_key_to_raw((char *)key_material,
643eb633035STom Caputi WRAPPING_KEY_LEN * 2, key);
644eb633035STom Caputi if (ret != 0) {
645eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
646eb633035STom Caputi "Invalid hex key provided."));
647eb633035STom Caputi goto error;
648eb633035STom Caputi }
649eb633035STom Caputi break;
650eb633035STom Caputi case ZFS_KEYFORMAT_PASSPHRASE:
651eb633035STom Caputi salt = LE_64(salt);
652eb633035STom Caputi ret = pbkdf2(key_material, strlen((char *)key_material),
653eb633035STom Caputi ((uint8_t *)&salt), sizeof (uint64_t), iters,
654eb633035STom Caputi key, WRAPPING_KEY_LEN);
655eb633035STom Caputi if (ret != 0) {
656eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
657eb633035STom Caputi "Failed to generate key from passphrase."));
658eb633035STom Caputi goto error;
659eb633035STom Caputi }
660eb633035STom Caputi break;
661eb633035STom Caputi default:
662eb633035STom Caputi ret = EINVAL;
663eb633035STom Caputi goto error;
664eb633035STom Caputi }
665eb633035STom Caputi
666eb633035STom Caputi *key_out = key;
667eb633035STom Caputi return (0);
668eb633035STom Caputi
669eb633035STom Caputi error:
670eb633035STom Caputi free(key);
671eb633035STom Caputi
672eb633035STom Caputi *key_out = NULL;
673eb633035STom Caputi return (ret);
674eb633035STom Caputi }
675eb633035STom Caputi
676eb633035STom Caputi static boolean_t
encryption_feature_is_enabled(zpool_handle_t * zph)677eb633035STom Caputi encryption_feature_is_enabled(zpool_handle_t *zph)
678eb633035STom Caputi {
679eb633035STom Caputi nvlist_t *features;
680eb633035STom Caputi uint64_t feat_refcount;
681eb633035STom Caputi
682eb633035STom Caputi /* check that features can be enabled */
683eb633035STom Caputi if (zpool_get_prop_int(zph, ZPOOL_PROP_VERSION, NULL)
684eb633035STom Caputi < SPA_VERSION_FEATURES)
685eb633035STom Caputi return (B_FALSE);
686eb633035STom Caputi
687eb633035STom Caputi /* check for crypto feature */
688eb633035STom Caputi features = zpool_get_features(zph);
689eb633035STom Caputi if (!features || nvlist_lookup_uint64(features,
690eb633035STom Caputi spa_feature_table[SPA_FEATURE_ENCRYPTION].fi_guid,
691eb633035STom Caputi &feat_refcount) != 0)
692eb633035STom Caputi return (B_FALSE);
693eb633035STom Caputi
694eb633035STom Caputi return (B_TRUE);
695eb633035STom Caputi }
696eb633035STom Caputi
697eb633035STom Caputi static int
populate_create_encryption_params_nvlists(libzfs_handle_t * hdl,zfs_handle_t * zhp,boolean_t newkey,zfs_keyformat_t keyformat,char * keylocation,nvlist_t * props,uint8_t ** wkeydata,uint_t * wkeylen)698eb633035STom Caputi populate_create_encryption_params_nvlists(libzfs_handle_t *hdl,
699eb633035STom Caputi zfs_handle_t *zhp, boolean_t newkey, zfs_keyformat_t keyformat,
700eb633035STom Caputi char *keylocation, nvlist_t *props, uint8_t **wkeydata, uint_t *wkeylen)
701eb633035STom Caputi {
702eb633035STom Caputi int ret;
703eb633035STom Caputi uint64_t iters = 0, salt = 0;
704eb633035STom Caputi uint8_t *key_material = NULL;
705eb633035STom Caputi size_t key_material_len = 0;
706eb633035STom Caputi uint8_t *key_data = NULL;
707eb633035STom Caputi const char *fsname = (zhp) ? zfs_get_name(zhp) : NULL;
708eb633035STom Caputi
709eb633035STom Caputi /* get key material from keyformat and keylocation */
710eb633035STom Caputi ret = get_key_material(hdl, B_TRUE, newkey, keyformat, keylocation,
711eb633035STom Caputi fsname, &key_material, &key_material_len, NULL);
712eb633035STom Caputi if (ret != 0)
713eb633035STom Caputi goto error;
714eb633035STom Caputi
715eb633035STom Caputi /* passphrase formats require a salt and pbkdf2 iters property */
716eb633035STom Caputi if (keyformat == ZFS_KEYFORMAT_PASSPHRASE) {
717eb633035STom Caputi #ifdef sun
7184a3c0cdaSJason King arc4random_buf(&salt, sizeof (salt));
719eb633035STom Caputi #else
720eb633035STom Caputi random_init();
721eb633035STom Caputi
722eb633035STom Caputi ret = random_get_bytes((uint8_t *)&salt, sizeof (uint64_t));
723eb633035STom Caputi if (ret != 0) {
724eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
725eb633035STom Caputi "Failed to generate salt."));
726eb633035STom Caputi goto error;
727eb633035STom Caputi }
728eb633035STom Caputi
729eb633035STom Caputi random_fini();
730eb633035STom Caputi #endif
731eb633035STom Caputi
732eb633035STom Caputi ret = nvlist_add_uint64(props,
733eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), salt);
734eb633035STom Caputi if (ret != 0) {
735eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
736eb633035STom Caputi "Failed to add salt to properties."));
737eb633035STom Caputi goto error;
738eb633035STom Caputi }
739eb633035STom Caputi
740eb633035STom Caputi /*
741eb633035STom Caputi * If not otherwise specified, use the default number of
742eb633035STom Caputi * pbkdf2 iterations. If specified, we have already checked
743eb633035STom Caputi * that the given value is greater than MIN_PBKDF2_ITERATIONS
744eb633035STom Caputi * during zfs_valid_proplist().
745eb633035STom Caputi */
746eb633035STom Caputi ret = nvlist_lookup_uint64(props,
747eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);
748eb633035STom Caputi if (ret == ENOENT) {
749eb633035STom Caputi iters = DEFAULT_PBKDF2_ITERATIONS;
750eb633035STom Caputi ret = nvlist_add_uint64(props,
751eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), iters);
752eb633035STom Caputi if (ret != 0)
753eb633035STom Caputi goto error;
754eb633035STom Caputi } else if (ret != 0) {
755eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
756eb633035STom Caputi "Failed to get pbkdf2 iterations."));
757eb633035STom Caputi goto error;
758eb633035STom Caputi }
759eb633035STom Caputi } else {
760eb633035STom Caputi /* check that pbkdf2iters was not specified by the user */
761eb633035STom Caputi ret = nvlist_lookup_uint64(props,
762eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);
763eb633035STom Caputi if (ret == 0) {
764eb633035STom Caputi ret = EINVAL;
765eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
766eb633035STom Caputi "Cannot specify pbkdf2iters with a non-passphrase "
767eb633035STom Caputi "keyformat."));
768eb633035STom Caputi goto error;
769eb633035STom Caputi }
770eb633035STom Caputi }
771eb633035STom Caputi
772eb633035STom Caputi /* derive a key from the key material */
773eb633035STom Caputi ret = derive_key(hdl, (zfs_keyformat_t)keyformat, iters, key_material,
774eb633035STom Caputi key_material_len, salt, &key_data);
775eb633035STom Caputi if (ret != 0)
776eb633035STom Caputi goto error;
777eb633035STom Caputi
778eb633035STom Caputi free(key_material);
779eb633035STom Caputi
780eb633035STom Caputi *wkeydata = key_data;
781eb633035STom Caputi *wkeylen = WRAPPING_KEY_LEN;
782eb633035STom Caputi return (0);
783eb633035STom Caputi
784eb633035STom Caputi error:
785eb633035STom Caputi if (key_material != NULL)
786eb633035STom Caputi free(key_material);
787eb633035STom Caputi if (key_data != NULL)
788eb633035STom Caputi free(key_data);
789eb633035STom Caputi
790eb633035STom Caputi *wkeydata = NULL;
791eb633035STom Caputi *wkeylen = 0;
792eb633035STom Caputi return (ret);
793eb633035STom Caputi }
794eb633035STom Caputi
795eb633035STom Caputi static boolean_t
proplist_has_encryption_props(nvlist_t * props)796eb633035STom Caputi proplist_has_encryption_props(nvlist_t *props)
797eb633035STom Caputi {
798eb633035STom Caputi int ret;
799eb633035STom Caputi uint64_t intval;
800eb633035STom Caputi char *strval;
801eb633035STom Caputi
802eb633035STom Caputi ret = nvlist_lookup_uint64(props,
803eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &intval);
804eb633035STom Caputi if (ret == 0 && intval != ZIO_CRYPT_OFF)
805eb633035STom Caputi return (B_TRUE);
806eb633035STom Caputi
807eb633035STom Caputi ret = nvlist_lookup_string(props,
808eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &strval);
809eb633035STom Caputi if (ret == 0 && strcmp(strval, "none") != 0)
810eb633035STom Caputi return (B_TRUE);
811eb633035STom Caputi
812eb633035STom Caputi ret = nvlist_lookup_uint64(props,
813eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &intval);
814eb633035STom Caputi if (ret == 0)
815eb633035STom Caputi return (B_TRUE);
816eb633035STom Caputi
817eb633035STom Caputi ret = nvlist_lookup_uint64(props,
818eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &intval);
819eb633035STom Caputi if (ret == 0)
820eb633035STom Caputi return (B_TRUE);
821eb633035STom Caputi
822eb633035STom Caputi return (B_FALSE);
823eb633035STom Caputi }
824eb633035STom Caputi
825eb633035STom Caputi int
zfs_crypto_get_encryption_root(zfs_handle_t * zhp,boolean_t * is_encroot,char * buf)826eb633035STom Caputi zfs_crypto_get_encryption_root(zfs_handle_t *zhp, boolean_t *is_encroot,
827eb633035STom Caputi char *buf)
828eb633035STom Caputi {
829eb633035STom Caputi int ret;
830eb633035STom Caputi char prop_encroot[MAXNAMELEN];
831eb633035STom Caputi
832eb633035STom Caputi /* if the dataset isn't encrypted, just return */
833eb633035STom Caputi if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) == ZIO_CRYPT_OFF) {
834eb633035STom Caputi *is_encroot = B_FALSE;
835eb633035STom Caputi if (buf != NULL)
836eb633035STom Caputi buf[0] = '\0';
837eb633035STom Caputi return (0);
838eb633035STom Caputi }
839eb633035STom Caputi
840eb633035STom Caputi ret = zfs_prop_get(zhp, ZFS_PROP_ENCRYPTION_ROOT, prop_encroot,
841eb633035STom Caputi sizeof (prop_encroot), NULL, NULL, 0, B_TRUE);
842eb633035STom Caputi if (ret != 0) {
843eb633035STom Caputi *is_encroot = B_FALSE;
844eb633035STom Caputi if (buf != NULL)
845eb633035STom Caputi buf[0] = '\0';
846eb633035STom Caputi return (ret);
847eb633035STom Caputi }
848eb633035STom Caputi
849eb633035STom Caputi *is_encroot = strcmp(prop_encroot, zfs_get_name(zhp)) == 0;
850eb633035STom Caputi if (buf != NULL)
851eb633035STom Caputi (void) strcpy(buf, prop_encroot);
852eb633035STom Caputi
853eb633035STom Caputi return (0);
854eb633035STom Caputi }
855eb633035STom Caputi
856eb633035STom Caputi int
zfs_crypto_create(libzfs_handle_t * hdl,char * parent_name,nvlist_t * props,nvlist_t * pool_props,boolean_t stdin_available,uint8_t ** wkeydata_out,uint_t * wkeylen_out)857eb633035STom Caputi zfs_crypto_create(libzfs_handle_t *hdl, char *parent_name, nvlist_t *props,
8586ccda740Sloli nvlist_t *pool_props, boolean_t stdin_available, uint8_t **wkeydata_out,
8596ccda740Sloli uint_t *wkeylen_out)
860eb633035STom Caputi {
861eb633035STom Caputi int ret;
862eb633035STom Caputi uint64_t crypt = ZIO_CRYPT_INHERIT, pcrypt = ZIO_CRYPT_INHERIT;
863eb633035STom Caputi uint64_t keyformat = ZFS_KEYFORMAT_NONE;
864eb633035STom Caputi char *keylocation = NULL;
865eb633035STom Caputi zfs_handle_t *pzhp = NULL;
866eb633035STom Caputi uint8_t *wkeydata = NULL;
867eb633035STom Caputi uint_t wkeylen = 0;
868eb633035STom Caputi boolean_t local_crypt = B_TRUE;
869eb633035STom Caputi
870eb633035STom Caputi /* lookup crypt from props */
871eb633035STom Caputi ret = nvlist_lookup_uint64(props,
872eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &crypt);
873eb633035STom Caputi if (ret != 0)
874eb633035STom Caputi local_crypt = B_FALSE;
875eb633035STom Caputi
876eb633035STom Caputi /* lookup key location and format from props */
877eb633035STom Caputi (void) nvlist_lookup_uint64(props,
878eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);
879eb633035STom Caputi (void) nvlist_lookup_string(props,
880eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
881eb633035STom Caputi
882eb633035STom Caputi if (parent_name != NULL) {
883eb633035STom Caputi /* get a reference to parent dataset */
884eb633035STom Caputi pzhp = make_dataset_handle(hdl, parent_name);
885eb633035STom Caputi if (pzhp == NULL) {
886eb633035STom Caputi ret = ENOENT;
887eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
888eb633035STom Caputi "Failed to lookup parent."));
889eb633035STom Caputi goto out;
890eb633035STom Caputi }
891eb633035STom Caputi
892eb633035STom Caputi /* Lookup parent's crypt */
893eb633035STom Caputi pcrypt = zfs_prop_get_int(pzhp, ZFS_PROP_ENCRYPTION);
894eb633035STom Caputi
895eb633035STom Caputi /* Params require the encryption feature */
896eb633035STom Caputi if (!encryption_feature_is_enabled(pzhp->zpool_hdl)) {
897eb633035STom Caputi if (proplist_has_encryption_props(props)) {
898eb633035STom Caputi ret = EINVAL;
899eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
900eb633035STom Caputi "Encryption feature not enabled."));
901eb633035STom Caputi goto out;
902eb633035STom Caputi }
903eb633035STom Caputi
904eb633035STom Caputi ret = 0;
905eb633035STom Caputi goto out;
906eb633035STom Caputi }
907eb633035STom Caputi } else {
908eb633035STom Caputi /*
909eb633035STom Caputi * special case for root dataset where encryption feature
910eb633035STom Caputi * feature won't be on disk yet
911eb633035STom Caputi */
912eb633035STom Caputi if (!nvlist_exists(pool_props, "feature@encryption")) {
913eb633035STom Caputi if (proplist_has_encryption_props(props)) {
914eb633035STom Caputi ret = EINVAL;
915eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
916eb633035STom Caputi "Encryption feature not enabled."));
917eb633035STom Caputi goto out;
918eb633035STom Caputi }
919eb633035STom Caputi
920eb633035STom Caputi ret = 0;
921eb633035STom Caputi goto out;
922eb633035STom Caputi }
923eb633035STom Caputi
924eb633035STom Caputi pcrypt = ZIO_CRYPT_OFF;
925eb633035STom Caputi }
926eb633035STom Caputi
927eb633035STom Caputi /* Get the inherited encryption property if we don't have it locally */
928eb633035STom Caputi if (!local_crypt)
929eb633035STom Caputi crypt = pcrypt;
930eb633035STom Caputi
931eb633035STom Caputi /*
932eb633035STom Caputi * At this point crypt should be the actual encryption value. If
933eb633035STom Caputi * encryption is off just verify that no encryption properties have
934eb633035STom Caputi * been specified and return.
935eb633035STom Caputi */
936eb633035STom Caputi if (crypt == ZIO_CRYPT_OFF) {
937eb633035STom Caputi if (proplist_has_encryption_props(props)) {
938eb633035STom Caputi ret = EINVAL;
939eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
940eb633035STom Caputi "Encryption must be turned on to set encryption "
941eb633035STom Caputi "properties."));
942eb633035STom Caputi goto out;
943eb633035STom Caputi }
944eb633035STom Caputi
945eb633035STom Caputi ret = 0;
946eb633035STom Caputi goto out;
947eb633035STom Caputi }
948eb633035STom Caputi
949eb633035STom Caputi /*
950eb633035STom Caputi * If we have a parent crypt it is valid to specify encryption alone.
951eb633035STom Caputi * This will result in a child that is encrypted with the chosen
952eb633035STom Caputi * encryption suite that will also inherit the parent's key. If
953eb633035STom Caputi * the parent is not encrypted we need an encryption suite provided.
954eb633035STom Caputi */
955eb633035STom Caputi if (pcrypt == ZIO_CRYPT_OFF && keylocation == NULL &&
956eb633035STom Caputi keyformat == ZFS_KEYFORMAT_NONE) {
957eb633035STom Caputi ret = EINVAL;
958eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
959eb633035STom Caputi "Keyformat required for new encryption root."));
960eb633035STom Caputi goto out;
961eb633035STom Caputi }
962eb633035STom Caputi
963eb633035STom Caputi /*
964eb633035STom Caputi * Specifying a keylocation implies this will be a new encryption root.
965eb633035STom Caputi * Check that a keyformat is also specified.
966eb633035STom Caputi */
967eb633035STom Caputi if (keylocation != NULL && keyformat == ZFS_KEYFORMAT_NONE) {
968eb633035STom Caputi ret = EINVAL;
969eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
970eb633035STom Caputi "Keyformat required for new encryption root."));
971eb633035STom Caputi goto out;
972eb633035STom Caputi }
973eb633035STom Caputi
974eb633035STom Caputi /* default to prompt if no keylocation is specified */
975eb633035STom Caputi if (keyformat != ZFS_KEYFORMAT_NONE && keylocation == NULL) {
976eb633035STom Caputi keylocation = "prompt";
977eb633035STom Caputi ret = nvlist_add_string(props,
978eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION), keylocation);
979eb633035STom Caputi if (ret != 0)
980eb633035STom Caputi goto out;
981eb633035STom Caputi }
982eb633035STom Caputi
983eb633035STom Caputi /*
984eb633035STom Caputi * If a local key is provided, this dataset will be a new
985eb633035STom Caputi * encryption root. Populate the encryption params.
986eb633035STom Caputi */
987eb633035STom Caputi if (keylocation != NULL) {
9886ccda740Sloli /*
9896ccda740Sloli * 'zfs recv -o keylocation=prompt' won't work because stdin
9906ccda740Sloli * is being used by the send stream, so we disallow it.
9916ccda740Sloli */
9926ccda740Sloli if (!stdin_available && strcmp(keylocation, "prompt") == 0) {
9936ccda740Sloli ret = EINVAL;
9946ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Cannot use "
9956ccda740Sloli "'prompt' keylocation because stdin is in use."));
9966ccda740Sloli goto out;
9976ccda740Sloli }
9986ccda740Sloli
999eb633035STom Caputi ret = populate_create_encryption_params_nvlists(hdl, NULL,
1000eb633035STom Caputi B_FALSE, keyformat, keylocation, props, &wkeydata,
1001eb633035STom Caputi &wkeylen);
1002eb633035STom Caputi if (ret != 0)
1003eb633035STom Caputi goto out;
1004eb633035STom Caputi }
1005eb633035STom Caputi
1006eb633035STom Caputi if (pzhp != NULL)
1007eb633035STom Caputi zfs_close(pzhp);
1008eb633035STom Caputi
1009eb633035STom Caputi *wkeydata_out = wkeydata;
1010eb633035STom Caputi *wkeylen_out = wkeylen;
1011eb633035STom Caputi return (0);
1012eb633035STom Caputi
1013eb633035STom Caputi out:
1014eb633035STom Caputi if (pzhp != NULL)
1015eb633035STom Caputi zfs_close(pzhp);
1016eb633035STom Caputi if (wkeydata != NULL)
1017eb633035STom Caputi free(wkeydata);
1018eb633035STom Caputi
1019eb633035STom Caputi *wkeydata_out = NULL;
1020eb633035STom Caputi *wkeylen_out = 0;
1021eb633035STom Caputi return (ret);
1022eb633035STom Caputi }
1023eb633035STom Caputi
1024eb633035STom Caputi int
zfs_crypto_clone_check(libzfs_handle_t * hdl,zfs_handle_t * origin_zhp,char * parent_name,nvlist_t * props)1025eb633035STom Caputi zfs_crypto_clone_check(libzfs_handle_t *hdl, zfs_handle_t *origin_zhp,
1026eb633035STom Caputi char *parent_name, nvlist_t *props)
1027eb633035STom Caputi {
1028eb633035STom Caputi /*
1029eb633035STom Caputi * No encryption properties should be specified. They will all be
1030eb633035STom Caputi * inherited from the origin dataset.
1031eb633035STom Caputi */
1032eb633035STom Caputi if (nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_KEYFORMAT)) ||
1033eb633035STom Caputi nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_KEYLOCATION)) ||
1034eb633035STom Caputi nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_ENCRYPTION)) ||
1035eb633035STom Caputi nvlist_exists(props, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS))) {
1036eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1037eb633035STom Caputi "Encryption properties must inherit from origin dataset."));
1038a60ca23dSTom Caputi return (EINVAL);
1039eb633035STom Caputi }
1040eb633035STom Caputi
1041eb633035STom Caputi return (0);
1042eb633035STom Caputi }
1043eb633035STom Caputi
1044eb633035STom Caputi typedef struct loadkeys_cbdata {
1045eb633035STom Caputi uint64_t cb_numfailed;
1046eb633035STom Caputi uint64_t cb_numattempted;
1047eb633035STom Caputi } loadkey_cbdata_t;
1048eb633035STom Caputi
1049eb633035STom Caputi static int
load_keys_cb(zfs_handle_t * zhp,void * arg)1050eb633035STom Caputi load_keys_cb(zfs_handle_t *zhp, void *arg)
1051eb633035STom Caputi {
1052eb633035STom Caputi int ret;
1053eb633035STom Caputi boolean_t is_encroot;
1054eb633035STom Caputi loadkey_cbdata_t *cb = arg;
1055eb633035STom Caputi uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1056eb633035STom Caputi
1057eb633035STom Caputi /* only attempt to load keys for encryption roots */
1058eb633035STom Caputi ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
1059eb633035STom Caputi if (ret != 0 || !is_encroot)
1060eb633035STom Caputi goto out;
1061eb633035STom Caputi
1062eb633035STom Caputi /* don't attempt to load already loaded keys */
1063eb633035STom Caputi if (keystatus == ZFS_KEYSTATUS_AVAILABLE)
1064eb633035STom Caputi goto out;
1065eb633035STom Caputi
1066eb633035STom Caputi /* Attempt to load the key. Record status in cb. */
1067eb633035STom Caputi cb->cb_numattempted++;
1068eb633035STom Caputi
1069eb633035STom Caputi ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
1070eb633035STom Caputi if (ret)
1071eb633035STom Caputi cb->cb_numfailed++;
1072eb633035STom Caputi
1073eb633035STom Caputi out:
1074eb633035STom Caputi (void) zfs_iter_filesystems(zhp, load_keys_cb, cb);
1075eb633035STom Caputi zfs_close(zhp);
1076eb633035STom Caputi
1077eb633035STom Caputi /* always return 0, since this function is best effort */
1078eb633035STom Caputi return (0);
1079eb633035STom Caputi }
1080eb633035STom Caputi
1081eb633035STom Caputi /*
1082eb633035STom Caputi * This function is best effort. It attempts to load all the keys for the given
1083eb633035STom Caputi * filesystem and all of its children.
1084eb633035STom Caputi */
1085eb633035STom Caputi int
zfs_crypto_attempt_load_keys(libzfs_handle_t * hdl,char * fsname)1086eb633035STom Caputi zfs_crypto_attempt_load_keys(libzfs_handle_t *hdl, char *fsname)
1087eb633035STom Caputi {
1088eb633035STom Caputi int ret;
1089eb633035STom Caputi zfs_handle_t *zhp = NULL;
1090eb633035STom Caputi loadkey_cbdata_t cb = { 0 };
1091eb633035STom Caputi
1092eb633035STom Caputi zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1093eb633035STom Caputi if (zhp == NULL) {
1094eb633035STom Caputi ret = ENOENT;
1095eb633035STom Caputi goto error;
1096eb633035STom Caputi }
1097eb633035STom Caputi
1098eb633035STom Caputi ret = load_keys_cb(zfs_handle_dup(zhp), &cb);
1099eb633035STom Caputi if (ret)
1100eb633035STom Caputi goto error;
1101eb633035STom Caputi
1102eb633035STom Caputi (void) printf(gettext("%llu / %llu keys successfully loaded\n"),
1103eb633035STom Caputi (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
1104eb633035STom Caputi (u_longlong_t)cb.cb_numattempted);
1105eb633035STom Caputi
1106eb633035STom Caputi if (cb.cb_numfailed != 0) {
1107eb633035STom Caputi ret = -1;
1108eb633035STom Caputi goto error;
1109eb633035STom Caputi }
1110eb633035STom Caputi
1111eb633035STom Caputi zfs_close(zhp);
1112eb633035STom Caputi return (0);
1113eb633035STom Caputi
1114eb633035STom Caputi error:
1115eb633035STom Caputi if (zhp != NULL)
1116eb633035STom Caputi zfs_close(zhp);
1117eb633035STom Caputi return (ret);
1118eb633035STom Caputi }
1119eb633035STom Caputi
1120eb633035STom Caputi int
zfs_crypto_load_key(zfs_handle_t * zhp,boolean_t noop,char * alt_keylocation)1121eb633035STom Caputi zfs_crypto_load_key(zfs_handle_t *zhp, boolean_t noop, char *alt_keylocation)
1122eb633035STom Caputi {
1123eb633035STom Caputi int ret, attempts = 0;
1124eb633035STom Caputi char errbuf[1024];
1125eb633035STom Caputi uint64_t keystatus, iters = 0, salt = 0;
1126eb633035STom Caputi uint64_t keyformat = ZFS_KEYFORMAT_NONE;
1127eb633035STom Caputi char prop_keylocation[MAXNAMELEN];
1128eb633035STom Caputi char prop_encroot[MAXNAMELEN];
1129eb633035STom Caputi char *keylocation = NULL;
1130eb633035STom Caputi uint8_t *key_material = NULL, *key_data = NULL;
1131eb633035STom Caputi size_t key_material_len;
1132eb633035STom Caputi boolean_t is_encroot, can_retry = B_FALSE, correctible = B_FALSE;
1133eb633035STom Caputi
1134eb633035STom Caputi (void) snprintf(errbuf, sizeof (errbuf),
1135eb633035STom Caputi dgettext(TEXT_DOMAIN, "Key load error"));
1136eb633035STom Caputi
1137eb633035STom Caputi /* check that encryption is enabled for the pool */
1138eb633035STom Caputi if (!encryption_feature_is_enabled(zhp->zpool_hdl)) {
1139eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1140eb633035STom Caputi "Encryption feature not enabled."));
1141eb633035STom Caputi ret = EINVAL;
1142eb633035STom Caputi goto error;
1143eb633035STom Caputi }
1144eb633035STom Caputi
1145eb633035STom Caputi /* Fetch the keyformat. Check that the dataset is encrypted. */
1146eb633035STom Caputi keyformat = zfs_prop_get_int(zhp, ZFS_PROP_KEYFORMAT);
1147eb633035STom Caputi if (keyformat == ZFS_KEYFORMAT_NONE) {
1148eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1149eb633035STom Caputi "'%s' is not encrypted."), zfs_get_name(zhp));
1150eb633035STom Caputi ret = EINVAL;
1151eb633035STom Caputi goto error;
1152eb633035STom Caputi }
1153eb633035STom Caputi
1154eb633035STom Caputi /*
1155eb633035STom Caputi * Fetch the key location. Check that we are working with an
1156eb633035STom Caputi * encryption root.
1157eb633035STom Caputi */
1158eb633035STom Caputi ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, prop_encroot);
1159eb633035STom Caputi if (ret != 0) {
1160eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1161eb633035STom Caputi "Failed to get encryption root for '%s'."),
1162eb633035STom Caputi zfs_get_name(zhp));
1163eb633035STom Caputi goto error;
1164eb633035STom Caputi } else if (!is_encroot) {
1165eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1166eb633035STom Caputi "Keys must be loaded for encryption root of '%s' (%s)."),
1167eb633035STom Caputi zfs_get_name(zhp), prop_encroot);
1168eb633035STom Caputi ret = EINVAL;
1169eb633035STom Caputi goto error;
1170eb633035STom Caputi }
1171eb633035STom Caputi
1172eb633035STom Caputi /*
1173eb633035STom Caputi * if the caller has elected to override the keylocation property
1174eb633035STom Caputi * use that instead
1175eb633035STom Caputi */
1176eb633035STom Caputi if (alt_keylocation != NULL) {
1177eb633035STom Caputi keylocation = alt_keylocation;
1178eb633035STom Caputi } else {
1179eb633035STom Caputi ret = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION, prop_keylocation,
1180eb633035STom Caputi sizeof (prop_keylocation), NULL, NULL, 0, B_TRUE);
1181eb633035STom Caputi if (ret != 0) {
1182eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1183eb633035STom Caputi "Failed to get keylocation for '%s'."),
1184eb633035STom Caputi zfs_get_name(zhp));
1185eb633035STom Caputi goto error;
1186eb633035STom Caputi }
1187eb633035STom Caputi
1188eb633035STom Caputi keylocation = prop_keylocation;
1189eb633035STom Caputi }
1190eb633035STom Caputi
1191eb633035STom Caputi /* check that the key is unloaded unless this is a noop */
1192eb633035STom Caputi if (!noop) {
1193eb633035STom Caputi keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1194eb633035STom Caputi if (keystatus == ZFS_KEYSTATUS_AVAILABLE) {
1195eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1196eb633035STom Caputi "Key already loaded for '%s'."), zfs_get_name(zhp));
1197eb633035STom Caputi ret = EEXIST;
1198eb633035STom Caputi goto error;
1199eb633035STom Caputi }
1200eb633035STom Caputi }
1201eb633035STom Caputi
1202eb633035STom Caputi /* passphrase formats require a salt and pbkdf2_iters property */
1203eb633035STom Caputi if (keyformat == ZFS_KEYFORMAT_PASSPHRASE) {
1204eb633035STom Caputi salt = zfs_prop_get_int(zhp, ZFS_PROP_PBKDF2_SALT);
1205eb633035STom Caputi iters = zfs_prop_get_int(zhp, ZFS_PROP_PBKDF2_ITERS);
1206eb633035STom Caputi }
1207eb633035STom Caputi
1208eb633035STom Caputi try_again:
1209eb633035STom Caputi /* fetching and deriving the key are correctible errors. set the flag */
1210eb633035STom Caputi correctible = B_TRUE;
1211eb633035STom Caputi
1212eb633035STom Caputi /* get key material from key format and location */
1213eb633035STom Caputi ret = get_key_material(zhp->zfs_hdl, B_FALSE, B_FALSE, keyformat,
1214eb633035STom Caputi keylocation, zfs_get_name(zhp), &key_material, &key_material_len,
1215eb633035STom Caputi &can_retry);
1216eb633035STom Caputi if (ret != 0)
1217eb633035STom Caputi goto error;
1218eb633035STom Caputi
1219eb633035STom Caputi /* derive a key from the key material */
1220eb633035STom Caputi ret = derive_key(zhp->zfs_hdl, keyformat, iters, key_material,
1221eb633035STom Caputi key_material_len, salt, &key_data);
1222eb633035STom Caputi if (ret != 0)
1223eb633035STom Caputi goto error;
1224eb633035STom Caputi
1225eb633035STom Caputi correctible = B_FALSE;
1226eb633035STom Caputi
1227eb633035STom Caputi /* pass the wrapping key and noop flag to the ioctl */
1228eb633035STom Caputi ret = lzc_load_key(zhp->zfs_name, noop, key_data, WRAPPING_KEY_LEN);
1229eb633035STom Caputi if (ret != 0) {
1230eb633035STom Caputi switch (ret) {
1231eb633035STom Caputi case EINVAL:
1232eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1233eb633035STom Caputi "Invalid parameters provided for %s."),
1234eb633035STom Caputi zfs_get_name(zhp));
1235eb633035STom Caputi break;
1236eb633035STom Caputi case EEXIST:
1237eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1238eb633035STom Caputi "Key already loaded for '%s'."), zfs_get_name(zhp));
1239eb633035STom Caputi break;
1240eb633035STom Caputi case EBUSY:
1241eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1242eb633035STom Caputi "'%s' is busy."), zfs_get_name(zhp));
1243eb633035STom Caputi break;
1244eb633035STom Caputi case EACCES:
1245eb633035STom Caputi correctible = B_TRUE;
1246eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1247eb633035STom Caputi "Incorrect key provided for '%s'."),
1248eb633035STom Caputi zfs_get_name(zhp));
1249eb633035STom Caputi break;
1250eb633035STom Caputi }
1251eb633035STom Caputi goto error;
1252eb633035STom Caputi }
1253eb633035STom Caputi
1254eb633035STom Caputi free(key_material);
1255eb633035STom Caputi free(key_data);
1256eb633035STom Caputi
1257eb633035STom Caputi return (0);
1258eb633035STom Caputi
1259eb633035STom Caputi error:
1260eb633035STom Caputi (void) zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1261eb633035STom Caputi if (key_material != NULL) {
1262eb633035STom Caputi free(key_material);
1263eb633035STom Caputi key_material = NULL;
1264eb633035STom Caputi }
1265eb633035STom Caputi if (key_data != NULL) {
1266eb633035STom Caputi free(key_data);
1267eb633035STom Caputi key_data = NULL;
1268eb633035STom Caputi }
1269eb633035STom Caputi
1270eb633035STom Caputi /*
1271eb633035STom Caputi * Here we decide if it is ok to allow the user to retry entering their
1272eb633035STom Caputi * key. The can_retry flag will be set if the user is entering their
1273eb633035STom Caputi * key from an interactive prompt. The correctible flag will only be
1274eb633035STom Caputi * set if an error that occured could be corrected by retrying. Both
1275eb633035STom Caputi * flags are needed to allow the user to attempt key entry again
1276eb633035STom Caputi */
1277eb633035STom Caputi if (can_retry && correctible && attempts <= MAX_KEY_PROMPT_ATTEMPTS) {
1278eb633035STom Caputi attempts++;
1279eb633035STom Caputi goto try_again;
1280eb633035STom Caputi }
1281eb633035STom Caputi
1282eb633035STom Caputi return (ret);
1283eb633035STom Caputi }
1284eb633035STom Caputi
1285eb633035STom Caputi int
zfs_crypto_unload_key(zfs_handle_t * zhp)1286eb633035STom Caputi zfs_crypto_unload_key(zfs_handle_t *zhp)
1287eb633035STom Caputi {
1288eb633035STom Caputi int ret;
1289eb633035STom Caputi char errbuf[1024];
1290eb633035STom Caputi char prop_encroot[MAXNAMELEN];
1291eb633035STom Caputi uint64_t keystatus, keyformat;
1292eb633035STom Caputi boolean_t is_encroot;
1293eb633035STom Caputi
1294eb633035STom Caputi (void) snprintf(errbuf, sizeof (errbuf),
1295eb633035STom Caputi dgettext(TEXT_DOMAIN, "Key unload error"));
1296eb633035STom Caputi
1297eb633035STom Caputi /* check that encryption is enabled for the pool */
1298eb633035STom Caputi if (!encryption_feature_is_enabled(zhp->zpool_hdl)) {
1299eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1300eb633035STom Caputi "Encryption feature not enabled."));
1301eb633035STom Caputi ret = EINVAL;
1302eb633035STom Caputi goto error;
1303eb633035STom Caputi }
1304eb633035STom Caputi
1305eb633035STom Caputi /* Fetch the keyformat. Check that the dataset is encrypted. */
1306eb633035STom Caputi keyformat = zfs_prop_get_int(zhp, ZFS_PROP_KEYFORMAT);
1307eb633035STom Caputi if (keyformat == ZFS_KEYFORMAT_NONE) {
1308eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1309eb633035STom Caputi "'%s' is not encrypted."), zfs_get_name(zhp));
1310eb633035STom Caputi ret = EINVAL;
1311eb633035STom Caputi goto error;
1312eb633035STom Caputi }
1313eb633035STom Caputi
1314eb633035STom Caputi /*
1315eb633035STom Caputi * Fetch the key location. Check that we are working with an
1316eb633035STom Caputi * encryption root.
1317eb633035STom Caputi */
1318eb633035STom Caputi ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, prop_encroot);
1319eb633035STom Caputi if (ret != 0) {
1320eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1321eb633035STom Caputi "Failed to get encryption root for '%s'."),
1322eb633035STom Caputi zfs_get_name(zhp));
1323eb633035STom Caputi goto error;
1324eb633035STom Caputi } else if (!is_encroot) {
1325eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1326eb633035STom Caputi "Keys must be unloaded for encryption root of '%s' (%s)."),
1327eb633035STom Caputi zfs_get_name(zhp), prop_encroot);
1328eb633035STom Caputi ret = EINVAL;
1329eb633035STom Caputi goto error;
1330eb633035STom Caputi }
1331eb633035STom Caputi
1332eb633035STom Caputi /* check that the key is loaded */
1333eb633035STom Caputi keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1334eb633035STom Caputi if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
1335eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1336eb633035STom Caputi "Key already unloaded for '%s'."), zfs_get_name(zhp));
1337eb633035STom Caputi ret = EACCES;
1338eb633035STom Caputi goto error;
1339eb633035STom Caputi }
1340eb633035STom Caputi
1341eb633035STom Caputi /* call the ioctl */
1342eb633035STom Caputi ret = lzc_unload_key(zhp->zfs_name);
1343eb633035STom Caputi
1344eb633035STom Caputi if (ret != 0) {
1345eb633035STom Caputi switch (ret) {
1346eb633035STom Caputi case EACCES:
1347eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1348eb633035STom Caputi "Key already unloaded for '%s'."),
1349eb633035STom Caputi zfs_get_name(zhp));
1350eb633035STom Caputi break;
1351eb633035STom Caputi case EBUSY:
1352eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1353eb633035STom Caputi "'%s' is busy."), zfs_get_name(zhp));
1354eb633035STom Caputi break;
1355eb633035STom Caputi }
1356eb633035STom Caputi (void) zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1357eb633035STom Caputi }
1358eb633035STom Caputi
1359eb633035STom Caputi return (ret);
1360eb633035STom Caputi
1361eb633035STom Caputi error:
1362eb633035STom Caputi (void) zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1363eb633035STom Caputi return (ret);
1364eb633035STom Caputi }
1365eb633035STom Caputi
1366eb633035STom Caputi static int
zfs_crypto_verify_rewrap_nvlist(zfs_handle_t * zhp,nvlist_t * props,nvlist_t ** props_out,char * errbuf)1367eb633035STom Caputi zfs_crypto_verify_rewrap_nvlist(zfs_handle_t *zhp, nvlist_t *props,
1368eb633035STom Caputi nvlist_t **props_out, char *errbuf)
1369eb633035STom Caputi {
1370eb633035STom Caputi int ret;
1371eb633035STom Caputi nvpair_t *elem = NULL;
1372eb633035STom Caputi zfs_prop_t prop;
1373eb633035STom Caputi nvlist_t *new_props = NULL;
1374eb633035STom Caputi
1375eb633035STom Caputi new_props = fnvlist_alloc();
1376eb633035STom Caputi
1377eb633035STom Caputi /*
1378eb633035STom Caputi * loop through all provided properties, we should only have
1379eb633035STom Caputi * keyformat, keylocation and pbkdf2iters. The actual validation of
1380eb633035STom Caputi * values is done by zfs_valid_proplist().
1381eb633035STom Caputi */
1382eb633035STom Caputi while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
1383eb633035STom Caputi const char *propname = nvpair_name(elem);
1384eb633035STom Caputi prop = zfs_name_to_prop(propname);
1385eb633035STom Caputi
1386eb633035STom Caputi switch (prop) {
1387eb633035STom Caputi case ZFS_PROP_PBKDF2_ITERS:
1388eb633035STom Caputi case ZFS_PROP_KEYFORMAT:
1389eb633035STom Caputi case ZFS_PROP_KEYLOCATION:
1390eb633035STom Caputi break;
1391eb633035STom Caputi default:
1392eb633035STom Caputi ret = EINVAL;
1393eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1394eb633035STom Caputi "Only keyformat, keylocation and pbkdf2iters may "
1395eb633035STom Caputi "be set with this command."));
1396eb633035STom Caputi goto error;
1397eb633035STom Caputi }
1398eb633035STom Caputi }
1399eb633035STom Caputi
1400eb633035STom Caputi new_props = zfs_valid_proplist(zhp->zfs_hdl, zhp->zfs_type, props,
1401eb633035STom Caputi zfs_prop_get_int(zhp, ZFS_PROP_ZONED), NULL, zhp->zpool_hdl,
1402eb633035STom Caputi B_TRUE, errbuf);
1403eb633035STom Caputi if (new_props == NULL)
1404eb633035STom Caputi goto error;
1405eb633035STom Caputi
1406eb633035STom Caputi *props_out = new_props;
1407eb633035STom Caputi return (0);
1408eb633035STom Caputi
1409eb633035STom Caputi error:
1410eb633035STom Caputi nvlist_free(new_props);
1411eb633035STom Caputi *props_out = NULL;
1412eb633035STom Caputi return (ret);
1413eb633035STom Caputi }
1414eb633035STom Caputi
1415eb633035STom Caputi int
zfs_crypto_rewrap(zfs_handle_t * zhp,nvlist_t * raw_props,boolean_t inheritkey)1416eb633035STom Caputi zfs_crypto_rewrap(zfs_handle_t *zhp, nvlist_t *raw_props, boolean_t inheritkey)
1417eb633035STom Caputi {
1418eb633035STom Caputi int ret;
1419eb633035STom Caputi char errbuf[1024];
1420eb633035STom Caputi boolean_t is_encroot;
1421eb633035STom Caputi nvlist_t *props = NULL;
1422eb633035STom Caputi uint8_t *wkeydata = NULL;
1423eb633035STom Caputi uint_t wkeylen = 0;
1424eb633035STom Caputi dcp_cmd_t cmd = (inheritkey) ? DCP_CMD_INHERIT : DCP_CMD_NEW_KEY;
1425eb633035STom Caputi uint64_t crypt, pcrypt, keystatus, pkeystatus;
1426eb633035STom Caputi uint64_t keyformat = ZFS_KEYFORMAT_NONE;
1427eb633035STom Caputi zfs_handle_t *pzhp = NULL;
1428eb633035STom Caputi char *keylocation = NULL;
1429eb633035STom Caputi char origin_name[MAXNAMELEN];
1430eb633035STom Caputi char prop_keylocation[MAXNAMELEN];
1431eb633035STom Caputi char parent_name[ZFS_MAX_DATASET_NAME_LEN];
1432eb633035STom Caputi
1433eb633035STom Caputi (void) snprintf(errbuf, sizeof (errbuf),
1434eb633035STom Caputi dgettext(TEXT_DOMAIN, "Key change error"));
1435eb633035STom Caputi
1436eb633035STom Caputi /* check that encryption is enabled for the pool */
1437eb633035STom Caputi if (!encryption_feature_is_enabled(zhp->zpool_hdl)) {
1438eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1439eb633035STom Caputi "Encryption feature not enabled."));
1440eb633035STom Caputi ret = EINVAL;
1441eb633035STom Caputi goto error;
1442eb633035STom Caputi }
1443eb633035STom Caputi
1444eb633035STom Caputi /* get crypt from dataset */
1445eb633035STom Caputi crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1446eb633035STom Caputi if (crypt == ZIO_CRYPT_OFF) {
1447eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1448eb633035STom Caputi "Dataset not encrypted."));
1449eb633035STom Caputi ret = EINVAL;
1450eb633035STom Caputi goto error;
1451eb633035STom Caputi }
1452eb633035STom Caputi
1453eb633035STom Caputi /* get the encryption root of the dataset */
1454eb633035STom Caputi ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
1455eb633035STom Caputi if (ret != 0) {
1456eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1457eb633035STom Caputi "Failed to get encryption root for '%s'."),
1458eb633035STom Caputi zfs_get_name(zhp));
1459eb633035STom Caputi goto error;
1460eb633035STom Caputi }
1461eb633035STom Caputi
1462eb633035STom Caputi /* Clones use their origin's key and cannot rewrap it */
1463eb633035STom Caputi ret = zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin_name,
1464eb633035STom Caputi sizeof (origin_name), NULL, NULL, 0, B_TRUE);
1465eb633035STom Caputi if (ret == 0 && strcmp(origin_name, "") != 0) {
1466eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1467eb633035STom Caputi "Keys cannot be changed on clones."));
1468eb633035STom Caputi ret = EINVAL;
1469eb633035STom Caputi goto error;
1470eb633035STom Caputi }
1471eb633035STom Caputi
1472eb633035STom Caputi /*
1473eb633035STom Caputi * If the user wants to use the inheritkey variant of this function
1474eb633035STom Caputi * we don't need to collect any crypto arguments.
1475eb633035STom Caputi */
1476eb633035STom Caputi if (!inheritkey) {
1477eb633035STom Caputi /* validate the provided properties */
1478eb633035STom Caputi ret = zfs_crypto_verify_rewrap_nvlist(zhp, raw_props, &props,
1479eb633035STom Caputi errbuf);
1480eb633035STom Caputi if (ret != 0)
1481eb633035STom Caputi goto error;
1482eb633035STom Caputi
1483eb633035STom Caputi /*
1484eb633035STom Caputi * Load keyformat and keylocation from the nvlist. Fetch from
1485eb633035STom Caputi * the dataset properties if not specified.
1486eb633035STom Caputi */
1487eb633035STom Caputi (void) nvlist_lookup_uint64(props,
1488eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);
1489eb633035STom Caputi (void) nvlist_lookup_string(props,
1490eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
1491eb633035STom Caputi
1492eb633035STom Caputi if (is_encroot) {
1493eb633035STom Caputi /*
1494eb633035STom Caputi * If this is already an ecryption root, just keep
1495eb633035STom Caputi * any properties not set by the user.
1496eb633035STom Caputi */
1497eb633035STom Caputi if (keyformat == ZFS_KEYFORMAT_NONE) {
1498eb633035STom Caputi keyformat = zfs_prop_get_int(zhp,
1499eb633035STom Caputi ZFS_PROP_KEYFORMAT);
1500eb633035STom Caputi ret = nvlist_add_uint64(props,
1501eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYFORMAT),
1502eb633035STom Caputi keyformat);
1503eb633035STom Caputi }
1504eb633035STom Caputi
1505eb633035STom Caputi if (keylocation == NULL) {
1506eb633035STom Caputi ret = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
1507eb633035STom Caputi prop_keylocation, sizeof (prop_keylocation),
1508eb633035STom Caputi NULL, NULL, 0, B_TRUE);
1509eb633035STom Caputi if (ret != 0) {
1510eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl,
1511eb633035STom Caputi dgettext(TEXT_DOMAIN, "Failed to "
1512eb633035STom Caputi "get existing keylocation "
1513eb633035STom Caputi "property."));
1514eb633035STom Caputi goto error;
1515eb633035STom Caputi }
1516eb633035STom Caputi
1517eb633035STom Caputi keylocation = prop_keylocation;
1518eb633035STom Caputi }
1519eb633035STom Caputi } else {
1520eb633035STom Caputi /* need a new key for non-encryption roots */
1521eb633035STom Caputi if (keyformat == ZFS_KEYFORMAT_NONE) {
1522eb633035STom Caputi ret = EINVAL;
1523eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl,
1524eb633035STom Caputi dgettext(TEXT_DOMAIN, "Keyformat required "
1525eb633035STom Caputi "for new encryption root."));
1526eb633035STom Caputi goto error;
1527eb633035STom Caputi }
1528eb633035STom Caputi
1529eb633035STom Caputi /* default to prompt if no keylocation is specified */
1530eb633035STom Caputi if (keylocation == NULL) {
1531eb633035STom Caputi keylocation = "prompt";
1532eb633035STom Caputi ret = nvlist_add_string(props,
1533eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1534eb633035STom Caputi keylocation);
1535eb633035STom Caputi if (ret != 0)
1536eb633035STom Caputi goto error;
1537eb633035STom Caputi }
1538eb633035STom Caputi }
1539eb633035STom Caputi
1540eb633035STom Caputi /* fetch the new wrapping key and associated properties */
1541eb633035STom Caputi ret = populate_create_encryption_params_nvlists(zhp->zfs_hdl,
1542eb633035STom Caputi zhp, B_TRUE, keyformat, keylocation, props, &wkeydata,
1543eb633035STom Caputi &wkeylen);
1544eb633035STom Caputi if (ret != 0)
1545eb633035STom Caputi goto error;
1546eb633035STom Caputi } else {
1547eb633035STom Caputi /* check that zhp is an encryption root */
1548eb633035STom Caputi if (!is_encroot) {
1549eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1550eb633035STom Caputi "Key inheriting can only be performed on "
1551eb633035STom Caputi "encryption roots."));
1552eb633035STom Caputi ret = EINVAL;
1553eb633035STom Caputi goto error;
1554eb633035STom Caputi }
1555eb633035STom Caputi
1556eb633035STom Caputi /* get the parent's name */
1557eb633035STom Caputi ret = zfs_parent_name(zhp, parent_name, sizeof (parent_name));
1558eb633035STom Caputi if (ret != 0) {
1559eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1560eb633035STom Caputi "Root dataset cannot inherit key."));
1561eb633035STom Caputi ret = EINVAL;
1562eb633035STom Caputi goto error;
1563eb633035STom Caputi }
1564eb633035STom Caputi
1565eb633035STom Caputi /* get a handle to the parent */
1566eb633035STom Caputi pzhp = make_dataset_handle(zhp->zfs_hdl, parent_name);
1567eb633035STom Caputi if (pzhp == NULL) {
1568eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1569eb633035STom Caputi "Failed to lookup parent."));
1570eb633035STom Caputi ret = ENOENT;
1571eb633035STom Caputi goto error;
1572eb633035STom Caputi }
1573eb633035STom Caputi
1574eb633035STom Caputi /* parent must be encrypted */
1575eb633035STom Caputi pcrypt = zfs_prop_get_int(pzhp, ZFS_PROP_ENCRYPTION);
1576eb633035STom Caputi if (pcrypt == ZIO_CRYPT_OFF) {
1577eb633035STom Caputi zfs_error_aux(pzhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1578eb633035STom Caputi "Parent must be encrypted."));
1579eb633035STom Caputi ret = EINVAL;
1580eb633035STom Caputi goto error;
1581eb633035STom Caputi }
1582eb633035STom Caputi
1583eb633035STom Caputi /* check that the parent's key is loaded */
1584eb633035STom Caputi pkeystatus = zfs_prop_get_int(pzhp, ZFS_PROP_KEYSTATUS);
1585eb633035STom Caputi if (pkeystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
1586eb633035STom Caputi zfs_error_aux(pzhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1587eb633035STom Caputi "Parent key must be loaded."));
1588eb633035STom Caputi ret = EACCES;
1589eb633035STom Caputi goto error;
1590eb633035STom Caputi }
1591eb633035STom Caputi }
1592eb633035STom Caputi
1593eb633035STom Caputi /* check that the key is loaded */
1594eb633035STom Caputi keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
1595eb633035STom Caputi if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
1596eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1597eb633035STom Caputi "Key must be loaded."));
1598eb633035STom Caputi ret = EACCES;
1599eb633035STom Caputi goto error;
1600eb633035STom Caputi }
1601eb633035STom Caputi
1602eb633035STom Caputi /* call the ioctl */
1603eb633035STom Caputi ret = lzc_change_key(zhp->zfs_name, cmd, props, wkeydata, wkeylen);
1604eb633035STom Caputi if (ret != 0) {
1605eb633035STom Caputi switch (ret) {
1606eb633035STom Caputi case EINVAL:
1607eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1608eb633035STom Caputi "Invalid properties for key change."));
1609eb633035STom Caputi break;
1610eb633035STom Caputi case EACCES:
1611eb633035STom Caputi zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1612eb633035STom Caputi "Key is not currently loaded."));
1613eb633035STom Caputi break;
1614eb633035STom Caputi }
1615eb633035STom Caputi (void) zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1616eb633035STom Caputi }
1617eb633035STom Caputi
1618eb633035STom Caputi if (pzhp != NULL)
1619eb633035STom Caputi zfs_close(pzhp);
1620eb633035STom Caputi if (props != NULL)
1621eb633035STom Caputi nvlist_free(props);
1622eb633035STom Caputi if (wkeydata != NULL)
1623eb633035STom Caputi free(wkeydata);
1624eb633035STom Caputi
1625eb633035STom Caputi return (ret);
1626eb633035STom Caputi
1627eb633035STom Caputi error:
1628eb633035STom Caputi if (pzhp != NULL)
1629eb633035STom Caputi zfs_close(pzhp);
1630eb633035STom Caputi if (props != NULL)
1631eb633035STom Caputi nvlist_free(props);
1632eb633035STom Caputi if (wkeydata != NULL)
1633eb633035STom Caputi free(wkeydata);
1634eb633035STom Caputi
1635eb633035STom Caputi (void) zfs_error(zhp->zfs_hdl, EZFS_CRYPTOFAILED, errbuf);
1636eb633035STom Caputi return (ret);
1637eb633035STom Caputi }
1638