1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
|
---|
9 | *
|
---|
10 | * This software is licensed as described in the file COPYING, which
|
---|
11 | * you should have received as part of this distribution. The terms
|
---|
12 | * are also available at https://curl.se/docs/copyright.html.
|
---|
13 | *
|
---|
14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
---|
15 | * copies of the Software, and permit persons to whom the Software is
|
---|
16 | * furnished to do so, under the terms of the COPYING file.
|
---|
17 | *
|
---|
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
19 | * KIND, either express or implied.
|
---|
20 | *
|
---|
21 | * SPDX-License-Identifier: curl
|
---|
22 | *
|
---|
23 | ***************************************************************************/
|
---|
24 |
|
---|
25 | /* This file is for implementing all "generic" SSL functions that all libcurl
|
---|
26 | internals should use. It is then responsible for calling the proper
|
---|
27 | "backend" function.
|
---|
28 |
|
---|
29 | SSL-functions in libcurl should call functions in this source file, and not
|
---|
30 | to any specific SSL-layer.
|
---|
31 |
|
---|
32 | Curl_ssl_ - prefix for generic ones
|
---|
33 |
|
---|
34 | Note that this source code uses the functions of the configured SSL
|
---|
35 | backend via the global Curl_ssl instance.
|
---|
36 |
|
---|
37 | "SSL/TLS Strong Encryption: An Introduction"
|
---|
38 | https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html
|
---|
39 | */
|
---|
40 |
|
---|
41 | #include "curl_setup.h"
|
---|
42 |
|
---|
43 | #ifdef HAVE_SYS_TYPES_H
|
---|
44 | #include <sys/types.h>
|
---|
45 | #endif
|
---|
46 | #ifdef HAVE_SYS_STAT_H
|
---|
47 | #include <sys/stat.h>
|
---|
48 | #endif
|
---|
49 | #ifdef HAVE_FCNTL_H
|
---|
50 | #include <fcntl.h>
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #include "urldata.h"
|
---|
54 | #include "cfilters.h"
|
---|
55 |
|
---|
56 | #include "vtls.h" /* generic SSL protos etc */
|
---|
57 | #include "vtls_int.h"
|
---|
58 | #include "slist.h"
|
---|
59 | #include "sendf.h"
|
---|
60 | #include "strcase.h"
|
---|
61 | #include "url.h"
|
---|
62 | #include "progress.h"
|
---|
63 | #include "share.h"
|
---|
64 | #include "multiif.h"
|
---|
65 | #include "timeval.h"
|
---|
66 | #include "curl_md5.h"
|
---|
67 | #include "warnless.h"
|
---|
68 | #include "curl_base64.h"
|
---|
69 | #include "curl_printf.h"
|
---|
70 | #include "strdup.h"
|
---|
71 |
|
---|
72 | /* The last #include files should be: */
|
---|
73 | #include "curl_memory.h"
|
---|
74 | #include "memdebug.h"
|
---|
75 |
|
---|
76 | /* convenience macro to check if this handle is using a shared SSL session */
|
---|
77 | #define SSLSESSION_SHARED(data) (data->share && \
|
---|
78 | (data->share->specifier & \
|
---|
79 | (1<<CURL_LOCK_DATA_SSL_SESSION)))
|
---|
80 |
|
---|
81 | #define CLONE_STRING(var) \
|
---|
82 | do { \
|
---|
83 | if(source->var) { \
|
---|
84 | dest->var = strdup(source->var); \
|
---|
85 | if(!dest->var) \
|
---|
86 | return FALSE; \
|
---|
87 | } \
|
---|
88 | else \
|
---|
89 | dest->var = NULL; \
|
---|
90 | } while(0)
|
---|
91 |
|
---|
92 | #define CLONE_BLOB(var) \
|
---|
93 | do { \
|
---|
94 | if(blobdup(&dest->var, source->var)) \
|
---|
95 | return FALSE; \
|
---|
96 | } while(0)
|
---|
97 |
|
---|
98 | static CURLcode blobdup(struct curl_blob **dest,
|
---|
99 | struct curl_blob *src)
|
---|
100 | {
|
---|
101 | DEBUGASSERT(dest);
|
---|
102 | DEBUGASSERT(!*dest);
|
---|
103 | if(src) {
|
---|
104 | /* only if there's data to dupe! */
|
---|
105 | struct curl_blob *d;
|
---|
106 | d = malloc(sizeof(struct curl_blob) + src->len);
|
---|
107 | if(!d)
|
---|
108 | return CURLE_OUT_OF_MEMORY;
|
---|
109 | d->len = src->len;
|
---|
110 | /* Always duplicate because the connection may survive longer than the
|
---|
111 | handle that passed in the blob. */
|
---|
112 | d->flags = CURL_BLOB_COPY;
|
---|
113 | d->data = (void *)((char *)d + sizeof(struct curl_blob));
|
---|
114 | memcpy(d->data, src->data, src->len);
|
---|
115 | *dest = d;
|
---|
116 | }
|
---|
117 | return CURLE_OK;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /* returns TRUE if the blobs are identical */
|
---|
121 | static bool blobcmp(struct curl_blob *first, struct curl_blob *second)
|
---|
122 | {
|
---|
123 | if(!first && !second) /* both are NULL */
|
---|
124 | return TRUE;
|
---|
125 | if(!first || !second) /* one is NULL */
|
---|
126 | return FALSE;
|
---|
127 | if(first->len != second->len) /* different sizes */
|
---|
128 | return FALSE;
|
---|
129 | return !memcmp(first->data, second->data, first->len); /* same data */
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | bool
|
---|
134 | Curl_ssl_config_matches(struct ssl_primary_config *data,
|
---|
135 | struct ssl_primary_config *needle)
|
---|
136 | {
|
---|
137 | if((data->version == needle->version) &&
|
---|
138 | (data->version_max == needle->version_max) &&
|
---|
139 | (data->ssl_options == needle->ssl_options) &&
|
---|
140 | (data->verifypeer == needle->verifypeer) &&
|
---|
141 | (data->verifyhost == needle->verifyhost) &&
|
---|
142 | (data->verifystatus == needle->verifystatus) &&
|
---|
143 | blobcmp(data->cert_blob, needle->cert_blob) &&
|
---|
144 | blobcmp(data->ca_info_blob, needle->ca_info_blob) &&
|
---|
145 | blobcmp(data->issuercert_blob, needle->issuercert_blob) &&
|
---|
146 | Curl_safecmp(data->CApath, needle->CApath) &&
|
---|
147 | Curl_safecmp(data->CAfile, needle->CAfile) &&
|
---|
148 | Curl_safecmp(data->issuercert, needle->issuercert) &&
|
---|
149 | Curl_safecmp(data->clientcert, needle->clientcert) &&
|
---|
150 | #ifdef USE_TLS_SRP
|
---|
151 | !Curl_timestrcmp(data->username, needle->username) &&
|
---|
152 | !Curl_timestrcmp(data->password, needle->password) &&
|
---|
153 | (data->authtype == needle->authtype) &&
|
---|
154 | #endif
|
---|
155 | strcasecompare(data->cipher_list, needle->cipher_list) &&
|
---|
156 | strcasecompare(data->cipher_list13, needle->cipher_list13) &&
|
---|
157 | strcasecompare(data->curves, needle->curves) &&
|
---|
158 | strcasecompare(data->CRLfile, needle->CRLfile) &&
|
---|
159 | strcasecompare(data->pinned_key, needle->pinned_key))
|
---|
160 | return TRUE;
|
---|
161 |
|
---|
162 | return FALSE;
|
---|
163 | }
|
---|
164 |
|
---|
165 | bool
|
---|
166 | Curl_clone_primary_ssl_config(struct ssl_primary_config *source,
|
---|
167 | struct ssl_primary_config *dest)
|
---|
168 | {
|
---|
169 | dest->version = source->version;
|
---|
170 | dest->version_max = source->version_max;
|
---|
171 | dest->verifypeer = source->verifypeer;
|
---|
172 | dest->verifyhost = source->verifyhost;
|
---|
173 | dest->verifystatus = source->verifystatus;
|
---|
174 | dest->sessionid = source->sessionid;
|
---|
175 | dest->ssl_options = source->ssl_options;
|
---|
176 | #ifdef USE_TLS_SRP
|
---|
177 | dest->authtype = source->authtype;
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | CLONE_BLOB(cert_blob);
|
---|
181 | CLONE_BLOB(ca_info_blob);
|
---|
182 | CLONE_BLOB(issuercert_blob);
|
---|
183 | CLONE_STRING(CApath);
|
---|
184 | CLONE_STRING(CAfile);
|
---|
185 | CLONE_STRING(issuercert);
|
---|
186 | CLONE_STRING(clientcert);
|
---|
187 | CLONE_STRING(cipher_list);
|
---|
188 | CLONE_STRING(cipher_list13);
|
---|
189 | CLONE_STRING(pinned_key);
|
---|
190 | CLONE_STRING(curves);
|
---|
191 | CLONE_STRING(CRLfile);
|
---|
192 | #ifdef USE_TLS_SRP
|
---|
193 | CLONE_STRING(username);
|
---|
194 | CLONE_STRING(password);
|
---|
195 | #endif
|
---|
196 |
|
---|
197 | return TRUE;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void Curl_free_primary_ssl_config(struct ssl_primary_config *sslc)
|
---|
201 | {
|
---|
202 | Curl_safefree(sslc->CApath);
|
---|
203 | Curl_safefree(sslc->CAfile);
|
---|
204 | Curl_safefree(sslc->issuercert);
|
---|
205 | Curl_safefree(sslc->clientcert);
|
---|
206 | Curl_safefree(sslc->cipher_list);
|
---|
207 | Curl_safefree(sslc->cipher_list13);
|
---|
208 | Curl_safefree(sslc->pinned_key);
|
---|
209 | Curl_safefree(sslc->cert_blob);
|
---|
210 | Curl_safefree(sslc->ca_info_blob);
|
---|
211 | Curl_safefree(sslc->issuercert_blob);
|
---|
212 | Curl_safefree(sslc->curves);
|
---|
213 | Curl_safefree(sslc->CRLfile);
|
---|
214 | #ifdef USE_TLS_SRP
|
---|
215 | Curl_safefree(sslc->username);
|
---|
216 | Curl_safefree(sslc->password);
|
---|
217 | #endif
|
---|
218 | }
|
---|
219 |
|
---|
220 | #ifdef USE_SSL
|
---|
221 | static int multissl_setup(const struct Curl_ssl *backend);
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | curl_sslbackend Curl_ssl_backend(void)
|
---|
225 | {
|
---|
226 | #ifdef USE_SSL
|
---|
227 | multissl_setup(NULL);
|
---|
228 | return Curl_ssl->info.id;
|
---|
229 | #else
|
---|
230 | return CURLSSLBACKEND_NONE;
|
---|
231 | #endif
|
---|
232 | }
|
---|
233 |
|
---|
234 | #ifdef USE_SSL
|
---|
235 |
|
---|
236 | /* "global" init done? */
|
---|
237 | static bool init_ssl = FALSE;
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Global SSL init
|
---|
241 | *
|
---|
242 | * @retval 0 error initializing SSL
|
---|
243 | * @retval 1 SSL initialized successfully
|
---|
244 | */
|
---|
245 | int Curl_ssl_init(void)
|
---|
246 | {
|
---|
247 | /* make sure this is only done once */
|
---|
248 | if(init_ssl)
|
---|
249 | return 1;
|
---|
250 | init_ssl = TRUE; /* never again */
|
---|
251 |
|
---|
252 | return Curl_ssl->init();
|
---|
253 | }
|
---|
254 |
|
---|
255 | #if defined(CURL_WITH_MULTI_SSL)
|
---|
256 | static const struct Curl_ssl Curl_ssl_multi;
|
---|
257 | #endif
|
---|
258 |
|
---|
259 | /* Global cleanup */
|
---|
260 | void Curl_ssl_cleanup(void)
|
---|
261 | {
|
---|
262 | if(init_ssl) {
|
---|
263 | /* only cleanup if we did a previous init */
|
---|
264 | Curl_ssl->cleanup();
|
---|
265 | #if defined(CURL_WITH_MULTI_SSL)
|
---|
266 | Curl_ssl = &Curl_ssl_multi;
|
---|
267 | #endif
|
---|
268 | init_ssl = FALSE;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | static bool ssl_prefs_check(struct Curl_easy *data)
|
---|
273 | {
|
---|
274 | /* check for CURLOPT_SSLVERSION invalid parameter value */
|
---|
275 | const long sslver = data->set.ssl.primary.version;
|
---|
276 | if((sslver < 0) || (sslver >= CURL_SSLVERSION_LAST)) {
|
---|
277 | failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION");
|
---|
278 | return FALSE;
|
---|
279 | }
|
---|
280 |
|
---|
281 | switch(data->set.ssl.primary.version_max) {
|
---|
282 | case CURL_SSLVERSION_MAX_NONE:
|
---|
283 | case CURL_SSLVERSION_MAX_DEFAULT:
|
---|
284 | break;
|
---|
285 |
|
---|
286 | default:
|
---|
287 | if((data->set.ssl.primary.version_max >> 16) < sslver) {
|
---|
288 | failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION");
|
---|
289 | return FALSE;
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | return TRUE;
|
---|
294 | }
|
---|
295 |
|
---|
296 | static struct ssl_connect_data *cf_ctx_new(struct Curl_easy *data)
|
---|
297 | {
|
---|
298 | struct ssl_connect_data *ctx;
|
---|
299 |
|
---|
300 | (void)data;
|
---|
301 | ctx = calloc(1, sizeof(*ctx));
|
---|
302 | if(!ctx)
|
---|
303 | return NULL;
|
---|
304 |
|
---|
305 | ctx->backend = calloc(1, Curl_ssl->sizeof_ssl_backend_data);
|
---|
306 | if(!ctx->backend) {
|
---|
307 | free(ctx);
|
---|
308 | return NULL;
|
---|
309 | }
|
---|
310 | return ctx;
|
---|
311 | }
|
---|
312 |
|
---|
313 | static void cf_ctx_free(struct ssl_connect_data *ctx)
|
---|
314 | {
|
---|
315 | if(ctx) {
|
---|
316 | free(ctx->backend);
|
---|
317 | free(ctx);
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | static void cf_ctx_set_data(struct Curl_cfilter *cf,
|
---|
322 | struct Curl_easy *data)
|
---|
323 | {
|
---|
324 | if(cf->ctx)
|
---|
325 | ((struct ssl_connect_data *)cf->ctx)->call_data = data;
|
---|
326 | }
|
---|
327 |
|
---|
328 | static CURLcode ssl_connect(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
329 | {
|
---|
330 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
331 | CURLcode result;
|
---|
332 |
|
---|
333 | if(!ssl_prefs_check(data))
|
---|
334 | return CURLE_SSL_CONNECT_ERROR;
|
---|
335 |
|
---|
336 | /* mark this is being ssl-enabled from here on. */
|
---|
337 | connssl->state = ssl_connection_negotiating;
|
---|
338 |
|
---|
339 | result = Curl_ssl->connect_blocking(cf, data);
|
---|
340 |
|
---|
341 | if(!result) {
|
---|
342 | Curl_pgrsTime(data, TIMER_APPCONNECT); /* SSL is connected */
|
---|
343 | DEBUGASSERT(connssl->state == ssl_connection_complete);
|
---|
344 | }
|
---|
345 |
|
---|
346 | return result;
|
---|
347 | }
|
---|
348 |
|
---|
349 | static CURLcode
|
---|
350 | ssl_connect_nonblocking(struct Curl_cfilter *cf, struct Curl_easy *data,
|
---|
351 | bool *done)
|
---|
352 | {
|
---|
353 | if(!ssl_prefs_check(data))
|
---|
354 | return CURLE_SSL_CONNECT_ERROR;
|
---|
355 |
|
---|
356 | /* mark this is being ssl requested from here on. */
|
---|
357 | return Curl_ssl->connect_nonblocking(cf, data, done);
|
---|
358 | }
|
---|
359 |
|
---|
360 | /*
|
---|
361 | * Lock shared SSL session data
|
---|
362 | */
|
---|
363 | void Curl_ssl_sessionid_lock(struct Curl_easy *data)
|
---|
364 | {
|
---|
365 | if(SSLSESSION_SHARED(data))
|
---|
366 | Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
|
---|
367 | }
|
---|
368 |
|
---|
369 | /*
|
---|
370 | * Unlock shared SSL session data
|
---|
371 | */
|
---|
372 | void Curl_ssl_sessionid_unlock(struct Curl_easy *data)
|
---|
373 | {
|
---|
374 | if(SSLSESSION_SHARED(data))
|
---|
375 | Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
|
---|
376 | }
|
---|
377 |
|
---|
378 | /*
|
---|
379 | * Check if there's a session ID for the given connection in the cache, and if
|
---|
380 | * there's one suitable, it is provided. Returns TRUE when no entry matched.
|
---|
381 | */
|
---|
382 | bool Curl_ssl_getsessionid(struct Curl_cfilter *cf,
|
---|
383 | struct Curl_easy *data,
|
---|
384 | void **ssl_sessionid,
|
---|
385 | size_t *idsize) /* set 0 if unknown */
|
---|
386 | {
|
---|
387 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
388 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
389 | struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
|
---|
390 | struct Curl_ssl_session *check;
|
---|
391 | size_t i;
|
---|
392 | long *general_age;
|
---|
393 | bool no_match = TRUE;
|
---|
394 |
|
---|
395 | *ssl_sessionid = NULL;
|
---|
396 | if(!ssl_config)
|
---|
397 | return TRUE;
|
---|
398 |
|
---|
399 | DEBUGASSERT(ssl_config->primary.sessionid);
|
---|
400 |
|
---|
401 | if(!ssl_config->primary.sessionid || !data->state.session)
|
---|
402 | /* session ID re-use is disabled or the session cache has not been
|
---|
403 | setup */
|
---|
404 | return TRUE;
|
---|
405 |
|
---|
406 | /* Lock if shared */
|
---|
407 | if(SSLSESSION_SHARED(data))
|
---|
408 | general_age = &data->share->sessionage;
|
---|
409 | else
|
---|
410 | general_age = &data->state.sessionage;
|
---|
411 |
|
---|
412 | for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) {
|
---|
413 | check = &data->state.session[i];
|
---|
414 | if(!check->sessionid)
|
---|
415 | /* not session ID means blank entry */
|
---|
416 | continue;
|
---|
417 | if(strcasecompare(connssl->hostname, check->name) &&
|
---|
418 | ((!cf->conn->bits.conn_to_host && !check->conn_to_host) ||
|
---|
419 | (cf->conn->bits.conn_to_host && check->conn_to_host &&
|
---|
420 | strcasecompare(cf->conn->conn_to_host.name, check->conn_to_host))) &&
|
---|
421 | ((!cf->conn->bits.conn_to_port && check->conn_to_port == -1) ||
|
---|
422 | (cf->conn->bits.conn_to_port && check->conn_to_port != -1 &&
|
---|
423 | cf->conn->conn_to_port == check->conn_to_port)) &&
|
---|
424 | (connssl->port == check->remote_port) &&
|
---|
425 | strcasecompare(cf->conn->handler->scheme, check->scheme) &&
|
---|
426 | Curl_ssl_config_matches(conn_config, &check->ssl_config)) {
|
---|
427 | /* yes, we have a session ID! */
|
---|
428 | (*general_age)++; /* increase general age */
|
---|
429 | check->age = *general_age; /* set this as used in this age */
|
---|
430 | *ssl_sessionid = check->sessionid;
|
---|
431 | if(idsize)
|
---|
432 | *idsize = check->idsize;
|
---|
433 | no_match = FALSE;
|
---|
434 | break;
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | DEBUGF(infof(data, DMSG(data, "%s Session ID in cache for %s %s://%s:%d"),
|
---|
439 | no_match? "Didn't find": "Found",
|
---|
440 | Curl_ssl_cf_is_proxy(cf) ? "proxy" : "host",
|
---|
441 | cf->conn->handler->scheme, connssl->hostname, connssl->port));
|
---|
442 | return no_match;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Kill a single session ID entry in the cache.
|
---|
447 | */
|
---|
448 | void Curl_ssl_kill_session(struct Curl_ssl_session *session)
|
---|
449 | {
|
---|
450 | if(session->sessionid) {
|
---|
451 | /* defensive check */
|
---|
452 |
|
---|
453 | /* free the ID the SSL-layer specific way */
|
---|
454 | Curl_ssl->session_free(session->sessionid);
|
---|
455 |
|
---|
456 | session->sessionid = NULL;
|
---|
457 | session->age = 0; /* fresh */
|
---|
458 |
|
---|
459 | Curl_free_primary_ssl_config(&session->ssl_config);
|
---|
460 |
|
---|
461 | Curl_safefree(session->name);
|
---|
462 | Curl_safefree(session->conn_to_host);
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 | /*
|
---|
467 | * Delete the given session ID from the cache.
|
---|
468 | */
|
---|
469 | void Curl_ssl_delsessionid(struct Curl_easy *data, void *ssl_sessionid)
|
---|
470 | {
|
---|
471 | size_t i;
|
---|
472 |
|
---|
473 | for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) {
|
---|
474 | struct Curl_ssl_session *check = &data->state.session[i];
|
---|
475 |
|
---|
476 | if(check->sessionid == ssl_sessionid) {
|
---|
477 | Curl_ssl_kill_session(check);
|
---|
478 | break;
|
---|
479 | }
|
---|
480 | }
|
---|
481 | }
|
---|
482 |
|
---|
483 | /*
|
---|
484 | * Store session id in the session cache. The ID passed on to this function
|
---|
485 | * must already have been extracted and allocated the proper way for the SSL
|
---|
486 | * layer. Curl_XXXX_session_free() will be called to free/kill the session ID
|
---|
487 | * later on.
|
---|
488 | */
|
---|
489 | CURLcode Curl_ssl_addsessionid(struct Curl_cfilter *cf,
|
---|
490 | struct Curl_easy *data,
|
---|
491 | void *ssl_sessionid,
|
---|
492 | size_t idsize,
|
---|
493 | bool *added)
|
---|
494 | {
|
---|
495 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
496 | struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
|
---|
497 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
498 | size_t i;
|
---|
499 | struct Curl_ssl_session *store;
|
---|
500 | long oldest_age;
|
---|
501 | char *clone_host;
|
---|
502 | char *clone_conn_to_host;
|
---|
503 | int conn_to_port;
|
---|
504 | long *general_age;
|
---|
505 |
|
---|
506 | if(added)
|
---|
507 | *added = FALSE;
|
---|
508 |
|
---|
509 | if(!data->state.session)
|
---|
510 | return CURLE_OK;
|
---|
511 |
|
---|
512 | store = &data->state.session[0];
|
---|
513 | oldest_age = data->state.session[0].age; /* zero if unused */
|
---|
514 | (void)ssl_config;
|
---|
515 | DEBUGASSERT(ssl_config->primary.sessionid);
|
---|
516 |
|
---|
517 | clone_host = strdup(connssl->hostname);
|
---|
518 | if(!clone_host)
|
---|
519 | return CURLE_OUT_OF_MEMORY; /* bail out */
|
---|
520 |
|
---|
521 | if(cf->conn->bits.conn_to_host) {
|
---|
522 | clone_conn_to_host = strdup(cf->conn->conn_to_host.name);
|
---|
523 | if(!clone_conn_to_host) {
|
---|
524 | free(clone_host);
|
---|
525 | return CURLE_OUT_OF_MEMORY; /* bail out */
|
---|
526 | }
|
---|
527 | }
|
---|
528 | else
|
---|
529 | clone_conn_to_host = NULL;
|
---|
530 |
|
---|
531 | if(cf->conn->bits.conn_to_port)
|
---|
532 | conn_to_port = cf->conn->conn_to_port;
|
---|
533 | else
|
---|
534 | conn_to_port = -1;
|
---|
535 |
|
---|
536 | /* Now we should add the session ID and the host name to the cache, (remove
|
---|
537 | the oldest if necessary) */
|
---|
538 |
|
---|
539 | /* If using shared SSL session, lock! */
|
---|
540 | if(SSLSESSION_SHARED(data)) {
|
---|
541 | general_age = &data->share->sessionage;
|
---|
542 | }
|
---|
543 | else {
|
---|
544 | general_age = &data->state.sessionage;
|
---|
545 | }
|
---|
546 |
|
---|
547 | /* find an empty slot for us, or find the oldest */
|
---|
548 | for(i = 1; (i < data->set.general_ssl.max_ssl_sessions) &&
|
---|
549 | data->state.session[i].sessionid; i++) {
|
---|
550 | if(data->state.session[i].age < oldest_age) {
|
---|
551 | oldest_age = data->state.session[i].age;
|
---|
552 | store = &data->state.session[i];
|
---|
553 | }
|
---|
554 | }
|
---|
555 | if(i == data->set.general_ssl.max_ssl_sessions)
|
---|
556 | /* cache is full, we must "kill" the oldest entry! */
|
---|
557 | Curl_ssl_kill_session(store);
|
---|
558 | else
|
---|
559 | store = &data->state.session[i]; /* use this slot */
|
---|
560 |
|
---|
561 | /* now init the session struct wisely */
|
---|
562 | store->sessionid = ssl_sessionid;
|
---|
563 | store->idsize = idsize;
|
---|
564 | store->age = *general_age; /* set current age */
|
---|
565 | /* free it if there's one already present */
|
---|
566 | free(store->name);
|
---|
567 | free(store->conn_to_host);
|
---|
568 | store->name = clone_host; /* clone host name */
|
---|
569 | store->conn_to_host = clone_conn_to_host; /* clone connect to host name */
|
---|
570 | store->conn_to_port = conn_to_port; /* connect to port number */
|
---|
571 | /* port number */
|
---|
572 | store->remote_port = connssl->port;
|
---|
573 | store->scheme = cf->conn->handler->scheme;
|
---|
574 |
|
---|
575 | if(!Curl_clone_primary_ssl_config(conn_config, &store->ssl_config)) {
|
---|
576 | Curl_free_primary_ssl_config(&store->ssl_config);
|
---|
577 | store->sessionid = NULL; /* let caller free sessionid */
|
---|
578 | free(clone_host);
|
---|
579 | free(clone_conn_to_host);
|
---|
580 | return CURLE_OUT_OF_MEMORY;
|
---|
581 | }
|
---|
582 |
|
---|
583 | if(added)
|
---|
584 | *added = TRUE;
|
---|
585 |
|
---|
586 | DEBUGF(infof(data, DMSG(data, "Added Session ID to cache for %s://%s:%d"
|
---|
587 | " [%s]"), store->scheme, store->name, store->remote_port,
|
---|
588 | Curl_ssl_cf_is_proxy(cf) ? "PROXY" : "server"));
|
---|
589 | return CURLE_OK;
|
---|
590 | }
|
---|
591 |
|
---|
592 | void Curl_free_multi_ssl_backend_data(struct multi_ssl_backend_data *mbackend)
|
---|
593 | {
|
---|
594 | if(Curl_ssl->free_multi_ssl_backend_data && mbackend)
|
---|
595 | Curl_ssl->free_multi_ssl_backend_data(mbackend);
|
---|
596 | }
|
---|
597 |
|
---|
598 | void Curl_ssl_close_all(struct Curl_easy *data)
|
---|
599 | {
|
---|
600 | /* kill the session ID cache if not shared */
|
---|
601 | if(data->state.session && !SSLSESSION_SHARED(data)) {
|
---|
602 | size_t i;
|
---|
603 | for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++)
|
---|
604 | /* the single-killer function handles empty table slots */
|
---|
605 | Curl_ssl_kill_session(&data->state.session[i]);
|
---|
606 |
|
---|
607 | /* free the cache data */
|
---|
608 | Curl_safefree(data->state.session);
|
---|
609 | }
|
---|
610 |
|
---|
611 | Curl_ssl->close_all(data);
|
---|
612 | }
|
---|
613 |
|
---|
614 | int Curl_ssl_get_select_socks(struct Curl_cfilter *cf, struct Curl_easy *data,
|
---|
615 | curl_socket_t *socks)
|
---|
616 | {
|
---|
617 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
618 |
|
---|
619 | (void)data;
|
---|
620 | if(connssl->connecting_state == ssl_connect_2_writing) {
|
---|
621 | /* write mode */
|
---|
622 | socks[0] = cf->conn->sock[FIRSTSOCKET];
|
---|
623 | return GETSOCK_WRITESOCK(0);
|
---|
624 | }
|
---|
625 | if(connssl->connecting_state == ssl_connect_2_reading) {
|
---|
626 | /* read mode */
|
---|
627 | socks[0] = cf->conn->sock[FIRSTSOCKET];
|
---|
628 | return GETSOCK_READSOCK(0);
|
---|
629 | }
|
---|
630 |
|
---|
631 | return GETSOCK_BLANK;
|
---|
632 | }
|
---|
633 |
|
---|
634 | /* Selects an SSL crypto engine
|
---|
635 | */
|
---|
636 | CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine)
|
---|
637 | {
|
---|
638 | return Curl_ssl->set_engine(data, engine);
|
---|
639 | }
|
---|
640 |
|
---|
641 | /* Selects the default SSL crypto engine
|
---|
642 | */
|
---|
643 | CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data)
|
---|
644 | {
|
---|
645 | return Curl_ssl->set_engine_default(data);
|
---|
646 | }
|
---|
647 |
|
---|
648 | /* Return list of OpenSSL crypto engine names. */
|
---|
649 | struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data)
|
---|
650 | {
|
---|
651 | return Curl_ssl->engines_list(data);
|
---|
652 | }
|
---|
653 |
|
---|
654 | /*
|
---|
655 | * This sets up a session ID cache to the specified size. Make sure this code
|
---|
656 | * is agnostic to what underlying SSL technology we use.
|
---|
657 | */
|
---|
658 | CURLcode Curl_ssl_initsessions(struct Curl_easy *data, size_t amount)
|
---|
659 | {
|
---|
660 | struct Curl_ssl_session *session;
|
---|
661 |
|
---|
662 | if(data->state.session)
|
---|
663 | /* this is just a precaution to prevent multiple inits */
|
---|
664 | return CURLE_OK;
|
---|
665 |
|
---|
666 | session = calloc(amount, sizeof(struct Curl_ssl_session));
|
---|
667 | if(!session)
|
---|
668 | return CURLE_OUT_OF_MEMORY;
|
---|
669 |
|
---|
670 | /* store the info in the SSL section */
|
---|
671 | data->set.general_ssl.max_ssl_sessions = amount;
|
---|
672 | data->state.session = session;
|
---|
673 | data->state.sessionage = 1; /* this is brand new */
|
---|
674 | return CURLE_OK;
|
---|
675 | }
|
---|
676 |
|
---|
677 | static size_t multissl_version(char *buffer, size_t size);
|
---|
678 |
|
---|
679 | void Curl_ssl_version(char *buffer, size_t size)
|
---|
680 | {
|
---|
681 | #ifdef CURL_WITH_MULTI_SSL
|
---|
682 | (void)multissl_version(buffer, size);
|
---|
683 | #else
|
---|
684 | (void)Curl_ssl->version(buffer, size);
|
---|
685 | #endif
|
---|
686 | }
|
---|
687 |
|
---|
688 | /*
|
---|
689 | * This function tries to determine connection status.
|
---|
690 | *
|
---|
691 | * Return codes:
|
---|
692 | * 1 means the connection is still in place
|
---|
693 | * 0 means the connection has been closed
|
---|
694 | * -1 means the connection status is unknown
|
---|
695 | */
|
---|
696 | int Curl_ssl_check_cxn(struct Curl_easy *data, struct connectdata *conn)
|
---|
697 | {
|
---|
698 | struct Curl_cfilter *cf = Curl_ssl_cf_get_ssl(conn->cfilter[FIRSTSOCKET]);
|
---|
699 | return cf? Curl_ssl->check_cxn(cf, data) : -1;
|
---|
700 | }
|
---|
701 |
|
---|
702 | void Curl_ssl_free_certinfo(struct Curl_easy *data)
|
---|
703 | {
|
---|
704 | struct curl_certinfo *ci = &data->info.certs;
|
---|
705 |
|
---|
706 | if(ci->num_of_certs) {
|
---|
707 | /* free all individual lists used */
|
---|
708 | int i;
|
---|
709 | for(i = 0; i<ci->num_of_certs; i++) {
|
---|
710 | curl_slist_free_all(ci->certinfo[i]);
|
---|
711 | ci->certinfo[i] = NULL;
|
---|
712 | }
|
---|
713 |
|
---|
714 | free(ci->certinfo); /* free the actual array too */
|
---|
715 | ci->certinfo = NULL;
|
---|
716 | ci->num_of_certs = 0;
|
---|
717 | }
|
---|
718 | }
|
---|
719 |
|
---|
720 | CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num)
|
---|
721 | {
|
---|
722 | struct curl_certinfo *ci = &data->info.certs;
|
---|
723 | struct curl_slist **table;
|
---|
724 |
|
---|
725 | /* Free any previous certificate information structures */
|
---|
726 | Curl_ssl_free_certinfo(data);
|
---|
727 |
|
---|
728 | /* Allocate the required certificate information structures */
|
---|
729 | table = calloc((size_t) num, sizeof(struct curl_slist *));
|
---|
730 | if(!table)
|
---|
731 | return CURLE_OUT_OF_MEMORY;
|
---|
732 |
|
---|
733 | ci->num_of_certs = num;
|
---|
734 | ci->certinfo = table;
|
---|
735 |
|
---|
736 | return CURLE_OK;
|
---|
737 | }
|
---|
738 |
|
---|
739 | /*
|
---|
740 | * 'value' is NOT a null-terminated string
|
---|
741 | */
|
---|
742 | CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data,
|
---|
743 | int certnum,
|
---|
744 | const char *label,
|
---|
745 | const char *value,
|
---|
746 | size_t valuelen)
|
---|
747 | {
|
---|
748 | struct curl_certinfo *ci = &data->info.certs;
|
---|
749 | char *output;
|
---|
750 | struct curl_slist *nl;
|
---|
751 | CURLcode result = CURLE_OK;
|
---|
752 | size_t labellen = strlen(label);
|
---|
753 | size_t outlen = labellen + 1 + valuelen + 1; /* label:value\0 */
|
---|
754 |
|
---|
755 | output = malloc(outlen);
|
---|
756 | if(!output)
|
---|
757 | return CURLE_OUT_OF_MEMORY;
|
---|
758 |
|
---|
759 | /* sprintf the label and colon */
|
---|
760 | msnprintf(output, outlen, "%s:", label);
|
---|
761 |
|
---|
762 | /* memcpy the value (it might not be null-terminated) */
|
---|
763 | memcpy(&output[labellen + 1], value, valuelen);
|
---|
764 |
|
---|
765 | /* null-terminate the output */
|
---|
766 | output[labellen + 1 + valuelen] = 0;
|
---|
767 |
|
---|
768 | nl = Curl_slist_append_nodup(ci->certinfo[certnum], output);
|
---|
769 | if(!nl) {
|
---|
770 | free(output);
|
---|
771 | curl_slist_free_all(ci->certinfo[certnum]);
|
---|
772 | result = CURLE_OUT_OF_MEMORY;
|
---|
773 | }
|
---|
774 |
|
---|
775 | ci->certinfo[certnum] = nl;
|
---|
776 | return result;
|
---|
777 | }
|
---|
778 |
|
---|
779 | /*
|
---|
780 | * This is a convenience function for push_certinfo_len that takes a zero
|
---|
781 | * terminated value.
|
---|
782 | */
|
---|
783 | CURLcode Curl_ssl_push_certinfo(struct Curl_easy *data,
|
---|
784 | int certnum,
|
---|
785 | const char *label,
|
---|
786 | const char *value)
|
---|
787 | {
|
---|
788 | size_t valuelen = strlen(value);
|
---|
789 |
|
---|
790 | return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen);
|
---|
791 | }
|
---|
792 |
|
---|
793 | CURLcode Curl_ssl_random(struct Curl_easy *data,
|
---|
794 | unsigned char *entropy,
|
---|
795 | size_t length)
|
---|
796 | {
|
---|
797 | return Curl_ssl->random(data, entropy, length);
|
---|
798 | }
|
---|
799 |
|
---|
800 | /*
|
---|
801 | * Curl_ssl_snihost() converts the input host name to a suitable SNI name put
|
---|
802 | * in data->state.buffer. Returns a pointer to the name (or NULL if a problem)
|
---|
803 | * and stores the new length in 'olen'.
|
---|
804 | *
|
---|
805 | * SNI fields must not have any trailing dot and while RFC 6066 section 3 says
|
---|
806 | * the SNI field is case insensitive, browsers always send the data lowercase
|
---|
807 | * and subsequently there are numerous servers out there that don't work
|
---|
808 | * unless the name is lowercased.
|
---|
809 | */
|
---|
810 |
|
---|
811 | char *Curl_ssl_snihost(struct Curl_easy *data, const char *host, size_t *olen)
|
---|
812 | {
|
---|
813 | size_t len = strlen(host);
|
---|
814 | if(len && (host[len-1] == '.'))
|
---|
815 | len--;
|
---|
816 | if(len >= data->set.buffer_size)
|
---|
817 | return NULL;
|
---|
818 |
|
---|
819 | Curl_strntolower(data->state.buffer, host, len);
|
---|
820 | data->state.buffer[len] = 0;
|
---|
821 | if(olen)
|
---|
822 | *olen = len;
|
---|
823 | return data->state.buffer;
|
---|
824 | }
|
---|
825 |
|
---|
826 | /*
|
---|
827 | * Public key pem to der conversion
|
---|
828 | */
|
---|
829 |
|
---|
830 | static CURLcode pubkey_pem_to_der(const char *pem,
|
---|
831 | unsigned char **der, size_t *der_len)
|
---|
832 | {
|
---|
833 | char *stripped_pem, *begin_pos, *end_pos;
|
---|
834 | size_t pem_count, stripped_pem_count = 0, pem_len;
|
---|
835 | CURLcode result;
|
---|
836 |
|
---|
837 | /* if no pem, exit. */
|
---|
838 | if(!pem)
|
---|
839 | return CURLE_BAD_CONTENT_ENCODING;
|
---|
840 |
|
---|
841 | begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----");
|
---|
842 | if(!begin_pos)
|
---|
843 | return CURLE_BAD_CONTENT_ENCODING;
|
---|
844 |
|
---|
845 | pem_count = begin_pos - pem;
|
---|
846 | /* Invalid if not at beginning AND not directly following \n */
|
---|
847 | if(0 != pem_count && '\n' != pem[pem_count - 1])
|
---|
848 | return CURLE_BAD_CONTENT_ENCODING;
|
---|
849 |
|
---|
850 | /* 26 is length of "-----BEGIN PUBLIC KEY-----" */
|
---|
851 | pem_count += 26;
|
---|
852 |
|
---|
853 | /* Invalid if not directly following \n */
|
---|
854 | end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----");
|
---|
855 | if(!end_pos)
|
---|
856 | return CURLE_BAD_CONTENT_ENCODING;
|
---|
857 |
|
---|
858 | pem_len = end_pos - pem;
|
---|
859 |
|
---|
860 | stripped_pem = malloc(pem_len - pem_count + 1);
|
---|
861 | if(!stripped_pem)
|
---|
862 | return CURLE_OUT_OF_MEMORY;
|
---|
863 |
|
---|
864 | /*
|
---|
865 | * Here we loop through the pem array one character at a time between the
|
---|
866 | * correct indices, and place each character that is not '\n' or '\r'
|
---|
867 | * into the stripped_pem array, which should represent the raw base64 string
|
---|
868 | */
|
---|
869 | while(pem_count < pem_len) {
|
---|
870 | if('\n' != pem[pem_count] && '\r' != pem[pem_count])
|
---|
871 | stripped_pem[stripped_pem_count++] = pem[pem_count];
|
---|
872 | ++pem_count;
|
---|
873 | }
|
---|
874 | /* Place the null terminator in the correct place */
|
---|
875 | stripped_pem[stripped_pem_count] = '\0';
|
---|
876 |
|
---|
877 | result = Curl_base64_decode(stripped_pem, der, der_len);
|
---|
878 |
|
---|
879 | Curl_safefree(stripped_pem);
|
---|
880 |
|
---|
881 | return result;
|
---|
882 | }
|
---|
883 |
|
---|
884 | /*
|
---|
885 | * Generic pinned public key check.
|
---|
886 | */
|
---|
887 |
|
---|
888 | CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data,
|
---|
889 | const char *pinnedpubkey,
|
---|
890 | const unsigned char *pubkey, size_t pubkeylen)
|
---|
891 | {
|
---|
892 | FILE *fp;
|
---|
893 | unsigned char *buf = NULL, *pem_ptr = NULL;
|
---|
894 | CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
---|
895 |
|
---|
896 | /* if a path wasn't specified, don't pin */
|
---|
897 | if(!pinnedpubkey)
|
---|
898 | return CURLE_OK;
|
---|
899 | if(!pubkey || !pubkeylen)
|
---|
900 | return result;
|
---|
901 |
|
---|
902 | /* only do this if pinnedpubkey starts with "sha256//", length 8 */
|
---|
903 | if(strncmp(pinnedpubkey, "sha256//", 8) == 0) {
|
---|
904 | CURLcode encode;
|
---|
905 | size_t encodedlen, pinkeylen;
|
---|
906 | char *encoded, *pinkeycopy, *begin_pos, *end_pos;
|
---|
907 | unsigned char *sha256sumdigest;
|
---|
908 |
|
---|
909 | if(!Curl_ssl->sha256sum) {
|
---|
910 | /* without sha256 support, this cannot match */
|
---|
911 | return result;
|
---|
912 | }
|
---|
913 |
|
---|
914 | /* compute sha256sum of public key */
|
---|
915 | sha256sumdigest = malloc(CURL_SHA256_DIGEST_LENGTH);
|
---|
916 | if(!sha256sumdigest)
|
---|
917 | return CURLE_OUT_OF_MEMORY;
|
---|
918 | encode = Curl_ssl->sha256sum(pubkey, pubkeylen,
|
---|
919 | sha256sumdigest, CURL_SHA256_DIGEST_LENGTH);
|
---|
920 |
|
---|
921 | if(encode != CURLE_OK)
|
---|
922 | return encode;
|
---|
923 |
|
---|
924 | encode = Curl_base64_encode((char *)sha256sumdigest,
|
---|
925 | CURL_SHA256_DIGEST_LENGTH, &encoded,
|
---|
926 | &encodedlen);
|
---|
927 | Curl_safefree(sha256sumdigest);
|
---|
928 |
|
---|
929 | if(encode)
|
---|
930 | return encode;
|
---|
931 |
|
---|
932 | infof(data, " public key hash: sha256//%s", encoded);
|
---|
933 |
|
---|
934 | /* it starts with sha256//, copy so we can modify it */
|
---|
935 | pinkeylen = strlen(pinnedpubkey) + 1;
|
---|
936 | pinkeycopy = malloc(pinkeylen);
|
---|
937 | if(!pinkeycopy) {
|
---|
938 | Curl_safefree(encoded);
|
---|
939 | return CURLE_OUT_OF_MEMORY;
|
---|
940 | }
|
---|
941 | memcpy(pinkeycopy, pinnedpubkey, pinkeylen);
|
---|
942 | /* point begin_pos to the copy, and start extracting keys */
|
---|
943 | begin_pos = pinkeycopy;
|
---|
944 | do {
|
---|
945 | end_pos = strstr(begin_pos, ";sha256//");
|
---|
946 | /*
|
---|
947 | * if there is an end_pos, null terminate,
|
---|
948 | * otherwise it'll go to the end of the original string
|
---|
949 | */
|
---|
950 | if(end_pos)
|
---|
951 | end_pos[0] = '\0';
|
---|
952 |
|
---|
953 | /* compare base64 sha256 digests, 8 is the length of "sha256//" */
|
---|
954 | if(encodedlen == strlen(begin_pos + 8) &&
|
---|
955 | !memcmp(encoded, begin_pos + 8, encodedlen)) {
|
---|
956 | result = CURLE_OK;
|
---|
957 | break;
|
---|
958 | }
|
---|
959 |
|
---|
960 | /*
|
---|
961 | * change back the null-terminator we changed earlier,
|
---|
962 | * and look for next begin
|
---|
963 | */
|
---|
964 | if(end_pos) {
|
---|
965 | end_pos[0] = ';';
|
---|
966 | begin_pos = strstr(end_pos, "sha256//");
|
---|
967 | }
|
---|
968 | } while(end_pos && begin_pos);
|
---|
969 | Curl_safefree(encoded);
|
---|
970 | Curl_safefree(pinkeycopy);
|
---|
971 | return result;
|
---|
972 | }
|
---|
973 |
|
---|
974 | fp = fopen(pinnedpubkey, "rb");
|
---|
975 | if(!fp)
|
---|
976 | return result;
|
---|
977 |
|
---|
978 | do {
|
---|
979 | long filesize;
|
---|
980 | size_t size, pem_len;
|
---|
981 | CURLcode pem_read;
|
---|
982 |
|
---|
983 | /* Determine the file's size */
|
---|
984 | if(fseek(fp, 0, SEEK_END))
|
---|
985 | break;
|
---|
986 | filesize = ftell(fp);
|
---|
987 | if(fseek(fp, 0, SEEK_SET))
|
---|
988 | break;
|
---|
989 | if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE)
|
---|
990 | break;
|
---|
991 |
|
---|
992 | /*
|
---|
993 | * if the size of our certificate is bigger than the file
|
---|
994 | * size then it can't match
|
---|
995 | */
|
---|
996 | size = curlx_sotouz((curl_off_t) filesize);
|
---|
997 | if(pubkeylen > size)
|
---|
998 | break;
|
---|
999 |
|
---|
1000 | /*
|
---|
1001 | * Allocate buffer for the pinned key
|
---|
1002 | * With 1 additional byte for null terminator in case of PEM key
|
---|
1003 | */
|
---|
1004 | buf = malloc(size + 1);
|
---|
1005 | if(!buf)
|
---|
1006 | break;
|
---|
1007 |
|
---|
1008 | /* Returns number of elements read, which should be 1 */
|
---|
1009 | if((int) fread(buf, size, 1, fp) != 1)
|
---|
1010 | break;
|
---|
1011 |
|
---|
1012 | /* If the sizes are the same, it can't be base64 encoded, must be der */
|
---|
1013 | if(pubkeylen == size) {
|
---|
1014 | if(!memcmp(pubkey, buf, pubkeylen))
|
---|
1015 | result = CURLE_OK;
|
---|
1016 | break;
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /*
|
---|
1020 | * Otherwise we will assume it's PEM and try to decode it
|
---|
1021 | * after placing null terminator
|
---|
1022 | */
|
---|
1023 | buf[size] = '\0';
|
---|
1024 | pem_read = pubkey_pem_to_der((const char *)buf, &pem_ptr, &pem_len);
|
---|
1025 | /* if it wasn't read successfully, exit */
|
---|
1026 | if(pem_read)
|
---|
1027 | break;
|
---|
1028 |
|
---|
1029 | /*
|
---|
1030 | * if the size of our certificate doesn't match the size of
|
---|
1031 | * the decoded file, they can't be the same, otherwise compare
|
---|
1032 | */
|
---|
1033 | if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen))
|
---|
1034 | result = CURLE_OK;
|
---|
1035 | } while(0);
|
---|
1036 |
|
---|
1037 | Curl_safefree(buf);
|
---|
1038 | Curl_safefree(pem_ptr);
|
---|
1039 | fclose(fp);
|
---|
1040 |
|
---|
1041 | return result;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | /*
|
---|
1045 | * Check whether the SSL backend supports the status_request extension.
|
---|
1046 | */
|
---|
1047 | bool Curl_ssl_cert_status_request(void)
|
---|
1048 | {
|
---|
1049 | return Curl_ssl->cert_status_request();
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | /*
|
---|
1053 | * Check whether the SSL backend supports false start.
|
---|
1054 | */
|
---|
1055 | bool Curl_ssl_false_start(struct Curl_easy *data)
|
---|
1056 | {
|
---|
1057 | (void)data;
|
---|
1058 | return Curl_ssl->false_start();
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | /*
|
---|
1062 | * Default implementations for unsupported functions.
|
---|
1063 | */
|
---|
1064 |
|
---|
1065 | int Curl_none_init(void)
|
---|
1066 | {
|
---|
1067 | return 1;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | void Curl_none_cleanup(void)
|
---|
1071 | { }
|
---|
1072 |
|
---|
1073 | int Curl_none_shutdown(struct Curl_cfilter *cf UNUSED_PARAM,
|
---|
1074 | struct Curl_easy *data UNUSED_PARAM)
|
---|
1075 | {
|
---|
1076 | (void)data;
|
---|
1077 | (void)cf;
|
---|
1078 | return 0;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | int Curl_none_check_cxn(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
1082 | {
|
---|
1083 | (void)cf;
|
---|
1084 | (void)data;
|
---|
1085 | return -1;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | CURLcode Curl_none_random(struct Curl_easy *data UNUSED_PARAM,
|
---|
1089 | unsigned char *entropy UNUSED_PARAM,
|
---|
1090 | size_t length UNUSED_PARAM)
|
---|
1091 | {
|
---|
1092 | (void)data;
|
---|
1093 | (void)entropy;
|
---|
1094 | (void)length;
|
---|
1095 | return CURLE_NOT_BUILT_IN;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | void Curl_none_close_all(struct Curl_easy *data UNUSED_PARAM)
|
---|
1099 | {
|
---|
1100 | (void)data;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | void Curl_none_session_free(void *ptr UNUSED_PARAM)
|
---|
1104 | {
|
---|
1105 | (void)ptr;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | bool Curl_none_data_pending(struct Curl_cfilter *cf UNUSED_PARAM,
|
---|
1109 | const struct Curl_easy *data UNUSED_PARAM)
|
---|
1110 | {
|
---|
1111 | (void)cf;
|
---|
1112 | (void)data;
|
---|
1113 | return 0;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | bool Curl_none_cert_status_request(void)
|
---|
1117 | {
|
---|
1118 | return FALSE;
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | CURLcode Curl_none_set_engine(struct Curl_easy *data UNUSED_PARAM,
|
---|
1122 | const char *engine UNUSED_PARAM)
|
---|
1123 | {
|
---|
1124 | (void)data;
|
---|
1125 | (void)engine;
|
---|
1126 | return CURLE_NOT_BUILT_IN;
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | CURLcode Curl_none_set_engine_default(struct Curl_easy *data UNUSED_PARAM)
|
---|
1130 | {
|
---|
1131 | (void)data;
|
---|
1132 | return CURLE_NOT_BUILT_IN;
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | struct curl_slist *Curl_none_engines_list(struct Curl_easy *data UNUSED_PARAM)
|
---|
1136 | {
|
---|
1137 | (void)data;
|
---|
1138 | return (struct curl_slist *)NULL;
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | bool Curl_none_false_start(void)
|
---|
1142 | {
|
---|
1143 | return FALSE;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | static int multissl_init(void)
|
---|
1147 | {
|
---|
1148 | if(multissl_setup(NULL))
|
---|
1149 | return 1;
|
---|
1150 | return Curl_ssl->init();
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | static CURLcode multissl_connect(struct Curl_cfilter *cf,
|
---|
1154 | struct Curl_easy *data)
|
---|
1155 | {
|
---|
1156 | if(multissl_setup(NULL))
|
---|
1157 | return CURLE_FAILED_INIT;
|
---|
1158 | return Curl_ssl->connect_blocking(cf, data);
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | static CURLcode multissl_connect_nonblocking(struct Curl_cfilter *cf,
|
---|
1162 | struct Curl_easy *data,
|
---|
1163 | bool *done)
|
---|
1164 | {
|
---|
1165 | if(multissl_setup(NULL))
|
---|
1166 | return CURLE_FAILED_INIT;
|
---|
1167 | return Curl_ssl->connect_nonblocking(cf, data, done);
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | static int multissl_get_select_socks(struct Curl_cfilter *cf,
|
---|
1171 | struct Curl_easy *data,
|
---|
1172 | curl_socket_t *socks)
|
---|
1173 | {
|
---|
1174 | if(multissl_setup(NULL))
|
---|
1175 | return 0;
|
---|
1176 | return Curl_ssl->get_select_socks(cf, data, socks);
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | static void *multissl_get_internals(struct ssl_connect_data *connssl,
|
---|
1180 | CURLINFO info)
|
---|
1181 | {
|
---|
1182 | if(multissl_setup(NULL))
|
---|
1183 | return NULL;
|
---|
1184 | return Curl_ssl->get_internals(connssl, info);
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | static void multissl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
1188 | {
|
---|
1189 | if(multissl_setup(NULL))
|
---|
1190 | return;
|
---|
1191 | Curl_ssl->close(cf, data);
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | static ssize_t multissl_recv_plain(struct Curl_cfilter *cf,
|
---|
1195 | struct Curl_easy *data,
|
---|
1196 | char *buf, size_t len, CURLcode *code)
|
---|
1197 | {
|
---|
1198 | if(multissl_setup(NULL))
|
---|
1199 | return CURLE_FAILED_INIT;
|
---|
1200 | return Curl_ssl->recv_plain(cf, data, buf, len, code);
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | static ssize_t multissl_send_plain(struct Curl_cfilter *cf,
|
---|
1204 | struct Curl_easy *data,
|
---|
1205 | const void *mem, size_t len,
|
---|
1206 | CURLcode *code)
|
---|
1207 | {
|
---|
1208 | if(multissl_setup(NULL))
|
---|
1209 | return CURLE_FAILED_INIT;
|
---|
1210 | return Curl_ssl->send_plain(cf, data, mem, len, code);
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | static const struct Curl_ssl Curl_ssl_multi = {
|
---|
1214 | { CURLSSLBACKEND_NONE, "multi" }, /* info */
|
---|
1215 | 0, /* supports nothing */
|
---|
1216 | (size_t)-1, /* something insanely large to be on the safe side */
|
---|
1217 |
|
---|
1218 | multissl_init, /* init */
|
---|
1219 | Curl_none_cleanup, /* cleanup */
|
---|
1220 | multissl_version, /* version */
|
---|
1221 | Curl_none_check_cxn, /* check_cxn */
|
---|
1222 | Curl_none_shutdown, /* shutdown */
|
---|
1223 | Curl_none_data_pending, /* data_pending */
|
---|
1224 | Curl_none_random, /* random */
|
---|
1225 | Curl_none_cert_status_request, /* cert_status_request */
|
---|
1226 | multissl_connect, /* connect */
|
---|
1227 | multissl_connect_nonblocking, /* connect_nonblocking */
|
---|
1228 | multissl_get_select_socks, /* getsock */
|
---|
1229 | multissl_get_internals, /* get_internals */
|
---|
1230 | multissl_close, /* close_one */
|
---|
1231 | Curl_none_close_all, /* close_all */
|
---|
1232 | Curl_none_session_free, /* session_free */
|
---|
1233 | Curl_none_set_engine, /* set_engine */
|
---|
1234 | Curl_none_set_engine_default, /* set_engine_default */
|
---|
1235 | Curl_none_engines_list, /* engines_list */
|
---|
1236 | Curl_none_false_start, /* false_start */
|
---|
1237 | NULL, /* sha256sum */
|
---|
1238 | NULL, /* associate_connection */
|
---|
1239 | NULL, /* disassociate_connection */
|
---|
1240 | NULL, /* free_multi_ssl_backend_data */
|
---|
1241 | multissl_recv_plain, /* recv decrypted data */
|
---|
1242 | multissl_send_plain, /* send data to encrypt */
|
---|
1243 | };
|
---|
1244 |
|
---|
1245 | const struct Curl_ssl *Curl_ssl =
|
---|
1246 | #if defined(CURL_WITH_MULTI_SSL)
|
---|
1247 | &Curl_ssl_multi;
|
---|
1248 | #elif defined(USE_WOLFSSL)
|
---|
1249 | &Curl_ssl_wolfssl;
|
---|
1250 | #elif defined(USE_SECTRANSP)
|
---|
1251 | &Curl_ssl_sectransp;
|
---|
1252 | #elif defined(USE_GNUTLS)
|
---|
1253 | &Curl_ssl_gnutls;
|
---|
1254 | #elif defined(USE_GSKIT)
|
---|
1255 | &Curl_ssl_gskit;
|
---|
1256 | #elif defined(USE_MBEDTLS)
|
---|
1257 | &Curl_ssl_mbedtls;
|
---|
1258 | #elif defined(USE_NSS)
|
---|
1259 | &Curl_ssl_nss;
|
---|
1260 | #elif defined(USE_RUSTLS)
|
---|
1261 | &Curl_ssl_rustls;
|
---|
1262 | #elif defined(USE_OPENSSL)
|
---|
1263 | &Curl_ssl_openssl;
|
---|
1264 | #elif defined(USE_SCHANNEL)
|
---|
1265 | &Curl_ssl_schannel;
|
---|
1266 | #elif defined(USE_BEARSSL)
|
---|
1267 | &Curl_ssl_bearssl;
|
---|
1268 | #else
|
---|
1269 | #error "Missing struct Curl_ssl for selected SSL backend"
|
---|
1270 | #endif
|
---|
1271 |
|
---|
1272 | static const struct Curl_ssl *available_backends[] = {
|
---|
1273 | #if defined(USE_WOLFSSL)
|
---|
1274 | &Curl_ssl_wolfssl,
|
---|
1275 | #endif
|
---|
1276 | #if defined(USE_SECTRANSP)
|
---|
1277 | &Curl_ssl_sectransp,
|
---|
1278 | #endif
|
---|
1279 | #if defined(USE_GNUTLS)
|
---|
1280 | &Curl_ssl_gnutls,
|
---|
1281 | #endif
|
---|
1282 | #if defined(USE_GSKIT)
|
---|
1283 | &Curl_ssl_gskit,
|
---|
1284 | #endif
|
---|
1285 | #if defined(USE_MBEDTLS)
|
---|
1286 | &Curl_ssl_mbedtls,
|
---|
1287 | #endif
|
---|
1288 | #if defined(USE_NSS)
|
---|
1289 | &Curl_ssl_nss,
|
---|
1290 | #endif
|
---|
1291 | #if defined(USE_OPENSSL)
|
---|
1292 | &Curl_ssl_openssl,
|
---|
1293 | #endif
|
---|
1294 | #if defined(USE_SCHANNEL)
|
---|
1295 | &Curl_ssl_schannel,
|
---|
1296 | #endif
|
---|
1297 | #if defined(USE_BEARSSL)
|
---|
1298 | &Curl_ssl_bearssl,
|
---|
1299 | #endif
|
---|
1300 | #if defined(USE_RUSTLS)
|
---|
1301 | &Curl_ssl_rustls,
|
---|
1302 | #endif
|
---|
1303 | NULL
|
---|
1304 | };
|
---|
1305 |
|
---|
1306 | static size_t multissl_version(char *buffer, size_t size)
|
---|
1307 | {
|
---|
1308 | static const struct Curl_ssl *selected;
|
---|
1309 | static char backends[200];
|
---|
1310 | static size_t backends_len;
|
---|
1311 | const struct Curl_ssl *current;
|
---|
1312 |
|
---|
1313 | current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl;
|
---|
1314 |
|
---|
1315 | if(current != selected) {
|
---|
1316 | char *p = backends;
|
---|
1317 | char *end = backends + sizeof(backends);
|
---|
1318 | int i;
|
---|
1319 |
|
---|
1320 | selected = current;
|
---|
1321 |
|
---|
1322 | backends[0] = '\0';
|
---|
1323 |
|
---|
1324 | for(i = 0; available_backends[i]; ++i) {
|
---|
1325 | char vb[200];
|
---|
1326 | bool paren = (selected != available_backends[i]);
|
---|
1327 |
|
---|
1328 | if(available_backends[i]->version(vb, sizeof(vb))) {
|
---|
1329 | p += msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""),
|
---|
1330 | (paren ? "(" : ""), vb, (paren ? ")" : ""));
|
---|
1331 | }
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | backends_len = p - backends;
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | if(!size)
|
---|
1338 | return 0;
|
---|
1339 |
|
---|
1340 | if(size <= backends_len) {
|
---|
1341 | strncpy(buffer, backends, size - 1);
|
---|
1342 | buffer[size - 1] = '\0';
|
---|
1343 | return size - 1;
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | strcpy(buffer, backends);
|
---|
1347 | return backends_len;
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 | static int multissl_setup(const struct Curl_ssl *backend)
|
---|
1351 | {
|
---|
1352 | const char *env;
|
---|
1353 | char *env_tmp;
|
---|
1354 |
|
---|
1355 | if(Curl_ssl != &Curl_ssl_multi)
|
---|
1356 | return 1;
|
---|
1357 |
|
---|
1358 | if(backend) {
|
---|
1359 | Curl_ssl = backend;
|
---|
1360 | return 0;
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | if(!available_backends[0])
|
---|
1364 | return 1;
|
---|
1365 |
|
---|
1366 | env = env_tmp = curl_getenv("CURL_SSL_BACKEND");
|
---|
1367 | #ifdef CURL_DEFAULT_SSL_BACKEND
|
---|
1368 | if(!env)
|
---|
1369 | env = CURL_DEFAULT_SSL_BACKEND;
|
---|
1370 | #endif
|
---|
1371 | if(env) {
|
---|
1372 | int i;
|
---|
1373 | for(i = 0; available_backends[i]; i++) {
|
---|
1374 | if(strcasecompare(env, available_backends[i]->info.name)) {
|
---|
1375 | Curl_ssl = available_backends[i];
|
---|
1376 | free(env_tmp);
|
---|
1377 | return 0;
|
---|
1378 | }
|
---|
1379 | }
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | /* Fall back to first available backend */
|
---|
1383 | Curl_ssl = available_backends[0];
|
---|
1384 | free(env_tmp);
|
---|
1385 | return 0;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | /* This function is used to select the SSL backend to use. It is called by
|
---|
1389 | curl_global_sslset (easy.c) which uses the global init lock. */
|
---|
1390 | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name,
|
---|
1391 | const curl_ssl_backend ***avail)
|
---|
1392 | {
|
---|
1393 | int i;
|
---|
1394 |
|
---|
1395 | if(avail)
|
---|
1396 | *avail = (const curl_ssl_backend **)&available_backends;
|
---|
1397 |
|
---|
1398 | if(Curl_ssl != &Curl_ssl_multi)
|
---|
1399 | return id == Curl_ssl->info.id ||
|
---|
1400 | (name && strcasecompare(name, Curl_ssl->info.name)) ?
|
---|
1401 | CURLSSLSET_OK :
|
---|
1402 | #if defined(CURL_WITH_MULTI_SSL)
|
---|
1403 | CURLSSLSET_TOO_LATE;
|
---|
1404 | #else
|
---|
1405 | CURLSSLSET_UNKNOWN_BACKEND;
|
---|
1406 | #endif
|
---|
1407 |
|
---|
1408 | for(i = 0; available_backends[i]; i++) {
|
---|
1409 | if(available_backends[i]->info.id == id ||
|
---|
1410 | (name && strcasecompare(available_backends[i]->info.name, name))) {
|
---|
1411 | multissl_setup(available_backends[i]);
|
---|
1412 | return CURLSSLSET_OK;
|
---|
1413 | }
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | return CURLSSLSET_UNKNOWN_BACKEND;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | #else /* USE_SSL */
|
---|
1420 | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name,
|
---|
1421 | const curl_ssl_backend ***avail)
|
---|
1422 | {
|
---|
1423 | (void)id;
|
---|
1424 | (void)name;
|
---|
1425 | (void)avail;
|
---|
1426 | return CURLSSLSET_NO_BACKENDS;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | #endif /* !USE_SSL */
|
---|
1430 |
|
---|
1431 | #ifdef USE_SSL
|
---|
1432 |
|
---|
1433 | static void cf_close(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
1434 | {
|
---|
1435 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
1436 | /* TODO: close_one closes BOTH conn->ssl AND conn->proxy_ssl for this
|
---|
1437 | * sockindex (if in use). Gladly, it is safe to call more than once. */
|
---|
1438 | if(connssl) {
|
---|
1439 | Curl_ssl->close(cf, data);
|
---|
1440 | connssl->state = ssl_connection_none;
|
---|
1441 | }
|
---|
1442 | cf->connected = FALSE;
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | static void reinit_hostname(struct Curl_cfilter *cf)
|
---|
1446 | {
|
---|
1447 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
1448 |
|
---|
1449 | #ifndef CURL_DISABLE_PROXY
|
---|
1450 | if(Curl_ssl_cf_is_proxy(cf)) {
|
---|
1451 | /* TODO: there is not definition for a proxy setup on a secondary conn */
|
---|
1452 | connssl->hostname = cf->conn->http_proxy.host.name;
|
---|
1453 | connssl->dispname = cf->conn->http_proxy.host.dispname;
|
---|
1454 | connssl->port = cf->conn->http_proxy.port;
|
---|
1455 | }
|
---|
1456 | else
|
---|
1457 | #endif
|
---|
1458 | {
|
---|
1459 | /* TODO: secondaryhostname is set to the IP address we connect to
|
---|
1460 | * in the FTP handler, it is assumed that host verification uses the
|
---|
1461 | * hostname from FIRSTSOCKET */
|
---|
1462 | if(cf->sockindex == SECONDARYSOCKET && 0) {
|
---|
1463 | connssl->hostname = cf->conn->secondaryhostname;
|
---|
1464 | connssl->dispname = connssl->hostname;
|
---|
1465 | connssl->port = cf->conn->secondary_port;
|
---|
1466 | }
|
---|
1467 | else {
|
---|
1468 | connssl->hostname = cf->conn->host.name;
|
---|
1469 | connssl->dispname = cf->conn->host.dispname;
|
---|
1470 | connssl->port = cf->conn->remote_port;
|
---|
1471 | }
|
---|
1472 | }
|
---|
1473 | DEBUGASSERT(connssl->hostname);
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | static void ssl_cf_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
1477 | {
|
---|
1478 | cf_ctx_set_data(cf, data);
|
---|
1479 | cf_close(cf, data);
|
---|
1480 | cf_ctx_free(cf->ctx);
|
---|
1481 | cf->ctx = NULL;
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | static void ssl_cf_close(struct Curl_cfilter *cf,
|
---|
1485 | struct Curl_easy *data)
|
---|
1486 | {
|
---|
1487 | cf_ctx_set_data(cf, data);
|
---|
1488 | cf_close(cf, data);
|
---|
1489 | cf->next->cft->close(cf->next, data);
|
---|
1490 | cf_ctx_set_data(cf, NULL);
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | static CURLcode ssl_cf_connect(struct Curl_cfilter *cf,
|
---|
1494 | struct Curl_easy *data,
|
---|
1495 | bool blocking, bool *done)
|
---|
1496 | {
|
---|
1497 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
1498 | CURLcode result;
|
---|
1499 |
|
---|
1500 | if(cf->connected) {
|
---|
1501 | *done = TRUE;
|
---|
1502 | return CURLE_OK;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | cf_ctx_set_data(cf, data);
|
---|
1506 | (void)connssl;
|
---|
1507 | DEBUGASSERT(data->conn);
|
---|
1508 | DEBUGASSERT(data->conn == cf->conn);
|
---|
1509 | DEBUGASSERT(connssl);
|
---|
1510 | DEBUGASSERT(cf->conn->host.name);
|
---|
1511 |
|
---|
1512 | result = cf->next->cft->connect(cf->next, data, blocking, done);
|
---|
1513 | if(result || !*done)
|
---|
1514 | goto out;
|
---|
1515 |
|
---|
1516 | /* TODO: right now we do not fully control when hostname is set,
|
---|
1517 | * assign it on each connect call. */
|
---|
1518 | reinit_hostname(cf);
|
---|
1519 | *done = FALSE;
|
---|
1520 |
|
---|
1521 | if(blocking) {
|
---|
1522 | result = ssl_connect(cf, data);
|
---|
1523 | *done = (result == CURLE_OK);
|
---|
1524 | }
|
---|
1525 | else {
|
---|
1526 | result = ssl_connect_nonblocking(cf, data, done);
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 | if(!result && *done) {
|
---|
1530 | cf->connected = TRUE;
|
---|
1531 | if(cf->sockindex == FIRSTSOCKET && !Curl_ssl_cf_is_proxy(cf))
|
---|
1532 | Curl_pgrsTime(data, TIMER_APPCONNECT); /* SSL is connected */
|
---|
1533 | DEBUGASSERT(connssl->state == ssl_connection_complete);
|
---|
1534 | }
|
---|
1535 | out:
|
---|
1536 | cf_ctx_set_data(cf, NULL);
|
---|
1537 | return result;
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | static bool ssl_cf_data_pending(struct Curl_cfilter *cf,
|
---|
1541 | const struct Curl_easy *data)
|
---|
1542 | {
|
---|
1543 | bool result;
|
---|
1544 |
|
---|
1545 | cf_ctx_set_data(cf, (struct Curl_easy *)data);
|
---|
1546 | if(cf->ctx && Curl_ssl->data_pending(cf, data))
|
---|
1547 | result = TRUE;
|
---|
1548 | else
|
---|
1549 | result = cf->next->cft->has_data_pending(cf->next, data);
|
---|
1550 | cf_ctx_set_data(cf, NULL);
|
---|
1551 | return result;
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | static ssize_t ssl_cf_send(struct Curl_cfilter *cf,
|
---|
1555 | struct Curl_easy *data, const void *buf, size_t len,
|
---|
1556 | CURLcode *err)
|
---|
1557 | {
|
---|
1558 | ssize_t nwritten;
|
---|
1559 |
|
---|
1560 | *err = CURLE_OK;
|
---|
1561 | cf_ctx_set_data(cf, data);
|
---|
1562 | nwritten = Curl_ssl->send_plain(cf, data, buf, len, err);
|
---|
1563 | cf_ctx_set_data(cf, NULL);
|
---|
1564 | return nwritten;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | static ssize_t ssl_cf_recv(struct Curl_cfilter *cf,
|
---|
1568 | struct Curl_easy *data, char *buf, size_t len,
|
---|
1569 | CURLcode *err)
|
---|
1570 | {
|
---|
1571 | ssize_t nread;
|
---|
1572 |
|
---|
1573 | *err = CURLE_OK;
|
---|
1574 | cf_ctx_set_data(cf, data);
|
---|
1575 | nread = Curl_ssl->recv_plain(cf, data, buf, len, err);
|
---|
1576 | cf_ctx_set_data(cf, NULL);
|
---|
1577 | return nread;
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | static int ssl_cf_get_select_socks(struct Curl_cfilter *cf,
|
---|
1581 | struct Curl_easy *data,
|
---|
1582 | curl_socket_t *socks)
|
---|
1583 | {
|
---|
1584 | int result;
|
---|
1585 |
|
---|
1586 | cf_ctx_set_data(cf, data);
|
---|
1587 | result = Curl_ssl->get_select_socks(cf, data, socks);
|
---|
1588 | cf_ctx_set_data(cf, NULL);
|
---|
1589 | return result;
|
---|
1590 | }
|
---|
1591 |
|
---|
1592 | static void ssl_cf_attach_data(struct Curl_cfilter *cf,
|
---|
1593 | struct Curl_easy *data)
|
---|
1594 | {
|
---|
1595 | if(Curl_ssl->attach_data) {
|
---|
1596 | cf_ctx_set_data(cf, data);
|
---|
1597 | Curl_ssl->attach_data(cf, data);
|
---|
1598 | cf_ctx_set_data(cf, NULL);
|
---|
1599 | }
|
---|
1600 | }
|
---|
1601 |
|
---|
1602 | static void ssl_cf_detach_data(struct Curl_cfilter *cf,
|
---|
1603 | struct Curl_easy *data)
|
---|
1604 | {
|
---|
1605 | if(Curl_ssl->detach_data) {
|
---|
1606 | cf_ctx_set_data(cf, data);
|
---|
1607 | Curl_ssl->detach_data(cf, data);
|
---|
1608 | cf_ctx_set_data(cf, NULL);
|
---|
1609 | }
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | static const struct Curl_cftype cft_ssl = {
|
---|
1613 | "SSL",
|
---|
1614 | CF_TYPE_SSL,
|
---|
1615 | ssl_cf_destroy,
|
---|
1616 | Curl_cf_def_setup,
|
---|
1617 | ssl_cf_connect,
|
---|
1618 | ssl_cf_close,
|
---|
1619 | Curl_cf_def_get_host,
|
---|
1620 | ssl_cf_get_select_socks,
|
---|
1621 | ssl_cf_data_pending,
|
---|
1622 | ssl_cf_send,
|
---|
1623 | ssl_cf_recv,
|
---|
1624 | ssl_cf_attach_data,
|
---|
1625 | ssl_cf_detach_data,
|
---|
1626 | };
|
---|
1627 |
|
---|
1628 | static const struct Curl_cftype cft_ssl_proxy = {
|
---|
1629 | "SSL-PROXY",
|
---|
1630 | CF_TYPE_SSL,
|
---|
1631 | ssl_cf_destroy,
|
---|
1632 | Curl_cf_def_setup,
|
---|
1633 | ssl_cf_connect,
|
---|
1634 | ssl_cf_close,
|
---|
1635 | Curl_cf_def_get_host,
|
---|
1636 | ssl_cf_get_select_socks,
|
---|
1637 | ssl_cf_data_pending,
|
---|
1638 | ssl_cf_send,
|
---|
1639 | ssl_cf_recv,
|
---|
1640 | ssl_cf_attach_data,
|
---|
1641 | ssl_cf_detach_data,
|
---|
1642 | };
|
---|
1643 |
|
---|
1644 | CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data,
|
---|
1645 | struct connectdata *conn,
|
---|
1646 | int sockindex)
|
---|
1647 | {
|
---|
1648 | struct Curl_cfilter *cf;
|
---|
1649 | struct ssl_connect_data *ctx;
|
---|
1650 | CURLcode result;
|
---|
1651 |
|
---|
1652 | DEBUGASSERT(data->conn);
|
---|
1653 | ctx = cf_ctx_new(data);
|
---|
1654 | if(!ctx) {
|
---|
1655 | result = CURLE_OUT_OF_MEMORY;
|
---|
1656 | goto out;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | result = Curl_cf_create(&cf, &cft_ssl, ctx);
|
---|
1660 | if(result)
|
---|
1661 | goto out;
|
---|
1662 |
|
---|
1663 | Curl_conn_cf_add(data, conn, sockindex, cf);
|
---|
1664 |
|
---|
1665 | result = CURLE_OK;
|
---|
1666 |
|
---|
1667 | out:
|
---|
1668 | if(result)
|
---|
1669 | cf_ctx_free(ctx);
|
---|
1670 | return result;
|
---|
1671 | }
|
---|
1672 |
|
---|
1673 | #ifndef CURL_DISABLE_PROXY
|
---|
1674 | CURLcode Curl_ssl_cfilter_proxy_add(struct Curl_easy *data,
|
---|
1675 | struct connectdata *conn,
|
---|
1676 | int sockindex)
|
---|
1677 | {
|
---|
1678 | struct Curl_cfilter *cf;
|
---|
1679 | struct ssl_connect_data *ctx;
|
---|
1680 | CURLcode result;
|
---|
1681 |
|
---|
1682 | ctx = cf_ctx_new(data);
|
---|
1683 | if(!ctx) {
|
---|
1684 | result = CURLE_OUT_OF_MEMORY;
|
---|
1685 | goto out;
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | result = Curl_cf_create(&cf, &cft_ssl_proxy, ctx);
|
---|
1689 | if(result)
|
---|
1690 | goto out;
|
---|
1691 |
|
---|
1692 | Curl_conn_cf_add(data, conn, sockindex, cf);
|
---|
1693 |
|
---|
1694 | result = CURLE_OK;
|
---|
1695 |
|
---|
1696 | out:
|
---|
1697 | if(result)
|
---|
1698 | cf_ctx_free(ctx);
|
---|
1699 | return result;
|
---|
1700 | }
|
---|
1701 |
|
---|
1702 | #endif /* !CURL_DISABLE_PROXY */
|
---|
1703 |
|
---|
1704 | bool Curl_ssl_supports(struct Curl_easy *data, int option)
|
---|
1705 | {
|
---|
1706 | (void)data;
|
---|
1707 | return (Curl_ssl->supports & option)? TRUE : FALSE;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | void *Curl_ssl_get_internals(struct Curl_easy *data, int sockindex,
|
---|
1711 | CURLINFO info, int n)
|
---|
1712 | {
|
---|
1713 | void *result = NULL;
|
---|
1714 | (void)n;
|
---|
1715 | if(data->conn) {
|
---|
1716 | struct Curl_cfilter *cf;
|
---|
1717 | /* get first filter in chain, if any is present */
|
---|
1718 | cf = Curl_ssl_cf_get_ssl(data->conn->cfilter[sockindex]);
|
---|
1719 | if(cf) {
|
---|
1720 | cf_ctx_set_data(cf, data);
|
---|
1721 | result = Curl_ssl->get_internals(cf->ctx, info);
|
---|
1722 | cf_ctx_set_data(cf, NULL);
|
---|
1723 | }
|
---|
1724 | }
|
---|
1725 | return result;
|
---|
1726 | }
|
---|
1727 |
|
---|
1728 | CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data,
|
---|
1729 | int sockindex)
|
---|
1730 | {
|
---|
1731 | struct Curl_cfilter *cf = data->conn? data->conn->cfilter[sockindex] : NULL;
|
---|
1732 | CURLcode result = CURLE_OK;
|
---|
1733 |
|
---|
1734 | (void)data;
|
---|
1735 | for(; cf; cf = cf->next) {
|
---|
1736 | if(cf->cft == &cft_ssl) {
|
---|
1737 | if(Curl_ssl->shut_down(cf, data))
|
---|
1738 | result = CURLE_SSL_SHUTDOWN_FAILED;
|
---|
1739 | Curl_conn_cf_discard(cf, data);
|
---|
1740 | break;
|
---|
1741 | }
|
---|
1742 | }
|
---|
1743 | return result;
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 | static struct Curl_cfilter *get_ssl_cf_engaged(struct connectdata *conn,
|
---|
1747 | int sockindex)
|
---|
1748 | {
|
---|
1749 | struct Curl_cfilter *cf, *lowest_ssl_cf = NULL;
|
---|
1750 |
|
---|
1751 | for(cf = conn->cfilter[sockindex]; cf; cf = cf->next) {
|
---|
1752 | if(cf->cft == &cft_ssl || cf->cft == &cft_ssl_proxy) {
|
---|
1753 | lowest_ssl_cf = cf;
|
---|
1754 | if(cf->connected || (cf->next && cf->next->connected)) {
|
---|
1755 | /* connected or about to start */
|
---|
1756 | return cf;
|
---|
1757 | }
|
---|
1758 | }
|
---|
1759 | }
|
---|
1760 | return lowest_ssl_cf;
|
---|
1761 | }
|
---|
1762 |
|
---|
1763 | bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf)
|
---|
1764 | {
|
---|
1765 | return (cf->cft == &cft_ssl_proxy);
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | struct ssl_config_data *
|
---|
1769 | Curl_ssl_cf_get_config(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
1770 | {
|
---|
1771 | #ifdef CURL_DISABLE_PROXY
|
---|
1772 | (void)cf;
|
---|
1773 | return &data->set.ssl;
|
---|
1774 | #else
|
---|
1775 | return Curl_ssl_cf_is_proxy(cf)? &data->set.proxy_ssl : &data->set.ssl;
|
---|
1776 | #endif
|
---|
1777 | }
|
---|
1778 |
|
---|
1779 | struct ssl_config_data *
|
---|
1780 | Curl_ssl_get_config(struct Curl_easy *data, int sockindex)
|
---|
1781 | {
|
---|
1782 | struct Curl_cfilter *cf;
|
---|
1783 |
|
---|
1784 | (void)data;
|
---|
1785 | DEBUGASSERT(data->conn);
|
---|
1786 | cf = get_ssl_cf_engaged(data->conn, sockindex);
|
---|
1787 | return cf? Curl_ssl_cf_get_config(cf, data) : &data->set.ssl;
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 | struct ssl_primary_config *
|
---|
1791 | Curl_ssl_cf_get_primary_config(struct Curl_cfilter *cf)
|
---|
1792 | {
|
---|
1793 | #ifdef CURL_DISABLE_PROXY
|
---|
1794 | return &cf->conn->ssl_config;
|
---|
1795 | #else
|
---|
1796 | return Curl_ssl_cf_is_proxy(cf)?
|
---|
1797 | &cf->conn->proxy_ssl_config : &cf->conn->ssl_config;
|
---|
1798 | #endif
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | struct ssl_primary_config *
|
---|
1802 | Curl_ssl_get_primary_config(struct Curl_easy *data,
|
---|
1803 | struct connectdata *conn,
|
---|
1804 | int sockindex)
|
---|
1805 | {
|
---|
1806 | struct Curl_cfilter *cf;
|
---|
1807 |
|
---|
1808 | (void)data;
|
---|
1809 | DEBUGASSERT(conn);
|
---|
1810 | cf = get_ssl_cf_engaged(conn, sockindex);
|
---|
1811 | return cf? Curl_ssl_cf_get_primary_config(cf) : NULL;
|
---|
1812 | }
|
---|
1813 |
|
---|
1814 | struct Curl_cfilter *Curl_ssl_cf_get_ssl(struct Curl_cfilter *cf)
|
---|
1815 | {
|
---|
1816 | for(; cf; cf = cf->next) {
|
---|
1817 | if(cf->cft == &cft_ssl || cf->cft == &cft_ssl_proxy)
|
---|
1818 | return cf;
|
---|
1819 | }
|
---|
1820 | return NULL;
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 | #endif /* USE_SSL */
|
---|