1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *     * Redistributions of source code must retain the above
9  *       copyright notice, this list of conditions and the
10  *       following disclaimer.
11  *     * Redistributions in binary form must reproduce the
12  *       above copyright notice, this list of conditions and
13  *       the following disclaimer in the documentation and/or
14  *       other materials provided with the distribution.
15  *     * The names of contributors to this software may not be
16  *       used to endorse or promote products derived from this
17  *       software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30  * DAMAGE.
31  *
32  * Author: Stef Walter <stefw@collabora.co.uk>
33  */
34 
35 #include "config.h"
36 
37 #include "pkcs11.h"
38 #include "p11-kit.h"
39 
40 /**
41  * SECTION:p11-kit-util
42  * @title: Utilities
43  * @short_description: PKCS\#11 utilities
44  *
45  * Utility functions for working with PKCS\#11.
46  */
47 
48 #ifdef ENABLE_NLS
49 #include <libintl.h>
50 #define _(x) dgettext(PACKAGE_NAME, x)
51 #else
52 #define _(x) (x)
53 #endif
54 
55 /**
56  * p11_kit_strerror:
57  * @rv: The code to get a message for.
58  *
59  * Get a message for a PKCS\#11 return value or error code. Do not
60  * pass CKR_OK or other such non errors to this function.
61  *
62  * Returns: The user readable and localized message.
63  **/
64 const char*
p11_kit_strerror(CK_RV rv)65 p11_kit_strerror (CK_RV rv)
66 {
67 	switch (rv) {
68 
69 	/* These are not really errors, or not current */
70 	case CKR_OK:
71 	case CKR_NO_EVENT:
72 	case CKR_FUNCTION_NOT_PARALLEL:
73 	case CKR_SESSION_PARALLEL_NOT_SUPPORTED:
74 		return "";
75 
76 	case CKR_CANCEL:
77 	case CKR_FUNCTION_CANCELED:
78 		return _("The operation was cancelled");
79 
80 	case CKR_HOST_MEMORY:
81 		return _("Insufficient memory available");
82 	case CKR_SLOT_ID_INVALID:
83 		return _("The specified slot ID is not valid");
84 	case CKR_GENERAL_ERROR:
85 		return _("Internal error");
86 	case CKR_FUNCTION_FAILED:
87 		return _("The operation failed");
88 	case CKR_ARGUMENTS_BAD:
89 		return _("Invalid arguments");
90 	case CKR_NEED_TO_CREATE_THREADS:
91 		return _("The module cannot create needed threads");
92 	case CKR_CANT_LOCK:
93 		return _("The module cannot lock data properly");
94 	case CKR_ATTRIBUTE_READ_ONLY:
95 		return _("The field is read-only");
96 	case CKR_ATTRIBUTE_SENSITIVE:
97 		return _("The field is sensitive and cannot be revealed");
98 	case CKR_ATTRIBUTE_TYPE_INVALID:
99 		return _("The field is invalid or does not exist");
100 	case CKR_ATTRIBUTE_VALUE_INVALID:
101 		return _("Invalid value for field");
102 	case CKR_DATA_INVALID:
103 		return _("The data is not valid or unrecognized");
104 	case CKR_DATA_LEN_RANGE:
105 		return _("The data is too long");
106 	case CKR_DEVICE_ERROR:
107 		return _("An error occurred on the device");
108 	case CKR_DEVICE_MEMORY:
109 		return _("Insufficient memory available on the device");
110 	case CKR_DEVICE_REMOVED:
111 		return _("The device was removed or unplugged");
112 	case CKR_ENCRYPTED_DATA_INVALID:
113 		return _("The encrypted data is not valid or unrecognized");
114 	case CKR_ENCRYPTED_DATA_LEN_RANGE:
115 		return _("The encrypted data is too long");
116 	case CKR_FUNCTION_NOT_SUPPORTED:
117 		return _("This operation is not supported");
118 	case CKR_KEY_HANDLE_INVALID:
119 		return _("The key is missing or invalid");
120 	case CKR_KEY_SIZE_RANGE:
121 		return _("The key is the wrong size");
122 	case CKR_KEY_TYPE_INCONSISTENT:
123 		return _("The key is of the wrong type");
124 	case CKR_KEY_NOT_NEEDED:
125 		return _("No key is needed");
126 	case CKR_KEY_CHANGED:
127 		return _("The key is different than before");
128 	case CKR_KEY_NEEDED:
129 		return _("A key is needed");
130 	case CKR_KEY_INDIGESTIBLE:
131 		return _("Cannot include the key in the digest");
132 	case CKR_KEY_FUNCTION_NOT_PERMITTED:
133 		return _("This operation cannot be done with this key");
134 	case CKR_KEY_NOT_WRAPPABLE:
135 		return _("The key cannot be wrapped");
136 	case CKR_KEY_UNEXTRACTABLE:
137 		return _("Cannot export this key");
138 	case CKR_MECHANISM_INVALID:
139 		return _("The crypto mechanism is invalid or unrecognized");
140 	case CKR_MECHANISM_PARAM_INVALID:
141 		return _("The crypto mechanism has an invalid argument");
142 	case CKR_OBJECT_HANDLE_INVALID:
143 		return _("The object is missing or invalid");
144 	case CKR_OPERATION_ACTIVE:
145 		return _("Another operation is already taking place");
146 	case CKR_OPERATION_NOT_INITIALIZED:
147 		return _("No operation is taking place");
148 	case CKR_PIN_INCORRECT:
149 		return _("The password or PIN is incorrect");
150 	case CKR_PIN_INVALID:
151 		return _("The password or PIN is invalid");
152 	case CKR_PIN_LEN_RANGE:
153 		return _("The password or PIN is of an invalid length");
154 	case CKR_PIN_EXPIRED:
155 		return _("The password or PIN has expired");
156 	case CKR_PIN_LOCKED:
157 		return _("The password or PIN is locked");
158 	case CKR_SESSION_CLOSED:
159 		return _("The session is closed");
160 	case CKR_SESSION_COUNT:
161 		return _("Too many sessions are active");
162 	case CKR_SESSION_HANDLE_INVALID:
163 		return _("The session is invalid");
164 	case CKR_SESSION_READ_ONLY:
165 		return _("The session is read-only");
166 	case CKR_SESSION_EXISTS:
167 		return _("An open session exists");
168 	case CKR_SESSION_READ_ONLY_EXISTS:
169 		return _("A read-only session exists");
170 	case CKR_SESSION_READ_WRITE_SO_EXISTS:
171 		return _("An administrator session exists");
172 	case CKR_SIGNATURE_INVALID:
173 		return _("The signature is bad or corrupted");
174 	case CKR_SIGNATURE_LEN_RANGE:
175 		return _("The signature is unrecognized or corrupted");
176 	case CKR_TEMPLATE_INCOMPLETE:
177 		return _("Certain required fields are missing");
178 	case CKR_TEMPLATE_INCONSISTENT:
179 		return _("Certain fields have invalid values");
180 	case CKR_TOKEN_NOT_PRESENT:
181 		return _("The device is not present or unplugged");
182 	case CKR_TOKEN_NOT_RECOGNIZED:
183 		return _("The device is invalid or unrecognizable");
184 	case CKR_TOKEN_WRITE_PROTECTED:
185 		return _("The device is write protected");
186 	case CKR_UNWRAPPING_KEY_HANDLE_INVALID:
187 		return _("Cannot import because the key is invalid");
188 	case CKR_UNWRAPPING_KEY_SIZE_RANGE:
189 		return _("Cannot import because the key is of the wrong size");
190 	case CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT:
191 		return _("Cannot import because the key is of the wrong type");
192 	case CKR_USER_ALREADY_LOGGED_IN:
193 		return _("You are already logged in");
194 	case CKR_USER_NOT_LOGGED_IN:
195 		return _("No user has logged in");
196 	case CKR_USER_PIN_NOT_INITIALIZED:
197 		return _("The user's password or PIN is not set");
198 	case CKR_USER_TYPE_INVALID:
199 		return _("The user is of an invalid type");
200 	case CKR_USER_ANOTHER_ALREADY_LOGGED_IN:
201 		return _("Another user is already logged in");
202 	case CKR_USER_TOO_MANY_TYPES:
203 		return _("Too many users of different types are logged in");
204 	case CKR_WRAPPED_KEY_INVALID:
205 		return _("Cannot import an invalid key");
206 	case CKR_WRAPPED_KEY_LEN_RANGE:
207 		return _("Cannot import a key of the wrong size");
208 	case CKR_WRAPPING_KEY_HANDLE_INVALID:
209 		return _("Cannot export because the key is invalid");
210 	case CKR_WRAPPING_KEY_SIZE_RANGE:
211 		return _("Cannot export because the key is of the wrong size");
212 	case CKR_WRAPPING_KEY_TYPE_INCONSISTENT:
213 		return _("Cannot export because the key is of the wrong type");
214 	case CKR_RANDOM_SEED_NOT_SUPPORTED:
215 		return _("Unable to initialize the random number generator");
216 	case CKR_RANDOM_NO_RNG:
217 		return _("No random number generator available");
218 	case CKR_DOMAIN_PARAMS_INVALID:
219 		return _("The crypto mechanism has an invalid parameter");
220 	case CKR_BUFFER_TOO_SMALL:
221 		return _("Not enough space to store the result");
222 	case CKR_SAVED_STATE_INVALID:
223 		return _("The saved state is invalid");
224 	case CKR_INFORMATION_SENSITIVE:
225 		return _("The information is sensitive and cannot be revealed");
226 	case CKR_STATE_UNSAVEABLE:
227 		return _("The state cannot be saved");
228 	case CKR_CRYPTOKI_NOT_INITIALIZED:
229 		return _("The module has not been initialized");
230 	case CKR_CRYPTOKI_ALREADY_INITIALIZED:
231 		return _("The module has already been initialized");
232 	case CKR_MUTEX_BAD:
233 		return _("Cannot lock data");
234 	case CKR_MUTEX_NOT_LOCKED:
235 		return _("The data cannot be locked");
236 	case CKR_FUNCTION_REJECTED:
237 		return _("The request was rejected by the user");
238 
239 	default:
240 		return _("Unknown error");
241 	}
242 }
243