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