| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) Daniel Stenberg, <[email protected]>, et al. |
| 9 |
* Copyright (C) Nick Zitzmann, <[email protected]>. |
| 10 |
* |
| 11 |
* This software is licensed as described in the file COPYING, which |
| 12 |
* you should have received as part of this distribution. The terms |
| 13 |
* are also available at https://curl.se/docs/copyright.html. |
| 14 |
* |
| 15 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 16 |
* copies of the Software, and permit persons to whom the Software is |
| 17 |
* furnished to do so, under the terms of the COPYING file. |
| 18 |
* |
| 19 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 20 |
* KIND, either express or implied. |
| 21 |
* |
| 22 |
* SPDX-License-Identifier: curl |
| 23 |
* |
| 24 |
***************************************************************************/ |
| 25 |
|
| 26 |
/* |
| 27 |
* Source file for all iOS and macOS Secure Transport-specific code for the |
| 28 |
* TLS/SSL layer. No code but vtls.c should ever call or use these functions. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "../curl_setup.h" |
| 32 |
|
| 33 |
#ifdef USE_SECTRANSP |
| 34 |
|
| 35 |
#include "../urldata.h" /* for the Curl_easy definition */ |
| 36 |
#include "../curlx/base64.h" |
| 37 |
#include "../curlx/strparse.h" |
| 38 |
#include "../multiif.h" |
| 39 |
#include "../strcase.h" |
| 40 |
#include "x509asn1.h" |
| 41 |
#include "vtls_scache.h" |
| 42 |
#include "../strerror.h" |
| 43 |
#include "cipher_suite.h" |
| 44 |
|
| 45 |
#ifdef __clang__ |
| 46 |
#pragma clang diagnostic push |
| 47 |
#pragma clang diagnostic ignored "-Wunreachable-code" |
| 48 |
#endif /* __clang__ */ |
| 49 |
|
| 50 |
#ifdef __GNUC__ |
| 51 |
#pragma GCC diagnostic push |
| 52 |
#pragma GCC diagnostic ignored "-Waddress" |
| 53 |
#endif |
| 54 |
|
| 55 |
#if defined(__GNUC__) && defined(__APPLE__) |
| 56 |
#pragma GCC diagnostic push |
| 57 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 58 |
#endif |
| 59 |
|
| 60 |
#include <limits.h> |
| 61 |
|
| 62 |
#include <Security/Security.h> |
| 63 |
/* For some reason, when building for iOS, the omnibus header above does |
| 64 |
* not include SecureTransport.h as of iOS SDK 5.1. */ |
| 65 |
#include <Security/SecureTransport.h> |
| 66 |
#include <CoreFoundation/CoreFoundation.h> |
| 67 |
#include <CommonCrypto/CommonDigest.h> |
| 68 |
|
| 69 |
/* The Security framework has changed greatly between iOS and different macOS |
| 70 |
versions, and we will try to support as many of them as we can (back to |
| 71 |
Leopard and iOS 5) by using macros and weak-linking. |
| 72 |
|
| 73 |
In general, you want to build this using the most recent OS SDK, since some |
| 74 |
features require curl to be built against the latest SDK. TLS 1.1 and 1.2 |
| 75 |
support, for instance, require the macOS 10.8 SDK or later. TLS 1.3 |
| 76 |
requires the macOS 10.13 or iOS 11 SDK or later. */ |
| 77 |
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) |
| 78 |
|
| 79 |
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1050 |
| 80 |
#error "The Secure Transport backend requires Leopard or later." |
| 81 |
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED < 1050 */ |
| 82 |
|
| 83 |
#define CURL_BUILD_IOS 0 |
| 84 |
#define CURL_BUILD_IOS_7 0 |
| 85 |
#define CURL_BUILD_IOS_9 0 |
| 86 |
#define CURL_BUILD_IOS_11 0 |
| 87 |
#define CURL_BUILD_IOS_13 0 |
| 88 |
#define CURL_BUILD_MAC 1 |
| 89 |
/* This is the maximum API level we are allowed to use when building: */ |
| 90 |
#define CURL_BUILD_MAC_10_5 MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 |
| 91 |
#define CURL_BUILD_MAC_10_6 MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
| 92 |
#define CURL_BUILD_MAC_10_7 MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 |
| 93 |
#define CURL_BUILD_MAC_10_8 MAC_OS_X_VERSION_MAX_ALLOWED >= 1080 |
| 94 |
#define CURL_BUILD_MAC_10_9 MAC_OS_X_VERSION_MAX_ALLOWED >= 1090 |
| 95 |
#define CURL_BUILD_MAC_10_11 MAC_OS_X_VERSION_MAX_ALLOWED >= 101100 |
| 96 |
#define CURL_BUILD_MAC_10_13 MAC_OS_X_VERSION_MAX_ALLOWED >= 101300 |
| 97 |
#define CURL_BUILD_MAC_10_15 MAC_OS_X_VERSION_MAX_ALLOWED >= 101500 |
| 98 |
/* These macros mean "the following code is present to allow runtime backward |
| 99 |
compatibility with at least this cat or earlier": |
| 100 |
(You set this at build-time using the compiler command line option |
| 101 |
"-mmacosx-version-min.") */ |
| 102 |
#define CURL_SUPPORT_MAC_10_5 MAC_OS_X_VERSION_MIN_REQUIRED <= 1050 |
| 103 |
#define CURL_SUPPORT_MAC_10_6 MAC_OS_X_VERSION_MIN_REQUIRED <= 1060 |
| 104 |
#define CURL_SUPPORT_MAC_10_7 MAC_OS_X_VERSION_MIN_REQUIRED <= 1070 |
| 105 |
#define CURL_SUPPORT_MAC_10_8 MAC_OS_X_VERSION_MIN_REQUIRED <= 1080 |
| 106 |
#define CURL_SUPPORT_MAC_10_9 MAC_OS_X_VERSION_MIN_REQUIRED <= 1090 |
| 107 |
|
| 108 |
#elif TARGET_OS_EMBEDDED || TARGET_OS_IPHONE |
| 109 |
#define CURL_BUILD_IOS 1 |
| 110 |
#define CURL_BUILD_IOS_7 __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 |
| 111 |
#define CURL_BUILD_IOS_9 __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000 |
| 112 |
#define CURL_BUILD_IOS_11 __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 |
| 113 |
#define CURL_BUILD_IOS_13 __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 |
| 114 |
#define CURL_BUILD_MAC 0 |
| 115 |
#define CURL_BUILD_MAC_10_5 0 |
| 116 |
#define CURL_BUILD_MAC_10_6 0 |
| 117 |
#define CURL_BUILD_MAC_10_7 0 |
| 118 |
#define CURL_BUILD_MAC_10_8 0 |
| 119 |
#define CURL_BUILD_MAC_10_9 0 |
| 120 |
#define CURL_BUILD_MAC_10_11 0 |
| 121 |
#define CURL_BUILD_MAC_10_13 0 |
| 122 |
#define CURL_BUILD_MAC_10_15 0 |
| 123 |
#define CURL_SUPPORT_MAC_10_5 0 |
| 124 |
#define CURL_SUPPORT_MAC_10_6 0 |
| 125 |
#define CURL_SUPPORT_MAC_10_7 0 |
| 126 |
#define CURL_SUPPORT_MAC_10_8 0 |
| 127 |
#define CURL_SUPPORT_MAC_10_9 0 |
| 128 |
|
| 129 |
#else |
| 130 |
#error "The Secure Transport backend requires iOS or macOS." |
| 131 |
#endif /* (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) */ |
| 132 |
|
| 133 |
#if CURL_BUILD_MAC |
| 134 |
#include <sys/sysctl.h> |
| 135 |
#endif /* CURL_BUILD_MAC */ |
| 136 |
|
| 137 |
#include "../sendf.h" |
| 138 |
#include "../curlx/inet_pton.h" |
| 139 |
#include "../connect.h" |
| 140 |
#include "../select.h" |
| 141 |
#include "vtls.h" |
| 142 |
#include "vtls_int.h" |
| 143 |
#include "sectransp.h" |
| 144 |
#include "../curl_printf.h" |
| 145 |
#include "../strdup.h" |
| 146 |
|
| 147 |
#include "../curl_memory.h" |
| 148 |
/* The last #include file should be: */ |
| 149 |
#include "../memdebug.h" |
| 150 |
|
| 151 |
|
| 152 |
/* From MacTypes.h (which we cannot include because it is not present in |
| 153 |
iOS: */ |
| 154 |
#define ioErr -36 |
| 155 |
#define paramErr -50 |
| 156 |
|
| 157 |
struct st_ssl_backend_data { |
| 158 |
SSLContextRef ssl_ctx; |
| 159 |
bool ssl_direction; /* true if writing, false if reading */ |
| 160 |
size_t ssl_write_buffered_length; |
| 161 |
BIT(sent_shutdown); |
| 162 |
}; |
| 163 |
|
| 164 |
/* Create the list of default ciphers to use by making an intersection of the |
| 165 |
* ciphers supported by Secure Transport and the list below, using the order |
| 166 |
* of the former. |
| 167 |
* This list is based on TLS recommendations by Mozilla, balancing between |
| 168 |
* security and wide compatibility: "Most ciphers that are not clearly broken |
| 169 |
* and dangerous to use are supported" |
| 170 |
*/ |
| 171 |
static const uint16_t default_ciphers[] = { |
| 172 |
TLS_RSA_WITH_3DES_EDE_CBC_SHA, /* 0x000A */ |
| 173 |
TLS_RSA_WITH_AES_128_CBC_SHA, /* 0x002F */ |
| 174 |
TLS_RSA_WITH_AES_256_CBC_SHA, /* 0x0035 */ |
| 175 |
|
| 176 |
#if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS |
| 177 |
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC009 */ |
| 178 |
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC00A */ |
| 179 |
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, /* 0xC013 */ |
| 180 |
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, /* 0xC014 */ |
| 181 |
#endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */ |
| 182 |
|
| 183 |
#if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS |
| 184 |
TLS_RSA_WITH_AES_128_CBC_SHA256, /* 0x003C */ |
| 185 |
TLS_RSA_WITH_AES_256_CBC_SHA256, /* 0x003D */ |
| 186 |
TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, /* 0x0067 */ |
| 187 |
TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, /* 0x006B */ |
| 188 |
TLS_RSA_WITH_AES_128_GCM_SHA256, /* 0x009C */ |
| 189 |
TLS_RSA_WITH_AES_256_GCM_SHA384, /* 0x009D */ |
| 190 |
TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, /* 0x009E */ |
| 191 |
TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, /* 0x009F */ |
| 192 |
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC023 */ |
| 193 |
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC024 */ |
| 194 |
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, /* 0xC027 */ |
| 195 |
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, /* 0xC028 */ |
| 196 |
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02B */ |
| 197 |
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02C */ |
| 198 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* 0xC02F */ |
| 199 |
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, /* 0xC030 */ |
| 200 |
#endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */ |
| 201 |
|
| 202 |
#if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 |
| 203 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA8 */ |
| 204 |
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA9 */ |
| 205 |
|
| 206 |
/* TLSv1.3 is not supported by Secure Transport, but there is also other |
| 207 |
* code referencing TLSv1.3, like: kTLSProtocol13 ? */ |
| 208 |
TLS_AES_128_GCM_SHA256, /* 0x1301 */ |
| 209 |
TLS_AES_256_GCM_SHA384, /* 0x1302 */ |
| 210 |
TLS_CHACHA20_POLY1305_SHA256, /* 0x1303 */ |
| 211 |
#endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */ |
| 212 |
}; |
| 213 |
|
| 214 |
/* pinned public key support tests */ |
| 215 |
|
| 216 |
/* version 1 supports macOS 10.12+ and iOS 10+ */ |
| 217 |
#if ((TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || \ |
| 218 |
(!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200)) |
| 219 |
#define SECTRANSP_PINNEDPUBKEY_V1 1 |
| 220 |
#endif |
| 221 |
|
| 222 |
/* version 2 supports macOS 10.7+ */ |
| 223 |
#if (!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070) |
| 224 |
#define SECTRANSP_PINNEDPUBKEY_V2 1 |
| 225 |
#endif |
| 226 |
|
| 227 |
#if defined(SECTRANSP_PINNEDPUBKEY_V1) || defined(SECTRANSP_PINNEDPUBKEY_V2) |
| 228 |
/* this backend supports CURLOPT_PINNEDPUBLICKEY */ |
| 229 |
#define SECTRANSP_PINNEDPUBKEY 1 |
| 230 |
#endif /* SECTRANSP_PINNEDPUBKEY */ |
| 231 |
|
| 232 |
#ifdef SECTRANSP_PINNEDPUBKEY |
| 233 |
/* both new and old APIs return rsa keys missing the spki header (not DER) */ |
| 234 |
static const unsigned char rsa4096SpkiHeader[] = { |
| 235 |
0x30, 0x82, 0x02, 0x22, 0x30, 0x0d, |
| 236 |
0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 237 |
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, |
| 238 |
0x00, 0x03, 0x82, 0x02, 0x0f, 0x00}; |
| 239 |
|
| 240 |
static const unsigned char rsa2048SpkiHeader[] = { |
| 241 |
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, |
| 242 |
0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 243 |
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, |
| 244 |
0x00, 0x03, 0x82, 0x01, 0x0f, 0x00}; |
| 245 |
#ifdef SECTRANSP_PINNEDPUBKEY_V1 |
| 246 |
/* the *new* version does not return DER encoded ecdsa certs like the old... */ |
| 247 |
static const unsigned char ecDsaSecp256r1SpkiHeader[] = { |
| 248 |
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, |
| 249 |
0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, |
| 250 |
0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, |
| 251 |
0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
| 252 |
0x42, 0x00}; |
| 253 |
|
| 254 |
static const unsigned char ecDsaSecp384r1SpkiHeader[] = { |
| 255 |
0x30, 0x76, 0x30, 0x10, 0x06, 0x07, |
| 256 |
0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, |
| 257 |
0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, |
| 258 |
0x00, 0x22, 0x03, 0x62, 0x00}; |
| 259 |
#endif /* SECTRANSP_PINNEDPUBKEY_V1 */ |
| 260 |
#endif /* SECTRANSP_PINNEDPUBKEY */ |
| 261 |
|
| 262 |
static OSStatus sectransp_bio_cf_in_read(SSLConnectionRef connection, |
| 263 |
void *buf, |
| 264 |
size_t *dataLength) /* IN/OUT */ |
| 265 |
{ |
| 266 |
const struct Curl_cfilter *cf = (const struct Curl_cfilter *)connection; |
| 267 |
struct ssl_connect_data *connssl = cf->ctx; |
| 268 |
struct st_ssl_backend_data *backend = |
| 269 |
(struct st_ssl_backend_data *)connssl->backend; |
| 270 |
struct Curl_easy *data = CF_DATA_CURRENT(cf); |
| 271 |
ssize_t nread; |
| 272 |
CURLcode result; |
| 273 |
OSStatus rtn = noErr; |
| 274 |
|
| 275 |
DEBUGASSERT(data); |
| 276 |
nread = Curl_conn_cf_recv(cf->next, data, buf, *dataLength, &result); |
| 277 |
CURL_TRC_CF(data, cf, "bio_read(len=%zu) -> %zd, result=%d", |
| 278 |
*dataLength, nread, result); |
| 279 |
if(nread < 0) { |
| 280 |
switch(result) { |
| 281 |
case CURLE_OK: |
| 282 |
case CURLE_AGAIN: |
| 283 |
rtn = errSSLWouldBlock; |
| 284 |
backend->ssl_direction = FALSE; |
| 285 |
break; |
| 286 |
default: |
| 287 |
rtn = ioErr; |
| 288 |
break; |
| 289 |
} |
| 290 |
nread = 0; |
| 291 |
} |
| 292 |
else if(nread == 0) { |
| 293 |
rtn = errSSLClosedGraceful; |
| 294 |
} |
| 295 |
else if((size_t)nread < *dataLength) { |
| 296 |
rtn = errSSLWouldBlock; |
| 297 |
} |
| 298 |
*dataLength = nread; |
| 299 |
return rtn; |
| 300 |
} |
| 301 |
|
| 302 |
static OSStatus sectransp_bio_cf_out_write(SSLConnectionRef connection, |
| 303 |
const void *buf, |
| 304 |
size_t *dataLength) /* IN/OUT */ |
| 305 |
{ |
| 306 |
const struct Curl_cfilter *cf = (const struct Curl_cfilter *)connection; |
| 307 |
struct ssl_connect_data *connssl = cf->ctx; |
| 308 |
struct st_ssl_backend_data *backend = |
| 309 |
(struct st_ssl_backend_data *)connssl->backend; |
| 310 |
struct Curl_easy *data = CF_DATA_CURRENT(cf); |
| 311 |
ssize_t nwritten; |
| 312 |
CURLcode result; |
| 313 |
OSStatus rtn = noErr; |
| 314 |
|
| 315 |
DEBUGASSERT(data); |
| 316 |
nwritten = Curl_conn_cf_send(cf->next, data, buf, *dataLength, FALSE, |
| 317 |
&result); |
| 318 |
CURL_TRC_CF(data, cf, "bio_send(len=%zu) -> %zd, result=%d", |
| 319 |
*dataLength, nwritten, result); |
| 320 |
if(nwritten <= 0) { |
| 321 |
if(result == CURLE_AGAIN) { |
| 322 |
rtn = errSSLWouldBlock; |
| 323 |
backend->ssl_direction = TRUE; |
| 324 |
} |
| 325 |
else { |
| 326 |
rtn = ioErr; |
| 327 |
} |
| 328 |
nwritten = 0; |
| 329 |
} |
| 330 |
else if((size_t)nwritten < *dataLength) { |
| 331 |
rtn = errSSLWouldBlock; |
| 332 |
} |
| 333 |
*dataLength = nwritten; |
| 334 |
return rtn; |
| 335 |
} |
| 336 |
|
| 337 |
#if CURL_BUILD_MAC |
| 338 |
CF_INLINE void GetDarwinVersionNumber(int *major, int *minor) |
| 339 |
{ |
| 340 |
int mib[2]; |
| 341 |
size_t os_version_len; |
| 342 |
char buf[256]; |
| 343 |
|
| 344 |
/* Get the Darwin kernel version from the kernel using sysctl(): */ |
| 345 |
mib[0] = CTL_KERN; |
| 346 |
mib[1] = KERN_OSRELEASE; |
| 347 |
if(sysctl(mib, 2, NULL, &os_version_len, NULL, 0) == -1) |
| 348 |
return; |
| 349 |
if(os_version_len < sizeof(buf)) { |
| 350 |
if(sysctl(mib, 2, buf, &os_version_len, NULL, 0) != -1) { |
| 351 |
const char *os = buf; |
| 352 |
curl_off_t fnum; |
| 353 |
curl_off_t snum; |
| 354 |
/* Parse the version: */ |
| 355 |
if(!curlx_str_number(&os, &fnum, INT_MAX) && |
| 356 |
!curlx_str_single(&os, '.') && |
| 357 |
!curlx_str_number(&os, &snum, INT_MAX)) { |
| 358 |
*major = (int)fnum; |
| 359 |
*minor = (int)snum; |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
#endif /* CURL_BUILD_MAC */ |
| 365 |
|
| 366 |
/* Apple provides a myriad of ways of getting information about a certificate |
| 367 |
into a string. Some are not available under iOS or newer cats. Here's a |
| 368 |
unified function for getting a string describing the certificate that ought |
| 369 |
to work in all cats starting with Leopard. */ |
| 370 |
CF_INLINE CFStringRef getsubject(SecCertificateRef cert) |
| 371 |
{ |
| 372 |
CFStringRef server_cert_summary = CFSTR("(null)"); |
| 373 |
|
| 374 |
#if CURL_BUILD_IOS |
| 375 |
/* iOS: There is only one way to do this. */ |
| 376 |
server_cert_summary = SecCertificateCopySubjectSummary(cert); |
| 377 |
#else |
| 378 |
#if CURL_BUILD_MAC_10_7 |
| 379 |
/* Lion & later: Get the long description if we can. */ |
| 380 |
if(&SecCertificateCopyLongDescription) |
| 381 |
server_cert_summary = |
| 382 |
SecCertificateCopyLongDescription(NULL, cert, NULL); |
| 383 |
else |
| 384 |
#endif /* CURL_BUILD_MAC_10_7 */ |
| 385 |
#if CURL_BUILD_MAC_10_6 |
| 386 |
/* Snow Leopard: Get the certificate summary. */ |
| 387 |
if(&SecCertificateCopySubjectSummary) |
| 388 |
server_cert_summary = SecCertificateCopySubjectSummary(cert); |
| 389 |
else |
| 390 |
#endif /* CURL_BUILD_MAC_10_6 */ |
| 391 |
/* Leopard is as far back as we go... */ |
| 392 |
(void)SecCertificateCopyCommonName(cert, &server_cert_summary); |
| 393 |
#endif /* CURL_BUILD_IOS */ |
| 394 |
return server_cert_summary; |
| 395 |
} |
| 396 |
|
| 397 |
static CURLcode CopyCertSubject(struct Curl_easy *data, |
| 398 |
SecCertificateRef cert, char **certp) |
| 399 |
{ |
| 400 |
CFStringRef c = getsubject(cert); |
| 401 |
CURLcode result = CURLE_OK; |
| 402 |
const char *direct; |
| 403 |
char *cbuf = NULL; |
| 404 |
*certp = NULL; |
| 405 |
|
| 406 |
if(!c) { |
| 407 |
failf(data, "SSL: invalid CA certificate subject"); |
| 408 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 409 |
} |
| 410 |
|
| 411 |
/* If the subject is already available as UTF-8 encoded (ie 'direct') then |
| 412 |
use that, else convert it. */ |
| 413 |
direct = CFStringGetCStringPtr(c, kCFStringEncodingUTF8); |
| 414 |
if(direct) { |
| 415 |
*certp = strdup(direct); |
| 416 |
if(!*certp) { |
| 417 |
failf(data, "SSL: out of memory"); |
| 418 |
result = CURLE_OUT_OF_MEMORY; |
| 419 |
} |
| 420 |
} |
| 421 |
else { |
| 422 |
size_t cbuf_size = ((size_t)CFStringGetLength(c) * 4) + 1; |
| 423 |
cbuf = calloc(1, cbuf_size); |
| 424 |
if(cbuf) { |
| 425 |
if(!CFStringGetCString(c, cbuf, (CFIndex)cbuf_size, |
| 426 |
kCFStringEncodingUTF8)) { |
| 427 |
failf(data, "SSL: invalid CA certificate subject"); |
| 428 |
result = CURLE_PEER_FAILED_VERIFICATION; |
| 429 |
} |
| 430 |
else |
| 431 |
/* pass back the buffer */ |
| 432 |
*certp = cbuf; |
| 433 |
} |
| 434 |
else { |
| 435 |
failf(data, "SSL: could not allocate %zu bytes of memory", cbuf_size); |
| 436 |
result = CURLE_OUT_OF_MEMORY; |
| 437 |
} |
| 438 |
} |
| 439 |
if(result) |
| 440 |
free(cbuf); |
| 441 |
CFRelease(c); |
| 442 |
return result; |
| 443 |
} |
| 444 |
|
| 445 |
#if CURL_SUPPORT_MAC_10_6 |
| 446 |
/* The SecKeychainSearch API was deprecated in Lion, and using it will raise |
| 447 |
deprecation warnings, so let's not compile this unless it is necessary: */ |
| 448 |
static OSStatus CopyIdentityWithLabelOldSchool(char *label, |
| 449 |
SecIdentityRef *out_c_a_k) |
| 450 |
{ |
| 451 |
OSStatus status = errSecItemNotFound; |
| 452 |
SecKeychainAttributeList attr_list; |
| 453 |
SecKeychainAttribute attr; |
| 454 |
SecKeychainSearchRef search = NULL; |
| 455 |
SecCertificateRef cert = NULL; |
| 456 |
|
| 457 |
/* Set up the attribute list: */ |
| 458 |
attr_list.count = 1L; |
| 459 |
attr_list.attr = &attr; |
| 460 |
|
| 461 |
/* Set up our lone search criterion: */ |
| 462 |
attr.tag = kSecLabelItemAttr; |
| 463 |
attr.data = label; |
| 464 |
attr.length = (UInt32)strlen(label); |
| 465 |
|
| 466 |
/* Start searching: */ |
| 467 |
status = SecKeychainSearchCreateFromAttributes(NULL, |
| 468 |
kSecCertificateItemClass, |
| 469 |
&attr_list, |
| 470 |
&search); |
| 471 |
if(status == noErr) { |
| 472 |
status = SecKeychainSearchCopyNext(search, |
| 473 |
(SecKeychainItemRef *)&cert); |
| 474 |
if(status == noErr && cert) { |
| 475 |
/* If we found a certificate, does it have a private key? */ |
| 476 |
status = SecIdentityCreateWithCertificate(NULL, cert, out_c_a_k); |
| 477 |
CFRelease(cert); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
if(search) |
| 482 |
CFRelease(search); |
| 483 |
return status; |
| 484 |
} |
| 485 |
#endif /* CURL_SUPPORT_MAC_10_6 */ |
| 486 |
|
| 487 |
static OSStatus CopyIdentityWithLabel(char *label, |
| 488 |
SecIdentityRef *out_cert_and_key) |
| 489 |
{ |
| 490 |
OSStatus status = errSecItemNotFound; |
| 491 |
|
| 492 |
#if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS |
| 493 |
CFArrayRef keys_list; |
| 494 |
CFIndex keys_list_count; |
| 495 |
CFIndex i; |
| 496 |
|
| 497 |
/* SecItemCopyMatching() was introduced in iOS and Snow Leopard. |
| 498 |
kSecClassIdentity was introduced in Lion. If both exist, let's use them |
| 499 |
to find the certificate. */ |
| 500 |
if(&SecItemCopyMatching && kSecClassIdentity) { |
| 501 |
CFTypeRef keys[5]; |
| 502 |
CFTypeRef values[5]; |
| 503 |
CFDictionaryRef query_dict; |
| 504 |
CFStringRef label_cf = CFStringCreateWithCString(NULL, label, |
| 505 |
kCFStringEncodingUTF8); |
| 506 |
|
| 507 |
/* Set up our search criteria and expected results: */ |
| 508 |
values[0] = kSecClassIdentity; /* we want a certificate and a key */ |
| 509 |
keys[0] = kSecClass; |
| 510 |
values[1] = kCFBooleanTrue; /* we want a reference */ |
| 511 |
keys[1] = kSecReturnRef; |
| 512 |
values[2] = kSecMatchLimitAll; /* kSecMatchLimitOne would be better if the |
| 513 |
* label matching below worked correctly */ |
| 514 |
keys[2] = kSecMatchLimit; |
| 515 |
/* identity searches need a SecPolicyRef in order to work */ |
| 516 |
values[3] = SecPolicyCreateSSL(FALSE, NULL); |
| 517 |
keys[3] = kSecMatchPolicy; |
| 518 |
/* match the name of the certificate (does not work in macOS 10.12.1) */ |
| 519 |
values[4] = label_cf; |
| 520 |
keys[4] = kSecAttrLabel; |
| 521 |
query_dict = CFDictionaryCreate(NULL, (const void **)keys, |
| 522 |
(const void **)values, 5L, |
| 523 |
&kCFCopyStringDictionaryKeyCallBacks, |
| 524 |
&kCFTypeDictionaryValueCallBacks); |
| 525 |
CFRelease(values[3]); |
| 526 |
|
| 527 |
/* Do we have a match? */ |
| 528 |
status = SecItemCopyMatching(query_dict, (CFTypeRef *) &keys_list); |
| 529 |
|
| 530 |
/* Because kSecAttrLabel matching does not work with kSecClassIdentity, |
| 531 |
* we need to find the correct identity ourselves */ |
| 532 |
if(status == noErr) { |
| 533 |
keys_list_count = CFArrayGetCount(keys_list); |
| 534 |
*out_cert_and_key = NULL; |
| 535 |
status = 1; |
| 536 |
for(i = 0; i < keys_list_count; i++) { |
| 537 |
OSStatus err = noErr; |
| 538 |
SecCertificateRef cert = NULL; |
| 539 |
const void *item = CFArrayGetValueAtIndex(keys_list, i); |
| 540 |
SecIdentityRef identity = (SecIdentityRef)CURL_UNCONST(item); |
| 541 |
err = SecIdentityCopyCertificate(identity, &cert); |
| 542 |
if(err == noErr) { |
| 543 |
CFStringRef common_name = NULL; |
| 544 |
OSStatus copy_status = noErr; |
| 545 |
#if CURL_BUILD_IOS |
| 546 |
common_name = SecCertificateCopySubjectSummary(cert); |
| 547 |
#elif CURL_BUILD_MAC_10_7 |
| 548 |
copy_status = SecCertificateCopyCommonName(cert, &common_name); |
| 549 |
#endif |
| 550 |
if(copy_status == noErr && |
| 551 |
CFStringCompare(common_name, label_cf, 0) == kCFCompareEqualTo) { |
| 552 |
CFRelease(cert); |
| 553 |
CFRelease(common_name); |
| 554 |
CFRetain(identity); |
| 555 |
*out_cert_and_key = identity; |
| 556 |
status = noErr; |
| 557 |
break; |
| 558 |
} |
| 559 |
if(common_name) |
| 560 |
CFRelease(common_name); |
| 561 |
} |
| 562 |
CFRelease(cert); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
if(keys_list) |
| 567 |
CFRelease(keys_list); |
| 568 |
CFRelease(query_dict); |
| 569 |
CFRelease(label_cf); |
| 570 |
} |
| 571 |
else { |
| 572 |
#if CURL_SUPPORT_MAC_10_6 |
| 573 |
/* On Leopard and Snow Leopard, fall back to SecKeychainSearch. */ |
| 574 |
status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key); |
| 575 |
#endif /* CURL_SUPPORT_MAC_10_6 */ |
| 576 |
} |
| 577 |
#elif CURL_SUPPORT_MAC_10_6 |
| 578 |
/* For developers building on older cats, we have no choice but to fall back |
| 579 |
to SecKeychainSearch. */ |
| 580 |
status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key); |
| 581 |
#endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */ |
| 582 |
return status; |
| 583 |
} |
| 584 |
|
| 585 |
static OSStatus CopyIdentityFromPKCS12File(const char *cPath, |
| 586 |
const struct curl_blob *blob, |
| 587 |
const char *cPassword, |
| 588 |
SecIdentityRef *out_cert_and_key) |
| 589 |
{ |
| 590 |
OSStatus status = errSecItemNotFound; |
| 591 |
CFURLRef pkcs_url = NULL; |
| 592 |
CFStringRef password = cPassword ? CFStringCreateWithCString(NULL, |
| 593 |
cPassword, kCFStringEncodingUTF8) : NULL; |
| 594 |
CFDataRef pkcs_data = NULL; |
| 595 |
|
| 596 |
/* We can import P12 files on iOS or macOS 10.7 or later: */ |
| 597 |
/* These constants are documented as having first appeared in 10.6 but they |
| 598 |
raise linker errors when used on that cat for some reason. */ |
| 599 |
#if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS |
| 600 |
bool resource_imported; |
| 601 |
|
| 602 |
if(blob) { |
| 603 |
pkcs_data = CFDataCreate(kCFAllocatorDefault, |
| 604 |
(const unsigned char *)blob->data, |
| 605 |
(CFIndex)blob->len); |
| 606 |
status = (pkcs_data != NULL) ? errSecSuccess : errSecAllocate; |
| 607 |
resource_imported = (pkcs_data != NULL); |
| 608 |
} |
| 609 |
else { |
| 610 |
pkcs_url = |
| 611 |
CFURLCreateFromFileSystemRepresentation(NULL, |
| 612 |
(const UInt8 *)cPath, |
| 613 |
(CFIndex)strlen(cPath), FALSE); |
| 614 |
resource_imported = |
| 615 |
CFURLCreateDataAndPropertiesFromResource(NULL, |
| 616 |
pkcs_url, &pkcs_data, |
| 617 |
NULL, NULL, &status); |
| 618 |
} |
| 619 |
|
| 620 |
if(resource_imported) { |
| 621 |
CFArrayRef items = NULL; |
| 622 |
|
| 623 |
/* On iOS SecPKCS12Import will never add the client certificate to the |
| 624 |
* Keychain. |
| 625 |
* |
| 626 |
* It gives us back a SecIdentityRef that we can use directly. */ |
| 627 |
#if CURL_BUILD_IOS |
| 628 |
const void *cKeys[] = {kSecImportExportPassphrase}; |
| 629 |
const void *cValues[] = {password}; |
| 630 |
CFDictionaryRef options = CFDictionaryCreate(NULL, cKeys, cValues, |
| 631 |
password ? 1L : 0L, NULL, NULL); |
| 632 |
|
| 633 |
if(options) { |
| 634 |
status = SecPKCS12Import(pkcs_data, options, &items); |
| 635 |
CFRelease(options); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
/* On macOS SecPKCS12Import will always add the client certificate to |
| 640 |
* the Keychain. |
| 641 |
* |
| 642 |
* As this does not match iOS, and apps may not want to see their client |
| 643 |
* certificate saved in the user's keychain, we use SecItemImport |
| 644 |
* with a NULL keychain to avoid importing it. |
| 645 |
* |
| 646 |
* This returns a SecCertificateRef from which we can construct a |
| 647 |
* SecIdentityRef. |
| 648 |
*/ |
| 649 |
#elif CURL_BUILD_MAC_10_7 |
| 650 |
SecItemImportExportKeyParameters keyParams; |
| 651 |
SecExternalFormat inputFormat = kSecFormatPKCS12; |
| 652 |
SecExternalItemType inputType = kSecItemTypeCertificate; |
| 653 |
|
| 654 |
memset(&keyParams, 0x00, sizeof(keyParams)); |
| 655 |
keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION; |
| 656 |
keyParams.passphrase = password; |
| 657 |
|
| 658 |
status = SecItemImport(pkcs_data, NULL, &inputFormat, &inputType, |
| 659 |
0, &keyParams, NULL, &items); |
| 660 |
#endif |
| 661 |
|
| 662 |
|
| 663 |
/* Extract the SecIdentityRef */ |
| 664 |
if(status == errSecSuccess && items && CFArrayGetCount(items)) { |
| 665 |
CFIndex i, count; |
| 666 |
count = CFArrayGetCount(items); |
| 667 |
|
| 668 |
for(i = 0; i < count; i++) { |
| 669 |
const CFTypeRef item = CFArrayGetValueAtIndex(items, i); |
| 670 |
CFTypeID itemID = CFGetTypeID(item); |
| 671 |
|
| 672 |
if(itemID == CFDictionaryGetTypeID()) { |
| 673 |
const CFTypeRef identity = CFDictionaryGetValue( |
| 674 |
(CFDictionaryRef)item, |
| 675 |
kSecImportItemIdentity); |
| 676 |
CFRetain(identity); |
| 677 |
*out_cert_and_key = (SecIdentityRef)CURL_UNCONST(identity); |
| 678 |
break; |
| 679 |
} |
| 680 |
#if CURL_BUILD_MAC_10_7 |
| 681 |
else if(itemID == SecCertificateGetTypeID()) { |
| 682 |
status = SecIdentityCreateWithCertificate(NULL, |
| 683 |
(SecCertificateRef)CURL_UNCONST(item), |
| 684 |
out_cert_and_key); |
| 685 |
break; |
| 686 |
} |
| 687 |
#endif |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
if(items) |
| 692 |
CFRelease(items); |
| 693 |
CFRelease(pkcs_data); |
| 694 |
} |
| 695 |
#endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */ |
| 696 |
if(password) |
| 697 |
CFRelease(password); |
| 698 |
if(pkcs_url) |
| 699 |
CFRelease(pkcs_url); |
| 700 |
return status; |
| 701 |
} |
| 702 |
|
| 703 |
/* This code was borrowed from nss.c, with some modifications: |
| 704 |
* Determine whether the nickname passed in is a filename that needs to |
| 705 |
* be loaded as a PEM or a nickname. |
| 706 |
* |
| 707 |
* returns 1 for a file |
| 708 |
* returns 0 for not a file |
| 709 |
*/ |
| 710 |
CF_INLINE bool is_file(const char *filename) |
| 711 |
{ |
| 712 |
struct_stat st; |
| 713 |
|
| 714 |
if(!filename) |
| 715 |
return FALSE; |
| 716 |
|
| 717 |
if(stat(filename, &st) == 0) |
| 718 |
return S_ISREG(st.st_mode); |
| 719 |
return FALSE; |
| 720 |
} |
| 721 |
|
| 722 |
static CURLcode |
| 723 |
sectransp_set_ssl_version_min_max(struct Curl_easy *data, |
| 724 |
struct st_ssl_backend_data *backend, |
| 725 |
struct ssl_primary_config *conn_config) |
| 726 |
{ |
| 727 |
#if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS |
| 728 |
OSStatus err; |
| 729 |
SSLProtocol ver_min; |
| 730 |
SSLProtocol ver_max; |
| 731 |
|
| 732 |
#if CURL_SUPPORT_MAC_10_7 |
| 733 |
if(!&SSLSetProtocolVersionMax) |
| 734 |
goto legacy; |
| 735 |
#endif |
| 736 |
|
| 737 |
switch(conn_config->version) { |
| 738 |
case CURL_SSLVERSION_DEFAULT: |
| 739 |
case CURL_SSLVERSION_TLSv1: |
| 740 |
case CURL_SSLVERSION_TLSv1_0: |
| 741 |
ver_min = kTLSProtocol1; |
| 742 |
break; |
| 743 |
case CURL_SSLVERSION_TLSv1_1: |
| 744 |
ver_min = kTLSProtocol11; |
| 745 |
break; |
| 746 |
case CURL_SSLVERSION_TLSv1_2: |
| 747 |
ver_min = kTLSProtocol12; |
| 748 |
break; |
| 749 |
case CURL_SSLVERSION_TLSv1_3: |
| 750 |
default: |
| 751 |
failf(data, "SSL: unsupported minimum TLS version value"); |
| 752 |
return CURLE_SSL_CONNECT_ERROR; |
| 753 |
} |
| 754 |
|
| 755 |
switch(conn_config->version_max) { |
| 756 |
case CURL_SSLVERSION_MAX_DEFAULT: |
| 757 |
case CURL_SSLVERSION_MAX_NONE: |
| 758 |
case CURL_SSLVERSION_MAX_TLSv1_3: |
| 759 |
case CURL_SSLVERSION_MAX_TLSv1_2: |
| 760 |
ver_max = kTLSProtocol12; |
| 761 |
break; |
| 762 |
case CURL_SSLVERSION_MAX_TLSv1_1: |
| 763 |
ver_max = kTLSProtocol11; |
| 764 |
break; |
| 765 |
case CURL_SSLVERSION_MAX_TLSv1_0: |
| 766 |
ver_max = kTLSProtocol1; |
| 767 |
break; |
| 768 |
default: |
| 769 |
failf(data, "SSL: unsupported maximum TLS version value"); |
| 770 |
return CURLE_SSL_CONNECT_ERROR; |
| 771 |
} |
| 772 |
|
| 773 |
err = SSLSetProtocolVersionMin(backend->ssl_ctx, ver_min); |
| 774 |
if(err != noErr) { |
| 775 |
failf(data, "SSL: failed to set minimum TLS version"); |
| 776 |
return CURLE_SSL_CONNECT_ERROR; |
| 777 |
} |
| 778 |
err = SSLSetProtocolVersionMax(backend->ssl_ctx, ver_max); |
| 779 |
if(err != noErr) { |
| 780 |
failf(data, "SSL: failed to set maximum TLS version"); |
| 781 |
return CURLE_SSL_CONNECT_ERROR; |
| 782 |
} |
| 783 |
|
| 784 |
return CURLE_OK; |
| 785 |
#endif |
| 786 |
#if CURL_SUPPORT_MAC_10_7 |
| 787 |
goto legacy; |
| 788 |
legacy: |
| 789 |
switch(conn_config->version) { |
| 790 |
case CURL_SSLVERSION_DEFAULT: |
| 791 |
case CURL_SSLVERSION_TLSv1: |
| 792 |
case CURL_SSLVERSION_TLSv1_0: |
| 793 |
break; |
| 794 |
default: |
| 795 |
failf(data, "SSL: unsupported minimum TLS version value"); |
| 796 |
return CURLE_SSL_CONNECT_ERROR; |
| 797 |
} |
| 798 |
|
| 799 |
/* only TLS 1.0 is supported, disable SSL 3.0 and SSL 2.0 */ |
| 800 |
SSLSetProtocolVersionEnabled(backend->ssl_ctx, kSSLProtocolAll, FALSE); |
| 801 |
SSLSetProtocolVersionEnabled(backend->ssl_ctx, kTLSProtocol1, TRUE); |
| 802 |
|
| 803 |
return CURLE_OK; |
| 804 |
#endif |
| 805 |
} |
| 806 |
|
| 807 |
static int sectransp_cipher_suite_get_str(uint16_t id, char *buf, |
| 808 |
size_t buf_size, bool prefer_rfc) |
| 809 |
{ |
| 810 |
/* are these fortezza suites even supported ? */ |
| 811 |
if(id == SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA) |
| 812 |
msnprintf(buf, buf_size, "%s", "SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA"); |
| 813 |
else if(id == SSL_FORTEZZA_DMS_WITH_NULL_SHA) |
| 814 |
msnprintf(buf, buf_size, "%s", "SSL_FORTEZZA_DMS_WITH_NULL_SHA"); |
| 815 |
/* can TLS_EMPTY_RENEGOTIATION_INFO_SCSV even be set ? */ |
| 816 |
else if(id == TLS_EMPTY_RENEGOTIATION_INFO_SCSV) |
| 817 |
msnprintf(buf, buf_size, "%s", "TLS_EMPTY_RENEGOTIATION_INFO_SCSV"); |
| 818 |
/* do we still need to support these SSL2-only ciphers ? */ |
| 819 |
else if(id == SSL_RSA_WITH_RC2_CBC_MD5) |
| 820 |
msnprintf(buf, buf_size, "%s", "SSL_RSA_WITH_RC2_CBC_MD5"); |
| 821 |
else if(id == SSL_RSA_WITH_IDEA_CBC_MD5) |
| 822 |
msnprintf(buf, buf_size, "%s", "SSL_RSA_WITH_IDEA_CBC_MD5"); |
| 823 |
else if(id == SSL_RSA_WITH_DES_CBC_MD5) |
| 824 |
msnprintf(buf, buf_size, "%s", "SSL_RSA_WITH_DES_CBC_MD5"); |
| 825 |
else if(id == SSL_RSA_WITH_3DES_EDE_CBC_MD5) |
| 826 |
msnprintf(buf, buf_size, "%s", "SSL_RSA_WITH_3DES_EDE_CBC_MD5"); |
| 827 |
else |
| 828 |
return Curl_cipher_suite_get_str(id, buf, buf_size, prefer_rfc); |
| 829 |
return 0; |
| 830 |
} |
| 831 |
|
| 832 |
static uint16_t sectransp_cipher_suite_walk_str(const char **str, |
| 833 |
const char **end) |
| 834 |
{ |
| 835 |
uint16_t id = Curl_cipher_suite_walk_str(str, end); |
| 836 |
size_t len = *end - *str; |
| 837 |
|
| 838 |
if(!id) { |
| 839 |
/* are these fortezza suites even supported ? */ |
| 840 |
if(strncasecompare("SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA", *str, len)) |
| 841 |
id = SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA; |
| 842 |
else if(strncasecompare("SSL_FORTEZZA_DMS_WITH_NULL_SHA", *str, len)) |
| 843 |
id = SSL_FORTEZZA_DMS_WITH_NULL_SHA; |
| 844 |
/* can TLS_EMPTY_RENEGOTIATION_INFO_SCSV even be set ? */ |
| 845 |
else if(strncasecompare("TLS_EMPTY_RENEGOTIATION_INFO_SCSV", *str, len)) |
| 846 |
id = TLS_EMPTY_RENEGOTIATION_INFO_SCSV; |
| 847 |
/* do we still need to support these SSL2-only ciphers ? */ |
| 848 |
else if(strncasecompare("SSL_RSA_WITH_RC2_CBC_MD5", *str, len)) |
| 849 |
id = SSL_RSA_WITH_RC2_CBC_MD5; |
| 850 |
else if(strncasecompare("SSL_RSA_WITH_IDEA_CBC_MD5", *str, len)) |
| 851 |
id = SSL_RSA_WITH_IDEA_CBC_MD5; |
| 852 |
else if(strncasecompare("SSL_RSA_WITH_DES_CBC_MD5", *str, len)) |
| 853 |
id = SSL_RSA_WITH_DES_CBC_MD5; |
| 854 |
else if(strncasecompare("SSL_RSA_WITH_3DES_EDE_CBC_MD5", *str, len)) |
| 855 |
id = SSL_RSA_WITH_3DES_EDE_CBC_MD5; |
| 856 |
} |
| 857 |
return id; |
| 858 |
} |
| 859 |
|
| 860 |
/* allocated memory must be freed */ |
| 861 |
static SSLCipherSuite * sectransp_get_supported_ciphers(SSLContextRef ssl_ctx, |
| 862 |
size_t *len) |
| 863 |
{ |
| 864 |
SSLCipherSuite *ciphers = NULL; |
| 865 |
OSStatus err = noErr; |
| 866 |
*len = 0; |
| 867 |
|
| 868 |
err = SSLGetNumberSupportedCiphers(ssl_ctx, len); |
| 869 |
if(err != noErr) |
| 870 |
goto failed; |
| 871 |
|
| 872 |
ciphers = malloc(*len * sizeof(SSLCipherSuite)); |
| 873 |
if(!ciphers) |
| 874 |
goto failed; |
| 875 |
|
| 876 |
err = SSLGetSupportedCiphers(ssl_ctx, ciphers, len); |
| 877 |
if(err != noErr) |
| 878 |
goto failed; |
| 879 |
|
| 880 |
#if CURL_BUILD_MAC |
| 881 |
{ |
| 882 |
int maj = 0, min = 0; |
| 883 |
GetDarwinVersionNumber(&maj, &min); |
| 884 |
/* There is a known bug in early versions of Mountain Lion where ST's ECC |
| 885 |
ciphers (cipher suite 0xC001 through 0xC032) simply do not work. |
| 886 |
Work around the problem here by disabling those ciphers if we are |
| 887 |
running in an affected version of macOS. */ |
| 888 |
if(maj == 12 && min <= 3) { |
| 889 |
size_t i = 0, j = 0; |
| 890 |
for(; i < *len; i++) { |
| 891 |
if(ciphers[i] >= 0xC001 && ciphers[i] <= 0xC032) |
| 892 |
continue; |
| 893 |
ciphers[j++] = ciphers[i]; |
| 894 |
} |
| 895 |
*len = j; |
| 896 |
} |
| 897 |
} |
| 898 |
#endif |
| 899 |
|
| 900 |
return ciphers; |
| 901 |
failed: |
| 902 |
*len = 0; |
| 903 |
Curl_safefree(ciphers); |
| 904 |
return NULL; |
| 905 |
} |
| 906 |
|
| 907 |
static CURLcode sectransp_set_default_ciphers(struct Curl_easy *data, |
| 908 |
SSLContextRef ssl_ctx) |
| 909 |
{ |
| 910 |
CURLcode ret = CURLE_SSL_CIPHER; |
| 911 |
size_t count = 0, i, j; |
| 912 |
OSStatus err; |
| 913 |
size_t supported_len; |
| 914 |
SSLCipherSuite *ciphers = NULL; |
| 915 |
|
| 916 |
ciphers = sectransp_get_supported_ciphers(ssl_ctx, &supported_len); |
| 917 |
if(!ciphers) { |
| 918 |
failf(data, "SSL: Failed to get supported ciphers"); |
| 919 |
goto failed; |
| 920 |
} |
| 921 |
|
| 922 |
/* Intersect the ciphers supported by Secure Transport with the default |
| 923 |
* ciphers, using the order of the former. */ |
| 924 |
for(i = 0; i < supported_len; i++) { |
| 925 |
for(j = 0; j < CURL_ARRAYSIZE(default_ciphers); j++) { |
| 926 |
if(default_ciphers[j] == ciphers[i]) { |
| 927 |
ciphers[count++] = ciphers[i]; |
| 928 |
break; |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
if(count == 0) { |
| 934 |
failf(data, "SSL: no supported default ciphers"); |
| 935 |
goto failed; |
| 936 |
} |
| 937 |
|
| 938 |
err = SSLSetEnabledCiphers(ssl_ctx, ciphers, count); |
| 939 |
if(err != noErr) { |
| 940 |
failf(data, "SSL: SSLSetEnabledCiphers() failed: OSStatus %d", err); |
| 941 |
goto failed; |
| 942 |
} |
| 943 |
|
| 944 |
ret = CURLE_OK; |
| 945 |
failed: |
| 946 |
Curl_safefree(ciphers); |
| 947 |
return ret; |
| 948 |
} |
| 949 |
|
| 950 |
static CURLcode sectransp_set_selected_ciphers(struct Curl_easy *data, |
| 951 |
SSLContextRef ssl_ctx, |
| 952 |
const char *ciphers) |
| 953 |
{ |
| 954 |
CURLcode ret = CURLE_SSL_CIPHER; |
| 955 |
size_t count = 0, i; |
| 956 |
const char *ptr, *end; |
| 957 |
OSStatus err; |
| 958 |
size_t supported_len; |
| 959 |
SSLCipherSuite *supported = NULL; |
| 960 |
SSLCipherSuite *selected = NULL; |
| 961 |
|
| 962 |
supported = sectransp_get_supported_ciphers(ssl_ctx, &supported_len); |
| 963 |
if(!supported) { |
| 964 |
failf(data, "SSL: Failed to get supported ciphers"); |
| 965 |
goto failed; |
| 966 |
} |
| 967 |
|
| 968 |
selected = malloc(supported_len * sizeof(SSLCipherSuite)); |
| 969 |
if(!selected) { |
| 970 |
failf(data, "SSL: Failed to allocate memory"); |
| 971 |
goto failed; |
| 972 |
} |
| 973 |
|
| 974 |
for(ptr = ciphers; ptr[0] != '\0' && count < supported_len; ptr = end) { |
| 975 |
uint16_t id = sectransp_cipher_suite_walk_str(&ptr, &end); |
| 976 |
|
| 977 |
/* Check if cipher is supported */ |
| 978 |
if(id) { |
| 979 |
for(i = 0; i < supported_len && supported[i] != id; i++); |
| 980 |
if(i == supported_len) |
| 981 |
id = 0; |
| 982 |
} |
| 983 |
if(!id) { |
| 984 |
if(ptr[0] != '\0') |
| 985 |
infof(data, "SSL: unknown cipher in list: \"%.*s\"", (int) (end - ptr), |
| 986 |
ptr); |
| 987 |
continue; |
| 988 |
} |
| 989 |
|
| 990 |
/* No duplicates allowed (so selected cannot overflow) */ |
| 991 |
for(i = 0; i < count && selected[i] != id; i++); |
| 992 |
if(i < count) { |
| 993 |
infof(data, "SSL: duplicate cipher in list: \"%.*s\"", (int) (end - ptr), |
| 994 |
ptr); |
| 995 |
continue; |
| 996 |
} |
| 997 |
|
| 998 |
selected[count++] = id; |
| 999 |
} |
| 1000 |
|
| 1001 |
if(count == 0) { |
| 1002 |
failf(data, "SSL: no supported cipher in list"); |
| 1003 |
goto failed; |
| 1004 |
} |
| 1005 |
|
| 1006 |
err = SSLSetEnabledCiphers(ssl_ctx, selected, count); |
| 1007 |
if(err != noErr) { |
| 1008 |
failf(data, "SSL: SSLSetEnabledCiphers() failed: OSStatus %d", err); |
| 1009 |
goto failed; |
| 1010 |
} |
| 1011 |
|
| 1012 |
ret = CURLE_OK; |
| 1013 |
failed: |
| 1014 |
Curl_safefree(supported); |
| 1015 |
Curl_safefree(selected); |
| 1016 |
return ret; |
| 1017 |
} |
| 1018 |
|
| 1019 |
static void sectransp_session_free(void *sessionid) |
| 1020 |
{ |
| 1021 |
/* ST, as of iOS 5 and Mountain Lion, has no public method of deleting a |
| 1022 |
cached session ID inside the Security framework. There is a private |
| 1023 |
function that does this, but I do not want to have to explain to you why I |
| 1024 |
got your application rejected from the App Store due to the use of a |
| 1025 |
private API, so the best we can do is free up our own char array that we |
| 1026 |
created way back in sectransp_connect_step1... */ |
| 1027 |
Curl_safefree(sessionid); |
| 1028 |
} |
| 1029 |
|
| 1030 |
static CURLcode sectransp_connect_step1(struct Curl_cfilter *cf, |
| 1031 |
struct Curl_easy *data) |
| 1032 |
{ |
| 1033 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1034 |
struct st_ssl_backend_data *backend = |
| 1035 |
(struct st_ssl_backend_data *)connssl->backend; |
| 1036 |
struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf); |
| 1037 |
struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data); |
| 1038 |
const struct curl_blob *ssl_cablob = conn_config->ca_info_blob; |
| 1039 |
const char * const ssl_cafile = |
| 1040 |
/* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */ |
| 1041 |
(ssl_cablob ? NULL : conn_config->CAfile); |
| 1042 |
const bool verifypeer = conn_config->verifypeer; |
| 1043 |
char * const ssl_cert = ssl_config->primary.clientcert; |
| 1044 |
const struct curl_blob *ssl_cert_blob = ssl_config->primary.cert_blob; |
| 1045 |
char *ciphers; |
| 1046 |
OSStatus err = noErr; |
| 1047 |
CURLcode result; |
| 1048 |
#if CURL_BUILD_MAC |
| 1049 |
int darwinver_maj = 0, darwinver_min = 0; |
| 1050 |
|
| 1051 |
DEBUGASSERT(backend); |
| 1052 |
|
| 1053 |
CURL_TRC_CF(data, cf, "connect_step1"); |
| 1054 |
GetDarwinVersionNumber(&darwinver_maj, &darwinver_min); |
| 1055 |
#endif /* CURL_BUILD_MAC */ |
| 1056 |
|
| 1057 |
#if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS |
| 1058 |
if(&SSLCreateContext) { /* use the newer API if available */ |
| 1059 |
if(backend->ssl_ctx) |
| 1060 |
CFRelease(backend->ssl_ctx); |
| 1061 |
backend->ssl_ctx = SSLCreateContext(NULL, kSSLClientSide, kSSLStreamType); |
| 1062 |
if(!backend->ssl_ctx) { |
| 1063 |
failf(data, "SSL: could not create a context"); |
| 1064 |
return CURLE_OUT_OF_MEMORY; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
else { |
| 1068 |
/* The old ST API does not exist under iOS, so do not compile it: */ |
| 1069 |
#if CURL_SUPPORT_MAC_10_8 |
| 1070 |
if(backend->ssl_ctx) |
| 1071 |
(void)SSLDisposeContext(backend->ssl_ctx); |
| 1072 |
err = SSLNewContext(FALSE, &(backend->ssl_ctx)); |
| 1073 |
if(err != noErr) { |
| 1074 |
failf(data, "SSL: could not create a context: OSStatus %d", err); |
| 1075 |
return CURLE_OUT_OF_MEMORY; |
| 1076 |
} |
| 1077 |
#endif /* CURL_SUPPORT_MAC_10_8 */ |
| 1078 |
} |
| 1079 |
#else |
| 1080 |
if(backend->ssl_ctx) |
| 1081 |
(void)SSLDisposeContext(backend->ssl_ctx); |
| 1082 |
err = SSLNewContext(FALSE, &(backend->ssl_ctx)); |
| 1083 |
if(err != noErr) { |
| 1084 |
failf(data, "SSL: could not create a context: OSStatus %d", err); |
| 1085 |
return CURLE_OUT_OF_MEMORY; |
| 1086 |
} |
| 1087 |
#endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */ |
| 1088 |
backend->ssl_write_buffered_length = 0UL; /* reset buffered write length */ |
| 1089 |
|
| 1090 |
result = sectransp_set_ssl_version_min_max(data, backend, conn_config); |
| 1091 |
if(result != CURLE_OK) |
| 1092 |
return result; |
| 1093 |
|
| 1094 |
if(connssl->alpn) { |
| 1095 |
#if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 |
| 1096 |
#ifdef HAVE_BUILTIN_AVAILABLE |
| 1097 |
if(__builtin_available(macOS 10.13.4, iOS 11, tvOS 11, *)) { |
| 1098 |
#else |
| 1099 |
if(&SSLSetALPNProtocols && &SSLCopyALPNProtocols) { |
| 1100 |
#endif |
| 1101 |
struct alpn_proto_buf proto; |
| 1102 |
size_t i; |
| 1103 |
CFStringRef cstr; |
| 1104 |
CFMutableArrayRef alpnArr = CFArrayCreateMutable(NULL, 0, |
| 1105 |
&kCFTypeArrayCallBacks); |
| 1106 |
for(i = 0; i < connssl->alpn->count; ++i) { |
| 1107 |
cstr = CFStringCreateWithCString(NULL, connssl->alpn->entries[i], |
| 1108 |
kCFStringEncodingUTF8); |
| 1109 |
if(!cstr) |
| 1110 |
return CURLE_OUT_OF_MEMORY; |
| 1111 |
CFArrayAppendValue(alpnArr, cstr); |
| 1112 |
CFRelease(cstr); |
| 1113 |
} |
| 1114 |
err = SSLSetALPNProtocols(backend->ssl_ctx, alpnArr); |
| 1115 |
if(err != noErr) |
| 1116 |
infof(data, "WARNING: failed to set ALPN protocols; OSStatus %d", |
| 1117 |
err); |
| 1118 |
CFRelease(alpnArr); |
| 1119 |
Curl_alpn_to_proto_str(&proto, connssl->alpn); |
| 1120 |
infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data); |
| 1121 |
} |
| 1122 |
#endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */ |
| 1123 |
} |
| 1124 |
|
| 1125 |
if(ssl_config->key) { |
| 1126 |
infof(data, "WARNING: SSL: CURLOPT_SSLKEY is ignored by Secure " |
| 1127 |
"Transport. The private key must be in the Keychain."); |
| 1128 |
} |
| 1129 |
|
| 1130 |
if(ssl_cert || ssl_cert_blob) { |
| 1131 |
bool is_cert_data = ssl_cert_blob != NULL; |
| 1132 |
bool is_cert_file = (!is_cert_data) && is_file(ssl_cert); |
| 1133 |
SecIdentityRef cert_and_key = NULL; |
| 1134 |
|
| 1135 |
/* User wants to authenticate with a client cert. Look for it. Assume that |
| 1136 |
the user wants to use an identity loaded from the Keychain. If not, try |
| 1137 |
it as a file on disk */ |
| 1138 |
|
| 1139 |
if(!is_cert_data) |
| 1140 |
err = CopyIdentityWithLabel(ssl_cert, &cert_and_key); |
| 1141 |
else |
| 1142 |
err = !noErr; |
| 1143 |
if((err != noErr) && (is_cert_file || is_cert_data)) { |
| 1144 |
if(!ssl_config->cert_type) |
| 1145 |
infof(data, "SSL: Certificate type not set, assuming " |
| 1146 |
"PKCS#12 format."); |
| 1147 |
else if(!strcasecompare(ssl_config->cert_type, "P12")) { |
| 1148 |
failf(data, "SSL: The Security framework only supports " |
| 1149 |
"loading identities that are in PKCS#12 format."); |
| 1150 |
return CURLE_SSL_CERTPROBLEM; |
| 1151 |
} |
| 1152 |
|
| 1153 |
err = CopyIdentityFromPKCS12File(ssl_cert, ssl_cert_blob, |
| 1154 |
ssl_config->key_passwd, |
| 1155 |
&cert_and_key); |
| 1156 |
} |
| 1157 |
|
| 1158 |
if(err == noErr && cert_and_key) { |
| 1159 |
SecCertificateRef cert = NULL; |
| 1160 |
CFTypeRef certs_c[1]; |
| 1161 |
CFArrayRef certs; |
| 1162 |
|
| 1163 |
/* If we found one, print it out: */ |
| 1164 |
err = SecIdentityCopyCertificate(cert_and_key, &cert); |
| 1165 |
if(err == noErr) { |
| 1166 |
char *certp; |
| 1167 |
result = CopyCertSubject(data, cert, &certp); |
| 1168 |
if(!result) { |
| 1169 |
infof(data, "Client certificate: %s", certp); |
| 1170 |
free(certp); |
| 1171 |
} |
| 1172 |
|
| 1173 |
CFRelease(cert); |
| 1174 |
if(result == CURLE_PEER_FAILED_VERIFICATION) |
| 1175 |
return CURLE_SSL_CERTPROBLEM; |
| 1176 |
if(result) |
| 1177 |
return result; |
| 1178 |
} |
| 1179 |
certs_c[0] = cert_and_key; |
| 1180 |
certs = CFArrayCreate(NULL, (const void **)certs_c, 1L, |
| 1181 |
&kCFTypeArrayCallBacks); |
| 1182 |
err = SSLSetCertificate(backend->ssl_ctx, certs); |
| 1183 |
if(certs) |
| 1184 |
CFRelease(certs); |
| 1185 |
if(err != noErr) { |
| 1186 |
failf(data, "SSL: SSLSetCertificate() failed: OSStatus %d", err); |
| 1187 |
return CURLE_SSL_CERTPROBLEM; |
| 1188 |
} |
| 1189 |
CFRelease(cert_and_key); |
| 1190 |
} |
| 1191 |
else { |
| 1192 |
const char *cert_showfilename_error = |
| 1193 |
is_cert_data ? "(memory blob)" : ssl_cert; |
| 1194 |
|
| 1195 |
switch(err) { |
| 1196 |
case errSecAuthFailed: case -25264: /* errSecPkcs12VerifyFailure */ |
| 1197 |
failf(data, "SSL: Incorrect password for the certificate \"%s\" " |
| 1198 |
"and its private key.", cert_showfilename_error); |
| 1199 |
break; |
| 1200 |
case -26275: /* errSecDecode */ case -25257: /* errSecUnknownFormat */ |
| 1201 |
failf(data, "SSL: Couldn't make sense of the data in the " |
| 1202 |
"certificate \"%s\" and its private key.", |
| 1203 |
cert_showfilename_error); |
| 1204 |
break; |
| 1205 |
case -25260: /* errSecPassphraseRequired */ |
| 1206 |
failf(data, "SSL The certificate \"%s\" requires a password.", |
| 1207 |
cert_showfilename_error); |
| 1208 |
break; |
| 1209 |
case errSecItemNotFound: |
| 1210 |
failf(data, "SSL: cannot find the certificate \"%s\" and its private " |
| 1211 |
"key in the Keychain.", cert_showfilename_error); |
| 1212 |
break; |
| 1213 |
default: |
| 1214 |
failf(data, "SSL: cannot load the certificate \"%s\" and its private " |
| 1215 |
"key: OSStatus %d", cert_showfilename_error, err); |
| 1216 |
break; |
| 1217 |
} |
| 1218 |
return CURLE_SSL_CERTPROBLEM; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* SSL always tries to verify the peer, this only says whether it should |
| 1223 |
* fail to connect if the verification fails, or if it should continue |
| 1224 |
* anyway. In the latter case the result of the verification is checked with |
| 1225 |
* SSL_get_verify_result() below. */ |
| 1226 |
#if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS |
| 1227 |
/* Snow Leopard introduced the SSLSetSessionOption() function, but due to |
| 1228 |
a library bug with the way the kSSLSessionOptionBreakOnServerAuth flag |
| 1229 |
works, it does not work as expected under Snow Leopard, Lion or |
| 1230 |
Mountain Lion. |
| 1231 |
So we need to call SSLSetEnableCertVerify() on those older cats in order |
| 1232 |
to disable certificate validation if the user turned that off. |
| 1233 |
(Secure Transport always validates the certificate chain by default.) |
| 1234 |
Note: |
| 1235 |
Darwin 11.x.x is Lion (10.7) |
| 1236 |
Darwin 12.x.x is Mountain Lion (10.8) |
| 1237 |
Darwin 13.x.x is Mavericks (10.9) |
| 1238 |
Darwin 14.x.x is Yosemite (10.10) |
| 1239 |
Darwin 15.x.x is El Capitan (10.11) |
| 1240 |
*/ |
| 1241 |
#if CURL_BUILD_MAC |
| 1242 |
if(&SSLSetSessionOption && darwinver_maj >= 13) { |
| 1243 |
#else |
| 1244 |
if(&SSLSetSessionOption) { |
| 1245 |
#endif /* CURL_BUILD_MAC */ |
| 1246 |
bool break_on_auth = !conn_config->verifypeer || |
| 1247 |
ssl_cafile || ssl_cablob; |
| 1248 |
err = SSLSetSessionOption(backend->ssl_ctx, |
| 1249 |
kSSLSessionOptionBreakOnServerAuth, |
| 1250 |
break_on_auth); |
| 1251 |
if(err != noErr) { |
| 1252 |
failf(data, "SSL: SSLSetSessionOption() failed: OSStatus %d", err); |
| 1253 |
return CURLE_SSL_CONNECT_ERROR; |
| 1254 |
} |
| 1255 |
} |
| 1256 |
else { |
| 1257 |
#if CURL_SUPPORT_MAC_10_8 |
| 1258 |
err = SSLSetEnableCertVerify(backend->ssl_ctx, |
| 1259 |
conn_config->verifypeer ? true : FALSE); |
| 1260 |
if(err != noErr) { |
| 1261 |
failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err); |
| 1262 |
return CURLE_SSL_CONNECT_ERROR; |
| 1263 |
} |
| 1264 |
#endif /* CURL_SUPPORT_MAC_10_8 */ |
| 1265 |
} |
| 1266 |
#else |
| 1267 |
err = SSLSetEnableCertVerify(backend->ssl_ctx, |
| 1268 |
conn_config->verifypeer ? true : FALSE); |
| 1269 |
if(err != noErr) { |
| 1270 |
failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err); |
| 1271 |
return CURLE_SSL_CONNECT_ERROR; |
| 1272 |
} |
| 1273 |
#endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */ |
| 1274 |
|
| 1275 |
if((ssl_cafile || ssl_cablob) && verifypeer) { |
| 1276 |
bool is_cert_data = ssl_cablob != NULL; |
| 1277 |
bool is_cert_file = (!is_cert_data) && is_file(ssl_cafile); |
| 1278 |
|
| 1279 |
if(!(is_cert_file || is_cert_data)) { |
| 1280 |
failf(data, "SSL: cannot load CA certificate file %s", |
| 1281 |
ssl_cafile ? ssl_cafile : "(blob memory)"); |
| 1282 |
return CURLE_SSL_CACERT_BADFILE; |
| 1283 |
} |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* Configure hostname check. SNI is used if available. |
| 1287 |
* Both hostname check and SNI require SSLSetPeerDomainName(). |
| 1288 |
* Also: the verifyhost setting influences SNI usage */ |
| 1289 |
if(conn_config->verifyhost) { |
| 1290 |
char *server = connssl->peer.sni ? |
| 1291 |
connssl->peer.sni : connssl->peer.hostname; |
| 1292 |
err = SSLSetPeerDomainName(backend->ssl_ctx, server, strlen(server)); |
| 1293 |
|
| 1294 |
if(err != noErr) { |
| 1295 |
failf(data, "SSL: SSLSetPeerDomainName() failed: OSStatus %d", |
| 1296 |
err); |
| 1297 |
return CURLE_SSL_CONNECT_ERROR; |
| 1298 |
} |
| 1299 |
|
| 1300 |
if(connssl->peer.type != CURL_SSL_PEER_DNS) { |
| 1301 |
infof(data, "WARNING: using IP address, SNI is being disabled by " |
| 1302 |
"the OS."); |
| 1303 |
} |
| 1304 |
} |
| 1305 |
else { |
| 1306 |
infof(data, "WARNING: disabling hostname validation also disables SNI."); |
| 1307 |
} |
| 1308 |
|
| 1309 |
ciphers = conn_config->cipher_list; |
| 1310 |
if(ciphers) { |
| 1311 |
result = sectransp_set_selected_ciphers(data, backend->ssl_ctx, ciphers); |
| 1312 |
} |
| 1313 |
else { |
| 1314 |
result = sectransp_set_default_ciphers(data, backend->ssl_ctx); |
| 1315 |
} |
| 1316 |
if(result != CURLE_OK) { |
| 1317 |
failf(data, "SSL: Unable to set ciphers for SSL/TLS handshake. " |
| 1318 |
"Error code: %d", (int)result); |
| 1319 |
return CURLE_SSL_CIPHER; |
| 1320 |
} |
| 1321 |
|
| 1322 |
#if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 |
| 1323 |
/* We want to enable 1/n-1 when using a CBC cipher unless the user |
| 1324 |
specifically does not want us doing that: */ |
| 1325 |
if(&SSLSetSessionOption) { |
| 1326 |
SSLSetSessionOption(backend->ssl_ctx, kSSLSessionOptionSendOneByteRecord, |
| 1327 |
!ssl_config->enable_beast); |
| 1328 |
SSLSetSessionOption(backend->ssl_ctx, kSSLSessionOptionFalseStart, |
| 1329 |
ssl_config->falsestart); /* false start support */ |
| 1330 |
} |
| 1331 |
#endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */ |
| 1332 |
|
| 1333 |
/* Check if there is a cached ID we can/should use here! */ |
| 1334 |
if(ssl_config->primary.cache_session) { |
| 1335 |
char *ssl_sessionid; |
| 1336 |
size_t ssl_sessionid_len; |
| 1337 |
|
| 1338 |
Curl_ssl_scache_lock(data); |
| 1339 |
ssl_sessionid = Curl_ssl_scache_get_obj(cf, data, |
| 1340 |
connssl->peer.scache_key); |
| 1341 |
if(ssl_sessionid) { |
| 1342 |
/* we got a session id, use it! */ |
| 1343 |
err = SSLSetPeerID(backend->ssl_ctx, ssl_sessionid, |
| 1344 |
strlen(ssl_sessionid)); |
| 1345 |
Curl_ssl_scache_unlock(data); |
| 1346 |
if(err != noErr) { |
| 1347 |
failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err); |
| 1348 |
return CURLE_SSL_CONNECT_ERROR; |
| 1349 |
} |
| 1350 |
else |
| 1351 |
infof(data, "SSL reusing session ID"); |
| 1352 |
} |
| 1353 |
/* If there is not one, then let's make one up! This has to be done prior |
| 1354 |
to starting the handshake. */ |
| 1355 |
else { |
| 1356 |
ssl_sessionid = |
| 1357 |
aprintf("%s:%d:%d:%s:%d", |
| 1358 |
ssl_cafile ? ssl_cafile : "(blob memory)", |
| 1359 |
verifypeer, conn_config->verifyhost, connssl->peer.hostname, |
| 1360 |
connssl->peer.port); |
| 1361 |
ssl_sessionid_len = strlen(ssl_sessionid); |
| 1362 |
|
| 1363 |
err = SSLSetPeerID(backend->ssl_ctx, ssl_sessionid, ssl_sessionid_len); |
| 1364 |
if(err != noErr) { |
| 1365 |
Curl_ssl_scache_unlock(data); |
| 1366 |
failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err); |
| 1367 |
return CURLE_SSL_CONNECT_ERROR; |
| 1368 |
} |
| 1369 |
|
| 1370 |
/* This is all a bit weird, as we have not handshaked yet. |
| 1371 |
* I hope this backend will go away soon. */ |
| 1372 |
result = Curl_ssl_scache_add_obj(cf, data, connssl->peer.scache_key, |
| 1373 |
(void *)ssl_sessionid, |
| 1374 |
sectransp_session_free); |
| 1375 |
Curl_ssl_scache_unlock(data); |
| 1376 |
if(result) |
| 1377 |
return result; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
err = SSLSetIOFuncs(backend->ssl_ctx, |
| 1382 |
sectransp_bio_cf_in_read, |
| 1383 |
sectransp_bio_cf_out_write); |
| 1384 |
if(err != noErr) { |
| 1385 |
failf(data, "SSL: SSLSetIOFuncs() failed: OSStatus %d", err); |
| 1386 |
return CURLE_SSL_CONNECT_ERROR; |
| 1387 |
} |
| 1388 |
|
| 1389 |
err = SSLSetConnection(backend->ssl_ctx, cf); |
| 1390 |
if(err != noErr) { |
| 1391 |
failf(data, "SSL: SSLSetConnection() failed: %d", err); |
| 1392 |
return CURLE_SSL_CONNECT_ERROR; |
| 1393 |
} |
| 1394 |
|
| 1395 |
connssl->connecting_state = ssl_connect_2; |
| 1396 |
return CURLE_OK; |
| 1397 |
} |
| 1398 |
|
| 1399 |
static long pem_to_der(const char *in, unsigned char **out, size_t *outlen) |
| 1400 |
{ |
| 1401 |
char *sep_start, *sep_end, *cert_start, *cert_end; |
| 1402 |
size_t i, j, err; |
| 1403 |
size_t len; |
| 1404 |
char *b64; |
| 1405 |
|
| 1406 |
/* Jump through the separators at the beginning of the certificate. */ |
| 1407 |
sep_start = strstr(in, "-----"); |
| 1408 |
if(!sep_start) |
| 1409 |
return 0; |
| 1410 |
cert_start = strstr(sep_start + 1, "-----"); |
| 1411 |
if(!cert_start) |
| 1412 |
return -1; |
| 1413 |
|
| 1414 |
cert_start += 5; |
| 1415 |
|
| 1416 |
/* Find separator after the end of the certificate. */ |
| 1417 |
cert_end = strstr(cert_start, "-----"); |
| 1418 |
if(!cert_end) |
| 1419 |
return -1; |
| 1420 |
|
| 1421 |
sep_end = strstr(cert_end + 1, "-----"); |
| 1422 |
if(!sep_end) |
| 1423 |
return -1; |
| 1424 |
sep_end += 5; |
| 1425 |
|
| 1426 |
len = cert_end - cert_start; |
| 1427 |
b64 = malloc(len + 1); |
| 1428 |
if(!b64) |
| 1429 |
return -1; |
| 1430 |
|
| 1431 |
/* Create base64 string without linefeeds. */ |
| 1432 |
for(i = 0, j = 0; i < len; i++) { |
| 1433 |
if(cert_start[i] != '\r' && cert_start[i] != '\n') |
| 1434 |
b64[j++] = cert_start[i]; |
| 1435 |
} |
| 1436 |
b64[j] = '\0'; |
| 1437 |
|
| 1438 |
err = curlx_base64_decode((const char *)b64, out, outlen); |
| 1439 |
free(b64); |
| 1440 |
if(err) { |
| 1441 |
free(*out); |
| 1442 |
return -1; |
| 1443 |
} |
| 1444 |
|
| 1445 |
return sep_end - in; |
| 1446 |
} |
| 1447 |
|
| 1448 |
#define MAX_CERTS_SIZE (50*1024*1024) /* arbitrary - to catch mistakes */ |
| 1449 |
|
| 1450 |
static int read_cert(const char *file, unsigned char **out, size_t *outlen) |
| 1451 |
{ |
| 1452 |
int fd; |
| 1453 |
ssize_t n; |
| 1454 |
unsigned char buf[512]; |
| 1455 |
struct dynbuf certs; |
| 1456 |
|
| 1457 |
curlx_dyn_init(&certs, MAX_CERTS_SIZE); |
| 1458 |
|
| 1459 |
fd = open(file, 0); |
| 1460 |
if(fd < 0) |
| 1461 |
return -1; |
| 1462 |
|
| 1463 |
for(;;) { |
| 1464 |
n = read(fd, buf, sizeof(buf)); |
| 1465 |
if(!n) |
| 1466 |
break; |
| 1467 |
if(n < 0) { |
| 1468 |
close(fd); |
| 1469 |
curlx_dyn_free(&certs); |
| 1470 |
return -1; |
| 1471 |
} |
| 1472 |
if(curlx_dyn_addn(&certs, buf, n)) { |
| 1473 |
close(fd); |
| 1474 |
return -1; |
| 1475 |
} |
| 1476 |
} |
| 1477 |
close(fd); |
| 1478 |
|
| 1479 |
*out = curlx_dyn_uptr(&certs); |
| 1480 |
*outlen = curlx_dyn_len(&certs); |
| 1481 |
|
| 1482 |
return 0; |
| 1483 |
} |
| 1484 |
|
| 1485 |
static CURLcode append_cert_to_array(struct Curl_easy *data, |
| 1486 |
const unsigned char *buf, size_t buflen, |
| 1487 |
CFMutableArrayRef array) |
| 1488 |
{ |
| 1489 |
char *certp; |
| 1490 |
CURLcode result; |
| 1491 |
SecCertificateRef cacert; |
| 1492 |
CFDataRef certdata; |
| 1493 |
|
| 1494 |
certdata = CFDataCreate(kCFAllocatorDefault, buf, (CFIndex)buflen); |
| 1495 |
if(!certdata) { |
| 1496 |
failf(data, "SSL: failed to allocate array for CA certificate"); |
| 1497 |
return CURLE_OUT_OF_MEMORY; |
| 1498 |
} |
| 1499 |
|
| 1500 |
cacert = SecCertificateCreateWithData(kCFAllocatorDefault, certdata); |
| 1501 |
CFRelease(certdata); |
| 1502 |
if(!cacert) { |
| 1503 |
failf(data, "SSL: failed to create SecCertificate from CA certificate"); |
| 1504 |
return CURLE_SSL_CACERT_BADFILE; |
| 1505 |
} |
| 1506 |
|
| 1507 |
/* Check if cacert is valid. */ |
| 1508 |
result = CopyCertSubject(data, cacert, &certp); |
| 1509 |
switch(result) { |
| 1510 |
case CURLE_OK: |
| 1511 |
break; |
| 1512 |
case CURLE_PEER_FAILED_VERIFICATION: |
| 1513 |
CFRelease(cacert); |
| 1514 |
return CURLE_SSL_CACERT_BADFILE; |
| 1515 |
case CURLE_OUT_OF_MEMORY: |
| 1516 |
default: |
| 1517 |
CFRelease(cacert); |
| 1518 |
return result; |
| 1519 |
} |
| 1520 |
free(certp); |
| 1521 |
|
| 1522 |
CFArrayAppendValue(array, cacert); |
| 1523 |
CFRelease(cacert); |
| 1524 |
|
| 1525 |
return CURLE_OK; |
| 1526 |
} |
| 1527 |
|
| 1528 |
static CURLcode verify_cert_buf(struct Curl_cfilter *cf, |
| 1529 |
struct Curl_easy *data, |
| 1530 |
const unsigned char *certbuf, size_t buflen, |
| 1531 |
SSLContextRef ctx) |
| 1532 |
{ |
| 1533 |
int n = 0; |
| 1534 |
CURLcode rc; |
| 1535 |
long res; |
| 1536 |
unsigned char *der; |
| 1537 |
size_t derlen, offset = 0; |
| 1538 |
OSStatus ret; |
| 1539 |
SecTrustResultType trust_eval; |
| 1540 |
CFMutableArrayRef array = NULL; |
| 1541 |
SecTrustRef trust = NULL; |
| 1542 |
CURLcode result = CURLE_PEER_FAILED_VERIFICATION; |
| 1543 |
(void)cf; |
| 1544 |
/* |
| 1545 |
* Certbuf now contains the contents of the certificate file, which can be |
| 1546 |
* - a single DER certificate, |
| 1547 |
* - a single PEM certificate or |
| 1548 |
* - a bunch of PEM certificates (certificate bundle). |
| 1549 |
* |
| 1550 |
* Go through certbuf, and convert any PEM certificate in it into DER |
| 1551 |
* format. |
| 1552 |
*/ |
| 1553 |
array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); |
| 1554 |
if(!array) { |
| 1555 |
failf(data, "SSL: out of memory creating CA certificate array"); |
| 1556 |
result = CURLE_OUT_OF_MEMORY; |
| 1557 |
goto out; |
| 1558 |
} |
| 1559 |
|
| 1560 |
while(offset < buflen) { |
| 1561 |
n++; |
| 1562 |
|
| 1563 |
/* |
| 1564 |
* Check if the certificate is in PEM format, and convert it to DER. If |
| 1565 |
* this fails, we assume the certificate is in DER format. |
| 1566 |
*/ |
| 1567 |
res = pem_to_der((const char *)certbuf + offset, &der, &derlen); |
| 1568 |
if(res < 0) { |
| 1569 |
failf(data, "SSL: invalid CA certificate #%d (offset %zu) in bundle", |
| 1570 |
n, offset); |
| 1571 |
result = CURLE_SSL_CACERT_BADFILE; |
| 1572 |
goto out; |
| 1573 |
} |
| 1574 |
offset += res; |
| 1575 |
|
| 1576 |
if(res == 0 && offset == 0) { |
| 1577 |
/* This is not a PEM file, probably a certificate in DER format. */ |
| 1578 |
rc = append_cert_to_array(data, certbuf, buflen, array); |
| 1579 |
if(rc != CURLE_OK) { |
| 1580 |
CURL_TRC_CF(data, cf, "append_cert for CA failed"); |
| 1581 |
result = rc; |
| 1582 |
goto out; |
| 1583 |
} |
| 1584 |
break; |
| 1585 |
} |
| 1586 |
else if(res == 0) { |
| 1587 |
/* No more certificates in the bundle. */ |
| 1588 |
break; |
| 1589 |
} |
| 1590 |
|
| 1591 |
rc = append_cert_to_array(data, der, derlen, array); |
| 1592 |
free(der); |
| 1593 |
if(rc != CURLE_OK) { |
| 1594 |
CURL_TRC_CF(data, cf, "append_cert for CA failed"); |
| 1595 |
result = rc; |
| 1596 |
goto out; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
|
| 1600 |
ret = SSLCopyPeerTrust(ctx, &trust); |
| 1601 |
if(!trust) { |
| 1602 |
failf(data, "SSL: error getting certificate chain"); |
| 1603 |
goto out; |
| 1604 |
} |
| 1605 |
else if(ret != noErr) { |
| 1606 |
failf(data, "SSLCopyPeerTrust() returned error %d", ret); |
| 1607 |
goto out; |
| 1608 |
} |
| 1609 |
|
| 1610 |
CURL_TRC_CF(data, cf, "setting %d trust anchors", n); |
| 1611 |
ret = SecTrustSetAnchorCertificates(trust, array); |
| 1612 |
if(ret != noErr) { |
| 1613 |
failf(data, "SecTrustSetAnchorCertificates() returned error %d", ret); |
| 1614 |
goto out; |
| 1615 |
} |
| 1616 |
ret = SecTrustSetAnchorCertificatesOnly(trust, TRUE); |
| 1617 |
if(ret != noErr) { |
| 1618 |
failf(data, "SecTrustSetAnchorCertificatesOnly() returned error %d", ret); |
| 1619 |
goto out; |
| 1620 |
} |
| 1621 |
|
| 1622 |
trust_eval = 0; |
| 1623 |
ret = SecTrustEvaluate(trust, &trust_eval); |
| 1624 |
if(ret != noErr) { |
| 1625 |
failf(data, "SecTrustEvaluate() returned error %d", ret); |
| 1626 |
goto out; |
| 1627 |
} |
| 1628 |
|
| 1629 |
switch(trust_eval) { |
| 1630 |
case kSecTrustResultUnspecified: |
| 1631 |
/* what does this really mean? */ |
| 1632 |
CURL_TRC_CF(data, cf, "trust result: Unspecified"); |
| 1633 |
result = CURLE_OK; |
| 1634 |
goto out; |
| 1635 |
case kSecTrustResultProceed: |
| 1636 |
CURL_TRC_CF(data, cf, "trust result: Proceed"); |
| 1637 |
result = CURLE_OK; |
| 1638 |
goto out; |
| 1639 |
|
| 1640 |
case kSecTrustResultRecoverableTrustFailure: |
| 1641 |
failf(data, "SSL: peer not verified: RecoverableTrustFailure"); |
| 1642 |
goto out; |
| 1643 |
case kSecTrustResultDeny: |
| 1644 |
failf(data, "SSL: peer not verified: Deny"); |
| 1645 |
goto out; |
| 1646 |
default: |
| 1647 |
failf(data, "SSL: perr not verified: result=%d", trust_eval); |
| 1648 |
goto out; |
| 1649 |
} |
| 1650 |
|
| 1651 |
out: |
| 1652 |
if(trust) |
| 1653 |
CFRelease(trust); |
| 1654 |
if(array) |
| 1655 |
CFRelease(array); |
| 1656 |
return result; |
| 1657 |
} |
| 1658 |
|
| 1659 |
static CURLcode verify_cert(struct Curl_cfilter *cf, |
| 1660 |
struct Curl_easy *data, const char *cafile, |
| 1661 |
const struct curl_blob *ca_info_blob, |
| 1662 |
SSLContextRef ctx) |
| 1663 |
{ |
| 1664 |
CURLcode result; |
| 1665 |
unsigned char *certbuf; |
| 1666 |
size_t buflen; |
| 1667 |
bool free_certbuf = FALSE; |
| 1668 |
|
| 1669 |
if(ca_info_blob) { |
| 1670 |
CURL_TRC_CF(data, cf, "verify_peer, CA from config blob"); |
| 1671 |
certbuf = ca_info_blob->data; |
| 1672 |
buflen = ca_info_blob->len; |
| 1673 |
} |
| 1674 |
else if(cafile) { |
| 1675 |
CURL_TRC_CF(data, cf, "verify_peer, CA from file '%s'", cafile); |
| 1676 |
if(read_cert(cafile, &certbuf, &buflen) < 0) { |
| 1677 |
failf(data, "SSL: failed to read or invalid CA certificate"); |
| 1678 |
return CURLE_SSL_CACERT_BADFILE; |
| 1679 |
} |
| 1680 |
free_certbuf = TRUE; |
| 1681 |
} |
| 1682 |
else |
| 1683 |
return CURLE_SSL_CACERT_BADFILE; |
| 1684 |
|
| 1685 |
result = verify_cert_buf(cf, data, certbuf, buflen, ctx); |
| 1686 |
if(free_certbuf) |
| 1687 |
free(certbuf); |
| 1688 |
return result; |
| 1689 |
} |
| 1690 |
|
| 1691 |
|
| 1692 |
#ifdef SECTRANSP_PINNEDPUBKEY |
| 1693 |
static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data, |
| 1694 |
SSLContextRef ctx, |
| 1695 |
const char *pinnedpubkey) |
| 1696 |
{ /* Scratch */ |
| 1697 |
size_t pubkeylen, realpubkeylen, spkiHeaderLength = 24; |
| 1698 |
const unsigned char *pubkey = NULL; |
| 1699 |
unsigned char *realpubkey = NULL; |
| 1700 |
const unsigned char *spkiHeader = NULL; |
| 1701 |
CFDataRef publicKeyBits = NULL; |
| 1702 |
|
| 1703 |
/* Result is returned to caller */ |
| 1704 |
CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 1705 |
|
| 1706 |
/* if a path was not specified, do not pin */ |
| 1707 |
if(!pinnedpubkey) |
| 1708 |
return CURLE_OK; |
| 1709 |
|
| 1710 |
|
| 1711 |
if(!ctx) |
| 1712 |
return result; |
| 1713 |
|
| 1714 |
do { |
| 1715 |
SecTrustRef trust; |
| 1716 |
OSStatus ret; |
| 1717 |
SecKeyRef keyRef; |
| 1718 |
|
| 1719 |
ret = SSLCopyPeerTrust(ctx, &trust); |
| 1720 |
if(ret != noErr || !trust) |
| 1721 |
break; |
| 1722 |
|
| 1723 |
keyRef = SecTrustCopyPublicKey(trust); |
| 1724 |
CFRelease(trust); |
| 1725 |
if(!keyRef) |
| 1726 |
break; |
| 1727 |
|
| 1728 |
#ifdef SECTRANSP_PINNEDPUBKEY_V1 |
| 1729 |
|
| 1730 |
publicKeyBits = SecKeyCopyExternalRepresentation(keyRef, NULL); |
| 1731 |
CFRelease(keyRef); |
| 1732 |
if(!publicKeyBits) |
| 1733 |
break; |
| 1734 |
|
| 1735 |
#elif SECTRANSP_PINNEDPUBKEY_V2 |
| 1736 |
|
| 1737 |
{ |
| 1738 |
OSStatus success; |
| 1739 |
success = SecItemExport(keyRef, kSecFormatOpenSSL, 0, NULL, |
| 1740 |
&publicKeyBits); |
| 1741 |
CFRelease(keyRef); |
| 1742 |
if(success != errSecSuccess || !publicKeyBits) |
| 1743 |
break; |
| 1744 |
} |
| 1745 |
|
| 1746 |
#endif /* SECTRANSP_PINNEDPUBKEY_V2 */ |
| 1747 |
|
| 1748 |
pubkeylen = (size_t)CFDataGetLength(publicKeyBits); |
| 1749 |
pubkey = (const unsigned char *)CFDataGetBytePtr(publicKeyBits); |
| 1750 |
|
| 1751 |
switch(pubkeylen) { |
| 1752 |
case 526: |
| 1753 |
/* 4096 bit RSA pubkeylen == 526 */ |
| 1754 |
spkiHeader = rsa4096SpkiHeader; |
| 1755 |
break; |
| 1756 |
case 270: |
| 1757 |
/* 2048 bit RSA pubkeylen == 270 */ |
| 1758 |
spkiHeader = rsa2048SpkiHeader; |
| 1759 |
break; |
| 1760 |
#ifdef SECTRANSP_PINNEDPUBKEY_V1 |
| 1761 |
case 65: |
| 1762 |
/* ecDSA secp256r1 pubkeylen == 65 */ |
| 1763 |
spkiHeader = ecDsaSecp256r1SpkiHeader; |
| 1764 |
spkiHeaderLength = 26; |
| 1765 |
break; |
| 1766 |
case 97: |
| 1767 |
/* ecDSA secp384r1 pubkeylen == 97 */ |
| 1768 |
spkiHeader = ecDsaSecp384r1SpkiHeader; |
| 1769 |
spkiHeaderLength = 23; |
| 1770 |
break; |
| 1771 |
default: |
| 1772 |
infof(data, "SSL: unhandled public key length: %zu", pubkeylen); |
| 1773 |
#elif SECTRANSP_PINNEDPUBKEY_V2 |
| 1774 |
default: |
| 1775 |
/* ecDSA secp256r1 pubkeylen == 91 header already included? |
| 1776 |
* ecDSA secp384r1 header already included too |
| 1777 |
* we assume rest of algorithms do same, so do nothing |
| 1778 |
*/ |
| 1779 |
result = Curl_pin_peer_pubkey(data, pinnedpubkey, pubkey, |
| 1780 |
pubkeylen); |
| 1781 |
#endif /* SECTRANSP_PINNEDPUBKEY_V2 */ |
| 1782 |
continue; /* break from loop */ |
| 1783 |
} |
| 1784 |
|
| 1785 |
realpubkeylen = pubkeylen + spkiHeaderLength; |
| 1786 |
realpubkey = malloc(realpubkeylen); |
| 1787 |
if(!realpubkey) |
| 1788 |
break; |
| 1789 |
|
| 1790 |
memcpy(realpubkey, spkiHeader, spkiHeaderLength); |
| 1791 |
memcpy(realpubkey + spkiHeaderLength, pubkey, pubkeylen); |
| 1792 |
|
| 1793 |
result = Curl_pin_peer_pubkey(data, pinnedpubkey, realpubkey, |
| 1794 |
realpubkeylen); |
| 1795 |
|
| 1796 |
} while(0); |
| 1797 |
|
| 1798 |
Curl_safefree(realpubkey); |
| 1799 |
if(publicKeyBits) |
| 1800 |
CFRelease(publicKeyBits); |
| 1801 |
|
| 1802 |
return result; |
| 1803 |
} |
| 1804 |
#endif /* SECTRANSP_PINNEDPUBKEY */ |
| 1805 |
|
| 1806 |
static CURLcode sectransp_connect_step2(struct Curl_cfilter *cf, |
| 1807 |
struct Curl_easy *data) |
| 1808 |
{ |
| 1809 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1810 |
struct st_ssl_backend_data *backend = |
| 1811 |
(struct st_ssl_backend_data *)connssl->backend; |
| 1812 |
struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf); |
| 1813 |
OSStatus err; |
| 1814 |
SSLCipherSuite cipher; |
| 1815 |
SSLProtocol protocol = 0; |
| 1816 |
|
| 1817 |
DEBUGASSERT(ssl_connect_2 == connssl->connecting_state); |
| 1818 |
DEBUGASSERT(backend); |
| 1819 |
CURL_TRC_CF(data, cf, "connect_step2"); |
| 1820 |
|
| 1821 |
/* Here goes nothing: */ |
| 1822 |
check_handshake: |
| 1823 |
connssl->io_need = CURL_SSL_IO_NEED_NONE; |
| 1824 |
err = SSLHandshake(backend->ssl_ctx); |
| 1825 |
|
| 1826 |
if(err != noErr) { |
| 1827 |
switch(err) { |
| 1828 |
case errSSLWouldBlock: /* they are not done with us yet */ |
| 1829 |
connssl->io_need = backend->ssl_direction ? |
| 1830 |
CURL_SSL_IO_NEED_SEND : CURL_SSL_IO_NEED_RECV; |
| 1831 |
return CURLE_OK; |
| 1832 |
|
| 1833 |
/* The below is errSSLServerAuthCompleted; it is not defined in |
| 1834 |
Leopard's headers */ |
| 1835 |
case -9841: |
| 1836 |
if((conn_config->CAfile || conn_config->ca_info_blob) && |
| 1837 |
conn_config->verifypeer) { |
| 1838 |
CURLcode result = verify_cert(cf, data, conn_config->CAfile, |
| 1839 |
conn_config->ca_info_blob, |
| 1840 |
backend->ssl_ctx); |
| 1841 |
if(result) |
| 1842 |
return result; |
| 1843 |
} |
| 1844 |
/* the documentation says we need to call SSLHandshake() again */ |
| 1845 |
goto check_handshake; |
| 1846 |
|
| 1847 |
/* Problem with encrypt / decrypt */ |
| 1848 |
case errSSLPeerDecodeError: |
| 1849 |
failf(data, "Decode failed"); |
| 1850 |
break; |
| 1851 |
case errSSLDecryptionFail: |
| 1852 |
case errSSLPeerDecryptionFail: |
| 1853 |
failf(data, "Decryption failed"); |
| 1854 |
break; |
| 1855 |
case errSSLPeerDecryptError: |
| 1856 |
failf(data, "A decryption error occurred"); |
| 1857 |
break; |
| 1858 |
case errSSLBadCipherSuite: |
| 1859 |
failf(data, "A bad SSL cipher suite was encountered"); |
| 1860 |
break; |
| 1861 |
case errSSLCrypto: |
| 1862 |
failf(data, "An underlying cryptographic error was encountered"); |
| 1863 |
break; |
| 1864 |
#if CURL_BUILD_MAC_10_11 || CURL_BUILD_IOS_9 |
| 1865 |
case errSSLWeakPeerEphemeralDHKey: |
| 1866 |
failf(data, "Indicates a weak ephemeral Diffie-Hellman key"); |
| 1867 |
break; |
| 1868 |
#endif |
| 1869 |
|
| 1870 |
/* Problem with the message record validation */ |
| 1871 |
case errSSLBadRecordMac: |
| 1872 |
case errSSLPeerBadRecordMac: |
| 1873 |
failf(data, "A record with a bad message authentication code (MAC) " |
| 1874 |
"was encountered"); |
| 1875 |
break; |
| 1876 |
case errSSLRecordOverflow: |
| 1877 |
case errSSLPeerRecordOverflow: |
| 1878 |
failf(data, "A record overflow occurred"); |
| 1879 |
break; |
| 1880 |
|
| 1881 |
/* Problem with zlib decompression */ |
| 1882 |
case errSSLPeerDecompressFail: |
| 1883 |
failf(data, "Decompression failed"); |
| 1884 |
break; |
| 1885 |
|
| 1886 |
/* Problem with access */ |
| 1887 |
case errSSLPeerAccessDenied: |
| 1888 |
failf(data, "Access was denied"); |
| 1889 |
break; |
| 1890 |
case errSSLPeerInsufficientSecurity: |
| 1891 |
failf(data, "There is insufficient security for this operation"); |
| 1892 |
break; |
| 1893 |
|
| 1894 |
/* These are all certificate problems with the server: */ |
| 1895 |
case errSSLXCertChainInvalid: |
| 1896 |
failf(data, "SSL certificate problem: Invalid certificate chain"); |
| 1897 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1898 |
case errSSLUnknownRootCert: |
| 1899 |
failf(data, "SSL certificate problem: Untrusted root certificate"); |
| 1900 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1901 |
case errSSLNoRootCert: |
| 1902 |
failf(data, "SSL certificate problem: No root certificate"); |
| 1903 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1904 |
case errSSLCertNotYetValid: |
| 1905 |
failf(data, "SSL certificate problem: The certificate chain had a " |
| 1906 |
"certificate that is not yet valid"); |
| 1907 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1908 |
case errSSLCertExpired: |
| 1909 |
case errSSLPeerCertExpired: |
| 1910 |
failf(data, "SSL certificate problem: Certificate chain had an " |
| 1911 |
"expired certificate"); |
| 1912 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1913 |
case errSSLBadCert: |
| 1914 |
case errSSLPeerBadCert: |
| 1915 |
failf(data, "SSL certificate problem: Couldn't understand the server " |
| 1916 |
"certificate format"); |
| 1917 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1918 |
case errSSLPeerUnsupportedCert: |
| 1919 |
failf(data, "SSL certificate problem: An unsupported certificate " |
| 1920 |
"format was encountered"); |
| 1921 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1922 |
case errSSLPeerCertRevoked: |
| 1923 |
failf(data, "SSL certificate problem: The certificate was revoked"); |
| 1924 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1925 |
case errSSLPeerCertUnknown: |
| 1926 |
failf(data, "SSL certificate problem: The certificate is unknown"); |
| 1927 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1928 |
|
| 1929 |
/* These are all certificate problems with the client: */ |
| 1930 |
case errSecAuthFailed: |
| 1931 |
failf(data, "SSL authentication failed"); |
| 1932 |
break; |
| 1933 |
case errSSLPeerHandshakeFail: |
| 1934 |
failf(data, "SSL peer handshake failed, the server most likely " |
| 1935 |
"requires a client certificate to connect"); |
| 1936 |
break; |
| 1937 |
case errSSLPeerUnknownCA: |
| 1938 |
failf(data, "SSL server rejected the client certificate due to " |
| 1939 |
"the certificate being signed by an unknown certificate " |
| 1940 |
"authority"); |
| 1941 |
break; |
| 1942 |
|
| 1943 |
/* This error is raised if the server's cert did not match the server's |
| 1944 |
hostname: */ |
| 1945 |
case errSSLHostNameMismatch: |
| 1946 |
failf(data, "SSL certificate peer verification failed, the " |
| 1947 |
"certificate did not match \"%s\"\n", connssl->peer.dispname); |
| 1948 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1949 |
|
| 1950 |
/* Problem with SSL / TLS negotiation */ |
| 1951 |
case errSSLNegotiation: |
| 1952 |
failf(data, "Could not negotiate an SSL cipher suite with the server"); |
| 1953 |
break; |
| 1954 |
case errSSLBadConfiguration: |
| 1955 |
failf(data, "A configuration error occurred"); |
| 1956 |
break; |
| 1957 |
case errSSLProtocol: |
| 1958 |
failf(data, "SSL protocol error"); |
| 1959 |
break; |
| 1960 |
case errSSLPeerProtocolVersion: |
| 1961 |
failf(data, "A bad protocol version was encountered"); |
| 1962 |
break; |
| 1963 |
case errSSLPeerNoRenegotiation: |
| 1964 |
failf(data, "No renegotiation is allowed"); |
| 1965 |
break; |
| 1966 |
|
| 1967 |
/* Generic handshake errors: */ |
| 1968 |
case errSSLConnectionRefused: |
| 1969 |
failf(data, "Server dropped the connection during the SSL handshake"); |
| 1970 |
break; |
| 1971 |
case errSSLClosedAbort: |
| 1972 |
failf(data, "Server aborted the SSL handshake"); |
| 1973 |
break; |
| 1974 |
case errSSLClosedGraceful: |
| 1975 |
failf(data, "The connection closed gracefully"); |
| 1976 |
break; |
| 1977 |
case errSSLClosedNoNotify: |
| 1978 |
failf(data, "The server closed the session with no notification"); |
| 1979 |
break; |
| 1980 |
/* Sometimes paramErr happens with buggy ciphers: */ |
| 1981 |
case paramErr: |
| 1982 |
case errSSLInternal: |
| 1983 |
case errSSLPeerInternalError: |
| 1984 |
failf(data, "Internal SSL engine error encountered during the " |
| 1985 |
"SSL handshake"); |
| 1986 |
break; |
| 1987 |
case errSSLFatalAlert: |
| 1988 |
failf(data, "Fatal SSL engine error encountered during the SSL " |
| 1989 |
"handshake"); |
| 1990 |
break; |
| 1991 |
/* Unclassified error */ |
| 1992 |
case errSSLBufferOverflow: |
| 1993 |
failf(data, "An insufficient buffer was provided"); |
| 1994 |
break; |
| 1995 |
case errSSLIllegalParam: |
| 1996 |
failf(data, "An illegal parameter was encountered"); |
| 1997 |
break; |
| 1998 |
case errSSLModuleAttach: |
| 1999 |
failf(data, "Module attach failure"); |
| 2000 |
break; |
| 2001 |
case errSSLSessionNotFound: |
| 2002 |
failf(data, "An attempt to restore an unknown session failed"); |
| 2003 |
break; |
| 2004 |
case errSSLPeerExportRestriction: |
| 2005 |
failf(data, "An export restriction occurred"); |
| 2006 |
break; |
| 2007 |
case errSSLPeerUserCancelled: |
| 2008 |
failf(data, "The user canceled the operation"); |
| 2009 |
break; |
| 2010 |
case errSSLPeerUnexpectedMsg: |
| 2011 |
failf(data, "Peer rejected unexpected message"); |
| 2012 |
break; |
| 2013 |
#if CURL_BUILD_MAC_10_11 || CURL_BUILD_IOS_9 |
| 2014 |
/* Treating non-fatal error as fatal like before */ |
| 2015 |
case errSSLClientHelloReceived: |
| 2016 |
failf(data, "A non-fatal result for providing a server name " |
| 2017 |
"indication"); |
| 2018 |
break; |
| 2019 |
#endif |
| 2020 |
|
| 2021 |
/* Error codes defined in the enum but should never be returned. |
| 2022 |
We list them here just in case. */ |
| 2023 |
#if CURL_BUILD_MAC_10_6 |
| 2024 |
/* Only returned when kSSLSessionOptionBreakOnCertRequested is set */ |
| 2025 |
case errSSLClientCertRequested: |
| 2026 |
failf(data, "Server requested a client certificate during the " |
| 2027 |
"handshake"); |
| 2028 |
return CURLE_SSL_CLIENTCERT; |
| 2029 |
#endif |
| 2030 |
#if CURL_BUILD_MAC_10_9 |
| 2031 |
/* Alias for errSSLLast, end of error range */ |
| 2032 |
case errSSLUnexpectedRecord: |
| 2033 |
failf(data, "Unexpected (skipped) record in DTLS"); |
| 2034 |
break; |
| 2035 |
#endif |
| 2036 |
default: |
| 2037 |
/* May also return codes listed in Security Framework Result Codes */ |
| 2038 |
failf(data, "Unknown SSL protocol error in connection to %s:%d", |
| 2039 |
connssl->peer.hostname, err); |
| 2040 |
break; |
| 2041 |
} |
| 2042 |
return CURLE_SSL_CONNECT_ERROR; |
| 2043 |
} |
| 2044 |
else { |
| 2045 |
char cipher_str[64]; |
| 2046 |
/* we have been connected fine, we are not waiting for anything else. */ |
| 2047 |
connssl->connecting_state = ssl_connect_3; |
| 2048 |
|
| 2049 |
#ifdef SECTRANSP_PINNEDPUBKEY |
| 2050 |
if(data->set.str[STRING_SSL_PINNEDPUBLICKEY]) { |
| 2051 |
CURLcode result = |
| 2052 |
pkp_pin_peer_pubkey(data, backend->ssl_ctx, |
| 2053 |
data->set.str[STRING_SSL_PINNEDPUBLICKEY]); |
| 2054 |
if(result) { |
| 2055 |
failf(data, "SSL: public key does not match pinned public key"); |
| 2056 |
return result; |
| 2057 |
} |
| 2058 |
} |
| 2059 |
#endif /* SECTRANSP_PINNEDPUBKEY */ |
| 2060 |
|
| 2061 |
/* Informational message */ |
| 2062 |
(void)SSLGetNegotiatedCipher(backend->ssl_ctx, &cipher); |
| 2063 |
(void)SSLGetNegotiatedProtocolVersion(backend->ssl_ctx, &protocol); |
| 2064 |
|
| 2065 |
sectransp_cipher_suite_get_str((uint16_t) cipher, cipher_str, |
| 2066 |
sizeof(cipher_str), TRUE); |
| 2067 |
switch(protocol) { |
| 2068 |
case kSSLProtocol2: |
| 2069 |
infof(data, "SSL 2.0 connection using %s", cipher_str); |
| 2070 |
break; |
| 2071 |
case kSSLProtocol3: |
| 2072 |
infof(data, "SSL 3.0 connection using %s", cipher_str); |
| 2073 |
break; |
| 2074 |
case kTLSProtocol1: |
| 2075 |
infof(data, "TLS 1.0 connection using %s", cipher_str); |
| 2076 |
break; |
| 2077 |
#if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS |
| 2078 |
case kTLSProtocol11: |
| 2079 |
infof(data, "TLS 1.1 connection using %s", cipher_str); |
| 2080 |
break; |
| 2081 |
case kTLSProtocol12: |
| 2082 |
infof(data, "TLS 1.2 connection using %s", cipher_str); |
| 2083 |
break; |
| 2084 |
#endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */ |
| 2085 |
#if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 |
| 2086 |
case kTLSProtocol13: |
| 2087 |
infof(data, "TLS 1.3 connection using %s", cipher_str); |
| 2088 |
break; |
| 2089 |
#endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */ |
| 2090 |
default: |
| 2091 |
infof(data, "Unknown protocol connection"); |
| 2092 |
break; |
| 2093 |
} |
| 2094 |
|
| 2095 |
if(connssl->alpn) { |
| 2096 |
#if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 |
| 2097 |
#ifdef HAVE_BUILTIN_AVAILABLE |
| 2098 |
if(__builtin_available(macOS 10.13.4, iOS 11, tvOS 11, *)) { |
| 2099 |
#else |
| 2100 |
if(&SSLSetALPNProtocols && &SSLCopyALPNProtocols) { |
| 2101 |
#endif |
| 2102 |
CFArrayRef alpnArr = NULL; |
| 2103 |
CFStringRef chosenProtocol = NULL; |
| 2104 |
err = SSLCopyALPNProtocols(backend->ssl_ctx, &alpnArr); |
| 2105 |
|
| 2106 |
if(err == noErr && alpnArr && CFArrayGetCount(alpnArr) >= 1) |
| 2107 |
chosenProtocol = CFArrayGetValueAtIndex(alpnArr, 0); |
| 2108 |
|
| 2109 |
#ifdef USE_HTTP2 |
| 2110 |
if(chosenProtocol && |
| 2111 |
!CFStringCompare(chosenProtocol, CFSTR(ALPN_H2), 0)) { |
| 2112 |
cf->conn->alpn = CURL_HTTP_VERSION_2; |
| 2113 |
} |
| 2114 |
else |
| 2115 |
#endif |
| 2116 |
if(chosenProtocol && |
| 2117 |
!CFStringCompare(chosenProtocol, CFSTR(ALPN_HTTP_1_1), 0)) { |
| 2118 |
cf->conn->alpn = CURL_HTTP_VERSION_1_1; |
| 2119 |
} |
| 2120 |
else |
| 2121 |
infof(data, VTLS_INFOF_NO_ALPN); |
| 2122 |
|
| 2123 |
/* chosenProtocol is a reference to the string within alpnArr |
| 2124 |
and does not need to be freed separately */ |
| 2125 |
if(alpnArr) |
| 2126 |
CFRelease(alpnArr); |
| 2127 |
} |
| 2128 |
#endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */ |
| 2129 |
} |
| 2130 |
|
| 2131 |
return CURLE_OK; |
| 2132 |
} |
| 2133 |
} |
| 2134 |
|
| 2135 |
static CURLcode |
| 2136 |
add_cert_to_certinfo(struct Curl_easy *data, |
| 2137 |
const SecCertificateRef server_cert, |
| 2138 |
int idx) |
| 2139 |
{ |
| 2140 |
CURLcode result = CURLE_OK; |
| 2141 |
const char *beg; |
| 2142 |
const char *end; |
| 2143 |
CFDataRef cert_data = SecCertificateCopyData(server_cert); |
| 2144 |
|
| 2145 |
if(!cert_data) |
| 2146 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 2147 |
|
| 2148 |
beg = (const char *)CFDataGetBytePtr(cert_data); |
| 2149 |
end = beg + CFDataGetLength(cert_data); |
| 2150 |
result = Curl_extract_certinfo(data, idx, beg, end); |
| 2151 |
CFRelease(cert_data); |
| 2152 |
return result; |
| 2153 |
} |
| 2154 |
|
| 2155 |
static CURLcode |
| 2156 |
collect_server_cert_single(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 2157 |
const SecCertificateRef server_cert, |
| 2158 |
CFIndex idx) |
| 2159 |
{ |
| 2160 |
CURLcode result = CURLE_OK; |
| 2161 |
struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data); |
| 2162 |
#ifndef CURL_DISABLE_VERBOSE_STRINGS |
| 2163 |
if(data->set.verbose) { |
| 2164 |
char *certp; |
| 2165 |
result = CopyCertSubject(data, server_cert, &certp); |
| 2166 |
if(!result) { |
| 2167 |
infof(data, "Server certificate: %s", certp); |
| 2168 |
free(certp); |
| 2169 |
} |
| 2170 |
} |
| 2171 |
#endif |
| 2172 |
if(ssl_config->certinfo) |
| 2173 |
result = add_cert_to_certinfo(data, server_cert, (int)idx); |
| 2174 |
return result; |
| 2175 |
} |
| 2176 |
|
| 2177 |
/* This should be called during step3 of the connection at the earliest */ |
| 2178 |
static CURLcode collect_server_cert(struct Curl_cfilter *cf, |
| 2179 |
struct Curl_easy *data) |
| 2180 |
{ |
| 2181 |
#ifndef CURL_DISABLE_VERBOSE_STRINGS |
| 2182 |
const bool show_verbose_server_cert = data->set.verbose; |
| 2183 |
#else |
| 2184 |
const bool show_verbose_server_cert = FALSE; |
| 2185 |
#endif |
| 2186 |
struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data); |
| 2187 |
CURLcode result = ssl_config->certinfo ? |
| 2188 |
CURLE_PEER_FAILED_VERIFICATION : CURLE_OK; |
| 2189 |
struct ssl_connect_data *connssl = cf->ctx; |
| 2190 |
struct st_ssl_backend_data *backend = |
| 2191 |
(struct st_ssl_backend_data *)connssl->backend; |
| 2192 |
CFArrayRef server_certs = NULL; |
| 2193 |
SecCertificateRef server_cert; |
| 2194 |
OSStatus err; |
| 2195 |
CFIndex i, count; |
| 2196 |
SecTrustRef trust = NULL; |
| 2197 |
|
| 2198 |
DEBUGASSERT(backend); |
| 2199 |
|
| 2200 |
if(!show_verbose_server_cert && !ssl_config->certinfo) |
| 2201 |
return CURLE_OK; |
| 2202 |
|
| 2203 |
if(!backend->ssl_ctx) |
| 2204 |
return result; |
| 2205 |
|
| 2206 |
#if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS |
| 2207 |
#if CURL_BUILD_IOS |
| 2208 |
#pragma unused(server_certs) |
| 2209 |
err = SSLCopyPeerTrust(backend->ssl_ctx, &trust); |
| 2210 |
/* For some reason, SSLCopyPeerTrust() can return noErr and yet return |
| 2211 |
a null trust, so be on guard for that: */ |
| 2212 |
if(err == noErr && trust) { |
| 2213 |
count = SecTrustGetCertificateCount(trust); |
| 2214 |
if(ssl_config->certinfo) |
| 2215 |
result = Curl_ssl_init_certinfo(data, (int)count); |
| 2216 |
for(i = 0L ; !result && (i < count) ; i++) { |
| 2217 |
server_cert = SecTrustGetCertificateAtIndex(trust, i); |
| 2218 |
result = collect_server_cert_single(cf, data, server_cert, i); |
| 2219 |
} |
| 2220 |
CFRelease(trust); |
| 2221 |
} |
| 2222 |
#else |
| 2223 |
/* SSLCopyPeerCertificates() is deprecated as of Mountain Lion. |
| 2224 |
The function SecTrustGetCertificateAtIndex() is officially present |
| 2225 |
in Lion, but it is unfortunately also present in Snow Leopard as |
| 2226 |
private API and does not work as expected. So we have to look for |
| 2227 |
a different symbol to make sure this code is only executed under |
| 2228 |
Lion or later. */ |
| 2229 |
if(&SecTrustCopyPublicKey) { |
| 2230 |
#pragma unused(server_certs) |
| 2231 |
err = SSLCopyPeerTrust(backend->ssl_ctx, &trust); |
| 2232 |
/* For some reason, SSLCopyPeerTrust() can return noErr and yet return |
| 2233 |
a null trust, so be on guard for that: */ |
| 2234 |
if(err == noErr && trust) { |
| 2235 |
count = SecTrustGetCertificateCount(trust); |
| 2236 |
if(ssl_config->certinfo) |
| 2237 |
result = Curl_ssl_init_certinfo(data, (int)count); |
| 2238 |
for(i = 0L ; !result && (i < count) ; i++) { |
| 2239 |
server_cert = SecTrustGetCertificateAtIndex(trust, i); |
| 2240 |
result = collect_server_cert_single(cf, data, server_cert, i); |
| 2241 |
} |
| 2242 |
CFRelease(trust); |
| 2243 |
} |
| 2244 |
} |
| 2245 |
else { |
| 2246 |
#if CURL_SUPPORT_MAC_10_8 |
| 2247 |
err = SSLCopyPeerCertificates(backend->ssl_ctx, &server_certs); |
| 2248 |
/* Just in case SSLCopyPeerCertificates() returns null too... */ |
| 2249 |
if(err == noErr && server_certs) { |
| 2250 |
count = CFArrayGetCount(server_certs); |
| 2251 |
if(ssl_config->certinfo) |
| 2252 |
result = Curl_ssl_init_certinfo(data, (int)count); |
| 2253 |
for(i = 0L ; !result && (i < count) ; i++) { |
| 2254 |
const void *item = CFArrayGetValueAtIndex(server_certs, i); |
| 2255 |
server_cert = (SecCertificateRef)CURL_UNCONST(item); |
| 2256 |
result = collect_server_cert_single(cf, data, server_cert, i); |
| 2257 |
} |
| 2258 |
CFRelease(server_certs); |
| 2259 |
} |
| 2260 |
#endif /* CURL_SUPPORT_MAC_10_8 */ |
| 2261 |
} |
| 2262 |
#endif /* CURL_BUILD_IOS */ |
| 2263 |
#else |
| 2264 |
#pragma unused(trust) |
| 2265 |
err = SSLCopyPeerCertificates(backend->ssl_ctx, &server_certs); |
| 2266 |
if(err == noErr) { |
| 2267 |
count = CFArrayGetCount(server_certs); |
| 2268 |
if(ssl_config->certinfo) |
| 2269 |
result = Curl_ssl_init_certinfo(data, (int)count); |
| 2270 |
for(i = 0L ; !result && (i < count) ; i++) { |
| 2271 |
const void *item = CFArrayGetValueAtIndex(server_certs, i); |
| 2272 |
server_cert = (SecCertificateRef)CURL_UNCONST(item); |
| 2273 |
result = collect_server_cert_single(cf, data, server_cert, i); |
| 2274 |
} |
| 2275 |
CFRelease(server_certs); |
| 2276 |
} |
| 2277 |
#endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */ |
| 2278 |
return result; |
| 2279 |
} |
| 2280 |
|
| 2281 |
static CURLcode sectransp_connect_step3(struct Curl_cfilter *cf, |
| 2282 |
struct Curl_easy *data) |
| 2283 |
{ |
| 2284 |
struct ssl_connect_data *connssl = cf->ctx; |
| 2285 |
CURLcode result; |
| 2286 |
|
| 2287 |
CURL_TRC_CF(data, cf, "connect_step3"); |
| 2288 |
/* There is no step 3! |
| 2289 |
* Well, okay, let's collect server certificates, and if verbose mode is on, |
| 2290 |
* let's print the details of the server certificates. */ |
| 2291 |
result = collect_server_cert(cf, data); |
| 2292 |
if(result) |
| 2293 |
return result; |
| 2294 |
|
| 2295 |
connssl->connecting_state = ssl_connect_done; |
| 2296 |
return CURLE_OK; |
| 2297 |
} |
| 2298 |
|
| 2299 |
static CURLcode sectransp_connect(struct Curl_cfilter *cf, |
| 2300 |
struct Curl_easy *data, |
| 2301 |
bool *done) |
| 2302 |
{ |
| 2303 |
CURLcode result; |
| 2304 |
struct ssl_connect_data *connssl = cf->ctx; |
| 2305 |
|
| 2306 |
/* check if the connection has already been established */ |
| 2307 |
if(ssl_connection_complete == connssl->state) { |
| 2308 |
*done = TRUE; |
| 2309 |
return CURLE_OK; |
| 2310 |
} |
| 2311 |
|
| 2312 |
*done = FALSE; |
| 2313 |
connssl->io_need = CURL_SSL_IO_NEED_NONE; |
| 2314 |
|
| 2315 |
if(ssl_connect_1 == connssl->connecting_state) { |
| 2316 |
result = sectransp_connect_step1(cf, data); |
| 2317 |
if(result) |
| 2318 |
return result; |
| 2319 |
} |
| 2320 |
|
| 2321 |
if(ssl_connect_2 == connssl->connecting_state) { |
| 2322 |
result = sectransp_connect_step2(cf, data); |
| 2323 |
if(result) |
| 2324 |
return result; |
| 2325 |
} |
| 2326 |
|
| 2327 |
if(ssl_connect_3 == connssl->connecting_state) { |
| 2328 |
result = sectransp_connect_step3(cf, data); |
| 2329 |
if(result) |
| 2330 |
return result; |
| 2331 |
} |
| 2332 |
|
| 2333 |
if(ssl_connect_done == connssl->connecting_state) { |
| 2334 |
CURL_TRC_CF(data, cf, "connected"); |
| 2335 |
connssl->state = ssl_connection_complete; |
| 2336 |
*done = TRUE; |
| 2337 |
} |
| 2338 |
|
| 2339 |
return CURLE_OK; |
| 2340 |
} |
| 2341 |
|
| 2342 |
static ssize_t sectransp_recv(struct Curl_cfilter *cf, |
| 2343 |
struct Curl_easy *data, |
| 2344 |
char *buf, |
| 2345 |
size_t buffersize, |
| 2346 |
CURLcode *curlcode); |
| 2347 |
|
| 2348 |
static CURLcode sectransp_shutdown(struct Curl_cfilter *cf, |
| 2349 |
struct Curl_easy *data, |
| 2350 |
bool send_shutdown, bool *done) |
| 2351 |
{ |
| 2352 |
struct ssl_connect_data *connssl = cf->ctx; |
| 2353 |
struct st_ssl_backend_data *backend = |
| 2354 |
(struct st_ssl_backend_data *)connssl->backend; |
| 2355 |
CURLcode result = CURLE_OK; |
| 2356 |
ssize_t nread = 0; |
| 2357 |
char buf[1024]; |
| 2358 |
size_t i; |
| 2359 |
|
| 2360 |
DEBUGASSERT(backend); |
| 2361 |
if(!backend->ssl_ctx || cf->shutdown) { |
| 2362 |
*done = TRUE; |
| 2363 |
goto out; |
| 2364 |
} |
| 2365 |
|
| 2366 |
connssl->io_need = CURL_SSL_IO_NEED_NONE; |
| 2367 |
*done = FALSE; |
| 2368 |
|
| 2369 |
if(send_shutdown && !backend->sent_shutdown) { |
| 2370 |
OSStatus err; |
| 2371 |
|
| 2372 |
CURL_TRC_CF(data, cf, "shutdown, send close notify"); |
| 2373 |
err = SSLClose(backend->ssl_ctx); |
| 2374 |
switch(err) { |
| 2375 |
case noErr: |
| 2376 |
backend->sent_shutdown = TRUE; |
| 2377 |
break; |
| 2378 |
case errSSLWouldBlock: |
| 2379 |
connssl->io_need = CURL_SSL_IO_NEED_SEND; |
| 2380 |
result = CURLE_OK; |
| 2381 |
goto out; |
| 2382 |
default: |
| 2383 |
CURL_TRC_CF(data, cf, "shutdown, error: %d", (int)err); |
| 2384 |
result = CURLE_SEND_ERROR; |
| 2385 |
goto out; |
| 2386 |
} |
| 2387 |
} |
| 2388 |
|
| 2389 |
for(i = 0; i < 10; ++i) { |
| 2390 |
if(!backend->sent_shutdown) { |
| 2391 |
nread = sectransp_recv(cf, data, buf, (int)sizeof(buf), &result); |
| 2392 |
} |
| 2393 |
else { |
| 2394 |
/* We would like to read the close notify from the server using |
| 2395 |
* Secure Transport, however SSLRead() no longer works after we |
| 2396 |
* sent the notify from our side. So, we just read from the |
| 2397 |
* underlying filter and hope it will end. */ |
| 2398 |
nread = Curl_conn_cf_recv(cf->next, data, buf, sizeof(buf), &result); |
| 2399 |
} |
| 2400 |
CURL_TRC_CF(data, cf, "shutdown read -> %zd, %d", nread, result); |
| 2401 |
if(nread <= 0) |
| 2402 |
break; |
| 2403 |
} |
| 2404 |
|
| 2405 |
if(nread > 0) { |
| 2406 |
/* still data coming in? */ |
| 2407 |
connssl->io_need = CURL_SSL_IO_NEED_RECV; |
| 2408 |
} |
| 2409 |
else if(nread == 0) { |
| 2410 |
/* We got the close notify alert and are done. */ |
| 2411 |
CURL_TRC_CF(data, cf, "shutdown done"); |
| 2412 |
*done = TRUE; |
| 2413 |
} |
| 2414 |
else if(result == CURLE_AGAIN) { |
| 2415 |
connssl->io_need = CURL_SSL_IO_NEED_RECV; |
| 2416 |
result = CURLE_OK; |
| 2417 |
} |
| 2418 |
else { |
| 2419 |
DEBUGASSERT(result); |
| 2420 |
CURL_TRC_CF(data, cf, "shutdown, error: %d", result); |
| 2421 |
} |
| 2422 |
|
| 2423 |
out: |
| 2424 |
cf->shutdown = (result || *done); |
| 2425 |
return result; |
| 2426 |
} |
| 2427 |
|
| 2428 |
static void sectransp_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 2429 |
{ |
| 2430 |
struct ssl_connect_data *connssl = cf->ctx; |
| 2431 |
struct st_ssl_backend_data *backend = |
| 2432 |
(struct st_ssl_backend_data *)connssl->backend; |
| 2433 |
|
| 2434 |
(void) data; |
| 2435 |
|
| 2436 |
DEBUGASSERT(backend); |
| 2437 |
|
| 2438 |
if(backend->ssl_ctx) { |
| 2439 |
CURL_TRC_CF(data, cf, "close"); |
| 2440 |
#if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS |
| 2441 |
if(&SSLCreateContext) |
| 2442 |
CFRelease(backend->ssl_ctx); |
| 2443 |
#if CURL_SUPPORT_MAC_10_8 |
| 2444 |
else |
| 2445 |
(void)SSLDisposeContext(backend->ssl_ctx); |
| 2446 |
#endif /* CURL_SUPPORT_MAC_10_8 */ |
| 2447 |
#else |
| 2448 |
(void)SSLDisposeContext(backend->ssl_ctx); |
| 2449 |
#endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */ |
| 2450 |
backend->ssl_ctx = NULL; |
| 2451 |
} |
| 2452 |
} |
| 2453 |
|
| 2454 |
static size_t sectransp_version(char *buffer, size_t size) |
| 2455 |
{ |
| 2456 |
return msnprintf(buffer, size, "SecureTransport"); |
| 2457 |
} |
| 2458 |
|
| 2459 |
static bool sectransp_data_pending(struct Curl_cfilter *cf, |
| 2460 |
const struct Curl_easy *data) |
| 2461 |
{ |
| 2462 |
const struct ssl_connect_data *connssl = cf->ctx; |
| 2463 |
struct st_ssl_backend_data *backend = |
| 2464 |
(struct st_ssl_backend_data *)connssl->backend; |
| 2465 |
OSStatus err; |
| 2466 |
size_t buffer; |
| 2467 |
|
| 2468 |
(void)data; |
| 2469 |
DEBUGASSERT(backend); |
| 2470 |
|
| 2471 |
if(backend->ssl_ctx) { /* SSL is in use */ |
| 2472 |
CURL_TRC_CF((struct Curl_easy *)CURL_UNCONST(data), cf, "data_pending"); |
| 2473 |
err = SSLGetBufferedReadSize(backend->ssl_ctx, &buffer); |
| 2474 |
if(err == noErr) |
| 2475 |
return buffer > 0UL; |
| 2476 |
return FALSE; |
| 2477 |
} |
| 2478 |
else |
| 2479 |
return FALSE; |
| 2480 |
} |
| 2481 |
|
| 2482 |
static CURLcode sectransp_random(struct Curl_easy *data UNUSED_PARAM, |
| 2483 |
unsigned char *entropy, size_t length) |
| 2484 |
{ |
| 2485 |
/* arc4random_buf() is not available on cats older than Lion, so let's |
| 2486 |
do this manually for the benefit of the older cats. */ |
| 2487 |
size_t i; |
| 2488 |
u_int32_t random_number = 0; |
| 2489 |
|
| 2490 |
(void)data; |
| 2491 |
|
| 2492 |
for(i = 0 ; i < length ; i++) { |
| 2493 |
if(i % sizeof(u_int32_t) == 0) |
| 2494 |
random_number = arc4random(); |
| 2495 |
entropy[i] = random_number & 0xFF; |
| 2496 |
random_number >>= 8; |
| 2497 |
} |
| 2498 |
i = random_number = 0; |
| 2499 |
return CURLE_OK; |
| 2500 |
} |
| 2501 |
|
| 2502 |
static CURLcode sectransp_sha256sum(const unsigned char *tmp, /* input */ |
| 2503 |
size_t tmplen, |
| 2504 |
unsigned char *sha256sum, /* output */ |
| 2505 |
size_t sha256len) |
| 2506 |
{ |
| 2507 |
(void)sha256len; |
| 2508 |
assert(sha256len >= CURL_SHA256_DIGEST_LENGTH); |
| 2509 |
(void)CC_SHA256(tmp, (CC_LONG)tmplen, sha256sum); |
| 2510 |
return CURLE_OK; |
| 2511 |
} |
| 2512 |
|
| 2513 |
static bool sectransp_false_start(void) |
| 2514 |
{ |
| 2515 |
#if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 |
| 2516 |
if(&SSLSetSessionOption) |
| 2517 |
return TRUE; |
| 2518 |
#endif |
| 2519 |
return FALSE; |
| 2520 |
} |
| 2521 |
|
| 2522 |
static ssize_t sectransp_send(struct Curl_cfilter *cf, |
| 2523 |
struct Curl_easy *data, |
| 2524 |
const void *mem, |
| 2525 |
size_t len, |
| 2526 |
CURLcode *curlcode) |
| 2527 |
{ |
| 2528 |
struct ssl_connect_data *connssl = cf->ctx; |
| 2529 |
struct st_ssl_backend_data *backend = |
| 2530 |
(struct st_ssl_backend_data *)connssl->backend; |
| 2531 |
size_t processed = 0UL; |
| 2532 |
OSStatus err; |
| 2533 |
|
| 2534 |
DEBUGASSERT(backend); |
| 2535 |
|
| 2536 |
/* The SSLWrite() function works a little differently than expected. The |
| 2537 |
fourth argument (processed) is currently documented in Apple's |
| 2538 |
documentation as: "On return, the length, in bytes, of the data actually |
| 2539 |
written." |
| 2540 |
|
| 2541 |
Now, one could interpret that as "written to the socket," but actually, |
| 2542 |
it returns the amount of data that was written to a buffer internal to |
| 2543 |
the SSLContextRef instead. So it is possible for SSLWrite() to return |
| 2544 |
errSSLWouldBlock and a number of bytes "written" because those bytes were |
| 2545 |
encrypted and written to a buffer, not to the socket. |
| 2546 |
|
| 2547 |
So if this happens, then we need to keep calling SSLWrite() over and |
| 2548 |
over again with no new data until it quits returning errSSLWouldBlock. */ |
| 2549 |
|
| 2550 |
/* Do we have buffered data to write from the last time we were called? */ |
| 2551 |
if(backend->ssl_write_buffered_length) { |
| 2552 |
/* Write the buffered data: */ |
| 2553 |
err = SSLWrite(backend->ssl_ctx, NULL, 0UL, &processed); |
| 2554 |
switch(err) { |
| 2555 |
case noErr: |
| 2556 |
/* processed is always going to be 0 because we did not write to |
| 2557 |
the buffer, so return how much was written to the socket */ |
| 2558 |
processed = backend->ssl_write_buffered_length; |
| 2559 |
backend->ssl_write_buffered_length = 0UL; |
| 2560 |
break; |
| 2561 |
case errSSLWouldBlock: /* argh, try again */ |
| 2562 |
*curlcode = CURLE_AGAIN; |
| 2563 |
return -1L; |
| 2564 |
default: |
| 2565 |
failf(data, "SSLWrite() returned error %d", err); |
| 2566 |
*curlcode = CURLE_SEND_ERROR; |
| 2567 |
return -1L; |
| 2568 |
} |
| 2569 |
} |
| 2570 |
else { |
| 2571 |
/* We have got new data to write: */ |
| 2572 |
err = SSLWrite(backend->ssl_ctx, mem, len, &processed); |
| 2573 |
if(err != noErr) { |
| 2574 |
switch(err) { |
| 2575 |
case errSSLWouldBlock: |
| 2576 |
/* Data was buffered but not sent, we have to tell the caller |
| 2577 |
to try sending again, and remember how much was buffered */ |
| 2578 |
backend->ssl_write_buffered_length = len; |
| 2579 |
*curlcode = CURLE_AGAIN; |
| 2580 |
return -1L; |
| 2581 |
default: |
| 2582 |
failf(data, "SSLWrite() returned error %d", err); |
| 2583 |
*curlcode = CURLE_SEND_ERROR; |
| 2584 |
return -1L; |
| 2585 |
} |
| 2586 |
} |
| 2587 |
} |
| 2588 |
return (ssize_t)processed; |
| 2589 |
} |
| 2590 |
|
| 2591 |
static ssize_t sectransp_recv(struct Curl_cfilter *cf, |
| 2592 |
struct Curl_easy *data, |
| 2593 |
char *buf, |
| 2594 |
size_t buffersize, |
| 2595 |
CURLcode *curlcode) |
| 2596 |
{ |
| 2597 |
struct ssl_connect_data *connssl = cf->ctx; |
| 2598 |
struct st_ssl_backend_data *backend = |
| 2599 |
(struct st_ssl_backend_data *)connssl->backend; |
| 2600 |
struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf); |
| 2601 |
size_t processed = 0UL; |
| 2602 |
OSStatus err; |
| 2603 |
|
| 2604 |
DEBUGASSERT(backend); |
| 2605 |
|
| 2606 |
again: |
| 2607 |
*curlcode = CURLE_OK; |
| 2608 |
err = SSLRead(backend->ssl_ctx, buf, buffersize, &processed); |
| 2609 |
|
| 2610 |
if(err != noErr) { |
| 2611 |
switch(err) { |
| 2612 |
case errSSLWouldBlock: /* return how much we read (if anything) */ |
| 2613 |
if(processed) { |
| 2614 |
return (ssize_t)processed; |
| 2615 |
} |
| 2616 |
*curlcode = CURLE_AGAIN; |
| 2617 |
return -1L; |
| 2618 |
|
| 2619 |
/* errSSLClosedGraceful - server gracefully shut down the SSL session |
| 2620 |
errSSLClosedNoNotify - server hung up on us instead of sending a |
| 2621 |
closure alert notice, read() is returning 0 |
| 2622 |
Either way, inform the caller that the server disconnected. */ |
| 2623 |
case errSSLClosedGraceful: |
| 2624 |
case errSSLClosedNoNotify: |
| 2625 |
*curlcode = CURLE_OK; |
| 2626 |
return 0; |
| 2627 |
|
| 2628 |
/* The below is errSSLPeerAuthCompleted; it is not defined in |
| 2629 |
Leopard's headers */ |
| 2630 |
case -9841: |
| 2631 |
if((conn_config->CAfile || conn_config->ca_info_blob) && |
| 2632 |
conn_config->verifypeer) { |
| 2633 |
CURLcode result = verify_cert(cf, data, conn_config->CAfile, |
| 2634 |
conn_config->ca_info_blob, |
| 2635 |
backend->ssl_ctx); |
| 2636 |
if(result) { |
| 2637 |
*curlcode = result; |
| 2638 |
return -1; |
| 2639 |
} |
| 2640 |
} |
| 2641 |
goto again; |
| 2642 |
default: |
| 2643 |
failf(data, "SSLRead() return error %d", err); |
| 2644 |
*curlcode = CURLE_RECV_ERROR; |
| 2645 |
return -1L; |
| 2646 |
} |
| 2647 |
} |
| 2648 |
return (ssize_t)processed; |
| 2649 |
} |
| 2650 |
|
| 2651 |
static void *sectransp_get_internals(struct ssl_connect_data *connssl, |
| 2652 |
CURLINFO info UNUSED_PARAM) |
| 2653 |
{ |
| 2654 |
struct st_ssl_backend_data *backend = |
| 2655 |
(struct st_ssl_backend_data *)connssl->backend; |
| 2656 |
(void)info; |
| 2657 |
DEBUGASSERT(backend); |
| 2658 |
return backend->ssl_ctx; |
| 2659 |
} |
| 2660 |
|
| 2661 |
const struct Curl_ssl Curl_ssl_sectransp = { |
| 2662 |
{ CURLSSLBACKEND_SECURETRANSPORT, "secure-transport" }, /* info */ |
| 2663 |
|
| 2664 |
SSLSUPP_CAINFO_BLOB | |
| 2665 |
SSLSUPP_CERTINFO | |
| 2666 |
#ifdef SECTRANSP_PINNEDPUBKEY |
| 2667 |
SSLSUPP_PINNEDPUBKEY | |
| 2668 |
#endif /* SECTRANSP_PINNEDPUBKEY */ |
| 2669 |
SSLSUPP_HTTPS_PROXY | |
| 2670 |
SSLSUPP_CIPHER_LIST, |
| 2671 |
|
| 2672 |
sizeof(struct st_ssl_backend_data), |
| 2673 |
|
| 2674 |
NULL, /* init */ |
| 2675 |
NULL, /* cleanup */ |
| 2676 |
sectransp_version, /* version */ |
| 2677 |
sectransp_shutdown, /* shutdown */ |
| 2678 |
sectransp_data_pending, /* data_pending */ |
| 2679 |
sectransp_random, /* random */ |
| 2680 |
NULL, /* cert_status_request */ |
| 2681 |
sectransp_connect, /* connect */ |
| 2682 |
Curl_ssl_adjust_pollset, /* adjust_pollset */ |
| 2683 |
sectransp_get_internals, /* get_internals */ |
| 2684 |
sectransp_close, /* close_one */ |
| 2685 |
NULL, /* close_all */ |
| 2686 |
NULL, /* set_engine */ |
| 2687 |
NULL, /* set_engine_default */ |
| 2688 |
NULL, /* engines_list */ |
| 2689 |
sectransp_false_start, /* false_start */ |
| 2690 |
sectransp_sha256sum, /* sha256sum */ |
| 2691 |
sectransp_recv, /* recv decrypted data */ |
| 2692 |
sectransp_send, /* send data to encrypt */ |
| 2693 |
NULL, /* get_channel_binding */ |
| 2694 |
}; |
| 2695 |
|
| 2696 |
#if defined(__GNUC__) && defined(__APPLE__) |
| 2697 |
#pragma GCC diagnostic pop |
| 2698 |
#endif |
| 2699 |
|
| 2700 |
#ifdef __GNUC__ |
| 2701 |
#pragma GCC diagnostic pop |
| 2702 |
#endif |
| 2703 |
|
| 2704 |
#ifdef __clang__ |
| 2705 |
#pragma clang diagnostic pop |
| 2706 |
#endif |
| 2707 |
|
| 2708 |
#endif /* USE_SECTRANSP */ |