1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
|
---|
9 | * Copyright (C) Marc Hoersken, <[email protected]>
|
---|
10 | * Copyright (C) Mark Salisbury, <[email protected]>
|
---|
11 | *
|
---|
12 | * This software is licensed as described in the file COPYING, which
|
---|
13 | * you should have received as part of this distribution. The terms
|
---|
14 | * are also available at https://curl.se/docs/copyright.html.
|
---|
15 | *
|
---|
16 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
---|
17 | * copies of the Software, and permit persons to whom the Software is
|
---|
18 | * furnished to do so, under the terms of the COPYING file.
|
---|
19 | *
|
---|
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
21 | * KIND, either express or implied.
|
---|
22 | *
|
---|
23 | * SPDX-License-Identifier: curl
|
---|
24 | *
|
---|
25 | ***************************************************************************/
|
---|
26 |
|
---|
27 | /*
|
---|
28 | * Source file for all Schannel-specific code for the TLS/SSL layer. No code
|
---|
29 | * but vtls.c should ever call or use these functions.
|
---|
30 | */
|
---|
31 |
|
---|
32 | #include "curl_setup.h"
|
---|
33 |
|
---|
34 | #ifdef USE_SCHANNEL
|
---|
35 |
|
---|
36 | #ifndef USE_WINDOWS_SSPI
|
---|
37 | # error "Can't compile SCHANNEL support without SSPI."
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #include "schannel.h"
|
---|
41 | #include "schannel_int.h"
|
---|
42 | #include "vtls.h"
|
---|
43 | #include "vtls_int.h"
|
---|
44 | #include "strcase.h"
|
---|
45 | #include "sendf.h"
|
---|
46 | #include "connect.h" /* for the connect timeout */
|
---|
47 | #include "strerror.h"
|
---|
48 | #include "select.h" /* for the socket readiness */
|
---|
49 | #include "inet_pton.h" /* for IP addr SNI check */
|
---|
50 | #include "curl_multibyte.h"
|
---|
51 | #include "warnless.h"
|
---|
52 | #include "x509asn1.h"
|
---|
53 | #include "curl_printf.h"
|
---|
54 | #include "multiif.h"
|
---|
55 | #include "version_win32.h"
|
---|
56 | #include "rand.h"
|
---|
57 |
|
---|
58 | /* The last #include file should be: */
|
---|
59 | #include "curl_memory.h"
|
---|
60 | #include "memdebug.h"
|
---|
61 |
|
---|
62 | /* ALPN requires version 8.1 of the Windows SDK, which was
|
---|
63 | shipped with Visual Studio 2013, aka _MSC_VER 1800:
|
---|
64 |
|
---|
65 | https://technet.microsoft.com/en-us/library/hh831771%28v=ws.11%29.aspx
|
---|
66 | */
|
---|
67 | #if defined(_MSC_VER) && (_MSC_VER >= 1800) && !defined(_USING_V110_SDK71_)
|
---|
68 | # define HAS_ALPN 1
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #ifndef BCRYPT_CHACHA20_POLY1305_ALGORITHM
|
---|
72 | #define BCRYPT_CHACHA20_POLY1305_ALGORITHM L"CHACHA20_POLY1305"
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | #ifndef BCRYPT_CHAIN_MODE_CCM
|
---|
76 | #define BCRYPT_CHAIN_MODE_CCM L"ChainingModeCCM"
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | #ifndef BCRYPT_CHAIN_MODE_GCM
|
---|
80 | #define BCRYPT_CHAIN_MODE_GCM L"ChainingModeGCM"
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | #ifndef BCRYPT_AES_ALGORITHM
|
---|
84 | #define BCRYPT_AES_ALGORITHM L"AES"
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #ifndef BCRYPT_SHA256_ALGORITHM
|
---|
88 | #define BCRYPT_SHA256_ALGORITHM L"SHA256"
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | #ifndef BCRYPT_SHA384_ALGORITHM
|
---|
92 | #define BCRYPT_SHA384_ALGORITHM L"SHA384"
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
96 | #ifdef UNICODE
|
---|
97 | #define CURL_CERT_STORE_PROV_SYSTEM CERT_STORE_PROV_SYSTEM_W
|
---|
98 | #else
|
---|
99 | #define CURL_CERT_STORE_PROV_SYSTEM CERT_STORE_PROV_SYSTEM_A
|
---|
100 | #endif
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | #ifndef SP_PROT_TLS1_0_CLIENT
|
---|
104 | #define SP_PROT_TLS1_0_CLIENT SP_PROT_TLS1_CLIENT
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | #ifndef SP_PROT_TLS1_1_CLIENT
|
---|
108 | #define SP_PROT_TLS1_1_CLIENT 0x00000200
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | #ifndef SP_PROT_TLS1_2_CLIENT
|
---|
112 | #define SP_PROT_TLS1_2_CLIENT 0x00000800
|
---|
113 | #endif
|
---|
114 |
|
---|
115 | #ifndef SP_PROT_TLS1_3_CLIENT
|
---|
116 | #define SP_PROT_TLS1_3_CLIENT 0x00002000
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | #ifndef SCH_USE_STRONG_CRYPTO
|
---|
120 | #define SCH_USE_STRONG_CRYPTO 0x00400000
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | #ifndef SECBUFFER_ALERT
|
---|
124 | #define SECBUFFER_ALERT 17
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | /* Both schannel buffer sizes must be > 0 */
|
---|
128 | #define CURL_SCHANNEL_BUFFER_INIT_SIZE 4096
|
---|
129 | #define CURL_SCHANNEL_BUFFER_FREE_SIZE 1024
|
---|
130 |
|
---|
131 | #define CERT_THUMBPRINT_STR_LEN 40
|
---|
132 | #define CERT_THUMBPRINT_DATA_LEN 20
|
---|
133 |
|
---|
134 | /* Uncomment to force verbose output
|
---|
135 | * #define infof(x, y, ...) printf(y, __VA_ARGS__)
|
---|
136 | * #define failf(x, y, ...) printf(y, __VA_ARGS__)
|
---|
137 | */
|
---|
138 |
|
---|
139 | #ifndef CALG_SHA_256
|
---|
140 | # define CALG_SHA_256 0x0000800c
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | #ifndef PKCS12_NO_PERSIST_KEY
|
---|
144 | #define PKCS12_NO_PERSIST_KEY 0x00008000
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | static CURLcode schannel_pkp_pin_peer_pubkey(struct Curl_cfilter *cf,
|
---|
148 | struct Curl_easy *data,
|
---|
149 | const char *pinnedpubkey);
|
---|
150 |
|
---|
151 | static void InitSecBuffer(SecBuffer *buffer, unsigned long BufType,
|
---|
152 | void *BufDataPtr, unsigned long BufByteSize)
|
---|
153 | {
|
---|
154 | buffer->cbBuffer = BufByteSize;
|
---|
155 | buffer->BufferType = BufType;
|
---|
156 | buffer->pvBuffer = BufDataPtr;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static void InitSecBufferDesc(SecBufferDesc *desc, SecBuffer *BufArr,
|
---|
160 | unsigned long NumArrElem)
|
---|
161 | {
|
---|
162 | desc->ulVersion = SECBUFFER_VERSION;
|
---|
163 | desc->pBuffers = BufArr;
|
---|
164 | desc->cBuffers = NumArrElem;
|
---|
165 | }
|
---|
166 |
|
---|
167 | static CURLcode
|
---|
168 | schannel_set_ssl_version_min_max(DWORD *enabled_protocols,
|
---|
169 | struct Curl_cfilter *cf,
|
---|
170 | struct Curl_easy *data)
|
---|
171 | {
|
---|
172 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
173 | long ssl_version = conn_config->version;
|
---|
174 | long ssl_version_max = conn_config->version_max;
|
---|
175 | long i = ssl_version;
|
---|
176 |
|
---|
177 | switch(ssl_version_max) {
|
---|
178 | case CURL_SSLVERSION_MAX_NONE:
|
---|
179 | case CURL_SSLVERSION_MAX_DEFAULT:
|
---|
180 |
|
---|
181 | /* Windows Server 2022 and newer (including Windows 11) support TLS 1.3
|
---|
182 | built-in. Previous builds of Windows 10 had broken TLS 1.3
|
---|
183 | implementations that could be enabled via registry.
|
---|
184 | */
|
---|
185 | if(curlx_verify_windows_version(10, 0, 20348, PLATFORM_WINNT,
|
---|
186 | VERSION_GREATER_THAN_EQUAL)) {
|
---|
187 | ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_3;
|
---|
188 | }
|
---|
189 | else /* Windows 10 and older */
|
---|
190 | ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2;
|
---|
191 |
|
---|
192 | break;
|
---|
193 | }
|
---|
194 |
|
---|
195 | for(; i <= (ssl_version_max >> 16); ++i) {
|
---|
196 | switch(i) {
|
---|
197 | case CURL_SSLVERSION_TLSv1_0:
|
---|
198 | (*enabled_protocols) |= SP_PROT_TLS1_0_CLIENT;
|
---|
199 | break;
|
---|
200 | case CURL_SSLVERSION_TLSv1_1:
|
---|
201 | (*enabled_protocols) |= SP_PROT_TLS1_1_CLIENT;
|
---|
202 | break;
|
---|
203 | case CURL_SSLVERSION_TLSv1_2:
|
---|
204 | (*enabled_protocols) |= SP_PROT_TLS1_2_CLIENT;
|
---|
205 | break;
|
---|
206 | case CURL_SSLVERSION_TLSv1_3:
|
---|
207 |
|
---|
208 | /* Windows Server 2022 and newer */
|
---|
209 | if(curlx_verify_windows_version(10, 0, 20348, PLATFORM_WINNT,
|
---|
210 | VERSION_GREATER_THAN_EQUAL)) {
|
---|
211 | (*enabled_protocols) |= SP_PROT_TLS1_3_CLIENT;
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | else { /* Windows 10 and older */
|
---|
215 | failf(data, "schannel: TLS 1.3 not supported on Windows prior to 11");
|
---|
216 | return CURLE_SSL_CONNECT_ERROR;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 | return CURLE_OK;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /* longest is 26, buffer is slightly bigger */
|
---|
224 | #define LONGEST_ALG_ID 32
|
---|
225 | #define CIPHEROPTION(x) {#x, x}
|
---|
226 |
|
---|
227 | struct algo {
|
---|
228 | const char *name;
|
---|
229 | int id;
|
---|
230 | };
|
---|
231 |
|
---|
232 | static const struct algo algs[]= {
|
---|
233 | CIPHEROPTION(CALG_MD2),
|
---|
234 | CIPHEROPTION(CALG_MD4),
|
---|
235 | CIPHEROPTION(CALG_MD5),
|
---|
236 | CIPHEROPTION(CALG_SHA),
|
---|
237 | CIPHEROPTION(CALG_SHA1),
|
---|
238 | CIPHEROPTION(CALG_MAC),
|
---|
239 | CIPHEROPTION(CALG_RSA_SIGN),
|
---|
240 | CIPHEROPTION(CALG_DSS_SIGN),
|
---|
241 | /* ifdefs for the options that are defined conditionally in wincrypt.h */
|
---|
242 | #ifdef CALG_NO_SIGN
|
---|
243 | CIPHEROPTION(CALG_NO_SIGN),
|
---|
244 | #endif
|
---|
245 | CIPHEROPTION(CALG_RSA_KEYX),
|
---|
246 | CIPHEROPTION(CALG_DES),
|
---|
247 | #ifdef CALG_3DES_112
|
---|
248 | CIPHEROPTION(CALG_3DES_112),
|
---|
249 | #endif
|
---|
250 | CIPHEROPTION(CALG_3DES),
|
---|
251 | CIPHEROPTION(CALG_DESX),
|
---|
252 | CIPHEROPTION(CALG_RC2),
|
---|
253 | CIPHEROPTION(CALG_RC4),
|
---|
254 | CIPHEROPTION(CALG_SEAL),
|
---|
255 | #ifdef CALG_DH_SF
|
---|
256 | CIPHEROPTION(CALG_DH_SF),
|
---|
257 | #endif
|
---|
258 | CIPHEROPTION(CALG_DH_EPHEM),
|
---|
259 | #ifdef CALG_AGREEDKEY_ANY
|
---|
260 | CIPHEROPTION(CALG_AGREEDKEY_ANY),
|
---|
261 | #endif
|
---|
262 | #ifdef CALG_HUGHES_MD5
|
---|
263 | CIPHEROPTION(CALG_HUGHES_MD5),
|
---|
264 | #endif
|
---|
265 | CIPHEROPTION(CALG_SKIPJACK),
|
---|
266 | #ifdef CALG_TEK
|
---|
267 | CIPHEROPTION(CALG_TEK),
|
---|
268 | #endif
|
---|
269 | CIPHEROPTION(CALG_CYLINK_MEK),
|
---|
270 | CIPHEROPTION(CALG_SSL3_SHAMD5),
|
---|
271 | #ifdef CALG_SSL3_MASTER
|
---|
272 | CIPHEROPTION(CALG_SSL3_MASTER),
|
---|
273 | #endif
|
---|
274 | #ifdef CALG_SCHANNEL_MASTER_HASH
|
---|
275 | CIPHEROPTION(CALG_SCHANNEL_MASTER_HASH),
|
---|
276 | #endif
|
---|
277 | #ifdef CALG_SCHANNEL_MAC_KEY
|
---|
278 | CIPHEROPTION(CALG_SCHANNEL_MAC_KEY),
|
---|
279 | #endif
|
---|
280 | #ifdef CALG_SCHANNEL_ENC_KEY
|
---|
281 | CIPHEROPTION(CALG_SCHANNEL_ENC_KEY),
|
---|
282 | #endif
|
---|
283 | #ifdef CALG_PCT1_MASTER
|
---|
284 | CIPHEROPTION(CALG_PCT1_MASTER),
|
---|
285 | #endif
|
---|
286 | #ifdef CALG_SSL2_MASTER
|
---|
287 | CIPHEROPTION(CALG_SSL2_MASTER),
|
---|
288 | #endif
|
---|
289 | #ifdef CALG_TLS1_MASTER
|
---|
290 | CIPHEROPTION(CALG_TLS1_MASTER),
|
---|
291 | #endif
|
---|
292 | #ifdef CALG_RC5
|
---|
293 | CIPHEROPTION(CALG_RC5),
|
---|
294 | #endif
|
---|
295 | #ifdef CALG_HMAC
|
---|
296 | CIPHEROPTION(CALG_HMAC),
|
---|
297 | #endif
|
---|
298 | #ifdef CALG_TLS1PRF
|
---|
299 | CIPHEROPTION(CALG_TLS1PRF),
|
---|
300 | #endif
|
---|
301 | #ifdef CALG_HASH_REPLACE_OWF
|
---|
302 | CIPHEROPTION(CALG_HASH_REPLACE_OWF),
|
---|
303 | #endif
|
---|
304 | #ifdef CALG_AES_128
|
---|
305 | CIPHEROPTION(CALG_AES_128),
|
---|
306 | #endif
|
---|
307 | #ifdef CALG_AES_192
|
---|
308 | CIPHEROPTION(CALG_AES_192),
|
---|
309 | #endif
|
---|
310 | #ifdef CALG_AES_256
|
---|
311 | CIPHEROPTION(CALG_AES_256),
|
---|
312 | #endif
|
---|
313 | #ifdef CALG_AES
|
---|
314 | CIPHEROPTION(CALG_AES),
|
---|
315 | #endif
|
---|
316 | #ifdef CALG_SHA_256
|
---|
317 | CIPHEROPTION(CALG_SHA_256),
|
---|
318 | #endif
|
---|
319 | #ifdef CALG_SHA_384
|
---|
320 | CIPHEROPTION(CALG_SHA_384),
|
---|
321 | #endif
|
---|
322 | #ifdef CALG_SHA_512
|
---|
323 | CIPHEROPTION(CALG_SHA_512),
|
---|
324 | #endif
|
---|
325 | #ifdef CALG_ECDH
|
---|
326 | CIPHEROPTION(CALG_ECDH),
|
---|
327 | #endif
|
---|
328 | #ifdef CALG_ECMQV
|
---|
329 | CIPHEROPTION(CALG_ECMQV),
|
---|
330 | #endif
|
---|
331 | #ifdef CALG_ECDSA
|
---|
332 | CIPHEROPTION(CALG_ECDSA),
|
---|
333 | #endif
|
---|
334 | #ifdef CALG_ECDH_EPHEM
|
---|
335 | CIPHEROPTION(CALG_ECDH_EPHEM),
|
---|
336 | #endif
|
---|
337 | {NULL, 0},
|
---|
338 | };
|
---|
339 |
|
---|
340 | static int
|
---|
341 | get_alg_id_by_name(char *name)
|
---|
342 | {
|
---|
343 | char *nameEnd = strchr(name, ':');
|
---|
344 | size_t n = nameEnd ? (size_t)(nameEnd - name) : strlen(name);
|
---|
345 | int i;
|
---|
346 |
|
---|
347 | for(i = 0; algs[i].name; i++) {
|
---|
348 | if((n == strlen(algs[i].name) && !strncmp(algs[i].name, name, n)))
|
---|
349 | return algs[i].id;
|
---|
350 | }
|
---|
351 | return 0; /* not found */
|
---|
352 | }
|
---|
353 |
|
---|
354 | #define NUM_CIPHERS 47 /* There are 47 options listed above */
|
---|
355 |
|
---|
356 | static CURLcode
|
---|
357 | set_ssl_ciphers(SCHANNEL_CRED *schannel_cred, char *ciphers,
|
---|
358 | ALG_ID *algIds)
|
---|
359 | {
|
---|
360 | char *startCur = ciphers;
|
---|
361 | int algCount = 0;
|
---|
362 | while(startCur && (0 != *startCur) && (algCount < NUM_CIPHERS)) {
|
---|
363 | long alg = strtol(startCur, 0, 0);
|
---|
364 | if(!alg)
|
---|
365 | alg = get_alg_id_by_name(startCur);
|
---|
366 | if(alg)
|
---|
367 | algIds[algCount++] = alg;
|
---|
368 | else if(!strncmp(startCur, "USE_STRONG_CRYPTO",
|
---|
369 | sizeof("USE_STRONG_CRYPTO") - 1) ||
|
---|
370 | !strncmp(startCur, "SCH_USE_STRONG_CRYPTO",
|
---|
371 | sizeof("SCH_USE_STRONG_CRYPTO") - 1))
|
---|
372 | schannel_cred->dwFlags |= SCH_USE_STRONG_CRYPTO;
|
---|
373 | else
|
---|
374 | return CURLE_SSL_CIPHER;
|
---|
375 | startCur = strchr(startCur, ':');
|
---|
376 | if(startCur)
|
---|
377 | startCur++;
|
---|
378 | }
|
---|
379 | schannel_cred->palgSupportedAlgs = algIds;
|
---|
380 | schannel_cred->cSupportedAlgs = algCount;
|
---|
381 | return CURLE_OK;
|
---|
382 | }
|
---|
383 |
|
---|
384 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
385 |
|
---|
386 | /* Function allocates memory for store_path only if CURLE_OK is returned */
|
---|
387 | static CURLcode
|
---|
388 | get_cert_location(TCHAR *path, DWORD *store_name, TCHAR **store_path,
|
---|
389 | TCHAR **thumbprint)
|
---|
390 | {
|
---|
391 | TCHAR *sep;
|
---|
392 | TCHAR *store_path_start;
|
---|
393 | size_t store_name_len;
|
---|
394 |
|
---|
395 | sep = _tcschr(path, TEXT('\\'));
|
---|
396 | if(!sep)
|
---|
397 | return CURLE_SSL_CERTPROBLEM;
|
---|
398 |
|
---|
399 | store_name_len = sep - path;
|
---|
400 |
|
---|
401 | if(_tcsncmp(path, TEXT("CurrentUser"), store_name_len) == 0)
|
---|
402 | *store_name = CERT_SYSTEM_STORE_CURRENT_USER;
|
---|
403 | else if(_tcsncmp(path, TEXT("LocalMachine"), store_name_len) == 0)
|
---|
404 | *store_name = CERT_SYSTEM_STORE_LOCAL_MACHINE;
|
---|
405 | else if(_tcsncmp(path, TEXT("CurrentService"), store_name_len) == 0)
|
---|
406 | *store_name = CERT_SYSTEM_STORE_CURRENT_SERVICE;
|
---|
407 | else if(_tcsncmp(path, TEXT("Services"), store_name_len) == 0)
|
---|
408 | *store_name = CERT_SYSTEM_STORE_SERVICES;
|
---|
409 | else if(_tcsncmp(path, TEXT("Users"), store_name_len) == 0)
|
---|
410 | *store_name = CERT_SYSTEM_STORE_USERS;
|
---|
411 | else if(_tcsncmp(path, TEXT("CurrentUserGroupPolicy"),
|
---|
412 | store_name_len) == 0)
|
---|
413 | *store_name = CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY;
|
---|
414 | else if(_tcsncmp(path, TEXT("LocalMachineGroupPolicy"),
|
---|
415 | store_name_len) == 0)
|
---|
416 | *store_name = CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY;
|
---|
417 | else if(_tcsncmp(path, TEXT("LocalMachineEnterprise"),
|
---|
418 | store_name_len) == 0)
|
---|
419 | *store_name = CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE;
|
---|
420 | else
|
---|
421 | return CURLE_SSL_CERTPROBLEM;
|
---|
422 |
|
---|
423 | store_path_start = sep + 1;
|
---|
424 |
|
---|
425 | sep = _tcschr(store_path_start, TEXT('\\'));
|
---|
426 | if(!sep)
|
---|
427 | return CURLE_SSL_CERTPROBLEM;
|
---|
428 |
|
---|
429 | *thumbprint = sep + 1;
|
---|
430 | if(_tcslen(*thumbprint) != CERT_THUMBPRINT_STR_LEN)
|
---|
431 | return CURLE_SSL_CERTPROBLEM;
|
---|
432 |
|
---|
433 | *sep = TEXT('\0');
|
---|
434 | *store_path = _tcsdup(store_path_start);
|
---|
435 | *sep = TEXT('\\');
|
---|
436 | if(!*store_path)
|
---|
437 | return CURLE_OUT_OF_MEMORY;
|
---|
438 |
|
---|
439 | return CURLE_OK;
|
---|
440 | }
|
---|
441 | #endif
|
---|
442 |
|
---|
443 | static bool algo(const char *check, char *namep, size_t nlen)
|
---|
444 | {
|
---|
445 | return (strlen(check) == nlen) && !strncmp(check, namep, nlen);
|
---|
446 | }
|
---|
447 |
|
---|
448 | static CURLcode
|
---|
449 | schannel_acquire_credential_handle(struct Curl_cfilter *cf,
|
---|
450 | struct Curl_easy *data)
|
---|
451 | {
|
---|
452 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
453 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
454 | struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
|
---|
455 |
|
---|
456 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
457 | PCCERT_CONTEXT client_certs[1] = { NULL };
|
---|
458 | HCERTSTORE client_cert_store = NULL;
|
---|
459 | #endif
|
---|
460 | SECURITY_STATUS sspi_status = SEC_E_OK;
|
---|
461 | CURLcode result;
|
---|
462 |
|
---|
463 | /* setup Schannel API options */
|
---|
464 | DWORD flags = 0;
|
---|
465 | DWORD enabled_protocols = 0;
|
---|
466 |
|
---|
467 | struct schannel_ssl_backend_data *backend =
|
---|
468 | (struct schannel_ssl_backend_data *)(connssl->backend);
|
---|
469 |
|
---|
470 | DEBUGASSERT(backend);
|
---|
471 |
|
---|
472 | if(conn_config->verifypeer) {
|
---|
473 | #ifdef HAS_MANUAL_VERIFY_API
|
---|
474 | if(backend->use_manual_cred_validation)
|
---|
475 | flags = SCH_CRED_MANUAL_CRED_VALIDATION;
|
---|
476 | else
|
---|
477 | #endif
|
---|
478 | flags = SCH_CRED_AUTO_CRED_VALIDATION;
|
---|
479 |
|
---|
480 | if(ssl_config->no_revoke) {
|
---|
481 | flags |= SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
|
---|
482 | SCH_CRED_IGNORE_REVOCATION_OFFLINE;
|
---|
483 |
|
---|
484 | DEBUGF(infof(data, "schannel: disabled server certificate revocation "
|
---|
485 | "checks"));
|
---|
486 | }
|
---|
487 | else if(ssl_config->revoke_best_effort) {
|
---|
488 | flags |= SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
|
---|
489 | SCH_CRED_IGNORE_REVOCATION_OFFLINE | SCH_CRED_REVOCATION_CHECK_CHAIN;
|
---|
490 |
|
---|
491 | DEBUGF(infof(data, "schannel: ignore revocation offline errors"));
|
---|
492 | }
|
---|
493 | else {
|
---|
494 | flags |= SCH_CRED_REVOCATION_CHECK_CHAIN;
|
---|
495 |
|
---|
496 | DEBUGF(infof(data,
|
---|
497 | "schannel: checking server certificate revocation"));
|
---|
498 | }
|
---|
499 | }
|
---|
500 | else {
|
---|
501 | flags = SCH_CRED_MANUAL_CRED_VALIDATION |
|
---|
502 | SCH_CRED_IGNORE_NO_REVOCATION_CHECK |
|
---|
503 | SCH_CRED_IGNORE_REVOCATION_OFFLINE;
|
---|
504 | DEBUGF(infof(data,
|
---|
505 | "schannel: disabled server cert revocation checks"));
|
---|
506 | }
|
---|
507 |
|
---|
508 | if(!conn_config->verifyhost) {
|
---|
509 | flags |= SCH_CRED_NO_SERVERNAME_CHECK;
|
---|
510 | DEBUGF(infof(data, "schannel: verifyhost setting prevents Schannel from "
|
---|
511 | "comparing the supplied target name with the subject "
|
---|
512 | "names in server certificates."));
|
---|
513 | }
|
---|
514 |
|
---|
515 | if(!ssl_config->auto_client_cert) {
|
---|
516 | flags &= ~SCH_CRED_USE_DEFAULT_CREDS;
|
---|
517 | flags |= SCH_CRED_NO_DEFAULT_CREDS;
|
---|
518 | infof(data, "schannel: disabled automatic use of client certificate");
|
---|
519 | }
|
---|
520 | else
|
---|
521 | infof(data, "schannel: enabled automatic use of client certificate");
|
---|
522 |
|
---|
523 | switch(conn_config->version) {
|
---|
524 | case CURL_SSLVERSION_DEFAULT:
|
---|
525 | case CURL_SSLVERSION_TLSv1:
|
---|
526 | case CURL_SSLVERSION_TLSv1_0:
|
---|
527 | case CURL_SSLVERSION_TLSv1_1:
|
---|
528 | case CURL_SSLVERSION_TLSv1_2:
|
---|
529 | case CURL_SSLVERSION_TLSv1_3:
|
---|
530 | {
|
---|
531 | result = schannel_set_ssl_version_min_max(&enabled_protocols, cf, data);
|
---|
532 | if(result != CURLE_OK)
|
---|
533 | return result;
|
---|
534 | break;
|
---|
535 | }
|
---|
536 | case CURL_SSLVERSION_SSLv3:
|
---|
537 | case CURL_SSLVERSION_SSLv2:
|
---|
538 | failf(data, "SSL versions not supported");
|
---|
539 | return CURLE_NOT_BUILT_IN;
|
---|
540 | default:
|
---|
541 | failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
|
---|
542 | return CURLE_SSL_CONNECT_ERROR;
|
---|
543 | }
|
---|
544 |
|
---|
545 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
546 | /* client certificate */
|
---|
547 | if(data->set.ssl.primary.clientcert || data->set.ssl.primary.cert_blob) {
|
---|
548 | DWORD cert_store_name = 0;
|
---|
549 | TCHAR *cert_store_path = NULL;
|
---|
550 | TCHAR *cert_thumbprint_str = NULL;
|
---|
551 | CRYPT_HASH_BLOB cert_thumbprint;
|
---|
552 | BYTE cert_thumbprint_data[CERT_THUMBPRINT_DATA_LEN];
|
---|
553 | HCERTSTORE cert_store = NULL;
|
---|
554 | FILE *fInCert = NULL;
|
---|
555 | void *certdata = NULL;
|
---|
556 | size_t certsize = 0;
|
---|
557 | bool blob = data->set.ssl.primary.cert_blob != NULL;
|
---|
558 | TCHAR *cert_path = NULL;
|
---|
559 | if(blob) {
|
---|
560 | certdata = data->set.ssl.primary.cert_blob->data;
|
---|
561 | certsize = data->set.ssl.primary.cert_blob->len;
|
---|
562 | }
|
---|
563 | else {
|
---|
564 | cert_path = curlx_convert_UTF8_to_tchar(
|
---|
565 | data->set.ssl.primary.clientcert);
|
---|
566 | if(!cert_path)
|
---|
567 | return CURLE_OUT_OF_MEMORY;
|
---|
568 |
|
---|
569 | result = get_cert_location(cert_path, &cert_store_name,
|
---|
570 | &cert_store_path, &cert_thumbprint_str);
|
---|
571 |
|
---|
572 | if(result && (data->set.ssl.primary.clientcert[0]!='\0'))
|
---|
573 | fInCert = fopen(data->set.ssl.primary.clientcert, "rb");
|
---|
574 |
|
---|
575 | if(result && !fInCert) {
|
---|
576 | failf(data, "schannel: Failed to get certificate location"
|
---|
577 | " or file for %s",
|
---|
578 | data->set.ssl.primary.clientcert);
|
---|
579 | curlx_unicodefree(cert_path);
|
---|
580 | return result;
|
---|
581 | }
|
---|
582 | }
|
---|
583 |
|
---|
584 | if((fInCert || blob) && (data->set.ssl.cert_type) &&
|
---|
585 | (!strcasecompare(data->set.ssl.cert_type, "P12"))) {
|
---|
586 | failf(data, "schannel: certificate format compatibility error "
|
---|
587 | " for %s",
|
---|
588 | blob ? "(memory blob)" : data->set.ssl.primary.clientcert);
|
---|
589 | curlx_unicodefree(cert_path);
|
---|
590 | return CURLE_SSL_CERTPROBLEM;
|
---|
591 | }
|
---|
592 |
|
---|
593 | if(fInCert || blob) {
|
---|
594 | /* Reading a .P12 or .pfx file, like the example at bottom of
|
---|
595 | https://social.msdn.microsoft.com/Forums/windowsdesktop/
|
---|
596 | en-US/3e7bc95f-b21a-4bcd-bd2c-7f996718cae5
|
---|
597 | */
|
---|
598 | CRYPT_DATA_BLOB datablob;
|
---|
599 | WCHAR* pszPassword;
|
---|
600 | size_t pwd_len = 0;
|
---|
601 | int str_w_len = 0;
|
---|
602 | const char *cert_showfilename_error = blob ?
|
---|
603 | "(memory blob)" : data->set.ssl.primary.clientcert;
|
---|
604 | curlx_unicodefree(cert_path);
|
---|
605 | if(fInCert) {
|
---|
606 | long cert_tell = 0;
|
---|
607 | bool continue_reading = fseek(fInCert, 0, SEEK_END) == 0;
|
---|
608 | if(continue_reading)
|
---|
609 | cert_tell = ftell(fInCert);
|
---|
610 | if(cert_tell < 0)
|
---|
611 | continue_reading = FALSE;
|
---|
612 | else
|
---|
613 | certsize = (size_t)cert_tell;
|
---|
614 | if(continue_reading)
|
---|
615 | continue_reading = fseek(fInCert, 0, SEEK_SET) == 0;
|
---|
616 | if(continue_reading)
|
---|
617 | certdata = malloc(certsize + 1);
|
---|
618 | if((!certdata) ||
|
---|
619 | ((int) fread(certdata, certsize, 1, fInCert) != 1))
|
---|
620 | continue_reading = FALSE;
|
---|
621 | fclose(fInCert);
|
---|
622 | if(!continue_reading) {
|
---|
623 | failf(data, "schannel: Failed to read cert file %s",
|
---|
624 | data->set.ssl.primary.clientcert);
|
---|
625 | free(certdata);
|
---|
626 | return CURLE_SSL_CERTPROBLEM;
|
---|
627 | }
|
---|
628 | }
|
---|
629 |
|
---|
630 | /* Convert key-pair data to the in-memory certificate store */
|
---|
631 | datablob.pbData = (BYTE*)certdata;
|
---|
632 | datablob.cbData = (DWORD)certsize;
|
---|
633 |
|
---|
634 | if(data->set.ssl.key_passwd)
|
---|
635 | pwd_len = strlen(data->set.ssl.key_passwd);
|
---|
636 | pszPassword = (WCHAR*)malloc(sizeof(WCHAR)*(pwd_len + 1));
|
---|
637 | if(pszPassword) {
|
---|
638 | if(pwd_len > 0)
|
---|
639 | str_w_len = MultiByteToWideChar(CP_UTF8,
|
---|
640 | MB_ERR_INVALID_CHARS,
|
---|
641 | data->set.ssl.key_passwd,
|
---|
642 | (int)pwd_len,
|
---|
643 | pszPassword, (int)(pwd_len + 1));
|
---|
644 |
|
---|
645 | if((str_w_len >= 0) && (str_w_len <= (int)pwd_len))
|
---|
646 | pszPassword[str_w_len] = 0;
|
---|
647 | else
|
---|
648 | pszPassword[0] = 0;
|
---|
649 |
|
---|
650 | if(curlx_verify_windows_version(6, 0, 0, PLATFORM_WINNT,
|
---|
651 | VERSION_GREATER_THAN_EQUAL))
|
---|
652 | cert_store = PFXImportCertStore(&datablob, pszPassword,
|
---|
653 | PKCS12_NO_PERSIST_KEY);
|
---|
654 | else
|
---|
655 | cert_store = PFXImportCertStore(&datablob, pszPassword, 0);
|
---|
656 |
|
---|
657 | free(pszPassword);
|
---|
658 | }
|
---|
659 | if(!blob)
|
---|
660 | free(certdata);
|
---|
661 | if(!cert_store) {
|
---|
662 | DWORD errorcode = GetLastError();
|
---|
663 | if(errorcode == ERROR_INVALID_PASSWORD)
|
---|
664 | failf(data, "schannel: Failed to import cert file %s, "
|
---|
665 | "password is bad",
|
---|
666 | cert_showfilename_error);
|
---|
667 | else
|
---|
668 | failf(data, "schannel: Failed to import cert file %s, "
|
---|
669 | "last error is 0x%lx",
|
---|
670 | cert_showfilename_error, errorcode);
|
---|
671 | return CURLE_SSL_CERTPROBLEM;
|
---|
672 | }
|
---|
673 |
|
---|
674 | client_certs[0] = CertFindCertificateInStore(
|
---|
675 | cert_store, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0,
|
---|
676 | CERT_FIND_ANY, NULL, NULL);
|
---|
677 |
|
---|
678 | if(!client_certs[0]) {
|
---|
679 | failf(data, "schannel: Failed to get certificate from file %s"
|
---|
680 | ", last error is 0x%lx",
|
---|
681 | cert_showfilename_error, GetLastError());
|
---|
682 | CertCloseStore(cert_store, 0);
|
---|
683 | return CURLE_SSL_CERTPROBLEM;
|
---|
684 | }
|
---|
685 | }
|
---|
686 | else {
|
---|
687 | cert_store =
|
---|
688 | CertOpenStore(CURL_CERT_STORE_PROV_SYSTEM, 0,
|
---|
689 | (HCRYPTPROV)NULL,
|
---|
690 | CERT_STORE_OPEN_EXISTING_FLAG | cert_store_name,
|
---|
691 | cert_store_path);
|
---|
692 | if(!cert_store) {
|
---|
693 | char *path_utf8 =
|
---|
694 | curlx_convert_tchar_to_UTF8(cert_store_path);
|
---|
695 | failf(data, "schannel: Failed to open cert store %lx %s, "
|
---|
696 | "last error is 0x%lx",
|
---|
697 | cert_store_name,
|
---|
698 | (path_utf8 ? path_utf8 : "(unknown)"),
|
---|
699 | GetLastError());
|
---|
700 | free(cert_store_path);
|
---|
701 | curlx_unicodefree(path_utf8);
|
---|
702 | curlx_unicodefree(cert_path);
|
---|
703 | return CURLE_SSL_CERTPROBLEM;
|
---|
704 | }
|
---|
705 | free(cert_store_path);
|
---|
706 |
|
---|
707 | cert_thumbprint.pbData = cert_thumbprint_data;
|
---|
708 | cert_thumbprint.cbData = CERT_THUMBPRINT_DATA_LEN;
|
---|
709 |
|
---|
710 | if(!CryptStringToBinary(cert_thumbprint_str,
|
---|
711 | CERT_THUMBPRINT_STR_LEN,
|
---|
712 | CRYPT_STRING_HEX,
|
---|
713 | cert_thumbprint_data,
|
---|
714 | &cert_thumbprint.cbData,
|
---|
715 | NULL, NULL)) {
|
---|
716 | curlx_unicodefree(cert_path);
|
---|
717 | CertCloseStore(cert_store, 0);
|
---|
718 | return CURLE_SSL_CERTPROBLEM;
|
---|
719 | }
|
---|
720 |
|
---|
721 | client_certs[0] = CertFindCertificateInStore(
|
---|
722 | cert_store, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0,
|
---|
723 | CERT_FIND_HASH, &cert_thumbprint, NULL);
|
---|
724 |
|
---|
725 | curlx_unicodefree(cert_path);
|
---|
726 |
|
---|
727 | if(!client_certs[0]) {
|
---|
728 | /* CRYPT_E_NOT_FOUND / E_INVALIDARG */
|
---|
729 | CertCloseStore(cert_store, 0);
|
---|
730 | return CURLE_SSL_CERTPROBLEM;
|
---|
731 | }
|
---|
732 | }
|
---|
733 | client_cert_store = cert_store;
|
---|
734 | }
|
---|
735 | #else
|
---|
736 | if(data->set.ssl.primary.clientcert || data->set.ssl.primary.cert_blob) {
|
---|
737 | failf(data, "schannel: client cert support not built in");
|
---|
738 | return CURLE_NOT_BUILT_IN;
|
---|
739 | }
|
---|
740 | #endif
|
---|
741 |
|
---|
742 | /* allocate memory for the reusable credential handle */
|
---|
743 | backend->cred = (struct Curl_schannel_cred *)
|
---|
744 | calloc(1, sizeof(struct Curl_schannel_cred));
|
---|
745 | if(!backend->cred) {
|
---|
746 | failf(data, "schannel: unable to allocate memory");
|
---|
747 |
|
---|
748 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
749 | if(client_certs[0])
|
---|
750 | CertFreeCertificateContext(client_certs[0]);
|
---|
751 | if(client_cert_store)
|
---|
752 | CertCloseStore(client_cert_store, 0);
|
---|
753 | #endif
|
---|
754 |
|
---|
755 | return CURLE_OUT_OF_MEMORY;
|
---|
756 | }
|
---|
757 | backend->cred->refcount = 1;
|
---|
758 |
|
---|
759 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
760 | /* Since we did not persist the key, we need to extend the store's
|
---|
761 | * lifetime until the end of the connection
|
---|
762 | */
|
---|
763 | backend->cred->client_cert_store = client_cert_store;
|
---|
764 | #endif
|
---|
765 |
|
---|
766 | /* We support TLS 1.3 starting in Windows 10 version 1809 (OS build 17763) as
|
---|
767 | long as the user did not set a legacy algorithm list
|
---|
768 | (CURLOPT_SSL_CIPHER_LIST). */
|
---|
769 | if(!conn_config->cipher_list &&
|
---|
770 | curlx_verify_windows_version(10, 0, 17763, PLATFORM_WINNT,
|
---|
771 | VERSION_GREATER_THAN_EQUAL)) {
|
---|
772 |
|
---|
773 | char *ciphers13 = 0;
|
---|
774 |
|
---|
775 | bool disable_aes_gcm_sha384 = FALSE;
|
---|
776 | bool disable_aes_gcm_sha256 = FALSE;
|
---|
777 | bool disable_chacha_poly = FALSE;
|
---|
778 | bool disable_aes_ccm_8_sha256 = FALSE;
|
---|
779 | bool disable_aes_ccm_sha256 = FALSE;
|
---|
780 |
|
---|
781 | SCH_CREDENTIALS credentials = { 0 };
|
---|
782 | TLS_PARAMETERS tls_parameters = { 0 };
|
---|
783 | CRYPTO_SETTINGS crypto_settings[4] = { { 0 } };
|
---|
784 | UNICODE_STRING blocked_ccm_modes[1] = { { 0 } };
|
---|
785 | UNICODE_STRING blocked_gcm_modes[1] = { { 0 } };
|
---|
786 |
|
---|
787 | int crypto_settings_idx = 0;
|
---|
788 |
|
---|
789 |
|
---|
790 | /* If TLS 1.3 ciphers are explicitly listed, then
|
---|
791 | * disable all the ciphers and re-enable which
|
---|
792 | * ciphers the user has provided.
|
---|
793 | */
|
---|
794 | ciphers13 = conn_config->cipher_list13;
|
---|
795 | if(ciphers13) {
|
---|
796 | const int remaining_ciphers = 5;
|
---|
797 |
|
---|
798 | /* detect which remaining ciphers to enable
|
---|
799 | and then disable everything else.
|
---|
800 | */
|
---|
801 |
|
---|
802 | char *startCur = ciphers13;
|
---|
803 | int algCount = 0;
|
---|
804 | char *nameEnd;
|
---|
805 |
|
---|
806 | disable_aes_gcm_sha384 = TRUE;
|
---|
807 | disable_aes_gcm_sha256 = TRUE;
|
---|
808 | disable_chacha_poly = TRUE;
|
---|
809 | disable_aes_ccm_8_sha256 = TRUE;
|
---|
810 | disable_aes_ccm_sha256 = TRUE;
|
---|
811 |
|
---|
812 | while(startCur && (0 != *startCur) && (algCount < remaining_ciphers)) {
|
---|
813 | size_t n;
|
---|
814 | char *namep;
|
---|
815 | nameEnd = strchr(startCur, ':');
|
---|
816 | n = nameEnd ? (size_t)(nameEnd - startCur) : strlen(startCur);
|
---|
817 | namep = startCur;
|
---|
818 |
|
---|
819 | if(disable_aes_gcm_sha384 &&
|
---|
820 | algo("TLS_AES_256_GCM_SHA384", namep, n)) {
|
---|
821 | disable_aes_gcm_sha384 = FALSE;
|
---|
822 | }
|
---|
823 | else if(disable_aes_gcm_sha256
|
---|
824 | && algo("TLS_AES_128_GCM_SHA256", namep, n)) {
|
---|
825 | disable_aes_gcm_sha256 = FALSE;
|
---|
826 | }
|
---|
827 | else if(disable_chacha_poly
|
---|
828 | && algo("TLS_CHACHA20_POLY1305_SHA256", namep, n)) {
|
---|
829 | disable_chacha_poly = FALSE;
|
---|
830 | }
|
---|
831 | else if(disable_aes_ccm_8_sha256
|
---|
832 | && algo("TLS_AES_128_CCM_8_SHA256", namep, n)) {
|
---|
833 | disable_aes_ccm_8_sha256 = FALSE;
|
---|
834 | }
|
---|
835 | else if(disable_aes_ccm_sha256
|
---|
836 | && algo("TLS_AES_128_CCM_SHA256", namep, n)) {
|
---|
837 | disable_aes_ccm_sha256 = FALSE;
|
---|
838 | }
|
---|
839 | else {
|
---|
840 | failf(data, "schannel: Unknown TLS 1.3 cipher: %.*s", (int)n, namep);
|
---|
841 | return CURLE_SSL_CIPHER;
|
---|
842 | }
|
---|
843 |
|
---|
844 | startCur = nameEnd;
|
---|
845 | if(startCur)
|
---|
846 | startCur++;
|
---|
847 |
|
---|
848 | algCount++;
|
---|
849 | }
|
---|
850 | }
|
---|
851 |
|
---|
852 | if(disable_aes_gcm_sha384 && disable_aes_gcm_sha256
|
---|
853 | && disable_chacha_poly && disable_aes_ccm_8_sha256
|
---|
854 | && disable_aes_ccm_sha256) {
|
---|
855 | failf(data, "schannel: All available TLS 1.3 ciphers were disabled");
|
---|
856 | return CURLE_SSL_CIPHER;
|
---|
857 | }
|
---|
858 |
|
---|
859 | /* Disable TLS_AES_128_CCM_8_SHA256 and/or TLS_AES_128_CCM_SHA256 */
|
---|
860 | if(disable_aes_ccm_8_sha256 || disable_aes_ccm_sha256) {
|
---|
861 | /*
|
---|
862 | Disallow AES_CCM algorithm.
|
---|
863 | */
|
---|
864 | blocked_ccm_modes[0].Length = sizeof(BCRYPT_CHAIN_MODE_CCM);
|
---|
865 | blocked_ccm_modes[0].MaximumLength = sizeof(BCRYPT_CHAIN_MODE_CCM);
|
---|
866 | blocked_ccm_modes[0].Buffer = (PWSTR)BCRYPT_CHAIN_MODE_CCM;
|
---|
867 |
|
---|
868 | crypto_settings[crypto_settings_idx].eAlgorithmUsage =
|
---|
869 | TlsParametersCngAlgUsageCipher;
|
---|
870 | crypto_settings[crypto_settings_idx].rgstrChainingModes =
|
---|
871 | blocked_ccm_modes;
|
---|
872 | crypto_settings[crypto_settings_idx].cChainingModes =
|
---|
873 | ARRAYSIZE(blocked_ccm_modes);
|
---|
874 | crypto_settings[crypto_settings_idx].strCngAlgId.Length =
|
---|
875 | sizeof(BCRYPT_AES_ALGORITHM);
|
---|
876 | crypto_settings[crypto_settings_idx].strCngAlgId.MaximumLength =
|
---|
877 | sizeof(BCRYPT_AES_ALGORITHM);
|
---|
878 | crypto_settings[crypto_settings_idx].strCngAlgId.Buffer =
|
---|
879 | (PWSTR)BCRYPT_AES_ALGORITHM;
|
---|
880 |
|
---|
881 | /* only disabling one of the CCM modes */
|
---|
882 | if(disable_aes_ccm_8_sha256 != disable_aes_ccm_sha256) {
|
---|
883 | if(disable_aes_ccm_8_sha256)
|
---|
884 | crypto_settings[crypto_settings_idx].dwMinBitLength = 128;
|
---|
885 | else /* disable_aes_ccm_sha256 */
|
---|
886 | crypto_settings[crypto_settings_idx].dwMaxBitLength = 64;
|
---|
887 | }
|
---|
888 |
|
---|
889 | crypto_settings_idx++;
|
---|
890 | }
|
---|
891 |
|
---|
892 | /* Disable TLS_AES_256_GCM_SHA384 and/or TLS_AES_128_GCM_SHA256 */
|
---|
893 | if(disable_aes_gcm_sha384 || disable_aes_gcm_sha256) {
|
---|
894 |
|
---|
895 | /*
|
---|
896 | Disallow AES_GCM algorithm
|
---|
897 | */
|
---|
898 | blocked_gcm_modes[0].Length = sizeof(BCRYPT_CHAIN_MODE_GCM);
|
---|
899 | blocked_gcm_modes[0].MaximumLength = sizeof(BCRYPT_CHAIN_MODE_GCM);
|
---|
900 | blocked_gcm_modes[0].Buffer = (PWSTR)BCRYPT_CHAIN_MODE_GCM;
|
---|
901 |
|
---|
902 | /* if only one is disabled, then explicitly disable the
|
---|
903 | digest cipher suite (sha384 or sha256) */
|
---|
904 | if(disable_aes_gcm_sha384 != disable_aes_gcm_sha256) {
|
---|
905 | crypto_settings[crypto_settings_idx].eAlgorithmUsage =
|
---|
906 | TlsParametersCngAlgUsageDigest;
|
---|
907 | crypto_settings[crypto_settings_idx].strCngAlgId.Length =
|
---|
908 | sizeof(disable_aes_gcm_sha384 ?
|
---|
909 | BCRYPT_SHA384_ALGORITHM : BCRYPT_SHA256_ALGORITHM);
|
---|
910 | crypto_settings[crypto_settings_idx].strCngAlgId.MaximumLength =
|
---|
911 | sizeof(disable_aes_gcm_sha384 ?
|
---|
912 | BCRYPT_SHA384_ALGORITHM : BCRYPT_SHA256_ALGORITHM);
|
---|
913 | crypto_settings[crypto_settings_idx].strCngAlgId.Buffer =
|
---|
914 | (PWSTR)(disable_aes_gcm_sha384 ?
|
---|
915 | BCRYPT_SHA384_ALGORITHM : BCRYPT_SHA256_ALGORITHM);
|
---|
916 | }
|
---|
917 | else { /* Disable both AES_GCM ciphers */
|
---|
918 | crypto_settings[crypto_settings_idx].eAlgorithmUsage =
|
---|
919 | TlsParametersCngAlgUsageCipher;
|
---|
920 | crypto_settings[crypto_settings_idx].strCngAlgId.Length =
|
---|
921 | sizeof(BCRYPT_AES_ALGORITHM);
|
---|
922 | crypto_settings[crypto_settings_idx].strCngAlgId.MaximumLength =
|
---|
923 | sizeof(BCRYPT_AES_ALGORITHM);
|
---|
924 | crypto_settings[crypto_settings_idx].strCngAlgId.Buffer =
|
---|
925 | (PWSTR)BCRYPT_AES_ALGORITHM;
|
---|
926 | }
|
---|
927 |
|
---|
928 | crypto_settings[crypto_settings_idx].rgstrChainingModes =
|
---|
929 | blocked_gcm_modes;
|
---|
930 | crypto_settings[crypto_settings_idx].cChainingModes = 1;
|
---|
931 |
|
---|
932 | crypto_settings_idx++;
|
---|
933 | }
|
---|
934 |
|
---|
935 | /*
|
---|
936 | Disable ChaCha20-Poly1305.
|
---|
937 | */
|
---|
938 | if(disable_chacha_poly) {
|
---|
939 | crypto_settings[crypto_settings_idx].eAlgorithmUsage =
|
---|
940 | TlsParametersCngAlgUsageCipher;
|
---|
941 | crypto_settings[crypto_settings_idx].strCngAlgId.Length =
|
---|
942 | sizeof(BCRYPT_CHACHA20_POLY1305_ALGORITHM);
|
---|
943 | crypto_settings[crypto_settings_idx].strCngAlgId.MaximumLength =
|
---|
944 | sizeof(BCRYPT_CHACHA20_POLY1305_ALGORITHM);
|
---|
945 | crypto_settings[crypto_settings_idx].strCngAlgId.Buffer =
|
---|
946 | (PWSTR)BCRYPT_CHACHA20_POLY1305_ALGORITHM;
|
---|
947 | crypto_settings_idx++;
|
---|
948 | }
|
---|
949 |
|
---|
950 | tls_parameters.pDisabledCrypto = crypto_settings;
|
---|
951 |
|
---|
952 | /* The number of blocked suites */
|
---|
953 | tls_parameters.cDisabledCrypto = crypto_settings_idx;
|
---|
954 | credentials.pTlsParameters = &tls_parameters;
|
---|
955 | credentials.cTlsParameters = 1;
|
---|
956 |
|
---|
957 | credentials.dwVersion = SCH_CREDENTIALS_VERSION;
|
---|
958 | credentials.dwFlags = flags | SCH_USE_STRONG_CRYPTO;
|
---|
959 |
|
---|
960 | credentials.pTlsParameters->grbitDisabledProtocols =
|
---|
961 | (DWORD)~enabled_protocols;
|
---|
962 |
|
---|
963 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
964 | if(client_certs[0]) {
|
---|
965 | credentials.cCreds = 1;
|
---|
966 | credentials.paCred = client_certs;
|
---|
967 | }
|
---|
968 | #endif
|
---|
969 |
|
---|
970 | sspi_status =
|
---|
971 | s_pSecFn->AcquireCredentialsHandle(NULL, (TCHAR*)UNISP_NAME,
|
---|
972 | SECPKG_CRED_OUTBOUND, NULL,
|
---|
973 | &credentials, NULL, NULL,
|
---|
974 | &backend->cred->cred_handle,
|
---|
975 | &backend->cred->time_stamp);
|
---|
976 | }
|
---|
977 | else {
|
---|
978 | /* Pre-Windows 10 1809 or the user set a legacy algorithm list. Although MS
|
---|
979 | doesn't document it, currently Schannel will not negotiate TLS 1.3 when
|
---|
980 | SCHANNEL_CRED is used. */
|
---|
981 | ALG_ID algIds[NUM_CIPHERS];
|
---|
982 | char *ciphers = conn_config->cipher_list;
|
---|
983 | SCHANNEL_CRED schannel_cred = { 0 };
|
---|
984 | schannel_cred.dwVersion = SCHANNEL_CRED_VERSION;
|
---|
985 | schannel_cred.dwFlags = flags;
|
---|
986 | schannel_cred.grbitEnabledProtocols = enabled_protocols;
|
---|
987 |
|
---|
988 | if(ciphers) {
|
---|
989 | if((enabled_protocols & SP_PROT_TLS1_3_CLIENT)) {
|
---|
990 | infof(data, "schannel: WARNING: This version of Schannel may "
|
---|
991 | "negotiate a less-secure TLS version than TLS 1.3 because the "
|
---|
992 | "user set an algorithm cipher list.");
|
---|
993 | }
|
---|
994 | if(conn_config->cipher_list13) {
|
---|
995 | failf(data, "schannel: This version of Schannel does not support "
|
---|
996 | "setting an algorithm cipher list and TLS 1.3 cipher list at "
|
---|
997 | "the same time");
|
---|
998 | return CURLE_SSL_CIPHER;
|
---|
999 | }
|
---|
1000 | result = set_ssl_ciphers(&schannel_cred, ciphers, algIds);
|
---|
1001 | if(CURLE_OK != result) {
|
---|
1002 | failf(data, "schannel: Failed setting algorithm cipher list");
|
---|
1003 | return result;
|
---|
1004 | }
|
---|
1005 | }
|
---|
1006 | else {
|
---|
1007 | schannel_cred.dwFlags = flags | SCH_USE_STRONG_CRYPTO;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
1011 | if(client_certs[0]) {
|
---|
1012 | schannel_cred.cCreds = 1;
|
---|
1013 | schannel_cred.paCred = client_certs;
|
---|
1014 | }
|
---|
1015 | #endif
|
---|
1016 |
|
---|
1017 | sspi_status =
|
---|
1018 | s_pSecFn->AcquireCredentialsHandle(NULL, (TCHAR*)UNISP_NAME,
|
---|
1019 | SECPKG_CRED_OUTBOUND, NULL,
|
---|
1020 | &schannel_cred, NULL, NULL,
|
---|
1021 | &backend->cred->cred_handle,
|
---|
1022 | &backend->cred->time_stamp);
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
1026 | if(client_certs[0])
|
---|
1027 | CertFreeCertificateContext(client_certs[0]);
|
---|
1028 | #endif
|
---|
1029 |
|
---|
1030 | if(sspi_status != SEC_E_OK) {
|
---|
1031 | char buffer[STRERROR_LEN];
|
---|
1032 | failf(data, "schannel: AcquireCredentialsHandle failed: %s",
|
---|
1033 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
1034 | Curl_safefree(backend->cred);
|
---|
1035 | switch(sspi_status) {
|
---|
1036 | case SEC_E_INSUFFICIENT_MEMORY:
|
---|
1037 | return CURLE_OUT_OF_MEMORY;
|
---|
1038 | case SEC_E_NO_CREDENTIALS:
|
---|
1039 | case SEC_E_SECPKG_NOT_FOUND:
|
---|
1040 | case SEC_E_NOT_OWNER:
|
---|
1041 | case SEC_E_UNKNOWN_CREDENTIALS:
|
---|
1042 | case SEC_E_INTERNAL_ERROR:
|
---|
1043 | default:
|
---|
1044 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | return CURLE_OK;
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | static CURLcode
|
---|
1052 | schannel_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
1053 | {
|
---|
1054 | ssize_t written = -1;
|
---|
1055 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
1056 | struct schannel_ssl_backend_data *backend =
|
---|
1057 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
1058 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
1059 | struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
|
---|
1060 | SecBuffer outbuf;
|
---|
1061 | SecBufferDesc outbuf_desc;
|
---|
1062 | SecBuffer inbuf;
|
---|
1063 | SecBufferDesc inbuf_desc;
|
---|
1064 | #ifdef HAS_ALPN
|
---|
1065 | unsigned char alpn_buffer[128];
|
---|
1066 | #endif
|
---|
1067 | SECURITY_STATUS sspi_status = SEC_E_OK;
|
---|
1068 | struct Curl_schannel_cred *old_cred = NULL;
|
---|
1069 | CURLcode result;
|
---|
1070 |
|
---|
1071 | DEBUGASSERT(backend);
|
---|
1072 | DEBUGF(infof(data,
|
---|
1073 | "schannel: SSL/TLS connection with %s port %d (step 1/3)",
|
---|
1074 | connssl->peer.hostname, connssl->port));
|
---|
1075 |
|
---|
1076 | if(curlx_verify_windows_version(5, 1, 0, PLATFORM_WINNT,
|
---|
1077 | VERSION_LESS_THAN_EQUAL)) {
|
---|
1078 | /* Schannel in Windows XP (OS version 5.1) uses legacy handshakes and
|
---|
1079 | algorithms that may not be supported by all servers. */
|
---|
1080 | infof(data, "schannel: Windows version is old and may not be able to "
|
---|
1081 | "connect to some servers due to lack of SNI, algorithms, etc.");
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | #ifdef HAS_ALPN
|
---|
1085 | /* ALPN is only supported on Windows 8.1 / Server 2012 R2 and above.
|
---|
1086 | Also it doesn't seem to be supported for Wine, see curl bug #983. */
|
---|
1087 | backend->use_alpn = connssl->alpn &&
|
---|
1088 | !GetProcAddress(GetModuleHandle(TEXT("ntdll")),
|
---|
1089 | "wine_get_version") &&
|
---|
1090 | curlx_verify_windows_version(6, 3, 0, PLATFORM_WINNT,
|
---|
1091 | VERSION_GREATER_THAN_EQUAL);
|
---|
1092 | #else
|
---|
1093 | backend->use_alpn = false;
|
---|
1094 | #endif
|
---|
1095 |
|
---|
1096 | #ifdef _WIN32_WCE
|
---|
1097 | #ifdef HAS_MANUAL_VERIFY_API
|
---|
1098 | /* certificate validation on CE doesn't seem to work right; we'll
|
---|
1099 | * do it following a more manual process. */
|
---|
1100 | backend->use_manual_cred_validation = true;
|
---|
1101 | #else
|
---|
1102 | #error "compiler too old to support requisite manual cert verify for Win CE"
|
---|
1103 | #endif
|
---|
1104 | #else
|
---|
1105 | #ifdef HAS_MANUAL_VERIFY_API
|
---|
1106 | if(conn_config->CAfile || conn_config->ca_info_blob) {
|
---|
1107 | if(curlx_verify_windows_version(6, 1, 0, PLATFORM_WINNT,
|
---|
1108 | VERSION_GREATER_THAN_EQUAL)) {
|
---|
1109 | backend->use_manual_cred_validation = true;
|
---|
1110 | }
|
---|
1111 | else {
|
---|
1112 | failf(data, "schannel: this version of Windows is too old to support "
|
---|
1113 | "certificate verification via CA bundle file.");
|
---|
1114 | return CURLE_SSL_CACERT_BADFILE;
|
---|
1115 | }
|
---|
1116 | }
|
---|
1117 | else
|
---|
1118 | backend->use_manual_cred_validation = false;
|
---|
1119 | #else
|
---|
1120 | if(conn_config->CAfile || conn_config->ca_info_blob) {
|
---|
1121 | failf(data, "schannel: CA cert support not built in");
|
---|
1122 | return CURLE_NOT_BUILT_IN;
|
---|
1123 | }
|
---|
1124 | #endif
|
---|
1125 | #endif
|
---|
1126 |
|
---|
1127 | backend->cred = NULL;
|
---|
1128 |
|
---|
1129 | /* check for an existing reusable credential handle */
|
---|
1130 | if(ssl_config->primary.sessionid) {
|
---|
1131 | Curl_ssl_sessionid_lock(data);
|
---|
1132 | if(!Curl_ssl_getsessionid(cf, data, (void **)&old_cred, NULL)) {
|
---|
1133 | backend->cred = old_cred;
|
---|
1134 | DEBUGF(infof(data, "schannel: reusing existing credential handle"));
|
---|
1135 |
|
---|
1136 | /* increment the reference counter of the credential/session handle */
|
---|
1137 | backend->cred->refcount++;
|
---|
1138 | DEBUGF(infof(data,
|
---|
1139 | "schannel: incremented credential handle refcount = %d",
|
---|
1140 | backend->cred->refcount));
|
---|
1141 | }
|
---|
1142 | Curl_ssl_sessionid_unlock(data);
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | if(!backend->cred) {
|
---|
1146 | char *snihost;
|
---|
1147 | result = schannel_acquire_credential_handle(cf, data);
|
---|
1148 | if(result)
|
---|
1149 | return result;
|
---|
1150 | /* schannel_acquire_credential_handle() sets backend->cred accordingly or
|
---|
1151 | it returns error otherwise. */
|
---|
1152 |
|
---|
1153 | /* A hostname associated with the credential is needed by
|
---|
1154 | InitializeSecurityContext for SNI and other reasons. */
|
---|
1155 | snihost = connssl->peer.sni? connssl->peer.sni : connssl->peer.hostname;
|
---|
1156 | backend->cred->sni_hostname = curlx_convert_UTF8_to_tchar(snihost);
|
---|
1157 | if(!backend->cred->sni_hostname)
|
---|
1158 | return CURLE_OUT_OF_MEMORY;
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | /* Warn if SNI is disabled due to use of an IP address */
|
---|
1162 | if(connssl->peer.type != CURL_SSL_PEER_DNS) {
|
---|
1163 | infof(data, "schannel: using IP address, SNI is not supported by OS.");
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | #ifdef HAS_ALPN
|
---|
1167 | if(backend->use_alpn) {
|
---|
1168 | int cur = 0;
|
---|
1169 | int list_start_index = 0;
|
---|
1170 | unsigned int *extension_len = NULL;
|
---|
1171 | unsigned short* list_len = NULL;
|
---|
1172 | struct alpn_proto_buf proto;
|
---|
1173 |
|
---|
1174 | /* The first four bytes will be an unsigned int indicating number
|
---|
1175 | of bytes of data in the rest of the buffer. */
|
---|
1176 | extension_len = (unsigned int *)(void *)(&alpn_buffer[cur]);
|
---|
1177 | cur += (int)sizeof(unsigned int);
|
---|
1178 |
|
---|
1179 | /* The next four bytes are an indicator that this buffer will contain
|
---|
1180 | ALPN data, as opposed to NPN, for example. */
|
---|
1181 | *(unsigned int *)(void *)&alpn_buffer[cur] =
|
---|
1182 | SecApplicationProtocolNegotiationExt_ALPN;
|
---|
1183 | cur += (int)sizeof(unsigned int);
|
---|
1184 |
|
---|
1185 | /* The next two bytes will be an unsigned short indicating the number
|
---|
1186 | of bytes used to list the preferred protocols. */
|
---|
1187 | list_len = (unsigned short*)(void *)(&alpn_buffer[cur]);
|
---|
1188 | cur += (int)sizeof(unsigned short);
|
---|
1189 |
|
---|
1190 | list_start_index = cur;
|
---|
1191 |
|
---|
1192 | result = Curl_alpn_to_proto_buf(&proto, connssl->alpn);
|
---|
1193 | if(result) {
|
---|
1194 | failf(data, "Error setting ALPN");
|
---|
1195 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1196 | }
|
---|
1197 | memcpy(&alpn_buffer[cur], proto.data, proto.len);
|
---|
1198 | cur += proto.len;
|
---|
1199 |
|
---|
1200 | *list_len = curlx_uitous(cur - list_start_index);
|
---|
1201 | *extension_len = (unsigned int)(*list_len +
|
---|
1202 | sizeof(unsigned int) + sizeof(unsigned short));
|
---|
1203 |
|
---|
1204 | InitSecBuffer(&inbuf, SECBUFFER_APPLICATION_PROTOCOLS, alpn_buffer, cur);
|
---|
1205 | InitSecBufferDesc(&inbuf_desc, &inbuf, 1);
|
---|
1206 |
|
---|
1207 | Curl_alpn_to_proto_str(&proto, connssl->alpn);
|
---|
1208 | infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
|
---|
1209 | }
|
---|
1210 | else {
|
---|
1211 | InitSecBuffer(&inbuf, SECBUFFER_EMPTY, NULL, 0);
|
---|
1212 | InitSecBufferDesc(&inbuf_desc, &inbuf, 1);
|
---|
1213 | }
|
---|
1214 | #else /* HAS_ALPN */
|
---|
1215 | InitSecBuffer(&inbuf, SECBUFFER_EMPTY, NULL, 0);
|
---|
1216 | InitSecBufferDesc(&inbuf_desc, &inbuf, 1);
|
---|
1217 | #endif
|
---|
1218 |
|
---|
1219 | /* setup output buffer */
|
---|
1220 | InitSecBuffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
|
---|
1221 | InitSecBufferDesc(&outbuf_desc, &outbuf, 1);
|
---|
1222 |
|
---|
1223 | /* security request flags */
|
---|
1224 | backend->req_flags = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT |
|
---|
1225 | ISC_REQ_CONFIDENTIALITY | ISC_REQ_ALLOCATE_MEMORY |
|
---|
1226 | ISC_REQ_STREAM;
|
---|
1227 |
|
---|
1228 | if(!ssl_config->auto_client_cert) {
|
---|
1229 | backend->req_flags |= ISC_REQ_USE_SUPPLIED_CREDS;
|
---|
1230 | }
|
---|
1231 |
|
---|
1232 | /* allocate memory for the security context handle */
|
---|
1233 | backend->ctxt = (struct Curl_schannel_ctxt *)
|
---|
1234 | calloc(1, sizeof(struct Curl_schannel_ctxt));
|
---|
1235 | if(!backend->ctxt) {
|
---|
1236 | failf(data, "schannel: unable to allocate memory");
|
---|
1237 | return CURLE_OUT_OF_MEMORY;
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | /* Schannel InitializeSecurityContext:
|
---|
1241 | https://msdn.microsoft.com/en-us/library/windows/desktop/aa375924.aspx
|
---|
1242 |
|
---|
1243 | At the moment we don't pass inbuf unless we're using ALPN since we only
|
---|
1244 | use it for that, and Wine (for which we currently disable ALPN) is giving
|
---|
1245 | us problems with inbuf regardless. https://github.com/curl/curl/issues/983
|
---|
1246 | */
|
---|
1247 | sspi_status = s_pSecFn->InitializeSecurityContext(
|
---|
1248 | &backend->cred->cred_handle, NULL, backend->cred->sni_hostname,
|
---|
1249 | backend->req_flags, 0, 0,
|
---|
1250 | (backend->use_alpn ? &inbuf_desc : NULL),
|
---|
1251 | 0, &backend->ctxt->ctxt_handle,
|
---|
1252 | &outbuf_desc, &backend->ret_flags, &backend->ctxt->time_stamp);
|
---|
1253 |
|
---|
1254 | if(sspi_status != SEC_I_CONTINUE_NEEDED) {
|
---|
1255 | char buffer[STRERROR_LEN];
|
---|
1256 | Curl_safefree(backend->ctxt);
|
---|
1257 | switch(sspi_status) {
|
---|
1258 | case SEC_E_INSUFFICIENT_MEMORY:
|
---|
1259 | failf(data, "schannel: initial InitializeSecurityContext failed: %s",
|
---|
1260 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
1261 | return CURLE_OUT_OF_MEMORY;
|
---|
1262 | case SEC_E_WRONG_PRINCIPAL:
|
---|
1263 | failf(data, "schannel: SNI or certificate check failed: %s",
|
---|
1264 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
1265 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1266 | /*
|
---|
1267 | case SEC_E_INVALID_HANDLE:
|
---|
1268 | case SEC_E_INVALID_TOKEN:
|
---|
1269 | case SEC_E_LOGON_DENIED:
|
---|
1270 | case SEC_E_TARGET_UNKNOWN:
|
---|
1271 | case SEC_E_NO_AUTHENTICATING_AUTHORITY:
|
---|
1272 | case SEC_E_INTERNAL_ERROR:
|
---|
1273 | case SEC_E_NO_CREDENTIALS:
|
---|
1274 | case SEC_E_UNSUPPORTED_FUNCTION:
|
---|
1275 | case SEC_E_APPLICATION_PROTOCOL_MISMATCH:
|
---|
1276 | */
|
---|
1277 | default:
|
---|
1278 | failf(data, "schannel: initial InitializeSecurityContext failed: %s",
|
---|
1279 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
1280 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1281 | }
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | DEBUGF(infof(data, "schannel: sending initial handshake data: "
|
---|
1285 | "sending %lu bytes.", outbuf.cbBuffer));
|
---|
1286 |
|
---|
1287 | /* send initial handshake data which is now stored in output buffer */
|
---|
1288 | written = Curl_conn_cf_send(cf->next, data,
|
---|
1289 | outbuf.pvBuffer, outbuf.cbBuffer,
|
---|
1290 | &result);
|
---|
1291 | s_pSecFn->FreeContextBuffer(outbuf.pvBuffer);
|
---|
1292 | if((result != CURLE_OK) || (outbuf.cbBuffer != (size_t) written)) {
|
---|
1293 | failf(data, "schannel: failed to send initial handshake data: "
|
---|
1294 | "sent %zd of %lu bytes", written, outbuf.cbBuffer);
|
---|
1295 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | DEBUGF(infof(data, "schannel: sent initial handshake data: "
|
---|
1299 | "sent %zd bytes", written));
|
---|
1300 |
|
---|
1301 | backend->recv_unrecoverable_err = CURLE_OK;
|
---|
1302 | backend->recv_sspi_close_notify = false;
|
---|
1303 | backend->recv_connection_closed = false;
|
---|
1304 | backend->recv_renegotiating = false;
|
---|
1305 | backend->encdata_is_incomplete = false;
|
---|
1306 |
|
---|
1307 | /* continue to second handshake step */
|
---|
1308 | connssl->connecting_state = ssl_connect_2;
|
---|
1309 |
|
---|
1310 | return CURLE_OK;
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | static CURLcode
|
---|
1314 | schannel_connect_step2(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
1315 | {
|
---|
1316 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
1317 | struct schannel_ssl_backend_data *backend =
|
---|
1318 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
1319 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
1320 | int i;
|
---|
1321 | ssize_t nread = -1, written = -1;
|
---|
1322 | unsigned char *reallocated_buffer;
|
---|
1323 | SecBuffer outbuf[3];
|
---|
1324 | SecBufferDesc outbuf_desc;
|
---|
1325 | SecBuffer inbuf[2];
|
---|
1326 | SecBufferDesc inbuf_desc;
|
---|
1327 | SECURITY_STATUS sspi_status = SEC_E_OK;
|
---|
1328 | CURLcode result;
|
---|
1329 | bool doread;
|
---|
1330 | const char *pubkey_ptr;
|
---|
1331 |
|
---|
1332 | DEBUGASSERT(backend);
|
---|
1333 |
|
---|
1334 | doread = (connssl->connecting_state != ssl_connect_2_writing) ? TRUE : FALSE;
|
---|
1335 |
|
---|
1336 | DEBUGF(infof(data,
|
---|
1337 | "schannel: SSL/TLS connection with %s port %d (step 2/3)",
|
---|
1338 | connssl->peer.hostname, connssl->port));
|
---|
1339 |
|
---|
1340 | if(!backend->cred || !backend->ctxt)
|
---|
1341 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1342 |
|
---|
1343 | /* buffer to store previously received and decrypted data */
|
---|
1344 | if(!backend->decdata_buffer) {
|
---|
1345 | backend->decdata_offset = 0;
|
---|
1346 | backend->decdata_length = CURL_SCHANNEL_BUFFER_INIT_SIZE;
|
---|
1347 | backend->decdata_buffer = malloc(backend->decdata_length);
|
---|
1348 | if(!backend->decdata_buffer) {
|
---|
1349 | failf(data, "schannel: unable to allocate memory");
|
---|
1350 | return CURLE_OUT_OF_MEMORY;
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | /* buffer to store previously received and encrypted data */
|
---|
1355 | if(!backend->encdata_buffer) {
|
---|
1356 | backend->encdata_is_incomplete = false;
|
---|
1357 | backend->encdata_offset = 0;
|
---|
1358 | backend->encdata_length = CURL_SCHANNEL_BUFFER_INIT_SIZE;
|
---|
1359 | backend->encdata_buffer = malloc(backend->encdata_length);
|
---|
1360 | if(!backend->encdata_buffer) {
|
---|
1361 | failf(data, "schannel: unable to allocate memory");
|
---|
1362 | return CURLE_OUT_OF_MEMORY;
|
---|
1363 | }
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | /* if we need a bigger buffer to read a full message, increase buffer now */
|
---|
1367 | if(backend->encdata_length - backend->encdata_offset <
|
---|
1368 | CURL_SCHANNEL_BUFFER_FREE_SIZE) {
|
---|
1369 | /* increase internal encrypted data buffer */
|
---|
1370 | size_t reallocated_length = backend->encdata_offset +
|
---|
1371 | CURL_SCHANNEL_BUFFER_FREE_SIZE;
|
---|
1372 | reallocated_buffer = realloc(backend->encdata_buffer,
|
---|
1373 | reallocated_length);
|
---|
1374 |
|
---|
1375 | if(!reallocated_buffer) {
|
---|
1376 | failf(data, "schannel: unable to re-allocate memory");
|
---|
1377 | return CURLE_OUT_OF_MEMORY;
|
---|
1378 | }
|
---|
1379 | else {
|
---|
1380 | backend->encdata_buffer = reallocated_buffer;
|
---|
1381 | backend->encdata_length = reallocated_length;
|
---|
1382 | }
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | for(;;) {
|
---|
1386 | if(doread) {
|
---|
1387 | /* read encrypted handshake data from socket */
|
---|
1388 | nread = Curl_conn_cf_recv(cf->next, data,
|
---|
1389 | (char *) (backend->encdata_buffer +
|
---|
1390 | backend->encdata_offset),
|
---|
1391 | backend->encdata_length -
|
---|
1392 | backend->encdata_offset,
|
---|
1393 | &result);
|
---|
1394 | if(result == CURLE_AGAIN) {
|
---|
1395 | if(connssl->connecting_state != ssl_connect_2_writing)
|
---|
1396 | connssl->connecting_state = ssl_connect_2_reading;
|
---|
1397 | DEBUGF(infof(data, "schannel: failed to receive handshake, "
|
---|
1398 | "need more data"));
|
---|
1399 | return CURLE_OK;
|
---|
1400 | }
|
---|
1401 | else if((result != CURLE_OK) || (nread == 0)) {
|
---|
1402 | failf(data, "schannel: failed to receive handshake, "
|
---|
1403 | "SSL/TLS connection failed");
|
---|
1404 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | /* increase encrypted data buffer offset */
|
---|
1408 | backend->encdata_offset += nread;
|
---|
1409 | backend->encdata_is_incomplete = false;
|
---|
1410 | DEBUGF(infof(data, "schannel: encrypted data got %zd", nread));
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | DEBUGF(infof(data,
|
---|
1414 | "schannel: encrypted data buffer: offset %zu length %zu",
|
---|
1415 | backend->encdata_offset, backend->encdata_length));
|
---|
1416 |
|
---|
1417 | /* setup input buffers */
|
---|
1418 | InitSecBuffer(&inbuf[0], SECBUFFER_TOKEN, malloc(backend->encdata_offset),
|
---|
1419 | curlx_uztoul(backend->encdata_offset));
|
---|
1420 | InitSecBuffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
|
---|
1421 | InitSecBufferDesc(&inbuf_desc, inbuf, 2);
|
---|
1422 |
|
---|
1423 | /* setup output buffers */
|
---|
1424 | InitSecBuffer(&outbuf[0], SECBUFFER_TOKEN, NULL, 0);
|
---|
1425 | InitSecBuffer(&outbuf[1], SECBUFFER_ALERT, NULL, 0);
|
---|
1426 | InitSecBuffer(&outbuf[2], SECBUFFER_EMPTY, NULL, 0);
|
---|
1427 | InitSecBufferDesc(&outbuf_desc, outbuf, 3);
|
---|
1428 |
|
---|
1429 | if(!inbuf[0].pvBuffer) {
|
---|
1430 | failf(data, "schannel: unable to allocate memory");
|
---|
1431 | return CURLE_OUT_OF_MEMORY;
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | /* copy received handshake data into input buffer */
|
---|
1435 | memcpy(inbuf[0].pvBuffer, backend->encdata_buffer,
|
---|
1436 | backend->encdata_offset);
|
---|
1437 |
|
---|
1438 | sspi_status = s_pSecFn->InitializeSecurityContext(
|
---|
1439 | &backend->cred->cred_handle, &backend->ctxt->ctxt_handle,
|
---|
1440 | backend->cred->sni_hostname, backend->req_flags,
|
---|
1441 | 0, 0, &inbuf_desc, 0, NULL,
|
---|
1442 | &outbuf_desc, &backend->ret_flags, &backend->ctxt->time_stamp);
|
---|
1443 |
|
---|
1444 | /* free buffer for received handshake data */
|
---|
1445 | Curl_safefree(inbuf[0].pvBuffer);
|
---|
1446 |
|
---|
1447 | /* check if the handshake was incomplete */
|
---|
1448 | if(sspi_status == SEC_E_INCOMPLETE_MESSAGE) {
|
---|
1449 | backend->encdata_is_incomplete = true;
|
---|
1450 | connssl->connecting_state = ssl_connect_2_reading;
|
---|
1451 | DEBUGF(infof(data,
|
---|
1452 | "schannel: received incomplete message, need more data"));
|
---|
1453 | return CURLE_OK;
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | /* If the server has requested a client certificate, attempt to continue
|
---|
1457 | the handshake without one. This will allow connections to servers which
|
---|
1458 | request a client certificate but do not require it. */
|
---|
1459 | if(sspi_status == SEC_I_INCOMPLETE_CREDENTIALS &&
|
---|
1460 | !(backend->req_flags & ISC_REQ_USE_SUPPLIED_CREDS)) {
|
---|
1461 | backend->req_flags |= ISC_REQ_USE_SUPPLIED_CREDS;
|
---|
1462 | connssl->connecting_state = ssl_connect_2_writing;
|
---|
1463 | DEBUGF(infof(data,
|
---|
1464 | "schannel: a client certificate has been requested"));
|
---|
1465 | return CURLE_OK;
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | /* check if the handshake needs to be continued */
|
---|
1469 | if(sspi_status == SEC_I_CONTINUE_NEEDED || sspi_status == SEC_E_OK) {
|
---|
1470 | for(i = 0; i < 3; i++) {
|
---|
1471 | /* search for handshake tokens that need to be send */
|
---|
1472 | if(outbuf[i].BufferType == SECBUFFER_TOKEN && outbuf[i].cbBuffer > 0) {
|
---|
1473 | DEBUGF(infof(data, "schannel: sending next handshake data: "
|
---|
1474 | "sending %lu bytes.", outbuf[i].cbBuffer));
|
---|
1475 |
|
---|
1476 | /* send handshake token to server */
|
---|
1477 | written = Curl_conn_cf_send(cf->next, data,
|
---|
1478 | outbuf[i].pvBuffer, outbuf[i].cbBuffer,
|
---|
1479 | &result);
|
---|
1480 | if((result != CURLE_OK) ||
|
---|
1481 | (outbuf[i].cbBuffer != (size_t) written)) {
|
---|
1482 | failf(data, "schannel: failed to send next handshake data: "
|
---|
1483 | "sent %zd of %lu bytes", written, outbuf[i].cbBuffer);
|
---|
1484 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1485 | }
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | /* free obsolete buffer */
|
---|
1489 | if(outbuf[i].pvBuffer) {
|
---|
1490 | s_pSecFn->FreeContextBuffer(outbuf[i].pvBuffer);
|
---|
1491 | }
|
---|
1492 | }
|
---|
1493 | }
|
---|
1494 | else {
|
---|
1495 | char buffer[STRERROR_LEN];
|
---|
1496 | switch(sspi_status) {
|
---|
1497 | case SEC_E_INSUFFICIENT_MEMORY:
|
---|
1498 | failf(data, "schannel: next InitializeSecurityContext failed: %s",
|
---|
1499 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
1500 | return CURLE_OUT_OF_MEMORY;
|
---|
1501 | case SEC_E_WRONG_PRINCIPAL:
|
---|
1502 | failf(data, "schannel: SNI or certificate check failed: %s",
|
---|
1503 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
1504 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1505 | case SEC_E_UNTRUSTED_ROOT:
|
---|
1506 | failf(data, "schannel: %s",
|
---|
1507 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
1508 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1509 | /*
|
---|
1510 | case SEC_E_INVALID_HANDLE:
|
---|
1511 | case SEC_E_INVALID_TOKEN:
|
---|
1512 | case SEC_E_LOGON_DENIED:
|
---|
1513 | case SEC_E_TARGET_UNKNOWN:
|
---|
1514 | case SEC_E_NO_AUTHENTICATING_AUTHORITY:
|
---|
1515 | case SEC_E_INTERNAL_ERROR:
|
---|
1516 | case SEC_E_NO_CREDENTIALS:
|
---|
1517 | case SEC_E_UNSUPPORTED_FUNCTION:
|
---|
1518 | case SEC_E_APPLICATION_PROTOCOL_MISMATCH:
|
---|
1519 | */
|
---|
1520 | default:
|
---|
1521 | failf(data, "schannel: next InitializeSecurityContext failed: %s",
|
---|
1522 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
1523 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1524 | }
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | /* check if there was additional remaining encrypted data */
|
---|
1528 | if(inbuf[1].BufferType == SECBUFFER_EXTRA && inbuf[1].cbBuffer > 0) {
|
---|
1529 | DEBUGF(infof(data, "schannel: encrypted data length: %lu",
|
---|
1530 | inbuf[1].cbBuffer));
|
---|
1531 | /*
|
---|
1532 | There are two cases where we could be getting extra data here:
|
---|
1533 | 1) If we're renegotiating a connection and the handshake is already
|
---|
1534 | complete (from the server perspective), it can encrypted app data
|
---|
1535 | (not handshake data) in an extra buffer at this point.
|
---|
1536 | 2) (sspi_status == SEC_I_CONTINUE_NEEDED) We are negotiating a
|
---|
1537 | connection and this extra data is part of the handshake.
|
---|
1538 | We should process the data immediately; waiting for the socket to
|
---|
1539 | be ready may fail since the server is done sending handshake data.
|
---|
1540 | */
|
---|
1541 | /* check if the remaining data is less than the total amount
|
---|
1542 | and therefore begins after the already processed data */
|
---|
1543 | if(backend->encdata_offset > inbuf[1].cbBuffer) {
|
---|
1544 | memmove(backend->encdata_buffer,
|
---|
1545 | (backend->encdata_buffer + backend->encdata_offset) -
|
---|
1546 | inbuf[1].cbBuffer, inbuf[1].cbBuffer);
|
---|
1547 | backend->encdata_offset = inbuf[1].cbBuffer;
|
---|
1548 | if(sspi_status == SEC_I_CONTINUE_NEEDED) {
|
---|
1549 | doread = FALSE;
|
---|
1550 | continue;
|
---|
1551 | }
|
---|
1552 | }
|
---|
1553 | }
|
---|
1554 | else {
|
---|
1555 | backend->encdata_offset = 0;
|
---|
1556 | }
|
---|
1557 | break;
|
---|
1558 | }
|
---|
1559 |
|
---|
1560 | /* check if the handshake needs to be continued */
|
---|
1561 | if(sspi_status == SEC_I_CONTINUE_NEEDED) {
|
---|
1562 | connssl->connecting_state = ssl_connect_2_reading;
|
---|
1563 | return CURLE_OK;
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | /* check if the handshake is complete */
|
---|
1567 | if(sspi_status == SEC_E_OK) {
|
---|
1568 | connssl->connecting_state = ssl_connect_3;
|
---|
1569 | DEBUGF(infof(data, "schannel: SSL/TLS handshake complete"));
|
---|
1570 | }
|
---|
1571 |
|
---|
1572 | pubkey_ptr = Curl_ssl_cf_is_proxy(cf)?
|
---|
1573 | data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]:
|
---|
1574 | data->set.str[STRING_SSL_PINNEDPUBLICKEY];
|
---|
1575 | if(pubkey_ptr) {
|
---|
1576 | result = schannel_pkp_pin_peer_pubkey(cf, data, pubkey_ptr);
|
---|
1577 | if(result) {
|
---|
1578 | failf(data, "SSL: public key does not match pinned public key");
|
---|
1579 | return result;
|
---|
1580 | }
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 | #ifdef HAS_MANUAL_VERIFY_API
|
---|
1584 | if(conn_config->verifypeer && backend->use_manual_cred_validation) {
|
---|
1585 | /* Certificate verification also verifies the hostname if verifyhost */
|
---|
1586 | return Curl_verify_certificate(cf, data);
|
---|
1587 | }
|
---|
1588 | #endif
|
---|
1589 |
|
---|
1590 | /* Verify the hostname manually when certificate verification is disabled,
|
---|
1591 | because in that case Schannel won't verify it. */
|
---|
1592 | if(!conn_config->verifypeer && conn_config->verifyhost)
|
---|
1593 | return Curl_verify_host(cf, data);
|
---|
1594 |
|
---|
1595 | return CURLE_OK;
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | static bool
|
---|
1599 | valid_cert_encoding(const CERT_CONTEXT *cert_context)
|
---|
1600 | {
|
---|
1601 | return (cert_context != NULL) &&
|
---|
1602 | ((cert_context->dwCertEncodingType & X509_ASN_ENCODING) != 0) &&
|
---|
1603 | (cert_context->pbCertEncoded != NULL) &&
|
---|
1604 | (cert_context->cbCertEncoded > 0);
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | typedef bool(*Read_crt_func)(const CERT_CONTEXT *ccert_context,
|
---|
1608 | bool reverse_order, void *arg);
|
---|
1609 |
|
---|
1610 | static void
|
---|
1611 | traverse_cert_store(const CERT_CONTEXT *context, Read_crt_func func,
|
---|
1612 | void *arg)
|
---|
1613 | {
|
---|
1614 | const CERT_CONTEXT *current_context = NULL;
|
---|
1615 | bool should_continue = true;
|
---|
1616 | bool first = true;
|
---|
1617 | bool reverse_order = false;
|
---|
1618 | while(should_continue &&
|
---|
1619 | (current_context = CertEnumCertificatesInStore(
|
---|
1620 | context->hCertStore,
|
---|
1621 | current_context)) != NULL) {
|
---|
1622 | /* Windows 11 22H2 OS Build 22621.674 or higher enumerates certificates in
|
---|
1623 | leaf-to-root order while all previous versions of Windows enumerate
|
---|
1624 | certificates in root-to-leaf order. Determine the order of enumeration
|
---|
1625 | by comparing SECPKG_ATTR_REMOTE_CERT_CONTEXT's pbCertContext with the
|
---|
1626 | first certificate's pbCertContext. */
|
---|
1627 | if(first && context->pbCertEncoded != current_context->pbCertEncoded)
|
---|
1628 | reverse_order = true;
|
---|
1629 | should_continue = func(current_context, reverse_order, arg);
|
---|
1630 | first = false;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | if(current_context)
|
---|
1634 | CertFreeCertificateContext(current_context);
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | static bool
|
---|
1638 | cert_counter_callback(const CERT_CONTEXT *ccert_context, bool reverse_order,
|
---|
1639 | void *certs_count)
|
---|
1640 | {
|
---|
1641 | (void)reverse_order; /* unused */
|
---|
1642 | if(valid_cert_encoding(ccert_context))
|
---|
1643 | (*(int *)certs_count)++;
|
---|
1644 | return true;
|
---|
1645 | }
|
---|
1646 |
|
---|
1647 | struct Adder_args
|
---|
1648 | {
|
---|
1649 | struct Curl_easy *data;
|
---|
1650 | CURLcode result;
|
---|
1651 | int idx;
|
---|
1652 | int certs_count;
|
---|
1653 | };
|
---|
1654 |
|
---|
1655 | static bool
|
---|
1656 | add_cert_to_certinfo(const CERT_CONTEXT *ccert_context, bool reverse_order,
|
---|
1657 | void *raw_arg)
|
---|
1658 | {
|
---|
1659 | struct Adder_args *args = (struct Adder_args*)raw_arg;
|
---|
1660 | args->result = CURLE_OK;
|
---|
1661 | if(valid_cert_encoding(ccert_context)) {
|
---|
1662 | const char *beg = (const char *) ccert_context->pbCertEncoded;
|
---|
1663 | const char *end = beg + ccert_context->cbCertEncoded;
|
---|
1664 | int insert_index = reverse_order ? (args->certs_count - 1) - args->idx :
|
---|
1665 | args->idx;
|
---|
1666 | args->result = Curl_extract_certinfo(args->data, insert_index,
|
---|
1667 | beg, end);
|
---|
1668 | args->idx++;
|
---|
1669 | }
|
---|
1670 | return args->result == CURLE_OK;
|
---|
1671 | }
|
---|
1672 |
|
---|
1673 | static CURLcode
|
---|
1674 | schannel_connect_step3(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
1675 | {
|
---|
1676 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
1677 | struct schannel_ssl_backend_data *backend =
|
---|
1678 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
1679 | struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
|
---|
1680 | CURLcode result = CURLE_OK;
|
---|
1681 | SECURITY_STATUS sspi_status = SEC_E_OK;
|
---|
1682 | CERT_CONTEXT *ccert_context = NULL;
|
---|
1683 | #ifdef HAS_ALPN
|
---|
1684 | SecPkgContext_ApplicationProtocol alpn_result;
|
---|
1685 | #endif
|
---|
1686 |
|
---|
1687 | DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
|
---|
1688 | DEBUGASSERT(backend);
|
---|
1689 |
|
---|
1690 | DEBUGF(infof(data,
|
---|
1691 | "schannel: SSL/TLS connection with %s port %d (step 3/3)",
|
---|
1692 | connssl->peer.hostname, connssl->port));
|
---|
1693 |
|
---|
1694 | if(!backend->cred)
|
---|
1695 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1696 |
|
---|
1697 | /* check if the required context attributes are met */
|
---|
1698 | if(backend->ret_flags != backend->req_flags) {
|
---|
1699 | if(!(backend->ret_flags & ISC_RET_SEQUENCE_DETECT))
|
---|
1700 | failf(data, "schannel: failed to setup sequence detection");
|
---|
1701 | if(!(backend->ret_flags & ISC_RET_REPLAY_DETECT))
|
---|
1702 | failf(data, "schannel: failed to setup replay detection");
|
---|
1703 | if(!(backend->ret_flags & ISC_RET_CONFIDENTIALITY))
|
---|
1704 | failf(data, "schannel: failed to setup confidentiality");
|
---|
1705 | if(!(backend->ret_flags & ISC_RET_ALLOCATED_MEMORY))
|
---|
1706 | failf(data, "schannel: failed to setup memory allocation");
|
---|
1707 | if(!(backend->ret_flags & ISC_RET_STREAM))
|
---|
1708 | failf(data, "schannel: failed to setup stream orientation");
|
---|
1709 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1710 | }
|
---|
1711 |
|
---|
1712 | #ifdef HAS_ALPN
|
---|
1713 | if(backend->use_alpn) {
|
---|
1714 | sspi_status =
|
---|
1715 | s_pSecFn->QueryContextAttributes(&backend->ctxt->ctxt_handle,
|
---|
1716 | SECPKG_ATTR_APPLICATION_PROTOCOL,
|
---|
1717 | &alpn_result);
|
---|
1718 |
|
---|
1719 | if(sspi_status != SEC_E_OK) {
|
---|
1720 | failf(data, "schannel: failed to retrieve ALPN result");
|
---|
1721 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1722 | }
|
---|
1723 |
|
---|
1724 | if(alpn_result.ProtoNegoStatus ==
|
---|
1725 | SecApplicationProtocolNegotiationStatus_Success) {
|
---|
1726 | unsigned char prev_alpn = cf->conn->alpn;
|
---|
1727 |
|
---|
1728 | Curl_alpn_set_negotiated(cf, data, alpn_result.ProtocolId,
|
---|
1729 | alpn_result.ProtocolIdSize);
|
---|
1730 | if(backend->recv_renegotiating) {
|
---|
1731 | if(prev_alpn != cf->conn->alpn &&
|
---|
1732 | prev_alpn != CURL_HTTP_VERSION_NONE) {
|
---|
1733 | /* Renegotiation selected a different protocol now, we cannot
|
---|
1734 | * deal with this */
|
---|
1735 | failf(data, "schannel: server selected an ALPN protocol too late");
|
---|
1736 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1737 | }
|
---|
1738 | }
|
---|
1739 | }
|
---|
1740 | else {
|
---|
1741 | if(!backend->recv_renegotiating)
|
---|
1742 | Curl_alpn_set_negotiated(cf, data, NULL, 0);
|
---|
1743 | }
|
---|
1744 | }
|
---|
1745 | #endif
|
---|
1746 |
|
---|
1747 | /* save the current session data for possible reuse */
|
---|
1748 | if(ssl_config->primary.sessionid) {
|
---|
1749 | bool incache;
|
---|
1750 | bool added = FALSE;
|
---|
1751 | struct Curl_schannel_cred *old_cred = NULL;
|
---|
1752 |
|
---|
1753 | Curl_ssl_sessionid_lock(data);
|
---|
1754 | incache = !(Curl_ssl_getsessionid(cf, data, (void **)&old_cred, NULL));
|
---|
1755 | if(incache) {
|
---|
1756 | if(old_cred != backend->cred) {
|
---|
1757 | DEBUGF(infof(data,
|
---|
1758 | "schannel: old credential handle is stale, removing"));
|
---|
1759 | /* we're not taking old_cred ownership here, no refcount++ is needed */
|
---|
1760 | Curl_ssl_delsessionid(data, (void *)old_cred);
|
---|
1761 | incache = FALSE;
|
---|
1762 | }
|
---|
1763 | }
|
---|
1764 | if(!incache) {
|
---|
1765 | result = Curl_ssl_addsessionid(cf, data, backend->cred,
|
---|
1766 | sizeof(struct Curl_schannel_cred),
|
---|
1767 | &added);
|
---|
1768 | if(result) {
|
---|
1769 | Curl_ssl_sessionid_unlock(data);
|
---|
1770 | failf(data, "schannel: failed to store credential handle");
|
---|
1771 | return result;
|
---|
1772 | }
|
---|
1773 | else if(added) {
|
---|
1774 | /* this cred session is now also referenced by sessionid cache */
|
---|
1775 | backend->cred->refcount++;
|
---|
1776 | DEBUGF(infof(data,
|
---|
1777 | "schannel: stored credential handle in session cache"));
|
---|
1778 | }
|
---|
1779 | }
|
---|
1780 | Curl_ssl_sessionid_unlock(data);
|
---|
1781 | }
|
---|
1782 |
|
---|
1783 | if(data->set.ssl.certinfo) {
|
---|
1784 | int certs_count = 0;
|
---|
1785 | sspi_status =
|
---|
1786 | s_pSecFn->QueryContextAttributes(&backend->ctxt->ctxt_handle,
|
---|
1787 | SECPKG_ATTR_REMOTE_CERT_CONTEXT,
|
---|
1788 | &ccert_context);
|
---|
1789 |
|
---|
1790 | if((sspi_status != SEC_E_OK) || !ccert_context) {
|
---|
1791 | failf(data, "schannel: failed to retrieve remote cert context");
|
---|
1792 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | traverse_cert_store(ccert_context, cert_counter_callback, &certs_count);
|
---|
1796 |
|
---|
1797 | result = Curl_ssl_init_certinfo(data, certs_count);
|
---|
1798 | if(!result) {
|
---|
1799 | struct Adder_args args;
|
---|
1800 | args.data = data;
|
---|
1801 | args.idx = 0;
|
---|
1802 | args.certs_count = certs_count;
|
---|
1803 | traverse_cert_store(ccert_context, add_cert_to_certinfo, &args);
|
---|
1804 | result = args.result;
|
---|
1805 | }
|
---|
1806 | CertFreeCertificateContext(ccert_context);
|
---|
1807 | if(result)
|
---|
1808 | return result;
|
---|
1809 | }
|
---|
1810 |
|
---|
1811 | connssl->connecting_state = ssl_connect_done;
|
---|
1812 |
|
---|
1813 | return CURLE_OK;
|
---|
1814 | }
|
---|
1815 |
|
---|
1816 | static CURLcode
|
---|
1817 | schannel_connect_common(struct Curl_cfilter *cf,
|
---|
1818 | struct Curl_easy *data,
|
---|
1819 | bool nonblocking, bool *done)
|
---|
1820 | {
|
---|
1821 | CURLcode result;
|
---|
1822 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
1823 | curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
|
---|
1824 | timediff_t timeout_ms;
|
---|
1825 | int what;
|
---|
1826 |
|
---|
1827 | /* check if the connection has already been established */
|
---|
1828 | if(ssl_connection_complete == connssl->state) {
|
---|
1829 | *done = TRUE;
|
---|
1830 | return CURLE_OK;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | if(ssl_connect_1 == connssl->connecting_state) {
|
---|
1834 | /* check out how much more time we're allowed */
|
---|
1835 | timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
---|
1836 |
|
---|
1837 | if(timeout_ms < 0) {
|
---|
1838 | /* no need to continue if time already is up */
|
---|
1839 | failf(data, "SSL/TLS connection timeout");
|
---|
1840 | return CURLE_OPERATION_TIMEDOUT;
|
---|
1841 | }
|
---|
1842 |
|
---|
1843 | result = schannel_connect_step1(cf, data);
|
---|
1844 | if(result)
|
---|
1845 | return result;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | while(ssl_connect_2 == connssl->connecting_state ||
|
---|
1849 | ssl_connect_2_reading == connssl->connecting_state ||
|
---|
1850 | ssl_connect_2_writing == connssl->connecting_state) {
|
---|
1851 |
|
---|
1852 | /* check out how much more time we're allowed */
|
---|
1853 | timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
---|
1854 |
|
---|
1855 | if(timeout_ms < 0) {
|
---|
1856 | /* no need to continue if time already is up */
|
---|
1857 | failf(data, "SSL/TLS connection timeout");
|
---|
1858 | return CURLE_OPERATION_TIMEDOUT;
|
---|
1859 | }
|
---|
1860 |
|
---|
1861 | /* if ssl is expecting something, check if it's available. */
|
---|
1862 | if(connssl->connecting_state == ssl_connect_2_reading
|
---|
1863 | || connssl->connecting_state == ssl_connect_2_writing) {
|
---|
1864 |
|
---|
1865 | curl_socket_t writefd = ssl_connect_2_writing ==
|
---|
1866 | connssl->connecting_state ? sockfd : CURL_SOCKET_BAD;
|
---|
1867 | curl_socket_t readfd = ssl_connect_2_reading ==
|
---|
1868 | connssl->connecting_state ? sockfd : CURL_SOCKET_BAD;
|
---|
1869 |
|
---|
1870 | what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
|
---|
1871 | nonblocking ? 0 : timeout_ms);
|
---|
1872 | if(what < 0) {
|
---|
1873 | /* fatal error */
|
---|
1874 | failf(data, "select/poll on SSL/TLS socket, errno: %d", SOCKERRNO);
|
---|
1875 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1876 | }
|
---|
1877 | else if(0 == what) {
|
---|
1878 | if(nonblocking) {
|
---|
1879 | *done = FALSE;
|
---|
1880 | return CURLE_OK;
|
---|
1881 | }
|
---|
1882 | else {
|
---|
1883 | /* timeout */
|
---|
1884 | failf(data, "SSL/TLS connection timeout");
|
---|
1885 | return CURLE_OPERATION_TIMEDOUT;
|
---|
1886 | }
|
---|
1887 | }
|
---|
1888 | /* socket is readable or writable */
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | /* Run transaction, and return to the caller if it failed or if
|
---|
1892 | * this connection is part of a multi handle and this loop would
|
---|
1893 | * execute again. This permits the owner of a multi handle to
|
---|
1894 | * abort a connection attempt before step2 has completed while
|
---|
1895 | * ensuring that a client using select() or epoll() will always
|
---|
1896 | * have a valid fdset to wait on.
|
---|
1897 | */
|
---|
1898 | result = schannel_connect_step2(cf, data);
|
---|
1899 | if(result || (nonblocking &&
|
---|
1900 | (ssl_connect_2 == connssl->connecting_state ||
|
---|
1901 | ssl_connect_2_reading == connssl->connecting_state ||
|
---|
1902 | ssl_connect_2_writing == connssl->connecting_state)))
|
---|
1903 | return result;
|
---|
1904 |
|
---|
1905 | } /* repeat step2 until all transactions are done. */
|
---|
1906 |
|
---|
1907 | if(ssl_connect_3 == connssl->connecting_state) {
|
---|
1908 | result = schannel_connect_step3(cf, data);
|
---|
1909 | if(result)
|
---|
1910 | return result;
|
---|
1911 | }
|
---|
1912 |
|
---|
1913 | if(ssl_connect_done == connssl->connecting_state) {
|
---|
1914 | connssl->state = ssl_connection_complete;
|
---|
1915 |
|
---|
1916 | #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
|
---|
1917 | /* When SSPI is used in combination with Schannel
|
---|
1918 | * we need the Schannel context to create the Schannel
|
---|
1919 | * binding to pass the IIS extended protection checks.
|
---|
1920 | * Available on Windows 7 or later.
|
---|
1921 | */
|
---|
1922 | {
|
---|
1923 | struct schannel_ssl_backend_data *backend =
|
---|
1924 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
1925 | DEBUGASSERT(backend);
|
---|
1926 | cf->conn->sslContext = &backend->ctxt->ctxt_handle;
|
---|
1927 | }
|
---|
1928 | #endif
|
---|
1929 |
|
---|
1930 | *done = TRUE;
|
---|
1931 | }
|
---|
1932 | else
|
---|
1933 | *done = FALSE;
|
---|
1934 |
|
---|
1935 | /* reset our connection state machine */
|
---|
1936 | connssl->connecting_state = ssl_connect_1;
|
---|
1937 |
|
---|
1938 | return CURLE_OK;
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | static ssize_t
|
---|
1942 | schannel_send(struct Curl_cfilter *cf, struct Curl_easy *data,
|
---|
1943 | const void *buf, size_t len, CURLcode *err)
|
---|
1944 | {
|
---|
1945 | ssize_t written = -1;
|
---|
1946 | size_t data_len = 0;
|
---|
1947 | unsigned char *ptr = NULL;
|
---|
1948 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
1949 | SecBuffer outbuf[4];
|
---|
1950 | SecBufferDesc outbuf_desc;
|
---|
1951 | SECURITY_STATUS sspi_status = SEC_E_OK;
|
---|
1952 | CURLcode result;
|
---|
1953 | struct schannel_ssl_backend_data *backend =
|
---|
1954 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
1955 |
|
---|
1956 | DEBUGASSERT(backend);
|
---|
1957 |
|
---|
1958 | /* check if the maximum stream sizes were queried */
|
---|
1959 | if(backend->stream_sizes.cbMaximumMessage == 0) {
|
---|
1960 | sspi_status = s_pSecFn->QueryContextAttributes(
|
---|
1961 | &backend->ctxt->ctxt_handle,
|
---|
1962 | SECPKG_ATTR_STREAM_SIZES,
|
---|
1963 | &backend->stream_sizes);
|
---|
1964 | if(sspi_status != SEC_E_OK) {
|
---|
1965 | *err = CURLE_SEND_ERROR;
|
---|
1966 | return -1;
|
---|
1967 | }
|
---|
1968 | }
|
---|
1969 |
|
---|
1970 | /* check if the buffer is longer than the maximum message length */
|
---|
1971 | if(len > backend->stream_sizes.cbMaximumMessage) {
|
---|
1972 | len = backend->stream_sizes.cbMaximumMessage;
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 | /* calculate the complete message length and allocate a buffer for it */
|
---|
1976 | data_len = backend->stream_sizes.cbHeader + len +
|
---|
1977 | backend->stream_sizes.cbTrailer;
|
---|
1978 | ptr = (unsigned char *) malloc(data_len);
|
---|
1979 | if(!ptr) {
|
---|
1980 | *err = CURLE_OUT_OF_MEMORY;
|
---|
1981 | return -1;
|
---|
1982 | }
|
---|
1983 |
|
---|
1984 | /* setup output buffers (header, data, trailer, empty) */
|
---|
1985 | InitSecBuffer(&outbuf[0], SECBUFFER_STREAM_HEADER,
|
---|
1986 | ptr, backend->stream_sizes.cbHeader);
|
---|
1987 | InitSecBuffer(&outbuf[1], SECBUFFER_DATA,
|
---|
1988 | ptr + backend->stream_sizes.cbHeader, curlx_uztoul(len));
|
---|
1989 | InitSecBuffer(&outbuf[2], SECBUFFER_STREAM_TRAILER,
|
---|
1990 | ptr + backend->stream_sizes.cbHeader + len,
|
---|
1991 | backend->stream_sizes.cbTrailer);
|
---|
1992 | InitSecBuffer(&outbuf[3], SECBUFFER_EMPTY, NULL, 0);
|
---|
1993 | InitSecBufferDesc(&outbuf_desc, outbuf, 4);
|
---|
1994 |
|
---|
1995 | /* copy data into output buffer */
|
---|
1996 | memcpy(outbuf[1].pvBuffer, buf, len);
|
---|
1997 |
|
---|
1998 | /* https://msdn.microsoft.com/en-us/library/windows/desktop/aa375390.aspx */
|
---|
1999 | sspi_status = s_pSecFn->EncryptMessage(&backend->ctxt->ctxt_handle, 0,
|
---|
2000 | &outbuf_desc, 0);
|
---|
2001 |
|
---|
2002 | /* check if the message was encrypted */
|
---|
2003 | if(sspi_status == SEC_E_OK) {
|
---|
2004 | written = 0;
|
---|
2005 |
|
---|
2006 | /* send the encrypted message including header, data and trailer */
|
---|
2007 | len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
|
---|
2008 |
|
---|
2009 | /*
|
---|
2010 | It's important to send the full message which includes the header,
|
---|
2011 | encrypted payload, and trailer. Until the client receives all the
|
---|
2012 | data a coherent message has not been delivered and the client
|
---|
2013 | can't read any of it.
|
---|
2014 |
|
---|
2015 | If we wanted to buffer the unwritten encrypted bytes, we would
|
---|
2016 | tell the client that all data it has requested to be sent has been
|
---|
2017 | sent. The unwritten encrypted bytes would be the first bytes to
|
---|
2018 | send on the next invocation.
|
---|
2019 | Here's the catch with this - if we tell the client that all the
|
---|
2020 | bytes have been sent, will the client call this method again to
|
---|
2021 | send the buffered data? Looking at who calls this function, it
|
---|
2022 | seems the answer is NO.
|
---|
2023 | */
|
---|
2024 |
|
---|
2025 | /* send entire message or fail */
|
---|
2026 | while(len > (size_t)written) {
|
---|
2027 | ssize_t this_write = 0;
|
---|
2028 | int what;
|
---|
2029 | timediff_t timeout_ms = Curl_timeleft(data, NULL, FALSE);
|
---|
2030 | if(timeout_ms < 0) {
|
---|
2031 | /* we already got the timeout */
|
---|
2032 | failf(data, "schannel: timed out sending data "
|
---|
2033 | "(bytes sent: %zd)", written);
|
---|
2034 | *err = CURLE_OPERATION_TIMEDOUT;
|
---|
2035 | written = -1;
|
---|
2036 | break;
|
---|
2037 | }
|
---|
2038 | else if(!timeout_ms)
|
---|
2039 | timeout_ms = TIMEDIFF_T_MAX;
|
---|
2040 | what = SOCKET_WRITABLE(Curl_conn_cf_get_socket(cf, data), timeout_ms);
|
---|
2041 | if(what < 0) {
|
---|
2042 | /* fatal error */
|
---|
2043 | failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
|
---|
2044 | *err = CURLE_SEND_ERROR;
|
---|
2045 | written = -1;
|
---|
2046 | break;
|
---|
2047 | }
|
---|
2048 | else if(0 == what) {
|
---|
2049 | failf(data, "schannel: timed out sending data "
|
---|
2050 | "(bytes sent: %zd)", written);
|
---|
2051 | *err = CURLE_OPERATION_TIMEDOUT;
|
---|
2052 | written = -1;
|
---|
2053 | break;
|
---|
2054 | }
|
---|
2055 | /* socket is writable */
|
---|
2056 |
|
---|
2057 | this_write = Curl_conn_cf_send(cf->next, data,
|
---|
2058 | ptr + written, len - written,
|
---|
2059 | &result);
|
---|
2060 | if(result == CURLE_AGAIN)
|
---|
2061 | continue;
|
---|
2062 | else if(result != CURLE_OK) {
|
---|
2063 | *err = result;
|
---|
2064 | written = -1;
|
---|
2065 | break;
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | written += this_write;
|
---|
2069 | }
|
---|
2070 | }
|
---|
2071 | else if(sspi_status == SEC_E_INSUFFICIENT_MEMORY) {
|
---|
2072 | *err = CURLE_OUT_OF_MEMORY;
|
---|
2073 | }
|
---|
2074 | else{
|
---|
2075 | *err = CURLE_SEND_ERROR;
|
---|
2076 | }
|
---|
2077 |
|
---|
2078 | Curl_safefree(ptr);
|
---|
2079 |
|
---|
2080 | if(len == (size_t)written)
|
---|
2081 | /* Encrypted message including header, data and trailer entirely sent.
|
---|
2082 | The return value is the number of unencrypted bytes that were sent. */
|
---|
2083 | written = outbuf[1].cbBuffer;
|
---|
2084 |
|
---|
2085 | return written;
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 | static ssize_t
|
---|
2089 | schannel_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
|
---|
2090 | char *buf, size_t len, CURLcode *err)
|
---|
2091 | {
|
---|
2092 | size_t size = 0;
|
---|
2093 | ssize_t nread = -1;
|
---|
2094 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
2095 | unsigned char *reallocated_buffer;
|
---|
2096 | size_t reallocated_length;
|
---|
2097 | bool done = FALSE;
|
---|
2098 | SecBuffer inbuf[4];
|
---|
2099 | SecBufferDesc inbuf_desc;
|
---|
2100 | SECURITY_STATUS sspi_status = SEC_E_OK;
|
---|
2101 | /* we want the length of the encrypted buffer to be at least large enough
|
---|
2102 | that it can hold all the bytes requested and some TLS record overhead. */
|
---|
2103 | size_t min_encdata_length = len + CURL_SCHANNEL_BUFFER_FREE_SIZE;
|
---|
2104 | struct schannel_ssl_backend_data *backend =
|
---|
2105 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
2106 |
|
---|
2107 | DEBUGASSERT(backend);
|
---|
2108 |
|
---|
2109 | /****************************************************************************
|
---|
2110 | * Don't return or set backend->recv_unrecoverable_err unless in the cleanup.
|
---|
2111 | * The pattern for return error is set *err, optional infof, goto cleanup.
|
---|
2112 | *
|
---|
2113 | * Our priority is to always return as much decrypted data to the caller as
|
---|
2114 | * possible, even if an error occurs. The state of the decrypted buffer must
|
---|
2115 | * always be valid. Transfer of decrypted data to the caller's buffer is
|
---|
2116 | * handled in the cleanup.
|
---|
2117 | */
|
---|
2118 |
|
---|
2119 | DEBUGF(infof(data, "schannel: client wants to read %zu bytes", len));
|
---|
2120 | *err = CURLE_OK;
|
---|
2121 |
|
---|
2122 | if(len && len <= backend->decdata_offset) {
|
---|
2123 | infof(data, "schannel: enough decrypted data is already available");
|
---|
2124 | goto cleanup;
|
---|
2125 | }
|
---|
2126 | else if(backend->recv_unrecoverable_err) {
|
---|
2127 | *err = backend->recv_unrecoverable_err;
|
---|
2128 | infof(data, "schannel: an unrecoverable error occurred in a prior call");
|
---|
2129 | goto cleanup;
|
---|
2130 | }
|
---|
2131 | else if(backend->recv_sspi_close_notify) {
|
---|
2132 | /* once a server has indicated shutdown there is no more encrypted data */
|
---|
2133 | infof(data, "schannel: server indicated shutdown in a prior call");
|
---|
2134 | goto cleanup;
|
---|
2135 | }
|
---|
2136 | /* It's debatable what to return when !len. Regardless we can't return
|
---|
2137 | immediately because there may be data to decrypt (in the case we want to
|
---|
2138 | decrypt all encrypted cached data) so handle !len later in cleanup.
|
---|
2139 | */
|
---|
2140 | else if(len && !backend->recv_connection_closed) {
|
---|
2141 | /* increase enc buffer in order to fit the requested amount of data */
|
---|
2142 | size = backend->encdata_length - backend->encdata_offset;
|
---|
2143 | if(size < CURL_SCHANNEL_BUFFER_FREE_SIZE ||
|
---|
2144 | backend->encdata_length < min_encdata_length) {
|
---|
2145 | reallocated_length = backend->encdata_offset +
|
---|
2146 | CURL_SCHANNEL_BUFFER_FREE_SIZE;
|
---|
2147 | if(reallocated_length < min_encdata_length) {
|
---|
2148 | reallocated_length = min_encdata_length;
|
---|
2149 | }
|
---|
2150 | reallocated_buffer = realloc(backend->encdata_buffer,
|
---|
2151 | reallocated_length);
|
---|
2152 | if(!reallocated_buffer) {
|
---|
2153 | *err = CURLE_OUT_OF_MEMORY;
|
---|
2154 | failf(data, "schannel: unable to re-allocate memory");
|
---|
2155 | goto cleanup;
|
---|
2156 | }
|
---|
2157 |
|
---|
2158 | backend->encdata_buffer = reallocated_buffer;
|
---|
2159 | backend->encdata_length = reallocated_length;
|
---|
2160 | size = backend->encdata_length - backend->encdata_offset;
|
---|
2161 | DEBUGF(infof(data, "schannel: encdata_buffer resized %zu",
|
---|
2162 | backend->encdata_length));
|
---|
2163 | }
|
---|
2164 |
|
---|
2165 | DEBUGF(infof(data,
|
---|
2166 | "schannel: encrypted data buffer: offset %zu length %zu",
|
---|
2167 | backend->encdata_offset, backend->encdata_length));
|
---|
2168 |
|
---|
2169 | /* read encrypted data from socket */
|
---|
2170 | nread = Curl_conn_cf_recv(cf->next, data,
|
---|
2171 | (char *)(backend->encdata_buffer +
|
---|
2172 | backend->encdata_offset),
|
---|
2173 | size, err);
|
---|
2174 | if(*err) {
|
---|
2175 | nread = -1;
|
---|
2176 | if(*err == CURLE_AGAIN)
|
---|
2177 | DEBUGF(infof(data,
|
---|
2178 | "schannel: recv returned CURLE_AGAIN"));
|
---|
2179 | else if(*err == CURLE_RECV_ERROR)
|
---|
2180 | infof(data, "schannel: recv returned CURLE_RECV_ERROR");
|
---|
2181 | else
|
---|
2182 | infof(data, "schannel: recv returned error %d", *err);
|
---|
2183 | }
|
---|
2184 | else if(nread == 0) {
|
---|
2185 | backend->recv_connection_closed = true;
|
---|
2186 | DEBUGF(infof(data, "schannel: server closed the connection"));
|
---|
2187 | }
|
---|
2188 | else if(nread > 0) {
|
---|
2189 | backend->encdata_offset += (size_t)nread;
|
---|
2190 | backend->encdata_is_incomplete = false;
|
---|
2191 | DEBUGF(infof(data, "schannel: encrypted data got %zd", nread));
|
---|
2192 | }
|
---|
2193 | }
|
---|
2194 |
|
---|
2195 | DEBUGF(infof(data,
|
---|
2196 | "schannel: encrypted data buffer: offset %zu length %zu",
|
---|
2197 | backend->encdata_offset, backend->encdata_length));
|
---|
2198 |
|
---|
2199 | /* decrypt loop */
|
---|
2200 | while(backend->encdata_offset > 0 && sspi_status == SEC_E_OK &&
|
---|
2201 | (!len || backend->decdata_offset < len ||
|
---|
2202 | backend->recv_connection_closed)) {
|
---|
2203 | /* prepare data buffer for DecryptMessage call */
|
---|
2204 | InitSecBuffer(&inbuf[0], SECBUFFER_DATA, backend->encdata_buffer,
|
---|
2205 | curlx_uztoul(backend->encdata_offset));
|
---|
2206 |
|
---|
2207 | /* we need 3 more empty input buffers for possible output */
|
---|
2208 | InitSecBuffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
|
---|
2209 | InitSecBuffer(&inbuf[2], SECBUFFER_EMPTY, NULL, 0);
|
---|
2210 | InitSecBuffer(&inbuf[3], SECBUFFER_EMPTY, NULL, 0);
|
---|
2211 | InitSecBufferDesc(&inbuf_desc, inbuf, 4);
|
---|
2212 |
|
---|
2213 | /* https://msdn.microsoft.com/en-us/library/windows/desktop/aa375348.aspx
|
---|
2214 | */
|
---|
2215 | sspi_status = s_pSecFn->DecryptMessage(&backend->ctxt->ctxt_handle,
|
---|
2216 | &inbuf_desc, 0, NULL);
|
---|
2217 |
|
---|
2218 | /* check if everything went fine (server may want to renegotiate
|
---|
2219 | or shutdown the connection context) */
|
---|
2220 | if(sspi_status == SEC_E_OK || sspi_status == SEC_I_RENEGOTIATE ||
|
---|
2221 | sspi_status == SEC_I_CONTEXT_EXPIRED) {
|
---|
2222 | /* check for successfully decrypted data, even before actual
|
---|
2223 | renegotiation or shutdown of the connection context */
|
---|
2224 | if(inbuf[1].BufferType == SECBUFFER_DATA) {
|
---|
2225 | DEBUGF(infof(data, "schannel: decrypted data length: %lu",
|
---|
2226 | inbuf[1].cbBuffer));
|
---|
2227 |
|
---|
2228 | /* increase buffer in order to fit the received amount of data */
|
---|
2229 | size = inbuf[1].cbBuffer > CURL_SCHANNEL_BUFFER_FREE_SIZE ?
|
---|
2230 | inbuf[1].cbBuffer : CURL_SCHANNEL_BUFFER_FREE_SIZE;
|
---|
2231 | if(backend->decdata_length - backend->decdata_offset < size ||
|
---|
2232 | backend->decdata_length < len) {
|
---|
2233 | /* increase internal decrypted data buffer */
|
---|
2234 | reallocated_length = backend->decdata_offset + size;
|
---|
2235 | /* make sure that the requested amount of data fits */
|
---|
2236 | if(reallocated_length < len) {
|
---|
2237 | reallocated_length = len;
|
---|
2238 | }
|
---|
2239 | reallocated_buffer = realloc(backend->decdata_buffer,
|
---|
2240 | reallocated_length);
|
---|
2241 | if(!reallocated_buffer) {
|
---|
2242 | *err = CURLE_OUT_OF_MEMORY;
|
---|
2243 | failf(data, "schannel: unable to re-allocate memory");
|
---|
2244 | goto cleanup;
|
---|
2245 | }
|
---|
2246 | backend->decdata_buffer = reallocated_buffer;
|
---|
2247 | backend->decdata_length = reallocated_length;
|
---|
2248 | }
|
---|
2249 |
|
---|
2250 | /* copy decrypted data to internal buffer */
|
---|
2251 | size = inbuf[1].cbBuffer;
|
---|
2252 | if(size) {
|
---|
2253 | memcpy(backend->decdata_buffer + backend->decdata_offset,
|
---|
2254 | inbuf[1].pvBuffer, size);
|
---|
2255 | backend->decdata_offset += size;
|
---|
2256 | }
|
---|
2257 |
|
---|
2258 | DEBUGF(infof(data, "schannel: decrypted data added: %zu", size));
|
---|
2259 | DEBUGF(infof(data,
|
---|
2260 | "schannel: decrypted cached: offset %zu length %zu",
|
---|
2261 | backend->decdata_offset, backend->decdata_length));
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | /* check for remaining encrypted data */
|
---|
2265 | if(inbuf[3].BufferType == SECBUFFER_EXTRA && inbuf[3].cbBuffer > 0) {
|
---|
2266 | DEBUGF(infof(data, "schannel: encrypted data length: %lu",
|
---|
2267 | inbuf[3].cbBuffer));
|
---|
2268 |
|
---|
2269 | /* check if the remaining data is less than the total amount
|
---|
2270 | * and therefore begins after the already processed data
|
---|
2271 | */
|
---|
2272 | if(backend->encdata_offset > inbuf[3].cbBuffer) {
|
---|
2273 | /* move remaining encrypted data forward to the beginning of
|
---|
2274 | buffer */
|
---|
2275 | memmove(backend->encdata_buffer,
|
---|
2276 | (backend->encdata_buffer + backend->encdata_offset) -
|
---|
2277 | inbuf[3].cbBuffer, inbuf[3].cbBuffer);
|
---|
2278 | backend->encdata_offset = inbuf[3].cbBuffer;
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | DEBUGF(infof(data,
|
---|
2282 | "schannel: encrypted cached: offset %zu length %zu",
|
---|
2283 | backend->encdata_offset, backend->encdata_length));
|
---|
2284 | }
|
---|
2285 | else {
|
---|
2286 | /* reset encrypted buffer offset, because there is no data remaining */
|
---|
2287 | backend->encdata_offset = 0;
|
---|
2288 | }
|
---|
2289 |
|
---|
2290 | /* check if server wants to renegotiate the connection context */
|
---|
2291 | if(sspi_status == SEC_I_RENEGOTIATE) {
|
---|
2292 | infof(data, "schannel: remote party requests renegotiation");
|
---|
2293 | if(*err && *err != CURLE_AGAIN) {
|
---|
2294 | infof(data, "schannel: can't renegotiate, an error is pending");
|
---|
2295 | goto cleanup;
|
---|
2296 | }
|
---|
2297 |
|
---|
2298 | /* begin renegotiation */
|
---|
2299 | infof(data, "schannel: renegotiating SSL/TLS connection");
|
---|
2300 | connssl->state = ssl_connection_negotiating;
|
---|
2301 | connssl->connecting_state = ssl_connect_2_writing;
|
---|
2302 | backend->recv_renegotiating = true;
|
---|
2303 | *err = schannel_connect_common(cf, data, FALSE, &done);
|
---|
2304 | backend->recv_renegotiating = false;
|
---|
2305 | if(*err) {
|
---|
2306 | infof(data, "schannel: renegotiation failed");
|
---|
2307 | goto cleanup;
|
---|
2308 | }
|
---|
2309 | /* now retry receiving data */
|
---|
2310 | sspi_status = SEC_E_OK;
|
---|
2311 | infof(data, "schannel: SSL/TLS connection renegotiated");
|
---|
2312 | continue;
|
---|
2313 | }
|
---|
2314 | /* check if the server closed the connection */
|
---|
2315 | else if(sspi_status == SEC_I_CONTEXT_EXPIRED) {
|
---|
2316 | /* In Windows 2000 SEC_I_CONTEXT_EXPIRED (close_notify) is not
|
---|
2317 | returned so we have to work around that in cleanup. */
|
---|
2318 | backend->recv_sspi_close_notify = true;
|
---|
2319 | if(!backend->recv_connection_closed)
|
---|
2320 | backend->recv_connection_closed = true;
|
---|
2321 | infof(data,
|
---|
2322 | "schannel: server close notification received (close_notify)");
|
---|
2323 | goto cleanup;
|
---|
2324 | }
|
---|
2325 | }
|
---|
2326 | else if(sspi_status == SEC_E_INCOMPLETE_MESSAGE) {
|
---|
2327 | backend->encdata_is_incomplete = true;
|
---|
2328 | if(!*err)
|
---|
2329 | *err = CURLE_AGAIN;
|
---|
2330 | infof(data, "schannel: failed to decrypt data, need more data");
|
---|
2331 | goto cleanup;
|
---|
2332 | }
|
---|
2333 | else {
|
---|
2334 | #ifndef CURL_DISABLE_VERBOSE_STRINGS
|
---|
2335 | char buffer[STRERROR_LEN];
|
---|
2336 | infof(data, "schannel: failed to read data from server: %s",
|
---|
2337 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
2338 | #endif
|
---|
2339 | *err = CURLE_RECV_ERROR;
|
---|
2340 | goto cleanup;
|
---|
2341 | }
|
---|
2342 | }
|
---|
2343 |
|
---|
2344 | DEBUGF(infof(data,
|
---|
2345 | "schannel: encrypted data buffer: offset %zu length %zu",
|
---|
2346 | backend->encdata_offset, backend->encdata_length));
|
---|
2347 |
|
---|
2348 | DEBUGF(infof(data,
|
---|
2349 | "schannel: decrypted data buffer: offset %zu length %zu",
|
---|
2350 | backend->decdata_offset, backend->decdata_length));
|
---|
2351 |
|
---|
2352 | cleanup:
|
---|
2353 | /* Warning- there is no guarantee the encdata state is valid at this point */
|
---|
2354 | DEBUGF(infof(data, "schannel: schannel_recv cleanup"));
|
---|
2355 |
|
---|
2356 | /* Error if the connection has closed without a close_notify.
|
---|
2357 |
|
---|
2358 | The behavior here is a matter of debate. We don't want to be vulnerable
|
---|
2359 | to a truncation attack however there's some browser precedent for
|
---|
2360 | ignoring the close_notify for compatibility reasons.
|
---|
2361 |
|
---|
2362 | Additionally, Windows 2000 (v5.0) is a special case since it seems it
|
---|
2363 | doesn't return close_notify. In that case if the connection was closed we
|
---|
2364 | assume it was graceful (close_notify) since there doesn't seem to be a
|
---|
2365 | way to tell.
|
---|
2366 | */
|
---|
2367 | if(len && !backend->decdata_offset && backend->recv_connection_closed &&
|
---|
2368 | !backend->recv_sspi_close_notify) {
|
---|
2369 | bool isWin2k = curlx_verify_windows_version(5, 0, 0, PLATFORM_WINNT,
|
---|
2370 | VERSION_EQUAL);
|
---|
2371 |
|
---|
2372 | if(isWin2k && sspi_status == SEC_E_OK)
|
---|
2373 | backend->recv_sspi_close_notify = true;
|
---|
2374 | else {
|
---|
2375 | *err = CURLE_RECV_ERROR;
|
---|
2376 | infof(data, "schannel: server closed abruptly (missing close_notify)");
|
---|
2377 | }
|
---|
2378 | }
|
---|
2379 |
|
---|
2380 | /* Any error other than CURLE_AGAIN is an unrecoverable error. */
|
---|
2381 | if(*err && *err != CURLE_AGAIN)
|
---|
2382 | backend->recv_unrecoverable_err = *err;
|
---|
2383 |
|
---|
2384 | size = len < backend->decdata_offset ? len : backend->decdata_offset;
|
---|
2385 | if(size) {
|
---|
2386 | memcpy(buf, backend->decdata_buffer, size);
|
---|
2387 | memmove(backend->decdata_buffer, backend->decdata_buffer + size,
|
---|
2388 | backend->decdata_offset - size);
|
---|
2389 | backend->decdata_offset -= size;
|
---|
2390 | DEBUGF(infof(data, "schannel: decrypted data returned %zu", size));
|
---|
2391 | DEBUGF(infof(data,
|
---|
2392 | "schannel: decrypted data buffer: offset %zu length %zu",
|
---|
2393 | backend->decdata_offset, backend->decdata_length));
|
---|
2394 | *err = CURLE_OK;
|
---|
2395 | return (ssize_t)size;
|
---|
2396 | }
|
---|
2397 |
|
---|
2398 | if(!*err && !backend->recv_connection_closed)
|
---|
2399 | *err = CURLE_AGAIN;
|
---|
2400 |
|
---|
2401 | /* It's debatable what to return when !len. We could return whatever error
|
---|
2402 | we got from decryption but instead we override here so the return is
|
---|
2403 | consistent.
|
---|
2404 | */
|
---|
2405 | if(!len)
|
---|
2406 | *err = CURLE_OK;
|
---|
2407 |
|
---|
2408 | return *err ? -1 : 0;
|
---|
2409 | }
|
---|
2410 |
|
---|
2411 | static CURLcode schannel_connect_nonblocking(struct Curl_cfilter *cf,
|
---|
2412 | struct Curl_easy *data,
|
---|
2413 | bool *done)
|
---|
2414 | {
|
---|
2415 | return schannel_connect_common(cf, data, TRUE, done);
|
---|
2416 | }
|
---|
2417 |
|
---|
2418 | static CURLcode schannel_connect(struct Curl_cfilter *cf,
|
---|
2419 | struct Curl_easy *data)
|
---|
2420 | {
|
---|
2421 | CURLcode result;
|
---|
2422 | bool done = FALSE;
|
---|
2423 |
|
---|
2424 | result = schannel_connect_common(cf, data, FALSE, &done);
|
---|
2425 | if(result)
|
---|
2426 | return result;
|
---|
2427 |
|
---|
2428 | DEBUGASSERT(done);
|
---|
2429 |
|
---|
2430 | return CURLE_OK;
|
---|
2431 | }
|
---|
2432 |
|
---|
2433 | static bool schannel_data_pending(struct Curl_cfilter *cf,
|
---|
2434 | const struct Curl_easy *data)
|
---|
2435 | {
|
---|
2436 | const struct ssl_connect_data *connssl = cf->ctx;
|
---|
2437 | struct schannel_ssl_backend_data *backend =
|
---|
2438 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
2439 |
|
---|
2440 | (void)data;
|
---|
2441 | DEBUGASSERT(backend);
|
---|
2442 |
|
---|
2443 | if(backend->ctxt) /* SSL/TLS is in use */
|
---|
2444 | return (backend->decdata_offset > 0 ||
|
---|
2445 | (backend->encdata_offset > 0 && !backend->encdata_is_incomplete) ||
|
---|
2446 | backend->recv_connection_closed ||
|
---|
2447 | backend->recv_sspi_close_notify ||
|
---|
2448 | backend->recv_unrecoverable_err);
|
---|
2449 | else
|
---|
2450 | return FALSE;
|
---|
2451 | }
|
---|
2452 |
|
---|
2453 | static void schannel_session_free(void *ptr)
|
---|
2454 | {
|
---|
2455 | /* this is expected to be called under sessionid lock */
|
---|
2456 | struct Curl_schannel_cred *cred = ptr;
|
---|
2457 |
|
---|
2458 | if(cred) {
|
---|
2459 | cred->refcount--;
|
---|
2460 | if(cred->refcount == 0) {
|
---|
2461 | s_pSecFn->FreeCredentialsHandle(&cred->cred_handle);
|
---|
2462 | curlx_unicodefree(cred->sni_hostname);
|
---|
2463 | #ifdef HAS_CLIENT_CERT_PATH
|
---|
2464 | if(cred->client_cert_store) {
|
---|
2465 | CertCloseStore(cred->client_cert_store, 0);
|
---|
2466 | cred->client_cert_store = NULL;
|
---|
2467 | }
|
---|
2468 | #endif
|
---|
2469 | Curl_safefree(cred);
|
---|
2470 | }
|
---|
2471 | }
|
---|
2472 | }
|
---|
2473 |
|
---|
2474 | /* shut down the SSL connection and clean up related memory.
|
---|
2475 | this function can be called multiple times on the same connection including
|
---|
2476 | if the SSL connection failed (eg connection made but failed handshake). */
|
---|
2477 | static int schannel_shutdown(struct Curl_cfilter *cf,
|
---|
2478 | struct Curl_easy *data)
|
---|
2479 | {
|
---|
2480 | /* See https://msdn.microsoft.com/en-us/library/windows/desktop/aa380138.aspx
|
---|
2481 | * Shutting Down an Schannel Connection
|
---|
2482 | */
|
---|
2483 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
2484 | struct schannel_ssl_backend_data *backend =
|
---|
2485 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
2486 |
|
---|
2487 | DEBUGASSERT(data);
|
---|
2488 | DEBUGASSERT(backend);
|
---|
2489 |
|
---|
2490 | if(backend->ctxt) {
|
---|
2491 | infof(data, "schannel: shutting down SSL/TLS connection with %s port %d",
|
---|
2492 | connssl->peer.hostname, connssl->port);
|
---|
2493 | }
|
---|
2494 |
|
---|
2495 | if(backend->cred && backend->ctxt) {
|
---|
2496 | SecBufferDesc BuffDesc;
|
---|
2497 | SecBuffer Buffer;
|
---|
2498 | SECURITY_STATUS sspi_status;
|
---|
2499 | SecBuffer outbuf;
|
---|
2500 | SecBufferDesc outbuf_desc;
|
---|
2501 | CURLcode result;
|
---|
2502 | DWORD dwshut = SCHANNEL_SHUTDOWN;
|
---|
2503 |
|
---|
2504 | InitSecBuffer(&Buffer, SECBUFFER_TOKEN, &dwshut, sizeof(dwshut));
|
---|
2505 | InitSecBufferDesc(&BuffDesc, &Buffer, 1);
|
---|
2506 |
|
---|
2507 | sspi_status = s_pSecFn->ApplyControlToken(&backend->ctxt->ctxt_handle,
|
---|
2508 | &BuffDesc);
|
---|
2509 |
|
---|
2510 | if(sspi_status != SEC_E_OK) {
|
---|
2511 | char buffer[STRERROR_LEN];
|
---|
2512 | failf(data, "schannel: ApplyControlToken failure: %s",
|
---|
2513 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
2514 | }
|
---|
2515 |
|
---|
2516 | /* setup output buffer */
|
---|
2517 | InitSecBuffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
|
---|
2518 | InitSecBufferDesc(&outbuf_desc, &outbuf, 1);
|
---|
2519 |
|
---|
2520 | sspi_status = s_pSecFn->InitializeSecurityContext(
|
---|
2521 | &backend->cred->cred_handle,
|
---|
2522 | &backend->ctxt->ctxt_handle,
|
---|
2523 | backend->cred->sni_hostname,
|
---|
2524 | backend->req_flags,
|
---|
2525 | 0,
|
---|
2526 | 0,
|
---|
2527 | NULL,
|
---|
2528 | 0,
|
---|
2529 | &backend->ctxt->ctxt_handle,
|
---|
2530 | &outbuf_desc,
|
---|
2531 | &backend->ret_flags,
|
---|
2532 | &backend->ctxt->time_stamp);
|
---|
2533 |
|
---|
2534 | if((sspi_status == SEC_E_OK) || (sspi_status == SEC_I_CONTEXT_EXPIRED)) {
|
---|
2535 | /* send close message which is in output buffer */
|
---|
2536 | ssize_t written = Curl_conn_cf_send(cf->next, data,
|
---|
2537 | outbuf.pvBuffer, outbuf.cbBuffer,
|
---|
2538 | &result);
|
---|
2539 | s_pSecFn->FreeContextBuffer(outbuf.pvBuffer);
|
---|
2540 | if((result != CURLE_OK) || (outbuf.cbBuffer != (size_t) written)) {
|
---|
2541 | infof(data, "schannel: failed to send close msg: %s"
|
---|
2542 | " (bytes written: %zd)", curl_easy_strerror(result), written);
|
---|
2543 | }
|
---|
2544 | }
|
---|
2545 | }
|
---|
2546 |
|
---|
2547 | /* free SSPI Schannel API security context handle */
|
---|
2548 | if(backend->ctxt) {
|
---|
2549 | DEBUGF(infof(data, "schannel: clear security context handle"));
|
---|
2550 | s_pSecFn->DeleteSecurityContext(&backend->ctxt->ctxt_handle);
|
---|
2551 | Curl_safefree(backend->ctxt);
|
---|
2552 | }
|
---|
2553 |
|
---|
2554 | /* free SSPI Schannel API credential handle */
|
---|
2555 | if(backend->cred) {
|
---|
2556 | Curl_ssl_sessionid_lock(data);
|
---|
2557 | schannel_session_free(backend->cred);
|
---|
2558 | Curl_ssl_sessionid_unlock(data);
|
---|
2559 | backend->cred = NULL;
|
---|
2560 | }
|
---|
2561 |
|
---|
2562 | /* free internal buffer for received encrypted data */
|
---|
2563 | if(backend->encdata_buffer) {
|
---|
2564 | Curl_safefree(backend->encdata_buffer);
|
---|
2565 | backend->encdata_length = 0;
|
---|
2566 | backend->encdata_offset = 0;
|
---|
2567 | backend->encdata_is_incomplete = false;
|
---|
2568 | }
|
---|
2569 |
|
---|
2570 | /* free internal buffer for received decrypted data */
|
---|
2571 | if(backend->decdata_buffer) {
|
---|
2572 | Curl_safefree(backend->decdata_buffer);
|
---|
2573 | backend->decdata_length = 0;
|
---|
2574 | backend->decdata_offset = 0;
|
---|
2575 | }
|
---|
2576 |
|
---|
2577 | return CURLE_OK;
|
---|
2578 | }
|
---|
2579 |
|
---|
2580 | static void schannel_close(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
2581 | {
|
---|
2582 | schannel_shutdown(cf, data);
|
---|
2583 | }
|
---|
2584 |
|
---|
2585 | static int schannel_init(void)
|
---|
2586 | {
|
---|
2587 | return (Curl_sspi_global_init() == CURLE_OK ? 1 : 0);
|
---|
2588 | }
|
---|
2589 |
|
---|
2590 | static void schannel_cleanup(void)
|
---|
2591 | {
|
---|
2592 | Curl_sspi_global_cleanup();
|
---|
2593 | }
|
---|
2594 |
|
---|
2595 | static size_t schannel_version(char *buffer, size_t size)
|
---|
2596 | {
|
---|
2597 | size = msnprintf(buffer, size, "Schannel");
|
---|
2598 |
|
---|
2599 | return size;
|
---|
2600 | }
|
---|
2601 |
|
---|
2602 | static CURLcode schannel_random(struct Curl_easy *data UNUSED_PARAM,
|
---|
2603 | unsigned char *entropy, size_t length)
|
---|
2604 | {
|
---|
2605 | (void)data;
|
---|
2606 |
|
---|
2607 | return Curl_win32_random(entropy, length);
|
---|
2608 | }
|
---|
2609 |
|
---|
2610 | static CURLcode schannel_pkp_pin_peer_pubkey(struct Curl_cfilter *cf,
|
---|
2611 | struct Curl_easy *data,
|
---|
2612 | const char *pinnedpubkey)
|
---|
2613 | {
|
---|
2614 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
2615 | struct schannel_ssl_backend_data *backend =
|
---|
2616 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
2617 | CERT_CONTEXT *pCertContextServer = NULL;
|
---|
2618 |
|
---|
2619 | /* Result is returned to caller */
|
---|
2620 | CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
---|
2621 |
|
---|
2622 | DEBUGASSERT(backend);
|
---|
2623 |
|
---|
2624 | /* if a path wasn't specified, don't pin */
|
---|
2625 | if(!pinnedpubkey)
|
---|
2626 | return CURLE_OK;
|
---|
2627 |
|
---|
2628 | do {
|
---|
2629 | SECURITY_STATUS sspi_status;
|
---|
2630 | const char *x509_der;
|
---|
2631 | DWORD x509_der_len;
|
---|
2632 | struct Curl_X509certificate x509_parsed;
|
---|
2633 | struct Curl_asn1Element *pubkey;
|
---|
2634 |
|
---|
2635 | sspi_status =
|
---|
2636 | s_pSecFn->QueryContextAttributes(&backend->ctxt->ctxt_handle,
|
---|
2637 | SECPKG_ATTR_REMOTE_CERT_CONTEXT,
|
---|
2638 | &pCertContextServer);
|
---|
2639 |
|
---|
2640 | if((sspi_status != SEC_E_OK) || !pCertContextServer) {
|
---|
2641 | char buffer[STRERROR_LEN];
|
---|
2642 | failf(data, "schannel: Failed to read remote certificate context: %s",
|
---|
2643 | Curl_sspi_strerror(sspi_status, buffer, sizeof(buffer)));
|
---|
2644 | break; /* failed */
|
---|
2645 | }
|
---|
2646 |
|
---|
2647 |
|
---|
2648 | if(!(((pCertContextServer->dwCertEncodingType & X509_ASN_ENCODING) != 0) &&
|
---|
2649 | (pCertContextServer->cbCertEncoded > 0)))
|
---|
2650 | break;
|
---|
2651 |
|
---|
2652 | x509_der = (const char *)pCertContextServer->pbCertEncoded;
|
---|
2653 | x509_der_len = pCertContextServer->cbCertEncoded;
|
---|
2654 | memset(&x509_parsed, 0, sizeof(x509_parsed));
|
---|
2655 | if(Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len))
|
---|
2656 | break;
|
---|
2657 |
|
---|
2658 | pubkey = &x509_parsed.subjectPublicKeyInfo;
|
---|
2659 | if(!pubkey->header || pubkey->end <= pubkey->header) {
|
---|
2660 | failf(data, "SSL: failed retrieving public key from server certificate");
|
---|
2661 | break;
|
---|
2662 | }
|
---|
2663 |
|
---|
2664 | result = Curl_pin_peer_pubkey(data,
|
---|
2665 | pinnedpubkey,
|
---|
2666 | (const unsigned char *)pubkey->header,
|
---|
2667 | (size_t)(pubkey->end - pubkey->header));
|
---|
2668 | if(result) {
|
---|
2669 | failf(data, "SSL: public key does not match pinned public key");
|
---|
2670 | }
|
---|
2671 | } while(0);
|
---|
2672 |
|
---|
2673 | if(pCertContextServer)
|
---|
2674 | CertFreeCertificateContext(pCertContextServer);
|
---|
2675 |
|
---|
2676 | return result;
|
---|
2677 | }
|
---|
2678 |
|
---|
2679 | static void schannel_checksum(const unsigned char *input,
|
---|
2680 | size_t inputlen,
|
---|
2681 | unsigned char *checksum,
|
---|
2682 | size_t checksumlen,
|
---|
2683 | DWORD provType,
|
---|
2684 | const unsigned int algId)
|
---|
2685 | {
|
---|
2686 | HCRYPTPROV hProv = 0;
|
---|
2687 | HCRYPTHASH hHash = 0;
|
---|
2688 | DWORD cbHashSize = 0;
|
---|
2689 | DWORD dwHashSizeLen = (DWORD)sizeof(cbHashSize);
|
---|
2690 | DWORD dwChecksumLen = (DWORD)checksumlen;
|
---|
2691 |
|
---|
2692 | /* since this can fail in multiple ways, zero memory first so we never
|
---|
2693 | * return old data
|
---|
2694 | */
|
---|
2695 | memset(checksum, 0, checksumlen);
|
---|
2696 |
|
---|
2697 | if(!CryptAcquireContext(&hProv, NULL, NULL, provType,
|
---|
2698 | CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
|
---|
2699 | return; /* failed */
|
---|
2700 |
|
---|
2701 | do {
|
---|
2702 | if(!CryptCreateHash(hProv, algId, 0, 0, &hHash))
|
---|
2703 | break; /* failed */
|
---|
2704 |
|
---|
2705 | if(!CryptHashData(hHash, input, (DWORD)inputlen, 0))
|
---|
2706 | break; /* failed */
|
---|
2707 |
|
---|
2708 | /* get hash size */
|
---|
2709 | if(!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *)&cbHashSize,
|
---|
2710 | &dwHashSizeLen, 0))
|
---|
2711 | break; /* failed */
|
---|
2712 |
|
---|
2713 | /* check hash size */
|
---|
2714 | if(checksumlen < cbHashSize)
|
---|
2715 | break; /* failed */
|
---|
2716 |
|
---|
2717 | if(CryptGetHashParam(hHash, HP_HASHVAL, checksum, &dwChecksumLen, 0))
|
---|
2718 | break; /* failed */
|
---|
2719 | } while(0);
|
---|
2720 |
|
---|
2721 | if(hHash)
|
---|
2722 | CryptDestroyHash(hHash);
|
---|
2723 |
|
---|
2724 | if(hProv)
|
---|
2725 | CryptReleaseContext(hProv, 0);
|
---|
2726 | }
|
---|
2727 |
|
---|
2728 | static CURLcode schannel_sha256sum(const unsigned char *input,
|
---|
2729 | size_t inputlen,
|
---|
2730 | unsigned char *sha256sum,
|
---|
2731 | size_t sha256len)
|
---|
2732 | {
|
---|
2733 | schannel_checksum(input, inputlen, sha256sum, sha256len,
|
---|
2734 | PROV_RSA_AES, CALG_SHA_256);
|
---|
2735 | return CURLE_OK;
|
---|
2736 | }
|
---|
2737 |
|
---|
2738 | static void *schannel_get_internals(struct ssl_connect_data *connssl,
|
---|
2739 | CURLINFO info UNUSED_PARAM)
|
---|
2740 | {
|
---|
2741 | struct schannel_ssl_backend_data *backend =
|
---|
2742 | (struct schannel_ssl_backend_data *)connssl->backend;
|
---|
2743 | (void)info;
|
---|
2744 | DEBUGASSERT(backend);
|
---|
2745 | return &backend->ctxt->ctxt_handle;
|
---|
2746 | }
|
---|
2747 |
|
---|
2748 | HCERTSTORE Curl_schannel_get_cached_cert_store(struct Curl_cfilter *cf,
|
---|
2749 | const struct Curl_easy *data)
|
---|
2750 | {
|
---|
2751 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
2752 | struct Curl_multi *multi = data->multi_easy ? data->multi_easy : data->multi;
|
---|
2753 | const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
|
---|
2754 | struct schannel_multi_ssl_backend_data *mbackend;
|
---|
2755 | const struct ssl_general_config *cfg = &data->set.general_ssl;
|
---|
2756 | timediff_t timeout_ms;
|
---|
2757 | timediff_t elapsed_ms;
|
---|
2758 | struct curltime now;
|
---|
2759 | unsigned char info_blob_digest[CURL_SHA256_DIGEST_LENGTH];
|
---|
2760 |
|
---|
2761 | DEBUGASSERT(multi);
|
---|
2762 |
|
---|
2763 | if(!multi || !multi->ssl_backend_data) {
|
---|
2764 | return NULL;
|
---|
2765 | }
|
---|
2766 |
|
---|
2767 | mbackend = (struct schannel_multi_ssl_backend_data *)multi->ssl_backend_data;
|
---|
2768 | if(!mbackend->cert_store) {
|
---|
2769 | return NULL;
|
---|
2770 | }
|
---|
2771 |
|
---|
2772 | /* zero ca_cache_timeout completely disables caching */
|
---|
2773 | if(!cfg->ca_cache_timeout) {
|
---|
2774 | return NULL;
|
---|
2775 | }
|
---|
2776 |
|
---|
2777 | /* check for cache timeout by using the cached_x509_store_expired timediff
|
---|
2778 | calculation pattern from openssl.c.
|
---|
2779 | negative timeout means retain forever. */
|
---|
2780 | timeout_ms = cfg->ca_cache_timeout * (timediff_t)1000;
|
---|
2781 | if(timeout_ms >= 0) {
|
---|
2782 | now = Curl_now();
|
---|
2783 | elapsed_ms = Curl_timediff(now, mbackend->time);
|
---|
2784 | if(elapsed_ms >= timeout_ms) {
|
---|
2785 | return NULL;
|
---|
2786 | }
|
---|
2787 | }
|
---|
2788 |
|
---|
2789 | if(ca_info_blob) {
|
---|
2790 | if(!mbackend->CAinfo_blob_digest) {
|
---|
2791 | return NULL;
|
---|
2792 | }
|
---|
2793 | if(mbackend->CAinfo_blob_size != ca_info_blob->len) {
|
---|
2794 | return NULL;
|
---|
2795 | }
|
---|
2796 | schannel_sha256sum((const unsigned char *)ca_info_blob->data,
|
---|
2797 | ca_info_blob->len,
|
---|
2798 | info_blob_digest,
|
---|
2799 | CURL_SHA256_DIGEST_LENGTH);
|
---|
2800 | if(memcmp(mbackend->CAinfo_blob_digest,
|
---|
2801 | info_blob_digest,
|
---|
2802 | CURL_SHA256_DIGEST_LENGTH)) {
|
---|
2803 | return NULL;
|
---|
2804 | }
|
---|
2805 | }
|
---|
2806 | else {
|
---|
2807 | if(!conn_config->CAfile || !mbackend->CAfile ||
|
---|
2808 | strcmp(mbackend->CAfile, conn_config->CAfile)) {
|
---|
2809 | return NULL;
|
---|
2810 | }
|
---|
2811 | }
|
---|
2812 |
|
---|
2813 | return mbackend->cert_store;
|
---|
2814 | }
|
---|
2815 |
|
---|
2816 | bool Curl_schannel_set_cached_cert_store(struct Curl_cfilter *cf,
|
---|
2817 | const struct Curl_easy *data,
|
---|
2818 | HCERTSTORE cert_store)
|
---|
2819 | {
|
---|
2820 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
2821 | struct Curl_multi *multi = data->multi_easy ? data->multi_easy : data->multi;
|
---|
2822 | const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
|
---|
2823 | struct schannel_multi_ssl_backend_data *mbackend;
|
---|
2824 | unsigned char *CAinfo_blob_digest = NULL;
|
---|
2825 | size_t CAinfo_blob_size = 0;
|
---|
2826 | char *CAfile = NULL;
|
---|
2827 |
|
---|
2828 | DEBUGASSERT(multi);
|
---|
2829 |
|
---|
2830 | if(!multi) {
|
---|
2831 | return false;
|
---|
2832 | }
|
---|
2833 |
|
---|
2834 | if(!multi->ssl_backend_data) {
|
---|
2835 | multi->ssl_backend_data =
|
---|
2836 | calloc(1, sizeof(struct schannel_multi_ssl_backend_data));
|
---|
2837 | if(!multi->ssl_backend_data) {
|
---|
2838 | return false;
|
---|
2839 | }
|
---|
2840 | }
|
---|
2841 |
|
---|
2842 | mbackend = (struct schannel_multi_ssl_backend_data *)multi->ssl_backend_data;
|
---|
2843 |
|
---|
2844 |
|
---|
2845 | if(ca_info_blob) {
|
---|
2846 | CAinfo_blob_digest = malloc(CURL_SHA256_DIGEST_LENGTH);
|
---|
2847 | if(!CAinfo_blob_digest) {
|
---|
2848 | return false;
|
---|
2849 | }
|
---|
2850 | schannel_sha256sum((const unsigned char *)ca_info_blob->data,
|
---|
2851 | ca_info_blob->len,
|
---|
2852 | CAinfo_blob_digest,
|
---|
2853 | CURL_SHA256_DIGEST_LENGTH);
|
---|
2854 | CAinfo_blob_size = ca_info_blob->len;
|
---|
2855 | }
|
---|
2856 | else {
|
---|
2857 | if(conn_config->CAfile) {
|
---|
2858 | CAfile = strdup(conn_config->CAfile);
|
---|
2859 | if(!CAfile) {
|
---|
2860 | return false;
|
---|
2861 | }
|
---|
2862 | }
|
---|
2863 | }
|
---|
2864 |
|
---|
2865 | /* free old cache data */
|
---|
2866 | if(mbackend->cert_store) {
|
---|
2867 | CertCloseStore(mbackend->cert_store, 0);
|
---|
2868 | }
|
---|
2869 | free(mbackend->CAinfo_blob_digest);
|
---|
2870 | free(mbackend->CAfile);
|
---|
2871 |
|
---|
2872 | mbackend->time = Curl_now();
|
---|
2873 | mbackend->cert_store = cert_store;
|
---|
2874 | mbackend->CAinfo_blob_digest = CAinfo_blob_digest;
|
---|
2875 | mbackend->CAinfo_blob_size = CAinfo_blob_size;
|
---|
2876 | mbackend->CAfile = CAfile;
|
---|
2877 | return true;
|
---|
2878 | }
|
---|
2879 |
|
---|
2880 | static void schannel_free_multi_ssl_backend_data(
|
---|
2881 | struct multi_ssl_backend_data *msbd)
|
---|
2882 | {
|
---|
2883 | struct schannel_multi_ssl_backend_data *mbackend =
|
---|
2884 | (struct schannel_multi_ssl_backend_data*)msbd;
|
---|
2885 | if(mbackend->cert_store) {
|
---|
2886 | CertCloseStore(mbackend->cert_store, 0);
|
---|
2887 | }
|
---|
2888 | free(mbackend->CAinfo_blob_digest);
|
---|
2889 | free(mbackend->CAfile);
|
---|
2890 | free(mbackend);
|
---|
2891 | }
|
---|
2892 |
|
---|
2893 | const struct Curl_ssl Curl_ssl_schannel = {
|
---|
2894 | { CURLSSLBACKEND_SCHANNEL, "schannel" }, /* info */
|
---|
2895 |
|
---|
2896 | SSLSUPP_CERTINFO |
|
---|
2897 | #ifdef HAS_MANUAL_VERIFY_API
|
---|
2898 | SSLSUPP_CAINFO_BLOB |
|
---|
2899 | #endif
|
---|
2900 | SSLSUPP_PINNEDPUBKEY |
|
---|
2901 | SSLSUPP_TLS13_CIPHERSUITES |
|
---|
2902 | SSLSUPP_HTTPS_PROXY,
|
---|
2903 |
|
---|
2904 | sizeof(struct schannel_ssl_backend_data),
|
---|
2905 |
|
---|
2906 | schannel_init, /* init */
|
---|
2907 | schannel_cleanup, /* cleanup */
|
---|
2908 | schannel_version, /* version */
|
---|
2909 | Curl_none_check_cxn, /* check_cxn */
|
---|
2910 | schannel_shutdown, /* shutdown */
|
---|
2911 | schannel_data_pending, /* data_pending */
|
---|
2912 | schannel_random, /* random */
|
---|
2913 | Curl_none_cert_status_request, /* cert_status_request */
|
---|
2914 | schannel_connect, /* connect */
|
---|
2915 | schannel_connect_nonblocking, /* connect_nonblocking */
|
---|
2916 | Curl_ssl_adjust_pollset, /* adjust_pollset */
|
---|
2917 | schannel_get_internals, /* get_internals */
|
---|
2918 | schannel_close, /* close_one */
|
---|
2919 | Curl_none_close_all, /* close_all */
|
---|
2920 | schannel_session_free, /* session_free */
|
---|
2921 | Curl_none_set_engine, /* set_engine */
|
---|
2922 | Curl_none_set_engine_default, /* set_engine_default */
|
---|
2923 | Curl_none_engines_list, /* engines_list */
|
---|
2924 | Curl_none_false_start, /* false_start */
|
---|
2925 | schannel_sha256sum, /* sha256sum */
|
---|
2926 | NULL, /* associate_connection */
|
---|
2927 | NULL, /* disassociate_connection */
|
---|
2928 | schannel_free_multi_ssl_backend_data, /* free_multi_ssl_backend_data */
|
---|
2929 | schannel_recv, /* recv decrypted data */
|
---|
2930 | schannel_send, /* send data to encrypt */
|
---|
2931 | };
|
---|
2932 |
|
---|
2933 | #endif /* USE_SCHANNEL */
|
---|