17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51b22764fSDaniel OpenSolaris Anderson * Common Development and Distribution License (the "License").
61b22764fSDaniel OpenSolaris Anderson * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
2232e0ab73SMisaki Miyashita * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate */
246ea3c060SGarrett D'Amore /*
256ea3c060SGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
26*9d1587b4SJohn Levon * Copyright (c) 2018, Joyent, Inc.
276ea3c060SGarrett D'Amore */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <locale.h>
357c478bd9Sstevel@tonic-gate #include <libgen.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
3732e0ab73SMisaki Miyashita #include <sys/varargs.h>
387c478bd9Sstevel@tonic-gate #include <zone.h>
397c478bd9Sstevel@tonic-gate #include <sys/crypto/ioctladmin.h>
407c478bd9Sstevel@tonic-gate #include "cryptoadm.h"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_NUM 5
437c478bd9Sstevel@tonic-gate #define DEFAULT_SOFT_NUM 10
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static crypto_get_soft_info_t *setup_get_soft_info(char *, int);
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate * Prepare the argument for the LOAD_SOFT_CONFIG ioctl call for the
497c478bd9Sstevel@tonic-gate * provider pointed by pent. Return NULL if out of memory.
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate crypto_load_soft_config_t *
setup_soft_conf(entry_t * pent)527c478bd9Sstevel@tonic-gate setup_soft_conf(entry_t *pent)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate crypto_load_soft_config_t *pload_soft_conf;
557c478bd9Sstevel@tonic-gate mechlist_t *plist;
561b22764fSDaniel OpenSolaris Anderson uint_t sup_count;
571b22764fSDaniel OpenSolaris Anderson size_t extra_mech_size = 0;
581b22764fSDaniel OpenSolaris Anderson int i;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate if (pent == NULL) {
617c478bd9Sstevel@tonic-gate return (NULL);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate sup_count = pent->sup_count;
657c478bd9Sstevel@tonic-gate if (sup_count > 1) {
667c478bd9Sstevel@tonic-gate extra_mech_size = sizeof (crypto_mech_name_t) *
677c478bd9Sstevel@tonic-gate (sup_count - 1);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate pload_soft_conf = malloc(sizeof (crypto_load_soft_config_t) +
717c478bd9Sstevel@tonic-gate extra_mech_size);
727c478bd9Sstevel@tonic-gate if (pload_soft_conf == NULL) {
737c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
747c478bd9Sstevel@tonic-gate return (NULL);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate (void) strlcpy(pload_soft_conf->sc_name, pent->name, MAXNAMELEN);
787c478bd9Sstevel@tonic-gate pload_soft_conf->sc_count = sup_count;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate i = 0;
817c478bd9Sstevel@tonic-gate plist = pent->suplist;
827c478bd9Sstevel@tonic-gate while (i < sup_count) {
837c478bd9Sstevel@tonic-gate (void) strlcpy(pload_soft_conf->sc_list[i++],
847c478bd9Sstevel@tonic-gate plist->name, CRYPTO_MAX_MECH_NAME);
857c478bd9Sstevel@tonic-gate plist = plist->next;
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate return (pload_soft_conf);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * Prepare the argument for the LOAD_SOFT_DISABLED ioctl call for the
947c478bd9Sstevel@tonic-gate * provider pointed by pent. Return NULL if out of memory.
957c478bd9Sstevel@tonic-gate */
967c478bd9Sstevel@tonic-gate crypto_load_soft_disabled_t *
setup_soft_dis(entry_t * pent)977c478bd9Sstevel@tonic-gate setup_soft_dis(entry_t *pent)
987c478bd9Sstevel@tonic-gate {
991b22764fSDaniel OpenSolaris Anderson crypto_load_soft_disabled_t *pload_soft_dis = NULL;
1001b22764fSDaniel OpenSolaris Anderson mechlist_t *plist = NULL;
1011b22764fSDaniel OpenSolaris Anderson size_t extra_mech_size = 0;
1021b22764fSDaniel OpenSolaris Anderson uint_t dis_count;
1031b22764fSDaniel OpenSolaris Anderson int i;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if (pent == NULL) {
1067c478bd9Sstevel@tonic-gate return (NULL);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate dis_count = pent->dis_count;
1107c478bd9Sstevel@tonic-gate if (dis_count > 1) {
1117c478bd9Sstevel@tonic-gate extra_mech_size = sizeof (crypto_mech_name_t) *
1127c478bd9Sstevel@tonic-gate (dis_count - 1);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate pload_soft_dis = malloc(sizeof (crypto_load_soft_disabled_t) +
1167c478bd9Sstevel@tonic-gate extra_mech_size);
1177c478bd9Sstevel@tonic-gate if (pload_soft_dis == NULL) {
1187c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
1197c478bd9Sstevel@tonic-gate return (NULL);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate (void) strlcpy(pload_soft_dis->sd_name, pent->name, MAXNAMELEN);
1237c478bd9Sstevel@tonic-gate pload_soft_dis->sd_count = dis_count;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate i = 0;
1267c478bd9Sstevel@tonic-gate plist = pent->dislist;
1277c478bd9Sstevel@tonic-gate while (i < dis_count) {
1287c478bd9Sstevel@tonic-gate (void) strlcpy(pload_soft_dis->sd_list[i++],
1297c478bd9Sstevel@tonic-gate plist->name, CRYPTO_MAX_MECH_NAME);
1307c478bd9Sstevel@tonic-gate plist = plist->next;
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate return (pload_soft_dis);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate * Prepare the argument for the LOAD_DEV_DISABLED ioctl call for the
1397c478bd9Sstevel@tonic-gate * provider pointed by pent. Return NULL if out of memory.
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate crypto_load_dev_disabled_t *
setup_dev_dis(entry_t * pent)1427c478bd9Sstevel@tonic-gate setup_dev_dis(entry_t *pent)
1437c478bd9Sstevel@tonic-gate {
1441b22764fSDaniel OpenSolaris Anderson crypto_load_dev_disabled_t *pload_dev_dis = NULL;
1451b22764fSDaniel OpenSolaris Anderson mechlist_t *plist = NULL;
1461b22764fSDaniel OpenSolaris Anderson size_t extra_mech_size = 0;
1471b22764fSDaniel OpenSolaris Anderson uint_t dis_count;
1481b22764fSDaniel OpenSolaris Anderson int i;
1491b22764fSDaniel OpenSolaris Anderson char pname[MAXNAMELEN];
1501b22764fSDaniel OpenSolaris Anderson int inst_num;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (pent == NULL) {
1537c478bd9Sstevel@tonic-gate return (NULL);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /* get the device name and the instance number */
1577c478bd9Sstevel@tonic-gate if (split_hw_provname(pent->name, pname, &inst_num) == FAILURE) {
1587c478bd9Sstevel@tonic-gate return (NULL);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate /* allocate space for pload_dev_des */
1627c478bd9Sstevel@tonic-gate dis_count = pent->dis_count;
1637c478bd9Sstevel@tonic-gate if (dis_count > 1) {
1647c478bd9Sstevel@tonic-gate extra_mech_size = sizeof (crypto_mech_name_t) *
1657c478bd9Sstevel@tonic-gate (dis_count - 1);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate pload_dev_dis = malloc(sizeof (crypto_load_dev_disabled_t) +
1697c478bd9Sstevel@tonic-gate extra_mech_size);
1707c478bd9Sstevel@tonic-gate if (pload_dev_dis == NULL) {
1717c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
1727c478bd9Sstevel@tonic-gate return (NULL);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate /* set the values for pload_dev_dis */
1767c478bd9Sstevel@tonic-gate (void) strlcpy(pload_dev_dis->dd_dev_name, pname, MAXNAMELEN);
1777c478bd9Sstevel@tonic-gate pload_dev_dis->dd_dev_instance = inst_num;
1787c478bd9Sstevel@tonic-gate pload_dev_dis->dd_count = dis_count;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate i = 0;
1817c478bd9Sstevel@tonic-gate plist = pent->dislist;
1827c478bd9Sstevel@tonic-gate while (i < dis_count) {
1837c478bd9Sstevel@tonic-gate (void) strlcpy(pload_dev_dis->dd_list[i++],
1847c478bd9Sstevel@tonic-gate plist->name, CRYPTO_MAX_MECH_NAME);
1857c478bd9Sstevel@tonic-gate plist = plist->next;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate return (pload_dev_dis);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate * Prepare the calling argument of the UNLOAD_SOFT_MODULE ioctl call for the
1947c478bd9Sstevel@tonic-gate * provider pointed by pent. Return NULL if out of memory.
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate crypto_unload_soft_module_t *
setup_unload_soft(entry_t * pent)1977c478bd9Sstevel@tonic-gate setup_unload_soft(entry_t *pent)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate crypto_unload_soft_module_t *punload_soft;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate if (pent == NULL) {
2027c478bd9Sstevel@tonic-gate return (NULL);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate punload_soft = malloc(sizeof (crypto_unload_soft_module_t));
2067c478bd9Sstevel@tonic-gate if (punload_soft == NULL) {
2077c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
2087c478bd9Sstevel@tonic-gate return (NULL);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate (void) strlcpy(punload_soft->sm_name, pent->name, MAXNAMELEN);
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate return (punload_soft);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * Prepare the calling argument for the GET_SOFT_INFO call for the provider
2197c478bd9Sstevel@tonic-gate * with the number of mechanisms specified in the second argument.
2201b22764fSDaniel OpenSolaris Anderson *
2211b22764fSDaniel OpenSolaris Anderson * Called by get_soft_info().
2227c478bd9Sstevel@tonic-gate */
2237c478bd9Sstevel@tonic-gate static crypto_get_soft_info_t *
setup_get_soft_info(char * provname,int count)2247c478bd9Sstevel@tonic-gate setup_get_soft_info(char *provname, int count)
2257c478bd9Sstevel@tonic-gate {
2261b22764fSDaniel OpenSolaris Anderson crypto_get_soft_info_t *psoft_info;
2271b22764fSDaniel OpenSolaris Anderson size_t extra_mech_size = 0;
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate if (provname == NULL) {
2307c478bd9Sstevel@tonic-gate return (NULL);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate if (count > 1) {
2347c478bd9Sstevel@tonic-gate extra_mech_size = sizeof (crypto_mech_name_t) * (count - 1);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate psoft_info = malloc(sizeof (crypto_get_soft_info_t) + extra_mech_size);
2387c478bd9Sstevel@tonic-gate if (psoft_info == NULL) {
2397c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
2407c478bd9Sstevel@tonic-gate return (NULL);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate (void) strlcpy(psoft_info->si_name, provname, MAXNAMELEN);
2447c478bd9Sstevel@tonic-gate psoft_info->si_count = count;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate return (psoft_info);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate * Get the device list from kernel.
2527c478bd9Sstevel@tonic-gate */
2537c478bd9Sstevel@tonic-gate int
get_dev_list(crypto_get_dev_list_t ** ppdevlist)2547c478bd9Sstevel@tonic-gate get_dev_list(crypto_get_dev_list_t **ppdevlist)
2557c478bd9Sstevel@tonic-gate {
2561b22764fSDaniel OpenSolaris Anderson crypto_get_dev_list_t *pdevlist;
2571b22764fSDaniel OpenSolaris Anderson int fd = -1;
2581b22764fSDaniel OpenSolaris Anderson int count = DEFAULT_DEV_NUM;
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate pdevlist = malloc(sizeof (crypto_get_dev_list_t) +
2617c478bd9Sstevel@tonic-gate sizeof (crypto_dev_list_entry_t) * (count - 1));
2627c478bd9Sstevel@tonic-gate if (pdevlist == NULL) {
2637c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
2647c478bd9Sstevel@tonic-gate return (FAILURE);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate if ((fd = open(ADMIN_IOCTL_DEVICE, O_RDONLY)) == -1) {
2687c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("failed to open %s: %s"),
2697c478bd9Sstevel@tonic-gate ADMIN_IOCTL_DEVICE, strerror(errno));
270*9d1587b4SJohn Levon free(pdevlist);
2717c478bd9Sstevel@tonic-gate return (FAILURE);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate pdevlist->dl_dev_count = count;
2757c478bd9Sstevel@tonic-gate if (ioctl(fd, CRYPTO_GET_DEV_LIST, pdevlist) == -1) {
2767c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_DEV_LIST ioctl failed: %s",
2777c478bd9Sstevel@tonic-gate strerror(errno));
2787c478bd9Sstevel@tonic-gate free(pdevlist);
2797c478bd9Sstevel@tonic-gate (void) close(fd);
2807c478bd9Sstevel@tonic-gate return (FAILURE);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate /* BUFFER is too small, get the number of devices and retry it. */
2847c478bd9Sstevel@tonic-gate if (pdevlist->dl_return_value == CRYPTO_BUFFER_TOO_SMALL) {
2857c478bd9Sstevel@tonic-gate count = pdevlist->dl_dev_count;
2867c478bd9Sstevel@tonic-gate free(pdevlist);
2877c478bd9Sstevel@tonic-gate pdevlist = malloc(sizeof (crypto_get_dev_list_t) +
2887c478bd9Sstevel@tonic-gate sizeof (crypto_dev_list_entry_t) * (count - 1));
2897c478bd9Sstevel@tonic-gate if (pdevlist == NULL) {
2907c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
2917c478bd9Sstevel@tonic-gate (void) close(fd);
2927c478bd9Sstevel@tonic-gate return (FAILURE);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate if (ioctl(fd, CRYPTO_GET_DEV_LIST, pdevlist) == -1) {
2967c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_DEV_LIST ioctl failed: %s",
2977c478bd9Sstevel@tonic-gate strerror(errno));
2987c478bd9Sstevel@tonic-gate free(pdevlist);
2997c478bd9Sstevel@tonic-gate (void) close(fd);
3007c478bd9Sstevel@tonic-gate return (FAILURE);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate if (pdevlist->dl_return_value != CRYPTO_SUCCESS) {
3057c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_DEV_LIST ioctl failed, "
3067c478bd9Sstevel@tonic-gate "return_value = %d", pdevlist->dl_return_value);
3077c478bd9Sstevel@tonic-gate free(pdevlist);
3087c478bd9Sstevel@tonic-gate (void) close(fd);
3097c478bd9Sstevel@tonic-gate return (FAILURE);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate *ppdevlist = pdevlist;
3137c478bd9Sstevel@tonic-gate (void) close(fd);
3147c478bd9Sstevel@tonic-gate return (SUCCESS);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate * Get all the mechanisms supported by the hardware provider.
3207c478bd9Sstevel@tonic-gate * The result will be stored in the second argument.
3217c478bd9Sstevel@tonic-gate */
3227c478bd9Sstevel@tonic-gate int
get_dev_info(char * devname,int inst_num,int count,mechlist_t ** ppmechlist)3237c478bd9Sstevel@tonic-gate get_dev_info(char *devname, int inst_num, int count, mechlist_t **ppmechlist)
3247c478bd9Sstevel@tonic-gate {
3251b22764fSDaniel OpenSolaris Anderson crypto_get_dev_info_t *dev_info;
3261b22764fSDaniel OpenSolaris Anderson mechlist_t *phead;
3271b22764fSDaniel OpenSolaris Anderson mechlist_t *pcur;
3281b22764fSDaniel OpenSolaris Anderson mechlist_t *pmech;
3291b22764fSDaniel OpenSolaris Anderson int fd = -1;
3301b22764fSDaniel OpenSolaris Anderson int i;
3311b22764fSDaniel OpenSolaris Anderson int rc;
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate if (devname == NULL || count < 1) {
3347c478bd9Sstevel@tonic-gate cryptodebug("get_dev_info(): devname is NULL or bogus count");
3357c478bd9Sstevel@tonic-gate return (FAILURE);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate /* Set up the argument for the CRYPTO_GET_DEV_INFO ioctl call */
3397c478bd9Sstevel@tonic-gate dev_info = malloc(sizeof (crypto_get_dev_info_t) +
3407c478bd9Sstevel@tonic-gate sizeof (crypto_mech_name_t) * (count - 1));
3417c478bd9Sstevel@tonic-gate if (dev_info == NULL) {
3427c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
3437c478bd9Sstevel@tonic-gate return (FAILURE);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate (void) strlcpy(dev_info->di_dev_name, devname, MAXNAMELEN);
3467c478bd9Sstevel@tonic-gate dev_info->di_dev_instance = inst_num;
3477c478bd9Sstevel@tonic-gate dev_info->di_count = count;
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate /* Open the ioctl device */
3507c478bd9Sstevel@tonic-gate if ((fd = open(ADMIN_IOCTL_DEVICE, O_RDONLY)) == -1) {
3517c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("failed to open %s: %s"),
3527c478bd9Sstevel@tonic-gate ADMIN_IOCTL_DEVICE, strerror(errno));
3537c478bd9Sstevel@tonic-gate free(dev_info);
3547c478bd9Sstevel@tonic-gate return (FAILURE);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate if (ioctl(fd, CRYPTO_GET_DEV_INFO, dev_info) == -1) {
3587c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_DEV_INFO ioctl failed: %s",
3597c478bd9Sstevel@tonic-gate strerror(errno));
3607c478bd9Sstevel@tonic-gate free(dev_info);
3617c478bd9Sstevel@tonic-gate (void) close(fd);
3627c478bd9Sstevel@tonic-gate return (FAILURE);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate if (dev_info->di_return_value != CRYPTO_SUCCESS) {
3667c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_DEV_INFO ioctl failed, "
3677c478bd9Sstevel@tonic-gate "return_value = %d", dev_info->di_return_value);
3687c478bd9Sstevel@tonic-gate free(dev_info);
3697c478bd9Sstevel@tonic-gate (void) close(fd);
3707c478bd9Sstevel@tonic-gate return (FAILURE);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate phead = pcur = NULL;
3747c478bd9Sstevel@tonic-gate rc = SUCCESS;
3757c478bd9Sstevel@tonic-gate for (i = 0; i < dev_info->di_count; i++) {
3767c478bd9Sstevel@tonic-gate pmech = create_mech(&dev_info->di_list[i][0]);
3777c478bd9Sstevel@tonic-gate if (pmech == NULL) {
3787c478bd9Sstevel@tonic-gate rc = FAILURE;
3797c478bd9Sstevel@tonic-gate break;
3807c478bd9Sstevel@tonic-gate } else {
3817c478bd9Sstevel@tonic-gate if (phead == NULL) {
3827c478bd9Sstevel@tonic-gate phead = pcur = pmech;
3837c478bd9Sstevel@tonic-gate } else {
3847c478bd9Sstevel@tonic-gate pcur->next = pmech;
3857c478bd9Sstevel@tonic-gate pcur = pmech;
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate if (rc == SUCCESS) {
3917c478bd9Sstevel@tonic-gate *ppmechlist = phead;
3927c478bd9Sstevel@tonic-gate } else {
3937c478bd9Sstevel@tonic-gate free_mechlist(phead);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate free(dev_info);
3977c478bd9Sstevel@tonic-gate (void) close(fd);
3987c478bd9Sstevel@tonic-gate return (rc);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate * Get the supported mechanism list of the software provider from kernel.
4041b22764fSDaniel OpenSolaris Anderson *
4051b22764fSDaniel OpenSolaris Anderson * Parameters phardlist and psoftlist are supplied by get_kcfconf_info().
4061b22764fSDaniel OpenSolaris Anderson * If NULL, this function calls get_kcfconf_info() internally.
4077c478bd9Sstevel@tonic-gate */
4087c478bd9Sstevel@tonic-gate int
get_soft_info(char * provname,mechlist_t ** ppmechlist,entrylist_t * phardlist,entrylist_t * psoftlist)4091b22764fSDaniel OpenSolaris Anderson get_soft_info(char *provname, mechlist_t **ppmechlist,
410d616ad8eSHai-May Chao entrylist_t *phardlist, entrylist_t *psoftlist)
4117c478bd9Sstevel@tonic-gate {
4121b22764fSDaniel OpenSolaris Anderson boolean_t in_kernel = B_FALSE;
4137c478bd9Sstevel@tonic-gate crypto_get_soft_info_t *psoft_info;
4141b22764fSDaniel OpenSolaris Anderson mechlist_t *phead;
4151b22764fSDaniel OpenSolaris Anderson mechlist_t *pmech;
4161b22764fSDaniel OpenSolaris Anderson mechlist_t *pcur;
4171b22764fSDaniel OpenSolaris Anderson entry_t *pent = NULL;
4181b22764fSDaniel OpenSolaris Anderson int count;
4191b22764fSDaniel OpenSolaris Anderson int fd = -1;
4201b22764fSDaniel OpenSolaris Anderson int rc;
4211b22764fSDaniel OpenSolaris Anderson int i;
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate if (provname == NULL) {
4247c478bd9Sstevel@tonic-gate return (FAILURE);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate if (getzoneid() == GLOBAL_ZONEID) {
4287c478bd9Sstevel@tonic-gate /* use kcf.conf for kernel software providers in global zone */
429d616ad8eSHai-May Chao if ((pent = getent_kef(provname, phardlist, psoftlist)) ==
430d616ad8eSHai-May Chao NULL) {
4311b22764fSDaniel OpenSolaris Anderson
4321b22764fSDaniel OpenSolaris Anderson /* No kcf.conf entry for this provider */
4331b22764fSDaniel OpenSolaris Anderson if (check_kernel_for_soft(provname, NULL, &in_kernel)
4341b22764fSDaniel OpenSolaris Anderson == FAILURE) {
4351b22764fSDaniel OpenSolaris Anderson return (FAILURE);
4361b22764fSDaniel OpenSolaris Anderson } else if (in_kernel == B_FALSE) {
4371b22764fSDaniel OpenSolaris Anderson cryptoerror(LOG_STDERR,
4381b22764fSDaniel OpenSolaris Anderson gettext("%s does not exist."), provname);
4391b22764fSDaniel OpenSolaris Anderson return (FAILURE);
4401b22764fSDaniel OpenSolaris Anderson }
4411b22764fSDaniel OpenSolaris Anderson
4421b22764fSDaniel OpenSolaris Anderson /*
4431b22764fSDaniel OpenSolaris Anderson * Set mech count to 1. It will be reset to the
4441b22764fSDaniel OpenSolaris Anderson * correct value later if the setup buffer is too small.
4451b22764fSDaniel OpenSolaris Anderson */
4461b22764fSDaniel OpenSolaris Anderson count = 1;
4471b22764fSDaniel OpenSolaris Anderson } else {
4481b22764fSDaniel OpenSolaris Anderson count = pent->sup_count;
4491b22764fSDaniel OpenSolaris Anderson free_entry(pent);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate } else {
4527c478bd9Sstevel@tonic-gate /*
4531b22764fSDaniel OpenSolaris Anderson * kcf.conf not there in non-global zone: set mech count to 1.
4541b22764fSDaniel OpenSolaris Anderson * It will be reset to the correct value later if the setup
4551b22764fSDaniel OpenSolaris Anderson * buffer is too small.
4567c478bd9Sstevel@tonic-gate */
4577c478bd9Sstevel@tonic-gate count = 1;
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate if ((psoft_info = setup_get_soft_info(provname, count)) == NULL) {
4617c478bd9Sstevel@tonic-gate return (FAILURE);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate if ((fd = open(ADMIN_IOCTL_DEVICE, O_RDONLY)) == -1) {
4657c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("failed to open %s: %s"),
4667c478bd9Sstevel@tonic-gate ADMIN_IOCTL_DEVICE, strerror(errno));
4677c478bd9Sstevel@tonic-gate free(psoft_info);
4687c478bd9Sstevel@tonic-gate return (FAILURE);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate /* make GET_SOFT_INFO ioctl call */
4727c478bd9Sstevel@tonic-gate if ((rc = ioctl(fd, CRYPTO_GET_SOFT_INFO, psoft_info)) == -1) {
4737c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_SOFT_INFO ioctl failed: %s",
4747c478bd9Sstevel@tonic-gate strerror(errno));
4757c478bd9Sstevel@tonic-gate (void) close(fd);
4767c478bd9Sstevel@tonic-gate free(psoft_info);
4777c478bd9Sstevel@tonic-gate return (FAILURE);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate /* BUFFER is too small, get the number of mechanisms and retry it. */
4817c478bd9Sstevel@tonic-gate if (psoft_info->si_return_value == CRYPTO_BUFFER_TOO_SMALL) {
4827c478bd9Sstevel@tonic-gate count = psoft_info->si_count;
4837c478bd9Sstevel@tonic-gate free(psoft_info);
4847c478bd9Sstevel@tonic-gate if ((psoft_info = setup_get_soft_info(provname, count))
4857c478bd9Sstevel@tonic-gate == NULL) {
4867c478bd9Sstevel@tonic-gate (void) close(fd);
4877c478bd9Sstevel@tonic-gate return (FAILURE);
4887c478bd9Sstevel@tonic-gate } else {
4897c478bd9Sstevel@tonic-gate rc = ioctl(fd, CRYPTO_GET_SOFT_INFO, psoft_info);
4907c478bd9Sstevel@tonic-gate if (rc == -1) {
4917c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_SOFT_INFO ioctl "
4927c478bd9Sstevel@tonic-gate "failed: %s", strerror(errno));
4937c478bd9Sstevel@tonic-gate (void) close(fd);
4947c478bd9Sstevel@tonic-gate free(psoft_info);
4957c478bd9Sstevel@tonic-gate return (FAILURE);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate (void) close(fd);
5017c478bd9Sstevel@tonic-gate if (psoft_info->si_return_value != CRYPTO_SUCCESS) {
5027c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_SOFT_INFO ioctl failed, "
5037c478bd9Sstevel@tonic-gate "return_value = %d", psoft_info->si_return_value);
5047c478bd9Sstevel@tonic-gate free(psoft_info);
5057c478bd9Sstevel@tonic-gate return (FAILURE);
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate
5091b22764fSDaniel OpenSolaris Anderson /* Build the mechanism linked list and return it */
5107c478bd9Sstevel@tonic-gate rc = SUCCESS;
5117c478bd9Sstevel@tonic-gate phead = pcur = NULL;
5127c478bd9Sstevel@tonic-gate for (i = 0; i < psoft_info->si_count; i++) {
5137c478bd9Sstevel@tonic-gate pmech = create_mech(&psoft_info->si_list[i][0]);
5147c478bd9Sstevel@tonic-gate if (pmech == NULL) {
5157c478bd9Sstevel@tonic-gate rc = FAILURE;
5167c478bd9Sstevel@tonic-gate break;
5177c478bd9Sstevel@tonic-gate } else {
5187c478bd9Sstevel@tonic-gate if (phead == NULL) {
5197c478bd9Sstevel@tonic-gate phead = pcur = pmech;
5207c478bd9Sstevel@tonic-gate } else {
5217c478bd9Sstevel@tonic-gate pcur->next = pmech;
5227c478bd9Sstevel@tonic-gate pcur = pmech;
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate if (rc == FAILURE) {
5287c478bd9Sstevel@tonic-gate free_mechlist(phead);
5297c478bd9Sstevel@tonic-gate } else {
5307c478bd9Sstevel@tonic-gate *ppmechlist = phead;
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate
5337c478bd9Sstevel@tonic-gate free(psoft_info);
5347c478bd9Sstevel@tonic-gate return (rc);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate * Get the kernel software provider list from kernel.
5407c478bd9Sstevel@tonic-gate */
5417c478bd9Sstevel@tonic-gate int
get_soft_list(crypto_get_soft_list_t ** ppsoftlist)5427c478bd9Sstevel@tonic-gate get_soft_list(crypto_get_soft_list_t **ppsoftlist)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate crypto_get_soft_list_t *psoftlist = NULL;
5451b22764fSDaniel OpenSolaris Anderson int count = DEFAULT_SOFT_NUM;
5461b22764fSDaniel OpenSolaris Anderson int len;
5471b22764fSDaniel OpenSolaris Anderson int fd = -1;
5487c478bd9Sstevel@tonic-gate
5497c478bd9Sstevel@tonic-gate if ((fd = open(ADMIN_IOCTL_DEVICE, O_RDONLY)) == -1) {
5507c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("failed to open %s: %s"),
5517c478bd9Sstevel@tonic-gate ADMIN_IOCTL_DEVICE, strerror(errno));
5527c478bd9Sstevel@tonic-gate return (FAILURE);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate
5557c478bd9Sstevel@tonic-gate len = MAXNAMELEN * count;
5567c478bd9Sstevel@tonic-gate psoftlist = malloc(sizeof (crypto_get_soft_list_t) + len);
5577c478bd9Sstevel@tonic-gate if (psoftlist == NULL) {
5587c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
5597c478bd9Sstevel@tonic-gate (void) close(fd);
5607c478bd9Sstevel@tonic-gate return (FAILURE);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate psoftlist->sl_soft_names = (caddr_t)(psoftlist + 1);
5637c478bd9Sstevel@tonic-gate psoftlist->sl_soft_count = count;
5647c478bd9Sstevel@tonic-gate psoftlist->sl_soft_len = len;
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate if (ioctl(fd, CRYPTO_GET_SOFT_LIST, psoftlist) == -1) {
5677c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_SOFT_LIST ioctl failed: %s",
5687c478bd9Sstevel@tonic-gate strerror(errno));
5697c478bd9Sstevel@tonic-gate free(psoftlist);
5707c478bd9Sstevel@tonic-gate (void) close(fd);
5717c478bd9Sstevel@tonic-gate return (FAILURE);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate * if BUFFER is too small, get the number of software providers and
5767c478bd9Sstevel@tonic-gate * the minimum length needed for names and length and retry it.
5777c478bd9Sstevel@tonic-gate */
5787c478bd9Sstevel@tonic-gate if (psoftlist->sl_return_value == CRYPTO_BUFFER_TOO_SMALL) {
5797c478bd9Sstevel@tonic-gate count = psoftlist->sl_soft_count;
5807c478bd9Sstevel@tonic-gate len = psoftlist->sl_soft_len;
5817c478bd9Sstevel@tonic-gate free(psoftlist);
5827c478bd9Sstevel@tonic-gate psoftlist = malloc(sizeof (crypto_get_soft_list_t) + len);
5837c478bd9Sstevel@tonic-gate if (psoftlist == NULL) {
5847c478bd9Sstevel@tonic-gate cryptodebug("out of memory.");
5857c478bd9Sstevel@tonic-gate (void) close(fd);
5867c478bd9Sstevel@tonic-gate return (FAILURE);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate psoftlist->sl_soft_names = (caddr_t)(psoftlist + 1);
5897c478bd9Sstevel@tonic-gate psoftlist->sl_soft_count = count;
5907c478bd9Sstevel@tonic-gate psoftlist->sl_soft_len = len;
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate if (ioctl(fd, CRYPTO_GET_SOFT_LIST, psoftlist) == -1) {
5937c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_SOFT_LIST ioctl failed:"
5947c478bd9Sstevel@tonic-gate "%s", strerror(errno));
5957c478bd9Sstevel@tonic-gate free(psoftlist);
5967c478bd9Sstevel@tonic-gate (void) close(fd);
5977c478bd9Sstevel@tonic-gate return (FAILURE);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate if (psoftlist->sl_return_value != CRYPTO_SUCCESS) {
6027c478bd9Sstevel@tonic-gate cryptodebug("CRYPTO_GET_SOFT_LIST ioctl failed, "
6037c478bd9Sstevel@tonic-gate "return_value = %d", psoftlist->sl_return_value);
6047c478bd9Sstevel@tonic-gate free(psoftlist);
6057c478bd9Sstevel@tonic-gate (void) close(fd);
6067c478bd9Sstevel@tonic-gate return (FAILURE);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate *ppsoftlist = psoftlist;
6107c478bd9Sstevel@tonic-gate (void) close(fd);
6117c478bd9Sstevel@tonic-gate return (SUCCESS);
6127c478bd9Sstevel@tonic-gate }
613