openssl.c 4.15 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 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
#ifdef GIT_OPENSSL_DYNAMIC
# include <dlfcn.h>

int handle_count;
void *openssl_handle;

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 &&
		    (openssl_handle = dlopen("libssl.so.10", RTLD_NOW)) == NULL) {
			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

48 49
#ifdef GIT_SHA1_OPENSSL

50 51 52 53 54 55
# 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

56
int git_hash_sha1_global_init(void)
57
{
58 59 60 61 62 63 64 65 66 67 68 69 70
#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

71 72 73
	return 0;
}

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

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

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

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

	return 0;
}

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

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

	return 0;
}

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

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

	return 0;
}

#endif

#ifdef GIT_SHA256_OPENSSL

124 125 126 127 128 129
# 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

130 131
int git_hash_sha256_global_init(void)
{
132 133 134 135 136 137 138 139 140 141 142 143 144
#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

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
	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");
176 177 178 179 180
		return -1;
	}

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

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