1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "c_mock_common.h"
16
17 #include <dlfcn.h>
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/rand.h>
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 static const std::string OPENSSL_LIB_PATH = "libcrypto_openssl.z.so";
27
28 typedef int (*RandBytesFunc)(unsigned char *buf, int num);
29 typedef const EVP_CIPHER *(*EvpAes128Ctr)(void);
30 typedef const EVP_CIPHER *(*EvpAes192Ctr)(void);
31 typedef const EVP_CIPHER *(*EvpAes256Ctr)(void);
32 typedef unsigned long (*ErrGetError)(void);
33 typedef void (*ErrErrorStringN)(unsigned long e, char *buf, size_t len);
34 typedef EVP_CIPHER_CTX *(*EvpCipherCtxNew)(void);
35 typedef int (*EvpEncryptInitEx)(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
36 const unsigned char *key, const unsigned char *iv);
37 typedef int (*EvpDecryptInitEx)(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
38 const unsigned char *key, const unsigned char *iv);
39 typedef void (*EvpCipherCtxFree)(EVP_CIPHER_CTX *c);
40 typedef int (*EvpCipherCtxSetPadding)(EVP_CIPHER_CTX *c, int pad);
41 typedef int (*EvpEncryptUpdate)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl);
42 typedef int (*EvpEncryptFinalEx)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
43 typedef int (*EvpDecryptUpdate)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl);
44 typedef int (*EvpDecryptFinalEx)(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
45 typedef const EVP_MD *(*EvpSha256)(void);
46 typedef const EVP_MD *(*EvpSha384)(void);
47 typedef const EVP_MD *(*EvpSha512)(void);
48 typedef int (*EvpDigest)(const void *data, size_t count, unsigned char *md, unsigned int *size,
49 const EVP_MD *type, ENGINE *impl);
50 typedef EVP_MD_CTX *(*EvpMDCtxNew)(void);
51 typedef int (*EvpDigestInitEx)(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
52 typedef void (*EvpMdCtxSetFlags)(EVP_MD_CTX *ctx, int flags);
53 typedef void (*EvpMdCtxFree)(EVP_MD_CTX *ctx);
54 typedef int (*EvpDigestUpdate)(EVP_MD_CTX *ctx, const void *d, size_t cnt);
55 typedef int (*EvpDigestFinalEx)(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
56
57 static void *g_opensslHandle = nullptr;
58
GetOpensslLibFunc(const char * funcName)59 static void *GetOpensslLibFunc(const char *funcName)
60 {
61 if (g_opensslHandle == nullptr) {
62 g_opensslHandle = dlopen(OPENSSL_LIB_PATH.c_str(), RTLD_LAZY);
63 if (g_opensslHandle == nullptr) {
64 return nullptr;
65 }
66 }
67
68 return dlsym(g_opensslHandle, funcName);
69 }
70
RAND_bytes(unsigned char * buf,int num)71 int RAND_bytes(unsigned char *buf, int num)
72 {
73 if (IsFuncNeedMock("RAND_bytes")) {
74 return -1;
75 }
76
77 RandBytesFunc func = reinterpret_cast<RandBytesFunc>(GetOpensslLibFunc("RAND_bytes"));
78 if (func == nullptr) {
79 return -1;
80 }
81 return (*func)(buf, num);
82 }
83
EVP_aes_128_ctr(void)84 const EVP_CIPHER *EVP_aes_128_ctr(void)
85 {
86 if (IsFuncNeedMock("EVP_aes_128_ctr")) {
87 return nullptr;
88 }
89
90 EvpAes128Ctr func = reinterpret_cast<EvpAes128Ctr>(GetOpensslLibFunc("EVP_aes_128_ctr"));
91 if (func == nullptr) {
92 return nullptr;
93 }
94 return (*func)();
95 }
96
EVP_aes_192_ctr(void)97 const EVP_CIPHER *EVP_aes_192_ctr(void)
98 {
99 if (IsFuncNeedMock("EVP_aes_192_ctr")) {
100 return nullptr;
101 }
102
103 EvpAes192Ctr func = reinterpret_cast<EvpAes192Ctr>(GetOpensslLibFunc("EVP_aes_192_ctr"));
104 if (func == nullptr) {
105 return nullptr;
106 }
107 return (*func)();
108 }
109
EVP_aes_256_ctr(void)110 const EVP_CIPHER *EVP_aes_256_ctr(void)
111 {
112 if (IsFuncNeedMock("EVP_aes_256_ctr")) {
113 return nullptr;
114 }
115
116 EvpAes256Ctr func = reinterpret_cast<EvpAes256Ctr>(GetOpensslLibFunc("EVP_aes_256_ctr"));
117 if (func == nullptr) {
118 return nullptr;
119 }
120 return (*func)();
121 }
122
ERR_get_error(void)123 unsigned long ERR_get_error(void)
124 {
125 if (IsFuncNeedMock("ERR_get_error")) {
126 return 0;
127 }
128
129 ErrGetError func = reinterpret_cast<ErrGetError>(GetOpensslLibFunc("ERR_get_error"));
130 if (func == nullptr) {
131 return 0;
132 }
133 return (*func)();
134 }
135
ERR_error_string_n(unsigned long e,char * buf,size_t len)136 void ERR_error_string_n(unsigned long e, char *buf, size_t len)
137 {
138 if (IsFuncNeedMock("ERR_error_string_n")) {
139 return;
140 }
141
142 ErrErrorStringN func = reinterpret_cast<ErrErrorStringN>(GetOpensslLibFunc("ERR_error_string_n"));
143 if (func == nullptr) {
144 return;
145 }
146 (*func)(e, buf, len);
147 }
148
EVP_CIPHER_CTX_new(void)149 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
150 {
151 if (IsFuncNeedMock("EVP_CIPHER_CTX_new")) {
152 return nullptr;
153 }
154
155 EvpCipherCtxNew func = reinterpret_cast<EvpCipherCtxNew>(GetOpensslLibFunc("EVP_CIPHER_CTX_new"));
156 if (func == nullptr) {
157 return nullptr;
158 }
159 return (*func)();
160 }
161
EVP_EncryptInit_ex(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,ENGINE * impl,const unsigned char * key,const unsigned char * iv)162 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
163 const unsigned char *key, const unsigned char *iv)
164 {
165 if (IsFuncNeedMock("EVP_EncryptInit_ex")) {
166 return -1;
167 }
168
169 EvpEncryptInitEx func = reinterpret_cast<EvpEncryptInitEx>(GetOpensslLibFunc("EVP_EncryptInit_ex"));
170 if (func == nullptr) {
171 return -1;
172 }
173 return (*func)(ctx, cipher, impl, key, iv);
174 }
175
EVP_DecryptInit_ex(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher,ENGINE * impl,const unsigned char * key,const unsigned char * iv)176 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
177 const unsigned char *key, const unsigned char *iv)
178 {
179 if (IsFuncNeedMock("EVP_DecryptInit_ex")) {
180 return -1;
181 }
182
183 EvpDecryptInitEx func = reinterpret_cast<EvpDecryptInitEx>(GetOpensslLibFunc("EVP_DecryptInit_ex"));
184 if (func == nullptr) {
185 return -1;
186 }
187 return (*func)(ctx, cipher, impl, key, iv);
188 }
189
EVP_CIPHER_CTX_free(EVP_CIPHER_CTX * c)190 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *c)
191 {
192 if (IsFuncNeedMock("EVP_CIPHER_CTX_free")) {
193 return;
194 }
195
196 EvpCipherCtxFree func = reinterpret_cast<EvpCipherCtxFree>(GetOpensslLibFunc("EVP_CIPHER_CTX_free"));
197 if (func == nullptr) {
198 return;
199 }
200 (*func)(c);
201 }
202
EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX * c,int pad)203 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
204 {
205 if (IsFuncNeedMock("EVP_CIPHER_CTX_set_padding")) {
206 return -1;
207 }
208
209 EvpCipherCtxSetPadding func =
210 reinterpret_cast<EvpCipherCtxSetPadding>(GetOpensslLibFunc("EVP_CIPHER_CTX_set_padding"));
211 if (func == nullptr) {
212 return -1;
213 }
214 return (*func)(c, pad);
215 }
216
EVP_EncryptUpdate(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl,const unsigned char * in,int inl)217 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl)
218 {
219 if (IsFuncNeedMock("EVP_EncryptUpdate")) {
220 return -1;
221 }
222
223 EvpEncryptUpdate func =
224 reinterpret_cast<EvpEncryptUpdate>(GetOpensslLibFunc("EVP_EncryptUpdate"));
225 if (func == nullptr) {
226 return -1;
227 }
228 return (*func)(ctx, out, outl, in, inl);
229 }
230
EVP_EncryptFinal_ex(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl)231 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
232 {
233 if (IsFuncNeedMock("EVP_EncryptFinal_ex")) {
234 return -1;
235 }
236
237 EvpEncryptFinalEx func =
238 reinterpret_cast<EvpEncryptFinalEx>(GetOpensslLibFunc("EVP_EncryptFinal_ex"));
239 if (func == nullptr) {
240 return -1;
241 }
242 return (*func)(ctx, out, outl);
243 }
244
EVP_DecryptUpdate(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl,const unsigned char * in,int inl)245 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl)
246 {
247 if (IsFuncNeedMock("EVP_DecryptUpdate")) {
248 return -1;
249 }
250
251 EvpDecryptUpdate func =
252 reinterpret_cast<EvpDecryptUpdate>(GetOpensslLibFunc("EVP_DecryptUpdate"));
253 if (func == nullptr) {
254 return -1;
255 }
256 return (*func)(ctx, out, outl, in, inl);
257 }
258
EVP_DecryptFinal_ex(EVP_CIPHER_CTX * ctx,unsigned char * outm,int * outl)259 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl)
260 {
261 if (IsFuncNeedMock("EVP_DecryptFinal_ex")) {
262 return -1;
263 }
264
265 EvpDecryptFinalEx func =
266 reinterpret_cast<EvpDecryptFinalEx>(GetOpensslLibFunc("EVP_DecryptFinal_ex"));
267 if (func == nullptr) {
268 return -1;
269 }
270 return (*func)(ctx, outm, outl);
271 }
272
EVP_sha256(void)273 const EVP_MD *EVP_sha256(void)
274 {
275 if (IsFuncNeedMock("EVP_sha256")) {
276 return nullptr;
277 }
278
279 EvpSha256 func =
280 reinterpret_cast<EvpSha256>(GetOpensslLibFunc("EVP_sha256"));
281 if (func == nullptr) {
282 return nullptr;
283 }
284 return (*func)();
285 }
286
EVP_sha384(void)287 const EVP_MD *EVP_sha384(void)
288 {
289 if (IsFuncNeedMock("EVP_sha384")) {
290 return nullptr;
291 }
292
293 EvpSha384 func =
294 reinterpret_cast<EvpSha384>(GetOpensslLibFunc("EVP_sha384"));
295 if (func == nullptr) {
296 return nullptr;
297 }
298 return (*func)();
299 }
300
EVP_sha512(void)301 const EVP_MD *EVP_sha512(void)
302 {
303 if (IsFuncNeedMock("EVP_sha512")) {
304 return nullptr;
305 }
306
307 EvpSha512 func =
308 reinterpret_cast<EvpSha512>(GetOpensslLibFunc("EVP_sha512"));
309 if (func == nullptr) {
310 return nullptr;
311 }
312 return (*func)();
313 }
314
EVP_Digest(const void * data,size_t count,unsigned char * md,unsigned int * size,const EVP_MD * type,ENGINE * impl)315 int EVP_Digest(const void *data, size_t count, unsigned char *md, unsigned int *size,
316 const EVP_MD *type, ENGINE *impl)
317 {
318 if (IsFuncNeedMock("EVP_Digest")) {
319 return -1;
320 }
321
322 EvpDigest func =
323 reinterpret_cast<EvpDigest>(GetOpensslLibFunc("EVP_Digest"));
324 if (func == nullptr) {
325 return -1;
326 }
327 return (*func)(data, count, md, size, type, impl);
328 }
329
EVP_MD_CTX_new(void)330 EVP_MD_CTX *EVP_MD_CTX_new(void)
331 {
332 if (IsFuncNeedMock("EVP_MD_CTX_new")) {
333 return nullptr;
334 }
335
336 EvpMDCtxNew func =
337 reinterpret_cast<EvpMDCtxNew>(GetOpensslLibFunc("EVP_MD_CTX_new"));
338 if (func == nullptr) {
339 return nullptr;
340 }
341 return (*func)();
342 }
343
EVP_DigestInit_ex(EVP_MD_CTX * ctx,const EVP_MD * type,ENGINE * impl)344 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
345 {
346 if (IsFuncNeedMock("EVP_DigestInit_ex")) {
347 return -1;
348 }
349
350 EvpDigestInitEx func =
351 reinterpret_cast<EvpDigestInitEx>(GetOpensslLibFunc("EVP_DigestInit_ex"));
352 if (func == nullptr) {
353 return -1;
354 }
355 return (*func)(ctx, type, impl);
356 }
357
EVP_MD_CTX_set_flags(EVP_MD_CTX * ctx,int flags)358 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
359 {
360 if (IsFuncNeedMock("EVP_MD_CTX_set_flags")) {
361 return;
362 }
363
364 EvpMdCtxSetFlags func =
365 reinterpret_cast<EvpMdCtxSetFlags>(GetOpensslLibFunc("EVP_MD_CTX_set_flags"));
366 if (func == nullptr) {
367 return;
368 }
369 (*func)(ctx, flags);
370 }
371
EVP_MD_CTX_free(EVP_MD_CTX * ctx)372 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
373 {
374 if (IsFuncNeedMock("EVP_MD_CTX_free")) {
375 return;
376 }
377
378 EvpMdCtxFree func =
379 reinterpret_cast<EvpMdCtxFree>(GetOpensslLibFunc("EVP_MD_CTX_free"));
380 if (func == nullptr) {
381 return;
382 }
383 (*func)(ctx);
384 }
385
EVP_DigestUpdate(EVP_MD_CTX * ctx,const void * d,size_t cnt)386 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt)
387 {
388 if (IsFuncNeedMock("EVP_DigestUpdate")) {
389 return -1;
390 }
391
392 EvpDigestUpdate func =
393 reinterpret_cast<EvpDigestUpdate>(GetOpensslLibFunc("EVP_DigestUpdate"));
394 if (func == nullptr) {
395 return -1;
396 }
397 return (*func)(ctx, d, cnt);
398 }
399
EVP_DigestFinal_ex(EVP_MD_CTX * ctx,unsigned char * md,unsigned int * s)400 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s)
401 {
402 if (IsFuncNeedMock("EVP_DigestFinal_ex")) {
403 return -1;
404 }
405
406 EvpDigestFinalEx func =
407 reinterpret_cast<EvpDigestFinalEx>(GetOpensslLibFunc("EVP_DigestFinal_ex"));
408 if (func == nullptr) {
409 return -1;
410 }
411 return (*func)(ctx, md, s);
412 }
413 #ifdef __cplusplus
414 }
415 #endif
416