-
Notifications
You must be signed in to change notification settings - Fork 13
/
goopenssl.c
248 lines (220 loc) · 9.85 KB
/
goopenssl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//go:build unix || windows
#include "goopenssl.h"
#ifdef _WIN32
# include <windows.h>
# define dlsym (void*)GetProcAddress
#else
# include <dlfcn.h> // dlsym
#endif
#include <stdio.h> // fprintf
// Approach taken from .Net System.Security.Cryptography.Native
// https://github.com/dotnet/runtime/blob/f64246ce08fb7a58221b2b7c8e68f69c02522b0d/src/libraries/Native/Unix/System.Security.Cryptography.Native/opensslshim.c
#define DEFINEFUNC(ret, func, args, argscall) ret (*_g_##func)args;
#define DEFINEFUNC_LEGACY_1_1(ret, func, args, argscall) DEFINEFUNC(ret, func, args, argscall)
#define DEFINEFUNC_LEGACY_1_0(ret, func, args, argscall) DEFINEFUNC(ret, func, args, argscall)
#define DEFINEFUNC_LEGACY_1(ret, func, args, argscall) DEFINEFUNC(ret, func, args, argscall)
#define DEFINEFUNC_1_1(ret, func, args, argscall) DEFINEFUNC(ret, func, args, argscall)
#define DEFINEFUNC_1_1_1(ret, func, args, argscall) DEFINEFUNC(ret, func, args, argscall)
#define DEFINEFUNC_3_0(ret, func, args, argscall) DEFINEFUNC(ret, func, args, argscall)
#define DEFINEFUNC_RENAMED_1_1(ret, func, oldfunc, args, argscall) DEFINEFUNC(ret, func, args, argscall)
#define DEFINEFUNC_RENAMED_3_0(ret, func, oldfunc, args, argscall) DEFINEFUNC(ret, func, args, argscall)
#define DEFINEFUNC_VARIADIC_3_0(ret, func, newname, args, argscall) DEFINEFUNC(ret, newname, args, argscall)
FOR_ALL_OPENSSL_FUNCTIONS
#undef DEFINEFUNC
#undef DEFINEFUNC_LEGACY_1_1
#undef DEFINEFUNC_LEGACY_1_0
#undef DEFINEFUNC_LEGACY_1
#undef DEFINEFUNC_1_1
#undef DEFINEFUNC_1_1_1
#undef DEFINEFUNC_3_0
#undef DEFINEFUNC_RENAMED_1_1
#undef DEFINEFUNC_RENAMED_3_0
#undef DEFINEFUNC_VARIADIC_3_0
// go_openssl_fips_enabled returns 1 if FIPS mode is enabled, 0 otherwise.
// As a special case, it returns -1 if it cannot determine if FIPS mode is enabled.
// See openssl.FIPS for details about its implementation.
//
// This function is reimplemented here because openssl.FIPS assumes that
// all the OpenSSL bindings are loaded, that is, go_openssl_load_functions has
// already been called. On the other hand, go_openssl_fips_enabled is called from
// openssl.CheckVersion, which is used to check if a given OpenSSL shared library
// exists and is FIPS compliant. That shared library might not be the one that
// was passed to go_openssl_load_functions, or it might not even have been called at all.
//
// It is written in C because it is not possible to directly call C function pointers
// retrieved using dlsym from Go.
int
go_openssl_fips_enabled(void* handle)
{
// For OpenSSL 1.x.
int (*FIPS_mode)(void);
FIPS_mode = (int (*)(void))dlsym(handle, "FIPS_mode");
if (FIPS_mode != NULL)
return FIPS_mode();
// For OpenSSL 3.x.
int (*EVP_default_properties_is_fips_enabled)(void*) = (int (*)(void*))dlsym(handle, "EVP_default_properties_is_fips_enabled");
void *(*EVP_MD_fetch)(void*, const char*, const char*) = (void* (*)(void*, const char*, const char*))dlsym(handle, "EVP_MD_fetch");
void (*EVP_MD_free)(void*) = (void (*)(void*))dlsym(handle, "EVP_MD_free");
if (EVP_default_properties_is_fips_enabled == NULL || EVP_MD_fetch == NULL || EVP_MD_free == NULL) {
// Shouldn't happen, but if it does, we can't determine if FIPS mode is enabled.
return -1;
}
if (EVP_default_properties_is_fips_enabled(NULL) != 1)
return 0;
void *md = EVP_MD_fetch(NULL, "SHA2-256", NULL);
if (md == NULL)
return 0;
EVP_MD_free(md);
return 1;
}
// Load all the functions stored in FOR_ALL_OPENSSL_FUNCTIONS
// and assign them to their corresponding function pointer
// defined in goopenssl.h.
void
go_openssl_load_functions(void* handle, unsigned int major, unsigned int minor, unsigned int patch)
{
#define DEFINEFUNC_INTERNAL(name, func) \
_g_##name = dlsym(handle, func); \
if (_g_##name == NULL) { \
fprintf(stderr, "Cannot get required symbol " #func " from libcrypto version %u.%u\n", major, minor); \
abort(); \
}
#define DEFINEFUNC(ret, func, args, argscall) \
DEFINEFUNC_INTERNAL(func, #func)
#define DEFINEFUNC_LEGACY_1_1(ret, func, args, argscall) \
if (major == 1 && minor == 1) \
{ \
DEFINEFUNC_INTERNAL(func, #func) \
}
#define DEFINEFUNC_LEGACY_1_0(ret, func, args, argscall) \
if (major == 1 && minor == 0) \
{ \
DEFINEFUNC_INTERNAL(func, #func) \
}
#define DEFINEFUNC_LEGACY_1(ret, func, args, argscall) \
if (major == 1) \
{ \
DEFINEFUNC_INTERNAL(func, #func) \
}
#define DEFINEFUNC_1_1(ret, func, args, argscall) \
if (major == 3 || (major == 1 && minor == 1)) \
{ \
DEFINEFUNC_INTERNAL(func, #func) \
}
#define DEFINEFUNC_1_1_1(ret, func, args, argscall) \
if (major == 3 || (major == 1 && minor == 1 && patch == 1)) \
{ \
DEFINEFUNC_INTERNAL(func, #func) \
}
#define DEFINEFUNC_3_0(ret, func, args, argscall) \
if (major == 3) \
{ \
DEFINEFUNC_INTERNAL(func, #func) \
}
#define DEFINEFUNC_RENAMED_1_1(ret, func, oldfunc, args, argscall) \
if (major == 1 && minor == 0) \
{ \
DEFINEFUNC_INTERNAL(func, #oldfunc) \
} \
else \
{ \
DEFINEFUNC_INTERNAL(func, #func) \
}
#define DEFINEFUNC_RENAMED_3_0(ret, func, oldfunc, args, argscall) \
if (major == 1) \
{ \
DEFINEFUNC_INTERNAL(func, #oldfunc) \
} \
else \
{ \
DEFINEFUNC_INTERNAL(func, #func) \
}
#define DEFINEFUNC_VARIADIC_3_0(ret, func, newname, args, argscall) \
if (major == 3) \
{ \
DEFINEFUNC_INTERNAL(newname, #func) \
}
FOR_ALL_OPENSSL_FUNCTIONS
#undef DEFINEFUNC
#undef DEFINEFUNC_LEGACY_1_1
#undef DEFINEFUNC_LEGACY_1_0
#undef DEFINEFUNC_LEGACY_1
#undef DEFINEFUNC_1_1
#undef DEFINEFUNC_1_1_1
#undef DEFINEFUNC_3_0
#undef DEFINEFUNC_RENAMED_1_1
#undef DEFINEFUNC_RENAMED_3_0
#undef DEFINEFUNC_VARIADIC_3_0
}
static unsigned long
version_num(void* handle)
{
unsigned long (*fn)(void);
// OPENSSL_version_num is defined in OpenSSL 1.1.0 and 1.1.1.
fn = (unsigned long (*)(void))dlsym(handle, "OpenSSL_version_num");
if (fn != NULL)
return fn();
// SSLeay is defined in OpenSSL 1.0.2.
fn = (unsigned long (*)(void))dlsym(handle, "SSLeay");
if (fn != NULL)
return fn();
return 0;
}
int
go_openssl_version_major(void* handle)
{
unsigned int (*fn)(void);
// OPENSSL_version_major is supported since OpenSSL 3.
fn = (unsigned int (*)(void))dlsym(handle, "OPENSSL_version_major");
if (fn != NULL)
return (int)fn();
// If OPENSSL_version_major is not defined, try with OpenSSL 1 functions.
unsigned long num = version_num(handle);
if (num < 0x10000000L || num >= 0x20000000L)
return -1;
return 1;
}
int
go_openssl_version_minor(void* handle)
{
unsigned int (*fn)(void);
// OPENSSL_version_minor is supported since OpenSSL 3.
fn = (unsigned int (*)(void))dlsym(handle, "OPENSSL_version_minor");
if (fn != NULL)
return (int)fn();
// If OPENSSL_version_minor is not defined, try with OpenSSL 1 functions.
unsigned long num = version_num(handle);
// OpenSSL version number follows this schema:
// MNNFFPPS: major minor fix patch status.
if (num < 0x10000000L || num >= 0x10200000L)
{
// We only support minor version 0 and 1,
// so there is no need to implement an algorithm
// that decodes the version number into individual components.
return -1;
}
if (num >= 0x10100000L)
return 1;
return 0;
}
int
go_openssl_version_patch(void* handle)
{
unsigned int (*fn)(void);
// OPENSSL_version_patch is supported since OpenSSL 3.
fn = (unsigned int (*)(void))dlsym(handle, "OPENSSL_version_patch");
if (fn != NULL)
return (int)fn();
// If OPENSSL_version_patch is not defined, try with OpenSSL 1 functions.
unsigned long num = version_num(handle);
// OpenSSL version number follows this schema:
// MNNFFPPS: major minor fix patch status.
if (num < 0x10000000L || num >= 0x10200000L)
{
// We only support minor version 0 and 1,
// so there is no need to implement an algorithm
// that decodes the version number into individual components.
return -1;
}
return (num >> 12) & 0xff;
}