Commit 94fc83b6d7f179190142b91abd0d22240aced0c1

Etienne Samson 2019-06-13T16:48:35

cmake: Modulize our TLS & hash detection The interactions between `USE_HTTPS` and `SHA1_BACKEND` have been streamlined. Previously we would have accepted not quite working configurations (like, `-DUSE_HTTPS=OFF -DSHA1_BACKEND=OpenSSL`) and, as the OpenSSL detection only ran with `USE_HTTPS`, the link would fail. The detection was moved to a new `USE_SHA1`, modeled after `USE_HTTPS`, which takes the values "CollisionDetection/Backend/Generic", to better match how the "hashing backend" is selected, the default (ON) being "CollisionDetection". Note that, as `SHA1_BACKEND` is still used internally, you might need to check what customization you're using it for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b7f2bc7..127449c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -52,11 +52,9 @@ OPTION(TAGS				"Generate tags"						OFF)
 OPTION(PROFILE				"Generate profiling information"			OFF)
 OPTION(ENABLE_TRACE			"Enables tracing support"				OFF)
 OPTION(LIBGIT2_FILENAME			"Name of the produced binary"				OFF)
-
-   SET(SHA1_BACKEND 			"CollisionDetection"			       CACHE STRING
-       "Backend to use for SHA1. One of Generic, OpenSSL, Win32, CommonCrypto, mbedTLS, CollisionDetection.")
 OPTION(USE_SSH				"Link with libssh2 to enable SSH support"		 ON)
 OPTION(USE_HTTPS			"Enable HTTPS support. Can be set to a specific backend" ON)
+OPTION(USE_SHA1				"Enable SHA1. Can be set to CollisionDetection(ON)/HTTPS/Generic" ON)
 OPTION(USE_GSSAPI			"Link with libgssapi for SPNEGO auth"			OFF)
 OPTION(USE_STANDALONE_FUZZERS		"Enable standalone fuzzers (compatible with gcc)"	OFF)
 OPTION(VALGRIND				"Configure build for valgrind"				OFF)
diff --git a/azure-pipelines.yml b/azure-pipelines.yml
index 2ea2ffb..7c63b2b 100644
--- a/azure-pipelines.yml
+++ b/azure-pipelines.yml
@@ -29,7 +29,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=gcc
-       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DSHA1_BACKEND=mbedTLS -DDEPRECATE_HARD=ON
+       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON
        LEAK_CHECK=valgrind
 
 - job: linux_amd64_trusty_clang_openssl
@@ -55,7 +55,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=clang
-       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DSHA1_BACKEND=mbedTLS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON
+       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON
        LEAK_CHECK=valgrind
 
 - job: macos
diff --git a/azure-pipelines/nightly.yml b/azure-pipelines/nightly.yml
index 696dbf1..57b5a6f 100644
--- a/azure-pipelines/nightly.yml
+++ b/azure-pipelines/nightly.yml
@@ -26,7 +26,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=gcc
-       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DSHA1_BACKEND=mbedTLS -DDEPRECATE_HARD=ON
+       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON
        LEAK_CHECK=valgrind
        RUN_INVASIVE_TESTS=true
 
@@ -54,7 +54,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=clang
-       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DSHA1_BACKEND=mbedTLS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON
+       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON
        LEAK_CHECK=valgrind
        RUN_INVASIVE_TESTS=true
 
