Print this page
fixup .text where possible
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/crypto/io/crypto.c
+++ new/usr/src/uts/common/crypto/io/crypto.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 */
24 24
25 25
26 26 /*
27 27 * The ioctl interface for cryptographic commands.
28 28 */
29 29
30 30 #include <sys/types.h>
31 31 #include <sys/modctl.h>
32 32 #include <sys/conf.h>
33 33 #include <sys/stat.h>
34 34 #include <sys/ddi.h>
35 35 #include <sys/sunddi.h>
36 36 #include <sys/kmem.h>
37 37 #include <sys/errno.h>
38 38 #include <sys/ksynch.h>
39 39 #include <sys/file.h>
40 40 #include <sys/open.h>
41 41 #include <sys/cred.h>
42 42 #include <sys/proc.h>
43 43 #include <sys/task.h>
44 44 #include <sys/mkdev.h>
45 45 #include <sys/model.h>
46 46 #include <sys/sysmacros.h>
47 47 #include <sys/crypto/common.h>
48 48 #include <sys/crypto/api.h>
49 49 #include <sys/crypto/impl.h>
50 50 #include <sys/crypto/sched_impl.h>
51 51 #include <sys/crypto/ioctl.h>
52 52
53 53 extern int kcf_des3_threshold;
54 54 extern int kcf_aes_threshold;
55 55 extern int kcf_rc4_threshold;
56 56 extern int kcf_md5_threshold;
57 57 extern int kcf_sha1_threshold;
58 58
59 59 /*
60 60 * Locking notes:
61 61 *
62 62 * crypto_locks protects the global array of minor structures.
63 63 * crypto_locks is an array of locks indexed by the cpuid. A reader needs
64 64 * to hold a single lock while a writer needs to hold all locks.
65 65 * krwlock_t is not an option here because the hold time
66 66 * is very small for these locks.
67 67 *
68 68 * The fields in the minor structure are protected by the cm_lock member
69 69 * of the minor structure. The cm_cv is used to signal decrements
70 70 * in the cm_refcnt, and is used with the cm_lock.
71 71 *
72 72 * The locking order is crypto_locks followed by cm_lock.
73 73 */
74 74
75 75 /*
76 76 * DDI entry points.
77 77 */
78 78 static int crypto_attach(dev_info_t *, ddi_attach_cmd_t);
79 79 static int crypto_detach(dev_info_t *, ddi_detach_cmd_t);
80 80 static int crypto_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
81 81 static int crypto_open(dev_t *, int, int, cred_t *);
82 82 static int crypto_close(dev_t, int, int, cred_t *);
83 83 static int crypto_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
84 84
85 85 static int cipher_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
86 86 crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
87 87 crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
88 88
89 89 static int common_digest(dev_t, caddr_t, int, int (*)(crypto_context_t,
90 90 crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
91 91
92 92 static int cipher(dev_t, caddr_t, int, int (*)(crypto_context_t,
93 93 crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
94 94
95 95 static int cipher_update(dev_t, caddr_t, int, int (*)(crypto_context_t,
96 96 crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
97 97
98 98 static int common_final(dev_t, caddr_t, int, int (*)(crypto_context_t,
99 99 crypto_data_t *, crypto_call_req_t *));
100 100
101 101 static int sign_verify_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
102 102 crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
103 103 crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
104 104
105 105 static int sign_verify_update(dev_t dev, caddr_t arg, int mode,
106 106 int (*)(crypto_context_t, crypto_data_t *, crypto_call_req_t *));
107 107
108 108 static void crypto_release_provider_session(crypto_minor_t *,
109 109 crypto_provider_session_t *);
110 110 static int crypto_buffer_check(size_t);
111 111 static int crypto_free_find_ctx(crypto_session_data_t *);
112 112 static int crypto_get_provider_list(crypto_minor_t *, uint_t *,
113 113 crypto_provider_entry_t **, boolean_t);
114 114
115 115 /* number of minor numbers to allocate at a time */
116 116 #define CRYPTO_MINOR_CHUNK 16
117 117
118 118 /*
119 119 * There are two limits associated with kernel memory. The first,
120 120 * CRYPTO_MAX_BUFFER_LEN, is the maximum number of bytes that can be
121 121 * allocated for a single copyin/copyout buffer. The second limit is
122 122 * the total number of bytes that can be allocated by a process
123 123 * for copyin/copyout buffers. The latter is enforced by the
124 124 * project.max-crypto-memory resource control.
125 125 */
126 126
127 127 #define CRYPTO_MAX_BUFFER_LEN (2 * 1024 * 1024)
128 128 #define CRYPTO_MAX_FIND_COUNT 512
129 129
130 130 /*
131 131 * We preapprove some bytes for each session to avoid making the costly
132 132 * crypto_buffer_check() calls. The preapproval is done when a new session
133 133 * is created and that cost is amortized over later crypto calls.
134 134 * Most applications create a session and then do a bunch of crypto calls
135 135 * in that session. So, they benefit from this optimization.
136 136 *
137 137 * Note that we may hit the project.max-crypto-memory limit a bit sooner
138 138 * because of this preapproval. But it is acceptable since the preapproved
139 139 * amount is insignificant compared to the default max-crypto-memory limit
140 140 * which is quarter of the machine's memory. The preapproved amount is
141 141 * roughly 2 * 16K(maximum SSL record size).
142 142 */
143 143 #define CRYPTO_PRE_APPROVED_LIMIT (32 * 1024)
144 144
145 145 /* The session table grows by CRYPTO_SESSION_CHUNK increments */
146 146 #define CRYPTO_SESSION_CHUNK 100
147 147
148 148 size_t crypto_max_buffer_len = CRYPTO_MAX_BUFFER_LEN;
149 149 size_t crypto_pre_approved_limit = CRYPTO_PRE_APPROVED_LIMIT;
150 150
151 151 #define INIT_RAW_CRYPTO_DATA(data, len) \
152 152 (data).cd_format = CRYPTO_DATA_RAW; \
153 153 (data).cd_raw.iov_base = kmem_alloc(len, KM_SLEEP); \
154 154 (data).cd_raw.iov_len = len; \
155 155 (data).cd_offset = 0; \
156 156 (data).cd_length = len;
157 157
158 158 static struct kmem_cache *crypto_session_cache;
159 159 static crypto_minor_t **crypto_minors = NULL;
160 160 static dev_info_t *crypto_dip = NULL;
161 161 static minor_t crypto_minor_chunk = CRYPTO_MINOR_CHUNK;
162 162 static minor_t crypto_minors_table_count = 0;
163 163
164 164 /*
165 165 * Minors are started from 1 because vmem_alloc()
166 166 * returns 0 in case of failure.
167 167 */
168 168 static vmem_t *crypto_arena = NULL; /* Arena for device minors */
169 169 static minor_t crypto_minors_count = 0;
170 170 static kcf_lock_withpad_t *crypto_locks;
171 171
172 172 #define CRYPTO_ENTER_ALL_LOCKS() \
173 173 for (i = 0; i < max_ncpus; i++) \
174 174 mutex_enter(&crypto_locks[i].kl_lock);
175 175
176 176 #define CRYPTO_EXIT_ALL_LOCKS() \
177 177 for (i = 0; i < max_ncpus; i++) \
178 178 mutex_exit(&crypto_locks[i].kl_lock);
179 179
180 180 #define RETURN_LIST B_TRUE
181 181 #define DONT_RETURN_LIST B_FALSE
182 182
183 183 #define CRYPTO_OPS_OFFSET(f) offsetof(crypto_ops_t, co_##f)
184 184 #define CRYPTO_RANDOM_OFFSET(f) offsetof(crypto_random_number_ops_t, f)
185 185 #define CRYPTO_SESSION_OFFSET(f) offsetof(crypto_session_ops_t, f)
186 186 #define CRYPTO_OBJECT_OFFSET(f) offsetof(crypto_object_ops_t, f)
187 187 #define CRYPTO_PROVIDER_OFFSET(f) \
188 188 offsetof(crypto_provider_management_ops_t, f)
189 189
190 190 #define CRYPTO_CANCEL_CTX(spp) { \
191 191 crypto_cancel_ctx(*(spp)); \
192 192 *(spp) = NULL; \
193 193 }
194 194
195 195 #define CRYPTO_CANCEL_ALL_CTX(sp) { \
196 196 if ((sp)->sd_digest_ctx != NULL) { \
197 197 crypto_cancel_ctx((sp)->sd_digest_ctx); \
198 198 (sp)->sd_digest_ctx = NULL; \
199 199 } \
200 200 if ((sp)->sd_encr_ctx != NULL) { \
201 201 crypto_cancel_ctx((sp)->sd_encr_ctx); \
202 202 (sp)->sd_encr_ctx = NULL; \
203 203 } \
204 204 if ((sp)->sd_decr_ctx != NULL) { \
205 205 crypto_cancel_ctx((sp)->sd_decr_ctx); \
206 206 (sp)->sd_decr_ctx = NULL; \
207 207 } \
208 208 if ((sp)->sd_sign_ctx != NULL) { \
209 209 crypto_cancel_ctx((sp)->sd_sign_ctx); \
210 210 (sp)->sd_sign_ctx = NULL; \
211 211 } \
212 212 if ((sp)->sd_verify_ctx != NULL) { \
213 213 crypto_cancel_ctx((sp)->sd_verify_ctx); \
214 214 (sp)->sd_verify_ctx = NULL; \
215 215 } \
216 216 if ((sp)->sd_sign_recover_ctx != NULL) { \
217 217 crypto_cancel_ctx((sp)->sd_sign_recover_ctx); \
218 218 (sp)->sd_sign_recover_ctx = NULL; \
219 219 } \
220 220 if ((sp)->sd_verify_recover_ctx != NULL) { \
221 221 crypto_cancel_ctx((sp)->sd_verify_recover_ctx); \
222 222 (sp)->sd_verify_recover_ctx = NULL; \
223 223 } \
224 224 }
225 225
226 226 #define CRYPTO_DECREMENT_RCTL(val) if ((val) != 0) { \
227 227 kproject_t *projp; \
228 228 mutex_enter(&curproc->p_lock); \
229 229 projp = curproc->p_task->tk_proj; \
230 230 ASSERT(projp != NULL); \
231 231 mutex_enter(&(projp->kpj_data.kpd_crypto_lock)); \
232 232 projp->kpj_data.kpd_crypto_mem -= (val); \
233 233 mutex_exit(&(projp->kpj_data.kpd_crypto_lock)); \
234 234 curproc->p_crypto_mem -= (val); \
235 235 mutex_exit(&curproc->p_lock); \
236 236 }
237 237
238 238 /*
239 239 * We do not need to hold sd_lock in the macros below
240 240 * as they are called after doing a get_session_ptr() which
241 241 * sets the CRYPTO_SESSION_IS_BUSY flag.
242 242 */
243 243 #define CRYPTO_DECREMENT_RCTL_SESSION(sp, val, rctl_chk) \
244 244 if (((val) != 0) && ((sp) != NULL)) { \
245 245 ASSERT(((sp)->sd_flags & CRYPTO_SESSION_IS_BUSY) != 0); \
246 246 if (rctl_chk) { \
247 247 CRYPTO_DECREMENT_RCTL(val); \
248 248 } else { \
249 249 (sp)->sd_pre_approved_amount += (val); \
250 250 } \
251 251 }
252 252
253 253 #define CRYPTO_BUFFER_CHECK(sp, need, rctl_chk) \
254 254 ((sp->sd_pre_approved_amount >= need) ? \
255 255 (sp->sd_pre_approved_amount -= need, \
256 256 rctl_chk = B_FALSE, CRYPTO_SUCCESS) : \
257 257 (rctl_chk = B_TRUE, crypto_buffer_check(need)))
258 258
259 259 /*
260 260 * Module linkage.
261 261 */
262 262 static struct cb_ops cbops = {
263 263 crypto_open, /* cb_open */
264 264 crypto_close, /* cb_close */
265 265 nodev, /* cb_strategy */
266 266 nodev, /* cb_print */
267 267 nodev, /* cb_dump */
268 268 nodev, /* cb_read */
269 269 nodev, /* cb_write */
270 270 crypto_ioctl, /* cb_ioctl */
271 271 nodev, /* cb_devmap */
272 272 nodev, /* cb_mmap */
273 273 nodev, /* cb_segmap */
274 274 nochpoll, /* cb_chpoll */
275 275 ddi_prop_op, /* cb_prop_op */
276 276 NULL, /* cb_streamtab */
277 277 D_MP, /* cb_flag */
278 278 CB_REV, /* cb_rev */
279 279 nodev, /* cb_aread */
280 280 nodev, /* cb_awrite */
281 281 };
282 282
283 283 static struct dev_ops devops = {
284 284 DEVO_REV, /* devo_rev */
285 285 0, /* devo_refcnt */
286 286 crypto_getinfo, /* devo_getinfo */
287 287 nulldev, /* devo_identify */
288 288 nulldev, /* devo_probe */
289 289 crypto_attach, /* devo_attach */
290 290 crypto_detach, /* devo_detach */
291 291 nodev, /* devo_reset */
292 292 &cbops, /* devo_cb_ops */
293 293 NULL, /* devo_bus_ops */
294 294 NULL, /* devo_power */
295 295 ddi_quiesce_not_needed, /* devo_quiesce */
↓ open down ↓ |
295 lines elided |
↑ open up ↑ |
296 296 };
297 297
298 298 static struct modldrv modldrv = {
299 299 &mod_driverops, /* drv_modops */
300 300 "Cryptographic Library Interface", /* drv_linkinfo */
301 301 &devops,
302 302 };
303 303
304 304 static struct modlinkage modlinkage = {
305 305 MODREV_1, /* ml_rev */
306 - &modldrv, /* ml_linkage */
307 - NULL
306 + { &modldrv, /* ml_linkage */
307 + NULL }
308 308 };
309 309
310 310 /*
311 311 * DDI entry points.
312 312 */
313 313 int
314 314 _init(void)
315 315 {
316 316 return (mod_install(&modlinkage));
317 317 }
318 318
319 319 int
320 320 _fini(void)
321 321 {
322 322 return (mod_remove(&modlinkage));
323 323 }
324 324
325 325 int
326 326 _info(struct modinfo *modinfop)
327 327 {
328 328 return (mod_info(&modlinkage, modinfop));
329 329 }
330 330
331 331 /* ARGSUSED */
332 332 static int
333 333 crypto_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
334 334 {
335 335 switch (cmd) {
336 336 case DDI_INFO_DEVT2DEVINFO:
337 337 *result = crypto_dip;
338 338 return (DDI_SUCCESS);
339 339
340 340 case DDI_INFO_DEVT2INSTANCE:
341 341 *result = (void *)0;
342 342 return (DDI_SUCCESS);
343 343 }
344 344 return (DDI_FAILURE);
345 345 }
346 346
347 347 static int
348 348 crypto_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
349 349 {
350 350 int i;
351 351
352 352 if (cmd != DDI_ATTACH) {
353 353 return (DDI_FAILURE);
354 354 }
355 355
356 356 if (ddi_get_instance(dip) != 0) {
357 357 /* we only allow instance 0 to attach */
358 358 return (DDI_FAILURE);
359 359 }
360 360
361 361 crypto_session_cache = kmem_cache_create("crypto_session_cache",
362 362 sizeof (crypto_session_data_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
363 363
364 364 if (crypto_session_cache == NULL)
365 365 return (DDI_FAILURE);
366 366
367 367 /* create the minor node */
368 368 if (ddi_create_minor_node(dip, "crypto", S_IFCHR, 0,
369 369 DDI_PSEUDO, 0) != DDI_SUCCESS) {
370 370 kmem_cache_destroy(crypto_session_cache);
371 371 crypto_session_cache = NULL;
372 372 cmn_err(CE_WARN, "crypto_attach: failed creating minor node");
373 373 ddi_remove_minor_node(dip, NULL);
374 374 return (DDI_FAILURE);
375 375 }
376 376
377 377 crypto_locks = kmem_zalloc(max_ncpus * sizeof (kcf_lock_withpad_t),
378 378 KM_SLEEP);
379 379 for (i = 0; i < max_ncpus; i++)
380 380 mutex_init(&crypto_locks[i].kl_lock, NULL, MUTEX_DRIVER, NULL);
381 381
382 382 crypto_dip = dip;
383 383
384 384 /* allocate integer space for minor numbers */
385 385 crypto_arena = vmem_create("crypto", (void *)1,
386 386 CRYPTO_MINOR_CHUNK, 1, NULL, NULL, NULL, 0,
387 387 VM_SLEEP | VMC_IDENTIFIER);
388 388
389 389 return (DDI_SUCCESS);
390 390 }
391 391
392 392 static int
393 393 crypto_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
394 394 {
395 395 minor_t i;
396 396 kcf_lock_withpad_t *mp;
397 397
398 398 if (cmd != DDI_DETACH)
399 399 return (DDI_FAILURE);
400 400
401 401 mp = &crypto_locks[CPU_SEQID];
402 402 mutex_enter(&mp->kl_lock);
403 403
404 404 /* check if device is open */
405 405 for (i = 0; i < crypto_minors_table_count; i++) {
406 406 if (crypto_minors[i] != NULL) {
407 407 mutex_exit(&mp->kl_lock);
408 408 return (DDI_FAILURE);
409 409 }
410 410 }
411 411 mutex_exit(&mp->kl_lock);
412 412
413 413 crypto_dip = NULL;
414 414 ddi_remove_minor_node(dip, NULL);
415 415
416 416 kmem_cache_destroy(crypto_session_cache);
417 417 crypto_session_cache = NULL;
418 418
419 419 kmem_free(crypto_minors,
420 420 sizeof (crypto_minor_t *) * crypto_minors_table_count);
421 421 crypto_minors = NULL;
422 422 crypto_minors_table_count = 0;
423 423 for (i = 0; i < max_ncpus; i++)
424 424 mutex_destroy(&crypto_locks[i].kl_lock);
425 425 kmem_free(crypto_locks, max_ncpus * sizeof (kcf_lock_withpad_t));
426 426 crypto_locks = NULL;
427 427
428 428 vmem_destroy(crypto_arena);
429 429 crypto_arena = NULL;
430 430
431 431 return (DDI_SUCCESS);
432 432 }
433 433
434 434 /* ARGSUSED3 */
435 435 static int
436 436 crypto_open(dev_t *devp, int flag, int otyp, cred_t *credp)
437 437 {
438 438 crypto_minor_t *cm = NULL;
439 439 minor_t mn;
440 440 kcf_lock_withpad_t *mp;
441 441 int i;
442 442
443 443 if (otyp != OTYP_CHR)
444 444 return (ENXIO);
445 445
446 446 if (crypto_dip == NULL)
447 447 return (ENXIO);
448 448
449 449 /* exclusive opens are not supported */
450 450 if (flag & FEXCL)
451 451 return (ENOTSUP);
452 452
453 453 again:
454 454 mp = &crypto_locks[CPU_SEQID];
455 455 mutex_enter(&mp->kl_lock);
456 456
457 457 /* grow the minors table if needed */
458 458 if (crypto_minors_count >= crypto_minors_table_count) {
459 459 crypto_minor_t **newtable;
460 460 minor_t chunk = crypto_minor_chunk;
461 461 minor_t saved_count;
462 462 size_t new_size;
463 463 ulong_t big_count;
464 464
465 465 big_count = crypto_minors_count + chunk;
466 466 if (big_count > MAXMIN) {
467 467 mutex_exit(&mp->kl_lock);
468 468 return (ENOMEM);
469 469 }
470 470
471 471 saved_count = crypto_minors_table_count;
472 472 new_size = sizeof (crypto_minor_t *) *
473 473 (crypto_minors_table_count + chunk);
474 474
475 475 mutex_exit(&mp->kl_lock);
476 476
477 477 newtable = kmem_zalloc(new_size, KM_SLEEP);
478 478 CRYPTO_ENTER_ALL_LOCKS();
479 479 /*
480 480 * Check if table grew while we were sleeping.
481 481 * The minors table never shrinks.
482 482 */
483 483 if (crypto_minors_table_count > saved_count) {
484 484 CRYPTO_EXIT_ALL_LOCKS();
485 485 kmem_free(newtable, new_size);
486 486 goto again;
487 487 }
488 488
489 489 /* we assume that bcopy() will return if count is 0 */
490 490 bcopy(crypto_minors, newtable,
491 491 sizeof (crypto_minor_t *) * crypto_minors_table_count);
492 492
493 493 kmem_free(crypto_minors,
494 494 sizeof (crypto_minor_t *) * crypto_minors_table_count);
495 495
496 496 /* grow the minors number space */
497 497 if (crypto_minors_table_count != 0) {
498 498 (void) vmem_add(crypto_arena,
499 499 (void *)(uintptr_t)(crypto_minors_table_count + 1),
500 500 crypto_minor_chunk, VM_SLEEP);
501 501 }
502 502
503 503 crypto_minors = newtable;
504 504 crypto_minors_table_count += chunk;
505 505 CRYPTO_EXIT_ALL_LOCKS();
506 506 } else {
507 507 mutex_exit(&mp->kl_lock);
508 508 }
509 509
510 510 /* allocate a new minor number starting with 1 */
511 511 mn = (minor_t)(uintptr_t)vmem_alloc(crypto_arena, 1, VM_SLEEP);
512 512
513 513 cm = kmem_zalloc(sizeof (crypto_minor_t), KM_SLEEP);
514 514 mutex_init(&cm->cm_lock, NULL, MUTEX_DRIVER, NULL);
515 515 cv_init(&cm->cm_cv, NULL, CV_DRIVER, NULL);
516 516
517 517 CRYPTO_ENTER_ALL_LOCKS();
518 518 cm->cm_refcnt = 1;
519 519 crypto_minors[mn - 1] = cm;
520 520 crypto_minors_count++;
521 521 CRYPTO_EXIT_ALL_LOCKS();
522 522
523 523 *devp = makedevice(getmajor(*devp), mn);
524 524
525 525 return (0);
526 526 }
527 527
528 528 /* ARGSUSED1 */
529 529 static int
530 530 crypto_close(dev_t dev, int flag, int otyp, cred_t *credp)
531 531 {
532 532 crypto_minor_t *cm = NULL;
533 533 crypto_session_data_t *sp;
534 534 minor_t mn = getminor(dev);
535 535 uint_t i;
536 536 size_t total = 0;
537 537 kcf_lock_withpad_t *mp;
538 538
539 539 mp = &crypto_locks[CPU_SEQID];
540 540 mutex_enter(&mp->kl_lock);
541 541
542 542 if (mn > crypto_minors_table_count) {
543 543 mutex_exit(&mp->kl_lock);
544 544 cmn_err(CE_WARN, "crypto_close: bad minor (too big) %d", mn);
545 545 return (ENODEV);
546 546 }
547 547
548 548 cm = crypto_minors[mn - 1];
549 549 if (cm == NULL) {
550 550 mutex_exit(&mp->kl_lock);
551 551 cmn_err(CE_WARN, "crypto_close: duplicate close of minor %d",
552 552 getminor(dev));
553 553 return (ENODEV);
554 554 }
555 555
556 556 mutex_exit(&mp->kl_lock);
557 557
558 558 CRYPTO_ENTER_ALL_LOCKS();
559 559 /*
560 560 * We free the minor number, mn, from the crypto_arena
561 561 * only later. This ensures that we won't race with another
562 562 * thread in crypto_open with the same minor number.
563 563 */
564 564 crypto_minors[mn - 1] = NULL;
565 565 crypto_minors_count--;
566 566 CRYPTO_EXIT_ALL_LOCKS();
567 567
568 568 mutex_enter(&cm->cm_lock);
569 569 cm->cm_refcnt --; /* decrement refcnt held in open */
570 570 while (cm->cm_refcnt > 0) {
571 571 cv_wait(&cm->cm_cv, &cm->cm_lock);
572 572 }
573 573
574 574 vmem_free(crypto_arena, (void *)(uintptr_t)mn, 1);
575 575
576 576 /* free all session table entries starting with 1 */
577 577 for (i = 1; i < cm->cm_session_table_count; i++) {
578 578 if (cm->cm_session_table[i] == NULL)
579 579 continue;
580 580
581 581 sp = cm->cm_session_table[i];
582 582 ASSERT((sp->sd_flags & CRYPTO_SESSION_IS_BUSY) == 0);
583 583 ASSERT(sp->sd_pre_approved_amount == 0 ||
584 584 sp->sd_pre_approved_amount == crypto_pre_approved_limit);
585 585 total += sp->sd_pre_approved_amount;
586 586 if (sp->sd_find_init_cookie != NULL) {
587 587 (void) crypto_free_find_ctx(sp);
588 588 }
589 589 crypto_release_provider_session(cm, sp->sd_provider_session);
590 590 KCF_PROV_REFRELE(sp->sd_provider);
591 591 CRYPTO_CANCEL_ALL_CTX(sp);
592 592 mutex_destroy(&sp->sd_lock);
593 593 cv_destroy(&sp->sd_cv);
594 594 kmem_cache_free(crypto_session_cache, sp);
595 595 cm->cm_session_table[i] = NULL;
596 596 }
597 597
598 598 /* free the session table */
599 599 if (cm->cm_session_table != NULL && cm->cm_session_table_count > 0)
600 600 kmem_free(cm->cm_session_table, cm->cm_session_table_count *
601 601 sizeof (void *));
602 602
603 603 total += (cm->cm_session_table_count * sizeof (void *));
604 604 CRYPTO_DECREMENT_RCTL(total);
605 605
606 606 kcf_free_provider_tab(cm->cm_provider_count,
607 607 cm->cm_provider_array);
608 608
609 609 mutex_exit(&cm->cm_lock);
610 610 mutex_destroy(&cm->cm_lock);
611 611 cv_destroy(&cm->cm_cv);
612 612 kmem_free(cm, sizeof (crypto_minor_t));
613 613
614 614 return (0);
615 615 }
616 616
617 617 static crypto_minor_t *
618 618 crypto_hold_minor(minor_t minor)
619 619 {
620 620 crypto_minor_t *cm;
621 621 kcf_lock_withpad_t *mp;
622 622
623 623 if (minor > crypto_minors_table_count)
624 624 return (NULL);
625 625
626 626 mp = &crypto_locks[CPU_SEQID];
627 627 mutex_enter(&mp->kl_lock);
628 628
629 629 if ((cm = crypto_minors[minor - 1]) != NULL) {
630 630 atomic_inc_32(&cm->cm_refcnt);
631 631 }
632 632 mutex_exit(&mp->kl_lock);
633 633 return (cm);
634 634 }
635 635
636 636 static void
637 637 crypto_release_minor(crypto_minor_t *cm)
638 638 {
639 639 if (atomic_dec_32_nv(&cm->cm_refcnt) == 0) {
640 640 cv_signal(&cm->cm_cv);
641 641 }
642 642 }
643 643
644 644 /*
645 645 * Build a list of functions and other information for the provider, pd.
646 646 */
647 647 static void
648 648 crypto_build_function_list(crypto_function_list_t *fl, kcf_provider_desc_t *pd)
649 649 {
650 650 crypto_ops_t *ops;
651 651 crypto_digest_ops_t *digest_ops;
652 652 crypto_cipher_ops_t *cipher_ops;
653 653 crypto_mac_ops_t *mac_ops;
654 654 crypto_sign_ops_t *sign_ops;
655 655 crypto_verify_ops_t *verify_ops;
656 656 crypto_dual_ops_t *dual_ops;
657 657 crypto_random_number_ops_t *random_number_ops;
658 658 crypto_session_ops_t *session_ops;
659 659 crypto_object_ops_t *object_ops;
660 660 crypto_key_ops_t *key_ops;
661 661 crypto_provider_management_ops_t *provider_ops;
662 662
663 663 if ((ops = pd->pd_ops_vector) == NULL)
664 664 return;
665 665
666 666 if ((digest_ops = ops->co_digest_ops) != NULL) {
667 667 if (digest_ops->digest_init != NULL)
668 668 fl->fl_digest_init = B_TRUE;
669 669 if (digest_ops->digest != NULL)
670 670 fl->fl_digest = B_TRUE;
671 671 if (digest_ops->digest_update != NULL)
672 672 fl->fl_digest_update = B_TRUE;
673 673 if (digest_ops->digest_key != NULL)
674 674 fl->fl_digest_key = B_TRUE;
675 675 if (digest_ops->digest_final != NULL)
676 676 fl->fl_digest_final = B_TRUE;
677 677 }
678 678 if ((cipher_ops = ops->co_cipher_ops) != NULL) {
679 679 if (cipher_ops->encrypt_init != NULL)
680 680 fl->fl_encrypt_init = B_TRUE;
681 681 if (cipher_ops->encrypt != NULL)
682 682 fl->fl_encrypt = B_TRUE;
683 683 if (cipher_ops->encrypt_update != NULL)
684 684 fl->fl_encrypt_update = B_TRUE;
685 685 if (cipher_ops->encrypt_final != NULL)
686 686 fl->fl_encrypt_final = B_TRUE;
687 687 if (cipher_ops->decrypt_init != NULL)
688 688 fl->fl_decrypt_init = B_TRUE;
689 689 if (cipher_ops->decrypt != NULL)
690 690 fl->fl_decrypt = B_TRUE;
691 691 if (cipher_ops->decrypt_update != NULL)
692 692 fl->fl_decrypt_update = B_TRUE;
693 693 if (cipher_ops->decrypt_final != NULL)
694 694 fl->fl_decrypt_final = B_TRUE;
695 695 }
696 696 if ((mac_ops = ops->co_mac_ops) != NULL) {
697 697 if (mac_ops->mac_init != NULL)
698 698 fl->fl_mac_init = B_TRUE;
699 699 if (mac_ops->mac != NULL)
700 700 fl->fl_mac = B_TRUE;
701 701 if (mac_ops->mac_update != NULL)
702 702 fl->fl_mac_update = B_TRUE;
703 703 if (mac_ops->mac_final != NULL)
704 704 fl->fl_mac_final = B_TRUE;
705 705 }
706 706 if ((sign_ops = ops->co_sign_ops) != NULL) {
707 707 if (sign_ops->sign_init != NULL)
708 708 fl->fl_sign_init = B_TRUE;
709 709 if (sign_ops->sign != NULL)
710 710 fl->fl_sign = B_TRUE;
711 711 if (sign_ops->sign_update != NULL)
712 712 fl->fl_sign_update = B_TRUE;
713 713 if (sign_ops->sign_final != NULL)
714 714 fl->fl_sign_final = B_TRUE;
715 715 if (sign_ops->sign_recover_init != NULL)
716 716 fl->fl_sign_recover_init = B_TRUE;
717 717 if (sign_ops->sign_recover != NULL)
718 718 fl->fl_sign_recover = B_TRUE;
719 719 }
720 720 if ((verify_ops = ops->co_verify_ops) != NULL) {
721 721 if (verify_ops->verify_init != NULL)
722 722 fl->fl_verify_init = B_TRUE;
723 723 if (verify_ops->verify != NULL)
724 724 fl->fl_verify = B_TRUE;
725 725 if (verify_ops->verify_update != NULL)
726 726 fl->fl_verify_update = B_TRUE;
727 727 if (verify_ops->verify_final != NULL)
728 728 fl->fl_verify_final = B_TRUE;
729 729 if (verify_ops->verify_recover_init != NULL)
730 730 fl->fl_verify_recover_init = B_TRUE;
731 731 if (verify_ops->verify_recover != NULL)
732 732 fl->fl_verify_recover = B_TRUE;
733 733 }
734 734 if ((dual_ops = ops->co_dual_ops) != NULL) {
735 735 if (dual_ops->digest_encrypt_update != NULL)
736 736 fl->fl_digest_encrypt_update = B_TRUE;
737 737 if (dual_ops->decrypt_digest_update != NULL)
738 738 fl->fl_decrypt_digest_update = B_TRUE;
739 739 if (dual_ops->sign_encrypt_update != NULL)
740 740 fl->fl_sign_encrypt_update = B_TRUE;
741 741 if (dual_ops->decrypt_verify_update != NULL)
742 742 fl->fl_decrypt_verify_update = B_TRUE;
743 743 }
744 744 if ((random_number_ops = ops->co_random_ops) != NULL) {
745 745 if (random_number_ops->seed_random != NULL)
746 746 fl->fl_seed_random = B_TRUE;
747 747 if (random_number_ops->generate_random != NULL)
748 748 fl->fl_generate_random = B_TRUE;
749 749 }
750 750 if ((session_ops = ops->co_session_ops) != NULL) {
751 751 if (session_ops->session_open != NULL)
752 752 fl->fl_session_open = B_TRUE;
753 753 if (session_ops->session_close != NULL)
754 754 fl->fl_session_close = B_TRUE;
755 755 if (session_ops->session_login != NULL)
756 756 fl->fl_session_login = B_TRUE;
757 757 if (session_ops->session_logout != NULL)
758 758 fl->fl_session_logout = B_TRUE;
759 759 }
760 760 if ((object_ops = ops->co_object_ops) != NULL) {
761 761 if (object_ops->object_create != NULL)
762 762 fl->fl_object_create = B_TRUE;
763 763 if (object_ops->object_copy != NULL)
764 764 fl->fl_object_copy = B_TRUE;
765 765 if (object_ops->object_destroy != NULL)
766 766 fl->fl_object_destroy = B_TRUE;
767 767 if (object_ops->object_get_size != NULL)
768 768 fl->fl_object_get_size = B_TRUE;
769 769 if (object_ops->object_get_attribute_value != NULL)
770 770 fl->fl_object_get_attribute_value = B_TRUE;
771 771 if (object_ops->object_set_attribute_value != NULL)
772 772 fl->fl_object_set_attribute_value = B_TRUE;
773 773 if (object_ops->object_find_init != NULL)
774 774 fl->fl_object_find_init = B_TRUE;
775 775 if (object_ops->object_find != NULL)
776 776 fl->fl_object_find = B_TRUE;
777 777 if (object_ops->object_find_final != NULL)
778 778 fl->fl_object_find_final = B_TRUE;
779 779 }
780 780 if ((key_ops = ops->co_key_ops) != NULL) {
781 781 if (key_ops->key_generate != NULL)
782 782 fl->fl_key_generate = B_TRUE;
783 783 if (key_ops->key_generate_pair != NULL)
784 784 fl->fl_key_generate_pair = B_TRUE;
785 785 if (key_ops->key_wrap != NULL)
786 786 fl->fl_key_wrap = B_TRUE;
787 787 if (key_ops->key_unwrap != NULL)
788 788 fl->fl_key_unwrap = B_TRUE;
789 789 if (key_ops->key_derive != NULL)
790 790 fl->fl_key_derive = B_TRUE;
791 791 }
792 792 if ((provider_ops = ops->co_provider_ops) != NULL) {
793 793 if (provider_ops->init_token != NULL)
794 794 fl->fl_init_token = B_TRUE;
795 795 if (provider_ops->init_pin != NULL)
796 796 fl->fl_init_pin = B_TRUE;
797 797 if (provider_ops->set_pin != NULL)
798 798 fl->fl_set_pin = B_TRUE;
799 799 }
800 800
801 801 fl->prov_is_hash_limited = pd->pd_flags & CRYPTO_HASH_NO_UPDATE;
802 802 if (fl->prov_is_hash_limited) {
803 803 fl->prov_hash_limit = min(pd->pd_hash_limit,
804 804 min(CRYPTO_MAX_BUFFER_LEN,
805 805 curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl));
806 806 }
807 807
808 808 fl->prov_is_hmac_limited = pd->pd_flags & CRYPTO_HMAC_NO_UPDATE;
809 809 if (fl->prov_is_hmac_limited) {
810 810 fl->prov_hmac_limit = min(pd->pd_hmac_limit,
811 811 min(CRYPTO_MAX_BUFFER_LEN,
812 812 curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl));
813 813 }
814 814
815 815 if (fl->prov_is_hash_limited || fl->prov_is_hmac_limited) {
816 816 /*
817 817 * XXX - The threshold should ideally be per hash/HMAC
818 818 * mechanism. For now, we use the same value for all
819 819 * hash/HMAC mechanisms. Empirical evidence suggests this
820 820 * is fine.
821 821 */
822 822 fl->prov_hash_threshold = kcf_md5_threshold;
823 823 }
824 824
825 825 fl->total_threshold_count = MAX_NUM_THRESHOLD;
826 826 fl->fl_threshold[0].mech_type = CKM_DES3_CBC;
827 827 fl->fl_threshold[0].mech_threshold = kcf_des3_threshold;
828 828 fl->fl_threshold[1].mech_type = CKM_DES3_ECB;
829 829 fl->fl_threshold[1].mech_threshold = kcf_des3_threshold;
830 830 fl->fl_threshold[2].mech_type = CKM_AES_CBC;
831 831 fl->fl_threshold[2].mech_threshold = kcf_aes_threshold;
832 832 fl->fl_threshold[3].mech_type = CKM_AES_ECB;
833 833 fl->fl_threshold[3].mech_threshold = kcf_aes_threshold;
834 834 fl->fl_threshold[4].mech_type = CKM_RC4;
835 835 fl->fl_threshold[4].mech_threshold = kcf_rc4_threshold;
836 836 fl->fl_threshold[5].mech_type = CKM_MD5;
837 837 fl->fl_threshold[5].mech_threshold = kcf_md5_threshold;
838 838 fl->fl_threshold[6].mech_type = CKM_SHA_1;
839 839 fl->fl_threshold[6].mech_threshold = kcf_sha1_threshold;
840 840 }
841 841
842 842 /* ARGSUSED */
843 843 static int
844 844 get_function_list(dev_t dev, caddr_t arg, int mode, int *rval)
845 845 {
846 846 crypto_get_function_list_t get_function_list;
847 847 crypto_minor_t *cm;
848 848 crypto_provider_id_t provider_id;
849 849 crypto_function_list_t *fl;
850 850 kcf_provider_desc_t *provider;
851 851 int rv;
852 852
853 853 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
854 854 cmn_err(CE_WARN, "get_function_list: failed holding minor");
855 855 return (ENXIO);
856 856 }
857 857
858 858 if (copyin(arg, &get_function_list, sizeof (get_function_list)) != 0) {
859 859 crypto_release_minor(cm);
860 860 return (EFAULT);
861 861 }
862 862
863 863 /* initialize provider_array */
864 864 if (cm->cm_provider_array == NULL) {
865 865 rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
866 866 if (rv != CRYPTO_SUCCESS) {
867 867 goto release_minor;
868 868 }
869 869 }
870 870
871 871 provider_id = get_function_list.fl_provider_id;
872 872 mutex_enter(&cm->cm_lock);
873 873 /* index must be less than count of providers */
874 874 if (provider_id >= cm->cm_provider_count) {
875 875 mutex_exit(&cm->cm_lock);
876 876 rv = CRYPTO_ARGUMENTS_BAD;
877 877 goto release_minor;
878 878 }
879 879
880 880 ASSERT(cm->cm_provider_array != NULL);
881 881 provider = cm->cm_provider_array[provider_id];
882 882 mutex_exit(&cm->cm_lock);
883 883
884 884 fl = &get_function_list.fl_list;
885 885 bzero(fl, sizeof (crypto_function_list_t));
886 886
887 887 if (provider->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
888 888 crypto_build_function_list(fl, provider);
889 889 } else {
890 890 kcf_provider_desc_t *prev = NULL, *pd;
891 891
892 892 mutex_enter(&provider->pd_lock);
893 893 while (kcf_get_next_logical_provider_member(provider,
894 894 prev, &pd)) {
895 895 prev = pd;
896 896 crypto_build_function_list(fl, pd);
897 897 KCF_PROV_REFRELE(pd);
898 898 }
899 899 mutex_exit(&provider->pd_lock);
900 900 }
901 901
902 902 rv = CRYPTO_SUCCESS;
903 903
904 904 release_minor:
905 905 crypto_release_minor(cm);
906 906
907 907 get_function_list.fl_return_value = rv;
908 908
909 909 if (copyout(&get_function_list, arg, sizeof (get_function_list)) != 0) {
910 910 return (EFAULT);
911 911 }
912 912 return (0);
913 913 }
914 914
915 915 /*
916 916 * This ioctl maps a PKCS#11 mechanism string into an internal number
917 917 * that is used by the kernel. pn_internal_number is set to the
918 918 * internal number.
919 919 */
920 920 /* ARGSUSED */
921 921 static int
922 922 get_mechanism_number(dev_t dev, caddr_t arg, int mode, int *rval)
923 923 {
924 924 STRUCT_DECL(crypto_get_mechanism_number, get_number);
925 925 crypto_mech_type_t number;
926 926 size_t len;
927 927 char *mechanism_name;
928 928 int rv;
929 929
930 930 STRUCT_INIT(get_number, mode);
931 931
932 932 if (copyin(arg, STRUCT_BUF(get_number), STRUCT_SIZE(get_number)) != 0)
933 933 return (EFAULT);
934 934
935 935 len = STRUCT_FGET(get_number, pn_mechanism_len);
936 936 if (len == 0 || len > CRYPTO_MAX_MECH_NAME) {
937 937 rv = CRYPTO_ARGUMENTS_BAD;
938 938 goto out;
939 939 }
940 940 mechanism_name = kmem_alloc(len, KM_SLEEP);
941 941
942 942 if (copyin(STRUCT_FGETP(get_number, pn_mechanism_string),
943 943 mechanism_name, len) != 0) {
944 944 kmem_free(mechanism_name, len);
945 945 return (EFAULT);
946 946 }
947 947
948 948 /*
949 949 * Get mechanism number from kcf. We set the load_module
950 950 * flag to false since we use only hardware providers.
951 951 */
952 952 number = crypto_mech2id_common(mechanism_name, B_FALSE);
953 953 kmem_free(mechanism_name, len);
954 954 if (number == CRYPTO_MECH_INVALID) {
955 955 rv = CRYPTO_ARGUMENTS_BAD;
956 956 goto out;
957 957 }
958 958
959 959 bcopy((char *)&number, (char *)STRUCT_FADDR(get_number,
960 960 pn_internal_number), sizeof (number));
961 961
962 962 rv = CRYPTO_SUCCESS;
963 963 out:
964 964 STRUCT_FSET(get_number, pn_return_value, rv);
965 965
966 966 if (copyout(STRUCT_BUF(get_number), arg,
967 967 STRUCT_SIZE(get_number)) != 0) {
968 968 return (EFAULT);
969 969 }
970 970 return (0);
971 971 }
972 972
973 973 /*
974 974 * This ioctl returns an array of crypto_mech_name_t entries.
975 975 * It lists all the PKCS#11 mechanisms available in the kernel.
976 976 */
977 977 /* ARGSUSED */
978 978 static int
979 979 get_mechanism_list(dev_t dev, caddr_t arg, int mode, int *rval)
980 980 {
981 981 STRUCT_DECL(crypto_get_mechanism_list, get_list);
982 982 crypto_mech_name_t *entries;
983 983 size_t copyout_size;
984 984 uint_t req_count;
985 985 uint_t count;
986 986 ulong_t offset;
987 987 int error = 0;
988 988
989 989 STRUCT_INIT(get_list, mode);
990 990
991 991 if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
992 992 return (EFAULT);
993 993 }
994 994
995 995 entries = crypto_get_mech_list(&count, KM_SLEEP);
996 996
997 997 /* Number of entries caller thinks we have */
998 998 req_count = STRUCT_FGET(get_list, ml_count);
999 999
1000 1000 STRUCT_FSET(get_list, ml_count, count);
1001 1001 STRUCT_FSET(get_list, ml_return_value, CRYPTO_SUCCESS);
1002 1002
1003 1003 /* check if buffer is too small */
1004 1004 if (count > req_count) {
1005 1005 STRUCT_FSET(get_list, ml_return_value, CRYPTO_BUFFER_TOO_SMALL);
1006 1006 }
1007 1007
1008 1008 /* copyout the first stuff */
1009 1009 if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
1010 1010 error = EFAULT;
1011 1011 }
1012 1012
1013 1013 /*
1014 1014 * If only requesting number of entries or buffer too small or an
1015 1015 * error occurred, stop here
1016 1016 */
1017 1017 if (req_count == 0 || count > req_count || error != 0) {
1018 1018 goto out;
1019 1019 }
1020 1020
1021 1021 copyout_size = count * sizeof (crypto_mech_name_t);
1022 1022
1023 1023 /* copyout entries */
1024 1024 offset = (ulong_t)STRUCT_FADDR(get_list, ml_list);
1025 1025 offset -= (ulong_t)STRUCT_BUF(get_list);
1026 1026 if (copyout(entries, arg + offset, copyout_size) != 0) {
1027 1027 error = EFAULT;
1028 1028 }
1029 1029
1030 1030 out:
1031 1031 crypto_free_mech_list(entries, count);
1032 1032 return (error);
1033 1033 }
1034 1034
1035 1035 /*
1036 1036 * Copyout kernel array of mech_infos to user space.
1037 1037 */
1038 1038 /* ARGSUSED */
1039 1039 static int
1040 1040 copyout_mechinfos(int mode, caddr_t out, uint_t count,
1041 1041 crypto_mechanism_info_t *k_minfos, caddr_t u_minfos)
1042 1042 {
1043 1043 STRUCT_DECL(crypto_mechanism_info, mi);
1044 1044 caddr_t p;
1045 1045 size_t len;
1046 1046 int i;
1047 1047
1048 1048 if (count == 0)
1049 1049 return (0);
1050 1050
1051 1051 STRUCT_INIT(mi, mode);
1052 1052
1053 1053 len = count * STRUCT_SIZE(mi);
1054 1054
1055 1055 ASSERT(u_minfos != NULL);
1056 1056 p = u_minfos;
1057 1057 for (i = 0; i < count; i++) {
1058 1058 STRUCT_FSET(mi, mi_min_key_size, k_minfos[i].mi_min_key_size);
1059 1059 STRUCT_FSET(mi, mi_max_key_size, k_minfos[i].mi_max_key_size);
1060 1060 STRUCT_FSET(mi, mi_keysize_unit, k_minfos[i].mi_keysize_unit);
1061 1061 STRUCT_FSET(mi, mi_usage, k_minfos[i].mi_usage);
1062 1062 bcopy(STRUCT_BUF(mi), p, STRUCT_SIZE(mi));
1063 1063 p += STRUCT_SIZE(mi);
1064 1064 }
1065 1065
1066 1066 if (copyout(u_minfos, out, len) != 0)
1067 1067 return (EFAULT);
1068 1068
1069 1069 return (0);
1070 1070 }
1071 1071
1072 1072 /*
1073 1073 * This ioctl returns information for the specified mechanism.
1074 1074 */
1075 1075 /* ARGSUSED */
1076 1076 static int
1077 1077 get_all_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
1078 1078 {
1079 1079 STRUCT_DECL(crypto_get_all_mechanism_info, get_all_mech);
1080 1080 #ifdef _LP64
1081 1081 STRUCT_DECL(crypto_mechanism_info, mi);
1082 1082 #else
1083 1083 /* LINTED E_FUNC_SET_NOT_USED */
1084 1084 STRUCT_DECL(crypto_mechanism_info, mi);
1085 1085 #endif
1086 1086 crypto_mech_name_t mech_name;
1087 1087 crypto_mech_type_t mech_type;
1088 1088 crypto_mechanism_info_t *mech_infos = NULL;
1089 1089 uint_t num_mech_infos = 0;
1090 1090 uint_t req_count;
1091 1091 caddr_t u_minfos;
1092 1092 ulong_t offset;
1093 1093 int error = 0;
1094 1094 int rv;
1095 1095
1096 1096 STRUCT_INIT(get_all_mech, mode);
1097 1097 STRUCT_INIT(mi, mode);
1098 1098
1099 1099 if (copyin(arg, STRUCT_BUF(get_all_mech),
1100 1100 STRUCT_SIZE(get_all_mech)) != 0) {
1101 1101 return (EFAULT);
1102 1102 }
1103 1103
1104 1104 (void) strncpy(mech_name, STRUCT_FGET(get_all_mech, mi_mechanism_name),
1105 1105 CRYPTO_MAX_MECH_NAME);
1106 1106 mech_type = crypto_mech2id(mech_name);
1107 1107
1108 1108 if (mech_type == CRYPTO_MECH_INVALID) {
1109 1109 rv = CRYPTO_ARGUMENTS_BAD;
1110 1110 goto out1;
1111 1111 }
1112 1112
1113 1113 rv = crypto_get_all_mech_info(mech_type, &mech_infos, &num_mech_infos,
1114 1114 KM_SLEEP);
1115 1115 if (rv != CRYPTO_SUCCESS) {
1116 1116 goto out1;
1117 1117 }
1118 1118 /* rv is CRYPTO_SUCCESS at this point */
1119 1119
1120 1120 /* Number of entries caller thinks we have */
1121 1121 req_count = STRUCT_FGET(get_all_mech, mi_count);
1122 1122
1123 1123 STRUCT_FSET(get_all_mech, mi_count, num_mech_infos);
1124 1124
1125 1125 /* check if buffer is too small */
1126 1126 if (num_mech_infos > req_count) {
1127 1127 rv = CRYPTO_BUFFER_TOO_SMALL;
1128 1128 }
1129 1129
1130 1130 out1:
1131 1131 STRUCT_FSET(get_all_mech, mi_return_value, rv);
1132 1132
1133 1133 /* copy the first part */
1134 1134 if (copyout(STRUCT_BUF(get_all_mech), arg,
1135 1135 STRUCT_SIZE(get_all_mech)) != 0) {
1136 1136 error = EFAULT;
1137 1137 }
1138 1138
1139 1139 /*
1140 1140 * If only requesting number of entries, or there are no entries,
1141 1141 * or rv is not CRYPTO_SUCCESS due to buffer too small or some other
1142 1142 * crypto error, or an error occurred with copyout, stop here
1143 1143 */
1144 1144 if (req_count == 0 || num_mech_infos == 0 || rv != CRYPTO_SUCCESS ||
1145 1145 error != 0) {
1146 1146 goto out2;
1147 1147 }
1148 1148
1149 1149 /* copyout mech_infos */
1150 1150 offset = (ulong_t)STRUCT_FADDR(get_all_mech, mi_list);
1151 1151 offset -= (ulong_t)STRUCT_BUF(get_all_mech);
1152 1152
1153 1153 u_minfos = kmem_alloc(num_mech_infos * STRUCT_SIZE(mi), KM_SLEEP);
1154 1154 error = copyout_mechinfos(mode, arg + offset, num_mech_infos,
1155 1155 mech_infos, u_minfos);
1156 1156 kmem_free(u_minfos, num_mech_infos * STRUCT_SIZE(mi));
1157 1157 out2:
1158 1158 if (mech_infos != NULL)
1159 1159 crypto_free_all_mech_info(mech_infos, num_mech_infos);
1160 1160 return (error);
1161 1161 }
1162 1162
1163 1163 /*
1164 1164 * Side-effects:
1165 1165 * 1. This routine stores provider descriptor pointers in an array
1166 1166 * and increments each descriptor's reference count. The array
1167 1167 * is stored in per-minor number storage.
1168 1168 * 2. Destroys the old array and creates a new one every time
1169 1169 * this routine is called.
1170 1170 */
1171 1171 int
1172 1172 crypto_get_provider_list(crypto_minor_t *cm, uint_t *count,
1173 1173 crypto_provider_entry_t **array, boolean_t return_slot_list)
1174 1174 {
1175 1175 kcf_provider_desc_t **provider_array;
1176 1176 crypto_provider_entry_t *p = NULL;
1177 1177 uint_t provider_count;
1178 1178 int rval;
1179 1179 int i;
1180 1180
1181 1181 /*
1182 1182 * Take snapshot of provider table returning only HW entries
1183 1183 * that are in a usable state. Also returns logical provider entries.
1184 1184 */
1185 1185 rval = kcf_get_slot_list(&provider_count, &provider_array, B_FALSE);
1186 1186 if (rval != CRYPTO_SUCCESS)
1187 1187 return (rval);
1188 1188
1189 1189 /* allocate memory before taking cm->cm_lock */
1190 1190 if (return_slot_list) {
1191 1191 if (provider_count != 0) {
1192 1192 p = kmem_alloc(provider_count *
1193 1193 sizeof (crypto_provider_entry_t), KM_SLEEP);
1194 1194 for (i = 0; i < provider_count; i++) {
1195 1195 p[i].pe_provider_id = i;
1196 1196 p[i].pe_mechanism_count =
1197 1197 provider_array[i]->pd_mech_list_count;
1198 1198 }
1199 1199 }
1200 1200 *array = p;
1201 1201 *count = provider_count;
1202 1202 }
1203 1203
1204 1204 /*
1205 1205 * Free existing array of providers and replace with new list.
1206 1206 */
1207 1207 mutex_enter(&cm->cm_lock);
1208 1208 if (cm->cm_provider_array != NULL) {
1209 1209 ASSERT(cm->cm_provider_count > 0);
1210 1210 kcf_free_provider_tab(cm->cm_provider_count,
1211 1211 cm->cm_provider_array);
1212 1212 }
1213 1213
1214 1214 cm->cm_provider_array = provider_array;
1215 1215 cm->cm_provider_count = provider_count;
1216 1216 mutex_exit(&cm->cm_lock);
1217 1217
1218 1218 return (CRYPTO_SUCCESS);
1219 1219 }
1220 1220
1221 1221 /*
1222 1222 * This ioctl returns an array of crypto_provider_entry_t entries.
1223 1223 * This is how consumers learn which hardware providers are available.
1224 1224 */
1225 1225 /* ARGSUSED */
1226 1226 static int
1227 1227 get_provider_list(dev_t dev, caddr_t arg, int mode, int *rval)
1228 1228 {
1229 1229 STRUCT_DECL(crypto_get_provider_list, get_list);
1230 1230 crypto_provider_entry_t *entries;
1231 1231 crypto_minor_t *cm;
1232 1232 size_t copyout_size;
1233 1233 uint_t req_count;
1234 1234 uint_t count;
1235 1235 ulong_t offset;
1236 1236 int rv;
1237 1237
1238 1238 STRUCT_INIT(get_list, mode);
1239 1239
1240 1240 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
1241 1241 cmn_err(CE_WARN, "get_provider_list: failed holding minor");
1242 1242 return (ENXIO);
1243 1243 }
1244 1244
1245 1245 if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
1246 1246 crypto_release_minor(cm);
1247 1247 return (EFAULT);
1248 1248 }
1249 1249
1250 1250 rv = crypto_get_provider_list(cm, &count, &entries, RETURN_LIST);
1251 1251 if (rv != CRYPTO_SUCCESS) {
1252 1252 crypto_release_minor(cm);
1253 1253 STRUCT_FSET(get_list, pl_return_value, rv);
1254 1254 if (copyout(STRUCT_BUF(get_list), arg,
1255 1255 STRUCT_SIZE(get_list)) != 0) {
1256 1256 return (EFAULT);
1257 1257 }
1258 1258 return (0);
1259 1259 }
1260 1260 crypto_release_minor(cm);
1261 1261
1262 1262 /* Number of slots caller thinks we have */
1263 1263 req_count = STRUCT_FGET(get_list, pl_count);
1264 1264
1265 1265 /* Check if only requesting number of slots */
1266 1266 if (req_count == 0) {
1267 1267
1268 1268 STRUCT_FSET(get_list, pl_count, count);
1269 1269 STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
1270 1270
1271 1271 crypto_free_provider_list(entries, count);
1272 1272 if (copyout(STRUCT_BUF(get_list), arg,
1273 1273 STRUCT_SIZE(get_list)) != 0) {
1274 1274 return (EFAULT);
1275 1275 }
1276 1276 return (0);
1277 1277 }
1278 1278
1279 1279 /* check if buffer is too small */
1280 1280 req_count = STRUCT_FGET(get_list, pl_count);
1281 1281 if (count > req_count) {
1282 1282 STRUCT_FSET(get_list, pl_count, count);
1283 1283 STRUCT_FSET(get_list, pl_return_value, CRYPTO_BUFFER_TOO_SMALL);
1284 1284 crypto_free_provider_list(entries, count);
1285 1285 if (copyout(STRUCT_BUF(get_list), arg,
1286 1286 STRUCT_SIZE(get_list)) != 0) {
1287 1287 return (EFAULT);
1288 1288 }
1289 1289 return (0);
1290 1290 }
1291 1291
1292 1292 STRUCT_FSET(get_list, pl_count, count);
1293 1293 STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
1294 1294
1295 1295 copyout_size = count * sizeof (crypto_provider_entry_t);
1296 1296
1297 1297 /* copyout the first stuff */
1298 1298 if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
1299 1299 crypto_free_provider_list(entries, count);
1300 1300 return (EFAULT);
1301 1301 }
1302 1302
1303 1303 if (count == 0) {
1304 1304 crypto_free_provider_list(entries, count);
1305 1305 return (0);
1306 1306 }
1307 1307
1308 1308 /* copyout entries */
1309 1309 offset = (ulong_t)STRUCT_FADDR(get_list, pl_list);
1310 1310 offset -= (ulong_t)STRUCT_BUF(get_list);
1311 1311 if (copyout(entries, arg + offset, copyout_size) != 0) {
1312 1312 crypto_free_provider_list(entries, count);
1313 1313 return (EFAULT);
1314 1314 }
1315 1315
1316 1316 crypto_free_provider_list(entries, count);
1317 1317 return (0);
1318 1318 }
1319 1319
1320 1320 static void
1321 1321 ext_to_provider_data(int mode, kcf_provider_desc_t *provider,
1322 1322 crypto_provider_ext_info_t *ei, void *out)
1323 1323 {
1324 1324 STRUCT_DECL(crypto_provider_data, pd);
1325 1325 STRUCT_DECL(crypto_version, version);
1326 1326
1327 1327 STRUCT_INIT(pd, mode);
1328 1328 STRUCT_INIT(version, mode);
1329 1329
1330 1330 bcopy(provider->pd_description, STRUCT_FGET(pd, pd_prov_desc),
1331 1331 CRYPTO_PROVIDER_DESCR_MAX_LEN);
1332 1332
1333 1333 bcopy(ei->ei_label, STRUCT_FGET(pd, pd_label), CRYPTO_EXT_SIZE_LABEL);
1334 1334 bcopy(ei->ei_manufacturerID, STRUCT_FGET(pd, pd_manufacturerID),
1335 1335 CRYPTO_EXT_SIZE_MANUF);
1336 1336 bcopy(ei->ei_model, STRUCT_FGET(pd, pd_model), CRYPTO_EXT_SIZE_MODEL);
1337 1337 bcopy(ei->ei_serial_number, STRUCT_FGET(pd, pd_serial_number),
1338 1338 CRYPTO_EXT_SIZE_SERIAL);
1339 1339 /*
1340 1340 * We do not support ioctls for dual-function crypto operations yet.
1341 1341 * So, we clear this flag as it might have been set by a provider.
1342 1342 */
1343 1343 ei->ei_flags &= ~CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
1344 1344
1345 1345 STRUCT_FSET(pd, pd_flags, ei->ei_flags);
1346 1346 STRUCT_FSET(pd, pd_max_session_count, ei->ei_max_session_count);
1347 1347 STRUCT_FSET(pd, pd_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
1348 1348 STRUCT_FSET(pd, pd_max_rw_session_count, ei->ei_max_session_count);
1349 1349 STRUCT_FSET(pd, pd_rw_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
1350 1350 STRUCT_FSET(pd, pd_max_pin_len, ei->ei_max_pin_len);
1351 1351 STRUCT_FSET(pd, pd_min_pin_len, ei->ei_min_pin_len);
1352 1352 STRUCT_FSET(pd, pd_total_public_memory, ei->ei_total_public_memory);
1353 1353 STRUCT_FSET(pd, pd_free_public_memory, ei->ei_free_public_memory);
1354 1354 STRUCT_FSET(pd, pd_total_private_memory, ei->ei_total_private_memory);
1355 1355 STRUCT_FSET(pd, pd_free_private_memory, ei->ei_free_private_memory);
1356 1356 STRUCT_FSET(version, cv_major, ei->ei_hardware_version.cv_major);
1357 1357 STRUCT_FSET(version, cv_minor, ei->ei_hardware_version.cv_minor);
1358 1358 bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_hardware_version),
1359 1359 STRUCT_SIZE(version));
1360 1360 STRUCT_FSET(version, cv_major, ei->ei_firmware_version.cv_major);
1361 1361 STRUCT_FSET(version, cv_minor, ei->ei_firmware_version.cv_minor);
1362 1362 bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_firmware_version),
1363 1363 STRUCT_SIZE(version));
1364 1364 bcopy(ei->ei_time, STRUCT_FGET(pd, pd_time), CRYPTO_EXT_SIZE_TIME);
1365 1365 bcopy(STRUCT_BUF(pd), out, STRUCT_SIZE(pd));
1366 1366 }
1367 1367
1368 1368 /*
1369 1369 * Utility routine to construct a crypto_provider_ext_info structure. Some
1370 1370 * of the fields are constructed from information in the provider structure.
1371 1371 * The rest of the fields have default values. We need to do this for
1372 1372 * providers which do not support crypto_provider_management_ops routines.
1373 1373 */
1374 1374 static void
1375 1375 fabricate_ext_info(kcf_provider_desc_t *provider,
1376 1376 crypto_provider_ext_info_t *ei)
1377 1377 {
1378 1378 /* empty label */
1379 1379 (void) memset(ei->ei_label, ' ', CRYPTO_EXT_SIZE_LABEL);
1380 1380
1381 1381 (void) memset(ei->ei_manufacturerID, ' ', CRYPTO_EXT_SIZE_MANUF);
1382 1382 (void) strncpy((char *)ei->ei_manufacturerID, "Unknown", 7);
1383 1383
1384 1384 (void) memset(ei->ei_model, ' ', CRYPTO_EXT_SIZE_MODEL);
1385 1385 (void) strncpy((char *)ei->ei_model, "Unknown", 7);
1386 1386
1387 1387 (void) memset(ei->ei_serial_number, ' ', CRYPTO_EXT_SIZE_SERIAL);
1388 1388 (void) strncpy((char *)ei->ei_serial_number, "Unknown", 7);
1389 1389
1390 1390 if (KCF_PROV_RANDOM_OPS(provider) != NULL)
1391 1391 ei->ei_flags |= CRYPTO_EXTF_RNG;
1392 1392 if (KCF_PROV_DUAL_OPS(provider) != NULL)
1393 1393 ei->ei_flags |= CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
1394 1394
1395 1395 ei->ei_max_session_count = CRYPTO_UNAVAILABLE_INFO;
1396 1396 ei->ei_max_pin_len = 0;
1397 1397 ei->ei_min_pin_len = 0;
1398 1398 ei->ei_total_public_memory = CRYPTO_UNAVAILABLE_INFO;
1399 1399 ei->ei_free_public_memory = CRYPTO_UNAVAILABLE_INFO;
1400 1400 ei->ei_total_private_memory = CRYPTO_UNAVAILABLE_INFO;
1401 1401 ei->ei_free_private_memory = CRYPTO_UNAVAILABLE_INFO;
1402 1402 ei->ei_hardware_version.cv_major = 1;
1403 1403 ei->ei_hardware_version.cv_minor = 0;
1404 1404 ei->ei_firmware_version.cv_major = 1;
1405 1405 ei->ei_firmware_version.cv_minor = 0;
1406 1406 }
1407 1407
1408 1408 /* ARGSUSED */
1409 1409 static int
1410 1410 get_provider_info(dev_t dev, caddr_t arg, int mode, int *rval)
1411 1411 {
1412 1412 STRUCT_DECL(crypto_get_provider_info, get_info);
1413 1413 crypto_minor_t *cm;
1414 1414 crypto_provider_id_t provider_id;
1415 1415 kcf_provider_desc_t *provider, *real_provider;
1416 1416 crypto_provider_ext_info_t *ext_info = NULL;
1417 1417 size_t need;
1418 1418 int error = 0;
1419 1419 int rv;
1420 1420 kcf_req_params_t params;
1421 1421
1422 1422 STRUCT_INIT(get_info, mode);
1423 1423
1424 1424 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
1425 1425 cmn_err(CE_WARN, "get_provider_info: failed holding minor");
1426 1426 return (ENXIO);
1427 1427 }
1428 1428
1429 1429 if (copyin(arg, STRUCT_BUF(get_info), STRUCT_SIZE(get_info)) != 0) {
1430 1430 crypto_release_minor(cm);
1431 1431 return (EFAULT);
1432 1432 }
1433 1433
1434 1434 need = sizeof (crypto_provider_ext_info_t);
1435 1435 if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
1436 1436 need = 0;
1437 1437 goto release_minor;
1438 1438 }
1439 1439
1440 1440 /* initialize provider_array */
1441 1441 if (cm->cm_provider_array == NULL) {
1442 1442 rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
1443 1443 if (rv != CRYPTO_SUCCESS) {
1444 1444 goto release_minor;
1445 1445 }
1446 1446 }
1447 1447
1448 1448 ext_info = kmem_zalloc(need, KM_SLEEP);
1449 1449
1450 1450 provider_id = STRUCT_FGET(get_info, gi_provider_id);
1451 1451 mutex_enter(&cm->cm_lock);
1452 1452 /* index must be less than count of providers */
1453 1453 if (provider_id >= cm->cm_provider_count) {
1454 1454 mutex_exit(&cm->cm_lock);
1455 1455 rv = CRYPTO_ARGUMENTS_BAD;
1456 1456 goto release_minor;
1457 1457 }
1458 1458
1459 1459 ASSERT(cm->cm_provider_array != NULL);
1460 1460 provider = cm->cm_provider_array[provider_id];
1461 1461 KCF_PROV_REFHOLD(provider);
1462 1462 mutex_exit(&cm->cm_lock);
1463 1463
1464 1464 (void) kcf_get_hardware_provider_nomech(
1465 1465 CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(ext_info),
1466 1466 provider, &real_provider);
1467 1467
1468 1468 if (real_provider != NULL) {
1469 1469 ASSERT(real_provider == provider ||
1470 1470 provider->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
1471 1471 KCF_WRAP_PROVMGMT_OPS_PARAMS(¶ms, KCF_OP_MGMT_EXTINFO,
1472 1472 0, NULL, 0, NULL, 0, NULL, ext_info, provider);
1473 1473 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms,
1474 1474 B_FALSE);
1475 1475 ASSERT(rv != CRYPTO_NOT_SUPPORTED);
1476 1476 KCF_PROV_REFRELE(real_provider);
1477 1477 } else {
1478 1478 /* do the best we can */
1479 1479 fabricate_ext_info(provider, ext_info);
1480 1480 rv = CRYPTO_SUCCESS;
1481 1481 }
1482 1482 KCF_PROV_REFRELE(provider);
1483 1483
1484 1484 if (rv == CRYPTO_SUCCESS) {
1485 1485 ext_to_provider_data(mode, provider, ext_info,
1486 1486 STRUCT_FADDR(get_info, gi_provider_data));
1487 1487 }
1488 1488
1489 1489 release_minor:
1490 1490 CRYPTO_DECREMENT_RCTL(need);
1491 1491 crypto_release_minor(cm);
1492 1492
1493 1493 if (ext_info != NULL)
1494 1494 kmem_free(ext_info, sizeof (crypto_provider_ext_info_t));
1495 1495
1496 1496 if (error != 0)
1497 1497 return (error);
1498 1498
1499 1499 STRUCT_FSET(get_info, gi_return_value, rv);
1500 1500 if (copyout(STRUCT_BUF(get_info), arg, STRUCT_SIZE(get_info)) != 0) {
1501 1501 return (EFAULT);
1502 1502 }
1503 1503 return (0);
1504 1504 }
1505 1505
1506 1506 /*
1507 1507 * This ioctl returns an array of crypto_mech_name_t entries.
1508 1508 * This is how consumers learn which mechanisms are permitted
1509 1509 * by a provider.
1510 1510 */
1511 1511 /* ARGSUSED */
1512 1512 static int
1513 1513 get_provider_mechanisms(dev_t dev, caddr_t arg, int mode, int *rval)
1514 1514 {
1515 1515 STRUCT_DECL(crypto_get_provider_mechanisms, get_mechanisms);
1516 1516 crypto_mech_name_t *entries;
1517 1517 crypto_minor_t *cm;
1518 1518 size_t copyout_size;
1519 1519 uint_t req_count;
1520 1520 uint_t count;
1521 1521 ulong_t offset;
1522 1522 int err;
1523 1523
1524 1524 STRUCT_INIT(get_mechanisms, mode);
1525 1525
1526 1526 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
1527 1527 cmn_err(CE_WARN,
1528 1528 "get_provider_mechanisms: failed holding minor");
1529 1529 return (ENXIO);
1530 1530 }
1531 1531
1532 1532 if (copyin(arg, STRUCT_BUF(get_mechanisms),
1533 1533 STRUCT_SIZE(get_mechanisms)) != 0) {
1534 1534 crypto_release_minor(cm);
1535 1535 return (EFAULT);
1536 1536 }
1537 1537
1538 1538 /* get array of mechanisms from the core module */
1539 1539 if ((err = crypto_get_provider_mechanisms(cm,
1540 1540 STRUCT_FGET(get_mechanisms, pm_provider_id),
1541 1541 &count, &entries)) != 0) {
1542 1542 crypto_release_minor(cm);
1543 1543 STRUCT_FSET(get_mechanisms, pm_return_value, err);
1544 1544 if (copyout(STRUCT_BUF(get_mechanisms), arg,
1545 1545 STRUCT_SIZE(get_mechanisms)) != 0) {
1546 1546 return (EFAULT);
1547 1547 }
1548 1548 return (0);
1549 1549 }
1550 1550 crypto_release_minor(cm);
1551 1551 /* Number of mechs caller thinks we have */
1552 1552 req_count = STRUCT_FGET(get_mechanisms, pm_count);
1553 1553
1554 1554 /* Check if caller is just requesting a count of mechanisms */
1555 1555 if (req_count == 0) {
1556 1556 STRUCT_FSET(get_mechanisms, pm_count, count);
1557 1557 STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
1558 1558
1559 1559 crypto_free_mech_list(entries, count);
1560 1560 if (copyout(STRUCT_BUF(get_mechanisms), arg,
1561 1561 STRUCT_SIZE(get_mechanisms)) != 0) {
1562 1562 return (EFAULT);
1563 1563 }
1564 1564 return (0);
1565 1565 }
1566 1566
1567 1567 /* check if buffer is too small */
1568 1568 if (count > req_count) {
1569 1569 STRUCT_FSET(get_mechanisms, pm_count, count);
1570 1570 STRUCT_FSET(get_mechanisms, pm_return_value,
1571 1571 CRYPTO_BUFFER_TOO_SMALL);
1572 1572 crypto_free_mech_list(entries, count);
1573 1573 if (copyout(STRUCT_BUF(get_mechanisms), arg,
1574 1574 STRUCT_SIZE(get_mechanisms)) != 0) {
1575 1575 return (EFAULT);
1576 1576 }
1577 1577 return (0);
1578 1578 }
1579 1579
1580 1580 STRUCT_FSET(get_mechanisms, pm_count, count);
1581 1581 STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
1582 1582
1583 1583 copyout_size = count * sizeof (crypto_mech_name_t);
1584 1584
1585 1585 /* copyout the first stuff */
1586 1586 if (copyout(STRUCT_BUF(get_mechanisms), arg,
1587 1587 STRUCT_SIZE(get_mechanisms)) != 0) {
1588 1588 crypto_free_mech_list(entries, count);
1589 1589 return (EFAULT);
1590 1590 }
1591 1591
1592 1592 if (count == 0) {
1593 1593 return (0);
1594 1594 }
1595 1595
1596 1596 /* copyout entries */
1597 1597 offset = (ulong_t)STRUCT_FADDR(get_mechanisms, pm_list);
1598 1598 offset -= (ulong_t)STRUCT_BUF(get_mechanisms);
1599 1599 if (copyout(entries, arg + offset, copyout_size) != 0) {
1600 1600 crypto_free_mech_list(entries, count);
1601 1601 return (EFAULT);
1602 1602 }
1603 1603
1604 1604 crypto_free_mech_list(entries, count);
1605 1605 return (0);
1606 1606 }
1607 1607
1608 1608 /*
1609 1609 * This ioctl returns information about a provider's mechanism.
1610 1610 */
1611 1611 /* ARGSUSED */
1612 1612 static int
1613 1613 get_provider_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
1614 1614 {
1615 1615 crypto_get_provider_mechanism_info_t mechanism_info;
1616 1616 crypto_minor_t *cm;
1617 1617 kcf_provider_desc_t *pd;
1618 1618 crypto_mech_info_t *mi = NULL;
1619 1619 int rv = CRYPTO_SUCCESS;
1620 1620 int i;
1621 1621
1622 1622 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
1623 1623 cmn_err(CE_WARN,
1624 1624 "get_provider_mechanism_info: failed holding minor");
1625 1625 return (ENXIO);
1626 1626 }
1627 1627
1628 1628 if (copyin(arg, &mechanism_info, sizeof (mechanism_info)) != 0) {
1629 1629 crypto_release_minor(cm);
1630 1630 return (EFAULT);
1631 1631 }
1632 1632
1633 1633 /* initialize provider table */
1634 1634 if (cm->cm_provider_array == NULL) {
1635 1635 rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
1636 1636 if (rv != CRYPTO_SUCCESS) {
1637 1637 mutex_enter(&cm->cm_lock);
1638 1638 goto fail;
1639 1639 }
1640 1640 }
1641 1641
1642 1642 /*
1643 1643 * Provider ID must be less than the count of providers
1644 1644 * obtained by calling get_provider_list().
1645 1645 */
1646 1646 mutex_enter(&cm->cm_lock);
1647 1647 if (mechanism_info.mi_provider_id >= cm->cm_provider_count) {
1648 1648 rv = CRYPTO_ARGUMENTS_BAD;
1649 1649 goto fail;
1650 1650 }
1651 1651
1652 1652 pd = cm->cm_provider_array[mechanism_info.mi_provider_id];
1653 1653
1654 1654 /* First check if the provider supports the mechanism. */
1655 1655 for (i = 0; i < pd->pd_mech_list_count; i++) {
1656 1656 if (strncmp(pd->pd_mechanisms[i].cm_mech_name,
1657 1657 mechanism_info.mi_mechanism_name,
1658 1658 CRYPTO_MAX_MECH_NAME) == 0) {
1659 1659 mi = &pd->pd_mechanisms[i];
1660 1660 break;
1661 1661 }
1662 1662 }
1663 1663
1664 1664 if (mi == NULL) {
1665 1665 rv = CRYPTO_ARGUMENTS_BAD;
1666 1666 goto fail;
1667 1667 }
1668 1668
1669 1669 /* Now check if the mechanism is enabled for the provider. */
1670 1670 if (is_mech_disabled(pd, mechanism_info.mi_mechanism_name)) {
1671 1671 rv = CRYPTO_MECHANISM_INVALID;
1672 1672 goto fail;
1673 1673 }
1674 1674
1675 1675 mechanism_info.mi_min_key_size = mi->cm_min_key_length;
1676 1676 mechanism_info.mi_max_key_size = mi->cm_max_key_length;
1677 1677 mechanism_info.mi_flags = mi->cm_func_group_mask;
1678 1678
1679 1679 fail:
1680 1680 mutex_exit(&cm->cm_lock);
1681 1681 crypto_release_minor(cm);
1682 1682 mechanism_info.mi_return_value = rv;
1683 1683 if (copyout(&mechanism_info, arg, sizeof (mechanism_info)) != 0) {
1684 1684 return (EFAULT);
1685 1685 }
1686 1686
1687 1687 return (0);
1688 1688 }
1689 1689
1690 1690 /*
1691 1691 * Every open of /dev/crypto multiplexes all PKCS#11 sessions across
1692 1692 * a single session to each provider. Calls to open and close session
1693 1693 * are not made to providers that do not support sessions. For these
1694 1694 * providers, a session number of 0 is passed during subsequent operations,
1695 1695 * and it is ignored by the provider.
1696 1696 */
1697 1697 static int
1698 1698 crypto_get_provider_session(crypto_minor_t *cm,
1699 1699 crypto_provider_id_t provider_index, crypto_provider_session_t **output_ps)
1700 1700 {
1701 1701 kcf_provider_desc_t *pd, *real_provider;
1702 1702 kcf_req_params_t params;
1703 1703 crypto_provider_session_t *ps, *new_ps;
1704 1704 crypto_session_id_t provider_session_id = 0;
1705 1705 int rv;
1706 1706
1707 1707 ASSERT(MUTEX_HELD(&cm->cm_lock));
1708 1708
1709 1709 /* pd may be a logical provider */
1710 1710 pd = cm->cm_provider_array[provider_index];
1711 1711
1712 1712 again:
1713 1713 /*
1714 1714 * Check if there is already a session to the provider.
1715 1715 * Sessions may be to a logical provider or a real provider.
1716 1716 */
1717 1717 for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
1718 1718 if (ps->ps_provider == pd)
1719 1719 break;
1720 1720 }
1721 1721
1722 1722 /* found existing session */
1723 1723 if (ps != NULL) {
1724 1724 ps->ps_refcnt++;
1725 1725 *output_ps = ps;
1726 1726 return (CRYPTO_SUCCESS);
1727 1727 }
1728 1728 mutex_exit(&cm->cm_lock);
1729 1729
1730 1730 /* find a hardware provider that supports session ops */
1731 1731 (void) kcf_get_hardware_provider_nomech(CRYPTO_OPS_OFFSET(session_ops),
1732 1732 CRYPTO_SESSION_OFFSET(session_open), pd, &real_provider);
1733 1733
1734 1734 if (real_provider != NULL) {
1735 1735 ASSERT(real_provider == pd ||
1736 1736 pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
1737 1737 /* open session to provider */
1738 1738 KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_OPEN,
1739 1739 &provider_session_id, 0, CRYPTO_USER, NULL, 0, pd);
1740 1740 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms,
1741 1741 B_FALSE);
1742 1742 if (rv != CRYPTO_SUCCESS) {
1743 1743 mutex_enter(&cm->cm_lock);
1744 1744 KCF_PROV_REFRELE(real_provider);
1745 1745 return (rv);
1746 1746 }
1747 1747 }
1748 1748
1749 1749 /* allocate crypto_provider_session structure */
1750 1750 new_ps = kmem_zalloc(sizeof (crypto_provider_session_t), KM_SLEEP);
1751 1751
1752 1752 /*
1753 1753 * Check if someone opened a session to the provider
1754 1754 * while we dropped the lock.
1755 1755 */
1756 1756 mutex_enter(&cm->cm_lock);
1757 1757 for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
1758 1758 if (ps->ps_provider == pd) {
1759 1759 mutex_exit(&cm->cm_lock);
1760 1760 kmem_free(new_ps, sizeof (crypto_provider_session_t));
1761 1761 if (real_provider != NULL) {
1762 1762 KCF_WRAP_SESSION_OPS_PARAMS(¶ms,
1763 1763 KCF_OP_SESSION_CLOSE, NULL,
1764 1764 provider_session_id, CRYPTO_USER, NULL, 0,
1765 1765 pd);
1766 1766 (void) kcf_submit_request(real_provider, NULL,
1767 1767 NULL, ¶ms, B_FALSE);
1768 1768 KCF_PROV_REFRELE(real_provider);
1769 1769 }
1770 1770 mutex_enter(&cm->cm_lock);
1771 1771 goto again;
1772 1772
1773 1773 }
1774 1774 }
1775 1775
1776 1776 /* increment refcnt and attach to crypto_minor structure */
1777 1777 new_ps->ps_session = provider_session_id;
1778 1778 new_ps->ps_refcnt = 1;
1779 1779 KCF_PROV_REFHOLD(pd);
1780 1780 new_ps->ps_provider = pd;
1781 1781 if (real_provider != NULL) {
1782 1782 new_ps->ps_real_provider = real_provider;
1783 1783 }
1784 1784 new_ps->ps_next = cm->cm_provider_session;
1785 1785 cm->cm_provider_session = new_ps;
1786 1786
1787 1787 *output_ps = new_ps;
1788 1788 return (CRYPTO_SUCCESS);
1789 1789 }
1790 1790
1791 1791 /*
1792 1792 * Release a provider session.
1793 1793 * If the reference count goes to zero, then close the session
1794 1794 * to the provider.
1795 1795 */
1796 1796 static void
1797 1797 crypto_release_provider_session(crypto_minor_t *cm,
1798 1798 crypto_provider_session_t *provider_session)
1799 1799 {
1800 1800 kcf_req_params_t params;
1801 1801 crypto_provider_session_t *ps = NULL, **prev;
1802 1802
1803 1803 ASSERT(MUTEX_HELD(&cm->cm_lock));
1804 1804
1805 1805 /* verify that provider_session is valid */
1806 1806 for (ps = cm->cm_provider_session, prev = &cm->cm_provider_session;
1807 1807 ps != NULL; prev = &ps->ps_next, ps = ps->ps_next) {
1808 1808 if (ps == provider_session) {
1809 1809 break;
1810 1810 }
1811 1811 }
1812 1812
1813 1813 if (ps == NULL)
1814 1814 return;
1815 1815
1816 1816 ps->ps_refcnt--;
1817 1817
1818 1818 if (ps->ps_refcnt > 0)
1819 1819 return;
1820 1820
1821 1821 if (ps->ps_real_provider != NULL) {
1822 1822 /* close session with provider */
1823 1823 KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_CLOSE, NULL,
1824 1824 ps->ps_session, CRYPTO_USER, NULL, 0, ps->ps_provider);
1825 1825 (void) kcf_submit_request(ps->ps_real_provider,
1826 1826 NULL, NULL, ¶ms, B_FALSE);
1827 1827 KCF_PROV_REFRELE(ps->ps_real_provider);
1828 1828 }
1829 1829 KCF_PROV_REFRELE(ps->ps_provider);
1830 1830 *prev = ps->ps_next;
1831 1831 kmem_free(ps, sizeof (*ps));
1832 1832 }
1833 1833
1834 1834 static int
1835 1835 grow_session_table(crypto_minor_t *cm)
1836 1836 {
1837 1837 crypto_session_data_t **session_table;
1838 1838 crypto_session_data_t **new;
1839 1839 uint_t session_table_count;
1840 1840 uint_t need;
1841 1841 size_t current_allocation;
1842 1842 size_t new_allocation;
1843 1843 int rv;
1844 1844
1845 1845 ASSERT(MUTEX_HELD(&cm->cm_lock));
1846 1846
1847 1847 session_table_count = cm->cm_session_table_count;
1848 1848 session_table = cm->cm_session_table;
1849 1849 need = session_table_count + CRYPTO_SESSION_CHUNK;
1850 1850
1851 1851 current_allocation = session_table_count * sizeof (void *);
1852 1852 new_allocation = need * sizeof (void *);
1853 1853
1854 1854 /*
1855 1855 * Memory needed to grow the session table is checked
1856 1856 * against the project.max-crypto-memory resource control.
1857 1857 */
1858 1858 if ((rv = crypto_buffer_check(new_allocation - current_allocation)) !=
1859 1859 CRYPTO_SUCCESS) {
1860 1860 return (rv);
1861 1861 }
1862 1862
1863 1863 /* drop lock while we allocate memory */
1864 1864 mutex_exit(&cm->cm_lock);
1865 1865 new = kmem_zalloc(new_allocation, KM_SLEEP);
1866 1866 mutex_enter(&cm->cm_lock);
1867 1867
1868 1868 /* check if another thread increased the table size */
1869 1869 if (session_table_count != cm->cm_session_table_count) {
1870 1870 kmem_free(new, new_allocation);
1871 1871 return (CRYPTO_SUCCESS);
1872 1872 }
1873 1873
1874 1874 bcopy(session_table, new, current_allocation);
1875 1875 kmem_free(session_table, current_allocation);
1876 1876 cm->cm_session_table = new;
1877 1877 cm->cm_session_table_count += CRYPTO_SESSION_CHUNK;
1878 1878
1879 1879 return (CRYPTO_SUCCESS);
1880 1880 }
1881 1881
1882 1882 /*
1883 1883 * Find unused entry in session table and return it's index.
1884 1884 * Initialize session table entry.
1885 1885 */
1886 1886 /* ARGSUSED */
1887 1887 static int
1888 1888 crypto_open_session(dev_t dev, uint_t flags, crypto_session_id_t *session_index,
1889 1889 crypto_provider_id_t provider_id)
1890 1890 {
1891 1891 crypto_session_data_t **session_table;
1892 1892 crypto_session_data_t *sp;
1893 1893 crypto_minor_t *cm;
1894 1894 uint_t session_table_count;
1895 1895 uint_t i;
1896 1896 int rv;
1897 1897 crypto_provider_session_t *ps;
1898 1898 kcf_provider_desc_t *provider;
1899 1899
1900 1900 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
1901 1901 cmn_err(CE_WARN, "crypto_open_session: failed holding minor");
1902 1902 return (CRYPTO_FAILED);
1903 1903 }
1904 1904
1905 1905 /* initialize provider_array */
1906 1906 if (cm->cm_provider_array == NULL) {
1907 1907 rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
1908 1908 if (rv != 0) {
1909 1909 crypto_release_minor(cm);
1910 1910 return (rv);
1911 1911 }
1912 1912 }
1913 1913
1914 1914 mutex_enter(&cm->cm_lock);
1915 1915 /* index must be less than count of providers */
1916 1916 if (provider_id >= cm->cm_provider_count) {
1917 1917 mutex_exit(&cm->cm_lock);
1918 1918 crypto_release_minor(cm);
1919 1919 return (CRYPTO_INVALID_PROVIDER_ID);
1920 1920 }
1921 1921 ASSERT(cm->cm_provider_array != NULL);
1922 1922
1923 1923 rv = crypto_get_provider_session(cm, provider_id, &ps);
1924 1924 if (rv != CRYPTO_SUCCESS) {
1925 1925 mutex_exit(&cm->cm_lock);
1926 1926 crypto_release_minor(cm);
1927 1927 return (rv);
1928 1928 }
1929 1929 provider = cm->cm_provider_array[provider_id];
1930 1930
1931 1931 again:
1932 1932 session_table_count = cm->cm_session_table_count;
1933 1933 session_table = cm->cm_session_table;
1934 1934
1935 1935 /* session handles start with 1 */
1936 1936 for (i = 1; i < session_table_count; i++) {
1937 1937 if (session_table[i] == NULL)
1938 1938 break;
1939 1939 }
1940 1940
1941 1941 if (i == session_table_count || session_table_count == 0) {
1942 1942 if ((rv = grow_session_table(cm)) != CRYPTO_SUCCESS) {
1943 1943 crypto_release_provider_session(cm, ps);
1944 1944 mutex_exit(&cm->cm_lock);
1945 1945 crypto_release_minor(cm);
1946 1946 return (rv);
1947 1947 }
1948 1948 goto again;
1949 1949 }
1950 1950
1951 1951 sp = kmem_cache_alloc(crypto_session_cache, KM_SLEEP);
1952 1952 sp->sd_flags = 0;
1953 1953 sp->sd_find_init_cookie = NULL;
1954 1954 sp->sd_digest_ctx = NULL;
1955 1955 sp->sd_encr_ctx = NULL;
1956 1956 sp->sd_decr_ctx = NULL;
1957 1957 sp->sd_sign_ctx = NULL;
1958 1958 sp->sd_verify_ctx = NULL;
1959 1959 sp->sd_sign_recover_ctx = NULL;
1960 1960 sp->sd_verify_recover_ctx = NULL;
1961 1961 mutex_init(&sp->sd_lock, NULL, MUTEX_DRIVER, NULL);
1962 1962 cv_init(&sp->sd_cv, NULL, CV_DRIVER, NULL);
1963 1963 KCF_PROV_REFHOLD(provider);
1964 1964 sp->sd_provider = provider;
1965 1965 sp->sd_provider_session = ps;
1966 1966
1967 1967 /* See the comment for CRYPTO_PRE_APPROVED_LIMIT. */
1968 1968 if ((rv = crypto_buffer_check(crypto_pre_approved_limit)) !=
1969 1969 CRYPTO_SUCCESS) {
1970 1970 sp->sd_pre_approved_amount = 0;
1971 1971 } else {
1972 1972 sp->sd_pre_approved_amount = (int)crypto_pre_approved_limit;
1973 1973 }
1974 1974
1975 1975 cm->cm_session_table[i] = sp;
1976 1976 mutex_exit(&cm->cm_lock);
1977 1977 crypto_release_minor(cm);
1978 1978 *session_index = i;
1979 1979
1980 1980 return (CRYPTO_SUCCESS);
1981 1981 }
1982 1982
1983 1983 /*
1984 1984 * Close a session.
1985 1985 */
1986 1986 static int
1987 1987 crypto_close_session(dev_t dev, crypto_session_id_t session_index)
1988 1988 {
1989 1989 crypto_session_data_t **session_table;
1990 1990 crypto_session_data_t *sp;
1991 1991 crypto_minor_t *cm;
1992 1992
1993 1993 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
1994 1994 cmn_err(CE_WARN, "crypto_close_session: failed holding minor");
1995 1995 return (CRYPTO_FAILED);
1996 1996 }
1997 1997
1998 1998 mutex_enter(&cm->cm_lock);
1999 1999 session_table = cm->cm_session_table;
2000 2000
2001 2001 if ((session_index) == 0 ||
2002 2002 (session_index >= cm->cm_session_table_count)) {
2003 2003 mutex_exit(&cm->cm_lock);
2004 2004 crypto_release_minor(cm);
2005 2005 return (CRYPTO_SESSION_HANDLE_INVALID);
2006 2006 }
2007 2007
2008 2008 sp = session_table[session_index];
2009 2009 if (sp == NULL) {
2010 2010 mutex_exit(&cm->cm_lock);
2011 2011 crypto_release_minor(cm);
2012 2012 return (CRYPTO_SESSION_HANDLE_INVALID);
2013 2013 }
2014 2014 /*
2015 2015 * If session is in use, free it when the thread
2016 2016 * finishes with the session.
2017 2017 */
2018 2018 mutex_enter(&sp->sd_lock);
2019 2019 if (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
2020 2020 sp->sd_flags |= CRYPTO_SESSION_IS_CLOSED;
2021 2021 mutex_exit(&sp->sd_lock);
2022 2022 } else {
2023 2023 ASSERT(sp->sd_pre_approved_amount == 0 ||
2024 2024 sp->sd_pre_approved_amount == crypto_pre_approved_limit);
2025 2025 CRYPTO_DECREMENT_RCTL(sp->sd_pre_approved_amount);
2026 2026
2027 2027 if (sp->sd_find_init_cookie != NULL) {
2028 2028 (void) crypto_free_find_ctx(sp);
2029 2029 }
2030 2030
2031 2031 crypto_release_provider_session(cm, sp->sd_provider_session);
2032 2032 KCF_PROV_REFRELE(sp->sd_provider);
2033 2033 CRYPTO_CANCEL_ALL_CTX(sp);
2034 2034 mutex_destroy(&sp->sd_lock);
2035 2035 cv_destroy(&sp->sd_cv);
2036 2036 kmem_cache_free(crypto_session_cache, sp);
2037 2037 session_table[session_index] = NULL;
2038 2038 }
2039 2039
2040 2040 mutex_exit(&cm->cm_lock);
2041 2041 crypto_release_minor(cm);
2042 2042
2043 2043 return (CRYPTO_SUCCESS);
2044 2044 }
2045 2045
2046 2046 /*
2047 2047 * This ioctl opens a session and returns the session ID in os_session.
2048 2048 */
2049 2049 /* ARGSUSED */
2050 2050 static int
2051 2051 open_session(dev_t dev, caddr_t arg, int mode, int *rval)
2052 2052 {
2053 2053 crypto_open_session_t open_session;
2054 2054 crypto_session_id_t session;
2055 2055 int rv;
2056 2056
2057 2057 if (copyin(arg, &open_session, sizeof (open_session)) != 0)
2058 2058 return (EFAULT);
2059 2059
2060 2060 rv = crypto_open_session(dev, open_session.os_flags,
2061 2061 &session, open_session.os_provider_id);
2062 2062 if (rv != CRYPTO_SUCCESS) {
2063 2063 open_session.os_return_value = rv;
2064 2064 if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
2065 2065 return (EFAULT);
2066 2066 }
2067 2067 return (0);
2068 2068 }
2069 2069
2070 2070 open_session.os_session = session;
2071 2071 open_session.os_return_value = CRYPTO_SUCCESS;
2072 2072
2073 2073 if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
2074 2074 return (EFAULT);
2075 2075 }
2076 2076 return (0);
2077 2077 }
2078 2078
2079 2079 /*
2080 2080 * This ioctl closes a session.
2081 2081 */
2082 2082 /* ARGSUSED */
2083 2083 static int
2084 2084 close_session(dev_t dev, caddr_t arg, int mode, int *rval)
2085 2085 {
2086 2086 crypto_close_session_t close_session;
2087 2087 int rv;
2088 2088
2089 2089 if (copyin(arg, &close_session, sizeof (close_session)) != 0)
2090 2090 return (EFAULT);
2091 2091
2092 2092 rv = crypto_close_session(dev, close_session.cs_session);
2093 2093 close_session.cs_return_value = rv;
2094 2094 if (copyout(&close_session, arg, sizeof (close_session)) != 0) {
2095 2095 return (EFAULT);
2096 2096 }
2097 2097 return (0);
2098 2098 }
2099 2099
2100 2100 /*
2101 2101 * Copy data model dependent mechanism structure into a kernel mechanism
2102 2102 * structure. Allocate param storage if necessary.
2103 2103 */
2104 2104 static boolean_t
2105 2105 copyin_mech(int mode, crypto_session_data_t *sp, crypto_mechanism_t *in_mech,
2106 2106 crypto_mechanism_t *out_mech, size_t *out_rctl_bytes,
2107 2107 boolean_t *out_rctl_chk, int *out_rv, int *out_error)
2108 2108 {
2109 2109 STRUCT_DECL(crypto_mechanism, mech);
2110 2110 caddr_t param;
2111 2111 size_t param_len;
2112 2112 size_t rctl_bytes = 0;
2113 2113 int error = 0;
2114 2114 int rv = 0;
2115 2115
2116 2116 STRUCT_INIT(mech, mode);
2117 2117 bcopy(in_mech, STRUCT_BUF(mech), STRUCT_SIZE(mech));
2118 2118 param = STRUCT_FGETP(mech, cm_param);
2119 2119 param_len = STRUCT_FGET(mech, cm_param_len);
2120 2120 out_mech->cm_type = STRUCT_FGET(mech, cm_type);
2121 2121 out_mech->cm_param = NULL;
2122 2122 out_mech->cm_param_len = 0;
2123 2123 if (param != NULL && param_len != 0) {
2124 2124 if (param_len > crypto_max_buffer_len) {
2125 2125 cmn_err(CE_NOTE, "copyin_mech: buffer greater than "
2126 2126 "%ld bytes, pid = %d", crypto_max_buffer_len,
2127 2127 curproc->p_pid);
2128 2128 rv = CRYPTO_ARGUMENTS_BAD;
2129 2129 goto out;
2130 2130 }
2131 2131
2132 2132 rv = CRYPTO_BUFFER_CHECK(sp, param_len, *out_rctl_chk);
2133 2133 if (rv != CRYPTO_SUCCESS) {
2134 2134 goto out;
2135 2135 }
2136 2136 rctl_bytes = param_len;
2137 2137
2138 2138 out_mech->cm_param = kmem_alloc(param_len, KM_SLEEP);
2139 2139 if (copyin((char *)param, out_mech->cm_param, param_len) != 0) {
2140 2140 kmem_free(out_mech->cm_param, param_len);
2141 2141 out_mech->cm_param = NULL;
2142 2142 error = EFAULT;
2143 2143 goto out;
2144 2144 }
2145 2145 out_mech->cm_param_len = param_len;
2146 2146 }
2147 2147 out:
2148 2148 *out_rctl_bytes = rctl_bytes;
2149 2149 *out_rv = rv;
2150 2150 *out_error = error;
2151 2151 return ((rv | error) ? B_FALSE : B_TRUE);
2152 2152 }
2153 2153
2154 2154 /*
2155 2155 * Free key attributes when key type is CRYPTO_KEY_ATTR_LIST.
2156 2156 * The crypto_key structure is not freed.
2157 2157 */
2158 2158 static void
2159 2159 crypto_free_key_attributes(crypto_key_t *key)
2160 2160 {
2161 2161 crypto_object_attribute_t *attrs;
2162 2162 size_t len = 0;
2163 2163 int i;
2164 2164
2165 2165 ASSERT(key->ck_format == CRYPTO_KEY_ATTR_LIST);
2166 2166 if (key->ck_count == 0 || key->ck_attrs == NULL)
2167 2167 return;
2168 2168
2169 2169 /* compute the size of the container */
2170 2170 len = key->ck_count * sizeof (crypto_object_attribute_t);
2171 2171
2172 2172 /* total up the size of all attributes in the container */
2173 2173 for (i = 0; i < key->ck_count; i++) {
2174 2174 attrs = &key->ck_attrs[i];
2175 2175 if (attrs->oa_value_len != 0 &&
2176 2176 attrs->oa_value != NULL) {
2177 2177 len += roundup(attrs->oa_value_len, sizeof (caddr_t));
2178 2178 }
2179 2179 }
2180 2180
2181 2181 bzero(key->ck_attrs, len);
2182 2182 kmem_free(key->ck_attrs, len);
2183 2183 }
2184 2184
2185 2185 /*
2186 2186 * Frees allocated storage in the key structure, but doesn't free
2187 2187 * the key structure.
2188 2188 */
2189 2189 static void
2190 2190 free_crypto_key(crypto_key_t *key)
2191 2191 {
2192 2192 switch (key->ck_format) {
2193 2193 case CRYPTO_KEY_RAW: {
2194 2194 size_t len;
2195 2195
2196 2196 if (key->ck_length == 0 || key->ck_data == NULL)
2197 2197 break;
2198 2198
2199 2199 len = CRYPTO_BITS2BYTES(key->ck_length);
2200 2200 bzero(key->ck_data, len);
2201 2201 kmem_free(key->ck_data, len);
2202 2202 break;
2203 2203 }
2204 2204
2205 2205 case CRYPTO_KEY_ATTR_LIST:
2206 2206 crypto_free_key_attributes(key);
2207 2207 break;
2208 2208
2209 2209 default:
2210 2210 break;
2211 2211 }
2212 2212 }
2213 2213
2214 2214 /*
2215 2215 * Copy in an array of crypto_object_attribute structures from user-space.
2216 2216 * Kernel memory is allocated for the array and the value of each attribute
2217 2217 * in the array. Since unprivileged users can specify the size of attributes,
2218 2218 * the amount of memory needed is charged against the
2219 2219 * project.max-crypto-memory resource control.
2220 2220 *
2221 2221 * Attribute values are copied in from user-space if copyin_value is set to
2222 2222 * B_TRUE. This routine returns B_TRUE if the copyin was successful.
2223 2223 */
2224 2224 static boolean_t
2225 2225 copyin_attributes(int mode, crypto_session_data_t *sp,
2226 2226 uint_t count, caddr_t oc_attributes,
2227 2227 crypto_object_attribute_t **k_attrs_out, size_t *k_attrs_size_out,
2228 2228 caddr_t *u_attrs_out, int *out_rv, int *out_error, size_t *out_rctl_bytes,
2229 2229 boolean_t *out_rctl_chk, boolean_t copyin_value)
2230 2230 {
2231 2231 STRUCT_DECL(crypto_object_attribute, oa);
2232 2232 crypto_object_attribute_t *k_attrs = NULL;
2233 2233 caddr_t attrs = NULL, ap, p, value;
2234 2234 caddr_t k_attrs_buf;
2235 2235 size_t k_attrs_len;
2236 2236 size_t k_attrs_buf_len = 0;
2237 2237 size_t k_attrs_total_len = 0;
2238 2238 size_t tmp_len;
2239 2239 size_t rctl_bytes = 0;
2240 2240 size_t len = 0;
2241 2241 size_t value_len;
2242 2242 int error = 0;
2243 2243 int rv = 0;
2244 2244 int i;
2245 2245
2246 2246 STRUCT_INIT(oa, mode);
2247 2247
2248 2248 if (count == 0) {
2249 2249 rv = CRYPTO_SUCCESS;
2250 2250 goto out;
2251 2251 }
2252 2252
2253 2253 if (count > CRYPTO_MAX_ATTRIBUTE_COUNT) {
2254 2254 rv = CRYPTO_ARGUMENTS_BAD;
2255 2255 goto out;
2256 2256 }
2257 2257
2258 2258 /* compute size of crypto_object_attribute array */
2259 2259 len = count * STRUCT_SIZE(oa);
2260 2260
2261 2261 /* this allocation is not charged against the user's resource limit */
2262 2262 attrs = kmem_alloc(len, KM_SLEEP);
2263 2263 if (copyin(oc_attributes, attrs, len) != 0) {
2264 2264 error = EFAULT;
2265 2265 goto out;
2266 2266 }
2267 2267
2268 2268 /* figure out how much memory to allocate for all of the attributes */
2269 2269 ap = attrs;
2270 2270 for (i = 0; i < count; i++) {
2271 2271 bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
2272 2272 tmp_len = roundup(STRUCT_FGET(oa, oa_value_len),
2273 2273 sizeof (caddr_t));
2274 2274 if (tmp_len > crypto_max_buffer_len) {
2275 2275 cmn_err(CE_NOTE, "copyin_attributes: buffer greater "
2276 2276 "than %ld bytes, pid = %d", crypto_max_buffer_len,
2277 2277 curproc->p_pid);
2278 2278 rv = CRYPTO_ARGUMENTS_BAD;
2279 2279 goto out;
2280 2280 }
2281 2281 if (STRUCT_FGETP(oa, oa_value) != NULL)
2282 2282 k_attrs_buf_len += tmp_len;
2283 2283 ap += STRUCT_SIZE(oa);
2284 2284 }
2285 2285
2286 2286 k_attrs_len = count * sizeof (crypto_object_attribute_t);
2287 2287 k_attrs_total_len = k_attrs_buf_len + k_attrs_len;
2288 2288
2289 2289 rv = CRYPTO_BUFFER_CHECK(sp, k_attrs_total_len, *out_rctl_chk);
2290 2290 if (rv != CRYPTO_SUCCESS) {
2291 2291 goto out;
2292 2292 }
2293 2293 rctl_bytes = k_attrs_total_len;
2294 2294
2295 2295 /* one big allocation for everything */
2296 2296 k_attrs = kmem_alloc(k_attrs_total_len, KM_SLEEP);
2297 2297 k_attrs_buf = (char *)k_attrs + k_attrs_len;
2298 2298
2299 2299 ap = attrs;
2300 2300 p = k_attrs_buf;
2301 2301 for (i = 0; i < count; i++) {
2302 2302 bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
2303 2303 k_attrs[i].oa_type = STRUCT_FGET(oa, oa_type);
2304 2304 value = STRUCT_FGETP(oa, oa_value);
2305 2305 value_len = STRUCT_FGET(oa, oa_value_len);
2306 2306 if (value != NULL && value_len != 0 && copyin_value) {
2307 2307 if (copyin(value, p, value_len) != 0) {
2308 2308 kmem_free(k_attrs, k_attrs_total_len);
2309 2309 k_attrs = NULL;
2310 2310 error = EFAULT;
2311 2311 goto out;
2312 2312 }
2313 2313 }
2314 2314
2315 2315 if (value != NULL) {
2316 2316 k_attrs[i].oa_value = p;
2317 2317 p += roundup(value_len, sizeof (caddr_t));
2318 2318 } else {
2319 2319 k_attrs[i].oa_value = NULL;
2320 2320 }
2321 2321 k_attrs[i].oa_value_len = value_len;
2322 2322 ap += STRUCT_SIZE(oa);
2323 2323 }
2324 2324 out:
2325 2325 if (attrs != NULL) {
2326 2326 /*
2327 2327 * Free the array if there is a failure or the caller
2328 2328 * doesn't want the array to be returned.
2329 2329 */
2330 2330 if (error != 0 || rv != CRYPTO_SUCCESS || u_attrs_out == NULL) {
2331 2331 kmem_free(attrs, len);
2332 2332 attrs = NULL;
2333 2333 }
2334 2334 }
2335 2335
2336 2336 if (u_attrs_out != NULL)
2337 2337 *u_attrs_out = attrs;
2338 2338 if (k_attrs_size_out != NULL)
2339 2339 *k_attrs_size_out = k_attrs_total_len;
2340 2340 *k_attrs_out = k_attrs;
2341 2341 *out_rctl_bytes = rctl_bytes;
2342 2342 *out_rv = rv;
2343 2343 *out_error = error;
2344 2344 return ((rv | error) ? B_FALSE : B_TRUE);
2345 2345 }
2346 2346
2347 2347 /*
2348 2348 * Copy data model dependent raw key into a kernel key
2349 2349 * structure. Checks key length or attribute lengths against
2350 2350 * resource controls before allocating memory. Returns B_TRUE
2351 2351 * if both error and rv are set to 0.
2352 2352 */
2353 2353 static boolean_t
2354 2354 copyin_key(int mode, crypto_session_data_t *sp, crypto_key_t *in_key,
2355 2355 crypto_key_t *out_key, size_t *out_rctl_bytes,
2356 2356 boolean_t *out_rctl_chk, int *out_rv, int *out_error)
2357 2357 {
2358 2358 STRUCT_DECL(crypto_key, key);
2359 2359 crypto_object_attribute_t *k_attrs = NULL;
2360 2360 size_t key_bits;
2361 2361 size_t key_bytes = 0;
2362 2362 size_t rctl_bytes = 0;
2363 2363 int count;
2364 2364 int error = 0;
2365 2365 int rv = CRYPTO_SUCCESS;
2366 2366
2367 2367 STRUCT_INIT(key, mode);
2368 2368 bcopy(in_key, STRUCT_BUF(key), STRUCT_SIZE(key));
2369 2369 out_key->ck_format = STRUCT_FGET(key, ck_format);
2370 2370 switch (out_key->ck_format) {
2371 2371 case CRYPTO_KEY_RAW:
2372 2372 key_bits = STRUCT_FGET(key, ck_length);
2373 2373 if (key_bits != 0) {
2374 2374 if (key_bits >
2375 2375 (CRYPTO_BYTES2BITS(crypto_max_buffer_len))) {
2376 2376 cmn_err(CE_NOTE, "copyin_key: buffer greater "
2377 2377 "than %ld bytes, pid = %d",
2378 2378 crypto_max_buffer_len, curproc->p_pid);
2379 2379 rv = CRYPTO_ARGUMENTS_BAD;
2380 2380 goto out;
2381 2381 }
2382 2382 key_bytes = CRYPTO_BITS2BYTES(key_bits);
2383 2383
2384 2384 rv = CRYPTO_BUFFER_CHECK(sp, key_bytes,
2385 2385 *out_rctl_chk);
2386 2386 if (rv != CRYPTO_SUCCESS) {
2387 2387 goto out;
2388 2388 }
2389 2389 rctl_bytes = key_bytes;
2390 2390
2391 2391 out_key->ck_data = kmem_alloc(key_bytes, KM_SLEEP);
2392 2392
2393 2393 if (copyin((char *)STRUCT_FGETP(key, ck_data),
2394 2394 out_key->ck_data, key_bytes) != 0) {
2395 2395 kmem_free(out_key->ck_data, key_bytes);
2396 2396 out_key->ck_data = NULL;
2397 2397 out_key->ck_length = 0;
2398 2398 error = EFAULT;
2399 2399 goto out;
2400 2400 }
2401 2401 }
2402 2402 out_key->ck_length = (ulong_t)key_bits;
2403 2403 break;
2404 2404
2405 2405 case CRYPTO_KEY_ATTR_LIST:
2406 2406 count = STRUCT_FGET(key, ck_count);
2407 2407
2408 2408 if (copyin_attributes(mode, sp, count,
2409 2409 (caddr_t)STRUCT_FGETP(key, ck_attrs), &k_attrs, NULL, NULL,
2410 2410 &rv, &error, &rctl_bytes, out_rctl_chk, B_TRUE)) {
2411 2411 out_key->ck_count = count;
2412 2412 out_key->ck_attrs = k_attrs;
2413 2413 k_attrs = NULL;
2414 2414 } else {
2415 2415 out_key->ck_count = 0;
2416 2416 out_key->ck_attrs = NULL;
2417 2417 }
2418 2418 break;
2419 2419
2420 2420 case CRYPTO_KEY_REFERENCE:
2421 2421 out_key->ck_obj_id = STRUCT_FGET(key, ck_obj_id);
2422 2422 break;
2423 2423
2424 2424 default:
2425 2425 rv = CRYPTO_ARGUMENTS_BAD;
2426 2426 }
2427 2427
2428 2428 out:
2429 2429 *out_rctl_bytes = rctl_bytes;
2430 2430 *out_rv = rv;
2431 2431 *out_error = error;
2432 2432 return ((rv | error) ? B_FALSE : B_TRUE);
2433 2433 }
2434 2434
2435 2435 /*
2436 2436 * This routine does two things:
2437 2437 * 1. Given a crypto_minor structure and a session ID, it returns
2438 2438 * a valid session pointer.
2439 2439 * 2. It checks that the provider, to which the session has been opened,
2440 2440 * has not been removed.
2441 2441 */
2442 2442 static boolean_t
2443 2443 get_session_ptr(crypto_session_id_t i, crypto_minor_t *cm,
2444 2444 crypto_session_data_t **session_ptr, int *out_error, int *out_rv)
2445 2445 {
2446 2446 crypto_session_data_t *sp = NULL;
2447 2447 int rv = CRYPTO_SESSION_HANDLE_INVALID;
2448 2448 int error = 0;
2449 2449
2450 2450 mutex_enter(&cm->cm_lock);
2451 2451 if ((i < cm->cm_session_table_count) &&
2452 2452 (cm->cm_session_table[i] != NULL)) {
2453 2453 sp = cm->cm_session_table[i];
2454 2454 mutex_enter(&sp->sd_lock);
2455 2455 mutex_exit(&cm->cm_lock);
2456 2456 while (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
2457 2457 if (cv_wait_sig(&sp->sd_cv, &sp->sd_lock) == 0) {
2458 2458 mutex_exit(&sp->sd_lock);
2459 2459 sp = NULL;
2460 2460 error = EINTR;
2461 2461 goto out;
2462 2462 }
2463 2463 }
2464 2464
2465 2465 if (sp->sd_flags & CRYPTO_SESSION_IS_CLOSED) {
2466 2466 mutex_exit(&sp->sd_lock);
2467 2467 sp = NULL;
2468 2468 goto out;
2469 2469 }
2470 2470
2471 2471 if (KCF_IS_PROV_REMOVED(sp->sd_provider)) {
2472 2472 mutex_exit(&sp->sd_lock);
2473 2473 sp = NULL;
2474 2474 rv = CRYPTO_DEVICE_ERROR;
2475 2475 goto out;
2476 2476 }
2477 2477
2478 2478 rv = CRYPTO_SUCCESS;
2479 2479 sp->sd_flags |= CRYPTO_SESSION_IS_BUSY;
2480 2480 mutex_exit(&sp->sd_lock);
2481 2481 } else {
2482 2482 mutex_exit(&cm->cm_lock);
2483 2483 }
2484 2484 out:
2485 2485 *session_ptr = sp;
2486 2486 *out_error = error;
2487 2487 *out_rv = rv;
2488 2488 return ((rv == CRYPTO_SUCCESS && error == 0) ? B_TRUE : B_FALSE);
2489 2489 }
2490 2490
2491 2491 #define CRYPTO_SESSION_RELE(s) if ((s) != NULL) { \
2492 2492 mutex_enter(&((s)->sd_lock)); \
2493 2493 (s)->sd_flags &= ~CRYPTO_SESSION_IS_BUSY; \
2494 2494 cv_broadcast(&(s)->sd_cv); \
2495 2495 mutex_exit(&((s)->sd_lock)); \
2496 2496 }
2497 2497
2498 2498 /* ARGSUSED */
2499 2499 static int
2500 2500 encrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
2501 2501 {
2502 2502 return (cipher_init(dev, arg, mode, crypto_encrypt_init_prov));
2503 2503 }
2504 2504
2505 2505 /* ARGSUSED */
2506 2506 static int
2507 2507 decrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
2508 2508 {
2509 2509 return (cipher_init(dev, arg, mode, crypto_decrypt_init_prov));
2510 2510 }
2511 2511
2512 2512 /*
2513 2513 * umech is a mechanism structure that has been copied from user address
2514 2514 * space into kernel address space. Only one copyin has been done.
2515 2515 * The mechanism parameter, if non-null, still points to user address space.
2516 2516 * If the mechanism parameter contains pointers, they are pointers into
2517 2517 * user address space.
2518 2518 *
2519 2519 * kmech is a umech with all pointers and structures in kernel address space.
2520 2520 *
2521 2521 * This routine calls the provider's entry point to copy a umech parameter
2522 2522 * into kernel address space. Kernel memory is allocated by the provider.
2523 2523 */
2524 2524 static int
2525 2525 crypto_provider_copyin_mech_param(kcf_provider_desc_t *pd,
2526 2526 crypto_mechanism_t *umech, crypto_mechanism_t *kmech, int mode, int *error)
2527 2527 {
2528 2528 crypto_mech_type_t provider_mech_type;
2529 2529 int rv;
2530 2530
2531 2531 /* get the provider's mech number */
2532 2532 provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2533 2533
2534 2534 kmech->cm_param = NULL;
2535 2535 kmech->cm_param_len = 0;
2536 2536 kmech->cm_type = provider_mech_type;
2537 2537 rv = KCF_PROV_COPYIN_MECH(pd, umech, kmech, error, mode);
2538 2538 kmech->cm_type = umech->cm_type;
2539 2539
2540 2540 return (rv);
2541 2541 }
2542 2542
2543 2543 /*
2544 2544 * umech is a mechanism structure that has been copied from user address
2545 2545 * space into kernel address space. Only one copyin has been done.
2546 2546 * The mechanism parameter, if non-null, still points to user address space.
2547 2547 * If the mechanism parameter contains pointers, they are pointers into
2548 2548 * user address space.
2549 2549 *
2550 2550 * kmech is a umech with all pointers and structures in kernel address space.
2551 2551 *
2552 2552 * This routine calls the provider's entry point to copy a kmech parameter
2553 2553 * into user address space using umech as a template containing
2554 2554 * user address pointers.
2555 2555 */
2556 2556 static int
2557 2557 crypto_provider_copyout_mech_param(kcf_provider_desc_t *pd,
2558 2558 crypto_mechanism_t *kmech, crypto_mechanism_t *umech, int mode, int *error)
2559 2559 {
2560 2560 crypto_mech_type_t provider_mech_type;
2561 2561 int rv;
2562 2562
2563 2563 /* get the provider's mech number */
2564 2564 provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2565 2565
2566 2566 kmech->cm_type = provider_mech_type;
2567 2567 rv = KCF_PROV_COPYOUT_MECH(pd, kmech, umech, error, mode);
2568 2568 kmech->cm_type = umech->cm_type;
2569 2569
2570 2570 return (rv);
2571 2571 }
2572 2572
2573 2573 /*
2574 2574 * Call the provider's entry point to free kernel memory that has been
2575 2575 * allocated for the mechanism's parameter.
2576 2576 */
2577 2577 static void
2578 2578 crypto_free_mech(kcf_provider_desc_t *pd, boolean_t allocated_by_crypto_module,
2579 2579 crypto_mechanism_t *mech)
2580 2580 {
2581 2581 crypto_mech_type_t provider_mech_type;
2582 2582
2583 2583 if (allocated_by_crypto_module) {
2584 2584 if (mech->cm_param != NULL)
2585 2585 kmem_free(mech->cm_param, mech->cm_param_len);
2586 2586 } else {
2587 2587 /* get the provider's mech number */
2588 2588 provider_mech_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
2589 2589
2590 2590 if (mech->cm_param != NULL && mech->cm_param_len != 0) {
2591 2591 mech->cm_type = provider_mech_type;
2592 2592 (void) KCF_PROV_FREE_MECH(pd, mech);
2593 2593 }
2594 2594 }
2595 2595 }
2596 2596
2597 2597 /*
2598 2598 * ASSUMPTION: crypto_encrypt_init and crypto_decrypt_init
2599 2599 * structures are identical except for field names.
2600 2600 */
2601 2601 static int
2602 2602 cipher_init(dev_t dev, caddr_t arg, int mode, int (*init)(crypto_provider_t,
2603 2603 crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
2604 2604 crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *))
2605 2605 {
2606 2606 STRUCT_DECL(crypto_encrypt_init, encrypt_init);
2607 2607 kcf_provider_desc_t *real_provider = NULL;
2608 2608 crypto_session_id_t session_id;
2609 2609 crypto_mechanism_t mech;
2610 2610 crypto_key_t key;
2611 2611 crypto_minor_t *cm;
2612 2612 crypto_session_data_t *sp = NULL;
2613 2613 crypto_context_t cc;
2614 2614 crypto_ctx_t **ctxpp;
2615 2615 size_t mech_rctl_bytes = 0;
2616 2616 boolean_t mech_rctl_chk = B_FALSE;
2617 2617 size_t key_rctl_bytes = 0;
2618 2618 boolean_t key_rctl_chk = B_FALSE;
2619 2619 int error = 0;
2620 2620 int rv;
2621 2621 boolean_t allocated_by_crypto_module = B_FALSE;
2622 2622 crypto_func_group_t fg;
2623 2623
2624 2624 STRUCT_INIT(encrypt_init, mode);
2625 2625
2626 2626 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
2627 2627 cmn_err(CE_WARN, "cipher_init: failed holding minor");
2628 2628 return (ENXIO);
2629 2629 }
2630 2630
2631 2631 if (copyin(arg, STRUCT_BUF(encrypt_init),
2632 2632 STRUCT_SIZE(encrypt_init)) != 0) {
2633 2633 crypto_release_minor(cm);
2634 2634 return (EFAULT);
2635 2635 }
2636 2636
2637 2637 mech.cm_param = NULL;
2638 2638 bzero(&key, sizeof (crypto_key_t));
2639 2639
2640 2640 session_id = STRUCT_FGET(encrypt_init, ei_session);
2641 2641
2642 2642 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
2643 2643 goto out;
2644 2644 }
2645 2645
2646 2646 bcopy(STRUCT_FADDR(encrypt_init, ei_mech), &mech.cm_type,
2647 2647 sizeof (crypto_mech_type_t));
2648 2648
2649 2649 if (init == crypto_encrypt_init_prov) {
2650 2650 fg = CRYPTO_FG_ENCRYPT;
2651 2651 } else {
2652 2652 fg = CRYPTO_FG_DECRYPT;
2653 2653 }
2654 2654
2655 2655 /* We need the key length for provider selection so copy it in now. */
2656 2656 if (!copyin_key(mode, sp, STRUCT_FADDR(encrypt_init, ei_key), &key,
2657 2657 &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
2658 2658 goto out;
2659 2659 }
2660 2660
2661 2661 if ((rv = kcf_get_hardware_provider(mech.cm_type, &key,
2662 2662 CRYPTO_MECH_INVALID, NULL, sp->sd_provider, &real_provider, fg))
2663 2663 != CRYPTO_SUCCESS) {
2664 2664 goto out;
2665 2665 }
2666 2666
2667 2667 rv = crypto_provider_copyin_mech_param(real_provider,
2668 2668 STRUCT_FADDR(encrypt_init, ei_mech), &mech, mode, &error);
2669 2669
2670 2670 if (rv == CRYPTO_NOT_SUPPORTED) {
2671 2671 allocated_by_crypto_module = B_TRUE;
2672 2672 if (!copyin_mech(mode, sp, STRUCT_FADDR(encrypt_init, ei_mech),
2673 2673 &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
2674 2674 goto out;
2675 2675 }
2676 2676 } else {
2677 2677 if (rv != CRYPTO_SUCCESS)
2678 2678 goto out;
2679 2679 }
2680 2680
2681 2681 rv = (init)(real_provider, sp->sd_provider_session->ps_session,
2682 2682 &mech, &key, NULL, &cc, NULL);
2683 2683
2684 2684 /*
2685 2685 * Check if a context already exists. If so, it means it is being
2686 2686 * abandoned. So, cancel it to avoid leaking it.
2687 2687 */
2688 2688 ctxpp = (init == crypto_encrypt_init_prov) ?
2689 2689 &sp->sd_encr_ctx : &sp->sd_decr_ctx;
2690 2690
2691 2691 if (*ctxpp != NULL)
2692 2692 CRYPTO_CANCEL_CTX(ctxpp);
2693 2693 *ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
2694 2694
2695 2695 out:
2696 2696 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
2697 2697 CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
2698 2698 CRYPTO_SESSION_RELE(sp);
2699 2699 crypto_release_minor(cm);
2700 2700
2701 2701 if (real_provider != NULL) {
2702 2702 crypto_free_mech(real_provider,
2703 2703 allocated_by_crypto_module, &mech);
2704 2704 KCF_PROV_REFRELE(real_provider);
2705 2705 }
2706 2706
2707 2707 free_crypto_key(&key);
2708 2708
2709 2709 if (error != 0)
2710 2710 /* XXX free context */
2711 2711 return (error);
2712 2712
2713 2713 STRUCT_FSET(encrypt_init, ei_return_value, rv);
2714 2714 if (copyout(STRUCT_BUF(encrypt_init), arg,
2715 2715 STRUCT_SIZE(encrypt_init)) != 0) {
2716 2716 /* XXX free context */
2717 2717 return (EFAULT);
2718 2718 }
2719 2719 return (0);
2720 2720 }
2721 2721
2722 2722 /* ARGSUSED */
2723 2723 static int
2724 2724 encrypt(dev_t dev, caddr_t arg, int mode, int *rval)
2725 2725 {
2726 2726 return (cipher(dev, arg, mode, crypto_encrypt_single));
2727 2727 }
2728 2728
2729 2729 /* ARGSUSED */
2730 2730 static int
2731 2731 decrypt(dev_t dev, caddr_t arg, int mode, int *rval)
2732 2732 {
2733 2733 return (cipher(dev, arg, mode, crypto_decrypt_single));
2734 2734 }
2735 2735
2736 2736 /*
2737 2737 * ASSUMPTION: crypto_encrypt and crypto_decrypt structures
2738 2738 * are identical except for field names.
2739 2739 */
2740 2740 static int
2741 2741 cipher(dev_t dev, caddr_t arg, int mode,
2742 2742 int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
2743 2743 crypto_call_req_t *))
2744 2744 {
2745 2745 STRUCT_DECL(crypto_encrypt, encrypt);
2746 2746 crypto_session_id_t session_id;
2747 2747 crypto_minor_t *cm;
2748 2748 crypto_session_data_t *sp = NULL;
2749 2749 crypto_ctx_t **ctxpp;
2750 2750 crypto_data_t data, encr;
2751 2751 size_t datalen, encrlen, need = 0;
2752 2752 boolean_t do_inplace;
2753 2753 char *encrbuf;
2754 2754 int error = 0;
2755 2755 int rv;
2756 2756 boolean_t rctl_chk = B_FALSE;
2757 2757
2758 2758 STRUCT_INIT(encrypt, mode);
2759 2759
2760 2760 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
2761 2761 cmn_err(CE_WARN, "cipher: failed holding minor");
2762 2762 return (ENXIO);
2763 2763 }
2764 2764
2765 2765 if (copyin(arg, STRUCT_BUF(encrypt), STRUCT_SIZE(encrypt)) != 0) {
2766 2766 crypto_release_minor(cm);
2767 2767 return (EFAULT);
2768 2768 }
2769 2769
2770 2770 data.cd_raw.iov_base = NULL;
2771 2771 encr.cd_raw.iov_base = NULL;
2772 2772
2773 2773 datalen = STRUCT_FGET(encrypt, ce_datalen);
2774 2774 encrlen = STRUCT_FGET(encrypt, ce_encrlen);
2775 2775
2776 2776 /*
2777 2777 * Don't allocate output buffer unless both buffer pointer and
2778 2778 * buffer length are not NULL or 0 (length).
2779 2779 */
2780 2780 encrbuf = STRUCT_FGETP(encrypt, ce_encrbuf);
2781 2781 if (encrbuf == NULL || encrlen == 0) {
2782 2782 encrlen = 0;
2783 2783 }
2784 2784
2785 2785 if (datalen > crypto_max_buffer_len ||
2786 2786 encrlen > crypto_max_buffer_len) {
2787 2787 cmn_err(CE_NOTE, "cipher: buffer greater than %ld bytes, "
2788 2788 "pid = %d", crypto_max_buffer_len, curproc->p_pid);
2789 2789 rv = CRYPTO_ARGUMENTS_BAD;
2790 2790 goto release_minor;
2791 2791 }
2792 2792
2793 2793 session_id = STRUCT_FGET(encrypt, ce_session);
2794 2794
2795 2795 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
2796 2796 goto release_minor;
2797 2797 }
2798 2798
2799 2799 do_inplace = (STRUCT_FGET(encrypt, ce_flags) &
2800 2800 CRYPTO_INPLACE_OPERATION) != 0;
2801 2801 need = do_inplace ? datalen : datalen + encrlen;
2802 2802
2803 2803 if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
2804 2804 CRYPTO_SUCCESS) {
2805 2805 need = 0;
2806 2806 goto release_minor;
2807 2807 }
2808 2808
2809 2809 INIT_RAW_CRYPTO_DATA(data, datalen);
2810 2810 data.cd_miscdata = NULL;
2811 2811
2812 2812 if (datalen != 0 && copyin(STRUCT_FGETP(encrypt, ce_databuf),
2813 2813 data.cd_raw.iov_base, datalen) != 0) {
2814 2814 error = EFAULT;
2815 2815 goto release_minor;
2816 2816 }
2817 2817
2818 2818 if (do_inplace) {
2819 2819 /* set out = in for in-place */
2820 2820 encr = data;
2821 2821 } else {
2822 2822 INIT_RAW_CRYPTO_DATA(encr, encrlen);
2823 2823 }
2824 2824
2825 2825 ctxpp = (single == crypto_encrypt_single) ?
2826 2826 &sp->sd_encr_ctx : &sp->sd_decr_ctx;
2827 2827
2828 2828 if (do_inplace)
2829 2829 /* specify in-place buffers with output = NULL */
2830 2830 rv = (single)(*ctxpp, &encr, NULL, NULL);
2831 2831 else
2832 2832 rv = (single)(*ctxpp, &data, &encr, NULL);
2833 2833
2834 2834 if (KCF_CONTEXT_DONE(rv))
2835 2835 *ctxpp = NULL;
2836 2836
2837 2837 if (rv == CRYPTO_SUCCESS) {
2838 2838 ASSERT(encr.cd_length <= encrlen);
2839 2839 if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
2840 2840 encrbuf, encr.cd_length) != 0) {
2841 2841 error = EFAULT;
2842 2842 goto release_minor;
2843 2843 }
2844 2844 STRUCT_FSET(encrypt, ce_encrlen,
2845 2845 (ulong_t)encr.cd_length);
2846 2846 }
2847 2847
2848 2848 if (rv == CRYPTO_BUFFER_TOO_SMALL) {
2849 2849 /*
2850 2850 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
2851 2851 * of section 11.2 of the pkcs11 spec. We catch it here and
2852 2852 * provide the correct pkcs11 return value.
2853 2853 */
2854 2854 if (STRUCT_FGETP(encrypt, ce_encrbuf) == NULL)
2855 2855 rv = CRYPTO_SUCCESS;
2856 2856 STRUCT_FSET(encrypt, ce_encrlen,
2857 2857 (ulong_t)encr.cd_length);
2858 2858 }
2859 2859
2860 2860 release_minor:
2861 2861 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
2862 2862 CRYPTO_SESSION_RELE(sp);
2863 2863 crypto_release_minor(cm);
2864 2864
2865 2865 if (data.cd_raw.iov_base != NULL)
2866 2866 kmem_free(data.cd_raw.iov_base, datalen);
2867 2867
2868 2868 if (!do_inplace && encr.cd_raw.iov_base != NULL)
2869 2869 kmem_free(encr.cd_raw.iov_base, encrlen);
2870 2870
2871 2871 if (error != 0)
2872 2872 return (error);
2873 2873
2874 2874 STRUCT_FSET(encrypt, ce_return_value, rv);
2875 2875 if (copyout(STRUCT_BUF(encrypt), arg, STRUCT_SIZE(encrypt)) != 0) {
2876 2876 return (EFAULT);
2877 2877 }
2878 2878 return (0);
2879 2879 }
2880 2880
2881 2881 /* ARGSUSED */
2882 2882 static int
2883 2883 encrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
2884 2884 {
2885 2885 return (cipher_update(dev, arg, mode, crypto_encrypt_update));
2886 2886 }
2887 2887
2888 2888 /* ARGSUSED */
2889 2889 static int
2890 2890 decrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
2891 2891 {
2892 2892 return (cipher_update(dev, arg, mode, crypto_decrypt_update));
2893 2893 }
2894 2894
2895 2895 /*
2896 2896 * ASSUMPTION: crypto_encrypt_update and crypto_decrypt_update
2897 2897 * structures are identical except for field names.
2898 2898 */
2899 2899 static int
2900 2900 cipher_update(dev_t dev, caddr_t arg, int mode,
2901 2901 int (*update)(crypto_context_t, crypto_data_t *, crypto_data_t *,
2902 2902 crypto_call_req_t *))
2903 2903 {
2904 2904 STRUCT_DECL(crypto_encrypt_update, encrypt_update);
2905 2905 crypto_session_id_t session_id;
2906 2906 crypto_minor_t *cm;
2907 2907 crypto_session_data_t *sp = NULL;
2908 2908 crypto_ctx_t **ctxpp;
2909 2909 crypto_data_t data, encr;
2910 2910 size_t datalen, encrlen, need = 0;
2911 2911 boolean_t do_inplace;
2912 2912 char *encrbuf;
2913 2913 int error = 0;
2914 2914 int rv;
2915 2915 boolean_t rctl_chk = B_FALSE;
2916 2916
2917 2917 STRUCT_INIT(encrypt_update, mode);
2918 2918
2919 2919 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
2920 2920 cmn_err(CE_WARN, "cipher_update: failed holding minor");
2921 2921 return (ENXIO);
2922 2922 }
2923 2923
2924 2924 if (copyin(arg, STRUCT_BUF(encrypt_update),
2925 2925 STRUCT_SIZE(encrypt_update)) != 0) {
2926 2926 crypto_release_minor(cm);
2927 2927 return (EFAULT);
2928 2928 }
2929 2929
2930 2930 data.cd_raw.iov_base = NULL;
2931 2931 encr.cd_raw.iov_base = NULL;
2932 2932
2933 2933 datalen = STRUCT_FGET(encrypt_update, eu_datalen);
2934 2934 encrlen = STRUCT_FGET(encrypt_update, eu_encrlen);
2935 2935
2936 2936 /*
2937 2937 * Don't allocate output buffer unless both buffer pointer and
2938 2938 * buffer length are not NULL or 0 (length).
2939 2939 */
2940 2940 encrbuf = STRUCT_FGETP(encrypt_update, eu_encrbuf);
2941 2941 if (encrbuf == NULL || encrlen == 0) {
2942 2942 encrlen = 0;
2943 2943 }
2944 2944
2945 2945 if (datalen > crypto_max_buffer_len ||
2946 2946 encrlen > crypto_max_buffer_len) {
2947 2947 cmn_err(CE_NOTE, "cipher_update: buffer greater than %ld "
2948 2948 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
2949 2949 rv = CRYPTO_ARGUMENTS_BAD;
2950 2950 goto out;
2951 2951 }
2952 2952
2953 2953 session_id = STRUCT_FGET(encrypt_update, eu_session);
2954 2954
2955 2955 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
2956 2956 goto out;
2957 2957 }
2958 2958
2959 2959 do_inplace = (STRUCT_FGET(encrypt_update, eu_flags) &
2960 2960 CRYPTO_INPLACE_OPERATION) != 0;
2961 2961 need = do_inplace ? datalen : datalen + encrlen;
2962 2962
2963 2963 if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
2964 2964 CRYPTO_SUCCESS) {
2965 2965 need = 0;
2966 2966 goto out;
2967 2967 }
2968 2968
2969 2969 INIT_RAW_CRYPTO_DATA(data, datalen);
2970 2970 data.cd_miscdata = NULL;
2971 2971
2972 2972 if (datalen != 0 && copyin(STRUCT_FGETP(encrypt_update, eu_databuf),
2973 2973 data.cd_raw.iov_base, datalen) != 0) {
2974 2974 error = EFAULT;
2975 2975 goto out;
2976 2976 }
2977 2977
2978 2978 if (do_inplace) {
2979 2979 /* specify in-place buffers with output = input */
2980 2980 encr = data;
2981 2981 } else {
2982 2982 INIT_RAW_CRYPTO_DATA(encr, encrlen);
2983 2983 }
2984 2984
2985 2985 ctxpp = (update == crypto_encrypt_update) ?
2986 2986 &sp->sd_encr_ctx : &sp->sd_decr_ctx;
2987 2987
2988 2988 if (do_inplace)
2989 2989 /* specify in-place buffers with output = NULL */
2990 2990 rv = (update)(*ctxpp, &encr, NULL, NULL);
2991 2991 else
2992 2992 rv = (update)(*ctxpp, &data, &encr, NULL);
2993 2993
2994 2994 if (rv == CRYPTO_SUCCESS || rv == CRYPTO_BUFFER_TOO_SMALL) {
2995 2995 if (rv == CRYPTO_SUCCESS) {
2996 2996 ASSERT(encr.cd_length <= encrlen);
2997 2997 if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
2998 2998 encrbuf, encr.cd_length) != 0) {
2999 2999 error = EFAULT;
3000 3000 goto out;
3001 3001 }
3002 3002 } else {
3003 3003 /*
3004 3004 * The providers return CRYPTO_BUFFER_TOO_SMALL even
3005 3005 * for case 1 of section 11.2 of the pkcs11 spec.
3006 3006 * We catch it here and provide the correct pkcs11
3007 3007 * return value.
3008 3008 */
3009 3009 if (STRUCT_FGETP(encrypt_update, eu_encrbuf) == NULL)
3010 3010 rv = CRYPTO_SUCCESS;
3011 3011 }
3012 3012 STRUCT_FSET(encrypt_update, eu_encrlen,
3013 3013 (ulong_t)encr.cd_length);
3014 3014 } else {
3015 3015 CRYPTO_CANCEL_CTX(ctxpp);
3016 3016 }
3017 3017 out:
3018 3018 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
3019 3019 CRYPTO_SESSION_RELE(sp);
3020 3020 crypto_release_minor(cm);
3021 3021
3022 3022 if (data.cd_raw.iov_base != NULL)
3023 3023 kmem_free(data.cd_raw.iov_base, datalen);
3024 3024
3025 3025 if (!do_inplace && (encr.cd_raw.iov_base != NULL))
3026 3026 kmem_free(encr.cd_raw.iov_base, encrlen);
3027 3027
3028 3028 if (error != 0)
3029 3029 return (error);
3030 3030
3031 3031 STRUCT_FSET(encrypt_update, eu_return_value, rv);
3032 3032 if (copyout(STRUCT_BUF(encrypt_update), arg,
3033 3033 STRUCT_SIZE(encrypt_update)) != 0) {
3034 3034 return (EFAULT);
3035 3035 }
3036 3036 return (0);
3037 3037 }
3038 3038
3039 3039 /* ARGSUSED */
3040 3040 static int
3041 3041 encrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
3042 3042 {
3043 3043 return (common_final(dev, arg, mode, crypto_encrypt_final));
3044 3044 }
3045 3045
3046 3046 /* ARGSUSED */
3047 3047 static int
3048 3048 decrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
3049 3049 {
3050 3050 return (common_final(dev, arg, mode, crypto_decrypt_final));
3051 3051 }
3052 3052
3053 3053 /*
3054 3054 * ASSUMPTION: crypto_encrypt_final, crypto_decrypt_final, crypto_sign_final,
3055 3055 * and crypto_digest_final structures are identical except for field names.
3056 3056 */
3057 3057 static int
3058 3058 common_final(dev_t dev, caddr_t arg, int mode,
3059 3059 int (*final)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
3060 3060 {
3061 3061 STRUCT_DECL(crypto_encrypt_final, encrypt_final);
3062 3062 crypto_session_id_t session_id;
3063 3063 crypto_minor_t *cm;
3064 3064 crypto_session_data_t *sp = NULL;
3065 3065 crypto_ctx_t **ctxpp;
3066 3066 crypto_data_t encr;
3067 3067 size_t encrlen, need = 0;
3068 3068 char *encrbuf;
3069 3069 int error = 0;
3070 3070 int rv;
3071 3071 boolean_t rctl_chk = B_FALSE;
3072 3072
3073 3073 STRUCT_INIT(encrypt_final, mode);
3074 3074
3075 3075 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
3076 3076 cmn_err(CE_WARN, "common_final: failed holding minor");
3077 3077 return (ENXIO);
3078 3078 }
3079 3079
3080 3080 if (copyin(arg, STRUCT_BUF(encrypt_final),
3081 3081 STRUCT_SIZE(encrypt_final)) != 0) {
3082 3082 crypto_release_minor(cm);
3083 3083 return (EFAULT);
3084 3084 }
3085 3085
3086 3086 encr.cd_format = CRYPTO_DATA_RAW;
3087 3087 encr.cd_raw.iov_base = NULL;
3088 3088
3089 3089 encrlen = STRUCT_FGET(encrypt_final, ef_encrlen);
3090 3090
3091 3091 /*
3092 3092 * Don't allocate output buffer unless both buffer pointer and
3093 3093 * buffer length are not NULL or 0 (length).
3094 3094 */
3095 3095 encrbuf = STRUCT_FGETP(encrypt_final, ef_encrbuf);
3096 3096 if (encrbuf == NULL || encrlen == 0) {
3097 3097 encrlen = 0;
3098 3098 }
3099 3099
3100 3100 if (encrlen > crypto_max_buffer_len) {
3101 3101 cmn_err(CE_NOTE, "common_final: buffer greater than %ld "
3102 3102 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
3103 3103 rv = CRYPTO_ARGUMENTS_BAD;
3104 3104 goto release_minor;
3105 3105 }
3106 3106
3107 3107 session_id = STRUCT_FGET(encrypt_final, ef_session);
3108 3108
3109 3109 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
3110 3110 goto release_minor;
3111 3111 }
3112 3112
3113 3113 if ((rv = CRYPTO_BUFFER_CHECK(sp, encrlen, rctl_chk)) !=
3114 3114 CRYPTO_SUCCESS) {
3115 3115 goto release_minor;
3116 3116 }
3117 3117 need = encrlen;
3118 3118 encr.cd_raw.iov_base = kmem_alloc(encrlen, KM_SLEEP);
3119 3119 encr.cd_raw.iov_len = encrlen;
3120 3120
3121 3121 encr.cd_offset = 0;
3122 3122 encr.cd_length = encrlen;
3123 3123
3124 3124 ASSERT(final == crypto_encrypt_final ||
3125 3125 final == crypto_decrypt_final || final == crypto_sign_final ||
3126 3126 final == crypto_digest_final);
3127 3127
3128 3128 if (final == crypto_encrypt_final) {
3129 3129 ctxpp = &sp->sd_encr_ctx;
3130 3130 } else if (final == crypto_decrypt_final) {
3131 3131 ctxpp = &sp->sd_decr_ctx;
3132 3132 } else if (final == crypto_sign_final) {
3133 3133 ctxpp = &sp->sd_sign_ctx;
3134 3134 } else {
3135 3135 ctxpp = &sp->sd_digest_ctx;
3136 3136 }
3137 3137
3138 3138 rv = (final)(*ctxpp, &encr, NULL);
3139 3139 if (KCF_CONTEXT_DONE(rv))
3140 3140 *ctxpp = NULL;
3141 3141
3142 3142 if (rv == CRYPTO_SUCCESS) {
3143 3143 ASSERT(encr.cd_length <= encrlen);
3144 3144 if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
3145 3145 encrbuf, encr.cd_length) != 0) {
3146 3146 error = EFAULT;
3147 3147 goto release_minor;
3148 3148 }
3149 3149 STRUCT_FSET(encrypt_final, ef_encrlen,
3150 3150 (ulong_t)encr.cd_length);
3151 3151 }
3152 3152
3153 3153 if (rv == CRYPTO_BUFFER_TOO_SMALL) {
3154 3154 /*
3155 3155 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
3156 3156 * of section 11.2 of the pkcs11 spec. We catch it here and
3157 3157 * provide the correct pkcs11 return value.
3158 3158 */
3159 3159 if (STRUCT_FGETP(encrypt_final, ef_encrbuf) == NULL)
3160 3160 rv = CRYPTO_SUCCESS;
3161 3161 STRUCT_FSET(encrypt_final, ef_encrlen,
3162 3162 (ulong_t)encr.cd_length);
3163 3163 }
3164 3164
3165 3165 release_minor:
3166 3166 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
3167 3167 CRYPTO_SESSION_RELE(sp);
3168 3168 crypto_release_minor(cm);
3169 3169
3170 3170 if (encr.cd_raw.iov_base != NULL)
3171 3171 kmem_free(encr.cd_raw.iov_base, encrlen);
3172 3172
3173 3173 if (error != 0)
3174 3174 return (error);
3175 3175
3176 3176 STRUCT_FSET(encrypt_final, ef_return_value, rv);
3177 3177 if (copyout(STRUCT_BUF(encrypt_final), arg,
3178 3178 STRUCT_SIZE(encrypt_final)) != 0) {
3179 3179 return (EFAULT);
3180 3180 }
3181 3181 return (0);
3182 3182 }
3183 3183
3184 3184 /* ARGSUSED */
3185 3185 static int
3186 3186 digest_init(dev_t dev, caddr_t arg, int mode, int *rval)
3187 3187 {
3188 3188 STRUCT_DECL(crypto_digest_init, digest_init);
3189 3189 kcf_provider_desc_t *real_provider = NULL;
3190 3190 crypto_session_id_t session_id;
3191 3191 crypto_mechanism_t mech;
3192 3192 crypto_minor_t *cm;
3193 3193 crypto_session_data_t *sp = NULL;
3194 3194 crypto_context_t cc;
3195 3195 size_t rctl_bytes = 0;
3196 3196 boolean_t rctl_chk = B_FALSE;
3197 3197 int error = 0;
3198 3198 int rv;
3199 3199
3200 3200 STRUCT_INIT(digest_init, mode);
3201 3201
3202 3202 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
3203 3203 cmn_err(CE_WARN, "digest_init: failed holding minor");
3204 3204 return (ENXIO);
3205 3205 }
3206 3206
3207 3207 if (copyin(arg, STRUCT_BUF(digest_init),
3208 3208 STRUCT_SIZE(digest_init)) != 0) {
3209 3209 crypto_release_minor(cm);
3210 3210 return (EFAULT);
3211 3211 }
3212 3212
3213 3213 mech.cm_param = NULL;
3214 3214
3215 3215 session_id = STRUCT_FGET(digest_init, di_session);
3216 3216
3217 3217 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
3218 3218 goto out;
3219 3219 }
3220 3220
3221 3221 if (!copyin_mech(mode, sp, STRUCT_FADDR(digest_init, di_mech), &mech,
3222 3222 &rctl_bytes, &rctl_chk, &rv, &error)) {
3223 3223 goto out;
3224 3224 }
3225 3225
3226 3226 if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
3227 3227 CRYPTO_MECH_INVALID, NULL, sp->sd_provider, &real_provider,
3228 3228 CRYPTO_FG_DIGEST)) != CRYPTO_SUCCESS) {
3229 3229 goto out;
3230 3230 }
3231 3231
3232 3232 rv = crypto_digest_init_prov(real_provider,
3233 3233 sp->sd_provider_session->ps_session, &mech, &cc, NULL);
3234 3234
3235 3235 /*
3236 3236 * Check if a context already exists. If so, it means it is being
3237 3237 * abandoned. So, cancel it to avoid leaking it.
3238 3238 */
3239 3239 if (sp->sd_digest_ctx != NULL)
3240 3240 CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
3241 3241 sp->sd_digest_ctx = (rv == CRYPTO_SUCCESS) ? cc : NULL;
3242 3242 out:
3243 3243 CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
3244 3244 CRYPTO_SESSION_RELE(sp);
3245 3245 crypto_release_minor(cm);
3246 3246
3247 3247 if (real_provider != NULL)
3248 3248 KCF_PROV_REFRELE(real_provider);
3249 3249
3250 3250 if (mech.cm_param != NULL)
3251 3251 kmem_free(mech.cm_param, mech.cm_param_len);
3252 3252
3253 3253 if (error != 0)
3254 3254 return (error);
3255 3255
3256 3256 STRUCT_FSET(digest_init, di_return_value, rv);
3257 3257 if (copyout(STRUCT_BUF(digest_init), arg,
3258 3258 STRUCT_SIZE(digest_init)) != 0) {
3259 3259 return (EFAULT);
3260 3260 }
3261 3261 return (0);
3262 3262 }
3263 3263
3264 3264 /* ARGSUSED */
3265 3265 static int
3266 3266 digest_update(dev_t dev, caddr_t arg, int mode, int *rval)
3267 3267 {
3268 3268 STRUCT_DECL(crypto_digest_update, digest_update);
3269 3269 crypto_session_id_t session_id;
3270 3270 crypto_minor_t *cm;
3271 3271 crypto_session_data_t *sp = NULL;
3272 3272 crypto_data_t data;
3273 3273 size_t datalen, need = 0;
3274 3274 int error = 0;
3275 3275 int rv;
3276 3276 boolean_t rctl_chk = B_FALSE;
3277 3277
3278 3278 STRUCT_INIT(digest_update, mode);
3279 3279
3280 3280 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
3281 3281 cmn_err(CE_WARN, "digest_update: failed holding minor");
3282 3282 return (ENXIO);
3283 3283 }
3284 3284
3285 3285 if (copyin(arg, STRUCT_BUF(digest_update),
3286 3286 STRUCT_SIZE(digest_update)) != 0) {
3287 3287 crypto_release_minor(cm);
3288 3288 return (EFAULT);
3289 3289 }
3290 3290
3291 3291 data.cd_format = CRYPTO_DATA_RAW;
3292 3292 data.cd_raw.iov_base = NULL;
3293 3293
3294 3294 datalen = STRUCT_FGET(digest_update, du_datalen);
3295 3295 if (datalen > crypto_max_buffer_len) {
3296 3296 cmn_err(CE_NOTE, "digest_update: buffer greater than %ld "
3297 3297 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
3298 3298 rv = CRYPTO_ARGUMENTS_BAD;
3299 3299 goto release_minor;
3300 3300 }
3301 3301
3302 3302 session_id = STRUCT_FGET(digest_update, du_session);
3303 3303
3304 3304 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
3305 3305 goto release_minor;
3306 3306 }
3307 3307
3308 3308 if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
3309 3309 CRYPTO_SUCCESS) {
3310 3310 goto release_minor;
3311 3311 }
3312 3312
3313 3313 need = datalen;
3314 3314 data.cd_raw.iov_base = kmem_alloc(datalen, KM_SLEEP);
3315 3315 data.cd_raw.iov_len = datalen;
3316 3316
3317 3317 if (datalen != 0 && copyin(STRUCT_FGETP(digest_update, du_databuf),
3318 3318 data.cd_raw.iov_base, datalen) != 0) {
3319 3319 error = EFAULT;
3320 3320 goto release_minor;
3321 3321 }
3322 3322
3323 3323 data.cd_offset = 0;
3324 3324 data.cd_length = datalen;
3325 3325
3326 3326 rv = crypto_digest_update(sp->sd_digest_ctx, &data, NULL);
3327 3327 if (rv != CRYPTO_SUCCESS)
3328 3328 CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
3329 3329
3330 3330 release_minor:
3331 3331 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
3332 3332 CRYPTO_SESSION_RELE(sp);
3333 3333 crypto_release_minor(cm);
3334 3334
3335 3335 if (data.cd_raw.iov_base != NULL)
3336 3336 kmem_free(data.cd_raw.iov_base, datalen);
3337 3337
3338 3338 if (error != 0)
3339 3339 return (error);
3340 3340
3341 3341 STRUCT_FSET(digest_update, du_return_value, rv);
3342 3342 if (copyout(STRUCT_BUF(digest_update), arg,
3343 3343 STRUCT_SIZE(digest_update)) != 0) {
3344 3344 return (EFAULT);
3345 3345 }
3346 3346 return (0);
3347 3347 }
3348 3348
3349 3349 /* ARGSUSED */
3350 3350 static int
3351 3351 digest_key(dev_t dev, caddr_t arg, int mode, int *rval)
3352 3352 {
3353 3353 STRUCT_DECL(crypto_digest_key, digest_key);
3354 3354 crypto_session_id_t session_id;
3355 3355 crypto_key_t key;
3356 3356 crypto_minor_t *cm;
3357 3357 crypto_session_data_t *sp = NULL;
3358 3358 size_t rctl_bytes = 0;
3359 3359 boolean_t key_rctl_chk = B_FALSE;
3360 3360 int error = 0;
3361 3361 int rv;
3362 3362
3363 3363 STRUCT_INIT(digest_key, mode);
3364 3364
3365 3365 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
3366 3366 cmn_err(CE_WARN, "digest_key: failed holding minor");
3367 3367 return (ENXIO);
3368 3368 }
3369 3369
3370 3370 if (copyin(arg, STRUCT_BUF(digest_key), STRUCT_SIZE(digest_key)) != 0) {
3371 3371 crypto_release_minor(cm);
3372 3372 return (EFAULT);
3373 3373 }
3374 3374
3375 3375 bzero(&key, sizeof (crypto_key_t));
3376 3376
3377 3377 session_id = STRUCT_FGET(digest_key, dk_session);
3378 3378
3379 3379 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
3380 3380 goto out;
3381 3381 }
3382 3382
3383 3383 if (!copyin_key(mode, sp, STRUCT_FADDR(digest_key, dk_key), &key,
3384 3384 &rctl_bytes, &key_rctl_chk, &rv, &error)) {
3385 3385 goto out;
3386 3386 }
3387 3387
3388 3388 rv = crypto_digest_key_prov(sp->sd_digest_ctx, &key, NULL);
3389 3389 if (rv != CRYPTO_SUCCESS)
3390 3390 CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
3391 3391 out:
3392 3392 CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, key_rctl_chk);
3393 3393 CRYPTO_SESSION_RELE(sp);
3394 3394 crypto_release_minor(cm);
3395 3395
3396 3396 free_crypto_key(&key);
3397 3397
3398 3398 if (error != 0)
3399 3399 return (error);
3400 3400
3401 3401 STRUCT_FSET(digest_key, dk_return_value, rv);
3402 3402 if (copyout(STRUCT_BUF(digest_key), arg,
3403 3403 STRUCT_SIZE(digest_key)) != 0) {
3404 3404 return (EFAULT);
3405 3405 }
3406 3406 return (0);
3407 3407 }
3408 3408
3409 3409 /* ARGSUSED */
3410 3410 static int
3411 3411 digest_final(dev_t dev, caddr_t arg, int mode, int *rval)
3412 3412 {
3413 3413 return (common_final(dev, arg, mode, crypto_digest_final));
3414 3414 }
3415 3415
3416 3416 /* ARGSUSED */
3417 3417 static int
3418 3418 digest(dev_t dev, caddr_t arg, int mode, int *rval)
3419 3419 {
3420 3420 return (common_digest(dev, arg, mode, crypto_digest_single));
3421 3421 }
3422 3422
3423 3423 /*
3424 3424 * ASSUMPTION: crypto_digest, crypto_sign, crypto_sign_recover,
3425 3425 * and crypto_verify_recover are identical except for field names.
3426 3426 */
3427 3427 static int
3428 3428 common_digest(dev_t dev, caddr_t arg, int mode,
3429 3429 int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
3430 3430 crypto_call_req_t *))
3431 3431 {
3432 3432 STRUCT_DECL(crypto_digest, crypto_digest);
3433 3433 crypto_session_id_t session_id;
3434 3434 crypto_minor_t *cm;
3435 3435 crypto_session_data_t *sp = NULL;
3436 3436 crypto_data_t data, digest;
3437 3437 crypto_ctx_t **ctxpp;
3438 3438 size_t datalen, digestlen, need = 0;
3439 3439 char *digestbuf;
3440 3440 int error = 0;
3441 3441 int rv;
3442 3442 boolean_t rctl_chk = B_FALSE;
3443 3443
3444 3444 STRUCT_INIT(crypto_digest, mode);
3445 3445
3446 3446 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
3447 3447 cmn_err(CE_WARN, "common_digest: failed holding minor");
3448 3448 return (ENXIO);
3449 3449 }
3450 3450
3451 3451 if (copyin(arg, STRUCT_BUF(crypto_digest),
3452 3452 STRUCT_SIZE(crypto_digest)) != 0) {
3453 3453 crypto_release_minor(cm);
3454 3454 return (EFAULT);
3455 3455 }
3456 3456
3457 3457 data.cd_raw.iov_base = NULL;
3458 3458 digest.cd_raw.iov_base = NULL;
3459 3459
3460 3460 datalen = STRUCT_FGET(crypto_digest, cd_datalen);
3461 3461 digestlen = STRUCT_FGET(crypto_digest, cd_digestlen);
3462 3462
3463 3463 /*
3464 3464 * Don't allocate output buffer unless both buffer pointer and
3465 3465 * buffer length are not NULL or 0 (length).
3466 3466 */
3467 3467 digestbuf = STRUCT_FGETP(crypto_digest, cd_digestbuf);
3468 3468 if (digestbuf == NULL || digestlen == 0) {
3469 3469 digestlen = 0;
3470 3470 }
3471 3471
3472 3472 if (datalen > crypto_max_buffer_len ||
3473 3473 digestlen > crypto_max_buffer_len) {
3474 3474 cmn_err(CE_NOTE, "common_digest: buffer greater than %ld "
3475 3475 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
3476 3476 rv = CRYPTO_ARGUMENTS_BAD;
3477 3477 goto release_minor;
3478 3478 }
3479 3479
3480 3480 session_id = STRUCT_FGET(crypto_digest, cd_session);
3481 3481
3482 3482 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
3483 3483 goto release_minor;
3484 3484 }
3485 3485
3486 3486 need = datalen + digestlen;
3487 3487 if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
3488 3488 CRYPTO_SUCCESS) {
3489 3489 need = 0;
3490 3490 goto release_minor;
3491 3491 }
3492 3492
3493 3493 INIT_RAW_CRYPTO_DATA(data, datalen);
3494 3494
3495 3495 if (datalen != 0 && copyin(STRUCT_FGETP(crypto_digest, cd_databuf),
3496 3496 data.cd_raw.iov_base, datalen) != 0) {
3497 3497 error = EFAULT;
3498 3498 goto release_minor;
3499 3499 }
3500 3500
3501 3501 INIT_RAW_CRYPTO_DATA(digest, digestlen);
3502 3502
3503 3503 ASSERT(single == crypto_digest_single ||
3504 3504 single == crypto_sign_single ||
3505 3505 single == crypto_verify_recover_single ||
3506 3506 single == crypto_sign_recover_single);
3507 3507
3508 3508 if (single == crypto_digest_single) {
3509 3509 ctxpp = &sp->sd_digest_ctx;
3510 3510 } else if (single == crypto_sign_single) {
3511 3511 ctxpp = &sp->sd_sign_ctx;
3512 3512 } else if (single == crypto_verify_recover_single) {
3513 3513 ctxpp = &sp->sd_verify_recover_ctx;
3514 3514 } else {
3515 3515 ctxpp = &sp->sd_sign_recover_ctx;
3516 3516 }
3517 3517 rv = (single)(*ctxpp, &data, &digest, NULL);
3518 3518 if (KCF_CONTEXT_DONE(rv))
3519 3519 *ctxpp = NULL;
3520 3520
3521 3521 if (rv == CRYPTO_SUCCESS) {
3522 3522 ASSERT(digest.cd_length <= digestlen);
3523 3523 if (digest.cd_length != 0 && copyout(digest.cd_raw.iov_base,
3524 3524 digestbuf, digest.cd_length) != 0) {
3525 3525 error = EFAULT;
3526 3526 goto release_minor;
3527 3527 }
3528 3528 STRUCT_FSET(crypto_digest, cd_digestlen,
3529 3529 (ulong_t)digest.cd_length);
3530 3530 }
3531 3531
3532 3532 if (rv == CRYPTO_BUFFER_TOO_SMALL) {
3533 3533 /*
3534 3534 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
3535 3535 * of section 11.2 of the pkcs11 spec. We catch it here and
3536 3536 * provide the correct pkcs11 return value.
3537 3537 */
3538 3538 if (STRUCT_FGETP(crypto_digest, cd_digestbuf) == NULL)
3539 3539 rv = CRYPTO_SUCCESS;
3540 3540 STRUCT_FSET(crypto_digest, cd_digestlen,
3541 3541 (ulong_t)digest.cd_length);
3542 3542 }
3543 3543
3544 3544 release_minor:
3545 3545 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
3546 3546 CRYPTO_SESSION_RELE(sp);
3547 3547 crypto_release_minor(cm);
3548 3548
3549 3549 if (data.cd_raw.iov_base != NULL)
3550 3550 kmem_free(data.cd_raw.iov_base, datalen);
3551 3551
3552 3552 if (digest.cd_raw.iov_base != NULL)
3553 3553 kmem_free(digest.cd_raw.iov_base, digestlen);
3554 3554
3555 3555 if (error != 0)
3556 3556 return (error);
3557 3557
3558 3558 STRUCT_FSET(crypto_digest, cd_return_value, rv);
3559 3559 if (copyout(STRUCT_BUF(crypto_digest), arg,
3560 3560 STRUCT_SIZE(crypto_digest)) != 0) {
3561 3561 return (EFAULT);
3562 3562 }
3563 3563 return (0);
3564 3564 }
3565 3565
3566 3566 /*
3567 3567 * A helper function that does what the name suggests.
3568 3568 * Returns 0 on success and non-zero otherwise.
3569 3569 * On failure, out_pin is set to 0.
3570 3570 */
3571 3571 int
3572 3572 get_pin_and_session_ptr(char *in_pin, char **out_pin, size_t pin_len,
3573 3573 crypto_minor_t *cm, crypto_session_id_t sid, crypto_session_data_t **sp,
3574 3574 int *rv, int *error)
3575 3575 {
3576 3576 char *tmp_pin = NULL;
3577 3577 int tmp_error = 0, tmp_rv = 0;
3578 3578
3579 3579 if (pin_len > KCF_MAX_PIN_LEN) {
3580 3580 tmp_rv = CRYPTO_PIN_LEN_RANGE;
3581 3581 goto out;
3582 3582 }
3583 3583 tmp_pin = kmem_alloc(pin_len, KM_SLEEP);
3584 3584
3585 3585 if (pin_len != 0 && copyin(in_pin, tmp_pin, pin_len) != 0) {
3586 3586 tmp_error = EFAULT;
3587 3587 goto out;
3588 3588 }
3589 3589
3590 3590 (void) get_session_ptr(sid, cm, sp, &tmp_error, &tmp_rv);
3591 3591 out:
3592 3592 *out_pin = tmp_pin;
3593 3593 *rv = tmp_rv;
3594 3594 *error = tmp_error;
3595 3595 return (tmp_rv | tmp_error);
3596 3596 }
3597 3597
3598 3598 /* ARGSUSED */
3599 3599 static int
3600 3600 set_pin(dev_t dev, caddr_t arg, int mode, int *rval)
3601 3601 {
3602 3602 STRUCT_DECL(crypto_set_pin, set_pin);
3603 3603 kcf_provider_desc_t *real_provider;
3604 3604 kcf_req_params_t params;
3605 3605 crypto_minor_t *cm;
3606 3606 crypto_session_data_t *sp;
3607 3607 char *old_pin = NULL;
3608 3608 char *new_pin = NULL;
3609 3609 size_t old_pin_len;
3610 3610 size_t new_pin_len;
3611 3611 int error = 0;
3612 3612 int rv;
3613 3613
3614 3614 STRUCT_INIT(set_pin, mode);
3615 3615
3616 3616 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
3617 3617 cmn_err(CE_WARN, "set_pin: failed holding minor");
3618 3618 return (ENXIO);
3619 3619 }
3620 3620
3621 3621 if (copyin(arg, STRUCT_BUF(set_pin),
3622 3622 STRUCT_SIZE(set_pin)) != 0) {
3623 3623 crypto_release_minor(cm);
3624 3624 return (EFAULT);
3625 3625 }
3626 3626
3627 3627 old_pin_len = STRUCT_FGET(set_pin, sp_old_len);
3628 3628
3629 3629 if (get_pin_and_session_ptr(STRUCT_FGETP(set_pin, sp_old_pin),
3630 3630 &old_pin, old_pin_len, cm, STRUCT_FGET(set_pin, sp_session),
3631 3631 &sp, &rv, &error) != 0)
3632 3632 goto release_minor;
3633 3633
3634 3634 new_pin_len = STRUCT_FGET(set_pin, sp_new_len);
3635 3635 if (new_pin_len > KCF_MAX_PIN_LEN) {
3636 3636 rv = CRYPTO_PIN_LEN_RANGE;
3637 3637 goto out;
3638 3638 }
3639 3639 new_pin = kmem_alloc(new_pin_len, KM_SLEEP);
3640 3640
3641 3641 if (new_pin_len != 0 && copyin(STRUCT_FGETP(set_pin, sp_new_pin),
3642 3642 new_pin, new_pin_len) != 0) {
3643 3643 error = EFAULT;
3644 3644 goto out;
3645 3645 }
3646 3646
3647 3647 if ((rv = kcf_get_hardware_provider_nomech(
3648 3648 CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(set_pin),
3649 3649 sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
3650 3650 goto out;
3651 3651 }
3652 3652
3653 3653 KCF_WRAP_PROVMGMT_OPS_PARAMS(¶ms, KCF_OP_MGMT_SETPIN,
3654 3654 sp->sd_provider_session->ps_session, old_pin, old_pin_len,
3655 3655 new_pin, new_pin_len, NULL, NULL, real_provider);
3656 3656
3657 3657 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
3658 3658 KCF_PROV_REFRELE(real_provider);
3659 3659
3660 3660 out:
3661 3661 CRYPTO_SESSION_RELE(sp);
3662 3662
3663 3663 release_minor:
3664 3664 crypto_release_minor(cm);
3665 3665
3666 3666 if (old_pin != NULL) {
3667 3667 bzero(old_pin, old_pin_len);
3668 3668 kmem_free(old_pin, old_pin_len);
3669 3669 }
3670 3670
3671 3671 if (new_pin != NULL) {
3672 3672 bzero(new_pin, new_pin_len);
3673 3673 kmem_free(new_pin, new_pin_len);
3674 3674 }
3675 3675
3676 3676 if (error != 0)
3677 3677 return (error);
3678 3678
3679 3679 STRUCT_FSET(set_pin, sp_return_value, rv);
3680 3680 if (copyout(STRUCT_BUF(set_pin), arg, STRUCT_SIZE(set_pin)) != 0) {
3681 3681 return (EFAULT);
3682 3682 }
3683 3683 return (0);
3684 3684 }
3685 3685
3686 3686 /* ARGSUSED */
3687 3687 static int
3688 3688 login(dev_t dev, caddr_t arg, int mode, int *rval)
3689 3689 {
3690 3690 STRUCT_DECL(crypto_login, login);
3691 3691 kcf_provider_desc_t *real_provider;
3692 3692 kcf_req_params_t params;
3693 3693 crypto_minor_t *cm;
3694 3694 crypto_session_data_t *sp;
3695 3695 size_t pin_len;
3696 3696 char *pin;
3697 3697 uint_t user_type;
3698 3698 int error = 0;
3699 3699 int rv;
3700 3700
3701 3701 STRUCT_INIT(login, mode);
3702 3702
3703 3703 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
3704 3704 cmn_err(CE_WARN, "login: failed holding minor");
3705 3705 return (ENXIO);
3706 3706 }
3707 3707
3708 3708 if (copyin(arg, STRUCT_BUF(login), STRUCT_SIZE(login)) != 0) {
3709 3709 crypto_release_minor(cm);
3710 3710 return (EFAULT);
3711 3711 }
3712 3712
3713 3713 user_type = STRUCT_FGET(login, co_user_type);
3714 3714
3715 3715 pin_len = STRUCT_FGET(login, co_pin_len);
3716 3716
3717 3717 if (get_pin_and_session_ptr(STRUCT_FGETP(login, co_pin),
3718 3718 &pin, pin_len, cm, STRUCT_FGET(login, co_session),
3719 3719 &sp, &rv, &error) != 0) {
3720 3720 if (rv == CRYPTO_PIN_LEN_RANGE)
3721 3721 rv = CRYPTO_PIN_INCORRECT;
3722 3722 goto release_minor;
3723 3723 }
3724 3724
3725 3725 if ((rv = kcf_get_hardware_provider_nomech(
3726 3726 CRYPTO_OPS_OFFSET(session_ops),
3727 3727 CRYPTO_SESSION_OFFSET(session_login), sp->sd_provider,
3728 3728 &real_provider)) != CRYPTO_SUCCESS) {
3729 3729 goto out;
3730 3730 }
3731 3731
3732 3732 KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_LOGIN, NULL,
3733 3733 sp->sd_provider_session->ps_session, user_type, pin, pin_len,
3734 3734 real_provider);
3735 3735
3736 3736 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
3737 3737 KCF_PROV_REFRELE(real_provider);
3738 3738
3739 3739 out:
3740 3740 CRYPTO_SESSION_RELE(sp);
3741 3741
3742 3742 release_minor:
3743 3743 crypto_release_minor(cm);
3744 3744
3745 3745 if (pin != NULL) {
3746 3746 bzero(pin, pin_len);
3747 3747 kmem_free(pin, pin_len);
3748 3748 }
3749 3749
3750 3750 if (error != 0)
3751 3751 return (error);
3752 3752
3753 3753 STRUCT_FSET(login, co_return_value, rv);
3754 3754 if (copyout(STRUCT_BUF(login), arg, STRUCT_SIZE(login)) != 0) {
3755 3755 return (EFAULT);
3756 3756 }
3757 3757 return (0);
3758 3758 }
3759 3759
3760 3760 /* ARGSUSED */
3761 3761 static int
3762 3762 logout(dev_t dev, caddr_t arg, int mode, int *rval)
3763 3763 {
3764 3764 crypto_logout_t logout;
3765 3765 kcf_provider_desc_t *real_provider;
3766 3766 kcf_req_params_t params;
3767 3767 crypto_minor_t *cm;
3768 3768 crypto_session_data_t *sp;
3769 3769 int error = 0;
3770 3770 int rv;
3771 3771
3772 3772 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
3773 3773 cmn_err(CE_WARN, "logout: failed holding minor");
3774 3774 return (ENXIO);
3775 3775 }
3776 3776
3777 3777 if (copyin(arg, &logout, sizeof (logout)) != 0) {
3778 3778 crypto_release_minor(cm);
3779 3779 return (EFAULT);
3780 3780 }
3781 3781
3782 3782 if (!get_session_ptr(logout.cl_session, cm, &sp, &error, &rv)) {
3783 3783 goto release_minor;
3784 3784 }
3785 3785
3786 3786 if ((rv = kcf_get_hardware_provider_nomech(
3787 3787 CRYPTO_OPS_OFFSET(session_ops),
3788 3788 CRYPTO_SESSION_OFFSET(session_logout), sp->sd_provider,
3789 3789 &real_provider)) != CRYPTO_SUCCESS) {
3790 3790 goto out;
3791 3791 }
3792 3792
3793 3793 KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_LOGOUT, NULL,
3794 3794 sp->sd_provider_session->ps_session, 0, NULL, 0, real_provider);
3795 3795 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
3796 3796 KCF_PROV_REFRELE(real_provider);
3797 3797
3798 3798 out:
3799 3799 CRYPTO_SESSION_RELE(sp);
3800 3800
3801 3801 release_minor:
3802 3802 crypto_release_minor(cm);
3803 3803
3804 3804 if (error != 0)
3805 3805 return (error);
3806 3806
3807 3807 logout.cl_return_value = rv;
3808 3808 if (copyout(&logout, arg, sizeof (logout)) != 0) {
3809 3809 return (EFAULT);
3810 3810 }
3811 3811 return (0);
3812 3812 }
3813 3813
3814 3814 /* ARGSUSED */
3815 3815 static int
3816 3816 sign_init(dev_t dev, caddr_t arg, int mode, int *rval)
3817 3817 {
3818 3818 return (sign_verify_init(dev, arg, mode, crypto_sign_init_prov));
3819 3819 }
3820 3820
3821 3821 /* ARGSUSED */
3822 3822 static int
3823 3823 sign_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
3824 3824 {
3825 3825 return (sign_verify_init(dev, arg, mode,
3826 3826 crypto_sign_recover_init_prov));
3827 3827 }
3828 3828
3829 3829 /* ARGSUSED */
3830 3830 static int
3831 3831 verify_init(dev_t dev, caddr_t arg, int mode, int *rval)
3832 3832 {
3833 3833 return (sign_verify_init(dev, arg, mode, crypto_verify_init_prov));
3834 3834 }
3835 3835
3836 3836 /* ARGSUSED */
3837 3837 static int
3838 3838 verify_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
3839 3839 {
3840 3840 return (sign_verify_init(dev, arg, mode,
3841 3841 crypto_verify_recover_init_prov));
3842 3842 }
3843 3843
3844 3844 /*
3845 3845 * ASSUMPTION: crypto_sign_init, crypto_verify_init, crypto_sign_recover_init,
3846 3846 * and crypto_verify_recover_init structures are identical
3847 3847 * except for field names.
3848 3848 */
3849 3849 static int
3850 3850 sign_verify_init(dev_t dev, caddr_t arg, int mode,
3851 3851 int (*init)(crypto_provider_t, crypto_session_id_t,
3852 3852 crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
3853 3853 crypto_context_t *, crypto_call_req_t *))
3854 3854 {
3855 3855 STRUCT_DECL(crypto_sign_init, sign_init);
3856 3856 kcf_provider_desc_t *real_provider = NULL;
3857 3857 crypto_session_id_t session_id;
3858 3858 crypto_mechanism_t mech;
3859 3859 crypto_key_t key;
3860 3860 crypto_minor_t *cm;
3861 3861 crypto_session_data_t *sp = NULL;
3862 3862 crypto_context_t cc;
3863 3863 crypto_ctx_t **ctxpp;
3864 3864 size_t mech_rctl_bytes = 0;
3865 3865 boolean_t mech_rctl_chk = B_FALSE;
3866 3866 size_t key_rctl_bytes = 0;
3867 3867 boolean_t key_rctl_chk = B_FALSE;
3868 3868 int error = 0;
3869 3869 int rv;
3870 3870 boolean_t allocated_by_crypto_module = B_FALSE;
3871 3871 crypto_func_group_t fg;
3872 3872
3873 3873 STRUCT_INIT(sign_init, mode);
3874 3874
3875 3875 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
3876 3876 cmn_err(CE_WARN, "sign_verify_init: failed holding minor");
3877 3877 return (ENXIO);
3878 3878 }
3879 3879
3880 3880 if (copyin(arg, STRUCT_BUF(sign_init), STRUCT_SIZE(sign_init)) != 0) {
3881 3881 crypto_release_minor(cm);
3882 3882 return (EFAULT);
3883 3883 }
3884 3884
3885 3885 mech.cm_param = NULL;
3886 3886 bzero(&key, sizeof (key));
3887 3887
3888 3888 session_id = STRUCT_FGET(sign_init, si_session);
3889 3889
3890 3890 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
3891 3891 goto out;
3892 3892 }
3893 3893
3894 3894 bcopy(STRUCT_FADDR(sign_init, si_mech), &mech.cm_type,
3895 3895 sizeof (crypto_mech_type_t));
3896 3896
3897 3897 ASSERT(init == crypto_sign_init_prov ||
3898 3898 init == crypto_verify_init_prov ||
3899 3899 init == crypto_sign_recover_init_prov ||
3900 3900 init == crypto_verify_recover_init_prov);
3901 3901
3902 3902 if (init == crypto_sign_init_prov) {
3903 3903 fg = CRYPTO_FG_SIGN;
3904 3904 ctxpp = &sp->sd_sign_ctx;
3905 3905 } else if (init == crypto_verify_init_prov) {
3906 3906 fg = CRYPTO_FG_VERIFY;
3907 3907 ctxpp = &sp->sd_verify_ctx;
3908 3908 } else if (init == crypto_sign_recover_init_prov) {
3909 3909 fg = CRYPTO_FG_SIGN_RECOVER;
3910 3910 ctxpp = &sp->sd_sign_recover_ctx;
3911 3911 } else {
3912 3912 fg = CRYPTO_FG_VERIFY_RECOVER;
3913 3913 ctxpp = &sp->sd_verify_recover_ctx;
3914 3914 }
3915 3915
3916 3916 /* We need the key length for provider selection so copy it in now. */
3917 3917 if (!copyin_key(mode, sp, STRUCT_FADDR(sign_init, si_key), &key,
3918 3918 &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
3919 3919 goto out;
3920 3920 }
3921 3921
3922 3922 if ((rv = kcf_get_hardware_provider(mech.cm_type, &key,
3923 3923 CRYPTO_MECH_INVALID, NULL, sp->sd_provider, &real_provider,
3924 3924 fg)) != CRYPTO_SUCCESS) {
3925 3925 goto out;
3926 3926 }
3927 3927
3928 3928 rv = crypto_provider_copyin_mech_param(real_provider,
3929 3929 STRUCT_FADDR(sign_init, si_mech), &mech, mode, &error);
3930 3930
3931 3931 if (rv == CRYPTO_NOT_SUPPORTED) {
3932 3932 allocated_by_crypto_module = B_TRUE;
3933 3933 if (!copyin_mech(mode, sp, STRUCT_FADDR(sign_init, si_mech),
3934 3934 &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
3935 3935 goto out;
3936 3936 }
3937 3937 } else {
3938 3938 if (rv != CRYPTO_SUCCESS)
3939 3939 goto out;
3940 3940 }
3941 3941
3942 3942 rv = (init)(real_provider, sp->sd_provider_session->ps_session,
3943 3943 &mech, &key, NULL, &cc, NULL);
3944 3944
3945 3945 /*
3946 3946 * Check if a context already exists. If so, it means it is being
3947 3947 * abandoned. So, cancel it to avoid leaking it.
3948 3948 */
3949 3949 if (*ctxpp != NULL)
3950 3950 CRYPTO_CANCEL_CTX(ctxpp);
3951 3951 *ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
3952 3952
3953 3953 out:
3954 3954 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
3955 3955 CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
3956 3956 CRYPTO_SESSION_RELE(sp);
3957 3957 crypto_release_minor(cm);
3958 3958
3959 3959 if (real_provider != NULL) {
3960 3960 crypto_free_mech(real_provider,
3961 3961 allocated_by_crypto_module, &mech);
3962 3962 KCF_PROV_REFRELE(real_provider);
3963 3963 }
3964 3964
3965 3965 free_crypto_key(&key);
3966 3966
3967 3967 if (error != 0)
3968 3968 return (error);
3969 3969
3970 3970 STRUCT_FSET(sign_init, si_return_value, rv);
3971 3971 if (copyout(STRUCT_BUF(sign_init), arg, STRUCT_SIZE(sign_init)) != 0) {
3972 3972 return (EFAULT);
3973 3973 }
3974 3974 return (0);
3975 3975 }
3976 3976
3977 3977 /* ARGSUSED */
3978 3978 static int
3979 3979 sign(dev_t dev, caddr_t arg, int mode, int *rval)
3980 3980 {
3981 3981 return (common_digest(dev, arg, mode, crypto_sign_single));
3982 3982 }
3983 3983
3984 3984 /* ARGSUSED */
3985 3985 static int
3986 3986 sign_recover(dev_t dev, caddr_t arg, int mode, int *rval)
3987 3987 {
3988 3988 return (common_digest(dev, arg, mode, crypto_sign_recover_single));
3989 3989 }
3990 3990
3991 3991 /* ARGSUSED */
3992 3992 static int
3993 3993 verify(dev_t dev, caddr_t arg, int mode, int *rval)
3994 3994 {
3995 3995 STRUCT_DECL(crypto_verify, verify);
3996 3996 crypto_session_id_t session_id;
3997 3997 crypto_minor_t *cm;
3998 3998 crypto_session_data_t *sp = NULL;
3999 3999 crypto_data_t data, sign;
4000 4000 size_t datalen, signlen, need = 0;
4001 4001 int error = 0;
4002 4002 int rv;
4003 4003 boolean_t rctl_chk = B_FALSE;
4004 4004
4005 4005 STRUCT_INIT(verify, mode);
4006 4006
4007 4007 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4008 4008 cmn_err(CE_WARN, "verify: failed holding minor");
4009 4009 return (ENXIO);
4010 4010 }
4011 4011
4012 4012 if (copyin(arg, STRUCT_BUF(verify), STRUCT_SIZE(verify)) != 0) {
4013 4013 crypto_release_minor(cm);
4014 4014 return (EFAULT);
4015 4015 }
4016 4016
4017 4017 data.cd_raw.iov_base = NULL;
4018 4018 sign.cd_raw.iov_base = NULL;
4019 4019
4020 4020 datalen = STRUCT_FGET(verify, cv_datalen);
4021 4021 signlen = STRUCT_FGET(verify, cv_signlen);
4022 4022 if (datalen > crypto_max_buffer_len ||
4023 4023 signlen > crypto_max_buffer_len) {
4024 4024 cmn_err(CE_NOTE, "verify: buffer greater than %ld bytes, "
4025 4025 "pid = %d", crypto_max_buffer_len, curproc->p_pid);
4026 4026 rv = CRYPTO_ARGUMENTS_BAD;
4027 4027 goto release_minor;
4028 4028 }
4029 4029
4030 4030 session_id = STRUCT_FGET(verify, cv_session);
4031 4031
4032 4032 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4033 4033 goto release_minor;
4034 4034 }
4035 4035
4036 4036 need = datalen + signlen;
4037 4037 if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
4038 4038 CRYPTO_SUCCESS) {
4039 4039 need = 0;
4040 4040 goto release_minor;
4041 4041 }
4042 4042
4043 4043 INIT_RAW_CRYPTO_DATA(data, datalen);
4044 4044 INIT_RAW_CRYPTO_DATA(sign, signlen);
4045 4045
4046 4046 if (datalen != 0 && copyin(STRUCT_FGETP(verify, cv_databuf),
4047 4047 data.cd_raw.iov_base, datalen) != 0) {
4048 4048 error = EFAULT;
4049 4049 goto release_minor;
4050 4050 }
4051 4051
4052 4052 if (signlen != 0 && copyin(STRUCT_FGETP(verify, cv_signbuf),
4053 4053 sign.cd_raw.iov_base, signlen) != 0) {
4054 4054 error = EFAULT;
4055 4055 goto release_minor;
4056 4056 }
4057 4057
4058 4058 rv = crypto_verify_single(sp->sd_verify_ctx, &data, &sign, NULL);
4059 4059 if (KCF_CONTEXT_DONE(rv))
4060 4060 sp->sd_verify_ctx = NULL;
4061 4061
4062 4062 release_minor:
4063 4063 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
4064 4064 CRYPTO_SESSION_RELE(sp);
4065 4065 crypto_release_minor(cm);
4066 4066
4067 4067 if (data.cd_raw.iov_base != NULL)
4068 4068 kmem_free(data.cd_raw.iov_base, datalen);
4069 4069
4070 4070 if (sign.cd_raw.iov_base != NULL)
4071 4071 kmem_free(sign.cd_raw.iov_base, signlen);
4072 4072
4073 4073 if (error != 0)
4074 4074 return (error);
4075 4075
4076 4076 STRUCT_FSET(verify, cv_return_value, rv);
4077 4077 if (copyout(STRUCT_BUF(verify), arg, STRUCT_SIZE(verify)) != 0) {
4078 4078 return (EFAULT);
4079 4079 }
4080 4080 return (0);
4081 4081 }
4082 4082
4083 4083 /* ARGSUSED */
4084 4084 static int
4085 4085 verify_recover(dev_t dev, caddr_t arg, int mode, int *rval)
4086 4086 {
4087 4087 return (common_digest(dev, arg, mode, crypto_verify_recover_single));
4088 4088 }
4089 4089
4090 4090 /* ARGSUSED */
4091 4091 static int
4092 4092 sign_update(dev_t dev, caddr_t arg, int mode, int *rval)
4093 4093 {
4094 4094 return (sign_verify_update(dev, arg, mode, crypto_sign_update));
4095 4095 }
4096 4096
4097 4097 /* ARGSUSED */
4098 4098 static int
4099 4099 verify_update(dev_t dev, caddr_t arg, int mode, int *rval)
4100 4100 {
4101 4101 return (sign_verify_update(dev, arg, mode, crypto_verify_update));
4102 4102 }
4103 4103
4104 4104 /*
4105 4105 * ASSUMPTION: crypto_sign_update and crypto_verify_update structures
4106 4106 * are identical except for field names.
4107 4107 */
4108 4108 static int
4109 4109 sign_verify_update(dev_t dev, caddr_t arg, int mode,
4110 4110 int (*update)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
4111 4111 {
4112 4112 STRUCT_DECL(crypto_sign_update, sign_update);
4113 4113 crypto_session_id_t session_id;
4114 4114 crypto_minor_t *cm;
4115 4115 crypto_session_data_t *sp = NULL;
4116 4116 crypto_ctx_t **ctxpp;
4117 4117 crypto_data_t data;
4118 4118 size_t datalen, need = 0;
4119 4119 int error = 0;
4120 4120 int rv;
4121 4121 boolean_t rctl_chk = B_FALSE;
4122 4122
4123 4123 STRUCT_INIT(sign_update, mode);
4124 4124
4125 4125 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4126 4126 cmn_err(CE_WARN, "sign_verify_update: failed holding minor");
4127 4127 return (ENXIO);
4128 4128 }
4129 4129
4130 4130 if (copyin(arg, STRUCT_BUF(sign_update),
4131 4131 STRUCT_SIZE(sign_update)) != 0) {
4132 4132 crypto_release_minor(cm);
4133 4133 return (EFAULT);
4134 4134 }
4135 4135
4136 4136 data.cd_raw.iov_base = NULL;
4137 4137
4138 4138 datalen = STRUCT_FGET(sign_update, su_datalen);
4139 4139 if (datalen > crypto_max_buffer_len) {
4140 4140 cmn_err(CE_NOTE, "sign_verify_update: buffer greater than %ld "
4141 4141 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
4142 4142 rv = CRYPTO_ARGUMENTS_BAD;
4143 4143 goto release_minor;
4144 4144 }
4145 4145
4146 4146 session_id = STRUCT_FGET(sign_update, su_session);
4147 4147
4148 4148 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4149 4149 goto release_minor;
4150 4150 }
4151 4151
4152 4152 if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
4153 4153 CRYPTO_SUCCESS) {
4154 4154 goto release_minor;
4155 4155 }
4156 4156 need = datalen;
4157 4157
4158 4158 INIT_RAW_CRYPTO_DATA(data, datalen);
4159 4159
4160 4160 if (datalen != 0 && copyin(STRUCT_FGETP(sign_update, su_databuf),
4161 4161 data.cd_raw.iov_base, datalen) != 0) {
4162 4162 error = EFAULT;
4163 4163 goto release_minor;
4164 4164 }
4165 4165
4166 4166 ctxpp = (update == crypto_sign_update) ?
4167 4167 &sp->sd_sign_ctx : &sp->sd_verify_ctx;
4168 4168
4169 4169 rv = (update)(*ctxpp, &data, NULL);
4170 4170 if (rv != CRYPTO_SUCCESS)
4171 4171 CRYPTO_CANCEL_CTX(ctxpp);
4172 4172
4173 4173 release_minor:
4174 4174 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
4175 4175 CRYPTO_SESSION_RELE(sp);
4176 4176 crypto_release_minor(cm);
4177 4177
4178 4178 if (data.cd_raw.iov_base != NULL)
4179 4179 kmem_free(data.cd_raw.iov_base, datalen);
4180 4180
4181 4181 if (error != 0)
4182 4182 return (error);
4183 4183
4184 4184 STRUCT_FSET(sign_update, su_return_value, rv);
4185 4185 if (copyout(STRUCT_BUF(sign_update), arg,
4186 4186 STRUCT_SIZE(sign_update)) != 0) {
4187 4187 return (EFAULT);
4188 4188 }
4189 4189 return (0);
4190 4190 }
4191 4191
4192 4192 /* ARGSUSED */
4193 4193 static int
4194 4194 sign_final(dev_t dev, caddr_t arg, int mode, int *rval)
4195 4195 {
4196 4196 return (common_final(dev, arg, mode, crypto_sign_final));
4197 4197 }
4198 4198
4199 4199 /*
4200 4200 * Can't use the common final because it does a copyout of
4201 4201 * the final part.
4202 4202 */
4203 4203 /* ARGSUSED */
4204 4204 static int
4205 4205 verify_final(dev_t dev, caddr_t arg, int mode, int *rval)
4206 4206 {
4207 4207 STRUCT_DECL(crypto_verify_final, verify_final);
4208 4208 crypto_session_id_t session_id;
4209 4209 crypto_minor_t *cm;
4210 4210 crypto_session_data_t *sp = NULL;
4211 4211 crypto_data_t sign;
4212 4212 size_t signlen, need = 0;
4213 4213 int error = 0;
4214 4214 int rv;
4215 4215 boolean_t rctl_chk = B_FALSE;
4216 4216
4217 4217 STRUCT_INIT(verify_final, mode);
4218 4218
4219 4219 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4220 4220 cmn_err(CE_WARN, "verify_final: failed holding minor");
4221 4221 return (ENXIO);
4222 4222 }
4223 4223
4224 4224 if (copyin(arg, STRUCT_BUF(verify_final),
4225 4225 STRUCT_SIZE(verify_final)) != 0) {
4226 4226 crypto_release_minor(cm);
4227 4227 return (EFAULT);
4228 4228 }
4229 4229
4230 4230 sign.cd_raw.iov_base = NULL;
4231 4231
4232 4232 signlen = STRUCT_FGET(verify_final, vf_signlen);
4233 4233 if (signlen > crypto_max_buffer_len) {
4234 4234 cmn_err(CE_NOTE, "verify_final: buffer greater than %ld "
4235 4235 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
4236 4236 rv = CRYPTO_ARGUMENTS_BAD;
4237 4237 goto release_minor;
4238 4238 }
4239 4239
4240 4240 session_id = STRUCT_FGET(verify_final, vf_session);
4241 4241
4242 4242 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4243 4243 goto release_minor;
4244 4244 }
4245 4245
4246 4246 if ((rv = CRYPTO_BUFFER_CHECK(sp, signlen, rctl_chk)) !=
4247 4247 CRYPTO_SUCCESS) {
4248 4248 goto release_minor;
4249 4249 }
4250 4250 need = signlen;
4251 4251
4252 4252 INIT_RAW_CRYPTO_DATA(sign, signlen);
4253 4253
4254 4254 if (signlen != 0 && copyin(STRUCT_FGETP(verify_final, vf_signbuf),
4255 4255 sign.cd_raw.iov_base, signlen) != 0) {
4256 4256 error = EFAULT;
4257 4257 goto release_minor;
4258 4258 }
4259 4259
4260 4260 rv = crypto_verify_final(sp->sd_verify_ctx, &sign, NULL);
4261 4261 if (KCF_CONTEXT_DONE(rv))
4262 4262 sp->sd_verify_ctx = NULL;
4263 4263
4264 4264 release_minor:
4265 4265 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
4266 4266 CRYPTO_SESSION_RELE(sp);
4267 4267 crypto_release_minor(cm);
4268 4268
4269 4269 if (sign.cd_raw.iov_base != NULL)
4270 4270 kmem_free(sign.cd_raw.iov_base, signlen);
4271 4271
4272 4272 if (error != 0)
4273 4273 return (error);
4274 4274
4275 4275 STRUCT_FSET(verify_final, vf_return_value, rv);
4276 4276 if (copyout(STRUCT_BUF(verify_final), arg,
4277 4277 STRUCT_SIZE(verify_final)) != 0) {
4278 4278 return (EFAULT);
4279 4279 }
4280 4280 return (0);
4281 4281 }
4282 4282
4283 4283 /* ARGSUSED */
4284 4284 static int
4285 4285 seed_random(dev_t dev, caddr_t arg, int mode, int *rval)
4286 4286 {
4287 4287 STRUCT_DECL(crypto_seed_random, seed_random);
4288 4288 kcf_provider_desc_t *real_provider = NULL;
4289 4289 kcf_req_params_t params;
4290 4290 crypto_session_id_t session_id;
4291 4291 crypto_minor_t *cm;
4292 4292 crypto_session_data_t *sp = NULL;
4293 4293 uchar_t *seed_buffer = NULL;
4294 4294 size_t seed_len;
4295 4295 size_t need = 0;
4296 4296 int error = 0;
4297 4297 int rv;
4298 4298 boolean_t rctl_chk = B_FALSE;
4299 4299
4300 4300 STRUCT_INIT(seed_random, mode);
4301 4301
4302 4302 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4303 4303 cmn_err(CE_WARN, "seed_random: failed holding minor");
4304 4304 return (ENXIO);
4305 4305 }
4306 4306
4307 4307 if (copyin(arg, STRUCT_BUF(seed_random),
4308 4308 STRUCT_SIZE(seed_random)) != 0) {
4309 4309 crypto_release_minor(cm);
4310 4310 return (EFAULT);
4311 4311 }
4312 4312
4313 4313 seed_len = STRUCT_FGET(seed_random, sr_seedlen);
4314 4314 if (seed_len > crypto_max_buffer_len) {
4315 4315 cmn_err(CE_NOTE, "seed_random: buffer greater than %ld "
4316 4316 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
4317 4317 rv = CRYPTO_ARGUMENTS_BAD;
4318 4318 goto release_minor;
4319 4319 }
4320 4320
4321 4321 session_id = STRUCT_FGET(seed_random, sr_session);
4322 4322
4323 4323 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4324 4324 goto release_minor;
4325 4325 }
4326 4326
4327 4327 if ((rv = CRYPTO_BUFFER_CHECK(sp, seed_len, rctl_chk)) !=
4328 4328 CRYPTO_SUCCESS) {
4329 4329 goto release_minor;
4330 4330 }
4331 4331 need = seed_len;
4332 4332 seed_buffer = kmem_alloc(seed_len, KM_SLEEP);
4333 4333
4334 4334 if (seed_len != 0 && copyin(STRUCT_FGETP(seed_random, sr_seedbuf),
4335 4335 seed_buffer, seed_len) != 0) {
4336 4336 error = EFAULT;
4337 4337 goto release_minor;
4338 4338 }
4339 4339
4340 4340 if ((rv = kcf_get_hardware_provider_nomech(
4341 4341 CRYPTO_OPS_OFFSET(random_ops), CRYPTO_RANDOM_OFFSET(seed_random),
4342 4342 sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
4343 4343 goto release_minor;
4344 4344 }
4345 4345
4346 4346 KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, KCF_OP_RANDOM_SEED,
4347 4347 sp->sd_provider_session->ps_session, seed_buffer, seed_len, 0,
4348 4348 CRYPTO_SEED_NOW);
4349 4349
4350 4350 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4351 4351
4352 4352 release_minor:
4353 4353 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
4354 4354 CRYPTO_SESSION_RELE(sp);
4355 4355 crypto_release_minor(cm);
4356 4356
4357 4357 if (real_provider != NULL)
4358 4358 KCF_PROV_REFRELE(real_provider);
4359 4359
4360 4360 if (seed_buffer != NULL)
4361 4361 kmem_free(seed_buffer, seed_len);
4362 4362
4363 4363 if (error != 0)
4364 4364 return (error);
4365 4365
4366 4366 STRUCT_FSET(seed_random, sr_return_value, rv);
4367 4367 if (copyout(STRUCT_BUF(seed_random), arg,
4368 4368 STRUCT_SIZE(seed_random)) != 0) {
4369 4369 return (EFAULT);
4370 4370 }
4371 4371 return (0);
4372 4372 }
4373 4373
4374 4374 /* ARGSUSED */
4375 4375 static int
4376 4376 generate_random(dev_t dev, caddr_t arg, int mode, int *rval)
4377 4377 {
4378 4378 STRUCT_DECL(crypto_generate_random, generate_random);
4379 4379 kcf_provider_desc_t *real_provider = NULL;
4380 4380 kcf_req_params_t params;
4381 4381 crypto_session_id_t session_id;
4382 4382 crypto_minor_t *cm;
4383 4383 crypto_session_data_t *sp = NULL;
4384 4384 uchar_t *buffer = NULL;
4385 4385 size_t len;
4386 4386 size_t need = 0;
4387 4387 int error = 0;
4388 4388 int rv;
4389 4389 boolean_t rctl_chk = B_FALSE;
4390 4390
4391 4391 STRUCT_INIT(generate_random, mode);
4392 4392
4393 4393 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4394 4394 cmn_err(CE_WARN, "generate_random: failed holding minor");
4395 4395 return (ENXIO);
4396 4396 }
4397 4397
4398 4398 if (copyin(arg, STRUCT_BUF(generate_random),
4399 4399 STRUCT_SIZE(generate_random)) != 0) {
4400 4400 crypto_release_minor(cm);
4401 4401 return (EFAULT);
4402 4402 }
4403 4403
4404 4404 len = STRUCT_FGET(generate_random, gr_buflen);
4405 4405 if (len > crypto_max_buffer_len) {
4406 4406 cmn_err(CE_NOTE, "generate_random: buffer greater than %ld "
4407 4407 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
4408 4408 rv = CRYPTO_ARGUMENTS_BAD;
4409 4409 goto release_minor;
4410 4410 }
4411 4411
4412 4412 session_id = STRUCT_FGET(generate_random, gr_session);
4413 4413
4414 4414 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4415 4415 goto release_minor;
4416 4416 }
4417 4417
4418 4418 if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
4419 4419 CRYPTO_SUCCESS) {
4420 4420 goto release_minor;
4421 4421 }
4422 4422 need = len;
4423 4423 buffer = kmem_alloc(len, KM_SLEEP);
4424 4424
4425 4425 if ((rv = kcf_get_hardware_provider_nomech(
4426 4426 CRYPTO_OPS_OFFSET(random_ops),
4427 4427 CRYPTO_RANDOM_OFFSET(generate_random), sp->sd_provider,
4428 4428 &real_provider)) != CRYPTO_SUCCESS) {
4429 4429 goto release_minor;
4430 4430 }
4431 4431
4432 4432 KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, KCF_OP_RANDOM_GENERATE,
4433 4433 sp->sd_provider_session->ps_session, buffer, len, 0, 0);
4434 4434
4435 4435 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4436 4436
4437 4437 if (rv == CRYPTO_SUCCESS) {
4438 4438 if (len != 0 && copyout(buffer,
4439 4439 STRUCT_FGETP(generate_random, gr_buf), len) != 0) {
4440 4440 error = EFAULT;
4441 4441 }
4442 4442 }
4443 4443
4444 4444 release_minor:
4445 4445 CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
4446 4446 CRYPTO_SESSION_RELE(sp);
4447 4447 crypto_release_minor(cm);
4448 4448
4449 4449 if (real_provider != NULL)
4450 4450 KCF_PROV_REFRELE(real_provider);
4451 4451
4452 4452 if (buffer != NULL) {
4453 4453 /* random numbers are often used to create keys */
4454 4454 bzero(buffer, len);
4455 4455 kmem_free(buffer, len);
4456 4456 }
4457 4457
4458 4458 if (error != 0)
4459 4459 return (error);
4460 4460
4461 4461 STRUCT_FSET(generate_random, gr_return_value, rv);
4462 4462 if (copyout(STRUCT_BUF(generate_random), arg,
4463 4463 STRUCT_SIZE(generate_random)) != 0) {
4464 4464 return (EFAULT);
4465 4465 }
4466 4466 return (0);
4467 4467 }
4468 4468
4469 4469 /*
4470 4470 * Copyout a kernel array of attributes to user space.
4471 4471 * u_attrs is the corresponding user space array containing
4472 4472 * user space pointers necessary for the copyout.
4473 4473 */
4474 4474 /* ARGSUSED */
4475 4475 static int
4476 4476 copyout_attributes(int mode, caddr_t out, uint_t count,
4477 4477 crypto_object_attribute_t *k_attrs, caddr_t u_attrs)
4478 4478 {
4479 4479 STRUCT_DECL(crypto_object_attribute, oa);
4480 4480 caddr_t p, valuep;
4481 4481 size_t value_len;
4482 4482 size_t len;
4483 4483 int i;
4484 4484 int error = 0;
4485 4485
4486 4486 if (count == 0)
4487 4487 return (0);
4488 4488
4489 4489 STRUCT_INIT(oa, mode);
4490 4490
4491 4491 len = count * STRUCT_SIZE(oa);
4492 4492
4493 4493 ASSERT(u_attrs != NULL);
4494 4494 p = u_attrs;
4495 4495 for (i = 0; i < count; i++) {
4496 4496 /* can this bcopy be eliminated? */
4497 4497 bcopy(p, STRUCT_BUF(oa), STRUCT_SIZE(oa));
4498 4498 value_len = k_attrs[i].oa_value_len;
4499 4499 STRUCT_FSET(oa, oa_type, k_attrs[i].oa_type);
4500 4500 STRUCT_FSET(oa, oa_value_len, (ssize_t)value_len);
4501 4501 valuep = STRUCT_FGETP(oa, oa_value);
4502 4502 if ((valuep != NULL) && (value_len != (size_t)-1)) {
4503 4503 if (copyout(k_attrs[i].oa_value,
4504 4504 valuep, value_len) != 0) {
4505 4505 error = EFAULT;
4506 4506 goto out;
4507 4507 }
4508 4508 }
4509 4509 bcopy(STRUCT_BUF(oa), p, STRUCT_SIZE(oa));
4510 4510 p += STRUCT_SIZE(oa);
4511 4511 }
4512 4512 if (copyout(u_attrs, out, len)) {
4513 4513 error = EFAULT;
4514 4514 }
4515 4515 out:
4516 4516 return (error);
4517 4517 }
4518 4518
4519 4519
4520 4520 /* ARGSUSED */
4521 4521 static int
4522 4522 object_create(dev_t dev, caddr_t arg, int mode, int *rval)
4523 4523 {
4524 4524 STRUCT_DECL(crypto_object_create, object_create);
4525 4525 kcf_provider_desc_t *real_provider = NULL;
4526 4526 kcf_req_params_t params;
4527 4527 crypto_object_attribute_t *k_attrs = NULL;
4528 4528 crypto_session_id_t session_id;
4529 4529 crypto_minor_t *cm;
4530 4530 crypto_session_data_t *sp = NULL;
4531 4531 crypto_object_id_t object_handle;
4532 4532 caddr_t oc_attributes;
4533 4533 size_t k_attrs_size;
4534 4534 size_t rctl_bytes = 0;
4535 4535 boolean_t rctl_chk = B_FALSE;
4536 4536 int error = 0;
4537 4537 int rv;
4538 4538 uint_t count;
4539 4539
4540 4540 STRUCT_INIT(object_create, mode);
4541 4541
4542 4542 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4543 4543 cmn_err(CE_WARN, "object_create: failed holding minor");
4544 4544 return (ENXIO);
4545 4545 }
4546 4546
4547 4547 if (copyin(arg, STRUCT_BUF(object_create),
4548 4548 STRUCT_SIZE(object_create)) != 0) {
4549 4549 crypto_release_minor(cm);
4550 4550 return (EFAULT);
4551 4551 }
4552 4552
4553 4553 count = STRUCT_FGET(object_create, oc_count);
4554 4554 oc_attributes = STRUCT_FGETP(object_create, oc_attributes);
4555 4555
4556 4556 session_id = STRUCT_FGET(object_create, oc_session);
4557 4557 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4558 4558 goto release_minor;
4559 4559 }
4560 4560 if (!copyin_attributes(mode, sp, count, oc_attributes, &k_attrs,
4561 4561 &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
4562 4562 &rctl_chk, B_TRUE)) {
4563 4563 goto release_minor;
4564 4564 }
4565 4565
4566 4566 if ((rv = kcf_get_hardware_provider_nomech(
4567 4567 CRYPTO_OPS_OFFSET(object_ops),
4568 4568 CRYPTO_OBJECT_OFFSET(object_create), sp->sd_provider,
4569 4569 &real_provider)) != CRYPTO_SUCCESS) {
4570 4570 goto release_minor;
4571 4571 }
4572 4572
4573 4573 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_CREATE,
4574 4574 sp->sd_provider_session->ps_session, 0, k_attrs, count,
4575 4575 &object_handle, 0, NULL, NULL, 0, NULL);
4576 4576
4577 4577 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4578 4578
4579 4579 if (rv == CRYPTO_SUCCESS)
4580 4580 STRUCT_FSET(object_create, oc_handle, object_handle);
4581 4581
4582 4582 release_minor:
4583 4583 CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
4584 4584
4585 4585 if (k_attrs != NULL)
4586 4586 kmem_free(k_attrs, k_attrs_size);
4587 4587
4588 4588 if (error != 0)
4589 4589 goto out;
4590 4590
4591 4591 STRUCT_FSET(object_create, oc_return_value, rv);
4592 4592 if (copyout(STRUCT_BUF(object_create), arg,
4593 4593 STRUCT_SIZE(object_create)) != 0) {
4594 4594 if (rv == CRYPTO_SUCCESS) {
4595 4595 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
4596 4596 KCF_OP_OBJECT_DESTROY,
4597 4597 sp->sd_provider_session->ps_session, object_handle,
4598 4598 NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
4599 4599
4600 4600 (void) kcf_submit_request(real_provider, NULL,
4601 4601 NULL, ¶ms, B_FALSE);
4602 4602
4603 4603 error = EFAULT;
4604 4604 }
4605 4605 }
4606 4606 out:
4607 4607 CRYPTO_SESSION_RELE(sp);
4608 4608 crypto_release_minor(cm);
4609 4609 if (real_provider != NULL)
4610 4610 KCF_PROV_REFRELE(real_provider);
4611 4611 return (error);
4612 4612 }
4613 4613
4614 4614 /* ARGSUSED */
4615 4615 static int
4616 4616 object_copy(dev_t dev, caddr_t arg, int mode, int *rval)
4617 4617 {
4618 4618 STRUCT_DECL(crypto_object_copy, object_copy);
4619 4619 kcf_provider_desc_t *real_provider = NULL;
4620 4620 kcf_req_params_t params;
4621 4621 crypto_object_attribute_t *k_attrs = NULL;
4622 4622 crypto_session_id_t session_id;
4623 4623 crypto_minor_t *cm;
4624 4624 crypto_session_data_t *sp = NULL;
4625 4625 crypto_object_id_t handle, new_handle;
4626 4626 caddr_t oc_new_attributes;
4627 4627 size_t k_attrs_size;
4628 4628 size_t rctl_bytes = 0;
4629 4629 boolean_t rctl_chk = B_FALSE;
4630 4630 int error = 0;
4631 4631 int rv;
4632 4632 uint_t count;
4633 4633
4634 4634 STRUCT_INIT(object_copy, mode);
4635 4635
4636 4636 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4637 4637 cmn_err(CE_WARN, "object_copy: failed holding minor");
4638 4638 return (ENXIO);
4639 4639 }
4640 4640
4641 4641 if (copyin(arg, STRUCT_BUF(object_copy),
4642 4642 STRUCT_SIZE(object_copy)) != 0) {
4643 4643 crypto_release_minor(cm);
4644 4644 return (EFAULT);
4645 4645 }
4646 4646
4647 4647 count = STRUCT_FGET(object_copy, oc_count);
4648 4648 oc_new_attributes = STRUCT_FGETP(object_copy, oc_new_attributes);
4649 4649
4650 4650 session_id = STRUCT_FGET(object_copy, oc_session);
4651 4651
4652 4652 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4653 4653 goto release_minor;
4654 4654 }
4655 4655 if (!copyin_attributes(mode, sp, count, oc_new_attributes, &k_attrs,
4656 4656 &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
4657 4657 &rctl_chk, B_TRUE)) {
4658 4658 goto release_minor;
4659 4659 }
4660 4660
4661 4661 if ((rv = kcf_get_hardware_provider_nomech(
4662 4662 CRYPTO_OPS_OFFSET(object_ops),
4663 4663 CRYPTO_OBJECT_OFFSET(object_copy), sp->sd_provider,
4664 4664 &real_provider)) != CRYPTO_SUCCESS) {
4665 4665 goto release_minor;
4666 4666 }
4667 4667
4668 4668 handle = STRUCT_FGET(object_copy, oc_handle);
4669 4669 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_COPY,
4670 4670 sp->sd_provider_session->ps_session, handle, k_attrs, count,
4671 4671 &new_handle, 0, NULL, NULL, 0, NULL);
4672 4672
4673 4673 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4674 4674
4675 4675 if (rv == CRYPTO_SUCCESS)
4676 4676 STRUCT_FSET(object_copy, oc_new_handle, new_handle);
4677 4677
4678 4678 release_minor:
4679 4679 CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
4680 4680
4681 4681 if (k_attrs != NULL)
4682 4682 kmem_free(k_attrs, k_attrs_size);
4683 4683
4684 4684 if (error != 0)
4685 4685 goto out;
4686 4686
4687 4687 STRUCT_FSET(object_copy, oc_return_value, rv);
4688 4688 if (copyout(STRUCT_BUF(object_copy), arg,
4689 4689 STRUCT_SIZE(object_copy)) != 0) {
4690 4690 if (rv == CRYPTO_SUCCESS) {
4691 4691 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
4692 4692 KCF_OP_OBJECT_DESTROY,
4693 4693 sp->sd_provider_session->ps_session, new_handle,
4694 4694 NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
4695 4695
4696 4696 (void) kcf_submit_request(real_provider, NULL,
4697 4697 NULL, ¶ms, B_FALSE);
4698 4698
4699 4699 error = EFAULT;
4700 4700 }
4701 4701 }
4702 4702 out:
4703 4703 CRYPTO_SESSION_RELE(sp);
4704 4704 crypto_release_minor(cm);
4705 4705 if (real_provider != NULL)
4706 4706 KCF_PROV_REFRELE(real_provider);
4707 4707 return (error);
4708 4708 }
4709 4709
4710 4710 /* ARGSUSED */
4711 4711 static int
4712 4712 object_destroy(dev_t dev, caddr_t arg, int mode, int *rval)
4713 4713 {
4714 4714 STRUCT_DECL(crypto_object_destroy, object_destroy);
4715 4715 kcf_provider_desc_t *real_provider;
4716 4716 kcf_req_params_t params;
4717 4717 crypto_session_id_t session_id;
4718 4718 crypto_minor_t *cm;
4719 4719 crypto_session_data_t *sp;
4720 4720 crypto_object_id_t handle;
4721 4721 int error = 0;
4722 4722 int rv;
4723 4723
4724 4724 STRUCT_INIT(object_destroy, mode);
4725 4725
4726 4726 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4727 4727 cmn_err(CE_WARN, "object_destroy: failed holding minor");
4728 4728 return (ENXIO);
4729 4729 }
4730 4730
4731 4731 if (copyin(arg, STRUCT_BUF(object_destroy),
4732 4732 STRUCT_SIZE(object_destroy)) != 0) {
4733 4733 crypto_release_minor(cm);
4734 4734 return (EFAULT);
4735 4735 }
4736 4736
4737 4737 session_id = STRUCT_FGET(object_destroy, od_session);
4738 4738
4739 4739 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4740 4740 goto release_minor;
4741 4741 }
4742 4742
4743 4743 if ((rv = kcf_get_hardware_provider_nomech(
4744 4744 CRYPTO_OPS_OFFSET(object_ops),
4745 4745 CRYPTO_OBJECT_OFFSET(object_destroy), sp->sd_provider,
4746 4746 &real_provider)) != CRYPTO_SUCCESS) {
4747 4747 goto out;
4748 4748 }
4749 4749
4750 4750 handle = STRUCT_FGET(object_destroy, od_handle);
4751 4751 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_DESTROY,
4752 4752 sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, 0,
4753 4753 NULL, NULL, 0, NULL);
4754 4754
4755 4755 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4756 4756 KCF_PROV_REFRELE(real_provider);
4757 4757
4758 4758 out:
4759 4759 CRYPTO_SESSION_RELE(sp);
4760 4760
4761 4761 release_minor:
4762 4762 crypto_release_minor(cm);
4763 4763
4764 4764 if (error != 0)
4765 4765 return (error);
4766 4766
4767 4767 STRUCT_FSET(object_destroy, od_return_value, rv);
4768 4768
4769 4769 if (copyout(STRUCT_BUF(object_destroy), arg,
4770 4770 STRUCT_SIZE(object_destroy)) != 0) {
4771 4771 return (EFAULT);
4772 4772 }
4773 4773 return (0);
4774 4774 }
4775 4775
4776 4776 /* ARGSUSED */
4777 4777 static int
4778 4778 object_get_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
4779 4779 {
4780 4780 STRUCT_DECL(crypto_object_get_attribute_value, get_attribute_value);
4781 4781 #ifdef _LP64
4782 4782 STRUCT_DECL(crypto_object_attribute, oa);
4783 4783 #else
4784 4784 /* LINTED E_FUNC_SET_NOT_USED */
4785 4785 STRUCT_DECL(crypto_object_attribute, oa);
4786 4786 #endif
4787 4787 kcf_provider_desc_t *real_provider;
4788 4788 kcf_req_params_t params;
4789 4789 crypto_object_attribute_t *k_attrs = NULL;
4790 4790 crypto_session_id_t session_id;
4791 4791 crypto_minor_t *cm;
4792 4792 crypto_session_data_t *sp = NULL;
4793 4793 crypto_object_id_t handle;
4794 4794 caddr_t og_attributes;
4795 4795 caddr_t u_attrs = NULL;
4796 4796 size_t k_attrs_size;
4797 4797 size_t rctl_bytes = 0;
4798 4798 boolean_t rctl_chk = B_FALSE;
4799 4799 int error = 0;
4800 4800 int rv;
4801 4801 uint_t count;
4802 4802
4803 4803 STRUCT_INIT(get_attribute_value, mode);
4804 4804 STRUCT_INIT(oa, mode);
4805 4805
4806 4806 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4807 4807 cmn_err(CE_WARN,
4808 4808 "object_get_attribute_value: failed holding minor");
4809 4809 return (ENXIO);
4810 4810 }
4811 4811
4812 4812 if (copyin(arg, STRUCT_BUF(get_attribute_value),
4813 4813 STRUCT_SIZE(get_attribute_value)) != 0) {
4814 4814 crypto_release_minor(cm);
4815 4815 return (EFAULT);
4816 4816 }
4817 4817
4818 4818 count = STRUCT_FGET(get_attribute_value, og_count);
4819 4819 og_attributes = STRUCT_FGETP(get_attribute_value, og_attributes);
4820 4820
4821 4821 session_id = STRUCT_FGET(get_attribute_value, og_session);
4822 4822
4823 4823 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4824 4824 goto release_minor;
4825 4825 }
4826 4826 if (!copyin_attributes(mode, sp, count, og_attributes, &k_attrs,
4827 4827 &k_attrs_size, &u_attrs, &rv, &error, &rctl_bytes,
4828 4828 &rctl_chk, B_FALSE)) {
4829 4829 goto release_minor;
4830 4830 }
4831 4831
4832 4832 if ((rv = kcf_get_hardware_provider_nomech(
4833 4833 CRYPTO_OPS_OFFSET(object_ops),
4834 4834 CRYPTO_OBJECT_OFFSET(object_get_attribute_value),
4835 4835 sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
4836 4836 goto out;
4837 4837 }
4838 4838
4839 4839 handle = STRUCT_FGET(get_attribute_value, og_handle);
4840 4840 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_GET_ATTRIBUTE_VALUE,
4841 4841 sp->sd_provider_session->ps_session, handle, k_attrs, count, NULL,
4842 4842 0, NULL, NULL, 0, NULL);
4843 4843
4844 4844 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4845 4845 KCF_PROV_REFRELE(real_provider);
4846 4846
4847 4847 out:
4848 4848 if (rv == CRYPTO_SUCCESS || rv == CRYPTO_ATTRIBUTE_SENSITIVE ||
4849 4849 rv == CRYPTO_ATTRIBUTE_TYPE_INVALID ||
4850 4850 rv == CRYPTO_BUFFER_TOO_SMALL) {
4851 4851 error = copyout_attributes(mode,
4852 4852 STRUCT_FGETP(get_attribute_value, og_attributes),
4853 4853 count, k_attrs, u_attrs);
4854 4854 }
4855 4855
4856 4856 release_minor:
4857 4857 CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
4858 4858 CRYPTO_SESSION_RELE(sp);
4859 4859 crypto_release_minor(cm);
4860 4860
4861 4861 if (k_attrs != NULL)
4862 4862 kmem_free(k_attrs, k_attrs_size);
4863 4863
4864 4864 if (u_attrs != NULL)
4865 4865 kmem_free(u_attrs, count * STRUCT_SIZE(oa));
4866 4866
4867 4867 if (error != 0)
4868 4868 return (error);
4869 4869
4870 4870 STRUCT_FSET(get_attribute_value, og_return_value, rv);
4871 4871 if (copyout(STRUCT_BUF(get_attribute_value), arg,
4872 4872 STRUCT_SIZE(get_attribute_value)) != 0) {
4873 4873 return (EFAULT);
4874 4874 }
4875 4875 return (0);
4876 4876 }
4877 4877
4878 4878 /* ARGSUSED */
4879 4879 static int
4880 4880 object_get_size(dev_t dev, caddr_t arg, int mode, int *rval)
4881 4881 {
4882 4882 STRUCT_DECL(crypto_object_get_size, object_get_size);
4883 4883 kcf_provider_desc_t *real_provider;
4884 4884 kcf_req_params_t params;
4885 4885 crypto_session_id_t session_id;
4886 4886 crypto_minor_t *cm;
4887 4887 crypto_session_data_t *sp = NULL;
4888 4888 crypto_object_id_t handle;
4889 4889 size_t size;
4890 4890 int error = 0;
4891 4891 int rv;
4892 4892
4893 4893 STRUCT_INIT(object_get_size, mode);
4894 4894
4895 4895 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4896 4896 cmn_err(CE_WARN, "object_get_size: failed holding minor");
4897 4897 return (ENXIO);
4898 4898 }
4899 4899
4900 4900 if (copyin(arg, STRUCT_BUF(object_get_size),
4901 4901 STRUCT_SIZE(object_get_size)) != 0) {
4902 4902 crypto_release_minor(cm);
4903 4903 return (EFAULT);
4904 4904 }
4905 4905
4906 4906 session_id = STRUCT_FGET(object_get_size, gs_session);
4907 4907
4908 4908 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4909 4909 goto release_minor;
4910 4910 }
4911 4911
4912 4912 if ((rv = kcf_get_hardware_provider_nomech(
4913 4913 CRYPTO_OPS_OFFSET(object_ops),
4914 4914 CRYPTO_OBJECT_OFFSET(object_get_size),
4915 4915 sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
4916 4916 goto release_minor;
4917 4917 }
4918 4918
4919 4919 handle = STRUCT_FGET(object_get_size, gs_handle);
4920 4920 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_GET_SIZE,
4921 4921 sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, &size,
4922 4922 NULL, NULL, 0, NULL);
4923 4923
4924 4924 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4925 4925 KCF_PROV_REFRELE(real_provider);
4926 4926
4927 4927 if (rv == CRYPTO_SUCCESS) {
4928 4928 STRUCT_FSET(object_get_size, gs_size, (ulong_t)size);
4929 4929 }
4930 4930
4931 4931 release_minor:
4932 4932 crypto_release_minor(cm);
4933 4933 CRYPTO_SESSION_RELE(sp);
4934 4934
4935 4935 if (error != 0)
4936 4936 return (error);
4937 4937
4938 4938 STRUCT_FSET(object_get_size, gs_return_value, rv);
4939 4939 if (copyout(STRUCT_BUF(object_get_size), arg,
4940 4940 STRUCT_SIZE(object_get_size)) != 0) {
4941 4941 return (EFAULT);
4942 4942 }
4943 4943 return (0);
4944 4944 }
4945 4945
4946 4946 /* ARGSUSED */
4947 4947 static int
4948 4948 object_set_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
4949 4949 {
4950 4950 STRUCT_DECL(crypto_object_set_attribute_value, set_attribute_value);
4951 4951 kcf_provider_desc_t *real_provider;
4952 4952 kcf_req_params_t params;
4953 4953 crypto_object_attribute_t *k_attrs = NULL;
4954 4954 crypto_session_id_t session_id;
4955 4955 crypto_minor_t *cm;
4956 4956 crypto_session_data_t *sp = NULL;
4957 4957 crypto_object_id_t object_handle;
4958 4958 caddr_t sa_attributes;
4959 4959 size_t k_attrs_size;
4960 4960 size_t rctl_bytes = 0;
4961 4961 boolean_t rctl_chk = B_FALSE;
4962 4962 int error = 0;
4963 4963 int rv;
4964 4964 uint_t count;
4965 4965
4966 4966 STRUCT_INIT(set_attribute_value, mode);
4967 4967
4968 4968 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
4969 4969 cmn_err(CE_WARN,
4970 4970 "object_set_attribute_value: failed holding minor");
4971 4971 return (ENXIO);
4972 4972 }
4973 4973
4974 4974 if (copyin(arg, STRUCT_BUF(set_attribute_value),
4975 4975 STRUCT_SIZE(set_attribute_value)) != 0) {
4976 4976 crypto_release_minor(cm);
4977 4977 return (EFAULT);
4978 4978 }
4979 4979
4980 4980 count = STRUCT_FGET(set_attribute_value, sa_count);
4981 4981 sa_attributes = STRUCT_FGETP(set_attribute_value, sa_attributes);
4982 4982
4983 4983 session_id = STRUCT_FGET(set_attribute_value, sa_session);
4984 4984
4985 4985 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
4986 4986 goto release_minor;
4987 4987 }
4988 4988 if (!copyin_attributes(mode, sp, count, sa_attributes, &k_attrs,
4989 4989 &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
4990 4990 &rctl_chk, B_TRUE)) {
4991 4991 goto release_minor;
4992 4992 }
4993 4993
4994 4994 if ((rv = kcf_get_hardware_provider_nomech(
4995 4995 CRYPTO_OPS_OFFSET(object_ops),
4996 4996 CRYPTO_OBJECT_OFFSET(object_set_attribute_value),
4997 4997 sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
4998 4998 goto release_minor;
4999 4999 }
5000 5000
5001 5001 object_handle = STRUCT_FGET(set_attribute_value, sa_handle);
5002 5002 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_SET_ATTRIBUTE_VALUE,
5003 5003 sp->sd_provider_session->ps_session, object_handle, k_attrs, count,
5004 5004 NULL, 0, NULL, NULL, 0, NULL);
5005 5005
5006 5006 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5007 5007 KCF_PROV_REFRELE(real_provider);
5008 5008
5009 5009 release_minor:
5010 5010 CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
5011 5011 CRYPTO_SESSION_RELE(sp);
5012 5012 crypto_release_minor(cm);
5013 5013
5014 5014 if (k_attrs != NULL)
5015 5015 kmem_free(k_attrs, k_attrs_size);
5016 5016
5017 5017 if (error != 0)
5018 5018 return (error);
5019 5019
5020 5020 STRUCT_FSET(set_attribute_value, sa_return_value, rv);
5021 5021 if (copyout(STRUCT_BUF(set_attribute_value), arg,
5022 5022 STRUCT_SIZE(set_attribute_value)) != 0) {
5023 5023 return (EFAULT);
5024 5024 }
5025 5025 return (0);
5026 5026 }
5027 5027
5028 5028 /* ARGSUSED */
5029 5029 static int
5030 5030 object_find_init(dev_t dev, caddr_t arg, int mode, int *rval)
5031 5031 {
5032 5032 STRUCT_DECL(crypto_object_find_init, find_init);
5033 5033 kcf_provider_desc_t *real_provider = NULL;
5034 5034 kcf_req_params_t params;
5035 5035 crypto_object_attribute_t *k_attrs = NULL;
5036 5036 crypto_session_id_t session_id;
5037 5037 crypto_minor_t *cm;
5038 5038 crypto_session_data_t *sp = NULL;
5039 5039 caddr_t attributes;
5040 5040 size_t k_attrs_size;
5041 5041 size_t rctl_bytes = 0;
5042 5042 boolean_t rctl_chk = B_FALSE;
5043 5043 int error = 0;
5044 5044 int rv;
5045 5045 uint_t count;
5046 5046 void *cookie;
5047 5047
5048 5048 STRUCT_INIT(find_init, mode);
5049 5049
5050 5050 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5051 5051 cmn_err(CE_WARN, "object_find_init: failed holding minor");
5052 5052 return (ENXIO);
5053 5053 }
5054 5054
5055 5055 if (copyin(arg, STRUCT_BUF(find_init), STRUCT_SIZE(find_init)) != 0) {
5056 5056 crypto_release_minor(cm);
5057 5057 return (EFAULT);
5058 5058 }
5059 5059
5060 5060 count = STRUCT_FGET(find_init, fi_count);
5061 5061 attributes = STRUCT_FGETP(find_init, fi_attributes);
5062 5062
5063 5063 session_id = STRUCT_FGET(find_init, fi_session);
5064 5064
5065 5065 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5066 5066 goto release_minor;
5067 5067 }
5068 5068 if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
5069 5069 &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
5070 5070 &rctl_chk, B_TRUE)) {
5071 5071 goto release_minor;
5072 5072 }
5073 5073
5074 5074 if ((rv = kcf_get_hardware_provider_nomech(
5075 5075 CRYPTO_OPS_OFFSET(object_ops),
5076 5076 CRYPTO_OBJECT_OFFSET(object_find_init),
5077 5077 sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
5078 5078 goto release_minor;
5079 5079 }
5080 5080
5081 5081 /* check for an active find */
5082 5082 if (sp->sd_find_init_cookie != NULL) {
5083 5083 rv = CRYPTO_OPERATION_IS_ACTIVE;
5084 5084 goto release_minor;
5085 5085 }
5086 5086
5087 5087 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_FIND_INIT,
5088 5088 sp->sd_provider_session->ps_session, 0, k_attrs, count, NULL, 0,
5089 5089 &cookie, NULL, 0, NULL);
5090 5090
5091 5091 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5092 5092
5093 5093 if (rv == CRYPTO_SUCCESS) {
5094 5094 /*
5095 5095 * The cookie is allocated by a provider at the start of an
5096 5096 * object search. It is freed when the search is terminated
5097 5097 * by a final operation, or when the session is closed.
5098 5098 * It contains state information about which object handles
5099 5099 * have been returned to the caller.
5100 5100 */
5101 5101 sp->sd_find_init_cookie = cookie;
5102 5102 }
5103 5103
5104 5104 release_minor:
5105 5105 CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
5106 5106 CRYPTO_SESSION_RELE(sp);
5107 5107 crypto_release_minor(cm);
5108 5108
5109 5109 if (real_provider != NULL)
5110 5110 KCF_PROV_REFRELE(real_provider);
5111 5111
5112 5112 if (k_attrs != NULL)
5113 5113 kmem_free(k_attrs, k_attrs_size);
5114 5114
5115 5115 if (error != 0)
5116 5116 return (error);
5117 5117
5118 5118 STRUCT_FSET(find_init, fi_return_value, rv);
5119 5119 if (copyout(STRUCT_BUF(find_init), arg, STRUCT_SIZE(find_init)) != 0) {
5120 5120 return (EFAULT);
5121 5121 }
5122 5122 return (0);
5123 5123 }
5124 5124
5125 5125 /* ARGSUSED */
5126 5126 static int
5127 5127 object_find_update(dev_t dev, caddr_t arg, int mode, int *rval)
5128 5128 {
5129 5129 STRUCT_DECL(crypto_object_find_update, find_update);
5130 5130 kcf_provider_desc_t *real_provider;
5131 5131 kcf_req_params_t params;
5132 5132 crypto_minor_t *cm;
5133 5133 crypto_session_data_t *sp = NULL;
5134 5134 crypto_object_id_t *buffer = NULL;
5135 5135 crypto_session_id_t session_id;
5136 5136 size_t len, rctl_bytes = 0;
5137 5137 uint_t count, max_count;
5138 5138 int rv, error = 0;
5139 5139 boolean_t rctl_chk = B_FALSE;
5140 5140
5141 5141 STRUCT_INIT(find_update, mode);
5142 5142
5143 5143 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5144 5144 cmn_err(CE_WARN, "object_find_update: failed holding minor");
5145 5145 return (ENXIO);
5146 5146 }
5147 5147
5148 5148 if (copyin(arg, STRUCT_BUF(find_update),
5149 5149 STRUCT_SIZE(find_update)) != 0) {
5150 5150 crypto_release_minor(cm);
5151 5151 return (EFAULT);
5152 5152 }
5153 5153
5154 5154 max_count = STRUCT_FGET(find_update, fu_max_count);
5155 5155 if (max_count > CRYPTO_MAX_FIND_COUNT) {
5156 5156 cmn_err(CE_NOTE, "object_find_update: count greater than %d, "
5157 5157 "pid = %d", CRYPTO_MAX_FIND_COUNT, curproc->p_pid);
5158 5158 rv = CRYPTO_ARGUMENTS_BAD;
5159 5159 goto release_minor;
5160 5160 }
5161 5161 len = max_count * sizeof (crypto_object_id_t);
5162 5162 session_id = STRUCT_FGET(find_update, fu_session);
5163 5163
5164 5164 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5165 5165 goto release_minor;
5166 5166 }
5167 5167 if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
5168 5168 CRYPTO_SUCCESS) {
5169 5169 goto release_minor;
5170 5170 }
5171 5171 rctl_bytes = len;
5172 5172 buffer = kmem_alloc(len, KM_SLEEP);
5173 5173
5174 5174 if ((rv = kcf_get_hardware_provider_nomech(
5175 5175 CRYPTO_OPS_OFFSET(object_ops),
5176 5176 CRYPTO_OBJECT_OFFSET(object_find), sp->sd_provider,
5177 5177 &real_provider)) != CRYPTO_SUCCESS) {
5178 5178 goto release_minor;
5179 5179 }
5180 5180
5181 5181 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_FIND,
5182 5182 sp->sd_provider_session->ps_session, 0, NULL, 0, buffer, 0,
5183 5183 NULL, sp->sd_find_init_cookie, max_count, &count);
5184 5184
5185 5185 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5186 5186 KCF_PROV_REFRELE(real_provider);
5187 5187
5188 5188 if (rv == CRYPTO_SUCCESS) {
5189 5189 if (count > max_count) {
5190 5190 /* bad bad provider */
5191 5191 rv = CRYPTO_FAILED;
5192 5192 goto release_minor;
5193 5193 }
5194 5194 if (count != 0) {
5195 5195 /* copyout handles */
5196 5196 if (copyout(buffer,
5197 5197 STRUCT_FGETP(find_update, fu_handles),
5198 5198 count * sizeof (crypto_object_id_t)) != 0) {
5199 5199 error = EFAULT;
5200 5200 }
5201 5201 }
5202 5202 STRUCT_FSET(find_update, fu_count, count);
5203 5203 }
5204 5204
5205 5205 release_minor:
5206 5206 CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
5207 5207 CRYPTO_SESSION_RELE(sp);
5208 5208 crypto_release_minor(cm);
5209 5209
5210 5210 if (buffer != NULL)
5211 5211 kmem_free(buffer, len);
5212 5212
5213 5213 if (error != 0)
5214 5214 return (error);
5215 5215
5216 5216 STRUCT_FSET(find_update, fu_return_value, rv);
5217 5217 if (copyout(STRUCT_BUF(find_update), arg,
5218 5218 STRUCT_SIZE(find_update)) != 0) {
5219 5219 return (EFAULT);
5220 5220 }
5221 5221
5222 5222 return (0);
5223 5223 }
5224 5224
5225 5225 /*
5226 5226 * Free provider-allocated storage used for find object searches.
5227 5227 */
5228 5228 static int
5229 5229 crypto_free_find_ctx(crypto_session_data_t *sp)
5230 5230 {
5231 5231 kcf_provider_desc_t *real_provider;
5232 5232 kcf_req_params_t params;
5233 5233 int rv;
5234 5234
5235 5235 if ((rv = kcf_get_hardware_provider_nomech(
5236 5236 CRYPTO_OPS_OFFSET(object_ops),
5237 5237 CRYPTO_OBJECT_OFFSET(object_find_final),
5238 5238 sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
5239 5239 return (rv);
5240 5240 }
5241 5241
5242 5242 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_FIND_FINAL,
5243 5243 sp->sd_provider_session->ps_session, 0, NULL, 0, NULL, 0,
5244 5244 NULL, sp->sd_find_init_cookie, 0, NULL);
5245 5245
5246 5246 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5247 5247 KCF_PROV_REFRELE(real_provider);
5248 5248 return (rv);
5249 5249 }
5250 5250
5251 5251 /* ARGSUSED */
5252 5252 static int
5253 5253 object_find_final(dev_t dev, caddr_t arg, int mode, int *rval)
5254 5254 {
5255 5255 STRUCT_DECL(crypto_object_find_final, object_find_final);
5256 5256 crypto_session_id_t session_id;
5257 5257 crypto_minor_t *cm;
5258 5258 crypto_session_data_t *sp;
5259 5259 int error = 0;
5260 5260 int rv;
5261 5261
5262 5262 STRUCT_INIT(object_find_final, mode);
5263 5263
5264 5264 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5265 5265 cmn_err(CE_WARN, "object_find_final: failed holding minor");
5266 5266 return (ENXIO);
5267 5267 }
5268 5268
5269 5269 if (copyin(arg, STRUCT_BUF(object_find_final),
5270 5270 STRUCT_SIZE(object_find_final)) != 0) {
5271 5271 crypto_release_minor(cm);
5272 5272 return (EFAULT);
5273 5273 }
5274 5274
5275 5275 session_id = STRUCT_FGET(object_find_final, ff_session);
5276 5276
5277 5277 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5278 5278 goto release_minor;
5279 5279 }
5280 5280
5281 5281 if ((rv = crypto_free_find_ctx(sp)) == CRYPTO_SUCCESS) {
5282 5282 sp->sd_find_init_cookie = NULL;
5283 5283 }
5284 5284
5285 5285 CRYPTO_SESSION_RELE(sp);
5286 5286
5287 5287 release_minor:
5288 5288 crypto_release_minor(cm);
5289 5289
5290 5290 if (error != 0)
5291 5291 return (error);
5292 5292
5293 5293 STRUCT_FSET(object_find_final, ff_return_value, rv);
5294 5294
5295 5295 if (copyout(STRUCT_BUF(object_find_final), arg,
5296 5296 STRUCT_SIZE(object_find_final)) != 0) {
5297 5297 return (EFAULT);
5298 5298 }
5299 5299 return (0);
5300 5300 }
5301 5301
5302 5302 /* ARGSUSED */
5303 5303 static int
5304 5304 object_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
5305 5305 {
5306 5306 STRUCT_DECL(crypto_object_generate_key, generate_key);
5307 5307 kcf_provider_desc_t *real_provider = NULL;
5308 5308 kcf_req_params_t params;
5309 5309 crypto_mechanism_t mech;
5310 5310 crypto_object_attribute_t *k_attrs = NULL;
5311 5311 crypto_session_id_t session_id;
5312 5312 crypto_minor_t *cm;
5313 5313 crypto_session_data_t *sp = NULL;
5314 5314 crypto_object_id_t key_handle;
5315 5315 caddr_t attributes;
5316 5316 size_t k_attrs_size;
5317 5317 size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
5318 5318 boolean_t mech_rctl_chk = B_FALSE;
5319 5319 boolean_t key_rctl_chk = B_FALSE;
5320 5320 uint_t count;
5321 5321 int error = 0;
5322 5322 int rv;
5323 5323 boolean_t allocated_by_crypto_module = B_FALSE;
5324 5324
5325 5325 STRUCT_INIT(generate_key, mode);
5326 5326
5327 5327 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5328 5328 cmn_err(CE_WARN, "object_generate_key: failed holding minor");
5329 5329 return (ENXIO);
5330 5330 }
5331 5331
5332 5332 if (copyin(arg, STRUCT_BUF(generate_key),
5333 5333 STRUCT_SIZE(generate_key)) != 0) {
5334 5334 crypto_release_minor(cm);
5335 5335 return (EFAULT);
5336 5336 }
5337 5337
5338 5338 session_id = STRUCT_FGET(generate_key, gk_session);
5339 5339
5340 5340 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5341 5341 goto release_minor;
5342 5342 }
5343 5343
5344 5344 bcopy(STRUCT_FADDR(generate_key, gk_mechanism), &mech.cm_type,
5345 5345 sizeof (crypto_mech_type_t));
5346 5346
5347 5347 if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
5348 5348 CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
5349 5349 &real_provider, CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
5350 5350 goto release_minor;
5351 5351 }
5352 5352
5353 5353 rv = crypto_provider_copyin_mech_param(real_provider,
5354 5354 STRUCT_FADDR(generate_key, gk_mechanism), &mech, mode, &error);
5355 5355
5356 5356 if (rv == CRYPTO_NOT_SUPPORTED) {
5357 5357 allocated_by_crypto_module = B_TRUE;
5358 5358 if (!copyin_mech(mode, sp,
5359 5359 STRUCT_FADDR(generate_key, gk_mechanism),
5360 5360 &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5361 5361 goto release_minor;
5362 5362 }
5363 5363 } else {
5364 5364 if (rv != CRYPTO_SUCCESS)
5365 5365 goto release_minor;
5366 5366 }
5367 5367
5368 5368 count = STRUCT_FGET(generate_key, gk_count);
5369 5369 attributes = STRUCT_FGETP(generate_key, gk_attributes);
5370 5370 if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
5371 5371 &k_attrs_size, NULL, &rv, &error, &key_rctl_bytes,
5372 5372 &key_rctl_chk, B_TRUE)) {
5373 5373 goto release_minor;
5374 5374 }
5375 5375
5376 5376 KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE,
5377 5377 sp->sd_provider_session->ps_session, &mech, k_attrs, count,
5378 5378 &key_handle, NULL, 0, NULL, NULL, NULL, 0);
5379 5379
5380 5380 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5381 5381
5382 5382 if (rv == CRYPTO_SUCCESS)
5383 5383 STRUCT_FSET(generate_key, gk_handle, key_handle);
5384 5384
5385 5385 release_minor:
5386 5386 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
5387 5387 CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
5388 5388
5389 5389 if (k_attrs != NULL)
5390 5390 kmem_free(k_attrs, k_attrs_size);
5391 5391
5392 5392 if (error != 0)
5393 5393 goto out;
5394 5394
5395 5395 STRUCT_FSET(generate_key, gk_return_value, rv);
5396 5396 if (copyout(STRUCT_BUF(generate_key), arg,
5397 5397 STRUCT_SIZE(generate_key)) != 0) {
5398 5398 if (rv == CRYPTO_SUCCESS) {
5399 5399 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
5400 5400 KCF_OP_OBJECT_DESTROY,
5401 5401 sp->sd_provider_session->ps_session, key_handle,
5402 5402 NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
5403 5403
5404 5404 (void) kcf_submit_request(real_provider, NULL,
5405 5405 NULL, ¶ms, B_FALSE);
5406 5406
5407 5407 error = EFAULT;
5408 5408 }
5409 5409 }
5410 5410 out:
5411 5411 CRYPTO_SESSION_RELE(sp);
5412 5412 crypto_release_minor(cm);
5413 5413
5414 5414 if (real_provider != NULL) {
5415 5415 crypto_free_mech(real_provider,
5416 5416 allocated_by_crypto_module, &mech);
5417 5417 KCF_PROV_REFRELE(real_provider);
5418 5418 }
5419 5419 return (error);
5420 5420 }
5421 5421
5422 5422 /* ARGSUSED */
5423 5423 static int
5424 5424 nostore_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
5425 5425 {
5426 5426 STRUCT_DECL(crypto_nostore_generate_key, generate_key);
5427 5427 #ifdef _LP64
5428 5428 STRUCT_DECL(crypto_object_attribute, oa);
5429 5429 #else
5430 5430 /* LINTED E_FUNC_SET_NOT_USED */
5431 5431 STRUCT_DECL(crypto_object_attribute, oa);
5432 5432 #endif
5433 5433 kcf_provider_desc_t *real_provider = NULL;
5434 5434 kcf_req_params_t params;
5435 5435 crypto_mechanism_t mech;
5436 5436 crypto_object_attribute_t *k_in_attrs = NULL;
5437 5437 crypto_object_attribute_t *k_out_attrs = NULL;
5438 5438 crypto_session_id_t session_id;
5439 5439 crypto_minor_t *cm;
5440 5440 crypto_session_data_t *sp = NULL;
5441 5441 caddr_t in_attributes;
5442 5442 caddr_t out_attributes;
5443 5443 size_t k_in_attrs_size;
5444 5444 size_t k_out_attrs_size;
5445 5445 size_t mech_rctl_bytes = 0;
5446 5446 boolean_t mech_rctl_chk = B_FALSE;
5447 5447 size_t in_key_rctl_bytes = 0, out_key_rctl_bytes = 0;
5448 5448 boolean_t in_key_rctl_chk = B_FALSE;
5449 5449 boolean_t out_key_rctl_chk = B_FALSE;
5450 5450 uint_t in_count;
5451 5451 uint_t out_count;
5452 5452 int error = 0;
5453 5453 int rv;
5454 5454 boolean_t allocated_by_crypto_module = B_FALSE;
5455 5455 caddr_t u_attrs = NULL;
5456 5456
5457 5457 STRUCT_INIT(generate_key, mode);
5458 5458 STRUCT_INIT(oa, mode);
5459 5459
5460 5460 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5461 5461 cmn_err(CE_WARN, "nostore_generate_key: failed holding minor");
5462 5462 return (ENXIO);
5463 5463 }
5464 5464
5465 5465 if (copyin(arg, STRUCT_BUF(generate_key),
5466 5466 STRUCT_SIZE(generate_key)) != 0) {
5467 5467 crypto_release_minor(cm);
5468 5468 return (EFAULT);
5469 5469 }
5470 5470
5471 5471 session_id = STRUCT_FGET(generate_key, ngk_session);
5472 5472
5473 5473 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5474 5474 goto release_minor;
5475 5475 }
5476 5476
5477 5477 bcopy(STRUCT_FADDR(generate_key, ngk_mechanism), &mech.cm_type,
5478 5478 sizeof (crypto_mech_type_t));
5479 5479
5480 5480 if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
5481 5481 CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
5482 5482 &real_provider, CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
5483 5483 goto release_minor;
5484 5484 }
5485 5485
5486 5486 rv = crypto_provider_copyin_mech_param(real_provider,
5487 5487 STRUCT_FADDR(generate_key, ngk_mechanism), &mech, mode, &error);
5488 5488
5489 5489 if (rv == CRYPTO_NOT_SUPPORTED) {
5490 5490 allocated_by_crypto_module = B_TRUE;
5491 5491 if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key,
5492 5492 ngk_mechanism), &mech, &mech_rctl_bytes,
5493 5493 &mech_rctl_chk, &rv, &error)) {
5494 5494 goto release_minor;
5495 5495 }
5496 5496 } else {
5497 5497 if (rv != CRYPTO_SUCCESS)
5498 5498 goto release_minor;
5499 5499 }
5500 5500
5501 5501 in_count = STRUCT_FGET(generate_key, ngk_in_count);
5502 5502 in_attributes = STRUCT_FGETP(generate_key, ngk_in_attributes);
5503 5503 if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
5504 5504 &k_in_attrs_size, NULL, &rv, &error, &in_key_rctl_bytes,
5505 5505 &in_key_rctl_chk, B_TRUE)) {
5506 5506 goto release_minor;
5507 5507 }
5508 5508
5509 5509 out_count = STRUCT_FGET(generate_key, ngk_out_count);
5510 5510 out_attributes = STRUCT_FGETP(generate_key, ngk_out_attributes);
5511 5511 if (!copyin_attributes(mode, sp, out_count, out_attributes,
5512 5512 &k_out_attrs,
5513 5513 &k_out_attrs_size, &u_attrs, &rv, &error, &out_key_rctl_bytes,
5514 5514 &out_key_rctl_chk, B_FALSE)) {
5515 5515 goto release_minor;
5516 5516 }
5517 5517
5518 5518 KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE,
5519 5519 sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
5520 5520 NULL, 0, NULL, k_out_attrs, out_count, NULL, 0);
5521 5521
5522 5522 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5523 5523
5524 5524 if (rv == CRYPTO_SUCCESS) {
5525 5525 error = copyout_attributes(mode, out_attributes,
5526 5526 out_count, k_out_attrs, u_attrs);
5527 5527 }
5528 5528 release_minor:
5529 5529 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
5530 5530 CRYPTO_DECREMENT_RCTL_SESSION(sp, in_key_rctl_bytes, in_key_rctl_chk);
5531 5531 CRYPTO_DECREMENT_RCTL_SESSION(sp, out_key_rctl_bytes,
5532 5532 out_key_rctl_chk);
5533 5533
5534 5534 if (k_in_attrs != NULL)
5535 5535 kmem_free(k_in_attrs, k_in_attrs_size);
5536 5536 if (k_out_attrs != NULL) {
5537 5537 bzero(k_out_attrs, k_out_attrs_size);
5538 5538 kmem_free(k_out_attrs, k_out_attrs_size);
5539 5539 }
5540 5540
5541 5541 if (u_attrs != NULL)
5542 5542 kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
5543 5543
5544 5544 if (error != 0)
5545 5545 goto out;
5546 5546
5547 5547 STRUCT_FSET(generate_key, ngk_return_value, rv);
5548 5548 if (copyout(STRUCT_BUF(generate_key), arg,
5549 5549 STRUCT_SIZE(generate_key)) != 0) {
5550 5550 error = EFAULT;
5551 5551 }
5552 5552 out:
5553 5553 CRYPTO_SESSION_RELE(sp);
5554 5554 crypto_release_minor(cm);
5555 5555
5556 5556 if (real_provider != NULL) {
5557 5557 crypto_free_mech(real_provider,
5558 5558 allocated_by_crypto_module, &mech);
5559 5559 KCF_PROV_REFRELE(real_provider);
5560 5560 }
5561 5561 return (error);
5562 5562 }
5563 5563
5564 5564 /* ARGSUSED */
5565 5565 static int
5566 5566 object_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
5567 5567 {
5568 5568 STRUCT_DECL(crypto_object_generate_key_pair, generate_key_pair);
5569 5569 kcf_provider_desc_t *real_provider = NULL;
5570 5570 kcf_req_params_t params;
5571 5571 crypto_mechanism_t mech;
5572 5572 crypto_object_attribute_t *k_pub_attrs = NULL;
5573 5573 crypto_object_attribute_t *k_pri_attrs = NULL;
5574 5574 crypto_session_id_t session_id;
5575 5575 crypto_minor_t *cm;
5576 5576 crypto_session_data_t *sp = NULL;
5577 5577 crypto_object_id_t pub_handle;
5578 5578 crypto_object_id_t pri_handle;
5579 5579 caddr_t pri_attributes;
5580 5580 caddr_t pub_attributes;
5581 5581 size_t k_pub_attrs_size, k_pri_attrs_size;
5582 5582 size_t mech_rctl_bytes = 0;
5583 5583 boolean_t mech_rctl_chk = B_FALSE;
5584 5584 size_t pub_rctl_bytes = 0;
5585 5585 boolean_t pub_rctl_chk = B_FALSE;
5586 5586 size_t pri_rctl_bytes = 0;
5587 5587 boolean_t pri_rctl_chk = B_FALSE;
5588 5588 uint_t pub_count;
5589 5589 uint_t pri_count;
5590 5590 int error = 0;
5591 5591 int rv;
5592 5592 boolean_t allocated_by_crypto_module = B_FALSE;
5593 5593
5594 5594 STRUCT_INIT(generate_key_pair, mode);
5595 5595
5596 5596 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5597 5597 cmn_err(CE_WARN,
5598 5598 "object_generate_key_pair: failed holding minor");
5599 5599 return (ENXIO);
5600 5600 }
5601 5601
5602 5602 if (copyin(arg, STRUCT_BUF(generate_key_pair),
5603 5603 STRUCT_SIZE(generate_key_pair)) != 0) {
5604 5604 crypto_release_minor(cm);
5605 5605 return (EFAULT);
5606 5606 }
5607 5607
5608 5608 session_id = STRUCT_FGET(generate_key_pair, kp_session);
5609 5609
5610 5610 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5611 5611 goto release_minor;
5612 5612 }
5613 5613
5614 5614 bcopy(STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech.cm_type,
5615 5615 sizeof (crypto_mech_type_t));
5616 5616
5617 5617 if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
5618 5618 CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
5619 5619 &real_provider, CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
5620 5620 goto release_minor;
5621 5621 }
5622 5622
5623 5623 rv = crypto_provider_copyin_mech_param(real_provider,
5624 5624 STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech, mode, &error);
5625 5625
5626 5626 if (rv == CRYPTO_NOT_SUPPORTED) {
5627 5627 allocated_by_crypto_module = B_TRUE;
5628 5628 if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
5629 5629 kp_mechanism), &mech, &mech_rctl_bytes,
5630 5630 &mech_rctl_chk, &rv, &error)) {
5631 5631 goto release_minor;
5632 5632 }
5633 5633 } else {
5634 5634 if (rv != CRYPTO_SUCCESS)
5635 5635 goto release_minor;
5636 5636 }
5637 5637
5638 5638 pub_count = STRUCT_FGET(generate_key_pair, kp_public_count);
5639 5639 pri_count = STRUCT_FGET(generate_key_pair, kp_private_count);
5640 5640
5641 5641 pub_attributes = STRUCT_FGETP(generate_key_pair, kp_public_attributes);
5642 5642 if (!copyin_attributes(mode, sp, pub_count, pub_attributes,
5643 5643 &k_pub_attrs, &k_pub_attrs_size, NULL, &rv, &error, &pub_rctl_bytes,
5644 5644 &pub_rctl_chk, B_TRUE)) {
5645 5645 goto release_minor;
5646 5646 }
5647 5647
5648 5648 pri_attributes = STRUCT_FGETP(generate_key_pair, kp_private_attributes);
5649 5649 if (!copyin_attributes(mode, sp, pri_count, pri_attributes,
5650 5650 &k_pri_attrs, &k_pri_attrs_size, NULL, &rv, &error,
5651 5651 &pri_rctl_bytes, &pri_rctl_chk, B_TRUE)) {
5652 5652 goto release_minor;
5653 5653 }
5654 5654
5655 5655 KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE_PAIR,
5656 5656 sp->sd_provider_session->ps_session, &mech, k_pub_attrs,
5657 5657 pub_count, &pub_handle, k_pri_attrs, pri_count, &pri_handle,
5658 5658 NULL, NULL, 0);
5659 5659
5660 5660 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5661 5661
5662 5662 if (rv == CRYPTO_SUCCESS) {
5663 5663 STRUCT_FSET(generate_key_pair, kp_public_handle, pub_handle);
5664 5664 STRUCT_FSET(generate_key_pair, kp_private_handle, pri_handle);
5665 5665 }
5666 5666
5667 5667 release_minor:
5668 5668 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
5669 5669 CRYPTO_DECREMENT_RCTL_SESSION(sp, pub_rctl_bytes, pub_rctl_chk);
5670 5670 CRYPTO_DECREMENT_RCTL_SESSION(sp, pri_rctl_bytes, pri_rctl_chk);
5671 5671
5672 5672 if (k_pub_attrs != NULL)
5673 5673 kmem_free(k_pub_attrs, k_pub_attrs_size);
5674 5674
5675 5675 if (k_pri_attrs != NULL)
5676 5676 kmem_free(k_pri_attrs, k_pri_attrs_size);
5677 5677
5678 5678 if (error != 0)
5679 5679 goto out;
5680 5680
5681 5681 STRUCT_FSET(generate_key_pair, kp_return_value, rv);
5682 5682 if (copyout(STRUCT_BUF(generate_key_pair), arg,
5683 5683 STRUCT_SIZE(generate_key_pair)) != 0) {
5684 5684 if (rv == CRYPTO_SUCCESS) {
5685 5685 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
5686 5686 KCF_OP_OBJECT_DESTROY,
5687 5687 sp->sd_provider_session->ps_session, pub_handle,
5688 5688 NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
5689 5689
5690 5690 (void) kcf_submit_request(real_provider, NULL,
5691 5691 NULL, ¶ms, B_FALSE);
5692 5692
5693 5693 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
5694 5694 KCF_OP_OBJECT_DESTROY,
5695 5695 sp->sd_provider_session->ps_session, pri_handle,
5696 5696 NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
5697 5697
5698 5698 (void) kcf_submit_request(real_provider, NULL,
5699 5699 NULL, ¶ms, B_FALSE);
5700 5700
5701 5701 error = EFAULT;
5702 5702 }
5703 5703 }
5704 5704 out:
5705 5705 CRYPTO_SESSION_RELE(sp);
5706 5706 crypto_release_minor(cm);
5707 5707
5708 5708 if (real_provider != NULL) {
5709 5709 crypto_free_mech(real_provider,
5710 5710 allocated_by_crypto_module, &mech);
5711 5711 KCF_PROV_REFRELE(real_provider);
5712 5712 }
5713 5713 return (error);
5714 5714 }
5715 5715
5716 5716 /* ARGSUSED */
5717 5717 static int
5718 5718 nostore_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
5719 5719 {
5720 5720 STRUCT_DECL(crypto_nostore_generate_key_pair, generate_key_pair);
5721 5721 #ifdef _LP64
5722 5722 STRUCT_DECL(crypto_object_attribute, oa);
5723 5723 #else
5724 5724 /* LINTED E_FUNC_SET_NOT_USED */
5725 5725 STRUCT_DECL(crypto_object_attribute, oa);
5726 5726 #endif
5727 5727 kcf_provider_desc_t *real_provider = NULL;
5728 5728 kcf_req_params_t params;
5729 5729 crypto_mechanism_t mech;
5730 5730 crypto_object_attribute_t *k_in_pub_attrs = NULL;
5731 5731 crypto_object_attribute_t *k_in_pri_attrs = NULL;
5732 5732 crypto_object_attribute_t *k_out_pub_attrs = NULL;
5733 5733 crypto_object_attribute_t *k_out_pri_attrs = NULL;
5734 5734 crypto_session_id_t session_id;
5735 5735 crypto_minor_t *cm;
5736 5736 crypto_session_data_t *sp = NULL;
5737 5737 caddr_t in_pri_attributes;
5738 5738 caddr_t in_pub_attributes;
5739 5739 caddr_t out_pri_attributes;
5740 5740 caddr_t out_pub_attributes;
5741 5741 size_t k_in_pub_attrs_size, k_in_pri_attrs_size;
5742 5742 size_t k_out_pub_attrs_size, k_out_pri_attrs_size;
5743 5743 size_t mech_rctl_bytes = 0;
5744 5744 boolean_t mech_rctl_chk = B_FALSE;
5745 5745 size_t in_pub_rctl_bytes = 0;
5746 5746 boolean_t in_pub_rctl_chk = B_FALSE;
5747 5747 size_t in_pri_rctl_bytes = 0;
5748 5748 boolean_t in_pri_rctl_chk = B_FALSE;
5749 5749 size_t out_pub_rctl_bytes = 0;
5750 5750 boolean_t out_pub_rctl_chk = B_FALSE;
5751 5751 size_t out_pri_rctl_bytes = 0;
5752 5752 boolean_t out_pri_rctl_chk = B_FALSE;
5753 5753 uint_t in_pub_count;
5754 5754 uint_t in_pri_count;
5755 5755 uint_t out_pub_count;
5756 5756 uint_t out_pri_count;
5757 5757 int error = 0;
5758 5758 int rv;
5759 5759 boolean_t allocated_by_crypto_module = B_FALSE;
5760 5760 caddr_t u_pub_attrs = NULL;
5761 5761 caddr_t u_pri_attrs = NULL;
5762 5762
5763 5763 STRUCT_INIT(generate_key_pair, mode);
5764 5764 STRUCT_INIT(oa, mode);
5765 5765
5766 5766 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5767 5767 cmn_err(CE_WARN,
5768 5768 "nostore_generate_key_pair: failed holding minor");
5769 5769 return (ENXIO);
5770 5770 }
5771 5771
5772 5772 if (copyin(arg, STRUCT_BUF(generate_key_pair),
5773 5773 STRUCT_SIZE(generate_key_pair)) != 0) {
5774 5774 crypto_release_minor(cm);
5775 5775 return (EFAULT);
5776 5776 }
5777 5777
5778 5778 session_id = STRUCT_FGET(generate_key_pair, nkp_session);
5779 5779
5780 5780 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5781 5781 goto release_minor;
5782 5782 }
5783 5783
5784 5784 bcopy(STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech.cm_type,
5785 5785 sizeof (crypto_mech_type_t));
5786 5786
5787 5787 if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
5788 5788 CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
5789 5789 &real_provider, CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
5790 5790 goto release_minor;
5791 5791 }
5792 5792
5793 5793 rv = crypto_provider_copyin_mech_param(real_provider,
5794 5794 STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech, mode,
5795 5795 &error);
5796 5796
5797 5797 if (rv == CRYPTO_NOT_SUPPORTED) {
5798 5798 allocated_by_crypto_module = B_TRUE;
5799 5799 if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
5800 5800 nkp_mechanism), &mech, &mech_rctl_bytes,
5801 5801 &mech_rctl_chk, &rv, &error)) {
5802 5802 goto release_minor;
5803 5803 }
5804 5804 } else {
5805 5805 if (rv != CRYPTO_SUCCESS)
5806 5806 goto release_minor;
5807 5807 }
5808 5808
5809 5809 in_pub_count = STRUCT_FGET(generate_key_pair, nkp_in_public_count);
5810 5810 in_pri_count = STRUCT_FGET(generate_key_pair, nkp_in_private_count);
5811 5811
5812 5812 in_pub_attributes = STRUCT_FGETP(generate_key_pair,
5813 5813 nkp_in_public_attributes);
5814 5814 if (!copyin_attributes(mode, sp, in_pub_count, in_pub_attributes,
5815 5815 &k_in_pub_attrs, &k_in_pub_attrs_size, NULL, &rv, &error,
5816 5816 &in_pub_rctl_bytes, &in_pub_rctl_chk, B_TRUE)) {
5817 5817 goto release_minor;
5818 5818 }
5819 5819
5820 5820 in_pri_attributes = STRUCT_FGETP(generate_key_pair,
5821 5821 nkp_in_private_attributes);
5822 5822 if (!copyin_attributes(mode, sp, in_pri_count, in_pri_attributes,
5823 5823 &k_in_pri_attrs, &k_in_pri_attrs_size, NULL, &rv, &error,
5824 5824 &in_pri_rctl_bytes, &in_pri_rctl_chk, B_TRUE)) {
5825 5825 goto release_minor;
5826 5826 }
5827 5827
5828 5828 out_pub_count = STRUCT_FGET(generate_key_pair, nkp_out_public_count);
5829 5829 out_pri_count = STRUCT_FGET(generate_key_pair, nkp_out_private_count);
5830 5830
5831 5831 out_pub_attributes = STRUCT_FGETP(generate_key_pair,
5832 5832 nkp_out_public_attributes);
5833 5833 if (!copyin_attributes(mode, sp, out_pub_count, out_pub_attributes,
5834 5834 &k_out_pub_attrs, &k_out_pub_attrs_size, &u_pub_attrs, &rv, &error,
5835 5835 &out_pub_rctl_bytes, &out_pub_rctl_chk, B_FALSE)) {
5836 5836 goto release_minor;
5837 5837 }
5838 5838
5839 5839 out_pri_attributes = STRUCT_FGETP(generate_key_pair,
5840 5840 nkp_out_private_attributes);
5841 5841 if (!copyin_attributes(mode, sp, out_pri_count, out_pri_attributes,
5842 5842 &k_out_pri_attrs, &k_out_pri_attrs_size, &u_pri_attrs, &rv, &error,
5843 5843 &out_pri_rctl_bytes, &out_pri_rctl_chk, B_FALSE)) {
5844 5844 goto release_minor;
5845 5845 }
5846 5846
5847 5847 KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE_PAIR,
5848 5848 sp->sd_provider_session->ps_session, &mech, k_in_pub_attrs,
5849 5849 in_pub_count, k_in_pri_attrs, in_pri_count, NULL, k_out_pub_attrs,
5850 5850 out_pub_count, k_out_pri_attrs, out_pri_count);
5851 5851
5852 5852 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5853 5853
5854 5854 if (rv == CRYPTO_SUCCESS) {
5855 5855 error = copyout_attributes(mode, out_pub_attributes,
5856 5856 out_pub_count, k_out_pub_attrs, u_pub_attrs);
5857 5857 if (error != CRYPTO_SUCCESS)
5858 5858 goto release_minor;
5859 5859 error = copyout_attributes(mode, out_pri_attributes,
5860 5860 out_pri_count, k_out_pri_attrs, u_pri_attrs);
5861 5861 }
5862 5862
5863 5863 release_minor:
5864 5864 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
5865 5865 CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pub_rctl_bytes, in_pub_rctl_chk);
5866 5866 CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pri_rctl_bytes, in_pri_rctl_chk);
5867 5867 CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pub_rctl_bytes,
5868 5868 out_pub_rctl_chk);
5869 5869 CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pri_rctl_bytes,
5870 5870 out_pri_rctl_chk);
5871 5871
5872 5872 if (k_in_pub_attrs != NULL)
5873 5873 kmem_free(k_in_pub_attrs, k_in_pub_attrs_size);
5874 5874
5875 5875 if (k_in_pri_attrs != NULL)
5876 5876 kmem_free(k_in_pri_attrs, k_in_pri_attrs_size);
5877 5877
5878 5878 if (k_out_pub_attrs != NULL)
5879 5879 kmem_free(k_out_pub_attrs, k_out_pub_attrs_size);
5880 5880
5881 5881 if (k_out_pri_attrs != NULL) {
5882 5882 bzero(k_out_pri_attrs, k_out_pri_attrs_size);
5883 5883 kmem_free(k_out_pri_attrs, k_out_pri_attrs_size);
5884 5884 }
5885 5885
5886 5886 if (u_pub_attrs != NULL)
5887 5887 kmem_free(u_pub_attrs, out_pub_count * STRUCT_SIZE(oa));
5888 5888
5889 5889 if (u_pri_attrs != NULL)
5890 5890 kmem_free(u_pri_attrs, out_pri_count * STRUCT_SIZE(oa));
5891 5891
5892 5892 if (error != 0)
5893 5893 goto out;
5894 5894
5895 5895 STRUCT_FSET(generate_key_pair, nkp_return_value, rv);
5896 5896 if (copyout(STRUCT_BUF(generate_key_pair), arg,
5897 5897 STRUCT_SIZE(generate_key_pair)) != 0) {
5898 5898 error = EFAULT;
5899 5899 }
5900 5900 out:
5901 5901 CRYPTO_SESSION_RELE(sp);
5902 5902 crypto_release_minor(cm);
5903 5903
5904 5904 if (real_provider != NULL) {
5905 5905 crypto_free_mech(real_provider,
5906 5906 allocated_by_crypto_module, &mech);
5907 5907 KCF_PROV_REFRELE(real_provider);
5908 5908 }
5909 5909 return (error);
5910 5910 }
5911 5911
5912 5912 /* ARGSUSED */
5913 5913 static int
5914 5914 object_wrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
5915 5915 {
5916 5916 STRUCT_DECL(crypto_object_wrap_key, wrap_key);
5917 5917 kcf_provider_desc_t *real_provider = NULL;
5918 5918 kcf_req_params_t params;
5919 5919 crypto_mechanism_t mech;
5920 5920 crypto_key_t key;
5921 5921 crypto_session_id_t session_id;
5922 5922 crypto_minor_t *cm;
5923 5923 crypto_session_data_t *sp = NULL;
5924 5924 crypto_object_id_t handle;
5925 5925 size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
5926 5926 boolean_t mech_rctl_chk = B_FALSE;
5927 5927 boolean_t key_rctl_chk = B_FALSE;
5928 5928 size_t wrapped_key_rctl_bytes = 0;
5929 5929 boolean_t wrapped_key_rctl_chk = B_FALSE;
5930 5930 size_t wrapped_key_len, new_wrapped_key_len;
5931 5931 uchar_t *wrapped_key = NULL;
5932 5932 char *wrapped_key_buffer;
5933 5933 int error = 0;
5934 5934 int rv;
5935 5935 boolean_t allocated_by_crypto_module = B_FALSE;
5936 5936
5937 5937 STRUCT_INIT(wrap_key, mode);
5938 5938
5939 5939 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5940 5940 cmn_err(CE_WARN, "object_wrap_key: failed holding minor");
5941 5941 return (ENXIO);
5942 5942 }
5943 5943
5944 5944 if (copyin(arg, STRUCT_BUF(wrap_key), STRUCT_SIZE(wrap_key)) != 0) {
5945 5945 crypto_release_minor(cm);
5946 5946 return (EFAULT);
5947 5947 }
5948 5948
5949 5949 bzero(&key, sizeof (crypto_key_t));
5950 5950
5951 5951 session_id = STRUCT_FGET(wrap_key, wk_session);
5952 5952
5953 5953 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5954 5954 goto out;
5955 5955 }
5956 5956
5957 5957 bcopy(STRUCT_FADDR(wrap_key, wk_mechanism), &mech.cm_type,
5958 5958 sizeof (crypto_mech_type_t));
5959 5959
5960 5960 /* We need the key length for provider selection so copy it in now. */
5961 5961 if (!copyin_key(mode, sp, STRUCT_FADDR(wrap_key, wk_wrapping_key), &key,
5962 5962 &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
5963 5963 goto out;
5964 5964 }
5965 5965
5966 5966 wrapped_key_len = STRUCT_FGET(wrap_key, wk_wrapped_key_len);
5967 5967
5968 5968 if ((rv = kcf_get_hardware_provider(mech.cm_type, &key,
5969 5969 CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
5970 5970 &real_provider, CRYPTO_FG_WRAP)) != CRYPTO_SUCCESS) {
5971 5971 goto out;
5972 5972 }
5973 5973
5974 5974 rv = crypto_provider_copyin_mech_param(real_provider,
5975 5975 STRUCT_FADDR(wrap_key, wk_mechanism), &mech, mode, &error);
5976 5976
5977 5977 if (rv == CRYPTO_NOT_SUPPORTED) {
5978 5978 allocated_by_crypto_module = B_TRUE;
5979 5979 if (!copyin_mech(mode, sp, STRUCT_FADDR(wrap_key, wk_mechanism),
5980 5980 &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5981 5981 goto out;
5982 5982 }
5983 5983 } else {
5984 5984 if (rv != CRYPTO_SUCCESS)
5985 5985 goto out;
5986 5986 }
5987 5987
5988 5988 /*
5989 5989 * Don't allocate output buffer unless both buffer pointer and
5990 5990 * buffer length are not NULL or 0 (length).
5991 5991 */
5992 5992 wrapped_key_buffer = STRUCT_FGETP(wrap_key, wk_wrapped_key);
5993 5993 if (wrapped_key_buffer == NULL || wrapped_key_len == 0) {
5994 5994 wrapped_key_len = 0;
5995 5995 }
5996 5996
5997 5997 if (wrapped_key_len > crypto_max_buffer_len) {
5998 5998 cmn_err(CE_NOTE, "object_wrap_key: buffer greater than %ld "
5999 5999 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
6000 6000 rv = CRYPTO_ARGUMENTS_BAD;
6001 6001 goto out;
6002 6002 }
6003 6003
6004 6004 if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
6005 6005 wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
6006 6006 goto out;
6007 6007 }
6008 6008
6009 6009 /* new_wrapped_key_len can be modified by the provider */
6010 6010 wrapped_key_rctl_bytes = new_wrapped_key_len = wrapped_key_len;
6011 6011 wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
6012 6012
6013 6013 handle = STRUCT_FGET(wrap_key, wk_object_handle);
6014 6014 KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_WRAP,
6015 6015 sp->sd_provider_session->ps_session, &mech, NULL, 0, &handle,
6016 6016 NULL, 0, NULL, &key, wrapped_key, &new_wrapped_key_len);
6017 6017
6018 6018 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
6019 6019
6020 6020 if (rv == CRYPTO_SUCCESS) {
6021 6021 if (wrapped_key_len != 0 && copyout(wrapped_key,
6022 6022 wrapped_key_buffer, new_wrapped_key_len) != 0) {
6023 6023 error = EFAULT;
6024 6024 }
6025 6025 STRUCT_FSET(wrap_key, wk_wrapped_key_len,
6026 6026 (ulong_t)new_wrapped_key_len);
6027 6027 }
6028 6028
6029 6029 if (rv == CRYPTO_BUFFER_TOO_SMALL) {
6030 6030 /*
6031 6031 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
6032 6032 * of section 11.2 of the pkcs11 spec. We catch it here and
6033 6033 * provide the correct pkcs11 return value.
6034 6034 */
6035 6035 if (STRUCT_FGETP(wrap_key, wk_wrapped_key) == NULL)
6036 6036 rv = CRYPTO_SUCCESS;
6037 6037 STRUCT_FSET(wrap_key, wk_wrapped_key_len,
6038 6038 (ulong_t)new_wrapped_key_len);
6039 6039 }
6040 6040
6041 6041 out:
6042 6042 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
6043 6043 CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
6044 6044 CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
6045 6045 wrapped_key_rctl_chk);
6046 6046 CRYPTO_SESSION_RELE(sp);
6047 6047
6048 6048 crypto_release_minor(cm);
6049 6049
6050 6050 if (real_provider != NULL) {
6051 6051 crypto_free_mech(real_provider,
6052 6052 allocated_by_crypto_module, &mech);
6053 6053 KCF_PROV_REFRELE(real_provider);
6054 6054 }
6055 6055
6056 6056 if (wrapped_key != NULL)
6057 6057 kmem_free(wrapped_key, wrapped_key_len);
6058 6058
6059 6059 free_crypto_key(&key);
6060 6060
6061 6061 if (error != 0)
6062 6062 return (error);
6063 6063
6064 6064 STRUCT_FSET(wrap_key, wk_return_value, rv);
6065 6065 if (copyout(STRUCT_BUF(wrap_key), arg, STRUCT_SIZE(wrap_key)) != 0) {
6066 6066 return (EFAULT);
6067 6067 }
6068 6068 return (0);
6069 6069 }
6070 6070
6071 6071 /* ARGSUSED */
6072 6072 static int
6073 6073 object_unwrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
6074 6074 {
6075 6075 STRUCT_DECL(crypto_object_unwrap_key, unwrap_key);
6076 6076 kcf_provider_desc_t *real_provider = NULL;
6077 6077 kcf_req_params_t params;
6078 6078 crypto_mechanism_t mech;
6079 6079 crypto_key_t unwrapping_key;
6080 6080 crypto_session_id_t session_id;
6081 6081 crypto_minor_t *cm;
6082 6082 crypto_session_data_t *sp = NULL;
6083 6083 crypto_object_id_t handle;
6084 6084 crypto_object_attribute_t *k_attrs = NULL;
6085 6085 size_t k_attrs_size;
6086 6086 size_t mech_rctl_bytes = 0, unwrapping_key_rctl_bytes = 0;
6087 6087 boolean_t mech_rctl_chk = B_FALSE;
6088 6088 boolean_t unwrapping_key_rctl_chk = B_FALSE;
6089 6089 size_t wrapped_key_rctl_bytes = 0, k_attrs_rctl_bytes = 0;
6090 6090 boolean_t wrapped_key_rctl_chk = B_FALSE;
6091 6091 boolean_t k_attrs_rctl_chk = B_FALSE;
6092 6092 size_t wrapped_key_len;
6093 6093 uchar_t *wrapped_key = NULL;
6094 6094 int error = 0;
6095 6095 int rv;
6096 6096 uint_t count;
6097 6097 caddr_t uk_attributes;
6098 6098 boolean_t allocated_by_crypto_module = B_FALSE;
6099 6099
6100 6100 STRUCT_INIT(unwrap_key, mode);
6101 6101
6102 6102 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
6103 6103 cmn_err(CE_WARN, "object_unwrap_key: failed holding minor");
6104 6104 return (ENXIO);
6105 6105 }
6106 6106
6107 6107 if (copyin(arg, STRUCT_BUF(unwrap_key), STRUCT_SIZE(unwrap_key)) != 0) {
6108 6108 crypto_release_minor(cm);
6109 6109 return (EFAULT);
6110 6110 }
6111 6111
6112 6112 bzero(&unwrapping_key, sizeof (unwrapping_key));
6113 6113
6114 6114 session_id = STRUCT_FGET(unwrap_key, uk_session);
6115 6115
6116 6116 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
6117 6117 goto release_minor;
6118 6118 }
6119 6119
6120 6120 bcopy(STRUCT_FADDR(unwrap_key, uk_mechanism), &mech.cm_type,
6121 6121 sizeof (crypto_mech_type_t));
6122 6122
6123 6123 /* We need the key length for provider selection so copy it in now. */
6124 6124 if (!copyin_key(mode, sp, STRUCT_FADDR(unwrap_key, uk_unwrapping_key),
6125 6125 &unwrapping_key, &unwrapping_key_rctl_bytes,
6126 6126 &unwrapping_key_rctl_chk, &rv, &error)) {
6127 6127 goto release_minor;
6128 6128 }
6129 6129
6130 6130 if ((rv = kcf_get_hardware_provider(mech.cm_type, &unwrapping_key,
6131 6131 CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
6132 6132 &real_provider, CRYPTO_FG_UNWRAP)) != CRYPTO_SUCCESS) {
6133 6133 goto release_minor;
6134 6134 }
6135 6135
6136 6136 rv = crypto_provider_copyin_mech_param(real_provider,
6137 6137 STRUCT_FADDR(unwrap_key, uk_mechanism), &mech, mode, &error);
6138 6138
6139 6139 if (rv == CRYPTO_NOT_SUPPORTED) {
6140 6140 allocated_by_crypto_module = B_TRUE;
6141 6141 if (!copyin_mech(mode, sp,
6142 6142 STRUCT_FADDR(unwrap_key, uk_mechanism),
6143 6143 &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6144 6144 goto release_minor;
6145 6145 }
6146 6146 } else {
6147 6147 if (rv != CRYPTO_SUCCESS)
6148 6148 goto release_minor;
6149 6149 }
6150 6150
6151 6151 count = STRUCT_FGET(unwrap_key, uk_count);
6152 6152 uk_attributes = STRUCT_FGETP(unwrap_key, uk_attributes);
6153 6153 if (!copyin_attributes(mode, sp, count, uk_attributes, &k_attrs,
6154 6154 &k_attrs_size, NULL, &rv, &error, &k_attrs_rctl_bytes,
6155 6155 &k_attrs_rctl_chk, B_TRUE)) {
6156 6156 goto release_minor;
6157 6157 }
6158 6158
6159 6159 wrapped_key_len = STRUCT_FGET(unwrap_key, uk_wrapped_key_len);
6160 6160 if (wrapped_key_len > crypto_max_buffer_len) {
6161 6161 cmn_err(CE_NOTE, "object_unwrap_key: buffer greater than %ld "
6162 6162 "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
6163 6163 rv = CRYPTO_ARGUMENTS_BAD;
6164 6164 goto release_minor;
6165 6165 }
6166 6166
6167 6167 if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
6168 6168 wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
6169 6169 goto release_minor;
6170 6170 }
6171 6171 wrapped_key_rctl_bytes = wrapped_key_len;
6172 6172 wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
6173 6173
6174 6174 if (wrapped_key_len != 0 && copyin(STRUCT_FGETP(unwrap_key,
6175 6175 uk_wrapped_key), wrapped_key, wrapped_key_len) != 0) {
6176 6176 error = EFAULT;
6177 6177 goto release_minor;
6178 6178 }
6179 6179
6180 6180 /* wrapped_key_len is not modified by the unwrap operation */
6181 6181 KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_UNWRAP,
6182 6182 sp->sd_provider_session->ps_session, &mech, k_attrs, count, &handle,
6183 6183 NULL, 0, NULL, &unwrapping_key, wrapped_key, &wrapped_key_len);
6184 6184
6185 6185 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
6186 6186
6187 6187 if (rv == CRYPTO_SUCCESS)
6188 6188 STRUCT_FSET(unwrap_key, uk_object_handle, handle);
6189 6189
6190 6190 release_minor:
6191 6191 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
6192 6192 CRYPTO_DECREMENT_RCTL_SESSION(sp, unwrapping_key_rctl_bytes,
6193 6193 unwrapping_key_rctl_chk);
6194 6194 CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
6195 6195 wrapped_key_rctl_chk);
6196 6196 CRYPTO_DECREMENT_RCTL_SESSION(sp, k_attrs_rctl_bytes,
6197 6197 k_attrs_rctl_chk);
6198 6198
6199 6199 if (k_attrs != NULL)
6200 6200 kmem_free(k_attrs, k_attrs_size);
6201 6201
6202 6202 if (wrapped_key != NULL)
6203 6203 kmem_free(wrapped_key, wrapped_key_len);
6204 6204
6205 6205 free_crypto_key(&unwrapping_key);
6206 6206
6207 6207 if (error != 0)
6208 6208 goto out;
6209 6209
6210 6210 STRUCT_FSET(unwrap_key, uk_return_value, rv);
6211 6211 if (copyout(STRUCT_BUF(unwrap_key), arg,
6212 6212 STRUCT_SIZE(unwrap_key)) != 0) {
6213 6213 if (rv == CRYPTO_SUCCESS) {
6214 6214 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
6215 6215 KCF_OP_OBJECT_DESTROY,
6216 6216 sp->sd_provider_session->ps_session, handle,
6217 6217 NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
6218 6218
6219 6219 (void) kcf_submit_request(real_provider, NULL,
6220 6220 NULL, ¶ms, B_FALSE);
6221 6221
6222 6222 error = EFAULT;
6223 6223 }
6224 6224 }
6225 6225 out:
6226 6226 CRYPTO_SESSION_RELE(sp);
6227 6227 crypto_release_minor(cm);
6228 6228
6229 6229 if (real_provider != NULL) {
6230 6230 crypto_free_mech(real_provider,
6231 6231 allocated_by_crypto_module, &mech);
6232 6232 KCF_PROV_REFRELE(real_provider);
6233 6233 }
6234 6234
6235 6235 return (error);
6236 6236 }
6237 6237
6238 6238 /* ARGSUSED */
6239 6239 static int
6240 6240 object_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
6241 6241 {
6242 6242 STRUCT_DECL(crypto_derive_key, derive_key);
6243 6243 kcf_provider_desc_t *real_provider = NULL;
6244 6244 kcf_req_params_t params;
6245 6245 crypto_object_attribute_t *k_attrs = NULL;
6246 6246 crypto_mechanism_t mech;
6247 6247 crypto_key_t base_key;
6248 6248 crypto_session_id_t session_id;
6249 6249 crypto_minor_t *cm;
6250 6250 crypto_session_data_t *sp = NULL;
6251 6251 crypto_object_id_t handle;
6252 6252 size_t k_attrs_size;
6253 6253 size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
6254 6254 boolean_t mech_rctl_chk = B_FALSE;
6255 6255 boolean_t key_rctl_chk = B_FALSE;
6256 6256 size_t attributes_rctl_bytes = 0;
6257 6257 boolean_t attributes_rctl_chk = B_FALSE;
6258 6258 caddr_t attributes;
6259 6259 uint_t count;
6260 6260 int error = 0;
6261 6261 int rv;
6262 6262 boolean_t allocated_by_crypto_module = B_FALSE;
6263 6263 boolean_t please_destroy_object = B_FALSE;
6264 6264
6265 6265 STRUCT_INIT(derive_key, mode);
6266 6266
6267 6267 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
6268 6268 cmn_err(CE_WARN, "object_derive_key: failed holding minor");
6269 6269 return (ENXIO);
6270 6270 }
6271 6271
6272 6272 if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
6273 6273 crypto_release_minor(cm);
6274 6274 return (EFAULT);
6275 6275 }
6276 6276
6277 6277 bzero(&base_key, sizeof (base_key));
6278 6278
6279 6279 session_id = STRUCT_FGET(derive_key, dk_session);
6280 6280
6281 6281 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
6282 6282 goto release_minor;
6283 6283 }
6284 6284
6285 6285 bcopy(STRUCT_FADDR(derive_key, dk_mechanism), &mech.cm_type,
6286 6286 sizeof (crypto_mech_type_t));
6287 6287
6288 6288 /* We need the key length for provider selection so copy it in now. */
6289 6289 if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, dk_base_key),
6290 6290 &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
6291 6291 goto release_minor;
6292 6292 }
6293 6293
6294 6294 if ((rv = kcf_get_hardware_provider(mech.cm_type, &base_key,
6295 6295 CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
6296 6296 &real_provider, CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
6297 6297 goto release_minor;
6298 6298 }
6299 6299
6300 6300 rv = crypto_provider_copyin_mech_param(real_provider,
6301 6301 STRUCT_FADDR(derive_key, dk_mechanism), &mech, mode, &error);
6302 6302
6303 6303 if (rv == CRYPTO_NOT_SUPPORTED) {
6304 6304 allocated_by_crypto_module = B_TRUE;
6305 6305 if (!copyin_mech(mode, sp,
6306 6306 STRUCT_FADDR(derive_key, dk_mechanism),
6307 6307 &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6308 6308 goto release_minor;
6309 6309 }
6310 6310 } else {
6311 6311 if (rv != CRYPTO_SUCCESS)
6312 6312 goto release_minor;
6313 6313 }
6314 6314
6315 6315 count = STRUCT_FGET(derive_key, dk_count);
6316 6316
6317 6317 attributes = STRUCT_FGETP(derive_key, dk_attributes);
6318 6318 if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
6319 6319 &k_attrs_size, NULL, &rv, &error,
6320 6320 &attributes_rctl_bytes, &attributes_rctl_chk, B_TRUE)) {
6321 6321 goto release_minor;
6322 6322 }
6323 6323
6324 6324 KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_DERIVE,
6325 6325 sp->sd_provider_session->ps_session, &mech, k_attrs, count,
6326 6326 &handle, NULL, 0, NULL, &base_key, NULL, NULL);
6327 6327
6328 6328 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
6329 6329
6330 6330 if (rv == CRYPTO_SUCCESS) {
6331 6331 STRUCT_FSET(derive_key, dk_object_handle, handle);
6332 6332
6333 6333 rv = crypto_provider_copyout_mech_param(real_provider,
6334 6334 &mech, STRUCT_FADDR(derive_key, dk_mechanism),
6335 6335 mode, &error);
6336 6336
6337 6337 if (rv == CRYPTO_NOT_SUPPORTED) {
6338 6338 rv = CRYPTO_SUCCESS;
6339 6339 goto release_minor;
6340 6340 }
6341 6341
6342 6342 if (rv != CRYPTO_SUCCESS)
6343 6343 please_destroy_object = B_TRUE;
6344 6344 }
6345 6345
6346 6346 release_minor:
6347 6347 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
6348 6348 CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
6349 6349 CRYPTO_DECREMENT_RCTL_SESSION(sp, attributes_rctl_bytes,
6350 6350 attributes_rctl_chk);
6351 6351
6352 6352 if (k_attrs != NULL)
6353 6353 kmem_free(k_attrs, k_attrs_size);
6354 6354
6355 6355 free_crypto_key(&base_key);
6356 6356
6357 6357 if (error != 0)
6358 6358 goto out;
6359 6359
6360 6360 STRUCT_FSET(derive_key, dk_return_value, rv);
6361 6361 if (copyout(STRUCT_BUF(derive_key), arg,
6362 6362 STRUCT_SIZE(derive_key)) != 0) {
6363 6363 if (rv == CRYPTO_SUCCESS) {
6364 6364 please_destroy_object = B_TRUE;
6365 6365 error = EFAULT;
6366 6366 }
6367 6367 }
6368 6368 out:
6369 6369 if (please_destroy_object) {
6370 6370 KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_DESTROY,
6371 6371 sp->sd_provider_session->ps_session, handle,
6372 6372 NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
6373 6373
6374 6374 (void) kcf_submit_request(real_provider, NULL,
6375 6375 NULL, ¶ms, B_FALSE);
6376 6376 }
6377 6377
6378 6378 CRYPTO_SESSION_RELE(sp);
6379 6379 crypto_release_minor(cm);
6380 6380
6381 6381 if (real_provider != NULL) {
6382 6382 crypto_free_mech(real_provider,
6383 6383 allocated_by_crypto_module, &mech);
6384 6384 KCF_PROV_REFRELE(real_provider);
6385 6385 }
6386 6386 return (error);
6387 6387 }
6388 6388
6389 6389 /* ARGSUSED */
6390 6390 static int
6391 6391 nostore_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
6392 6392 {
6393 6393 STRUCT_DECL(crypto_nostore_derive_key, derive_key);
6394 6394 #ifdef _LP64
6395 6395 STRUCT_DECL(crypto_object_attribute, oa);
6396 6396 #else
6397 6397 /* LINTED E_FUNC_SET_NOT_USED */
6398 6398 STRUCT_DECL(crypto_object_attribute, oa);
6399 6399 #endif
6400 6400 kcf_provider_desc_t *real_provider = NULL;
6401 6401 kcf_req_params_t params;
6402 6402 crypto_object_attribute_t *k_in_attrs = NULL;
6403 6403 crypto_object_attribute_t *k_out_attrs = NULL;
6404 6404 crypto_mechanism_t mech;
6405 6405 crypto_key_t base_key;
6406 6406 crypto_session_id_t session_id;
6407 6407 crypto_minor_t *cm;
6408 6408 crypto_session_data_t *sp = NULL;
6409 6409 size_t k_in_attrs_size, k_out_attrs_size;
6410 6410 size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
6411 6411 boolean_t mech_rctl_chk = B_FALSE;
6412 6412 boolean_t key_rctl_chk = B_FALSE;
6413 6413 size_t in_attributes_rctl_bytes = 0;
6414 6414 size_t out_attributes_rctl_bytes = 0;
6415 6415 boolean_t in_attributes_rctl_chk = B_FALSE;
6416 6416 boolean_t out_attributes_rctl_chk = B_FALSE;
6417 6417 caddr_t in_attributes, out_attributes;
6418 6418 uint_t in_count, out_count;
6419 6419 int error = 0;
6420 6420 int rv;
6421 6421 boolean_t allocated_by_crypto_module = B_FALSE;
6422 6422 caddr_t u_attrs = NULL;
6423 6423
6424 6424 STRUCT_INIT(derive_key, mode);
6425 6425 STRUCT_INIT(oa, mode);
6426 6426
6427 6427 if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
6428 6428 cmn_err(CE_WARN, "nostore_derive_key: failed holding minor");
6429 6429 return (ENXIO);
6430 6430 }
6431 6431
6432 6432 if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
6433 6433 crypto_release_minor(cm);
6434 6434 return (EFAULT);
6435 6435 }
6436 6436
6437 6437 bzero(&base_key, sizeof (base_key));
6438 6438
6439 6439 session_id = STRUCT_FGET(derive_key, ndk_session);
6440 6440
6441 6441 if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
6442 6442 goto release_minor;
6443 6443 }
6444 6444
6445 6445 bcopy(STRUCT_FADDR(derive_key, ndk_mechanism), &mech.cm_type,
6446 6446 sizeof (crypto_mech_type_t));
6447 6447
6448 6448 /* We need the key length for provider selection so copy it in now. */
6449 6449 if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, ndk_base_key),
6450 6450 &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
6451 6451 goto release_minor;
6452 6452 }
6453 6453
6454 6454 if ((rv = kcf_get_hardware_provider(mech.cm_type, &base_key,
6455 6455 CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
6456 6456 &real_provider, CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
6457 6457 goto release_minor;
6458 6458 }
6459 6459
6460 6460 rv = crypto_provider_copyin_mech_param(real_provider,
6461 6461 STRUCT_FADDR(derive_key, ndk_mechanism), &mech, mode, &error);
6462 6462
6463 6463 if (rv == CRYPTO_NOT_SUPPORTED) {
6464 6464 allocated_by_crypto_module = B_TRUE;
6465 6465 if (!copyin_mech(mode, sp,
6466 6466 STRUCT_FADDR(derive_key, ndk_mechanism),
6467 6467 &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6468 6468 goto release_minor;
6469 6469 }
6470 6470 } else {
6471 6471 if (rv != CRYPTO_SUCCESS)
6472 6472 goto release_minor;
6473 6473 }
6474 6474
6475 6475 in_count = STRUCT_FGET(derive_key, ndk_in_count);
6476 6476 out_count = STRUCT_FGET(derive_key, ndk_out_count);
6477 6477
6478 6478 in_attributes = STRUCT_FGETP(derive_key, ndk_in_attributes);
6479 6479 if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
6480 6480 &k_in_attrs_size, NULL, &rv, &error, &in_attributes_rctl_bytes,
6481 6481 &in_attributes_rctl_chk, B_TRUE)) {
6482 6482 goto release_minor;
6483 6483 }
6484 6484
6485 6485 out_attributes = STRUCT_FGETP(derive_key, ndk_out_attributes);
6486 6486 if (!copyin_attributes(mode, sp, out_count, out_attributes,
6487 6487 &k_out_attrs, &k_out_attrs_size, &u_attrs, &rv, &error,
6488 6488 &out_attributes_rctl_bytes,
6489 6489 &out_attributes_rctl_chk, B_FALSE)) {
6490 6490 goto release_minor;
6491 6491 }
6492 6492
6493 6493 KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_DERIVE,
6494 6494 sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
6495 6495 NULL, 0, &base_key, k_out_attrs, out_count, NULL, 0);
6496 6496
6497 6497 rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
6498 6498
6499 6499 if (rv == CRYPTO_SUCCESS) {
6500 6500 rv = crypto_provider_copyout_mech_param(real_provider,
6501 6501 &mech, STRUCT_FADDR(derive_key, ndk_mechanism),
6502 6502 mode, &error);
6503 6503
6504 6504 if (rv == CRYPTO_NOT_SUPPORTED) {
6505 6505 rv = CRYPTO_SUCCESS;
6506 6506 }
6507 6507 /* copyout the derived secret */
6508 6508 if (copyout_attributes(mode, out_attributes, out_count,
6509 6509 k_out_attrs, u_attrs) != 0)
6510 6510 error = EFAULT;
6511 6511 }
6512 6512
6513 6513 release_minor:
6514 6514 CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
6515 6515 CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
6516 6516 CRYPTO_DECREMENT_RCTL_SESSION(sp, in_attributes_rctl_bytes,
6517 6517 in_attributes_rctl_chk);
6518 6518 CRYPTO_DECREMENT_RCTL_SESSION(sp, out_attributes_rctl_bytes,
6519 6519 out_attributes_rctl_chk);
6520 6520
6521 6521 if (k_in_attrs != NULL)
6522 6522 kmem_free(k_in_attrs, k_in_attrs_size);
6523 6523 if (k_out_attrs != NULL) {
6524 6524 bzero(k_out_attrs, k_out_attrs_size);
6525 6525 kmem_free(k_out_attrs, k_out_attrs_size);
6526 6526 }
6527 6527
6528 6528 if (u_attrs != NULL)
6529 6529 kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
6530 6530
6531 6531 free_crypto_key(&base_key);
6532 6532
6533 6533 if (error != 0)
6534 6534 goto out;
6535 6535
6536 6536 STRUCT_FSET(derive_key, ndk_return_value, rv);
6537 6537 if (copyout(STRUCT_BUF(derive_key), arg,
6538 6538 STRUCT_SIZE(derive_key)) != 0) {
6539 6539 error = EFAULT;
6540 6540 }
6541 6541 out:
6542 6542 CRYPTO_SESSION_RELE(sp);
6543 6543 crypto_release_minor(cm);
6544 6544
6545 6545 if (real_provider != NULL) {
6546 6546 crypto_free_mech(real_provider,
6547 6547 allocated_by_crypto_module, &mech);
6548 6548 KCF_PROV_REFRELE(real_provider);
6549 6549 }
6550 6550 return (error);
6551 6551 }
6552 6552
6553 6553 /* ARGSUSED */
6554 6554 static int
6555 6555 crypto_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *c,
6556 6556 int *rval)
6557 6557 {
6558 6558 #define ARG ((caddr_t)arg)
6559 6559
6560 6560 switch (cmd) {
6561 6561 case CRYPTO_GET_FUNCTION_LIST:
6562 6562 return (get_function_list(dev, ARG, mode, rval));
6563 6563
6564 6564 case CRYPTO_GET_MECHANISM_NUMBER:
6565 6565 return (get_mechanism_number(dev, ARG, mode, rval));
6566 6566
6567 6567 case CRYPTO_GET_MECHANISM_LIST:
6568 6568 return (get_mechanism_list(dev, ARG, mode, rval));
6569 6569
6570 6570 case CRYPTO_GET_ALL_MECHANISM_INFO:
6571 6571 return (get_all_mechanism_info(dev, ARG, mode, rval));
6572 6572
6573 6573 case CRYPTO_GET_PROVIDER_LIST:
6574 6574 return (get_provider_list(dev, ARG, mode, rval));
6575 6575
6576 6576 case CRYPTO_GET_PROVIDER_INFO:
6577 6577 return (get_provider_info(dev, ARG, mode, rval));
6578 6578
6579 6579 case CRYPTO_GET_PROVIDER_MECHANISMS:
6580 6580 return (get_provider_mechanisms(dev, ARG, mode, rval));
6581 6581
6582 6582 case CRYPTO_GET_PROVIDER_MECHANISM_INFO:
6583 6583 return (get_provider_mechanism_info(dev, ARG, mode, rval));
6584 6584
6585 6585 case CRYPTO_OPEN_SESSION:
6586 6586 return (open_session(dev, ARG, mode, rval));
6587 6587
6588 6588 case CRYPTO_CLOSE_SESSION:
6589 6589 return (close_session(dev, ARG, mode, rval));
6590 6590
6591 6591 case CRYPTO_ENCRYPT_INIT:
6592 6592 return (encrypt_init(dev, ARG, mode, rval));
6593 6593
6594 6594 case CRYPTO_DECRYPT_INIT:
6595 6595 return (decrypt_init(dev, ARG, mode, rval));
6596 6596
6597 6597 case CRYPTO_ENCRYPT:
6598 6598 return (encrypt(dev, ARG, mode, rval));
6599 6599
6600 6600 case CRYPTO_DECRYPT:
6601 6601 return (decrypt(dev, ARG, mode, rval));
6602 6602
6603 6603 case CRYPTO_ENCRYPT_UPDATE:
6604 6604 return (encrypt_update(dev, ARG, mode, rval));
6605 6605
6606 6606 case CRYPTO_DECRYPT_UPDATE:
6607 6607 return (decrypt_update(dev, ARG, mode, rval));
6608 6608
6609 6609 case CRYPTO_ENCRYPT_FINAL:
6610 6610 return (encrypt_final(dev, ARG, mode, rval));
6611 6611
6612 6612 case CRYPTO_DECRYPT_FINAL:
6613 6613 return (decrypt_final(dev, ARG, mode, rval));
6614 6614
6615 6615 case CRYPTO_DIGEST_INIT:
6616 6616 return (digest_init(dev, ARG, mode, rval));
6617 6617
6618 6618 case CRYPTO_DIGEST:
6619 6619 return (digest(dev, ARG, mode, rval));
6620 6620
6621 6621 case CRYPTO_DIGEST_UPDATE:
6622 6622 return (digest_update(dev, ARG, mode, rval));
6623 6623
6624 6624 case CRYPTO_DIGEST_KEY:
6625 6625 return (digest_key(dev, ARG, mode, rval));
6626 6626
6627 6627 case CRYPTO_DIGEST_FINAL:
6628 6628 return (digest_final(dev, ARG, mode, rval));
6629 6629
6630 6630 case CRYPTO_SIGN_INIT:
6631 6631 return (sign_init(dev, ARG, mode, rval));
6632 6632
6633 6633 case CRYPTO_SIGN:
6634 6634 return (sign(dev, ARG, mode, rval));
6635 6635
6636 6636 case CRYPTO_SIGN_UPDATE:
6637 6637 return (sign_update(dev, ARG, mode, rval));
6638 6638
6639 6639 case CRYPTO_SIGN_FINAL:
6640 6640 return (sign_final(dev, ARG, mode, rval));
6641 6641
6642 6642 case CRYPTO_SIGN_RECOVER_INIT:
6643 6643 return (sign_recover_init(dev, ARG, mode, rval));
6644 6644
6645 6645 case CRYPTO_SIGN_RECOVER:
6646 6646 return (sign_recover(dev, ARG, mode, rval));
6647 6647
6648 6648 case CRYPTO_VERIFY_INIT:
6649 6649 return (verify_init(dev, ARG, mode, rval));
6650 6650
6651 6651 case CRYPTO_VERIFY:
6652 6652 return (verify(dev, ARG, mode, rval));
6653 6653
6654 6654 case CRYPTO_VERIFY_UPDATE:
6655 6655 return (verify_update(dev, ARG, mode, rval));
6656 6656
6657 6657 case CRYPTO_VERIFY_FINAL:
6658 6658 return (verify_final(dev, ARG, mode, rval));
6659 6659
6660 6660 case CRYPTO_VERIFY_RECOVER_INIT:
6661 6661 return (verify_recover_init(dev, ARG, mode, rval));
6662 6662
6663 6663 case CRYPTO_VERIFY_RECOVER:
6664 6664 return (verify_recover(dev, ARG, mode, rval));
6665 6665
6666 6666 case CRYPTO_SET_PIN:
6667 6667 return (set_pin(dev, ARG, mode, rval));
6668 6668
6669 6669 case CRYPTO_LOGIN:
6670 6670 return (login(dev, ARG, mode, rval));
6671 6671
6672 6672 case CRYPTO_LOGOUT:
6673 6673 return (logout(dev, ARG, mode, rval));
6674 6674
6675 6675 case CRYPTO_SEED_RANDOM:
6676 6676 return (seed_random(dev, ARG, mode, rval));
6677 6677
6678 6678 case CRYPTO_GENERATE_RANDOM:
6679 6679 return (generate_random(dev, ARG, mode, rval));
6680 6680
6681 6681 case CRYPTO_OBJECT_CREATE:
6682 6682 return (object_create(dev, ARG, mode, rval));
6683 6683
6684 6684 case CRYPTO_OBJECT_COPY:
6685 6685 return (object_copy(dev, ARG, mode, rval));
6686 6686
6687 6687 case CRYPTO_OBJECT_DESTROY:
6688 6688 return (object_destroy(dev, ARG, mode, rval));
6689 6689
6690 6690 case CRYPTO_OBJECT_GET_ATTRIBUTE_VALUE:
6691 6691 return (object_get_attribute_value(dev, ARG, mode, rval));
6692 6692
6693 6693 case CRYPTO_OBJECT_GET_SIZE:
6694 6694 return (object_get_size(dev, ARG, mode, rval));
6695 6695
6696 6696 case CRYPTO_OBJECT_SET_ATTRIBUTE_VALUE:
6697 6697 return (object_set_attribute_value(dev, ARG, mode, rval));
6698 6698
6699 6699 case CRYPTO_OBJECT_FIND_INIT:
6700 6700 return (object_find_init(dev, ARG, mode, rval));
6701 6701
6702 6702 case CRYPTO_OBJECT_FIND_UPDATE:
6703 6703 return (object_find_update(dev, ARG, mode, rval));
6704 6704
6705 6705 case CRYPTO_OBJECT_FIND_FINAL:
6706 6706 return (object_find_final(dev, ARG, mode, rval));
6707 6707
6708 6708 case CRYPTO_GENERATE_KEY:
6709 6709 return (object_generate_key(dev, ARG, mode, rval));
6710 6710
6711 6711 case CRYPTO_GENERATE_KEY_PAIR:
6712 6712 return (object_generate_key_pair(dev, ARG, mode, rval));
6713 6713
6714 6714 case CRYPTO_WRAP_KEY:
6715 6715 return (object_wrap_key(dev, ARG, mode, rval));
6716 6716
6717 6717 case CRYPTO_UNWRAP_KEY:
6718 6718 return (object_unwrap_key(dev, ARG, mode, rval));
6719 6719
6720 6720 case CRYPTO_DERIVE_KEY:
6721 6721 return (object_derive_key(dev, ARG, mode, rval));
6722 6722
6723 6723 case CRYPTO_NOSTORE_GENERATE_KEY:
6724 6724 return (nostore_generate_key(dev, ARG, mode, rval));
6725 6725
6726 6726 case CRYPTO_NOSTORE_GENERATE_KEY_PAIR:
6727 6727 return (nostore_generate_key_pair(dev, ARG, mode, rval));
6728 6728
6729 6729 case CRYPTO_NOSTORE_DERIVE_KEY:
6730 6730 return (nostore_derive_key(dev, ARG, mode, rval));
6731 6731 }
6732 6732 return (EINVAL);
6733 6733 }
6734 6734
6735 6735 /*
6736 6736 * Check for the project.max-crypto-memory resource control.
6737 6737 */
6738 6738 static int
6739 6739 crypto_buffer_check(size_t need)
6740 6740 {
6741 6741 kproject_t *kpj;
6742 6742
6743 6743 if (need == 0)
6744 6744 return (CRYPTO_SUCCESS);
6745 6745
6746 6746 mutex_enter(&curproc->p_lock);
6747 6747 kpj = curproc->p_task->tk_proj;
6748 6748 mutex_enter(&(kpj->kpj_data.kpd_crypto_lock));
6749 6749
6750 6750 if (kpj->kpj_data.kpd_crypto_mem + need >
6751 6751 kpj->kpj_data.kpd_crypto_mem_ctl) {
6752 6752 if (rctl_test(rc_project_crypto_mem,
6753 6753 kpj->kpj_rctls, curproc, need, 0) & RCT_DENY) {
6754 6754 mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
6755 6755 mutex_exit(&curproc->p_lock);
6756 6756 return (CRYPTO_HOST_MEMORY);
6757 6757 }
6758 6758 }
6759 6759
6760 6760 kpj->kpj_data.kpd_crypto_mem += need;
6761 6761 mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
6762 6762
6763 6763 curproc->p_crypto_mem += need;
6764 6764 mutex_exit(&curproc->p_lock);
6765 6765
6766 6766 return (CRYPTO_SUCCESS);
6767 6767 }
↓ open down ↓ |
6450 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX