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