rugged 0.27.5 → 0.27.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rugged/version.rb +1 -1
  3. data/vendor/libgit2/CMakeLists.txt +2 -2
  4. data/vendor/libgit2/cmake/Modules/FindmbedTLS.cmake +93 -0
  5. data/vendor/libgit2/deps/http-parser/CMakeLists.txt +2 -0
  6. data/vendor/libgit2/include/git2.h +1 -0
  7. data/vendor/libgit2/include/git2/attr.h +18 -7
  8. data/vendor/libgit2/include/git2/blame.h +39 -22
  9. data/vendor/libgit2/include/git2/blob.h +1 -1
  10. data/vendor/libgit2/include/git2/branch.h +1 -1
  11. data/vendor/libgit2/include/git2/buffer.h +14 -2
  12. data/vendor/libgit2/include/git2/checkout.h +13 -12
  13. data/vendor/libgit2/include/git2/cherrypick.h +6 -4
  14. data/vendor/libgit2/include/git2/clone.h +8 -6
  15. data/vendor/libgit2/include/git2/commit.h +28 -0
  16. data/vendor/libgit2/include/git2/common.h +27 -0
  17. data/vendor/libgit2/include/git2/config.h +1 -0
  18. data/vendor/libgit2/include/git2/describe.h +30 -7
  19. data/vendor/libgit2/include/git2/diff.h +32 -22
  20. data/vendor/libgit2/include/git2/errors.h +1 -0
  21. data/vendor/libgit2/include/git2/ignore.h +2 -2
  22. data/vendor/libgit2/include/git2/mailmap.h +115 -0
  23. data/vendor/libgit2/include/git2/merge.h +10 -8
  24. data/vendor/libgit2/include/git2/proxy.h +9 -5
  25. data/vendor/libgit2/include/git2/rebase.h +7 -4
  26. data/vendor/libgit2/include/git2/refspec.h +17 -0
  27. data/vendor/libgit2/include/git2/remote.h +12 -10
  28. data/vendor/libgit2/include/git2/repository.h +7 -5
  29. data/vendor/libgit2/include/git2/revert.h +5 -3
  30. data/vendor/libgit2/include/git2/stash.h +11 -8
  31. data/vendor/libgit2/include/git2/status.h +7 -3
  32. data/vendor/libgit2/include/git2/submodule.h +8 -7
  33. data/vendor/libgit2/include/git2/sys/alloc.h +101 -0
  34. data/vendor/libgit2/include/git2/sys/index.h +3 -0
  35. data/vendor/libgit2/include/git2/sys/mempack.h +35 -35
  36. data/vendor/libgit2/include/git2/sys/merge.h +6 -1
  37. data/vendor/libgit2/include/git2/sys/path.h +55 -0
  38. data/vendor/libgit2/include/git2/transaction.h +1 -0
  39. data/vendor/libgit2/include/git2/types.h +8 -5
  40. data/vendor/libgit2/include/git2/version.h +2 -2
  41. data/vendor/libgit2/include/git2/worktree.h +46 -11
  42. data/vendor/libgit2/src/CMakeLists.txt +87 -15
  43. data/vendor/libgit2/src/alloc.c +47 -0
  44. data/vendor/libgit2/src/alloc.h +40 -0
  45. data/vendor/libgit2/src/apply.c +4 -4
  46. data/vendor/libgit2/src/attr.c +4 -4
  47. data/vendor/libgit2/src/attr_file.c +8 -7
  48. data/vendor/libgit2/src/attrcache.c +2 -2
  49. data/vendor/libgit2/src/blame.c +11 -4
  50. data/vendor/libgit2/src/blame.h +1 -0
  51. data/vendor/libgit2/src/blame_git.c +8 -5
  52. data/vendor/libgit2/src/blob.c +5 -5
  53. data/vendor/libgit2/src/branch.c +20 -20
  54. data/vendor/libgit2/src/buffer.c +9 -4
  55. data/vendor/libgit2/src/buffer.h +1 -1
  56. data/vendor/libgit2/src/checkout.c +33 -24
  57. data/vendor/libgit2/src/cherrypick.c +3 -3
  58. data/vendor/libgit2/src/clone.c +13 -13
  59. data/vendor/libgit2/src/commit.c +16 -3
  60. data/vendor/libgit2/src/common.h +3 -1
  61. data/vendor/libgit2/src/config.c +3 -3
  62. data/vendor/libgit2/src/config_file.c +193 -241
  63. data/vendor/libgit2/src/config_parse.c +89 -66
  64. data/vendor/libgit2/src/config_parse.h +2 -2
  65. data/vendor/libgit2/src/crlf.c +9 -3
  66. data/vendor/libgit2/src/diff.c +2 -2
  67. data/vendor/libgit2/src/diff_driver.c +3 -3
  68. data/vendor/libgit2/src/diff_file.c +3 -3
  69. data/vendor/libgit2/src/diff_generate.c +4 -3
  70. data/vendor/libgit2/src/diff_print.c +8 -8
  71. data/vendor/libgit2/src/diff_tform.c +1 -1
  72. data/vendor/libgit2/src/diff_xdiff.c +12 -0
  73. data/vendor/libgit2/src/features.h.in +2 -0
  74. data/vendor/libgit2/src/fetch.c +2 -2
  75. data/vendor/libgit2/src/fetchhead.c +5 -5
  76. data/vendor/libgit2/src/filebuf.c +4 -4
  77. data/vendor/libgit2/src/fileops.c +10 -10
  78. data/vendor/libgit2/src/filter.c +3 -3
  79. data/vendor/libgit2/src/global.c +17 -12
  80. data/vendor/libgit2/src/hash.h +2 -0
  81. data/vendor/libgit2/src/hash/hash_mbedtls.c +38 -0
  82. data/vendor/libgit2/src/hash/hash_mbedtls.h +20 -0
  83. data/vendor/libgit2/src/ignore.c +15 -20
  84. data/vendor/libgit2/src/index.c +77 -36
  85. data/vendor/libgit2/src/index.h +10 -0
  86. data/vendor/libgit2/src/indexer.c +9 -9
  87. data/vendor/libgit2/src/iterator.c +8 -8
  88. data/vendor/libgit2/src/khash.h +3 -1
  89. data/vendor/libgit2/src/mailmap.c +485 -0
  90. data/vendor/libgit2/src/mailmap.h +35 -0
  91. data/vendor/libgit2/src/merge.c +7 -7
  92. data/vendor/libgit2/src/netops.c +5 -5
  93. data/vendor/libgit2/src/notes.c +2 -2
  94. data/vendor/libgit2/src/odb.c +7 -7
  95. data/vendor/libgit2/src/odb_loose.c +13 -13
  96. data/vendor/libgit2/src/odb_pack.c +3 -3
  97. data/vendor/libgit2/src/pack-objects.c +2 -2
  98. data/vendor/libgit2/src/pack.c +5 -5
  99. data/vendor/libgit2/src/pack.h +1 -1
  100. data/vendor/libgit2/src/patch.c +1 -1
  101. data/vendor/libgit2/src/patch_generate.c +2 -2
  102. data/vendor/libgit2/src/patch_parse.c +11 -4
  103. data/vendor/libgit2/src/path.c +42 -66
  104. data/vendor/libgit2/src/path.h +2 -72
  105. data/vendor/libgit2/src/pathspec.c +1 -1
  106. data/vendor/libgit2/src/push.c +2 -2
  107. data/vendor/libgit2/src/rebase.c +19 -22
  108. data/vendor/libgit2/src/refdb_fs.c +65 -34
  109. data/vendor/libgit2/src/refs.c +6 -6
  110. data/vendor/libgit2/src/refspec.c +30 -5
  111. data/vendor/libgit2/src/refspec.h +1 -1
  112. data/vendor/libgit2/src/remote.c +40 -40
  113. data/vendor/libgit2/src/repository.c +63 -59
  114. data/vendor/libgit2/src/reset.c +1 -1
  115. data/vendor/libgit2/src/revert.c +3 -3
  116. data/vendor/libgit2/src/revparse.c +4 -4
  117. data/vendor/libgit2/src/revwalk.c +44 -10
  118. data/vendor/libgit2/src/revwalk.h +2 -1
  119. data/vendor/libgit2/src/settings.c +25 -1
  120. data/vendor/libgit2/src/signature.c +1 -1
  121. data/vendor/libgit2/src/stash.c +80 -34
  122. data/vendor/libgit2/src/status.c +1 -1
  123. data/vendor/libgit2/src/stdalloc.c +120 -0
  124. data/vendor/libgit2/src/stdalloc.h +17 -0
  125. data/vendor/libgit2/src/streams/mbedtls.c +463 -0
  126. data/vendor/libgit2/src/streams/mbedtls.h +20 -0
  127. data/vendor/libgit2/src/streams/openssl.c +132 -34
  128. data/vendor/libgit2/src/streams/openssl.h +0 -107
  129. data/vendor/libgit2/src/streams/tls.c +3 -0
  130. data/vendor/libgit2/src/submodule.c +117 -82
  131. data/vendor/libgit2/src/sysdir.c +4 -4
  132. data/vendor/libgit2/src/tag.c +7 -7
  133. data/vendor/libgit2/src/trace.h +1 -1
  134. data/vendor/libgit2/src/trailer.c +6 -6
  135. data/vendor/libgit2/src/transport.c +2 -2
  136. data/vendor/libgit2/src/transports/auth.c +1 -1
  137. data/vendor/libgit2/src/transports/auth_negotiate.c +2 -2
  138. data/vendor/libgit2/src/transports/git.c +1 -1
  139. data/vendor/libgit2/src/transports/http.c +12 -12
  140. data/vendor/libgit2/src/transports/local.c +7 -7
  141. data/vendor/libgit2/src/transports/smart.c +17 -8
  142. data/vendor/libgit2/src/transports/smart.h +5 -5
  143. data/vendor/libgit2/src/transports/smart_pkt.c +122 -130
  144. data/vendor/libgit2/src/transports/smart_protocol.c +48 -38
  145. data/vendor/libgit2/src/transports/ssh.c +1 -1
  146. data/vendor/libgit2/src/transports/winhttp.c +6 -6
  147. data/vendor/libgit2/src/tree.c +12 -53
  148. data/vendor/libgit2/src/tree.h +0 -12
  149. data/vendor/libgit2/src/util.c +16 -0
  150. data/vendor/libgit2/src/util.h +12 -135
  151. data/vendor/libgit2/src/win32/findfile.c +2 -2
  152. data/vendor/libgit2/src/win32/posix_w32.c +1 -1
  153. data/vendor/libgit2/src/win32/w32_crtdbg_stacktrace.c +94 -0
  154. data/vendor/libgit2/src/win32/w32_crtdbg_stacktrace.h +28 -75
  155. data/vendor/libgit2/src/worktree.c +64 -43
  156. data/vendor/libgit2/src/worktree.h +2 -0
  157. data/vendor/libgit2/src/xdiff/xdiffi.c +3 -1
  158. metadata +16 -2
