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