stransport_stream.c 7.39 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 9
#include "stransport_stream.h"

10 11 12 13 14 15 16 17 18
#ifdef GIT_SECURE_TRANSPORT

#include <CoreFoundation/CoreFoundation.h>
#include <Security/SecureTransport.h>
#include <Security/SecCertificate.h>

#include "git2/transport.h"

#include "socket_stream.h"
19
#include "curl_stream.h"
20

21
static int stransport_error(OSStatus ret)
22
{
23 24
	CFStringRef message;

25
	if (ret == noErr || ret == errSSLClosedGraceful) {
26 27 28
		giterr_clear();
		return 0;
	}
29

30
#if !TARGET_OS_IPHONE
31 32 33 34 35
	message = SecCopyErrorMessageString(ret, NULL);
	GITERR_CHECK_ALLOC(message);

	giterr_set(GITERR_NET, "SecureTransport error: %s", CFStringGetCStringPtr(message, kCFStringEncodingUTF8));
	CFRelease(message);
36 37
#else
    giterr_set(GITERR_NET, "SecureTransport error: OSStatus %d", (unsigned int)ret);
38
    GIT_UNUSED(message);
39 40
#endif

41
	return -1;
42 43 44 45 46 47 48 49 50 51
}

typedef struct {
	git_stream parent;
	git_stream *io;
	SSLContextRef ctx;
	CFDataRef der_data;
	git_cert_x509 cert_info;
} stransport_stream;

52
static int stransport_connect(git_stream *stream)
53 54 55
{
	stransport_stream *st = (stransport_stream *) stream;
	int error;
56 57
	SecTrustRef trust = NULL;
	SecTrustResultType sec_res;
58 59 60 61 62
	OSStatus ret;

	if ((error = git_stream_connect(st->io)) < 0)
		return error;

63 64 65 66 67 68 69 70 71
	ret = SSLHandshake(st->ctx);
	if (ret != errSSLServerAuthCompleted) {
		giterr_set(GITERR_SSL, "unexpected return value from ssl handshake %d", ret);
		return -1;
	}

	if ((ret = SSLCopyPeerTrust(st->ctx, &trust)) != noErr)
		goto on_error;

72 73 74
	if (!trust)
		return GIT_ECERTIFICATE;

75 76 77 78 79 80 81 82 83 84 85 86 87
	if ((ret = SecTrustEvaluate(trust, &sec_res)) != noErr)
		goto on_error;

	CFRelease(trust);

	if (sec_res == kSecTrustResultInvalid || sec_res == kSecTrustResultOtherError) {
		giterr_set(GITERR_SSL, "internal security trust error");
		return -1;
	}

	if (sec_res == kSecTrustResultDeny || sec_res == kSecTrustResultRecoverableTrustFailure ||
	    sec_res == kSecTrustResultFatalTrustFailure)
		return GIT_ECERTIFICATE;
88 89

	return 0;
90 91 92 93 94 95

on_error:
	if (trust)
		CFRelease(trust);

	return stransport_error(ret);
96 97
}

98
static int stransport_certificate(git_cert **out, git_stream *stream)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
{
	stransport_stream *st = (stransport_stream *) stream;
	SecTrustRef trust = NULL;
	SecCertificateRef sec_cert;
	OSStatus ret;

	if ((ret = SSLCopyPeerTrust(st->ctx, &trust)) != noErr)
		return stransport_error(ret);

	sec_cert = SecTrustGetCertificateAtIndex(trust, 0);
	st->der_data = SecCertificateCopyData(sec_cert);
	CFRelease(trust);

	if (st->der_data == NULL) {
		giterr_set(GITERR_SSL, "retrieved invalid certificate data");
		return -1;
	}

117
	st->cert_info.parent.cert_type = GIT_CERT_X509;
118 119 120 121 122 123 124
	st->cert_info.data = (void *) CFDataGetBytePtr(st->der_data);
	st->cert_info.len = CFDataGetLength(st->der_data);

	*out = (git_cert *)&st->cert_info;
	return 0;
}

125
static int stransport_set_proxy(
126 127
	git_stream *stream,
	const git_proxy_options *proxy_opts)
128 129 130
{
	stransport_stream *st = (stransport_stream *) stream;

131
	return git_stream_set_proxy(st->io, proxy_opts);
132 133
}

134 135 136 137 138 139 140 141 142 143 144 145
/*
 * Contrary to typical network IO callbacks, Secure Transport write callback is
 * expected to write *all* passed data, not just as much as it can, and any
 * other case would be considered a failure.
 *
 * This behavior is actually not specified in the Apple documentation, but is
 * required for things to work correctly (and incidentally, that's also how
 * Apple implements it in its projects at opensource.apple.com).
 *
 * Libgit2 streams happen to already have this very behavior so this is just
 * passthrough.
 */
146 147 148 149
static OSStatus write_cb(SSLConnectionRef conn, const void *data, size_t *len)
{
	git_stream *io = (git_stream *) conn;

150 151
	if (git_stream_write(io, data, *len, 0) < 0) {
		return -36; /* "ioErr" from MacErrors.h which is not available on iOS */
152 153 154 155 156
	}

	return noErr;
}