@@ -294,7 +294,7 @@ int git_status_list_new(
294
294
 
295
295
  /* refresh index from disk unless prevented */
296
296
  if ((flags & GIT_STATUS_OPT_NO_REFRESH) == 0 &&
297
- git_index_read(index, false) < 0)
297
+ git_index_read_safely(index) < 0)
298
298
  giterr_clear();
299
299
 
300
300
  status = git_status_list_alloc(index);
@@ -0,0 +1,120 @@
1
+ /*
2
+ * Copyright (C) the libgit2 contributors. All rights reserved.
3
+ *
4
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
5
+ * a Linking Exception. For full terms see the included COPYING file.
6
+ */
7
+
8
+ #include "stdalloc.h"
9
+
10
+ static void *stdalloc__malloc(size_t len, const char *file, int line)
11
+ {
12
+ void *ptr = malloc(len);
13
+
14
+ GIT_UNUSED(file);
15
+ GIT_UNUSED(line);
16
+
17
+ if (!ptr) giterr_set_oom();
18
+ return ptr;
19
+ }
20
+
21
+ static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line)
22
+ {
23
+ void *ptr = calloc(nelem, elsize);
24
+
25
+ GIT_UNUSED(file);
26
+ GIT_UNUSED(line);
27
+
28
+ if (!ptr) giterr_set_oom();
29
+ return ptr;
30
+ }
31
+
32
+ static char *stdalloc__strdup(const char *str, const char *file, int line)
33
+ {
34
+ char *ptr = strdup(str);
35
+
36
+ GIT_UNUSED(file);
37
+ GIT_UNUSED(line);
38
+
39
+ if (!ptr) giterr_set_oom();
40
+ return ptr;
41
+ }
42
+
43
+ static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line)
44
+ {
45
+ size_t length = 0, alloclength;
46
+ char *ptr;
47
+
48
+ length = p_strnlen(str, n);
49
+
50
+ if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
51
+ !(ptr = stdalloc__malloc(alloclength, file, line)))
52
+ return NULL;
53
+
54
+ if (length)
55
+ memcpy(ptr, str, length);
56
+
57
+ ptr[length] = '\0';
58
+
59
+ return ptr;
60
+ }
61
+
62
+ static char *stdalloc__substrdup(const char *start, size_t n, const char *file, int line)
63
+ {
64
+ char *ptr;
65
+ size_t alloclen;
66
+
67
+ if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
68
+ !(ptr = stdalloc__malloc(alloclen, file, line)))
69
+ return NULL;
70
+
71
+ memcpy(ptr, start, n);
72
+ ptr[n] = '\0';
73
+ return ptr;
74
+ }
75
+
76
+ static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line)
77
+ {
78
+ void *new_ptr = realloc(ptr, size);
79
+
80
+ GIT_UNUSED(file);
81
+ GIT_UNUSED(line);
82
+
83
+ if (!new_ptr) giterr_set_oom();
84
+ return new_ptr;
85
+ }
86
+
87
+ static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
88
+ {
89
+ size_t newsize;
90
+
91
+ GIT_UNUSED(file);
92
+ GIT_UNUSED(line);
93
+
94
+ return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
95
+ NULL : realloc(ptr, newsize);
96
+ }
97
+
98
+ static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
99
+ {
100
+ return stdalloc__reallocarray(NULL, nelem, elsize, file, line);
101
+ }
102
+
103
+ static void stdalloc__free(void *ptr)
104
+ {
105
+ free(ptr);
106
+ }
107
+
108
+ int git_stdalloc_init_allocator(git_allocator *allocator)
109
+ {
110
+ allocator->gmalloc = stdalloc__malloc;
111
+ allocator->gcalloc = stdalloc__calloc;
112
+ allocator->gstrdup = stdalloc__strdup;
113
+ allocator->gstrndup = stdalloc__strndup;
114
+ allocator->gsubstrdup = stdalloc__substrdup;
115
+ allocator->grealloc = stdalloc__realloc;
116
+ allocator->greallocarray = stdalloc__reallocarray;
117
+ allocator->gmallocarray = stdalloc__mallocarray;
118
+ allocator->gfree = stdalloc__free;
119
+ return 0;
120
+ }
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright (C) the libgit2 contributors. All rights reserved.
3
+ *
4
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
5
+ * a Linking Exception. For full terms see the included COPYING file.
6
+ */
7
+
8
+ #ifndef INCLUDE_stdalloc_h__
9
+ #define INCLUDE_stdalloc_h__
10
+
11
+ #include "alloc.h"
12
+
13
+ #include "common.h"
14
+
15
+ int git_stdalloc_init_allocator(git_allocator *allocator);
16
+
17
+ #endif
@@ -0,0 +1,463 @@
1
+ /*
2
+ * Copyright (C) the libgit2 contributors. All rights reserved.
3
+ *
4
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
5
+ * a Linking Exception. For full terms see the included COPYING file.
6
+ */
7
+
8
+ #include "streams/mbedtls.h"
9
+
10
+ #ifdef GIT_MBEDTLS
11
+
12
+ #include <ctype.h>
13
+
14
+ #include "global.h"
15
+ #include "stream.h"
16
+ #include "streams/socket.h"
17
+ #include "netops.h"
18
+ #include "git2/transport.h"
19
+ #include "util.h"
20
+
21
+ #ifdef GIT_CURL
22
+ # include "streams/curl.h"
23
+ #endif
24
+
25
+ #ifndef GIT_DEFAULT_CERT_LOCATION
26
+ #define GIT_DEFAULT_CERT_LOCATION NULL
27
+ #endif
28
+
29
+ /* Work around C90-conformance issues */
30
+ #if defined(_MSC_VER)
31
+ # define inline __inline
32
+ #elif defined(__GNUC__)
33
+ # define inline __inline__
34
+ #else
35
+ # define inline
36
+ #endif
37
+
38
+ #include <mbedtls/config.h>
39
+ #include <mbedtls/ssl.h>
40
+ #include <mbedtls/error.h>
41
+ #include <mbedtls/entropy.h>
42
+ #include <mbedtls/ctr_drbg.h>
43
+
44
+ #undef inline
45
+
46
+ mbedtls_ssl_config *git__ssl_conf;
47
+ mbedtls_entropy_context *mbedtls_entropy;
48
+
49
+ #define GIT_SSL_DEFAULT_CIPHERS "TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256:TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256:TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384:TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-DSS-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-DSS-WITH-AES-256-GCM-SHA384:TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA256:TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA256:TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA:TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA:TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA384:TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA384:TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA:TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA:TLS-DHE-RSA-WITH-AES-128-CBC-SHA256:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-CBC-SHA:TLS-DHE-RSA-WITH-AES-256-CBC-SHA:TLS-DHE-DSS-WITH-AES-128-CBC-SHA256:TLS-DHE-DSS-WITH-AES-256-CBC-SHA256:TLS-DHE-DSS-WITH-AES-128-CBC-SHA:TLS-DHE-DSS-WITH-AES-256-CBC-SHA:TLS-RSA-WITH-AES-128-GCM-SHA256:TLS-RSA-WITH-AES-256-GCM-SHA384:TLS-RSA-WITH-AES-128-CBC-SHA256:TLS-RSA-WITH-AES-256-CBC-SHA256:TLS-RSA-WITH-AES-128-CBC-SHA:TLS-RSA-WITH-AES-256-CBC-SHA"
50
+ #define GIT_SSL_DEFAULT_CIPHERS_COUNT 30
51
+
52
+ /**
53
+ * This function aims to clean-up the SSL context which
54
+ * we allocated.
55
+ */
56
+ static void shutdown_ssl(void)
57
+ {
58
+ if (git__ssl_conf) {
59
+ mbedtls_x509_crt_free(git__ssl_conf->ca_chain);
60
+ git__free(git__ssl_conf->ca_chain);
61
+ mbedtls_ctr_drbg_free(git__ssl_conf->p_rng);
62
+ git__free(git__ssl_conf->p_rng);
63
+ mbedtls_ssl_config_free(git__ssl_conf);
64
+ git__free(git__ssl_conf);
65
+ git__ssl_conf = NULL;
66
+ }
67
+ if (mbedtls_entropy) {
68
+ mbedtls_entropy_free(mbedtls_entropy);
69
+ git__free(mbedtls_entropy);
70
+ mbedtls_entropy = NULL;
71
+ }
72
+ }
73
+
74
+ int git_mbedtls__set_cert_location(const char *path, int is_dir);
75
+
76
+ int git_mbedtls_stream_global_init(void)
77
+ {
78
+ int loaded = 0;
79
+ char *crtpath = GIT_DEFAULT_CERT_LOCATION;
80
+ struct stat statbuf;
81
+ mbedtls_ctr_drbg_context *ctr_drbg = NULL;
82
+
83
+ int *ciphers_list = NULL;
84
+ int ciphers_known = 0;
85
+ char *cipher_name = NULL;
86
+ char *cipher_string = NULL;
87
+ char *cipher_string_tmp = NULL;
88
+
89
+ mbedtls_x509_crt *cacert = NULL;
90
+
91
+ git__ssl_conf = git__malloc(sizeof(mbedtls_ssl_config));
92
+ mbedtls_ssl_config_init(git__ssl_conf);
93
+ if (mbedtls_ssl_config_defaults(git__ssl_conf,
94
+ MBEDTLS_SSL_IS_CLIENT,
95
+ MBEDTLS_SSL_TRANSPORT_STREAM,
96
+ MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
97
+ giterr_set(GITERR_SSL, "failed to initialize mbedTLS");
98
+ goto cleanup;
99
+ }
100
+
101
+ /* configure TLSv1 */
102
+ mbedtls_ssl_conf_min_version(git__ssl_conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0);
103
+
104
+ /* verify_server_cert is responsible for making the check.
105
+ * OPTIONAL because REQUIRED drops the certificate as soon as the check
106
+ * is made, so we can never see the certificate and override it. */
107
+ mbedtls_ssl_conf_authmode(git__ssl_conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
108
+
109
+ /* set the list of allowed ciphersuites */
110
+ ciphers_list = calloc(GIT_SSL_DEFAULT_CIPHERS_COUNT, sizeof(int));
111
+ ciphers_known = 0;
112
+ cipher_string = cipher_string_tmp = git__strdup(GIT_SSL_DEFAULT_CIPHERS);
113
+ while ((cipher_name = git__strtok(&cipher_string_tmp, ":")) != NULL) {
114
+ int cipherid = mbedtls_ssl_get_ciphersuite_id(cipher_name);
115
+ if (cipherid == 0) continue;
116
+
117
+ ciphers_list[ciphers_known++] = cipherid;
118
+ }
119
+ git__free(cipher_string);
120
+
121
+ if (!ciphers_known) {
122
+ giterr_set(GITERR_SSL, "no cipher could be enabled");
123
+ goto cleanup;
124
+ }
125
+ mbedtls_ssl_conf_ciphersuites(git__ssl_conf, ciphers_list);
126
+
127
+ /* Seeding the random number generator */
128
+ mbedtls_entropy = git__malloc(sizeof(mbedtls_entropy_context));
129
+ mbedtls_entropy_init(mbedtls_entropy);
130
+
131
+ ctr_drbg = git__malloc(sizeof(mbedtls_ctr_drbg_context));
132
+ mbedtls_ctr_drbg_init(ctr_drbg);
133
+ if (mbedtls_ctr_drbg_seed(ctr_drbg,
134
+ mbedtls_entropy_func,
135
+ mbedtls_entropy, NULL, 0) != 0) {
136
+ giterr_set(GITERR_SSL, "failed to initialize mbedTLS entropy pool");
137
+ goto cleanup;
138
+ }
139
+
140
+ mbedtls_ssl_conf_rng(git__ssl_conf, mbedtls_ctr_drbg_random, ctr_drbg);
141
+
142
+ /* load default certificates */
143
+ if (crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISREG(statbuf.st_mode))
144
+ loaded = (git_mbedtls__set_cert_location(crtpath, 0) == 0);
145
+ if (!loaded && crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISDIR(statbuf.st_mode))
146
+ loaded = (git_mbedtls__set_cert_location(crtpath, 1) == 0);
147
+
148
+ git__on_shutdown(shutdown_ssl);
149
+
150
+ return 0;
151
+
152
+ cleanup:
153
+ mbedtls_x509_crt_free(cacert);
154
+ git__free(cacert);
155
+ mbedtls_ctr_drbg_free(ctr_drbg);
156
+ git__free(ctr_drbg);
157
+ mbedtls_ssl_config_free(git__ssl_conf);
158
+ git__free(git__ssl_conf);
159
+ git__ssl_conf = NULL;
160
+
161
+ return -1;
162
+ }
163
+
164
+ mbedtls_ssl_config *git__ssl_conf;
165
+
166
+ static int bio_read(void *b, unsigned char *buf, size_t len)
167
+ {
168
+ git_stream *io = (git_stream *) b;
169
+ return (int) git_stream_read(io, buf, len);
170
+ }
171
+
172
+ static int bio_write(void *b, const unsigned char *buf, size_t len)
173
+ {
174
+ git_stream *io = (git_stream *) b;
175
+ return (int) git_stream_write(io, (const char *)buf, len, 0);
176
+ }
177
+
178
+ static int ssl_set_error(mbedtls_ssl_context *ssl, int error)
179
+ {
180
+ char errbuf[512];
181
+ int ret = -1;
182
+
183
+ assert(error != MBEDTLS_ERR_SSL_WANT_READ);
184
+ assert(error != MBEDTLS_ERR_SSL_WANT_WRITE);
185
+
186
+ if (error != 0)
187
+ mbedtls_strerror( error, errbuf, 512 );
188
+
189
+ switch(error) {
190
+ case 0:
191
+ giterr_set(GITERR_SSL, "SSL error: unknown error");
192
+ break;
193
+
194
+ case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:
195
+ giterr_set(GITERR_SSL, "SSL error: %#04x [%x] - %s", error, ssl->session_negotiate->verify_result, errbuf);
196
+ ret = GIT_ECERTIFICATE;
197
+ break;
198
+
199
+ default:
200
+ giterr_set(GITERR_SSL, "SSL error: %#04x - %s", error, errbuf);
201
+ }
202
+
203
+ return ret;
204
+ }
205
+
206
+ static int ssl_teardown(mbedtls_ssl_context *ssl)
207
+ {
208
+ int ret = 0;
209
+
210
+ ret = mbedtls_ssl_close_notify(ssl);
211
+ if (ret < 0)
212
+ ret = ssl_set_error(ssl, ret);
213
+
214
+ mbedtls_ssl_free(ssl);
215
+ return ret;
216
+ }
217
+
218
+ static int verify_server_cert(mbedtls_ssl_context *ssl)
219
+ {
220
+ int ret = -1;
221
+
222
+ if ((ret = mbedtls_ssl_get_verify_result(ssl)) != 0) {
223
+ char vrfy_buf[512];
224
+ int len = mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "", ret);
225
+ if (len >= 1) vrfy_buf[len - 1] = '\0'; /* Remove trailing \n */
226
+ giterr_set(GITERR_SSL, "the SSL certificate is invalid: %#04x - %s", ret, vrfy_buf);
227
+ return GIT_ECERTIFICATE;
228
+ }
229
+
230
+ return 0;
231
+ }
232
+
233
+ typedef struct {
234
+ git_stream parent;
235
+ git_stream *io;
236
+ bool connected;
237
+ char *host;
238
+ mbedtls_ssl_context *ssl;
239
+ git_cert_x509 cert_info;
240
+ } mbedtls_stream;
241
+
242
+
243
+ int mbedtls_connect(git_stream *stream)
244
+ {
245
+ int ret;
246
+ mbedtls_stream *st = (mbedtls_stream *) stream;
247
+
248
+ if ((ret = git_stream_connect(st->io)) < 0)
249
+ return ret;
250
+
251
+ st->connected = true;
252
+
253
+ mbedtls_ssl_set_hostname(st->ssl, st->host);
254
+
255
+ mbedtls_ssl_set_bio(st->ssl, st->io, bio_write, bio_read, NULL);
256
+
257
+ if ((ret = mbedtls_ssl_handshake(st->ssl)) != 0)
258
+ return ssl_set_error(st->ssl, ret);
259
+
260
+ return verify_server_cert(st->ssl);
261
+ }
262
+
263
+ int mbedtls_certificate(git_cert **out, git_stream *stream)
264
+ {
265
+ unsigned char *encoded_cert;
266
+ mbedtls_stream *st = (mbedtls_stream *) stream;
267
+
268
+ const mbedtls_x509_crt *cert = mbedtls_ssl_get_peer_cert(st->ssl);
269
+ if (!cert) {
270
+ giterr_set(GITERR_SSL, "the server did not provide a certificate");
271
+ return -1;
272
+ }
273
+
274
+ /* Retrieve the length of the certificate first */
275
+ if (cert->raw.len == 0) {
276
+ giterr_set(GITERR_NET, "failed to retrieve certificate information");
277
+ return -1;
278
+ }
279
+
280
+ encoded_cert = git__malloc(cert->raw.len);
281
+ GITERR_CHECK_ALLOC(encoded_cert);
282
+ memcpy(encoded_cert, cert->raw.p, cert->raw.len);
283
+
284
+ st->cert_info.parent.cert_type = GIT_CERT_X509;
285
+ st->cert_info.data = encoded_cert;
286
+ st->cert_info.len = cert->raw.len;
287
+
288
+ *out = &st->cert_info.parent;
289
+
290
+ return 0;
291
+ }
292
+
293
+ static int mbedtls_set_proxy(git_stream *stream, const git_proxy_options *proxy_options)
294
+ {
295
+ mbedtls_stream *st = (mbedtls_stream *) stream;
296
+
297
+ return git_stream_set_proxy(st->io, proxy_options);
298
+ }
299
+
300
+ ssize_t mbedtls_stream_write(git_stream *stream, const char *data, size_t len, int flags)
301
+ {
302
+ size_t read = 0;
303
+ mbedtls_stream *st = (mbedtls_stream *) stream;
304
+
305
+ GIT_UNUSED(flags);
306
+
307
+ do {
308
+ int error = mbedtls_ssl_write(st->ssl, (const unsigned char *)data + read, len - read);
309
+ if (error <= 0) {
310
+ return ssl_set_error(st->ssl, error);
311
+ }
312
+ read += error;
313
+ } while (read < len);
314
+
315
+ return read;
316
+ }
317
+
318
+ ssize_t mbedtls_stream_read(git_stream *stream, void *data, size_t len)
319
+ {
320
+ mbedtls_stream *st = (mbedtls_stream *) stream;
321
+ int ret;
322
+
323
+ if ((ret = mbedtls_ssl_read(st->ssl, (unsigned char *)data, len)) <= 0)
324
+ ssl_set_error(st->ssl, ret);
325
+
326
+ return ret;
327
+ }
328
+
329
+ int mbedtls_stream_close(git_stream *stream)
330
+ {
331
+ mbedtls_stream *st = (mbedtls_stream *) stream;
332
+ int ret = 0;
333
+
334
+ if (st->connected && (ret = ssl_teardown(st->ssl)) != 0)
335
+ return -1;
336
+
337
+ st->connected = false;
338
+
339
+ return git_stream_close(st->io);
340
+ }
341
+
342
+ void mbedtls_stream_free(git_stream *stream)
343
+ {
344
+ mbedtls_stream *st = (mbedtls_stream *) stream;
345
+
346
+ git__free(st->host);
347
+ git__free(st->cert_info.data);
348
+ git_stream_free(st->io);
349
+ git__free(st->ssl);
350
+ git__free(st);
351
+ }
352
+
353
+ int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port)
354
+ {
355
+ int error;
356
+ mbedtls_stream *st;
357
+
358
+ st = git__calloc(1, sizeof(mbedtls_stream));
359
+ GITERR_CHECK_ALLOC(st);
360
+
361
+ #ifdef GIT_CURL
362
+ error = git_curl_stream_new(&st->io, host, port);
363
+ #else
364
+ error = git_socket_stream_new(&st->io, host, port);
365
+ #endif
366
+
367
+ if (error < 0)
368
+ goto out_err;
369
+
370
+ st->ssl = git__malloc(sizeof(mbedtls_ssl_context));
371
+ GITERR_CHECK_ALLOC(st->ssl);
372
+ mbedtls_ssl_init(st->ssl);
373
+ if (mbedtls_ssl_setup(st->ssl, git__ssl_conf)) {
374
+ giterr_set(GITERR_SSL, "failed to create ssl object");
375
+ error = -1;
376
+ goto out_err;
377
+ }
378
+
379
+ st->host = git__strdup(host);
380
+ GITERR_CHECK_ALLOC(st->host);
381
+
382
+ st->parent.version = GIT_STREAM_VERSION;
383
+ st->parent.encrypted = 1;
384
+ st->parent.proxy_support = git_stream_supports_proxy(st->io);
385
+ st->parent.connect = mbedtls_connect;
386
+ st->parent.certificate = mbedtls_certificate;
387
+ st->parent.set_proxy = mbedtls_set_proxy;
388
+ st->parent.read = mbedtls_stream_read;
389
+ st->parent.write = mbedtls_stream_write;
390
+ st->parent.close = mbedtls_stream_close;
391
+ st->parent.free = mbedtls_stream_free;
392
+
393
+ *out = (git_stream *) st;
394
+ return 0;
395
+
396
+ out_err:
397
+ mbedtls_ssl_free(st->ssl);
398
+ git_stream_free(st->io);
399
+ git__free(st);
400
+
401
+ return error;
402
+ }
403
+
404
+ int git_mbedtls__set_cert_location(const char *path, int is_dir)
405
+ {
406
+ int ret = 0;
407
+ char errbuf[512];
408
+ mbedtls_x509_crt *cacert;
409
+
410
+ assert(path != NULL);
411
+
412
+ cacert = git__malloc(sizeof(mbedtls_x509_crt));
413
+ mbedtls_x509_crt_init(cacert);
414
+ if (is_dir) {
415
+ ret = mbedtls_x509_crt_parse_path(cacert, path);
416
+ } else {
417
+ ret = mbedtls_x509_crt_parse_file(cacert, path);
418
+ }
419
+ /* mbedtls_x509_crt_parse_path returns the number of invalid certs on success */
420
+ if (ret < 0) {
421
+ mbedtls_x509_crt_free(cacert);
422
+ git__free(cacert);
423
+ mbedtls_strerror( ret, errbuf, 512 );
424
+ giterr_set(GITERR_SSL, "failed to load CA certificates: %#04x - %s", ret, errbuf);
425
+ return -1;
426
+ }
427
+
428
+ mbedtls_x509_crt_free(git__ssl_conf->ca_chain);
429
+ git__free(git__ssl_conf->ca_chain);
430
+ mbedtls_ssl_conf_ca_chain(git__ssl_conf, cacert, NULL);
431
+
432
+ return 0;
433
+ }
434
+
435
+ #else
436
+
437
+ #include "stream.h"
438
+
439
+ int git_mbedtls_stream_global_init(void)
440
+ {
441
+ return 0;
442
+ }
443
+
444
+ int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port)
445
+ {
446
+ GIT_UNUSED(out);
447
+ GIT_UNUSED(host);
448
+ GIT_UNUSED(port);
449
+
450
+ giterr_set(GITERR_SSL, "mbedTLS is not supported in this version");
451
+ return -1;
452
+ }
453
+
454
+ int git_mbedtls__set_cert_location(const char *path, int is_dir)
455
+ {
456
+ GIT_UNUSED(path);
457
+ GIT_UNUSED(is_dir);
458
+
459
+ giterr_set(GITERR_SSL, "mbedTLS is not supported in this version");
460
+ return -1;
461
+ }
462
+
463
+ #endif