xref: /titanic_52/usr/src/lib/libcryptoutil/common/mechstr.c (revision 84ab085a13f931bc78e7415e7ce921dbaa14fcb3)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Convert Algorithm names as strings to PKCS#11 Mech numbers and vice versa.
31  */
32 
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  * The table below is dynamically generated by parsing pkcs11t.h
43  * from the Makefile.
44  */
45 static const struct {
46 	const char *str;
47 	CK_MECHANISM_TYPE mech;
48 } mapping[] = {
49 	{ "CKM_RSA_PKCS_KEY_PAIR_GEN", CKM_RSA_PKCS_KEY_PAIR_GEN },
50 	{ "CKM_RSA_PKCS", CKM_RSA_PKCS },
51 	{ "CKM_RSA_9796", CKM_RSA_9796 },
52 	{ "CKM_RSA_X_509", CKM_RSA_X_509 },
53 	{ "CKM_MD2_RSA_PKCS", CKM_MD2_RSA_PKCS },
54 	{ "CKM_MD5_RSA_PKCS", CKM_MD5_RSA_PKCS },
55 	{ "CKM_SHA1_RSA_PKCS", CKM_SHA1_RSA_PKCS },
56 	{ "CKM_RIPEMD128_RSA_PKCS", CKM_RIPEMD128_RSA_PKCS },
57 	{ "CKM_RIPEMD160_RSA_PKCS", CKM_RIPEMD160_RSA_PKCS },
58 	{ "CKM_RSA_PKCS_OAEP", CKM_RSA_PKCS_OAEP },
59 	{ "CKM_RSA_X9_31_KEY_PAIR_GEN", CKM_RSA_X9_31_KEY_PAIR_GEN },
60 	{ "CKM_RSA_X9_31", CKM_RSA_X9_31 },
61 	{ "CKM_SHA1_RSA_X9_31", CKM_SHA1_RSA_X9_31 },
62 	{ "CKM_RSA_PKCS_PSS", CKM_RSA_PKCS_PSS },
63 	{ "CKM_SHA1_RSA_PKCS_PSS", CKM_SHA1_RSA_PKCS_PSS },
64 	{ "CKM_DSA_KEY_PAIR_GEN", CKM_DSA_KEY_PAIR_GEN },
65 	{ "CKM_DSA", CKM_DSA },
66 	{ "CKM_DSA_SHA1", CKM_DSA_SHA1 },
67 	{ "CKM_DH_PKCS_KEY_PAIR_GEN", CKM_DH_PKCS_KEY_PAIR_GEN },
68 	{ "CKM_DH_PKCS_DERIVE", CKM_DH_PKCS_DERIVE },
69 	{ "CKM_X9_42_DH_KEY_PAIR_GEN", CKM_X9_42_DH_KEY_PAIR_GEN },
70 	{ "CKM_X9_42_DH_DERIVE", CKM_X9_42_DH_DERIVE },
71 	{ "CKM_X9_42_DH_HYBRID_DERIVE", CKM_X9_42_DH_HYBRID_DERIVE },
72 	{ "CKM_X9_42_MQV_DERIVE", CKM_X9_42_MQV_DERIVE },
73 	{ "CKM_RC2_KEY_GEN", CKM_RC2_KEY_GEN },
74 	{ "CKM_RC2_ECB", CKM_RC2_ECB },
75 	{ "CKM_RC2_CBC", CKM_RC2_CBC },
76 	{ "CKM_RC2_MAC", CKM_RC2_MAC },
77 	{ "CKM_RC2_MAC_GENERAL", CKM_RC2_MAC_GENERAL },
78 	{ "CKM_RC2_CBC_PAD", CKM_RC2_CBC_PAD },
79 	{ "CKM_RC4_KEY_GEN", CKM_RC4_KEY_GEN },
80 	{ "CKM_RC4", CKM_RC4 },
81 	{ "CKM_DES_KEY_GEN", CKM_DES_KEY_GEN },
82 	{ "CKM_DES_ECB", CKM_DES_ECB },
83 	{ "CKM_DES_CBC", CKM_DES_CBC },
84 	{ "CKM_DES_MAC", CKM_DES_MAC },
85 	{ "CKM_DES_MAC_GENERAL", CKM_DES_MAC_GENERAL },
86 	{ "CKM_DES_CBC_PAD", CKM_DES_CBC_PAD },
87 	{ "CKM_DES2_KEY_GEN", CKM_DES2_KEY_GEN },
88 	{ "CKM_DES3_KEY_GEN", CKM_DES3_KEY_GEN },
89 	{ "CKM_DES3_ECB", CKM_DES3_ECB },
90 	{ "CKM_DES3_CBC", CKM_DES3_CBC },
91 	{ "CKM_DES3_MAC", CKM_DES3_MAC },
92 	{ "CKM_DES3_MAC_GENERAL", CKM_DES3_MAC_GENERAL },
93 	{ "CKM_DES3_CBC_PAD", CKM_DES3_CBC_PAD },
94 	{ "CKM_CDMF_KEY_GEN", CKM_CDMF_KEY_GEN },
95 	{ "CKM_CDMF_ECB", CKM_CDMF_ECB },
96 	{ "CKM_CDMF_CBC", CKM_CDMF_CBC },
97 	{ "CKM_CDMF_MAC", CKM_CDMF_MAC },
98 	{ "CKM_CDMF_MAC_GENERAL", CKM_CDMF_MAC_GENERAL },
99 	{ "CKM_CDMF_CBC_PAD", CKM_CDMF_CBC_PAD },
100 	{ "CKM_MD2", CKM_MD2 },
101 	{ "CKM_MD2_HMAC", CKM_MD2_HMAC },
102 	{ "CKM_MD2_HMAC_GENERAL", CKM_MD2_HMAC_GENERAL },
103 	{ "CKM_MD5", CKM_MD5 },
104 	{ "CKM_MD5_HMAC", CKM_MD5_HMAC },
105 	{ "CKM_MD5_HMAC_GENERAL", CKM_MD5_HMAC_GENERAL },
106 	{ "CKM_SHA_1", CKM_SHA_1 },
107 	{ "CKM_SHA_1_HMAC", CKM_SHA_1_HMAC },
108 	{ "CKM_SHA_1_HMAC_GENERAL", CKM_SHA_1_HMAC_GENERAL },
109 	{ "CKM_RIPEMD128", CKM_RIPEMD128 },
110 	{ "CKM_RIPEMD128_HMAC", CKM_RIPEMD128_HMAC },
111 	{ "CKM_RIPEMD128_HMAC_GENERAL", CKM_RIPEMD128_HMAC_GENERAL },
112 	{ "CKM_RIPEMD160", CKM_RIPEMD160 },
113 	{ "CKM_RIPEMD160_HMAC", CKM_RIPEMD160_HMAC },
114 	{ "CKM_RIPEMD160_HMAC_GENERAL", CKM_RIPEMD160_HMAC_GENERAL },
115 	{ "CKM_CAST_KEY_GEN", CKM_CAST_KEY_GEN },
116 	{ "CKM_CAST_ECB", CKM_CAST_ECB },
117 	{ "CKM_CAST_CBC", CKM_CAST_CBC },
118 	{ "CKM_CAST_MAC", CKM_CAST_MAC },
119 	{ "CKM_CAST_MAC_GENERAL", CKM_CAST_MAC_GENERAL },
120 	{ "CKM_CAST_CBC_PAD", CKM_CAST_CBC_PAD },
121 	{ "CKM_CAST3_KEY_GEN", CKM_CAST3_KEY_GEN },
122 	{ "CKM_CAST3_ECB", CKM_CAST3_ECB },
123 	{ "CKM_CAST3_CBC", CKM_CAST3_CBC },
124 	{ "CKM_CAST3_MAC", CKM_CAST3_MAC },
125 	{ "CKM_CAST3_MAC_GENERAL", CKM_CAST3_MAC_GENERAL },
126 	{ "CKM_CAST3_CBC_PAD", CKM_CAST3_CBC_PAD },
127 	{ "CKM_CAST5_KEY_GEN", CKM_CAST5_KEY_GEN },
128 	{ "CKM_CAST128_KEY_GEN", CKM_CAST128_KEY_GEN },
129 	{ "CKM_CAST5_ECB", CKM_CAST5_ECB },
130 	{ "CKM_CAST128_ECB", CKM_CAST128_ECB },
131 	{ "CKM_CAST5_CBC", CKM_CAST5_CBC },
132 	{ "CKM_CAST128_CBC", CKM_CAST128_CBC },
133 	{ "CKM_CAST5_MAC", CKM_CAST5_MAC },
134 	{ "CKM_CAST128_MAC", CKM_CAST128_MAC },
135 	{ "CKM_CAST5_MAC_GENERAL", CKM_CAST5_MAC_GENERAL },
136 	{ "CKM_CAST128_MAC_GENERAL", CKM_CAST128_MAC_GENERAL },
137 	{ "CKM_CAST5_CBC_PAD", CKM_CAST5_CBC_PAD },
138 	{ "CKM_CAST128_CBC_PAD", CKM_CAST128_CBC_PAD },
139 	{ "CKM_RC5_KEY_GEN", CKM_RC5_KEY_GEN },
140 	{ "CKM_RC5_ECB", CKM_RC5_ECB },
141 	{ "CKM_RC5_CBC", CKM_RC5_CBC },
142 	{ "CKM_RC5_MAC", CKM_RC5_MAC },
143 	{ "CKM_RC5_MAC_GENERAL", CKM_RC5_MAC_GENERAL },
144 	{ "CKM_RC5_CBC_PAD", CKM_RC5_CBC_PAD },
145 	{ "CKM_IDEA_KEY_GEN", CKM_IDEA_KEY_GEN },
146 	{ "CKM_IDEA_ECB", CKM_IDEA_ECB },
147 	{ "CKM_IDEA_CBC", CKM_IDEA_CBC },
148 	{ "CKM_IDEA_MAC", CKM_IDEA_MAC },
149 	{ "CKM_IDEA_MAC_GENERAL", CKM_IDEA_MAC_GENERAL },
150 	{ "CKM_IDEA_CBC_PAD", CKM_IDEA_CBC_PAD },
151 	{ "CKM_GENERIC_SECRET_KEY_GEN", CKM_GENERIC_SECRET_KEY_GEN },
152 	{ "CKM_CONCATENATE_BASE_AND_KEY", CKM_CONCATENATE_BASE_AND_KEY },
153 	{ "CKM_CONCATENATE_BASE_AND_DATA", CKM_CONCATENATE_BASE_AND_DATA },
154 	{ "CKM_CONCATENATE_DATA_AND_BASE", CKM_CONCATENATE_DATA_AND_BASE },
155 	{ "CKM_XOR_BASE_AND_DATA", CKM_XOR_BASE_AND_DATA },
156 	{ "CKM_EXTRACT_KEY_FROM_KEY", CKM_EXTRACT_KEY_FROM_KEY },
157 	{ "CKM_SSL3_PRE_MASTER_KEY_GEN", CKM_SSL3_PRE_MASTER_KEY_GEN },
158 	{ "CKM_SSL3_MASTER_KEY_DERIVE", CKM_SSL3_MASTER_KEY_DERIVE },
159 	{ "CKM_SSL3_KEY_AND_MAC_DERIVE", CKM_SSL3_KEY_AND_MAC_DERIVE },
160 	{ "CKM_SSL3_MASTER_KEY_DERIVE_DH", CKM_SSL3_MASTER_KEY_DERIVE_DH },
161 	{ "CKM_TLS_PRE_MASTER_KEY_GEN", CKM_TLS_PRE_MASTER_KEY_GEN },
162 	{ "CKM_TLS_MASTER_KEY_DERIVE", CKM_TLS_MASTER_KEY_DERIVE },
163 	{ "CKM_TLS_KEY_AND_MAC_DERIVE", CKM_TLS_KEY_AND_MAC_DERIVE },
164 	{ "CKM_TLS_MASTER_KEY_DERIVE_DH", CKM_TLS_MASTER_KEY_DERIVE_DH },
165 	{ "CKM_SSL3_MD5_MAC", CKM_SSL3_MD5_MAC },
166 	{ "CKM_SSL3_SHA1_MAC", CKM_SSL3_SHA1_MAC },
167 	{ "CKM_MD5_KEY_DERIVATION", CKM_MD5_KEY_DERIVATION },
168 	{ "CKM_MD2_KEY_DERIVATION", CKM_MD2_KEY_DERIVATION },
169 	{ "CKM_SHA1_KEY_DERIVATION", CKM_SHA1_KEY_DERIVATION },
170 	{ "CKM_PBE_MD2_DES_CBC", CKM_PBE_MD2_DES_CBC },
171 	{ "CKM_PBE_MD5_DES_CBC", CKM_PBE_MD5_DES_CBC },
172 	{ "CKM_PBE_MD5_CAST_CBC", CKM_PBE_MD5_CAST_CBC },
173 	{ "CKM_PBE_MD5_CAST3_CBC", CKM_PBE_MD5_CAST3_CBC },
174 	{ "CKM_PBE_MD5_CAST5_CBC", CKM_PBE_MD5_CAST5_CBC },
175 	{ "CKM_PBE_MD5_CAST128_CBC", CKM_PBE_MD5_CAST128_CBC },
176 	{ "CKM_PBE_SHA1_CAST5_CBC", CKM_PBE_SHA1_CAST5_CBC },
177 	{ "CKM_PBE_SHA1_CAST128_CBC", CKM_PBE_SHA1_CAST128_CBC },
178 	{ "CKM_PBE_SHA1_RC4_128", CKM_PBE_SHA1_RC4_128 },
179 	{ "CKM_PBE_SHA1_RC4_40", CKM_PBE_SHA1_RC4_40 },
180 	{ "CKM_PBE_SHA1_DES3_EDE_CBC", CKM_PBE_SHA1_DES3_EDE_CBC },
181 	{ "CKM_PBE_SHA1_DES2_EDE_CBC", CKM_PBE_SHA1_DES2_EDE_CBC },
182 	{ "CKM_PBE_SHA1_RC2_128_CBC", CKM_PBE_SHA1_RC2_128_CBC },
183 	{ "CKM_PBE_SHA1_RC2_40_CBC", CKM_PBE_SHA1_RC2_40_CBC },
184 	{ "CKM_PKCS5_PBKD2", CKM_PKCS5_PBKD2 },
185 	{ "CKM_PBA_SHA1_WITH_SHA1_HMAC", CKM_PBA_SHA1_WITH_SHA1_HMAC },
186 	{ "CKM_KEY_WRAP_LYNKS", CKM_KEY_WRAP_LYNKS },
187 	{ "CKM_KEY_WRAP_SET_OAEP", CKM_KEY_WRAP_SET_OAEP },
188 	{ "CKM_SKIPJACK_KEY_GEN", CKM_SKIPJACK_KEY_GEN },
189 	{ "CKM_SKIPJACK_ECB64", CKM_SKIPJACK_ECB64 },
190 	{ "CKM_SKIPJACK_CBC64", CKM_SKIPJACK_CBC64 },
191 	{ "CKM_SKIPJACK_OFB64", CKM_SKIPJACK_OFB64 },
192 	{ "CKM_SKIPJACK_CFB64", CKM_SKIPJACK_CFB64 },
193 	{ "CKM_SKIPJACK_CFB32", CKM_SKIPJACK_CFB32 },
194 	{ "CKM_SKIPJACK_CFB16", CKM_SKIPJACK_CFB16 },
195 	{ "CKM_SKIPJACK_CFB8", CKM_SKIPJACK_CFB8 },
196 	{ "CKM_SKIPJACK_WRAP", CKM_SKIPJACK_WRAP },
197 	{ "CKM_SKIPJACK_PRIVATE_WRAP", CKM_SKIPJACK_PRIVATE_WRAP },
198 	{ "CKM_SKIPJACK_RELAYX", CKM_SKIPJACK_RELAYX },
199 	{ "CKM_KEA_KEY_PAIR_GEN", CKM_KEA_KEY_PAIR_GEN },
200 	{ "CKM_KEA_KEY_DERIVE", CKM_KEA_KEY_DERIVE },
201 	{ "CKM_FORTEZZA_TIMESTAMP", CKM_FORTEZZA_TIMESTAMP },
202 	{ "CKM_BATON_KEY_GEN", CKM_BATON_KEY_GEN },
203 	{ "CKM_BATON_ECB128", CKM_BATON_ECB128 },
204 	{ "CKM_BATON_ECB96", CKM_BATON_ECB96 },
205 	{ "CKM_BATON_CBC128", CKM_BATON_CBC128 },
206 	{ "CKM_BATON_COUNTER", CKM_BATON_COUNTER },
207 	{ "CKM_BATON_SHUFFLE", CKM_BATON_SHUFFLE },
208 	{ "CKM_BATON_WRAP", CKM_BATON_WRAP },
209 	{ "CKM_ECDSA_KEY_PAIR_GEN", CKM_ECDSA_KEY_PAIR_GEN },
210 	{ "CKM_EC_KEY_PAIR_GEN", CKM_EC_KEY_PAIR_GEN },
211 	{ "CKM_ECDSA", CKM_ECDSA },
212 	{ "CKM_ECDSA_SHA1", CKM_ECDSA_SHA1 },
213 	{ "CKM_ECDH1_DERIVE", CKM_ECDH1_DERIVE },
214 	{ "CKM_ECDH1_COFACTOR_DERIVE", CKM_ECDH1_COFACTOR_DERIVE },
215 	{ "CKM_ECMQV_DERIVE", CKM_ECMQV_DERIVE },
216 	{ "CKM_JUNIPER_KEY_GEN", CKM_JUNIPER_KEY_GEN },
217 	{ "CKM_JUNIPER_ECB128", CKM_JUNIPER_ECB128 },
218 	{ "CKM_JUNIPER_CBC128", CKM_JUNIPER_CBC128 },
219 	{ "CKM_JUNIPER_COUNTER", CKM_JUNIPER_COUNTER },
220 	{ "CKM_JUNIPER_SHUFFLE", CKM_JUNIPER_SHUFFLE },
221 	{ "CKM_JUNIPER_WRAP", CKM_JUNIPER_WRAP },
222 	{ "CKM_FASTHASH", CKM_FASTHASH },
223 	{ "CKM_AES_KEY_GEN", CKM_AES_KEY_GEN },
224 	{ "CKM_AES_ECB", CKM_AES_ECB },
225 	{ "CKM_AES_CBC", CKM_AES_CBC },
226 	{ "CKM_AES_MAC", CKM_AES_MAC },
227 	{ "CKM_AES_MAC_GENERAL", CKM_AES_MAC_GENERAL },
228 	{ "CKM_AES_CBC_PAD", CKM_AES_CBC_PAD },
229 	{ "CKM_DSA_PARAMETER_GEN", CKM_DSA_PARAMETER_GEN },
230 	{ "CKM_DH_PKCS_PARAMETER_GEN", CKM_DH_PKCS_PARAMETER_GEN },
231 	{ "CKM_X9_42_DH_PARAMETER_GEN", CKM_X9_42_DH_PARAMETER_GEN },
232 	{ "CKM_VENDOR_DEFINED", CKM_VENDOR_DEFINED },
233 	{ NULL, 0 }
234 };
235 
236 /*
237  * pkcs11_mech2str - convert PKCS#11 mech to a string
238  *
239  * Anything below CKM_VENDOR_DEFINED that wasn't in the mapping table
240  * at build time causes NULL to be returned.  Anything above it also
241  * returns NULL since we have no way to know what its real name is.
242  */
243 char
244 *pkcs11_mech2str(CK_MECHANISM_TYPE mech)
245 {
246 	int i;
247 	char buf[11];	/* Num chars for representing ulong in ASCII */
248 
249 	if (mech > CKM_VENDOR_DEFINED) {
250 		(void) snprintf(buf, sizeof (buf), "%#lx", mech);
251 		return (strdup(buf));
252 	}
253 
254 	for (i = 0; mapping[i].str; i++) {
255 		if (mapping[i].mech == mech)
256 			return (strdup(mapping[i].str));
257 	}
258 
259 	return (NULL);
260 }
261 
262 /*
263  * pkcs11_str2mech - convert a string into a PKCS#11 mech number.
264  *
265  * Since there isn't reserved value for an invalid mech we return
266  * CKR_MECHANISM_INVALID for anything we don't recognise.
267  * The value in mech isn't meaningful in these cases.
268  */
269 CK_RV
270 pkcs11_str2mech(char *mech_str, CK_MECHANISM_TYPE_PTR mech)
271 {
272 	int i;
273 	char *tmech_str;
274 
275 	if (mech_str == NULL)
276 		return (CKR_MECHANISM_INVALID);
277 
278 	if (strncasecmp(mech_str, "0x8", 3) == 0) {
279 		cryptodebug("pkcs11_str2mech: hex string passed in: %s",
280 		    mech_str);
281 		*mech = strtoll(mech_str, NULL, 16);
282 		return (CKR_OK);
283 	}
284 
285 	if (strncasecmp(mech_str, "CKM_", 4) != 0) {
286 		size_t tmech_strlen = strlen(mech_str) + 4 + 1;
287 		cryptodebug("pkcs11_str2mech: no CKM_ prefix: %s", mech_str);
288 		tmech_str = malloc(tmech_strlen * sizeof (char));
289 		(void) snprintf(tmech_str, tmech_strlen, "CKM_%s", mech_str);
290 		cryptodebug("pkcs11_str2mech: with prefix: %s", tmech_str);
291 	} else {
292 		tmech_str = mech_str;
293 	}
294 
295 	for (i = 0; mapping[i].str; i++) {
296 		if (strcasecmp(mapping[i].str, tmech_str) == 0) {
297 			*mech = mapping[i].mech;
298 			if (tmech_str != mech_str)
299 				free(tmech_str);
300 			return (CKR_OK);
301 		}
302 	}
303 	if (tmech_str != mech_str)
304 		free(tmech_str);
305 
306 	return (CKR_MECHANISM_INVALID);
307 }
308