157
static ssize_t stransport_write(git_stream *stream, const char *data, size_t len, int flags)
158 159 160 161 162 163 164 165 166 167 168 169 170 171
{
	stransport_stream *st = (stransport_stream *) stream;
	size_t data_len, processed;
	OSStatus ret;

	GIT_UNUSED(flags);

	data_len = len;
	if ((ret = SSLWrite(st->ctx, data, data_len, &processed)) != noErr)
		return stransport_error(ret);

	return processed;
}

172 173 174 175 176 177 178 179 180
/*
 * Contrary to typical network IO callbacks, Secure Transport read callback is
 * expected to read *exactly* the requested number of bytes, not just as much
 * as it can, and any other case would be considered a failure.
 *
 * This behavior is actually not specified in the Apple documentation, but is
 * required for things to work correctly (and incidentally, that's also how
 * Apple implements it in its projects at opensource.apple.com).
 */
181 182 183
static OSStatus read_cb(SSLConnectionRef conn, void *data, size_t *len)
{
	git_stream *io = (git_stream *) conn;
184 185
	OSStatus error = noErr;
	size_t off = 0;
186 187 188
	ssize_t ret;

	do {
189
		ret = git_stream_read(io, data + off, *len - off);
190
		if (ret < 0) {
191 192 193 194 195 196
			error = -36; /* "ioErr" from MacErrors.h which is not available on iOS */
			break;
		}
		if (ret == 0) {
			error = errSSLClosedGraceful;
			break;
197 198
		}

199 200
		off += ret;
	} while (off < *len);
201

202 203
	*len = off;
	return error;
204 205
}

206
static ssize_t stransport_read(git_stream *stream, void *data, size_t len)
207 208 209 210 211 212 213 214 215 216 217
{
	stransport_stream *st = (stransport_stream *) stream;
	size_t processed;
	OSStatus ret;

	if ((ret = SSLRead(st->ctx, data, len, &processed)) != noErr)
		return stransport_error(ret);

	return processed;
}

218
static int stransport_close(git_stream *stream)
219 220 221 222
{
	stransport_stream *st = (stransport_stream *) stream;
	OSStatus ret;

223 224
	ret = SSLClose(st->ctx);
	if (ret != noErr && ret != errSSLClosedGraceful)
225 226 227 228 229
		return stransport_error(ret);

	return git_stream_close(st->io);
}

230
static void stransport_free(git_stream *stream)
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
{
	stransport_stream *st = (stransport_stream *) stream;

	git_stream_free(st->io);
	CFRelease(st->ctx);
	if (st->der_data)
		CFRelease(st->der_data);
	git__free(st);
}

int git_stransport_stream_new(git_stream **out, const char *host, const char *port)
{
	stransport_stream *st;
	int error;
	OSStatus ret;

	assert(out && host);

	st = git__calloc(1, sizeof(stransport_stream));
	GITERR_CHECK_ALLOC(st);

252 253 254
#ifdef GIT_CURL
	error = git_curl_stream_new(&st->io, host, port);
#else
255
	error = git_socket_stream_new(&st->io, host, port);
256 257 258
#endif

	if (error < 0){
259 260 261 262 263 264 265
		git__free(st);
		return error;
	}

	st->ctx = SSLCreateContext(NULL, kSSLClientSide, kSSLStreamType);
	if (!st->ctx) {
		giterr_set(GITERR_NET, "failed to create SSL context");
266
		git__free(st);
267 268 269 270 271
		return -1;
	}

	if ((ret = SSLSetIOFuncs(st->ctx, read_cb, write_cb)) != noErr ||
	    (ret = SSLSetConnection(st->ctx, st->io)) != noErr ||
272
	    (ret = SSLSetSessionOption(st->ctx, kSSLSessionOptionBreakOnServerAuth, true)) != noErr ||
273 274
	    (ret = SSLSetProtocolVersionMin(st->ctx, kTLSProtocol1)) != noErr ||
	    (ret = SSLSetProtocolVersionMax(st->ctx, kTLSProtocol12)) != noErr ||
275
	    (ret = SSLSetPeerDomainName(st->ctx, host, strlen(host))) != noErr) {
276 277
		CFRelease(st->ctx);
		git__free(st);
278 279 280 281 282
		return stransport_error(ret);
	}

	st->parent.version = GIT_STREAM_VERSION;
	st->parent.encrypted = 1;
283
	st->parent.proxy_support = git_stream_supports_proxy(st->io);
284 285
	st->parent.connect = stransport_connect;
	st->parent.certificate = stransport_certificate;
286
	st->parent.set_proxy = stransport_set_proxy;
287 288 289 290 291 292 293 294 295 296
	st->parent.read = stransport_read;
	st->parent.write = stransport_write;
	st->parent.close = stransport_close;
	st->parent.free = stransport_free;

	*out = (git_stream *) st;
	return 0;
}

#endif