2 * TLS support code for CUPS using GNU TLS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
11 /**** This file is included from tls.c ****/
14 * Include necessary headers...
24 static int tls_auto_create
= 0;
25 /* Auto-create self-signed certs? */
26 static char *tls_common_name
= NULL
;
27 /* Default common name */
28 static gnutls_x509_crl_t tls_crl
= NULL
;/* Certificate revocation list */
29 static char *tls_keypath
= NULL
;
30 /* Server cert keychain path */
31 static _cups_mutex_t tls_mutex
= _CUPS_MUTEX_INITIALIZER
;
32 /* Mutex for keychain/certs */
33 static int tls_options
= -1,/* Options for TLS connections */
34 tls_min_version
= _HTTP_TLS_1_0
,
35 tls_max_version
= _HTTP_TLS_MAX
;
42 static gnutls_x509_crt_t
http_gnutls_create_credential(http_credential_t
*credential
);
43 static const char *http_gnutls_default_path(char *buffer
, size_t bufsize
);
44 static void http_gnutls_load_crl(void);
45 static const char *http_gnutls_make_path(char *buffer
, size_t bufsize
, const char *dirname
, const char *filename
, const char *ext
);
46 static ssize_t
http_gnutls_read(gnutls_transport_ptr_t ptr
, void *data
, size_t length
);
47 static ssize_t
http_gnutls_write(gnutls_transport_ptr_t ptr
, const void *data
, size_t length
);
51 * 'cupsMakeServerCredentials()' - Make a self-signed certificate and private key pair.
53 * @since CUPS 2.0/OS 10.10@
56 int /* O - 1 on success, 0 on failure */
57 cupsMakeServerCredentials(
58 const char *path
, /* I - Path to keychain/directory */
59 const char *common_name
, /* I - Common name */
60 int num_alt_names
, /* I - Number of subject alternate names */
61 const char **alt_names
, /* I - Subject Alternate Names */
62 time_t expiration_date
) /* I - Expiration date */
64 gnutls_x509_crt_t crt
; /* Self-signed certificate */
65 gnutls_x509_privkey_t key
; /* Encryption private key */
66 char temp
[1024], /* Temporary directory name */
67 crtfile
[1024], /* Certificate filename */
68 keyfile
[1024]; /* Private key filename */
69 cups_lang_t
*language
; /* Default language info */
70 cups_file_t
*fp
; /* Key/cert file */
71 unsigned char buffer
[8192]; /* Buffer for x509 data */
72 size_t bytes
; /* Number of bytes of data */
73 unsigned char serial
[4]; /* Serial number buffer */
74 time_t curtime
; /* Current time */
75 int result
; /* Result of GNU TLS calls */
78 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
));
85 path
= http_gnutls_default_path(temp
, sizeof(temp
));
87 if (!path
|| !common_name
)
89 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
93 http_gnutls_make_path(crtfile
, sizeof(crtfile
), path
, common_name
, "crt");
94 http_gnutls_make_path(keyfile
, sizeof(keyfile
), path
, common_name
, "key");
97 * Create the encryption key...
100 DEBUG_puts("1cupsMakeServerCredentials: Creating key pair.");
102 gnutls_x509_privkey_init(&key
);
103 gnutls_x509_privkey_generate(key
, GNUTLS_PK_RSA
, 2048, 0);
105 DEBUG_puts("1cupsMakeServerCredentials: Key pair created.");
111 bytes
= sizeof(buffer
);
113 if ((result
= gnutls_x509_privkey_export(key
, GNUTLS_X509_FMT_PEM
, buffer
, &bytes
)) < 0)
115 DEBUG_printf(("1cupsMakeServerCredentials: Unable to export private key: %s", gnutls_strerror(result
)));
116 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, gnutls_strerror(result
), 0);
117 gnutls_x509_privkey_deinit(key
);
120 else if ((fp
= cupsFileOpen(keyfile
, "w")) != NULL
)
122 DEBUG_printf(("1cupsMakeServerCredentials: Writing private key to \"%s\".", keyfile
));
123 cupsFileWrite(fp
, (char *)buffer
, bytes
);
128 DEBUG_printf(("1cupsMakeServerCredentials: Unable to create private key file \"%s\": %s", keyfile
, strerror(errno
)));
129 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
130 gnutls_x509_privkey_deinit(key
);
135 * Create the self-signed certificate...
138 DEBUG_puts("1cupsMakeServerCredentials: Generating self-signed X.509 certificate.");
140 language
= cupsLangDefault();
141 curtime
= time(NULL
);
142 serial
[0] = curtime
>> 24;
143 serial
[1] = curtime
>> 16;
144 serial
[2] = curtime
>> 8;
147 gnutls_x509_crt_init(&crt
);
148 if (strlen(language
->language
) == 5)
149 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
150 language
->language
+ 3, 2);
152 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COUNTRY_NAME
, 0,
154 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_COMMON_NAME
, 0,
155 common_name
, strlen(common_name
));
156 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATION_NAME
, 0,
157 common_name
, strlen(common_name
));
158 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME
,
160 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME
, 0,
162 gnutls_x509_crt_set_dn_by_oid(crt
, GNUTLS_OID_X520_LOCALITY_NAME
, 0,
164 /* gnutls_x509_crt_set_dn_by_oid(crt, GNUTLS_OID_PKCS9_EMAIL, 0,
165 ServerAdmin, strlen(ServerAdmin));*/
166 gnutls_x509_crt_set_key(crt
, key
);
167 gnutls_x509_crt_set_serial(crt
, serial
, sizeof(serial
));
168 gnutls_x509_crt_set_activation_time(crt
, curtime
);
169 gnutls_x509_crt_set_expiration_time(crt
, curtime
+ 10 * 365 * 86400);
170 gnutls_x509_crt_set_ca_status(crt
, 0);
171 if (num_alt_names
> 0)
172 gnutls_x509_crt_set_subject_alternative_name(crt
, GNUTLS_SAN_DNSNAME
, alt_names
[0]);
173 gnutls_x509_crt_set_key_purpose_oid(crt
, GNUTLS_KP_TLS_WWW_SERVER
, 0);
174 gnutls_x509_crt_set_key_usage(crt
, GNUTLS_KEY_KEY_ENCIPHERMENT
);
175 gnutls_x509_crt_set_version(crt
, 3);
177 bytes
= sizeof(buffer
);
178 if (gnutls_x509_crt_get_key_id(crt
, 0, buffer
, &bytes
) >= 0)
179 gnutls_x509_crt_set_subject_key_id(crt
, buffer
, bytes
);
181 gnutls_x509_crt_sign(crt
, crt
, key
);
187 bytes
= sizeof(buffer
);
188 if ((result
= gnutls_x509_crt_export(crt
, GNUTLS_X509_FMT_PEM
, buffer
, &bytes
)) < 0)
190 DEBUG_printf(("1cupsMakeServerCredentials: Unable to export public key and X.509 certificate: %s", gnutls_strerror(result
)));
191 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, gnutls_strerror(result
), 0);
192 gnutls_x509_crt_deinit(crt
);
193 gnutls_x509_privkey_deinit(key
);
196 else if ((fp
= cupsFileOpen(crtfile
, "w")) != NULL
)
198 DEBUG_printf(("1cupsMakeServerCredentials: Writing public key and X.509 certificate to \"%s\".", crtfile
));
199 cupsFileWrite(fp
, (char *)buffer
, bytes
);
204 DEBUG_printf(("1cupsMakeServerCredentials: Unable to create public key and X.509 certificate file \"%s\": %s", crtfile
, strerror(errno
)));
205 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
206 gnutls_x509_crt_deinit(crt
);
207 gnutls_x509_privkey_deinit(key
);
215 gnutls_x509_crt_deinit(crt
);
216 gnutls_x509_privkey_deinit(key
);
218 DEBUG_puts("1cupsMakeServerCredentials: Successfully created credentials.");
225 * 'cupsSetServerCredentials()' - Set the default server credentials.
227 * Note: The server credentials are used by all threads in the running process.
228 * This function is threadsafe.
230 * @since CUPS 2.0/OS 10.10@
233 int /* O - 1 on success, 0 on failure */
234 cupsSetServerCredentials(
235 const char *path
, /* I - Path to keychain/directory */
236 const char *common_name
, /* I - Default common name for server */
237 int auto_create
) /* I - 1 = automatically create self-signed certificates */
239 char temp
[1024]; /* Default path buffer */
242 DEBUG_printf(("cupsSetServerCredentials(path=\"%s\", common_name=\"%s\", auto_create=%d)", path
, common_name
, auto_create
));
245 * Use defaults as needed...
249 path
= http_gnutls_default_path(temp
, sizeof(temp
));
252 * Range check input...
255 if (!path
|| !common_name
)
257 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
261 _cupsMutexLock(&tls_mutex
);
268 _cupsStrFree(tls_keypath
);
271 _cupsStrFree(tls_common_name
);
274 * Save the new values...
277 tls_keypath
= _cupsStrAlloc(path
);
278 tls_auto_create
= auto_create
;
279 tls_common_name
= _cupsStrAlloc(common_name
);
281 _cupsMutexUnlock(&tls_mutex
);
288 * 'httpCopyCredentials()' - Copy the credentials associated with the peer in
289 * an encrypted connection.
291 * @since CUPS 1.5/macOS 10.7@
294 int /* O - Status of call (0 = success) */
296 http_t
*http
, /* I - Connection to server */
297 cups_array_t
**credentials
) /* O - Array of credentials */
299 unsigned count
; /* Number of certificates */
300 const gnutls_datum_t
*certs
; /* Certificates */
303 DEBUG_printf(("httpCopyCredentials(http=%p, credentials=%p)", http
, credentials
));
308 if (!http
|| !http
->tls
|| !credentials
)
311 *credentials
= cupsArrayNew(NULL
, NULL
);
312 certs
= gnutls_certificate_get_peers(http
->tls
, &count
);
314 DEBUG_printf(("1httpCopyCredentials: certs=%p, count=%u", certs
, count
));
320 httpAddCredential(*credentials
, certs
->data
, certs
->size
);
331 * '_httpCreateCredentials()' - Create credentials in the internal format.
334 http_tls_credentials_t
/* O - Internal credentials */
335 _httpCreateCredentials(
336 cups_array_t
*credentials
) /* I - Array of credentials */
345 * '_httpFreeCredentials()' - Free internal credentials.
349 _httpFreeCredentials(
350 http_tls_credentials_t credentials
) /* I - Internal credentials */
357 * 'httpCredentialsAreValidForName()' - Return whether the credentials are valid for the given name.
359 * @since CUPS 2.0/OS 10.10@
362 int /* O - 1 if valid, 0 otherwise */
363 httpCredentialsAreValidForName(
364 cups_array_t
*credentials
, /* I - Credentials */
365 const char *common_name
) /* I - Name to check */
367 gnutls_x509_crt_t cert
; /* Certificate */
368 int result
= 0; /* Result */
371 cert
= http_gnutls_create_credential((http_credential_t
*)cupsArrayFirst(credentials
));
374 result
= gnutls_x509_crt_check_hostname(cert
, common_name
) != 0;
378 int i
, /* Looping var */
379 count
; /* Number of revoked certificates */
380 unsigned char cserial
[1024], /* Certificate serial number */
381 rserial
[1024]; /* Revoked serial number */
382 size_t cserial_size
, /* Size of cert serial number */
383 rserial_size
; /* Size of revoked serial number */
385 _cupsMutexLock(&tls_mutex
);
387 count
= gnutls_x509_crl_get_crt_count(tls_crl
);
391 cserial_size
= sizeof(cserial
);
392 gnutls_x509_crt_get_serial(cert
, cserial
, &cserial_size
);
394 for (i
= 0; i
< count
; i
++)
396 rserial_size
= sizeof(rserial
);
397 if (!gnutls_x509_crl_get_crt_serial(tls_crl
, (unsigned)i
, rserial
, &rserial_size
, NULL
) && cserial_size
== rserial_size
&& !memcmp(cserial
, rserial
, (int)rserial_size
))
405 _cupsMutexUnlock(&tls_mutex
);
408 gnutls_x509_crt_deinit(cert
);
416 * 'httpCredentialsGetTrust()' - Return the trust of credentials.
418 * @since CUPS 2.0/OS 10.10@
421 http_trust_t
/* O - Level of trust */
422 httpCredentialsGetTrust(
423 cups_array_t
*credentials
, /* I - Credentials */
424 const char *common_name
) /* I - Common name for trust lookup */
426 http_trust_t trust
= HTTP_TRUST_OK
;
428 gnutls_x509_crt_t cert
; /* Certificate */
429 cups_array_t
*tcreds
= NULL
; /* Trusted credentials */
430 _cups_globals_t
*cg
= _cupsGlobals();
431 /* Per-thread globals */
436 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No common name specified."), 1);
437 return (HTTP_TRUST_UNKNOWN
);
440 if ((cert
= http_gnutls_create_credential((http_credential_t
*)cupsArrayFirst(credentials
))) == NULL
)
442 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to create credentials from array."), 1);
443 return (HTTP_TRUST_UNKNOWN
);
446 if (cg
->any_root
< 0)
449 http_gnutls_load_crl();
453 * Look this common name up in the default keychains...
456 httpLoadCredentials(NULL
, &tcreds
, common_name
);
460 char credentials_str
[1024], /* String for incoming credentials */
461 tcreds_str
[1024]; /* String for saved credentials */
463 httpCredentialsString(credentials
, credentials_str
, sizeof(credentials_str
));
464 httpCredentialsString(tcreds
, tcreds_str
, sizeof(tcreds_str
));
466 if (strcmp(credentials_str
, tcreds_str
))
469 * Credentials don't match, let's look at the expiration date of the new
470 * credentials and allow if the new ones have a later expiration...
473 if (!cg
->trust_first
)
476 * Do not trust certificates on first use...
479 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Trust on first use is disabled."), 1);
481 trust
= HTTP_TRUST_INVALID
;
483 else if (httpCredentialsGetExpiration(credentials
) <= httpCredentialsGetExpiration(tcreds
))
486 * The new credentials are not newly issued...
489 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("New credentials are older than stored credentials."), 1);
491 trust
= HTTP_TRUST_INVALID
;
493 else if (!httpCredentialsAreValidForName(credentials
, common_name
))
496 * The common name does not match the issued certificate...
499 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("New credentials are not valid for name."), 1);
501 trust
= HTTP_TRUST_INVALID
;
503 else if (httpCredentialsGetExpiration(tcreds
) < time(NULL
))
506 * Save the renewed credentials...
509 trust
= HTTP_TRUST_RENEWED
;
511 httpSaveCredentials(NULL
, credentials
, common_name
);
515 httpFreeCredentials(tcreds
);
517 else if (cg
->validate_certs
&& !httpCredentialsAreValidForName(credentials
, common_name
))
519 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No stored credentials, not valid for name."), 1);
520 trust
= HTTP_TRUST_INVALID
;
522 else if (!cg
->trust_first
)
525 * See if we have a site CA certificate we can compare...
528 if (!httpLoadCredentials(NULL
, &tcreds
, "site"))
530 if (cupsArrayCount(credentials
) != (cupsArrayCount(tcreds
) + 1))
533 * Certificate isn't directly generated from the CA cert...
536 trust
= HTTP_TRUST_INVALID
;
541 * Do a tail comparison of the two certificates...
544 http_credential_t
*a
, *b
; /* Certificates */
546 for (a
= (http_credential_t
*)cupsArrayFirst(tcreds
), b
= (http_credential_t
*)cupsArrayIndex(credentials
, 1);
548 a
= (http_credential_t
*)cupsArrayNext(tcreds
), b
= (http_credential_t
*)cupsArrayNext(credentials
))
549 if (a
->datalen
!= b
->datalen
|| memcmp(a
->data
, b
->data
, a
->datalen
))
553 trust
= HTTP_TRUST_INVALID
;
556 if (trust
!= HTTP_TRUST_OK
)
557 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Credentials do not validate against site CA certificate."), 1);
561 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Trust on first use is disabled."), 1);
562 trust
= HTTP_TRUST_INVALID
;
566 if (trust
== HTTP_TRUST_OK
&& !cg
->expired_certs
)
568 time_t curtime
; /* Current date/time */
571 if (curtime
< gnutls_x509_crt_get_activation_time(cert
) ||
572 curtime
> gnutls_x509_crt_get_expiration_time(cert
))
574 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Credentials have expired."), 1);
575 trust
= HTTP_TRUST_EXPIRED
;
579 if (trust
== HTTP_TRUST_OK
&& !cg
->any_root
&& cupsArrayCount(credentials
) == 1)
581 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Self-signed credentials are blocked."), 1);
582 trust
= HTTP_TRUST_INVALID
;
585 gnutls_x509_crt_deinit(cert
);
592 * 'httpCredentialsGetExpiration()' - Return the expiration date of the credentials.
594 * @since CUPS 2.0/OS 10.10@
597 time_t /* O - Expiration date of credentials */
598 httpCredentialsGetExpiration(
599 cups_array_t
*credentials
) /* I - Credentials */
601 gnutls_x509_crt_t cert
; /* Certificate */
602 time_t result
= 0; /* Result */
605 cert
= http_gnutls_create_credential((http_credential_t
*)cupsArrayFirst(credentials
));
608 result
= gnutls_x509_crt_get_expiration_time(cert
);
609 gnutls_x509_crt_deinit(cert
);
617 * 'httpCredentialsString()' - Return a string representing the credentials.
619 * @since CUPS 2.0/OS 10.10@
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 */
628 http_credential_t
*first
; /* First certificate */
629 gnutls_x509_crt_t cert
; /* Certificate */
632 DEBUG_printf(("httpCredentialsString(credentials=%p, buffer=%p, bufsize=" CUPS_LLFMT
")", credentials
, buffer
, CUPS_LLCAST bufsize
));
637 if (buffer
&& bufsize
> 0)
640 if ((first
= (http_credential_t
*)cupsArrayFirst(credentials
)) != NULL
&&
641 (cert
= http_gnutls_create_credential(first
)) != NULL
)
643 char name
[256], /* Common name associated with cert */
644 issuer
[256]; /* Issuer associated with cert */
645 size_t len
; /* Length of string */
646 time_t expiration
; /* Expiration date of cert */
647 int sigalg
; /* Signature algorithm */
648 unsigned char md5_digest
[16]; /* MD5 result */
650 len
= sizeof(name
) - 1;
651 if (gnutls_x509_crt_get_dn_by_oid(cert
, GNUTLS_OID_X520_COMMON_NAME
, 0, 0, name
, &len
) >= 0)
654 strlcpy(name
, "unknown", sizeof(name
));
656 len
= sizeof(issuer
) - 1;
657 if (gnutls_x509_crt_get_issuer_dn_by_oid(cert
, GNUTLS_OID_X520_ORGANIZATION_NAME
, 0, 0, issuer
, &len
) >= 0)
660 strlcpy(issuer
, "unknown", sizeof(issuer
));
662 expiration
= gnutls_x509_crt_get_expiration_time(cert
);
663 sigalg
= gnutls_x509_crt_get_signature_algorithm(cert
);
665 cupsHashData("md5", first
->data
, first
->datalen
, md5_digest
, sizeof(md5_digest
));
667 snprintf(buffer
, bufsize
, "%s (issued by %s) / %s / %s / %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", name
, issuer
, httpGetDateString(expiration
), gnutls_sign_get_name(sigalg
), md5_digest
[0], md5_digest
[1], md5_digest
[2], md5_digest
[3], md5_digest
[4], md5_digest
[5], md5_digest
[6], md5_digest
[7], md5_digest
[8], md5_digest
[9], md5_digest
[10], md5_digest
[11], md5_digest
[12], md5_digest
[13], md5_digest
[14], md5_digest
[15]);
669 gnutls_x509_crt_deinit(cert
);
672 DEBUG_printf(("1httpCredentialsString: Returning \"%s\".", buffer
));
674 return (strlen(buffer
));
679 * 'httpLoadCredentials()' - Load X.509 credentials from a keychain file.
681 * @since CUPS 2.0/OS 10.10@
684 int /* O - 0 on success, -1 on error */
686 const char *path
, /* I - Keychain/PKCS#12 path */
687 cups_array_t
**credentials
, /* IO - Credentials */
688 const char *common_name
) /* I - Common name for credentials */
690 cups_file_t
*fp
; /* Certificate file */
691 char filename
[1024], /* filename.crt */
692 temp
[1024], /* Temporary string */
693 line
[256]; /* Base64-encoded line */
694 unsigned char *data
= NULL
; /* Buffer for cert data */
695 size_t alloc_data
= 0, /* Bytes allocated */
696 num_data
= 0; /* Bytes used */
697 int decoded
; /* Bytes decoded */
698 int in_certificate
= 0;
699 /* In a certificate? */
702 if (!credentials
|| !common_name
)
706 path
= http_gnutls_default_path(temp
, sizeof(temp
));
710 http_gnutls_make_path(filename
, sizeof(filename
), path
, common_name
, "crt");
712 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
715 while (cupsFileGets(fp
, line
, sizeof(line
)))
717 if (!strcmp(line
, "-----BEGIN CERTIFICATE-----"))
722 * Missing END CERTIFICATE...
725 httpFreeCredentials(*credentials
);
732 else if (!strcmp(line
, "-----END CERTIFICATE-----"))
734 if (!in_certificate
|| !num_data
)
740 httpFreeCredentials(*credentials
);
746 *credentials
= cupsArrayNew(NULL
, NULL
);
748 if (httpAddCredential(*credentials
, data
, num_data
))
750 httpFreeCredentials(*credentials
);
758 else if (in_certificate
)
768 else if ((num_data
+ strlen(line
)) >= alloc_data
)
770 unsigned char *tdata
= realloc(data
, alloc_data
+ 1024);
771 /* Expanded buffer */
775 httpFreeCredentials(*credentials
);
784 decoded
= alloc_data
- num_data
;
785 httpDecode64_2((char *)data
+ num_data
, &decoded
, line
);
786 num_data
+= (size_t)decoded
;
795 * Missing END CERTIFICATE...
798 httpFreeCredentials(*credentials
);
805 return (*credentials
? 0 : -1);
810 * 'httpSaveCredentials()' - Save X.509 credentials to a keychain file.
812 * @since CUPS 2.0/OS 10.10@
815 int /* O - -1 on error, 0 on success */
817 const char *path
, /* I - Keychain/PKCS#12 path */
818 cups_array_t
*credentials
, /* I - Credentials */
819 const char *common_name
) /* I - Common name for credentials */
821 cups_file_t
*fp
; /* Certificate file */
822 char filename
[1024], /* filename.crt */
823 nfilename
[1024],/* filename.crt.N */
824 temp
[1024], /* Temporary string */
825 line
[256]; /* Base64-encoded line */
826 const unsigned char *ptr
; /* Pointer into certificate */
827 ssize_t remaining
; /* Bytes left */
828 http_credential_t
*cred
; /* Current credential */
831 if (!credentials
|| !common_name
)
835 path
= http_gnutls_default_path(temp
, sizeof(temp
));
839 http_gnutls_make_path(filename
, sizeof(filename
), path
, common_name
, "crt");
840 snprintf(nfilename
, sizeof(nfilename
), "%s.N", filename
);
842 if ((fp
= cupsFileOpen(nfilename
, "w")) == NULL
)
845 fchmod(cupsFileNumber(fp
), 0600);
847 for (cred
= (http_credential_t
*)cupsArrayFirst(credentials
);
849 cred
= (http_credential_t
*)cupsArrayNext(credentials
))
851 cupsFilePuts(fp
, "-----BEGIN CERTIFICATE-----\n");
852 for (ptr
= cred
->data
, remaining
= (ssize_t
)cred
->datalen
; remaining
> 0; remaining
-= 45, ptr
+= 45)
854 httpEncode64_2(line
, sizeof(line
), (char *)ptr
, remaining
> 45 ? 45 : remaining
);
855 cupsFilePrintf(fp
, "%s\n", line
);
857 cupsFilePuts(fp
, "-----END CERTIFICATE-----\n");
862 return (rename(nfilename
, filename
));
867 * 'http_gnutls_create_credential()' - Create a single credential in the internal format.
870 static gnutls_x509_crt_t
/* O - Certificate */
871 http_gnutls_create_credential(
872 http_credential_t
*credential
) /* I - Credential */
874 int result
; /* Result from GNU TLS */
875 gnutls_x509_crt_t cert
; /* Certificate */
876 gnutls_datum_t datum
; /* Data record */
879 DEBUG_printf(("3http_gnutls_create_credential(credential=%p)", credential
));
884 if ((result
= gnutls_x509_crt_init(&cert
)) < 0)
886 DEBUG_printf(("4http_gnutls_create_credential: init error: %s", gnutls_strerror(result
)));
890 datum
.data
= credential
->data
;
891 datum
.size
= credential
->datalen
;
893 if ((result
= gnutls_x509_crt_import(cert
, &datum
, GNUTLS_X509_FMT_DER
)) < 0)
895 DEBUG_printf(("4http_gnutls_create_credential: import error: %s", gnutls_strerror(result
)));
897 gnutls_x509_crt_deinit(cert
);
906 * 'http_gnutls_default_path()' - Get the default credential store path.
909 static const char * /* O - Path or NULL on error */
910 http_gnutls_default_path(char *buffer
,/* I - Path buffer */
911 size_t bufsize
)/* I - Size of path buffer */
913 const char *home
= getenv("HOME"); /* HOME environment variable */
916 if (getuid() && home
)
918 snprintf(buffer
, bufsize
, "%s/.cups", home
);
919 if (access(buffer
, 0))
921 DEBUG_printf(("1http_gnutls_default_path: Making directory \"%s\".", buffer
));
922 if (mkdir(buffer
, 0700))
924 DEBUG_printf(("1http_gnutls_default_path: Failed to make directory: %s", strerror(errno
)));
929 snprintf(buffer
, bufsize
, "%s/.cups/ssl", home
);
930 if (access(buffer
, 0))
932 DEBUG_printf(("1http_gnutls_default_path: Making directory \"%s\".", buffer
));
933 if (mkdir(buffer
, 0700))
935 DEBUG_printf(("1http_gnutls_default_path: Failed to make directory: %s", strerror(errno
)));
941 strlcpy(buffer
, CUPS_SERVERROOT
"/ssl", bufsize
);
943 DEBUG_printf(("1http_gnutls_default_path: Using default path \"%s\".", buffer
));
950 * 'http_gnutls_load_crl()' - Load the certificate revocation list, if any.
954 http_gnutls_load_crl(void)
956 _cupsMutexLock(&tls_mutex
);
958 if (!gnutls_x509_crl_init(&tls_crl
))
960 cups_file_t
*fp
; /* CRL file */
961 char filename
[1024], /* site.crl */
962 line
[256]; /* Base64-encoded line */
963 unsigned char *data
= NULL
; /* Buffer for cert data */
964 size_t alloc_data
= 0, /* Bytes allocated */
965 num_data
= 0; /* Bytes used */
966 int decoded
; /* Bytes decoded */
967 gnutls_datum_t datum
; /* Data record */
970 http_gnutls_make_path(filename
, sizeof(filename
), CUPS_SERVERROOT
, "site", "crl");
972 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
974 while (cupsFileGets(fp
, line
, sizeof(line
)))
976 if (!strcmp(line
, "-----BEGIN X509 CRL-----"))
981 * Missing END X509 CRL...
987 else if (!strcmp(line
, "-----END X509 CRL-----"))
999 datum
.size
= num_data
;
1001 gnutls_x509_crl_import(tls_crl
, &datum
, GNUTLS_X509_FMT_PEM
);
1007 if (alloc_data
== 0)
1009 data
= malloc(2048);
1015 else if ((num_data
+ strlen(line
)) >= alloc_data
)
1017 unsigned char *tdata
= realloc(data
, alloc_data
+ 1024);
1018 /* Expanded buffer */
1027 decoded
= alloc_data
- num_data
;
1028 httpDecode64_2((char *)data
+ num_data
, &decoded
, line
);
1029 num_data
+= (size_t)decoded
;
1040 _cupsMutexUnlock(&tls_mutex
);
1045 * 'http_gnutls_make_path()' - Format a filename for a certificate or key file.
1048 static const char * /* O - Filename */
1049 http_gnutls_make_path(
1050 char *buffer
, /* I - Filename buffer */
1051 size_t bufsize
, /* I - Size of buffer */
1052 const char *dirname
, /* I - Directory */
1053 const char *filename
, /* I - Filename (usually hostname) */
1054 const char *ext
) /* I - Extension */
1056 char *bufptr
, /* Pointer into buffer */
1057 *bufend
= buffer
+ bufsize
- 1; /* End of buffer */
1060 snprintf(buffer
, bufsize
, "%s/", dirname
);
1061 bufptr
= buffer
+ strlen(buffer
);
1063 while (*filename
&& bufptr
< bufend
)
1065 if (_cups_isalnum(*filename
) || *filename
== '-' || *filename
== '.')
1066 *bufptr
++ = *filename
;
1073 if (bufptr
< bufend
)
1076 strlcpy(bufptr
, ext
, (size_t)(bufend
- bufptr
+ 1));
1083 * 'http_gnutls_read()' - Read function for the GNU TLS library.
1086 static ssize_t
/* O - Number of bytes read or -1 on error */
1088 gnutls_transport_ptr_t ptr
, /* I - Connection to server */
1089 void *data
, /* I - Buffer */
1090 size_t length
) /* I - Number of bytes to read */
1092 http_t
*http
; /* HTTP connection */
1093 ssize_t bytes
; /* Bytes read */
1096 DEBUG_printf(("6http_gnutls_read(ptr=%p, data=%p, length=%d)", ptr
, data
, (int)length
));
1098 http
= (http_t
*)ptr
;
1100 if (!http
->blocking
|| http
->timeout_value
> 0.0)
1103 * Make sure we have data before we read...
1106 while (!_httpWait(http
, http
->wait_value
, 0))
1108 if (http
->timeout_cb
&& (*http
->timeout_cb
)(http
, http
->timeout_data
))
1111 http
->error
= ETIMEDOUT
;
1116 bytes
= recv(http
->fd
, data
, length
, 0);
1117 DEBUG_printf(("6http_gnutls_read: bytes=%d", (int)bytes
));
1123 * 'http_gnutls_write()' - Write function for the GNU TLS library.
1126 static ssize_t
/* O - Number of bytes written or -1 on error */
1128 gnutls_transport_ptr_t ptr
, /* I - Connection to server */
1129 const void *data
, /* I - Data buffer */
1130 size_t length
) /* I - Number of bytes to write */
1132 ssize_t bytes
; /* Bytes written */
1135 DEBUG_printf(("6http_gnutls_write(ptr=%p, data=%p, length=%d)", ptr
, data
,
1137 bytes
= send(((http_t
*)ptr
)->fd
, data
, length
, 0);
1138 DEBUG_printf(("http_gnutls_write: bytes=%d", (int)bytes
));
1145 * '_httpTLSInitialize()' - Initialize the TLS stack.
1149 _httpTLSInitialize(void)
1152 * Initialize GNU TLS...
1155 gnutls_global_init();
1160 * '_httpTLSPending()' - Return the number of pending TLS-encrypted bytes.
1163 size_t /* O - Bytes available */
1164 _httpTLSPending(http_t
*http
) /* I - HTTP connection */
1166 return (gnutls_record_check_pending(http
->tls
));
1171 * '_httpTLSRead()' - Read from a SSL/TLS connection.
1174 int /* O - Bytes read */
1175 _httpTLSRead(http_t
*http
, /* I - Connection to server */
1176 char *buf
, /* I - Buffer to store data */
1177 int len
) /* I - Length of buffer */
1179 ssize_t result
; /* Return value */
1182 result
= gnutls_record_recv(http
->tls
, buf
, (size_t)len
);
1184 if (result
< 0 && !errno
)
1187 * Convert GNU TLS error to errno value...
1192 case GNUTLS_E_INTERRUPTED
:
1196 case GNUTLS_E_AGAIN
:
1208 return ((int)result
);
1213 * '_httpTLSSetCredentials()' - Set the TLS credentials.
1216 int /* O - Status of connection */
1217 _httpTLSSetCredentials(http_t
*http
) /* I - Connection to server */
1226 * '_httpTLSSetOptions()' - Set TLS protocol and cipher suite options.
1230 _httpTLSSetOptions(int options
, /* I - Options */
1231 int min_version
, /* I - Minimum TLS version */
1232 int max_version
) /* I - Maximum TLS version */
1234 if (!(options
& _HTTP_TLS_SET_DEFAULT
) || tls_options
< 0)
1236 tls_options
= options
;
1237 tls_min_version
= min_version
;
1238 tls_max_version
= max_version
;
1244 * '_httpTLSStart()' - Set up SSL/TLS support on a connection.
1247 int /* O - 0 on success, -1 on failure */
1248 _httpTLSStart(http_t
*http
) /* I - Connection to server */
1250 char hostname
[256], /* Hostname */
1251 *hostptr
; /* Pointer into hostname */
1252 int status
; /* Status of handshake */
1253 gnutls_certificate_credentials_t
*credentials
;
1254 /* TLS credentials */
1255 char priority_string
[2048];
1256 /* Priority string */
1257 int version
; /* Current version */
1258 double old_timeout
; /* Old timeout value */
1259 http_timeout_cb_t old_cb
; /* Old timeout callback */
1260 void *old_data
; /* Old timeout data */
1261 static const char * const versions
[] =/* SSL/TLS versions */
1272 DEBUG_printf(("3_httpTLSStart(http=%p)", http
));
1274 if (tls_options
< 0)
1276 DEBUG_puts("4_httpTLSStart: Setting defaults.");
1278 DEBUG_printf(("4_httpTLSStart: tls_options=%x", tls_options
));
1281 if (http
->mode
== _HTTP_MODE_SERVER
&& !tls_keypath
)
1283 DEBUG_puts("4_httpTLSStart: cupsSetServerCredentials not called.");
1284 http
->error
= errno
= EINVAL
;
1285 http
->status
= HTTP_STATUS_ERROR
;
1286 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Server credentials not set."), 1);
1291 credentials
= (gnutls_certificate_credentials_t
*)
1292 malloc(sizeof(gnutls_certificate_credentials_t
));
1293 if (credentials
== NULL
)
1295 DEBUG_printf(("8_httpStartTLS: Unable to allocate credentials: %s",
1297 http
->error
= errno
;
1298 http
->status
= HTTP_STATUS_ERROR
;
1299 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
1304 gnutls_certificate_allocate_credentials(credentials
);
1305 status
= gnutls_init(&http
->tls
, http
->mode
== _HTTP_MODE_CLIENT
? GNUTLS_CLIENT
: GNUTLS_SERVER
);
1307 status
= gnutls_set_default_priority(http
->tls
);
1312 http
->status
= HTTP_STATUS_ERROR
;
1314 DEBUG_printf(("4_httpTLSStart: Unable to initialize common TLS parameters: %s", gnutls_strerror(status
)));
1315 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, gnutls_strerror(status
), 0);
1317 gnutls_deinit(http
->tls
);
1318 gnutls_certificate_free_credentials(*credentials
);
1325 if (http
->mode
== _HTTP_MODE_CLIENT
)
1328 * Client: get the hostname to use for TLS...
1331 if (httpAddrLocalhost(http
->hostaddr
))
1333 strlcpy(hostname
, "localhost", sizeof(hostname
));
1338 * Otherwise make sure the hostname we have does not end in a trailing dot.
1341 strlcpy(hostname
, http
->hostname
, sizeof(hostname
));
1342 if ((hostptr
= hostname
+ strlen(hostname
) - 1) >= hostname
&&
1347 status
= gnutls_server_name_set(http
->tls
, GNUTLS_NAME_DNS
, hostname
, strlen(hostname
));
1352 * Server: get certificate and private key...
1355 char crtfile
[1024], /* Certificate file */
1356 keyfile
[1024]; /* Private key file */
1357 int have_creds
= 0; /* Have credentials? */
1359 if (http
->fields
[HTTP_FIELD_HOST
])
1362 * Use hostname for TLS upgrade...
1365 strlcpy(hostname
, http
->fields
[HTTP_FIELD_HOST
], sizeof(hostname
));
1370 * Resolve hostname from connection address...
1373 http_addr_t addr
; /* Connection address */
1374 socklen_t addrlen
; /* Length of address */
1376 addrlen
= sizeof(addr
);
1377 if (getsockname(http
->fd
, (struct sockaddr
*)&addr
, &addrlen
))
1379 DEBUG_printf(("4_httpTLSStart: Unable to get socket address: %s", strerror(errno
)));
1382 else if (httpAddrLocalhost(&addr
))
1386 httpAddrLookup(&addr
, hostname
, sizeof(hostname
));
1387 DEBUG_printf(("4_httpTLSStart: Resolved socket address to \"%s\".", hostname
));
1391 if (isdigit(hostname
[0] & 255) || hostname
[0] == '[')
1392 hostname
[0] = '\0'; /* Don't allow numeric addresses */
1397 * First look in the CUPS keystore...
1400 http_gnutls_make_path(crtfile
, sizeof(crtfile
), tls_keypath
, hostname
, "crt");
1401 http_gnutls_make_path(keyfile
, sizeof(keyfile
), tls_keypath
, hostname
, "key");
1403 if (access(crtfile
, R_OK
) || access(keyfile
, R_OK
))
1406 * No CUPS-managed certs, look for CA certs...
1409 char cacrtfile
[1024], cakeyfile
[1024]; /* CA cert files */
1411 snprintf(cacrtfile
, sizeof(cacrtfile
), "/etc/letsencrypt/live/%s/fullchain.pem", hostname
);
1412 snprintf(cakeyfile
, sizeof(cakeyfile
), "/etc/letsencrypt/live/%s/privkey.pem", hostname
);
1414 if ((access(cacrtfile
, R_OK
) || access(cakeyfile
, R_OK
)) && (hostptr
= strchr(hostname
, '.')) != NULL
)
1417 * Try just domain name...
1421 if (strchr(hostptr
, '.'))
1423 snprintf(cacrtfile
, sizeof(cacrtfile
), "/etc/letsencrypt/live/%s/fullchain.pem", hostptr
);
1424 snprintf(cakeyfile
, sizeof(cakeyfile
), "/etc/letsencrypt/live/%s/privkey.pem", hostptr
);
1428 if (!access(cacrtfile
, R_OK
) && !access(cakeyfile
, R_OK
))
1431 * Use the CA certs...
1434 strlcpy(crtfile
, cacrtfile
, sizeof(crtfile
));
1435 strlcpy(keyfile
, cakeyfile
, sizeof(keyfile
));
1439 have_creds
= !access(crtfile
, R_OK
) && !access(keyfile
, R_OK
);
1441 else if (tls_common_name
)
1444 * First look in the CUPS keystore...
1447 http_gnutls_make_path(crtfile
, sizeof(crtfile
), tls_keypath
, tls_common_name
, "crt");
1448 http_gnutls_make_path(keyfile
, sizeof(keyfile
), tls_keypath
, tls_common_name
, "key");
1450 if (access(crtfile
, R_OK
) || access(keyfile
, R_OK
))
1453 * No CUPS-managed certs, look for CA certs...
1456 char cacrtfile
[1024], cakeyfile
[1024]; /* CA cert files */
1458 snprintf(cacrtfile
, sizeof(cacrtfile
), "/etc/letsencrypt/live/%s/fullchain.pem", tls_common_name
);
1459 snprintf(cakeyfile
, sizeof(cakeyfile
), "/etc/letsencrypt/live/%s/privkey.pem", tls_common_name
);
1461 if ((access(cacrtfile
, R_OK
) || access(cakeyfile
, R_OK
)) && (hostptr
= strchr(tls_common_name
, '.')) != NULL
)
1464 * Try just domain name...
1468 if (strchr(hostptr
, '.'))
1470 snprintf(cacrtfile
, sizeof(cacrtfile
), "/etc/letsencrypt/live/%s/fullchain.pem", hostptr
);
1471 snprintf(cakeyfile
, sizeof(cakeyfile
), "/etc/letsencrypt/live/%s/privkey.pem", hostptr
);
1475 if (!access(cacrtfile
, R_OK
) && !access(cakeyfile
, R_OK
))
1478 * Use the CA certs...
1481 strlcpy(crtfile
, cacrtfile
, sizeof(crtfile
));
1482 strlcpy(keyfile
, cakeyfile
, sizeof(keyfile
));
1486 have_creds
= !access(crtfile
, R_OK
) && !access(keyfile
, R_OK
);
1489 if (!have_creds
&& tls_auto_create
&& (hostname
[0] || tls_common_name
))
1491 DEBUG_printf(("4_httpTLSStart: Auto-create credentials for \"%s\".", hostname
[0] ? hostname
: tls_common_name
));
1493 if (!cupsMakeServerCredentials(tls_keypath
, hostname
[0] ? hostname
: tls_common_name
, 0, NULL
, time(NULL
) + 365 * 86400))
1495 DEBUG_puts("4_httpTLSStart: cupsMakeServerCredentials failed.");
1496 http
->error
= errno
= EINVAL
;
1497 http
->status
= HTTP_STATUS_ERROR
;
1498 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to create server credentials."), 1);
1504 DEBUG_printf(("4_httpTLSStart: Using certificate \"%s\" and private key \"%s\".", crtfile
, keyfile
));
1507 status
= gnutls_certificate_set_x509_key_file(*credentials
, crtfile
, keyfile
, GNUTLS_X509_FMT_PEM
);
1511 status
= gnutls_credentials_set(http
->tls
, GNUTLS_CRD_CERTIFICATE
, *credentials
);
1516 http
->status
= HTTP_STATUS_ERROR
;
1518 DEBUG_printf(("4_httpTLSStart: Unable to complete client/server setup: %s", gnutls_strerror(status
)));
1519 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, gnutls_strerror(status
), 0);
1521 gnutls_deinit(http
->tls
);
1522 gnutls_certificate_free_credentials(*credentials
);
1529 strlcpy(priority_string
, "NORMAL", sizeof(priority_string
));
1531 if (tls_max_version
< _HTTP_TLS_MAX
)
1534 * Require specific TLS versions...
1537 strlcat(priority_string
, ":-VERS-TLS-ALL", sizeof(priority_string
));
1538 for (version
= tls_min_version
; version
<= tls_max_version
; version
++)
1540 strlcat(priority_string
, ":+", sizeof(priority_string
));
1541 strlcat(priority_string
, versions
[version
], sizeof(priority_string
));
1544 else if (tls_min_version
== _HTTP_TLS_SSL3
)
1547 * Allow all versions of TLS and SSL/3.0...
1550 strlcat(priority_string
, ":+VERS-TLS-ALL:+VERS-SSL3.0", sizeof(priority_string
));
1555 * Require a minimum version...
1558 strlcat(priority_string
, ":+VERS-TLS-ALL", sizeof(priority_string
));
1559 for (version
= 0; version
< tls_min_version
; version
++)
1561 strlcat(priority_string
, ":-", sizeof(priority_string
));
1562 strlcat(priority_string
, versions
[version
], sizeof(priority_string
));
1566 if (tls_options
& _HTTP_TLS_ALLOW_RC4
)
1567 strlcat(priority_string
, ":+ARCFOUR-128", sizeof(priority_string
));
1569 strlcat(priority_string
, ":!ARCFOUR-128", sizeof(priority_string
));
1571 strlcat(priority_string
, ":!ANON-DH", sizeof(priority_string
));
1573 if (tls_options
& _HTTP_TLS_DENY_CBC
)
1574 strlcat(priority_string
, ":!AES-128-CBC:!AES-256-CBC:!CAMELLIA-128-CBC:!CAMELLIA-256-CBC:!3DES-CBC", sizeof(priority_string
));
1576 #ifdef HAVE_GNUTLS_PRIORITY_SET_DIRECT
1577 gnutls_priority_set_direct(http
->tls
, priority_string
, NULL
);
1580 gnutls_priority_t priority
; /* Priority */
1582 gnutls_priority_init(&priority
, priority_string
, NULL
);
1583 gnutls_priority_set(http
->tls
, priority
);
1584 gnutls_priority_deinit(priority
);
1585 #endif /* HAVE_GNUTLS_PRIORITY_SET_DIRECT */
1587 gnutls_transport_set_ptr(http
->tls
, (gnutls_transport_ptr_t
)http
);
1588 gnutls_transport_set_pull_function(http
->tls
, http_gnutls_read
);
1589 #ifdef HAVE_GNUTLS_TRANSPORT_SET_PULL_TIMEOUT_FUNCTION
1590 gnutls_transport_set_pull_timeout_function(http
->tls
, (gnutls_pull_timeout_func
)httpWait
);
1591 #endif /* HAVE_GNUTLS_TRANSPORT_SET_PULL_TIMEOUT_FUNCTION */
1592 gnutls_transport_set_push_function(http
->tls
, http_gnutls_write
);
1595 * Enforce a minimum timeout of 10 seconds for the TLS handshake...
1598 old_timeout
= http
->timeout_value
;
1599 old_cb
= http
->timeout_cb
;
1600 old_data
= http
->timeout_data
;
1602 if (!old_cb
|| old_timeout
< 10.0)
1604 DEBUG_puts("4_httpTLSStart: Setting timeout to 10 seconds.");
1605 httpSetTimeout(http
, 10.0, NULL
, NULL
);
1609 * Do the TLS handshake...
1612 while ((status
= gnutls_handshake(http
->tls
)) != GNUTLS_E_SUCCESS
)
1614 DEBUG_printf(("5_httpStartTLS: gnutls_handshake returned %d (%s)",
1615 status
, gnutls_strerror(status
)));
1617 if (gnutls_error_is_fatal(status
))
1620 http
->status
= HTTP_STATUS_ERROR
;
1622 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, gnutls_strerror(status
), 0);
1624 gnutls_deinit(http
->tls
);
1625 gnutls_certificate_free_credentials(*credentials
);
1629 httpSetTimeout(http
, old_timeout
, old_cb
, old_data
);
1636 * Restore the previous timeout settings...
1639 httpSetTimeout(http
, old_timeout
, old_cb
, old_data
);
1641 http
->tls_credentials
= credentials
;
1648 * '_httpTLSStop()' - Shut down SSL/TLS on a connection.
1652 _httpTLSStop(http_t
*http
) /* I - Connection to server */
1654 int error
; /* Error code */
1657 error
= gnutls_bye(http
->tls
, http
->mode
== _HTTP_MODE_CLIENT
? GNUTLS_SHUT_RDWR
: GNUTLS_SHUT_WR
);
1658 if (error
!= GNUTLS_E_SUCCESS
)
1659 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, gnutls_strerror(errno
), 0);
1661 gnutls_deinit(http
->tls
);
1664 if (http
->tls_credentials
)
1666 gnutls_certificate_free_credentials(*(http
->tls_credentials
));
1667 free(http
->tls_credentials
);
1668 http
->tls_credentials
= NULL
;
1674 * '_httpTLSWrite()' - Write to a SSL/TLS connection.
1677 int /* O - Bytes written */
1678 _httpTLSWrite(http_t
*http
, /* I - Connection to server */
1679 const char *buf
, /* I - Buffer holding data */
1680 int len
) /* I - Length of buffer */
1682 ssize_t result
; /* Return value */
1685 DEBUG_printf(("2http_write_ssl(http=%p, buf=%p, len=%d)", http
, buf
, len
));
1687 result
= gnutls_record_send(http
->tls
, buf
, (size_t)len
);
1689 if (result
< 0 && !errno
)
1692 * Convert GNU TLS error to errno value...
1697 case GNUTLS_E_INTERRUPTED
:
1701 case GNUTLS_E_AGAIN
:
1713 DEBUG_printf(("3http_write_ssl: Returning %d.", (int)result
));
1715 return ((int)result
);