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/opensslconf.h>
  71 #include <openssl/err.h>
  72 #include "hw_pk11_err.h"
  73 
  74 /* BEGIN ERROR CODES */
  75 #ifndef OPENSSL_NO_ERR
  76 static ERR_STRING_DATA pk11_str_functs[]=
  77 {
  78 { ERR_PACK(0, PK11_F_INIT, 0),                  "PK11_INIT"},
  79 { ERR_PACK(0, PK11_F_FINISH, 0),                "PK11_FINISH"},
  80 { ERR_PACK(0, PK11_F_DESTROY, 0),               "PK11_DESTROY"},
  81 { ERR_PACK(0, PK11_F_CTRL, 0),                  "PK11_CTRL"},
  82 { ERR_PACK(0, PK11_F_RSA_INIT, 0),              "PK11_RSA_INIT"},
  83 { ERR_PACK(0, PK11_F_RSA_FINISH, 0),            "PK11_RSA_FINISH"},
  84 { ERR_PACK(0, PK11_F_GET_PUB_RSA_KEY, 0),       "PK11_GET_PUB_RSA_KEY"},
  85 { ERR_PACK(0, PK11_F_GET_PRIV_RSA_KEY, 0),      "PK11_GET_PRIV_RSA_KEY"},
  86 { ERR_PACK(0, PK11_F_RSA_GEN_KEY, 0),           "PK11_RSA_GEN_KEY"},
  87 { ERR_PACK(0, PK11_F_RSA_PUB_ENC, 0),           "PK11_RSA_PUB_ENC"},
  88 { ERR_PACK(0, PK11_F_RSA_PRIV_ENC, 0),          "PK11_RSA_PRIV_ENC"},
  89 { ERR_PACK(0, PK11_F_RSA_PUB_DEC, 0),           "PK11_RSA_PUB_DEC"},
  90 { ERR_PACK(0, PK11_F_RSA_PRIV_DEC, 0),          "PK11_RSA_PRIV_DEC"},
  91 { ERR_PACK(0, PK11_F_RSA_SIGN, 0),              "PK11_RSA_SIGN"},
  92 { ERR_PACK(0, PK11_F_RSA_VERIFY, 0),            "PK11_RSA_VERIFY"},
  93 { ERR_PACK(0, PK11_F_RAND_ADD, 0),              "PK11_RAND_ADD"},
  94 { ERR_PACK(0, PK11_F_RAND_BYTES, 0),            "PK11_RAND_BYTES"},
  95 { ERR_PACK(0, PK11_F_GET_SESSION, 0),           "PK11_GET_SESSION"},
  96 { ERR_PACK(0, PK11_F_FREE_SESSION, 0),          "PK11_FREE_SESSION"},
  97 { ERR_PACK(0, PK11_F_LOAD_PUBKEY, 0),           "PK11_LOAD_PUBKEY"},
  98 { ERR_PACK(0, PK11_F_LOAD_PRIVKEY, 0),          "PK11_LOAD_PRIV_KEY"},
  99 { ERR_PACK(0, PK11_F_RSA_PUB_ENC_LOW, 0),       "PK11_RSA_PUB_ENC_LOW"},
 100 { ERR_PACK(0, PK11_F_RSA_PRIV_ENC_LOW, 0),      "PK11_RSA_PRIV_ENC_LOW"},
 101 { ERR_PACK(0, PK11_F_RSA_PUB_DEC_LOW, 0),       "PK11_RSA_PUB_DEC_LOW"},
 102 { ERR_PACK(0, PK11_F_RSA_PRIV_DEC_LOW, 0),      "PK11_RSA_PRIV_DEC_LOW"},
 103 { ERR_PACK(0, PK11_F_DSA_SIGN, 0),              "PK11_DSA_SIGN"},
 104 { ERR_PACK(0, PK11_F_DSA_VERIFY, 0),            "PK11_DSA_VERIFY"},
 105 { ERR_PACK(0, PK11_F_DSA_INIT, 0),              "PK11_DSA_INIT"},
 106 { ERR_PACK(0, PK11_F_DSA_FINISH, 0),            "PK11_DSA_FINISH"},
 107 { ERR_PACK(0, PK11_F_GET_PUB_DSA_KEY, 0),       "PK11_GET_PUB_DSA_KEY"},
 108 { ERR_PACK(0, PK11_F_GET_PRIV_DSA_KEY, 0),      "PK11_GET_PRIV_DSA_KEY"},
 109 { ERR_PACK(0, PK11_F_DH_INIT, 0),               "PK11_DH_INIT"},
 110 { ERR_PACK(0, PK11_F_DH_FINISH, 0),             "PK11_DH_FINISH"},
 111 { ERR_PACK(0, PK11_F_MOD_EXP_DH, 0),            "PK11_MOD_EXP_DH"},
 112 { ERR_PACK(0, PK11_F_GET_DH_KEY, 0),            "PK11_GET_DH_KEY"},
 113 { ERR_PACK(0, PK11_F_FREE_ALL_SESSIONS, 0),     "PK11_FREE_ALL_SESSIONS"},
 114 { ERR_PACK(0, PK11_F_SETUP_SESSION, 0),         "PK11_SETUP_SESSION"},
 115 { ERR_PACK(0, PK11_F_DESTROY_OBJECT, 0),        "PK11_DESTROY_OBJECT"},
 116 { ERR_PACK(0, PK11_F_CIPHER_INIT, 0),           "PK11_CIPHER_INIT"},
 117 { ERR_PACK(0, PK11_F_CIPHER_DO_CIPHER, 0),      "PK11_CIPHER_DO_CIPHER"},
 118 { ERR_PACK(0, PK11_F_GET_CIPHER_KEY, 0),        "PK11_GET_CIPHER_KEY"},
 119 { ERR_PACK(0, PK11_F_DIGEST_INIT, 0),           "PK11_DIGEST_INIT"},
 120 { ERR_PACK(0, PK11_F_DIGEST_UPDATE, 0),         "PK11_DIGEST_UPDATE"},
 121 { ERR_PACK(0, PK11_F_DIGEST_FINAL, 0),          "PK11_DIGEST_FINAL"},
 122 { ERR_PACK(0, PK11_F_CHOOSE_SLOT, 0),           "PK11_CHOOSE_SLOT"},
 123 { ERR_PACK(0, PK11_F_CIPHER_FINAL, 0),          "PK11_CIPHER_FINAL"},
 124 { ERR_PACK(0, PK11_F_LIBRARY_INIT, 0),          "PK11_LIBRARY_INIT"},
 125 { ERR_PACK(0, PK11_F_LOAD, 0),                  "ENGINE_LOAD_PK11"},
 126 { ERR_PACK(0, PK11_F_DH_GEN_KEY, 0),            "PK11_DH_GEN_KEY"},
 127 { ERR_PACK(0, PK11_F_DH_COMP_KEY, 0),           "PK11_DH_COMP_KEY"},
 128 { ERR_PACK(0, PK11_F_DIGEST_COPY, 0),           "PK11_DIGEST_COPY"},
 129 { ERR_PACK(0, PK11_F_CIPHER_CLEANUP, 0),        "PK11_CIPHER_CLEANUP"},
 130 { ERR_PACK(0, PK11_F_ACTIVE_ADD, 0),            "PK11_ACTIVE_ADD"},
 131 { ERR_PACK(0, PK11_F_ACTIVE_DELETE, 0),         "PK11_ACTIVE_DELETE"},
 132 { ERR_PACK(0, PK11_F_CHECK_HW_MECHANISMS, 0),   "PK11_CHECK_HW_MECHANISMS"},
 133 { ERR_PACK(0, PK11_F_INIT_SYMMETRIC, 0),        "PK11_INIT_SYMMETRIC"},
 134 { ERR_PACK(0, PK11_F_ADD_AES_CTR_NIDS, 0),      "PK11_ADD_AES_CTR_NIDS"},
 135 { 0, NULL}
 136 };
 137 
 138 static ERR_STRING_DATA pk11_str_reasons[]=
 139 {
 140 { PK11_R_ALREADY_LOADED,                "PKCS#11 DSO already loaded"},
 141 { PK11_R_DSO_FAILURE,                   "unable to load PKCS#11 DSO"},
 142 { PK11_R_NOT_LOADED,                    "PKCS#11 DSO not loaded"},
 143 { PK11_R_PASSED_NULL_PARAMETER,         "null parameter passed"},
 144 { PK11_R_COMMAND_NOT_IMPLEMENTED,       "command not implemented"},
 145 { PK11_R_INITIALIZE,                    "C_Initialize failed"},
 146 { PK11_R_FINALIZE,                      "C_Finalize failed"},
 147 { PK11_R_GETINFO,                       "C_GetInfo faile"},
 148 { PK11_R_GETSLOTLIST,                   "C_GetSlotList failed"},
 149 { PK11_R_NO_MODULUS_OR_NO_EXPONENT,     "no modulus or no exponent"},
 150 { PK11_R_ATTRIBUT_SENSITIVE_OR_INVALID, "attr sensitive or invalid"},
 151 { PK11_R_GETATTRIBUTVALUE,              "C_GetAttributeValue failed"},
 152 { PK11_R_NO_MODULUS,                    "no modulus"},
 153 { PK11_R_NO_EXPONENT,                   "no exponent"},
 154 { PK11_R_FINDOBJECTSINIT,               "C_FindObjectsInit failed"},
 155 { PK11_R_FINDOBJECTS,                   "C_FindObjects failed"},
 156 { PK11_R_FINDOBJECTSFINAL,              "C_FindObjectsFinal failed"},
 157 { PK11_R_CREATEOBJECT,                  "C_CreateObject failed"},
 158 { PK11_R_DESTROYOBJECT,                 "C_DestroyObject failed"},
 159 { PK11_R_OPENSESSION,                   "C_OpenSession failed"},
 160 { PK11_R_CLOSESESSION,                  "C_CloseSession failed"},
 161 { PK11_R_ENCRYPTINIT,                   "C_EncryptInit failed"},
 162 { PK11_R_ENCRYPT,                       "C_Encrypt failed"},
 163 { PK11_R_SIGNINIT,                      "C_SignInit failed"},
 164 { PK11_R_SIGN,                          "C_Sign failed"},
 165 { PK11_R_DECRYPTINIT,                   "C_DecryptInit failed"},
 166 { PK11_R_DECRYPT,                       "C_Decrypt failed"},
 167 { PK11_R_VERIFYINIT,                    "C_VerifyRecover failed"},
 168 { PK11_R_VERIFY,                        "C_Verify failed"},
 169 { PK11_R_VERIFYRECOVERINIT,             "C_VerifyRecoverInit failed"},
 170 { PK11_R_VERIFYRECOVER,                 "C_VerifyRecover failed"},
 171 { PK11_R_GEN_KEY,                       "C_GenerateKeyPair failed"},
 172 { PK11_R_SEEDRANDOM,                    "C_SeedRandom failed"},
 173 { PK11_R_GENERATERANDOM,                "C_GenerateRandom failed"},
 174 { PK11_R_INVALID_MESSAGE_LENGTH,        "invalid message length"},
 175 { PK11_R_UNKNOWN_ALGORITHM_TYPE,        "unknown algorithm type"},
 176 { PK11_R_UNKNOWN_ASN1_OBJECT_ID,        "unknown asn1 onject id"},
 177 { PK11_R_UNKNOWN_PADDING_TYPE,          "unknown padding type"},
 178 { PK11_R_PADDING_CHECK_FAILED,          "padding check failed"},
 179 { PK11_R_DIGEST_TOO_BIG,                "digest too big"},
 180 { PK11_R_MALLOC_FAILURE,                "malloc failure"},
 181 { PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED,  "ctl command not implemented"},
 182 { PK11_R_DATA_GREATER_THAN_MOD_LEN,     "data is bigger than mod"},
 183 { PK11_R_DATA_TOO_LARGE_FOR_MODULUS,    "data is too larger for mod"},
 184 { PK11_R_MISSING_KEY_COMPONENT,         "a dsa component is missing"},
 185 { PK11_R_INVALID_SIGNATURE_LENGTH,      "invalid signature length"},
 186 { PK11_R_INVALID_DSA_SIGNATURE_R,       "missing r in dsa verify"},
 187 { PK11_R_INVALID_DSA_SIGNATURE_S,       "missing s in dsa verify"},
 188 { PK11_R_INCONSISTENT_KEY,              "inconsistent key type"},
 189 { PK11_R_ENCRYPTUPDATE,                 "C_EncryptUpdate failed"},
 190 { PK11_R_DECRYPTUPDATE,                 "C_DecryptUpdate failed"},
 191 { PK11_R_DIGESTINIT,                    "C_DigestInit failed"},
 192 { PK11_R_DIGESTUPDATE,                  "C_DigestUpdate failed"},
 193 { PK11_R_DIGESTFINAL,                   "C_DigestFinal failed"},
 194 { PK11_R_ENCRYPTFINAL,                  "C_EncryptFinal failed"},
 195 { PK11_R_DECRYPTFINAL,                  "C_DecryptFinal failed"},
 196 { PK11_R_NO_PRNG_SUPPORT,               "Slot does not support PRNG"},
 197 { PK11_R_GETTOKENINFO,                  "C_GetTokenInfo failed"},
 198 { PK11_R_DERIVEKEY,                     "C_DeriveKey failed"},
 199 { PK11_R_GET_OPERATION_STATE,           "C_GetOperationState failed"},
 200 { PK11_R_SET_OPERATION_STATE,           "C_SetOperationState failed"},
 201 { PK11_R_INVALID_HANDLE,                "invalid PKCS#11 object handle"},
 202 { PK11_R_KEY_OR_IV_LEN_PROBLEM,         "IV or key length incorrect"},
 203 { PK11_R_INVALID_OPERATION_TYPE,        "invalid operation type"},
 204 { PK11_R_ADD_NID_FAILED,                "failed to add NID" },
 205 { 0,    NULL}
 206 };
 207 #endif  /* OPENSSL_NO_ERR */
 208 
 209 static int pk11_lib_error_code = 0;
 210 static int pk11_error_init = 1;
 211 
 212 static void
 213 ERR_load_pk11_strings(void)
 214         {
 215         if (pk11_lib_error_code == 0)
 216                 pk11_lib_error_code = ERR_get_next_error_library();
 217 
 218         if (pk11_error_init)
 219                 {
 220                 pk11_error_init = 0;
 221 #ifndef OPENSSL_NO_ERR
 222                 ERR_load_strings(pk11_lib_error_code, pk11_str_functs);
 223                 ERR_load_strings(pk11_lib_error_code, pk11_str_reasons);
 224 #endif
 225                 }
 226 }
 227 
 228 static void
 229 ERR_unload_pk11_strings(void)
 230         {
 231         if (pk11_error_init == 0)
 232                 {
 233 #ifndef OPENSSL_NO_ERR
 234                 ERR_unload_strings(pk11_lib_error_code, pk11_str_functs);
 235                 ERR_unload_strings(pk11_lib_error_code, pk11_str_reasons);
 236 #endif
 237                 pk11_error_init = 1;
 238                 }
 239 }
 240 
 241 void
 242 ERR_pk11_error(int function, int reason, char *file, int line)
 243 {
 244         if (pk11_lib_error_code == 0)
 245                 pk11_lib_error_code = ERR_get_next_error_library();
 246         ERR_PUT_error(pk11_lib_error_code, function, reason, file, line);
 247 }
 248 
 249 void
 250 PK11err_add_data(int function, int reason, CK_RV rv)
 251 {
 252         char tmp_buf[20];
 253 
 254         PK11err(function, reason);
 255         (void) snprintf(tmp_buf, sizeof (tmp_buf), "%lx", rv);
 256         ERR_add_error_data(2, "PK11 CK_RV=0X", tmp_buf);
 257 }