8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / cmd-crypto / cryptoadm / adm_kef_ioctl.c
bloba65316abbc9e20c73ec5be9c93e842f8bb8e65ac
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <strings.h>
32 #include <unistd.h>
33 #include <locale.h>
34 #include <libgen.h>
35 #include <sys/types.h>
36 #include <sys/varargs.h>
37 #include <zone.h>
38 #include <sys/crypto/ioctladmin.h>
39 #include "cryptoadm.h"
41 #define DEFAULT_DEV_NUM 5
42 #define DEFAULT_SOFT_NUM 10
44 static crypto_get_soft_info_t *setup_get_soft_info(char *, int);
47 * Prepare the argument for the LOAD_SOFT_CONFIG ioctl call for the
48 * provider pointed by pent. Return NULL if out of memory.
50 crypto_load_soft_config_t *
51 setup_soft_conf(entry_t *pent)
53 crypto_load_soft_config_t *pload_soft_conf;
54 mechlist_t *plist;
55 uint_t sup_count;
56 size_t extra_mech_size = 0;
57 int i;
59 if (pent == NULL) {
60 return (NULL);
63 sup_count = pent->sup_count;
64 if (sup_count > 1) {
65 extra_mech_size = sizeof (crypto_mech_name_t) *
66 (sup_count - 1);
69 pload_soft_conf = malloc(sizeof (crypto_load_soft_config_t) +
70 extra_mech_size);
71 if (pload_soft_conf == NULL) {
72 cryptodebug("out of memory.");
73 return (NULL);
76 (void) strlcpy(pload_soft_conf->sc_name, pent->name, MAXNAMELEN);
77 pload_soft_conf->sc_count = sup_count;
79 i = 0;
80 plist = pent->suplist;
81 while (i < sup_count) {
82 (void) strlcpy(pload_soft_conf->sc_list[i++],
83 plist->name, CRYPTO_MAX_MECH_NAME);
84 plist = plist->next;
87 return (pload_soft_conf);
92 * Prepare the argument for the LOAD_SOFT_DISABLED ioctl call for the
93 * provider pointed by pent. Return NULL if out of memory.
95 crypto_load_soft_disabled_t *
96 setup_soft_dis(entry_t *pent)
98 crypto_load_soft_disabled_t *pload_soft_dis = NULL;
99 mechlist_t *plist = NULL;
100 size_t extra_mech_size = 0;
101 uint_t dis_count;
102 int i;
104 if (pent == NULL) {
105 return (NULL);
108 dis_count = pent->dis_count;
109 if (dis_count > 1) {
110 extra_mech_size = sizeof (crypto_mech_name_t) *
111 (dis_count - 1);
114 pload_soft_dis = malloc(sizeof (crypto_load_soft_disabled_t) +
115 extra_mech_size);
116 if (pload_soft_dis == NULL) {
117 cryptodebug("out of memory.");
118 return (NULL);
121 (void) strlcpy(pload_soft_dis->sd_name, pent->name, MAXNAMELEN);
122 pload_soft_dis->sd_count = dis_count;
124 i = 0;
125 plist = pent->dislist;
126 while (i < dis_count) {
127 (void) strlcpy(pload_soft_dis->sd_list[i++],
128 plist->name, CRYPTO_MAX_MECH_NAME);
129 plist = plist->next;
132 return (pload_soft_dis);
137 * Prepare the argument for the LOAD_DEV_DISABLED ioctl call for the
138 * provider pointed by pent. Return NULL if out of memory.
140 crypto_load_dev_disabled_t *
141 setup_dev_dis(entry_t *pent)
143 crypto_load_dev_disabled_t *pload_dev_dis = NULL;
144 mechlist_t *plist = NULL;
145 size_t extra_mech_size = 0;
146 uint_t dis_count;
147 int i;
148 char pname[MAXNAMELEN];
149 int inst_num;
151 if (pent == NULL) {
152 return (NULL);
155 /* get the device name and the instance number */
156 if (split_hw_provname(pent->name, pname, &inst_num) == FAILURE) {
157 return (NULL);
160 /* allocate space for pload_dev_des */
161 dis_count = pent->dis_count;
162 if (dis_count > 1) {
163 extra_mech_size = sizeof (crypto_mech_name_t) *
164 (dis_count - 1);
167 pload_dev_dis = malloc(sizeof (crypto_load_dev_disabled_t) +
168 extra_mech_size);
169 if (pload_dev_dis == NULL) {
170 cryptodebug("out of memory.");
171 return (NULL);
174 /* set the values for pload_dev_dis */
175 (void) strlcpy(pload_dev_dis->dd_dev_name, pname, MAXNAMELEN);
176 pload_dev_dis->dd_dev_instance = inst_num;
177 pload_dev_dis->dd_count = dis_count;
179 i = 0;
180 plist = pent->dislist;
181 while (i < dis_count) {
182 (void) strlcpy(pload_dev_dis->dd_list[i++],
183 plist->name, CRYPTO_MAX_MECH_NAME);
184 plist = plist->next;
187 return (pload_dev_dis);
192 * Prepare the calling argument of the UNLOAD_SOFT_MODULE ioctl call for the
193 * provider pointed by pent. Return NULL if out of memory.
195 crypto_unload_soft_module_t *
196 setup_unload_soft(entry_t *pent)
198 crypto_unload_soft_module_t *punload_soft;
200 if (pent == NULL) {
201 return (NULL);
204 punload_soft = malloc(sizeof (crypto_unload_soft_module_t));
205 if (punload_soft == NULL) {
206 cryptodebug("out of memory.");
207 return (NULL);
210 (void) strlcpy(punload_soft->sm_name, pent->name, MAXNAMELEN);
212 return (punload_soft);
217 * Prepare the calling argument for the GET_SOFT_INFO call for the provider
218 * with the number of mechanisms specified in the second argument.
220 * Called by get_soft_info().
222 static crypto_get_soft_info_t *
223 setup_get_soft_info(char *provname, int count)
225 crypto_get_soft_info_t *psoft_info;
226 size_t extra_mech_size = 0;
228 if (provname == NULL) {
229 return (NULL);
232 if (count > 1) {
233 extra_mech_size = sizeof (crypto_mech_name_t) * (count - 1);
236 psoft_info = malloc(sizeof (crypto_get_soft_info_t) + extra_mech_size);
237 if (psoft_info == NULL) {
238 cryptodebug("out of memory.");
239 return (NULL);
242 (void) strlcpy(psoft_info->si_name, provname, MAXNAMELEN);
243 psoft_info->si_count = count;
245 return (psoft_info);
250 * Get the device list from kernel.
253 get_dev_list(crypto_get_dev_list_t **ppdevlist)
255 crypto_get_dev_list_t *pdevlist;
256 int fd = -1;
257 int count = DEFAULT_DEV_NUM;
259 pdevlist = malloc(sizeof (crypto_get_dev_list_t) +
260 sizeof (crypto_dev_list_entry_t) * (count - 1));
261 if (pdevlist == NULL) {
262 cryptodebug("out of memory.");
263 return (FAILURE);
266 if ((fd = open(ADMIN_IOCTL_DEVICE, O_RDONLY)) == -1) {
267 cryptoerror(LOG_STDERR, gettext("failed to open %s: %s"),
268 ADMIN_IOCTL_DEVICE, strerror(errno));
269 return (FAILURE);
272 pdevlist->dl_dev_count = count;
273 if (ioctl(fd, CRYPTO_GET_DEV_LIST, pdevlist) == -1) {
274 cryptodebug("CRYPTO_GET_DEV_LIST ioctl failed: %s",
275 strerror(errno));
276 free(pdevlist);
277 (void) close(fd);
278 return (FAILURE);
281 /* BUFFER is too small, get the number of devices and retry it. */
282 if (pdevlist->dl_return_value == CRYPTO_BUFFER_TOO_SMALL) {
283 count = pdevlist->dl_dev_count;
284 free(pdevlist);
285 pdevlist = malloc(sizeof (crypto_get_dev_list_t) +
286 sizeof (crypto_dev_list_entry_t) * (count - 1));
287 if (pdevlist == NULL) {
288 cryptodebug("out of memory.");
289 (void) close(fd);
290 return (FAILURE);
293 if (ioctl(fd, CRYPTO_GET_DEV_LIST, pdevlist) == -1) {
294 cryptodebug("CRYPTO_GET_DEV_LIST ioctl failed: %s",
295 strerror(errno));
296 free(pdevlist);
297 (void) close(fd);
298 return (FAILURE);
302 if (pdevlist->dl_return_value != CRYPTO_SUCCESS) {
303 cryptodebug("CRYPTO_GET_DEV_LIST ioctl failed, "
304 "return_value = %d", pdevlist->dl_return_value);
305 free(pdevlist);
306 (void) close(fd);
307 return (FAILURE);
310 *ppdevlist = pdevlist;
311 (void) close(fd);
312 return (SUCCESS);
317 * Get all the mechanisms supported by the hardware provider.
318 * The result will be stored in the second argument.
321 get_dev_info(char *devname, int inst_num, int count, mechlist_t **ppmechlist)
323 crypto_get_dev_info_t *dev_info;
324 mechlist_t *phead;
325 mechlist_t *pcur;
326 mechlist_t *pmech;
327 int fd = -1;
328 int i;
329 int rc;
331 if (devname == NULL || count < 1) {
332 cryptodebug("get_dev_info(): devname is NULL or bogus count");
333 return (FAILURE);
336 /* Set up the argument for the CRYPTO_GET_DEV_INFO ioctl call */
337 dev_info = malloc(sizeof (crypto_get_dev_info_t) +
338 sizeof (crypto_mech_name_t) * (count - 1));
339 if (dev_info == NULL) {
340 cryptodebug("out of memory.");
341 return (FAILURE);
343 (void) strlcpy(dev_info->di_dev_name, devname, MAXNAMELEN);
344 dev_info->di_dev_instance = inst_num;
345 dev_info->di_count = count;
347 /* Open the ioctl device */
348 if ((fd = open(ADMIN_IOCTL_DEVICE, O_RDONLY)) == -1) {
349 cryptoerror(LOG_STDERR, gettext("failed to open %s: %s"),
350 ADMIN_IOCTL_DEVICE, strerror(errno));
351 free(dev_info);
352 return (FAILURE);
355 if (ioctl(fd, CRYPTO_GET_DEV_INFO, dev_info) == -1) {
356 cryptodebug("CRYPTO_GET_DEV_INFO ioctl failed: %s",
357 strerror(errno));
358 free(dev_info);
359 (void) close(fd);
360 return (FAILURE);
363 if (dev_info->di_return_value != CRYPTO_SUCCESS) {
364 cryptodebug("CRYPTO_GET_DEV_INFO ioctl failed, "
365 "return_value = %d", dev_info->di_return_value);
366 free(dev_info);
367 (void) close(fd);
368 return (FAILURE);
371 phead = pcur = NULL;
372 rc = SUCCESS;
373 for (i = 0; i < dev_info->di_count; i++) {
374 pmech = create_mech(&dev_info->di_list[i][0]);
375 if (pmech == NULL) {
376 rc = FAILURE;
377 break;
378 } else {
379 if (phead == NULL) {
380 phead = pcur = pmech;
381 } else {
382 pcur->next = pmech;
383 pcur = pmech;
388 if (rc == SUCCESS) {
389 *ppmechlist = phead;
390 } else {
391 free_mechlist(phead);
394 free(dev_info);
395 (void) close(fd);
396 return (rc);
401 * Get the supported mechanism list of the software provider from kernel.
403 * Parameters phardlist and psoftlist are supplied by get_kcfconf_info().
404 * If NULL, this function calls get_kcfconf_info() internally.
407 get_soft_info(char *provname, mechlist_t **ppmechlist,
408 entrylist_t *phardlist, entrylist_t *psoftlist)
410 boolean_t in_kernel = B_FALSE;
411 crypto_get_soft_info_t *psoft_info;
412 mechlist_t *phead;
413 mechlist_t *pmech;
414 mechlist_t *pcur;
415 entry_t *pent = NULL;
416 int count;
417 int fd = -1;
418 int rc;
419 int i;
421 if (provname == NULL) {
422 return (FAILURE);
425 if (getzoneid() == GLOBAL_ZONEID) {
426 /* use kcf.conf for kernel software providers in global zone */
427 if ((pent = getent_kef(provname, phardlist, psoftlist)) ==
428 NULL) {
430 /* No kcf.conf entry for this provider */
431 if (check_kernel_for_soft(provname, NULL, &in_kernel)
432 == FAILURE) {
433 return (FAILURE);
434 } else if (in_kernel == B_FALSE) {
435 cryptoerror(LOG_STDERR,
436 gettext("%s does not exist."), provname);
437 return (FAILURE);
441 * Set mech count to 1. It will be reset to the
442 * correct value later if the setup buffer is too small.
444 count = 1;
445 } else {
446 count = pent->sup_count;
447 free_entry(pent);
449 } else {
451 * kcf.conf not there in non-global zone: set mech count to 1.
452 * It will be reset to the correct value later if the setup
453 * buffer is too small.
455 count = 1;
458 if ((psoft_info = setup_get_soft_info(provname, count)) == NULL) {
459 return (FAILURE);
462 if ((fd = open(ADMIN_IOCTL_DEVICE, O_RDONLY)) == -1) {
463 cryptoerror(LOG_STDERR, gettext("failed to open %s: %s"),
464 ADMIN_IOCTL_DEVICE, strerror(errno));
465 free(psoft_info);
466 return (FAILURE);
469 /* make GET_SOFT_INFO ioctl call */
470 if ((rc = ioctl(fd, CRYPTO_GET_SOFT_INFO, psoft_info)) == -1) {
471 cryptodebug("CRYPTO_GET_SOFT_INFO ioctl failed: %s",
472 strerror(errno));
473 (void) close(fd);
474 free(psoft_info);
475 return (FAILURE);
478 /* BUFFER is too small, get the number of mechanisms and retry it. */
479 if (psoft_info->si_return_value == CRYPTO_BUFFER_TOO_SMALL) {
480 count = psoft_info->si_count;
481 free(psoft_info);
482 if ((psoft_info = setup_get_soft_info(provname, count))
483 == NULL) {
484 (void) close(fd);
485 return (FAILURE);
486 } else {
487 rc = ioctl(fd, CRYPTO_GET_SOFT_INFO, psoft_info);
488 if (rc == -1) {
489 cryptodebug("CRYPTO_GET_SOFT_INFO ioctl "
490 "failed: %s", strerror(errno));
491 (void) close(fd);
492 free(psoft_info);
493 return (FAILURE);
498 (void) close(fd);
499 if (psoft_info->si_return_value != CRYPTO_SUCCESS) {
500 cryptodebug("CRYPTO_GET_SOFT_INFO ioctl failed, "
501 "return_value = %d", psoft_info->si_return_value);
502 free(psoft_info);
503 return (FAILURE);
507 /* Build the mechanism linked list and return it */
508 rc = SUCCESS;
509 phead = pcur = NULL;
510 for (i = 0; i < psoft_info->si_count; i++) {
511 pmech = create_mech(&psoft_info->si_list[i][0]);
512 if (pmech == NULL) {
513 rc = FAILURE;
514 break;
515 } else {
516 if (phead == NULL) {
517 phead = pcur = pmech;
518 } else {
519 pcur->next = pmech;
520 pcur = pmech;
525 if (rc == FAILURE) {
526 free_mechlist(phead);
527 } else {
528 *ppmechlist = phead;
531 free(psoft_info);
532 return (rc);
537 * Get the kernel software provider list from kernel.
540 get_soft_list(crypto_get_soft_list_t **ppsoftlist)
542 crypto_get_soft_list_t *psoftlist = NULL;
543 int count = DEFAULT_SOFT_NUM;
544 int len;
545 int fd = -1;
547 if ((fd = open(ADMIN_IOCTL_DEVICE, O_RDONLY)) == -1) {
548 cryptoerror(LOG_STDERR, gettext("failed to open %s: %s"),
549 ADMIN_IOCTL_DEVICE, strerror(errno));
550 return (FAILURE);
553 len = MAXNAMELEN * count;
554 psoftlist = malloc(sizeof (crypto_get_soft_list_t) + len);
555 if (psoftlist == NULL) {
556 cryptodebug("out of memory.");
557 (void) close(fd);
558 return (FAILURE);
560 psoftlist->sl_soft_names = (caddr_t)(psoftlist + 1);
561 psoftlist->sl_soft_count = count;
562 psoftlist->sl_soft_len = len;
564 if (ioctl(fd, CRYPTO_GET_SOFT_LIST, psoftlist) == -1) {
565 cryptodebug("CRYPTO_GET_SOFT_LIST ioctl failed: %s",
566 strerror(errno));
567 free(psoftlist);
568 (void) close(fd);
569 return (FAILURE);
573 * if BUFFER is too small, get the number of software providers and
574 * the minimum length needed for names and length and retry it.
576 if (psoftlist->sl_return_value == CRYPTO_BUFFER_TOO_SMALL) {
577 count = psoftlist->sl_soft_count;
578 len = psoftlist->sl_soft_len;
579 free(psoftlist);
580 psoftlist = malloc(sizeof (crypto_get_soft_list_t) + len);
581 if (psoftlist == NULL) {
582 cryptodebug("out of memory.");
583 (void) close(fd);
584 return (FAILURE);
586 psoftlist->sl_soft_names = (caddr_t)(psoftlist + 1);
587 psoftlist->sl_soft_count = count;
588 psoftlist->sl_soft_len = len;
590 if (ioctl(fd, CRYPTO_GET_SOFT_LIST, psoftlist) == -1) {
591 cryptodebug("CRYPTO_GET_SOFT_LIST ioctl failed:"
592 "%s", strerror(errno));
593 free(psoftlist);
594 (void) close(fd);
595 return (FAILURE);
599 if (psoftlist->sl_return_value != CRYPTO_SUCCESS) {
600 cryptodebug("CRYPTO_GET_SOFT_LIST ioctl failed, "
601 "return_value = %d", psoftlist->sl_return_value);
602 free(psoftlist);
603 (void) close(fd);
604 return (FAILURE);
607 *ppsoftlist = psoftlist;
608 (void) close(fd);
609 return (SUCCESS);