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