1/*
2 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3 * Use is subject to license terms.
4 */
5
6/* crypto/engine/hw_pk11_err.c */
7/*
8 * This product includes software developed by the OpenSSL Project for
9 * use in the OpenSSL Toolkit (http://www.openssl.org/).
10 *
11 * This project also referenced hw_pkcs11-0.9.7b.patch written by
12 * Afchine Madjlessi.
13 */
14/*
15 * ====================================================================
16 * Copyright (c) 2000-2001 The OpenSSL Project.  All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 *
22 * 1. Redistributions of source code must retain the above copyright
23 *    notice, this list of conditions and the following disclaimer.
24 *
25 * 2. Redistributions in binary form must reproduce the above copyright
26 *    notice, this list of conditions and the following disclaimer in
27 *    the documentation and/or other materials provided with the
28 *    distribution.
29 *
30 * 3. All advertising materials mentioning features or use of this
31 *    software must display the following acknowledgment:
32 *    "This product includes software developed by the OpenSSL Project
33 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
34 *
35 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
36 *    endorse or promote products derived from this software without
37 *    prior written permission. For written permission, please contact
38 *    licensing@OpenSSL.org.
39 *
40 * 5. Products derived from this software may not be called "OpenSSL"
41 *    nor may "OpenSSL" appear in their names without prior written
42 *    permission of the OpenSSL Project.
43 *
44 * 6. Redistributions of any form whatsoever must retain the following
45 *    acknowledgment:
46 *    "This product includes software developed by the OpenSSL Project
47 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
50 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
53 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
58 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
60 * OF THE POSSIBILITY OF SUCH DAMAGE.
61 * ====================================================================
62 *
63 * This product includes cryptographic software written by Eric Young
64 * (eay@cryptsoft.com).  This product includes software written by Tim
65 * Hudson (tjh@cryptsoft.com).
66 *
67 */
68
69#include <stdio.h>
70#include <openssl/err.h>
71#include "hw_pk11_err.h"
72
73/* BEGIN ERROR CODES */
74#ifndef OPENSSL_NO_ERR
75static ERR_STRING_DATA pk11_str_functs[]=
76{
77{ ERR_PACK(0, PK11_F_INIT, 0),			"PK11_INIT"},
78{ ERR_PACK(0, PK11_F_FINISH, 0),		"PK11_FINISH"},
79{ ERR_PACK(0, PK11_F_DESTROY, 0),		"PK11_DESTROY"},
80{ ERR_PACK(0, PK11_F_CTRL, 0),			"PK11_CTRL"},
81{ ERR_PACK(0, PK11_F_RSA_INIT, 0),		"PK11_RSA_INIT"},
82{ ERR_PACK(0, PK11_F_RSA_FINISH, 0),		"PK11_RSA_FINISH"},
83{ ERR_PACK(0, PK11_F_GET_PUB_RSA_KEY, 0),	"PK11_GET_PUB_RSA_KEY"},
84{ ERR_PACK(0, PK11_F_GET_PRIV_RSA_KEY, 0),	"PK11_GET_PRIV_RSA_KEY"},
85{ ERR_PACK(0, PK11_F_RSA_GEN_KEY, 0),		"PK11_RSA_GEN_KEY"},
86{ ERR_PACK(0, PK11_F_RSA_PUB_ENC, 0),		"PK11_RSA_PUB_ENC"},
87{ ERR_PACK(0, PK11_F_RSA_PRIV_ENC, 0),		"PK11_RSA_PRIV_ENC"},
88{ ERR_PACK(0, PK11_F_RSA_PUB_DEC, 0),		"PK11_RSA_PUB_DEC"},
89{ ERR_PACK(0, PK11_F_RSA_PRIV_DEC, 0),		"PK11_RSA_PRIV_DEC"},
90{ ERR_PACK(0, PK11_F_RSA_SIGN, 0),		"PK11_RSA_SIGN"},
91{ ERR_PACK(0, PK11_F_RSA_VERIFY, 0),		"PK11_RSA_VERIFY"},
92{ ERR_PACK(0, PK11_F_RAND_ADD, 0),		"PK11_RAND_ADD"},
93{ ERR_PACK(0, PK11_F_RAND_BYTES, 0),		"PK11_RAND_BYTES"},
94{ ERR_PACK(0, PK11_F_GET_SESSION, 0),		"PK11_GET_SESSION"},
95{ ERR_PACK(0, PK11_F_FREE_SESSION, 0),		"PK11_FREE_SESSION"},
96{ ERR_PACK(0, PK11_F_LOAD_PUBKEY, 0),		"PK11_LOAD_PUBKEY"},
97{ ERR_PACK(0, PK11_F_LOAD_PRIVKEY, 0),		"PK11_LOAD_PRIV_KEY"},
98{ ERR_PACK(0, PK11_F_RSA_PUB_ENC_LOW, 0),	"PK11_RSA_PUB_ENC_LOW"},
99{ ERR_PACK(0, PK11_F_RSA_PRIV_ENC_LOW, 0),	"PK11_RSA_PRIV_ENC_LOW"},
100{ ERR_PACK(0, PK11_F_RSA_PUB_DEC_LOW, 0),	"PK11_RSA_PUB_DEC_LOW"},
101{ ERR_PACK(0, PK11_F_RSA_PRIV_DEC_LOW, 0),	"PK11_RSA_PRIV_DEC_LOW"},
102{ ERR_PACK(0, PK11_F_DSA_SIGN, 0),		"PK11_DSA_SIGN"},
103{ ERR_PACK(0, PK11_F_DSA_VERIFY, 0),		"PK11_DSA_VERIFY"},
104{ ERR_PACK(0, PK11_F_DSA_INIT, 0),		"PK11_DSA_INIT"},
105{ ERR_PACK(0, PK11_F_DSA_FINISH, 0),		"PK11_DSA_FINISH"},
106{ ERR_PACK(0, PK11_F_GET_PUB_DSA_KEY, 0),	"PK11_GET_PUB_DSA_KEY"},
107{ ERR_PACK(0, PK11_F_GET_PRIV_DSA_KEY, 0),	"PK11_GET_PRIV_DSA_KEY"},
108{ ERR_PACK(0, PK11_F_DH_INIT, 0),		"PK11_DH_INIT"},
109{ ERR_PACK(0, PK11_F_DH_FINISH, 0),		"PK11_DH_FINISH"},
110{ ERR_PACK(0, PK11_F_MOD_EXP_DH, 0),		"PK11_MOD_EXP_DH"},
111{ ERR_PACK(0, PK11_F_GET_DH_KEY, 0),		"PK11_GET_DH_KEY"},
112{ ERR_PACK(0, PK11_F_FREE_ALL_SESSIONS, 0),	"PK11_FREE_ALL_SESSIONS"},
113{ ERR_PACK(0, PK11_F_SETUP_SESSION, 0),		"PK11_SETUP_SESSION"},
114{ ERR_PACK(0, PK11_F_DESTROY_OBJECT, 0),	"PK11_DESTROY_OBJECT"},
115{ ERR_PACK(0, PK11_F_CIPHER_INIT, 0),		"PK11_CIPHER_INIT"},
116{ ERR_PACK(0, PK11_F_CIPHER_DO_CIPHER, 0),	"PK11_CIPHER_DO_CIPHER"},
117{ ERR_PACK(0, PK11_F_GET_CIPHER_KEY, 0),	"PK11_GET_CIPHER_KEY"},
118{ ERR_PACK(0, PK11_F_DIGEST_INIT, 0),		"PK11_DIGEST_INIT"},
119{ ERR_PACK(0, PK11_F_DIGEST_UPDATE, 0),		"PK11_DIGEST_UPDATE"},
120{ ERR_PACK(0, PK11_F_DIGEST_FINAL, 0),		"PK11_DIGEST_FINAL"},
121{ ERR_PACK(0, PK11_F_CHOOSE_SLOT, 0),		"PK11_CHOOSE_SLOT"},
122{ ERR_PACK(0, PK11_F_CIPHER_FINAL, 0),		"PK11_CIPHER_FINAL"},
123{ ERR_PACK(0, PK11_F_LIBRARY_INIT, 0),		"PK11_LIBRARY_INIT"},
124{ ERR_PACK(0, PK11_F_LOAD, 0),			"ENGINE_LOAD_PK11"},
125{ ERR_PACK(0, PK11_F_DH_GEN_KEY, 0),		"PK11_DH_GEN_KEY"},
126{ ERR_PACK(0, PK11_F_DH_COMP_KEY, 0),		"PK11_DH_COMP_KEY"},
127{ ERR_PACK(0, PK11_F_DIGEST_COPY, 0),		"PK11_DIGEST_COPY"},
128{ ERR_PACK(0, PK11_F_CIPHER_CLEANUP, 0),	"PK11_CIPHER_CLEANUP"},
129{ ERR_PACK(0, PK11_F_ACTIVE_ADD, 0),		"PK11_ACTIVE_ADD"},
130{ ERR_PACK(0, PK11_F_ACTIVE_DELETE, 0),		"PK11_ACTIVE_DELETE"},
131{ ERR_PACK(0, PK11_F_CHECK_HW_MECHANISMS, 0),	"PK11_CHECK_HW_MECHANISMS"},
132{ ERR_PACK(0, PK11_F_INIT_SYMMETRIC, 0),	"PK11_INIT_SYMMETRIC"},
133{ ERR_PACK(0, PK11_F_ADD_AES_CTR_NIDS, 0),	"PK11_ADD_AES_CTR_NIDS"},
134{ 0, NULL}
135};
136
137static ERR_STRING_DATA pk11_str_reasons[]=
138{
139{ PK11_R_ALREADY_LOADED,		"PKCS#11 DSO already loaded"},
140{ PK11_R_DSO_FAILURE,			"unable to load PKCS#11 DSO"},
141{ PK11_R_NOT_LOADED,			"PKCS#11 DSO not loaded"},
142{ PK11_R_PASSED_NULL_PARAMETER,		"null parameter passed"},
143{ PK11_R_COMMAND_NOT_IMPLEMENTED,	"command not implemented"},
144{ PK11_R_INITIALIZE,			"C_Initialize failed"},
145{ PK11_R_FINALIZE,			"C_Finalize failed"},
146{ PK11_R_GETINFO,			"C_GetInfo faile"},
147{ PK11_R_GETSLOTLIST,			"C_GetSlotList failed"},
148{ PK11_R_NO_MODULUS_OR_NO_EXPONENT,	"no modulus or no exponent"},
149{ PK11_R_ATTRIBUT_SENSITIVE_OR_INVALID,	"attr sensitive or invalid"},
150{ PK11_R_GETATTRIBUTVALUE,		"C_GetAttributeValue failed"},
151{ PK11_R_NO_MODULUS,			"no modulus"},
152{ PK11_R_NO_EXPONENT,			"no exponent"},
153{ PK11_R_FINDOBJECTSINIT,		"C_FindObjectsInit failed"},
154{ PK11_R_FINDOBJECTS,			"C_FindObjects failed"},
155{ PK11_R_FINDOBJECTSFINAL,		"C_FindObjectsFinal failed"},
156{ PK11_R_CREATEOBJECT,			"C_CreateObject failed"},
157{ PK11_R_DESTROYOBJECT,			"C_DestroyObject failed"},
158{ PK11_R_OPENSESSION,			"C_OpenSession failed"},
159{ PK11_R_CLOSESESSION,			"C_CloseSession failed"},
160{ PK11_R_ENCRYPTINIT,			"C_EncryptInit failed"},
161{ PK11_R_ENCRYPT,			"C_Encrypt failed"},
162{ PK11_R_SIGNINIT,			"C_SignInit failed"},
163{ PK11_R_SIGN,				"C_Sign failed"},
164{ PK11_R_DECRYPTINIT,			"C_DecryptInit failed"},
165{ PK11_R_DECRYPT,			"C_Decrypt failed"},
166{ PK11_R_VERIFYINIT,			"C_VerifyRecover failed"},
167{ PK11_R_VERIFY,			"C_Verify failed"},
168{ PK11_R_VERIFYRECOVERINIT,		"C_VerifyRecoverInit failed"},
169{ PK11_R_VERIFYRECOVER,			"C_VerifyRecover failed"},
170{ PK11_R_GEN_KEY,			"C_GenerateKeyPair failed"},
171{ PK11_R_SEEDRANDOM,			"C_SeedRandom failed"},
172{ PK11_R_GENERATERANDOM,		"C_GenerateRandom failed"},
173{ PK11_R_INVALID_MESSAGE_LENGTH,	"invalid message length"},
174{ PK11_R_UNKNOWN_ALGORITHM_TYPE,	"unknown algorithm type"},
175{ PK11_R_UNKNOWN_ASN1_OBJECT_ID,	"unknown asn1 onject id"},
176{ PK11_R_UNKNOWN_PADDING_TYPE,		"unknown padding type"},
177{ PK11_R_PADDING_CHECK_FAILED,		"padding check failed"},
178{ PK11_R_DIGEST_TOO_BIG,		"digest too big"},
179{ PK11_R_MALLOC_FAILURE,		"malloc failure"},
180{ PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED,	"ctl command not implemented"},
181{ PK11_R_DATA_GREATER_THAN_MOD_LEN,	"data is bigger than mod"},
182{ PK11_R_DATA_TOO_LARGE_FOR_MODULUS,	"data is too larger for mod"},
183{ PK11_R_MISSING_KEY_COMPONENT,		"a dsa component is missing"},
184{ PK11_R_INVALID_SIGNATURE_LENGTH,	"invalid signature length"},
185{ PK11_R_INVALID_DSA_SIGNATURE_R,	"missing r in dsa verify"},
186{ PK11_R_INVALID_DSA_SIGNATURE_S,	"missing s in dsa verify"},
187{ PK11_R_INCONSISTENT_KEY,		"inconsistent key type"},
188{ PK11_R_ENCRYPTUPDATE,			"C_EncryptUpdate failed"},
189{ PK11_R_DECRYPTUPDATE,			"C_DecryptUpdate failed"},
190{ PK11_R_DIGESTINIT,			"C_DigestInit failed"},
191{ PK11_R_DIGESTUPDATE,			"C_DigestUpdate failed"},
192{ PK11_R_DIGESTFINAL,			"C_DigestFinal failed"},
193{ PK11_R_ENCRYPTFINAL,			"C_EncryptFinal failed"},
194{ PK11_R_DECRYPTFINAL,			"C_DecryptFinal failed"},
195{ PK11_R_NO_PRNG_SUPPORT,		"Slot does not support PRNG"},
196{ PK11_R_GETTOKENINFO,			"C_GetTokenInfo failed"},
197{ PK11_R_DERIVEKEY,			"C_DeriveKey failed"},
198{ PK11_R_GET_OPERATION_STATE,		"C_GetOperationState failed"},
199{ PK11_R_SET_OPERATION_STATE,		"C_SetOperationState failed"},
200{ PK11_R_INVALID_HANDLE,		"invalid PKCS#11 object handle"},
201{ PK11_R_KEY_OR_IV_LEN_PROBLEM,		"IV or key length incorrect"},
202{ PK11_R_INVALID_OPERATION_TYPE,	"invalid operation type"},
203{ PK11_R_ADD_NID_FAILED,		"failed to add NID" },
204{ 0,	NULL}
205};
206#endif	/* OPENSSL_NO_ERR */
207
208static int pk11_lib_error_code = 0;
209static int pk11_error_init = 1;
210
211static void
212ERR_load_pk11_strings(void)
213	{
214	if (pk11_lib_error_code == 0)
215		pk11_lib_error_code = ERR_get_next_error_library();
216
217	if (pk11_error_init)
218		{
219		pk11_error_init = 0;
220#ifndef OPENSSL_NO_ERR
221		ERR_load_strings(pk11_lib_error_code, pk11_str_functs);
222		ERR_load_strings(pk11_lib_error_code, pk11_str_reasons);
223#endif
224		}
225}
226
227static void
228ERR_unload_pk11_strings(void)
229	{
230	if (pk11_error_init == 0)
231		{
232#ifndef OPENSSL_NO_ERR
233		ERR_unload_strings(pk11_lib_error_code, pk11_str_functs);
234		ERR_unload_strings(pk11_lib_error_code, pk11_str_reasons);
235#endif
236		pk11_error_init = 1;
237		}
238}
239
240void
241ERR_pk11_error(int function, int reason, char *file, int line)
242{
243	if (pk11_lib_error_code == 0)
244		pk11_lib_error_code = ERR_get_next_error_library();
245	ERR_PUT_error(pk11_lib_error_code, function, reason, file, line);
246}
247
248void
249PK11err_add_data(int function, int reason, CK_RV rv)
250{
251	char tmp_buf[20];
252
253	PK11err(function, reason);
254	(void) snprintf(tmp_buf, sizeof (tmp_buf), "%lx", rv);
255	ERR_add_error_data(2, "PK11 CK_RV=0X", tmp_buf);
256}
257