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 #if defined(DEBUG)
24 #  define CB_NAME "crypto_callback.debug"
25 #  define COMPILE_TYPE "debug"
26 
27 #elif defined(VALGRIND)
28 #  define CB_NAME "crypto_callback.valgrind"
29 #  define COMPILE_TYPE "valgrind"
30 
31 #elif defined(ADDRESS_SANITIZER)
32 #  define CB_NAME "crypto_callback.asan"
33 #  define COMPILE_TYPE "asan"
34 
35 #else
36 #  define CB_NAME "crypto_callback"
37 #  define COMPILE_TYPE "normal"
38 
39 #endif
40 
41 
42 #if defined(HAVE_DYNAMIC_CRYPTO_LIB)
43 #  define LINK_TYPE "dynamic"
44 #else
45 #  define LINK_TYPE "static"
46 #endif
47 
48 
49 #ifdef HAVE_DYNAMIC_CRYPTO_LIB
50 
51 char *crypto_callback_name = CB_NAME;
52 
change_basename(ErlNifBinary * bin,char * buf,size_t bufsz,const char * newfile)53 int change_basename(ErlNifBinary* bin, char* buf, size_t bufsz, const char* newfile)
54 {
55     size_t i;
56     size_t newlen;
57 
58     for (i = bin->size; i > 0; i--) {
59 	if (bin->data[i-1] == '/')
60 	    break;
61     }
62 
63     newlen = strlen(newfile);
64     if (i > SIZE_MAX - newlen)
65         goto err;
66 
67     if (i + newlen >= bufsz)
68         goto err;
69 
70     memcpy(buf, bin->data, i);
71     strcpy(buf+i, newfile);
72 
73     return 1;
74 
75  err:
76     return 0;
77 }
78 
error_handler(void * null,const char * errstr)79 void error_handler(void* null, const char* errstr)
80 {
81     PRINTF_ERR1("CRYPTO LOADING ERROR: '%s'", errstr);
82 }
83 #endif /* HAVE_DYNAMIC_CRYPTO_LIB */
84 
85 
info_nif(ErlNifEnv * env,int argc,const ERL_NIF_TERM argv[])86 ERL_NIF_TERM info_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
87 {/* () */
88     ERL_NIF_TERM  ret;
89 
90     ret = enif_make_new_map(env);
91 
92     enif_make_map_put(env, ret,
93                       enif_make_atom(env,"compile_type"),
94                       enif_make_atom(env, COMPILE_TYPE),
95                       &ret);
96 
97     enif_make_map_put(env, ret,
98                       enif_make_atom(env, "link_type"),
99                       enif_make_atom(env, LINK_TYPE),
100                       &ret);
101 
102     enif_make_map_put(env, ret,
103                       enif_make_atom(env, "cryptolib_version_compiled"),
104 #ifdef OPENSSL_VERSION_TEXT
105                       enif_make_string(env, OPENSSL_VERSION_TEXT, ERL_NIF_LATIN1),
106 #else
107                       /* Just to be really safe for versions/clones unknown to me lacking this macro */
108                       atom_undefined,
109 #endif
110                       &ret);
111 
112     enif_make_map_put(env, ret,
113                       enif_make_atom(env, "cryptolib_version_linked"),
114                       enif_make_string(env, SSLeay_version(SSLEAY_VERSION), ERL_NIF_LATIN1),
115                       &ret);
116     return ret;
117 }
118 
119 
info_lib(ErlNifEnv * env,int argc,const ERL_NIF_TERM argv[])120 ERL_NIF_TERM info_lib(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
121 {/* () */
122     /* [{<<"OpenSSL">>,9470143,<<"OpenSSL 0.9.8k 25 Mar 2009">>}] */
123 
124     ERL_NIF_TERM name_term, ver_term;
125     static const char libname[] = "OpenSSL";
126     size_t name_sz;
127     const char* ver;
128     size_t ver_sz;
129     int ver_num;
130     unsigned char *out_name, *out_ver;
131 
132     ASSERT(argc == 0);
133 
134     name_sz = strlen(libname);
135     ver = SSLeay_version(SSLEAY_VERSION);
136     ver_sz = strlen(ver);
137     ver_num = OPENSSL_VERSION_NUMBER;
138 
139     /* R16:
140      * Ignore library version number from SSLeay() and instead show header
141      * version. Otherwise user might try to call a function that is implemented
142      * by a newer library but not supported by the headers used at compile time.
143      * Example: DES_ede3_cfb_encrypt in 0.9.7i but not in 0.9.7d.
144      *
145      * Version string is still from library though.
146      */
147 
148     if ((out_name = enif_make_new_binary(env, name_sz, &name_term)) == NULL)
149         goto err;
150     if ((out_ver = enif_make_new_binary(env, ver_sz, &ver_term)) == NULL)
151         goto err;
152 
153     memcpy(out_name, libname, name_sz);
154     memcpy(out_ver, ver, ver_sz);
155 
156     return enif_make_list1(env, enif_make_tuple3(env, name_term,
157 						 enif_make_int(env, ver_num),
158 						 ver_term));
159 
160  err:
161     return enif_make_badarg(env);
162 }
163