tls.c 1.92 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
#include "common.h"
11
#include "streams/registry.h"
12
#include "streams/tls.h"
13
#include "streams/mbedtls.h"
14 15
#include "streams/openssl.h"
#include "streams/stransport.h"
16
#include "streams/schannel.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 25 26
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(host);
	GIT_ASSERT_ARG(port);
27

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

	if (!init) {
45
		git_error_set(GIT_ERROR_SSL, "there is no TLS stream available");
46 47 48 49 50 51 52 53 54
		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;
55
	git_stream_registration custom = {0};
56

57 58
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(in);
59

60
	if (git_stream_registry_lookup(&custom, GIT_STREAM_TLS) == 0) {
61
		wrap = custom.wrap;
62 63 64 65 66 67 68
	} 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;
69 70
#elif defined(GIT_SCHANNEL)
		wrap = git_schannel_stream_wrap;
71 72 73 74
#endif
	}

	if (!wrap) {
75
		git_error_set(GIT_ERROR_SSL, "there is no TLS stream available");
76 77 78 79
		return -1;
	}

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