|
7 | 7 |
|
8 | 8 | #include "openssl.h" |
9 | 9 |
|
| 10 | +#ifdef GIT_OPENSSL_DYNAMIC |
| 11 | +# include <dlfcn.h> |
| 12 | + |
| 13 | +int handle_count; |
| 14 | +void *openssl_handle; |
| 15 | + |
| 16 | +static int git_hash_openssl_global_shutdown(void) |
| 17 | +{ |
| 18 | + if (--handle_count == 0) { |
| 19 | + dlclose(openssl_handle); |
| 20 | + openssl_handle = NULL; |
| 21 | + } |
| 22 | + |
| 23 | + return 0; |
| 24 | +} |
| 25 | + |
| 26 | +static int git_hash_openssl_global_init(void) |
| 27 | +{ |
| 28 | + if (!handle_count) { |
| 29 | + if ((openssl_handle = dlopen("libssl.so.1.1", RTLD_NOW)) == NULL && |
| 30 | + (openssl_handle = dlopen("libssl.1.1.dylib", RTLD_NOW)) == NULL && |
| 31 | + (openssl_handle = dlopen("libssl.so.1.0.0", RTLD_NOW)) == NULL && |
| 32 | + (openssl_handle = dlopen("libssl.1.0.0.dylib", RTLD_NOW)) == NULL && |
| 33 | + (openssl_handle = dlopen("libssl.so.10", RTLD_NOW)) == NULL) { |
| 34 | + git_error_set(GIT_ERROR_SSL, "could not load ssl libraries"); |
| 35 | + return -1; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (git_hash_openssl_global_shutdown() < 0) |
| 40 | + return -1; |
| 41 | + |
| 42 | + handle_count++; |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +#endif |
| 47 | + |
10 | 48 | #ifdef GIT_SHA1_OPENSSL |
11 | 49 |
|
| 50 | +# ifdef GIT_OPENSSL_DYNAMIC |
| 51 | +static int (*SHA1_Init)(SHA_CTX *c); |
| 52 | +static int (*SHA1_Update)(SHA_CTX *c, const void *data, size_t len); |
| 53 | +static int (*SHA1_Final)(unsigned char *md, SHA_CTX *c); |
| 54 | +# endif |
| 55 | + |
12 | 56 | int git_hash_sha1_global_init(void) |
13 | 57 | { |
| 58 | +#ifdef GIT_OPENSSL_DYNAMIC |
| 59 | + if (git_hash_openssl_global_init() < 0) |
| 60 | + return -1; |
| 61 | + |
| 62 | + if ((SHA1_Init = dlsym(openssl_handle, "SHA1_Init")) == NULL || |
| 63 | + (SHA1_Update = dlsym(openssl_handle, "SHA1_Update")) == NULL || |
| 64 | + (SHA1_Final = dlsym(openssl_handle, "SHA1_Final")) == NULL) { |
| 65 | + const char *msg = dlerror(); |
| 66 | + git_error_set(GIT_ERROR_SSL, "could not load hash function: %s", msg ? msg : "unknown error"); |
| 67 | + return -1; |
| 68 | + } |
| 69 | +#endif |
| 70 | + |
14 | 71 | return 0; |
15 | 72 | } |
16 | 73 |
|
@@ -64,8 +121,27 @@ int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) |
64 | 121 |
|
65 | 122 | #ifdef GIT_SHA256_OPENSSL |
66 | 123 |
|
| 124 | +# ifdef GIT_OPENSSL_DYNAMIC |
| 125 | +static int (*SHA256_Init)(SHA256_CTX *c); |
| 126 | +static int (*SHA256_Update)(SHA256_CTX *c, const void *data, size_t len); |
| 127 | +static int (*SHA256_Final)(unsigned char *md, SHA256_CTX *c); |
| 128 | +#endif |
| 129 | + |
67 | 130 | int git_hash_sha256_global_init(void) |
68 | 131 | { |
| 132 | +#ifdef GIT_OPENSSL_DYNAMIC |
| 133 | + if (git_hash_openssl_global_init() < 0) |
| 134 | + return -1; |
| 135 | + |
| 136 | + if ((SHA256_Init = dlsym(openssl_handle, "SHA256_Init")) == NULL || |
| 137 | + (SHA256_Update = dlsym(openssl_handle, "SHA256_Update")) == NULL || |
| 138 | + (SHA256_Final = dlsym(openssl_handle, "SHA256_Final")) == NULL) { |
| 139 | + const char *msg = dlerror(); |
| 140 | + git_error_set(GIT_ERROR_SSL, "could not load hash function: %s", msg ? msg : "unknown error"); |
| 141 | + return -1; |
| 142 | + } |
| 143 | +#endif |
| 144 | + |
69 | 145 | return 0; |
70 | 146 | } |
71 | 147 |
|
|
0 commit comments