diff --git a/cmake/Modules/SelectHTTPSBackend.cmake b/cmake/Modules/SelectHTTPSBackend.cmake
new file mode 100644
index 0000000..a97ebe3
--- /dev/null
+++ b/cmake/Modules/SelectHTTPSBackend.cmake
@@ -0,0 +1,126 @@
+# Select the backend to use
+
+# We try to find any packages our backends might use
+FIND_PACKAGE(OpenSSL)
+FIND_PACKAGE(mbedTLS)
+IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
+	FIND_PACKAGE(Security)
+	FIND_PACKAGE(CoreFoundation)
+ENDIF()
+
+# Auto-select TLS backend
+IF (USE_HTTPS STREQUAL ON)
+	message(ON)
+	IF (SECURITY_FOUND)
+		IF (SECURITY_HAS_SSLCREATECONTEXT)
+			SET(HTTPS_BACKEND "SecureTransport")
+		ELSE()
+			MESSAGE("-- Security framework is too old, falling back to OpenSSL")
+			SET(HTTPS_BACKEND "OpenSSL")
+		ENDIF()
+	ELSEIF (WINHTTP)
+		SET(HTTPS_BACKEND "WinHTTP")
+	ELSEIF(OPENSSL_FOUND)
+		SET(HTTPS_BACKEND "OpenSSL")
+	ELSEIF(MBEDTLS_FOUND)
+		SET(HTTPS_BACKEND "mbedTLS")
+	ELSE()
+		MESSAGE(FATAL_ERROR "Unable to autodetect a usable HTTPS backend."
+			"Please pass the backend name explicitly (-DUSE_HTTPS=backend)")
+	ENDIF()
+ELSEIF(USE_HTTPS)
+	message(expl)
+	# HTTPS backend was explicitly set
+	SET(HTTPS_BACKEND ${USE_HTTPS})
+ELSE()
+	SET(HTTPS_BACKEND NO)
+ENDIF()
+
+IF(HTTPS_BACKEND)
+	# Check that we can find what's required for the selected backend
+	IF (HTTPS_BACKEND STREQUAL "SecureTransport")
+		IF (NOT COREFOUNDATION_FOUND)
+			MESSAGE(FATAL_ERROR "Cannot use SecureTransport backend, CoreFoundation.framework not found")
+		ENDIF()
+		IF (NOT SECURITY_FOUND)
+			MESSAGE(FATAL_ERROR "Cannot use SecureTransport backend, Security.framework not found")
+		ENDIF()
+		IF (NOT SECURITY_HAS_SSLCREATECONTEXT)
+			MESSAGE(FATAL_ERROR "Cannot use SecureTransport backend, SSLCreateContext not supported")
+		ENDIF()
+
+		SET(GIT_SECURE_TRANSPORT 1)
+		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${SECURITY_INCLUDE_DIR})
+		LIST(APPEND LIBGIT2_LIBS ${COREFOUNDATION_LIBRARIES} ${SECURITY_LIBRARIES})
+		LIST(APPEND LIBGIT2_PC_LIBS ${COREFOUNDATION_LDFLAGS} ${SECURITY_LDFLAGS})
+	ELSEIF (HTTPS_BACKEND STREQUAL "OpenSSL")
+		IF (NOT OPENSSL_FOUND)
+			MESSAGE(FATAL_ERROR "Asked for OpenSSL TLS backend, but it wasn't found")
+		ENDIF()
+
+		SET(GIT_OPENSSL 1)
+		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${OPENSSL_INCLUDE_DIR})
+		LIST(APPEND LIBGIT2_LIBS ${OPENSSL_LIBRARIES})
+		LIST(APPEND LIBGIT2_PC_LIBS ${OPENSSL_LDFLAGS})
+		LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
+	ELSEIF(HTTPS_BACKEND STREQUAL "mbedTLS")
+		IF (NOT MBEDTLS_FOUND)
+			MESSAGE(FATAL_ERROR "Asked for mbedTLS backend, but it wasn't found")
+		ENDIF()
+
+		IF(NOT CERT_LOCATION)
+			MESSAGE("Auto-detecting default certificates location")
+			IF(CMAKE_SYSTEM_NAME MATCHES Darwin)
+				# Check for an Homebrew installation
+				SET(OPENSSL_CMD "/usr/local/opt/openssl/bin/openssl")
+			ELSE()
+				SET(OPENSSL_CMD "openssl")
+			ENDIF()
+			EXECUTE_PROCESS(COMMAND ${OPENSSL_CMD} version -d OUTPUT_VARIABLE OPENSSL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
+			IF(OPENSSL_DIR)
+				STRING(REGEX REPLACE "^OPENSSLDIR: \"(.*)\"$" "\\1/" OPENSSL_DIR ${OPENSSL_DIR})
+
+				SET(OPENSSL_CA_LOCATIONS
+					"ca-bundle.pem"             # OpenSUSE Leap 42.1
+					"cert.pem"                  # Ubuntu 14.04, FreeBSD
+					"certs/ca-certificates.crt" # Ubuntu 16.04
+					"certs/ca.pem"              # Debian 7
+				)
+				FOREACH(SUFFIX IN LISTS OPENSSL_CA_LOCATIONS)
+					SET(LOC "${OPENSSL_DIR}${SUFFIX}")
+					IF(NOT CERT_LOCATION AND EXISTS "${OPENSSL_DIR}${SUFFIX}")
+						SET(CERT_LOCATION ${LOC})
+					ENDIF()
+				ENDFOREACH()
+			ELSE()
+				MESSAGE("Unable to find OpenSSL executable. Please provide default certificate location via CERT_LOCATION")
+			ENDIF()
+		ENDIF()
+
+		IF(CERT_LOCATION)
+			IF(NOT EXISTS ${CERT_LOCATION})
+				MESSAGE(FATAL_ERROR "Cannot use CERT_LOCATION=${CERT_LOCATION} as it doesn't exist")
+			ENDIF()
+			ADD_FEATURE_INFO(CERT_LOCATION ON "using certificates from ${CERT_LOCATION}")
+			ADD_DEFINITIONS(-DGIT_DEFAULT_CERT_LOCATION="${CERT_LOCATION}")
+		ENDIF()
+
+		SET(GIT_MBEDTLS 1)
+		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
+		LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
+		# mbedTLS has no pkgconfig file, hence we can't require it
+		# https://github.com/ARMmbed/mbedtls/issues/228
+		# For now, pass its link flags as our own
+		LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
+	ELSEIF (HTTPS_BACKEND STREQUAL "WinHTTP")
+		# WinHTTP setup was handled in the WinHTTP-specific block above
+	ELSE()
+		MESSAGE(FATAL_ERROR "Asked for backend ${HTTPS_BACKEND} but it wasn't found")
+	ENDIF()
+
+	SET(GIT_HTTPS 1)
+	ADD_FEATURE_INFO(HTTPS GIT_HTTPS "using ${HTTPS_BACKEND}")
+ELSE()
+	SET(GIT_HTTPS 0)
+	ADD_FEATURE_INFO(HTTPS NO "")
+ENDIF()
diff --git a/cmake/Modules/SelectHashes.cmake b/cmake/Modules/SelectHashes.cmake
new file mode 100644
index 0000000..450e2bd
--- /dev/null
+++ b/cmake/Modules/SelectHashes.cmake
@@ -0,0 +1,64 @@
+# Select a hash backend
+
+# USE_SHA1=CollisionDetection(ON)/HTTPS/Generic/OFF
+
+IF(USE_SHA1 STREQUAL ON OR USE_SHA1 STREQUAL "CollisionDetection")
+	SET(SHA1_BACKEND "CollisionDetection")
+ELSEIF(USE_SHA1 STREQUAL "HTTPS")
+	message("Checking HTTPS backend… ${HTTPS_BACKEND}")
+	IF(HTTPS_BACKEND STREQUAL "SecureTransport")
+		SET(SHA1_BACKEND "CommonCrypto")
+	ELSEIF(HTTPS_BACKEND STREQUAL "WinHTTP")
+		SET(SHA1_BACKEND "Win32")
+	ELSEIF(HTTPS_BACKEND)
+		SET(SHA1_BACKEND ${HTTPS_BACKEND})
+	ELSE()
+	ENDIF()
+	IF(NOT HTTPS_BACKEND)
+		SET(SHA1_BACKEND "CollisionDetection")
+	ENDIF()
+	message(STATUS "Using SHA1 backend ${SHA1_BACKEND}")
+ELSEIF(USE_SHA1 STREQUAL "Generic")
+	SET(SHA1_BACKEND "Generic")
+# ELSEIF(NOT USE_SHA1)
+ELSE()
+	MESSAGE(FATAL_ERROR "Invalid value for USE_SHA1: ${USE_SHA1}")
+ENDIF()
+
+IF(SHA1_BACKEND STREQUAL "CollisionDetection")
+	SET(GIT_SHA1_COLLISIONDETECT 1)
+	ADD_DEFINITIONS(-DSHA1DC_NO_STANDARD_INCLUDES=1)
+	ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\")
+	ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\")
+	FILE(GLOB SRC_SHA1 hash/hash_collisiondetect.c hash/sha1dc/*)
+ELSEIF(SHA1_BACKEND STREQUAL "OpenSSL")
+	# OPENSSL_FOUND should already be set, we're checking HTTPS_BACKEND
+
+	SET(GIT_SHA1_OPENSSL 1)
+	IF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
+		LIST(APPEND LIBGIT2_PC_LIBS "-lssl")
+	ELSE()
+		LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
+	ENDIF()
+ELSEIF(SHA1_BACKEND STREQUAL "CommonCrypto")
+	SET(GIT_SHA1_COMMON_CRYPTO 1)
+ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS")
+	SET(GIT_SHA1_MBEDTLS 1)
+	FILE(GLOB SRC_SHA1 hash/hash_mbedtls.c)
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
+	LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
+	# mbedTLS has no pkgconfig file, hence we can't require it
+	# https://github.com/ARMmbed/mbedtls/issues/228
+	# For now, pass its link flags as our own
+	LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
+ELSEIF(SHA1_BACKEND STREQUAL "Win32")
+	SET(GIT_SHA1_WIN32 1)
+	FILE(GLOB SRC_SHA1 hash/hash_win32.c)
+ELSEIF(SHA1_BACKEND STREQUAL "Generic")
+	FILE(GLOB SRC_SHA1 hash/hash_generic.c)
+# ELSEIF(NOT USE_SHA1)
+ELSE()
+	MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend: ${SHA1_BACKEND}")
+ENDIF()
+
+ADD_FEATURE_INFO(SHA ON "using ${SHA1_BACKEND}")
diff --git a/docs/changelog.md b/docs/changelog.md
index 75f8392..e5eaf07 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -10,6 +10,13 @@ v0.28 + 1
   system http-parser implementation despite incompatibilities, you can
   specify `-DUSE_HTTP_PARSER=system` to CMake.
 
+* The interactions between `USE_HTTPS` and `SHA1_BACKEND` have been
+  streamlined. The detection was moved to a new `USE_SHA1`, modeled after
+  `USE_HTTPS`, which takes the values "CollisionDetection/Backend/Generic", to
+  better match how the "hashing backend" is selected, the default (ON) being
+  "CollisionDetection". If you were using `SHA1_BACKEND` previously, you'll
+  need to check the value you've used, or switch to the autodetection.
+
 ### Changes or improvements
 
 * libgit2 can now correctly cope with URLs where the host contains a colon
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 5a0cf34..7ab1832 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -122,172 +122,8 @@ IF (WIN32 AND WINHTTP)
 	LIST(APPEND LIBGIT2_PC_LIBS "-lrpcrt4" "-lcrypt32" "-lole32")
 ENDIF()
 
-IF (USE_HTTPS)
-	# We try to find any packages our backends might use
-	FIND_PACKAGE(OpenSSL)
-	FIND_PACKAGE(mbedTLS)
-	IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
-		FIND_PACKAGE(Security)
-		FIND_PACKAGE(CoreFoundation)
-	ENDIF()
-
-	# Auto-select TLS backend
-	IF (USE_HTTPS STREQUAL ON)
-		IF (SECURITY_FOUND)
-			IF (SECURITY_HAS_SSLCREATECONTEXT)
-				SET(HTTPS_BACKEND "SecureTransport")
-			ELSE()
-				MESSAGE("-- Security framework is too old, falling back to OpenSSL")
-				SET(HTTPS_BACKEND "OpenSSL")
-			ENDIF()
-		ELSEIF (WINHTTP)
-			SET(HTTPS_BACKEND "WinHTTP")
-		ELSEIF(OPENSSL_FOUND)
-			SET(HTTPS_BACKEND "OpenSSL")
-		ELSEIF(MBEDTLS_FOUND)
-			SET(HTTPS_BACKEND "mbedTLS")
-		ELSE()
-			MESSAGE(FATAL_ERROR "Unable to autodetect a usable HTTPS backend."
-				"Please pass the backend name explicitly (-DUSE_HTTPS=backend)")
-		ENDIF()
-	ELSE()
-		# Backend was explicitly set
-		SET(HTTPS_BACKEND ${USE_HTTPS})
-	ENDIF()
-
-	# Check that we can find what's required for the selected backend
-	IF (HTTPS_BACKEND STREQUAL "SecureTransport")
-		IF (NOT COREFOUNDATION_FOUND)
-			MESSAGE(FATAL_ERROR "Cannot use SecureTransport backend, CoreFoundation.framework not found")
-		ENDIF()
-		IF (NOT SECURITY_FOUND)
-			MESSAGE(FATAL_ERROR "Cannot use SecureTransport backend, Security.framework not found")
-		ENDIF()
-		IF (NOT SECURITY_HAS_SSLCREATECONTEXT)
-			MESSAGE(FATAL_ERROR "Cannot use SecureTransport backend, SSLCreateContext not supported")
-		ENDIF()
-
-		SET(GIT_SECURE_TRANSPORT 1)
-		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${SECURITY_INCLUDE_DIR})
-		LIST(APPEND LIBGIT2_LIBS ${COREFOUNDATION_LIBRARIES} ${SECURITY_LIBRARIES})
-		LIST(APPEND LIBGIT2_PC_LIBS ${COREFOUNDATION_LDFLAGS} ${SECURITY_LDFLAGS})
-	ELSEIF (HTTPS_BACKEND STREQUAL "OpenSSL")
-		IF (NOT OPENSSL_FOUND)
-			MESSAGE(FATAL_ERROR "Asked for OpenSSL TLS backend, but it wasn't found")
-		ENDIF()
-
-		SET(GIT_OPENSSL 1)
-		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${OPENSSL_INCLUDE_DIR})
-		LIST(APPEND LIBGIT2_LIBS ${OPENSSL_LIBRARIES})
-		LIST(APPEND LIBGIT2_PC_LIBS ${OPENSSL_LDFLAGS})
-		LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
-	ELSEIF(HTTPS_BACKEND STREQUAL "mbedTLS")
-		IF (NOT MBEDTLS_FOUND)
-			MESSAGE(FATAL_ERROR "Asked for mbedTLS backend, but it wasn't found")
-		ENDIF()
-
-		IF(NOT CERT_LOCATION)
-			MESSAGE("Auto-detecting default certificates location")
-			IF(CMAKE_SYSTEM_NAME MATCHES Darwin)
-				# Check for an Homebrew installation
-				SET(OPENSSL_CMD "/usr/local/opt/openssl/bin/openssl")
-			ELSE()
-				SET(OPENSSL_CMD "openssl")
-			ENDIF()
-			EXECUTE_PROCESS(COMMAND ${OPENSSL_CMD} version -d OUTPUT_VARIABLE OPENSSL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
-			IF(OPENSSL_DIR)
-				STRING(REGEX REPLACE "^OPENSSLDIR: \"(.*)\"$" "\\1/" OPENSSL_DIR ${OPENSSL_DIR})
-
-				SET(OPENSSL_CA_LOCATIONS
-					"ca-bundle.pem"             # OpenSUSE Leap 42.1
-					"cert.pem"                  # Ubuntu 14.04, FreeBSD
-					"certs/ca-certificates.crt" # Ubuntu 16.04
-					"certs/ca.pem"              # Debian 7
-				)
-				FOREACH(SUFFIX IN LISTS OPENSSL_CA_LOCATIONS)
-					SET(LOC "${OPENSSL_DIR}${SUFFIX}")
-					IF(NOT CERT_LOCATION AND EXISTS "${OPENSSL_DIR}${SUFFIX}")
-						SET(CERT_LOCATION ${LOC})
-					ENDIF()
-				ENDFOREACH()
-			ELSE()
-				MESSAGE("Unable to find OpenSSL executable. Please provide default certificate location via CERT_LOCATION")
-			ENDIF()
-		ENDIF()
-
-		IF(CERT_LOCATION)
-			IF(NOT EXISTS ${CERT_LOCATION})
-				MESSAGE(FATAL_ERROR "Cannot use CERT_LOCATION=${CERT_LOCATION} as it doesn't exist")
-			ENDIF()
-			ADD_FEATURE_INFO(CERT_LOCATION ON "using certificates from ${CERT_LOCATION}")
-			ADD_DEFINITIONS(-DGIT_DEFAULT_CERT_LOCATION="${CERT_LOCATION}")
-		ENDIF()
-
-		SET(GIT_MBEDTLS 1)
-		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
-		LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
-		# mbedTLS has no pkgconfig file, hence we can't require it
-		# https://github.com/ARMmbed/mbedtls/issues/228
-		# For now, pass its link flags as our own
-		LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
-	ELSEIF (HTTPS_BACKEND STREQUAL "WinHTTP")
-		# WinHTTP setup was handled in the WinHTTP-specific block above
-	ELSE()
-		MESSAGE(FATAL_ERROR "Asked for backend ${HTTPS_BACKEND} but it wasn't found")
-	ENDIF()
-
-	ADD_FEATURE_INFO(HTTPS ON "using ${HTTPS_BACKEND}")
-	SET(GIT_HTTPS 1)
-ELSE()
-	ADD_FEATURE_INFO(HTTPS OFF "no support")
-ENDIF()
-
-# Specify sha1 implementation
-IF(SHA1_BACKEND STREQUAL "OpenSSL")
-	IF(NOT OPENSSL_FOUND)
-		FIND_PACKAGE(OpenSSL)
-		IF(NOT OPENSSL_FOUND)
-		    MESSAGE(FATAL_ERROR "Requested OpenSSL SHA1 backend, but OpenSSL could not be found")
-		ENDIF()
-	ENDIF()
-
-	ADD_FEATURE_INFO(SHA ON "using OpenSSL")
-	SET(GIT_SHA1_OPENSSL 1)
-	IF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
-		LIST(APPEND LIBGIT2_PC_LIBS "-lssl")
-	ELSE()
-		LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
-	ENDIF()
-ELSEIF(SHA1_BACKEND STREQUAL "CollisionDetection")
-	ADD_FEATURE_INFO(SHA ON "using CollisionDetection")
-	SET(GIT_SHA1_COLLISIONDETECT 1)
-	ADD_DEFINITIONS(-DSHA1DC_NO_STANDARD_INCLUDES=1)
-	ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\")
-	ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\")
-	FILE(GLOB SRC_SHA1 hash/hash_collisiondetect.c hash/sha1dc/*)
-ELSEIF(SHA1_BACKEND STREQUAL "Generic")
-	ADD_FEATURE_INFO(SHA ON "using Generic")
-	FILE(GLOB SRC_SHA1 hash/hash_generic.c)
-ELSEIF(SHA1_BACKEND STREQUAL "Win32")
-	ADD_FEATURE_INFO(SHA ON "using Win32")
-	SET(GIT_SHA1_WIN32 1)
-	FILE(GLOB SRC_SHA1 hash/hash_win32.c)
-ELSEIF(SHA1_BACKEND STREQUAL "CommonCrypto")
-	ADD_FEATURE_INFO(SHA ON "using CommonCrypto")
-	SET(GIT_SHA1_COMMON_CRYPTO 1)
-ELSEIF (SHA1_BACKEND STREQUAL "mbedTLS")
-	ADD_FEATURE_INFO(SHA ON "using mbedTLS")
-	SET(GIT_SHA1_MBEDTLS 1)
-	FILE(GLOB SRC_SHA1 hash/hash_mbedtls.c)
-	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
-	LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
-	# mbedTLS has no pkgconfig file, hence we can't require it
-	# https://github.com/ARMmbed/mbedtls/issues/228
-	# For now, pass its link flags as our own
-	LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
-ELSE()
-	MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend ${SHA1_BACKEND}")
-ENDIF()
+Include(SelectHTTPSBackend)
+Include(SelectHashes)
 
 # Specify regular expression implementation
 FIND_PACKAGE(PCRE)