Debian Codegraph @

deps/curl/lib/vtls/gtls.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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.haxx.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 /*
24 * Source file for all GnuTLS-specific code for the TLS/SSL layer. No code
25 * but vtls.c should ever call or use these functions.
26 *
27 * Note: don't use the GnuTLS' *_t variable type names in this source code,
28 * since they were not present in 1.0.X.
29 */
30
31 #include "curl_setup.h"
32
33 #ifdef USE_GNUTLS
34
35 #include <gnutls/abstract.h>
36 #include <gnutls/gnutls.h>
37 #include <gnutls/x509.h>
38
39 #ifdef USE_GNUTLS_NETTLE
40 #include <gnutls/crypto.h>
41 #include <nettle/md5.h>
42 #include <nettle/sha2.h>
43 #else
44 #include <gcrypt.h>
45 #endif
46
47 #include "urldata.h"
48 #include "sendf.h"
49 #include "inet_pton.h"
50 #include "gtls.h"
51 #include "vtls.h"
52 #include "parsedate.h"
53 #include "connect.h" /* for the connect timeout */
54 #include "select.h"
55 #include "strcase.h"
56 #include "warnless.h"
57 #include "x509asn1.h"
58 #include "multiif.h"
59 #include "curl_printf.h"
60 #include "curl_memory.h"
61 /* The last #include file should be: */
62 #include "memdebug.h"
63
64 /* Enable GnuTLS debugging by defining GTLSDEBUG */
65 /*#define GTLSDEBUG */
66
67 #ifdef GTLSDEBUG
68 static void tls_log_func(int level, const char *str)
69 {
70 fprintf(stderr, "|<%d>| %s", level, str);
71 }
72 #endif
73 static bool gtls_inited = FALSE;
74
75 #if defined(GNUTLS_VERSION_NUMBER)
76 # if (GNUTLS_VERSION_NUMBER >= 0x020c00)
77 # undef gnutls_transport_set_lowat
78 # define gnutls_transport_set_lowat(A,B) Curl_nop_stmt
79 # define USE_GNUTLS_PRIORITY_SET_DIRECT 1
80 # endif
81 # if (GNUTLS_VERSION_NUMBER >= 0x020c03)
82 # define GNUTLS_MAPS_WINSOCK_ERRORS 1
83 # endif
84
85 # if HAVE_GNUTLS_ALPN_SET_PROTOCOLS
86 # define HAS_ALPN
87 # endif
88
89 # if HAVE_GNUTLS_OCSP_REQ_INIT
90 # define HAS_OCSP
91 # endif
92
93 # if (GNUTLS_VERSION_NUMBER >= 0x030306)
94 # define HAS_CAPATH
95 # endif
96 #endif
97
98 #if (GNUTLS_VERSION_NUMBER >= 0x030603)
99 #define HAS_TLS13
100 #endif
101
102 #ifdef HAS_OCSP
103 # include <gnutls/ocsp.h>
104 #endif
105
106 struct ssl_backend_data {
107 gnutls_session_t session;
108 gnutls_certificate_credentials_t cred;
109 #ifdef USE_TLS_SRP
110 gnutls_srp_client_credentials_t srp_client_cred;
111 #endif
112 };
113
114 #define BACKEND connssl->backend
115
116 /*
117 * Custom push and pull callback functions used by GNU TLS to read and write
118 * to the socket. These functions are simple wrappers to send() and recv()
119 * (although here using the sread/swrite macros as defined by
120 * curl_setup_once.h).
121 * We use custom functions rather than the GNU TLS defaults because it allows
122 * us to get specific about the fourth "flags" argument, and to use arbitrary
123 * private data with gnutls_transport_set_ptr if we wish.
124 *
125 * When these custom push and pull callbacks fail, GNU TLS checks its own
126 * session-specific error variable, and when not set also its own global
127 * errno variable, in order to take appropriate action. GNU TLS does not
128 * require that the transport is actually a socket. This implies that for
129 * Windows builds these callbacks should ideally set the session-specific
130 * error variable using function gnutls_transport_set_errno or as a last
131 * resort global errno variable using gnutls_transport_set_global_errno,
132 * with a transport agnostic error value. This implies that some winsock
133 * error translation must take place in these callbacks.
134 *
135 * Paragraph above applies to GNU TLS versions older than 2.12.3, since
136 * this version GNU TLS does its own internal winsock error translation
137 * using system_errno() function.
138 */
139
140 #if defined(USE_WINSOCK) && !defined(GNUTLS_MAPS_WINSOCK_ERRORS)
141 # define gtls_EINTR 4
142 # define gtls_EIO 5
143 # define gtls_EAGAIN 11
144 static int gtls_mapped_sockerrno(void)
145 {
146 switch(SOCKERRNO) {
147 case WSAEWOULDBLOCK:
148 return gtls_EAGAIN;
149 case WSAEINTR:
150 return gtls_EINTR;
151 default:
152 break;
153 }
154 return gtls_EIO;
155 }
156 #endif
157
158 static ssize_t Curl_gtls_push(void *s, const void *buf, size_t len)
159 {
160 curl_socket_t sock = *(curl_socket_t *)s;
161 ssize_t ret = swrite(sock, buf, len);
162 #if defined(USE_WINSOCK) && !defined(GNUTLS_MAPS_WINSOCK_ERRORS)
163 if(ret < 0)
164 gnutls_transport_set_global_errno(gtls_mapped_sockerrno());
165 #endif
166 return ret;
167 }
168
169 static ssize_t Curl_gtls_pull(void *s, void *buf, size_t len)
170 {
171 curl_socket_t sock = *(curl_socket_t *)s;
172 ssize_t ret = sread(sock, buf, len);
173 #if defined(USE_WINSOCK) && !defined(GNUTLS_MAPS_WINSOCK_ERRORS)
174 if(ret < 0)
175 gnutls_transport_set_global_errno(gtls_mapped_sockerrno());
176 #endif
177 return ret;
178 }
179
180 static ssize_t Curl_gtls_push_ssl(void *s, const void *buf, size_t len)
181 {
182 return gnutls_record_send((gnutls_session_t) s, buf, len);
183 }
184
185 static ssize_t Curl_gtls_pull_ssl(void *s, void *buf, size_t len)
186 {
187 return gnutls_record_recv((gnutls_session_t) s, buf, len);
188 }
189
190 /* Curl_gtls_init()
191 *
192 * Global GnuTLS init, called from Curl_ssl_init(). This calls functions that
193 * are not thread-safe and thus this function itself is not thread-safe and
194 * must only be called from within curl_global_init() to keep the thread
195 * situation under control!
196 */
197 static int Curl_gtls_init(void)
198 {
199 int ret = 1;
200 if(!gtls_inited) {
201 ret = gnutls_global_init()?0:1;
202 #ifdef GTLSDEBUG
203 gnutls_global_set_log_function(tls_log_func);
204 gnutls_global_set_log_level(2);
205 #endif
206 gtls_inited = TRUE;
207 }
208 return ret;
209 }
210
211 static void Curl_gtls_cleanup(void)
212 {
213 if(gtls_inited) {
214 gnutls_global_deinit();
215 gtls_inited = FALSE;
216 }
217 }
218
219 #ifndef CURL_DISABLE_VERBOSE_STRINGS
220 static void showtime(struct Curl_easy *data,
221 const char *text,
222 time_t stamp)
223 {
224 struct tm buffer;
225 const struct tm *tm = &buffer;
226 char str[96];
227 CURLcode result = Curl_gmtime(stamp, &buffer);
228 if(result)
229 return;
230
231 msnprintf(str,
232 sizeof(str),
233 "\t %s: %s, %02d %s %4d %02d:%02d:%02d GMT",
234 text,
235 Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
236 tm->tm_mday,
237 Curl_month[tm->tm_mon],
238 tm->tm_year + 1900,
239 tm->tm_hour,
240 tm->tm_min,
241 tm->tm_sec);
242 infof(data, "%s\n", str);
243 }
244 #endif
245
246 static gnutls_datum_t load_file(const char *file)
247 {
248 FILE *f;
249 gnutls_datum_t loaded_file = { NULL, 0 };
250 long filelen;
251 void *ptr;
252
253 f = fopen(file, "rb");
254 if(!f)
255 return loaded_file;
256 if(fseek(f, 0, SEEK_END) != 0
257 || (filelen = ftell(f)) < 0
258 || fseek(f, 0, SEEK_SET) != 0
259 || !(ptr = malloc((size_t)filelen)))
260 goto out;
261 if(fread(ptr, 1, (size_t)filelen, f) < (size_t)filelen) {
262 free(ptr);
263 goto out;
264 }
265
266 loaded_file.data = ptr;
267 loaded_file.size = (unsigned int)filelen;
268 out:
269 fclose(f);
270 return loaded_file;
271 }
272
273 static void unload_file(gnutls_datum_t data)
274 {
275 free(data.data);
276 }
277
278
279 /* this function does a SSL/TLS (re-)handshake */
280 static CURLcode handshake(struct connectdata *conn,
281 int sockindex,
282 bool duringconnect,
283 bool nonblocking)
284 {
285 struct Curl_easy *data = conn->data;
286 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
287 gnutls_session_t session = BACKEND->session;
288 curl_socket_t sockfd = conn->sock[sockindex];
289
290 for(;;) {
291 timediff_t timeout_ms;
292 int rc;
293
294 /* check allowed time left */
295 timeout_ms = Curl_timeleft(data, NULL, duringconnect);
296
297 if(timeout_ms < 0) {
298 /* no need to continue if time already is up */
299 failf(data, "SSL connection timeout");
300 return CURLE_OPERATION_TIMEDOUT;
301 }
302
303 /* if ssl is expecting something, check if it's available. */
304 if(connssl->connecting_state == ssl_connect_2_reading
305 || connssl->connecting_state == ssl_connect_2_writing) {
306 int what;
307 curl_socket_t writefd = ssl_connect_2_writing ==
308 connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
309 curl_socket_t readfd = ssl_connect_2_reading ==
310 connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
311
312 what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
313 nonblocking?0:
314 timeout_ms?(time_t)timeout_ms:1000);
315 if(what < 0) {
316 /* fatal error */
317 failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
318 return CURLE_SSL_CONNECT_ERROR;
319 }
320 else if(0 == what) {
321 if(nonblocking)
322 return CURLE_OK;
323 else if(timeout_ms) {
324 /* timeout */
325 failf(data, "SSL connection timeout at %ld", (long)timeout_ms);
326 return CURLE_OPERATION_TIMEDOUT;
327 }
328 }
329 /* socket is readable or writable */
330 }
331
332 rc = gnutls_handshake(session);
333
334 if((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED)) {
335 connssl->connecting_state =
336 gnutls_record_get_direction(session)?
337 ssl_connect_2_writing:ssl_connect_2_reading;
338 continue;
339 }
340 else if((rc < 0) && !gnutls_error_is_fatal(rc)) {
341 const char *strerr = NULL;
342
343 if(rc == GNUTLS_E_WARNING_ALERT_RECEIVED) {
344 int alert = gnutls_alert_get(session);
345 strerr = gnutls_alert_get_name(alert);
346 }
347
348 if(strerr == NULL)
349 strerr = gnutls_strerror(rc);
350
351 infof(data, "gnutls_handshake() warning: %s\n", strerr);
352 continue;
353 }
354 else if(rc < 0) {
355 const char *strerr = NULL;
356
357 if(rc == GNUTLS_E_FATAL_ALERT_RECEIVED) {
358 int alert = gnutls_alert_get(session);
359 strerr = gnutls_alert_get_name(alert);
360 }
361
362 if(strerr == NULL)
363 strerr = gnutls_strerror(rc);
364
365 failf(data, "gnutls_handshake() failed: %s", strerr);
366 return CURLE_SSL_CONNECT_ERROR;
367 }
368
369 /* Reset our connect state machine */
370 connssl->connecting_state = ssl_connect_1;
371 return CURLE_OK;
372 }
373 }
374
375 static gnutls_x509_crt_fmt_t do_file_type(const char *type)
376 {
377 if(!type || !type[0])
378 return GNUTLS_X509_FMT_PEM;
379 if(strcasecompare(type, "PEM"))
380 return GNUTLS_X509_FMT_PEM;
381 if(strcasecompare(type, "DER"))
382 return GNUTLS_X509_FMT_DER;
383 return -1;
384 }
385
386 #ifndef USE_GNUTLS_PRIORITY_SET_DIRECT
387 static CURLcode
388 set_ssl_version_min_max(int *list, size_t list_size, struct connectdata *conn)
389 {
390 struct Curl_easy *data = conn->data;
391 long ssl_version = SSL_CONN_CONFIG(version);
392 long ssl_version_max = SSL_CONN_CONFIG(version_max);
393 long i = ssl_version;
394 long protocol_priority_idx = 0;
395
396 switch(ssl_version_max) {
397 case CURL_SSLVERSION_MAX_NONE:
398 case CURL_SSLVERSION_MAX_DEFAULT:
399 #ifdef HAS_TLS13
400 ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_3;
401 #endif
402 ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2;
403 break;
404 }
405
406 for(; i <= (ssl_version_max >> 16) &&
407 protocol_priority_idx < list_size; ++i) {
408 switch(i) {
409 case CURL_SSLVERSION_TLSv1_0:
410 protocol_priority[protocol_priority_idx++] = GNUTLS_TLS1_0;
411 break;
412 case CURL_SSLVERSION_TLSv1_1:
413 protocol_priority[protocol_priority_idx++] = GNUTLS_TLS1_1;
414 break;
415 case CURL_SSLVERSION_TLSv1_2:
416 protocol_priority[protocol_priority_idx++] = GNUTLS_TLS1_2;
417 break;
418 case CURL_SSLVERSION_TLSv1_3:
419 #ifdef HAS_TLS13
420 protocol_priority[protocol_priority_idx++] = GNUTLS_TLS1_3;
421 break;
422 #else
423 failf(data, "GnuTLS: TLS 1.3 is not yet supported");
424 return CURLE_SSL_CONNECT_ERROR;
425 #endif
426 }
427 }
428 return CURLE_OK;
429 }
430 #else
431 #define GNUTLS_CIPHERS "NORMAL:-ARCFOUR-128:-CTYPE-ALL:+CTYPE-X509"
432 /* If GnuTLS was compiled without support for SRP it will error out if SRP is
433 requested in the priority string, so treat it specially
434 */
435 #define GNUTLS_SRP "+SRP"
436
437 static CURLcode
438 set_ssl_version_min_max(const char **prioritylist, struct connectdata *conn)
439 {
440 struct Curl_easy *data = conn->data;
441 long ssl_version = SSL_CONN_CONFIG(version);
442 long ssl_version_max = SSL_CONN_CONFIG(version_max);
443
444 if(ssl_version_max == CURL_SSLVERSION_MAX_NONE) {
445 ssl_version_max = CURL_SSLVERSION_MAX_DEFAULT;
446 }
447 switch(ssl_version | ssl_version_max) {
448 case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_0:
449 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
450 "+VERS-TLS1.0:" GNUTLS_SRP;
451 return CURLE_OK;
452 case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_1:
453 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
454 "+VERS-TLS1.0:+VERS-TLS1.1:" GNUTLS_SRP;
455 return CURLE_OK;
456 case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_2:
457 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
458 "+VERS-TLS1.0:+VERS-TLS1.1:+VERS-TLS1.2:" GNUTLS_SRP;
459 return CURLE_OK;
460 case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_1:
461 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
462 "+VERS-TLS1.1:" GNUTLS_SRP;
463 return CURLE_OK;
464 case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_2:
465 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
466 "+VERS-TLS1.1:+VERS-TLS1.2:" GNUTLS_SRP;
467 return CURLE_OK;
468 case CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_TLSv1_2:
469 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
470 "+VERS-TLS1.2:" GNUTLS_SRP;
471 return CURLE_OK;
472 case CURL_SSLVERSION_TLSv1_3 | CURL_SSLVERSION_MAX_TLSv1_3:
473 #ifdef HAS_TLS13
474 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
475 "+VERS-TLS1.3:" GNUTLS_SRP;
476 return CURLE_OK;
477 #else
478 failf(data, "GnuTLS: TLS 1.3 is not yet supported");
479 return CURLE_SSL_CONNECT_ERROR;
480 #endif
481 case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_DEFAULT:
482 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
483 "+VERS-TLS1.0:+VERS-TLS1.1:+VERS-TLS1.2:"
484 #ifdef HAS_TLS13
485 "+VERS-TLS1.3:"
486 #endif
487 GNUTLS_SRP;
488 return CURLE_OK;
489 case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_DEFAULT:
490 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
491 "+VERS-TLS1.1:+VERS-TLS1.2:"
492 #ifdef HAS_TLS13
493 "+VERS-TLS1.3:"
494 #endif
495 GNUTLS_SRP;
496 return CURLE_OK;
497 case CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_DEFAULT:
498 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
499 "+VERS-TLS1.2:"
500 #ifdef HAS_TLS13
501 "+VERS-TLS1.3:"
502 #endif
503 GNUTLS_SRP;
504 return CURLE_OK;
505 case CURL_SSLVERSION_TLSv1_3 | CURL_SSLVERSION_MAX_DEFAULT:
506 *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
507 "+VERS-TLS1.2:"
508 #ifdef HAS_TLS13
509 "+VERS-TLS1.3:"
510 #endif
511 GNUTLS_SRP;
512 return CURLE_OK;
513 }
514
515 failf(data, "GnuTLS: cannot set ssl protocol");
516 return CURLE_SSL_CONNECT_ERROR;
517 }
518 #endif
519
520 static CURLcode
521 gtls_connect_step1(struct connectdata *conn,
522 int sockindex)
523 {
524 struct Curl_easy *data = conn->data;
525 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
526 unsigned int init_flags;
527 gnutls_session_t session;
528 int rc;
529 bool sni = TRUE; /* default is SNI enabled */
530 void *transport_ptr = NULL;
531 gnutls_push_func gnutls_transport_push = NULL;
532 gnutls_pull_func gnutls_transport_pull = NULL;
533 #ifdef ENABLE_IPV6
534 struct in6_addr addr;
535 #else
536 struct in_addr addr;
537 #endif
538 #ifndef USE_GNUTLS_PRIORITY_SET_DIRECT
539 static const int cipher_priority[] = {
540 /* These two ciphers were added to GnuTLS as late as ver. 3.0.1,
541 but this code path is only ever used for ver. < 2.12.0.
542 GNUTLS_CIPHER_AES_128_GCM,
543 GNUTLS_CIPHER_AES_256_GCM,
544 */
545 GNUTLS_CIPHER_AES_128_CBC,
546 GNUTLS_CIPHER_AES_256_CBC,
547 GNUTLS_CIPHER_CAMELLIA_128_CBC,
548 GNUTLS_CIPHER_CAMELLIA_256_CBC,
549 GNUTLS_CIPHER_3DES_CBC,
550 };
551 static const int cert_type_priority[] = { GNUTLS_CRT_X509, 0 };
552 int protocol_priority[] = { 0, 0, 0, 0 };
553 #else
554 const char *prioritylist;
555 const char *err = NULL;
556 #endif
557
558 const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
559 conn->host.name;
560
561 if(connssl->state == ssl_connection_complete)
562 /* to make us tolerant against being called more than once for the
563 same connection */
564 return CURLE_OK;
565
566 if(!gtls_inited)
567 Curl_gtls_init();
568
569 if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv2) {
570 failf(data, "GnuTLS does not support SSLv2");
571 return CURLE_SSL_CONNECT_ERROR;
572 }
573 else if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv3)
574 sni = FALSE; /* SSLv3 has no SNI */
575
576 /* allocate a cred struct */
577 rc = gnutls_certificate_allocate_credentials(&BACKEND->cred);
578 if(rc != GNUTLS_E_SUCCESS) {
579 failf(data, "gnutls_cert_all_cred() failed: %s", gnutls_strerror(rc));
580 return CURLE_SSL_CONNECT_ERROR;
581 }
582
583 #ifdef USE_TLS_SRP
584 if(SSL_SET_OPTION(authtype) == CURL_TLSAUTH_SRP) {
585 infof(data, "Using TLS-SRP username: %s\n", SSL_SET_OPTION(username));
586
587 rc = gnutls_srp_allocate_client_credentials(
588 &BACKEND->srp_client_cred);
589 if(rc != GNUTLS_E_SUCCESS) {
590 failf(data, "gnutls_srp_allocate_client_cred() failed: %s",
591 gnutls_strerror(rc));
592 return CURLE_OUT_OF_MEMORY;
593 }
594
595 rc = gnutls_srp_set_client_credentials(BACKEND->srp_client_cred,
596 SSL_SET_OPTION(username),
597 SSL_SET_OPTION(password));
598 if(rc != GNUTLS_E_SUCCESS) {
599 failf(data, "gnutls_srp_set_client_cred() failed: %s",
600 gnutls_strerror(rc));
601 return CURLE_BAD_FUNCTION_ARGUMENT;
602 }
603 }
604 #endif
605
606 if(SSL_CONN_CONFIG(CAfile)) {
607 /* set the trusted CA cert bundle file */
608 gnutls_certificate_set_verify_flags(BACKEND->cred,
609 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
610
611 rc = gnutls_certificate_set_x509_trust_file(BACKEND->cred,
612 SSL_CONN_CONFIG(CAfile),
613 GNUTLS_X509_FMT_PEM);
614 if(rc < 0) {
615 infof(data, "error reading ca cert file %s (%s)\n",
616 SSL_CONN_CONFIG(CAfile), gnutls_strerror(rc));
617 if(SSL_CONN_CONFIG(verifypeer))
618 return CURLE_SSL_CACERT_BADFILE;
619 }
620 else
621 infof(data, "found %d certificates in %s\n", rc,
622 SSL_CONN_CONFIG(CAfile));
623 }
624
625 #ifdef HAS_CAPATH
626 if(SSL_CONN_CONFIG(CApath)) {
627 /* set the trusted CA cert directory */
628 rc = gnutls_certificate_set_x509_trust_dir(BACKEND->cred,
629 SSL_CONN_CONFIG(CApath),
630 GNUTLS_X509_FMT_PEM);
631 if(rc < 0) {
632 infof(data, "error reading ca cert file %s (%s)\n",
633 SSL_CONN_CONFIG(CApath), gnutls_strerror(rc));
634 if(SSL_CONN_CONFIG(verifypeer))
635 return CURLE_SSL_CACERT_BADFILE;
636 }
637 else
638 infof(data, "found %d certificates in %s\n",
639 rc, SSL_CONN_CONFIG(CApath));
640 }
641 #endif
642
643 #ifdef CURL_CA_FALLBACK
644 /* use system ca certificate store as fallback */
645 if(SSL_CONN_CONFIG(verifypeer) &&
646 !(SSL_CONN_CONFIG(CAfile) || SSL_CONN_CONFIG(CApath))) {
647 gnutls_certificate_set_x509_system_trust(BACKEND->cred);
648 }
649 #endif
650
651 if(SSL_SET_OPTION(CRLfile)) {
652 /* set the CRL list file */
653 rc = gnutls_certificate_set_x509_crl_file(BACKEND->cred,
654 SSL_SET_OPTION(CRLfile),
655 GNUTLS_X509_FMT_PEM);
656 if(rc < 0) {
657 failf(data, "error reading crl file %s (%s)",
658 SSL_SET_OPTION(CRLfile), gnutls_strerror(rc));
659 return CURLE_SSL_CRL_BADFILE;
660 }
661 else
662 infof(data, "found %d CRL in %s\n",
663 rc, SSL_SET_OPTION(CRLfile));
664 }
665
666 /* Initialize TLS session as a client */
667 init_flags = GNUTLS_CLIENT;
668
669 #if defined(GNUTLS_FORCE_CLIENT_CERT)
670 init_flags |= GNUTLS_FORCE_CLIENT_CERT;
671 #endif
672
673 #if defined(GNUTLS_NO_TICKETS)
674 /* Disable TLS session tickets */
675 init_flags |= GNUTLS_NO_TICKETS;
676 #endif
677
678 rc = gnutls_init(&BACKEND->session, init_flags);
679 if(rc != GNUTLS_E_SUCCESS) {
680 failf(data, "gnutls_init() failed: %d", rc);
681 return CURLE_SSL_CONNECT_ERROR;
682 }
683
684 /* convenient assign */
685 session = BACKEND->session;
686
687 if((0 == Curl_inet_pton(AF_INET, hostname, &addr)) &&
688 #ifdef ENABLE_IPV6
689 (0 == Curl_inet_pton(AF_INET6, hostname, &addr)) &&
690 #endif
691 sni &&
692 (gnutls_server_name_set(session, GNUTLS_NAME_DNS, hostname,
693 strlen(hostname)) < 0))
694 infof(data, "WARNING: failed to configure server name indication (SNI) "
695 "TLS extension\n");
696
697 /* Use default priorities */
698 rc = gnutls_set_default_priority(session);
699 if(rc != GNUTLS_E_SUCCESS)
700 return CURLE_SSL_CONNECT_ERROR;
701
702 #ifndef USE_GNUTLS_PRIORITY_SET_DIRECT
703 rc = gnutls_cipher_set_priority(session, cipher_priority);
704 if(rc != GNUTLS_E_SUCCESS)
705 return CURLE_SSL_CONNECT_ERROR;
706
707 /* Sets the priority on the certificate types supported by gnutls. Priority
708 is higher for types specified before others. After specifying the types
709 you want, you must append a 0. */
710 rc = gnutls_certificate_type_set_priority(session, cert_type_priority);
711 if(rc != GNUTLS_E_SUCCESS)
712 return CURLE_SSL_CONNECT_ERROR;
713
714 if(SSL_CONN_CONFIG(cipher_list) != NULL) {
715 failf(data, "can't pass a custom cipher list to older GnuTLS"
716 " versions");
717 return CURLE_SSL_CONNECT_ERROR;
718 }
719
720 switch(SSL_CONN_CONFIG(version)) {
721 case CURL_SSLVERSION_SSLv3:
722 protocol_priority[0] = GNUTLS_SSL3;
723 break;
724 case CURL_SSLVERSION_DEFAULT:
725 case CURL_SSLVERSION_TLSv1:
726 protocol_priority[0] = GNUTLS_TLS1_0;
727 protocol_priority[1] = GNUTLS_TLS1_1;
728 protocol_priority[2] = GNUTLS_TLS1_2;
729 #ifdef HAS_TLS13
730 protocol_priority[3] = GNUTLS_TLS1_3;
731 #endif
732 break;
733 case CURL_SSLVERSION_TLSv1_0:
734 case CURL_SSLVERSION_TLSv1_1:
735 case CURL_SSLVERSION_TLSv1_2:
736 case CURL_SSLVERSION_TLSv1_3:
737 {
738 CURLcode result = set_ssl_version_min_max(protocol_priority,
739 sizeof(protocol_priority)/sizeof(protocol_priority[0]), conn);
740 if(result != CURLE_OK)
741 return result;
742 break;
743 }
744 case CURL_SSLVERSION_SSLv2:
745 failf(data, "GnuTLS does not support SSLv2");
746 return CURLE_SSL_CONNECT_ERROR;
747 default:
748 failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
749 return CURLE_SSL_CONNECT_ERROR;
750 }
751 rc = gnutls_protocol_set_priority(session, protocol_priority);
752 if(rc != GNUTLS_E_SUCCESS) {
753 failf(data, "Did you pass a valid GnuTLS cipher list?");
754 return CURLE_SSL_CONNECT_ERROR;
755 }
756
757 #else
758 /* Ensure +SRP comes at the *end* of all relevant strings so that it can be
759 * removed if a run-time error indicates that SRP is not supported by this
760 * GnuTLS version */
761 switch(SSL_CONN_CONFIG(version)) {
762 case CURL_SSLVERSION_SSLv3:
763 prioritylist = GNUTLS_CIPHERS ":-VERS-TLS-ALL:+VERS-SSL3.0";
764 break;
765 case CURL_SSLVERSION_DEFAULT:
766 case CURL_SSLVERSION_TLSv1:
767 prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:"
768 #ifdef HAS_TLS13
769 "+VERS-TLS1.3:"
770 #endif
771 GNUTLS_SRP;
772 break;
773 case CURL_SSLVERSION_TLSv1_0:
774 case CURL_SSLVERSION_TLSv1_1:
775 case CURL_SSLVERSION_TLSv1_2:
776 case CURL_SSLVERSION_TLSv1_3:
777 {
778 CURLcode result = set_ssl_version_min_max(&prioritylist, conn);
779 if(result != CURLE_OK)
780 return result;
781 break;
782 }
783 case CURL_SSLVERSION_SSLv2:
784 failf(data, "GnuTLS does not support SSLv2");
785 return CURLE_SSL_CONNECT_ERROR;
786 default:
787 failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
788 return CURLE_SSL_CONNECT_ERROR;
789 }
790 rc = gnutls_priority_set_direct(session, prioritylist, &err);
791 if((rc == GNUTLS_E_INVALID_REQUEST) && err) {
792 if(!strcmp(err, GNUTLS_SRP)) {
793 /* This GnuTLS was probably compiled without support for SRP.
794 * Note that fact and try again without it. */
795 int validprioritylen = curlx_uztosi(err - prioritylist);
796 char *prioritycopy = strdup(prioritylist);
797 if(!prioritycopy)
798 return CURLE_OUT_OF_MEMORY;
799
800 infof(data, "This GnuTLS does not support SRP\n");
801 if(validprioritylen)
802 /* Remove the :+SRP */
803 prioritycopy[validprioritylen - 1] = 0;
804 rc = gnutls_priority_set_direct(session, prioritycopy, &err);
805 free(prioritycopy);
806 }
807 }
808 if(rc != GNUTLS_E_SUCCESS) {
809 failf(data, "Error %d setting GnuTLS cipher list starting with %s",
810 rc, err);
811 return CURLE_SSL_CONNECT_ERROR;
812 }
813 #endif
814
815 #ifdef HAS_ALPN
816 if(conn->bits.tls_enable_alpn) {
817 int cur = 0;
818 gnutls_datum_t protocols[2];
819
820 #ifdef USE_NGHTTP2
821 if(data->set.httpversion >= CURL_HTTP_VERSION_2 &&
822 (!SSL_IS_PROXY() || !conn->bits.tunnel_proxy)) {
823 protocols[cur].data = (unsigned char *)NGHTTP2_PROTO_VERSION_ID;
824 protocols[cur].size = NGHTTP2_PROTO_VERSION_ID_LEN;
825 cur++;
826 infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID);
827 }
828 #endif
829
830 protocols[cur].data = (unsigned char *)ALPN_HTTP_1_1;
831 protocols[cur].size = ALPN_HTTP_1_1_LENGTH;
832 cur++;
833 infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1);
834
835 gnutls_alpn_set_protocols(session, protocols, cur, 0);
836 }
837 #endif
838
839 if(SSL_SET_OPTION(cert)) {
840 if(SSL_SET_OPTION(key_passwd)) {
841 #if HAVE_GNUTLS_CERTIFICATE_SET_X509_KEY_FILE2
842 const unsigned int supported_key_encryption_algorithms =
843 GNUTLS_PKCS_USE_PKCS12_3DES | GNUTLS_PKCS_USE_PKCS12_ARCFOUR |
844 GNUTLS_PKCS_USE_PKCS12_RC2_40 | GNUTLS_PKCS_USE_PBES2_3DES |
845 GNUTLS_PKCS_USE_PBES2_AES_128 | GNUTLS_PKCS_USE_PBES2_AES_192 |
846 GNUTLS_PKCS_USE_PBES2_AES_256;
847 rc = gnutls_certificate_set_x509_key_file2(
848 BACKEND->cred,
849 SSL_SET_OPTION(cert),
850 SSL_SET_OPTION(key) ?
851 SSL_SET_OPTION(key) : SSL_SET_OPTION(cert),
852 do_file_type(SSL_SET_OPTION(cert_type)),
853 SSL_SET_OPTION(key_passwd),
854 supported_key_encryption_algorithms);
855 if(rc != GNUTLS_E_SUCCESS) {
856 failf(data,
857 "error reading X.509 potentially-encrypted key file: %s",
858 gnutls_strerror(rc));
859 return CURLE_SSL_CONNECT_ERROR;
860 }
861 #else
862 failf(data, "gnutls lacks support for encrypted key files");
863 return CURLE_SSL_CONNECT_ERROR;
864 #endif
865 }
866 else {
867 if(gnutls_certificate_set_x509_key_file(
868 BACKEND->cred,
869 SSL_SET_OPTION(cert),
870 SSL_SET_OPTION(key) ?
871 SSL_SET_OPTION(key) : SSL_SET_OPTION(cert),
872 do_file_type(SSL_SET_OPTION(cert_type)) ) !=
873 GNUTLS_E_SUCCESS) {
874 failf(data, "error reading X.509 key or certificate file");
875 return CURLE_SSL_CONNECT_ERROR;
876 }
877 }
878 }
879
880 #ifdef USE_TLS_SRP
881 /* put the credentials to the current session */
882 if(SSL_SET_OPTION(authtype) == CURL_TLSAUTH_SRP) {
883 rc = gnutls_credentials_set(session, GNUTLS_CRD_SRP,
884 BACKEND->srp_client_cred);
885 if(rc != GNUTLS_E_SUCCESS) {
886 failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
887 return CURLE_SSL_CONNECT_ERROR;
888 }
889 }
890 else
891 #endif
892 {
893 rc = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
894 BACKEND->cred);
895 if(rc != GNUTLS_E_SUCCESS) {
896 failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
897 return CURLE_SSL_CONNECT_ERROR;
898 }
899 }
900
901 if(conn->proxy_ssl[sockindex].use) {
902 transport_ptr = conn->proxy_ssl[sockindex].backend->session;
903 gnutls_transport_push = Curl_gtls_push_ssl;
904 gnutls_transport_pull = Curl_gtls_pull_ssl;
905 }
906 else {
907 /* file descriptor for the socket */
908 transport_ptr = &conn->sock[sockindex];
909 gnutls_transport_push = Curl_gtls_push;
910 gnutls_transport_pull = Curl_gtls_pull;
911 }
912
913 /* set the connection handle */
914 gnutls_transport_set_ptr(session, transport_ptr);
915
916 /* register callback functions to send and receive data. */
917 gnutls_transport_set_push_function(session, gnutls_transport_push);
918 gnutls_transport_set_pull_function(session, gnutls_transport_pull);
919
920 /* lowat must be set to zero when using custom push and pull functions. */
921 gnutls_transport_set_lowat(session, 0);
922
923 #ifdef HAS_OCSP
924 if(SSL_CONN_CONFIG(verifystatus)) {
925 rc = gnutls_ocsp_status_request_enable_client(session, NULL, 0, NULL);
926 if(rc != GNUTLS_E_SUCCESS) {
927 failf(data, "gnutls_ocsp_status_request_enable_client() failed: %d", rc);
928 return CURLE_SSL_CONNECT_ERROR;
929 }
930 }
931 #endif
932
933 /* This might be a reconnect, so we check for a session ID in the cache
934 to speed up things */
935 if(SSL_SET_OPTION(primary.sessionid)) {
936 void *ssl_sessionid;
937 size_t ssl_idsize;
938
939 Curl_ssl_sessionid_lock(conn);
940 if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, &ssl_idsize, sockindex)) {
941 /* we got a session id, use it! */
942 gnutls_session_set_data(session, ssl_sessionid, ssl_idsize);
943
944 /* Informational message */
945 infof(data, "SSL re-using session ID\n");
946 }
947 Curl_ssl_sessionid_unlock(conn);
948 }
949
950 return CURLE_OK;
951 }
952
953 static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data,
954 gnutls_x509_crt_t cert,
955 const char *pinnedpubkey)
956 {
957 /* Scratch */
958 size_t len1 = 0, len2 = 0;
959 unsigned char *buff1 = NULL;
960
961 gnutls_pubkey_t key = NULL;
962
963 /* Result is returned to caller */
964 CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
965
966 /* if a path wasn't specified, don't pin */
967 if(NULL == pinnedpubkey)
968 return CURLE_OK;
969
970 if(NULL == cert)
971 return result;
972
973 do {
974 int ret;
975
976 /* Begin Gyrations to get the public key */
977 gnutls_pubkey_init(&key);
978
979 ret = gnutls_pubkey_import_x509(key, cert, 0);
980 if(ret < 0)
981 break; /* failed */
982
983 ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, NULL, &len1);
984 if(ret != GNUTLS_E_SHORT_MEMORY_BUFFER || len1 == 0)
985 break; /* failed */
986
987 buff1 = malloc(len1);
988 if(NULL == buff1)
989 break; /* failed */
990
991 len2 = len1;
992
993 ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, buff1, &len2);
994 if(ret < 0 || len1 != len2)
995 break; /* failed */
996
997 /* End Gyrations */
998
999 /* The one good exit point */
1000 result = Curl_pin_peer_pubkey(data, pinnedpubkey, buff1, len1);
1001 } while(0);
1002
1003 if(NULL != key)
1004 gnutls_pubkey_deinit(key);
1005
1006 Curl_safefree(buff1);
1007
1008 return result;
1009 }
1010
1011 static Curl_recv gtls_recv;
1012 static Curl_send gtls_send;
1013
1014 static CURLcode
1015 gtls_connect_step3(struct connectdata *conn,
1016 int sockindex)
1017 {
1018 unsigned int cert_list_size;
1019 const gnutls_datum_t *chainp;
1020 unsigned int verify_status = 0;
1021 gnutls_x509_crt_t x509_cert, x509_issuer;
1022 gnutls_datum_t issuerp;
1023 char certbuf[256] = ""; /* big enough? */
1024 size_t size;
1025 time_t certclock;
1026 const char *ptr;
1027 struct Curl_easy *data = conn->data;
1028 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
1029 gnutls_session_t session = BACKEND->session;
1030 int rc;
1031 #ifdef HAS_ALPN
1032 gnutls_datum_t proto;
1033 #endif
1034 CURLcode result = CURLE_OK;
1035 #ifndef CURL_DISABLE_VERBOSE_STRINGS
1036 unsigned int algo;
1037 unsigned int bits;
1038 gnutls_protocol_t version = gnutls_protocol_get_version(session);
1039 #endif
1040 const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
1041 conn->host.name;
1042
1043 /* the name of the cipher suite used, e.g. ECDHE_RSA_AES_256_GCM_SHA384. */
1044 ptr = gnutls_cipher_suite_get_name(gnutls_kx_get(session),
1045 gnutls_cipher_get(session),
1046 gnutls_mac_get(session));
1047
1048 infof(data, "SSL connection using %s / %s\n",
1049 gnutls_protocol_get_name(version), ptr);
1050
1051 /* This function will return the peer's raw certificate (chain) as sent by
1052 the peer. These certificates are in raw format (DER encoded for
1053 X.509). In case of a X.509 then a certificate list may be present. The
1054 first certificate in the list is the peer's certificate, following the
1055 issuer's certificate, then the issuer's issuer etc. */
1056
1057 chainp = gnutls_certificate_get_peers(session, &cert_list_size);
1058 if(!chainp) {
1059 if(SSL_CONN_CONFIG(verifypeer) ||
1060 SSL_CONN_CONFIG(verifyhost) ||
1061 SSL_SET_OPTION(issuercert)) {
1062 #ifdef USE_TLS_SRP
1063 if(SSL_SET_OPTION(authtype) == CURL_TLSAUTH_SRP
1064 && SSL_SET_OPTION(username) != NULL
1065 && !SSL_CONN_CONFIG(verifypeer)
1066 && gnutls_cipher_get(session)) {
1067 /* no peer cert, but auth is ok if we have SRP user and cipher and no
1068 peer verify */
1069 }
1070 else {
1071 #endif
1072 failf(data, "failed to get server cert");
1073 return CURLE_PEER_FAILED_VERIFICATION;
1074 #ifdef USE_TLS_SRP
1075 }
1076 #endif
1077 }
1078 infof(data, "\t common name: WARNING couldn't obtain\n");
1079 }
1080
1081 if(data->set.ssl.certinfo && chainp) {
1082 unsigned int i;
1083
1084 result = Curl_ssl_init_certinfo(data, cert_list_size);
1085 if(result)
1086 return result;
1087
1088 for(i = 0; i < cert_list_size; i++) {
1089 const char *beg = (const char *) chainp[i].data;
1090 const char *end = beg + chainp[i].size;
1091
1092 result = Curl_extract_certinfo(conn, i, beg, end);
1093 if(result)
1094 return result;
1095 }
1096 }
1097
1098 if(SSL_CONN_CONFIG(verifypeer)) {
1099 /* This function will try to verify the peer's certificate and return its
1100 status (trusted, invalid etc.). The value of status should be one or
1101 more of the gnutls_certificate_status_t enumerated elements bitwise
1102 or'd. To avoid denial of service attacks some default upper limits
1103 regarding the certificate key size and chain size are set. To override
1104 them use gnutls_certificate_set_verify_limits(). */
1105
1106 rc = gnutls_certificate_verify_peers2(session, &verify_status);
1107 if(rc < 0) {
1108 failf(data, "server cert verify failed: %d", rc);
1109 return CURLE_SSL_CONNECT_ERROR;
1110 }
1111
1112 /* verify_status is a bitmask of gnutls_certificate_status bits */
1113 if(verify_status & GNUTLS_CERT_INVALID) {
1114 if(SSL_CONN_CONFIG(verifypeer)) {
1115 failf(data, "server certificate verification failed. CAfile: %s "
1116 "CRLfile: %s", SSL_CONN_CONFIG(CAfile) ? SSL_CONN_CONFIG(CAfile):
1117 "none",
1118 SSL_SET_OPTION(CRLfile)?SSL_SET_OPTION(CRLfile):"none");
1119 return CURLE_PEER_FAILED_VERIFICATION;
1120 }
1121 else
1122 infof(data, "\t server certificate verification FAILED\n");
1123 }
1124 else
1125 infof(data, "\t server certificate verification OK\n");
1126 }
1127 else
1128 infof(data, "\t server certificate verification SKIPPED\n");
1129
1130 #ifdef HAS_OCSP
1131 if(SSL_CONN_CONFIG(verifystatus)) {
1132 if(gnutls_ocsp_status_request_is_checked(session, 0) == 0) {
1133 gnutls_datum_t status_request;
1134 gnutls_ocsp_resp_t ocsp_resp;
1135
1136 gnutls_ocsp_cert_status_t status;
1137 gnutls_x509_crl_reason_t reason;
1138
1139 rc = gnutls_ocsp_status_request_get(session, &status_request);
1140
1141 infof(data, "\t server certificate status verification FAILED\n");
1142
1143 if(rc == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
1144 failf(data, "No OCSP response received");
1145 return CURLE_SSL_INVALIDCERTSTATUS;
1146 }
1147
1148 if(rc < 0) {
1149 failf(data, "Invalid OCSP response received");
1150 return CURLE_SSL_INVALIDCERTSTATUS;
1151 }
1152
1153 gnutls_ocsp_resp_init(&ocsp_resp);
1154
1155 rc = gnutls_ocsp_resp_import(ocsp_resp, &status_request);
1156 if(rc < 0) {
1157 failf(data, "Invalid OCSP response received");
1158 return CURLE_SSL_INVALIDCERTSTATUS;
1159 }
1160
1161 (void)gnutls_ocsp_resp_get_single(ocsp_resp, 0, NULL, NULL, NULL, NULL,
1162 &status, NULL, NULL, NULL, &reason);
1163
1164 switch(status) {
1165 case GNUTLS_OCSP_CERT_GOOD:
1166 break;
1167
1168 case GNUTLS_OCSP_CERT_REVOKED: {
1169 const char *crl_reason;
1170
1171 switch(reason) {
1172 default:
1173 case GNUTLS_X509_CRLREASON_UNSPECIFIED:
1174 crl_reason = "unspecified reason";
1175 break;
1176
1177 case GNUTLS_X509_CRLREASON_KEYCOMPROMISE:
1178 crl_reason = "private key compromised";
1179 break;
1180
1181 case GNUTLS_X509_CRLREASON_CACOMPROMISE:
1182 crl_reason = "CA compromised";
1183 break;
1184
1185 case GNUTLS_X509_CRLREASON_AFFILIATIONCHANGED:
1186 crl_reason = "affiliation has changed";
1187 break;
1188
1189 case GNUTLS_X509_CRLREASON_SUPERSEDED:
1190 crl_reason = "certificate superseded";
1191 break;
1192
1193 case GNUTLS_X509_CRLREASON_CESSATIONOFOPERATION:
1194 crl_reason = "operation has ceased";
1195 break;
1196
1197 case GNUTLS_X509_CRLREASON_CERTIFICATEHOLD:
1198 crl_reason = "certificate is on hold";
1199 break;
1200
1201 case GNUTLS_X509_CRLREASON_REMOVEFROMCRL:
1202 crl_reason = "will be removed from delta CRL";
1203 break;
1204
1205 case GNUTLS_X509_CRLREASON_PRIVILEGEWITHDRAWN:
1206 crl_reason = "privilege withdrawn";
1207 break;
1208
1209 case GNUTLS_X509_CRLREASON_AACOMPROMISE:
1210 crl_reason = "AA compromised";
1211 break;
1212 }
1213
1214 failf(data, "Server certificate was revoked: %s", crl_reason);
1215 break;
1216 }
1217
1218 default:
1219 case GNUTLS_OCSP_CERT_UNKNOWN:
1220 failf(data, "Server certificate status is unknown");
1221 break;
1222 }
1223
1224 gnutls_ocsp_resp_deinit(ocsp_resp);
1225
1226 return CURLE_SSL_INVALIDCERTSTATUS;
1227 }
1228 else
1229 infof(data, "\t server certificate status verification OK\n");
1230 }
1231 else
1232 infof(data, "\t server certificate status verification SKIPPED\n");
1233 #endif
1234
1235 /* initialize an X.509 certificate structure. */
1236 gnutls_x509_crt_init(&x509_cert);
1237
1238 if(chainp)
1239 /* convert the given DER or PEM encoded Certificate to the native
1240 gnutls_x509_crt_t format */
1241 gnutls_x509_crt_import(x509_cert, chainp, GNUTLS_X509_FMT_DER);
1242
1243 if(SSL_SET_OPTION(issuercert)) {
1244 gnutls_x509_crt_init(&x509_issuer);
1245 issuerp = load_file(SSL_SET_OPTION(issuercert));
1246 gnutls_x509_crt_import(x509_issuer, &issuerp, GNUTLS_X509_FMT_PEM);
1247 rc = gnutls_x509_crt_check_issuer(x509_cert, x509_issuer);
1248 gnutls_x509_crt_deinit(x509_issuer);
1249 unload_file(issuerp);
1250 if(rc <= 0) {
1251 failf(data, "server certificate issuer check failed (IssuerCert: %s)",
1252 SSL_SET_OPTION(issuercert)?SSL_SET_OPTION(issuercert):"none");
1253 gnutls_x509_crt_deinit(x509_cert);
1254 return CURLE_SSL_ISSUER_ERROR;
1255 }
1256 infof(data, "\t server certificate issuer check OK (Issuer Cert: %s)\n",
1257 SSL_SET_OPTION(issuercert)?SSL_SET_OPTION(issuercert):"none");
1258 }
1259
1260 size = sizeof(certbuf);
1261 rc = gnutls_x509_crt_get_dn_by_oid(x509_cert, GNUTLS_OID_X520_COMMON_NAME,
1262 0, /* the first and only one */
1263 FALSE,
1264 certbuf,
1265 &size);
1266 if(rc) {
1267 infof(data, "error fetching CN from cert:%s\n",
1268 gnutls_strerror(rc));
1269 }
1270
1271 /* This function will check if the given certificate's subject matches the
1272 given hostname. This is a basic implementation of the matching described
1273 in RFC2818 (HTTPS), which takes into account wildcards, and the subject
1274 alternative name PKIX extension. Returns non zero on success, and zero on
1275 failure. */
1276 rc = gnutls_x509_crt_check_hostname(x509_cert, hostname);
1277 #if GNUTLS_VERSION_NUMBER < 0x030306
1278 /* Before 3.3.6, gnutls_x509_crt_check_hostname() didn't check IP
1279 addresses. */
1280 if(!rc) {
1281 #ifdef ENABLE_IPV6
1282 #define use_addr in6_addr
1283 #else
1284 #define use_addr in_addr
1285 #endif
1286 unsigned char addrbuf[sizeof(struct use_addr)];
1287 size_t addrlen = 0;
1288
1289 if(Curl_inet_pton(AF_INET, hostname, addrbuf) > 0)
1290 addrlen = 4;
1291 #ifdef ENABLE_IPV6
1292 else if(Curl_inet_pton(AF_INET6, hostname, addrbuf) > 0)
1293 addrlen = 16;
1294 #endif
1295
1296 if(addrlen) {
1297 unsigned char certaddr[sizeof(struct use_addr)];
1298 int i;
1299
1300 for(i = 0; ; i++) {
1301 size_t certaddrlen = sizeof(certaddr);
1302 int ret = gnutls_x509_crt_get_subject_alt_name(x509_cert, i, certaddr,
1303 &certaddrlen, NULL);
1304 /* If this happens, it wasn't an IP address. */
1305 if(ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
1306 continue;
1307 if(ret < 0)
1308 break;
1309 if(ret != GNUTLS_SAN_IPADDRESS)
1310 continue;
1311 if(certaddrlen == addrlen && !memcmp(addrbuf, certaddr, addrlen)) {
1312 rc = 1;
1313 break;
1314 }
1315 }
1316 }
1317 }
1318 #endif
1319 if(!rc) {
1320 const char * const dispname = SSL_IS_PROXY() ?
1321 conn->http_proxy.host.dispname : conn->host.dispname;
1322
1323 if(SSL_CONN_CONFIG(verifyhost)) {
1324 failf(data, "SSL: certificate subject name (%s) does not match "
1325 "target host name '%s'", certbuf, dispname);
1326 gnutls_x509_crt_deinit(x509_cert);
1327 return CURLE_PEER_FAILED_VERIFICATION;
1328 }
1329 else
1330 infof(data, "\t common name: %s (does not match '%s')\n",
1331 certbuf, dispname);
1332 }
1333 else
1334 infof(data, "\t common name: %s (matched)\n", certbuf);
1335
1336 /* Check for time-based validity */
1337 certclock = gnutls_x509_crt_get_expiration_time(x509_cert);
1338
1339 if(certclock == (time_t)-1) {
1340 if(SSL_CONN_CONFIG(verifypeer)) {
1341 failf(data, "server cert expiration date verify failed");
1342 gnutls_x509_crt_deinit(x509_cert);
1343 return CURLE_SSL_CONNECT_ERROR;
1344 }
1345 else
1346 infof(data, "\t server certificate expiration date verify FAILED\n");
1347 }
1348 else {
1349 if(certclock < time(NULL)) {
1350 if(SSL_CONN_CONFIG(verifypeer)) {
1351 failf(data, "server certificate expiration date has passed.");
1352 gnutls_x509_crt_deinit(x509_cert);
1353 return CURLE_PEER_FAILED_VERIFICATION;
1354 }
1355 else
1356 infof(data, "\t server certificate expiration date FAILED\n");
1357 }
1358 else
1359 infof(data, "\t server certificate expiration date OK\n");
1360 }
1361
1362 certclock = gnutls_x509_crt_get_activation_time(x509_cert);
1363
1364 if(certclock == (time_t)-1) {
1365 if(SSL_CONN_CONFIG(verifypeer)) {
1366 failf(data, "server cert activation date verify failed");
1367 gnutls_x509_crt_deinit(x509_cert);
1368 return CURLE_SSL_CONNECT_ERROR;
1369 }
1370 else
1371 infof(data, "\t server certificate activation date verify FAILED\n");
1372 }
1373 else {
1374 if(certclock > time(NULL)) {
1375 if(SSL_CONN_CONFIG(verifypeer)) {
1376 failf(data, "server certificate not activated yet.");
1377 gnutls_x509_crt_deinit(x509_cert);
1378 return CURLE_PEER_FAILED_VERIFICATION;
1379 }
1380 else
1381 infof(data, "\t server certificate activation date FAILED\n");
1382 }
1383 else
1384 infof(data, "\t server certificate activation date OK\n");
1385 }
1386
1387 ptr = SSL_IS_PROXY() ? data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
1388 data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
1389 if(ptr) {
1390 result = pkp_pin_peer_pubkey(data, x509_cert, ptr);
1391 if(result != CURLE_OK) {
1392 failf(data, "SSL: public key does not match pinned public key!");
1393 gnutls_x509_crt_deinit(x509_cert);
1394 return result;
1395 }
1396 }
1397
1398 /* Show:
1399
1400 - subject
1401 - start date
1402 - expire date
1403 - common name
1404 - issuer
1405
1406 */
1407
1408 #ifndef CURL_DISABLE_VERBOSE_STRINGS
1409 /* public key algorithm's parameters */
1410 algo = gnutls_x509_crt_get_pk_algorithm(x509_cert, &bits);
1411 infof(data, "\t certificate public key: %s\n",
1412 gnutls_pk_algorithm_get_name(algo));
1413
1414 /* version of the X.509 certificate. */
1415 infof(data, "\t certificate version: #%d\n",
1416 gnutls_x509_crt_get_version(x509_cert));
1417
1418
1419 size = sizeof(certbuf);
1420 gnutls_x509_crt_get_dn(x509_cert, certbuf, &size);
1421 infof(data, "\t subject: %s\n", certbuf);
1422
1423 certclock = gnutls_x509_crt_get_activation_time(x509_cert);
1424 showtime(data, "start date", certclock);
1425
1426 certclock = gnutls_x509_crt_get_expiration_time(x509_cert);
1427 showtime(data, "expire date", certclock);
1428
1429 size = sizeof(certbuf);
1430 gnutls_x509_crt_get_issuer_dn(x509_cert, certbuf, &size);
1431 infof(data, "\t issuer: %s\n", certbuf);
1432 #endif
1433
1434 gnutls_x509_crt_deinit(x509_cert);
1435
1436 #ifdef HAS_ALPN
1437 if(conn->bits.tls_enable_alpn) {
1438 rc = gnutls_alpn_get_selected_protocol(session, &proto);
1439 if(rc == 0) {
1440 infof(data, "ALPN, server accepted to use %.*s\n", proto.size,
1441 proto.data);
1442
1443 #ifdef USE_NGHTTP2
1444 if(proto.size == NGHTTP2_PROTO_VERSION_ID_LEN &&
1445 !memcmp(NGHTTP2_PROTO_VERSION_ID, proto.data,
1446 NGHTTP2_PROTO_VERSION_ID_LEN)) {
1447 conn->negnpn = CURL_HTTP_VERSION_2;
1448 }
1449 else
1450 #endif
1451 if(proto.size == ALPN_HTTP_1_1_LENGTH &&
1452 !memcmp(ALPN_HTTP_1_1, proto.data, ALPN_HTTP_1_1_LENGTH)) {
1453 conn->negnpn = CURL_HTTP_VERSION_1_1;
1454 }
1455 }
1456 else
1457 infof(data, "ALPN, server did not agree to a protocol\n");
1458
1459 Curl_multiuse_state(conn, conn->negnpn == CURL_HTTP_VERSION_2 ?
1460 BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
1461 }
1462 #endif
1463
1464 conn->ssl[sockindex].state = ssl_connection_complete;
1465 conn->recv[sockindex] = gtls_recv;
1466 conn->send[sockindex] = gtls_send;
1467
1468 if(SSL_SET_OPTION(primary.sessionid)) {
1469 /* we always unconditionally get the session id here, as even if we
1470 already got it from the cache and asked to use it in the connection, it
1471 might've been rejected and then a new one is in use now and we need to
1472 detect that. */
1473 void *connect_sessionid;
1474 size_t connect_idsize = 0;
1475
1476 /* get the session ID data size */
1477 gnutls_session_get_data(session, NULL, &connect_idsize);
1478 connect_sessionid = malloc(connect_idsize); /* get a buffer for it */
1479
1480 if(connect_sessionid) {
1481 bool incache;
1482 void *ssl_sessionid;
1483
1484 /* extract session ID to the allocated buffer */
1485 gnutls_session_get_data(session, connect_sessionid, &connect_idsize);
1486
1487 Curl_ssl_sessionid_lock(conn);
1488 incache = !(Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL,
1489 sockindex));
1490 if(incache) {
1491 /* there was one before in the cache, so instead of risking that the
1492 previous one was rejected, we just kill that and store the new */
1493 Curl_ssl_delsessionid(conn, ssl_sessionid);
1494 }
1495
1496 /* store this session id */
1497 result = Curl_ssl_addsessionid(conn, connect_sessionid, connect_idsize,
1498 sockindex);
1499 Curl_ssl_sessionid_unlock(conn);
1500 if(result) {
1501 free(connect_sessionid);
1502 result = CURLE_OUT_OF_MEMORY;
1503 }
1504 }
1505 else
1506 result = CURLE_OUT_OF_MEMORY;
1507 }
1508
1509 return result;
1510 }
1511
1512
1513 /*
1514 * This function is called after the TCP connect has completed. Setup the TLS
1515 * layer and do all necessary magic.
1516 */
1517 /* We use connssl->connecting_state to keep track of the connection status;
1518 there are three states: 'ssl_connect_1' (not started yet or complete),
1519 'ssl_connect_2_reading' (waiting for data from server), and
1520 'ssl_connect_2_writing' (waiting to be able to write).
1521 */
1522 static CURLcode
1523 gtls_connect_common(struct connectdata *conn,
1524 int sockindex,
1525 bool nonblocking,
1526 bool *done)
1527 {
1528 int rc;
1529 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
1530
1531 /* Initiate the connection, if not already done */
1532 if(ssl_connect_1 == connssl->connecting_state) {
1533 rc = gtls_connect_step1(conn, sockindex);
1534 if(rc)
1535 return rc;
1536 }
1537
1538 rc = handshake(conn, sockindex, TRUE, nonblocking);
1539 if(rc)
1540 /* handshake() sets its own error message with failf() */
1541 return rc;
1542
1543 /* Finish connecting once the handshake is done */
1544 if(ssl_connect_1 == connssl->connecting_state) {
1545 rc = gtls_connect_step3(conn, sockindex);
1546 if(rc)
1547 return rc;
1548 }
1549
1550 *done = ssl_connect_1 == connssl->connecting_state;
1551
1552 return CURLE_OK;
1553 }
1554
1555 static CURLcode Curl_gtls_connect_nonblocking(struct connectdata *conn,
1556 int sockindex, bool *done)
1557 {
1558 return gtls_connect_common(conn, sockindex, TRUE, done);
1559 }
1560
1561 static CURLcode Curl_gtls_connect(struct connectdata *conn, int sockindex)
1562 {
1563 CURLcode result;
1564 bool done = FALSE;
1565
1566 result = gtls_connect_common(conn, sockindex, FALSE, &done);
1567 if(result)
1568 return result;
1569
1570 DEBUGASSERT(done);
1571
1572 return CURLE_OK;
1573 }
1574
1575 static bool Curl_gtls_data_pending(const struct connectdata *conn,
1576 int connindex)
1577 {
1578 const struct ssl_connect_data *connssl = &conn->ssl[connindex];
1579 bool res = FALSE;
1580 if(BACKEND->session &&
1581 0 != gnutls_record_check_pending(BACKEND->session))
1582 res = TRUE;
1583
1584 connssl = &conn->proxy_ssl[connindex];
1585 if(BACKEND->session &&
1586 0 != gnutls_record_check_pending(BACKEND->session))
1587 res = TRUE;
1588
1589 return res;
1590 }
1591
1592 static ssize_t gtls_send(struct connectdata *conn,
1593 int sockindex,
1594 const void *mem,
1595 size_t len,
1596 CURLcode *curlcode)
1597 {
1598 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
1599 ssize_t rc = gnutls_record_send(BACKEND->session, mem, len);
1600
1601 if(rc < 0) {
1602 *curlcode = (rc == GNUTLS_E_AGAIN)
1603 ? CURLE_AGAIN
1604 : CURLE_SEND_ERROR;
1605
1606 rc = -1;
1607 }
1608
1609 return rc;
1610 }
1611
1612 static void close_one(struct ssl_connect_data *connssl)
1613 {
1614 if(BACKEND->session) {
1615 gnutls_bye(BACKEND->session, GNUTLS_SHUT_WR);
1616 gnutls_deinit(BACKEND->session);
1617 BACKEND->session = NULL;
1618 }
1619 if(BACKEND->cred) {
1620 gnutls_certificate_free_credentials(BACKEND->cred);
1621 BACKEND->cred = NULL;
1622 }
1623 #ifdef USE_TLS_SRP
1624 if(BACKEND->srp_client_cred) {
1625 gnutls_srp_free_client_credentials(BACKEND->srp_client_cred);
1626 BACKEND->srp_client_cred = NULL;
1627 }
1628 #endif
1629 }
1630
1631 static void Curl_gtls_close(struct connectdata *conn, int sockindex)
1632 {
1633 close_one(&conn->ssl[sockindex]);
1634 close_one(&conn->proxy_ssl[sockindex]);
1635 }
1636
1637 /*
1638 * This function is called to shut down the SSL layer but keep the
1639 * socket open (CCC - Clear Command Channel)
1640 */
1641 static int Curl_gtls_shutdown(struct connectdata *conn, int sockindex)
1642 {
1643 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
1644 int retval = 0;
1645 struct Curl_easy *data = conn->data;
1646
1647 #ifndef CURL_DISABLE_FTP
1648 /* This has only been tested on the proftpd server, and the mod_tls code
1649 sends a close notify alert without waiting for a close notify alert in
1650 response. Thus we wait for a close notify alert from the server, but
1651 we do not send one. Let's hope other servers do the same... */
1652
1653 if(data->set.ftp_ccc == CURLFTPSSL_CCC_ACTIVE)
1654 gnutls_bye(BACKEND->session, GNUTLS_SHUT_WR);
1655 #endif
1656
1657 if(BACKEND->session) {
1658 ssize_t result;
1659 bool done = FALSE;
1660 char buf[120];
1661
1662 while(!done) {
1663 int what = SOCKET_READABLE(conn->sock[sockindex],
1664 SSL_SHUTDOWN_TIMEOUT);
1665 if(what > 0) {
1666 /* Something to read, let's do it and hope that it is the close
1667 notify alert from the server */
1668 result = gnutls_record_recv(BACKEND->session,
1669 buf, sizeof(buf));
1670 switch(result) {
1671 case 0:
1672 /* This is the expected response. There was no data but only
1673 the close notify alert */
1674 done = TRUE;
1675 break;
1676 case GNUTLS_E_AGAIN:
1677 case GNUTLS_E_INTERRUPTED:
1678 infof(data, "GNUTLS_E_AGAIN || GNUTLS_E_INTERRUPTED\n");
1679 break;
1680 default:
1681 retval = -1;
1682 done = TRUE;
1683 break;
1684 }
1685 }
1686 else if(0 == what) {
1687 /* timeout */
1688 failf(data, "SSL shutdown timeout");
1689 done = TRUE;
1690 }
1691 else {
1692 /* anything that gets here is fatally bad */
1693 failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
1694 retval = -1;
1695 done = TRUE;
1696 }
1697 }
1698 gnutls_deinit(BACKEND->session);
1699 }
1700 gnutls_certificate_free_credentials(BACKEND->cred);
1701
1702 #ifdef USE_TLS_SRP
1703 if(SSL_SET_OPTION(authtype) == CURL_TLSAUTH_SRP
1704 && SSL_SET_OPTION(username) != NULL)
1705 gnutls_srp_free_client_credentials(BACKEND->srp_client_cred);
1706 #endif
1707
1708 BACKEND->cred = NULL;
1709 BACKEND->session = NULL;
1710
1711 return retval;
1712 }
1713
1714 static ssize_t gtls_recv(struct connectdata *conn, /* connection data */
1715 int num, /* socketindex */
1716 char *buf, /* store read data here */
1717 size_t buffersize, /* max amount to read */
1718 CURLcode *curlcode)
1719 {
1720 struct ssl_connect_data *connssl = &conn->ssl[num];
1721 ssize_t ret;
1722
1723 ret = gnutls_record_recv(BACKEND->session, buf, buffersize);
1724 if((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED)) {
1725 *curlcode = CURLE_AGAIN;
1726 return -1;
1727 }
1728
1729 if(ret == GNUTLS_E_REHANDSHAKE) {
1730 /* BLOCKING call, this is bad but a work-around for now. Fixing this "the
1731 proper way" takes a whole lot of work. */
1732 CURLcode result = handshake(conn, num, FALSE, FALSE);
1733 if(result)
1734 /* handshake() writes error message on its own */
1735 *curlcode = result;
1736 else
1737 *curlcode = CURLE_AGAIN; /* then return as if this was a wouldblock */
1738 return -1;
1739 }
1740
1741 if(ret < 0) {
1742 failf(conn->data, "GnuTLS recv error (%d): %s",
1743
1744 (int)ret, gnutls_strerror((int)ret));
1745 *curlcode = CURLE_RECV_ERROR;
1746 return -1;
1747 }
1748
1749 return ret;
1750 }
1751
1752 static void Curl_gtls_session_free(void *ptr)
1753 {
1754 free(ptr);
1755 }
1756
1757 static size_t Curl_gtls_version(char *buffer, size_t size)
1758 {
1759 return msnprintf(buffer, size, "GnuTLS/%s", gnutls_check_version(NULL));
1760 }
1761
1762 #ifndef USE_GNUTLS_NETTLE
1763 static int Curl_gtls_seed(struct Curl_easy *data)
1764 {
1765 /* we have the "SSL is seeded" boolean static to prevent multiple
1766 time-consuming seedings in vain */
1767 static bool ssl_seeded = FALSE;
1768
1769 /* Quickly add a bit of entropy */
1770 gcry_fast_random_poll();
1771
1772 if(!ssl_seeded || data->set.str[STRING_SSL_RANDOM_FILE] ||
1773 data->set.str[STRING_SSL_EGDSOCKET]) {
1774 ssl_seeded = TRUE;
1775 }
1776 return 0;
1777 }
1778 #endif
1779
1780 /* data might be NULL! */
1781 static CURLcode Curl_gtls_random(struct Curl_easy *data,
1782 unsigned char *entropy, size_t length)
1783 {
1784 #if defined(USE_GNUTLS_NETTLE)
1785 int rc;
1786 (void)data;
1787 rc = gnutls_rnd(GNUTLS_RND_RANDOM, entropy, length);
1788 return rc?CURLE_FAILED_INIT:CURLE_OK;
1789 #elif defined(USE_GNUTLS)
1790 if(data)
1791 Curl_gtls_seed(data); /* Initiate the seed if not already done */
1792 gcry_randomize(entropy, length, GCRY_STRONG_RANDOM);
1793 #endif
1794 return CURLE_OK;
1795 }
1796
1797 static CURLcode Curl_gtls_md5sum(unsigned char *tmp, /* input */
1798 size_t tmplen,
1799 unsigned char *md5sum, /* output */
1800 size_t md5len)
1801 {
1802 #if defined(USE_GNUTLS_NETTLE)
1803 struct md5_ctx MD5pw;
1804 md5_init(&MD5pw);
1805 md5_update(&MD5pw, (unsigned int)tmplen, tmp);
1806 md5_digest(&MD5pw, (unsigned int)md5len, md5sum);
1807 #elif defined(USE_GNUTLS)
1808 gcry_md_hd_t MD5pw;
1809 gcry_md_open(&MD5pw, GCRY_MD_MD5, 0);
1810 gcry_md_write(MD5pw, tmp, tmplen);
1811 memcpy(md5sum, gcry_md_read(MD5pw, 0), md5len);
1812 gcry_md_close(MD5pw);
1813 #endif
1814 return CURLE_OK;
1815 }
1816
1817 static CURLcode Curl_gtls_sha256sum(const unsigned char *tmp, /* input */
1818 size_t tmplen,
1819 unsigned char *sha256sum, /* output */
1820 size_t sha256len)
1821 {
1822 #if defined(USE_GNUTLS_NETTLE)
1823 struct sha256_ctx SHA256pw;
1824 sha256_init(&SHA256pw);
1825 sha256_update(&SHA256pw, (unsigned int)tmplen, tmp);
1826 sha256_digest(&SHA256pw, (unsigned int)sha256len, sha256sum);
1827 #elif defined(USE_GNUTLS)
1828 gcry_md_hd_t SHA256pw;
1829 gcry_md_open(&SHA256pw, GCRY_MD_SHA256, 0);
1830 gcry_md_write(SHA256pw, tmp, tmplen);
1831 memcpy(sha256sum, gcry_md_read(SHA256pw, 0), sha256len);
1832 gcry_md_close(SHA256pw);
1833 #endif
1834 return CURLE_OK;
1835 }
1836
1837 static bool Curl_gtls_cert_status_request(void)
1838 {
1839 #ifdef HAS_OCSP
1840 return TRUE;
1841 #else
1842 return FALSE;
1843 #endif
1844 }
1845
1846 static void *Curl_gtls_get_internals(struct ssl_connect_data *connssl,
1847 CURLINFO info UNUSED_PARAM)
1848 {
1849 (void)info;
1850 return BACKEND->session;
1851 }
1852
1853 const struct Curl_ssl Curl_ssl_gnutls = {
1854 { CURLSSLBACKEND_GNUTLS, "gnutls" }, /* info */
1855
1856 SSLSUPP_CA_PATH |
1857 SSLSUPP_CERTINFO |
1858 SSLSUPP_PINNEDPUBKEY |
1859 SSLSUPP_HTTPS_PROXY,
1860
1861 sizeof(struct ssl_backend_data),
1862
1863 Curl_gtls_init, /* init */
1864 Curl_gtls_cleanup, /* cleanup */
1865 Curl_gtls_version, /* version */
1866 Curl_none_check_cxn, /* check_cxn */
1867 Curl_gtls_shutdown, /* shutdown */
1868 Curl_gtls_data_pending, /* data_pending */
1869 Curl_gtls_random, /* random */
1870 Curl_gtls_cert_status_request, /* cert_status_request */
1871 Curl_gtls_connect, /* connect */
1872 Curl_gtls_connect_nonblocking, /* connect_nonblocking */
1873 Curl_gtls_get_internals, /* get_internals */
1874 Curl_gtls_close, /* close_one */
1875 Curl_none_close_all, /* close_all */
1876 Curl_gtls_session_free, /* session_free */
1877 Curl_none_set_engine, /* set_engine */
1878 Curl_none_set_engine_default, /* set_engine_default */
1879 Curl_none_engines_list, /* engines_list */
1880 Curl_none_false_start, /* false_start */
1881 Curl_gtls_md5sum, /* md5sum */
1882 Curl_gtls_sha256sum /* sha256sum */
1883 };
1884
1885 #endif /* USE_GNUTLS */