openssl.c 4.23 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8
#include "openssl.h"
9

10 11 12
#ifdef GIT_OPENSSL_DYNAMIC
# include <dlfcn.h>

13 14
static int handle_count;
static void *openssl_handle;
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

static int git_hash_openssl_global_shutdown(void)
{
	if (--handle_count == 0) {
		dlclose(openssl_handle);
		openssl_handle = NULL;
	}

	return 0;
}

static int git_hash_openssl_global_init(void)
{
	if (!handle_count) {
		if ((openssl_handle = dlopen("libssl.so.1.1", RTLD_NOW)) == NULL &&
		    (openssl_handle = dlopen("libssl.1.1.dylib", RTLD_NOW)) == NULL &&
		    (openssl_handle = dlopen("libssl.so.1.0.0", RTLD_NOW)) == NULL &&
		    (openssl_handle = dlopen("libssl.1.0.0.dylib", RTLD_NOW)) == NULL &&
33 34
		    (openssl_handle = dlopen("libssl.so.10", RTLD_NOW)) == NULL &&
		    (openssl_handle = dlopen("libssl.so.3", RTLD_NOW)) == NULL) {
35 36 37 38 39 40 41 42 43 44 45 46 47 48
			git_error_set(GIT_ERROR_SSL, "could not load ssl libraries");
			return -1;
		}
	}

	if (git_hash_openssl_global_shutdown() < 0)
		return -1;

	handle_count++;
	return 0;
}

#endif

49 50
#ifdef GIT_SHA1_OPENSSL

51 52 53 54 55 56
# ifdef GIT_OPENSSL_DYNAMIC
static int (*SHA1_Init)(SHA_CTX *c);
static int (*SHA1_Update)(SHA_CTX *c, const void *data, size_t len);
static int (*SHA1_Final)(unsigned char *md, SHA_CTX *c);
# endif

57
int git_hash_sha1_global_init(void)
58
{
59 60 61 62 63 64 65 66 67 68 69 70 71
#ifdef GIT_OPENSSL_DYNAMIC
	if (git_hash_openssl_global_init() < 0)
		return -1;

	if ((SHA1_Init = dlsym(openssl_handle, "SHA1_Init")) == NULL ||
	    (SHA1_Update = dlsym(openssl_handle, "SHA1_Update")) == NULL ||
	    (SHA1_Final = dlsym(openssl_handle, "SHA1_Final")) == NULL) {
		const char *msg = dlerror();
		git_error_set(GIT_ERROR_SSL, "could not load hash function: %s", msg ? msg : "unknown error");
		return -1;
	}
#endif

72 73 74
	return 0;
}

75
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
76
{
77
	return git_hash_sha1_init(ctx);
78 79
}

80
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
81 82 83 84
{
	GIT_UNUSED(ctx);
}

85
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
86
{
Edward Thomson committed
87
	GIT_ASSERT_ARG(ctx);
88 89

	if (SHA1_Init(&ctx->c) != 1) {
90
		git_error_set(GIT_ERROR_SHA, "failed to initialize sha1 context");
91 92 93 94 95 96
		return -1;
	}

	return 0;
}

97
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
98
{
Edward Thomson committed
99
	GIT_ASSERT_ARG(ctx);
100 101

	if (SHA1_Update(&ctx->c, data, len) != 1) {
102
		git_error_set(GIT_ERROR_SHA, "failed to update sha1");
103 104 105 106 107 108
		return -1;
	}

	return 0;
}

109
int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx)
110
{
Edward Thomson committed
111
	GIT_ASSERT_ARG(ctx);
112

113
	if (SHA1_Final(out, &ctx->c) != 1) {
114 115 116 117 118 119 120 121 122 123 124
		git_error_set(GIT_ERROR_SHA, "failed to finalize sha1");
		return -1;
	}

	return 0;
}

#endif

#ifdef GIT_SHA256_OPENSSL

125 126 127 128 129 130
# ifdef GIT_OPENSSL_DYNAMIC
static int (*SHA256_Init)(SHA256_CTX *c);
static int (*SHA256_Update)(SHA256_CTX *c, const void *data, size_t len);
static int (*SHA256_Final)(unsigned char *md, SHA256_CTX *c);
#endif

131 132
int git_hash_sha256_global_init(void)
{
133 134 135 136 137 138 139 140 141 142 143 144 145
#ifdef GIT_OPENSSL_DYNAMIC
	if (git_hash_openssl_global_init() < 0)
		return -1;

	if ((SHA256_Init = dlsym(openssl_handle, "SHA256_Init")) == NULL ||
	    (SHA256_Update = dlsym(openssl_handle, "SHA256_Update")) == NULL ||
	    (SHA256_Final = dlsym(openssl_handle, "SHA256_Final")) == NULL) {
		const char *msg = dlerror();
		git_error_set(GIT_ERROR_SSL, "could not load hash function: %s", msg ? msg : "unknown error");
		return -1;
	}
#endif

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
	return 0;
}

int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx)
{
	return git_hash_sha256_init(ctx);
}

void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx)
{
	GIT_UNUSED(ctx);
}

int git_hash_sha256_init(git_hash_sha256_ctx *ctx)
{
	GIT_ASSERT_ARG(ctx);

	if (SHA256_Init(&ctx->c) != 1) {
		git_error_set(GIT_ERROR_SHA, "failed to initialize sha256 context");
		return -1;
	}

	return 0;
}

int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len)
{
	GIT_ASSERT_ARG(ctx);

	if (SHA256_Update(&ctx->c, data, len) != 1) {
		git_error_set(GIT_ERROR_SHA, "failed to update sha256");
177 178 179 180 181
		return -1;
	}

	return 0;
}
182 183 184 185 186 187 188 189 190 191 192 193 194 195

int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
{
	GIT_ASSERT_ARG(ctx);

	if (SHA256_Final(out, &ctx->c) != 1) {
		git_error_set(GIT_ERROR_SHA, "failed to finalize sha256");
		return -1;
	}

	return 0;
}

#endif