2 * TLS support code for CUPS on macOS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
10 /**** This file is included from tls.c ****/
13 * Include necessary headers...
18 extern char **environ
;
21 #ifndef _SECURITY_VERSION_GREATER_THAN_57610_
22 typedef CF_OPTIONS(uint32_t, SecKeyUsage
) {
23 kSecKeyUsageAll
= 0x7FFFFFFF
25 #endif /* !_SECURITY_VERSION_GREATER_THAN_57610_ */
26 extern const void * kSecCSRChallengePassword
;
27 extern const void * kSecSubjectAltName
;
28 extern const void * kSecCertificateKeyUsage
;
29 extern const void * kSecCSRBasicContraintsPathLen
;
30 extern const void * kSecCertificateExtensions
;
31 extern const void * kSecCertificateExtensionsEncoded
;
32 extern const void * kSecOidCommonName
;
33 extern const void * kSecOidCountryName
;
34 extern const void * kSecOidStateProvinceName
;
35 extern const void * kSecOidLocalityName
;
36 extern const void * kSecOidOrganization
;
37 extern const void * kSecOidOrganizationalUnit
;
38 extern bool SecCertificateIsValid(SecCertificateRef certificate
, CFAbsoluteTime verifyTime
);
39 extern CFAbsoluteTime
SecCertificateNotValidAfter(SecCertificateRef certificate
);
40 extern SecCertificateRef
SecGenerateSelfSignedCertificate(CFArrayRef subject
, CFDictionaryRef parameters
, SecKeyRef publicKey
, SecKeyRef privateKey
);
41 extern SecIdentityRef
SecIdentityCreate(CFAllocatorRef allocator
, SecCertificateRef certificate
, SecKeyRef privateKey
);
45 * Constants, very secure stuff...
48 #define _CUPS_CDSA_PASSWORD "42" /* CUPS keychain password */
49 #define _CUPS_CDSA_PASSLEN 2 /* Length of keychain password */
56 static int tls_auto_create
= 0;
57 /* Auto-create self-signed certs? */
58 static char *tls_common_name
= NULL
;
59 /* Default common name */
61 static int tls_cups_keychain
= 0;
62 /* Opened the CUPS keychain? */
63 static SecKeychainRef tls_keychain
= NULL
;
64 /* Server cert keychain */
66 static SecIdentityRef tls_selfsigned
= NULL
;
67 /* Temporary self-signed cert */
68 #endif /* TARGET_OS_OSX */
69 static char *tls_keypath
= NULL
;
70 /* Server cert keychain path */
71 static _cups_mutex_t tls_mutex
= _CUPS_MUTEX_INITIALIZER
;
72 /* Mutex for keychain/certs */
73 static int tls_options
= -1,/* Options for TLS connections */
74 tls_min_version
= _HTTP_TLS_1_0
,
75 tls_max_version
= _HTTP_TLS_MAX
;
82 static CFArrayRef
http_cdsa_copy_server(const char *common_name
);
83 static SecCertificateRef
http_cdsa_create_credential(http_credential_t
*credential
);
85 static const char *http_cdsa_default_path(char *buffer
, size_t bufsize
);
86 static SecKeychainRef
http_cdsa_open_keychain(const char *path
, char *filename
, size_t filesize
);
87 static SecKeychainRef
http_cdsa_open_system_keychain(void);
88 #endif /* TARGET_OS_OSX */
89 static OSStatus
http_cdsa_read(SSLConnectionRef connection
, void *data
, size_t *dataLength
);
90 static int http_cdsa_set_credentials(http_t
*http
);
91 static OSStatus
http_cdsa_write(SSLConnectionRef connection
, const void *data
, size_t *dataLength
);
95 * 'cupsMakeServerCredentials()' - Make a self-signed certificate and private key pair.
97 * @since CUPS 2.0/OS 10.10@
100 int /* O - 1 on success, 0 on failure */
101 cupsMakeServerCredentials(
102 const char *path
, /* I - Keychain path or @code NULL@ for default */
103 const char *common_name
, /* I - Common name */
104 int num_alt_names
, /* I - Number of subject alternate names */
105 const char **alt_names
, /* I - Subject Alternate Names */
106 time_t expiration_date
) /* I - Expiration date */
109 int pid
, /* Process ID of command */
110 status
, /* Status of command */
112 char command
[1024], /* Command */
113 *argv
[5], /* Command-line arguments */
114 *envp
[1000], /* Environment variables */
115 days
[32], /* CERTTOOL_EXPIRATION_DAYS env var */
116 keychain
[1024], /* Keychain argument */
117 infofile
[1024], /* Type-in information for cert */
118 filename
[1024]; /* Default keychain path */
119 cups_file_t
*fp
; /* Seed/info file */
122 DEBUG_printf(("cupsMakeServerCredentials(path=\"%s\", common_name=\"%s\", num_alt_names=%d, alt_names=%p, expiration_date=%d)", path
, common_name
, num_alt_names
, (void *)alt_names
, (int)expiration_date
));
128 path
= http_cdsa_default_path(filename
, sizeof(filename
));
131 * Run the "certtool" command to generate a self-signed certificate...
134 if (!cupsFileFind("certtool", getenv("PATH"), 1, command
, sizeof(command
)))
138 * Create a file with the certificate information fields...
140 * Note: This assumes that the default questions are asked by the certtool
144 if ((fp
= cupsTempFile2(infofile
, sizeof(infofile
))) == NULL
)
148 "CUPS Self-Signed Certificate\n"
149 /* Enter key and certificate label */
150 "r\n" /* Generate RSA key pair */
151 "2048\n" /* 2048 bit encryption key */
152 "y\n" /* OK (y = yes) */
153 "b\n" /* Usage (b=signing/encryption) */
154 "2\n" /* Sign with SHA256 */
155 "y\n" /* OK (y = yes) */
156 "%s\n" /* Common name */
157 "\n" /* Country (default) */
158 "\n" /* Organization (default) */
159 "\n" /* Organizational unit (default) */
160 "\n" /* State/Province (default) */
161 "\n" /* Email address */
162 "y\n", /* OK (y = yes) */
166 snprintf(keychain
, sizeof(keychain
), "k=%s", path
);
168 argv
[0] = "certtool";
173 snprintf(days
, sizeof(days
), "CERTTOOL_EXPIRATION_DAYS=%d", (int)((expiration_date
- time(NULL
) + 86399) / 86400));
175 for (i
= 0; i
< (int)(sizeof(envp
) / sizeof(envp
[0]) - 2) && environ
[i
]; i
++)
176 envp
[i
+ 1] = environ
[i
];
179 posix_spawn_file_actions_t actions
; /* File actions */
181 posix_spawn_file_actions_init(&actions
);
182 posix_spawn_file_actions_addclose(&actions
, 0);
183 posix_spawn_file_actions_addopen(&actions
, 0, infofile
, O_RDONLY
, 0);
184 posix_spawn_file_actions_addclose(&actions
, 1);
185 posix_spawn_file_actions_addopen(&actions
, 1, "/dev/null", O_WRONLY
, 0);
186 posix_spawn_file_actions_addclose(&actions
, 2);
187 posix_spawn_file_actions_addopen(&actions
, 2, "/dev/null", O_WRONLY
, 0);
189 if (posix_spawn(&pid
, command
, &actions
, NULL
, argv
, envp
))
195 posix_spawn_file_actions_destroy(&actions
);
199 while (waitpid(pid
, &status
, 0) < 0)
209 int status
= 0; /* Return status */
210 OSStatus err
; /* Error code (if any) */
211 CFStringRef cfcommon_name
= NULL
;
212 /* CF string for server name */
213 SecIdentityRef ident
= NULL
; /* Identity */
214 SecKeyRef publicKey
= NULL
,
218 SecCertificateRef cert
= NULL
; /* Self-signed certificate */
219 CFMutableDictionaryRef keyParams
= NULL
;
220 /* Key generation parameters */
223 DEBUG_printf(("cupsMakeServerCredentials(path=\"%s\", common_name=\"%s\", num_alt_names=%d, alt_names=%p, expiration_date=%d)", path
, common_name
, num_alt_names
, alt_names
, (int)expiration_date
));
228 (void)expiration_date
;
232 DEBUG_puts("1cupsMakeServerCredentials: No keychain support compiled in, returning 0.");
238 DEBUG_puts("1cupsMakeServerCredentials: Using existing self-signed cert.");
242 cfcommon_name
= CFStringCreateWithCString(kCFAllocatorDefault
, common_name
, kCFStringEncodingUTF8
);
245 DEBUG_puts("1cupsMakeServerCredentials: Unable to create CF string of common name.");
250 * Create a public/private key pair...
253 keyParams
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
256 DEBUG_puts("1cupsMakeServerCredentials: Unable to create key parameters dictionary.");
260 CFDictionaryAddValue(keyParams
, kSecAttrKeyType
, kSecAttrKeyTypeRSA
);
261 CFDictionaryAddValue(keyParams
, kSecAttrKeySizeInBits
, CFSTR("2048"));
262 CFDictionaryAddValue(keyParams
, kSecAttrLabel
, cfcommon_name
);
264 err
= SecKeyGeneratePair(keyParams
, &publicKey
, &privateKey
);
267 DEBUG_printf(("1cupsMakeServerCredentials: Unable to generate key pair: %d.", (int)err
));
272 * Create a self-signed certificate using the public/private key pair...
275 CFIndex usageInt
= kSecKeyUsageAll
;
276 CFNumberRef usage
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberCFIndexType
, &usageInt
);
278 CFNumberRef len
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberCFIndexType
, &lenInt
);
279 CFTypeRef certKeys
[] = { kSecCSRBasicContraintsPathLen
, kSecSubjectAltName
, kSecCertificateKeyUsage
};
280 CFTypeRef certValues
[] = { len
, cfcommon_name
, usage
};
281 CFDictionaryRef certParams
= CFDictionaryCreate(kCFAllocatorDefault
, certKeys
, certValues
, sizeof(certKeys
) / sizeof(certKeys
[0]), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
285 const void *ca_o
[] = { kSecOidOrganization
, CFSTR("") };
286 const void *ca_cn
[] = { kSecOidCommonName
, cfcommon_name
};
287 CFArrayRef ca_o_dn
= CFArrayCreate(kCFAllocatorDefault
, ca_o
, 2, NULL
);
288 CFArrayRef ca_cn_dn
= CFArrayCreate(kCFAllocatorDefault
, ca_cn
, 2, NULL
);
289 const void *ca_dn_array
[2];
291 ca_dn_array
[0] = CFArrayCreate(kCFAllocatorDefault
, (const void **)&ca_o_dn
, 1, NULL
);
292 ca_dn_array
[1] = CFArrayCreate(kCFAllocatorDefault
, (const void **)&ca_cn_dn
, 1, NULL
);
294 CFArrayRef subject
= CFArrayCreate(kCFAllocatorDefault
, ca_dn_array
, 2, NULL
);
296 cert
= SecGenerateSelfSignedCertificate(subject
, certParams
, publicKey
, privateKey
);
299 CFRelease(certParams
);
303 DEBUG_puts("1cupsMakeServerCredentials: Unable to create self-signed certificate.");
307 ident
= SecIdentityCreate(kCFAllocatorDefault
, cert
, privateKey
);
311 _cupsMutexLock(&tls_mutex
);
316 tls_selfsigned
= ident
;
318 _cupsMutexLock(&tls_mutex
);
320 # if 0 /* Someday perhaps SecItemCopyMatching will work for identities, at which point */
321 CFTypeRef itemKeys
[] = { kSecClass
, kSecAttrLabel
, kSecValueRef
};
322 CFTypeRef itemValues
[] = { kSecClassIdentity
, cfcommon_name
, ident
};
323 CFDictionaryRef itemAttrs
= CFDictionaryCreate(kCFAllocatorDefault
, itemKeys
, itemValues
, sizeof(itemKeys
) / sizeof(itemKeys
[0]), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
325 err
= SecItemAdd(itemAttrs
, NULL
);
326 /* SecItemAdd consumes itemAttrs... */
332 DEBUG_printf(("1cupsMakeServerCredentials: Unable to add identity to keychain: %d.", (int)err
));
340 DEBUG_puts("1cupsMakeServerCredentials: Unable to create identity from cert and keys.");
343 * Cleanup and return...
349 CFRelease(cfcommon_name
);
352 CFRelease(keyParams
);
358 CFRelease(publicKey
);
361 CFRelease(privateKey
);
363 DEBUG_printf(("1cupsMakeServerCredentials: Returning %d.", status
));
366 #endif /* TARGET_OS_OSX */
371 * 'cupsSetServerCredentials()' - Set the default server credentials.
373 * Note: The server credentials are used by all threads in the running process.
374 * This function is threadsafe.
376 * @since CUPS 2.0/macOS 10.10@
379 int /* O - 1 on success, 0 on failure */
380 cupsSetServerCredentials(
381 const char *path
, /* I - Keychain path or @code NULL@ for default */
382 const char *common_name
, /* I - Default common name for server */
383 int auto_create
) /* I - 1 = automatically create self-signed certificates */
385 DEBUG_printf(("cupsSetServerCredentials(path=\"%s\", common_name=\"%s\", auto_create=%d)", path
, common_name
, auto_create
));
388 char filename
[1024]; /* Keychain filename */
389 SecKeychainRef keychain
= http_cdsa_open_keychain(path
, filename
, sizeof(filename
));
393 DEBUG_puts("1cupsSetServerCredentials: Unable to open keychain.");
397 _cupsMutexLock(&tls_mutex
);
400 * Close any keychain that is currently open...
404 CFRelease(tls_keychain
);
407 _cupsStrFree(tls_keypath
);
410 _cupsStrFree(tls_common_name
);
413 * Save the new keychain...
416 tls_keychain
= keychain
;
417 tls_keypath
= _cupsStrAlloc(filename
);
418 tls_auto_create
= auto_create
;
419 tls_common_name
= _cupsStrAlloc(common_name
);
421 _cupsMutexUnlock(&tls_mutex
);
423 DEBUG_puts("1cupsSetServerCredentials: Opened keychain, returning 1.");
429 DEBUG_puts("1cupsSetServerCredentials: No keychain support compiled in, returning 0.");
433 tls_auto_create
= auto_create
;
434 tls_common_name
= _cupsStrAlloc(common_name
);
437 #endif /* TARGET_OS_OSX */
442 * 'httpCopyCredentials()' - Copy the credentials associated with the peer in
443 * an encrypted connection.
445 * @since CUPS 1.5/macOS 10.7@
448 int /* O - Status of call (0 = success) */
450 http_t
*http
, /* I - Connection to server */
451 cups_array_t
**credentials
) /* O - Array of credentials */
453 OSStatus error
; /* Error code */
454 SecTrustRef peerTrust
; /* Peer trust reference */
455 CFIndex count
; /* Number of credentials */
456 SecCertificateRef secCert
; /* Certificate reference */
457 CFDataRef data
; /* Certificate data */
458 int i
; /* Looping var */
461 DEBUG_printf(("httpCopyCredentials(http=%p, credentials=%p)", (void *)http
, (void *)credentials
));
466 if (!http
|| !http
->tls
|| !credentials
)
469 if (!(error
= SSLCopyPeerTrust(http
->tls
, &peerTrust
)) && peerTrust
)
471 DEBUG_printf(("2httpCopyCredentials: Peer provided %d certificates.", (int)SecTrustGetCertificateCount(peerTrust
)));
473 if ((*credentials
= cupsArrayNew(NULL
, NULL
)) != NULL
)
475 count
= SecTrustGetCertificateCount(peerTrust
);
477 for (i
= 0; i
< count
; i
++)
479 secCert
= SecTrustGetCertificateAtIndex(peerTrust
, i
);
482 CFStringRef cf_name
= SecCertificateCopySubjectSummary(secCert
);
485 CFStringGetCString(cf_name
, name
, sizeof(name
), kCFStringEncodingUTF8
);
487 strlcpy(name
, "unknown", sizeof(name
));
489 DEBUG_printf(("2httpCopyCredentials: Certificate %d name is \"%s\".", i
, name
));
492 if ((data
= SecCertificateCopyData(secCert
)) != NULL
)
494 DEBUG_printf(("2httpCopyCredentials: Adding %d byte certificate blob.", (int)CFDataGetLength(data
)));
496 httpAddCredential(*credentials
, CFDataGetBytePtr(data
), (size_t)CFDataGetLength(data
));
502 CFRelease(peerTrust
);
510 * '_httpCreateCredentials()' - Create credentials in the internal format.
513 http_tls_credentials_t
/* O - Internal credentials */
514 _httpCreateCredentials(
515 cups_array_t
*credentials
) /* I - Array of credentials */
517 CFMutableArrayRef peerCerts
; /* Peer credentials reference */
518 SecCertificateRef secCert
; /* Certificate reference */
519 http_credential_t
*credential
; /* Credential data */
525 if ((peerCerts
= CFArrayCreateMutable(kCFAllocatorDefault
,
526 cupsArrayCount(credentials
),
527 &kCFTypeArrayCallBacks
)) == NULL
)
530 for (credential
= (http_credential_t
*)cupsArrayFirst(credentials
);
532 credential
= (http_credential_t
*)cupsArrayNext(credentials
))
534 if ((secCert
= http_cdsa_create_credential(credential
)) != NULL
)
536 CFArrayAppendValue(peerCerts
, secCert
);
546 * 'httpCredentialsAreValidForName()' - Return whether the credentials are valid for the given name.
548 * @since CUPS 2.0/macOS 10.10@
551 int /* O - 1 if valid, 0 otherwise */
552 httpCredentialsAreValidForName(
553 cups_array_t
*credentials
, /* I - Credentials */
554 const char *common_name
) /* I - Name to check */
556 SecCertificateRef secCert
; /* Certificate reference */
557 CFStringRef cfcert_name
= NULL
;
558 /* Certificate's common name (CF string) */
559 char cert_name
[256]; /* Certificate's common name (C string) */
560 int valid
= 1; /* Valid name? */
563 if ((secCert
= http_cdsa_create_credential((http_credential_t
*)cupsArrayFirst(credentials
))) == NULL
)
567 * Compare the common names...
570 if ((cfcert_name
= SecCertificateCopySubjectSummary(secCert
)) == NULL
)
573 * Can't get common name, cannot be valid...
578 else if (CFStringGetCString(cfcert_name
, cert_name
, sizeof(cert_name
), kCFStringEncodingUTF8
) &&
579 _cups_strcasecmp(common_name
, cert_name
))
582 * Not an exact match for the common name, check for wildcard certs...
585 const char *domain
= strchr(common_name
, '.');
586 /* Domain in common name */
588 if (strncmp(cert_name
, "*.", 2) || !domain
|| _cups_strcasecmp(domain
, cert_name
+ 1))
591 * Not a wildcard match.
594 /* TODO: Check subject alternate names */
600 CFRelease(cfcert_name
);
609 * 'httpCredentialsGetTrust()' - Return the trust of credentials.
611 * @since CUPS 2.0/macOS 10.10@
614 http_trust_t
/* O - Level of trust */
615 httpCredentialsGetTrust(
616 cups_array_t
*credentials
, /* I - Credentials */
617 const char *common_name
) /* I - Common name for trust lookup */
619 SecCertificateRef secCert
; /* Certificate reference */
620 http_trust_t trust
= HTTP_TRUST_OK
;
622 cups_array_t
*tcreds
= NULL
; /* Trusted credentials */
623 _cups_globals_t
*cg
= _cupsGlobals();
624 /* Per-thread globals */
629 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No common name specified."), 1);
630 return (HTTP_TRUST_UNKNOWN
);
633 if ((secCert
= http_cdsa_create_credential((http_credential_t
*)cupsArrayFirst(credentials
))) == NULL
)
635 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to create credentials from array."), 1);
636 return (HTTP_TRUST_UNKNOWN
);
639 if (cg
->any_root
< 0)
643 * Look this common name up in the default keychains...
646 httpLoadCredentials(NULL
, &tcreds
, common_name
);
650 char credentials_str
[1024], /* String for incoming credentials */
651 tcreds_str
[1024]; /* String for saved credentials */
653 httpCredentialsString(credentials
, credentials_str
, sizeof(credentials_str
));
654 httpCredentialsString(tcreds
, tcreds_str
, sizeof(tcreds_str
));
656 if (strcmp(credentials_str
, tcreds_str
))
659 * Credentials don't match, let's look at the expiration date of the new
660 * credentials and allow if the new ones have a later expiration...
663 if (!cg
->trust_first
)
666 * Do not trust certificates on first use...
669 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Trust on first use is disabled."), 1);
671 trust
= HTTP_TRUST_INVALID
;
673 else if (httpCredentialsGetExpiration(credentials
) <= httpCredentialsGetExpiration(tcreds
))
676 * The new credentials are not newly issued...
679 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("New credentials are older than stored credentials."), 1);
681 trust
= HTTP_TRUST_INVALID
;
683 else if (!httpCredentialsAreValidForName(credentials
, common_name
))
686 * The common name does not match the issued certificate...
689 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("New credentials are not valid for name."), 1);
691 trust
= HTTP_TRUST_INVALID
;
693 else if (httpCredentialsGetExpiration(tcreds
) < time(NULL
))
696 * Save the renewed credentials...
699 trust
= HTTP_TRUST_RENEWED
;
701 httpSaveCredentials(NULL
, credentials
, common_name
);
705 httpFreeCredentials(tcreds
);
707 else if (cg
->validate_certs
&& !httpCredentialsAreValidForName(credentials
, common_name
))
709 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No stored credentials, not valid for name."), 1);
710 trust
= HTTP_TRUST_INVALID
;
712 else if (!cg
->trust_first
)
715 * See if we have a site CA certificate we can compare...
718 if (!httpLoadCredentials(NULL
, &tcreds
, "site"))
720 if (cupsArrayCount(credentials
) != (cupsArrayCount(tcreds
) + 1))
723 * Certificate isn't directly generated from the CA cert...
726 trust
= HTTP_TRUST_INVALID
;
731 * Do a tail comparison of the two certificates...
734 http_credential_t
*a
, *b
; /* Certificates */
736 for (a
= (http_credential_t
*)cupsArrayFirst(tcreds
), b
= (http_credential_t
*)cupsArrayIndex(credentials
, 1);
738 a
= (http_credential_t
*)cupsArrayNext(tcreds
), b
= (http_credential_t
*)cupsArrayNext(credentials
))
739 if (a
->datalen
!= b
->datalen
|| memcmp(a
->data
, b
->data
, a
->datalen
))
743 trust
= HTTP_TRUST_INVALID
;
746 if (trust
!= HTTP_TRUST_OK
)
747 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Credentials do not validate against site CA certificate."), 1);
751 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Trust on first use is disabled."), 1);
752 trust
= HTTP_TRUST_INVALID
;
756 if (trust
== HTTP_TRUST_OK
&& !cg
->expired_certs
&& !SecCertificateIsValid(secCert
, CFAbsoluteTimeGetCurrent()))
758 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Credentials have expired."), 1);
759 trust
= HTTP_TRUST_EXPIRED
;
762 if (trust
== HTTP_TRUST_OK
&& !cg
->any_root
&& cupsArrayCount(credentials
) == 1)
764 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Self-signed credentials are blocked."), 1);
765 trust
= HTTP_TRUST_INVALID
;
775 * 'httpCredentialsGetExpiration()' - Return the expiration date of the credentials.
777 * @since CUPS 2.0/macOS 10.10@
780 time_t /* O - Expiration date of credentials */
781 httpCredentialsGetExpiration(
782 cups_array_t
*credentials
) /* I - Credentials */
784 SecCertificateRef secCert
; /* Certificate reference */
785 time_t expiration
; /* Expiration date */
788 if ((secCert
= http_cdsa_create_credential((http_credential_t
*)cupsArrayFirst(credentials
))) == NULL
)
791 expiration
= (time_t)(SecCertificateNotValidAfter(secCert
) + kCFAbsoluteTimeIntervalSince1970
);
800 * 'httpCredentialsString()' - Return a string representing the credentials.
802 * @since CUPS 2.0/macOS 10.10@
805 size_t /* O - Total size of credentials string */
806 httpCredentialsString(
807 cups_array_t
*credentials
, /* I - Credentials */
808 char *buffer
, /* I - Buffer or @code NULL@ */
809 size_t bufsize
) /* I - Size of buffer */
811 http_credential_t
*first
; /* First certificate */
812 SecCertificateRef secCert
; /* Certificate reference */
815 DEBUG_printf(("httpCredentialsString(credentials=%p, buffer=%p, bufsize=" CUPS_LLFMT
")", (void *)credentials
, (void *)buffer
, CUPS_LLCAST bufsize
));
820 if (buffer
&& bufsize
> 0)
823 if ((first
= (http_credential_t
*)cupsArrayFirst(credentials
)) != NULL
&&
824 (secCert
= http_cdsa_create_credential(first
)) != NULL
)
827 * Copy certificate (string) values from the SecCertificateRef and produce
828 * a one-line summary. The API for accessing certificate values like the
829 * issuer name is, um, "interesting"...
833 CFDictionaryRef cf_dict
; /* Dictionary for certificate */
834 # endif /* TARGET_OS_OSX */
835 CFStringRef cf_string
; /* CF string */
836 char commonName
[256],/* Common name associated with cert */
837 issuer
[256], /* Issuer name */
838 sigalg
[256]; /* Signature algorithm */
839 time_t expiration
; /* Expiration date of cert */
840 unsigned char md5_digest
[16]; /* MD5 result */
842 if (SecCertificateCopyCommonName(secCert
, &cf_string
) == noErr
)
844 CFStringGetCString(cf_string
, commonName
, (CFIndex
)sizeof(commonName
), kCFStringEncodingUTF8
);
845 CFRelease(cf_string
);
849 strlcpy(commonName
, "unknown", sizeof(commonName
));
852 strlcpy(issuer
, "unknown", sizeof(issuer
));
853 strlcpy(sigalg
, "UnknownSignature", sizeof(sigalg
));
856 if ((cf_dict
= SecCertificateCopyValues(secCert
, NULL
, NULL
)) != NULL
)
858 CFDictionaryRef cf_issuer
= CFDictionaryGetValue(cf_dict
, kSecOIDX509V1IssuerName
);
859 CFDictionaryRef cf_sigalg
= CFDictionaryGetValue(cf_dict
, kSecOIDX509V1SignatureAlgorithm
);
863 CFArrayRef cf_values
= CFDictionaryGetValue(cf_issuer
, kSecPropertyKeyValue
);
864 CFIndex i
, count
= CFArrayGetCount(cf_values
);
865 CFDictionaryRef cf_value
;
867 for (i
= 0; i
< count
; i
++)
869 cf_value
= CFArrayGetValueAtIndex(cf_values
, i
);
871 if (!CFStringCompare(CFDictionaryGetValue(cf_value
, kSecPropertyKeyLabel
), kSecOIDOrganizationName
, kCFCompareCaseInsensitive
))
872 CFStringGetCString(CFDictionaryGetValue(cf_value
, kSecPropertyKeyValue
), issuer
, (CFIndex
)sizeof(issuer
), kCFStringEncodingUTF8
);
878 CFArrayRef cf_values
= CFDictionaryGetValue(cf_sigalg
, kSecPropertyKeyValue
);
879 CFIndex i
, count
= CFArrayGetCount(cf_values
);
880 CFDictionaryRef cf_value
;
882 for (i
= 0; i
< count
; i
++)
884 cf_value
= CFArrayGetValueAtIndex(cf_values
, i
);
886 if (!CFStringCompare(CFDictionaryGetValue(cf_value
, kSecPropertyKeyLabel
), CFSTR("Algorithm"), kCFCompareCaseInsensitive
))
888 CFStringRef cf_algorithm
= CFDictionaryGetValue(cf_value
, kSecPropertyKeyValue
);
890 if (!CFStringCompare(cf_algorithm
, CFSTR("1.2.840.113549.1.1.5"), kCFCompareCaseInsensitive
))
891 strlcpy(sigalg
, "SHA1WithRSAEncryption", sizeof(sigalg
));
892 else if (!CFStringCompare(cf_algorithm
, CFSTR("1.2.840.113549.1.1.11"), kCFCompareCaseInsensitive
))
893 strlcpy(sigalg
, "SHA256WithRSAEncryption", sizeof(sigalg
));
894 else if (!CFStringCompare(cf_algorithm
, CFSTR("1.2.840.113549.1.1.4"), kCFCompareCaseInsensitive
))
895 strlcpy(sigalg
, "MD5WithRSAEncryption", sizeof(sigalg
));
902 # endif /* TARGET_OS_OSX */
904 expiration
= (time_t)(SecCertificateNotValidAfter(secCert
) + kCFAbsoluteTimeIntervalSince1970
);
906 cupsHashData("md5", first
->data
, first
->datalen
, md5_digest
, sizeof(md5_digest
));
908 snprintf(buffer
, bufsize
, "%s (issued by %s) / %s / %s / %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", commonName
, issuer
, httpGetDateString(expiration
), sigalg
, md5_digest
[0], md5_digest
[1], md5_digest
[2], md5_digest
[3], md5_digest
[4], md5_digest
[5], md5_digest
[6], md5_digest
[7], md5_digest
[8], md5_digest
[9], md5_digest
[10], md5_digest
[11], md5_digest
[12], md5_digest
[13], md5_digest
[14], md5_digest
[15]);
913 DEBUG_printf(("1httpCredentialsString: Returning \"%s\".", buffer
));
915 return (strlen(buffer
));
920 * '_httpFreeCredentials()' - Free internal credentials.
924 _httpFreeCredentials(
925 http_tls_credentials_t credentials
) /* I - Internal credentials */
930 CFRelease(credentials
);
935 * 'httpLoadCredentials()' - Load X.509 credentials from a keychain file.
937 * @since CUPS 2.0/OS 10.10@
940 int /* O - 0 on success, -1 on error */
942 const char *path
, /* I - Keychain path or @code NULL@ for default */
943 cups_array_t
**credentials
, /* IO - Credentials */
944 const char *common_name
) /* I - Common name for credentials */
946 OSStatus err
; /* Error info */
948 char filename
[1024]; /* Filename for keychain */
949 SecKeychainRef keychain
= NULL
,/* Keychain reference */
950 syschain
= NULL
;/* System keychain */
951 CFArrayRef list
; /* Keychain list */
952 #endif /* TARGET_OS_OSX */
953 SecCertificateRef cert
= NULL
; /* Certificate */
954 CFDataRef data
; /* Certificate data */
955 SecPolicyRef policy
= NULL
; /* Policy ref */
956 CFStringRef cfcommon_name
= NULL
;
958 CFMutableDictionaryRef query
= NULL
; /* Query qualifiers */
961 DEBUG_printf(("httpLoadCredentials(path=\"%s\", credentials=%p, common_name=\"%s\")", path
, (void *)credentials
, common_name
));
969 keychain
= http_cdsa_open_keychain(path
, filename
, sizeof(filename
));
974 syschain
= http_cdsa_open_system_keychain();
979 #endif /* TARGET_OS_OSX */
981 cfcommon_name
= CFStringCreateWithCString(kCFAllocatorDefault
, common_name
, kCFStringEncodingUTF8
);
983 policy
= SecPolicyCreateSSL(1, cfcommon_name
);
986 CFRelease(cfcommon_name
);
991 if (!(query
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
)))
994 CFDictionaryAddValue(query
, kSecClass
, kSecClassCertificate
);
995 CFDictionaryAddValue(query
, kSecMatchPolicy
, policy
);
996 CFDictionaryAddValue(query
, kSecReturnRef
, kCFBooleanTrue
);
997 CFDictionaryAddValue(query
, kSecMatchLimit
, kSecMatchLimitOne
);
1002 const void *values
[2] = { syschain
, keychain
};
1004 list
= CFArrayCreate(kCFAllocatorDefault
, (const void **)values
, 2, &kCFTypeArrayCallBacks
);
1007 list
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&keychain
, 1, &kCFTypeArrayCallBacks
);
1008 CFDictionaryAddValue(query
, kSecMatchSearchList
, list
);
1010 #endif /* TARGET_OS_OSX */
1012 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&cert
);
1017 if (CFGetTypeID(cert
) != SecCertificateGetTypeID())
1020 if ((data
= SecCertificateCopyData(cert
)) != NULL
)
1022 DEBUG_printf(("1httpLoadCredentials: Adding %d byte certificate blob.", (int)CFDataGetLength(data
)));
1024 *credentials
= cupsArrayNew(NULL
, NULL
);
1025 httpAddCredential(*credentials
, CFDataGetBytePtr(data
), (size_t)CFDataGetLength(data
));
1033 CFRelease(keychain
);
1036 CFRelease(syschain
);
1037 #endif /* TARGET_OS_OSX */
1045 DEBUG_printf(("1httpLoadCredentials: Returning %d.", *credentials
? 0 : -1));
1047 return (*credentials
? 0 : -1);
1052 * 'httpSaveCredentials()' - Save X.509 credentials to a keychain file.
1054 * @since CUPS 2.0/OS 10.10@
1057 int /* O - -1 on error, 0 on success */
1058 httpSaveCredentials(
1059 const char *path
, /* I - Keychain path or @code NULL@ for default */
1060 cups_array_t
*credentials
, /* I - Credentials */
1061 const char *common_name
) /* I - Common name for credentials */
1063 int ret
= -1; /* Return value */
1064 OSStatus err
; /* Error info */
1066 char filename
[1024]; /* Filename for keychain */
1067 SecKeychainRef keychain
= NULL
;/* Keychain reference */
1068 CFArrayRef list
; /* Keychain list */
1069 #endif /* TARGET_OS_OSX */
1070 SecCertificateRef cert
= NULL
; /* Certificate */
1071 CFMutableDictionaryRef attrs
= NULL
; /* Attributes for add */
1074 DEBUG_printf(("httpSaveCredentials(path=\"%s\", credentials=%p, common_name=\"%s\")", path
, (void *)credentials
, common_name
));
1078 if (!httpCredentialsAreValidForName(credentials
, common_name
))
1080 DEBUG_puts("1httpSaveCredentials: Common name does not match.");
1084 if ((cert
= http_cdsa_create_credential((http_credential_t
*)cupsArrayFirst(credentials
))) == NULL
)
1086 DEBUG_puts("1httpSaveCredentials: Unable to create certificate.");
1091 keychain
= http_cdsa_open_keychain(path
, filename
, sizeof(filename
));
1099 #endif /* TARGET_OS_OSX */
1101 if ((attrs
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
)) == NULL
)
1103 DEBUG_puts("1httpSaveCredentials: Unable to create dictionary.");
1107 CFDictionaryAddValue(attrs
, kSecClass
, kSecClassCertificate
);
1108 CFDictionaryAddValue(attrs
, kSecValueRef
, cert
);
1111 if ((list
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&keychain
, 1, &kCFTypeArrayCallBacks
)) == NULL
)
1113 DEBUG_puts("1httpSaveCredentials: Unable to create list of keychains.");
1116 CFDictionaryAddValue(attrs
, kSecMatchSearchList
, list
);
1118 #endif /* TARGET_OS_OSX */
1120 /* Note: SecItemAdd consumes "attrs"... */
1121 err
= SecItemAdd(attrs
, NULL
);
1122 DEBUG_printf(("1httpSaveCredentials: SecItemAdd returned %d.", (int)err
));
1128 CFRelease(keychain
);
1129 #endif /* TARGET_OS_OSX */
1133 DEBUG_printf(("1httpSaveCredentials: Returning %d.", ret
));
1140 * '_httpTLSInitialize()' - Initialize the TLS stack.
1144 _httpTLSInitialize(void)
1153 * '_httpTLSPending()' - Return the number of pending TLS-encrypted bytes.
1157 _httpTLSPending(http_t
*http
) /* I - HTTP connection */
1159 size_t bytes
; /* Bytes that are available */
1162 if (!SSLGetBufferedReadSize(http
->tls
, &bytes
))
1170 * '_httpTLSRead()' - Read from a SSL/TLS connection.
1173 int /* O - Bytes read */
1174 _httpTLSRead(http_t
*http
, /* I - HTTP connection */
1175 char *buf
, /* I - Buffer to store data */
1176 int len
) /* I - Length of buffer */
1178 int result
; /* Return value */
1179 OSStatus error
; /* Error info */
1180 size_t processed
; /* Number of bytes processed */
1183 error
= SSLRead(http
->tls
, buf
, (size_t)len
, &processed
);
1184 DEBUG_printf(("6_httpTLSRead: error=%d, processed=%d", (int)error
,
1189 result
= (int)processed
;
1192 case errSSLWouldBlock
:
1194 result
= (int)processed
;
1202 case errSSLClosedGraceful
:
1205 result
= (int)processed
;
1219 * '_httpTLSSetOptions()' - Set TLS protocol and cipher suite options.
1223 _httpTLSSetOptions(int options
, /* I - Options */
1224 int min_version
, /* I - Minimum TLS version */
1225 int max_version
) /* I - Maximum TLS version */
1227 if (!(options
& _HTTP_TLS_SET_DEFAULT
) || tls_options
< 0)
1229 tls_options
= options
;
1230 tls_min_version
= min_version
;
1231 tls_max_version
= max_version
;
1237 * '_httpTLSStart()' - Set up SSL/TLS support on a connection.
1240 int /* O - 0 on success, -1 on failure */
1241 _httpTLSStart(http_t
*http
) /* I - HTTP connection */
1243 char hostname
[256], /* Hostname */
1244 *hostptr
; /* Pointer into hostname */
1245 _cups_globals_t
*cg
= _cupsGlobals();
1246 /* Pointer to library globals */
1247 OSStatus error
; /* Error code */
1248 const char *message
= NULL
;/* Error message */
1249 char msgbuf
[1024]; /* Error message buffer */
1250 cups_array_t
*credentials
; /* Credentials array */
1251 cups_array_t
*names
; /* CUPS distinguished names */
1252 CFArrayRef dn_array
; /* CF distinguished names array */
1253 CFIndex count
; /* Number of credentials */
1254 CFDataRef data
; /* Certificate data */
1255 int i
; /* Looping var */
1256 http_credential_t
*credential
; /* Credential data */
1259 DEBUG_printf(("3_httpTLSStart(http=%p)", (void *)http
));
1261 if (tls_options
< 0)
1263 DEBUG_puts("4_httpTLSStart: Setting defaults.");
1265 DEBUG_printf(("4_httpTLSStart: tls_options=%x, tls_min_version=%d, tls_max_version=%d", tls_options
, tls_min_version
, tls_max_version
));
1269 if (http
->mode
== _HTTP_MODE_SERVER
&& !tls_keychain
)
1271 DEBUG_puts("4_httpTLSStart: cupsSetServerCredentials not called.");
1272 http
->error
= errno
= EINVAL
;
1273 http
->status
= HTTP_STATUS_ERROR
;
1274 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Server credentials not set."), 1);
1278 #endif /* TARGET_OS_OSX */
1280 if ((http
->tls
= SSLCreateContext(kCFAllocatorDefault
, http
->mode
== _HTTP_MODE_CLIENT
? kSSLClientSide
: kSSLServerSide
, kSSLStreamType
)) == NULL
)
1282 DEBUG_puts("4_httpTLSStart: SSLCreateContext failed.");
1283 http
->error
= errno
= ENOMEM
;
1284 http
->status
= HTTP_STATUS_ERROR
;
1285 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
1290 error
= SSLSetConnection(http
->tls
, http
);
1291 DEBUG_printf(("4_httpTLSStart: SSLSetConnection, error=%d", (int)error
));
1295 error
= SSLSetIOFuncs(http
->tls
, http_cdsa_read
, http_cdsa_write
);
1296 DEBUG_printf(("4_httpTLSStart: SSLSetIOFuncs, error=%d", (int)error
));
1301 error
= SSLSetSessionOption(http
->tls
, kSSLSessionOptionBreakOnServerAuth
,
1303 DEBUG_printf(("4_httpTLSStart: SSLSetSessionOption, error=%d", (int)error
));
1308 static const SSLProtocol protocols
[] = /* Min/max protocol versions */
1317 if (tls_min_version
< _HTTP_TLS_MAX
)
1319 error
= SSLSetProtocolVersionMin(http
->tls
, protocols
[tls_min_version
]);
1320 DEBUG_printf(("4_httpTLSStart: SSLSetProtocolVersionMin(%d), error=%d", protocols
[tls_min_version
], (int)error
));
1323 if (!error
&& tls_max_version
< _HTTP_TLS_MAX
)
1325 error
= SSLSetProtocolVersionMax(http
->tls
, protocols
[tls_max_version
]);
1326 DEBUG_printf(("4_httpTLSStart: SSLSetProtocolVersionMax(%d), error=%d", protocols
[tls_max_version
], (int)error
));
1332 SSLCipherSuite supported
[100]; /* Supported cipher suites */
1333 size_t num_supported
; /* Number of supported cipher suites */
1334 SSLCipherSuite enabled
[100]; /* Cipher suites to enable */
1335 size_t num_enabled
; /* Number of cipher suites to enable */
1337 num_supported
= sizeof(supported
) / sizeof(supported
[0]);
1338 error
= SSLGetSupportedCiphers(http
->tls
, supported
, &num_supported
);
1342 DEBUG_printf(("4_httpTLSStart: %d cipher suites supported.", (int)num_supported
));
1344 for (i
= 0, num_enabled
= 0; i
< (int)num_supported
&& num_enabled
< (sizeof(enabled
) / sizeof(enabled
[0])); i
++)
1346 switch (supported
[i
])
1348 /* Obviously insecure cipher suites that we never want to use */
1349 case SSL_NULL_WITH_NULL_NULL
:
1350 case SSL_RSA_WITH_NULL_MD5
:
1351 case SSL_RSA_WITH_NULL_SHA
:
1352 case SSL_RSA_EXPORT_WITH_RC4_40_MD5
:
1353 case SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
:
1354 case SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
:
1355 case SSL_RSA_WITH_DES_CBC_SHA
:
1356 case SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
:
1357 case SSL_DH_DSS_WITH_DES_CBC_SHA
:
1358 case SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
:
1359 case SSL_DH_RSA_WITH_DES_CBC_SHA
:
1360 case SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
:
1361 case SSL_DHE_DSS_WITH_DES_CBC_SHA
:
1362 case SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
:
1363 case SSL_DHE_RSA_WITH_DES_CBC_SHA
:
1364 case SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
:
1365 case SSL_DH_anon_WITH_RC4_128_MD5
:
1366 case SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
:
1367 case SSL_DH_anon_WITH_DES_CBC_SHA
:
1368 case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
:
1369 case SSL_FORTEZZA_DMS_WITH_NULL_SHA
:
1370 case TLS_DH_anon_WITH_AES_128_CBC_SHA
:
1371 case TLS_DH_anon_WITH_AES_256_CBC_SHA
:
1372 case TLS_ECDH_ECDSA_WITH_NULL_SHA
:
1373 case TLS_ECDHE_RSA_WITH_NULL_SHA
:
1374 case TLS_ECDH_anon_WITH_NULL_SHA
:
1375 case TLS_ECDH_anon_WITH_RC4_128_SHA
:
1376 case TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA
:
1377 case TLS_ECDH_anon_WITH_AES_128_CBC_SHA
:
1378 case TLS_ECDH_anon_WITH_AES_256_CBC_SHA
:
1379 case TLS_RSA_WITH_NULL_SHA256
:
1380 case TLS_DH_anon_WITH_AES_128_CBC_SHA256
:
1381 case TLS_DH_anon_WITH_AES_256_CBC_SHA256
:
1382 case TLS_PSK_WITH_NULL_SHA
:
1383 case TLS_DHE_PSK_WITH_NULL_SHA
:
1384 case TLS_RSA_PSK_WITH_NULL_SHA
:
1385 case TLS_DH_anon_WITH_AES_128_GCM_SHA256
:
1386 case TLS_DH_anon_WITH_AES_256_GCM_SHA384
:
1387 case TLS_PSK_WITH_NULL_SHA256
:
1388 case TLS_PSK_WITH_NULL_SHA384
:
1389 case TLS_DHE_PSK_WITH_NULL_SHA256
:
1390 case TLS_DHE_PSK_WITH_NULL_SHA384
:
1391 case TLS_RSA_PSK_WITH_NULL_SHA256
:
1392 case TLS_RSA_PSK_WITH_NULL_SHA384
:
1393 case SSL_RSA_WITH_DES_CBC_MD5
:
1394 DEBUG_printf(("4_httpTLSStart: Excluding insecure cipher suite %d", supported
[i
]));
1397 /* RC4 cipher suites that should only be used as a last resort */
1398 case SSL_RSA_WITH_RC4_128_MD5
:
1399 case SSL_RSA_WITH_RC4_128_SHA
:
1400 case TLS_ECDH_ECDSA_WITH_RC4_128_SHA
:
1401 case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
:
1402 case TLS_ECDH_RSA_WITH_RC4_128_SHA
:
1403 case TLS_ECDHE_RSA_WITH_RC4_128_SHA
:
1404 case TLS_PSK_WITH_RC4_128_SHA
:
1405 case TLS_DHE_PSK_WITH_RC4_128_SHA
:
1406 case TLS_RSA_PSK_WITH_RC4_128_SHA
:
1407 if (tls_options
& _HTTP_TLS_ALLOW_RC4
)
1408 enabled
[num_enabled
++] = supported
[i
];
1410 DEBUG_printf(("4_httpTLSStart: Excluding RC4 cipher suite %d", supported
[i
]));
1413 /* DH/DHE cipher suites that are problematic with parameters < 1024 bits */
1414 case TLS_DH_DSS_WITH_AES_128_CBC_SHA
:
1415 case TLS_DH_RSA_WITH_AES_128_CBC_SHA
:
1416 case TLS_DHE_DSS_WITH_AES_128_CBC_SHA
:
1417 case TLS_DHE_RSA_WITH_AES_128_CBC_SHA
:
1418 case TLS_DH_DSS_WITH_AES_256_CBC_SHA
:
1419 case TLS_DH_RSA_WITH_AES_256_CBC_SHA
:
1420 case TLS_DHE_DSS_WITH_AES_256_CBC_SHA
:
1421 case TLS_DHE_RSA_WITH_AES_256_CBC_SHA
:
1422 case TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA
:
1423 case TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA
:
1424 case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
:
1425 case TLS_DH_DSS_WITH_AES_128_CBC_SHA256
:
1426 case TLS_DH_RSA_WITH_AES_128_CBC_SHA256
:
1427 case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
:
1428 case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
:
1429 case TLS_DH_DSS_WITH_AES_256_CBC_SHA256
:
1430 case TLS_DH_RSA_WITH_AES_256_CBC_SHA256
:
1431 case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
:
1432 case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
:
1433 case TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA
:
1434 case TLS_DHE_PSK_WITH_AES_128_CBC_SHA
:
1435 case TLS_DHE_PSK_WITH_AES_256_CBC_SHA
:
1436 case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
:
1437 case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
:
1438 if (tls_options
& _HTTP_TLS_DENY_CBC
)
1440 DEBUG_printf(("4_httpTLSStart: Excluding CBC cipher suite %d", supported
[i
]));
1444 // case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 :
1445 // case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 :
1446 case TLS_DH_RSA_WITH_AES_128_GCM_SHA256
:
1447 case TLS_DH_RSA_WITH_AES_256_GCM_SHA384
:
1448 // case TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 :
1449 // case TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 :
1450 case TLS_DH_DSS_WITH_AES_128_GCM_SHA256
:
1451 case TLS_DH_DSS_WITH_AES_256_GCM_SHA384
:
1452 case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
:
1453 case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
:
1454 if (tls_options
& _HTTP_TLS_ALLOW_DH
)
1455 enabled
[num_enabled
++] = supported
[i
];
1457 DEBUG_printf(("4_httpTLSStart: Excluding DH/DHE cipher suite %d", supported
[i
]));
1460 case TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA
:
1461 case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
:
1462 case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
:
1463 case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
:
1464 case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
:
1465 case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
:
1466 case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
:
1467 case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
:
1468 case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
:
1469 case TLS_RSA_WITH_3DES_EDE_CBC_SHA
:
1470 case TLS_RSA_WITH_AES_128_CBC_SHA
:
1471 case TLS_RSA_WITH_AES_256_CBC_SHA
:
1472 if (tls_options
& _HTTP_TLS_DENY_CBC
)
1474 DEBUG_printf(("4_httpTLSStart: Excluding CBC cipher suite %d", supported
[i
]));
1478 /* Anything else we'll assume is "secure" */
1480 enabled
[num_enabled
++] = supported
[i
];
1485 DEBUG_printf(("4_httpTLSStart: %d cipher suites enabled.", (int)num_enabled
));
1486 error
= SSLSetEnabledCiphers(http
->tls
, enabled
, num_enabled
);
1490 if (!error
&& http
->mode
== _HTTP_MODE_CLIENT
)
1493 * Client: set client-side credentials, if any...
1496 if (cg
->client_cert_cb
)
1498 error
= SSLSetSessionOption(http
->tls
,
1499 kSSLSessionOptionBreakOnCertRequested
, true);
1500 DEBUG_printf(("4_httpTLSStart: kSSLSessionOptionBreakOnCertRequested, "
1501 "error=%d", (int)error
));
1505 error
= http_cdsa_set_credentials(http
);
1506 DEBUG_printf(("4_httpTLSStart: http_cdsa_set_credentials, error=%d",
1513 * Server: find/create a certificate for TLS...
1516 if (http
->fields
[HTTP_FIELD_HOST
])
1519 * Use hostname for TLS upgrade...
1522 strlcpy(hostname
, http
->fields
[HTTP_FIELD_HOST
], sizeof(hostname
));
1527 * Resolve hostname from connection address...
1530 http_addr_t addr
; /* Connection address */
1531 socklen_t addrlen
; /* Length of address */
1533 addrlen
= sizeof(addr
);
1534 if (getsockname(http
->fd
, (struct sockaddr
*)&addr
, &addrlen
))
1536 DEBUG_printf(("4_httpTLSStart: Unable to get socket address: %s", strerror(errno
)));
1539 else if (httpAddrLocalhost(&addr
))
1543 httpAddrLookup(&addr
, hostname
, sizeof(hostname
));
1544 DEBUG_printf(("4_httpTLSStart: Resolved socket address to \"%s\".", hostname
));
1548 if (isdigit(hostname
[0] & 255) || hostname
[0] == '[')
1549 hostname
[0] = '\0'; /* Don't allow numeric addresses */
1552 http
->tls_credentials
= http_cdsa_copy_server(hostname
);
1553 else if (tls_common_name
)
1554 http
->tls_credentials
= http_cdsa_copy_server(tls_common_name
);
1556 if (!http
->tls_credentials
&& tls_auto_create
&& (hostname
[0] || tls_common_name
))
1558 DEBUG_printf(("4_httpTLSStart: Auto-create credentials for \"%s\".", hostname
[0] ? hostname
: tls_common_name
));
1560 if (!cupsMakeServerCredentials(tls_keypath
, hostname
[0] ? hostname
: tls_common_name
, 0, NULL
, time(NULL
) + 365 * 86400))
1562 DEBUG_puts("4_httpTLSStart: cupsMakeServerCredentials failed.");
1563 http
->error
= errno
= EINVAL
;
1564 http
->status
= HTTP_STATUS_ERROR
;
1565 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to create server credentials."), 1);
1570 http
->tls_credentials
= http_cdsa_copy_server(hostname
[0] ? hostname
: tls_common_name
);
1573 if (!http
->tls_credentials
)
1575 DEBUG_puts("4_httpTLSStart: Unable to find server credentials.");
1576 http
->error
= errno
= EINVAL
;
1577 http
->status
= HTTP_STATUS_ERROR
;
1578 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to find server credentials."), 1);
1583 error
= SSLSetCertificate(http
->tls
, http
->tls_credentials
);
1585 DEBUG_printf(("4_httpTLSStart: SSLSetCertificate, error=%d", (int)error
));
1588 DEBUG_printf(("4_httpTLSStart: tls_credentials=%p", (void *)http
->tls_credentials
));
1591 * Let the server know which hostname/domain we are trying to connect to
1592 * in case it wants to serve up a certificate with a matching common name.
1595 if (!error
&& http
->mode
== _HTTP_MODE_CLIENT
)
1598 * Client: get the hostname to use for TLS...
1601 if (httpAddrLocalhost(http
->hostaddr
))
1603 strlcpy(hostname
, "localhost", sizeof(hostname
));
1608 * Otherwise make sure the hostname we have does not end in a trailing dot.
1611 strlcpy(hostname
, http
->hostname
, sizeof(hostname
));
1612 if ((hostptr
= hostname
+ strlen(hostname
) - 1) >= hostname
&&
1617 error
= SSLSetPeerDomainName(http
->tls
, hostname
, strlen(hostname
));
1619 DEBUG_printf(("4_httpTLSStart: SSLSetPeerDomainName, error=%d", (int)error
));
1624 int done
= 0; /* Are we done yet? */
1625 double old_timeout
; /* Old timeout value */
1626 http_timeout_cb_t old_cb
; /* Old timeout callback */
1627 void *old_data
; /* Old timeout data */
1630 * Enforce a minimum timeout of 10 seconds for the TLS handshake...
1633 old_timeout
= http
->timeout_value
;
1634 old_cb
= http
->timeout_cb
;
1635 old_data
= http
->timeout_data
;
1637 if (!old_cb
|| old_timeout
< 10.0)
1639 DEBUG_puts("4_httpTLSStart: Setting timeout to 10 seconds.");
1640 httpSetTimeout(http
, 10.0, NULL
, NULL
);
1644 * Do the TLS handshake...
1647 while (!error
&& !done
)
1649 error
= SSLHandshake(http
->tls
);
1651 DEBUG_printf(("4_httpTLSStart: SSLHandshake returned %d.", (int)error
));
1659 case errSSLWouldBlock
:
1660 error
= noErr
; /* Force a retry */
1661 usleep(1000); /* in 1 millisecond */
1664 case errSSLServerAuthCompleted
:
1666 if (cg
->server_cert_cb
)
1668 error
= httpCopyCredentials(http
, &credentials
);
1671 error
= (cg
->server_cert_cb
)(http
, http
->tls
, credentials
,
1672 cg
->server_cert_data
);
1673 httpFreeCredentials(credentials
);
1676 DEBUG_printf(("4_httpTLSStart: Server certificate callback "
1677 "returned %d.", (int)error
));
1681 case errSSLClientCertRequested
:
1684 if (cg
->client_cert_cb
)
1687 if (!(error
= SSLCopyDistinguishedNames(http
->tls
, &dn_array
)) &&
1690 if ((names
= cupsArrayNew(NULL
, NULL
)) != NULL
)
1692 for (i
= 0, count
= CFArrayGetCount(dn_array
); i
< count
; i
++)
1694 data
= (CFDataRef
)CFArrayGetValueAtIndex(dn_array
, i
);
1696 if ((credential
= malloc(sizeof(*credential
))) != NULL
)
1698 credential
->datalen
= (size_t)CFDataGetLength(data
);
1699 if ((credential
->data
= malloc(credential
->datalen
)))
1701 memcpy((void *)credential
->data
, CFDataGetBytePtr(data
),
1702 credential
->datalen
);
1703 cupsArrayAdd(names
, credential
);
1711 CFRelease(dn_array
);
1716 error
= (cg
->client_cert_cb
)(http
, http
->tls
, names
,
1717 cg
->client_cert_data
);
1719 DEBUG_printf(("4_httpTLSStart: Client certificate callback "
1720 "returned %d.", (int)error
));
1723 httpFreeCredentials(names
);
1727 case errSSLUnknownRootCert
:
1728 message
= _("Unable to establish a secure connection to host "
1729 "(untrusted certificate).");
1732 case errSSLNoRootCert
:
1733 message
= _("Unable to establish a secure connection to host "
1734 "(self-signed certificate).");
1737 case errSSLCertExpired
:
1738 message
= _("Unable to establish a secure connection to host "
1739 "(expired certificate).");
1742 case errSSLCertNotYetValid
:
1743 message
= _("Unable to establish a secure connection to host "
1744 "(certificate not yet valid).");
1747 case errSSLHostNameMismatch
:
1748 message
= _("Unable to establish a secure connection to host "
1749 "(host name mismatch).");
1752 case errSSLXCertChainInvalid
:
1753 message
= _("Unable to establish a secure connection to host "
1754 "(certificate chain invalid).");
1757 case errSSLConnectionRefused
:
1758 message
= _("Unable to establish a secure connection to host "
1759 "(peer dropped connection before responding).");
1768 * Restore the previous timeout settings...
1771 httpSetTimeout(http
, old_timeout
, old_cb
, old_data
);
1776 http
->error
= error
;
1777 http
->status
= HTTP_STATUS_ERROR
;
1778 errno
= ECONNREFUSED
;
1780 CFRelease(http
->tls
);
1784 * If an error string wasn't set by the callbacks use a generic one...
1789 if (!cg
->lang_default
)
1790 cg
->lang_default
= cupsLangDefault();
1792 snprintf(msgbuf
, sizeof(msgbuf
), _cupsLangString(cg
->lang_default
, _("Unable to establish a secure connection to host (%d).")), error
);
1796 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, message
, 1);
1806 * '_httpTLSStop()' - Shut down SSL/TLS on a connection.
1810 _httpTLSStop(http_t
*http
) /* I - HTTP connection */
1812 while (SSLClose(http
->tls
) == errSSLWouldBlock
)
1815 CFRelease(http
->tls
);
1817 if (http
->tls_credentials
)
1818 CFRelease(http
->tls_credentials
);
1821 http
->tls_credentials
= NULL
;
1826 * '_httpTLSWrite()' - Write to a SSL/TLS connection.
1829 int /* O - Bytes written */
1830 _httpTLSWrite(http_t
*http
, /* I - HTTP connection */
1831 const char *buf
, /* I - Buffer holding data */
1832 int len
) /* I - Length of buffer */
1834 ssize_t result
; /* Return value */
1835 OSStatus error
; /* Error info */
1836 size_t processed
; /* Number of bytes processed */
1839 DEBUG_printf(("2_httpTLSWrite(http=%p, buf=%p, len=%d)", (void *)http
, (void *)buf
, len
));
1841 error
= SSLWrite(http
->tls
, buf
, (size_t)len
, &processed
);
1846 result
= (int)processed
;
1849 case errSSLWouldBlock
:
1852 result
= (int)processed
;
1861 case errSSLClosedGraceful
:
1865 result
= (int)processed
;
1875 DEBUG_printf(("3_httpTLSWrite: Returning %d.", (int)result
));
1877 return ((int)result
);
1882 * 'http_cdsa_copy_server()' - Find and copy server credentials from the keychain.
1885 static CFArrayRef
/* O - Array of certificates or NULL */
1886 http_cdsa_copy_server(
1887 const char *common_name
) /* I - Server's hostname */
1890 OSStatus err
; /* Error info */
1891 SecIdentityRef identity
= NULL
;/* Identity */
1892 CFArrayRef certificates
= NULL
;
1893 /* Certificate array */
1894 SecPolicyRef policy
= NULL
; /* Policy ref */
1895 CFStringRef cfcommon_name
= NULL
;
1897 CFMutableDictionaryRef query
= NULL
; /* Query qualifiers */
1898 CFArrayRef list
= NULL
; /* Keychain list */
1899 SecKeychainRef syschain
= NULL
;/* System keychain */
1900 SecKeychainStatus status
= 0; /* Keychain status */
1903 DEBUG_printf(("3http_cdsa_copy_server(common_name=\"%s\")", common_name
));
1905 cfcommon_name
= CFStringCreateWithCString(kCFAllocatorDefault
, common_name
, kCFStringEncodingUTF8
);
1907 policy
= SecPolicyCreateSSL(1, cfcommon_name
);
1911 DEBUG_puts("4http_cdsa_copy_server: Unable to create SSL policy.");
1915 if (!(query
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
)))
1917 DEBUG_puts("4http_cdsa_copy_server: Unable to create query dictionary.");
1921 _cupsMutexLock(&tls_mutex
);
1923 err
= SecKeychainGetStatus(tls_keychain
, &status
);
1925 if (err
== noErr
&& !(status
& kSecUnlockStateStatus
) && tls_cups_keychain
)
1926 SecKeychainUnlock(tls_keychain
, _CUPS_CDSA_PASSLEN
, _CUPS_CDSA_PASSWORD
, TRUE
);
1928 CFDictionaryAddValue(query
, kSecClass
, kSecClassIdentity
);
1929 CFDictionaryAddValue(query
, kSecMatchPolicy
, policy
);
1930 CFDictionaryAddValue(query
, kSecReturnRef
, kCFBooleanTrue
);
1931 CFDictionaryAddValue(query
, kSecMatchLimit
, kSecMatchLimitOne
);
1933 syschain
= http_cdsa_open_system_keychain();
1937 const void *values
[2] = { syschain
, tls_keychain
};
1939 list
= CFArrayCreate(kCFAllocatorDefault
, (const void **)values
, 2, &kCFTypeArrayCallBacks
);
1942 list
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&tls_keychain
, 1, &kCFTypeArrayCallBacks
);
1944 CFDictionaryAddValue(query
, kSecMatchSearchList
, list
);
1947 err
= SecItemCopyMatching(query
, (CFTypeRef
*)&identity
);
1949 _cupsMutexUnlock(&tls_mutex
);
1953 DEBUG_printf(("4http_cdsa_copy_server: SecItemCopyMatching failed with status %d.", (int)err
));
1957 if (CFGetTypeID(identity
) != SecIdentityGetTypeID())
1959 DEBUG_puts("4http_cdsa_copy_server: Search returned something that is not an identity.");
1963 if ((certificates
= CFArrayCreate(NULL
, (const void **)&identity
, 1, &kCFTypeArrayCallBacks
)) == NULL
)
1965 DEBUG_puts("4http_cdsa_copy_server: Unable to create array of certificates.");
1972 CFRelease(syschain
);
1974 CFRelease(identity
);
1978 CFRelease(cfcommon_name
);
1982 DEBUG_printf(("4http_cdsa_copy_server: Returning %p.", (void *)certificates
));
1984 return (certificates
);
1989 if (!tls_selfsigned
)
1992 return (CFArrayCreate(NULL
, (const void **)&tls_selfsigned
, 1, &kCFTypeArrayCallBacks
));
1993 #endif /* TARGET_OS_OSX */
1998 * 'http_cdsa_create_credential()' - Create a single credential in the internal format.
2001 static SecCertificateRef
/* O - Certificate */
2002 http_cdsa_create_credential(
2003 http_credential_t
*credential
) /* I - Credential */
2005 SecCertificateRef cert
; /* Certificate */
2006 CFDataRef data
; /* Data object */
2012 data
= CFDataCreate(kCFAllocatorDefault
, credential
->data
, (CFIndex
)credential
->datalen
);
2013 cert
= SecCertificateCreateWithData(kCFAllocatorDefault
, data
);
2022 * 'http_cdsa_default_path()' - Get the default keychain path.
2025 static const char * /* O - Keychain path */
2026 http_cdsa_default_path(char *buffer
, /* I - Path buffer */
2027 size_t bufsize
) /* I - Size of buffer */
2029 const char *home
= getenv("HOME"); /* HOME environment variable */
2033 * Determine the default keychain path. Note that the login and system
2034 * keychains are no longer accessible to user applications starting in macOS
2035 * 10.11.4 (!), so we need to create our own keychain just for CUPS.
2038 if (getuid() && home
)
2039 snprintf(buffer
, bufsize
, "%s/.cups/ssl.keychain", home
);
2041 strlcpy(buffer
, "/etc/cups/ssl.keychain", bufsize
);
2043 DEBUG_printf(("1http_cdsa_default_path: Using default path \"%s\".", buffer
));
2050 * 'http_cdsa_open_keychain()' - Open (or create) a keychain.
2053 static SecKeychainRef
/* O - Keychain or NULL */
2054 http_cdsa_open_keychain(
2055 const char *path
, /* I - Path to keychain */
2056 char *filename
, /* I - Keychain filename */
2057 size_t filesize
) /* I - Size of filename buffer */
2059 SecKeychainRef keychain
= NULL
;/* Temporary keychain */
2060 OSStatus err
; /* Error code */
2061 Boolean interaction
; /* Interaction allowed? */
2062 SecKeychainStatus status
= 0; /* Keychain status */
2066 * Get the keychain filename...
2071 path
= http_cdsa_default_path(filename
, filesize
);
2072 tls_cups_keychain
= 1;
2076 strlcpy(filename
, path
, filesize
);
2077 tls_cups_keychain
= 0;
2081 * Save the interaction setting and disable while we open the keychain...
2084 SecKeychainGetUserInteractionAllowed(&interaction
);
2085 SecKeychainSetUserInteractionAllowed(FALSE
);
2087 if (access(path
, R_OK
) && tls_cups_keychain
)
2090 * Create a new keychain at the given path...
2093 err
= SecKeychainCreate(path
, _CUPS_CDSA_PASSLEN
, _CUPS_CDSA_PASSWORD
, FALSE
, NULL
, &keychain
);
2098 * Open the existing keychain and unlock as needed...
2101 err
= SecKeychainOpen(path
, &keychain
);
2104 err
= SecKeychainGetStatus(keychain
, &status
);
2106 if (err
== noErr
&& !(status
& kSecUnlockStateStatus
) && tls_cups_keychain
)
2107 err
= SecKeychainUnlock(keychain
, _CUPS_CDSA_PASSLEN
, _CUPS_CDSA_PASSWORD
, TRUE
);
2111 * Restore interaction setting...
2114 SecKeychainSetUserInteractionAllowed(interaction
);
2117 * Release the keychain if we had any errors...
2122 /* TODO: Set cups last error string */
2123 DEBUG_printf(("4http_cdsa_open_keychain: Unable to open keychain (%d), returning NULL.", (int)err
));
2127 CFRelease(keychain
);
2133 * Return the keychain or NULL...
2141 * 'http_cdsa_open_system_keychain()' - Open the System keychain.
2144 static SecKeychainRef
2145 http_cdsa_open_system_keychain(void)
2147 SecKeychainRef keychain
= NULL
;/* Temporary keychain */
2148 OSStatus err
; /* Error code */
2149 Boolean interaction
; /* Interaction allowed? */
2150 SecKeychainStatus status
= 0; /* Keychain status */
2154 * Save the interaction setting and disable while we open the keychain...
2157 SecKeychainGetUserInteractionAllowed(&interaction
);
2158 SecKeychainSetUserInteractionAllowed(TRUE
);
2160 err
= SecKeychainOpen("/Library/Keychains/System.keychain", &keychain
);
2163 err
= SecKeychainGetStatus(keychain
, &status
);
2165 if (err
== noErr
&& !(status
& kSecUnlockStateStatus
))
2166 err
= errSecInteractionNotAllowed
;
2169 * Restore interaction setting...
2172 SecKeychainSetUserInteractionAllowed(interaction
);
2175 * Release the keychain if we had any errors...
2180 /* TODO: Set cups last error string */
2181 DEBUG_printf(("4http_cdsa_open_system_keychain: Unable to open keychain (%d), returning NULL.", (int)err
));
2185 CFRelease(keychain
);
2191 * Return the keychain or NULL...
2196 #endif /* TARGET_OS_OSX */
2200 * 'http_cdsa_read()' - Read function for the CDSA library.
2203 static OSStatus
/* O - -1 on error, 0 on success */
2205 SSLConnectionRef connection
, /* I - SSL/TLS connection */
2206 void *data
, /* I - Data buffer */
2207 size_t *dataLength
) /* IO - Number of bytes */
2209 OSStatus result
; /* Return value */
2210 ssize_t bytes
; /* Number of bytes read */
2211 http_t
*http
; /* HTTP connection */
2214 http
= (http_t
*)connection
;
2216 if (!http
->blocking
|| http
->timeout_value
> 0.0)
2219 * Make sure we have data before we read...
2222 while (!_httpWait(http
, http
->wait_value
, 0))
2224 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
2227 http
->error
= ETIMEDOUT
;
2234 bytes
= recv(http
->fd
, data
, *dataLength
, 0);
2236 while (bytes
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
2238 if ((size_t)bytes
== *dataLength
)
2244 *dataLength
= (size_t)bytes
;
2245 result
= errSSLWouldBlock
;
2252 result
= errSSLClosedGraceful
;
2253 else if (errno
== EAGAIN
)
2254 result
= errSSLWouldBlock
;
2256 result
= errSSLClosedAbort
;
2264 * 'http_cdsa_set_credentials()' - Set the TLS credentials.
2267 static int /* O - Status of connection */
2268 http_cdsa_set_credentials(http_t
*http
) /* I - HTTP connection */
2270 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2271 OSStatus error
= 0; /* Error code */
2272 http_tls_credentials_t credentials
= NULL
;
2273 /* TLS credentials */
2276 DEBUG_printf(("7http_tls_set_credentials(%p)", (void *)http
));
2279 * Prefer connection specific credentials...
2282 if ((credentials
= http
->tls_credentials
) == NULL
)
2283 credentials
= cg
->tls_credentials
;
2287 error
= SSLSetCertificate(http
->tls
, credentials
);
2288 DEBUG_printf(("4http_tls_set_credentials: SSLSetCertificate, error=%d",
2292 DEBUG_puts("4http_tls_set_credentials: No credentials to set.");
2299 * 'http_cdsa_write()' - Write function for the CDSA library.
2302 static OSStatus
/* O - -1 on error, 0 on success */
2304 SSLConnectionRef connection
, /* I - SSL/TLS connection */
2305 const void *data
, /* I - Data buffer */
2306 size_t *dataLength
) /* IO - Number of bytes */
2308 OSStatus result
; /* Return value */
2309 ssize_t bytes
; /* Number of bytes read */
2310 http_t
*http
; /* HTTP connection */
2313 http
= (http_t
*)connection
;
2317 bytes
= write(http
->fd
, data
, *dataLength
);
2319 while (bytes
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
2321 if ((size_t)bytes
== *dataLength
)
2325 else if (bytes
>= 0)
2327 *dataLength
= (size_t)bytes
;
2328 result
= errSSLWouldBlock
;
2334 if (errno
== EAGAIN
)
2335 result
= errSSLWouldBlock
;
2337 result
= errSSLClosedAbort
;