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