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