Mercurial > vim
comparison src/crypt.c @ 27657:a077948be0f4 v8.2.4354
patch 8.2.4354: dynamic loading of libsodium not handled properly
Commit: https://github.com/vim/vim/commit/d68b2fc034fa3c824e0d4d53745cfe9eb8c5ecd6
Author: K.Takata <kentkt@csc.jp>
Date: Sat Feb 12 11:18:37 2022 +0000
patch 8.2.4354: dynamic loading of libsodium not handled properly
Problem: Dynamic loading of libsodium not handled properly.
Solution: Fix has() and :version. Show an error message when loading fails.
Fix memory leaks. (Ken Takata, closes #9754)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 12 Feb 2022 12:30:03 +0100 |
parents | e1cedf009920 |
children | ef7d9789919d |
comparison
equal
deleted
inserted
replaced
27656:85577ef6b2ce | 27657:a077948be0f4 |
---|---|
160 state; | 160 state; |
161 } sodium_state_T; | 161 } sodium_state_T; |
162 | 162 |
163 | 163 |
164 # ifdef DYNAMIC_SODIUM | 164 # ifdef DYNAMIC_SODIUM |
165 # ifdef MSWIN | |
166 # define SODIUM_PROC FARPROC | |
167 # define load_dll vimLoadLib | |
168 # define symbol_from_dll GetProcAddress | |
169 # define close_dll FreeLibrary | |
170 # define load_dll_error GetWin32Error | |
171 # else | |
172 # error Dynamic loading of libsodium is not supported for now. | |
173 //# define HINSTANCE void* | |
174 //# define SODIUM_PROC void* | |
175 //# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) | |
176 //# define symbol_from_dll dlsym | |
177 //# define close_dll dlclose | |
178 //# define load_dll_error dlerror | |
179 # endif | |
180 | |
165 # define sodium_init load_sodium | 181 # define sodium_init load_sodium |
166 # define sodium_free dll_sodium_free | 182 # define sodium_free dll_sodium_free |
167 # define sodium_malloc dll_sodium_malloc | 183 # define sodium_malloc dll_sodium_malloc |
168 # define sodium_memzero dll_sodium_memzero | 184 # define sodium_memzero dll_sodium_memzero |
169 # define sodium_mlock dll_sodium_mlock | 185 # define sodium_mlock dll_sodium_mlock |
212 = NULL; | 228 = NULL; |
213 static void (*dll_randombytes_buf)(void * const buf, const size_t size); | 229 static void (*dll_randombytes_buf)(void * const buf, const size_t size); |
214 | 230 |
215 static struct { | 231 static struct { |
216 const char *name; | 232 const char *name; |
217 FARPROC *ptr; | 233 SODIUM_PROC *ptr; |
218 } sodium_funcname_table[] = { | 234 } sodium_funcname_table[] = { |
219 {"sodium_init", (FARPROC*)&dll_sodium_init}, | 235 {"sodium_init", (SODIUM_PROC*)&dll_sodium_init}, |
220 {"sodium_free", (FARPROC*)&dll_sodium_free}, | 236 {"sodium_free", (SODIUM_PROC*)&dll_sodium_free}, |
221 {"sodium_malloc", (FARPROC*)&dll_sodium_malloc}, | 237 {"sodium_malloc", (SODIUM_PROC*)&dll_sodium_malloc}, |
222 {"sodium_memzero", (FARPROC*)&dll_sodium_memzero}, | 238 {"sodium_memzero", (SODIUM_PROC*)&dll_sodium_memzero}, |
223 {"sodium_mlock", (FARPROC*)&dll_sodium_mlock}, | 239 {"sodium_mlock", (SODIUM_PROC*)&dll_sodium_mlock}, |
224 {"sodium_munlock", (FARPROC*)&dll_sodium_munlock}, | 240 {"sodium_munlock", (SODIUM_PROC*)&dll_sodium_munlock}, |
225 {"crypto_secretstream_xchacha20poly1305_init_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_push}, | 241 {"crypto_secretstream_xchacha20poly1305_init_push", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_init_push}, |
226 {"crypto_secretstream_xchacha20poly1305_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_push}, | 242 {"crypto_secretstream_xchacha20poly1305_push", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_push}, |
227 {"crypto_secretstream_xchacha20poly1305_init_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_pull}, | 243 {"crypto_secretstream_xchacha20poly1305_init_pull", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_init_pull}, |
228 {"crypto_secretstream_xchacha20poly1305_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_pull}, | 244 {"crypto_secretstream_xchacha20poly1305_pull", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_pull}, |
229 {"crypto_pwhash", (FARPROC*)&dll_crypto_pwhash}, | 245 {"crypto_pwhash", (SODIUM_PROC*)&dll_crypto_pwhash}, |
230 {"randombytes_buf", (FARPROC*)&dll_randombytes_buf}, | 246 {"randombytes_buf", (SODIUM_PROC*)&dll_randombytes_buf}, |
231 {NULL, NULL} | 247 {NULL, NULL} |
232 }; | 248 }; |
233 | 249 |
234 static int | 250 static int |
235 load_sodium(void) | 251 sodium_runtime_link_init(int verbose) |
236 { | 252 { |
237 static HANDLE hsodium = NULL; | 253 static HINSTANCE hsodium = NULL; |
254 const char *libname = "libsodium.dll"; | |
238 int i; | 255 int i; |
239 | 256 |
240 if (hsodium != NULL) | 257 if (hsodium != NULL) |
241 return 0; | 258 return OK; |
242 | 259 |
243 hsodium = vimLoadLib("libsodium.dll"); | 260 hsodium = load_dll(libname); |
244 if (hsodium == NULL) | 261 if (hsodium == NULL) |
245 { | 262 { |
246 // TODO: Show error message. | 263 if (verbose) |
247 return -1; | 264 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error()); |
265 return FAIL; | |
248 } | 266 } |
249 | 267 |
250 for (i = 0; sodium_funcname_table[i].ptr; ++i) | 268 for (i = 0; sodium_funcname_table[i].ptr; ++i) |
251 { | 269 { |
252 if ((*sodium_funcname_table[i].ptr = GetProcAddress(hsodium, | 270 if ((*sodium_funcname_table[i].ptr = symbol_from_dll(hsodium, |
253 sodium_funcname_table[i].name)) == NULL) | 271 sodium_funcname_table[i].name)) == NULL) |
254 { | 272 { |
255 FreeLibrary(hsodium); | 273 FreeLibrary(hsodium); |
256 hsodium = NULL; | 274 hsodium = NULL; |
257 // TODO: Show error message. | 275 if (verbose) |
258 return -1; | 276 semsg(_(e_could_not_load_library_function_str), sodium_funcname_table[i].name); |
277 return FAIL; | |
259 } | 278 } |
260 } | 279 } |
280 return OK; | |
281 } | |
282 | |
283 static int | |
284 load_sodium(void) | |
285 { | |
286 if (sodium_runtime_link_init(TRUE) == FAIL) | |
287 return -1; | |
261 return dll_sodium_init(); | 288 return dll_sodium_init(); |
289 } | |
290 # endif | |
291 | |
292 # if defined(DYNAMIC_SODIUM) || defined(PROTO) | |
293 int | |
294 sodium_enabled(int verbose) | |
295 { | |
296 return sodium_runtime_link_init(verbose) == OK; | |
262 } | 297 } |
263 # endif | 298 # endif |
264 #endif | 299 #endif |
265 | 300 |
266 #define CRYPT_MAGIC_LEN 12 // cannot change | 301 #define CRYPT_MAGIC_LEN 12 // cannot change |