2 * TLS support code for CUPS on macOS.
4 * Copyright © 2007-2019 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
11 /**** This file is included from tls.c ****/
14 * Include necessary headers...
18 #include "tls-darwin.h"
21 * Constants, very secure stuff...
24 #define _CUPS_CDSA_PASSWORD "42" /* CUPS keychain password */
25 #define _CUPS_CDSA_PASSLEN 2 /* Length of keychain password */
32 static int tls_auto_create = 0;
33 /* Auto-create self-signed certs? */
34 static char *tls_common_name = NULL;
35 /* Default common name */
37 static int tls_cups_keychain = 0;
38 /* Opened the CUPS keychain? */
39 static SecKeychainRef tls_keychain = NULL;
40 /* Server cert keychain */
42 static SecIdentityRef tls_selfsigned = NULL;
43 /* Temporary self-signed cert */
44 #endif /* TARGET_OS_OSX */
45 static char *tls_keypath = NULL;
46 /* Server cert keychain path */
47 static _cups_mutex_t tls_mutex = _CUPS_MUTEX_INITIALIZER;
48 /* Mutex for keychain/certs */
49 static int tls_options = -1,/* Options for TLS connections */
50 tls_min_version = _HTTP_TLS_1_0,
51 tls_max_version = _HTTP_TLS_MAX;
58 static CFArrayRef http_cdsa_copy_server(const char *common_name);
59 static SecCertificateRef http_cdsa_create_credential(http_credential_t *credential);
61 static const char *http_cdsa_default_path(char *buffer, size_t bufsize);
62 static SecKeychainRef http_cdsa_open_keychain(const char *path, char *filename, size_t filesize);
63 static SecKeychainRef http_cdsa_open_system_keychain(void);
64 #endif /* TARGET_OS_OSX */
65 static OSStatus http_cdsa_read(SSLConnectionRef connection, void *data, size_t *dataLength);
66 static int http_cdsa_set_credentials(http_t *http);
67 static OSStatus http_cdsa_write(SSLConnectionRef connection, const void *data, size_t *dataLength);
71 * 'cupsMakeServerCredentials()' - Make a self-signed certificate and private key pair.
73 * @since CUPS 2.0/OS 10.10@
76 int /* O - 1 on success, 0 on failure */
77 cupsMakeServerCredentials(
78 const char *path, /* I - Keychain path or @code NULL@ for default */
79 const char *common_name, /* I - Common name */
80 int num_alt_names, /* I - Number of subject alternate names */
81 const char **alt_names, /* I - Subject Alternate Names */
82 time_t expiration_date) /* I - Expiration date */
85 int pid, /* Process ID of command */
86 status, /* Status of command */
88 char command[1024], /* Command */
89 *argv[5], /* Command-line arguments */
90 *envp[1000], /* Environment variables */
91 days[32], /* CERTTOOL_EXPIRATION_DAYS env var */
92 keychain[1024], /* Keychain argument */
93 infofile[1024], /* Type-in information for cert */
94 filename[1024]; /* Default keychain path */
95 cups_file_t *fp; /* Seed/info file */
98 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));
104 path = http_cdsa_default_path(filename, sizeof(filename));
107 * Run the "certtool" command to generate a self-signed certificate...
110 if (!cupsFileFind("certtool", getenv("PATH"), 1, command, sizeof(command)))
114 * Create a file with the certificate information fields...
116 * Note: This assumes that the default questions are asked by the certtool
120 if ((fp = cupsTempFile2(infofile, sizeof(infofile))) == NULL)
124 "CUPS Self-Signed Certificate\n"
125 /* Enter key and certificate label */
126 "r\n" /* Generate RSA key pair */
127 "2048\n" /* 2048 bit encryption key */
128 "y\n" /* OK (y = yes) */
129 "b\n" /* Usage (b=signing/encryption) */
130 "2\n" /* Sign with SHA256 */
131 "y\n" /* OK (y = yes) */
132 "%s\n" /* Common name */
133 "\n" /* Country (default) */
134 "\n" /* Organization (default) */
135 "\n" /* Organizational unit (default) */
136 "\n" /* State/Province (default) */
137 "\n" /* Email address */
138 "y\n", /* OK (y = yes) */
142 snprintf(keychain, sizeof(keychain), "k=%s", path);
144 argv[0] = "certtool";
149 snprintf(days, sizeof(days), "CERTTOOL_EXPIRATION_DAYS=%d", (int)((expiration_date - time(NULL) + 86399) / 86400));
151 for (i = 0; i < (int)(sizeof(envp) / sizeof(envp[0]) - 2) && environ[i]; i ++)
152 envp[i + 1] = environ[i];
155 posix_spawn_file_actions_t actions; /* File actions */
157 posix_spawn_file_actions_init(&actions);
158 posix_spawn_file_actions_addclose(&actions, 0);
159 posix_spawn_file_actions_addopen(&actions, 0, infofile, O_RDONLY, 0);
160 posix_spawn_file_actions_addclose(&actions, 1);
161 posix_spawn_file_actions_addopen(&actions, 1, "/dev/null", O_WRONLY, 0);
162 posix_spawn_file_actions_addclose(&actions, 2);
163 posix_spawn_file_actions_addopen(&actions, 2, "/dev/null", O_WRONLY, 0);
165 if (posix_spawn(&pid, command, &actions, NULL, argv, envp))
171 posix_spawn_file_actions_destroy(&actions);
175 while (waitpid(pid, &status, 0) < 0)
185 int status = 0; /* Return status */
186 OSStatus err; /* Error code (if any) */
187 CFStringRef cfcommon_name = NULL;
188 /* CF string for server name */
189 SecIdentityRef ident = NULL; /* Identity */
190 SecKeyRef publicKey = NULL,
194 SecCertificateRef cert = NULL; /* Self-signed certificate */
195 CFMutableDictionaryRef keyParams = NULL;
196 /* Key generation parameters */
199 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));
204 (void)expiration_date;
208 DEBUG_puts("1cupsMakeServerCredentials: No keychain support compiled in, returning 0.");
214 DEBUG_puts("1cupsMakeServerCredentials: Using existing self-signed cert.");
218 cfcommon_name = CFStringCreateWithCString(kCFAllocatorDefault, common_name, kCFStringEncodingUTF8);
221 DEBUG_puts("1cupsMakeServerCredentials: Unable to create CF string of common name.");
226 * Create a public/private key pair...
229 keyParams = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
232 DEBUG_puts("1cupsMakeServerCredentials: Unable to create key parameters dictionary.");
236 CFDictionaryAddValue(keyParams, kSecAttrKeyType, kSecAttrKeyTypeRSA);
237 CFDictionaryAddValue(keyParams, kSecAttrKeySizeInBits, CFSTR("2048"));
238 CFDictionaryAddValue(keyParams, kSecAttrLabel, cfcommon_name);
240 err = SecKeyGeneratePair(keyParams, &publicKey, &privateKey);
243 DEBUG_printf(("1cupsMakeServerCredentials: Unable to generate key pair: %d.", (int)err));
248 * Create a self-signed certificate using the public/private key pair...
251 CFIndex usageInt = kSecKeyUsageAll;
252 CFNumberRef usage = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &usageInt);
254 CFNumberRef len = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &lenInt);
255 CFTypeRef certKeys[] = { kSecCSRBasicContraintsPathLen, kSecSubjectAltName, kSecCertificateKeyUsage };
256 CFTypeRef certValues[] = { len, cfcommon_name, usage };
257 CFDictionaryRef certParams = CFDictionaryCreate(kCFAllocatorDefault, certKeys, certValues, sizeof(certKeys) / sizeof(certKeys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
261 const void *ca_o[] = { kSecOidOrganization, CFSTR("") };
262 const void *ca_cn[] = { kSecOidCommonName, cfcommon_name };
263 CFArrayRef ca_o_dn = CFArrayCreate(kCFAllocatorDefault, ca_o, 2, NULL);
264 CFArrayRef ca_cn_dn = CFArrayCreate(kCFAllocatorDefault, ca_cn, 2, NULL);
265 const void *ca_dn_array[2];
267 ca_dn_array[0] = CFArrayCreate(kCFAllocatorDefault, (const void **)&ca_o_dn, 1, NULL);
268 ca_dn_array[1] = CFArrayCreate(kCFAllocatorDefault, (const void **)&ca_cn_dn, 1, NULL);
270 CFArrayRef subject = CFArrayCreate(kCFAllocatorDefault, ca_dn_array, 2, NULL);
272 cert = SecGenerateSelfSignedCertificate(subject, certParams, publicKey, privateKey);
275 CFRelease(certParams);
279 DEBUG_puts("1cupsMakeServerCredentials: Unable to create self-signed certificate.");
283 ident = SecIdentityCreate(kCFAllocatorDefault, cert, privateKey);
287 _cupsMutexLock(&tls_mutex);
292 tls_selfsigned = ident;
294 _cupsMutexLock(&tls_mutex);
296 # if 0 /* Someday perhaps SecItemCopyMatching will work for identities, at which point */
297 CFTypeRef itemKeys[] = { kSecClass, kSecAttrLabel, kSecValueRef };
298 CFTypeRef itemValues[] = { kSecClassIdentity, cfcommon_name, ident };
299 CFDictionaryRef itemAttrs = CFDictionaryCreate(kCFAllocatorDefault, itemKeys, itemValues, sizeof(itemKeys) / sizeof(itemKeys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
301 err = SecItemAdd(itemAttrs, NULL);
302 /* SecItemAdd consumes itemAttrs... */
308 DEBUG_printf(("1cupsMakeServerCredentials: Unable to add identity to keychain: %d.", (int)err));
316 DEBUG_puts("1cupsMakeServerCredentials: Unable to create identity from cert and keys.");
319 * Cleanup and return...
325 CFRelease(cfcommon_name);
328 CFRelease(keyParams);
334 CFRelease(publicKey);
337 CFRelease(privateKey);
339 DEBUG_printf(("1cupsMakeServerCredentials: Returning %d.", status));
342 #endif /* TARGET_OS_OSX */
347 * 'cupsSetServerCredentials()' - Set the default server credentials.
349 * Note: The server credentials are used by all threads in the running process.
350 * This function is threadsafe.
352 * @since CUPS 2.0/macOS 10.10@
355 int /* O - 1 on success, 0 on failure */
356 cupsSetServerCredentials(
357 const char *path, /* I - Keychain path or @code NULL@ for default */
358 const char *common_name, /* I - Default common name for server */
359 int auto_create) /* I - 1 = automatically create self-signed certificates */
361 DEBUG_printf(("cupsSetServerCredentials(path=\"%s\", common_name=\"%s\", auto_create=%d)", path, common_name, auto_create));
364 char filename[1024]; /* Keychain filename */
365 SecKeychainRef keychain = http_cdsa_open_keychain(path, filename, sizeof(filename));
369 DEBUG_puts("1cupsSetServerCredentials: Unable to open keychain.");
373 _cupsMutexLock(&tls_mutex);
376 * Close any keychain that is currently open...
380 CFRelease(tls_keychain);
383 _cupsStrFree(tls_keypath);
386 _cupsStrFree(tls_common_name);
389 * Save the new keychain...
392 tls_keychain = keychain;
393 tls_keypath = _cupsStrAlloc(filename);
394 tls_auto_create = auto_create;
395 tls_common_name = _cupsStrAlloc(common_name);
397 _cupsMutexUnlock(&tls_mutex);
399 DEBUG_puts("1cupsSetServerCredentials: Opened keychain, returning 1.");
405 DEBUG_puts("1cupsSetServerCredentials: No keychain support compiled in, returning 0.");
409 tls_auto_create = auto_create;
410 tls_common_name = _cupsStrAlloc(common_name);
413 #endif /* TARGET_OS_OSX */
418 * 'httpCopyCredentials()' - Copy the credentials associated with the peer in
419 * an encrypted connection.
421 * @since CUPS 1.5/macOS 10.7@
424 int /* O - Status of call (0 = success) */
426 http_t *http, /* I - Connection to server */
427 cups_array_t **credentials) /* O - Array of credentials */
429 OSStatus error; /* Error code */
430 SecTrustRef peerTrust; /* Peer trust reference */
431 CFIndex count; /* Number of credentials */
432 SecCertificateRef secCert; /* Certificate reference */
433 CFDataRef data; /* Certificate data */
434 int i; /* Looping var */
437 DEBUG_printf(("httpCopyCredentials(http=%p, credentials=%p)", (void *)http, (void *)credentials));
442 if (!http || !http->tls || !credentials)
445 if (!(error = SSLCopyPeerTrust(http->tls, &peerTrust)) && peerTrust)
447 DEBUG_printf(("2httpCopyCredentials: Peer provided %d certificates.", (int)SecTrustGetCertificateCount(peerTrust)));
449 if ((*credentials = cupsArrayNew(NULL, NULL)) != NULL)
451 count = SecTrustGetCertificateCount(peerTrust);
453 for (i = 0; i < count; i ++)
455 secCert = SecTrustGetCertificateAtIndex(peerTrust, i);
458 CFStringRef cf_name = SecCertificateCopySubjectSummary(secCert);
461 CFStringGetCString(cf_name, name, sizeof(name), kCFStringEncodingUTF8);
463 strlcpy(name, "unknown", sizeof(name));
465 DEBUG_printf(("2httpCopyCredentials: Certificate %d name is \"%s\".", i, name));
468 if ((data = SecCertificateCopyData(secCert)) != NULL)
470 DEBUG_printf(("2httpCopyCredentials: Adding %d byte certificate blob.", (int)CFDataGetLength(data)));
472 httpAddCredential(*credentials, CFDataGetBytePtr(data), (size_t)CFDataGetLength(data));
478 CFRelease(peerTrust);
486 * '_httpCreateCredentials()' - Create credentials in the internal format.
489 http_tls_credentials_t /* O - Internal credentials */
490 _httpCreateCredentials(
491 cups_array_t *credentials) /* I - Array of credentials */
493 CFMutableArrayRef peerCerts; /* Peer credentials reference */
494 SecCertificateRef secCert; /* Certificate reference */
495 http_credential_t *credential; /* Credential data */
501 if ((peerCerts = CFArrayCreateMutable(kCFAllocatorDefault,
502 cupsArrayCount(credentials),
503 &kCFTypeArrayCallBacks)) == NULL)
506 for (credential = (http_credential_t *)cupsArrayFirst(credentials);
508 credential = (http_credential_t *)cupsArrayNext(credentials))
510 if ((secCert = http_cdsa_create_credential(credential)) != NULL)
512 CFArrayAppendValue(peerCerts, secCert);
522 * 'httpCredentialsAreValidForName()' - Return whether the credentials are valid for the given name.
524 * @since CUPS 2.0/macOS 10.10@
527 int /* O - 1 if valid, 0 otherwise */
528 httpCredentialsAreValidForName(
529 cups_array_t *credentials, /* I - Credentials */
530 const char *common_name) /* I - Name to check */
532 SecCertificateRef secCert; /* Certificate reference */
533 CFStringRef cfcert_name = NULL;
534 /* Certificate's common name (CF string) */
535 char cert_name[256]; /* Certificate's common name (C string) */
536 int valid = 1; /* Valid name? */
539 if ((secCert = http_cdsa_create_credential((http_credential_t *)cupsArrayFirst(credentials))) == NULL)
543 * Compare the common names...
546 if ((cfcert_name = SecCertificateCopySubjectSummary(secCert)) == NULL)
549 * Can't get common name, cannot be valid...
554 else if (CFStringGetCString(cfcert_name, cert_name, sizeof(cert_name), kCFStringEncodingUTF8) &&
555 _cups_strcasecmp(common_name, cert_name))
558 * Not an exact match for the common name, check for wildcard certs...
561 const char *domain = strchr(common_name, '.');
562 /* Domain in common name */
564 if (strncmp(cert_name, "*.", 2) || !domain || _cups_strcasecmp(domain, cert_name + 1))
567 * Not a wildcard match.
570 /* TODO: Check subject alternate names */
576 CFRelease(cfcert_name);
585 * 'httpCredentialsGetTrust()' - Return the trust of credentials.
587 * @since CUPS 2.0/macOS 10.10@
590 http_trust_t /* O - Level of trust */
591 httpCredentialsGetTrust(
592 cups_array_t *credentials, /* I - Credentials */
593 const char *common_name) /* I - Common name for trust lookup */
595 SecCertificateRef secCert; /* Certificate reference */
596 http_trust_t trust = HTTP_TRUST_OK;
598 cups_array_t *tcreds = NULL; /* Trusted credentials */
599 _cups_globals_t *cg = _cupsGlobals();
600 /* Per-thread globals */
605 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No common name specified."), 1);
606 return (HTTP_TRUST_UNKNOWN);
609 if ((secCert = http_cdsa_create_credential((http_credential_t *)cupsArrayFirst(credentials))) == NULL)
611 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create credentials from array."), 1);
612 return (HTTP_TRUST_UNKNOWN);
615 if (cg->any_root < 0)
619 * Look this common name up in the default keychains...
622 httpLoadCredentials(NULL, &tcreds, common_name);
626 char credentials_str[1024], /* String for incoming credentials */
627 tcreds_str[1024]; /* String for saved credentials */
629 httpCredentialsString(credentials, credentials_str, sizeof(credentials_str));
630 httpCredentialsString(tcreds, tcreds_str, sizeof(tcreds_str));
632 if (strcmp(credentials_str, tcreds_str))
635 * Credentials don't match, let's look at the expiration date of the new
636 * credentials and allow if the new ones have a later expiration...
639 if (!cg->trust_first)
642 * Do not trust certificates on first use...
645 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Trust on first use is disabled."), 1);
647 trust = HTTP_TRUST_INVALID;
649 else if (httpCredentialsGetExpiration(credentials) <= httpCredentialsGetExpiration(tcreds))
652 * The new credentials are not newly issued...
655 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("New credentials are older than stored credentials."), 1);
657 trust = HTTP_TRUST_INVALID;
659 else if (!httpCredentialsAreValidForName(credentials, common_name))
662 * The common name does not match the issued certificate...
665 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("New credentials are not valid for name."), 1);
667 trust = HTTP_TRUST_INVALID;
669 else if (httpCredentialsGetExpiration(tcreds) < time(NULL))
672 * Save the renewed credentials...
675 trust = HTTP_TRUST_RENEWED;
677 httpSaveCredentials(NULL, credentials, common_name);
681 httpFreeCredentials(tcreds);
683 else if (cg->validate_certs && !httpCredentialsAreValidForName(credentials, common_name))
685 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No stored credentials, not valid for name."), 1);
686 trust = HTTP_TRUST_INVALID;
688 else if (!cg->trust_first)
691 * See if we have a site CA certificate we can compare...
694 if (!httpLoadCredentials(NULL, &tcreds, "site"))
696 if (cupsArrayCount(credentials) != (cupsArrayCount(tcreds) + 1))
699 * Certificate isn't directly generated from the CA cert...
702 trust = HTTP_TRUST_INVALID;
707 * Do a tail comparison of the two certificates...
710 http_credential_t *a, *b; /* Certificates */
712 for (a = (http_credential_t *)cupsArrayFirst(tcreds), b = (http_credential_t *)cupsArrayIndex(credentials, 1);
714 a = (http_credential_t *)cupsArrayNext(tcreds), b = (http_credential_t *)cupsArrayNext(credentials))
715 if (a->datalen != b->datalen || memcmp(a->data, b->data, a->datalen))
719 trust = HTTP_TRUST_INVALID;
722 if (trust != HTTP_TRUST_OK)
723 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Credentials do not validate against site CA certificate."), 1);
727 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Trust on first use is disabled."), 1);
728 trust = HTTP_TRUST_INVALID;
732 if (trust == HTTP_TRUST_OK && !cg->expired_certs && !SecCertificateIsValid(secCert, CFAbsoluteTimeGetCurrent()))
734 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Credentials have expired."), 1);
735 trust = HTTP_TRUST_EXPIRED;
738 if (trust == HTTP_TRUST_OK && !cg->any_root && cupsArrayCount(credentials) == 1)
740 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Self-signed credentials are blocked."), 1);
741 trust = HTTP_TRUST_INVALID;
751 * 'httpCredentialsGetExpiration()' - Return the expiration date of the credentials.
753 * @since CUPS 2.0/macOS 10.10@
756 time_t /* O - Expiration date of credentials */
757 httpCredentialsGetExpiration(
758 cups_array_t *credentials) /* I - Credentials */
760 SecCertificateRef secCert; /* Certificate reference */
761 time_t expiration; /* Expiration date */
764 if ((secCert = http_cdsa_create_credential((http_credential_t *)cupsArrayFirst(credentials))) == NULL)
767 expiration = (time_t)(SecCertificateNotValidAfter(secCert) + kCFAbsoluteTimeIntervalSince1970);
776 * 'httpCredentialsString()' - Return a string representing the credentials.
778 * @since CUPS 2.0/macOS 10.10@
781 size_t /* O - Total size of credentials string */
782 httpCredentialsString(
783 cups_array_t *credentials, /* I - Credentials */
784 char *buffer, /* I - Buffer or @code NULL@ */
785 size_t bufsize) /* I - Size of buffer */
787 http_credential_t *first; /* First certificate */
788 SecCertificateRef secCert; /* Certificate reference */
791 DEBUG_printf(("httpCredentialsString(credentials=%p, buffer=%p, bufsize=" CUPS_LLFMT ")", (void *)credentials, (void *)buffer, CUPS_LLCAST bufsize));
796 if (buffer && bufsize > 0)
799 if ((first = (http_credential_t *)cupsArrayFirst(credentials)) != NULL &&
800 (secCert = http_cdsa_create_credential(first)) != NULL)
803 * Copy certificate (string) values from the SecCertificateRef and produce
804 * a one-line summary. The API for accessing certificate values like the
805 * issuer name is, um, "interesting"...
809 CFDictionaryRef cf_dict; /* Dictionary for certificate */
810 # endif /* TARGET_OS_OSX */
811 CFStringRef cf_string; /* CF string */
812 char commonName[256],/* Common name associated with cert */
813 issuer[256], /* Issuer name */
814 sigalg[256]; /* Signature algorithm */
815 time_t expiration; /* Expiration date of cert */
816 unsigned char md5_digest[16]; /* MD5 result */
818 if (SecCertificateCopyCommonName(secCert, &cf_string) == noErr)
820 CFStringGetCString(cf_string, commonName, (CFIndex)sizeof(commonName), kCFStringEncodingUTF8);
821 CFRelease(cf_string);
825 strlcpy(commonName, "unknown", sizeof(commonName));
828 strlcpy(issuer, "unknown", sizeof(issuer));
829 strlcpy(sigalg, "UnknownSignature", sizeof(sigalg));
832 if ((cf_dict = SecCertificateCopyValues(secCert, NULL, NULL)) != NULL)
834 CFDictionaryRef cf_issuer = CFDictionaryGetValue(cf_dict, kSecOIDX509V1IssuerName);
835 CFDictionaryRef cf_sigalg = CFDictionaryGetValue(cf_dict, kSecOIDX509V1SignatureAlgorithm);
839 CFArrayRef cf_values = CFDictionaryGetValue(cf_issuer, kSecPropertyKeyValue);
840 CFIndex i, count = CFArrayGetCount(cf_values);
841 CFDictionaryRef cf_value;
843 for (i = 0; i < count; i ++)
845 cf_value = CFArrayGetValueAtIndex(cf_values, i);
847 if (!CFStringCompare(CFDictionaryGetValue(cf_value, kSecPropertyKeyLabel), kSecOIDOrganizationName, kCFCompareCaseInsensitive))
848 CFStringGetCString(CFDictionaryGetValue(cf_value, kSecPropertyKeyValue), issuer, (CFIndex)sizeof(issuer), kCFStringEncodingUTF8);
854 CFArrayRef cf_values = CFDictionaryGetValue(cf_sigalg, kSecPropertyKeyValue);
855 CFIndex i, count = CFArrayGetCount(cf_values);
856 CFDictionaryRef cf_value;
858 for (i = 0; i < count; i ++)
860 cf_value = CFArrayGetValueAtIndex(cf_values, i);
862 if (!CFStringCompare(CFDictionaryGetValue(cf_value, kSecPropertyKeyLabel), CFSTR("Algorithm"), kCFCompareCaseInsensitive))
864 CFStringRef cf_algorithm = CFDictionaryGetValue(cf_value, kSecPropertyKeyValue);
866 if (!CFStringCompare(cf_algorithm, CFSTR("1.2.840.113549.1.1.5"), kCFCompareCaseInsensitive))
867 strlcpy(sigalg, "SHA1WithRSAEncryption", sizeof(sigalg));
868 else if (!CFStringCompare(cf_algorithm, CFSTR("1.2.840.113549.1.1.11"), kCFCompareCaseInsensitive))
869 strlcpy(sigalg, "SHA256WithRSAEncryption", sizeof(sigalg));
870 else if (!CFStringCompare(cf_algorithm, CFSTR("1.2.840.113549.1.1.4"), kCFCompareCaseInsensitive))
871 strlcpy(sigalg, "MD5WithRSAEncryption", sizeof(sigalg));
878 # endif /* TARGET_OS_OSX */
880 expiration = (time_t)(SecCertificateNotValidAfter(secCert) + kCFAbsoluteTimeIntervalSince1970);
882 cupsHashData("md5", first->data, first->datalen, md5_digest, sizeof(md5_digest));
884 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]);
889 DEBUG_printf(("1httpCredentialsString: Returning \"%s\".", buffer));
891 return (strlen(buffer));
896 * '_httpFreeCredentials()' - Free internal credentials.
900 _httpFreeCredentials(
901 http_tls_credentials_t credentials) /* I - Internal credentials */
906 CFRelease(credentials);
911 * 'httpLoadCredentials()' - Load X.509 credentials from a keychain file.
913 * @since CUPS 2.0/OS 10.10@
916 int /* O - 0 on success, -1 on error */
918 const char *path, /* I - Keychain path or @code NULL@ for default */
919 cups_array_t **credentials, /* IO - Credentials */
920 const char *common_name) /* I - Common name for credentials */
922 OSStatus err; /* Error info */
924 char filename[1024]; /* Filename for keychain */
925 SecKeychainRef keychain = NULL,/* Keychain reference */
926 syschain = NULL;/* System keychain */
927 CFArrayRef list; /* Keychain list */
928 #endif /* TARGET_OS_OSX */
929 SecCertificateRef cert = NULL; /* Certificate */
930 CFDataRef data; /* Certificate data */
931 SecPolicyRef policy = NULL; /* Policy ref */
932 CFStringRef cfcommon_name = NULL;
934 CFMutableDictionaryRef query = NULL; /* Query qualifiers */
937 DEBUG_printf(("httpLoadCredentials(path=\"%s\", credentials=%p, common_name=\"%s\")", path, (void *)credentials, common_name));
945 keychain = http_cdsa_open_keychain(path, filename, sizeof(filename));
950 syschain = http_cdsa_open_system_keychain();
955 #endif /* TARGET_OS_OSX */
957 cfcommon_name = CFStringCreateWithCString(kCFAllocatorDefault, common_name, kCFStringEncodingUTF8);
959 policy = SecPolicyCreateSSL(1, cfcommon_name);
962 CFRelease(cfcommon_name);
967 if (!(query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)))
970 CFDictionaryAddValue(query, kSecClass, kSecClassCertificate);
971 CFDictionaryAddValue(query, kSecMatchPolicy, policy);
972 CFDictionaryAddValue(query, kSecReturnRef, kCFBooleanTrue);
973 CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitOne);
978 const void *values[2] = { syschain, keychain };
980 list = CFArrayCreate(kCFAllocatorDefault, (const void **)values, 2, &kCFTypeArrayCallBacks);
983 list = CFArrayCreate(kCFAllocatorDefault, (const void **)&keychain, 1, &kCFTypeArrayCallBacks);
984 CFDictionaryAddValue(query, kSecMatchSearchList, list);
986 #endif /* TARGET_OS_OSX */
988 err = SecItemCopyMatching(query, (CFTypeRef *)&cert);
993 if (CFGetTypeID(cert) != SecCertificateGetTypeID())
996 if ((data = SecCertificateCopyData(cert)) != NULL)
998 DEBUG_printf(("1httpLoadCredentials: Adding %d byte certificate blob.", (int)CFDataGetLength(data)));
1000 *credentials = cupsArrayNew(NULL, NULL);
1001 httpAddCredential(*credentials, CFDataGetBytePtr(data), (size_t)CFDataGetLength(data));
1009 CFRelease(keychain);
1012 CFRelease(syschain);
1013 #endif /* TARGET_OS_OSX */
1021 DEBUG_printf(("1httpLoadCredentials: Returning %d.", *credentials ? 0 : -1));
1023 return (*credentials ? 0 : -1);
1028 * 'httpSaveCredentials()' - Save X.509 credentials to a keychain file.
1030 * @since CUPS 2.0/OS 10.10@
1033 int /* O - -1 on error, 0 on success */
1034 httpSaveCredentials(
1035 const char *path, /* I - Keychain path or @code NULL@ for default */
1036 cups_array_t *credentials, /* I - Credentials */
1037 const char *common_name) /* I - Common name for credentials */
1039 int ret = -1; /* Return value */
1040 OSStatus err; /* Error info */
1042 char filename[1024]; /* Filename for keychain */
1043 SecKeychainRef keychain = NULL;/* Keychain reference */
1044 CFArrayRef list; /* Keychain list */
1045 #endif /* TARGET_OS_OSX */
1046 SecCertificateRef cert = NULL; /* Certificate */
1047 CFMutableDictionaryRef attrs = NULL; /* Attributes for add */
1050 DEBUG_printf(("httpSaveCredentials(path=\"%s\", credentials=%p, common_name=\"%s\")", path, (void *)credentials, common_name));
1054 if (!httpCredentialsAreValidForName(credentials, common_name))
1056 DEBUG_puts("1httpSaveCredentials: Common name does not match.");
1060 if ((cert = http_cdsa_create_credential((http_credential_t *)cupsArrayFirst(credentials))) == NULL)
1062 DEBUG_puts("1httpSaveCredentials: Unable to create certificate.");
1067 keychain = http_cdsa_open_keychain(path, filename, sizeof(filename));
1075 #endif /* TARGET_OS_OSX */
1077 if ((attrs = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)) == NULL)
1079 DEBUG_puts("1httpSaveCredentials: Unable to create dictionary.");
1083 CFDictionaryAddValue(attrs, kSecClass, kSecClassCertificate);
1084 CFDictionaryAddValue(attrs, kSecValueRef, cert);
1087 if ((list = CFArrayCreate(kCFAllocatorDefault, (const void **)&keychain, 1, &kCFTypeArrayCallBacks)) == NULL)
1089 DEBUG_puts("1httpSaveCredentials: Unable to create list of keychains.");
1092 CFDictionaryAddValue(attrs, kSecMatchSearchList, list);
1094 #endif /* TARGET_OS_OSX */
1096 /* Note: SecItemAdd consumes "attrs"... */
1097 err = SecItemAdd(attrs, NULL);
1098 DEBUG_printf(("1httpSaveCredentials: SecItemAdd returned %d.", (int)err));
1104 CFRelease(keychain);
1105 #endif /* TARGET_OS_OSX */
1109 DEBUG_printf(("1httpSaveCredentials: Returning %d.", ret));
1116 * '_httpTLSInitialize()' - Initialize the TLS stack.
1120 _httpTLSInitialize(void)
1129 * '_httpTLSPending()' - Return the number of pending TLS-encrypted bytes.
1133 _httpTLSPending(http_t *http) /* I - HTTP connection */
1135 size_t bytes; /* Bytes that are available */
1138 if (!SSLGetBufferedReadSize(http->tls, &bytes))
1146 * '_httpTLSRead()' - Read from a SSL/TLS connection.
1149 int /* O - Bytes read */
1150 _httpTLSRead(http_t *http, /* I - HTTP connection */
1151 char *buf, /* I - Buffer to store data */
1152 int len) /* I - Length of buffer */
1154 int result; /* Return value */
1155 OSStatus error; /* Error info */
1156 size_t processed; /* Number of bytes processed */
1159 error = SSLRead(http->tls, buf, (size_t)len, &processed);
1160 DEBUG_printf(("6_httpTLSRead: error=%d, processed=%d", (int)error,
1165 result = (int)processed;
1168 case errSSLWouldBlock :
1170 result = (int)processed;
1178 case errSSLClosedGraceful :
1181 result = (int)processed;
1195 * '_httpTLSSetOptions()' - Set TLS protocol and cipher suite options.
1199 _httpTLSSetOptions(int options, /* I - Options */
1200 int min_version, /* I - Minimum TLS version */
1201 int max_version) /* I - Maximum TLS version */
1203 if (!(options & _HTTP_TLS_SET_DEFAULT) || tls_options < 0)
1205 tls_options = options;
1206 tls_min_version = min_version;
1207 tls_max_version = max_version;
1213 * '_httpTLSStart()' - Set up SSL/TLS support on a connection.
1216 int /* O - 0 on success, -1 on failure */
1217 _httpTLSStart(http_t *http) /* I - HTTP connection */
1219 char hostname[256], /* Hostname */
1220 *hostptr; /* Pointer into hostname */
1221 _cups_globals_t *cg = _cupsGlobals();
1222 /* Pointer to library globals */
1223 OSStatus error; /* Error code */
1224 const char *message = NULL;/* Error message */
1225 char msgbuf[1024]; /* Error message buffer */
1226 cups_array_t *credentials; /* Credentials array */
1227 cups_array_t *names; /* CUPS distinguished names */
1228 CFArrayRef dn_array; /* CF distinguished names array */
1229 CFIndex count; /* Number of credentials */
1230 CFDataRef data; /* Certificate data */
1231 int i; /* Looping var */
1232 http_credential_t *credential; /* Credential data */
1235 DEBUG_printf(("3_httpTLSStart(http=%p)", (void *)http));
1237 if (tls_options < 0)
1239 DEBUG_puts("4_httpTLSStart: Setting defaults.");
1241 DEBUG_printf(("4_httpTLSStart: tls_options=%x, tls_min_version=%d, tls_max_version=%d", tls_options, tls_min_version, tls_max_version));
1245 if (http->mode == _HTTP_MODE_SERVER && !tls_keychain)
1247 DEBUG_puts("4_httpTLSStart: cupsSetServerCredentials not called.");
1248 http->error = errno = EINVAL;
1249 http->status = HTTP_STATUS_ERROR;
1250 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Server credentials not set."), 1);
1254 #endif /* TARGET_OS_OSX */
1256 if ((http->tls = SSLCreateContext(kCFAllocatorDefault, http->mode == _HTTP_MODE_CLIENT ? kSSLClientSide : kSSLServerSide, kSSLStreamType)) == NULL)
1258 DEBUG_puts("4_httpTLSStart: SSLCreateContext failed.");
1259 http->error = errno = ENOMEM;
1260 http->status = HTTP_STATUS_ERROR;
1261 _cupsSetHTTPError(HTTP_STATUS_ERROR);
1266 error = SSLSetConnection(http->tls, http);
1267 DEBUG_printf(("4_httpTLSStart: SSLSetConnection, error=%d", (int)error));
1271 error = SSLSetIOFuncs(http->tls, http_cdsa_read, http_cdsa_write);
1272 DEBUG_printf(("4_httpTLSStart: SSLSetIOFuncs, error=%d", (int)error));
1277 error = SSLSetSessionOption(http->tls, kSSLSessionOptionBreakOnServerAuth,
1279 DEBUG_printf(("4_httpTLSStart: SSLSetSessionOption, error=%d", (int)error));
1284 static const SSLProtocol protocols[] = /* Min/max protocol versions */
1293 if (tls_min_version < _HTTP_TLS_MAX)
1295 error = SSLSetProtocolVersionMin(http->tls, protocols[tls_min_version]);
1296 DEBUG_printf(("4_httpTLSStart: SSLSetProtocolVersionMin(%d), error=%d", protocols[tls_min_version], (int)error));
1299 if (!error && tls_max_version < _HTTP_TLS_MAX)
1301 error = SSLSetProtocolVersionMax(http->tls, protocols[tls_max_version]);
1302 DEBUG_printf(("4_httpTLSStart: SSLSetProtocolVersionMax(%d), error=%d", protocols[tls_max_version], (int)error));
1308 SSLCipherSuite supported[100]; /* Supported cipher suites */
1309 size_t num_supported; /* Number of supported cipher suites */
1310 SSLCipherSuite enabled[100]; /* Cipher suites to enable */
1311 size_t num_enabled; /* Number of cipher suites to enable */
1313 num_supported = sizeof(supported) / sizeof(supported[0]);
1314 error = SSLGetSupportedCiphers(http->tls, supported, &num_supported);
1318 DEBUG_printf(("4_httpTLSStart: %d cipher suites supported.", (int)num_supported));
1320 for (i = 0, num_enabled = 0; i < (int)num_supported && num_enabled < (sizeof(enabled) / sizeof(enabled[0])); i ++)
1322 switch (supported[i])
1324 /* Obviously insecure cipher suites that we never want to use */
1325 case SSL_NULL_WITH_NULL_NULL :
1326 case SSL_RSA_WITH_NULL_MD5 :
1327 case SSL_RSA_WITH_NULL_SHA :
1328 case SSL_RSA_EXPORT_WITH_RC4_40_MD5 :
1329 case SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5 :
1330 case SSL_RSA_EXPORT_WITH_DES40_CBC_SHA :
1331 case SSL_RSA_WITH_DES_CBC_SHA :
1332 case SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA :
1333 case SSL_DH_DSS_WITH_DES_CBC_SHA :
1334 case SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA :
1335 case SSL_DH_RSA_WITH_DES_CBC_SHA :
1336 case SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA :
1337 case SSL_DHE_DSS_WITH_DES_CBC_SHA :
1338 case SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA :
1339 case SSL_DHE_RSA_WITH_DES_CBC_SHA :
1340 case SSL_DH_anon_EXPORT_WITH_RC4_40_MD5 :
1341 case SSL_DH_anon_WITH_RC4_128_MD5 :
1342 case SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA :
1343 case SSL_DH_anon_WITH_DES_CBC_SHA :
1344 case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA :
1345 case SSL_FORTEZZA_DMS_WITH_NULL_SHA :
1346 case TLS_DH_anon_WITH_AES_128_CBC_SHA :
1347 case TLS_DH_anon_WITH_AES_256_CBC_SHA :
1348 case TLS_ECDH_ECDSA_WITH_NULL_SHA :
1349 case TLS_ECDHE_RSA_WITH_NULL_SHA :
1350 case TLS_ECDH_anon_WITH_NULL_SHA :
1351 case TLS_ECDH_anon_WITH_RC4_128_SHA :
1352 case TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA :
1353 case TLS_ECDH_anon_WITH_AES_128_CBC_SHA :
1354 case TLS_ECDH_anon_WITH_AES_256_CBC_SHA :
1355 case TLS_RSA_WITH_NULL_SHA256 :
1356 case TLS_DH_anon_WITH_AES_128_CBC_SHA256 :
1357 case TLS_DH_anon_WITH_AES_256_CBC_SHA256 :
1358 case TLS_PSK_WITH_NULL_SHA :
1359 case TLS_DHE_PSK_WITH_NULL_SHA :
1360 case TLS_RSA_PSK_WITH_NULL_SHA :
1361 case TLS_DH_anon_WITH_AES_128_GCM_SHA256 :
1362 case TLS_DH_anon_WITH_AES_256_GCM_SHA384 :
1363 case TLS_PSK_WITH_NULL_SHA256 :
1364 case TLS_PSK_WITH_NULL_SHA384 :
1365 case TLS_DHE_PSK_WITH_NULL_SHA256 :
1366 case TLS_DHE_PSK_WITH_NULL_SHA384 :
1367 case TLS_RSA_PSK_WITH_NULL_SHA256 :
1368 case TLS_RSA_PSK_WITH_NULL_SHA384 :
1369 case SSL_RSA_WITH_DES_CBC_MD5 :
1370 DEBUG_printf(("4_httpTLSStart: Excluding insecure cipher suite %d", supported[i]));
1373 /* RC4 cipher suites that should only be used as a last resort */
1374 case SSL_RSA_WITH_RC4_128_MD5 :
1375 case SSL_RSA_WITH_RC4_128_SHA :
1376 case TLS_ECDH_ECDSA_WITH_RC4_128_SHA :
1377 case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA :
1378 case TLS_ECDH_RSA_WITH_RC4_128_SHA :
1379 case TLS_ECDHE_RSA_WITH_RC4_128_SHA :
1380 case TLS_PSK_WITH_RC4_128_SHA :
1381 case TLS_DHE_PSK_WITH_RC4_128_SHA :
1382 case TLS_RSA_PSK_WITH_RC4_128_SHA :
1383 if (tls_options & _HTTP_TLS_ALLOW_RC4)
1384 enabled[num_enabled ++] = supported[i];
1386 DEBUG_printf(("4_httpTLSStart: Excluding RC4 cipher suite %d", supported[i]));
1389 /* DH/DHE cipher suites that are problematic with parameters < 1024 bits */
1390 case TLS_DH_DSS_WITH_AES_128_CBC_SHA :
1391 case TLS_DH_RSA_WITH_AES_128_CBC_SHA :
1392 case TLS_DHE_DSS_WITH_AES_128_CBC_SHA :
1393 case TLS_DHE_RSA_WITH_AES_128_CBC_SHA :
1394 case TLS_DH_DSS_WITH_AES_256_CBC_SHA :
1395 case TLS_DH_RSA_WITH_AES_256_CBC_SHA :
1396 case TLS_DHE_DSS_WITH_AES_256_CBC_SHA :
1397 case TLS_DHE_RSA_WITH_AES_256_CBC_SHA :
1398 case TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA :
1399 case TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA :
1400 case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA :
1401 case TLS_DH_DSS_WITH_AES_128_CBC_SHA256 :
1402 case TLS_DH_RSA_WITH_AES_128_CBC_SHA256 :
1403 case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 :
1404 case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 :
1405 case TLS_DH_DSS_WITH_AES_256_CBC_SHA256 :
1406 case TLS_DH_RSA_WITH_AES_256_CBC_SHA256 :
1407 case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 :
1408 case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 :
1409 case TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA :
1410 case TLS_DHE_PSK_WITH_AES_128_CBC_SHA :
1411 case TLS_DHE_PSK_WITH_AES_256_CBC_SHA :
1412 case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 :
1413 case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 :
1414 if (tls_options & _HTTP_TLS_DENY_CBC)
1416 DEBUG_printf(("4_httpTLSStart: Excluding CBC cipher suite %d", supported[i]));
1420 // case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 :
1421 // case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 :
1422 case TLS_DH_RSA_WITH_AES_128_GCM_SHA256 :
1423 case TLS_DH_RSA_WITH_AES_256_GCM_SHA384 :
1424 // case TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 :
1425 // case TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 :
1426 case TLS_DH_DSS_WITH_AES_128_GCM_SHA256 :
1427 case TLS_DH_DSS_WITH_AES_256_GCM_SHA384 :
1428 case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 :
1429 case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 :
1430 if (tls_options & _HTTP_TLS_ALLOW_DH)
1431 enabled[num_enabled ++] = supported[i];
1433 DEBUG_printf(("4_httpTLSStart: Excluding DH/DHE cipher suite %d", supported[i]));
1436 case TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA :
1437 case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 :
1438 case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 :
1439 case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 :
1440 case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 :
1441 case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 :
1442 case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 :
1443 case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 :
1444 case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 :
1445 case TLS_RSA_WITH_3DES_EDE_CBC_SHA :
1446 case TLS_RSA_WITH_AES_128_CBC_SHA :
1447 case TLS_RSA_WITH_AES_256_CBC_SHA :
1448 if (tls_options & _HTTP_TLS_DENY_CBC)
1450 DEBUG_printf(("4_httpTLSStart: Excluding CBC cipher suite %d", supported[i]));
1454 /* Anything else we'll assume is "secure" */
1456 enabled[num_enabled ++] = supported[i];
1461 DEBUG_printf(("4_httpTLSStart: %d cipher suites enabled.", (int)num_enabled));
1462 error = SSLSetEnabledCiphers(http->tls, enabled, num_enabled);
1466 if (!error && http->mode == _HTTP_MODE_CLIENT)
1469 * Client: set client-side credentials, if any...
1472 if (cg->client_cert_cb)
1474 error = SSLSetSessionOption(http->tls,
1475 kSSLSessionOptionBreakOnCertRequested, true);
1476 DEBUG_printf(("4_httpTLSStart: kSSLSessionOptionBreakOnCertRequested, "
1477 "error=%d", (int)error));
1481 error = http_cdsa_set_credentials(http);
1482 DEBUG_printf(("4_httpTLSStart: http_cdsa_set_credentials, error=%d",
1489 * Server: find/create a certificate for TLS...
1492 if (http->fields[HTTP_FIELD_HOST])
1495 * Use hostname for TLS upgrade...
1498 strlcpy(hostname, http->fields[HTTP_FIELD_HOST], sizeof(hostname));
1503 * Resolve hostname from connection address...
1506 http_addr_t addr; /* Connection address */
1507 socklen_t addrlen; /* Length of address */
1509 addrlen = sizeof(addr);
1510 if (getsockname(http->fd, (struct sockaddr *)&addr, &addrlen))
1512 DEBUG_printf(("4_httpTLSStart: Unable to get socket address: %s", strerror(errno)));
1515 else if (httpAddrLocalhost(&addr))
1519 httpAddrLookup(&addr, hostname, sizeof(hostname));
1520 DEBUG_printf(("4_httpTLSStart: Resolved socket address to \"%s\".", hostname));
1524 if (isdigit(hostname[0] & 255) || hostname[0] == '[')
1525 hostname[0] = '\0'; /* Don't allow numeric addresses */
1528 http->tls_credentials = http_cdsa_copy_server(hostname);
1529 else if (tls_common_name)
1530 http->tls_credentials = http_cdsa_copy_server(tls_common_name);
1532 if (!http->tls_credentials && tls_auto_create && (hostname[0] || tls_common_name))
1534 DEBUG_printf(("4_httpTLSStart: Auto-create credentials for \"%s\".", hostname[0] ? hostname : tls_common_name));
1536 if (!cupsMakeServerCredentials(tls_keypath, hostname[0] ? hostname : tls_common_name, 0, NULL, time(NULL) + 365 * 86400))
1538 DEBUG_puts("4_httpTLSStart: cupsMakeServerCredentials failed.");
1539 http->error = errno = EINVAL;
1540 http->status = HTTP_STATUS_ERROR;
1541 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create server credentials."), 1);
1546 http->tls_credentials = http_cdsa_copy_server(hostname[0] ? hostname : tls_common_name);
1549 if (!http->tls_credentials)
1551 DEBUG_puts("4_httpTLSStart: Unable to find server credentials.");
1552 http->error = errno = EINVAL;
1553 http->status = HTTP_STATUS_ERROR;
1554 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to find server credentials."), 1);
1559 error = SSLSetCertificate(http->tls, http->tls_credentials);
1561 DEBUG_printf(("4_httpTLSStart: SSLSetCertificate, error=%d", (int)error));
1564 DEBUG_printf(("4_httpTLSStart: tls_credentials=%p", (void *)http->tls_credentials));
1567 * Let the server know which hostname/domain we are trying to connect to
1568 * in case it wants to serve up a certificate with a matching common name.
1571 if (!error && http->mode == _HTTP_MODE_CLIENT)
1574 * Client: get the hostname to use for TLS...
1577 if (httpAddrLocalhost(http->hostaddr))
1579 strlcpy(hostname, "localhost", sizeof(hostname));
1584 * Otherwise make sure the hostname we have does not end in a trailing dot.
1587 strlcpy(hostname, http->hostname, sizeof(hostname));
1588 if ((hostptr = hostname + strlen(hostname) - 1) >= hostname &&
1593 error = SSLSetPeerDomainName(http->tls, hostname, strlen(hostname));
1595 DEBUG_printf(("4_httpTLSStart: SSLSetPeerDomainName, error=%d", (int)error));
1600 int done = 0; /* Are we done yet? */
1601 double old_timeout; /* Old timeout value */
1602 http_timeout_cb_t old_cb; /* Old timeout callback */
1603 void *old_data; /* Old timeout data */
1606 * Enforce a minimum timeout of 10 seconds for the TLS handshake...
1609 old_timeout = http->timeout_value;
1610 old_cb = http->timeout_cb;
1611 old_data = http->timeout_data;
1613 if (!old_cb || old_timeout < 10.0)
1615 DEBUG_puts("4_httpTLSStart: Setting timeout to 10 seconds.");
1616 httpSetTimeout(http, 10.0, NULL, NULL);
1620 * Do the TLS handshake...
1623 while (!error && !done)
1625 error = SSLHandshake(http->tls);
1627 DEBUG_printf(("4_httpTLSStart: SSLHandshake returned %d.", (int)error));
1635 case errSSLWouldBlock :
1636 error = noErr; /* Force a retry */
1637 usleep(1000); /* in 1 millisecond */
1640 case errSSLServerAuthCompleted :
1642 if (cg->server_cert_cb)
1644 error = httpCopyCredentials(http, &credentials);
1647 error = (cg->server_cert_cb)(http, http->tls, credentials,
1648 cg->server_cert_data);
1649 httpFreeCredentials(credentials);
1652 DEBUG_printf(("4_httpTLSStart: Server certificate callback "
1653 "returned %d.", (int)error));
1657 case errSSLClientCertRequested :
1660 if (cg->client_cert_cb)
1663 if (!(error = SSLCopyDistinguishedNames(http->tls, &dn_array)) &&
1666 if ((names = cupsArrayNew(NULL, NULL)) != NULL)
1668 for (i = 0, count = CFArrayGetCount(dn_array); i < count; i++)
1670 data = (CFDataRef)CFArrayGetValueAtIndex(dn_array, i);
1672 if ((credential = malloc(sizeof(*credential))) != NULL)
1674 credential->datalen = (size_t)CFDataGetLength(data);
1675 if ((credential->data = malloc(credential->datalen)))
1677 memcpy((void *)credential->data, CFDataGetBytePtr(data),
1678 credential->datalen);
1679 cupsArrayAdd(names, credential);
1687 CFRelease(dn_array);
1692 error = (cg->client_cert_cb)(http, http->tls, names,
1693 cg->client_cert_data);
1695 DEBUG_printf(("4_httpTLSStart: Client certificate callback "
1696 "returned %d.", (int)error));
1699 httpFreeCredentials(names);
1703 case errSSLUnknownRootCert :
1704 message = _("Unable to establish a secure connection to host "
1705 "(untrusted certificate).");
1708 case errSSLNoRootCert :
1709 message = _("Unable to establish a secure connection to host "
1710 "(self-signed certificate).");
1713 case errSSLCertExpired :
1714 message = _("Unable to establish a secure connection to host "
1715 "(expired certificate).");
1718 case errSSLCertNotYetValid :
1719 message = _("Unable to establish a secure connection to host "
1720 "(certificate not yet valid).");
1723 case errSSLHostNameMismatch :
1724 message = _("Unable to establish a secure connection to host "
1725 "(host name mismatch).");
1728 case errSSLXCertChainInvalid :
1729 message = _("Unable to establish a secure connection to host "
1730 "(certificate chain invalid).");
1733 case errSSLConnectionRefused :
1734 message = _("Unable to establish a secure connection to host "
1735 "(peer dropped connection before responding).");
1744 * Restore the previous timeout settings...
1747 httpSetTimeout(http, old_timeout, old_cb, old_data);
1752 http->error = error;
1753 http->status = HTTP_STATUS_ERROR;
1754 errno = ECONNREFUSED;
1756 CFRelease(http->tls);
1760 * If an error string wasn't set by the callbacks use a generic one...
1765 if (!cg->lang_default)
1766 cg->lang_default = cupsLangDefault();
1768 snprintf(msgbuf, sizeof(msgbuf), _cupsLangString(cg->lang_default, _("Unable to establish a secure connection to host (%d).")), error);
1772 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, message, 1);
1782 * '_httpTLSStop()' - Shut down SSL/TLS on a connection.
1786 _httpTLSStop(http_t *http) /* I - HTTP connection */
1788 while (SSLClose(http->tls) == errSSLWouldBlock)
1791 CFRelease(http->tls);
1793 if (http->tls_credentials)
1794 CFRelease(http->tls_credentials);
1797 http->tls_credentials = NULL;
1802 * '_httpTLSWrite()' - Write to a SSL/TLS connection.
1805 int /* O - Bytes written */
1806 _httpTLSWrite(http_t *http, /* I - HTTP connection */
1807 const char *buf, /* I - Buffer holding data */
1808 int len) /* I - Length of buffer */
1810 ssize_t result; /* Return value */
1811 OSStatus error; /* Error info */
1812 size_t processed; /* Number of bytes processed */
1815 DEBUG_printf(("2_httpTLSWrite(http=%p, buf=%p, len=%d)", (void *)http, (void *)buf, len));
1817 error = SSLWrite(http->tls, buf, (size_t)len, &processed);
1822 result = (int)processed;
1825 case errSSLWouldBlock :
1828 result = (int)processed;
1837 case errSSLClosedGraceful :
1841 result = (int)processed;
1851 DEBUG_printf(("3_httpTLSWrite: Returning %d.", (int)result));
1853 return ((int)result);
1858 * 'http_cdsa_copy_server()' - Find and copy server credentials from the keychain.
1861 static CFArrayRef /* O - Array of certificates or NULL */
1862 http_cdsa_copy_server(
1863 const char *common_name) /* I - Server's hostname */
1866 OSStatus err; /* Error info */
1867 SecIdentityRef identity = NULL;/* Identity */
1868 CFArrayRef certificates = NULL;
1869 /* Certificate array */
1870 SecPolicyRef policy = NULL; /* Policy ref */
1871 CFStringRef cfcommon_name = NULL;
1873 CFMutableDictionaryRef query = NULL; /* Query qualifiers */
1874 CFArrayRef list = NULL; /* Keychain list */
1875 SecKeychainRef syschain = NULL;/* System keychain */
1876 SecKeychainStatus status = 0; /* Keychain status */
1879 DEBUG_printf(("3http_cdsa_copy_server(common_name=\"%s\")", common_name));
1881 cfcommon_name = CFStringCreateWithCString(kCFAllocatorDefault, common_name, kCFStringEncodingUTF8);
1883 policy = SecPolicyCreateSSL(1, cfcommon_name);
1887 DEBUG_puts("4http_cdsa_copy_server: Unable to create SSL policy.");
1891 if (!(query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)))
1893 DEBUG_puts("4http_cdsa_copy_server: Unable to create query dictionary.");
1897 _cupsMutexLock(&tls_mutex);
1899 err = SecKeychainGetStatus(tls_keychain, &status);
1901 if (err == noErr && !(status & kSecUnlockStateStatus) && tls_cups_keychain)
1902 SecKeychainUnlock(tls_keychain, _CUPS_CDSA_PASSLEN, _CUPS_CDSA_PASSWORD, TRUE);
1904 CFDictionaryAddValue(query, kSecClass, kSecClassIdentity);
1905 CFDictionaryAddValue(query, kSecMatchPolicy, policy);
1906 CFDictionaryAddValue(query, kSecReturnRef, kCFBooleanTrue);
1907 CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitOne);
1909 syschain = http_cdsa_open_system_keychain();
1913 const void *values[2] = { syschain, tls_keychain };
1915 list = CFArrayCreate(kCFAllocatorDefault, (const void **)values, 2, &kCFTypeArrayCallBacks);
1918 list = CFArrayCreate(kCFAllocatorDefault, (const void **)&tls_keychain, 1, &kCFTypeArrayCallBacks);
1920 CFDictionaryAddValue(query, kSecMatchSearchList, list);
1923 err = SecItemCopyMatching(query, (CFTypeRef *)&identity);
1925 _cupsMutexUnlock(&tls_mutex);
1929 DEBUG_printf(("4http_cdsa_copy_server: SecItemCopyMatching failed with status %d.", (int)err));
1933 if (CFGetTypeID(identity) != SecIdentityGetTypeID())
1935 DEBUG_puts("4http_cdsa_copy_server: Search returned something that is not an identity.");
1939 if ((certificates = CFArrayCreate(NULL, (const void **)&identity, 1, &kCFTypeArrayCallBacks)) == NULL)
1941 DEBUG_puts("4http_cdsa_copy_server: Unable to create array of certificates.");
1948 CFRelease(syschain);
1950 CFRelease(identity);
1954 CFRelease(cfcommon_name);
1958 DEBUG_printf(("4http_cdsa_copy_server: Returning %p.", (void *)certificates));
1960 return (certificates);
1965 if (!tls_selfsigned)
1968 return (CFArrayCreate(NULL, (const void **)&tls_selfsigned, 1, &kCFTypeArrayCallBacks));
1969 #endif /* TARGET_OS_OSX */
1974 * 'http_cdsa_create_credential()' - Create a single credential in the internal format.
1977 static SecCertificateRef /* O - Certificate */
1978 http_cdsa_create_credential(
1979 http_credential_t *credential) /* I - Credential */
1981 SecCertificateRef cert; /* Certificate */
1982 CFDataRef data; /* Data object */
1988 data = CFDataCreate(kCFAllocatorDefault, credential->data, (CFIndex)credential->datalen);
1989 cert = SecCertificateCreateWithData(kCFAllocatorDefault, data);
1998 * 'http_cdsa_default_path()' - Get the default keychain path.
2001 static const char * /* O - Keychain path */
2002 http_cdsa_default_path(char *buffer, /* I - Path buffer */
2003 size_t bufsize) /* I - Size of buffer */
2005 _cups_globals_t *cg = _cupsGlobals();
2006 /* Pointer to library globals */
2010 * Determine the default keychain path. Note that the login and system
2011 * keychains are no longer accessible to user applications starting in macOS
2012 * 10.11.4 (!), so we need to create our own keychain just for CUPS.
2016 snprintf(buffer, bufsize, "%s/.cups/ssl.keychain", cg->home);
2018 strlcpy(buffer, "/etc/cups/ssl.keychain", bufsize);
2020 DEBUG_printf(("1http_cdsa_default_path: Using default path \"%s\".", buffer));
2027 * 'http_cdsa_open_keychain()' - Open (or create) a keychain.
2030 static SecKeychainRef /* O - Keychain or NULL */
2031 http_cdsa_open_keychain(
2032 const char *path, /* I - Path to keychain */
2033 char *filename, /* I - Keychain filename */
2034 size_t filesize) /* I - Size of filename buffer */
2036 SecKeychainRef keychain = NULL;/* Temporary keychain */
2037 OSStatus err; /* Error code */
2038 Boolean interaction; /* Interaction allowed? */
2039 SecKeychainStatus status = 0; /* Keychain status */
2043 * Get the keychain filename...
2048 path = http_cdsa_default_path(filename, filesize);
2049 tls_cups_keychain = 1;
2053 strlcpy(filename, path, filesize);
2054 tls_cups_keychain = 0;
2058 * Save the interaction setting and disable while we open the keychain...
2061 SecKeychainGetUserInteractionAllowed(&interaction);
2062 SecKeychainSetUserInteractionAllowed(FALSE);
2064 if (access(path, R_OK) && tls_cups_keychain)
2067 * Create a new keychain at the given path...
2070 err = SecKeychainCreate(path, _CUPS_CDSA_PASSLEN, _CUPS_CDSA_PASSWORD, FALSE, NULL, &keychain);
2075 * Open the existing keychain and unlock as needed...
2078 err = SecKeychainOpen(path, &keychain);
2081 err = SecKeychainGetStatus(keychain, &status);
2083 if (err == noErr && !(status & kSecUnlockStateStatus) && tls_cups_keychain)
2084 err = SecKeychainUnlock(keychain, _CUPS_CDSA_PASSLEN, _CUPS_CDSA_PASSWORD, TRUE);
2088 * Restore interaction setting...
2091 SecKeychainSetUserInteractionAllowed(interaction);
2094 * Release the keychain if we had any errors...
2099 /* TODO: Set cups last error string */
2100 DEBUG_printf(("4http_cdsa_open_keychain: Unable to open keychain (%d), returning NULL.", (int)err));
2104 CFRelease(keychain);
2110 * Return the keychain or NULL...
2118 * 'http_cdsa_open_system_keychain()' - Open the System keychain.
2121 static SecKeychainRef
2122 http_cdsa_open_system_keychain(void)
2124 SecKeychainRef keychain = NULL;/* Temporary keychain */
2125 OSStatus err; /* Error code */
2126 Boolean interaction; /* Interaction allowed? */
2127 SecKeychainStatus status = 0; /* Keychain status */
2131 * Save the interaction setting and disable while we open the keychain...
2134 SecKeychainGetUserInteractionAllowed(&interaction);
2135 SecKeychainSetUserInteractionAllowed(TRUE);
2137 err = SecKeychainOpen("/Library/Keychains/System.keychain", &keychain);
2140 err = SecKeychainGetStatus(keychain, &status);
2142 if (err == noErr && !(status & kSecUnlockStateStatus))
2143 err = errSecInteractionNotAllowed;
2146 * Restore interaction setting...
2149 SecKeychainSetUserInteractionAllowed(interaction);
2152 * Release the keychain if we had any errors...
2157 /* TODO: Set cups last error string */
2158 DEBUG_printf(("4http_cdsa_open_system_keychain: Unable to open keychain (%d), returning NULL.", (int)err));
2162 CFRelease(keychain);
2168 * Return the keychain or NULL...
2173 #endif /* TARGET_OS_OSX */
2177 * 'http_cdsa_read()' - Read function for the CDSA library.
2180 static OSStatus /* O - -1 on error, 0 on success */
2182 SSLConnectionRef connection, /* I - SSL/TLS connection */
2183 void *data, /* I - Data buffer */
2184 size_t *dataLength) /* IO - Number of bytes */
2186 OSStatus result; /* Return value */
2187 ssize_t bytes; /* Number of bytes read */
2188 http_t *http; /* HTTP connection */
2191 http = (http_t *)connection;
2193 if (!http->blocking || http->timeout_value > 0.0)
2196 * Make sure we have data before we read...
2199 while (!_httpWait(http, http->wait_value, 0))
2201 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
2204 http->error = ETIMEDOUT;
2211 bytes = recv(http->fd, data, *dataLength, 0);
2213 while (bytes == -1 && (errno == EINTR || errno == EAGAIN));
2215 if ((size_t)bytes == *dataLength)
2221 *dataLength = (size_t)bytes;
2222 result = errSSLWouldBlock;
2229 result = errSSLClosedGraceful;
2230 else if (errno == EAGAIN)
2231 result = errSSLWouldBlock;
2233 result = errSSLClosedAbort;
2241 * 'http_cdsa_set_credentials()' - Set the TLS credentials.
2244 static int /* O - Status of connection */
2245 http_cdsa_set_credentials(http_t *http) /* I - HTTP connection */
2247 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
2248 OSStatus error = 0; /* Error code */
2249 http_tls_credentials_t credentials = NULL;
2250 /* TLS credentials */
2253 DEBUG_printf(("7http_tls_set_credentials(%p)", (void *)http));
2256 * Prefer connection specific credentials...
2259 if ((credentials = http->tls_credentials) == NULL)
2260 credentials = cg->tls_credentials;
2264 error = SSLSetCertificate(http->tls, credentials);
2265 DEBUG_printf(("4http_tls_set_credentials: SSLSetCertificate, error=%d",
2269 DEBUG_puts("4http_tls_set_credentials: No credentials to set.");
2276 * 'http_cdsa_write()' - Write function for the CDSA library.
2279 static OSStatus /* O - -1 on error, 0 on success */
2281 SSLConnectionRef connection, /* I - SSL/TLS connection */
2282 const void *data, /* I - Data buffer */
2283 size_t *dataLength) /* IO - Number of bytes */
2285 OSStatus result; /* Return value */
2286 ssize_t bytes; /* Number of bytes read */
2287 http_t *http; /* HTTP connection */
2290 http = (http_t *)connection;
2294 bytes = write(http->fd, data, *dataLength);
2296 while (bytes == -1 && (errno == EINTR || errno == EAGAIN));
2298 if ((size_t)bytes == *dataLength)
2302 else if (bytes >= 0)
2304 *dataLength = (size_t)bytes;
2305 result = errSSLWouldBlock;
2311 if (errno == EAGAIN)
2312 result = errSSLWouldBlock;
2314 result = errSSLClosedAbort;