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 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright 2015 Jason King.
27 */
28
29/*
30 * Block comment which describes the contents of this file.
31 */
32
33#include <stdio.h>
34#include <security/cryptoki.h>
35
36/*
37 * pkcs11_strerror: returns a string representation of the given return code.
38 * The string returned is static pointer.  It doesn't need to be free'd
39 * by the caller.
40 */
41char *
42pkcs11_strerror(CK_RV rv)
43{
44	static char errstr[128];
45
46	switch (rv) {
47		case CKR_OK:
48			return ("CKR_OK");
49		case CKR_CANCEL:
50			return ("CKR_CANCEL");
51		case CKR_HOST_MEMORY:
52			return ("CKR_HOST_MEMORY");
53		case CKR_SLOT_ID_INVALID:
54			return ("CKR_SLOT_ID_INVALID");
55		case CKR_GENERAL_ERROR:
56			return ("CKR_GENERAL_ERROR");
57		case CKR_FUNCTION_FAILED:
58			return ("CKR_FUNCTION_FAILED");
59		case CKR_ARGUMENTS_BAD:
60			return ("CKR_ARGUMENTS_BAD");
61		case CKR_NO_EVENT:
62			return ("CKR_NO_EVENT");
63		case CKR_NEED_TO_CREATE_THREADS:
64			return ("CKR_NEED_TO_CREATE_THREADS");
65		case CKR_CANT_LOCK:
66			return ("CKR_CANT_LOCK");
67		case CKR_ATTRIBUTE_READ_ONLY:
68			return ("CKR_ATTRIBUTE_READ_ONLY");
69		case CKR_ATTRIBUTE_SENSITIVE:
70			return ("CKR_ATTRIBUTE_SENSITIVE");
71		case CKR_ATTRIBUTE_TYPE_INVALID:
72			return ("CKR_ATTRIBUTE_TYPE_INVALID");
73		case CKR_ATTRIBUTE_VALUE_INVALID:
74			return ("CKR_ATTRIBUTE_VALUE_INVALID");
75		case CKR_ACTION_PROHIBITED:
76			return ("CKR_ACTION_PROHIBITED");
77		case CKR_DATA_INVALID:
78			return ("CKR_DATA_INVALID");
79		case CKR_DATA_LEN_RANGE:
80			return ("CKR_DATA_LEN_RANGE");
81		case CKR_DEVICE_ERROR:
82			return ("CKR_DEVICE_ERROR");
83		case CKR_DEVICE_MEMORY:
84			return ("CKR_DEVICE_MEMORY");
85		case CKR_DEVICE_REMOVED:
86			return ("CKR_DEVICE_REMOVED");
87		case CKR_ENCRYPTED_DATA_INVALID:
88			return ("CKR_ENCRYPTED_DATA_INVALID");
89		case CKR_ENCRYPTED_DATA_LEN_RANGE:
90			return ("CKR_ENCRYPTED_DATA_LEN_RANGE");
91		case CKR_FUNCTION_CANCELED:
92			return ("CKR_FUNCTION_CANCELED");
93		case CKR_FUNCTION_NOT_PARALLEL:
94			return ("CKR_FUNCTION_NOT_PARALLEL");
95		case CKR_FUNCTION_NOT_SUPPORTED:
96			return ("CKR_FUNCTION_NOT_SUPPORTED");
97		case CKR_KEY_HANDLE_INVALID:
98			return ("CKR_KEY_HANDLE_INVALID");
99		case CKR_KEY_SIZE_RANGE:
100			return ("CKR_KEY_SIZE_RANGE");
101		case CKR_KEY_TYPE_INCONSISTENT:
102			return ("CKR_KEY_TYPE_INCONSISTENT");
103		case CKR_KEY_NOT_NEEDED:
104			return ("CKR_KEY_NOT_NEEDED");
105		case CKR_KEY_CHANGED:
106			return ("CKR_KEY_CHANGED");
107		case CKR_KEY_NEEDED:
108			return ("CKR_KEY_NEEDED");
109		case CKR_KEY_INDIGESTIBLE:
110			return ("CKR_KEY_INDIGESTIBLE");
111		case CKR_KEY_FUNCTION_NOT_PERMITTED:
112			return ("CKR_KEY_FUNCTION_NOT_PERMITTED");
113		case CKR_KEY_NOT_WRAPPABLE:
114			return ("CKR_KEY_NOT_WRAPPABLE");
115		case CKR_KEY_UNEXTRACTABLE:
116			return ("CKR_KEY_UNEXTRACTABLE");
117		case CKR_MECHANISM_INVALID:
118			return ("CKR_MECHANISM_INVALID");
119		case CKR_MECHANISM_PARAM_INVALID:
120			return ("CKR_MECHANISM_PARAM_INVALID");
121		case CKR_OBJECT_HANDLE_INVALID:
122			return ("CKR_OBJECT_HANDLE_INVALID");
123		case CKR_OPERATION_ACTIVE:
124			return ("CKR_OPERATION_ACTIVE");
125		case CKR_OPERATION_NOT_INITIALIZED:
126			return ("CKR_OPERATION_NOT_INITIALIZED");
127		case CKR_PIN_INCORRECT:
128			return ("CKR_PIN_INCORRECT");
129		case CKR_PIN_INVALID:
130			return ("CKR_PIN_INVALID");
131		case CKR_PIN_LEN_RANGE:
132			return ("CKR_PIN_LEN_RANGE");
133		case CKR_PIN_EXPIRED:
134			return ("CKR_PIN_EXPIRED");
135		case CKR_PIN_LOCKED:
136			return ("CKR_PIN_LOCKED");
137		case CKR_SESSION_CLOSED:
138			return ("CKR_SESSION_CLOSED");
139		case CKR_SESSION_COUNT:
140			return ("CKR_SESSION_COUNT");
141		case CKR_SESSION_HANDLE_INVALID:
142			return ("CKR_SESSION_HANDLE_INVALID");
143		case CKR_SESSION_PARALLEL_NOT_SUPPORTED:
144			return ("CKR_SESSION_PARALLEL_NOT_SUPPORTED");
145		case CKR_SESSION_READ_ONLY:
146			return ("CKR_SESSION_READ_ONLY");
147		case CKR_SESSION_EXISTS:
148			return ("CKR_SESSION_EXISTS");
149		case CKR_SESSION_READ_ONLY_EXISTS:
150			return ("CKR_SESSION_READ_ONLY_EXISTS");
151		case CKR_SESSION_READ_WRITE_SO_EXISTS:
152			return ("CKR_SESSION_READ_WRITE_SO_EXISTS");
153		case CKR_SIGNATURE_INVALID:
154			return ("CKR_SIGNATURE_INVALID");
155		case CKR_SIGNATURE_LEN_RANGE:
156			return ("CKR_SIGNATURE_LEN_RANGE");
157		case CKR_TEMPLATE_INCOMPLETE:
158			return ("CKR_TEMPLATE_INCOMPLETE");
159		case CKR_TEMPLATE_INCONSISTENT:
160			return ("CKR_TEMPLATE_INCONSISTENT");
161		case CKR_TOKEN_NOT_PRESENT:
162			return ("CKR_TOKEN_NOT_PRESENT");
163		case CKR_TOKEN_NOT_RECOGNIZED:
164			return ("CKR_TOKEN_NOT_RECOGNIZED");
165		case CKR_TOKEN_WRITE_PROTECTED:
166			return ("CKR_TOKEN_WRITE_PROTECTED");
167		case CKR_UNWRAPPING_KEY_HANDLE_INVALID:
168			return ("CKR_UNWRAPPING_KEY_HANDLE_INVALID");
169		case CKR_UNWRAPPING_KEY_SIZE_RANGE:
170			return ("CKR_UNWRAPPING_KEY_SIZE_RANGE");
171		case CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT:
172			return ("CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT");
173		case CKR_USER_ALREADY_LOGGED_IN:
174			return ("CKR_USER_ALREADY_LOGGED_IN");
175		case CKR_USER_NOT_LOGGED_IN:
176			return ("CKR_USER_NOT_LOGGED_IN");
177		case CKR_USER_PIN_NOT_INITIALIZED:
178			return ("CKR_USER_PIN_NOT_INITIALIZED");
179		case CKR_USER_TYPE_INVALID:
180			return ("CKR_USER_TYPE_INVALID");
181		case CKR_USER_ANOTHER_ALREADY_LOGGED_IN:
182			return ("CKR_USER_ANOTHER_ALREADY_LOGGED_IN");
183		case CKR_USER_TOO_MANY_TYPES:
184			return ("CKR_USER_TOO_MANY_TYPES");
185		case CKR_WRAPPED_KEY_INVALID:
186			return ("CKR_WRAPPED_KEY_INVALID");
187		case CKR_WRAPPED_KEY_LEN_RANGE:
188			return ("CKR_WRAPPED_KEY_LEN_RANGE");
189		case CKR_WRAPPING_KEY_HANDLE_INVALID:
190			return ("CKR_WRAPPING_KEY_HANDLE_INVALID");
191		case CKR_WRAPPING_KEY_SIZE_RANGE:
192			return ("CKR_WRAPPING_KEY_SIZE_RANGE");
193		case CKR_WRAPPING_KEY_TYPE_INCONSISTENT:
194			return ("CKR_WRAPPING_KEY_TYPE_INCONSISTENT");
195		case CKR_RANDOM_SEED_NOT_SUPPORTED:
196			return ("CKR_RANDOM_SEED_NOT_SUPPORTED");
197		case CKR_RANDOM_NO_RNG:
198			return ("CKR_RANDOM_NO_RNG");
199		case CKR_DOMAIN_PARAMS_INVALID:
200			return ("CKR_DOMAIN_PARAMS_INVALID");
201		case CKR_CURVE_NOT_SUPPORTED:
202			return ("CLR_CURVE_NOT_SUPPORTED");
203		case CKR_BUFFER_TOO_SMALL:
204			return ("CKR_BUFFER_TOO_SMALL");
205		case CKR_SAVED_STATE_INVALID:
206			return ("CKR_SAVED_STATE_INVALID");
207		case CKR_INFORMATION_SENSITIVE:
208			return ("CKR_INFORMATION_SENSITIVE");
209		case CKR_STATE_UNSAVEABLE:
210			return ("CKR_STATE_UNSAVEABLE");
211		case CKR_CRYPTOKI_NOT_INITIALIZED:
212			return ("CKR_CRYPTOKI_NOT_INITIALIZED");
213		case CKR_CRYPTOKI_ALREADY_INITIALIZED:
214			return ("CKR_CRYPTOKI_ALREADY_INITIALIZED");
215		case CKR_MUTEX_BAD:
216			return ("CKR_MUTEX_BAD");
217		case CKR_MUTEX_NOT_LOCKED:
218			return ("CKR_MUTEX_NOT_LOCKED");
219		case CKR_NEW_PIN_MODE:
220			return ("CKR_NEW_PIN_MODE");
221		case CKR_NEXT_OTP:
222			return ("CKR_NEXT_OTP");
223		case CKR_EXCEEDED_MAX_ITERATIONS:
224			return ("CKR_EXCEEDED_MAX_ITERATIONS");
225		case CKR_FIPS_SELF_TEST_FAILED:
226			return ("CKR_FIPS_SELF_TEST_FAILED");
227		case CKR_LIBRARY_LOAD_FAILED:
228			return ("CKR_LIBRARY_LOAD_FAILED");
229		case CKR_PIN_TOO_WEAK:
230			return ("CKR_PIN_TOO_WEAK");
231		case CKR_PUBLIC_KEY_INVALID:
232			return ("CKR_PUBLIC_KEY_INVALID");
233		case CKR_FUNCTION_REJECTED:
234			return ("CKR_FUNCTION_REJECTED");
235		case CKR_VENDOR_DEFINED:
236			return ("CKR_VENDOR_DEFINED");
237		default:
238			/* rv not found */
239			(void) snprintf(errstr, sizeof (errstr),
240			    "Unknown return code: 0x%lx", rv);
241			return (errstr);
242	}
243}
244