]>
Commit | Line | Data |
---|---|---|
2c85b752 | 1 | /* |
2c85b752 MS |
2 | * TLS support code for CUPS on OS X. |
3 | * | |
1ae693e3 | 4 | * Copyright 2007-2016 by Apple Inc. |
2c85b752 MS |
5 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
6 | * | |
7 | * These coded instructions, statements, and computer programs are the | |
8 | * property of Apple Inc. and are protected by Federal copyright | |
9 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
10 | * which should have been included with this file. If this file is | |
11 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
12 | * | |
13 | * This file is subject to the Apple OS-Developed Software exception. | |
14 | */ | |
15 | ||
ebb24a07 | 16 | /**** This file is included from tls.c ****/ |
2c85b752 MS |
17 | |
18 | /* | |
dafebafd | 19 | * Include necessary headers... |
2c85b752 MS |
20 | */ |
21 | ||
dafebafd MS |
22 | #include <spawn.h> |
23 | ||
24 | extern char **environ; | |
25 | ||
26 | ||
c0459938 MS |
27 | /* |
28 | * Local globals... | |
29 | */ | |
30 | ||
eb66bc71 | 31 | #ifdef HAVE_SECKEYCHAINOPEN |
c0459938 MS |
32 | static int tls_auto_create = 0; |
33 | /* Auto-create self-signed certs? */ | |
34 | static char *tls_common_name = NULL; | |
35 | /* Default common name */ | |
41e0907c MS |
36 | static SecKeychainRef tls_keychain = NULL; |
37 | /* Server cert keychain */ | |
38 | static char *tls_keypath = NULL; | |
39 | /* Server cert keychain path */ | |
40 | static _cups_mutex_t tls_mutex = _CUPS_MUTEX_INITIALIZER; | |
41 | /* Mutex for keychain/certs */ | |
eb66bc71 | 42 | #endif /* HAVE_SECKEYCHAINOPEN */ |
4492316e | 43 | static int tls_options = -1;/* Options for TLS connections */ |
c0459938 MS |
44 | |
45 | ||
dafebafd MS |
46 | /* |
47 | * Local functions... | |
48 | */ | |
2c85b752 | 49 | |
eb66bc71 | 50 | #ifdef HAVE_SECKEYCHAINOPEN |
41e0907c | 51 | static CFArrayRef http_cdsa_copy_server(const char *common_name); |
eb66bc71 | 52 | #endif /* HAVE_SECKEYCHAINOPEN */ |
2ece34a9 | 53 | static SecCertificateRef http_cdsa_create_credential(http_credential_t *credential); |
005f7f1f | 54 | static const char *http_cdsa_default_path(char *buffer, size_t bufsize); |
41e0907c | 55 | static OSStatus http_cdsa_read(SSLConnectionRef connection, void *data, size_t *dataLength); |
88f1e9c8 | 56 | static int http_cdsa_set_credentials(http_t *http); |
41e0907c | 57 | static OSStatus http_cdsa_write(SSLConnectionRef connection, const void *data, size_t *dataLength); |
2c85b752 MS |
58 | |
59 | ||
3af9ac9e MS |
60 | /* |
61 | * 'cupsMakeServerCredentials()' - Make a self-signed certificate and private key pair. | |
62 | * | |
e1f19878 | 63 | * @since CUPS 2.0/OS 10.10@ |
3af9ac9e MS |
64 | */ |
65 | ||
66 | int /* O - 1 on success, 0 on failure */ | |
67 | cupsMakeServerCredentials( | |
f93b32b6 | 68 | const char *path, /* I - Keychain path or @code NULL@ for default */ |
3af9ac9e MS |
69 | const char *common_name, /* I - Common name */ |
70 | int num_alt_names, /* I - Number of subject alternate names */ | |
71 | const char **alt_names, /* I - Subject Alternate Names */ | |
72 | time_t expiration_date) /* I - Expiration date */ | |
73 | { | |
eb66bc71 | 74 | #if defined(HAVE_SECGENERATESELFSIGNEDCERTIFICATE) && defined(HAVE_SECKEYCHAINOPEN) |
f93b32b6 | 75 | char filename[1024]; /* Default keychain path */ |
41e0907c MS |
76 | int status = 0; /* Return status */ |
77 | OSStatus err; /* Error code (if any) */ | |
78 | CFStringRef cfcommon_name = NULL; | |
79 | /* CF string for server name */ | |
80 | SecIdentityRef ident = NULL; /* Identity */ | |
81 | SecKeyRef publicKey = NULL, | |
82 | /* Public key */ | |
83 | privateKey = NULL; | |
84 | /* Private key */ | |
85 | CFMutableDictionaryRef keyParams = NULL; | |
86 | /* Key generation parameters */ | |
87 | ||
88 | ||
172bdf5d MS |
89 | 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)); |
90 | ||
3af9ac9e MS |
91 | (void)num_alt_names; |
92 | (void)alt_names; | |
93 | (void)expiration_date; | |
94 | ||
f93b32b6 | 95 | if (!path) |
005f7f1f | 96 | path = http_cdsa_default_path(filename, sizeof(filename)); |
f93b32b6 MS |
97 | |
98 | cfcommon_name = CFStringCreateWithCString(kCFAllocatorDefault, common_name, kCFStringEncodingUTF8); | |
41e0907c MS |
99 | if (!cfcommon_name) |
100 | goto cleanup; | |
101 | ||
102 | /* | |
103 | * Create a public/private key pair... | |
104 | */ | |
105 | ||
f93b32b6 | 106 | keyParams = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); |
41e0907c MS |
107 | if (!keyParams) |
108 | goto cleanup; | |
109 | ||
1ae693e3 | 110 | CFDictionaryAddValue(keyParams, kSecAttrKeyType, kSecAttrKeyTypeECDSA); |
41e0907c | 111 | CFDictionaryAddValue(keyParams, kSecAttrKeySizeInBits, CFSTR("2048")); |
f93b32b6 | 112 | CFDictionaryAddValue(keyParams, kSecAttrLabel, CFSTR("CUPS Self-Signed Certificate")); |
41e0907c MS |
113 | |
114 | err = SecKeyGeneratePair(keyParams, &publicKey, &privateKey); | |
115 | if (err != noErr) | |
116 | goto cleanup; | |
117 | ||
118 | /* | |
119 | * Create a self-signed certificate using the public/private key pair... | |
120 | */ | |
121 | ||
122 | CFIndex usageInt = kSecKeyUsageAll; | |
123 | CFNumberRef usage = CFNumberCreate(alloc, kCFNumberCFIndexType, &usageInt); | |
124 | CFDictionaryRef certParams = CFDictionaryCreateMutable(kCFAllocatorDefault, | |
125 | kSecCSRBasicContraintsPathLen, CFINT(0), kSecSubjectAltName, cfcommon_name, kSecCertificateKeyUsage, usage, NULL, NULL); | |
126 | CFRelease(usage); | |
127 | ||
128 | const void *ca_o[] = { kSecOidOrganization, CFSTR("") }; | |
129 | const void *ca_cn[] = { kSecOidCommonName, cfcommon_name }; | |
130 | CFArrayRef ca_o_dn = CFArrayCreate(kCFAllocatorDefault, ca_o, 2, NULL); | |
131 | CFArrayRef ca_cn_dn = CFArrayCreate(kCFAllocatorDefault, ca_cn, 2, NULL); | |
132 | const void *ca_dn_array[2]; | |
133 | ||
134 | ca_dn_array[0] = CFArrayCreate(kCFAllocatorDefault, (const void **)&ca_o_dn, 1, NULL); | |
135 | ca_dn_array[1] = CFArrayCreate(kCFAllocatorDefault, (const void **)&ca_cn_dn, 1, NULL); | |
136 | ||
137 | CFArrayRef subject = CFArrayCreate(kCFAllocatorDefault, ca_dn_array, 2, NULL); | |
138 | SecCertificateRef cert = SecGenerateSelfSignedCertificate(subject, certParams, publicKey, privateKey); | |
139 | CFRelease(subject); | |
140 | CFRelease(certParams); | |
141 | ||
142 | if (!cert) | |
143 | goto cleanup; | |
144 | ||
145 | ident = SecIdentityCreate(kCFAllocatorDefault, cert, privateKey); | |
146 | ||
147 | if (ident) | |
148 | status = 1; | |
149 | ||
150 | /* | |
151 | * Cleanup and return... | |
152 | */ | |
153 | ||
154 | cleanup: | |
155 | ||
156 | if (cfcommon_name) | |
157 | CFRelease(cfcommon_name); | |
158 | ||
159 | if (keyParams) | |
160 | CFRelease(keyParams); | |
161 | ||
162 | if (ident) | |
163 | CFRelease(ident); | |
164 | ||
165 | if (cert) | |
166 | CFRelease(cert); | |
167 | ||
168 | if (publicKey) | |
169 | CFRelease(publicKey); | |
170 | ||
171 | if (privateKey) | |
172 | CFRelease(publicKey); | |
173 | ||
174 | return (status); | |
175 | ||
eb66bc71 | 176 | #else /* !(HAVE_SECGENERATESELFSIGNEDCERTIFICATE && HAVE_SECKEYCHAINOPEN) */ |
41e0907c | 177 | int pid, /* Process ID of command */ |
7d58a105 MS |
178 | status, /* Status of command */ |
179 | i; /* Looping var */ | |
41e0907c | 180 | char command[1024], /* Command */ |
724f2615 | 181 | *argv[5], /* Command-line arguments */ |
7d58a105 MS |
182 | *envp[1000], /* Environment variables */ |
183 | days[32], /* CERTTOOL_EXPIRATION_DAYS env var */ | |
41e0907c | 184 | keychain[1024], /* Keychain argument */ |
f93b32b6 MS |
185 | infofile[1024], /* Type-in information for cert */ |
186 | filename[1024]; /* Default keychain path */ | |
41e0907c MS |
187 | cups_file_t *fp; /* Seed/info file */ |
188 | ||
189 | ||
172bdf5d MS |
190 | 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)); |
191 | ||
41e0907c MS |
192 | (void)num_alt_names; |
193 | (void)alt_names; | |
41e0907c | 194 | |
f93b32b6 | 195 | if (!path) |
005f7f1f | 196 | path = http_cdsa_default_path(filename, sizeof(filename)); |
f93b32b6 | 197 | |
41e0907c MS |
198 | /* |
199 | * Run the "certtool" command to generate a self-signed certificate... | |
200 | */ | |
201 | ||
202 | if (!cupsFileFind("certtool", getenv("PATH"), 1, command, sizeof(command))) | |
203 | return (-1); | |
204 | ||
205 | /* | |
206 | * Create a file with the certificate information fields... | |
207 | * | |
208 | * Note: This assumes that the default questions are asked by the certtool | |
209 | * command... | |
210 | */ | |
211 | ||
212 | if ((fp = cupsTempFile2(infofile, sizeof(infofile))) == NULL) | |
213 | return (-1); | |
214 | ||
215 | cupsFilePrintf(fp, | |
216 | "CUPS Self-Signed Certificate\n" | |
217 | /* Enter key and certificate label */ | |
724f2615 | 218 | "r\n" /* Generate RSA key pair */ |
1ae693e3 | 219 | "2048\n" /* 2048 bit encryption key */ |
41e0907c MS |
220 | "y\n" /* OK (y = yes) */ |
221 | "b\n" /* Usage (b=signing/encryption) */ | |
1ae693e3 | 222 | "2\n" /* Sign with SHA256 */ |
41e0907c MS |
223 | "y\n" /* OK (y = yes) */ |
224 | "%s\n" /* Common name */ | |
225 | "\n" /* Country (default) */ | |
226 | "\n" /* Organization (default) */ | |
227 | "\n" /* Organizational unit (default) */ | |
228 | "\n" /* State/Province (default) */ | |
229 | "\n" /* Email address */ | |
230 | "y\n", /* OK (y = yes) */ | |
231 | common_name); | |
232 | cupsFileClose(fp); | |
233 | ||
234 | snprintf(keychain, sizeof(keychain), "k=%s", path); | |
235 | ||
236 | argv[0] = "certtool"; | |
237 | argv[1] = "c"; | |
238 | argv[2] = keychain; | |
239 | argv[3] = NULL; | |
240 | ||
7d58a105 MS |
241 | snprintf(days, sizeof(days), "CERTTOOL_EXPIRATION_DAYS=%d", (int)((expiration_date - time(NULL) + 86399) / 86400)); |
242 | envp[0] = days; | |
243 | for (i = 0; i < (int)(sizeof(envp) / sizeof(envp[0]) - 2) && environ[i]; i ++) | |
244 | envp[i + 1] = environ[i]; | |
245 | envp[i] = NULL; | |
246 | ||
41e0907c MS |
247 | posix_spawn_file_actions_t actions; /* File actions */ |
248 | ||
249 | posix_spawn_file_actions_init(&actions); | |
250 | posix_spawn_file_actions_addclose(&actions, 0); | |
251 | posix_spawn_file_actions_addopen(&actions, 0, infofile, O_RDONLY, 0); | |
f93b32b6 MS |
252 | posix_spawn_file_actions_addclose(&actions, 1); |
253 | posix_spawn_file_actions_addopen(&actions, 1, "/dev/null", O_WRONLY, 0); | |
254 | posix_spawn_file_actions_addclose(&actions, 2); | |
255 | posix_spawn_file_actions_addopen(&actions, 2, "/dev/null", O_WRONLY, 0); | |
41e0907c | 256 | |
7d58a105 | 257 | if (posix_spawn(&pid, command, &actions, NULL, argv, envp)) |
41e0907c MS |
258 | { |
259 | unlink(infofile); | |
260 | return (-1); | |
261 | } | |
262 | ||
263 | posix_spawn_file_actions_destroy(&actions); | |
264 | ||
265 | unlink(infofile); | |
266 | ||
267 | while (waitpid(pid, &status, 0) < 0) | |
268 | if (errno != EINTR) | |
269 | { | |
270 | status = -1; | |
271 | break; | |
272 | } | |
273 | ||
274 | return (!status); | |
eb66bc71 | 275 | #endif /* HAVE_SECGENERATESELFSIGNEDCERTIFICATE && HAVE_SECKEYCHAINOPEN */ |
3af9ac9e MS |
276 | } |
277 | ||
278 | ||
279 | /* | |
280 | * 'cupsSetServerCredentials()' - Set the default server credentials. | |
281 | * | |
282 | * Note: The server credentials are used by all threads in the running process. | |
283 | * This function is threadsafe. | |
284 | * | |
e1f19878 | 285 | * @since CUPS 2.0/OS X 10.10@ |
3af9ac9e MS |
286 | */ |
287 | ||
288 | int /* O - 1 on success, 0 on failure */ | |
289 | cupsSetServerCredentials( | |
f93b32b6 | 290 | const char *path, /* I - Keychain path or @code NULL@ for default */ |
3af9ac9e MS |
291 | const char *common_name, /* I - Default common name for server */ |
292 | int auto_create) /* I - 1 = automatically create self-signed certificates */ | |
293 | { | |
a27a134a MS |
294 | DEBUG_printf(("cupsSetServerCredentials(path=\"%s\", common_name=\"%s\", auto_create=%d)", path, common_name, auto_create)); |
295 | ||
eb66bc71 | 296 | #ifdef HAVE_SECKEYCHAINOPEN |
f93b32b6 | 297 | char filename[1024]; /* Filename for keychain */ |
c0459938 | 298 | SecKeychainRef keychain = NULL;/* Temporary keychain */ |
3af9ac9e | 299 | |
c0459938 | 300 | |
f93b32b6 | 301 | if (!path) |
005f7f1f | 302 | path = http_cdsa_default_path(filename, sizeof(filename)); |
f93b32b6 | 303 | |
c0459938 MS |
304 | if (SecKeychainOpen(path, &keychain) != noErr) |
305 | { | |
306 | /* TODO: Set cups last error string */ | |
a27a134a | 307 | DEBUG_puts("1cupsSetServerCredentials: Unable to open keychain, returning 0."); |
c0459938 MS |
308 | return (0); |
309 | } | |
310 | ||
311 | _cupsMutexLock(&tls_mutex); | |
312 | ||
313 | /* | |
314 | * Close any keychain that is currently open... | |
315 | */ | |
316 | ||
317 | if (tls_keychain) | |
318 | CFRelease(tls_keychain); | |
319 | ||
41e0907c MS |
320 | if (tls_keypath) |
321 | _cupsStrFree(tls_keypath); | |
322 | ||
c0459938 MS |
323 | if (tls_common_name) |
324 | _cupsStrFree(tls_common_name); | |
325 | ||
326 | /* | |
327 | * Save the new keychain... | |
328 | */ | |
329 | ||
330 | tls_keychain = keychain; | |
41e0907c | 331 | tls_keypath = _cupsStrAlloc(path); |
c0459938 MS |
332 | tls_auto_create = auto_create; |
333 | tls_common_name = _cupsStrAlloc(common_name); | |
334 | ||
335 | _cupsMutexUnlock(&tls_mutex); | |
336 | ||
a27a134a | 337 | DEBUG_puts("1cupsSetServerCredentials: Opened keychain, returning 1."); |
c0459938 | 338 | return (1); |
eb66bc71 MS |
339 | |
340 | #else | |
a27a134a | 341 | DEBUG_puts("1cupsSetServerCredentials: No keychain support compiled in, returning 0."); |
eb66bc71 MS |
342 | return (0); |
343 | #endif /* HAVE_SECKEYCHAINOPEN */ | |
3af9ac9e MS |
344 | } |
345 | ||
2c85b752 MS |
346 | |
347 | /* | |
348 | * 'httpCopyCredentials()' - Copy the credentials associated with the peer in | |
349 | * an encrypted connection. | |
350 | * | |
351 | * @since CUPS 1.5/OS X 10.7@ | |
352 | */ | |
353 | ||
354 | int /* O - Status of call (0 = success) */ | |
355 | httpCopyCredentials( | |
356 | http_t *http, /* I - Connection to server */ | |
357 | cups_array_t **credentials) /* O - Array of credentials */ | |
358 | { | |
359 | OSStatus error; /* Error code */ | |
360 | SecTrustRef peerTrust; /* Peer trust reference */ | |
361 | CFIndex count; /* Number of credentials */ | |
362 | SecCertificateRef secCert; /* Certificate reference */ | |
363 | CFDataRef data; /* Certificate data */ | |
364 | int i; /* Looping var */ | |
365 | ||
366 | ||
376d7c69 MS |
367 | DEBUG_printf(("httpCopyCredentials(http=%p, credentials=%p)", http, credentials)); |
368 | ||
2c85b752 MS |
369 | if (credentials) |
370 | *credentials = NULL; | |
371 | ||
372 | if (!http || !http->tls || !credentials) | |
373 | return (-1); | |
374 | ||
375 | if (!(error = SSLCopyPeerTrust(http->tls, &peerTrust)) && peerTrust) | |
376 | { | |
376d7c69 MS |
377 | DEBUG_printf(("2httpCopyCredentials: Peer provided %d certificates.", (int)SecTrustGetCertificateCount(peerTrust))); |
378 | ||
2c85b752 MS |
379 | if ((*credentials = cupsArrayNew(NULL, NULL)) != NULL) |
380 | { | |
381 | count = SecTrustGetCertificateCount(peerTrust); | |
382 | ||
383 | for (i = 0; i < count; i ++) | |
384 | { | |
385 | secCert = SecTrustGetCertificateAtIndex(peerTrust, i); | |
376d7c69 MS |
386 | |
387 | #ifdef DEBUG | |
388 | CFStringRef cf_name = SecCertificateCopySubjectSummary(secCert); | |
389 | char name[1024]; | |
390 | if (cf_name) | |
391 | CFStringGetCString(cf_name, name, sizeof(name), kCFStringEncodingUTF8); | |
392 | else | |
393 | strlcpy(name, "unknown", sizeof(name)); | |
394 | ||
395 | DEBUG_printf(("2httpCopyCredentials: Certificate %d name is \"%s\".", i, name)); | |
396 | #endif /* DEBUG */ | |
397 | ||
88f1e9c8 | 398 | if ((data = SecCertificateCopyData(secCert)) != NULL) |
2c85b752 | 399 | { |
376d7c69 MS |
400 | DEBUG_printf(("2httpCopyCredentials: Adding %d byte certificate blob.", (int)CFDataGetLength(data))); |
401 | ||
7e86f2f6 | 402 | httpAddCredential(*credentials, CFDataGetBytePtr(data), (size_t)CFDataGetLength(data)); |
2c85b752 MS |
403 | CFRelease(data); |
404 | } | |
405 | } | |
406 | } | |
407 | ||
408 | CFRelease(peerTrust); | |
409 | } | |
410 | ||
411 | return (error); | |
412 | } | |
413 | ||
414 | ||
415 | /* | |
416 | * '_httpCreateCredentials()' - Create credentials in the internal format. | |
417 | */ | |
418 | ||
419 | http_tls_credentials_t /* O - Internal credentials */ | |
420 | _httpCreateCredentials( | |
421 | cups_array_t *credentials) /* I - Array of credentials */ | |
422 | { | |
423 | CFMutableArrayRef peerCerts; /* Peer credentials reference */ | |
424 | SecCertificateRef secCert; /* Certificate reference */ | |
2c85b752 MS |
425 | http_credential_t *credential; /* Credential data */ |
426 | ||
427 | ||
428 | if (!credentials) | |
429 | return (NULL); | |
430 | ||
431 | if ((peerCerts = CFArrayCreateMutable(kCFAllocatorDefault, | |
432 | cupsArrayCount(credentials), | |
433 | &kCFTypeArrayCallBacks)) == NULL) | |
434 | return (NULL); | |
435 | ||
436 | for (credential = (http_credential_t *)cupsArrayFirst(credentials); | |
437 | credential; | |
438 | credential = (http_credential_t *)cupsArrayNext(credentials)) | |
439 | { | |
9653cfdf | 440 | if ((secCert = http_cdsa_create_credential(credential)) != NULL) |
2c85b752 | 441 | { |
9653cfdf MS |
442 | CFArrayAppendValue(peerCerts, secCert); |
443 | CFRelease(secCert); | |
2c85b752 MS |
444 | } |
445 | } | |
446 | ||
447 | return (peerCerts); | |
448 | } | |
449 | ||
450 | ||
3af9ac9e | 451 | /* |
524c65e6 | 452 | * 'httpCredentialsAreValidForName()' - Return whether the credentials are valid for the given name. |
3af9ac9e | 453 | * |
e1f19878 | 454 | * @since CUPS 2.0/OS X 10.10@ |
3af9ac9e MS |
455 | */ |
456 | ||
524c65e6 MS |
457 | int /* O - 1 if valid, 0 otherwise */ |
458 | httpCredentialsAreValidForName( | |
459 | cups_array_t *credentials, /* I - Credentials */ | |
460 | const char *common_name) /* I - Name to check */ | |
461 | { | |
462 | SecCertificateRef secCert; /* Certificate reference */ | |
463 | CFStringRef cfcert_name = NULL; | |
464 | /* Certificate's common name (CF string) */ | |
465 | char cert_name[256]; /* Certificate's common name (C string) */ | |
466 | int valid = 1; /* Valid name? */ | |
467 | ||
468 | ||
469 | if ((secCert = http_cdsa_create_credential((http_credential_t *)cupsArrayFirst(credentials))) == NULL) | |
470 | return (0); | |
471 | ||
472 | /* | |
473 | * Compare the common names... | |
474 | */ | |
475 | ||
476 | if ((cfcert_name = SecCertificateCopySubjectSummary(secCert)) == NULL) | |
477 | { | |
478 | /* | |
479 | * Can't get common name, cannot be valid... | |
480 | */ | |
481 | ||
482 | valid = 0; | |
483 | } | |
484 | else if (CFStringGetCString(cfcert_name, cert_name, sizeof(cert_name), kCFStringEncodingUTF8) && | |
485 | _cups_strcasecmp(common_name, cert_name)) | |
486 | { | |
487 | /* | |
488 | * Not an exact match for the common name, check for wildcard certs... | |
489 | */ | |
490 | ||
491 | const char *domain = strchr(common_name, '.'); | |
492 | /* Domain in common name */ | |
493 | ||
494 | if (strncmp(cert_name, "*.", 2) || !domain || _cups_strcasecmp(domain, cert_name + 1)) | |
495 | { | |
496 | /* | |
497 | * Not a wildcard match. | |
498 | */ | |
499 | ||
500 | /* TODO: Check subject alternate names */ | |
501 | valid = 0; | |
502 | } | |
503 | } | |
504 | ||
505 | if (cfcert_name) | |
506 | CFRelease(cfcert_name); | |
507 | ||
508 | CFRelease(secCert); | |
509 | ||
510 | return (valid); | |
511 | } | |
512 | ||
513 | ||
514 | /* | |
515 | * 'httpCredentialsGetTrust()' - Return the trust of credentials. | |
516 | * | |
e1f19878 | 517 | * @since CUPS 2.0/OS X 10.10@ |
524c65e6 MS |
518 | */ |
519 | ||
520 | http_trust_t /* O - Level of trust */ | |
521 | httpCredentialsGetTrust( | |
376d7c69 MS |
522 | cups_array_t *credentials, /* I - Credentials */ |
523 | const char *common_name) /* I - Common name for trust lookup */ | |
3af9ac9e | 524 | { |
9653cfdf | 525 | SecCertificateRef secCert; /* Certificate reference */ |
524c65e6 MS |
526 | http_trust_t trust = HTTP_TRUST_OK; |
527 | /* Trusted? */ | |
376d7c69 | 528 | cups_array_t *tcreds = NULL; /* Trusted credentials */ |
9653cfdf MS |
529 | _cups_globals_t *cg = _cupsGlobals(); |
530 | /* Per-thread globals */ | |
3af9ac9e | 531 | |
9653cfdf | 532 | |
376d7c69 | 533 | if (!common_name) |
524c65e6 | 534 | return (HTTP_TRUST_UNKNOWN); |
376d7c69 | 535 | |
9653cfdf | 536 | if ((secCert = http_cdsa_create_credential((http_credential_t *)cupsArrayFirst(credentials))) == NULL) |
524c65e6 | 537 | return (HTTP_TRUST_UNKNOWN); |
9653cfdf | 538 | |
3abb875b MS |
539 | if (cg->any_root < 0) |
540 | _cupsSetDefaults(); | |
541 | ||
376d7c69 | 542 | /* |
88f1e9c8 | 543 | * Look this common name up in the default keychains... |
376d7c69 MS |
544 | */ |
545 | ||
88f1e9c8 | 546 | httpLoadCredentials(NULL, &tcreds, common_name); |
376d7c69 MS |
547 | |
548 | if (tcreds) | |
549 | { | |
550 | char credentials_str[1024], /* String for incoming credentials */ | |
551 | tcreds_str[1024]; /* String for saved credentials */ | |
552 | ||
553 | httpCredentialsString(credentials, credentials_str, sizeof(credentials_str)); | |
554 | httpCredentialsString(tcreds, tcreds_str, sizeof(tcreds_str)); | |
555 | ||
556 | if (strcmp(credentials_str, tcreds_str)) | |
557 | { | |
558 | /* | |
559 | * Credentials don't match, let's look at the expiration date of the new | |
560 | * credentials and allow if the new ones have a later expiration... | |
561 | */ | |
562 | ||
563 | if (httpCredentialsGetExpiration(credentials) <= httpCredentialsGetExpiration(tcreds) || | |
524c65e6 | 564 | !httpCredentialsAreValidForName(credentials, common_name)) |
376d7c69 MS |
565 | { |
566 | /* | |
567 | * Either the new credentials are not newly issued, or the common name | |
568 | * does not match the issued certificate... | |
569 | */ | |
570 | ||
524c65e6 | 571 | trust = HTTP_TRUST_INVALID; |
376d7c69 | 572 | } |
524c65e6 | 573 | else if (httpCredentialsGetExpiration(tcreds) < time(NULL)) |
376d7c69 MS |
574 | { |
575 | /* | |
524c65e6 | 576 | * Save the renewed credentials... |
376d7c69 MS |
577 | */ |
578 | ||
524c65e6 MS |
579 | trust = HTTP_TRUST_RENEWED; |
580 | ||
581 | httpSaveCredentials(NULL, credentials, common_name); | |
376d7c69 MS |
582 | } |
583 | } | |
584 | ||
585 | httpFreeCredentials(tcreds); | |
586 | } | |
f51f3773 | 587 | else if (cg->validate_certs && !httpCredentialsAreValidForName(credentials, common_name)) |
524c65e6 | 588 | trust = HTTP_TRUST_INVALID; |
376d7c69 | 589 | |
9653cfdf | 590 | if (!cg->expired_certs && !SecCertificateIsValid(secCert, CFAbsoluteTimeGetCurrent())) |
524c65e6 | 591 | trust = HTTP_TRUST_EXPIRED; |
25731360 | 592 | else if (!cg->any_root && cupsArrayCount(credentials) == 1) |
524c65e6 | 593 | trust = HTTP_TRUST_INVALID; |
376d7c69 | 594 | |
9653cfdf MS |
595 | CFRelease(secCert); |
596 | ||
524c65e6 | 597 | return (trust); |
3af9ac9e MS |
598 | } |
599 | ||
600 | ||
601 | /* | |
602 | * 'httpCredentialsGetExpiration()' - Return the expiration date of the credentials. | |
603 | * | |
e1f19878 | 604 | * @since CUPS 2.0/OS X 10.10@ |
3af9ac9e MS |
605 | */ |
606 | ||
607 | time_t /* O - Expiration date of credentials */ | |
608 | httpCredentialsGetExpiration( | |
609 | cups_array_t *credentials) /* I - Credentials */ | |
610 | { | |
9653cfdf MS |
611 | SecCertificateRef secCert; /* Certificate reference */ |
612 | time_t expiration; /* Expiration date */ | |
3af9ac9e | 613 | |
9653cfdf MS |
614 | |
615 | if ((secCert = http_cdsa_create_credential((http_credential_t *)cupsArrayFirst(credentials))) == NULL) | |
616 | return (0); | |
617 | ||
376d7c69 | 618 | expiration = (time_t)(SecCertificateNotValidAfter(secCert) + kCFAbsoluteTimeIntervalSince1970); |
9653cfdf MS |
619 | |
620 | CFRelease(secCert); | |
621 | ||
622 | return (expiration); | |
3af9ac9e MS |
623 | } |
624 | ||
625 | ||
72d05bc9 MS |
626 | /* |
627 | * 'httpCredentialsString()' - Return a string representing the credentials. | |
628 | * | |
e1f19878 | 629 | * @since CUPS 2.0/OS X 10.10@ |
72d05bc9 MS |
630 | */ |
631 | ||
632 | size_t /* O - Total size of credentials string */ | |
633 | httpCredentialsString( | |
634 | cups_array_t *credentials, /* I - Credentials */ | |
635 | char *buffer, /* I - Buffer or @code NULL@ */ | |
636 | size_t bufsize) /* I - Size of buffer */ | |
637 | { | |
376d7c69 | 638 | http_credential_t *first; /* First certificate */ |
9653cfdf | 639 | SecCertificateRef secCert; /* Certificate reference */ |
9653cfdf MS |
640 | |
641 | ||
376d7c69 MS |
642 | DEBUG_printf(("httpCredentialsString(credentials=%p, buffer=%p, bufsize=" CUPS_LLFMT ")", credentials, buffer, CUPS_LLCAST bufsize)); |
643 | ||
9653cfdf MS |
644 | if (!buffer) |
645 | return (0); | |
72d05bc9 MS |
646 | |
647 | if (buffer && bufsize > 0) | |
648 | *buffer = '\0'; | |
649 | ||
376d7c69 MS |
650 | if ((first = (http_credential_t *)cupsArrayFirst(credentials)) != NULL && |
651 | (secCert = http_cdsa_create_credential(first)) != NULL) | |
9653cfdf | 652 | { |
376d7c69 MS |
653 | CFStringRef cf_name; /* CF common name string */ |
654 | char name[256]; /* Common name associated with cert */ | |
655 | time_t expiration; /* Expiration date of cert */ | |
656 | _cups_md5_state_t md5_state; /* MD5 state */ | |
657 | unsigned char md5_digest[16]; /* MD5 result */ | |
658 | ||
659 | if ((cf_name = SecCertificateCopySubjectSummary(secCert)) != NULL) | |
9653cfdf | 660 | { |
376d7c69 MS |
661 | CFStringGetCString(cf_name, name, (CFIndex)sizeof(name), kCFStringEncodingUTF8); |
662 | CFRelease(cf_name); | |
9653cfdf | 663 | } |
376d7c69 MS |
664 | else |
665 | strlcpy(name, "unknown", sizeof(name)); | |
666 | ||
667 | expiration = (time_t)(SecCertificateNotValidAfter(secCert) + kCFAbsoluteTimeIntervalSince1970); | |
668 | ||
669 | _cupsMD5Init(&md5_state); | |
670 | _cupsMD5Append(&md5_state, first->data, (int)first->datalen); | |
671 | _cupsMD5Finish(&md5_state, md5_digest); | |
672 | ||
673 | snprintf(buffer, bufsize, "%s / %s / %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", name, httpGetDateString(expiration), 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]); | |
9653cfdf MS |
674 | |
675 | CFRelease(secCert); | |
676 | } | |
677 | ||
376d7c69 MS |
678 | DEBUG_printf(("1httpCredentialsString: Returning \"%s\".", buffer)); |
679 | ||
9653cfdf | 680 | return (strlen(buffer)); |
72d05bc9 MS |
681 | } |
682 | ||
683 | ||
2c85b752 MS |
684 | /* |
685 | * '_httpFreeCredentials()' - Free internal credentials. | |
686 | */ | |
687 | ||
688 | void | |
689 | _httpFreeCredentials( | |
690 | http_tls_credentials_t credentials) /* I - Internal credentials */ | |
691 | { | |
692 | if (!credentials) | |
693 | return; | |
694 | ||
695 | CFRelease(credentials); | |
696 | } | |
697 | ||
698 | ||
72d05bc9 MS |
699 | /* |
700 | * 'httpLoadCredentials()' - Load X.509 credentials from a keychain file. | |
701 | * | |
e1f19878 | 702 | * @since CUPS 2.0/OS 10.10@ |
72d05bc9 MS |
703 | */ |
704 | ||
dafebafd | 705 | int /* O - 0 on success, -1 on error */ |
72d05bc9 | 706 | httpLoadCredentials( |
f93b32b6 | 707 | const char *path, /* I - Keychain path or @code NULL@ for default */ |
72d05bc9 MS |
708 | cups_array_t **credentials, /* IO - Credentials */ |
709 | const char *common_name) /* I - Common name for credentials */ | |
710 | { | |
88f1e9c8 | 711 | #ifdef HAVE_SECKEYCHAINOPEN |
dafebafd | 712 | OSStatus err; /* Error info */ |
88f1e9c8 | 713 | char filename[1024]; /* Filename for keychain */ |
dafebafd MS |
714 | SecKeychainRef keychain = NULL;/* Keychain reference */ |
715 | SecIdentitySearchRef search = NULL; /* Search reference */ | |
88f1e9c8 MS |
716 | SecCertificateRef cert = NULL; /* Certificate */ |
717 | CFDataRef data; /* Certificate data */ | |
dafebafd MS |
718 | SecPolicyRef policy = NULL; /* Policy ref */ |
719 | CFStringRef cfcommon_name = NULL; | |
720 | /* Server name */ | |
721 | CFMutableDictionaryRef query = NULL; /* Query qualifiers */ | |
722 | CFArrayRef list = NULL; /* Keychain list */ | |
723 | ||
724 | ||
88f1e9c8 MS |
725 | DEBUG_printf(("httpLoadCredentials(path=\"%s\", credentials=%p, common_name=\"%s\")", path, credentials, common_name)); |
726 | ||
727 | if (!credentials) | |
728 | return (-1); | |
729 | ||
730 | *credentials = NULL; | |
731 | ||
732 | if (!path) | |
005f7f1f | 733 | path = http_cdsa_default_path(filename, sizeof(filename)); |
88f1e9c8 | 734 | |
88f1e9c8 | 735 | if ((err = SecKeychainOpen(path, &keychain)) != noErr) |
dafebafd MS |
736 | goto cleanup; |
737 | ||
738 | cfcommon_name = CFStringCreateWithCString(kCFAllocatorDefault, common_name, kCFStringEncodingUTF8); | |
739 | ||
740 | policy = SecPolicyCreateSSL(1, cfcommon_name); | |
741 | ||
742 | if (cfcommon_name) | |
743 | CFRelease(cfcommon_name); | |
744 | ||
745 | if (!policy) | |
746 | goto cleanup; | |
747 | ||
748 | if (!(query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks))) | |
749 | goto cleanup; | |
750 | ||
88f1e9c8 | 751 | list = CFArrayCreate(kCFAllocatorDefault, (const void **)&keychain, 1, &kCFTypeArrayCallBacks); |
dafebafd | 752 | |
88f1e9c8 | 753 | CFDictionaryAddValue(query, kSecClass, kSecClassCertificate); |
dafebafd MS |
754 | CFDictionaryAddValue(query, kSecMatchPolicy, policy); |
755 | CFDictionaryAddValue(query, kSecReturnRef, kCFBooleanTrue); | |
756 | CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitOne); | |
757 | CFDictionaryAddValue(query, kSecMatchSearchList, list); | |
758 | ||
759 | CFRelease(list); | |
760 | ||
88f1e9c8 | 761 | err = SecItemCopyMatching(query, (CFTypeRef *)&cert); |
dafebafd MS |
762 | |
763 | if (err) | |
764 | goto cleanup; | |
765 | ||
88f1e9c8 | 766 | if (CFGetTypeID(cert) != SecCertificateGetTypeID()) |
dafebafd MS |
767 | goto cleanup; |
768 | ||
88f1e9c8 MS |
769 | if ((data = SecCertificateCopyData(cert)) != NULL) |
770 | { | |
771 | DEBUG_printf(("1httpLoadCredentials: Adding %d byte certificate blob.", (int)CFDataGetLength(data))); | |
772 | ||
773 | *credentials = cupsArrayNew(NULL, NULL); | |
774 | httpAddCredential(*credentials, CFDataGetBytePtr(data), (size_t)CFDataGetLength(data)); | |
775 | CFRelease(data); | |
776 | } | |
dafebafd MS |
777 | |
778 | cleanup : | |
779 | ||
780 | if (keychain) | |
781 | CFRelease(keychain); | |
782 | if (search) | |
783 | CFRelease(search); | |
88f1e9c8 MS |
784 | if (cert) |
785 | CFRelease(cert); | |
dafebafd MS |
786 | if (policy) |
787 | CFRelease(policy); | |
788 | if (query) | |
789 | CFRelease(query); | |
790 | ||
88f1e9c8 MS |
791 | DEBUG_printf(("1httpLoadCredentials: Returning %d.", *credentials ? 0 : -1)); |
792 | ||
793 | return (*credentials ? 0 : -1); | |
794 | ||
795 | #else | |
796 | (void)path; | |
797 | (void)credentials; | |
798 | (void)common_name; | |
799 | ||
800 | return (-1); | |
801 | #endif /* HAVE_SECKEYCHAINOPEN */ | |
72d05bc9 MS |
802 | } |
803 | ||
804 | ||
72d05bc9 MS |
805 | /* |
806 | * 'httpSaveCredentials()' - Save X.509 credentials to a keychain file. | |
807 | * | |
e1f19878 | 808 | * @since CUPS 2.0/OS 10.10@ |
72d05bc9 MS |
809 | */ |
810 | ||
811 | int /* O - -1 on error, 0 on success */ | |
812 | httpSaveCredentials( | |
f93b32b6 | 813 | const char *path, /* I - Keychain path or @code NULL@ for default */ |
72d05bc9 MS |
814 | cups_array_t *credentials, /* I - Credentials */ |
815 | const char *common_name) /* I - Common name for credentials */ | |
816 | { | |
eb66bc71 | 817 | #ifdef HAVE_SECKEYCHAINOPEN |
88f1e9c8 | 818 | int ret = -1; /* Return value */ |
41e0907c | 819 | OSStatus err; /* Error info */ |
88f1e9c8 MS |
820 | char filename[1024]; /* Filename for keychain */ |
821 | SecKeychainRef keychain = NULL;/* Keychain reference */ | |
41e0907c | 822 | SecIdentitySearchRef search = NULL; /* Search reference */ |
88f1e9c8 | 823 | SecCertificateRef cert = NULL; /* Certificate */ |
88f1e9c8 | 824 | CFMutableDictionaryRef attrs = NULL; /* Attributes for add */ |
41e0907c MS |
825 | CFArrayRef list = NULL; /* Keychain list */ |
826 | ||
827 | ||
88f1e9c8 MS |
828 | DEBUG_printf(("httpSaveCredentials(path=\"%s\", credentials=%p, common_name=\"%s\")", path, credentials, common_name)); |
829 | if (!credentials) | |
41e0907c MS |
830 | goto cleanup; |
831 | ||
524c65e6 MS |
832 | if (!httpCredentialsAreValidForName(credentials, common_name)) |
833 | { | |
834 | DEBUG_puts("1httpSaveCredentials: Common name does not match."); | |
835 | return (-1); | |
836 | } | |
837 | ||
88f1e9c8 MS |
838 | if ((cert = http_cdsa_create_credential((http_credential_t *)cupsArrayFirst(credentials))) == NULL) |
839 | { | |
840 | DEBUG_puts("1httpSaveCredentials: Unable to create certificate."); | |
41e0907c | 841 | goto cleanup; |
88f1e9c8 | 842 | } |
41e0907c | 843 | |
88f1e9c8 | 844 | if (!path) |
005f7f1f | 845 | path = http_cdsa_default_path(filename, sizeof(filename)); |
2c85b752 | 846 | |
88f1e9c8 | 847 | if ((err = SecKeychainOpen(path, &keychain)) != noErr) |
2c85b752 | 848 | { |
88f1e9c8 MS |
849 | DEBUG_printf(("1httpSaveCredentials: SecKeychainOpen returned %d.", (int)err)); |
850 | goto cleanup; | |
2c85b752 | 851 | } |
2c85b752 | 852 | |
88f1e9c8 | 853 | if ((list = CFArrayCreate(kCFAllocatorDefault, (const void **)&keychain, 1, &kCFTypeArrayCallBacks)) == NULL) |
2c85b752 | 854 | { |
88f1e9c8 MS |
855 | DEBUG_puts("1httpSaveCredentials: Unable to create list of keychains."); |
856 | goto cleanup; | |
2c85b752 | 857 | } |
88f1e9c8 | 858 | |
88f1e9c8 MS |
859 | if ((attrs = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)) == NULL) |
860 | { | |
861 | DEBUG_puts("1httpSaveCredentials: Unable to create dictionary."); | |
862 | goto cleanup; | |
2c85b752 MS |
863 | } |
864 | ||
88f1e9c8 | 865 | CFDictionaryAddValue(attrs, kSecClass, kSecClassCertificate); |
88f1e9c8 MS |
866 | CFDictionaryAddValue(attrs, kSecValueRef, cert); |
867 | CFDictionaryAddValue(attrs, kSecMatchSearchList, list); | |
2c85b752 | 868 | |
88f1e9c8 | 869 | /* Note: SecItemAdd consumes "attrs"... */ |
524c65e6 | 870 | err = SecItemAdd(attrs, NULL); |
88f1e9c8 | 871 | DEBUG_printf(("1httpSaveCredentials: SecItemAdd returned %d.", (int)err)); |
2c85b752 | 872 | |
88f1e9c8 | 873 | cleanup : |
2c85b752 | 874 | |
88f1e9c8 MS |
875 | if (list) |
876 | CFRelease(list); | |
877 | if (keychain) | |
878 | CFRelease(keychain); | |
879 | if (search) | |
880 | CFRelease(search); | |
881 | if (cert) | |
882 | CFRelease(cert); | |
2c85b752 | 883 | |
88f1e9c8 | 884 | DEBUG_printf(("1httpSaveCredentials: Returning %d.", ret)); |
2c85b752 | 885 | |
88f1e9c8 | 886 | return (ret); |
2c85b752 | 887 | |
88f1e9c8 MS |
888 | #else |
889 | (void)path; | |
890 | (void)credentials; | |
891 | (void)common_name; | |
2c85b752 | 892 | |
88f1e9c8 MS |
893 | return (-1); |
894 | #endif /* HAVE_SECKEYCHAINOPEN */ | |
2c85b752 MS |
895 | } |
896 | ||
897 | ||
898 | /* | |
25731360 | 899 | * '_httpTLSInitialize()' - Initialize the TLS stack. |
2c85b752 MS |
900 | */ |
901 | ||
25731360 MS |
902 | void |
903 | _httpTLSInitialize(void) | |
2c85b752 MS |
904 | { |
905 | /* | |
906 | * Nothing to do... | |
907 | */ | |
908 | } | |
909 | ||
910 | ||
911 | /* | |
25731360 | 912 | * '_httpTLSPending()' - Return the number of pending TLS-encrypted bytes. |
2c85b752 MS |
913 | */ |
914 | ||
25731360 MS |
915 | size_t |
916 | _httpTLSPending(http_t *http) /* I - HTTP connection */ | |
2c85b752 MS |
917 | { |
918 | size_t bytes; /* Bytes that are available */ | |
919 | ||
920 | ||
921 | if (!SSLGetBufferedReadSize(http->tls, &bytes)) | |
922 | return (bytes); | |
923 | ||
924 | return (0); | |
925 | } | |
926 | ||
927 | ||
928 | /* | |
25731360 | 929 | * '_httpTLSRead()' - Read from a SSL/TLS connection. |
2c85b752 MS |
930 | */ |
931 | ||
25731360 MS |
932 | int /* O - Bytes read */ |
933 | _httpTLSRead(http_t *http, /* I - HTTP connection */ | |
2c85b752 MS |
934 | char *buf, /* I - Buffer to store data */ |
935 | int len) /* I - Length of buffer */ | |
936 | { | |
937 | int result; /* Return value */ | |
938 | OSStatus error; /* Error info */ | |
939 | size_t processed; /* Number of bytes processed */ | |
940 | ||
941 | ||
7e86f2f6 | 942 | error = SSLRead(http->tls, buf, (size_t)len, &processed); |
25731360 | 943 | DEBUG_printf(("6_httpTLSRead: error=%d, processed=%d", (int)error, |
2c85b752 MS |
944 | (int)processed)); |
945 | switch (error) | |
946 | { | |
947 | case 0 : | |
948 | result = (int)processed; | |
949 | break; | |
950 | ||
951 | case errSSLWouldBlock : | |
952 | if (processed) | |
953 | result = (int)processed; | |
954 | else | |
955 | { | |
956 | result = -1; | |
957 | errno = EINTR; | |
958 | } | |
959 | break; | |
960 | ||
961 | case errSSLClosedGraceful : | |
962 | default : | |
963 | if (processed) | |
964 | result = (int)processed; | |
965 | else | |
966 | { | |
967 | result = -1; | |
968 | errno = EPIPE; | |
969 | } | |
970 | break; | |
971 | } | |
972 | ||
973 | return (result); | |
974 | } | |
975 | ||
976 | ||
63aefcd5 MS |
977 | /* |
978 | * '_httpTLSSetOptions()' - Set TLS protocol and cipher suite options. | |
979 | */ | |
980 | ||
981 | void | |
982 | _httpTLSSetOptions(int options) /* I - Options */ | |
983 | { | |
984 | tls_options = options; | |
985 | } | |
986 | ||
987 | ||
2c85b752 | 988 | /* |
25731360 | 989 | * '_httpTLSStart()' - Set up SSL/TLS support on a connection. |
2c85b752 MS |
990 | */ |
991 | ||
25731360 MS |
992 | int /* O - 0 on success, -1 on failure */ |
993 | _httpTLSStart(http_t *http) /* I - HTTP connection */ | |
2c85b752 MS |
994 | { |
995 | char hostname[256], /* Hostname */ | |
996 | *hostptr; /* Pointer into hostname */ | |
997 | _cups_globals_t *cg = _cupsGlobals(); | |
998 | /* Pointer to library globals */ | |
999 | OSStatus error; /* Error code */ | |
1000 | const char *message = NULL;/* Error message */ | |
1001 | cups_array_t *credentials; /* Credentials array */ | |
1002 | cups_array_t *names; /* CUPS distinguished names */ | |
1003 | CFArrayRef dn_array; /* CF distinguished names array */ | |
1004 | CFIndex count; /* Number of credentials */ | |
1005 | CFDataRef data; /* Certificate data */ | |
1006 | int i; /* Looping var */ | |
1007 | http_credential_t *credential; /* Credential data */ | |
1008 | ||
1009 | ||
b37d45d9 MS |
1010 | DEBUG_printf(("3_httpTLSStart(http=%p)", http)); |
1011 | ||
1012 | if (tls_options < 0) | |
1013 | { | |
1014 | DEBUG_puts("4_httpTLSStart: Setting defaults."); | |
1015 | _cupsSetDefaults(); | |
1016 | DEBUG_printf(("4_httpTLSStart: tls_options=%x", tls_options)); | |
1017 | } | |
2c85b752 | 1018 | |
c913d726 | 1019 | #ifdef HAVE_SECKEYCHAINOPEN |
41e0907c | 1020 | if (http->mode == _HTTP_MODE_SERVER && !tls_keychain) |
c913d726 MS |
1021 | #else |
1022 | if (http->mode == _HTTP_MODE_SERVER) | |
1023 | #endif /* HAVE_SECKEYCHAINOPEN */ | |
2c85b752 | 1024 | { |
25731360 | 1025 | DEBUG_puts("4_httpTLSStart: cupsSetServerCredentials not called."); |
41e0907c MS |
1026 | http->error = errno = EINVAL; |
1027 | http->status = HTTP_STATUS_ERROR; | |
1028 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Server credentials not set."), 1); | |
2c85b752 | 1029 | |
41e0907c | 1030 | return (-1); |
2c85b752 MS |
1031 | } |
1032 | ||
72d05bc9 | 1033 | if ((http->tls = SSLCreateContext(kCFAllocatorDefault, http->mode == _HTTP_MODE_CLIENT ? kSSLClientSide : kSSLServerSide, kSSLStreamType)) == NULL) |
2c85b752 | 1034 | { |
25731360 | 1035 | DEBUG_puts("4_httpTLSStart: SSLCreateContext failed."); |
2c85b752 MS |
1036 | http->error = errno = ENOMEM; |
1037 | http->status = HTTP_STATUS_ERROR; | |
1038 | _cupsSetHTTPError(HTTP_STATUS_ERROR); | |
1039 | ||
1040 | return (-1); | |
1041 | } | |
1042 | ||
1043 | error = SSLSetConnection(http->tls, http); | |
25731360 | 1044 | DEBUG_printf(("4_httpTLSStart: SSLSetConnection, error=%d", (int)error)); |
2c85b752 MS |
1045 | |
1046 | if (!error) | |
1047 | { | |
1048 | error = SSLSetIOFuncs(http->tls, http_cdsa_read, http_cdsa_write); | |
25731360 | 1049 | DEBUG_printf(("4_httpTLSStart: SSLSetIOFuncs, error=%d", (int)error)); |
2c85b752 MS |
1050 | } |
1051 | ||
1052 | if (!error) | |
1053 | { | |
1054 | error = SSLSetSessionOption(http->tls, kSSLSessionOptionBreakOnServerAuth, | |
1055 | true); | |
63aefcd5 MS |
1056 | DEBUG_printf(("4_httpTLSStart: SSLSetSessionOption, error=%d", (int)error)); |
1057 | } | |
1058 | ||
1059 | if (!error) | |
1060 | { | |
ee6226a5 | 1061 | SSLProtocol minProtocol; |
4492316e | 1062 | |
ee6226a5 MS |
1063 | if (tls_options & _HTTP_TLS_DENY_TLS10) |
1064 | minProtocol = kTLSProtocol11; | |
1065 | else if (tls_options & _HTTP_TLS_ALLOW_SSL3) | |
1066 | minProtocol = kSSLProtocol3; | |
1067 | else | |
1068 | minProtocol = kTLSProtocol1; | |
1069 | ||
1070 | error = SSLSetProtocolVersionMin(http->tls, minProtocol); | |
1071 | DEBUG_printf(("4_httpTLSStart: SSLSetProtocolVersionMin(%d), error=%d", minProtocol, (int)error)); | |
63aefcd5 MS |
1072 | } |
1073 | ||
ee6226a5 | 1074 | # if HAVE_SSLSETENABLEDCIPHERS |
63aefcd5 MS |
1075 | if (!error) |
1076 | { | |
1077 | SSLCipherSuite supported[100]; /* Supported cipher suites */ | |
1078 | size_t num_supported; /* Number of supported cipher suites */ | |
1079 | SSLCipherSuite enabled[100]; /* Cipher suites to enable */ | |
1080 | size_t num_enabled; /* Number of cipher suites to enable */ | |
1081 | ||
1082 | num_supported = sizeof(supported) / sizeof(supported[0]); | |
1083 | error = SSLGetSupportedCiphers(http->tls, supported, &num_supported); | |
1084 | ||
1085 | if (!error) | |
1086 | { | |
1087 | DEBUG_printf(("4_httpTLSStart: %d cipher suites supported.", (int)num_supported)); | |
1088 | ||
1089 | for (i = 0, num_enabled = 0; i < (int)num_supported && num_enabled < (sizeof(enabled) / sizeof(enabled[0])); i ++) | |
1090 | { | |
1091 | switch (supported[i]) | |
1092 | { | |
1093 | /* Obviously insecure cipher suites that we never want to use */ | |
1094 | case SSL_NULL_WITH_NULL_NULL : | |
1095 | case SSL_RSA_WITH_NULL_MD5 : | |
1096 | case SSL_RSA_WITH_NULL_SHA : | |
1097 | case SSL_RSA_EXPORT_WITH_RC4_40_MD5 : | |
1098 | case SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5 : | |
1099 | case SSL_RSA_EXPORT_WITH_DES40_CBC_SHA : | |
1100 | case SSL_RSA_WITH_DES_CBC_SHA : | |
1101 | case SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA : | |
1102 | case SSL_DH_DSS_WITH_DES_CBC_SHA : | |
1103 | case SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA : | |
1104 | case SSL_DH_RSA_WITH_DES_CBC_SHA : | |
1105 | case SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA : | |
1106 | case SSL_DHE_DSS_WITH_DES_CBC_SHA : | |
1107 | case SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA : | |
1108 | case SSL_DHE_RSA_WITH_DES_CBC_SHA : | |
1109 | case SSL_DH_anon_EXPORT_WITH_RC4_40_MD5 : | |
1110 | case SSL_DH_anon_WITH_RC4_128_MD5 : | |
1111 | case SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA : | |
1112 | case SSL_DH_anon_WITH_DES_CBC_SHA : | |
1113 | case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA : | |
1114 | case SSL_FORTEZZA_DMS_WITH_NULL_SHA : | |
1115 | case TLS_DH_anon_WITH_AES_128_CBC_SHA : | |
1116 | case TLS_DH_anon_WITH_AES_256_CBC_SHA : | |
1117 | case TLS_ECDH_ECDSA_WITH_NULL_SHA : | |
1118 | case TLS_ECDHE_RSA_WITH_NULL_SHA : | |
1119 | case TLS_ECDH_anon_WITH_NULL_SHA : | |
1120 | case TLS_ECDH_anon_WITH_RC4_128_SHA : | |
1121 | case TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA : | |
1122 | case TLS_ECDH_anon_WITH_AES_128_CBC_SHA : | |
1123 | case TLS_ECDH_anon_WITH_AES_256_CBC_SHA : | |
1124 | case TLS_RSA_WITH_NULL_SHA256 : | |
1125 | case TLS_DH_anon_WITH_AES_128_CBC_SHA256 : | |
1126 | case TLS_DH_anon_WITH_AES_256_CBC_SHA256 : | |
1127 | case TLS_PSK_WITH_NULL_SHA : | |
1128 | case TLS_DHE_PSK_WITH_NULL_SHA : | |
1129 | case TLS_RSA_PSK_WITH_NULL_SHA : | |
1130 | case TLS_DH_anon_WITH_AES_128_GCM_SHA256 : | |
1131 | case TLS_DH_anon_WITH_AES_256_GCM_SHA384 : | |
1132 | case TLS_PSK_WITH_NULL_SHA256 : | |
1133 | case TLS_PSK_WITH_NULL_SHA384 : | |
1134 | case TLS_DHE_PSK_WITH_NULL_SHA256 : | |
1135 | case TLS_DHE_PSK_WITH_NULL_SHA384 : | |
1136 | case TLS_RSA_PSK_WITH_NULL_SHA256 : | |
1137 | case TLS_RSA_PSK_WITH_NULL_SHA384 : | |
1138 | case SSL_RSA_WITH_DES_CBC_MD5 : | |
b37d45d9 | 1139 | DEBUG_printf(("4_httpTLSStart: Excluding insecure cipher suite %d", supported[i])); |
63aefcd5 MS |
1140 | break; |
1141 | ||
1142 | /* RC4 cipher suites that should only be used as a last resort */ | |
1143 | case SSL_RSA_WITH_RC4_128_MD5 : | |
1144 | case SSL_RSA_WITH_RC4_128_SHA : | |
1145 | case TLS_ECDH_ECDSA_WITH_RC4_128_SHA : | |
1146 | case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA : | |
1147 | case TLS_ECDH_RSA_WITH_RC4_128_SHA : | |
1148 | case TLS_ECDHE_RSA_WITH_RC4_128_SHA : | |
1149 | case TLS_PSK_WITH_RC4_128_SHA : | |
1150 | case TLS_DHE_PSK_WITH_RC4_128_SHA : | |
1151 | case TLS_RSA_PSK_WITH_RC4_128_SHA : | |
1152 | if (tls_options & _HTTP_TLS_ALLOW_RC4) | |
1153 | enabled[num_enabled ++] = supported[i]; | |
b37d45d9 MS |
1154 | else |
1155 | DEBUG_printf(("4_httpTLSStart: Excluding RC4 cipher suite %d", supported[i])); | |
63aefcd5 MS |
1156 | break; |
1157 | ||
ee6226a5 MS |
1158 | /* DH/DHE cipher suites that are problematic with parameters < 1024 bits */ |
1159 | case TLS_DH_DSS_WITH_AES_128_CBC_SHA : | |
1160 | case TLS_DH_RSA_WITH_AES_128_CBC_SHA : | |
1161 | case TLS_DHE_DSS_WITH_AES_128_CBC_SHA : | |
1162 | case TLS_DHE_RSA_WITH_AES_128_CBC_SHA : | |
1163 | case TLS_DH_DSS_WITH_AES_256_CBC_SHA : | |
1164 | case TLS_DH_RSA_WITH_AES_256_CBC_SHA : | |
1165 | case TLS_DHE_DSS_WITH_AES_256_CBC_SHA : | |
1166 | case TLS_DHE_RSA_WITH_AES_256_CBC_SHA : | |
1167 | case TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA : | |
1168 | case TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA : | |
bdc4056c | 1169 | // case TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA : |
ee6226a5 MS |
1170 | case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA : |
1171 | case TLS_DH_DSS_WITH_AES_128_CBC_SHA256 : | |
1172 | case TLS_DH_RSA_WITH_AES_128_CBC_SHA256 : | |
1173 | case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 : | |
1174 | case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 : | |
1175 | case TLS_DH_DSS_WITH_AES_256_CBC_SHA256 : | |
1176 | case TLS_DH_RSA_WITH_AES_256_CBC_SHA256 : | |
1177 | case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 : | |
1178 | case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 : | |
1179 | case TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA : | |
1180 | case TLS_DHE_PSK_WITH_AES_128_CBC_SHA : | |
1181 | case TLS_DHE_PSK_WITH_AES_256_CBC_SHA : | |
bdc4056c MS |
1182 | // case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 : |
1183 | // case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 : | |
ee6226a5 MS |
1184 | case TLS_DH_RSA_WITH_AES_128_GCM_SHA256 : |
1185 | case TLS_DH_RSA_WITH_AES_256_GCM_SHA384 : | |
bdc4056c MS |
1186 | // case TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 : |
1187 | // case TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 : | |
ee6226a5 MS |
1188 | case TLS_DH_DSS_WITH_AES_128_GCM_SHA256 : |
1189 | case TLS_DH_DSS_WITH_AES_256_GCM_SHA384 : | |
1190 | case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 : | |
1191 | case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 : | |
1192 | case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 : | |
1193 | case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 : | |
1194 | if (tls_options & _HTTP_TLS_ALLOW_DH) | |
1195 | enabled[num_enabled ++] = supported[i]; | |
b37d45d9 MS |
1196 | else |
1197 | DEBUG_printf(("4_httpTLSStart: Excluding DH/DHE cipher suite %d", supported[i])); | |
ee6226a5 MS |
1198 | break; |
1199 | ||
63aefcd5 MS |
1200 | /* Anything else we'll assume is secure */ |
1201 | default : | |
1202 | enabled[num_enabled ++] = supported[i]; | |
1203 | break; | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | DEBUG_printf(("4_httpTLSStart: %d cipher suites enabled.", (int)num_enabled)); | |
1208 | error = SSLSetEnabledCiphers(http->tls, enabled, num_enabled); | |
1209 | } | |
2c85b752 | 1210 | } |
ee6226a5 | 1211 | #endif /* HAVE_SSLSETENABLEDCIPHERS */ |
2c85b752 | 1212 | |
41e0907c | 1213 | if (!error && http->mode == _HTTP_MODE_CLIENT) |
2c85b752 | 1214 | { |
41e0907c MS |
1215 | /* |
1216 | * Client: set client-side credentials, if any... | |
1217 | */ | |
1218 | ||
2c85b752 MS |
1219 | if (cg->client_cert_cb) |
1220 | { | |
1221 | error = SSLSetSessionOption(http->tls, | |
1222 | kSSLSessionOptionBreakOnCertRequested, true); | |
25731360 | 1223 | DEBUG_printf(("4_httpTLSStart: kSSLSessionOptionBreakOnCertRequested, " |
2c85b752 MS |
1224 | "error=%d", (int)error)); |
1225 | } | |
1226 | else | |
1227 | { | |
88f1e9c8 MS |
1228 | error = http_cdsa_set_credentials(http); |
1229 | DEBUG_printf(("4_httpTLSStart: http_cdsa_set_credentials, error=%d", | |
2c85b752 MS |
1230 | (int)error)); |
1231 | } | |
1232 | } | |
41e0907c MS |
1233 | else if (!error) |
1234 | { | |
1235 | /* | |
1236 | * Server: find/create a certificate for TLS... | |
1237 | */ | |
1238 | ||
1239 | if (http->fields[HTTP_FIELD_HOST][0]) | |
1240 | { | |
1241 | /* | |
1242 | * Use hostname for TLS upgrade... | |
1243 | */ | |
1244 | ||
1245 | strlcpy(hostname, http->fields[HTTP_FIELD_HOST], sizeof(hostname)); | |
1246 | } | |
1247 | else | |
1248 | { | |
1249 | /* | |
1250 | * Resolve hostname from connection address... | |
1251 | */ | |
1252 | ||
1253 | http_addr_t addr; /* Connection address */ | |
1254 | socklen_t addrlen; /* Length of address */ | |
1255 | ||
1256 | addrlen = sizeof(addr); | |
1257 | if (getsockname(http->fd, (struct sockaddr *)&addr, &addrlen)) | |
1258 | { | |
25731360 | 1259 | DEBUG_printf(("4_httpTLSStart: Unable to get socket address: %s", strerror(errno))); |
41e0907c MS |
1260 | hostname[0] = '\0'; |
1261 | } | |
1262 | else if (httpAddrLocalhost(&addr)) | |
1263 | hostname[0] = '\0'; | |
1264 | else | |
a27a134a MS |
1265 | { |
1266 | httpAddrLookup(&addr, hostname, sizeof(hostname)); | |
25731360 | 1267 | DEBUG_printf(("4_httpTLSStart: Resolved socket address to \"%s\".", hostname)); |
a27a134a | 1268 | } |
41e0907c MS |
1269 | } |
1270 | ||
eb66bc71 | 1271 | #ifdef HAVE_SECKEYCHAINOPEN |
a27a134a MS |
1272 | if (isdigit(hostname[0] & 255) || hostname[0] == '[') |
1273 | hostname[0] = '\0'; /* Don't allow numeric addresses */ | |
1274 | ||
41e0907c MS |
1275 | if (hostname[0]) |
1276 | http->tls_credentials = http_cdsa_copy_server(hostname); | |
1277 | else if (tls_common_name) | |
1278 | http->tls_credentials = http_cdsa_copy_server(tls_common_name); | |
1279 | ||
1280 | if (!http->tls_credentials && tls_auto_create && (hostname[0] || tls_common_name)) | |
1281 | { | |
25731360 | 1282 | DEBUG_printf(("4_httpTLSStart: Auto-create credentials for \"%s\".", hostname[0] ? hostname : tls_common_name)); |
41e0907c MS |
1283 | |
1284 | if (!cupsMakeServerCredentials(tls_keypath, hostname[0] ? hostname : tls_common_name, 0, NULL, time(NULL) + 365 * 86400)) | |
1285 | { | |
25731360 | 1286 | DEBUG_puts("4_httpTLSStart: cupsMakeServerCredentials failed."); |
41e0907c MS |
1287 | http->error = errno = EINVAL; |
1288 | http->status = HTTP_STATUS_ERROR; | |
1289 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create server credentials."), 1); | |
1290 | ||
1291 | return (-1); | |
1292 | } | |
1293 | ||
1294 | http->tls_credentials = http_cdsa_copy_server(hostname[0] ? hostname : tls_common_name); | |
1295 | } | |
eb66bc71 | 1296 | #endif /* HAVE_SECKEYCHAINOPEN */ |
41e0907c MS |
1297 | |
1298 | if (!http->tls_credentials) | |
1299 | { | |
25731360 | 1300 | DEBUG_puts("4_httpTLSStart: Unable to find server credentials."); |
41e0907c MS |
1301 | http->error = errno = EINVAL; |
1302 | http->status = HTTP_STATUS_ERROR; | |
1303 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to find server credentials."), 1); | |
1304 | ||
1305 | return (-1); | |
1306 | } | |
1307 | ||
1308 | error = SSLSetCertificate(http->tls, http->tls_credentials); | |
1309 | ||
25731360 | 1310 | DEBUG_printf(("4_httpTLSStart: SSLSetCertificate, error=%d", (int)error)); |
41e0907c MS |
1311 | } |
1312 | ||
25731360 | 1313 | DEBUG_printf(("4_httpTLSStart: tls_credentials=%p", http->tls_credentials)); |
2c85b752 MS |
1314 | |
1315 | /* | |
1316 | * Let the server know which hostname/domain we are trying to connect to | |
1317 | * in case it wants to serve up a certificate with a matching common name. | |
1318 | */ | |
1319 | ||
41e0907c | 1320 | if (!error && http->mode == _HTTP_MODE_CLIENT) |
2c85b752 | 1321 | { |
41e0907c MS |
1322 | /* |
1323 | * Client: get the hostname to use for TLS... | |
1324 | */ | |
1325 | ||
1326 | if (httpAddrLocalhost(http->hostaddr)) | |
1327 | { | |
1328 | strlcpy(hostname, "localhost", sizeof(hostname)); | |
1329 | } | |
1330 | else | |
1331 | { | |
1332 | /* | |
1333 | * Otherwise make sure the hostname we have does not end in a trailing dot. | |
1334 | */ | |
1335 | ||
1336 | strlcpy(hostname, http->hostname, sizeof(hostname)); | |
1337 | if ((hostptr = hostname + strlen(hostname) - 1) >= hostname && | |
1338 | *hostptr == '.') | |
1339 | *hostptr = '\0'; | |
1340 | } | |
1341 | ||
2c85b752 MS |
1342 | error = SSLSetPeerDomainName(http->tls, hostname, strlen(hostname)); |
1343 | ||
25731360 | 1344 | DEBUG_printf(("4_httpTLSStart: SSLSetPeerDomainName, error=%d", (int)error)); |
2c85b752 MS |
1345 | } |
1346 | ||
1347 | if (!error) | |
1348 | { | |
1349 | int done = 0; /* Are we done yet? */ | |
1350 | ||
1351 | while (!error && !done) | |
1352 | { | |
1353 | error = SSLHandshake(http->tls); | |
1354 | ||
25731360 | 1355 | DEBUG_printf(("4_httpTLSStart: SSLHandshake returned %d.", (int)error)); |
2c85b752 MS |
1356 | |
1357 | switch (error) | |
1358 | { | |
1359 | case noErr : | |
1360 | done = 1; | |
1361 | break; | |
1362 | ||
1363 | case errSSLWouldBlock : | |
1364 | error = noErr; /* Force a retry */ | |
1365 | usleep(1000); /* in 1 millisecond */ | |
1366 | break; | |
1367 | ||
1368 | case errSSLServerAuthCompleted : | |
1369 | error = 0; | |
1370 | if (cg->server_cert_cb) | |
1371 | { | |
1372 | error = httpCopyCredentials(http, &credentials); | |
1373 | if (!error) | |
1374 | { | |
1375 | error = (cg->server_cert_cb)(http, http->tls, credentials, | |
1376 | cg->server_cert_data); | |
1377 | httpFreeCredentials(credentials); | |
1378 | } | |
1379 | ||
25731360 | 1380 | DEBUG_printf(("4_httpTLSStart: Server certificate callback " |
2c85b752 MS |
1381 | "returned %d.", (int)error)); |
1382 | } | |
1383 | break; | |
1384 | ||
1385 | case errSSLClientCertRequested : | |
1386 | error = 0; | |
1387 | ||
1388 | if (cg->client_cert_cb) | |
1389 | { | |
1390 | names = NULL; | |
1391 | if (!(error = SSLCopyDistinguishedNames(http->tls, &dn_array)) && | |
1392 | dn_array) | |
1393 | { | |
1394 | if ((names = cupsArrayNew(NULL, NULL)) != NULL) | |
1395 | { | |
1396 | for (i = 0, count = CFArrayGetCount(dn_array); i < count; i++) | |
1397 | { | |
1398 | data = (CFDataRef)CFArrayGetValueAtIndex(dn_array, i); | |
1399 | ||
1400 | if ((credential = malloc(sizeof(*credential))) != NULL) | |
1401 | { | |
7e86f2f6 | 1402 | credential->datalen = (size_t)CFDataGetLength(data); |
2c85b752 MS |
1403 | if ((credential->data = malloc(credential->datalen))) |
1404 | { | |
1405 | memcpy((void *)credential->data, CFDataGetBytePtr(data), | |
1406 | credential->datalen); | |
1407 | cupsArrayAdd(names, credential); | |
1408 | } | |
1409 | else | |
1410 | free(credential); | |
1411 | } | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | CFRelease(dn_array); | |
1416 | } | |
1417 | ||
1418 | if (!error) | |
1419 | { | |
1420 | error = (cg->client_cert_cb)(http, http->tls, names, | |
1421 | cg->client_cert_data); | |
1422 | ||
25731360 | 1423 | DEBUG_printf(("4_httpTLSStart: Client certificate callback " |
2c85b752 MS |
1424 | "returned %d.", (int)error)); |
1425 | } | |
1426 | ||
1427 | httpFreeCredentials(names); | |
1428 | } | |
1429 | break; | |
1430 | ||
1431 | case errSSLUnknownRootCert : | |
1432 | message = _("Unable to establish a secure connection to host " | |
1433 | "(untrusted certificate)."); | |
1434 | break; | |
1435 | ||
1436 | case errSSLNoRootCert : | |
1437 | message = _("Unable to establish a secure connection to host " | |
1438 | "(self-signed certificate)."); | |
1439 | break; | |
1440 | ||
1441 | case errSSLCertExpired : | |
1442 | message = _("Unable to establish a secure connection to host " | |
1443 | "(expired certificate)."); | |
1444 | break; | |
1445 | ||
1446 | case errSSLCertNotYetValid : | |
1447 | message = _("Unable to establish a secure connection to host " | |
1448 | "(certificate not yet valid)."); | |
1449 | break; | |
1450 | ||
1451 | case errSSLHostNameMismatch : | |
1452 | message = _("Unable to establish a secure connection to host " | |
1453 | "(host name mismatch)."); | |
1454 | break; | |
1455 | ||
1456 | case errSSLXCertChainInvalid : | |
1457 | message = _("Unable to establish a secure connection to host " | |
1458 | "(certificate chain invalid)."); | |
1459 | break; | |
1460 | ||
1461 | case errSSLConnectionRefused : | |
1462 | message = _("Unable to establish a secure connection to host " | |
1463 | "(peer dropped connection before responding)."); | |
1464 | break; | |
1465 | ||
1466 | default : | |
1467 | break; | |
1468 | } | |
1469 | } | |
1470 | } | |
1471 | ||
1472 | if (error) | |
1473 | { | |
1474 | http->error = error; | |
1475 | http->status = HTTP_STATUS_ERROR; | |
1476 | errno = ECONNREFUSED; | |
1477 | ||
1478 | CFRelease(http->tls); | |
1479 | http->tls = NULL; | |
1480 | ||
1481 | /* | |
1482 | * If an error string wasn't set by the callbacks use a generic one... | |
1483 | */ | |
1484 | ||
1485 | if (!message) | |
1486 | #ifdef HAVE_CSSMERRORSTRING | |
1487 | message = cssmErrorString(error); | |
1488 | #else | |
1489 | message = _("Unable to establish a secure connection to host."); | |
1490 | #endif /* HAVE_CSSMERRORSTRING */ | |
1491 | ||
1492 | _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, message, 1); | |
1493 | ||
1494 | return (-1); | |
1495 | } | |
1496 | ||
1497 | return (0); | |
1498 | } | |
1499 | ||
1500 | ||
1501 | /* | |
25731360 | 1502 | * '_httpTLSStop()' - Shut down SSL/TLS on a connection. |
2c85b752 MS |
1503 | */ |
1504 | ||
25731360 MS |
1505 | void |
1506 | _httpTLSStop(http_t *http) /* I - HTTP connection */ | |
2c85b752 MS |
1507 | { |
1508 | while (SSLClose(http->tls) == errSSLWouldBlock) | |
1509 | usleep(1000); | |
1510 | ||
1511 | CFRelease(http->tls); | |
1512 | ||
1513 | if (http->tls_credentials) | |
1514 | CFRelease(http->tls_credentials); | |
1515 | ||
1516 | http->tls = NULL; | |
1517 | http->tls_credentials = NULL; | |
1518 | } | |
1519 | ||
1520 | ||
1521 | /* | |
25731360 | 1522 | * '_httpTLSWrite()' - Write to a SSL/TLS connection. |
2c85b752 MS |
1523 | */ |
1524 | ||
25731360 MS |
1525 | int /* O - Bytes written */ |
1526 | _httpTLSWrite(http_t *http, /* I - HTTP connection */ | |
2c85b752 MS |
1527 | const char *buf, /* I - Buffer holding data */ |
1528 | int len) /* I - Length of buffer */ | |
1529 | { | |
1530 | ssize_t result; /* Return value */ | |
1531 | OSStatus error; /* Error info */ | |
1532 | size_t processed; /* Number of bytes processed */ | |
1533 | ||
1534 | ||
25731360 | 1535 | DEBUG_printf(("2_httpTLSWrite(http=%p, buf=%p, len=%d)", http, buf, len)); |
2c85b752 | 1536 | |
7e86f2f6 | 1537 | error = SSLWrite(http->tls, buf, (size_t)len, &processed); |
2c85b752 MS |
1538 | |
1539 | switch (error) | |
1540 | { | |
1541 | case 0 : | |
1542 | result = (int)processed; | |
1543 | break; | |
1544 | ||
1545 | case errSSLWouldBlock : | |
1546 | if (processed) | |
1547 | { | |
1548 | result = (int)processed; | |
1549 | } | |
1550 | else | |
1551 | { | |
1552 | result = -1; | |
1553 | errno = EINTR; | |
1554 | } | |
1555 | break; | |
1556 | ||
1557 | case errSSLClosedGraceful : | |
1558 | default : | |
1559 | if (processed) | |
1560 | { | |
1561 | result = (int)processed; | |
1562 | } | |
1563 | else | |
1564 | { | |
1565 | result = -1; | |
1566 | errno = EPIPE; | |
1567 | } | |
1568 | break; | |
1569 | } | |
1570 | ||
25731360 | 1571 | DEBUG_printf(("3_httpTLSWrite: Returning %d.", (int)result)); |
2c85b752 MS |
1572 | |
1573 | return ((int)result); | |
1574 | } | |
1575 | ||
1576 | ||
88f1e9c8 | 1577 | #ifdef HAVE_SECKEYCHAINOPEN |
2c85b752 | 1578 | /* |
88f1e9c8 | 1579 | * 'http_cdsa_copy_server()' - Find and copy server credentials from the keychain. |
2c85b752 MS |
1580 | */ |
1581 | ||
88f1e9c8 MS |
1582 | static CFArrayRef /* O - Array of certificates or NULL */ |
1583 | http_cdsa_copy_server( | |
1584 | const char *common_name) /* I - Server's hostname */ | |
2c85b752 | 1585 | { |
88f1e9c8 MS |
1586 | OSStatus err; /* Error info */ |
1587 | SecIdentitySearchRef search = NULL; /* Search reference */ | |
1588 | SecIdentityRef identity = NULL;/* Identity */ | |
1589 | CFArrayRef certificates = NULL; | |
1590 | /* Certificate array */ | |
1591 | SecPolicyRef policy = NULL; /* Policy ref */ | |
1592 | CFStringRef cfcommon_name = NULL; | |
1593 | /* Server name */ | |
1594 | CFMutableDictionaryRef query = NULL; /* Query qualifiers */ | |
1595 | CFArrayRef list = NULL; /* Keychain list */ | |
2c85b752 | 1596 | |
2c85b752 | 1597 | |
88f1e9c8 | 1598 | cfcommon_name = CFStringCreateWithCString(kCFAllocatorDefault, common_name, kCFStringEncodingUTF8); |
2c85b752 | 1599 | |
88f1e9c8 MS |
1600 | policy = SecPolicyCreateSSL(1, cfcommon_name); |
1601 | ||
1602 | if (cfcommon_name) | |
1603 | CFRelease(cfcommon_name); | |
1604 | ||
1605 | if (!policy) | |
1606 | goto cleanup; | |
1607 | ||
1608 | if (!(query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks))) | |
1609 | goto cleanup; | |
1610 | ||
f93b32b6 MS |
1611 | _cupsMutexLock(&tls_mutex); |
1612 | ||
88f1e9c8 MS |
1613 | list = CFArrayCreate(kCFAllocatorDefault, (const void **)&tls_keychain, 1, &kCFTypeArrayCallBacks); |
1614 | ||
1615 | CFDictionaryAddValue(query, kSecClass, kSecClassIdentity); | |
1616 | CFDictionaryAddValue(query, kSecMatchPolicy, policy); | |
1617 | CFDictionaryAddValue(query, kSecReturnRef, kCFBooleanTrue); | |
1618 | CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitOne); | |
1619 | CFDictionaryAddValue(query, kSecMatchSearchList, list); | |
1620 | ||
1621 | CFRelease(list); | |
1622 | ||
1623 | err = SecItemCopyMatching(query, (CFTypeRef *)&identity); | |
1624 | ||
f93b32b6 MS |
1625 | _cupsMutexUnlock(&tls_mutex); |
1626 | ||
88f1e9c8 MS |
1627 | if (err) |
1628 | goto cleanup; | |
1629 | ||
1630 | if (CFGetTypeID(identity) != SecIdentityGetTypeID()) | |
1631 | goto cleanup; | |
1632 | ||
1633 | if ((certificates = CFArrayCreate(NULL, (const void **)&identity, 1, &kCFTypeArrayCallBacks)) == NULL) | |
1634 | goto cleanup; | |
1635 | ||
1636 | cleanup : | |
1637 | ||
1638 | if (search) | |
1639 | CFRelease(search); | |
1640 | if (identity) | |
1641 | CFRelease(identity); | |
1642 | ||
1643 | if (policy) | |
1644 | CFRelease(policy); | |
1645 | if (query) | |
1646 | CFRelease(query); | |
1647 | ||
1648 | return (certificates); | |
2c85b752 | 1649 | } |
88f1e9c8 | 1650 | #endif /* HAVE_SECKEYCHAINOPEN */ |
2c85b752 MS |
1651 | |
1652 | ||
2ece34a9 MS |
1653 | /* |
1654 | * 'http_cdsa_create_credential()' - Create a single credential in the internal format. | |
1655 | */ | |
1656 | ||
1657 | static SecCertificateRef /* O - Certificate */ | |
1658 | http_cdsa_create_credential( | |
1659 | http_credential_t *credential) /* I - Credential */ | |
1660 | { | |
1661 | if (!credential) | |
1662 | return (NULL); | |
1663 | ||
1664 | return (SecCertificateCreateWithBytes(kCFAllocatorDefault, credential->data, (CFIndex)credential->datalen)); | |
1665 | } | |
1666 | ||
1667 | ||
005f7f1f MS |
1668 | /* |
1669 | * 'http_cdsa_default_path()' - Get the default keychain path. | |
1670 | */ | |
1671 | ||
1672 | static const char * /* O - Keychain path */ | |
1673 | http_cdsa_default_path(char *buffer, /* I - Path buffer */ | |
1674 | size_t bufsize) /* I - Size of buffer */ | |
1675 | { | |
1676 | const char *home = getenv("HOME"); /* HOME environment variable */ | |
1677 | ||
1678 | ||
1679 | if (getuid() && home) | |
1680 | snprintf(buffer, bufsize, "%s/Library/Keychains/login.keychain", home); | |
1681 | else | |
1682 | strlcpy(buffer, "/Library/Keychains/System.keychain", bufsize); | |
1683 | ||
1684 | DEBUG_printf(("1http_cdsa_default_path: Using default path \"%s\".", buffer)); | |
1685 | ||
1686 | return (buffer); | |
1687 | } | |
1688 | ||
1689 | ||
2c85b752 | 1690 | /* |
88f1e9c8 | 1691 | * 'http_cdsa_read()' - Read function for the CDSA library. |
2c85b752 MS |
1692 | */ |
1693 | ||
88f1e9c8 MS |
1694 | static OSStatus /* O - -1 on error, 0 on success */ |
1695 | http_cdsa_read( | |
1696 | SSLConnectionRef connection, /* I - SSL/TLS connection */ | |
1697 | void *data, /* I - Data buffer */ | |
1698 | size_t *dataLength) /* IO - Number of bytes */ | |
2c85b752 | 1699 | { |
88f1e9c8 MS |
1700 | OSStatus result; /* Return value */ |
1701 | ssize_t bytes; /* Number of bytes read */ | |
1702 | http_t *http; /* HTTP connection */ | |
2c85b752 | 1703 | |
2c85b752 | 1704 | |
88f1e9c8 | 1705 | http = (http_t *)connection; |
2c85b752 | 1706 | |
88f1e9c8 | 1707 | if (!http->blocking) |
2c85b752 MS |
1708 | { |
1709 | /* | |
88f1e9c8 | 1710 | * Make sure we have data before we read... |
2c85b752 MS |
1711 | */ |
1712 | ||
88f1e9c8 MS |
1713 | while (!_httpWait(http, http->wait_value, 0)) |
1714 | { | |
1715 | if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) | |
1716 | continue; | |
1717 | ||
1718 | http->error = ETIMEDOUT; | |
1719 | return (-1); | |
1720 | } | |
2c85b752 MS |
1721 | } |
1722 | ||
88f1e9c8 | 1723 | do |
2c85b752 | 1724 | { |
88f1e9c8 | 1725 | bytes = recv(http->fd, data, *dataLength, 0); |
2c85b752 | 1726 | } |
88f1e9c8 | 1727 | while (bytes == -1 && (errno == EINTR || errno == EAGAIN)); |
2c85b752 | 1728 | |
88f1e9c8 MS |
1729 | if ((size_t)bytes == *dataLength) |
1730 | { | |
1731 | result = 0; | |
1732 | } | |
1733 | else if (bytes > 0) | |
1734 | { | |
1735 | *dataLength = (size_t)bytes; | |
1736 | result = errSSLWouldBlock; | |
1737 | } | |
1738 | else | |
1739 | { | |
1740 | *dataLength = 0; | |
2c85b752 | 1741 | |
88f1e9c8 MS |
1742 | if (bytes == 0) |
1743 | result = errSSLClosedGraceful; | |
1744 | else if (errno == EAGAIN) | |
1745 | result = errSSLWouldBlock; | |
1746 | else | |
1747 | result = errSSLClosedAbort; | |
1748 | } | |
2c85b752 | 1749 | |
88f1e9c8 MS |
1750 | return (result); |
1751 | } | |
2c85b752 | 1752 | |
2c85b752 | 1753 | |
88f1e9c8 MS |
1754 | /* |
1755 | * 'http_cdsa_set_credentials()' - Set the TLS credentials. | |
1756 | */ | |
2c85b752 | 1757 | |
88f1e9c8 MS |
1758 | static int /* O - Status of connection */ |
1759 | http_cdsa_set_credentials(http_t *http) /* I - HTTP connection */ | |
1760 | { | |
1761 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
1762 | OSStatus error = 0; /* Error code */ | |
1763 | http_tls_credentials_t credentials = NULL; | |
1764 | /* TLS credentials */ | |
2c85b752 | 1765 | |
2c85b752 | 1766 | |
88f1e9c8 | 1767 | DEBUG_printf(("7http_tls_set_credentials(%p)", http)); |
2c85b752 | 1768 | |
88f1e9c8 MS |
1769 | /* |
1770 | * Prefer connection specific credentials... | |
1771 | */ | |
2c85b752 | 1772 | |
88f1e9c8 MS |
1773 | if ((credentials = http->tls_credentials) == NULL) |
1774 | credentials = cg->tls_credentials; | |
2c85b752 | 1775 | |
88f1e9c8 MS |
1776 | if (credentials) |
1777 | { | |
1778 | error = SSLSetCertificate(http->tls, credentials); | |
1779 | DEBUG_printf(("4http_tls_set_credentials: SSLSetCertificate, error=%d", | |
1780 | (int)error)); | |
2c85b752 | 1781 | } |
88f1e9c8 MS |
1782 | else |
1783 | DEBUG_puts("4http_tls_set_credentials: No credentials to set."); | |
1784 | ||
1785 | return (error); | |
1786 | } | |
1787 | ||
1788 | ||
1789 | /* | |
1790 | * 'http_cdsa_write()' - Write function for the CDSA library. | |
1791 | */ | |
1792 | ||
1793 | static OSStatus /* O - -1 on error, 0 on success */ | |
1794 | http_cdsa_write( | |
1795 | SSLConnectionRef connection, /* I - SSL/TLS connection */ | |
1796 | const void *data, /* I - Data buffer */ | |
1797 | size_t *dataLength) /* IO - Number of bytes */ | |
1798 | { | |
1799 | OSStatus result; /* Return value */ | |
1800 | ssize_t bytes; /* Number of bytes read */ | |
1801 | http_t *http; /* HTTP connection */ | |
2c85b752 | 1802 | |
2c85b752 | 1803 | |
88f1e9c8 MS |
1804 | http = (http_t *)connection; |
1805 | ||
1806 | do | |
2c85b752 | 1807 | { |
88f1e9c8 MS |
1808 | bytes = write(http->fd, data, *dataLength); |
1809 | } | |
1810 | while (bytes == -1 && (errno == EINTR || errno == EAGAIN)); | |
1811 | ||
1812 | if ((size_t)bytes == *dataLength) | |
1813 | { | |
1814 | result = 0; | |
1815 | } | |
1816 | else if (bytes >= 0) | |
1817 | { | |
1818 | *dataLength = (size_t)bytes; | |
1819 | result = errSSLWouldBlock; | |
2c85b752 MS |
1820 | } |
1821 | else | |
88f1e9c8 MS |
1822 | { |
1823 | *dataLength = 0; | |
2c85b752 | 1824 | |
88f1e9c8 MS |
1825 | if (errno == EAGAIN) |
1826 | result = errSSLWouldBlock; | |
1827 | else | |
1828 | result = errSSLClosedAbort; | |
1829 | } | |
1830 | ||
1831 | return (result); | |
2c85b752 | 1832 | } |