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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #pragma ident "@(#)mechstr.c 1.7 08/07/01 SMI"
27
28 /*
29 * Convert Algorithm names as strings to PKCS#11 Mech numbers and vice versa.
30 */
31
32 #include <limits.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <security/cryptoki.h>
37 #include <security/pkcs11t.h>
38
39 #include <cryptoutil.h>
40
41 /*
42 * This table is a one-to-one mapping between mechanism names and numbers.
43 * As such, it should not contain deprecated mechanism names (aliases).
44 */
45 typedef struct {
46 const char *str;
284 { "CKM_AES_ECB", CKM_AES_ECB },
285 { "CKM_AES_CBC", CKM_AES_CBC },
286 { "CKM_AES_MAC", CKM_AES_MAC },
287 { "CKM_AES_MAC_GENERAL", CKM_AES_MAC_GENERAL },
288 { "CKM_AES_CBC_PAD", CKM_AES_CBC_PAD },
289 { "CKM_AES_CTR", CKM_AES_CTR },
290 { "CKM_BLOWFISH_KEY_GEN", CKM_BLOWFISH_KEY_GEN },
291 { "CKM_BLOWFISH_CBC", CKM_BLOWFISH_CBC },
292 { "CKM_TWOFISH_KEY_GEN", CKM_TWOFISH_KEY_GEN },
293 { "CKM_TWOFISH_CBC", CKM_TWOFISH_CBC },
294 { "CKM_DES_ECB_ENCRYPT_DATA", CKM_DES_ECB_ENCRYPT_DATA },
295 { "CKM_DES_CBC_ENCRYPT_DATA", CKM_DES_CBC_ENCRYPT_DATA },
296 { "CKM_DES3_ECB_ENCRYPT_DATA", CKM_DES3_ECB_ENCRYPT_DATA },
297 { "CKM_DES3_CBC_ENCRYPT_DATA", CKM_DES3_CBC_ENCRYPT_DATA },
298 { "CKM_AES_ECB_ENCRYPT_DATA", CKM_AES_ECB_ENCRYPT_DATA },
299 { "CKM_AES_CBC_ENCRYPT_DATA", CKM_AES_CBC_ENCRYPT_DATA },
300 { "CKM_DSA_PARAMETER_GEN", CKM_DSA_PARAMETER_GEN },
301 { "CKM_DH_PKCS_PARAMETER_GEN", CKM_DH_PKCS_PARAMETER_GEN },
302 { "CKM_X9_42_DH_PARAMETER_GEN", CKM_X9_42_DH_PARAMETER_GEN },
303 /*
304 * Values above 0x8000000 (CKM_VENDOR_DEFINED) are represented
305 * as strings with hexadecimal numbers (e.g., "0x8123456").
306 */
307 { NULL, 0 }
308 };
309
310
311 /*
312 * pkcs11_mech_comp - compare two pkcs11_mapping_t structures
313 *
314 * Return a strcmp-like result (positive, zero, or negative).
315 * For use with bsearch(3C) in pkcs11_mech2str().
316 */
317 static int
318 pkcs11_mech_comp(const void *mapping1, const void *mapping2) {
319 return (((pkcs11_mapping_t *)mapping1)->mech -
320 ((pkcs11_mapping_t *)mapping2)->mech);
321 }
322
323
324 /*
325 * pkcs11_mech2str - convert PKCS#11 mech to a string
326 *
327 * Anything below CKM_VENDOR_DEFINED that wasn't in the mapping table
328 * at build time causes NULL to be returned. Anything above it also
329 * returns NULL since we have no way to know its real name.
330 */
331 const char
332 *pkcs11_mech2str(CK_MECHANISM_TYPE mech)
333 {
334 pkcs11_mapping_t target;
335 pkcs11_mapping_t *result = NULL;
336
337 if (mech > CKM_VENDOR_DEFINED) {
338 return (NULL);
339 }
340
341 /* Search for the mechanism number using bsearch(3C) */
342 target.mech = mech;
343 target.str = NULL;
344 result = (pkcs11_mapping_t *)bsearch((void *)&target, (void *)mapping,
345 (sizeof (mapping) / sizeof (pkcs11_mapping_t)) - 1,
346 sizeof (pkcs11_mapping_t), pkcs11_mech_comp);
347 if (result != NULL) {
348 return (result->str);
349 }
350
351 return (NULL);
352 }
353
354 /*
355 * pkcs11_str2mech - convert a string into a PKCS#11 mech number.
356 *
357 * Since there isn't a reserved value for an invalid mech we return
|
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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #pragma ident "@(#)mechstr.c 1.8 08/07/07 SMI"
27
28 /*
29 * Convert Algorithm names as strings to PKCS#11 Mech numbers and vice versa.
30 */
31
32 #include <limits.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <security/cryptoki.h>
37 #include <security/pkcs11t.h>
38
39 #include <cryptoutil.h>
40
41 /*
42 * This table is a one-to-one mapping between mechanism names and numbers.
43 * As such, it should not contain deprecated mechanism names (aliases).
44 */
45 typedef struct {
46 const char *str;
284 { "CKM_AES_ECB", CKM_AES_ECB },
285 { "CKM_AES_CBC", CKM_AES_CBC },
286 { "CKM_AES_MAC", CKM_AES_MAC },
287 { "CKM_AES_MAC_GENERAL", CKM_AES_MAC_GENERAL },
288 { "CKM_AES_CBC_PAD", CKM_AES_CBC_PAD },
289 { "CKM_AES_CTR", CKM_AES_CTR },
290 { "CKM_BLOWFISH_KEY_GEN", CKM_BLOWFISH_KEY_GEN },
291 { "CKM_BLOWFISH_CBC", CKM_BLOWFISH_CBC },
292 { "CKM_TWOFISH_KEY_GEN", CKM_TWOFISH_KEY_GEN },
293 { "CKM_TWOFISH_CBC", CKM_TWOFISH_CBC },
294 { "CKM_DES_ECB_ENCRYPT_DATA", CKM_DES_ECB_ENCRYPT_DATA },
295 { "CKM_DES_CBC_ENCRYPT_DATA", CKM_DES_CBC_ENCRYPT_DATA },
296 { "CKM_DES3_ECB_ENCRYPT_DATA", CKM_DES3_ECB_ENCRYPT_DATA },
297 { "CKM_DES3_CBC_ENCRYPT_DATA", CKM_DES3_CBC_ENCRYPT_DATA },
298 { "CKM_AES_ECB_ENCRYPT_DATA", CKM_AES_ECB_ENCRYPT_DATA },
299 { "CKM_AES_CBC_ENCRYPT_DATA", CKM_AES_CBC_ENCRYPT_DATA },
300 { "CKM_DSA_PARAMETER_GEN", CKM_DSA_PARAMETER_GEN },
301 { "CKM_DH_PKCS_PARAMETER_GEN", CKM_DH_PKCS_PARAMETER_GEN },
302 { "CKM_X9_42_DH_PARAMETER_GEN", CKM_X9_42_DH_PARAMETER_GEN },
303 /*
304 * Values >= 0x8000000 (CKM_VENDOR_DEFINED) are represented
305 * as strings with hexadecimal numbers (e.g., "0x8123456").
306 */
307 { NULL, 0 }
308 };
309
310
311 /*
312 * pkcs11_mech_comp - compare two pkcs11_mapping_t structures
313 *
314 * Return a strcmp-like result (positive, zero, or negative).
315 * For use with bsearch(3C) in pkcs11_mech2str().
316 */
317 static int
318 pkcs11_mech_comp(const void *mapping1, const void *mapping2) {
319 return (((pkcs11_mapping_t *)mapping1)->mech -
320 ((pkcs11_mapping_t *)mapping2)->mech);
321 }
322
323
324 /*
325 * pkcs11_mech2str - convert PKCS#11 mech to a string
326 *
327 * Anything below CKM_VENDOR_DEFINED that wasn't in the mapping table
328 * at build time causes NULL to be returned. Anything above it also
329 * returns NULL since we have no way to know its real name.
330 */
331 const char
332 *pkcs11_mech2str(CK_MECHANISM_TYPE mech)
333 {
334 pkcs11_mapping_t target;
335 pkcs11_mapping_t *result = NULL;
336
337 if (mech >= CKM_VENDOR_DEFINED) {
338 return (NULL);
339 }
340
341 /* Search for the mechanism number using bsearch(3C) */
342 target.mech = mech;
343 target.str = NULL;
344 result = (pkcs11_mapping_t *)bsearch((void *)&target, (void *)mapping,
345 (sizeof (mapping) / sizeof (pkcs11_mapping_t)) - 1,
346 sizeof (pkcs11_mapping_t), pkcs11_mech_comp);
347 if (result != NULL) {
348 return (result->str);
349 }
350
351 return (NULL);
352 }
353
354 /*
355 * pkcs11_str2mech - convert a string into a PKCS#11 mech number.
356 *
357 * Since there isn't a reserved value for an invalid mech we return
|