tls.c 1.73 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * 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.
 */

#include "git2/errors.h"

10 11
#include "common.h"
#include "global.h"
12
#include "streams/registry.h"
13
#include "streams/tls.h"
14
#include "streams/mbedtls.h"
15 16
#include "streams/openssl.h"
#include "streams/stransport.h"
17 18 19

int git_tls_stream_new(git_stream **out, const char *host, const char *port)
{
20
	int (*init)(git_stream **, const char *, const char *) = NULL;
21 22
	git_stream_registration custom = {0};
	int error;
23

24
	assert(out && host && port);
25

26
	if ((error = git_stream_registry_lookup(&custom, GIT_STREAM_TLS)) == 0) {
27 28
		init = custom.init;
	} else if (error == GIT_ENOTFOUND) {
29
#ifdef GIT_SECURE_TRANSPORT
30
		init = git_stransport_stream_new;
31
#elif defined(GIT_OPENSSL)
32
		init = git_openssl_stream_new;
33
#elif defined(GIT_MBEDTLS)
34
		init = git_mbedtls_stream_new;
35
#endif
36 37
	} else {
		return error;
38 39 40
	}

	if (!init) {
41
		git_error_set(GIT_ERROR_SSL, "there is no TLS stream available");
42 43 44 45 46 47 48 49 50
		return -1;
	}

	return init(out, host, port);
}

int git_tls_stream_wrap(git_stream **out, git_stream *in, const char *host)
{
	int (*wrap)(git_stream **, git_stream *, const char *) = NULL;
51
	git_stream_registration custom = {0};
52 53 54

	assert(out && in);

55
	if (git_stream_registry_lookup(&custom, GIT_STREAM_TLS) == 0) {
56
		wrap = custom.wrap;
57 58 59 60 61 62 63 64 65 66 67
	} else {
#ifdef GIT_SECURE_TRANSPORT
		wrap = git_stransport_stream_wrap;
#elif defined(GIT_OPENSSL)
		wrap = git_openssl_stream_wrap;
#elif defined(GIT_MBEDTLS)
		wrap = git_mbedtls_stream_wrap;
#endif
	}

	if (!wrap) {
68
		git_error_set(GIT_ERROR_SSL, "there is no TLS stream available");
69 70 71 72
		return -1;
	}

	return wrap(out, in, host);
73
}