]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_crypt.c
Add wrapper for Linux BLKFLSBUF ioctl
[mirror_zfs.git] / module / zfs / dsl_crypt.c
CommitLineData
b5256303
TC
1/*
2 * CDDL HEADER START
3 *
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
7 * 1.0 of the CDDL.
8 *
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.
12 *
13 * CDDL HEADER END
14 */
15
16/*
17 * Copyright (c) 2017, Datto, Inc. All rights reserved.
d52d80b7 18 * Copyright (c) 2018 by Delphix. All rights reserved.
b5256303
TC
19 */
20
21#include <sys/dsl_crypt.h>
22#include <sys/dsl_pool.h>
23#include <sys/zap.h>
24#include <sys/zil.h>
25#include <sys/dsl_dir.h>
26#include <sys/dsl_prop.h>
27#include <sys/spa_impl.h>
28#include <sys/dmu_objset.h>
29#include <sys/zvol.h>
30
31/*
32 * This file's primary purpose is for managing master encryption keys in
33 * memory and on disk. For more info on how these keys are used, see the
34 * block comment in zio_crypt.c.
35 *
36 * All master keys are stored encrypted on disk in the form of the DSL
37 * Crypto Key ZAP object. The binary key data in this object is always
38 * randomly generated and is encrypted with the user's wrapping key. This
39 * layer of indirection allows the user to change their key without
40 * needing to re-encrypt the entire dataset. The ZAP also holds on to the
41 * (non-encrypted) encryption algorithm identifier, IV, and MAC needed to
42 * safely decrypt the master key. For more info on the user's key see the
43 * block comment in libzfs_crypto.c
44 *
45 * In-memory encryption keys are managed through the spa_keystore. The
46 * keystore consists of 3 AVL trees, which are as follows:
47 *
48 * The Wrapping Key Tree:
49 * The wrapping key (wkey) tree stores the user's keys that are fed into the
50 * kernel through 'zfs load-key' and related commands. Datasets inherit their
51 * parent's wkey by default, so these structures are refcounted. The wrapping
52 * keys remain in memory until they are explicitly unloaded (with
53 * "zfs unload-key"). Unloading is only possible when no datasets are using
54 * them (refcount=0).
55 *
56 * The DSL Crypto Key Tree:
57 * The DSL Crypto Keys (DCK) are the in-memory representation of decrypted
58 * master keys. They are used by the functions in zio_crypt.c to perform
59 * encryption, decryption, and authentication. Snapshots and clones of a given
60 * dataset will share a DSL Crypto Key, so they are also refcounted. Once the
61 * refcount on a key hits zero, it is immediately zeroed out and freed.
62 *
63 * The Crypto Key Mapping Tree:
64 * The zio layer needs to lookup master keys by their dataset object id. Since
65 * the DSL Crypto Keys can belong to multiple datasets, we maintain a tree of
66 * dsl_key_mapping_t's which essentially just map the dataset object id to its
67 * appropriate DSL Crypto Key. The management for creating and destroying these
68 * mappings hooks into the code for owning and disowning datasets. Usually,
69 * there will only be one active dataset owner, but there are times
70 * (particularly during dataset creation and destruction) when this may not be
71 * true or the dataset may not be initialized enough to own. As a result, this
72 * object is also refcounted.
73 */
74
f00ab3f2
TC
75/*
76 * This tunable allows datasets to be raw received even if the stream does
77 * not include IVset guids or if the guids don't match. This is used as part
78 * of the resolution for ZPOOL_ERRATA_ZOL_8308_ENCRYPTION.
79 */
80int zfs_disable_ivset_guid_check = 0;
81
b5256303
TC
82static void
83dsl_wrapping_key_hold(dsl_wrapping_key_t *wkey, void *tag)
84{
c13060e4 85 (void) zfs_refcount_add(&wkey->wk_refcnt, tag);
b5256303
TC
86}
87
88static void
89dsl_wrapping_key_rele(dsl_wrapping_key_t *wkey, void *tag)
90{
424fd7c3 91 (void) zfs_refcount_remove(&wkey->wk_refcnt, tag);
b5256303
TC
92}
93
94static void
95dsl_wrapping_key_free(dsl_wrapping_key_t *wkey)
96{
424fd7c3 97 ASSERT0(zfs_refcount_count(&wkey->wk_refcnt));
b5256303
TC
98
99 if (wkey->wk_key.ck_data) {
100 bzero(wkey->wk_key.ck_data,
4807c0ba 101 CRYPTO_BITS2BYTES(wkey->wk_key.ck_length));
b5256303 102 kmem_free(wkey->wk_key.ck_data,
4807c0ba 103 CRYPTO_BITS2BYTES(wkey->wk_key.ck_length));
b5256303
TC
104 }
105
424fd7c3 106 zfs_refcount_destroy(&wkey->wk_refcnt);
b5256303
TC
107 kmem_free(wkey, sizeof (dsl_wrapping_key_t));
108}
109
110static int
111dsl_wrapping_key_create(uint8_t *wkeydata, zfs_keyformat_t keyformat,
112 uint64_t salt, uint64_t iters, dsl_wrapping_key_t **wkey_out)
113{
114 int ret;
115 dsl_wrapping_key_t *wkey;
116
117 /* allocate the wrapping key */
118 wkey = kmem_alloc(sizeof (dsl_wrapping_key_t), KM_SLEEP);
119 if (!wkey)
120 return (SET_ERROR(ENOMEM));
121
122 /* allocate and initialize the underlying crypto key */
123 wkey->wk_key.ck_data = kmem_alloc(WRAPPING_KEY_LEN, KM_SLEEP);
124 if (!wkey->wk_key.ck_data) {
125 ret = ENOMEM;
126 goto error;
127 }
128
129 wkey->wk_key.ck_format = CRYPTO_KEY_RAW;
4807c0ba 130 wkey->wk_key.ck_length = CRYPTO_BYTES2BITS(WRAPPING_KEY_LEN);
b5256303
TC
131 bcopy(wkeydata, wkey->wk_key.ck_data, WRAPPING_KEY_LEN);
132
133 /* initialize the rest of the struct */
424fd7c3 134 zfs_refcount_create(&wkey->wk_refcnt);
b5256303
TC
135 wkey->wk_keyformat = keyformat;
136 wkey->wk_salt = salt;
137 wkey->wk_iters = iters;
138
139 *wkey_out = wkey;
140 return (0);
141
142error:
143 dsl_wrapping_key_free(wkey);
144
145 *wkey_out = NULL;
146 return (ret);
147}
148
149int
150dsl_crypto_params_create_nvlist(dcp_cmd_t cmd, nvlist_t *props,
151 nvlist_t *crypto_args, dsl_crypto_params_t **dcp_out)
152{
153 int ret;
154 uint64_t crypt = ZIO_CRYPT_INHERIT;
155 uint64_t keyformat = ZFS_KEYFORMAT_NONE;
156 uint64_t salt = 0, iters = 0;
157 dsl_crypto_params_t *dcp = NULL;
158 dsl_wrapping_key_t *wkey = NULL;
159 uint8_t *wkeydata = NULL;
160 uint_t wkeydata_len = 0;
161 char *keylocation = NULL;
162
163 dcp = kmem_zalloc(sizeof (dsl_crypto_params_t), KM_SLEEP);
164 if (!dcp) {
165 ret = SET_ERROR(ENOMEM);
166 goto error;
167 }
168
169 dcp->cp_cmd = cmd;
170
171 /* get relevant arguments from the nvlists */
172 if (props != NULL) {
173 (void) nvlist_lookup_uint64(props,
174 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &crypt);
175 (void) nvlist_lookup_uint64(props,
176 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);
177 (void) nvlist_lookup_string(props,
178 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
179 (void) nvlist_lookup_uint64(props,
180 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), &salt);
181 (void) nvlist_lookup_uint64(props,
182 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);
183
184 dcp->cp_crypt = crypt;
185 }
186
187 if (crypto_args != NULL) {
188 (void) nvlist_lookup_uint8_array(crypto_args, "wkeydata",
189 &wkeydata, &wkeydata_len);
190 }
191
192 /* check for valid command */
193 if (dcp->cp_cmd >= DCP_CMD_MAX) {
194 ret = SET_ERROR(EINVAL);
195 goto error;
196 } else {
197 dcp->cp_cmd = cmd;
198 }
199
200 /* check for valid crypt */
201 if (dcp->cp_crypt >= ZIO_CRYPT_FUNCTIONS) {
202 ret = SET_ERROR(EINVAL);
203 goto error;
204 } else {
205 dcp->cp_crypt = crypt;
206 }
207
208 /* check for valid keyformat */
209 if (keyformat >= ZFS_KEYFORMAT_FORMATS) {
210 ret = SET_ERROR(EINVAL);
211 goto error;
212 }
213
214 /* check for a valid keylocation (of any kind) and copy it in */
215 if (keylocation != NULL) {
216 if (!zfs_prop_valid_keylocation(keylocation, B_FALSE)) {
217 ret = SET_ERROR(EINVAL);
218 goto error;
219 }
220
221 dcp->cp_keylocation = spa_strdup(keylocation);
222 }
223
224 /* check wrapping key length, if given */
225 if (wkeydata != NULL && wkeydata_len != WRAPPING_KEY_LEN) {
226 ret = SET_ERROR(EINVAL);
227 goto error;
228 }
229
e1cfd73f 230 /* if the user asked for the default crypt, determine that now */
b5256303
TC
231 if (dcp->cp_crypt == ZIO_CRYPT_ON)
232 dcp->cp_crypt = ZIO_CRYPT_ON_VALUE;
233
234 /* create the wrapping key from the raw data */
235 if (wkeydata != NULL) {
236 /* create the wrapping key with the verified parameters */
237 ret = dsl_wrapping_key_create(wkeydata, keyformat, salt,
238 iters, &wkey);
239 if (ret != 0)
240 goto error;
241
242 dcp->cp_wkey = wkey;
243 }
244
245 /*
246 * Remove the encryption properties from the nvlist since they are not
247 * maintained through the DSL.
248 */
249 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_ENCRYPTION));
250 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_KEYFORMAT));
251 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT));
252 (void) nvlist_remove_all(props,
253 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS));
254
255 *dcp_out = dcp;
256
257 return (0);
258
259error:
260 if (wkey != NULL)
261 dsl_wrapping_key_free(wkey);
262 if (dcp != NULL)
263 kmem_free(dcp, sizeof (dsl_crypto_params_t));
264
265 *dcp_out = NULL;
266 return (ret);
267}
268
269void
270dsl_crypto_params_free(dsl_crypto_params_t *dcp, boolean_t unload)
271{
272 if (dcp == NULL)
273 return;
274
275 if (dcp->cp_keylocation != NULL)
276 spa_strfree(dcp->cp_keylocation);
277 if (unload && dcp->cp_wkey != NULL)
278 dsl_wrapping_key_free(dcp->cp_wkey);
279
280 kmem_free(dcp, sizeof (dsl_crypto_params_t));
281}
282
283static int
284spa_crypto_key_compare(const void *a, const void *b)
285{
286 const dsl_crypto_key_t *dcka = a;
287 const dsl_crypto_key_t *dckb = b;
288
289 if (dcka->dck_obj < dckb->dck_obj)
290 return (-1);
291 if (dcka->dck_obj > dckb->dck_obj)
292 return (1);
293 return (0);
294}
295
296static int
297spa_key_mapping_compare(const void *a, const void *b)
298{
299 const dsl_key_mapping_t *kma = a;
300 const dsl_key_mapping_t *kmb = b;
301
302 if (kma->km_dsobj < kmb->km_dsobj)
303 return (-1);
304 if (kma->km_dsobj > kmb->km_dsobj)
305 return (1);
306 return (0);
307}
308
309static int
310spa_wkey_compare(const void *a, const void *b)
311{
312 const dsl_wrapping_key_t *wka = a;
313 const dsl_wrapping_key_t *wkb = b;
314
315 if (wka->wk_ddobj < wkb->wk_ddobj)
316 return (-1);
317 if (wka->wk_ddobj > wkb->wk_ddobj)
318 return (1);
319 return (0);
320}
321
322void
323spa_keystore_init(spa_keystore_t *sk)
324{
325 rw_init(&sk->sk_dk_lock, NULL, RW_DEFAULT, NULL);
326 rw_init(&sk->sk_km_lock, NULL, RW_DEFAULT, NULL);
327 rw_init(&sk->sk_wkeys_lock, NULL, RW_DEFAULT, NULL);
328 avl_create(&sk->sk_dsl_keys, spa_crypto_key_compare,
329 sizeof (dsl_crypto_key_t),
330 offsetof(dsl_crypto_key_t, dck_avl_link));
331 avl_create(&sk->sk_key_mappings, spa_key_mapping_compare,
332 sizeof (dsl_key_mapping_t),
333 offsetof(dsl_key_mapping_t, km_avl_link));
334 avl_create(&sk->sk_wkeys, spa_wkey_compare, sizeof (dsl_wrapping_key_t),
335 offsetof(dsl_wrapping_key_t, wk_avl_link));
336}
337
338void
339spa_keystore_fini(spa_keystore_t *sk)
340{
341 dsl_wrapping_key_t *wkey;
342 void *cookie = NULL;
343
344 ASSERT(avl_is_empty(&sk->sk_dsl_keys));
345 ASSERT(avl_is_empty(&sk->sk_key_mappings));
346
347 while ((wkey = avl_destroy_nodes(&sk->sk_wkeys, &cookie)) != NULL)
348 dsl_wrapping_key_free(wkey);
349
350 avl_destroy(&sk->sk_wkeys);
351 avl_destroy(&sk->sk_key_mappings);
352 avl_destroy(&sk->sk_dsl_keys);
353 rw_destroy(&sk->sk_wkeys_lock);
354 rw_destroy(&sk->sk_km_lock);
355 rw_destroy(&sk->sk_dk_lock);
356}
357
ae76f45c 358static int
b5256303
TC
359dsl_dir_get_encryption_root_ddobj(dsl_dir_t *dd, uint64_t *rddobj)
360{
361 if (dd->dd_crypto_obj == 0)
362 return (SET_ERROR(ENOENT));
363
364 return (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
365 DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1, rddobj));
366}
367
ae76f45c
TC
368int
369dsl_dir_get_encryption_version(dsl_dir_t *dd, uint64_t *version)
370{
371 *version = 0;
372
373 if (dd->dd_crypto_obj == 0)
374 return (SET_ERROR(ENOENT));
375
376 /* version 0 is implied by ENOENT */
377 (void) zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
378 DSL_CRYPTO_KEY_VERSION, 8, 1, version);
379
380 return (0);
381}
382
383boolean_t
384dsl_dir_incompatible_encryption_version(dsl_dir_t *dd)
385{
386 int ret;
387 uint64_t version = 0;
388
389 ret = dsl_dir_get_encryption_version(dd, &version);
390 if (ret != 0)
391 return (B_FALSE);
392
393 return (version != ZIO_CRYPT_KEY_CURRENT_VERSION);
394}
395
b5256303
TC
396static int
397spa_keystore_wkey_hold_ddobj_impl(spa_t *spa, uint64_t ddobj,
398 void *tag, dsl_wrapping_key_t **wkey_out)
399{
400 int ret;
401 dsl_wrapping_key_t search_wkey;
402 dsl_wrapping_key_t *found_wkey;
403
404 ASSERT(RW_LOCK_HELD(&spa->spa_keystore.sk_wkeys_lock));
405
406 /* init the search wrapping key */
407 search_wkey.wk_ddobj = ddobj;
408
409 /* lookup the wrapping key */
410 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, &search_wkey, NULL);
411 if (!found_wkey) {
412 ret = SET_ERROR(ENOENT);
413 goto error;
414 }
415
416 /* increment the refcount */
417 dsl_wrapping_key_hold(found_wkey, tag);
418
419 *wkey_out = found_wkey;
420 return (0);
421
422error:
423 *wkey_out = NULL;
424 return (ret);
425}
426
427static int
428spa_keystore_wkey_hold_dd(spa_t *spa, dsl_dir_t *dd, void *tag,
429 dsl_wrapping_key_t **wkey_out)
430{
431 int ret;
432 dsl_wrapping_key_t *wkey;
433 uint64_t rddobj;
434 boolean_t locked = B_FALSE;
435
436 if (!RW_WRITE_HELD(&spa->spa_keystore.sk_wkeys_lock)) {
437 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_READER);
438 locked = B_TRUE;
439 }
440
441 /* get the ddobj that the keylocation property was inherited from */
442 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
443 if (ret != 0)
444 goto error;
445
446 /* lookup the wkey in the avl tree */
447 ret = spa_keystore_wkey_hold_ddobj_impl(spa, rddobj, tag, &wkey);
448 if (ret != 0)
449 goto error;
450
451 /* unlock the wkey tree if we locked it */
452 if (locked)
453 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
454
455 *wkey_out = wkey;
456 return (0);
457
458error:
459 if (locked)
460 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
461
462 *wkey_out = NULL;
463 return (ret);
464}
465
466int
467dsl_crypto_can_set_keylocation(const char *dsname, const char *keylocation)
468{
469 int ret = 0;
470 dsl_dir_t *dd = NULL;
471 dsl_pool_t *dp = NULL;
b5256303
TC
472 uint64_t rddobj;
473
474 /* hold the dsl dir */
475 ret = dsl_pool_hold(dsname, FTAG, &dp);
476 if (ret != 0)
477 goto out;
478
479 ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);
fc1ecd16
DB
480 if (ret != 0) {
481 dd = NULL;
b5256303 482 goto out;
fc1ecd16 483 }
b5256303
TC
484
485 /* if dd is not encrypted, the value may only be "none" */
486 if (dd->dd_crypto_obj == 0) {
487 if (strcmp(keylocation, "none") != 0) {
488 ret = SET_ERROR(EACCES);
489 goto out;
490 }
491
492 ret = 0;
493 goto out;
494 }
495
496 /* check for a valid keylocation for encrypted datasets */
497 if (!zfs_prop_valid_keylocation(keylocation, B_TRUE)) {
498 ret = SET_ERROR(EINVAL);
499 goto out;
500 }
501
502 /* check that this is an encryption root */
503 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
504 if (ret != 0)
505 goto out;
506
507 if (rddobj != dd->dd_object) {
508 ret = SET_ERROR(EACCES);
509 goto out;
510 }
511
b5256303
TC
512 dsl_dir_rele(dd, FTAG);
513 dsl_pool_rele(dp, FTAG);
514
515 return (0);
516
517out:
b5256303
TC
518 if (dd != NULL)
519 dsl_dir_rele(dd, FTAG);
520 if (dp != NULL)
521 dsl_pool_rele(dp, FTAG);
522
523 return (ret);
524}
525
526static void
527dsl_crypto_key_free(dsl_crypto_key_t *dck)
528{
424fd7c3 529 ASSERT(zfs_refcount_count(&dck->dck_holds) == 0);
b5256303
TC
530
531 /* destroy the zio_crypt_key_t */
532 zio_crypt_key_destroy(&dck->dck_key);
533
534 /* free the refcount, wrapping key, and lock */
424fd7c3 535 zfs_refcount_destroy(&dck->dck_holds);
b5256303
TC
536 if (dck->dck_wkey)
537 dsl_wrapping_key_rele(dck->dck_wkey, dck);
538
539 /* free the key */
540 kmem_free(dck, sizeof (dsl_crypto_key_t));
541}
542
543static void
544dsl_crypto_key_rele(dsl_crypto_key_t *dck, void *tag)
545{
424fd7c3 546 if (zfs_refcount_remove(&dck->dck_holds, tag) == 0)
b5256303
TC
547 dsl_crypto_key_free(dck);
548}
549
550static int
551dsl_crypto_key_open(objset_t *mos, dsl_wrapping_key_t *wkey,
552 uint64_t dckobj, void *tag, dsl_crypto_key_t **dck_out)
553{
554 int ret;
ae76f45c 555 uint64_t crypt = 0, guid = 0, version = 0;
b5256303
TC
556 uint8_t raw_keydata[MASTER_KEY_MAX_LEN];
557 uint8_t raw_hmac_keydata[SHA512_HMAC_KEYLEN];
558 uint8_t iv[WRAPPING_IV_LEN];
559 uint8_t mac[WRAPPING_MAC_LEN];
560 dsl_crypto_key_t *dck;
561
562 /* allocate and initialize the key */
563 dck = kmem_zalloc(sizeof (dsl_crypto_key_t), KM_SLEEP);
564 if (!dck)
565 return (SET_ERROR(ENOMEM));
566
567 /* fetch all of the values we need from the ZAP */
568 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,
569 &crypt);
570 if (ret != 0)
571 goto error;
572
573 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1, &guid);
574 if (ret != 0)
575 goto error;
576
577 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,
578 MASTER_KEY_MAX_LEN, raw_keydata);
579 if (ret != 0)
580 goto error;
581
582 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,
583 SHA512_HMAC_KEYLEN, raw_hmac_keydata);
584 if (ret != 0)
585 goto error;
586
587 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,
588 iv);
589 if (ret != 0)
590 goto error;
591
592 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,
593 mac);
594 if (ret != 0)
595 goto error;
596
ae76f45c
TC
597 /* the initial on-disk format for encryption did not have a version */
598 (void) zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_VERSION, 8, 1, &version);
599
b5256303
TC
600 /*
601 * Unwrap the keys. If there is an error return EACCES to indicate
602 * an authentication failure.
603 */
10fa2545
BB
604 ret = zio_crypt_key_unwrap(&wkey->wk_key, crypt, version, guid,
605 raw_keydata, raw_hmac_keydata, iv, mac, &dck->dck_key);
b5256303
TC
606 if (ret != 0) {
607 ret = SET_ERROR(EACCES);
608 goto error;
609 }
610
611 /* finish initializing the dsl_crypto_key_t */
424fd7c3 612 zfs_refcount_create(&dck->dck_holds);
b5256303
TC
613 dsl_wrapping_key_hold(wkey, dck);
614 dck->dck_wkey = wkey;
615 dck->dck_obj = dckobj;
c13060e4 616 zfs_refcount_add(&dck->dck_holds, tag);
b5256303
TC
617
618 *dck_out = dck;
619 return (0);
620
621error:
622 if (dck != NULL) {
623 bzero(dck, sizeof (dsl_crypto_key_t));
624 kmem_free(dck, sizeof (dsl_crypto_key_t));
625 }
626
627 *dck_out = NULL;
628 return (ret);
629}
630
631static int
632spa_keystore_dsl_key_hold_impl(spa_t *spa, uint64_t dckobj, void *tag,
633 dsl_crypto_key_t **dck_out)
634{
635 int ret;
636 dsl_crypto_key_t search_dck;
637 dsl_crypto_key_t *found_dck;
638
639 ASSERT(RW_LOCK_HELD(&spa->spa_keystore.sk_dk_lock));
640
641 /* init the search key */
642 search_dck.dck_obj = dckobj;
643
644 /* find the matching key in the keystore */
645 found_dck = avl_find(&spa->spa_keystore.sk_dsl_keys, &search_dck, NULL);
646 if (!found_dck) {
647 ret = SET_ERROR(ENOENT);
648 goto error;
649 }
650
651 /* increment the refcount */
c13060e4 652 zfs_refcount_add(&found_dck->dck_holds, tag);
b5256303
TC
653
654 *dck_out = found_dck;
655 return (0);
656
657error:
658 *dck_out = NULL;
659 return (ret);
660}
661
662static int
663spa_keystore_dsl_key_hold_dd(spa_t *spa, dsl_dir_t *dd, void *tag,
664 dsl_crypto_key_t **dck_out)
665{
666 int ret;
667 avl_index_t where;
0d23f5e2 668 dsl_crypto_key_t *dck_io = NULL, *dck_ks = NULL;
b5256303
TC
669 dsl_wrapping_key_t *wkey = NULL;
670 uint64_t dckobj = dd->dd_crypto_obj;
671
0d23f5e2
BB
672 /* Lookup the key in the tree of currently loaded keys */
673 rw_enter(&spa->spa_keystore.sk_dk_lock, RW_READER);
674 ret = spa_keystore_dsl_key_hold_impl(spa, dckobj, tag, &dck_ks);
675 rw_exit(&spa->spa_keystore.sk_dk_lock);
676 if (ret == 0) {
677 *dck_out = dck_ks;
b5256303
TC
678 return (0);
679 }
680
0d23f5e2 681 /* Lookup the wrapping key from the keystore */
b5256303
TC
682 ret = spa_keystore_wkey_hold_dd(spa, dd, FTAG, &wkey);
683 if (ret != 0) {
0d23f5e2
BB
684 *dck_out = NULL;
685 return (SET_ERROR(EACCES));
b5256303
TC
686 }
687
0d23f5e2 688 /* Read the key from disk */
b5256303 689 ret = dsl_crypto_key_open(spa->spa_meta_objset, wkey, dckobj,
0d23f5e2
BB
690 tag, &dck_io);
691 if (ret != 0) {
692 dsl_wrapping_key_rele(wkey, FTAG);
693 *dck_out = NULL;
694 return (ret);
695 }
b5256303
TC
696
697 /*
0d23f5e2
BB
698 * Add the key to the keystore. It may already exist if it was
699 * added while performing the read from disk. In this case discard
700 * it and return the key from the keystore.
b5256303 701 */
0d23f5e2
BB
702 rw_enter(&spa->spa_keystore.sk_dk_lock, RW_WRITER);
703 ret = spa_keystore_dsl_key_hold_impl(spa, dckobj, tag, &dck_ks);
704 if (ret != 0) {
705 avl_find(&spa->spa_keystore.sk_dsl_keys, dck_io, &where);
706 avl_insert(&spa->spa_keystore.sk_dsl_keys, dck_io, where);
707 *dck_out = dck_io;
708 } else {
709 dsl_crypto_key_free(dck_io);
710 *dck_out = dck_ks;
711 }
b5256303 712
0d23f5e2 713 /* Release the wrapping key (the dsl key now has a reference to it) */
b5256303 714 dsl_wrapping_key_rele(wkey, FTAG);
b5256303
TC
715 rw_exit(&spa->spa_keystore.sk_dk_lock);
716
b5256303 717 return (0);
b5256303
TC
718}
719
720void
721spa_keystore_dsl_key_rele(spa_t *spa, dsl_crypto_key_t *dck, void *tag)
722{
723 rw_enter(&spa->spa_keystore.sk_dk_lock, RW_WRITER);
724
424fd7c3 725 if (zfs_refcount_remove(&dck->dck_holds, tag) == 0) {
b5256303
TC
726 avl_remove(&spa->spa_keystore.sk_dsl_keys, dck);
727 dsl_crypto_key_free(dck);
728 }
729
730 rw_exit(&spa->spa_keystore.sk_dk_lock);
731}
732
733int
734spa_keystore_load_wkey_impl(spa_t *spa, dsl_wrapping_key_t *wkey)
735{
736 int ret;
737 avl_index_t where;
738 dsl_wrapping_key_t *found_wkey;
739
740 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
741
742 /* insert the wrapping key into the keystore */
743 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, wkey, &where);
744 if (found_wkey != NULL) {
745 ret = SET_ERROR(EEXIST);
746 goto error_unlock;
747 }
748 avl_insert(&spa->spa_keystore.sk_wkeys, wkey, where);
749
750 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
751
752 return (0);
753
754error_unlock:
755 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
756 return (ret);
757}
758
759int
760spa_keystore_load_wkey(const char *dsname, dsl_crypto_params_t *dcp,
761 boolean_t noop)
762{
763 int ret;
764 dsl_dir_t *dd = NULL;
765 dsl_crypto_key_t *dck = NULL;
766 dsl_wrapping_key_t *wkey = dcp->cp_wkey;
767 dsl_pool_t *dp = NULL;
f44ad929 768 uint64_t rddobj, keyformat, salt, iters;
b5256303
TC
769
770 /*
771 * We don't validate the wrapping key's keyformat, salt, or iters
772 * since they will never be needed after the DCK has been wrapped.
773 */
774 if (dcp->cp_wkey == NULL ||
775 dcp->cp_cmd != DCP_CMD_NONE ||
776 dcp->cp_crypt != ZIO_CRYPT_INHERIT ||
777 dcp->cp_keylocation != NULL)
778 return (SET_ERROR(EINVAL));
779
780 ret = dsl_pool_hold(dsname, FTAG, &dp);
781 if (ret != 0)
782 goto error;
783
784 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {
f44ad929 785 ret = SET_ERROR(ENOTSUP);
b5256303
TC
786 goto error;
787 }
788
789 /* hold the dsl dir */
790 ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);
fc1ecd16
DB
791 if (ret != 0) {
792 dd = NULL;
b5256303 793 goto error;
fc1ecd16 794 }
b5256303 795
f44ad929
TC
796 /* confirm that dd is the encryption root */
797 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
798 if (ret != 0 || rddobj != dd->dd_object) {
799 ret = SET_ERROR(EINVAL);
800 goto error;
801 }
802
b5256303
TC
803 /* initialize the wkey's ddobj */
804 wkey->wk_ddobj = dd->dd_object;
805
806 /* verify that the wkey is correct by opening its dsl key */
807 ret = dsl_crypto_key_open(dp->dp_meta_objset, wkey,
808 dd->dd_crypto_obj, FTAG, &dck);
809 if (ret != 0)
810 goto error;
811
2637dda8
TC
812 /* initialize the wkey encryption parameters from the DSL Crypto Key */
813 ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,
814 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &keyformat);
815 if (ret != 0)
816 goto error;
817
818 ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,
819 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &salt);
820 if (ret != 0)
821 goto error;
822
823 ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,
824 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &iters);
825 if (ret != 0)
826 goto error;
827
828 ASSERT3U(keyformat, <, ZFS_KEYFORMAT_FORMATS);
829 ASSERT3U(keyformat, !=, ZFS_KEYFORMAT_NONE);
830 IMPLY(keyformat == ZFS_KEYFORMAT_PASSPHRASE, iters != 0);
831 IMPLY(keyformat == ZFS_KEYFORMAT_PASSPHRASE, salt != 0);
832 IMPLY(keyformat != ZFS_KEYFORMAT_PASSPHRASE, iters == 0);
833 IMPLY(keyformat != ZFS_KEYFORMAT_PASSPHRASE, salt == 0);
834
835 wkey->wk_keyformat = keyformat;
836 wkey->wk_salt = salt;
837 wkey->wk_iters = iters;
838
b5256303 839 /*
2637dda8
TC
840 * At this point we have verified the wkey and confirmed that it can
841 * be used to decrypt a DSL Crypto Key. We can simply cleanup and
b5256303
TC
842 * return if this is all the user wanted to do.
843 */
844 if (noop)
845 goto error;
846
847 /* insert the wrapping key into the keystore */
848 ret = spa_keystore_load_wkey_impl(dp->dp_spa, wkey);
849 if (ret != 0)
850 goto error;
851
852 dsl_crypto_key_rele(dck, FTAG);
853 dsl_dir_rele(dd, FTAG);
854 dsl_pool_rele(dp, FTAG);
855
856 /* create any zvols under this ds */
857 zvol_create_minors(dp->dp_spa, dsname, B_TRUE);
858
859 return (0);
860
861error:
862 if (dck != NULL)
863 dsl_crypto_key_rele(dck, FTAG);
864 if (dd != NULL)
865 dsl_dir_rele(dd, FTAG);
866 if (dp != NULL)
867 dsl_pool_rele(dp, FTAG);
868
869 return (ret);
870}
871
872int
873spa_keystore_unload_wkey_impl(spa_t *spa, uint64_t ddobj)
874{
875 int ret;
876 dsl_wrapping_key_t search_wkey;
877 dsl_wrapping_key_t *found_wkey;
878
879 /* init the search wrapping key */
880 search_wkey.wk_ddobj = ddobj;
881
882 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
883
884 /* remove the wrapping key from the keystore */
885 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys,
886 &search_wkey, NULL);
887 if (!found_wkey) {
85ce3f4f 888 ret = SET_ERROR(EACCES);
b5256303 889 goto error_unlock;
424fd7c3 890 } else if (zfs_refcount_count(&found_wkey->wk_refcnt) != 0) {
b5256303
TC
891 ret = SET_ERROR(EBUSY);
892 goto error_unlock;
893 }
894 avl_remove(&spa->spa_keystore.sk_wkeys, found_wkey);
895
896 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
897
898 /* free the wrapping key */
899 dsl_wrapping_key_free(found_wkey);
900
901 return (0);
902
903error_unlock:
904 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
905 return (ret);
906}
907
908int
909spa_keystore_unload_wkey(const char *dsname)
910{
911 int ret = 0;
912 dsl_dir_t *dd = NULL;
913 dsl_pool_t *dp = NULL;
52ce99dd
TC
914 spa_t *spa = NULL;
915
916 ret = spa_open(dsname, &spa, FTAG);
917 if (ret != 0)
918 return (ret);
919
920 /*
921 * Wait for any outstanding txg IO to complete, releasing any
922 * remaining references on the wkey.
923 */
924 if (spa_mode(spa) != FREAD)
925 txg_wait_synced(spa->spa_dsl_pool, 0);
926
927 spa_close(spa, FTAG);
b5256303
TC
928
929 /* hold the dsl dir */
930 ret = dsl_pool_hold(dsname, FTAG, &dp);
931 if (ret != 0)
932 goto error;
933
934 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {
935 ret = (SET_ERROR(ENOTSUP));
936 goto error;
937 }
938
939 ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);
fc1ecd16
DB
940 if (ret != 0) {
941 dd = NULL;
b5256303 942 goto error;
fc1ecd16 943 }
b5256303
TC
944
945 /* unload the wkey */
946 ret = spa_keystore_unload_wkey_impl(dp->dp_spa, dd->dd_object);
947 if (ret != 0)
948 goto error;
949
950 dsl_dir_rele(dd, FTAG);
951 dsl_pool_rele(dp, FTAG);
952
953 /* remove any zvols under this ds */
954 zvol_remove_minors(dp->dp_spa, dsname, B_TRUE);
955
956 return (0);
957
958error:
959 if (dd != NULL)
960 dsl_dir_rele(dd, FTAG);
961 if (dp != NULL)
962 dsl_pool_rele(dp, FTAG);
963
964 return (ret);
965}
966
52ce99dd
TC
967void
968key_mapping_add_ref(dsl_key_mapping_t *km, void *tag)
969{
970 ASSERT3U(zfs_refcount_count(&km->km_refcnt), >=, 1);
971 zfs_refcount_add(&km->km_refcnt, tag);
972}
973
974/*
975 * The locking here is a little tricky to ensure we don't cause unnecessary
976 * performance problems. We want to release a key mapping whenever someone
977 * decrements the refcount to 0, but freeing the mapping requires removing
978 * it from the spa_keystore, which requires holding sk_km_lock as a writer.
979 * Most of the time we don't want to hold this lock as a writer, since the
980 * same lock is held as a reader for each IO that needs to encrypt / decrypt
981 * data for any dataset and in practice we will only actually free the
982 * mapping after unmounting a dataset.
983 */
984void
985key_mapping_rele(spa_t *spa, dsl_key_mapping_t *km, void *tag)
986{
987 ASSERT3U(zfs_refcount_count(&km->km_refcnt), >=, 1);
988
989 if (zfs_refcount_remove(&km->km_refcnt, tag) != 0)
990 return;
991
992 /*
993 * We think we are going to need to free the mapping. Add a
994 * reference to prevent most other releasers from thinking
995 * this might be their responsibility. This is inherently
996 * racy, so we will confirm that we are legitimately the
997 * last holder once we have the sk_km_lock as a writer.
998 */
999 zfs_refcount_add(&km->km_refcnt, FTAG);
1000
1001 rw_enter(&spa->spa_keystore.sk_km_lock, RW_WRITER);
1002 if (zfs_refcount_remove(&km->km_refcnt, FTAG) != 0) {
1003 rw_exit(&spa->spa_keystore.sk_km_lock);
1004 return;
1005 }
1006
1007 avl_remove(&spa->spa_keystore.sk_key_mappings, km);
1008 rw_exit(&spa->spa_keystore.sk_km_lock);
1009
1010 spa_keystore_dsl_key_rele(spa, km->km_key, km);
1011 kmem_free(km, sizeof (dsl_key_mapping_t));
1012}
1013
b5256303 1014int
52ce99dd
TC
1015spa_keystore_create_mapping(spa_t *spa, dsl_dataset_t *ds, void *tag,
1016 dsl_key_mapping_t **km_out)
b5256303
TC
1017{
1018 int ret;
1019 avl_index_t where;
0d23f5e2 1020 dsl_key_mapping_t *km, *found_km;
b5256303
TC
1021 boolean_t should_free = B_FALSE;
1022
0d23f5e2
BB
1023 /* Allocate and initialize the mapping */
1024 km = kmem_zalloc(sizeof (dsl_key_mapping_t), KM_SLEEP);
424fd7c3 1025 zfs_refcount_create(&km->km_refcnt);
b5256303 1026
52ce99dd 1027 ret = spa_keystore_dsl_key_hold_dd(spa, ds->ds_dir, km, &km->km_key);
0d23f5e2 1028 if (ret != 0) {
424fd7c3 1029 zfs_refcount_destroy(&km->km_refcnt);
0d23f5e2 1030 kmem_free(km, sizeof (dsl_key_mapping_t));
52ce99dd
TC
1031
1032 if (km_out != NULL)
1033 *km_out = NULL;
0d23f5e2
BB
1034 return (ret);
1035 }
b5256303 1036
52ce99dd 1037 km->km_dsobj = ds->ds_object;
b5256303
TC
1038
1039 rw_enter(&spa->spa_keystore.sk_km_lock, RW_WRITER);
1040
1041 /*
1042 * If a mapping already exists, simply increment its refcount and
1043 * cleanup the one we made. We want to allocate / free outside of
1044 * the lock because this lock is also used by the zio layer to lookup
1045 * key mappings. Otherwise, use the one we created. Normally, there will
1046 * only be one active reference at a time (the objset owner), but there
1047 * are times when there could be multiple async users.
1048 */
1049 found_km = avl_find(&spa->spa_keystore.sk_key_mappings, km, &where);
1050 if (found_km != NULL) {
1051 should_free = B_TRUE;
c13060e4 1052 zfs_refcount_add(&found_km->km_refcnt, tag);
52ce99dd
TC
1053 if (km_out != NULL)
1054 *km_out = found_km;
b5256303 1055 } else {
c13060e4 1056 zfs_refcount_add(&km->km_refcnt, tag);
b5256303 1057 avl_insert(&spa->spa_keystore.sk_key_mappings, km, where);
52ce99dd
TC
1058 if (km_out != NULL)
1059 *km_out = km;
b5256303
TC
1060 }
1061
1062 rw_exit(&spa->spa_keystore.sk_km_lock);
1063
1064 if (should_free) {
1065 spa_keystore_dsl_key_rele(spa, km->km_key, km);
424fd7c3 1066 zfs_refcount_destroy(&km->km_refcnt);
b5256303
TC
1067 kmem_free(km, sizeof (dsl_key_mapping_t));
1068 }
1069
1070 return (0);
b5256303
TC
1071}
1072
b5256303
TC
1073int
1074spa_keystore_remove_mapping(spa_t *spa, uint64_t dsobj, void *tag)
1075{
1076 int ret;
1077 dsl_key_mapping_t search_km;
1078 dsl_key_mapping_t *found_km;
b5256303
TC
1079
1080 /* init the search key mapping */
1081 search_km.km_dsobj = dsobj;
1082
52ce99dd 1083 rw_enter(&spa->spa_keystore.sk_km_lock, RW_READER);
b5256303
TC
1084
1085 /* find the matching mapping */
1086 found_km = avl_find(&spa->spa_keystore.sk_key_mappings,
1087 &search_km, NULL);
1088 if (found_km == NULL) {
1089 ret = SET_ERROR(ENOENT);
1090 goto error_unlock;
1091 }
1092
b5256303
TC
1093 rw_exit(&spa->spa_keystore.sk_km_lock);
1094
52ce99dd 1095 key_mapping_rele(spa, found_km, tag);
b5256303
TC
1096
1097 return (0);
1098
1099error_unlock:
1100 rw_exit(&spa->spa_keystore.sk_km_lock);
1101 return (ret);
1102}
1103
1104/*
1105 * This function is primarily used by the zio and arc layer to lookup
1106 * DSL Crypto Keys for encryption. Callers must release the key with
1107 * spa_keystore_dsl_key_rele(). The function may also be called with
1108 * dck_out == NULL and tag == NULL to simply check that a key exists
1109 * without getting a reference to it.
1110 */
1111int
1112spa_keystore_lookup_key(spa_t *spa, uint64_t dsobj, void *tag,
1113 dsl_crypto_key_t **dck_out)
1114{
1115 int ret;
1116 dsl_key_mapping_t search_km;
1117 dsl_key_mapping_t *found_km;
1118
1119 ASSERT((tag != NULL && dck_out != NULL) ||
1120 (tag == NULL && dck_out == NULL));
1121
1122 /* init the search key mapping */
1123 search_km.km_dsobj = dsobj;
1124
1125 rw_enter(&spa->spa_keystore.sk_km_lock, RW_READER);
1126
1127 /* remove the mapping from the tree */
1128 found_km = avl_find(&spa->spa_keystore.sk_key_mappings, &search_km,
1129 NULL);
1130 if (found_km == NULL) {
1131 ret = SET_ERROR(ENOENT);
1132 goto error_unlock;
1133 }
1134
1135 if (found_km && tag)
c13060e4 1136 zfs_refcount_add(&found_km->km_key->dck_holds, tag);
b5256303
TC
1137
1138 rw_exit(&spa->spa_keystore.sk_km_lock);
1139
1140 if (dck_out != NULL)
1141 *dck_out = found_km->km_key;
1142 return (0);
1143
1144error_unlock:
1145 rw_exit(&spa->spa_keystore.sk_km_lock);
1146
1147 if (dck_out != NULL)
1148 *dck_out = NULL;
1149 return (ret);
1150}
1151
1152static int
1153dmu_objset_check_wkey_loaded(dsl_dir_t *dd)
1154{
1155 int ret;
1156 dsl_wrapping_key_t *wkey = NULL;
1157
1158 ret = spa_keystore_wkey_hold_dd(dd->dd_pool->dp_spa, dd, FTAG,
1159 &wkey);
1160 if (ret != 0)
1161 return (SET_ERROR(EACCES));
1162
1163 dsl_wrapping_key_rele(wkey, FTAG);
1164
1165 return (0);
1166}
1167
1168static zfs_keystatus_t
1169dsl_dataset_get_keystatus(dsl_dir_t *dd)
1170{
1171 /* check if this dd has a has a dsl key */
1172 if (dd->dd_crypto_obj == 0)
1173 return (ZFS_KEYSTATUS_NONE);
1174
1175 return (dmu_objset_check_wkey_loaded(dd) == 0 ?
1176 ZFS_KEYSTATUS_AVAILABLE : ZFS_KEYSTATUS_UNAVAILABLE);
1177}
1178
1179static int
1180dsl_dir_get_crypt(dsl_dir_t *dd, uint64_t *crypt)
1181{
1182 if (dd->dd_crypto_obj == 0) {
1183 *crypt = ZIO_CRYPT_OFF;
1184 return (0);
1185 }
1186
1187 return (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
1188 DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1, crypt));
1189}
1190
1191static void
1192dsl_crypto_key_sync_impl(objset_t *mos, uint64_t dckobj, uint64_t crypt,
1193 uint64_t root_ddobj, uint64_t guid, uint8_t *iv, uint8_t *mac,
1194 uint8_t *keydata, uint8_t *hmac_keydata, uint64_t keyformat,
1195 uint64_t salt, uint64_t iters, dmu_tx_t *tx)
1196{
1197 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,
1198 &crypt, tx));
1199 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1,
1200 &root_ddobj, tx));
1201 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1,
1202 &guid, tx));
1203 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,
1204 iv, tx));
1205 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,
1206 mac, tx));
1207 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,
1208 MASTER_KEY_MAX_LEN, keydata, tx));
1209 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,
1210 SHA512_HMAC_KEYLEN, hmac_keydata, tx));
1211 VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_KEYFORMAT),
1212 8, 1, &keyformat, tx));
1213 VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT),
1214 8, 1, &salt, tx));
1215 VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS),
1216 8, 1, &iters, tx));
1217}
1218
1219static void
1220dsl_crypto_key_sync(dsl_crypto_key_t *dck, dmu_tx_t *tx)
1221{
1222 zio_crypt_key_t *key = &dck->dck_key;
1223 dsl_wrapping_key_t *wkey = dck->dck_wkey;
1224 uint8_t keydata[MASTER_KEY_MAX_LEN];
1225 uint8_t hmac_keydata[SHA512_HMAC_KEYLEN];
1226 uint8_t iv[WRAPPING_IV_LEN];
1227 uint8_t mac[WRAPPING_MAC_LEN];
1228
1229 ASSERT(dmu_tx_is_syncing(tx));
1230 ASSERT3U(key->zk_crypt, <, ZIO_CRYPT_FUNCTIONS);
1231
1232 /* encrypt and store the keys along with the IV and MAC */
10fa2545
BB
1233 VERIFY0(zio_crypt_key_wrap(&dck->dck_wkey->wk_key, key, iv, mac,
1234 keydata, hmac_keydata));
b5256303
TC
1235
1236 /* update the ZAP with the obtained values */
10fa2545
BB
1237 dsl_crypto_key_sync_impl(tx->tx_pool->dp_meta_objset, dck->dck_obj,
1238 key->zk_crypt, wkey->wk_ddobj, key->zk_guid, iv, mac, keydata,
1239 hmac_keydata, wkey->wk_keyformat, wkey->wk_salt, wkey->wk_iters,
1240 tx);
b5256303
TC
1241}
1242
1243typedef struct spa_keystore_change_key_args {
1244 const char *skcka_dsname;
1245 dsl_crypto_params_t *skcka_cp;
1246} spa_keystore_change_key_args_t;
1247
1248static int
1249spa_keystore_change_key_check(void *arg, dmu_tx_t *tx)
1250{
1251 int ret;
1252 dsl_dir_t *dd = NULL;
1253 dsl_pool_t *dp = dmu_tx_pool(tx);
1254 spa_keystore_change_key_args_t *skcka = arg;
1255 dsl_crypto_params_t *dcp = skcka->skcka_cp;
1256 uint64_t rddobj;
1257
1258 /* check for the encryption feature */
1259 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {
1260 ret = SET_ERROR(ENOTSUP);
1261 goto error;
1262 }
1263
1264 /* check for valid key change command */
1265 if (dcp->cp_cmd != DCP_CMD_NEW_KEY &&
1266 dcp->cp_cmd != DCP_CMD_INHERIT &&
1267 dcp->cp_cmd != DCP_CMD_FORCE_NEW_KEY &&
1268 dcp->cp_cmd != DCP_CMD_FORCE_INHERIT) {
1269 ret = SET_ERROR(EINVAL);
1270 goto error;
1271 }
1272
1273 /* hold the dd */
1274 ret = dsl_dir_hold(dp, skcka->skcka_dsname, FTAG, &dd, NULL);
fc1ecd16
DB
1275 if (ret != 0) {
1276 dd = NULL;
b5256303 1277 goto error;
fc1ecd16 1278 }
b5256303
TC
1279
1280 /* verify that the dataset is encrypted */
1281 if (dd->dd_crypto_obj == 0) {
1282 ret = SET_ERROR(EINVAL);
1283 goto error;
1284 }
1285
1286 /* clones must always use their origin's key */
1287 if (dsl_dir_is_clone(dd)) {
1288 ret = SET_ERROR(EINVAL);
1289 goto error;
1290 }
1291
1292 /* lookup the ddobj we are inheriting the keylocation from */
1293 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
1294 if (ret != 0)
1295 goto error;
1296
85ce3f4f 1297 /* Handle inheritance */
b5256303
TC
1298 if (dcp->cp_cmd == DCP_CMD_INHERIT ||
1299 dcp->cp_cmd == DCP_CMD_FORCE_INHERIT) {
1300 /* no other encryption params should be given */
1301 if (dcp->cp_crypt != ZIO_CRYPT_INHERIT ||
1302 dcp->cp_keylocation != NULL ||
1303 dcp->cp_wkey != NULL) {
1304 ret = SET_ERROR(EINVAL);
1305 goto error;
1306 }
1307
1308 /* check that this is an encryption root */
1309 if (dd->dd_object != rddobj) {
1310 ret = SET_ERROR(EINVAL);
1311 goto error;
1312 }
1313
1314 /* check that the parent is encrypted */
1315 if (dd->dd_parent->dd_crypto_obj == 0) {
1316 ret = SET_ERROR(EINVAL);
1317 goto error;
1318 }
1319
1320 /* if we are rewrapping check that both keys are loaded */
1321 if (dcp->cp_cmd == DCP_CMD_INHERIT) {
1322 ret = dmu_objset_check_wkey_loaded(dd);
1323 if (ret != 0)
1324 goto error;
1325
1326 ret = dmu_objset_check_wkey_loaded(dd->dd_parent);
1327 if (ret != 0)
1328 goto error;
1329 }
1330
1331 dsl_dir_rele(dd, FTAG);
1332 return (0);
1333 }
1334
1335 /* handle forcing an encryption root without rewrapping */
1336 if (dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY) {
1337 /* no other encryption params should be given */
1338 if (dcp->cp_crypt != ZIO_CRYPT_INHERIT ||
1339 dcp->cp_keylocation != NULL ||
1340 dcp->cp_wkey != NULL) {
1341 ret = SET_ERROR(EINVAL);
1342 goto error;
1343 }
1344
1345 /* check that this is not an encryption root */
1346 if (dd->dd_object == rddobj) {
1347 ret = SET_ERROR(EINVAL);
1348 goto error;
1349 }
1350
1351 dsl_dir_rele(dd, FTAG);
1352 return (0);
1353 }
1354
1355 /* crypt cannot be changed after creation */
1356 if (dcp->cp_crypt != ZIO_CRYPT_INHERIT) {
1357 ret = SET_ERROR(EINVAL);
1358 goto error;
1359 }
1360
1361 /* we are not inheritting our parent's wkey so we need one ourselves */
1362 if (dcp->cp_wkey == NULL) {
1363 ret = SET_ERROR(EINVAL);
1364 goto error;
1365 }
1366
1367 /* check for a valid keyformat for the new wrapping key */
1368 if (dcp->cp_wkey->wk_keyformat >= ZFS_KEYFORMAT_FORMATS ||
1369 dcp->cp_wkey->wk_keyformat == ZFS_KEYFORMAT_NONE) {
1370 ret = SET_ERROR(EINVAL);
1371 goto error;
1372 }
1373
1374 /*
1375 * If this dataset is not currently an encryption root we need a new
1376 * keylocation for this dataset's new wrapping key. Otherwise we can
1377 * just keep the one we already had.
1378 */
1379 if (dd->dd_object != rddobj && dcp->cp_keylocation == NULL) {
1380 ret = SET_ERROR(EINVAL);
1381 goto error;
1382 }
1383
1384 /* check that the keylocation is valid if it is not NULL */
1385 if (dcp->cp_keylocation != NULL &&
1386 !zfs_prop_valid_keylocation(dcp->cp_keylocation, B_TRUE)) {
1387 ret = SET_ERROR(EINVAL);
1388 goto error;
1389 }
1390
1391 /* passphrases require pbkdf2 salt and iters */
1392 if (dcp->cp_wkey->wk_keyformat == ZFS_KEYFORMAT_PASSPHRASE) {
1393 if (dcp->cp_wkey->wk_salt == 0 ||
1394 dcp->cp_wkey->wk_iters < MIN_PBKDF2_ITERATIONS) {
1395 ret = SET_ERROR(EINVAL);
1396 goto error;
1397 }
1398 } else {
1399 if (dcp->cp_wkey->wk_salt != 0 || dcp->cp_wkey->wk_iters != 0) {
1400 ret = SET_ERROR(EINVAL);
1401 goto error;
1402 }
1403 }
1404
1405 /* make sure the dd's wkey is loaded */
1406 ret = dmu_objset_check_wkey_loaded(dd);
1407 if (ret != 0)
1408 goto error;
1409
1410 dsl_dir_rele(dd, FTAG);
1411
1412 return (0);
1413
1414error:
1415 if (dd != NULL)
1416 dsl_dir_rele(dd, FTAG);
1417
1418 return (ret);
1419}
1420
637f0c60
TC
1421/*
1422 * This function deals with the intricacies of updating wrapping
1423 * key references and encryption roots recursively in the event
1424 * of a call to 'zfs change-key' or 'zfs promote'. The 'skip'
1425 * parameter should always be set to B_FALSE when called
1426 * externally.
1427 */
b5256303
TC
1428static void
1429spa_keystore_change_key_sync_impl(uint64_t rddobj, uint64_t ddobj,
637f0c60
TC
1430 uint64_t new_rddobj, dsl_wrapping_key_t *wkey, boolean_t skip,
1431 dmu_tx_t *tx)
b5256303
TC
1432{
1433 zap_cursor_t *zc;
1434 zap_attribute_t *za;
1435 dsl_pool_t *dp = dmu_tx_pool(tx);
1436 dsl_dir_t *dd = NULL;
1437 dsl_crypto_key_t *dck = NULL;
1438 uint64_t curr_rddobj;
1439
1440 ASSERT(RW_WRITE_HELD(&dp->dp_spa->spa_keystore.sk_wkeys_lock));
1441
1442 /* hold the dd */
1443 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
1444
637f0c60 1445 /* ignore special dsl dirs */
b5256303
TC
1446 if (dd->dd_myname[0] == '$' || dd->dd_myname[0] == '%') {
1447 dsl_dir_rele(dd, FTAG);
1448 return;
1449 }
1450
b135b9f1
TC
1451 /*
1452 * Stop recursing if this dsl dir didn't inherit from the root
1453 * or if this dd is a clone.
1454 */
b5256303 1455 VERIFY0(dsl_dir_get_encryption_root_ddobj(dd, &curr_rddobj));
637f0c60 1456 if (!skip && (curr_rddobj != rddobj || dsl_dir_is_clone(dd))) {
b5256303
TC
1457 dsl_dir_rele(dd, FTAG);
1458 return;
1459 }
1460
1461 /*
1462 * If we don't have a wrapping key just update the dck to reflect the
1463 * new encryption root. Otherwise rewrap the entire dck and re-sync it
637f0c60 1464 * to disk. If skip is set, we don't do any of this work.
b5256303 1465 */
637f0c60
TC
1466 if (!skip) {
1467 if (wkey == NULL) {
1468 VERIFY0(zap_update(dp->dp_meta_objset,
1469 dd->dd_crypto_obj,
1470 DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1,
1471 &new_rddobj, tx));
1472 } else {
1473 VERIFY0(spa_keystore_dsl_key_hold_dd(dp->dp_spa, dd,
1474 FTAG, &dck));
1475 dsl_wrapping_key_hold(wkey, dck);
1476 dsl_wrapping_key_rele(dck->dck_wkey, dck);
1477 dck->dck_wkey = wkey;
1478 dsl_crypto_key_sync(dck, tx);
1479 spa_keystore_dsl_key_rele(dp->dp_spa, dck, FTAG);
1480 }
b5256303
TC
1481 }
1482
1483 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
1484 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1485
b135b9f1 1486 /* Recurse into all child dsl dirs. */
b5256303
TC
1487 for (zap_cursor_init(zc, dp->dp_meta_objset,
1488 dsl_dir_phys(dd)->dd_child_dir_zapobj);
1489 zap_cursor_retrieve(zc, za) == 0;
1490 zap_cursor_advance(zc)) {
1491 spa_keystore_change_key_sync_impl(rddobj,
637f0c60
TC
1492 za->za_first_integer, new_rddobj, wkey, B_FALSE, tx);
1493 }
1494 zap_cursor_fini(zc);
1495
1496 /*
1497 * Recurse into all dsl dirs of clones. We utilize the skip parameter
1498 * here so that we don't attempt to process the clones directly. This
1499 * is because the clone and its origin share the same dck, which has
1500 * already been updated.
1501 */
1502 for (zap_cursor_init(zc, dp->dp_meta_objset,
1503 dsl_dir_phys(dd)->dd_clones);
1504 zap_cursor_retrieve(zc, za) == 0;
1505 zap_cursor_advance(zc)) {
1506 dsl_dataset_t *clone;
1507
1508 VERIFY0(dsl_dataset_hold_obj(dp, za->za_first_integer,
1509 FTAG, &clone));
1510 spa_keystore_change_key_sync_impl(rddobj,
1511 clone->ds_dir->dd_object, new_rddobj, wkey, B_TRUE, tx);
1512 dsl_dataset_rele(clone, FTAG);
b5256303
TC
1513 }
1514 zap_cursor_fini(zc);
1515
b5256303
TC
1516 kmem_free(za, sizeof (zap_attribute_t));
1517 kmem_free(zc, sizeof (zap_cursor_t));
1518
1519 dsl_dir_rele(dd, FTAG);
1520}
1521
1522static void
1523spa_keystore_change_key_sync(void *arg, dmu_tx_t *tx)
1524{
1525 dsl_dataset_t *ds;
1526 avl_index_t where;
1527 dsl_pool_t *dp = dmu_tx_pool(tx);
1528 spa_t *spa = dp->dp_spa;
1529 spa_keystore_change_key_args_t *skcka = arg;
1530 dsl_crypto_params_t *dcp = skcka->skcka_cp;
1531 dsl_wrapping_key_t *wkey = NULL, *found_wkey;
1532 dsl_wrapping_key_t wkey_search;
1533 char *keylocation = dcp->cp_keylocation;
1534 uint64_t rddobj, new_rddobj;
1535
1536 /* create and initialize the wrapping key */
1537 VERIFY0(dsl_dataset_hold(dp, skcka->skcka_dsname, FTAG, &ds));
1538 ASSERT(!ds->ds_is_snapshot);
1539
1540 if (dcp->cp_cmd == DCP_CMD_NEW_KEY ||
1541 dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY) {
1542 /*
1543 * We are changing to a new wkey. Set additional properties
1544 * which can be sent along with this ioctl. Note that this
1545 * command can set keylocation even if it can't normally be
1546 * set via 'zfs set' due to a non-local keylocation.
1547 */
1548 if (dcp->cp_cmd == DCP_CMD_NEW_KEY) {
1549 wkey = dcp->cp_wkey;
1550 wkey->wk_ddobj = ds->ds_dir->dd_object;
1551 } else {
1552 keylocation = "prompt";
1553 }
1554
1555 if (keylocation != NULL) {
1556 dsl_prop_set_sync_impl(ds,
1557 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1558 ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1,
1559 keylocation, tx);
1560 }
1561
1562 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds->ds_dir, &rddobj));
1563 new_rddobj = ds->ds_dir->dd_object;
1564 } else {
1565 /*
1566 * We are inheritting the parent's wkey. Unset any local
1567 * keylocation and grab a reference to the wkey.
1568 */
1569 if (dcp->cp_cmd == DCP_CMD_INHERIT) {
1570 VERIFY0(spa_keystore_wkey_hold_dd(spa,
1571 ds->ds_dir->dd_parent, FTAG, &wkey));
1572 }
1573
1574 dsl_prop_set_sync_impl(ds,
1575 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), ZPROP_SRC_NONE,
1576 0, 0, NULL, tx);
1577
1578 rddobj = ds->ds_dir->dd_object;
62df1bc8
TC
1579 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds->ds_dir->dd_parent,
1580 &new_rddobj));
b5256303
TC
1581 }
1582
1583 if (wkey == NULL) {
1584 ASSERT(dcp->cp_cmd == DCP_CMD_FORCE_INHERIT ||
1585 dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY);
1586 }
1587
1588 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
1589
1590 /* recurse through all children and rewrap their keys */
1591 spa_keystore_change_key_sync_impl(rddobj, ds->ds_dir->dd_object,
637f0c60 1592 new_rddobj, wkey, B_FALSE, tx);
b5256303
TC
1593
1594 /*
1595 * All references to the old wkey should be released now (if it
1596 * existed). Replace the wrapping key.
1597 */
1598 wkey_search.wk_ddobj = ds->ds_dir->dd_object;
1599 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, &wkey_search, NULL);
1600 if (found_wkey != NULL) {
424fd7c3 1601 ASSERT0(zfs_refcount_count(&found_wkey->wk_refcnt));
b5256303
TC
1602 avl_remove(&spa->spa_keystore.sk_wkeys, found_wkey);
1603 dsl_wrapping_key_free(found_wkey);
1604 }
1605
1606 if (dcp->cp_cmd == DCP_CMD_NEW_KEY) {
1607 avl_find(&spa->spa_keystore.sk_wkeys, wkey, &where);
1608 avl_insert(&spa->spa_keystore.sk_wkeys, wkey, where);
1609 } else if (wkey != NULL) {
1610 dsl_wrapping_key_rele(wkey, FTAG);
1611 }
1612
1613 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
1614
1615 dsl_dataset_rele(ds, FTAG);
1616}
1617
1618int
1619spa_keystore_change_key(const char *dsname, dsl_crypto_params_t *dcp)
1620{
1621 spa_keystore_change_key_args_t skcka;
1622
1623 /* initialize the args struct */
1624 skcka.skcka_dsname = dsname;
1625 skcka.skcka_cp = dcp;
1626
1627 /*
1628 * Perform the actual work in syncing context. The blocks modified
1629 * here could be calculated but it would require holding the pool
e1cfd73f 1630 * lock and traversing all of the datasets that will have their keys
b5256303
TC
1631 * changed.
1632 */
1633 return (dsl_sync_task(dsname, spa_keystore_change_key_check,
1634 spa_keystore_change_key_sync, &skcka, 15,
1635 ZFS_SPACE_CHECK_RESERVED));
1636}
1637
1638int
1639dsl_dir_rename_crypt_check(dsl_dir_t *dd, dsl_dir_t *newparent)
1640{
1641 int ret;
1642 uint64_t curr_rddobj, parent_rddobj;
1643
da689887 1644 if (dd->dd_crypto_obj == 0)
b5256303 1645 return (0);
b5256303
TC
1646
1647 ret = dsl_dir_get_encryption_root_ddobj(dd, &curr_rddobj);
1648 if (ret != 0)
1649 goto error;
1650
1651 /*
1652 * if this is not an encryption root, we must make sure we are not
1653 * moving dd to a new encryption root
1654 */
1655 if (dd->dd_object != curr_rddobj) {
1656 ret = dsl_dir_get_encryption_root_ddobj(newparent,
1657 &parent_rddobj);
1658 if (ret != 0)
1659 goto error;
1660
1661 if (parent_rddobj != curr_rddobj) {
1662 ret = SET_ERROR(EACCES);
1663 goto error;
1664 }
1665 }
1666
1667 return (0);
1668
1669error:
1670 return (ret);
1671}
1672
1673/*
1674 * Check to make sure that a promote from targetdd to origindd will not require
1675 * any key rewraps.
1676 */
1677int
1678dsl_dataset_promote_crypt_check(dsl_dir_t *target, dsl_dir_t *origin)
1679{
1680 int ret;
1681 uint64_t rddobj, op_rddobj, tp_rddobj;
1682
1683 /* If the dataset is not encrypted we don't need to check anything */
1684 if (origin->dd_crypto_obj == 0)
1685 return (0);
1686
1687 /*
1688 * If we are not changing the first origin snapshot in a chain
1689 * the encryption root won't change either.
1690 */
1691 if (dsl_dir_is_clone(origin))
1692 return (0);
1693
1694 /*
1695 * If the origin is the encryption root we will update
1696 * the DSL Crypto Key to point to the target instead.
1697 */
1698 ret = dsl_dir_get_encryption_root_ddobj(origin, &rddobj);
1699 if (ret != 0)
1700 return (ret);
1701
1702 if (rddobj == origin->dd_object)
1703 return (0);
1704
1705 /*
1706 * The origin is inheriting its encryption root from its parent.
1707 * Check that the parent of the target has the same encryption root.
1708 */
1709 ret = dsl_dir_get_encryption_root_ddobj(origin->dd_parent, &op_rddobj);
53864800
TC
1710 if (ret == ENOENT)
1711 return (SET_ERROR(EACCES));
1712 else if (ret != 0)
b5256303
TC
1713 return (ret);
1714
1715 ret = dsl_dir_get_encryption_root_ddobj(target->dd_parent, &tp_rddobj);
53864800
TC
1716 if (ret == ENOENT)
1717 return (SET_ERROR(EACCES));
1718 else if (ret != 0)
b5256303
TC
1719 return (ret);
1720
1721 if (op_rddobj != tp_rddobj)
1722 return (SET_ERROR(EACCES));
1723
1724 return (0);
1725}
1726
1727void
1728dsl_dataset_promote_crypt_sync(dsl_dir_t *target, dsl_dir_t *origin,
1729 dmu_tx_t *tx)
1730{
1731 uint64_t rddobj;
1732 dsl_pool_t *dp = target->dd_pool;
1733 dsl_dataset_t *targetds;
1734 dsl_dataset_t *originds;
1735 char *keylocation;
1736
1737 if (origin->dd_crypto_obj == 0)
1738 return;
1739 if (dsl_dir_is_clone(origin))
1740 return;
1741
1742 VERIFY0(dsl_dir_get_encryption_root_ddobj(origin, &rddobj));
1743
1744 if (rddobj != origin->dd_object)
1745 return;
1746
1747 /*
e1cfd73f 1748 * If the target is being promoted to the encryption root update the
b5256303
TC
1749 * DSL Crypto Key and keylocation to reflect that. We also need to
1750 * update the DSL Crypto Keys of all children inheritting their
1751 * encryption root to point to the new target. Otherwise, the check
1752 * function ensured that the encryption root will not change.
1753 */
1754 keylocation = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
1755
1756 VERIFY0(dsl_dataset_hold_obj(dp,
1757 dsl_dir_phys(target)->dd_head_dataset_obj, FTAG, &targetds));
1758 VERIFY0(dsl_dataset_hold_obj(dp,
1759 dsl_dir_phys(origin)->dd_head_dataset_obj, FTAG, &originds));
1760
1761 VERIFY0(dsl_prop_get_dd(origin, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1762 1, ZAP_MAXVALUELEN, keylocation, NULL, B_FALSE));
1763 dsl_prop_set_sync_impl(targetds, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1764 ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1, keylocation, tx);
1765 dsl_prop_set_sync_impl(originds, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1766 ZPROP_SRC_NONE, 0, 0, NULL, tx);
1767
1768 rw_enter(&dp->dp_spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
1769 spa_keystore_change_key_sync_impl(rddobj, origin->dd_object,
637f0c60 1770 target->dd_object, NULL, B_FALSE, tx);
b5256303
TC
1771 rw_exit(&dp->dp_spa->spa_keystore.sk_wkeys_lock);
1772
1773 dsl_dataset_rele(targetds, FTAG);
1774 dsl_dataset_rele(originds, FTAG);
1775 kmem_free(keylocation, ZAP_MAXVALUELEN);
1776}
1777
b5256303 1778int
1fff937a
TC
1779dmu_objset_create_crypt_check(dsl_dir_t *parentdd, dsl_crypto_params_t *dcp,
1780 boolean_t *will_encrypt)
b5256303
TC
1781{
1782 int ret;
1783 uint64_t pcrypt, crypt;
d9c460a0
TC
1784 dsl_crypto_params_t dummy_dcp = { 0 };
1785
1fff937a
TC
1786 if (will_encrypt != NULL)
1787 *will_encrypt = B_FALSE;
1788
d9c460a0
TC
1789 if (dcp == NULL)
1790 dcp = &dummy_dcp;
b5256303
TC
1791
1792 if (dcp->cp_cmd != DCP_CMD_NONE)
1793 return (SET_ERROR(EINVAL));
1794
1795 if (parentdd != NULL) {
1796 ret = dsl_dir_get_crypt(parentdd, &pcrypt);
1797 if (ret != 0)
1798 return (ret);
1799 } else {
1800 pcrypt = ZIO_CRYPT_OFF;
1801 }
1802
1803 crypt = (dcp->cp_crypt == ZIO_CRYPT_INHERIT) ? pcrypt : dcp->cp_crypt;
1804
1805 ASSERT3U(pcrypt, !=, ZIO_CRYPT_INHERIT);
1806 ASSERT3U(crypt, !=, ZIO_CRYPT_INHERIT);
1807
b5256303
TC
1808 /* check for valid dcp with no encryption (inherited or local) */
1809 if (crypt == ZIO_CRYPT_OFF) {
1810 /* Must not specify encryption params */
1811 if (dcp->cp_wkey != NULL ||
1812 (dcp->cp_keylocation != NULL &&
1813 strcmp(dcp->cp_keylocation, "none") != 0))
1814 return (SET_ERROR(EINVAL));
1815
1816 return (0);
1817 }
1818
1fff937a
TC
1819 if (will_encrypt != NULL)
1820 *will_encrypt = B_TRUE;
1821
b5256303
TC
1822 /*
1823 * We will now definitely be encrypting. Check the feature flag. When
1824 * creating the pool the caller will check this for us since we won't
1fff937a 1825 * technically have the feature activated yet.
b5256303
TC
1826 */
1827 if (parentdd != NULL &&
1828 !spa_feature_is_enabled(parentdd->dd_pool->dp_spa,
1829 SPA_FEATURE_ENCRYPTION)) {
1830 return (SET_ERROR(EOPNOTSUPP));
1831 }
1832
eaed8405
TC
1833 /* Check for errata #4 (encryption enabled, bookmark_v2 disabled) */
1834 if (parentdd != NULL &&
1835 !spa_feature_is_enabled(parentdd->dd_pool->dp_spa,
1836 SPA_FEATURE_BOOKMARK_V2)) {
1837 return (SET_ERROR(EOPNOTSUPP));
1838 }
1839
85ce3f4f 1840 /* handle inheritance */
b5256303
TC
1841 if (dcp->cp_wkey == NULL) {
1842 ASSERT3P(parentdd, !=, NULL);
1843
1844 /* key must be fully unspecified */
1845 if (dcp->cp_keylocation != NULL)
1846 return (SET_ERROR(EINVAL));
1847
1848 /* parent must have a key to inherit */
1849 if (pcrypt == ZIO_CRYPT_OFF)
1850 return (SET_ERROR(EINVAL));
1851
1852 /* check for parent key */
1853 ret = dmu_objset_check_wkey_loaded(parentdd);
1854 if (ret != 0)
1855 return (ret);
1856
1857 return (0);
1858 }
1859
1860 /* At this point we should have a fully specified key. Check location */
1861 if (dcp->cp_keylocation == NULL ||
1862 !zfs_prop_valid_keylocation(dcp->cp_keylocation, B_TRUE))
1863 return (SET_ERROR(EINVAL));
1864
1865 /* Must have fully specified keyformat */
1866 switch (dcp->cp_wkey->wk_keyformat) {
1867 case ZFS_KEYFORMAT_HEX:
1868 case ZFS_KEYFORMAT_RAW:
1869 /* requires no pbkdf2 iters and salt */
1870 if (dcp->cp_wkey->wk_salt != 0 || dcp->cp_wkey->wk_iters != 0)
1871 return (SET_ERROR(EINVAL));
1872 break;
1873 case ZFS_KEYFORMAT_PASSPHRASE:
1874 /* requires pbkdf2 iters and salt */
1875 if (dcp->cp_wkey->wk_salt == 0 ||
1876 dcp->cp_wkey->wk_iters < MIN_PBKDF2_ITERATIONS)
1877 return (SET_ERROR(EINVAL));
1878 break;
1879 case ZFS_KEYFORMAT_NONE:
1880 default:
1881 /* keyformat must be specified and valid */
1882 return (SET_ERROR(EINVAL));
1883 }
1884
1885 return (0);
1886}
1887
1888void
1889dsl_dataset_create_crypt_sync(uint64_t dsobj, dsl_dir_t *dd,
1890 dsl_dataset_t *origin, dsl_crypto_params_t *dcp, dmu_tx_t *tx)
1891{
1892 dsl_pool_t *dp = dd->dd_pool;
1893 uint64_t crypt;
1894 dsl_wrapping_key_t *wkey;
1895
1896 /* clones always use their origin's wrapping key */
1897 if (dsl_dir_is_clone(dd)) {
1898 ASSERT3P(dcp, ==, NULL);
1899
1900 /*
1901 * If this is an encrypted clone we just need to clone the
1902 * dck into dd. Zapify the dd so we can do that.
1903 */
1904 if (origin->ds_dir->dd_crypto_obj != 0) {
1905 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1906 dsl_dir_zapify(dd, tx);
1907
1908 dd->dd_crypto_obj =
1909 dsl_crypto_key_clone_sync(origin->ds_dir, tx);
1910 VERIFY0(zap_add(dp->dp_meta_objset, dd->dd_object,
1911 DD_FIELD_CRYPTO_KEY_OBJ, sizeof (uint64_t), 1,
1912 &dd->dd_crypto_obj, tx));
1913 }
1914
1915 return;
1916 }
1917
1918 /*
1919 * A NULL dcp at this point indicates this is the origin dataset
1920 * which does not have an objset to encrypt. Raw receives will handle
b0918402 1921 * encryption separately later. In both cases we can simply return.
b5256303
TC
1922 */
1923 if (dcp == NULL || dcp->cp_cmd == DCP_CMD_RAW_RECV)
1924 return;
1925
1926 crypt = dcp->cp_crypt;
1927 wkey = dcp->cp_wkey;
1928
1929 /* figure out the effective crypt */
1930 if (crypt == ZIO_CRYPT_INHERIT && dd->dd_parent != NULL)
1931 VERIFY0(dsl_dir_get_crypt(dd->dd_parent, &crypt));
1932
1933 /* if we aren't doing encryption just return */
1934 if (crypt == ZIO_CRYPT_OFF || crypt == ZIO_CRYPT_INHERIT)
1935 return;
1936
1937 /* zapify the dd so that we can add the crypto key obj to it */
1938 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1939 dsl_dir_zapify(dd, tx);
1940
1941 /* use the new key if given or inherit from the parent */
1942 if (wkey == NULL) {
1943 VERIFY0(spa_keystore_wkey_hold_dd(dp->dp_spa,
1944 dd->dd_parent, FTAG, &wkey));
1945 } else {
1946 wkey->wk_ddobj = dd->dd_object;
1947 }
1948
4807c0ba
TC
1949 ASSERT3P(wkey, !=, NULL);
1950
b5256303
TC
1951 /* Create or clone the DSL crypto key and activate the feature */
1952 dd->dd_crypto_obj = dsl_crypto_key_create_sync(crypt, wkey, tx);
1953 VERIFY0(zap_add(dp->dp_meta_objset, dd->dd_object,
1954 DD_FIELD_CRYPTO_KEY_OBJ, sizeof (uint64_t), 1, &dd->dd_crypto_obj,
1955 tx));
d52d80b7
PD
1956 dsl_dataset_activate_feature(dsobj, SPA_FEATURE_ENCRYPTION,
1957 (void *)B_TRUE, tx);
b5256303
TC
1958
1959 /*
1960 * If we inherited the wrapping key we release our reference now.
1961 * Otherwise, this is a new key and we need to load it into the
1962 * keystore.
1963 */
1964 if (dcp->cp_wkey == NULL) {
1965 dsl_wrapping_key_rele(wkey, FTAG);
1966 } else {
1967 VERIFY0(spa_keystore_load_wkey_impl(dp->dp_spa, wkey));
1968 }
1969}
1970
1971typedef struct dsl_crypto_recv_key_arg {
1972 uint64_t dcrka_dsobj;
f00ab3f2 1973 uint64_t dcrka_fromobj;
b5256303 1974 dmu_objset_type_t dcrka_ostype;
b0918402
TC
1975 nvlist_t *dcrka_nvl;
1976 boolean_t dcrka_do_key;
b5256303
TC
1977} dsl_crypto_recv_key_arg_t;
1978
b0918402 1979static int
f00ab3f2
TC
1980dsl_crypto_recv_raw_objset_check(dsl_dataset_t *ds, dsl_dataset_t *fromds,
1981 dmu_objset_type_t ostype, nvlist_t *nvl, dmu_tx_t *tx)
b5256303
TC
1982{
1983 int ret;
b5256303
TC
1984 objset_t *os;
1985 dnode_t *mdn;
b5256303
TC
1986 uint8_t *buf = NULL;
1987 uint_t len;
f00ab3f2
TC
1988 uint64_t intval, nlevels, blksz, ibs;
1989 uint64_t nblkptr, maxblkid;
b5256303 1990
b0918402
TC
1991 if (ostype != DMU_OST_ZFS && ostype != DMU_OST_ZVOL)
1992 return (SET_ERROR(EINVAL));
b5256303
TC
1993
1994 /* raw receives also need info about the structure of the metadnode */
b5256303 1995 ret = nvlist_lookup_uint64(nvl, "mdn_compress", &intval);
b0918402
TC
1996 if (ret != 0 || intval >= ZIO_COMPRESS_LEGACY_FUNCTIONS)
1997 return (SET_ERROR(EINVAL));
1998
1999 ret = nvlist_lookup_uint64(nvl, "mdn_checksum", &intval);
2000 if (ret != 0 || intval >= ZIO_CHECKSUM_LEGACY_FUNCTIONS)
2001 return (SET_ERROR(EINVAL));
b5256303
TC
2002
2003 ret = nvlist_lookup_uint64(nvl, "mdn_nlevels", &nlevels);
b0918402
TC
2004 if (ret != 0 || nlevels > DN_MAX_LEVELS)
2005 return (SET_ERROR(EINVAL));
b5256303
TC
2006
2007 ret = nvlist_lookup_uint64(nvl, "mdn_blksz", &blksz);
b0918402
TC
2008 if (ret != 0 || blksz < SPA_MINBLOCKSIZE)
2009 return (SET_ERROR(EINVAL));
2010 else if (blksz > spa_maxblocksize(tx->tx_pool->dp_spa))
2011 return (SET_ERROR(ENOTSUP));
b5256303
TC
2012
2013 ret = nvlist_lookup_uint64(nvl, "mdn_indblkshift", &ibs);
b0918402
TC
2014 if (ret != 0 || ibs < DN_MIN_INDBLKSHIFT || ibs > DN_MAX_INDBLKSHIFT)
2015 return (SET_ERROR(ENOTSUP));
b5256303
TC
2016
2017 ret = nvlist_lookup_uint64(nvl, "mdn_nblkptr", &nblkptr);
b0918402
TC
2018 if (ret != 0 || nblkptr != DN_MAX_NBLKPTR)
2019 return (SET_ERROR(ENOTSUP));
b5256303 2020
ae76f45c 2021 ret = nvlist_lookup_uint64(nvl, "mdn_maxblkid", &maxblkid);
b0918402
TC
2022 if (ret != 0)
2023 return (SET_ERROR(EINVAL));
2024
2025 ret = nvlist_lookup_uint8_array(nvl, "portable_mac", &buf, &len);
2026 if (ret != 0 || len != ZIO_OBJSET_MAC_LEN)
2027 return (SET_ERROR(EINVAL));
ae76f45c 2028
b5256303
TC
2029 ret = dmu_objset_from_ds(ds, &os);
2030 if (ret != 0)
b0918402 2031 return (ret);
b5256303
TC
2032
2033 /*
2034 * Useraccounting is not portable and must be done with the keys loaded.
2035 * Therefore, whenever we do any kind of receive the useraccounting
2036 * must not be present.
2037 */
2038 ASSERT0(os->os_flags & OBJSET_FLAG_USERACCOUNTING_COMPLETE);
2039 ASSERT0(os->os_flags & OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
2040
2041 mdn = DMU_META_DNODE(os);
2042
2043 /*
b0918402 2044 * If we already created the objset, make sure its unchangeable
b5256303
TC
2045 * properties match the ones received in the nvlist.
2046 */
2047 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2048 if (!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)) &&
2049 (mdn->dn_nlevels != nlevels || mdn->dn_datablksz != blksz ||
2050 mdn->dn_indblkshift != ibs || mdn->dn_nblkptr != nblkptr)) {
b0918402
TC
2051 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2052 return (SET_ERROR(EINVAL));
b5256303
TC
2053 }
2054 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2055
f00ab3f2
TC
2056 /*
2057 * Check that the ivset guid of the fromds matches the one from the
2058 * send stream. Older versions of the encryption code did not have
2059 * an ivset guid on the from dataset and did not send one in the
2060 * stream. For these streams we provide the
2061 * zfs_disable_ivset_guid_check tunable to allow these datasets to
2062 * be received with a generated ivset guid.
2063 */
2064 if (fromds != NULL && !zfs_disable_ivset_guid_check) {
2065 uint64_t from_ivset_guid = 0;
2066 intval = 0;
2067
2068 (void) nvlist_lookup_uint64(nvl, "from_ivset_guid", &intval);
2069 (void) zap_lookup(tx->tx_pool->dp_meta_objset,
2070 fromds->ds_object, DS_FIELD_IVSET_GUID,
2071 sizeof (from_ivset_guid), 1, &from_ivset_guid);
2072
2073 if (intval == 0 || from_ivset_guid == 0)
2074 return (SET_ERROR(ZFS_ERR_FROM_IVSET_GUID_MISSING));
2075
2076 if (intval != from_ivset_guid)
2077 return (SET_ERROR(ZFS_ERR_FROM_IVSET_GUID_MISMATCH));
2078 }
2079
b5256303 2080 return (0);
b5256303
TC
2081}
2082
2083static void
b0918402
TC
2084dsl_crypto_recv_raw_objset_sync(dsl_dataset_t *ds, dmu_objset_type_t ostype,
2085 nvlist_t *nvl, dmu_tx_t *tx)
b5256303 2086{
b5256303 2087 dsl_pool_t *dp = tx->tx_pool;
b5256303
TC
2088 objset_t *os;
2089 dnode_t *mdn;
b0918402
TC
2090 zio_t *zio;
2091 uint8_t *portable_mac;
b5256303 2092 uint_t len;
ae76f45c 2093 uint64_t compress, checksum, nlevels, blksz, ibs, maxblkid;
b0918402 2094 boolean_t newds = B_FALSE;
b5256303 2095
b5256303
TC
2096 VERIFY0(dmu_objset_from_ds(ds, &os));
2097 mdn = DMU_META_DNODE(os);
2098
f00ab3f2
TC
2099 /*
2100 * Fetch the values we need from the nvlist. "to_ivset_guid" must
2101 * be set on the snapshot, which doesn't exist yet. The receive
2102 * code will take care of this for us later.
2103 */
b5256303
TC
2104 compress = fnvlist_lookup_uint64(nvl, "mdn_compress");
2105 checksum = fnvlist_lookup_uint64(nvl, "mdn_checksum");
2106 nlevels = fnvlist_lookup_uint64(nvl, "mdn_nlevels");
2107 blksz = fnvlist_lookup_uint64(nvl, "mdn_blksz");
2108 ibs = fnvlist_lookup_uint64(nvl, "mdn_indblkshift");
ae76f45c 2109 maxblkid = fnvlist_lookup_uint64(nvl, "mdn_maxblkid");
b0918402
TC
2110 VERIFY0(nvlist_lookup_uint8_array(nvl, "portable_mac", &portable_mac,
2111 &len));
b5256303
TC
2112
2113 /* if we haven't created an objset for the ds yet, do that now */
2114 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2115 if (BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) {
2116 (void) dmu_objset_create_impl_dnstats(dp->dp_spa, ds,
b0918402
TC
2117 dsl_dataset_get_blkptr(ds), ostype, nlevels, blksz,
2118 ibs, tx);
2119 newds = B_TRUE;
b5256303
TC
2120 }
2121 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2122
2123 /*
2124 * Set the portable MAC. The local MAC will always be zero since the
2125 * incoming data will all be portable and user accounting will be
2126 * deferred until the next mount. Afterwards, flag the os to be
2127 * written out raw next time.
2128 */
2129 arc_release(os->os_phys_buf, &os->os_phys_buf);
2130 bcopy(portable_mac, os->os_phys->os_portable_mac, ZIO_OBJSET_MAC_LEN);
2131 bzero(os->os_phys->os_local_mac, ZIO_OBJSET_MAC_LEN);
1b66810b 2132 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
b5256303
TC
2133
2134 /* set metadnode compression and checksum */
2135 mdn->dn_compress = compress;
2136 mdn->dn_checksum = checksum;
ae76f45c
TC
2137
2138 rw_enter(&mdn->dn_struct_rwlock, RW_WRITER);
369aa501 2139 dnode_new_blkid(mdn, maxblkid, tx, B_FALSE, B_TRUE);
ae76f45c
TC
2140 rw_exit(&mdn->dn_struct_rwlock);
2141
b0918402
TC
2142 /*
2143 * We can't normally dirty the dataset in syncing context unless
2144 * we are creating a new dataset. In this case, we perform a
2145 * pseudo txg sync here instead.
2146 */
2147 if (newds) {
2148 dsl_dataset_dirty(ds, tx);
2149 } else {
2150 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
2151 dsl_dataset_sync(ds, zio, tx);
2152 VERIFY0(zio_wait(zio));
2153
2154 /* dsl_dataset_sync_done will drop this reference. */
2155 dmu_buf_add_ref(ds->ds_dbuf, ds);
2156 dsl_dataset_sync_done(ds, tx);
2157 }
2158}
2159
2160int
2161dsl_crypto_recv_raw_key_check(dsl_dataset_t *ds, nvlist_t *nvl, dmu_tx_t *tx)
2162{
2163 int ret;
2164 objset_t *mos = tx->tx_pool->dp_meta_objset;
2165 uint8_t *buf = NULL;
2166 uint_t len;
f00ab3f2 2167 uint64_t intval, key_guid, version;
b0918402
TC
2168 boolean_t is_passphrase = B_FALSE;
2169
2170 ASSERT(dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT);
2171
2172 /*
2173 * Read and check all the encryption values from the nvlist. We need
2174 * all of the fields of a DSL Crypto Key, as well as a fully specified
2175 * wrapping key.
2176 */
2177 ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE, &intval);
2178 if (ret != 0 || intval >= ZIO_CRYPT_FUNCTIONS ||
2179 intval <= ZIO_CRYPT_OFF)
2180 return (SET_ERROR(EINVAL));
2181
2182 ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_GUID, &intval);
2183 if (ret != 0)
2184 return (SET_ERROR(EINVAL));
2185
2186 /*
2187 * If this is an incremental receive make sure the given key guid
2188 * matches the one we already have.
2189 */
2190 if (ds->ds_dir->dd_crypto_obj != 0) {
2191 ret = zap_lookup(mos, ds->ds_dir->dd_crypto_obj,
f00ab3f2 2192 DSL_CRYPTO_KEY_GUID, 8, 1, &key_guid);
b0918402
TC
2193 if (ret != 0)
2194 return (ret);
f00ab3f2 2195 if (intval != key_guid)
b0918402
TC
2196 return (SET_ERROR(EACCES));
2197 }
2198
2199 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,
2200 &buf, &len);
2201 if (ret != 0 || len != MASTER_KEY_MAX_LEN)
2202 return (SET_ERROR(EINVAL));
2203
2204 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,
2205 &buf, &len);
2206 if (ret != 0 || len != SHA512_HMAC_KEYLEN)
2207 return (SET_ERROR(EINVAL));
2208
2209 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_IV, &buf, &len);
2210 if (ret != 0 || len != WRAPPING_IV_LEN)
2211 return (SET_ERROR(EINVAL));
2212
2213 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, &buf, &len);
2214 if (ret != 0 || len != WRAPPING_MAC_LEN)
2215 return (SET_ERROR(EINVAL));
2216
2217 /*
2218 * We don't support receiving old on-disk formats. The version 0
2219 * implementation protected several fields in an objset that were
2220 * not always portable during a raw receive. As a result, we call
2221 * the old version an on-disk errata #3.
2222 */
2223 ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_VERSION, &version);
2224 if (ret != 0 || version != ZIO_CRYPT_KEY_CURRENT_VERSION)
2225 return (SET_ERROR(ENOTSUP));
2226
2227 ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_KEYFORMAT),
2228 &intval);
2229 if (ret != 0 || intval >= ZFS_KEYFORMAT_FORMATS ||
2230 intval == ZFS_KEYFORMAT_NONE)
2231 return (SET_ERROR(EINVAL));
2232
2233 is_passphrase = (intval == ZFS_KEYFORMAT_PASSPHRASE);
2234
2235 /*
2236 * for raw receives we allow any number of pbkdf2iters since there
2237 * won't be a chance for the user to change it.
2238 */
2239 ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS),
2240 &intval);
2241 if (ret != 0 || (is_passphrase == (intval == 0)))
2242 return (SET_ERROR(EINVAL));
2243
2244 ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT),
2245 &intval);
2246 if (ret != 0 || (is_passphrase == (intval == 0)))
2247 return (SET_ERROR(EINVAL));
2248
2249 return (0);
2250}
2251
2252void
2253dsl_crypto_recv_raw_key_sync(dsl_dataset_t *ds, nvlist_t *nvl, dmu_tx_t *tx)
2254{
2255 dsl_pool_t *dp = tx->tx_pool;
2256 objset_t *mos = dp->dp_meta_objset;
2257 dsl_dir_t *dd = ds->ds_dir;
2258 uint_t len;
2259 uint64_t rddobj, one = 1;
2260 uint8_t *keydata, *hmac_keydata, *iv, *mac;
f00ab3f2 2261 uint64_t crypt, key_guid, keyformat, iters, salt;
b0918402
TC
2262 uint64_t version = ZIO_CRYPT_KEY_CURRENT_VERSION;
2263 char *keylocation = "prompt";
2264
2265 /* lookup the values we need to create the DSL Crypto Key */
2266 crypt = fnvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE);
f00ab3f2 2267 key_guid = fnvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_GUID);
b0918402
TC
2268 keyformat = fnvlist_lookup_uint64(nvl,
2269 zfs_prop_to_name(ZFS_PROP_KEYFORMAT));
2270 iters = fnvlist_lookup_uint64(nvl,
2271 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS));
2272 salt = fnvlist_lookup_uint64(nvl,
2273 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT));
2274 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,
2275 &keydata, &len));
2276 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,
2277 &hmac_keydata, &len));
2278 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_IV, &iv, &len));
2279 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, &mac, &len));
b5256303
TC
2280
2281 /* if this is a new dataset setup the DSL Crypto Key. */
b0918402 2282 if (dd->dd_crypto_obj == 0) {
b5256303 2283 /* zapify the dsl dir so we can add the key object to it */
b0918402
TC
2284 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2285 dsl_dir_zapify(dd, tx);
b5256303
TC
2286
2287 /* create the DSL Crypto Key on disk and activate the feature */
b0918402 2288 dd->dd_crypto_obj = zap_create(mos,
b5256303
TC
2289 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
2290 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
b0918402 2291 dd->dd_crypto_obj, DSL_CRYPTO_KEY_REFCOUNT,
b5256303 2292 sizeof (uint64_t), 1, &one, tx));
ae76f45c 2293 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
b0918402 2294 dd->dd_crypto_obj, DSL_CRYPTO_KEY_VERSION,
ae76f45c 2295 sizeof (uint64_t), 1, &version, tx));
b5256303 2296
b0918402 2297 dsl_dataset_activate_feature(ds->ds_object,
d52d80b7
PD
2298 SPA_FEATURE_ENCRYPTION, (void *)B_TRUE, tx);
2299 ds->ds_feature[SPA_FEATURE_ENCRYPTION] = (void *)B_TRUE;
b5256303
TC
2300
2301 /* save the dd_crypto_obj on disk */
b0918402
TC
2302 VERIFY0(zap_add(mos, dd->dd_object, DD_FIELD_CRYPTO_KEY_OBJ,
2303 sizeof (uint64_t), 1, &dd->dd_crypto_obj, tx));
b5256303
TC
2304
2305 /*
2306 * Set the keylocation to prompt by default. If keylocation
b0918402 2307 * has been provided via the properties, this will be overridden
b5256303
TC
2308 * later.
2309 */
2310 dsl_prop_set_sync_impl(ds,
2311 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2312 ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1,
2313 keylocation, tx);
2314
b0918402 2315 rddobj = dd->dd_object;
b5256303 2316 } else {
b0918402 2317 VERIFY0(dsl_dir_get_encryption_root_ddobj(dd, &rddobj));
b5256303
TC
2318 }
2319
2320 /* sync the key data to the ZAP object on disk */
b0918402 2321 dsl_crypto_key_sync_impl(mos, dd->dd_crypto_obj, crypt,
f00ab3f2 2322 rddobj, key_guid, iv, mac, keydata, hmac_keydata, keyformat, salt,
b5256303 2323 iters, tx);
b0918402 2324}
b5256303 2325
b0918402
TC
2326int
2327dsl_crypto_recv_key_check(void *arg, dmu_tx_t *tx)
2328{
2329 int ret;
2330 dsl_crypto_recv_key_arg_t *dcrka = arg;
f00ab3f2 2331 dsl_dataset_t *ds = NULL, *fromds = NULL;
b0918402
TC
2332
2333 ret = dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_dsobj,
2334 FTAG, &ds);
2335 if (ret != 0)
f00ab3f2 2336 goto out;
b0918402 2337
f00ab3f2
TC
2338 if (dcrka->dcrka_fromobj != 0) {
2339 ret = dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_fromobj,
2340 FTAG, &fromds);
2341 if (ret != 0)
2342 goto out;
2343 }
2344
2345 ret = dsl_crypto_recv_raw_objset_check(ds, fromds,
b0918402
TC
2346 dcrka->dcrka_ostype, dcrka->dcrka_nvl, tx);
2347 if (ret != 0)
f00ab3f2 2348 goto out;
b0918402
TC
2349
2350 /*
2351 * We run this check even if we won't be doing this part of
2352 * the receive now so that we don't make the user wait until
2353 * the receive finishes to fail.
2354 */
2355 ret = dsl_crypto_recv_raw_key_check(ds, dcrka->dcrka_nvl, tx);
2356 if (ret != 0)
f00ab3f2 2357 goto out;
b0918402 2358
f00ab3f2 2359out:
b0918402
TC
2360 if (ds != NULL)
2361 dsl_dataset_rele(ds, FTAG);
f00ab3f2
TC
2362 if (fromds != NULL)
2363 dsl_dataset_rele(fromds, FTAG);
b0918402
TC
2364 return (ret);
2365}
2366
2367void
2368dsl_crypto_recv_key_sync(void *arg, dmu_tx_t *tx)
2369{
2370 dsl_crypto_recv_key_arg_t *dcrka = arg;
2371 dsl_dataset_t *ds;
2372
2373 VERIFY0(dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_dsobj,
2374 FTAG, &ds));
2375 dsl_crypto_recv_raw_objset_sync(ds, dcrka->dcrka_ostype,
2376 dcrka->dcrka_nvl, tx);
2377 if (dcrka->dcrka_do_key)
2378 dsl_crypto_recv_raw_key_sync(ds, dcrka->dcrka_nvl, tx);
b5256303
TC
2379 dsl_dataset_rele(ds, FTAG);
2380}
2381
2382/*
2383 * This function is used to sync an nvlist representing a DSL Crypto Key and
2384 * the associated encryption parameters. The key will be written exactly as is
2385 * without wrapping it.
2386 */
2387int
f00ab3f2 2388dsl_crypto_recv_raw(const char *poolname, uint64_t dsobj, uint64_t fromobj,
b0918402 2389 dmu_objset_type_t ostype, nvlist_t *nvl, boolean_t do_key)
b5256303
TC
2390{
2391 dsl_crypto_recv_key_arg_t dcrka;
2392
2393 dcrka.dcrka_dsobj = dsobj;
f00ab3f2 2394 dcrka.dcrka_fromobj = fromobj;
b5256303 2395 dcrka.dcrka_ostype = ostype;
b0918402
TC
2396 dcrka.dcrka_nvl = nvl;
2397 dcrka.dcrka_do_key = do_key;
b5256303
TC
2398
2399 return (dsl_sync_task(poolname, dsl_crypto_recv_key_check,
2400 dsl_crypto_recv_key_sync, &dcrka, 1, ZFS_SPACE_CHECK_NORMAL));
2401}
2402
2403int
f00ab3f2
TC
2404dsl_crypto_populate_key_nvlist(dsl_dataset_t *ds, uint64_t from_ivset_guid,
2405 nvlist_t **nvl_out)
b5256303
TC
2406{
2407 int ret;
2408 objset_t *os;
2409 dnode_t *mdn;
2410 uint64_t rddobj;
2411 nvlist_t *nvl = NULL;
2412 uint64_t dckobj = ds->ds_dir->dd_crypto_obj;
2637dda8 2413 dsl_dir_t *rdd = NULL;
b5256303
TC
2414 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2415 objset_t *mos = dp->dp_meta_objset;
f00ab3f2 2416 uint64_t crypt = 0, key_guid = 0, format = 0;
ae76f45c 2417 uint64_t iters = 0, salt = 0, version = 0;
f00ab3f2 2418 uint64_t to_ivset_guid = 0;
b5256303
TC
2419 uint8_t raw_keydata[MASTER_KEY_MAX_LEN];
2420 uint8_t raw_hmac_keydata[SHA512_HMAC_KEYLEN];
2421 uint8_t iv[WRAPPING_IV_LEN];
2422 uint8_t mac[WRAPPING_MAC_LEN];
2423
2424 ASSERT(dckobj != 0);
2425
2426 VERIFY0(dmu_objset_from_ds(ds, &os));
2427 mdn = DMU_META_DNODE(os);
2428
2429 ret = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
2430 if (ret != 0)
2431 goto error;
2432
2433 /* lookup values from the DSL Crypto Key */
b5256303
TC
2434 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,
2435 &crypt);
2436 if (ret != 0)
2437 goto error;
2438
f00ab3f2 2439 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1, &key_guid);
b5256303
TC
2440 if (ret != 0)
2441 goto error;
2442
2443 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,
2444 MASTER_KEY_MAX_LEN, raw_keydata);
2445 if (ret != 0)
2446 goto error;
2447
2448 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,
2449 SHA512_HMAC_KEYLEN, raw_hmac_keydata);
2450 if (ret != 0)
2451 goto error;
2452
2453 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,
2454 iv);
2455 if (ret != 0)
2456 goto error;
2457
2458 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,
2459 mac);
2460 if (ret != 0)
2461 goto error;
2462
f00ab3f2
TC
2463 /* see zfs_disable_ivset_guid_check tunable for errata info */
2464 ret = zap_lookup(mos, ds->ds_object, DS_FIELD_IVSET_GUID, 8, 1,
2465 &to_ivset_guid);
2466 if (ret != 0)
2467 ASSERT3U(dp->dp_spa->spa_errata, !=, 0);
2468
ae76f45c
TC
2469 /*
2470 * We don't support raw sends of legacy on-disk formats. See the
2471 * comment in dsl_crypto_recv_key_check() for details.
2472 */
2473 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_VERSION, 8, 1, &version);
2474 if (ret != 0 || version != ZIO_CRYPT_KEY_CURRENT_VERSION) {
2475 dp->dp_spa->spa_errata = ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;
2476 ret = SET_ERROR(ENOTSUP);
2477 goto error;
2478 }
2479
2637dda8
TC
2480 /*
2481 * Lookup wrapping key properties. An early version of the code did
2482 * not correctly add these values to the wrapping key or the DSL
2483 * Crypto Key on disk for non encryption roots, so to be safe we
2484 * always take the slightly circuitous route of looking it up from
2485 * the encryption root's key.
2486 */
2487 ret = dsl_dir_get_encryption_root_ddobj(ds->ds_dir, &rddobj);
b5256303
TC
2488 if (ret != 0)
2489 goto error;
2490
2637dda8
TC
2491 dsl_pool_config_enter(dp, FTAG);
2492
2493 ret = dsl_dir_hold_obj(dp, rddobj, NULL, FTAG, &rdd);
2494 if (ret != 0)
2495 goto error_unlock;
2496
2497 ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,
2498 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &format);
2499 if (ret != 0)
2500 goto error_unlock;
2501
b5256303 2502 if (format == ZFS_KEYFORMAT_PASSPHRASE) {
2637dda8 2503 ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,
b5256303
TC
2504 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &iters);
2505 if (ret != 0)
2637dda8 2506 goto error_unlock;
b5256303 2507
2637dda8 2508 ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,
b5256303
TC
2509 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &salt);
2510 if (ret != 0)
2637dda8 2511 goto error_unlock;
b5256303
TC
2512 }
2513
2637dda8
TC
2514 dsl_dir_rele(rdd, FTAG);
2515 dsl_pool_config_exit(dp, FTAG);
2516
b5256303 2517 fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE, crypt);
f00ab3f2 2518 fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_GUID, key_guid);
ae76f45c 2519 fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_VERSION, version);
b5256303
TC
2520 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,
2521 raw_keydata, MASTER_KEY_MAX_LEN));
2522 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,
2523 raw_hmac_keydata, SHA512_HMAC_KEYLEN));
2524 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_IV, iv,
2525 WRAPPING_IV_LEN));
2526 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, mac,
2527 WRAPPING_MAC_LEN));
2528 VERIFY0(nvlist_add_uint8_array(nvl, "portable_mac",
2529 os->os_phys->os_portable_mac, ZIO_OBJSET_MAC_LEN));
2530 fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_KEYFORMAT), format);
2531 fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), iters);
2532 fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), salt);
2533 fnvlist_add_uint64(nvl, "mdn_checksum", mdn->dn_checksum);
2534 fnvlist_add_uint64(nvl, "mdn_compress", mdn->dn_compress);
2535 fnvlist_add_uint64(nvl, "mdn_nlevels", mdn->dn_nlevels);
2536 fnvlist_add_uint64(nvl, "mdn_blksz", mdn->dn_datablksz);
2537 fnvlist_add_uint64(nvl, "mdn_indblkshift", mdn->dn_indblkshift);
2538 fnvlist_add_uint64(nvl, "mdn_nblkptr", mdn->dn_nblkptr);
ae76f45c 2539 fnvlist_add_uint64(nvl, "mdn_maxblkid", mdn->dn_maxblkid);
f00ab3f2
TC
2540 fnvlist_add_uint64(nvl, "to_ivset_guid", to_ivset_guid);
2541 fnvlist_add_uint64(nvl, "from_ivset_guid", from_ivset_guid);
b5256303
TC
2542
2543 *nvl_out = nvl;
2544 return (0);
2545
2637dda8
TC
2546error_unlock:
2547 dsl_pool_config_exit(dp, FTAG);
b5256303 2548error:
2637dda8
TC
2549 if (rdd != NULL)
2550 dsl_dir_rele(rdd, FTAG);
b5256303
TC
2551 nvlist_free(nvl);
2552
2553 *nvl_out = NULL;
2554 return (ret);
2555}
2556
2557uint64_t
2558dsl_crypto_key_create_sync(uint64_t crypt, dsl_wrapping_key_t *wkey,
2559 dmu_tx_t *tx)
2560{
2561 dsl_crypto_key_t dck;
ae76f45c
TC
2562 uint64_t version = ZIO_CRYPT_KEY_CURRENT_VERSION;
2563 uint64_t one = 1ULL;
b5256303
TC
2564
2565 ASSERT(dmu_tx_is_syncing(tx));
2566 ASSERT3U(crypt, <, ZIO_CRYPT_FUNCTIONS);
2567 ASSERT3U(crypt, >, ZIO_CRYPT_OFF);
2568
2569 /* create the DSL Crypto Key ZAP object */
2570 dck.dck_obj = zap_create(tx->tx_pool->dp_meta_objset,
2571 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
2572
2573 /* fill in the key (on the stack) and sync it to disk */
2574 dck.dck_wkey = wkey;
2575 VERIFY0(zio_crypt_key_init(crypt, &dck.dck_key));
2576
2577 dsl_crypto_key_sync(&dck, tx);
2578 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, dck.dck_obj,
2579 DSL_CRYPTO_KEY_REFCOUNT, sizeof (uint64_t), 1, &one, tx));
ae76f45c
TC
2580 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, dck.dck_obj,
2581 DSL_CRYPTO_KEY_VERSION, sizeof (uint64_t), 1, &version, tx));
b5256303
TC
2582
2583 zio_crypt_key_destroy(&dck.dck_key);
2584 bzero(&dck.dck_key, sizeof (zio_crypt_key_t));
2585
2586 return (dck.dck_obj);
2587}
2588
2589uint64_t
2590dsl_crypto_key_clone_sync(dsl_dir_t *origindd, dmu_tx_t *tx)
2591{
2592 objset_t *mos = tx->tx_pool->dp_meta_objset;
2593
2594 ASSERT(dmu_tx_is_syncing(tx));
2595
2596 VERIFY0(zap_increment(mos, origindd->dd_crypto_obj,
2597 DSL_CRYPTO_KEY_REFCOUNT, 1, tx));
2598
2599 return (origindd->dd_crypto_obj);
2600}
2601
2602void
2603dsl_crypto_key_destroy_sync(uint64_t dckobj, dmu_tx_t *tx)
2604{
2605 objset_t *mos = tx->tx_pool->dp_meta_objset;
2606 uint64_t refcnt;
2607
2608 /* Decrement the refcount, destroy if this is the last reference */
2609 VERIFY0(zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_REFCOUNT,
2610 sizeof (uint64_t), 1, &refcnt));
2611
2612 if (refcnt != 1) {
2613 VERIFY0(zap_increment(mos, dckobj, DSL_CRYPTO_KEY_REFCOUNT,
2614 -1, tx));
2615 } else {
2616 VERIFY0(zap_destroy(mos, dckobj, tx));
2617 }
2618}
2619
2620void
2621dsl_dataset_crypt_stats(dsl_dataset_t *ds, nvlist_t *nv)
2622{
2623 uint64_t intval;
2624 dsl_dir_t *dd = ds->ds_dir;
2625 dsl_dir_t *enc_root;
2626 char buf[ZFS_MAX_DATASET_NAME_LEN];
2627
2628 if (dd->dd_crypto_obj == 0)
2629 return;
2630
2631 intval = dsl_dataset_get_keystatus(dd);
2632 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEYSTATUS, intval);
2633
2634 if (dsl_dir_get_crypt(dd, &intval) == 0)
2635 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_ENCRYPTION, intval);
2636 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2637 DSL_CRYPTO_KEY_GUID, 8, 1, &intval) == 0) {
2638 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEY_GUID, intval);
2639 }
2640 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2641 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &intval) == 0) {
2642 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEYFORMAT, intval);
2643 }
2644 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2645 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &intval) == 0) {
2646 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_PBKDF2_SALT, intval);
2647 }
2648 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2649 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &intval) == 0) {
2650 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_PBKDF2_ITERS, intval);
2651 }
f00ab3f2
TC
2652 if (zap_lookup(dd->dd_pool->dp_meta_objset, ds->ds_object,
2653 DS_FIELD_IVSET_GUID, 8, 1, &intval) == 0) {
2654 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_IVSET_GUID, intval);
2655 }
b5256303
TC
2656
2657 if (dsl_dir_get_encryption_root_ddobj(dd, &intval) == 0) {
52a83dc6
TC
2658 if (dsl_dir_hold_obj(dd->dd_pool, intval, NULL, FTAG,
2659 &enc_root) == 0) {
2660 dsl_dir_name(enc_root, buf);
2661 dsl_dir_rele(enc_root, FTAG);
2662 dsl_prop_nvlist_add_string(nv,
2663 ZFS_PROP_ENCRYPTION_ROOT, buf);
2664 }
b5256303
TC
2665 }
2666}
2667
2668int
2669spa_crypt_get_salt(spa_t *spa, uint64_t dsobj, uint8_t *salt)
2670{
2671 int ret;
2672 dsl_crypto_key_t *dck = NULL;
2673
2674 /* look up the key from the spa's keystore */
2675 ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);
2676 if (ret != 0)
2677 goto error;
2678
2679 ret = zio_crypt_key_get_salt(&dck->dck_key, salt);
2680 if (ret != 0)
2681 goto error;
2682
2683 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2684 return (0);
2685
2686error:
2687 if (dck != NULL)
2688 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2689 return (ret);
2690}
2691
2692/*
2693 * Objset blocks are a special case for MAC generation. These blocks have 2
2694 * 256-bit MACs which are embedded within the block itself, rather than a
2695 * single 128 bit MAC. As a result, this function handles encoding and decoding
2696 * the MACs on its own, unlike other functions in this file.
2697 */
2698int
2699spa_do_crypt_objset_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj,
2700 abd_t *abd, uint_t datalen, boolean_t byteswap)
2701{
2702 int ret;
2703 dsl_crypto_key_t *dck = NULL;
2704 void *buf = abd_borrow_buf_copy(abd, datalen);
2705 objset_phys_t *osp = buf;
2706 uint8_t portable_mac[ZIO_OBJSET_MAC_LEN];
2707 uint8_t local_mac[ZIO_OBJSET_MAC_LEN];
2708
2709 /* look up the key from the spa's keystore */
2710 ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);
2711 if (ret != 0)
2712 goto error;
2713
2714 /* calculate both HMACs */
2715 ret = zio_crypt_do_objset_hmacs(&dck->dck_key, buf, datalen,
2716 byteswap, portable_mac, local_mac);
2717 if (ret != 0)
2718 goto error;
2719
2720 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2721
2722 /* if we are generating encode the HMACs in the objset_phys_t */
2723 if (generate) {
2724 bcopy(portable_mac, osp->os_portable_mac, ZIO_OBJSET_MAC_LEN);
2725 bcopy(local_mac, osp->os_local_mac, ZIO_OBJSET_MAC_LEN);
2726 abd_return_buf_copy(abd, buf, datalen);
2727 return (0);
2728 }
2729
2730 if (bcmp(portable_mac, osp->os_portable_mac, ZIO_OBJSET_MAC_LEN) != 0 ||
2731 bcmp(local_mac, osp->os_local_mac, ZIO_OBJSET_MAC_LEN) != 0) {
2732 abd_return_buf(abd, buf, datalen);
2733 return (SET_ERROR(ECKSUM));
2734 }
2735
2736 abd_return_buf(abd, buf, datalen);
2737
2738 return (0);
2739
2740error:
2741 if (dck != NULL)
2742 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2743 abd_return_buf(abd, buf, datalen);
2744 return (ret);
2745}
2746
2747int
2748spa_do_crypt_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj, abd_t *abd,
2749 uint_t datalen, uint8_t *mac)
2750{
2751 int ret;
2752 dsl_crypto_key_t *dck = NULL;
2753 uint8_t *buf = abd_borrow_buf_copy(abd, datalen);
2754 uint8_t digestbuf[ZIO_DATA_MAC_LEN];
2755
2756 /* look up the key from the spa's keystore */
2757 ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);
2758 if (ret != 0)
2759 goto error;
2760
2761 /* perform the hmac */
4807c0ba
TC
2762 ret = zio_crypt_do_hmac(&dck->dck_key, buf, datalen,
2763 digestbuf, ZIO_DATA_MAC_LEN);
b5256303
TC
2764 if (ret != 0)
2765 goto error;
2766
2767 abd_return_buf(abd, buf, datalen);
2768 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2769
2770 /*
2771 * Truncate and fill in mac buffer if we were asked to generate a MAC.
2772 * Otherwise verify that the MAC matched what we expected.
2773 */
2774 if (generate) {
2775 bcopy(digestbuf, mac, ZIO_DATA_MAC_LEN);
2776 return (0);
2777 }
2778
2779 if (bcmp(digestbuf, mac, ZIO_DATA_MAC_LEN) != 0)
2780 return (SET_ERROR(ECKSUM));
2781
2782 return (0);
2783
2784error:
2785 if (dck != NULL)
2786 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2787 abd_return_buf(abd, buf, datalen);
2788 return (ret);
2789}
2790
2791/*
2792 * This function serves as a multiplexer for encryption and decryption of
2793 * all blocks (except the L2ARC). For encryption, it will populate the IV,
2794 * salt, MAC, and cabd (the ciphertext). On decryption it will simply use
2795 * these fields to populate pabd (the plaintext).
2796 */
2797int
be9a5c35
TC
2798spa_do_crypt_abd(boolean_t encrypt, spa_t *spa, const zbookmark_phys_t *zb,
2799 dmu_object_type_t ot, boolean_t dedup, boolean_t bswap, uint8_t *salt,
2800 uint8_t *iv, uint8_t *mac, uint_t datalen, abd_t *pabd, abd_t *cabd,
2801 boolean_t *no_crypt)
b5256303
TC
2802{
2803 int ret;
b5256303
TC
2804 dsl_crypto_key_t *dck = NULL;
2805 uint8_t *plainbuf = NULL, *cipherbuf = NULL;
2806
2807 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
b5256303
TC
2808
2809 /* look up the key from the spa's keystore */
be9a5c35
TC
2810 ret = spa_keystore_lookup_key(spa, zb->zb_objset, FTAG, &dck);
2811 if (ret != 0) {
2812 ret = SET_ERROR(EACCES);
b5256303 2813 return (ret);
be9a5c35 2814 }
b5256303
TC
2815
2816 if (encrypt) {
2817 plainbuf = abd_borrow_buf_copy(pabd, datalen);
2818 cipherbuf = abd_borrow_buf(cabd, datalen);
2819 } else {
2820 plainbuf = abd_borrow_buf(pabd, datalen);
2821 cipherbuf = abd_borrow_buf_copy(cabd, datalen);
2822 }
2823
2824 /*
2825 * Both encryption and decryption functions need a salt for key
2826 * generation and an IV. When encrypting a non-dedup block, we
2827 * generate the salt and IV randomly to be stored by the caller. Dedup
2828 * blocks perform a (more expensive) HMAC of the plaintext to obtain
2829 * the salt and the IV. ZIL blocks have their salt and IV generated
2830 * at allocation time in zio_alloc_zil(). On decryption, we simply use
2831 * the provided values.
2832 */
be9a5c35 2833 if (encrypt && ot != DMU_OT_INTENT_LOG && !dedup) {
b5256303
TC
2834 ret = zio_crypt_key_get_salt(&dck->dck_key, salt);
2835 if (ret != 0)
2836 goto error;
2837
2838 ret = zio_crypt_generate_iv(iv);
2839 if (ret != 0)
2840 goto error;
be9a5c35 2841 } else if (encrypt && dedup) {
b5256303
TC
2842 ret = zio_crypt_generate_iv_salt_dedup(&dck->dck_key,
2843 plainbuf, datalen, iv, salt);
2844 if (ret != 0)
2845 goto error;
2846 }
2847
2848 /* call lower level function to perform encryption / decryption */
10fa2545
BB
2849 ret = zio_do_crypt_data(encrypt, &dck->dck_key, ot, bswap, salt, iv,
2850 mac, datalen, plainbuf, cipherbuf, no_crypt);
be9a5c35
TC
2851
2852 /*
2853 * Handle injected decryption faults. Unfortunately, we cannot inject
2854 * faults for dnode blocks because we might trigger the panic in
2855 * dbuf_prepare_encrypted_dnode_leaf(), which exists because syncing
2856 * context is not prepared to handle malicious decryption failures.
2857 */
2858 if (zio_injection_enabled && !encrypt && ot != DMU_OT_DNODE && ret == 0)
2859 ret = zio_handle_decrypt_injection(spa, zb, ot, ECKSUM);
b5256303
TC
2860 if (ret != 0)
2861 goto error;
2862
2863 if (encrypt) {
2864 abd_return_buf(pabd, plainbuf, datalen);
2865 abd_return_buf_copy(cabd, cipherbuf, datalen);
2866 } else {
2867 abd_return_buf_copy(pabd, plainbuf, datalen);
2868 abd_return_buf(cabd, cipherbuf, datalen);
2869 }
2870
2871 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2872
2873 return (0);
2874
2875error:
2876 if (encrypt) {
2877 /* zero out any state we might have changed while encrypting */
2878 bzero(salt, ZIO_DATA_SALT_LEN);
2879 bzero(iv, ZIO_DATA_IV_LEN);
2880 bzero(mac, ZIO_DATA_MAC_LEN);
2881 abd_return_buf(pabd, plainbuf, datalen);
2882 abd_return_buf_copy(cabd, cipherbuf, datalen);
2883 } else {
2884 abd_return_buf_copy(pabd, plainbuf, datalen);
2885 abd_return_buf(cabd, cipherbuf, datalen);
2886 }
2887
4807c0ba 2888 spa_keystore_dsl_key_rele(spa, dck, FTAG);
b5256303
TC
2889
2890 return (ret);
2891}
f00ab3f2
TC
2892
2893#if defined(_KERNEL)
2894module_param(zfs_disable_ivset_guid_check, int, 0644);
2895MODULE_PARM_DESC(zfs_disable_ivset_guid_check,
2896 "Set to allow raw receives without IVset guids");
2897#endif