openssl.c
Go to the documentation of this file.
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 OpenSSL-specific code for the TLS/SSL layer. No code
25  * but vtls.c should ever call or use these functions.
26  */
27 
28 /*
29  * The original SSLeay-using code for curl was written by Linas Vepstas and
30  * Sampo Kellomaki 1998.
31  */
32 
33 #include "curl_setup.h"
34 
35 #ifdef USE_OPENSSL
36 
37 #ifdef HAVE_LIMITS_H
38 #include <limits.h>
39 #endif
40 
41 #include "urldata.h"
42 #include "sendf.h"
43 #include "formdata.h" /* for the boundary function */
44 #include "url.h" /* for the ssl config check function */
45 #include "inet_pton.h"
46 #include "openssl.h"
47 #include "connect.h"
48 #include "slist.h"
49 #include "select.h"
50 #include "vtls.h"
51 #include "strcase.h"
52 #include "hostcheck.h"
53 #include "curl_printf.h"
54 #include <openssl/ssl.h>
55 #ifdef HAVE_OPENSSL_ENGINE_H
56 #include <openssl/engine.h>
57 #endif
58 #include <openssl/rand.h>
59 #include <openssl/x509v3.h>
60 #ifndef OPENSSL_NO_DSA
61 #include <openssl/dsa.h>
62 #endif
63 #include <openssl/dh.h>
64 #include <openssl/err.h>
65 #include <openssl/md5.h>
66 #include <openssl/conf.h>
67 #include <openssl/bn.h>
68 #include <openssl/rsa.h>
69 #include <openssl/bio.h>
70 #include <openssl/buffer.h>
71 
72 #ifdef HAVE_OPENSSL_PKCS12_H
73 #include <openssl/pkcs12.h>
74 #endif
75 
76 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_OCSP)
77 #include <openssl/ocsp.h>
78 #endif
79 
80 #include "warnless.h"
81 #include "non-ascii.h" /* for Curl_convert_from_utf8 prototype */
82 
83 /* The last #include files should be: */
84 #include "curl_memory.h"
85 #include "memdebug.h"
86 
87 #ifndef OPENSSL_VERSION_NUMBER
88 #error "OPENSSL_VERSION_NUMBER not defined"
89 #endif
90 
91 #if defined(HAVE_OPENSSL_ENGINE_H)
92 #include <openssl/ui.h>
93 #endif
94 
95 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
96 #define SSL_METHOD_QUAL const
97 #else
98 #define SSL_METHOD_QUAL
99 #endif
100 
101 #if (OPENSSL_VERSION_NUMBER >= 0x10000000L)
102 #define HAVE_ERR_REMOVE_THREAD_STATE 1
103 #endif
104 
105 #if !defined(HAVE_SSLV2_CLIENT_METHOD) || \
106  OPENSSL_VERSION_NUMBER >= 0x10100000L /* 1.1.0+ has no SSLv2 */
107 #undef OPENSSL_NO_SSL2 /* undef first to avoid compiler warnings */
108 #define OPENSSL_NO_SSL2
109 #endif
110 
111 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && /* OpenSSL 1.1.0+ */ \
112  !defined(LIBRESSL_VERSION_NUMBER)
113 #define SSLEAY_VERSION_NUMBER OPENSSL_VERSION_NUMBER
114 #define HAVE_X509_GET0_EXTENSIONS 1 /* added in 1.1.0 -pre1 */
115 #define HAVE_OPAQUE_EVP_PKEY 1 /* since 1.1.0 -pre3 */
116 #define HAVE_OPAQUE_RSA_DSA_DH 1 /* since 1.1.0 -pre5 */
117 #define CONST_EXTS const
118 #define HAVE_ERR_REMOVE_THREAD_STATE_DEPRECATED 1
119 #else
120 /* For OpenSSL before 1.1.0 */
121 #define ASN1_STRING_get0_data(x) ASN1_STRING_data(x)
122 #define X509_get0_notBefore(x) X509_get_notBefore(x)
123 #define X509_get0_notAfter(x) X509_get_notAfter(x)
124 #define CONST_EXTS /* nope */
125 #ifdef LIBRESSL_VERSION_NUMBER
126 static unsigned long OpenSSL_version_num(void)
127 {
128  return LIBRESSL_VERSION_NUMBER;
129 }
130 #else
131 #define OpenSSL_version_num() SSLeay()
132 #endif
133 #endif
134 
135 #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && /* 1.0.2 or later */ \
136  !defined(LIBRESSL_VERSION_NUMBER)
137 #define HAVE_X509_GET0_SIGNATURE 1
138 #endif
139 
140 #if OPENSSL_VERSION_NUMBER >= 0x10002003L && \
141  OPENSSL_VERSION_NUMBER <= 0x10002FFFL && \
142  !defined(OPENSSL_NO_COMP)
143 #define HAVE_SSL_COMP_FREE_COMPRESSION_METHODS 1
144 #endif
145 
146 #if (OPENSSL_VERSION_NUMBER < 0x0090808fL)
147 /* not present in older OpenSSL */
148 #define OPENSSL_load_builtin_modules(x)
149 #endif
150 
151 /*
152  * Whether SSL_CTX_set_keylog_callback is available.
153  * OpenSSL: supported since 1.1.1 https://github.com/openssl/openssl/pull/2287
154  * BoringSSL: supported since d28f59c27bac (committed 2015-11-19), the
155  * BORINGSSL_201512 macro from 2016-01-21 should be close enough.
156  * LibreSSL: unsupported in at least 2.5.1 (explicitly check for it since it
157  * lies and pretends to be OpenSSL 2.0.0).
158  */
159 #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && \
160  !defined(LIBRESSL_VERSION_NUMBER)) || \
161  defined(BORINGSSL_201512)
162 #define HAVE_KEYLOG_CALLBACK
163 #endif
164 
165 #if defined(LIBRESSL_VERSION_NUMBER)
166 #define OSSL_PACKAGE "LibreSSL"
167 #elif defined(OPENSSL_IS_BORINGSSL)
168 #define OSSL_PACKAGE "BoringSSL"
169 #else
170 #define OSSL_PACKAGE "OpenSSL"
171 #endif
172 
173 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
174 /* up2date versions of OpenSSL maintain the default reasonably secure without
175  * breaking compatibility, so it is better not to override the default by curl
176  */
177 #define DEFAULT_CIPHER_SELECTION NULL
178 #else
179 /* ... but it is not the case with old versions of OpenSSL */
180 #define DEFAULT_CIPHER_SELECTION \
181  "ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH"
182 #endif
183 
184 #ifdef ENABLE_SSLKEYLOGFILE
185 typedef struct ssl_tap_state {
186  int master_key_length;
187  unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH];
188  unsigned char client_random[SSL3_RANDOM_SIZE];
189 } ssl_tap_state_t;
190 #endif /* ENABLE_SSLKEYLOGFILE */
191 
192 struct ssl_backend_data {
193  /* these ones requires specific SSL-types */
194  SSL_CTX* ctx;
195  SSL* handle;
196  X509* server_cert;
197 #ifdef ENABLE_SSLKEYLOGFILE
198  /* tap_state holds the last seen master key if we're logging them */
199  ssl_tap_state_t tap_state;
200 #endif
201 };
202 
203 #define BACKEND connssl->backend
204 
205 /*
206  * Number of bytes to read from the random number seed file. This must be
207  * a finite value (because some entropy "files" like /dev/urandom have
208  * an infinite length), but must be large enough to provide enough
209  * entropy to properly seed OpenSSL's PRNG.
210  */
211 #define RAND_LOAD_LENGTH 1024
212 
213 #ifdef ENABLE_SSLKEYLOGFILE
214 /* The fp for the open SSLKEYLOGFILE, or NULL if not open */
215 static FILE *keylog_file_fp;
216 
217 #ifdef HAVE_KEYLOG_CALLBACK
218 static void ossl_keylog_callback(const SSL *ssl, const char *line)
219 {
220  (void)ssl;
221 
222  /* Using fputs here instead of fprintf since libcurl's fprintf replacement
223  may not be thread-safe. */
224  if(keylog_file_fp && line && *line) {
225  char stackbuf[256];
226  char *buf;
227  size_t linelen = strlen(line);
228 
229  if(linelen <= sizeof(stackbuf) - 2)
230  buf = stackbuf;
231  else {
232  buf = malloc(linelen + 2);
233  if(!buf)
234  return;
235  }
236  strncpy(buf, line, linelen);
237  buf[linelen] = '\n';
238  buf[linelen + 1] = '\0';
239 
240  fputs(buf, keylog_file_fp);
241  if(buf != stackbuf)
242  free(buf);
243  }
244 }
245 #else
246 #define KEYLOG_PREFIX "CLIENT_RANDOM "
247 #define KEYLOG_PREFIX_LEN (sizeof(KEYLOG_PREFIX) - 1)
248 /*
249  * tap_ssl_key is called by libcurl to make the CLIENT_RANDOMs if the OpenSSL
250  * being used doesn't have native support for doing that.
251  */
252 static void tap_ssl_key(const SSL *ssl, ssl_tap_state_t *state)
253 {
254  const char *hex = "0123456789ABCDEF";
255  int pos, i;
256  char line[KEYLOG_PREFIX_LEN + 2 * SSL3_RANDOM_SIZE + 1 +
257  2 * SSL_MAX_MASTER_KEY_LENGTH + 1 + 1];
258  const SSL_SESSION *session = SSL_get_session(ssl);
259  unsigned char client_random[SSL3_RANDOM_SIZE];
260  unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH];
261  int master_key_length = 0;
262 
263  if(!session || !keylog_file_fp)
264  return;
265 
266 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
267  /* ssl->s3 is not checked in openssl 1.1.0-pre6, but let's assume that
268  * we have a valid SSL context if we have a non-NULL session. */
269  SSL_get_client_random(ssl, client_random, SSL3_RANDOM_SIZE);
270  master_key_length =
271  SSL_SESSION_get_master_key(session, master_key, SSL_MAX_MASTER_KEY_LENGTH);
272 #else
273  if(ssl->s3 && session->master_key_length > 0) {
274  master_key_length = session->master_key_length;
275  memcpy(master_key, session->master_key, session->master_key_length);
276  memcpy(client_random, ssl->s3->client_random, SSL3_RANDOM_SIZE);
277  }
278 #endif
279 
280  if(master_key_length <= 0)
281  return;
282 
283  /* Skip writing keys if there is no key or it did not change. */
284  if(state->master_key_length == master_key_length &&
285  !memcmp(state->master_key, master_key, master_key_length) &&
286  !memcmp(state->client_random, client_random, SSL3_RANDOM_SIZE)) {
287  return;
288  }
289 
290  state->master_key_length = master_key_length;
291  memcpy(state->master_key, master_key, master_key_length);
292  memcpy(state->client_random, client_random, SSL3_RANDOM_SIZE);
293 
294  memcpy(line, KEYLOG_PREFIX, KEYLOG_PREFIX_LEN);
295  pos = KEYLOG_PREFIX_LEN;
296 
297  /* Client Random for SSLv3/TLS */
298  for(i = 0; i < SSL3_RANDOM_SIZE; i++) {
299  line[pos++] = hex[client_random[i] >> 4];
300  line[pos++] = hex[client_random[i] & 0xF];
301  }
302  line[pos++] = ' ';
303 
304  /* Master Secret (size is at most SSL_MAX_MASTER_KEY_LENGTH) */
305  for(i = 0; i < master_key_length; i++) {
306  line[pos++] = hex[master_key[i] >> 4];
307  line[pos++] = hex[master_key[i] & 0xF];
308  }
309  line[pos++] = '\n';
310  line[pos] = '\0';
311 
312  /* Using fputs here instead of fprintf since libcurl's fprintf replacement
313  may not be thread-safe. */
314  fputs(line, keylog_file_fp);
315 }
316 #endif /* !HAVE_KEYLOG_CALLBACK */
317 #endif /* ENABLE_SSLKEYLOGFILE */
318 
319 static const char *SSL_ERROR_to_str(int err)
320 {
321  switch(err) {
322  case SSL_ERROR_NONE:
323  return "SSL_ERROR_NONE";
324  case SSL_ERROR_SSL:
325  return "SSL_ERROR_SSL";
326  case SSL_ERROR_WANT_READ:
327  return "SSL_ERROR_WANT_READ";
328  case SSL_ERROR_WANT_WRITE:
329  return "SSL_ERROR_WANT_WRITE";
330  case SSL_ERROR_WANT_X509_LOOKUP:
331  return "SSL_ERROR_WANT_X509_LOOKUP";
332  case SSL_ERROR_SYSCALL:
333  return "SSL_ERROR_SYSCALL";
334  case SSL_ERROR_ZERO_RETURN:
335  return "SSL_ERROR_ZERO_RETURN";
336  case SSL_ERROR_WANT_CONNECT:
337  return "SSL_ERROR_WANT_CONNECT";
338  case SSL_ERROR_WANT_ACCEPT:
339  return "SSL_ERROR_WANT_ACCEPT";
340 #if defined(SSL_ERROR_WANT_ASYNC)
341  case SSL_ERROR_WANT_ASYNC:
342  return "SSL_ERROR_WANT_ASYNC";
343 #endif
344 #if defined(SSL_ERROR_WANT_ASYNC_JOB)
345  case SSL_ERROR_WANT_ASYNC_JOB:
346  return "SSL_ERROR_WANT_ASYNC_JOB";
347 #endif
348 #if defined(SSL_ERROR_WANT_EARLY)
349  case SSL_ERROR_WANT_EARLY:
350  return "SSL_ERROR_WANT_EARLY";
351 #endif
352  default:
353  return "SSL_ERROR unknown";
354  }
355 }
356 
357 /* Return error string for last OpenSSL error
358  */
359 static char *ossl_strerror(unsigned long error, char *buf, size_t size)
360 {
361  ERR_error_string_n(error, buf, size);
362  return buf;
363 }
364 
365 static int passwd_callback(char *buf, int num, int encrypting,
366  void *global_passwd)
367 {
368  DEBUGASSERT(0 == encrypting);
369 
370  if(!encrypting) {
371  int klen = curlx_uztosi(strlen((char *)global_passwd));
372  if(num > klen) {
373  memcpy(buf, global_passwd, klen + 1);
374  return klen;
375  }
376  }
377  return 0;
378 }
379 
380 /*
381  * rand_enough() returns TRUE if we have seeded the random engine properly.
382  */
383 static bool rand_enough(void)
384 {
385  return (0 != RAND_status()) ? TRUE : FALSE;
386 }
387 
388 static CURLcode Curl_ossl_seed(struct Curl_easy *data)
389 {
390  /* we have the "SSL is seeded" boolean static to prevent multiple
391  time-consuming seedings in vain */
392  static bool ssl_seeded = FALSE;
393  char fname[256];
394 
395  if(ssl_seeded)
396  return CURLE_OK;
397 
398  if(rand_enough()) {
399  /* OpenSSL 1.1.0+ will return here */
400  ssl_seeded = TRUE;
401  return CURLE_OK;
402  }
403 
404 #ifndef RANDOM_FILE
405  /* if RANDOM_FILE isn't defined, we only perform this if an option tells
406  us to! */
407  if(data->set.str[STRING_SSL_RANDOM_FILE])
408 #define RANDOM_FILE "" /* doesn't matter won't be used */
409 #endif
410  {
411  /* let the option override the define */
414  RANDOM_FILE),
415  RAND_LOAD_LENGTH);
416  if(rand_enough())
417  return CURLE_OK;
418  }
419 
420 #if defined(HAVE_RAND_EGD)
421  /* only available in OpenSSL 0.9.5 and later */
422  /* EGD_SOCKET is set at configure time or not at all */
423 #ifndef EGD_SOCKET
424  /* If we don't have the define set, we only do this if the egd-option
425  is set */
426  if(data->set.str[STRING_SSL_EGDSOCKET])
427 #define EGD_SOCKET "" /* doesn't matter won't be used */
428 #endif
429  {
430  /* If there's an option and a define, the option overrides the
431  define */
432  int ret = RAND_egd(data->set.str[STRING_SSL_EGDSOCKET]?
433  data->set.str[STRING_SSL_EGDSOCKET]:EGD_SOCKET);
434  if(-1 != ret) {
435  if(rand_enough())
436  return CURLE_OK;
437  }
438  }
439 #endif
440 
441  /* fallback to a custom seeding of the PRNG using a hash based on a current
442  time */
443  do {
444  unsigned char randb[64];
445  size_t len = sizeof(randb);
446  size_t i, i_max;
447  for(i = 0, i_max = len / sizeof(struct curltime); i < i_max; ++i) {
448  struct curltime tv = curlx_tvnow();
449  Curl_wait_ms(1);
450  tv.tv_sec *= i + 1;
451  tv.tv_usec *= (unsigned int)i + 2;
452  tv.tv_sec ^= ((curlx_tvnow().tv_sec + curlx_tvnow().tv_usec) *
453  (i + 3)) << 8;
454  tv.tv_usec ^= (unsigned int) ((curlx_tvnow().tv_sec +
455  curlx_tvnow().tv_usec) *
456  (i + 4)) << 16;
457  memcpy(&randb[i * sizeof(struct curltime)], &tv,
458  sizeof(struct curltime));
459  }
460  RAND_add(randb, (int)len, (double)len/2);
461  } while(!rand_enough());
462 
463  /* generates a default path for the random seed file */
464  fname[0] = 0; /* blank it first */
465  RAND_file_name(fname, sizeof(fname));
466  if(fname[0]) {
467  /* we got a file name to try */
468  RAND_load_file(fname, RAND_LOAD_LENGTH);
469  if(rand_enough())
470  return CURLE_OK;
471  }
472 
473  infof(data, "libcurl is now using a weak random seed!\n");
474  return (rand_enough() ? CURLE_OK :
475  CURLE_SSL_CONNECT_ERROR /* confusing error code */);
476 }
477 
478 #ifndef SSL_FILETYPE_ENGINE
479 #define SSL_FILETYPE_ENGINE 42
480 #endif
481 #ifndef SSL_FILETYPE_PKCS12
482 #define SSL_FILETYPE_PKCS12 43
483 #endif
484 static int do_file_type(const char *type)
485 {
486  if(!type || !type[0])
487  return SSL_FILETYPE_PEM;
488  if(strcasecompare(type, "PEM"))
489  return SSL_FILETYPE_PEM;
490  if(strcasecompare(type, "DER"))
491  return SSL_FILETYPE_ASN1;
492  if(strcasecompare(type, "ENG"))
493  return SSL_FILETYPE_ENGINE;
494  if(strcasecompare(type, "P12"))
495  return SSL_FILETYPE_PKCS12;
496  return -1;
497 }
498 
499 #if defined(HAVE_OPENSSL_ENGINE_H)
500 /*
501  * Supply default password to the engine user interface conversation.
502  * The password is passed by OpenSSL engine from ENGINE_load_private_key()
503  * last argument to the ui and can be obtained by UI_get0_user_data(ui) here.
504  */
505 static int ssl_ui_reader(UI *ui, UI_STRING *uis)
506 {
507  const char *password;
508  switch(UI_get_string_type(uis)) {
509  case UIT_PROMPT:
510  case UIT_VERIFY:
511  password = (const char *)UI_get0_user_data(ui);
512  if(password && (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD)) {
513  UI_set_result(ui, uis, password);
514  return 1;
515  }
516  default:
517  break;
518  }
519  return (UI_method_get_reader(UI_OpenSSL()))(ui, uis);
520 }
521 
522 /*
523  * Suppress interactive request for a default password if available.
524  */
525 static int ssl_ui_writer(UI *ui, UI_STRING *uis)
526 {
527  switch(UI_get_string_type(uis)) {
528  case UIT_PROMPT:
529  case UIT_VERIFY:
530  if(UI_get0_user_data(ui) &&
531  (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD)) {
532  return 1;
533  }
534  default:
535  break;
536  }
537  return (UI_method_get_writer(UI_OpenSSL()))(ui, uis);
538 }
539 #endif
540 
541 static
542 int cert_stuff(struct connectdata *conn,
543  SSL_CTX* ctx,
544  char *cert_file,
545  const char *cert_type,
546  char *key_file,
547  const char *key_type,
548  char *key_passwd)
549 {
550  struct Curl_easy *data = conn->data;
551  char error_buffer[256];
552  bool check_privkey = TRUE;
553 
554  int file_type = do_file_type(cert_type);
555 
556  if(cert_file || (file_type == SSL_FILETYPE_ENGINE)) {
557  SSL *ssl;
558  X509 *x509;
559  int cert_done = 0;
560 
561  if(key_passwd) {
562  /* set the password in the callback userdata */
563  SSL_CTX_set_default_passwd_cb_userdata(ctx, key_passwd);
564  /* Set passwd callback: */
565  SSL_CTX_set_default_passwd_cb(ctx, passwd_callback);
566  }
567 
568 
569  switch(file_type) {
570  case SSL_FILETYPE_PEM:
571  /* SSL_CTX_use_certificate_chain_file() only works on PEM files */
572  if(SSL_CTX_use_certificate_chain_file(ctx,
573  cert_file) != 1) {
574  failf(data,
575  "could not load PEM client certificate, " OSSL_PACKAGE
576  " error %s, "
577  "(no key found, wrong pass phrase, or wrong file format?)",
578  ossl_strerror(ERR_get_error(), error_buffer,
579  sizeof(error_buffer)) );
580  return 0;
581  }
582  break;
583 
584  case SSL_FILETYPE_ASN1:
585  /* SSL_CTX_use_certificate_file() works with either PEM or ASN1, but
586  we use the case above for PEM so this can only be performed with
587  ASN1 files. */
589  cert_file,
590  file_type) != 1) {
591  failf(data,
592  "could not load ASN1 client certificate, " OSSL_PACKAGE
593  " error %s, "
594  "(no key found, wrong pass phrase, or wrong file format?)",
595  ossl_strerror(ERR_get_error(), error_buffer,
596  sizeof(error_buffer)) );
597  return 0;
598  }
599  break;
600  case SSL_FILETYPE_ENGINE:
601 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(ENGINE_CTRL_GET_CMD_FROM_NAME)
602  {
603  if(data->state.engine) {
604  const char *cmd_name = "LOAD_CERT_CTRL";
605  struct {
606  const char *cert_id;
607  X509 *cert;
608  } params;
609 
610  params.cert_id = cert_file;
611  params.cert = NULL;
612 
613  /* Does the engine supports LOAD_CERT_CTRL ? */
614  if(!ENGINE_ctrl(data->state.engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
615  0, (void *)cmd_name, NULL)) {
616  failf(data, "ssl engine does not support loading certificates");
617  return 0;
618  }
619 
620  /* Load the certificate from the engine */
621  if(!ENGINE_ctrl_cmd(data->state.engine, cmd_name,
622  0, &params, NULL, 1)) {
623  failf(data, "ssl engine cannot load client cert with id"
624  " '%s' [%s]", cert_file,
625  ossl_strerror(ERR_get_error(), error_buffer,
626  sizeof(error_buffer)));
627  return 0;
628  }
629 
630  if(!params.cert) {
631  failf(data, "ssl engine didn't initialized the certificate "
632  "properly.");
633  return 0;
634  }
635 
636  if(SSL_CTX_use_certificate(ctx, params.cert) != 1) {
637  failf(data, "unable to set client certificate");
638  X509_free(params.cert);
639  return 0;
640  }
641  X509_free(params.cert); /* we don't need the handle any more... */
642  }
643  else {
644  failf(data, "crypto engine not set, can't load certificate");
645  return 0;
646  }
647  }
648  break;
649 #else
650  failf(data, "file type ENG for certificate not implemented");
651  return 0;
652 #endif
653 
654  case SSL_FILETYPE_PKCS12:
655  {
656 #ifdef HAVE_OPENSSL_PKCS12_H
657  FILE *f;
658  PKCS12 *p12;
659  EVP_PKEY *pri;
660  STACK_OF(X509) *ca = NULL;
661 
662  f = fopen(cert_file, "rb");
663  if(!f) {
664  failf(data, "could not open PKCS12 file '%s'", cert_file);
665  return 0;
666  }
667  p12 = d2i_PKCS12_fp(f, NULL);
668  fclose(f);
669 
670  if(!p12) {
671  failf(data, "error reading PKCS12 file '%s'", cert_file);
672  return 0;
673  }
674 
675  PKCS12_PBE_add();
676 
677  if(!PKCS12_parse(p12, key_passwd, &pri, &x509,
678  &ca)) {
679  failf(data,
680  "could not parse PKCS12 file, check password, " OSSL_PACKAGE
681  " error %s",
682  ossl_strerror(ERR_get_error(), error_buffer,
683  sizeof(error_buffer)) );
684  PKCS12_free(p12);
685  return 0;
686  }
687 
688  PKCS12_free(p12);
689 
690  if(SSL_CTX_use_certificate(ctx, x509) != 1) {
691  failf(data,
692  "could not load PKCS12 client certificate, " OSSL_PACKAGE
693  " error %s",
694  ossl_strerror(ERR_get_error(), error_buffer,
695  sizeof(error_buffer)) );
696  goto fail;
697  }
698 
699  if(SSL_CTX_use_PrivateKey(ctx, pri) != 1) {
700  failf(data, "unable to use private key from PKCS12 file '%s'",
701  cert_file);
702  goto fail;
703  }
704 
705  if(!SSL_CTX_check_private_key (ctx)) {
706  failf(data, "private key from PKCS12 file '%s' "
707  "does not match certificate in same file", cert_file);
708  goto fail;
709  }
710  /* Set Certificate Verification chain */
711  if(ca) {
712  while(sk_X509_num(ca)) {
713  /*
714  * Note that sk_X509_pop() is used below to make sure the cert is
715  * removed from the stack properly before getting passed to
716  * SSL_CTX_add_extra_chain_cert(), which takes ownership. Previously
717  * we used sk_X509_value() instead, but then we'd clean it in the
718  * subsequent sk_X509_pop_free() call.
719  */
720  X509 *x = sk_X509_pop(ca);
721  if(!SSL_CTX_add_client_CA(ctx, x)) {
722  X509_free(x);
723  failf(data, "cannot add certificate to client CA list");
724  goto fail;
725  }
726  if(!SSL_CTX_add_extra_chain_cert(ctx, x)) {
727  X509_free(x);
728  failf(data, "cannot add certificate to certificate chain");
729  goto fail;
730  }
731  }
732  }
733 
734  cert_done = 1;
735  fail:
736  EVP_PKEY_free(pri);
737  X509_free(x509);
738  sk_X509_pop_free(ca, X509_free);
739 
740  if(!cert_done)
741  return 0; /* failure! */
742  break;
743 #else
744  failf(data, "file type P12 for certificate not supported");
745  return 0;
746 #endif
747  }
748  default:
749  failf(data, "not supported file type '%s' for certificate", cert_type);
750  return 0;
751  }
752 
753  file_type = do_file_type(key_type);
754 
755  switch(file_type) {
756  case SSL_FILETYPE_PEM:
757  if(cert_done)
758  break;
759  if(!key_file)
760  /* cert & key can only be in PEM case in the same file */
761  key_file = cert_file;
762  /* FALLTHROUGH */
763  case SSL_FILETYPE_ASN1:
764  if(SSL_CTX_use_PrivateKey_file(ctx, key_file, file_type) != 1) {
765  failf(data, "unable to set private key file: '%s' type %s",
766  key_file, key_type?key_type:"PEM");
767  return 0;
768  }
769  break;
770  case SSL_FILETYPE_ENGINE:
771 #ifdef HAVE_OPENSSL_ENGINE_H
772  { /* XXXX still needs some work */
773  EVP_PKEY *priv_key = NULL;
774  if(data->state.engine) {
775  UI_METHOD *ui_method =
776  UI_create_method((char *)"curl user interface");
777  if(!ui_method) {
778  failf(data, "unable do create " OSSL_PACKAGE
779  " user-interface method");
780  return 0;
781  }
784  UI_method_set_reader(ui_method, ssl_ui_reader);
785  UI_method_set_writer(ui_method, ssl_ui_writer);
786  /* the typecast below was added to please mingw32 */
787  priv_key = (EVP_PKEY *)
788  ENGINE_load_private_key(data->state.engine, key_file,
789  ui_method,
790  key_passwd);
791  UI_destroy_method(ui_method);
792  if(!priv_key) {
793  failf(data, "failed to load private key from crypto engine");
794  return 0;
795  }
796  if(SSL_CTX_use_PrivateKey(ctx, priv_key) != 1) {
797  failf(data, "unable to set private key");
798  EVP_PKEY_free(priv_key);
799  return 0;
800  }
801  EVP_PKEY_free(priv_key); /* we don't need the handle any more... */
802  }
803  else {
804  failf(data, "crypto engine not set, can't load private key");
805  return 0;
806  }
807  }
808  break;
809 #else
810  failf(data, "file type ENG for private key not supported");
811  return 0;
812 #endif
813  case SSL_FILETYPE_PKCS12:
814  if(!cert_done) {
815  failf(data, "file type P12 for private key not supported");
816  return 0;
817  }
818  break;
819  default:
820  failf(data, "not supported file type for private key");
821  return 0;
822  }
823 
824  ssl = SSL_new(ctx);
825  if(!ssl) {
826  failf(data, "unable to create an SSL structure");
827  return 0;
828  }
829 
830  x509 = SSL_get_certificate(ssl);
831 
832  /* This version was provided by Evan Jordan and is supposed to not
833  leak memory as the previous version: */
834  if(x509) {
835  EVP_PKEY *pktmp = X509_get_pubkey(x509);
837  EVP_PKEY_free(pktmp);
838  }
839 
840 #ifndef OPENSSL_NO_RSA
841  {
842  /* If RSA is used, don't check the private key if its flags indicate
843  * it doesn't support it. */
844  EVP_PKEY *priv_key = SSL_get_privatekey(ssl);
845  if(EVP_PKEY_id(priv_key) == EVP_PKEY_RSA) {
846  RSA *rsa = EVP_PKEY_get1_RSA(priv_key);
847  if(RSA_flags(rsa) & RSA_METHOD_FLAG_NO_CHECK)
848  check_privkey = FALSE;
849  RSA_free(rsa); /* Decrement reference count */
850  }
851  }
852 #endif
853 
854  SSL_free(ssl);
855 
856  /* If we are using DSA, we can copy the parameters from
857  * the private key */
858 
859  if(check_privkey == TRUE) {
860  /* Now we know that a key and cert have been set against
861  * the SSL context */
862  if(!SSL_CTX_check_private_key(ctx)) {
863  failf(data, "Private key does not match the certificate public key");
864  return 0;
865  }
866  }
867  }
868  return 1;
869 }
870 
871 /* returns non-zero on failure */
872 static int x509_name_oneline(X509_NAME *a, char *buf, size_t size)
873 {
874 #if 0
875  return X509_NAME_oneline(a, buf, size);
876 #else
877  BIO *bio_out = BIO_new(BIO_s_mem());
878  BUF_MEM *biomem;
879  int rc;
880 
881  if(!bio_out)
882  return 1; /* alloc failed! */
883 
884  rc = X509_NAME_print_ex(bio_out, a, 0, XN_FLAG_SEP_SPLUS_SPC);
885  BIO_get_mem_ptr(bio_out, &biomem);
886 
887  if((size_t)biomem->length < size)
888  size = biomem->length;
889  else
890  size--; /* don't overwrite the buffer end */
891 
892  memcpy(buf, biomem->data, size);
893  buf[size] = 0;
894 
895  BIO_free(bio_out);
896 
897  return !rc;
898 #endif
899 }
900 
907 static int Curl_ossl_init(void)
908 {
909 #ifdef ENABLE_SSLKEYLOGFILE
910  const char *keylog_file_name;
911 #endif
912 
914 
915 #ifdef HAVE_ENGINE_LOAD_BUILTIN_ENGINES
917 #endif
918 
919  /* OPENSSL_config(NULL); is "strongly recommended" to use but unfortunately
920  that function makes an exit() call on wrongly formatted config files
921  which makes it hard to use in some situations. OPENSSL_config() itself
922  calls CONF_modules_load_file() and we use that instead and we ignore
923  its return code! */
924 
925  /* CONF_MFLAGS_DEFAULT_SECTION introduced some time between 0.9.8b and
926  0.9.8e */
927 #ifndef CONF_MFLAGS_DEFAULT_SECTION
928 #define CONF_MFLAGS_DEFAULT_SECTION 0x0
929 #endif
930 
931  CONF_modules_load_file(NULL, NULL,
932  CONF_MFLAGS_DEFAULT_SECTION|
933  CONF_MFLAGS_IGNORE_MISSING_FILE);
934 
935 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && \
936  !defined(LIBRESSL_VERSION_NUMBER)
937  /* OpenSSL 1.1.0+ takes care of initialization itself */
938 #else
939  /* Lets get nice error messages */
941 
942  /* Init the global ciphers and digests */
943  if(!SSLeay_add_ssl_algorithms())
944  return 0;
945 
946  OpenSSL_add_all_algorithms();
947 #endif
948 
949 #ifdef ENABLE_SSLKEYLOGFILE
950  keylog_file_name = curl_getenv("SSLKEYLOGFILE");
951  if(keylog_file_name && !keylog_file_fp) {
952  keylog_file_fp = fopen(keylog_file_name, FOPEN_APPENDTEXT);
953  if(keylog_file_fp) {
954  if(setvbuf(keylog_file_fp, NULL, _IOLBF, 4096)) {
955  fclose(keylog_file_fp);
956  keylog_file_fp = NULL;
957  }
958  }
959  }
960 #endif
961 
962  return 1;
963 }
964 
965 /* Global cleanup */
966 static void Curl_ossl_cleanup(void)
967 {
968 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && \
969  !defined(LIBRESSL_VERSION_NUMBER)
970  /* OpenSSL 1.1 deprecates all these cleanup functions and
971  turns them into no-ops in OpenSSL 1.0 compatibility mode */
972 #else
973  /* Free ciphers and digests lists */
974  EVP_cleanup();
975 
976 #ifdef HAVE_ENGINE_CLEANUP
977  /* Free engine list */
978  ENGINE_cleanup();
979 #endif
980 
981  /* Free OpenSSL error strings */
983 
984  /* Free thread local error state, destroying hash upon zero refcount */
985 #ifdef HAVE_ERR_REMOVE_THREAD_STATE
986  ERR_remove_thread_state(NULL);
987 #else
988  ERR_remove_state(0);
989 #endif
990 
991  /* Free all memory allocated by all configuration modules */
992  CONF_modules_free();
993 
994 #ifdef HAVE_SSL_COMP_FREE_COMPRESSION_METHODS
995  SSL_COMP_free_compression_methods();
996 #endif
997 #endif
998 
999 #ifdef ENABLE_SSLKEYLOGFILE
1000  if(keylog_file_fp) {
1001  fclose(keylog_file_fp);
1002  keylog_file_fp = NULL;
1003  }
1004 #endif
1005 }
1006 
1007 /*
1008  * This function is used to determine connection status.
1009  *
1010  * Return codes:
1011  * 1 means the connection is still in place
1012  * 0 means the connection has been closed
1013  * -1 means the connection status is unknown
1014  */
1015 static int Curl_ossl_check_cxn(struct connectdata *conn)
1016 {
1017  /* SSL_peek takes data out of the raw recv buffer without peeking so we use
1018  recv MSG_PEEK instead. Bug #795 */
1019 #ifdef MSG_PEEK
1020  char buf;
1021  ssize_t nread;
1022  nread = recv((RECV_TYPE_ARG1)conn->sock[FIRSTSOCKET], (RECV_TYPE_ARG2)&buf,
1023  (RECV_TYPE_ARG3)1, (RECV_TYPE_ARG4)MSG_PEEK);
1024  if(nread == 0)
1025  return 0; /* connection has been closed */
1026  if(nread == 1)
1027  return 1; /* connection still in place */
1028  else if(nread == -1) {
1029  int err = SOCKERRNO;
1030  if(err == EINPROGRESS ||
1031 #if defined(EAGAIN) && (EAGAIN != EWOULDBLOCK)
1032  err == EAGAIN ||
1033 #endif
1034  err == EWOULDBLOCK)
1035  return 1; /* connection still in place */
1036  if(err == ECONNRESET ||
1037 #ifdef ECONNABORTED
1038  err == ECONNABORTED ||
1039 #endif
1040 #ifdef ENETDOWN
1041  err == ENETDOWN ||
1042 #endif
1043 #ifdef ENETRESET
1044  err == ENETRESET ||
1045 #endif
1046 #ifdef ESHUTDOWN
1047  err == ESHUTDOWN ||
1048 #endif
1049 #ifdef ETIMEDOUT
1050  err == ETIMEDOUT ||
1051 #endif
1052  err == ENOTCONN)
1053  return 0; /* connection has been closed */
1054  }
1055 #endif
1056  return -1; /* connection status unknown */
1057 }
1058 
1059 /* Selects an OpenSSL crypto engine
1060  */
1061 static CURLcode Curl_ossl_set_engine(struct Curl_easy *data,
1062  const char *engine)
1063 {
1064 #if defined(USE_OPENSSL) && defined(HAVE_OPENSSL_ENGINE_H)
1065  ENGINE *e;
1066 
1067 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
1068  e = ENGINE_by_id(engine);
1069 #else
1070  /* avoid memory leak */
1071  for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) {
1072  const char *e_id = ENGINE_get_id(e);
1073  if(!strcmp(engine, e_id))
1074  break;
1075  }
1076 #endif
1077 
1078  if(!e) {
1079  failf(data, "SSL Engine '%s' not found", engine);
1081  }
1082 
1083  if(data->state.engine) {
1084  ENGINE_finish(data->state.engine);
1085  ENGINE_free(data->state.engine);
1086  data->state.engine = NULL;
1087  }
1088  if(!ENGINE_init(e)) {
1089  char buf[256];
1090 
1091  ENGINE_free(e);
1092  failf(data, "Failed to initialise SSL Engine '%s':\n%s",
1093  engine, ossl_strerror(ERR_get_error(), buf, sizeof(buf)));
1095  }
1096  data->state.engine = e;
1097  return CURLE_OK;
1098 #else
1099  (void)engine;
1100  failf(data, "SSL Engine not supported");
1102 #endif
1103 }
1104 
1105 /* Sets engine as default for all SSL operations
1106  */
1107 static CURLcode Curl_ossl_set_engine_default(struct Curl_easy *data)
1108 {
1109 #ifdef HAVE_OPENSSL_ENGINE_H
1110  if(data->state.engine) {
1111  if(ENGINE_set_default(data->state.engine, ENGINE_METHOD_ALL) > 0) {
1112  infof(data, "set default crypto engine '%s'\n",
1113  ENGINE_get_id(data->state.engine));
1114  }
1115  else {
1116  failf(data, "set default crypto engine '%s' failed",
1117  ENGINE_get_id(data->state.engine));
1119  }
1120  }
1121 #else
1122  (void) data;
1123 #endif
1124  return CURLE_OK;
1125 }
1126 
1127 /* Return list of OpenSSL crypto engine names.
1128  */
1129 static struct curl_slist *Curl_ossl_engines_list(struct Curl_easy *data)
1130 {
1131  struct curl_slist *list = NULL;
1132 #if defined(USE_OPENSSL) && defined(HAVE_OPENSSL_ENGINE_H)
1133  struct curl_slist *beg;
1134  ENGINE *e;
1135 
1136  for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) {
1137  beg = curl_slist_append(list, ENGINE_get_id(e));
1138  if(!beg) {
1139  curl_slist_free_all(list);
1140  return NULL;
1141  }
1142  list = beg;
1143  }
1144 #endif
1145  (void) data;
1146  return list;
1147 }
1148 
1149 
1150 static void ossl_close(struct ssl_connect_data *connssl)
1151 {
1152  if(BACKEND->handle) {
1153  (void)SSL_shutdown(BACKEND->handle);
1154  SSL_set_connect_state(BACKEND->handle);
1155 
1156  SSL_free(BACKEND->handle);
1157  BACKEND->handle = NULL;
1158  }
1159  if(BACKEND->ctx) {
1160  SSL_CTX_free(BACKEND->ctx);
1161  BACKEND->ctx = NULL;
1162  }
1163 }
1164 
1165 /*
1166  * This function is called when an SSL connection is closed.
1167  */
1168 static void Curl_ossl_close(struct connectdata *conn, int sockindex)
1169 {
1170  ossl_close(&conn->ssl[sockindex]);
1171  ossl_close(&conn->proxy_ssl[sockindex]);
1172 }
1173 
1174 /*
1175  * This function is called to shut down the SSL layer but keep the
1176  * socket open (CCC - Clear Command Channel)
1177  */
1178 static int Curl_ossl_shutdown(struct connectdata *conn, int sockindex)
1179 {
1180  int retval = 0;
1181  struct ssl_connect_data *connssl = &conn->ssl[sockindex];
1182  struct Curl_easy *data = conn->data;
1183  char buf[256]; /* We will use this for the OpenSSL error buffer, so it has
1184  to be at least 256 bytes long. */
1185  unsigned long sslerror;
1186  ssize_t nread;
1187  int buffsize;
1188  int err;
1189  int done = 0;
1190 
1191  /* This has only been tested on the proftpd server, and the mod_tls code
1192  sends a close notify alert without waiting for a close notify alert in
1193  response. Thus we wait for a close notify alert from the server, but
1194  we do not send one. Let's hope other servers do the same... */
1195 
1196  if(data->set.ftp_ccc == CURLFTPSSL_CCC_ACTIVE)
1197  (void)SSL_shutdown(BACKEND->handle);
1198 
1199  if(BACKEND->handle) {
1200  buffsize = (int)sizeof(buf);
1201  while(!done) {
1202  int what = SOCKET_READABLE(conn->sock[sockindex],
1203  SSL_SHUTDOWN_TIMEOUT);
1204  if(what > 0) {
1205  ERR_clear_error();
1206 
1207  /* Something to read, let's do it and hope that it is the close
1208  notify alert from the server */
1209  nread = (ssize_t)SSL_read(BACKEND->handle, buf, buffsize);
1210  err = SSL_get_error(BACKEND->handle, (int)nread);
1211 
1212  switch(err) {
1213  case SSL_ERROR_NONE: /* this is not an error */
1214  case SSL_ERROR_ZERO_RETURN: /* no more data */
1215  /* This is the expected response. There was no data but only
1216  the close notify alert */
1217  done = 1;
1218  break;
1219  case SSL_ERROR_WANT_READ:
1220  /* there's data pending, re-invoke SSL_read() */
1221  infof(data, "SSL_ERROR_WANT_READ\n");
1222  break;
1223  case SSL_ERROR_WANT_WRITE:
1224  /* SSL wants a write. Really odd. Let's bail out. */
1225  infof(data, "SSL_ERROR_WANT_WRITE\n");
1226  done = 1;
1227  break;
1228  default:
1229  /* openssl/ssl.h says "look at error stack/return value/errno" */
1230  sslerror = ERR_get_error();
1231  failf(conn->data, OSSL_PACKAGE " SSL_read on shutdown: %s, errno %d",
1232  (sslerror ?
1233  ossl_strerror(sslerror, buf, sizeof(buf)) :
1234  SSL_ERROR_to_str(err)),
1235  SOCKERRNO);
1236  done = 1;
1237  break;
1238  }
1239  }
1240  else if(0 == what) {
1241  /* timeout */
1242  failf(data, "SSL shutdown timeout");
1243  done = 1;
1244  }
1245  else {
1246  /* anything that gets here is fatally bad */
1247  failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
1248  retval = -1;
1249  done = 1;
1250  }
1251  } /* while()-loop for the select() */
1252 
1253  if(data->set.verbose) {
1254 #ifdef HAVE_SSL_GET_SHUTDOWN
1255  switch(SSL_get_shutdown(BACKEND->handle)) {
1256  case SSL_SENT_SHUTDOWN:
1257  infof(data, "SSL_get_shutdown() returned SSL_SENT_SHUTDOWN\n");
1258  break;
1259  case SSL_RECEIVED_SHUTDOWN:
1260  infof(data, "SSL_get_shutdown() returned SSL_RECEIVED_SHUTDOWN\n");
1261  break;
1262  case SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN:
1263  infof(data, "SSL_get_shutdown() returned SSL_SENT_SHUTDOWN|"
1264  "SSL_RECEIVED__SHUTDOWN\n");
1265  break;
1266  }
1267 #endif
1268  }
1269 
1270  SSL_free(BACKEND->handle);
1271  BACKEND->handle = NULL;
1272  }
1273  return retval;
1274 }
1275 
1276 static void Curl_ossl_session_free(void *ptr)
1277 {
1278  /* free the ID */
1279  SSL_SESSION_free(ptr);
1280 }
1281 
1282 /*
1283  * This function is called when the 'data' struct is going away. Close
1284  * down everything and free all resources!
1285  */
1286 static void Curl_ossl_close_all(struct Curl_easy *data)
1287 {
1288 #ifdef HAVE_OPENSSL_ENGINE_H
1289  if(data->state.engine) {
1290  ENGINE_finish(data->state.engine);
1291  ENGINE_free(data->state.engine);
1292  data->state.engine = NULL;
1293  }
1294 #else
1295  (void)data;
1296 #endif
1297 #if !defined(HAVE_ERR_REMOVE_THREAD_STATE_DEPRECATED) && \
1298  defined(HAVE_ERR_REMOVE_THREAD_STATE)
1299  /* OpenSSL 1.0.1 and 1.0.2 build an error queue that is stored per-thread
1300  so we need to clean it here in case the thread will be killed. All OpenSSL
1301  code should extract the error in association with the error so clearing
1302  this queue here should be harmless at worst. */
1303  ERR_remove_thread_state(NULL);
1304 #endif
1305 }
1306 
1307 /* ====================================================== */
1308 
1309 
1310 /* Quote from RFC2818 section 3.1 "Server Identity"
1311 
1312  If a subjectAltName extension of type dNSName is present, that MUST
1313  be used as the identity. Otherwise, the (most specific) Common Name
1314  field in the Subject field of the certificate MUST be used. Although
1315  the use of the Common Name is existing practice, it is deprecated and
1316  Certification Authorities are encouraged to use the dNSName instead.
1317 
1318  Matching is performed using the matching rules specified by
1319  [RFC2459]. If more than one identity of a given type is present in
1320  the certificate (e.g., more than one dNSName name, a match in any one
1321  of the set is considered acceptable.) Names may contain the wildcard
1322  character * which is considered to match any single domain name
1323  component or component fragment. E.g., *.a.com matches foo.a.com but
1324  not bar.foo.a.com. f*.com matches foo.com but not bar.com.
1325 
1326  In some cases, the URI is specified as an IP address rather than a
1327  hostname. In this case, the iPAddress subjectAltName must be present
1328  in the certificate and must exactly match the IP in the URI.
1329 
1330 */
1331 static CURLcode verifyhost(struct connectdata *conn, X509 *server_cert)
1332 {
1333  bool matched = FALSE;
1334  int target = GEN_DNS; /* target type, GEN_DNS or GEN_IPADD */
1335  size_t addrlen = 0;
1336  struct Curl_easy *data = conn->data;
1337  STACK_OF(GENERAL_NAME) *altnames;
1338 #ifdef ENABLE_IPV6
1339  struct in6_addr addr;
1340 #else
1341  struct in_addr addr;
1342 #endif
1344  bool dNSName = FALSE; /* if a dNSName field exists in the cert */
1345  bool iPAddress = FALSE; /* if a iPAddress field exists in the cert */
1346  const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
1347  conn->host.name;
1348  const char * const dispname = SSL_IS_PROXY() ?
1349  conn->http_proxy.host.dispname : conn->host.dispname;
1350 
1351 #ifdef ENABLE_IPV6
1352  if(conn->bits.ipv6_ip &&
1353  Curl_inet_pton(AF_INET6, hostname, &addr)) {
1354  target = GEN_IPADD;
1355  addrlen = sizeof(struct in6_addr);
1356  }
1357  else
1358 #endif
1359  if(Curl_inet_pton(AF_INET, hostname, &addr)) {
1360  target = GEN_IPADD;
1361  addrlen = sizeof(struct in_addr);
1362  }
1363 
1364  /* get a "list" of alternative names */
1365  altnames = X509_get_ext_d2i(server_cert, NID_subject_alt_name, NULL, NULL);
1366 
1367  if(altnames) {
1368  int numalts;
1369  int i;
1370  bool dnsmatched = FALSE;
1371  bool ipmatched = FALSE;
1372 
1373  /* get amount of alternatives, RFC2459 claims there MUST be at least
1374  one, but we don't depend on it... */
1375  numalts = sk_GENERAL_NAME_num(altnames);
1376 
1377  /* loop through all alternatives - until a dnsmatch */
1378  for(i = 0; (i < numalts) && !dnsmatched; i++) {
1379  /* get a handle to alternative name number i */
1380  const GENERAL_NAME *check = sk_GENERAL_NAME_value(altnames, i);
1381 
1382  if(check->type == GEN_DNS)
1383  dNSName = TRUE;
1384  else if(check->type == GEN_IPADD)
1385  iPAddress = TRUE;
1386 
1387  /* only check alternatives of the same type the target is */
1388  if(check->type == target) {
1389  /* get data and length */
1390  const char *altptr = (char *)ASN1_STRING_get0_data(check->d.ia5);
1391  size_t altlen = (size_t) ASN1_STRING_length(check->d.ia5);
1392 
1393  switch(target) {
1394  case GEN_DNS: /* name/pattern comparison */
1395  /* The OpenSSL man page explicitly says: "In general it cannot be
1396  assumed that the data returned by ASN1_STRING_data() is null
1397  terminated or does not contain embedded nulls." But also that
1398  "The actual format of the data will depend on the actual string
1399  type itself: for example for and IA5String the data will be ASCII"
1400 
1401  Gisle researched the OpenSSL sources:
1402  "I checked the 0.9.6 and 0.9.8 sources before my patch and
1403  it always 0-terminates an IA5String."
1404  */
1405  if((altlen == strlen(altptr)) &&
1406  /* if this isn't true, there was an embedded zero in the name
1407  string and we cannot match it. */
1408  Curl_cert_hostcheck(altptr, hostname)) {
1409  dnsmatched = TRUE;
1410  infof(data,
1411  " subjectAltName: host \"%s\" matched cert's \"%s\"\n",
1412  dispname, altptr);
1413  }
1414  break;
1415 
1416  case GEN_IPADD: /* IP address comparison */
1417  /* compare alternative IP address if the data chunk is the same size
1418  our server IP address is */
1419  if((altlen == addrlen) && !memcmp(altptr, &addr, altlen)) {
1420  ipmatched = TRUE;
1421  infof(data,
1422  " subjectAltName: host \"%s\" matched cert's IP address!\n",
1423  dispname);
1424  }
1425  break;
1426  }
1427  }
1428  }
1429  GENERAL_NAMES_free(altnames);
1430 
1431  if(dnsmatched || ipmatched)
1432  matched = TRUE;
1433  }
1434 
1435  if(matched)
1436  /* an alternative name matched */
1437  ;
1438  else if(dNSName || iPAddress) {
1439  infof(data, " subjectAltName does not match %s\n", dispname);
1440  failf(data, "SSL: no alternative certificate subject name matches "
1441  "target host name '%s'", dispname);
1443  }
1444  else {
1445  /* we have to look to the last occurrence of a commonName in the
1446  distinguished one to get the most significant one. */
1447  int j, i = -1;
1448 
1449  /* The following is done because of a bug in 0.9.6b */
1450 
1451  unsigned char *nulstr = (unsigned char *)"";
1452  unsigned char *peer_CN = nulstr;
1453 
1454  X509_NAME *name = X509_get_subject_name(server_cert);
1455  if(name)
1456  while((j = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0)
1457  i = j;
1458 
1459  /* we have the name entry and we will now convert this to a string
1460  that we can use for comparison. Doing this we support BMPstring,
1461  UTF8 etc. */
1462 
1463  if(i >= 0) {
1464  ASN1_STRING *tmp =
1466 
1467  /* In OpenSSL 0.9.7d and earlier, ASN1_STRING_to_UTF8 fails if the input
1468  is already UTF-8 encoded. We check for this case and copy the raw
1469  string manually to avoid the problem. This code can be made
1470  conditional in the future when OpenSSL has been fixed. Work-around
1471  brought by Alexis S. L. Carvalho. */
1472  if(tmp) {
1473  if(ASN1_STRING_type(tmp) == V_ASN1_UTF8STRING) {
1474  j = ASN1_STRING_length(tmp);
1475  if(j >= 0) {
1476  peer_CN = OPENSSL_malloc(j + 1);
1477  if(peer_CN) {
1478  memcpy(peer_CN, ASN1_STRING_get0_data(tmp), j);
1479  peer_CN[j] = '\0';
1480  }
1481  }
1482  }
1483  else /* not a UTF8 name */
1484  j = ASN1_STRING_to_UTF8(&peer_CN, tmp);
1485 
1486  if(peer_CN && (curlx_uztosi(strlen((char *)peer_CN)) != j)) {
1487  /* there was a terminating zero before the end of string, this
1488  cannot match and we return failure! */
1489  failf(data, "SSL: illegal cert name field");
1491  }
1492  }
1493  }
1494 
1495  if(peer_CN == nulstr)
1496  peer_CN = NULL;
1497  else {
1498  /* convert peer_CN from UTF8 */
1499  CURLcode rc = Curl_convert_from_utf8(data, (char *)peer_CN,
1500  strlen((char *)peer_CN));
1501  /* Curl_convert_from_utf8 calls failf if unsuccessful */
1502  if(rc) {
1503  OPENSSL_free(peer_CN);
1504  return rc;
1505  }
1506  }
1507 
1508  if(result)
1509  /* error already detected, pass through */
1510  ;
1511  else if(!peer_CN) {
1512  failf(data,
1513  "SSL: unable to obtain common name from peer certificate");
1515  }
1516  else if(!Curl_cert_hostcheck((const char *)peer_CN, hostname)) {
1517  failf(data, "SSL: certificate subject name '%s' does not match "
1518  "target host name '%s'", peer_CN, dispname);
1520  }
1521  else {
1522  infof(data, " common name: %s (matched)\n", peer_CN);
1523  }
1524  if(peer_CN)
1525  OPENSSL_free(peer_CN);
1526  }
1527 
1528  return result;
1529 }
1530 
1531 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \
1532  !defined(OPENSSL_NO_OCSP)
1533 static CURLcode verifystatus(struct connectdata *conn,
1534  struct ssl_connect_data *connssl)
1535 {
1536  int i, ocsp_status;
1537  const unsigned char *p;
1538  CURLcode result = CURLE_OK;
1539  struct Curl_easy *data = conn->data;
1540 
1541  OCSP_RESPONSE *rsp = NULL;
1542  OCSP_BASICRESP *br = NULL;
1543  X509_STORE *st = NULL;
1544  STACK_OF(X509) *ch = NULL;
1545 
1546  long len = SSL_get_tlsext_status_ocsp_resp(BACKEND->handle, &p);
1547 
1548  if(!p) {
1549  failf(data, "No OCSP response received");
1550  result = CURLE_SSL_INVALIDCERTSTATUS;
1551  goto end;
1552  }
1553 
1554  rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
1555  if(!rsp) {
1556  failf(data, "Invalid OCSP response");
1557  result = CURLE_SSL_INVALIDCERTSTATUS;
1558  goto end;
1559  }
1560 
1561  ocsp_status = OCSP_response_status(rsp);
1562  if(ocsp_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
1563  failf(data, "Invalid OCSP response status: %s (%d)",
1564  OCSP_response_status_str(ocsp_status), ocsp_status);
1565  result = CURLE_SSL_INVALIDCERTSTATUS;
1566  goto end;
1567  }
1568 
1569  br = OCSP_response_get1_basic(rsp);
1570  if(!br) {
1571  failf(data, "Invalid OCSP response");
1572  result = CURLE_SSL_INVALIDCERTSTATUS;
1573  goto end;
1574  }
1575 
1576  ch = SSL_get_peer_cert_chain(BACKEND->handle);
1577  st = SSL_CTX_get_cert_store(BACKEND->ctx);
1578 
1579 #if ((OPENSSL_VERSION_NUMBER <= 0x1000201fL) /* Fixed after 1.0.2a */ || \
1580  (defined(LIBRESSL_VERSION_NUMBER) && \
1581  LIBRESSL_VERSION_NUMBER <= 0x2040200fL))
1582  /* The authorized responder cert in the OCSP response MUST be signed by the
1583  peer cert's issuer (see RFC6960 section 4.2.2.2). If that's a root cert,
1584  no problem, but if it's an intermediate cert OpenSSL has a bug where it
1585  expects this issuer to be present in the chain embedded in the OCSP
1586  response. So we add it if necessary. */
1587 
1588  /* First make sure the peer cert chain includes both a peer and an issuer,
1589  and the OCSP response contains a responder cert. */
1590  if(sk_X509_num(ch) >= 2 && sk_X509_num(br->certs) >= 1) {
1591  X509 *responder = sk_X509_value(br->certs, sk_X509_num(br->certs) - 1);
1592 
1593  /* Find issuer of responder cert and add it to the OCSP response chain */
1594  for(i = 0; i < sk_X509_num(ch); i++) {
1595  X509 *issuer = sk_X509_value(ch, i);
1596  if(X509_check_issued(issuer, responder) == X509_V_OK) {
1597  if(!OCSP_basic_add1_cert(br, issuer)) {
1598  failf(data, "Could not add issuer cert to OCSP response");
1599  result = CURLE_SSL_INVALIDCERTSTATUS;
1600  goto end;
1601  }
1602  }
1603  }
1604  }
1605 #endif
1606 
1607  if(OCSP_basic_verify(br, ch, st, 0) <= 0) {
1608  failf(data, "OCSP response verification failed");
1609  result = CURLE_SSL_INVALIDCERTSTATUS;
1610  goto end;
1611  }
1612 
1613  for(i = 0; i < OCSP_resp_count(br); i++) {
1614  int cert_status, crl_reason;
1615  OCSP_SINGLERESP *single = NULL;
1616 
1617  ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
1618 
1619  single = OCSP_resp_get0(br, i);
1620  if(!single)
1621  continue;
1622 
1623  cert_status = OCSP_single_get0_status(single, &crl_reason, &rev,
1624  &thisupd, &nextupd);
1625 
1626  if(!OCSP_check_validity(thisupd, nextupd, 300L, -1L)) {
1627  failf(data, "OCSP response has expired");
1628  result = CURLE_SSL_INVALIDCERTSTATUS;
1629  goto end;
1630  }
1631 
1632  infof(data, "SSL certificate status: %s (%d)\n",
1633  OCSP_cert_status_str(cert_status), cert_status);
1634 
1635  switch(cert_status) {
1636  case V_OCSP_CERTSTATUS_GOOD:
1637  break;
1638 
1639  case V_OCSP_CERTSTATUS_REVOKED:
1640  result = CURLE_SSL_INVALIDCERTSTATUS;
1641 
1642  failf(data, "SSL certificate revocation reason: %s (%d)",
1643  OCSP_crl_reason_str(crl_reason), crl_reason);
1644  goto end;
1645 
1646  case V_OCSP_CERTSTATUS_UNKNOWN:
1647  result = CURLE_SSL_INVALIDCERTSTATUS;
1648  goto end;
1649  }
1650  }
1651 
1652 end:
1653  if(br) OCSP_BASICRESP_free(br);
1654  OCSP_RESPONSE_free(rsp);
1655 
1656  return result;
1657 }
1658 #endif
1659 
1660 #endif /* USE_OPENSSL */
1661 
1662 /* The SSL_CTRL_SET_MSG_CALLBACK doesn't exist in ancient OpenSSL versions
1663  and thus this cannot be done there. */
1664 #ifdef SSL_CTRL_SET_MSG_CALLBACK
1665 
1666 static const char *ssl_msg_type(int ssl_ver, int msg)
1667 {
1668 #ifdef SSL2_VERSION_MAJOR
1669  if(ssl_ver == SSL2_VERSION_MAJOR) {
1670  switch(msg) {
1671  case SSL2_MT_ERROR:
1672  return "Error";
1673  case SSL2_MT_CLIENT_HELLO:
1674  return "Client hello";
1675  case SSL2_MT_CLIENT_MASTER_KEY:
1676  return "Client key";
1677  case SSL2_MT_CLIENT_FINISHED:
1678  return "Client finished";
1679  case SSL2_MT_SERVER_HELLO:
1680  return "Server hello";
1681  case SSL2_MT_SERVER_VERIFY:
1682  return "Server verify";
1683  case SSL2_MT_SERVER_FINISHED:
1684  return "Server finished";
1685  case SSL2_MT_REQUEST_CERTIFICATE:
1686  return "Request CERT";
1687  case SSL2_MT_CLIENT_CERTIFICATE:
1688  return "Client CERT";
1689  }
1690  }
1691  else
1692 #endif
1693  if(ssl_ver == SSL3_VERSION_MAJOR) {
1694  switch(msg) {
1695  case SSL3_MT_HELLO_REQUEST:
1696  return "Hello request";
1697  case SSL3_MT_CLIENT_HELLO:
1698  return "Client hello";
1699  case SSL3_MT_SERVER_HELLO:
1700  return "Server hello";
1701 #ifdef SSL3_MT_NEWSESSION_TICKET
1702  case SSL3_MT_NEWSESSION_TICKET:
1703  return "Newsession Ticket";
1704 #endif
1705  case SSL3_MT_CERTIFICATE:
1706  return "Certificate";
1707  case SSL3_MT_SERVER_KEY_EXCHANGE:
1708  return "Server key exchange";
1709  case SSL3_MT_CLIENT_KEY_EXCHANGE:
1710  return "Client key exchange";
1711  case SSL3_MT_CERTIFICATE_REQUEST:
1712  return "Request CERT";
1713  case SSL3_MT_SERVER_DONE:
1714  return "Server finished";
1715  case SSL3_MT_CERTIFICATE_VERIFY:
1716  return "CERT verify";
1717  case SSL3_MT_FINISHED:
1718  return "Finished";
1719 #ifdef SSL3_MT_CERTIFICATE_STATUS
1720  case SSL3_MT_CERTIFICATE_STATUS:
1721  return "Certificate Status";
1722 #endif
1723  }
1724  }
1725  return "Unknown";
1726 }
1727 
1728 static const char *tls_rt_type(int type)
1729 {
1730  switch(type) {
1731 #ifdef SSL3_RT_HEADER
1732  case SSL3_RT_HEADER:
1733  return "TLS header";
1734 #endif
1735  case SSL3_RT_CHANGE_CIPHER_SPEC:
1736  return "TLS change cipher";
1737  case SSL3_RT_ALERT:
1738  return "TLS alert";
1739  case SSL3_RT_HANDSHAKE:
1740  return "TLS handshake";
1741  case SSL3_RT_APPLICATION_DATA:
1742  return "TLS app data";
1743  default:
1744  return "TLS Unknown";
1745  }
1746 }
1747 
1748 
1749 /*
1750  * Our callback from the SSL/TLS layers.
1751  */
1752 static void ssl_tls_trace(int direction, int ssl_ver, int content_type,
1753  const void *buf, size_t len, SSL *ssl,
1754  void *userp)
1755 {
1756  struct Curl_easy *data;
1757  const char *msg_name, *tls_rt_name;
1758  char ssl_buf[1024];
1759  char unknown[32];
1760  int msg_type, txt_len;
1761  const char *verstr = NULL;
1762  struct connectdata *conn = userp;
1763 
1764  if(!conn || !conn->data || !conn->data->set.fdebug ||
1765  (direction != 0 && direction != 1))
1766  return;
1767 
1768  data = conn->data;
1769 
1770  switch(ssl_ver) {
1771 #ifdef SSL2_VERSION /* removed in recent versions */
1772  case SSL2_VERSION:
1773  verstr = "SSLv2";
1774  break;
1775 #endif
1776 #ifdef SSL3_VERSION
1777  case SSL3_VERSION:
1778  verstr = "SSLv3";
1779  break;
1780 #endif
1781  case TLS1_VERSION:
1782  verstr = "TLSv1.0";
1783  break;
1784 #ifdef TLS1_1_VERSION
1785  case TLS1_1_VERSION:
1786  verstr = "TLSv1.1";
1787  break;
1788 #endif
1789 #ifdef TLS1_2_VERSION
1790  case TLS1_2_VERSION:
1791  verstr = "TLSv1.2";
1792  break;
1793 #endif
1794 #ifdef TLS1_3_VERSION
1795  case TLS1_3_VERSION:
1796  verstr = "TLSv1.3";
1797  break;
1798 #endif
1799  case 0:
1800  break;
1801  default:
1802  snprintf(unknown, sizeof(unknown), "(%x)", ssl_ver);
1803  verstr = unknown;
1804  break;
1805  }
1806 
1807  if(ssl_ver) {
1808  /* the info given when the version is zero is not that useful for us */
1809 
1810  ssl_ver >>= 8; /* check the upper 8 bits only below */
1811 
1812  /* SSLv2 doesn't seem to have TLS record-type headers, so OpenSSL
1813  * always pass-up content-type as 0. But the interesting message-type
1814  * is at 'buf[0]'.
1815  */
1816  if(ssl_ver == SSL3_VERSION_MAJOR && content_type)
1817  tls_rt_name = tls_rt_type(content_type);
1818  else
1819  tls_rt_name = "";
1820 
1821  msg_type = *(char *)buf;
1822  msg_name = ssl_msg_type(ssl_ver, msg_type);
1823 
1824  txt_len = snprintf(ssl_buf, sizeof(ssl_buf), "%s (%s), %s, %s (%d):\n",
1825  verstr, direction?"OUT":"IN",
1826  tls_rt_name, msg_name, msg_type);
1827  Curl_debug(data, CURLINFO_TEXT, ssl_buf, (size_t)txt_len, NULL);
1828  }
1829 
1830  Curl_debug(data, (direction == 1) ? CURLINFO_SSL_DATA_OUT :
1831  CURLINFO_SSL_DATA_IN, (char *)buf, len, NULL);
1832  (void) ssl;
1833 }
1834 #endif
1835 
1836 #ifdef USE_OPENSSL
1837 /* ====================================================== */
1838 
1839 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1840 # define use_sni(x) sni = (x)
1841 #else
1842 # define use_sni(x) Curl_nop_stmt
1843 #endif
1844 
1845 /* Check for OpenSSL 1.0.2 which has ALPN support. */
1846 #undef HAS_ALPN
1847 #if OPENSSL_VERSION_NUMBER >= 0x10002000L \
1848  && !defined(OPENSSL_NO_TLSEXT)
1849 # define HAS_ALPN 1
1850 #endif
1851 
1852 /* Check for OpenSSL 1.0.1 which has NPN support. */
1853 #undef HAS_NPN
1854 #if OPENSSL_VERSION_NUMBER >= 0x10001000L \
1855  && !defined(OPENSSL_NO_TLSEXT) \
1856  && !defined(OPENSSL_NO_NEXTPROTONEG)
1857 # define HAS_NPN 1
1858 #endif
1859 
1860 #ifdef HAS_NPN
1861 
1862 /*
1863  * in is a list of length prefixed strings. this function has to select
1864  * the protocol we want to use from the list and write its string into out.
1865  */
1866 
1867 static int
1868 select_next_protocol(unsigned char **out, unsigned char *outlen,
1869  const unsigned char *in, unsigned int inlen,
1870  const char *key, unsigned int keylen)
1871 {
1872  unsigned int i;
1873  for(i = 0; i + keylen <= inlen; i += in[i] + 1) {
1874  if(memcmp(&in[i + 1], key, keylen) == 0) {
1875  *out = (unsigned char *) &in[i + 1];
1876  *outlen = in[i];
1877  return 0;
1878  }
1879  }
1880  return -1;
1881 }
1882 
1883 static int
1884 select_next_proto_cb(SSL *ssl,
1885  unsigned char **out, unsigned char *outlen,
1886  const unsigned char *in, unsigned int inlen,
1887  void *arg)
1888 {
1889  struct connectdata *conn = (struct connectdata*) arg;
1890 
1891  (void)ssl;
1892 
1893 #ifdef USE_NGHTTP2
1894  if(conn->data->set.httpversion >= CURL_HTTP_VERSION_2 &&
1895  !select_next_protocol(out, outlen, in, inlen, NGHTTP2_PROTO_VERSION_ID,
1896  NGHTTP2_PROTO_VERSION_ID_LEN)) {
1897  infof(conn->data, "NPN, negotiated HTTP2 (%s)\n",
1898  NGHTTP2_PROTO_VERSION_ID);
1899  conn->negnpn = CURL_HTTP_VERSION_2;
1900  return SSL_TLSEXT_ERR_OK;
1901  }
1902 #endif
1903 
1904  if(!select_next_protocol(out, outlen, in, inlen, ALPN_HTTP_1_1,
1906  infof(conn->data, "NPN, negotiated HTTP1.1\n");
1907  conn->negnpn = CURL_HTTP_VERSION_1_1;
1908  return SSL_TLSEXT_ERR_OK;
1909  }
1910 
1911  infof(conn->data, "NPN, no overlap, use HTTP1.1\n");
1912  *out = (unsigned char *)ALPN_HTTP_1_1;
1913  *outlen = ALPN_HTTP_1_1_LENGTH;
1914  conn->negnpn = CURL_HTTP_VERSION_1_1;
1915 
1916  return SSL_TLSEXT_ERR_OK;
1917 }
1918 #endif /* HAS_NPN */
1919 
1920 static const char *
1921 get_ssl_version_txt(SSL *ssl)
1922 {
1923  if(!ssl)
1924  return "";
1925 
1926  switch(SSL_version(ssl)) {
1927 #ifdef TLS1_3_VERSION
1928  case TLS1_3_VERSION:
1929  return "TLSv1.3";
1930 #endif
1931 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
1932  case TLS1_2_VERSION:
1933  return "TLSv1.2";
1934  case TLS1_1_VERSION:
1935  return "TLSv1.1";
1936 #endif
1937  case TLS1_VERSION:
1938  return "TLSv1.0";
1939  case SSL3_VERSION:
1940  return "SSLv3";
1941  case SSL2_VERSION:
1942  return "SSLv2";
1943  }
1944  return "unknown";
1945 }
1946 
1947 static CURLcode
1948 set_ssl_version_min_max(long *ctx_options, struct connectdata *conn,
1949  int sockindex)
1950 {
1951 #if (OPENSSL_VERSION_NUMBER < 0x1000100FL) || !defined(TLS1_3_VERSION)
1952  /* convoluted #if condition just to avoid compiler warnings on unused
1953  variable */
1954  struct Curl_easy *data = conn->data;
1955 #endif
1956  long ssl_version = SSL_CONN_CONFIG(version);
1957  long ssl_version_max = SSL_CONN_CONFIG(version_max);
1958 
1959  if(ssl_version_max == CURL_SSLVERSION_MAX_NONE) {
1960  ssl_version_max = ssl_version << 16;
1961  }
1962 
1963  switch(ssl_version) {
1965 #ifdef TLS1_3_VERSION
1966  {
1967  struct ssl_connect_data *connssl = &conn->ssl[sockindex];
1968  SSL_CTX_set_max_proto_version(BACKEND->ctx, TLS1_3_VERSION);
1969  *ctx_options |= SSL_OP_NO_TLSv1_2;
1970  }
1971 #else
1972  (void)sockindex;
1973  failf(data, OSSL_PACKAGE " was built without TLS 1.3 support");
1974  return CURLE_NOT_BUILT_IN;
1975 #endif
1976  /* FALLTHROUGH */
1978 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
1979  *ctx_options |= SSL_OP_NO_TLSv1_1;
1980 #else
1981  failf(data, OSSL_PACKAGE " was built without TLS 1.2 support");
1982  return CURLE_NOT_BUILT_IN;
1983 #endif
1984  /* FALLTHROUGH */
1986 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
1987  *ctx_options |= SSL_OP_NO_TLSv1;
1988 #else
1989  failf(data, OSSL_PACKAGE " was built without TLS 1.1 support");
1990  return CURLE_NOT_BUILT_IN;
1991 #endif
1992  /* FALLTHROUGH */
1994  *ctx_options |= SSL_OP_NO_SSLv2;
1995  *ctx_options |= SSL_OP_NO_SSLv3;
1996  break;
1997  }
1998 
1999  switch(ssl_version_max) {
2001 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
2002  *ctx_options |= SSL_OP_NO_TLSv1_1;
2003 #endif
2004  /* FALLTHROUGH */
2006 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
2007  *ctx_options |= SSL_OP_NO_TLSv1_2;
2008 #endif
2009  /* FALLTHROUGH */
2012 #ifdef TLS1_3_VERSION
2013  *ctx_options |= SSL_OP_NO_TLSv1_3;
2014 #endif
2015  break;
2017 #ifdef TLS1_3_VERSION
2018  break;
2019 #else
2020  failf(data, OSSL_PACKAGE " was built without TLS 1.3 support");
2021  return CURLE_NOT_BUILT_IN;
2022 #endif
2023  }
2024  return CURLE_OK;
2025 }
2026 
2027 static CURLcode ossl_connect_step1(struct connectdata *conn, int sockindex)
2028 {
2029  CURLcode result = CURLE_OK;
2030  char *ciphers;
2031  struct Curl_easy *data = conn->data;
2032  SSL_METHOD_QUAL SSL_METHOD *req_method = NULL;
2033  X509_LOOKUP *lookup = NULL;
2034  curl_socket_t sockfd = conn->sock[sockindex];
2035  struct ssl_connect_data *connssl = &conn->ssl[sockindex];
2036  long ctx_options = 0;
2037 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2038  bool sni;
2039  const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
2040  conn->host.name;
2041 #ifdef ENABLE_IPV6
2042  struct in6_addr addr;
2043 #else
2044  struct in_addr addr;
2045 #endif
2046 #endif
2047  long * const certverifyresult = SSL_IS_PROXY() ?
2049  const long int ssl_version = SSL_CONN_CONFIG(version);
2050 #ifdef USE_TLS_SRP
2051  const enum CURL_TLSAUTH ssl_authtype = SSL_SET_OPTION(authtype);
2052 #endif
2053  char * const ssl_cert = SSL_SET_OPTION(cert);
2054  const char * const ssl_cert_type = SSL_SET_OPTION(cert_type);
2055  const char * const ssl_cafile = SSL_CONN_CONFIG(CAfile);
2056  const char * const ssl_capath = SSL_CONN_CONFIG(CApath);
2057  const bool verifypeer = SSL_CONN_CONFIG(verifypeer);
2058  const char * const ssl_crlfile = SSL_SET_OPTION(CRLfile);
2059  char error_buffer[256];
2060 
2062 
2063  /* Make funny stuff to get random input */
2064  result = Curl_ossl_seed(data);
2065  if(result)
2066  return result;
2067 
2068  *certverifyresult = !X509_V_OK;
2069 
2070  /* check to see if we've been told to use an explicit SSL/TLS version */
2071 
2072  switch(ssl_version) {
2074  case CURL_SSLVERSION_TLSv1:
2079  /* it will be handled later with the context options */
2080 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && \
2081  !defined(LIBRESSL_VERSION_NUMBER)
2082  req_method = TLS_client_method();
2083 #else
2084  req_method = SSLv23_client_method();
2085 #endif
2086  use_sni(TRUE);
2087  break;
2088  case CURL_SSLVERSION_SSLv2:
2089 #ifdef OPENSSL_NO_SSL2
2090  failf(data, OSSL_PACKAGE " was built without SSLv2 support");
2091  return CURLE_NOT_BUILT_IN;
2092 #else
2093 #ifdef USE_TLS_SRP
2094  if(ssl_authtype == CURL_TLSAUTH_SRP)
2095  return CURLE_SSL_CONNECT_ERROR;
2096 #endif
2097  req_method = SSLv2_client_method();
2098  use_sni(FALSE);
2099  break;
2100 #endif
2101  case CURL_SSLVERSION_SSLv3:
2102 #ifdef OPENSSL_NO_SSL3_METHOD
2103  failf(data, OSSL_PACKAGE " was built without SSLv3 support");
2104  return CURLE_NOT_BUILT_IN;
2105 #else
2106 #ifdef USE_TLS_SRP
2107  if(ssl_authtype == CURL_TLSAUTH_SRP)
2108  return CURLE_SSL_CONNECT_ERROR;
2109 #endif
2110  req_method = SSLv3_client_method();
2111  use_sni(FALSE);
2112  break;
2113 #endif
2114  default:
2115  failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
2116  return CURLE_SSL_CONNECT_ERROR;
2117  }
2118 
2119  if(BACKEND->ctx)
2120  SSL_CTX_free(BACKEND->ctx);
2121  BACKEND->ctx = SSL_CTX_new(req_method);
2122 
2123  if(!BACKEND->ctx) {
2124  failf(data, "SSL: couldn't create a context: %s",
2125  ossl_strerror(ERR_peek_error(), error_buffer, sizeof(error_buffer)));
2126  return CURLE_OUT_OF_MEMORY;
2127  }
2128 
2129 #ifdef SSL_MODE_RELEASE_BUFFERS
2130  SSL_CTX_set_mode(BACKEND->ctx, SSL_MODE_RELEASE_BUFFERS);
2131 #endif
2132 
2133 #ifdef SSL_CTRL_SET_MSG_CALLBACK
2134  if(data->set.fdebug && data->set.verbose) {
2135  /* the SSL trace callback is only used for verbose logging */
2136  SSL_CTX_set_msg_callback(BACKEND->ctx, ssl_tls_trace);
2137  SSL_CTX_set_msg_callback_arg(BACKEND->ctx, conn);
2138  }
2139 #endif
2140 
2141  /* OpenSSL contains code to work-around lots of bugs and flaws in various
2142  SSL-implementations. SSL_CTX_set_options() is used to enabled those
2143  work-arounds. The man page for this option states that SSL_OP_ALL enables
2144  all the work-arounds and that "It is usually safe to use SSL_OP_ALL to
2145  enable the bug workaround options if compatibility with somewhat broken
2146  implementations is desired."
2147 
2148  The "-no_ticket" option was introduced in Openssl0.9.8j. It's a flag to
2149  disable "rfc4507bis session ticket support". rfc4507bis was later turned
2150  into the proper RFC5077 it seems: https://tools.ietf.org/html/rfc5077
2151 
2152  The enabled extension concerns the session management. I wonder how often
2153  libcurl stops a connection and then resumes a TLS session. also, sending
2154  the session data is some overhead. .I suggest that you just use your
2155  proposed patch (which explicitly disables TICKET).
2156 
2157  If someone writes an application with libcurl and openssl who wants to
2158  enable the feature, one can do this in the SSL callback.
2159 
2160  SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG option enabling allowed proper
2161  interoperability with web server Netscape Enterprise Server 2.0.1 which
2162  was released back in 1996.
2163 
2164  Due to CVE-2010-4180, option SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG has
2165  become ineffective as of OpenSSL 0.9.8q and 1.0.0c. In order to mitigate
2166  CVE-2010-4180 when using previous OpenSSL versions we no longer enable
2167  this option regardless of OpenSSL version and SSL_OP_ALL definition.
2168 
2169  OpenSSL added a work-around for a SSL 3.0/TLS 1.0 CBC vulnerability
2170  (https://www.openssl.org/~bodo/tls-cbc.txt). In 0.9.6e they added a bit to
2171  SSL_OP_ALL that _disables_ that work-around despite the fact that
2172  SSL_OP_ALL is documented to do "rather harmless" workarounds. In order to
2173  keep the secure work-around, the SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS bit
2174  must not be set.
2175  */
2176 
2177  ctx_options = SSL_OP_ALL;
2178 
2179 #ifdef SSL_OP_NO_TICKET
2180  ctx_options |= SSL_OP_NO_TICKET;
2181 #endif
2182 
2183 #ifdef SSL_OP_NO_COMPRESSION
2184  ctx_options |= SSL_OP_NO_COMPRESSION;
2185 #endif
2186 
2187 #ifdef SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
2188  /* mitigate CVE-2010-4180 */
2189  ctx_options &= ~SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG;
2190 #endif
2191 
2192 #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
2193  /* unless the user explicitly ask to allow the protocol vulnerability we
2194  use the work-around */
2195  if(!SSL_SET_OPTION(enable_beast))
2196  ctx_options &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
2197 #endif
2198 
2199  switch(ssl_version) {
2200  case CURL_SSLVERSION_SSLv3:
2201 #ifdef USE_TLS_SRP
2202  if(ssl_authtype == CURL_TLSAUTH_SRP) {
2203  infof(data, "Set version TLSv1.x for SRP authorisation\n");
2204  }
2205 #endif
2206  ctx_options |= SSL_OP_NO_SSLv2;
2207  ctx_options |= SSL_OP_NO_TLSv1;
2208 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
2209  ctx_options |= SSL_OP_NO_TLSv1_1;
2210  ctx_options |= SSL_OP_NO_TLSv1_2;
2211 #ifdef TLS1_3_VERSION
2212  ctx_options |= SSL_OP_NO_TLSv1_3;
2213 #endif
2214 #endif
2215  break;
2216 
2218  case CURL_SSLVERSION_TLSv1:
2219  ctx_options |= SSL_OP_NO_SSLv2;
2220  ctx_options |= SSL_OP_NO_SSLv3;
2221  break;
2222 
2227  result = set_ssl_version_min_max(&ctx_options, conn, sockindex);
2228  if(result != CURLE_OK)
2229  return result;
2230  break;
2231 
2232  case CURL_SSLVERSION_SSLv2:
2233 #ifndef OPENSSL_NO_SSL2
2234  ctx_options |= SSL_OP_NO_SSLv3;
2235  ctx_options |= SSL_OP_NO_TLSv1;
2236 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL
2237  ctx_options |= SSL_OP_NO_TLSv1_1;
2238  ctx_options |= SSL_OP_NO_TLSv1_2;
2239 #ifdef TLS1_3_VERSION
2240  ctx_options |= SSL_OP_NO_TLSv1_3;
2241 #endif
2242 #endif
2243  break;
2244 #else
2245  failf(data, OSSL_PACKAGE " was built without SSLv2 support");
2246  return CURLE_NOT_BUILT_IN;
2247 #endif
2248 
2249  default:
2250  failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
2251  return CURLE_SSL_CONNECT_ERROR;
2252  }
2253 
2254  SSL_CTX_set_options(BACKEND->ctx, ctx_options);
2255 
2256 #ifdef HAS_NPN
2257  if(conn->bits.tls_enable_npn)
2258  SSL_CTX_set_next_proto_select_cb(BACKEND->ctx, select_next_proto_cb, conn);
2259 #endif
2260 
2261 #ifdef HAS_ALPN
2262  if(conn->bits.tls_enable_alpn) {
2263  int cur = 0;
2264  unsigned char protocols[128];
2265 
2266 #ifdef USE_NGHTTP2
2267  if(data->set.httpversion >= CURL_HTTP_VERSION_2 &&
2268  (!SSL_IS_PROXY() || !conn->bits.tunnel_proxy)) {
2269  protocols[cur++] = NGHTTP2_PROTO_VERSION_ID_LEN;
2270 
2271  memcpy(&protocols[cur], NGHTTP2_PROTO_VERSION_ID,
2272  NGHTTP2_PROTO_VERSION_ID_LEN);
2273  cur += NGHTTP2_PROTO_VERSION_ID_LEN;
2274  infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID);
2275  }
2276 #endif
2277 
2278  protocols[cur++] = ALPN_HTTP_1_1_LENGTH;
2279  memcpy(&protocols[cur], ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH);
2280  cur += ALPN_HTTP_1_1_LENGTH;
2281  infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1);
2282 
2283  /* expects length prefixed preference ordered list of protocols in wire
2284  * format
2285  */
2286  SSL_CTX_set_alpn_protos(BACKEND->ctx, protocols, cur);
2287  }
2288 #endif
2289 
2290  if(ssl_cert || ssl_cert_type) {
2291  if(!cert_stuff(conn, BACKEND->ctx, ssl_cert, ssl_cert_type,
2292  SSL_SET_OPTION(key), SSL_SET_OPTION(key_type),
2293  SSL_SET_OPTION(key_passwd))) {
2294  /* failf() is already done in cert_stuff() */
2295  return CURLE_SSL_CERTPROBLEM;
2296  }
2297  }
2298 
2299  ciphers = SSL_CONN_CONFIG(cipher_list);
2300  if(!ciphers)
2301  ciphers = (char *)DEFAULT_CIPHER_SELECTION;
2302  if(ciphers) {
2303  if(!SSL_CTX_set_cipher_list(BACKEND->ctx, ciphers)) {
2304  failf(data, "failed setting cipher list: %s", ciphers);
2305  return CURLE_SSL_CIPHER;
2306  }
2307  infof(data, "Cipher selection: %s\n", ciphers);
2308  }
2309 
2310 #ifdef USE_TLS_SRP
2311  if(ssl_authtype == CURL_TLSAUTH_SRP) {
2312  char * const ssl_username = SSL_SET_OPTION(username);
2313 
2314  infof(data, "Using TLS-SRP username: %s\n", ssl_username);
2315 
2316  if(!SSL_CTX_set_srp_username(BACKEND->ctx, ssl_username)) {
2317  failf(data, "Unable to set SRP user name");
2319  }
2320  if(!SSL_CTX_set_srp_password(BACKEND->ctx, SSL_SET_OPTION(password))) {
2321  failf(data, "failed setting SRP password");
2323  }
2324  if(!SSL_CONN_CONFIG(cipher_list)) {
2325  infof(data, "Setting cipher list SRP\n");
2326 
2327  if(!SSL_CTX_set_cipher_list(BACKEND->ctx, "SRP")) {
2328  failf(data, "failed setting SRP cipher list");
2329  return CURLE_SSL_CIPHER;
2330  }
2331  }
2332  }
2333 #endif
2334 
2335  if(ssl_cafile || ssl_capath) {
2336  /* tell SSL where to find CA certificates that are used to verify
2337  the servers certificate. */
2338  if(!SSL_CTX_load_verify_locations(BACKEND->ctx, ssl_cafile, ssl_capath)) {
2339  if(verifypeer) {
2340  /* Fail if we insist on successfully verifying the server. */
2341  failf(data, "error setting certificate verify locations:\n"
2342  " CAfile: %s\n CApath: %s",
2343  ssl_cafile ? ssl_cafile : "none",
2344  ssl_capath ? ssl_capath : "none");
2345  return CURLE_SSL_CACERT_BADFILE;
2346  }
2347  /* Just continue with a warning if no strict certificate verification
2348  is required. */
2349  infof(data, "error setting certificate verify locations,"
2350  " continuing anyway:\n");
2351  }
2352  else {
2353  /* Everything is fine. */
2354  infof(data, "successfully set certificate verify locations:\n");
2355  }
2356  infof(data,
2357  " CAfile: %s\n"
2358  " CApath: %s\n",
2359  ssl_cafile ? ssl_cafile : "none",
2360  ssl_capath ? ssl_capath : "none");
2361  }
2362 #ifdef CURL_CA_FALLBACK
2363  else if(verifypeer) {
2364  /* verfying the peer without any CA certificates won't
2365  work so use openssl's built in default as fallback */
2366  SSL_CTX_set_default_verify_paths(BACKEND->ctx);
2367  }
2368 #endif
2369 
2370  if(ssl_crlfile) {
2371  /* tell SSL where to find CRL file that is used to check certificate
2372  * revocation */
2373  lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(BACKEND->ctx),
2374  X509_LOOKUP_file());
2375  if(!lookup ||
2376  (!X509_load_crl_file(lookup, ssl_crlfile, X509_FILETYPE_PEM)) ) {
2377  failf(data, "error loading CRL file: %s", ssl_crlfile);
2378  return CURLE_SSL_CRL_BADFILE;
2379  }
2380  /* Everything is fine. */
2381  infof(data, "successfully load CRL file:\n");
2383  X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
2384 
2385  infof(data, " CRLfile: %s\n", ssl_crlfile);
2386  }
2387 
2388  /* Try building a chain using issuers in the trusted store first to avoid
2389  problems with server-sent legacy intermediates.
2390  Newer versions of OpenSSL do alternate chain checking by default which
2391  gives us the same fix without as much of a performance hit (slight), so we
2392  prefer that if available.
2393  https://rt.openssl.org/Ticket/Display.html?id=3621&user=guest&pass=guest
2394  */
2395 #if defined(X509_V_FLAG_TRUSTED_FIRST) && !defined(X509_V_FLAG_NO_ALT_CHAINS)
2396  if(verifypeer) {
2398  X509_V_FLAG_TRUSTED_FIRST);
2399  }
2400 #endif
2401 
2402  /* SSL always tries to verify the peer, this only says whether it should
2403  * fail to connect if the verification fails, or if it should continue
2404  * anyway. In the latter case the result of the verification is checked with
2405  * SSL_get_verify_result() below. */
2406  SSL_CTX_set_verify(BACKEND->ctx,
2407  verifypeer ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL);
2408 
2409  /* Enable logging of secrets to the file specified in env SSLKEYLOGFILE. */
2410 #if defined(ENABLE_SSLKEYLOGFILE) && defined(HAVE_KEYLOG_CALLBACK)
2411  if(keylog_file) {
2412  SSL_CTX_set_keylog_callback(connssl->ctx, ossl_keylog_callback);
2413  }
2414 #endif
2415 
2416  /* give application a chance to interfere with SSL set up. */
2417  if(data->set.ssl.fsslctx) {
2418  result = (*data->set.ssl.fsslctx)(data, BACKEND->ctx,
2419  data->set.ssl.fsslctxp);
2420  if(result) {
2421  failf(data, "error signaled by ssl ctx callback");
2422  return result;
2423  }
2424  }
2425 
2426  /* Lets make an SSL structure */
2427  if(BACKEND->handle)
2428  SSL_free(BACKEND->handle);
2429  BACKEND->handle = SSL_new(BACKEND->ctx);
2430  if(!BACKEND->handle) {
2431  failf(data, "SSL: couldn't create a context (handle)!");
2432  return CURLE_OUT_OF_MEMORY;
2433  }
2434 
2435 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \
2436  !defined(OPENSSL_NO_OCSP)
2437  if(SSL_CONN_CONFIG(verifystatus))
2438  SSL_set_tlsext_status_type(BACKEND->handle, TLSEXT_STATUSTYPE_ocsp);
2439 #endif
2440 
2441  SSL_set_connect_state(BACKEND->handle);
2442 
2443  BACKEND->server_cert = 0x0;
2444 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2445  if((0 == Curl_inet_pton(AF_INET, hostname, &addr)) &&
2446 #ifdef ENABLE_IPV6
2447  (0 == Curl_inet_pton(AF_INET6, hostname, &addr)) &&
2448 #endif
2449  sni &&
2450  !SSL_set_tlsext_host_name(BACKEND->handle, hostname))
2451  infof(data, "WARNING: failed to configure server name indication (SNI) "
2452  "TLS extension\n");
2453 #endif
2454 
2455  /* Check if there's a cached ID we can/should use here! */
2456  if(SSL_SET_OPTION(primary.sessionid)) {
2457  void *ssl_sessionid = NULL;
2458 
2459  Curl_ssl_sessionid_lock(conn);
2460  if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL, sockindex)) {
2461  /* we got a session id, use it! */
2462  if(!SSL_set_session(BACKEND->handle, ssl_sessionid)) {
2463  Curl_ssl_sessionid_unlock(conn);
2464  failf(data, "SSL: SSL_set_session failed: %s",
2465  ossl_strerror(ERR_get_error(), error_buffer,
2466  sizeof(error_buffer)));
2467  return CURLE_SSL_CONNECT_ERROR;
2468  }
2469  /* Informational message */
2470  infof(data, "SSL re-using session ID\n");
2471  }
2472  Curl_ssl_sessionid_unlock(conn);
2473  }
2474 
2475  if(conn->proxy_ssl[sockindex].use) {
2476  BIO *const bio = BIO_new(BIO_f_ssl());
2477  SSL *handle = conn->proxy_ssl[sockindex].backend->handle;
2478  DEBUGASSERT(ssl_connection_complete == conn->proxy_ssl[sockindex].state);
2479  DEBUGASSERT(handle != NULL);
2480  DEBUGASSERT(bio != NULL);
2481  BIO_set_ssl(bio, handle, FALSE);
2482  SSL_set_bio(BACKEND->handle, bio, bio);
2483  }
2484  else if(!SSL_set_fd(BACKEND->handle, (int)sockfd)) {
2485  /* pass the raw socket into the SSL layers */
2486  failf(data, "SSL: SSL_set_fd failed: %s",
2487  ossl_strerror(ERR_get_error(), error_buffer, sizeof(error_buffer)));
2488  return CURLE_SSL_CONNECT_ERROR;
2489  }
2490 
2491  connssl->connecting_state = ssl_connect_2;
2492 
2493  return CURLE_OK;
2494 }
2495 
2496 static CURLcode ossl_connect_step2(struct connectdata *conn, int sockindex)
2497 {
2498  struct Curl_easy *data = conn->data;
2499  int err;
2500  struct ssl_connect_data *connssl = &conn->ssl[sockindex];
2501  long * const certverifyresult = SSL_IS_PROXY() ?
2505  || ssl_connect_2_writing == connssl->connecting_state);
2506 
2507  ERR_clear_error();
2508 
2509  err = SSL_connect(BACKEND->handle);
2510  /* If keylogging is enabled but the keylog callback is not supported then log
2511  secrets here, immediately after SSL_connect by using tap_ssl_key. */
2512 #if defined(ENABLE_SSLKEYLOGFILE) && !defined(HAVE_KEYLOG_CALLBACK)
2513  tap_ssl_key(BACKEND->handle, &BACKEND->tap_state);
2514 #endif
2515 
2516  /* 1 is fine
2517  0 is "not successful but was shut down controlled"
2518  <0 is "handshake was not successful, because a fatal error occurred" */
2519  if(1 != err) {
2520  int detail = SSL_get_error(BACKEND->handle, err);
2521 
2522  if(SSL_ERROR_WANT_READ == detail) {
2524  return CURLE_OK;
2525  }
2526  if(SSL_ERROR_WANT_WRITE == detail) {
2528  return CURLE_OK;
2529  }
2530  else {
2531  /* untreated error */
2532  unsigned long errdetail;
2533  char error_buffer[256]="";
2534  CURLcode result;
2535  long lerr;
2536  int lib;
2537  int reason;
2538 
2539  /* the connection failed, we're not waiting for anything else. */
2540  connssl->connecting_state = ssl_connect_2;
2541 
2542  /* Get the earliest error code from the thread's error queue and removes
2543  the entry. */
2544  errdetail = ERR_get_error();
2545 
2546  /* Extract which lib and reason */
2547  lib = ERR_GET_LIB(errdetail);
2548  reason = ERR_GET_REASON(errdetail);
2549 
2550  if((lib == ERR_LIB_SSL) &&
2551  (reason == SSL_R_CERTIFICATE_VERIFY_FAILED)) {
2552  result = CURLE_SSL_CACERT;
2553 
2554  lerr = SSL_get_verify_result(BACKEND->handle);
2555  if(lerr != X509_V_OK) {
2556  *certverifyresult = lerr;
2557  snprintf(error_buffer, sizeof(error_buffer),
2558  "SSL certificate problem: %s",
2560  }
2561  else
2562  /* strcpy() is fine here as long as the string fits within
2563  error_buffer */
2564  strcpy(error_buffer, "SSL certificate verification failed");
2565  }
2566  else {
2567  result = CURLE_SSL_CONNECT_ERROR;
2568  ossl_strerror(errdetail, error_buffer, sizeof(error_buffer));
2569  }
2570 
2571  /* detail is already set to the SSL error above */
2572 
2573  /* If we e.g. use SSLv2 request-method and the server doesn't like us
2574  * (RST connection etc.), OpenSSL gives no explanation whatsoever and
2575  * the SO_ERROR is also lost.
2576  */
2577  if(CURLE_SSL_CONNECT_ERROR == result && errdetail == 0) {
2578  const char * const hostname = SSL_IS_PROXY() ?
2579  conn->http_proxy.host.name : conn->host.name;
2580  const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port;
2581  failf(data, OSSL_PACKAGE " SSL_connect: %s in connection to %s:%ld ",
2582  SSL_ERROR_to_str(detail), hostname, port);
2583  return result;
2584  }
2585 
2586  /* Could be a CERT problem */
2587  failf(data, "%s", error_buffer);
2588 
2589  return result;
2590  }
2591  }
2592  else {
2593  /* we have been connected fine, we're not waiting for anything else. */
2594  connssl->connecting_state = ssl_connect_3;
2595 
2596  /* Informational message */
2597  infof(data, "SSL connection using %s / %s\n",
2598  get_ssl_version_txt(BACKEND->handle),
2599  SSL_get_cipher(BACKEND->handle));
2600 
2601 #ifdef HAS_ALPN
2602  /* Sets data and len to negotiated protocol, len is 0 if no protocol was
2603  * negotiated
2604  */
2605  if(conn->bits.tls_enable_alpn) {
2606  const unsigned char *neg_protocol;
2607  unsigned int len;
2608  SSL_get0_alpn_selected(BACKEND->handle, &neg_protocol, &len);
2609  if(len != 0) {
2610  infof(data, "ALPN, server accepted to use %.*s\n", len, neg_protocol);
2611 
2612 #ifdef USE_NGHTTP2
2613  if(len == NGHTTP2_PROTO_VERSION_ID_LEN &&
2614  !memcmp(NGHTTP2_PROTO_VERSION_ID, neg_protocol, len)) {
2615  conn->negnpn = CURL_HTTP_VERSION_2;
2616  }
2617  else
2618 #endif
2619  if(len == ALPN_HTTP_1_1_LENGTH &&
2620  !memcmp(ALPN_HTTP_1_1, neg_protocol, ALPN_HTTP_1_1_LENGTH)) {
2621  conn->negnpn = CURL_HTTP_VERSION_1_1;
2622  }
2623  }
2624  else
2625  infof(data, "ALPN, server did not agree to a protocol\n");
2626  }
2627 #endif
2628 
2629  return CURLE_OK;
2630  }
2631 }
2632 
2633 static int asn1_object_dump(ASN1_OBJECT *a, char *buf, size_t len)
2634 {
2635  int i, ilen;
2636 
2637  ilen = (int)len;
2638  if(ilen < 0)
2639  return 1; /* buffer too big */
2640 
2641  i = i2t_ASN1_OBJECT(buf, ilen, a);
2642 
2643  if(i >= ilen)
2644  return 1; /* buffer too small */
2645 
2646  return 0;
2647 }
2648 
2649 #define push_certinfo(_label, _num) \
2650 do { \
2651  long info_len = BIO_get_mem_data(mem, &ptr); \
2652  Curl_ssl_push_certinfo_len(data, _num, _label, ptr, info_len); \
2653  if(1 != BIO_reset(mem)) \
2654  break; \
2655 } WHILE_FALSE
2656 
2657 static void pubkey_show(struct Curl_easy *data,
2658  BIO *mem,
2659  int num,
2660  const char *type,
2661  const char *name,
2662 #ifdef HAVE_OPAQUE_RSA_DSA_DH
2663  const
2664 #endif
2665  BIGNUM *bn)
2666 {
2667  char *ptr;
2668  char namebuf[32];
2669 
2670  snprintf(namebuf, sizeof(namebuf), "%s(%s)", type, name);
2671 
2672  if(bn)
2673  BN_print(mem, bn);
2674  push_certinfo(namebuf, num);
2675 }
2676 
2677 #ifdef HAVE_OPAQUE_RSA_DSA_DH
2678 #define print_pubkey_BN(_type, _name, _num) \
2679  pubkey_show(data, mem, _num, #_type, #_name, _name)
2680 
2681 #else
2682 #define print_pubkey_BN(_type, _name, _num) \
2683 do { \
2684  if(_type->_name) { \
2685  pubkey_show(data, mem, _num, #_type, #_name, _type->_name); \
2686  } \
2687 } WHILE_FALSE
2688 #endif
2689 
2690 static int X509V3_ext(struct Curl_easy *data,
2691  int certnum,
2692  CONST_EXTS STACK_OF(X509_EXTENSION) *exts)
2693 {
2694  int i;
2695  size_t j;
2696 
2697  if((int)sk_X509_EXTENSION_num(exts) <= 0)
2698  /* no extensions, bail out */
2699  return 1;
2700 
2701  for(i = 0; i < (int)sk_X509_EXTENSION_num(exts); i++) {
2702  ASN1_OBJECT *obj;
2703  X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
2704  BUF_MEM *biomem;
2705  char buf[512];
2706  char *ptr = buf;
2707  char namebuf[128];
2708  BIO *bio_out = BIO_new(BIO_s_mem());
2709 
2710  if(!bio_out)
2711  return 1;
2712 
2713  obj = X509_EXTENSION_get_object(ext);
2714 
2715  asn1_object_dump(obj, namebuf, sizeof(namebuf));
2716 
2717  if(!X509V3_EXT_print(bio_out, ext, 0, 0))
2718  ASN1_STRING_print(bio_out, (ASN1_STRING *)X509_EXTENSION_get_data(ext));
2719 
2720  BIO_get_mem_ptr(bio_out, &biomem);
2721 
2722  for(j = 0; j < (size_t)biomem->length; j++) {
2723  const char *sep = "";
2724  if(biomem->data[j] == '\n') {
2725  sep = ", ";
2726  j++; /* skip the newline */
2727  };
2728  while((j<(size_t)biomem->length) && (biomem->data[j] == ' '))
2729  j++;
2730  if(j<(size_t)biomem->length)
2731  ptr += snprintf(ptr, sizeof(buf)-(ptr-buf), "%s%c", sep,
2732  biomem->data[j]);
2733  }
2734 
2735  Curl_ssl_push_certinfo(data, certnum, namebuf, buf);
2736 
2737  BIO_free(bio_out);
2738 
2739  }
2740  return 0; /* all is fine */
2741 }
2742 
2743 static CURLcode get_cert_chain(struct connectdata *conn,
2744  struct ssl_connect_data *connssl)
2745 
2746 {
2747  CURLcode result;
2748  STACK_OF(X509) *sk;
2749  int i;
2750  struct Curl_easy *data = conn->data;
2751  int numcerts;
2752  BIO *mem;
2753 
2754  sk = SSL_get_peer_cert_chain(BACKEND->handle);
2755  if(!sk) {
2756  return CURLE_OUT_OF_MEMORY;
2757  }
2758 
2759  numcerts = sk_X509_num(sk);
2760 
2761  result = Curl_ssl_init_certinfo(data, numcerts);
2762  if(result) {
2763  return result;
2764  }
2765 
2766  mem = BIO_new(BIO_s_mem());
2767 
2768  for(i = 0; i < numcerts; i++) {
2769  ASN1_INTEGER *num;
2770  X509 *x = sk_X509_value(sk, i);
2771  EVP_PKEY *pubkey = NULL;
2772  int j;
2773  char *ptr;
2774  const ASN1_BIT_STRING *psig = NULL;
2775 
2776  X509_NAME_print_ex(mem, X509_get_subject_name(x), 0, XN_FLAG_ONELINE);
2777  push_certinfo("Subject", i);
2778 
2779  X509_NAME_print_ex(mem, X509_get_issuer_name(x), 0, XN_FLAG_ONELINE);
2780  push_certinfo("Issuer", i);
2781 
2782  BIO_printf(mem, "%lx", X509_get_version(x));
2783  push_certinfo("Version", i);
2784 
2785  num = X509_get_serialNumber(x);
2786  if(num->type == V_ASN1_NEG_INTEGER)
2787  BIO_puts(mem, "-");
2788  for(j = 0; j < num->length; j++)
2789  BIO_printf(mem, "%02x", num->data[j]);
2790  push_certinfo("Serial Number", i);
2791 
2792 #if defined(HAVE_X509_GET0_SIGNATURE) && defined(HAVE_X509_GET0_EXTENSIONS)
2793  {
2794  const X509_ALGOR *palg = NULL;
2795  ASN1_STRING *a = ASN1_STRING_new();
2796  if(a) {
2797  X509_get0_signature(&psig, &palg, x);
2798  X509_signature_print(mem, palg, a);
2799  ASN1_STRING_free(a);
2800 
2801  if(palg) {
2802  i2a_ASN1_OBJECT(mem, palg->algorithm);
2803  push_certinfo("Public Key Algorithm", i);
2804  }
2805  }
2806  X509V3_ext(data, i, X509_get0_extensions(x));
2807  }
2808 #else
2809  {
2810  /* before OpenSSL 1.0.2 */
2811  X509_CINF *cinf = x->cert_info;
2812 
2813  i2a_ASN1_OBJECT(mem, cinf->signature->algorithm);
2814  push_certinfo("Signature Algorithm", i);
2815 
2816  i2a_ASN1_OBJECT(mem, cinf->key->algor->algorithm);
2817  push_certinfo("Public Key Algorithm", i);
2818 
2819  X509V3_ext(data, i, cinf->extensions);
2820 
2821  psig = x->signature;
2822  }
2823 #endif
2824 
2825  ASN1_TIME_print(mem, X509_get0_notBefore(x));
2826  push_certinfo("Start date", i);
2827 
2828  ASN1_TIME_print(mem, X509_get0_notAfter(x));
2829  push_certinfo("Expire date", i);
2830 
2831  pubkey = X509_get_pubkey(x);
2832  if(!pubkey)
2833  infof(data, " Unable to load public key\n");
2834  else {
2835  int pktype;
2836 #ifdef HAVE_OPAQUE_EVP_PKEY
2837  pktype = EVP_PKEY_id(pubkey);
2838 #else
2839  pktype = pubkey->type;
2840 #endif
2841  switch(pktype) {
2842  case EVP_PKEY_RSA:
2843  {
2844  RSA *rsa;
2845 #ifdef HAVE_OPAQUE_EVP_PKEY
2846  rsa = EVP_PKEY_get0_RSA(pubkey);
2847 #else
2848  rsa = pubkey->pkey.rsa;
2849 #endif
2850 
2851 #ifdef HAVE_OPAQUE_RSA_DSA_DH
2852  {
2853  const BIGNUM *n;
2854  const BIGNUM *e;
2855 
2856  RSA_get0_key(rsa, &n, &e, NULL);
2857  BN_print(mem, n);
2858  push_certinfo("RSA Public Key", i);
2859  print_pubkey_BN(rsa, n, i);
2860  print_pubkey_BN(rsa, e, i);
2861  }
2862 #else
2863  BIO_printf(mem, "%d", BN_num_bits(rsa->n));
2864  push_certinfo("RSA Public Key", i);
2865  print_pubkey_BN(rsa, n, i);
2866  print_pubkey_BN(rsa, e, i);
2867 #endif
2868 
2869  break;
2870  }
2871  case EVP_PKEY_DSA:
2872  {
2873 #ifndef OPENSSL_NO_DSA
2874  DSA *dsa;
2875 #ifdef HAVE_OPAQUE_EVP_PKEY
2876  dsa = EVP_PKEY_get0_DSA(pubkey);
2877 #else
2878  dsa = pubkey->pkey.dsa;
2879 #endif
2880 #ifdef HAVE_OPAQUE_RSA_DSA_DH
2881  {
2882  const BIGNUM *p;
2883  const BIGNUM *q;
2884  const BIGNUM *g;
2885  const BIGNUM *pub_key;
2886 
2887  DSA_get0_pqg(dsa, &p, &q, &g);
2888  DSA_get0_key(dsa, &pub_key, NULL);
2889 
2890  print_pubkey_BN(dsa, p, i);
2891  print_pubkey_BN(dsa, q, i);
2892  print_pubkey_BN(dsa, g, i);
2893  print_pubkey_BN(dsa, pub_key, i);
2894  }
2895 #else
2896  print_pubkey_BN(dsa, p, i);
2897  print_pubkey_BN(dsa, q, i);
2898  print_pubkey_BN(dsa, g, i);
2899  print_pubkey_BN(dsa, pub_key, i);
2900 #endif
2901 #endif /* !OPENSSL_NO_DSA */
2902  break;
2903  }
2904  case EVP_PKEY_DH:
2905  {
2906  DH *dh;
2907 #ifdef HAVE_OPAQUE_EVP_PKEY
2908  dh = EVP_PKEY_get0_DH(pubkey);
2909 #else
2910  dh = pubkey->pkey.dh;
2911 #endif
2912 #ifdef HAVE_OPAQUE_RSA_DSA_DH
2913  {
2914  const BIGNUM *p;
2915  const BIGNUM *q;
2916  const BIGNUM *g;
2917  const BIGNUM *pub_key;
2918  DH_get0_pqg(dh, &p, &q, &g);
2919  DH_get0_key(dh, &pub_key, NULL);
2920  print_pubkey_BN(dh, p, i);
2921  print_pubkey_BN(dh, q, i);
2922  print_pubkey_BN(dh, g, i);
2923  print_pubkey_BN(dh, pub_key, i);
2924  }
2925 #else
2926  print_pubkey_BN(dh, p, i);
2927  print_pubkey_BN(dh, g, i);
2928  print_pubkey_BN(dh, pub_key, i);
2929 #endif
2930  break;
2931  }
2932 #if 0
2933  case EVP_PKEY_EC: /* symbol not present in OpenSSL 0.9.6 */
2934  /* left TODO */
2935  break;
2936 #endif
2937  }
2938  EVP_PKEY_free(pubkey);
2939  }
2940 
2941  if(psig) {
2942  for(j = 0; j < psig->length; j++)
2943  BIO_printf(mem, "%02x:", psig->data[j]);
2944  push_certinfo("Signature", i);
2945  }
2946 
2947  PEM_write_bio_X509(mem, x);
2948  push_certinfo("Cert", i);
2949  }
2950 
2951  BIO_free(mem);
2952 
2953  return CURLE_OK;
2954 }
2955 
2956 /*
2957  * Heavily modified from:
2958  * https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning#OpenSSL
2959  */
2960 static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data, X509* cert,
2961  const char *pinnedpubkey)
2962 {
2963  /* Scratch */
2964  int len1 = 0, len2 = 0;
2965  unsigned char *buff1 = NULL, *temp = NULL;
2966 
2967  /* Result is returned to caller */
2969 
2970  /* if a path wasn't specified, don't pin */
2971  if(!pinnedpubkey)
2972  return CURLE_OK;
2973 
2974  if(!cert)
2975  return result;
2976 
2977  do {
2978  /* Begin Gyrations to get the subjectPublicKeyInfo */
2979  /* Thanks to Viktor Dukhovni on the OpenSSL mailing list */
2980 
2981  /* https://groups.google.com/group/mailing.openssl.users/browse_thread
2982  /thread/d61858dae102c6c7 */
2983  len1 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL);
2984  if(len1 < 1)
2985  break; /* failed */
2986 
2987  /* https://www.openssl.org/docs/crypto/buffer.html */
2988  buff1 = temp = malloc(len1);
2989  if(!buff1)
2990  break; /* failed */
2991 
2992  /* https://www.openssl.org/docs/crypto/d2i_X509.html */
2993  len2 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &temp);
2994 
2995  /*
2996  * These checks are verifying we got back the same values as when we
2997  * sized the buffer. It's pretty weak since they should always be the
2998  * same. But it gives us something to test.
2999  */
3000  if((len1 != len2) || !temp || ((temp - buff1) != len1))
3001  break; /* failed */
3002 
3003  /* End Gyrations */
3004 
3005  /* The one good exit point */
3006  result = Curl_pin_peer_pubkey(data, pinnedpubkey, buff1, len1);
3007  } while(0);
3008 
3009  /* https://www.openssl.org/docs/crypto/buffer.html */
3010  if(buff1)
3011  free(buff1);
3012 
3013  return result;
3014 }
3015 
3016 /*
3017  * Get the server cert, verify it and show it etc, only call failf() if the
3018  * 'strict' argument is TRUE as otherwise all this is for informational
3019  * purposes only!
3020  *
3021  * We check certificates to authenticate the server; otherwise we risk
3022  * man-in-the-middle attack.
3023  */
3024 static CURLcode servercert(struct connectdata *conn,
3025  struct ssl_connect_data *connssl,
3026  bool strict)
3027 {
3028  CURLcode result = CURLE_OK;
3029  int rc;
3030  long lerr, len;
3031  struct Curl_easy *data = conn->data;
3032  X509 *issuer;
3033  FILE *fp;
3034  char buffer[2048];
3035  const char *ptr;
3036  long * const certverifyresult = SSL_IS_PROXY() ?
3038  BIO *mem = BIO_new(BIO_s_mem());
3039 
3040  if(data->set.ssl.certinfo)
3041  /* we've been asked to gather certificate info! */
3042  (void)get_cert_chain(conn, connssl);
3043 
3044  BACKEND->server_cert = SSL_get_peer_certificate(BACKEND->handle);
3045  if(!BACKEND->server_cert) {
3046  BIO_free(mem);
3047  if(!strict)
3048  return CURLE_OK;
3049 
3050  failf(data, "SSL: couldn't get peer certificate!");
3052  }
3053 
3054  infof(data, "%s certificate:\n", SSL_IS_PROXY() ? "Proxy" : "Server");
3055 
3056  rc = x509_name_oneline(X509_get_subject_name(BACKEND->server_cert),
3057  buffer, sizeof(buffer));
3058  infof(data, " subject: %s\n", rc?"[NONE]":buffer);
3059 
3060  ASN1_TIME_print(mem, X509_get0_notBefore(BACKEND->server_cert));
3061  len = BIO_get_mem_data(mem, (char **) &ptr);
3062  infof(data, " start date: %.*s\n", len, ptr);
3063  rc = BIO_reset(mem);
3064 
3065  ASN1_TIME_print(mem, X509_get0_notAfter(BACKEND->server_cert));
3066  len = BIO_get_mem_data(mem, (char **) &ptr);
3067  infof(data, " expire date: %.*s\n", len, ptr);
3068  rc = BIO_reset(mem);
3069 
3070  BIO_free(mem);
3071 
3072  if(SSL_CONN_CONFIG(verifyhost)) {
3073  result = verifyhost(conn, BACKEND->server_cert);
3074  if(result) {
3075  X509_free(BACKEND->server_cert);
3076  BACKEND->server_cert = NULL;
3077  return result;
3078  }
3079  }
3080 
3081  rc = x509_name_oneline(X509_get_issuer_name(BACKEND->server_cert),
3082  buffer, sizeof(buffer));
3083  if(rc) {
3084  if(strict)
3085  failf(data, "SSL: couldn't get X509-issuer name!");
3086  result = CURLE_SSL_CONNECT_ERROR;
3087  }
3088  else {
3089  infof(data, " issuer: %s\n", buffer);
3090 
3091  /* We could do all sorts of certificate verification stuff here before
3092  deallocating the certificate. */
3093 
3094  /* e.g. match issuer name with provided issuer certificate */
3095  if(SSL_SET_OPTION(issuercert)) {
3096  fp = fopen(SSL_SET_OPTION(issuercert), FOPEN_READTEXT);
3097  if(!fp) {
3098  if(strict)
3099  failf(data, "SSL: Unable to open issuer cert (%s)",
3100  SSL_SET_OPTION(issuercert));
3101  X509_free(BACKEND->server_cert);
3102  BACKEND->server_cert = NULL;
3103  return CURLE_SSL_ISSUER_ERROR;
3104  }
3105 
3106  issuer = PEM_read_X509(fp, NULL, ZERO_NULL, NULL);
3107  if(!issuer) {
3108  if(strict)
3109  failf(data, "SSL: Unable to read issuer cert (%s)",
3110  SSL_SET_OPTION(issuercert));
3111  X509_free(BACKEND->server_cert);
3112  X509_free(issuer);
3113  fclose(fp);
3114  return CURLE_SSL_ISSUER_ERROR;
3115  }
3116 
3117  fclose(fp);
3118 
3119  if(X509_check_issued(issuer, BACKEND->server_cert) != X509_V_OK) {
3120  if(strict)
3121  failf(data, "SSL: Certificate issuer check failed (%s)",
3122  SSL_SET_OPTION(issuercert));
3123  X509_free(BACKEND->server_cert);
3124  X509_free(issuer);
3125  BACKEND->server_cert = NULL;
3126  return CURLE_SSL_ISSUER_ERROR;
3127  }
3128 
3129  infof(data, " SSL certificate issuer check ok (%s)\n",
3130  SSL_SET_OPTION(issuercert));
3131  X509_free(issuer);
3132  }
3133 
3134  lerr = *certverifyresult = SSL_get_verify_result(BACKEND->handle);
3135 
3136  if(*certverifyresult != X509_V_OK) {
3137  if(SSL_CONN_CONFIG(verifypeer)) {
3138  /* We probably never reach this, because SSL_connect() will fail
3139  and we return earlier if verifypeer is set? */
3140  if(strict)
3141  failf(data, "SSL certificate verify result: %s (%ld)",
3142  X509_verify_cert_error_string(lerr), lerr);
3144  }
3145  else
3146  infof(data, " SSL certificate verify result: %s (%ld),"
3147  " continuing anyway.\n",
3148  X509_verify_cert_error_string(lerr), lerr);
3149  }
3150  else
3151  infof(data, " SSL certificate verify ok.\n");
3152  }
3153 
3154 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \
3155  !defined(OPENSSL_NO_OCSP)
3156  if(SSL_CONN_CONFIG(verifystatus)) {
3157  result = verifystatus(conn, connssl);
3158  if(result) {
3159  X509_free(BACKEND->server_cert);
3160  BACKEND->server_cert = NULL;
3161  return result;
3162  }
3163  }
3164 #endif
3165 
3166  if(!strict)
3167  /* when not strict, we don't bother about the verify cert problems */
3168  result = CURLE_OK;
3169 
3172  if(!result && ptr) {
3173  result = pkp_pin_peer_pubkey(data, BACKEND->server_cert, ptr);
3174  if(result)
3175  failf(data, "SSL: public key does not match pinned public key!");
3176  }
3177 
3178  X509_free(BACKEND->server_cert);
3179  BACKEND->server_cert = NULL;
3181 
3182  return result;
3183 }
3184 
3185 static CURLcode ossl_connect_step3(struct connectdata *conn, int sockindex)
3186 {
3187  CURLcode result = CURLE_OK;
3188  struct Curl_easy *data = conn->data;
3189  struct ssl_connect_data *connssl = &conn->ssl[sockindex];
3190 
3192 
3193  if(SSL_SET_OPTION(primary.sessionid)) {
3194  bool incache;
3195  SSL_SESSION *our_ssl_sessionid;
3196  void *old_ssl_sessionid = NULL;
3197 
3198  our_ssl_sessionid = SSL_get1_session(BACKEND->handle);
3199 
3200  /* SSL_get1_session() will increment the reference count and the session
3201  will stay in memory until explicitly freed with SSL_SESSION_free(3),
3202  regardless of its state. */
3203 
3204  Curl_ssl_sessionid_lock(conn);
3205  incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL,
3206  sockindex));
3207  if(incache) {
3208  if(old_ssl_sessionid != our_ssl_sessionid) {
3209  infof(data, "old SSL session ID is stale, removing\n");
3210  Curl_ssl_delsessionid(conn, old_ssl_sessionid);
3211  incache = FALSE;
3212  }
3213  }
3214 
3215  if(!incache) {
3216  result = Curl_ssl_addsessionid(conn, our_ssl_sessionid,
3217  0 /* unknown size */, sockindex);
3218  if(result) {
3219  Curl_ssl_sessionid_unlock(conn);
3220  failf(data, "failed to store ssl session");
3221  return result;
3222  }
3223  }
3224  else {
3225  /* Session was incache, so refcount already incremented earlier.
3226  * Avoid further increments with each SSL_get1_session() call.
3227  * This does not free the session as refcount remains > 0
3228  */
3229  SSL_SESSION_free(our_ssl_sessionid);
3230  }
3231  Curl_ssl_sessionid_unlock(conn);
3232  }
3233 
3234  /*
3235  * We check certificates to authenticate the server; otherwise we risk
3236  * man-in-the-middle attack; NEVERTHELESS, if we're told explicitly not to
3237  * verify the peer ignore faults and failures from the server cert
3238  * operations.
3239  */
3240 
3241  result = servercert(conn, connssl, (SSL_CONN_CONFIG(verifypeer) ||
3242  SSL_CONN_CONFIG(verifyhost)));
3243 
3244  if(!result)
3246 
3247  return result;
3248 }
3249 
3250 static Curl_recv ossl_recv;
3251 static Curl_send ossl_send;
3252 
3253 static CURLcode ossl_connect_common(struct connectdata *conn,
3254  int sockindex,
3255  bool nonblocking,
3256  bool *done)
3257 {
3258  CURLcode result;
3259  struct Curl_easy *data = conn->data;
3260  struct ssl_connect_data *connssl = &conn->ssl[sockindex];
3261  curl_socket_t sockfd = conn->sock[sockindex];
3262  time_t timeout_ms;
3263  int what;
3264 
3265  /* check if the connection has already been established */
3266  if(ssl_connection_complete == connssl->state) {
3267  *done = TRUE;
3268  return CURLE_OK;
3269  }
3270 
3271  if(ssl_connect_1 == connssl->connecting_state) {
3272  /* Find out how much more time we're allowed */
3273  timeout_ms = Curl_timeleft(data, NULL, TRUE);
3274 
3275  if(timeout_ms < 0) {
3276  /* no need to continue if time already is up */
3277  failf(data, "SSL connection timeout");
3278  return CURLE_OPERATION_TIMEDOUT;
3279  }
3280 
3281  result = ossl_connect_step1(conn, sockindex);
3282  if(result)
3283  return result;
3284  }
3285 
3286  while(ssl_connect_2 == connssl->connecting_state ||
3289 
3290  /* check allowed time left */
3291  timeout_ms = Curl_timeleft(data, NULL, TRUE);
3292 
3293  if(timeout_ms < 0) {
3294  /* no need to continue if time already is up */
3295  failf(data, "SSL connection timeout");
3296  return CURLE_OPERATION_TIMEDOUT;
3297  }
3298 
3299  /* if ssl is expecting something, check if it's available. */
3300  if(connssl->connecting_state == ssl_connect_2_reading ||
3302 
3304  connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
3306  connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
3307 
3308  what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
3309  nonblocking?0:timeout_ms);
3310  if(what < 0) {
3311  /* fatal error */
3312  failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
3313  return CURLE_SSL_CONNECT_ERROR;
3314  }
3315  if(0 == what) {
3316  if(nonblocking) {
3317  *done = FALSE;
3318  return CURLE_OK;
3319  }
3320  /* timeout */
3321  failf(data, "SSL connection timeout");
3322  return CURLE_OPERATION_TIMEDOUT;
3323  }
3324  /* socket is readable or writable */
3325  }
3326 
3327  /* Run transaction, and return to the caller if it failed or if this
3328  * connection is done nonblocking and this loop would execute again. This
3329  * permits the owner of a multi handle to abort a connection attempt
3330  * before step2 has completed while ensuring that a client using select()
3331  * or epoll() will always have a valid fdset to wait on.
3332  */
3333  result = ossl_connect_step2(conn, sockindex);
3334  if(result || (nonblocking &&
3335  (ssl_connect_2 == connssl->connecting_state ||
3338  return result;
3339 
3340  } /* repeat step2 until all transactions are done. */
3341 
3342  if(ssl_connect_3 == connssl->connecting_state) {
3343  result = ossl_connect_step3(conn, sockindex);
3344  if(result)
3345  return result;
3346  }
3347 
3348  if(ssl_connect_done == connssl->connecting_state) {
3349  connssl->state = ssl_connection_complete;
3350  conn->recv[sockindex] = ossl_recv;
3351  conn->send[sockindex] = ossl_send;
3352  *done = TRUE;
3353  }
3354  else
3355  *done = FALSE;
3356 
3357  /* Reset our connect state machine */
3358  connssl->connecting_state = ssl_connect_1;
3359 
3360  return CURLE_OK;
3361 }
3362 
3363 static CURLcode Curl_ossl_connect_nonblocking(struct connectdata *conn,
3364  int sockindex,
3365  bool *done)
3366 {
3367  return ossl_connect_common(conn, sockindex, TRUE, done);
3368 }
3369 
3370 static CURLcode Curl_ossl_connect(struct connectdata *conn, int sockindex)
3371 {
3372  CURLcode result;
3373  bool done = FALSE;
3374 
3375  result = ossl_connect_common(conn, sockindex, FALSE, &done);
3376  if(result)
3377  return result;
3378 
3379  DEBUGASSERT(done);
3380 
3381  return CURLE_OK;
3382 }
3383 
3384 static bool Curl_ossl_data_pending(const struct connectdata *conn,
3385  int connindex)
3386 {
3387  const struct ssl_connect_data *connssl = &conn->ssl[connindex];
3388  const struct ssl_connect_data *proxyssl = &conn->proxy_ssl[connindex];
3389  if(BACKEND->handle)
3390  /* SSL is in use */
3391  return (0 != SSL_pending(BACKEND->handle) ||
3392  (proxyssl->backend->handle &&
3393  0 != SSL_pending(proxyssl->backend->handle))) ?
3394  TRUE : FALSE;
3395  return FALSE;
3396 }
3397 
3398 static size_t Curl_ossl_version(char *buffer, size_t size);
3399 
3400 static ssize_t ossl_send(struct connectdata *conn,
3401  int sockindex,
3402  const void *mem,
3403  size_t len,
3404  CURLcode *curlcode)
3405 {
3406  /* SSL_write() is said to return 'int' while write() and send() returns
3407  'size_t' */
3408  int err;
3409  char error_buffer[256];
3410  unsigned long sslerror;
3411  int memlen;
3412  int rc;
3413  struct ssl_connect_data *connssl = &conn->ssl[sockindex];
3414 
3415  ERR_clear_error();
3416 
3417  memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
3418  rc = SSL_write(BACKEND->handle, mem, memlen);
3419 
3420  if(rc <= 0) {
3421  err = SSL_get_error(BACKEND->handle, rc);
3422 
3423  switch(err) {
3424  case SSL_ERROR_WANT_READ:
3425  case SSL_ERROR_WANT_WRITE:
3426  /* The operation did not complete; the same TLS/SSL I/O function
3427  should be called again later. This is basically an EWOULDBLOCK
3428  equivalent. */
3429  *curlcode = CURLE_AGAIN;
3430  return -1;
3431  case SSL_ERROR_SYSCALL:
3432  failf(conn->data, "SSL_write() returned SYSCALL, errno = %d",
3433  SOCKERRNO);
3434  *curlcode = CURLE_SEND_ERROR;
3435  return -1;
3436  case SSL_ERROR_SSL:
3437  /* A failure in the SSL library occurred, usually a protocol error.
3438  The OpenSSL error queue contains more information on the error. */
3439  sslerror = ERR_get_error();
3440  if(ERR_GET_LIB(sslerror) == ERR_LIB_SSL &&
3441  ERR_GET_REASON(sslerror) == SSL_R_BIO_NOT_SET &&
3442  conn->ssl[sockindex].state == ssl_connection_complete &&
3443  conn->proxy_ssl[sockindex].state == ssl_connection_complete) {
3444  char ver[120];
3445  Curl_ossl_version(ver, 120);
3446  failf(conn->data, "Error: %s does not support double SSL tunneling.",
3447  ver);
3448  }
3449  else
3450  failf(conn->data, "SSL_write() error: %s",
3451  ossl_strerror(sslerror, error_buffer, sizeof(error_buffer)));
3452  *curlcode = CURLE_SEND_ERROR;
3453  return -1;
3454  }
3455  /* a true error */
3456  failf(conn->data, OSSL_PACKAGE " SSL_write: %s, errno %d",
3457  SSL_ERROR_to_str(err), SOCKERRNO);
3458  *curlcode = CURLE_SEND_ERROR;
3459  return -1;
3460  }
3461  *curlcode = CURLE_OK;
3462  return (ssize_t)rc; /* number of bytes */
3463 }
3464 
3465 static ssize_t ossl_recv(struct connectdata *conn, /* connection data */
3466  int num, /* socketindex */
3467  char *buf, /* store read data here */
3468  size_t buffersize, /* max amount to read */
3469  CURLcode *curlcode)
3470 {
3471  char error_buffer[256];
3472  unsigned long sslerror;
3473  ssize_t nread;
3474  int buffsize;
3475  struct ssl_connect_data *connssl = &conn->ssl[num];
3476 
3477  ERR_clear_error();
3478 
3479  buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
3480  nread = (ssize_t)SSL_read(BACKEND->handle, buf, buffsize);
3481  if(nread <= 0) {
3482  /* failed SSL_read */
3483  int err = SSL_get_error(BACKEND->handle, (int)nread);
3484 
3485  switch(err) {
3486  case SSL_ERROR_NONE: /* this is not an error */
3487  case SSL_ERROR_ZERO_RETURN: /* no more data */
3488  break;
3489  case SSL_ERROR_WANT_READ:
3490  case SSL_ERROR_WANT_WRITE:
3491  /* there's data pending, re-invoke SSL_read() */
3492  *curlcode = CURLE_AGAIN;
3493  return -1;
3494  default:
3495  /* openssl/ssl.h for SSL_ERROR_SYSCALL says "look at error stack/return
3496  value/errno" */
3497  /* https://www.openssl.org/docs/crypto/ERR_get_error.html */
3498  sslerror = ERR_get_error();
3499  if((nread < 0) || sslerror) {
3500  /* If the return code was negative or there actually is an error in the
3501  queue */
3502  failf(conn->data, OSSL_PACKAGE " SSL_read: %s, errno %d",
3503  (sslerror ?
3504  ossl_strerror(sslerror, error_buffer, sizeof(error_buffer)) :
3505  SSL_ERROR_to_str(err)),
3506  SOCKERRNO);
3507  *curlcode = CURLE_RECV_ERROR;
3508  return -1;
3509  }
3510  }
3511  }
3512  return nread;
3513 }
3514 
3515 static size_t Curl_ossl_version(char *buffer, size_t size)
3516 {
3517 #ifdef OPENSSL_IS_BORINGSSL
3518  return snprintf(buffer, size, OSSL_PACKAGE);
3519 #else /* OPENSSL_IS_BORINGSSL */
3520  char sub[3];
3521  unsigned long ssleay_value;
3522  sub[2]='\0';
3523  sub[1]='\0';
3524  ssleay_value = OpenSSL_version_num();
3525  if(ssleay_value < 0x906000) {
3526  ssleay_value = SSLEAY_VERSION_NUMBER;
3527  sub[0]='\0';
3528  }
3529  else {
3530  if(ssleay_value&0xff0) {
3531  int minor_ver = (ssleay_value >> 4) & 0xff;
3532  if(minor_ver > 26) {
3533  /* handle extended version introduced for 0.9.8za */
3534  sub[1] = (char) ((minor_ver - 1) % 26 + 'a' + 1);
3535  sub[0] = 'z';
3536  }
3537  else {
3538  sub[0] = (char) (minor_ver + 'a' - 1);
3539  }
3540  }
3541  else
3542  sub[0]='\0';
3543  }
3544 
3545  return snprintf(buffer, size, "%s/%lx.%lx.%lx%s",
3546  OSSL_PACKAGE,
3547  (ssleay_value>>28)&0xf,
3548  (ssleay_value>>20)&0xff,
3549  (ssleay_value>>12)&0xff,
3550  sub);
3551 #endif /* OPENSSL_IS_BORINGSSL */
3552 }
3553 
3554 /* can be called with data == NULL */
3555 static CURLcode Curl_ossl_random(struct Curl_easy *data,
3556  unsigned char *entropy, size_t length)
3557 {
3558  int rc;
3559  if(data) {
3560  if(Curl_ossl_seed(data)) /* Initiate the seed if not already done */
3561  return CURLE_FAILED_INIT; /* couldn't seed for some reason */
3562  }
3563  else {
3564  if(!rand_enough())
3565  return CURLE_FAILED_INIT;
3566  }
3567  /* RAND_bytes() returns 1 on success, 0 otherwise. */
3568  rc = RAND_bytes(entropy, curlx_uztosi(length));
3569  return (rc == 1 ? CURLE_OK : CURLE_FAILED_INIT);
3570 }
3571 
3572 static CURLcode Curl_ossl_md5sum(unsigned char *tmp, /* input */
3573  size_t tmplen,
3574  unsigned char *md5sum /* output */,
3575  size_t unused)
3576 {
3577  MD5_CTX MD5pw;
3578  (void)unused;
3579  MD5_Init(&MD5pw);
3580  MD5_Update(&MD5pw, tmp, tmplen);
3581  MD5_Final(md5sum, &MD5pw);
3582  return CURLE_OK;
3583 }
3584 
3585 #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
3586 static void Curl_ossl_sha256sum(const unsigned char *tmp, /* input */
3587  size_t tmplen,
3588  unsigned char *sha256sum /* output */,
3589  size_t unused)
3590 {
3591  SHA256_CTX SHA256pw;
3592  (void)unused;
3593  SHA256_Init(&SHA256pw);
3594  SHA256_Update(&SHA256pw, tmp, tmplen);
3595  SHA256_Final(sha256sum, &SHA256pw);
3596 }
3597 #endif
3598 
3599 static bool Curl_ossl_cert_status_request(void)
3600 {
3601 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \
3602  !defined(OPENSSL_NO_OCSP)
3603  return TRUE;
3604 #else
3605  return FALSE;
3606 #endif
3607 }
3608 
3609 static void *Curl_ossl_get_internals(struct ssl_connect_data *connssl,
3610  CURLINFO info)
3611 {
3612  /* Legacy: CURLINFO_TLS_SESSION must return an SSL_CTX pointer. */
3613  return info == CURLINFO_TLS_SESSION ?
3614  (void *)BACKEND->ctx : (void *)BACKEND->handle;
3615 }
3616 
3617 const struct Curl_ssl Curl_ssl_openssl = {
3618  { CURLSSLBACKEND_OPENSSL, "openssl" }, /* info */
3619 
3620  1, /* have_ca_path */
3621  1, /* have_certinfo */
3622  1, /* have_pinnedpubkey */
3623  1, /* have_ssl_ctx */
3624  1, /* support_https_proxy */
3625 
3626  sizeof(struct ssl_backend_data),
3627 
3628  Curl_ossl_init, /* init */
3629  Curl_ossl_cleanup, /* cleanup */
3630  Curl_ossl_version, /* version */
3631  Curl_ossl_check_cxn, /* check_cxn */
3632  Curl_ossl_shutdown, /* shutdown */
3633  Curl_ossl_data_pending, /* data_pending */
3634  Curl_ossl_random, /* random */
3635  Curl_ossl_cert_status_request, /* cert_status_request */
3636  Curl_ossl_connect, /* connect */
3637  Curl_ossl_connect_nonblocking, /* connect_nonblocking */
3638  Curl_ossl_get_internals, /* get_internals */
3639  Curl_ossl_close, /* close */
3640  Curl_ossl_close_all, /* close_all */
3641  Curl_ossl_session_free, /* session_free */
3642  Curl_ossl_set_engine, /* set_engine */
3643  Curl_ossl_set_engine_default, /* set_engine_default */
3644  Curl_ossl_engines_list, /* engines_list */
3645  Curl_none_false_start, /* false_start */
3646  Curl_ossl_md5sum, /* md5sum */
3647 #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
3648  Curl_ossl_sha256sum /* sha256sum */
3649 #else
3650  NULL /* sha256sum */
3651 #endif
3652 };
3653 
3654 #endif /* USE_OPENSSL */
#define free(ptr)
Definition: curl_memory.h:130
#define PEM_write_bio_X509
Definition: setup-vms.h:257
int Curl_debug(struct Curl_easy *data, curl_infotype type, char *ptr, size_t size, struct connectdata *conn)
Definition: sendf.c:819
#define EVP_PKEY_free
Definition: setup-vms.h:242
#define X509_free
Definition: setup-vms.h:345
struct ssl_connect_data ssl[2]
Definition: urldata.h:887
#define SSL_CTX_use_PrivateKey
Definition: setup-vms.h:281
#define state(x, y)
Definition: ftp.c:100
#define X509_NAME_ENTRY_get_data
Definition: setup-vms.h:337
ssize_t( Curl_recv)(struct connectdata *conn, int sockindex, char *buf, size_t len, CURLcode *err)
Definition: urldata.h:737
#define RAND_egd
Definition: setup-vms.h:263
struct ConnectBits bits
Definition: urldata.h:893
#define UI_get_string_type
Definition: setup-vms.h:319
#define RAND_add
Definition: setup-vms.h:261
#define MD5_Final
Definition: setup-vms.h:249
#define SHA256_Update
Definition: setup-vms.h:364
ROSCPP_DECL bool check()
#define UI_method_set_reader
Definition: setup-vms.h:328
#define SSL_get_session
Definition: setup-vms.h:296
struct UserDefined set
Definition: urldata.h:1762
#define RECV_TYPE_ARG2
Definition: config-dos.h:108
#define ENGINE_load_builtin_engines
Definition: setup-vms.h:231
Curl_recv * recv[2]
Definition: urldata.h:881
#define SSL_CTX_add_client_CA
Definition: setup-vms.h:268
#define MD5_Init
Definition: setup-vms.h:250
#define SSL_read
Definition: setup-vms.h:304
#define X509_EXTENSION_get_data
Definition: setup-vms.h:334
#define SSL_CONN_CONFIG(var)
Definition: vtls.h:135
#define BIO_new
Definition: setup-vms.h:200
int negnpn
Definition: urldata.h:1027
struct hostname host
Definition: urldata.h:758
#define FIRSTSOCKET
Definition: urldata.h:487
time_t tv_sec
Definition: timeval.h:33
f
#define ALPN_HTTP_1_1
Definition: vtls.h:125
#define SSL_CTX_new
Definition: setup-vms.h:275
#define CURL_SOCKET_BAD
Definition: curl.h:131
Definition: vtls.h:29
struct curltime curlx_tvnow(void)
Definition: timeval.c:106
void * fsslctxp
Definition: urldata.h:231
#define failf
Definition: sendf.h:48
struct ssl_config_data proxy_ssl
Definition: urldata.h:1587
#define SSL_SET_OPTION(var)
Definition: vtls.h:133
#define SSL_get_verify_result
Definition: setup-vms.h:298
ssl_connect_state connecting_state
Definition: urldata.h:201
#define ENGINE_free
Definition: setup-vms.h:226
#define ENGINE_ctrl_cmd
Definition: setup-vms.h:224
#define SOCKERRNO
#define DEBUGASSERT(x)
UNITTEST_START char * ptr
Definition: unit1330.c:38
CURLcode
Definition: curl.h:454
#define MD5_Update
Definition: setup-vms.h:251
#define UI_method_get_writer
Definition: setup-vms.h:325
#define ERR_free_strings
Definition: setup-vms.h:237
#define SSL_connect
Definition: setup-vms.h:287
static const struct Curl_handler *const protocols[]
Definition: url.c:161
#define UI_OpenSSL
Definition: setup-vms.h:330
CURLINFO
Definition: curl.h:2439
#define BIO_s_mem
Definition: setup-vms.h:201
#define GENERAL_NAMES_free
Definition: setup-vms.h:244
#define X509_NAME_get_index_by_NID
Definition: setup-vms.h:339
#define PKCS12_PBE_add
Definition: setup-vms.h:258
#define X509_get_subject_name
Definition: setup-vms.h:350
#define SSL_get_peer_cert_chain
Definition: setup-vms.h:293
#define ENABLE_IPV6
Definition: config-os400.h:71
#define SSL_write
Definition: setup-vms.h:310
struct hostname host
Definition: urldata.h:833
static char * password
Definition: unit1304.c:27
#define SSL_CTX_use_PrivateKey_file
Definition: setup-vms.h:282
#define SSL_get1_session
Definition: setup-vms.h:289
if(strcmp(arg,"1305")!=0)
Definition: unit1305.c:127
#define SSL_CTX_set_cipher_list
Definition: setup-vms.h:276
#define SSL_get_peer_certificate
Definition: setup-vms.h:294
#define SSL_get_error
Definition: setup-vms.h:292
#define strcasecompare(a, b)
Definition: strcase.h:35
int curlx_uztosi(size_t uznum)
Definition: warnless.c:203
CURL_EXTERN char * curl_getenv(const char *variable)
Definition: getenv.c:51
#define ENGINE_get_id
Definition: setup-vms.h:228
#define malloc(size)
Definition: curl_memory.h:124
#define EVP_PKEY_copy_parameters
Definition: setup-vms.h:241
#define UI_method_get_reader
Definition: setup-vms.h:324
#define SSL_CTX_load_verify_locations
Definition: setup-vms.h:274
#define X509_LOOKUP_file
Definition: setup-vms.h:336
#define ERR_peek_error
Definition: setup-vms.h:239
char * name
Definition: urldata.h:444
UNITTEST_START int result
Definition: unit1304.c:49
const char ** p
Definition: unit1394.c:76
int j
char buffer[]
Definition: unit1308.c:48
#define PKCS12_free
Definition: setup-vms.h:259
struct ssl_config_data ssl
Definition: urldata.h:1586
#define RAND_status
Definition: setup-vms.h:266
unsigned int i
Definition: unit1303.c:79
#define RECV_TYPE_ARG3
Definition: config-dos.h:109
#define SSL_version
Definition: setup-vms.h:309
#define ENGINE_load_private_key
Definition: setup-vms.h:232
#define FOPEN_READTEXT
Definition: curl_setup.h:732
size_t len
Definition: curl_sasl.c:55
struct proxy_info http_proxy
Definition: urldata.h:839
curl_ftpccc ftp_ccc
Definition: urldata.h:1646
ssl_connection_state state
Definition: urldata.h:200
#define X509_get_serialNumber
Definition: setup-vms.h:349
long httpversion
Definition: urldata.h:1583
size_t klen
Definition: unit1602.c:54
#define SHA256_Init
Definition: setup-vms.h:363
memcpy(filename, filename1, strlen(filename1))
#define ERR_error_string_n
Definition: setup-vms.h:236
#define X509_NAME_get_entry
Definition: setup-vms.h:338
#define ZERO_NULL
Definition: curlx.c:131
#define SSL_CTX_use_certificate_file
Definition: setup-vms.h:285
#define X509_EXTENSION_get_object
Definition: setup-vms.h:335
int Curl_wait_ms(int timeout_ms)
Definition: select.c:75
#define ALPN_HTTP_1_1_LENGTH
Definition: vtls.h:124
#define ASN1_STRING_print
Definition: setup-vms.h:195
#define ERR_remove_state
Definition: setup-vms.h:240
#define SSL_set_fd
Definition: setup-vms.h:306
#define FALSE
time_t Curl_timeleft(struct Curl_easy *data, struct curltime *nowp, bool duringconnect)
Definition: connect.c:182
#define UI_method_get_opener
Definition: setup-vms.h:323
#define RECV_TYPE_ARG4
Definition: config-dos.h:110
bool tls_enable_npn
Definition: urldata.h:434
curl_easy_setopt expects a curl_off_t argument for this option curl_easy_setopt expects a curl_write_callback argument for this option curl_easy_setopt expects a curl_ioctl_callback argument for this option curl_easy_setopt expects a curl_opensocket_callback argument for this option curl_easy_setopt expects a curl_debug_callback argument for this option curl_easy_setopt expects a curl_conv_callback argument for this option curl_easy_setopt expects a private data pointer as argument for this option curl_easy_setopt expects a FILE *argument for this option curl_easy_setopt expects a struct curl_httppost *argument for this option curl_easy_setopt expects a struct curl_slist *argument for this option curl_easy_getinfo expects a pointer to char *for this info curl_easy_getinfo expects a pointer to double for this info curl_easy_getinfo expects a pointer to struct curl_tlssessioninfo *for this info curl_easy_getinfo expects a pointer to curl_socket_t for this info size_t
#define RAND_bytes
Definition: setup-vms.h:262
UNITTEST_START int rc
Definition: unit1301.c:31
bool tls_enable_alpn
Definition: urldata.h:435
bool Curl_none_false_start(void)
#define SSL_get_shutdown
Definition: setup-vms.h:297
#define SSL_get_certificate
Definition: setup-vms.h:290
#define EAGAIN
CURL_EXTERN struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: slist.c:89
#define SOCKET_READABLE(x, z)
Definition: select.h:79
#define CONF_modules_load_file
Definition: setup-vms.h:207
#define fail(msg)
Definition: curlcheck.h:51
Definition: unit1323.c:36
bool tunnel_proxy
Definition: urldata.h:400
#define BN_num_bits
Definition: setup-vms.h:203
UNITTEST_START struct Curl_easy data
Definition: unit1399.c:82
#define UI_set_result
Definition: setup-vms.h:331
#define ENGINE_ctrl
Definition: setup-vms.h:223
#define SSL_CTX_get_cert_store
Definition: setup-vms.h:273
#define ENGINE_init
Definition: setup-vms.h:230
#define UI_destroy_method
Definition: setup-vms.h:321
#define SSL_free
Definition: setup-vms.h:288
#define SSL_SESSION_free
Definition: setup-vms.h:286
TFSIMD_FORCE_INLINE const tfScalar & x() const
#define SSL_CTX_set_verify
Definition: setup-vms.h:280
curl_ssl_ctx_callback fsslctx
Definition: urldata.h:230
#define SSL_shutdown
Definition: setup-vms.h:308
#define X509_get_pubkey
Definition: setup-vms.h:348
Curl_send * send[2]
Definition: urldata.h:882
#define ERR_clear_error
Definition: setup-vms.h:234
#define SSL_CTX_use_certificate
Definition: setup-vms.h:284
#define BIO_free
Definition: setup-vms.h:199
Definition: curl.h:455
ssize_t( Curl_send)(struct connectdata *conn, int sockindex, const void *buf, size_t len, CURLcode *err)
Definition: urldata.h:730
#define ENGINE_set_default
Definition: setup-vms.h:233
int Curl_inet_pton(int af, const char *src, void *dst)
Definition: inet_pton.c:66
#define X509_get_ext_d2i
Definition: setup-vms.h:346
static unsigned short port
Definition: sockfilt.c:137
#define ENGINE_get_first
Definition: setup-vms.h:227
#define UI_method_set_writer
Definition: setup-vms.h:329
#define Curl_convert_from_utf8(a, b, c)
Definition: non-ascii.h:58
struct UrlState state
Definition: urldata.h:1769
#define EVP_cleanup
Definition: setup-vms.h:243
int Curl_socket_check(curl_socket_t readfd0, curl_socket_t readfd1, curl_socket_t writefd, time_t timeout_ms)
Definition: select.c:145
#define ENGINE_finish
Definition: setup-vms.h:225
#define ASN1_STRING_to_UTF8
Definition: setup-vms.h:196
#define RAND_load_file
Definition: setup-vms.h:265
#define SSL_CTX_free
Definition: setup-vms.h:272
def msg_type(f)
#define RANDOM_FILE
#define i2d_X509_PUBKEY
Definition: setup-vms.h:245
int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
#define ssize_t
Definition: config-win32.h:382
#define SSL_load_error_strings
Definition: setup-vms.h:300
curl_socket_t sock[2]
Definition: urldata.h:876
CURL_TLSAUTH
Definition: curl.h:1937
#define ASN1_STRING_type
Definition: setup-vms.h:197
#define X509_check_issued
Definition: setup-vms.h:344
#define ERR_get_error
Definition: setup-vms.h:238
#define UI_get0_user_data
Definition: setup-vms.h:317
#define ENGINE_get_next
Definition: setup-vms.h:229
#define SSL_CTX_check_private_key
Definition: setup-vms.h:270
char buf[3]
Definition: unit1398.c:32
#define SSL_get_privatekey
Definition: setup-vms.h:295
bool verbose
Definition: urldata.h:1635
#define infof
Definition: sendf.h:44
#define UI_method_get_closer
Definition: setup-vms.h:322
char * str[STRING_LAST]
Definition: urldata.h:1663
bool ipv6_ip
Definition: urldata.h:387
#define X509_STORE_add_lookup
Definition: setup-vms.h:342
struct ssl_connect_data proxy_ssl[2]
Definition: urldata.h:888
curl_debug_callback fdebug
Definition: urldata.h:1524
#define CURL_HTTP_VERSION_2
Definition: curl.h:1879
long port
Definition: urldata.h:841
#define SSL_set_session
Definition: setup-vms.h:307
#define SSL_CTX_set_default_passwd_cb
Definition: setup-vms.h:278
#define OPENSSL_load_builtin_modules
Definition: setup-vms.h:254
#define UI_method_set_opener
Definition: setup-vms.h:327
#define X509_load_crl_file
Definition: setup-vms.h:351
#define d2i_PKCS12_fp
Definition: setup-vms.h:353
size_t size
Definition: unit1302.c:52
#define i2t_ASN1_OBJECT
Definition: setup-vms.h:354
#define snprintf
Definition: curl_printf.h:42
#define UI_create_method
Definition: setup-vms.h:320
#define TRUE
#define X509_verify_cert_error_string
Definition: setup-vms.h:352
struct Curl_message msg
Definition: urldata.h:1744
#define SHA256_Final
Definition: setup-vms.h:362
#define SSLv23_client_method
Definition: setup-vms.h:312
#define SSL_CTX_set_msg_callback
Definition: setup-vms.h:279
#define X509_NAME_print_ex
Definition: setup-vms.h:340
Definition: md5.c:221
#define RECV_TYPE_ARG1
Definition: config-dos.h:107
#define SSL_new
Definition: setup-vms.h:301
void * SSL
Definition: net_skeleton.h:128
#define RAND_file_name
Definition: setup-vms.h:264
int curl_socket_t
Definition: curl.h:130
const char * name
Definition: curl_sasl.c:54
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
long certverifyresult
Definition: urldata.h:227
#define UI_get_input_flags
Definition: setup-vms.h:318
#define FOPEN_APPENDTEXT
Definition: curl_setup.h:734
const char * dispname
Definition: urldata.h:445
int key
Definition: unit1602.c:56
#define X509V3_EXT_print
Definition: setup-vms.h:332
Definition: debug.c:29
#define PKCS12_parse
Definition: setup-vms.h:260
#define ASN1_STRING_length
Definition: setup-vms.h:194
int remote_port
Definition: urldata.h:842
#define SSL_pending
Definition: setup-vms.h:303
#define X509_get_issuer_name
Definition: setup-vms.h:347
#define UI_method_set_closer
Definition: setup-vms.h:326
#define PEM_read_X509
Definition: setup-vms.h:256
unsigned int tv_usec
Definition: timeval.h:34
#define SSLv3_client_method
Definition: setup-vms.h:313
#define SSL_IS_PROXY()
Definition: vtls.h:130
#define X509_STORE_set_flags
Definition: setup-vms.h:343
void * SSL_CTX
Definition: net_skeleton.h:129
#define SSL_set_connect_state
Definition: setup-vms.h:305
struct Curl_easy * data
Definition: urldata.h:791


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16