Skip to content

Commit 1edde0b

Browse files
committed
mbedtls: use mbedTLS certificate verification
Taken from https://github.com/JuliaLang/julia/blob/8d47a314537779c8fb86642c54925613628a91b0/deps/patches/libgit2-mbedtls-verify.patch, with some modifications.
1 parent 4165bb7 commit 1edde0b

File tree

1 file changed

+6
-69
lines changed

1 file changed

+6
-69
lines changed

src/streams/mbedtls.c

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -202,82 +202,19 @@ static int ssl_teardown(mbedtls_ssl_context *ssl)
202202
return ret;
203203
}
204204

205-
static int check_host_name(const char *name, const char *host)
205+
static int verify_server_cert(mbedtls_ssl_context *ssl)
206206
{
207-
if (!strcasecmp(name, host))
208-
return 0;
209-
210-
if (gitno__match_host(name, host) < 0)
211-
return -1;
212-
213-
return 0;
214-
}
215-
216-
static int verify_server_cert(mbedtls_ssl_context *ssl, const char *host)
217-
{
218-
const mbedtls_x509_crt *cert;
219-
const mbedtls_x509_sequence *alts;
220-
int ret, matched = -1;
221-
size_t sn_size = 512;
222-
char subject_name[sn_size], alt_name[sn_size];
223-
207+
int ret = -1;
224208

225209
if ((ret = mbedtls_ssl_get_verify_result(ssl)) != 0) {
226210
char vrfy_buf[512];
227-
mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", ret );
228-
giterr_set(GITERR_SSL, "The SSL certificate is invalid: %s", vrfy_buf);
211+
int len = mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "", ret);
212+
if (len >= 1) vrfy_buf[len - 1] = '\0'; /* Remove trailing \n */
213+
giterr_set(GITERR_SSL, "the SSL certificate is invalid: %x - %s", ret, vrfy_buf);
229214
return GIT_ECERTIFICATE;
230215
}
231216

232-
cert = mbedtls_ssl_get_peer_cert(ssl);
233-
if (!cert) {
234-
giterr_set(GITERR_SSL, "the server did not provide a certificate");
235-
return -1;
236-
}
237-
238-
/* Check the alternative names */
239-
alts = &cert->subject_alt_names;
240-
while (alts != NULL && matched != 1) {
241-
// Buffer is too small
242-
if( alts->buf.len >= sn_size )
243-
goto on_error;
244-
245-
memcpy(alt_name, alts->buf.p, alts->buf.len);
246-
alt_name[alts->buf.len] = '\0';
247-
248-
if (!memchr(alt_name, '\0', alts->buf.len)) {
249-
if (check_host_name(alt_name, host) < 0)
250-
matched = 0;
251-
else
252-
matched = 1;
253-
}
254-
255-
alts = alts->next;
256-
}
257-
if (matched == 0)
258-
goto cert_fail_name;
259-
260-
if (matched == 1)
261-
return 0;
262-
263-
/* If no alternative names are available, check the common name */
264-
ret = mbedtls_x509_dn_gets(subject_name, sn_size, &cert->subject);
265-
if (ret == 0)
266-
goto on_error;
267-
if (memchr(subject_name, '\0', ret))
268-
goto cert_fail_name;
269-
270-
if (check_host_name(subject_name, host) < 0)
271-
goto cert_fail_name;
272-
273217
return 0;
274-
275-
on_error:
276-
return ssl_set_error(ssl, 0);
277-
278-
cert_fail_name:
279-
giterr_set(GITERR_SSL, "hostname does not match certificate");
280-
return GIT_ECERTIFICATE;
281218
}
282219

283220
typedef struct {
@@ -307,7 +244,7 @@ int mbedtls_connect(git_stream *stream)
307244
if ((ret = mbedtls_ssl_handshake(st->ssl)) != 0)
308245
return ssl_set_error(st->ssl, ret);
309246

310-
return verify_server_cert(st->ssl, st->host);
247+
return verify_server_cert(st->ssl);
311248
}
312249

313250
int mbedtls_certificate(git_cert **out, git_stream *stream)

0 commit comments

Comments
 (0)