1 /* 2 * CDDL HEADER START 3 * 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. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <stdio.h> 27 #include <link.h> 28 #include <fcntl.h> 29 #include <ctype.h> 30 #include <sys/param.h> 31 #include <sys/types.h> 32 #include <sys/stat.h> 33 #include <ber_der.h> 34 #include <kmfapiP.h> 35 #include <libgen.h> 36 #include <cryptoutil.h> 37 38 KMF_RETURN 39 kmf_create_keypair(KMF_HANDLE_T handle, 40 int num_args, 41 KMF_ATTRIBUTE *attrlist) 42 { 43 KMF_RETURN ret = KMF_OK; 44 KMF_PLUGIN *plugin; 45 KMF_KEYSTORE_TYPE kstype; 46 uint32_t len; 47 48 KMF_ATTRIBUTE_TESTER required_attrs[] = { 49 {KMF_KEYSTORE_TYPE_ATTR, FALSE, 1, sizeof (KMF_KEYSTORE_TYPE)}, 50 {KMF_PRIVKEY_HANDLE_ATTR, FALSE, sizeof (KMF_KEY_HANDLE), 51 sizeof (KMF_KEY_HANDLE)}, 52 {KMF_PUBKEY_HANDLE_ATTR, FALSE, sizeof (KMF_KEY_HANDLE), 53 sizeof (KMF_KEY_HANDLE)}, 54 }; 55 56 int num_req_attrs = sizeof (required_attrs) / 57 sizeof (KMF_ATTRIBUTE_TESTER); 58 59 if (handle == NULL) 60 return (KMF_ERR_BAD_PARAMETER); 61 62 CLEAR_ERROR(handle, ret); 63 64 ret = test_attributes(num_req_attrs, required_attrs, 65 0, NULL, num_args, attrlist); 66 67 if (ret != KMF_OK) 68 return (ret); 69 70 len = sizeof (kstype); 71 ret = kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR, attrlist, num_args, 72 &kstype, &len); 73 if (ret != KMF_OK) 74 return (ret); 75 76 plugin = FindPlugin(handle, kstype); 77 if (plugin != NULL && plugin->funclist->CreateKeypair != NULL) { 78 return (plugin->funclist->CreateKeypair(handle, num_args, 79 attrlist)); 80 } else { 81 return (KMF_ERR_PLUGIN_NOTFOUND); 82 } 83 } 84 85 KMF_RETURN 86 kmf_delete_key_from_keystore(KMF_HANDLE_T handle, 87 int num_args, 88 KMF_ATTRIBUTE *attrlist) 89 { 90 KMF_RETURN ret = KMF_OK; 91 KMF_PLUGIN *plugin; 92 KMF_KEYSTORE_TYPE kstype; 93 uint32_t len; 94 KMF_KEY_HANDLE *key; 95 96 97 KMF_ATTRIBUTE_TESTER required_attrs[] = { 98 {KMF_KEYSTORE_TYPE_ATTR, FALSE, 1, sizeof (KMF_KEYSTORE_TYPE)}, 99 {KMF_KEY_HANDLE_ATTR, FALSE, sizeof (KMF_KEY_HANDLE), 100 sizeof (KMF_KEY_HANDLE)}, 101 }; 102 103 int num_req_attrs = sizeof (required_attrs) / 104 sizeof (KMF_ATTRIBUTE_TESTER); 105 106 if (handle == NULL) 107 return (KMF_ERR_BAD_PARAMETER); 108 109 CLEAR_ERROR(handle, ret); 110 111 ret = test_attributes(num_req_attrs, required_attrs, 112 0, NULL, num_args, attrlist); 113 114 if (ret != KMF_OK) 115 return (ret); 116 117 len = sizeof (kstype); 118 ret = kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR, attrlist, num_args, 119 &kstype, &len); 120 if (ret != KMF_OK) 121 return (ret); 122 123 plugin = FindPlugin(handle, kstype); 124 if (plugin != NULL && plugin->funclist->DeleteKey != NULL) { 125 ret = plugin->funclist->DeleteKey(handle, num_args, attrlist); 126 } else { 127 ret = KMF_ERR_PLUGIN_NOTFOUND; 128 } 129 130 if (ret == KMF_OK) { 131 key = kmf_get_attr_ptr(KMF_KEY_HANDLE_ATTR, attrlist, num_args); 132 if (key == NULL) 133 return (KMF_ERR_BAD_PARAMETER); 134 if (key->keylabel != NULL) 135 free(key->keylabel); 136 137 if (key->israw && key->keyp != NULL) { 138 if (key->keyclass == KMF_ASYM_PUB || 139 key->keyclass == KMF_ASYM_PRI) { 140 kmf_free_raw_key(key->keyp); 141 free(key->keyp); 142 } else if (key->keyclass == KMF_SYMMETRIC) { 143 kmf_free_raw_sym_key(key->keyp); 144 } 145 /* Else we don't know how to free the memory. */ 146 } 147 148 (void) memset(key, 0, sizeof (KMF_KEY_HANDLE)); 149 } 150 151 return (ret); 152 } 153 154 KMF_RETURN 155 kmf_find_key(KMF_HANDLE_T handle, 156 int num_args, 157 KMF_ATTRIBUTE *attrlist) 158 { 159 KMF_RETURN ret = KMF_OK; 160 KMF_PLUGIN *plugin; 161 KMF_KEYSTORE_TYPE kstype; 162 uint32_t len; 163 164 KMF_ATTRIBUTE_TESTER required_attrs[] = { 165 {KMF_KEYSTORE_TYPE_ATTR, FALSE, 1, sizeof (KMF_KEYSTORE_TYPE)}, 166 {KMF_COUNT_ATTR, FALSE, sizeof (uint32_t), 167 sizeof (uint32_t)} 168 }; 169 170 int num_req_attrs = sizeof (required_attrs) / 171 sizeof (KMF_ATTRIBUTE_TESTER); 172 173 if (handle == NULL) 174 return (KMF_ERR_BAD_PARAMETER); 175 176 CLEAR_ERROR(handle, ret); 177 178 ret = test_attributes(num_req_attrs, required_attrs, 179 0, NULL, num_args, attrlist); 180 181 if (ret != KMF_OK) 182 return (ret); 183 184 len = sizeof (kstype); 185 ret = kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR, attrlist, num_args, 186 &kstype, &len); 187 if (ret != KMF_OK) 188 return (ret); 189 190 plugin = FindPlugin(handle, kstype); 191 if (plugin != NULL && plugin->funclist->FindKey != NULL) { 192 return (plugin->funclist->FindKey(handle, num_args, attrlist)); 193 } 194 195 return (KMF_ERR_PLUGIN_NOTFOUND); 196 } 197 198 KMF_RETURN 199 kmf_create_sym_key(KMF_HANDLE_T handle, 200 int num_args, 201 KMF_ATTRIBUTE *attrlist) 202 { 203 KMF_RETURN ret = KMF_OK; 204 KMF_PLUGIN *plugin; 205 KMF_KEYSTORE_TYPE kstype; 206 uint32_t len; 207 208 KMF_ATTRIBUTE_TESTER required_attrs[] = { 209 {KMF_KEYSTORE_TYPE_ATTR, FALSE, 1, sizeof (KMF_KEYSTORE_TYPE)}, 210 {KMF_KEY_HANDLE_ATTR, FALSE, sizeof (KMF_KEY_HANDLE), 211 sizeof (KMF_KEY_HANDLE)}, 212 {KMF_KEYALG_ATTR, FALSE, 1, sizeof (KMF_KEY_ALG)}, 213 }; 214 215 int num_req_attrs = sizeof (required_attrs) / 216 sizeof (KMF_ATTRIBUTE_TESTER); 217 218 if (handle == NULL) 219 return (KMF_ERR_BAD_PARAMETER); 220 221 CLEAR_ERROR(handle, ret); 222 223 ret = test_attributes(num_req_attrs, required_attrs, 224 0, NULL, num_args, attrlist); 225 226 if (ret != KMF_OK) 227 return (ret); 228 229 len = sizeof (kstype); 230 ret = kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR, attrlist, num_args, 231 &kstype, &len); 232 if (ret != KMF_OK) 233 return (ret); 234 235 plugin = FindPlugin(handle, kstype); 236 if (plugin != NULL && plugin->funclist->CreateSymKey != NULL) { 237 return (plugin->funclist->CreateSymKey(handle, num_args, 238 attrlist)); 239 } else { 240 return (KMF_ERR_PLUGIN_NOTFOUND); 241 } 242 } 243 244 KMF_RETURN 245 kmf_get_sym_key_value(KMF_HANDLE_T handle, KMF_KEY_HANDLE *symkey, 246 KMF_RAW_SYM_KEY *rkey) 247 { 248 KMF_PLUGIN *plugin; 249 KMF_RETURN ret; 250 251 CLEAR_ERROR(handle, ret); 252 if (ret != KMF_OK) 253 return (ret); 254 255 if (symkey == NULL || rkey == NULL) 256 return (KMF_ERR_BAD_PARAMETER); 257 258 plugin = FindPlugin(handle, symkey->kstype); 259 if (plugin != NULL && 260 plugin->funclist->GetSymKeyValue != NULL) { 261 return (plugin->funclist->GetSymKeyValue(handle, 262 symkey, rkey)); 263 } else { 264 return (KMF_ERR_PLUGIN_NOTFOUND); 265 } 266 } 267 268 KMF_RETURN 269 kmf_store_key(KMF_HANDLE_T handle, 270 int numattr, 271 KMF_ATTRIBUTE *attrlist) 272 { 273 KMF_RETURN ret = KMF_OK; 274 KMF_PLUGIN *plugin; 275 KMF_KEYSTORE_TYPE kstype; 276 277 KMF_ATTRIBUTE_TESTER required_attrs[] = { 278 {KMF_KEYSTORE_TYPE_ATTR, FALSE, 1, sizeof (KMF_KEYSTORE_TYPE)}, 279 }; 280 281 int num_req_attrs = sizeof (required_attrs) / 282 sizeof (KMF_ATTRIBUTE_TESTER); 283 284 if (handle == NULL) 285 return (KMF_ERR_BAD_PARAMETER); 286 287 CLEAR_ERROR(handle, ret); 288 289 ret = test_attributes(num_req_attrs, required_attrs, 290 0, NULL, numattr, attrlist); 291 292 if (ret != KMF_OK) 293 return (ret); 294 295 ret = kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR, attrlist, numattr, 296 &kstype, NULL); 297 if (ret != KMF_OK) 298 return (ret); 299 300 plugin = FindPlugin(handle, kstype); 301 if (plugin != NULL) { 302 if (plugin->funclist->StoreKey != NULL) 303 return (plugin->funclist->StoreKey(handle, 304 numattr, attrlist)); 305 else 306 return (KMF_ERR_FUNCTION_NOT_FOUND); 307 } 308 return (KMF_ERR_PLUGIN_NOTFOUND); 309 } 310