1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 2010-2020. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  */
20 
21 #include "info.h"
22 
23 #ifdef HAVE_DYNAMIC_CRYPTO_LIB
24 
25 # if defined(DEBUG)
26 char *crypto_callback_name = "crypto_callback.debug";
27 # elif defined(VALGRIND)
28 char *crypto_callback_name = "crypto_callback.valgrind";
29 # elif defined(ADDRESS_SANITIZER)
30 char *crypto_callback_name = "crypto_callback.asan";
31 # else
32 char *crypto_callback_name = "crypto_callback";
33 # endif
34 
change_basename(ErlNifBinary * bin,char * buf,size_t bufsz,const char * newfile)35 int change_basename(ErlNifBinary* bin, char* buf, size_t bufsz, const char* newfile)
36 {
37     size_t i;
38     size_t newlen;
39 
40     for (i = bin->size; i > 0; i--) {
41 	if (bin->data[i-1] == '/')
42 	    break;
43     }
44 
45     newlen = strlen(newfile);
46     if (i > SIZE_MAX - newlen)
47         goto err;
48 
49     if (i + newlen >= bufsz)
50         goto err;
51 
52     memcpy(buf, bin->data, i);
53     strcpy(buf+i, newfile);
54 
55     return 1;
56 
57  err:
58     return 0;
59 }
60 
error_handler(void * null,const char * errstr)61 void error_handler(void* null, const char* errstr)
62 {
63     PRINTF_ERR1("CRYPTO LOADING ERROR: '%s'", errstr);
64 }
65 #endif /* HAVE_DYNAMIC_CRYPTO_LIB */
66 
info_lib(ErlNifEnv * env,int argc,const ERL_NIF_TERM argv[])67 ERL_NIF_TERM info_lib(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
68 {/* () */
69     /* [{<<"OpenSSL">>,9470143,<<"OpenSSL 0.9.8k 25 Mar 2009">>}] */
70 
71     ERL_NIF_TERM name_term, ver_term;
72     static const char libname[] = "OpenSSL";
73     size_t name_sz;
74     const char* ver;
75     size_t ver_sz;
76     int ver_num;
77     unsigned char *out_name, *out_ver;
78 
79     ASSERT(argc == 0);
80 
81     name_sz = strlen(libname);
82     ver = SSLeay_version(SSLEAY_VERSION);
83     ver_sz = strlen(ver);
84     ver_num = OPENSSL_VERSION_NUMBER;
85 
86     /* R16:
87      * Ignore library version number from SSLeay() and instead show header
88      * version. Otherwise user might try to call a function that is implemented
89      * by a newer library but not supported by the headers used at compile time.
90      * Example: DES_ede3_cfb_encrypt in 0.9.7i but not in 0.9.7d.
91      *
92      * Version string is still from library though.
93      */
94 
95     if ((out_name = enif_make_new_binary(env, name_sz, &name_term)) == NULL)
96         goto err;
97     if ((out_ver = enif_make_new_binary(env, ver_sz, &ver_term)) == NULL)
98         goto err;
99 
100     memcpy(out_name, libname, name_sz);
101     memcpy(out_ver, ver, ver_sz);
102 
103     return enif_make_list1(env, enif_make_tuple3(env, name_term,
104 						 enif_make_int(env, ver_num),
105 						 ver_term));
106 
107  err:
108     return enif_make_badarg(env);
109 }
110