• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #include <stdint.h>
13 #include <openssl/err.h>
14 #include <tee_crypto_err.h>
15 #include <tee_err.h>
16 #include <tee_log.h>
17 
18 struct crypto_err_lib_t {
19     int32_t opensource_err_lib;
20     int32_t crypt_err_lib;
21 };
22 
23 static struct crypto_err_lib_t g_crypto_err_lib[] = {
24     { ERR_LIB_BN,     BN_LIB_ERR_ID     },
25     { ERR_LIB_RSA,    RSA_LIB_ERR_ID    },
26     { ERR_LIB_EVP,    EVP_LIB_ERR_ID    },
27     { ERR_LIB_PEM,    PEM_LIB_ERR_ID    },
28     { ERR_LIB_X509,   X509_LIB_ERR_ID   },
29     { ERR_LIB_ASN1,   ASN1_LIB_ERR_ID   },
30     { ERR_LIB_CRYPTO, CRYPTO_LIB_ERR_ID },
31     { ERR_LIB_EC,     EC_LIB_ERR_ID     },
32     { ERR_LIB_PKCS7,  PKCS7_LIB_ERR_ID  },
33 };
34 
get_soft_crypto_error(int32_t tee_error)35 int32_t get_soft_crypto_error(int32_t tee_error)
36 {
37     uint32_t err_status = (uint32_t)ERR_peek_last_error();
38     int32_t engine_error = ERR_GET_REASON(err_status);
39     if (engine_error == 0)
40         return tee_error;
41 
42     int32_t lib_error_id = ERR_GET_LIB(err_status);
43 
44     /* clear opensource lib err state */
45     ERR_clear_error();
46 
47     /* for common err(<100), opensource err lib was merged into COMM_LIB_ERR_ID */
48     if (engine_error <= MAX_COMMON_CRYPTO_ENGINE_ERR)
49         return TEE_EXT_ERROR_BASE | CRYPTO_MODULE_ERR_ID | COMM_LIB_ERR_ID | (uint32_t)engine_error;
50 
51     for (size_t i = 0; i < sizeof(g_crypto_err_lib) / sizeof(g_crypto_err_lib[0]); i++) {
52         if (lib_error_id == g_crypto_err_lib[i].opensource_err_lib)
53             return TEE_EXT_ERROR_BASE | CRYPTO_MODULE_ERR_ID |
54                    (uint32_t)g_crypto_err_lib[i].crypt_err_lib | (uint32_t)engine_error;
55     }
56 
57     return TEE_EXT_ERROR_BASE | CRYPTO_MODULE_ERR_ID | OTHER_LIB_ERR_ID | (uint32_t)engine_error;
58 }
59