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