Coverage Report

Created: 2026-04-12 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cups/cups/tls-openssl.c
Line
Count
Source
1
//
2
// TLS support code for CUPS using OpenSSL/LibreSSL.
3
//
4
// Note: This file is included from tls.c
5
//
6
// Copyright © 2020-2026 by OpenPrinting
7
// Copyright © 2007-2019 by Apple Inc.
8
// Copyright © 1997-2007 by Easy Software Products, all rights reserved.
9
//
10
// Licensed under Apache License v2.0.  See the file "LICENSE" for more
11
// information.
12
//
13
14
#include <openssl/x509v3.h>
15
#include <openssl/evp.h>
16
#include <openssl/objects.h>
17
#include <openssl/obj_mac.h>
18
19
20
//
21
// Local functions...
22
//
23
24
static long   http_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
25
static int    http_bio_free(BIO *data);
26
static int    http_bio_new(BIO *h);
27
static int    http_bio_puts(BIO *h, const char *str);
28
static int    http_bio_read(BIO *h, char *buf, int size);
29
static int    http_bio_write(BIO *h, const char *buf, int num);
30
31
static bool   openssl_add_ext(STACK_OF(X509_EXTENSION) *exts, int nid, const char *value);
32
static X509_NAME  *openssl_create_name(const char *organization, const char *org_unit, const char *locality, const char *state_province, const char *country, const char *common_name, const char *email);
33
static EVP_PKEY   *openssl_create_key(cups_credtype_t type);
34
static X509_EXTENSION *openssl_create_san(const char *common_name, size_t num_alt_names, const char * const *alt_names);
35
static time_t   openssl_get_date(X509 *cert, int which);
36
//static void   openssl_load_crl(void);
37
static STACK_OF(X509 *) openssl_load_x509(const char *credentials);
38
39
40
//
41
// Local globals...
42
//
43
44
static BIO_METHOD *tls_bio_method = NULL;
45
          // OpenSSL BIO method
46
static const char * const tls_purpose_oids[] =
47
{         // OIDs for each key purpose value
48
  "1.3.6.1.5.5.7.3.1",      // serverAuth
49
  "1.3.6.1.5.5.7.3.2",      // clientAuth
50
  "1.3.6.1.5.5.7.3.3",      // codeSigning
51
  "1.3.6.1.5.5.7.3.4",      // emailProtection
52
  "1.3.6.1.5.5.7.3.8",      // timeStamping
53
  "1.3.6.1.5.5.7.3.9"     // OCSPSigning
54
};
55
static const char * const tls_usage_strings[] =
56
{         // Strings for each key usage value
57
  "digitalSignature",
58
  "nonRepudiation",
59
  "keyEncipherment",
60
  "dataEncipherment",
61
  "keyAgreement",
62
  "keyCertSign",
63
  "cRLSign",
64
  "encipherOnly",
65
  "decipherOnly"
66
};
67
68
69
//
70
// 'cupsAreCredentialsValidForName()' - Return whether the credentials are valid
71
//                                      for the given name.
72
//
73
74
bool          // O - `true` if valid, `false` otherwise
75
cupsAreCredentialsValidForName(
76
    const char *common_name,    // I - Name to check
77
    const char *credentials)    // I - Credentials
78
0
{
79
0
  STACK_OF(X509)  *certs;   // Certificate chain
80
0
  bool      result = false;  // Result
81
82
83
0
  DEBUG_printf("cupsAreCredentialsValidForName(common_name=\"%s\", credentials=\"%s\")", common_name, credentials);
84
85
  // Range check input...
86
0
  if (!common_name || !credentials)
87
0
    return (false);
88
89
  // Load the credentials...
90
0
  if ((certs = openssl_load_x509(credentials)) != NULL)
91
0
  {
92
    // Check the hostname against the primary certificate...
93
0
    X509  *cert = sk_X509_value(certs, 0);
94
          // Primary certificate
95
0
    char  subjectName[256]; // Common name from certificate
96
0
    STACK_OF(GENERAL_NAME) *names = NULL;
97
          // subjectAltName values
98
99
0
    DEBUG_printf("1cupsAreCredentialsValidForName: certs=%p(num=%d), cert=%p", (void *)certs, sk_X509_num(certs), (void *)cert);
100
101
0
    X509_NAME_get_text_by_NID(X509_get_subject_name(cert), NID_commonName, subjectName, sizeof(subjectName));
102
0
    DEBUG_printf("1cupsAreCredentialsValidForName: subjectName=\"%s\"", subjectName);
103
104
0
    if (!_cups_strcasecmp(common_name, subjectName))
105
0
    {
106
0
      DEBUG_puts("1cupsAreCredentialsValidForName: Match.");
107
0
      result = true;
108
0
    }
109
110
#ifdef DEBUG
111
    char issuerName[256];
112
    X509_NAME_get_text_by_NID(X509_get_issuer_name(cert), NID_commonName, issuerName, sizeof(issuerName));
113
    DEBUG_printf("1cupsAreCredentialsValidForName: issuerName=\"%s\"", issuerName);
114
#endif // DEBUG
115
116
0
    if (!result)
117
0
    {
118
0
      names = X509_get_ext_d2i(cert, NID_subject_alt_name, /*crit*/NULL, /*idx*/NULL);
119
0
      DEBUG_printf("1cupsAreCredentialsValidForName: names=%p", (void *)names);
120
0
    }
121
122
0
    if (names)
123
0
    {
124
      // Got subjectAltName values, look at them...
125
0
      int i,      // Looping var
126
0
    count;      // Number of values
127
128
0
      for (i = 0, count = sk_GENERAL_NAME_num(names); i < count && !result; i ++)
129
0
      {
130
0
  const GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
131
          // subjectAltName value
132
133
0
        if (!name)
134
0
          continue;
135
136
0
        DEBUG_printf("1cupsAreCredentialsValidForName: subjectAltName[%d/%d].type=%d", i + 1, count, name->type);
137
0
  if (name->type == GEN_DNS)
138
0
  {
139
    // Match a DNS name...
140
0
    char  *dNSName;   // DNS name value
141
142
0
          if (ASN1_STRING_to_UTF8((unsigned char **)&dNSName, name->d.dNSName) > 0)
143
0
          {
144
0
            DEBUG_printf("1cupsAreCredentialsValidForName: subjectAltName[%d/%d].dNSName=\"%s\"", i + 1, count, dNSName);
145
146
0
            if (!_cups_strcasecmp(common_name, dNSName))
147
0
            {
148
              // Direct name match...
149
0
              DEBUG_puts("1cupsAreCredentialsValidForName: Match.");
150
0
              result = true;
151
0
      }
152
0
      else if (!strncmp(dNSName, "*.", 2))
153
0
      {
154
        // Compare wildcard...
155
0
        const char *domain_name = strchr(common_name, '.');
156
          // Domain name of common name
157
0
              if (domain_name && !_cups_strcasecmp(domain_name, dNSName + 1))
158
0
              {
159
0
    DEBUG_puts("1cupsAreCredentialsValidForName: Match.");
160
0
                result = true;
161
0
        }
162
0
      }
163
164
0
      OPENSSL_free(dNSName);
165
0
          }
166
0
        }
167
0
      }
168
169
0
      GENERAL_NAMES_free(names);
170
0
    }
171
172
0
    sk_X509_free(certs);
173
0
  }
174
175
0
  return (result);
176
0
}
177
178
179
//
180
// 'cupsCreateCredentials()' - Make an X.509 certificate and private key pair.
181
//
182
// This function creates an X.509 certificate and private key pair.  The
183
// certificate and key are stored in the directory "path" or, if "path" is
184
// `NULL`, in a per-user or system-wide (when running as root) certificate/key
185
// store.  The generated certificate is signed by the named root certificate or,
186
// if "root_name" is `NULL`, a site-wide default root certificate.  When
187
// "root_name" is `NULL` and there is no site-wide default root certificate, a
188
// self-signed certificate is generated instead.
189
//
190
// The "ca_cert" argument specifies whether a CA certificate should be created.
191
//
192
// The "purpose" argument specifies the purpose(s) used for the credentials as a
193
// bitwise OR of the following constants:
194
//
195
// - `CUPS_CREDPURPOSE_SERVER_AUTH` for validating TLS servers,
196
// - `CUPS_CREDPURPOSE_CLIENT_AUTH` for validating TLS clients,
197
// - `CUPS_CREDPURPOSE_CODE_SIGNING` for validating compiled code,
198
// - `CUPS_CREDPURPOSE_EMAIL_PROTECTION` for validating email messages,
199
// - `CUPS_CREDPURPOSE_TIME_STAMPING` for signing timestamps to objects, and/or
200
// - `CUPS_CREDPURPOSE_OCSP_SIGNING` for Online Certificate Status Protocol
201
//   message signing.
202
//
203
// The "type" argument specifies the type of credentials using one of the
204
// following constants:
205
//
206
// - `CUPS_CREDTYPE_DEFAULT`: default type (RSA-3072 or P-384),
207
// - `CUPS_CREDTYPE_RSA_2048_SHA256`: RSA with 2048-bit keys and SHA-256 hash,
208
// - `CUPS_CREDTYPE_RSA_3072_SHA256`: RSA with 3072-bit keys and SHA-256 hash,
209
// - `CUPS_CREDTYPE_RSA_4096_SHA256`: RSA with 4096-bit keys and SHA-256 hash,
210
// - `CUPS_CREDTYPE_ECDSA_P256_SHA256`: ECDSA using the P-256 curve with SHA-256 hash,
211
// - `CUPS_CREDTYPE_ECDSA_P384_SHA256`: ECDSA using the P-384 curve with SHA-256 hash, or
212
// - `CUPS_CREDTYPE_ECDSA_P521_SHA256`: ECDSA using the P-521 curve with SHA-256 hash.
213
//
214
// The "usage" argument specifies the usage(s) for the credentials as a bitwise
215
// OR of the following constants:
216
//
217
// - `CUPS_CREDUSAGE_DIGITAL_SIGNATURE`: digital signatures,
218
// - `CUPS_CREDUSAGE_NON_REPUDIATION`: non-repudiation/content commitment,
219
// - `CUPS_CREDUSAGE_KEY_ENCIPHERMENT`: key encipherment,
220
// - `CUPS_CREDUSAGE_DATA_ENCIPHERMENT`: data encipherment,
221
// - `CUPS_CREDUSAGE_KEY_AGREEMENT`: key agreement,
222
// - `CUPS_CREDUSAGE_KEY_CERT_SIGN`: key certicate signing,
223
// - `CUPS_CREDUSAGE_CRL_SIGN`: certificate revocation list signing,
224
// - `CUPS_CREDUSAGE_ENCIPHER_ONLY`: encipherment only,
225
// - `CUPS_CREDUSAGE_DECIPHER_ONLY`: decipherment only,
226
// - `CUPS_CREDUSAGE_DEFAULT_CA`: defaults for CA certificates,
227
// - `CUPS_CREDUSAGE_DEFAULT_TLS`: defaults for TLS certificates, and/or
228
// - `CUPS_CREDUSAGE_ALL`: all usages.
229
//
230
// The "organization", "org_unit", "locality", "state_province", and "country"
231
// arguments specify information about the identity and geolocation of the
232
// issuer.
233
//
234
// The "common_name" argument specifies the common name and the "num_alt_names"
235
// and "alt_names" arguments specify a list of DNS hostnames for the
236
// certificate.
237
//
238
// The "expiration_date" argument specifies the expiration date and time as a
239
// Unix `time_t` value in seconds.
240
//
241
242
bool          // O - `true` on success, `false` on failure
243
cupsCreateCredentials(
244
    const char         *path,   // I - Directory path for certificate/key store or `NULL` for default
245
    bool               ca_cert,   // I - `true` to create a CA certificate, `false` for a client/server certificate
246
    cups_credpurpose_t purpose,   // I - Credential purposes
247
    cups_credtype_t    type,    // I - Credential type
248
    cups_credusage_t   usage,   // I - Credential usages
249
    const char         *organization, // I - Organization or `NULL` to use common name
250
    const char         *org_unit, // I - Organizational unit or `NULL` for none
251
    const char         *locality, // I - City/town or `NULL` for "Unknown"
252
    const char         *state_province, // I - State/province or `NULL` for "Unknown"
253
    const char         *country,  // I - Country or `NULL` for locale-based default
254
    const char         *common_name,  // I - Common name
255
    const char         *email,    // I - Email address or `NULL` for none
256
    size_t             num_alt_names, // I - Number of subject alternate names
257
    const char * const *alt_names,  // I - Subject Alternate Names
258
    const char         *root_name,  // I - Root certificate/domain name or `NULL` for site/self-signed
259
    time_t             expiration_date) // I - Expiration date
260
0
{
261
0
  bool    result = false;    // Return value
262
0
  EVP_PKEY  *pkey;      // Key pair
263
0
  X509    *cert;      // Certificate
264
0
  X509    *root_cert = NULL; // Root certificate, if any
265
0
  EVP_PKEY  *root_key = NULL; // Root private key, if any
266
0
  char    defpath[1024],    // Default path
267
0
    crtfile[1024],    // Certificate filename
268
0
    keyfile[1024],    // Private key filename
269
0
    pubfile[1024],    // Public key filename
270
0
    root_crtfile[1024], // Root certificate filename
271
0
    root_keyfile[1024]; // Root private key filename
272
0
  time_t  curtime;    // Current time
273
0
  X509_NAME *name;      // Subject/issuer name
274
0
  ASN1_INTEGER  *serial;    // Serial number
275
0
  ASN1_TIME *notBefore,   // Initial date
276
0
    *notAfter;    // Expiration date
277
0
  BIO   *bio;     // Output file
278
0
  char    temp[1024],   // Temporary string
279
0
    *tempptr;   // Pointer into temporary string
280
0
  STACK_OF(X509_EXTENSION) *exts; // Extensions
281
0
  X509_EXTENSION *ext;      // Current extension
282
0
  unsigned  i;      // Looping var
283
0
  cups_credpurpose_t purpose_bit; // Current purpose
284
0
  cups_credusage_t usage_bit;   // Current usage
285
286
287
0
  DEBUG_printf("cupsCreateCredentials(path=\"%s\", ca_cert=%s, purpose=0x%x, type=%d, usage=0x%x, organization=\"%s\", org_unit=\"%s\", locality=\"%s\", state_province=\"%s\", country=\"%s\", common_name=\"%s\", num_alt_names=%u, alt_names=%p, root_name=\"%s\", expiration_date=%ld)", path, ca_cert ? "true" : "false", purpose, type, usage, organization, org_unit, locality, state_province, country, common_name, (unsigned)num_alt_names, (void *)alt_names, root_name, (long)expiration_date);
288
289
  // Filenames...
290
0
  if (!path)
291
0
    path = http_default_path(defpath, sizeof(defpath));
292
293
0
  if (!path || !common_name || !*common_name)
294
0
  {
295
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
296
0
    return (false);
297
0
  }
298
299
  // Create the encryption key...
300
0
  DEBUG_puts("1cupsCreateCredentials: Creating key pair.");
301
302
0
  if ((pkey = openssl_create_key(type)) == NULL)
303
0
    return (false);
304
305
0
  DEBUG_puts("1cupsCreateCredentials: Key pair created.");
306
307
  // Create the X.509 certificate...
308
0
  DEBUG_puts("1cupsCreateCredentials: Generating X.509 certificate.");
309
310
0
  if ((cert = X509_new()) == NULL)
311
0
  {
312
0
    EVP_PKEY_free(pkey);
313
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create X.509 certificate."), 1);
314
0
    return (false);
315
0
  }
316
317
0
  curtime = time(NULL);
318
319
0
  notBefore = ASN1_TIME_new();
320
0
  ASN1_TIME_set(notBefore, curtime);
321
0
  X509_set_notBefore(cert, notBefore);
322
0
  ASN1_TIME_free(notBefore);
323
324
0
  notAfter = ASN1_TIME_new();
325
0
  ASN1_TIME_set(notAfter, expiration_date);
326
0
  X509_set_notAfter(cert, notAfter);
327
0
  ASN1_TIME_free(notAfter);
328
329
0
  serial = ASN1_INTEGER_new();
330
0
  ASN1_INTEGER_set(serial, (long)curtime);
331
0
  X509_set_serialNumber(cert, serial);
332
0
  ASN1_INTEGER_free(serial);
333
334
0
  X509_set_pubkey(cert, pkey);
335
336
0
  name = openssl_create_name(organization, org_unit, locality, state_province, country, common_name, email);
337
338
0
  X509_set_subject_name(cert, name);
339
340
  // Try loading a root certificate...
341
0
  http_make_path(root_crtfile, sizeof(root_crtfile), path, root_name ? root_name : "_site_", "crt");
342
0
  http_make_path(root_keyfile, sizeof(root_keyfile), path, root_name ? root_name : "_site_", "key");
343
344
0
  if (!ca_cert && !access(root_crtfile, 0) && !access(root_keyfile, 0))
345
0
  {
346
0
    if ((bio = BIO_new_file(root_crtfile, "rb")) != NULL)
347
0
    {
348
0
      PEM_read_bio_X509(bio, &root_cert, /*cb*/NULL, /*u*/NULL);
349
0
      BIO_free(bio);
350
351
0
      if ((bio = BIO_new_file(root_keyfile, "rb")) != NULL)
352
0
      {
353
0
  PEM_read_bio_PrivateKey(bio, &root_key, /*cb*/NULL, /*u*/NULL);
354
0
  BIO_free(bio);
355
0
      }
356
357
0
      if (!root_key)
358
0
      {
359
        // Only use root certificate if we have the key...
360
0
        X509_free(root_cert);
361
0
        root_cert = NULL;
362
0
      }
363
0
    }
364
365
0
    if (!root_cert || !root_key)
366
0
    {
367
0
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to load X.509 CA certificate and private key."), 1);
368
0
      goto done;
369
0
    }
370
0
  }
371
372
0
  if (root_cert)
373
0
    X509_set_issuer_name(cert, X509_get_subject_name(root_cert));
374
0
  else
375
0
    X509_set_issuer_name(cert, name);
376
377
0
  X509_NAME_free(name);
378
379
0
  exts = sk_X509_EXTENSION_new_null();
380
381
0
  if (ca_cert)
382
0
  {
383
    // Add extensions that are required to make Chrome happy...
384
0
    openssl_add_ext(exts, NID_basic_constraints, "critical,CA:TRUE,pathlen:0");
385
0
  }
386
0
  else
387
0
  {
388
    // Add extension with DNS names and free buffer for GENERAL_NAME
389
0
    if ((ext = openssl_create_san(common_name, num_alt_names, alt_names)) == NULL)
390
0
      goto done;
391
392
0
    sk_X509_EXTENSION_push(exts, ext);
393
394
    // Add extensions that are required to make Chrome happy...
395
0
    openssl_add_ext(exts, NID_basic_constraints, "critical,CA:FALSE,pathlen:0");
396
0
  }
397
398
0
  cupsCopyString(temp, "critical", sizeof(temp));
399
0
  for (tempptr = temp + strlen(temp), i = 0, usage_bit = CUPS_CREDUSAGE_DIGITAL_SIGNATURE; i < (sizeof(tls_usage_strings) / sizeof(tls_usage_strings[0])); i ++, usage_bit *= 2)
400
0
  {
401
0
    if (!(usage & usage_bit))
402
0
      continue;
403
404
0
    snprintf(tempptr, sizeof(temp) - (size_t)(tempptr - temp), ",%s", tls_usage_strings[i]);
405
406
0
    tempptr += strlen(tempptr);
407
0
  }
408
0
  openssl_add_ext(exts, NID_key_usage, temp);
409
410
0
  temp[0] = '\0';
411
0
  for (tempptr = temp, i = 0, purpose_bit = CUPS_CREDPURPOSE_SERVER_AUTH; i < (sizeof(tls_purpose_oids) / sizeof(tls_purpose_oids[0])); i ++, purpose_bit *= 2)
412
0
  {
413
0
    if (!(purpose & purpose_bit))
414
0
      continue;
415
416
0
    if (tempptr == temp)
417
0
      cupsCopyString(temp, tls_purpose_oids[i], sizeof(temp));
418
0
    else
419
0
      snprintf(tempptr, sizeof(temp) - (size_t)(tempptr - temp), ",%s", tls_purpose_oids[i]);
420
421
0
    tempptr += strlen(tempptr);
422
0
  }
423
0
  openssl_add_ext(exts, NID_ext_key_usage, temp);
424
425
0
  openssl_add_ext(exts, NID_subject_key_identifier, "hash");
426
0
  openssl_add_ext(exts, NID_authority_key_identifier, "keyid,issuer");
427
428
0
  while ((ext = sk_X509_EXTENSION_pop(exts)) != NULL)
429
0
  {
430
0
    if (!X509_add_ext(cert, ext, -1))
431
0
    {
432
0
      sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
433
0
      goto done;
434
0
    }
435
0
  }
436
437
0
  X509_set_version(cert, 2); // v3
438
439
0
  if (root_key)
440
0
    X509_sign(cert, root_key, EVP_sha256());
441
0
  else
442
0
    X509_sign(cert, pkey, EVP_sha256());
443
444
  // Save them...
445
0
  http_make_path(crtfile, sizeof(crtfile), path, common_name, "crt");
446
0
  http_make_path(keyfile, sizeof(keyfile), path, common_name, "key");
447
0
  http_make_path(pubfile, sizeof(pubfile), path, common_name, "pub");
448
449
0
  if ((bio = BIO_new_file(keyfile, "wb")) == NULL)
450
0
  {
451
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
452
0
    goto done;
453
0
  }
454
455
0
  if (!PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL))
456
0
  {
457
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to write private key."), 1);
458
0
    BIO_free(bio);
459
0
    goto done;
460
0
  }
461
462
0
  BIO_free(bio);
463
464
0
  if ((bio = BIO_new_file(pubfile, "wb")) == NULL)
465
0
  {
466
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
467
0
    goto done;
468
0
  }
469
470
0
  if (!PEM_write_bio_PUBKEY(bio, pkey))
471
0
  {
472
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to write public key."), true);
473
0
    BIO_free(bio);
474
0
    goto done;
475
0
  }
476
477
0
  BIO_free(bio);
478
479
0
  if ((bio = BIO_new_file(crtfile, "wb")) == NULL)
480
0
  {
481
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
482
0
    goto done;
483
0
  }
484
485
0
  if (!PEM_write_bio_X509(bio, cert))
486
0
  {
487
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to write X.509 certificate."), 1);
488
0
    BIO_free(bio);
489
0
    goto done;
490
0
  }
491
492
0
  if (root_cert)
493
0
    PEM_write_bio_X509(bio, root_cert);
494
495
0
  BIO_free(bio);
496
497
0
  result = true;
498
0
  DEBUG_puts("1cupsCreateCredentials: Successfully created credentials.");
499
500
  // Cleanup...
501
0
  done:
502
503
0
  X509_free(cert);
504
0
  EVP_PKEY_free(pkey);
505
506
0
  if (root_cert)
507
0
    X509_free(root_cert);
508
0
  if (root_key)
509
0
    EVP_PKEY_free(root_key);
510
511
0
  return (result);
512
0
}
513
514
515
//
516
// 'cupsCreateCredentialsRequest()' - Make an X.509 Certificate Signing Request.
517
//
518
// This function creates an X.509 certificate signing request (CSR) and
519
// associated private key.  The CSR and key are stored in the directory "path"
520
// or, if "path" is `NULL`, in a per-user or system-wide (when running as root)
521
// certificate/key store.
522
//
523
// The "purpose" argument specifies the purpose(s) used for the credentials as a
524
// bitwise OR of the following constants:
525
//
526
// - `CUPS_CREDPURPOSE_SERVER_AUTH` for validating TLS servers,
527
// - `CUPS_CREDPURPOSE_CLIENT_AUTH` for validating TLS clients,
528
// - `CUPS_CREDPURPOSE_CODE_SIGNING` for validating compiled code,
529
// - `CUPS_CREDPURPOSE_EMAIL_PROTECTION` for validating email messages,
530
// - `CUPS_CREDPURPOSE_TIME_STAMPING` for signing timestamps to objects, and/or
531
// - `CUPS_CREDPURPOSE_OCSP_SIGNING` for Online Certificate Status Protocol
532
//   message signing.
533
//
534
// The "type" argument specifies the type of credentials using one of the
535
// following constants:
536
//
537
// - `CUPS_CREDTYPE_DEFAULT`: default type (RSA-3072 or P-384),
538
// - `CUPS_CREDTYPE_RSA_2048_SHA256`: RSA with 2048-bit keys and SHA-256 hash,
539
// - `CUPS_CREDTYPE_RSA_3072_SHA256`: RSA with 3072-bit keys and SHA-256 hash,
540
// - `CUPS_CREDTYPE_RSA_4096_SHA256`: RSA with 4096-bit keys and SHA-256 hash,
541
// - `CUPS_CREDTYPE_ECDSA_P256_SHA256`: ECDSA using the P-256 curve with SHA-256 hash,
542
// - `CUPS_CREDTYPE_ECDSA_P384_SHA256`: ECDSA using the P-384 curve with SHA-256 hash, or
543
// - `CUPS_CREDTYPE_ECDSA_P521_SHA256`: ECDSA using the P-521 curve with SHA-256 hash.
544
//
545
// The "usage" argument specifies the usage(s) for the credentials as a bitwise
546
// OR of the following constants:
547
//
548
// - `CUPS_CREDUSAGE_DIGITAL_SIGNATURE`: digital signatures,
549
// - `CUPS_CREDUSAGE_NON_REPUDIATION`: non-repudiation/content commitment,
550
// - `CUPS_CREDUSAGE_KEY_ENCIPHERMENT`: key encipherment,
551
// - `CUPS_CREDUSAGE_DATA_ENCIPHERMENT`: data encipherment,
552
// - `CUPS_CREDUSAGE_KEY_AGREEMENT`: key agreement,
553
// - `CUPS_CREDUSAGE_KEY_CERT_SIGN`: key certicate signing,
554
// - `CUPS_CREDUSAGE_CRL_SIGN`: certificate revocation list signing,
555
// - `CUPS_CREDUSAGE_ENCIPHER_ONLY`: encipherment only,
556
// - `CUPS_CREDUSAGE_DECIPHER_ONLY`: decipherment only,
557
// - `CUPS_CREDUSAGE_DEFAULT_CA`: defaults for CA certificates,
558
// - `CUPS_CREDUSAGE_DEFAULT_TLS`: defaults for TLS certificates, and/or
559
// - `CUPS_CREDUSAGE_ALL`: all usages.
560
//
561
// The "organization", "org_unit", "locality", "state_province", and "country"
562
// arguments specify information about the identity and geolocation of the
563
// issuer.
564
//
565
// The "common_name" argument specifies the common name and the "num_alt_names"
566
// and "alt_names" arguments specify a list of DNS hostnames for the
567
// certificate.
568
//
569
570
bool          // O - `true` on success, `false` on error
571
cupsCreateCredentialsRequest(
572
    const char         *path,   // I - Directory path for certificate/key store or `NULL` for default
573
    cups_credpurpose_t purpose,   // I - Credential purposes
574
    cups_credtype_t    type,    // I - Credential type
575
    cups_credusage_t   usage,   // I - Credential usages
576
    const char         *organization, // I - Organization or `NULL` to use common name
577
    const char         *org_unit, // I - Organizational unit or `NULL` for none
578
    const char         *locality, // I - City/town or `NULL` for "Unknown"
579
    const char         *state_province, // I - State/province or `NULL` for "Unknown"
580
    const char         *country,  // I - Country or `NULL` for locale-based default
581
    const char         *common_name,  // I - Common name
582
    const char         *email,    // I - Email address or `NULL` for none
583
    size_t             num_alt_names, // I - Number of subject alternate names
584
    const char * const *alt_names)  // I - Subject Alternate Names
585
0
{
586
0
  bool    ret = false;    // Return value
587
0
  EVP_PKEY  *pkey;      // Key pair
588
0
  X509_REQ  *csr;     // Certificate signing request
589
0
  X509_NAME *name;      // Subject/issuer name
590
0
  X509_EXTENSION *ext;      // X509 extension
591
0
  BIO   *bio;     // Output file
592
0
  char    temp[1024],   // Temporary directory name
593
0
    *tempptr,   // Pointer into temporary string
594
0
    csrfile[1024],    // Certificate signing request filename
595
0
    keyfile[1024],    // Private key filename
596
0
    pubfile[1024];    // Public key filename
597
0
  STACK_OF(X509_EXTENSION) *exts; // Extensions
598
0
  unsigned  i;      // Looping var
599
0
  cups_credpurpose_t purpose_bit; // Current purpose
600
0
  cups_credusage_t usage_bit;   // Current usage
601
602
603
0
  DEBUG_printf("cupsCreateCredentialsRequest(path=\"%s\", purpose=0x%x, type=%d, usage=0x%x, organization=\"%s\", org_unit=\"%s\", locality=\"%s\", state_province=\"%s\", country=\"%s\", common_name=\"%s\", num_alt_names=%u, alt_names=%p)", path, purpose, type, usage, organization, org_unit, locality, state_province, country, common_name, (unsigned)num_alt_names, (void *)alt_names);
604
605
  // Filenames...
606
0
  if (!path)
607
0
    path = http_default_path(temp, sizeof(temp));
608
609
0
  if (!path || !common_name || !*common_name)
610
0
  {
611
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
612
0
    return (false);
613
0
  }
614
615
0
  http_make_path(csrfile, sizeof(csrfile), path, common_name, "csr");
616
0
  http_make_path(keyfile, sizeof(keyfile), path, common_name, "ktm");
617
0
  http_make_path(pubfile, sizeof(pubfile), path, common_name, "pub");
618
619
  // Create the encryption key...
620
0
  DEBUG_puts("1cupsCreateCredentialsRequest: Creating key pair.");
621
622
0
  if ((pkey = openssl_create_key(type)) == NULL)
623
0
    return (false);
624
625
0
  DEBUG_puts("1cupsCreateCredentialsRequest: Key pair created.");
626
627
  // Create the X.509 certificate...
628
0
  DEBUG_puts("1cupsCreateCredentialsRequest: Generating self-signed X.509 certificate.");
629
630
0
  if ((csr = X509_REQ_new()) == NULL)
631
0
  {
632
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create X.509 certificate signing request."), 1);
633
0
    goto done;
634
0
  }
635
636
0
  X509_REQ_set_pubkey(csr, pkey);
637
638
0
  if ((name = openssl_create_name(organization, org_unit, locality, state_province, country, common_name, email)) == NULL)
639
0
    goto done;
640
641
0
  X509_REQ_set_subject_name(csr, name);
642
0
  X509_NAME_free(name);
643
644
  // Add extension with DNS names and free buffer for GENERAL_NAME
645
0
  exts = sk_X509_EXTENSION_new_null();
646
647
0
  if ((ext = openssl_create_san(common_name, num_alt_names, alt_names)) == NULL)
648
0
    goto done;
649
650
0
  sk_X509_EXTENSION_push(exts, ext);
651
652
0
  cupsCopyString(temp, "critical", sizeof(temp));
653
0
  for (tempptr = temp + strlen(temp), i = 0, usage_bit = CUPS_CREDUSAGE_DIGITAL_SIGNATURE; i < (sizeof(tls_usage_strings) / sizeof(tls_usage_strings[0])); i ++, usage_bit *= 2)
654
0
  {
655
0
    if (!(usage & usage_bit))
656
0
      continue;
657
658
0
    snprintf(tempptr, sizeof(temp) - (size_t)(tempptr - temp), ",%s", tls_usage_strings[i]);
659
660
0
    tempptr += strlen(tempptr);
661
0
  }
662
0
  openssl_add_ext(exts, NID_key_usage, temp);
663
664
0
  temp[0] = '\0';
665
0
  for (tempptr = temp, i = 0, purpose_bit = CUPS_CREDPURPOSE_SERVER_AUTH; i < (sizeof(tls_purpose_oids) / sizeof(tls_purpose_oids[0])); i ++, purpose_bit *= 2)
666
0
  {
667
0
    if (!(purpose & purpose_bit))
668
0
      continue;
669
670
0
    if (tempptr == temp)
671
0
      cupsCopyString(temp, tls_purpose_oids[i], sizeof(temp));
672
0
    else
673
0
      snprintf(tempptr, sizeof(temp) - (size_t)(tempptr - temp), ",%s", tls_purpose_oids[i]);
674
675
0
    tempptr += strlen(tempptr);
676
0
  }
677
0
  openssl_add_ext(exts, NID_ext_key_usage, temp);
678
679
0
  X509_REQ_add_extensions(csr, exts);
680
0
  X509_REQ_sign(csr, pkey, EVP_sha256());
681
682
0
  sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
683
684
  // Save them...
685
0
  if ((bio = BIO_new_file(keyfile, "wb")) == NULL)
686
0
  {
687
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
688
0
    goto done;
689
0
  }
690
691
0
  if (!PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL))
692
0
  {
693
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to write private key."), 1);
694
0
    BIO_free(bio);
695
0
    goto done;
696
0
  }
697
698
0
  BIO_free(bio);
699
700
0
  if ((bio = BIO_new_file(pubfile, "wb")) == NULL)
701
0
  {
702
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
703
0
    goto done;
704
0
  }
705
706
0
  if (!PEM_write_bio_PUBKEY(bio, pkey))
707
0
  {
708
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to write public key."), true);
709
0
    BIO_free(bio);
710
0
    goto done;
711
0
  }
712
713
0
  BIO_free(bio);
714
715
0
  if ((bio = BIO_new_file(csrfile, "wb")) == NULL)
716
0
  {
717
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
718
0
    goto done;
719
0
  }
720
721
0
  if (!PEM_write_bio_X509_REQ(bio, csr))
722
0
  {
723
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to write X.509 certificate signing request."), 1);
724
0
    BIO_free(bio);
725
0
    goto done;
726
0
  }
727
728
0
  BIO_free(bio);
729
730
0
  ret = true;
731
0
  DEBUG_puts("1cupsCreateCredentialsRequest: Successfully created signing request.");
732
733
  // Cleanup...
734
0
  done:
735
736
0
  X509_REQ_free(csr);
737
0
  EVP_PKEY_free(pkey);
738
739
0
  return (ret);
740
0
}
741
742
743
//
744
// 'cupsGetCredentialsExpiration()' - Return the expiration date of the credentials.
745
//
746
747
time_t          // O - Expiration date of credentials
748
cupsGetCredentialsExpiration(
749
    const char *credentials)    // I - Credentials
750
0
{
751
0
  time_t    result = 0; // Result
752
0
  STACK_OF(X509)  *certs;   // Certificate chain
753
754
755
0
  if ((certs = openssl_load_x509(credentials)) != NULL)
756
0
  {
757
0
    result = openssl_get_date(sk_X509_value(certs, 0), 1);
758
0
    sk_X509_free(certs);
759
0
  }
760
761
0
  return (result);
762
0
}
763
764
765
//
766
// 'cupsGetCredentialsInfo()' - Return a string describing the credentials.
767
//
768
769
char *          // O - Credentials description or `NULL` on error
770
cupsGetCredentialsInfo(
771
    const char *credentials,    // I - Credentials
772
    char       *buffer,     // I - Buffer
773
    size_t     bufsize)     // I - Size of buffer
774
0
{
775
0
  STACK_OF(X509)  *certs;   // Certificate chain
776
0
  X509      *cert;    // Certificate
777
778
779
  // Range check input...
780
0
  DEBUG_printf("cupsGetCredentialsInfo(credentials=%p, buffer=%p, bufsize=" CUPS_LLFMT ")", (void *)credentials, (void *)buffer, CUPS_LLCAST bufsize);
781
782
0
  if (buffer)
783
0
    *buffer = '\0';
784
785
0
  if (!credentials || !buffer || bufsize < 32)
786
0
  {
787
0
    DEBUG_puts("1cupsGetCredentialsInfo: Returning NULL.");
788
0
    return (NULL);
789
0
  }
790
791
0
  if ((certs = openssl_load_x509(credentials)) != NULL)
792
0
  {
793
0
    char    name[256],  // Common name associated with cert
794
0
      issuer[256],  // Issuer associated with cert
795
0
      expdate[256]; // Expiration data as string
796
0
    time_t    expiration; // Expiration date of cert
797
0
    const char    *sigalg;  // Signature algorithm
798
0
    unsigned char md5_digest[16]; // MD5 result
799
800
0
    cert = sk_X509_value(certs, 0);
801
802
0
    X509_NAME_get_text_by_NID(X509_get_subject_name(cert), NID_commonName, name, sizeof(name));
803
0
    X509_NAME_get_text_by_NID(X509_get_issuer_name(cert), NID_commonName, issuer, sizeof(issuer));
804
0
    expiration = openssl_get_date(cert, 1);
805
806
0
    switch (X509_get_signature_nid(cert))
807
0
    {
808
0
      case NID_ecdsa_with_SHA1 :
809
0
          sigalg = "SHA1WithECDSAEncryption";
810
0
          break;
811
0
      case NID_ecdsa_with_SHA224 :
812
0
          sigalg = "SHA224WithECDSAEncryption";
813
0
          break;
814
0
      case NID_ecdsa_with_SHA256 :
815
0
          sigalg = "SHA256WithECDSAEncryption";
816
0
          break;
817
0
      case NID_ecdsa_with_SHA384 :
818
0
          sigalg = "SHA384WithECDSAEncryption";
819
0
          break;
820
0
      case NID_ecdsa_with_SHA512 :
821
0
          sigalg = "SHA512WithECDSAEncryption";
822
0
          break;
823
0
      case NID_sha1WithRSAEncryption :
824
0
          sigalg = "SHA1WithRSAEncryption";
825
0
          break;
826
0
      case NID_sha224WithRSAEncryption :
827
0
          sigalg = "SHA224WithRSAEncryption";
828
0
          break;
829
0
      case NID_sha256WithRSAEncryption :
830
0
          sigalg = "SHA256WithRSAEncryption";
831
0
          break;
832
0
      case NID_sha384WithRSAEncryption :
833
0
          sigalg = "SHA384WithRSAEncryption";
834
0
          break;
835
0
      case NID_sha512WithRSAEncryption :
836
0
          sigalg = "SHA512WithRSAEncryption";
837
0
          break;
838
0
      default :
839
0
          sigalg = "Unknown";
840
0
          break;
841
0
    }
842
843
0
    cupsHashData("md5", credentials, strlen(credentials), md5_digest, sizeof(md5_digest));
844
845
0
    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, httpGetDateString2(expiration, expdate, sizeof(expdate)), 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]);
846
0
    sk_X509_free(certs);
847
0
  }
848
849
0
  DEBUG_printf("1cupsGetCredentialsInfo: Returning \"%s\".", buffer);
850
851
0
  return (buffer);
852
0
}
853
854
855
//
856
// 'cupsGetCredentialsTrust()' - Return the trust of credentials.
857
//
858
// This function determines the level of trust for the supplied credentials.
859
// The "path" parameter specifies the certificate/key store for known
860
// credentials and certificate authorities.  The "common_name" parameter
861
// specifies the FQDN of the service being accessed such as
862
// "printer.example.com".  The "credentials" parameter provides the credentials
863
// being evaluated, which are usually obtained with the
864
// @link httpCopyPeerCredentials@ function.  The "require_ca" parameter
865
// specifies whether a CA-signed certificate is required for trust.
866
//
867
// The `AllowAnyRoot`, `AllowExpiredCerts`, `TrustOnFirstUse`, and
868
// `ValidateCerts` options in the "client.conf" file (or corresponding
869
// preferences file on macOS) control the trust policy, which defaults to
870
// AllowAnyRoot=Yes, AllowExpiredCerts=No, TrustOnFirstUse=Yes, and
871
// ValidateCerts=No.  When the "require_ca" parameter is `true` the AllowAnyRoot
872
// and TrustOnFirstUse policies are turned off ("No").
873
//
874
// The returned trust value can be one of the following:
875
//
876
// - `HTTP_TRUST_OK`: Credentials are OK/trusted
877
// - `HTTP_TRUST_INVALID`: Credentials are invalid
878
// - `HTTP_TRUST_EXPIRED`: Credentials are expired
879
// - `HTTP_TRUST_RENEWED`: Credentials have been renewed
880
// - `HTTP_TRUST_UNKNOWN`: Credentials are unknown/new
881
//
882
883
http_trust_t        // O - Level of trust
884
cupsGetCredentialsTrust(
885
    const char *path,     // I - Directory path for certificate/key store or `NULL` for default
886
    const char *common_name,    // I - Common name for trust lookup
887
    const char *credentials,    // I - Credentials
888
    bool       require_ca)    // I - Require a CA-signed certificate?
889
0
{
890
0
  http_trust_t    trust = HTTP_TRUST_OK;
891
          // Trusted?
892
0
  STACK_OF(X509)  *certs;   // Certificate chain
893
0
  X509      *cert;    // Certificate
894
0
  char      *tcreds = NULL; // Trusted credentials
895
0
  char      defpath[1024];  // Default path
896
0
  _cups_globals_t *cg = _cupsGlobals(); // Per-thread globals
897
898
899
0
  DEBUG_printf("cupsGetCredentialsTrust(path=\"%s\", common_name=\"%s\", credentials=\"%lu bytes\", require_ca=%s)", path, common_name, (unsigned long)(credentials ? strlen(credentials) : 0), require_ca ? "true" : "false");
900
901
  // Range check input...
902
0
  if (!path)
903
0
    path = http_default_path(defpath, sizeof(defpath));
904
905
0
  if (!path || !credentials || !common_name)
906
0
  {
907
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), false);
908
0
    DEBUG_printf("1cupsGetCredentialsTrust: Returning %d.", HTTP_TRUST_UNKNOWN);
909
0
    return (HTTP_TRUST_UNKNOWN);
910
0
  }
911
912
  // Load the credentials...
913
0
  if ((certs = openssl_load_x509(credentials)) == NULL)
914
0
  {
915
0
    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("Unable to import credentials."), true);
916
0
    DEBUG_printf("1cupsGetCredentialsTrust: Returning %d.", HTTP_TRUST_UNKNOWN);
917
0
    return (HTTP_TRUST_UNKNOWN);
918
0
  }
919
920
0
  cert = sk_X509_value(certs, 0);
921
922
0
  if (!cg->client_conf_loaded)
923
0
  {
924
0
    _cupsSetDefaults();
925
//    openssl_load_crl();
926
0
  }
927
928
  // Look this common name up in the default keychains...
929
0
  if (sk_X509_num(certs) == 1 && (tcreds = cupsCopyCredentials(path, common_name)) != NULL)
930
0
  {
931
0
    char  credentials_str[1024],  // String for incoming credentials
932
0
    tcreds_str[1024]; // String for saved credentials
933
934
0
    cupsGetCredentialsInfo(credentials, credentials_str, sizeof(credentials_str));
935
0
    cupsGetCredentialsInfo(tcreds, tcreds_str, sizeof(tcreds_str));
936
937
0
    if (strcmp(credentials_str, tcreds_str))
938
0
    {
939
      // Credentials don't match, let's look at the expiration date of the new
940
      // credentials and allow if the new ones have a later expiration...
941
0
      if (!cg->trust_first || require_ca)
942
0
      {
943
        // Do not trust certificates on first use...
944
0
        _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("Trust on first use is disabled."), 1);
945
946
0
        trust = HTTP_TRUST_INVALID;
947
0
      }
948
0
      else if (cupsGetCredentialsExpiration(credentials) <= cupsGetCredentialsExpiration(tcreds))
949
0
      {
950
        // The new credentials are not newly issued...
951
0
        _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("New credentials are older than stored credentials."), 1);
952
953
0
        trust = HTTP_TRUST_INVALID;
954
0
      }
955
0
      else if (!cupsAreCredentialsValidForName(common_name, credentials))
956
0
      {
957
        // The common name does not match the issued certificate...
958
0
        _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("New credentials are not valid for name."), 1);
959
960
0
        trust = HTTP_TRUST_INVALID;
961
0
      }
962
0
      else if (cupsGetCredentialsExpiration(tcreds) < time(NULL))
963
0
      {
964
        // Save the renewed credentials...
965
0
  trust = HTTP_TRUST_RENEWED;
966
967
0
        cupsSaveCredentials(path, common_name, credentials, NULL);
968
0
      }
969
0
    }
970
971
0
    free(tcreds);
972
0
  }
973
0
  else if ((cg->validate_certs || require_ca) && !cupsAreCredentialsValidForName(common_name, credentials))
974
0
  {
975
0
    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("No stored credentials, not valid for name."), 1);
976
0
    trust = HTTP_TRUST_INVALID;
977
0
  }
978
0
  else if (sk_X509_num(certs) > 1)
979
0
  {
980
0
    if (!http_check_roots(credentials))
981
0
    {
982
      // See if we have a site CA certificate we can compare...
983
0
      if ((tcreds = cupsCopyCredentials(path, "_site_")) != NULL)
984
0
      {
985
0
  size_t  credslen,   // Length of credentials
986
0
      tcredslen;    // Length of trust root
987
988
989
  // Do a tail comparison of the root...
990
0
  credslen  = strlen(credentials);
991
0
  tcredslen = strlen(tcreds);
992
0
  if (credslen <= tcredslen || strcmp(credentials + (credslen - tcredslen), tcreds))
993
0
  {
994
    // Certificate isn't directly generated from the CA cert...
995
0
    trust = HTTP_TRUST_INVALID;
996
0
  }
997
998
0
  if (trust != HTTP_TRUST_OK)
999
0
    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("Credentials do not validate against site CA certificate."), 1);
1000
1001
0
  free(tcreds);
1002
0
      }
1003
0
    }
1004
0
  }
1005
0
  else if (require_ca)
1006
0
  {
1007
0
    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("Credentials are not CA-signed."), 1);
1008
0
    trust = HTTP_TRUST_INVALID;
1009
0
  }
1010
0
  else if (!cg->trust_first)
1011
0
  {
1012
0
    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("Trust on first use is disabled."), 1);
1013
0
    trust = HTTP_TRUST_INVALID;
1014
0
  }
1015
0
  else if (!cg->any_root || require_ca)
1016
0
  {
1017
0
    _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("Self-signed credentials are blocked."), 1);
1018
0
    trust = HTTP_TRUST_INVALID;
1019
0
  }
1020
1021
0
  if (trust == HTTP_TRUST_OK && !cg->expired_certs)
1022
0
  {
1023
0
    time_t  curtime;    // Current date/time
1024
1025
0
    time(&curtime);
1026
1027
0
    DEBUG_printf("1cupsGetCredentialsTrust: curtime=%ld, notBefore=%ld, notAfter=%ld", (long)curtime, (long)openssl_get_date(cert, 0), (long)openssl_get_date(cert, 1));
1028
1029
0
    if ((curtime + 86400) < openssl_get_date(cert, 0) || curtime > openssl_get_date(cert, 1))
1030
0
    {
1031
0
      _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, _("Credentials have expired."), 1);
1032
0
      trust = HTTP_TRUST_EXPIRED;
1033
0
    }
1034
0
  }
1035
1036
0
  sk_X509_free(certs);
1037
1038
0
  DEBUG_printf("1cupsGetCredentialsTrust: Returning %d.", trust);
1039
1040
0
  return (trust);
1041
0
}
1042
1043
1044
//
1045
// 'cupsSignCredentialsRequest()' - Sign an X.509 certificate signing request to produce an X.509 certificate chain.
1046
//
1047
// This function creates an X.509 certificate from a signing request.  The
1048
// certificate is stored in the directory "path" or, if "path" is `NULL`, in a
1049
// per-user or system-wide (when running as root) certificate/key store.  The
1050
// generated certificate is signed by the named root certificate or, if
1051
// "root_name" is `NULL`, a site-wide default root certificate.  When
1052
// "root_name" is `NULL` and there is no site-wide default root certificate, a
1053
// self-signed certificate is generated instead.
1054
//
1055
// The "allowed_purpose" argument specifies the allowed purpose(s) used for the
1056
// credentials as a bitwise OR of the following constants:
1057
//
1058
// - `CUPS_CREDPURPOSE_SERVER_AUTH` for validating TLS servers,
1059
// - `CUPS_CREDPURPOSE_CLIENT_AUTH` for validating TLS clients,
1060
// - `CUPS_CREDPURPOSE_CODE_SIGNING` for validating compiled code,
1061
// - `CUPS_CREDPURPOSE_EMAIL_PROTECTION` for validating email messages,
1062
// - `CUPS_CREDPURPOSE_TIME_STAMPING` for signing timestamps to objects, and/or
1063
// - `CUPS_CREDPURPOSE_OCSP_SIGNING` for Online Certificate Status Protocol
1064
//   message signing.
1065
//
1066
// The "allowed_usage" argument specifies the allowed usage(s) for the
1067
// credentials as a bitwise OR of the following constants:
1068
//
1069
// - `CUPS_CREDUSAGE_DIGITAL_SIGNATURE`: digital signatures,
1070
// - `CUPS_CREDUSAGE_NON_REPUDIATION`: non-repudiation/content commitment,
1071
// - `CUPS_CREDUSAGE_KEY_ENCIPHERMENT`: key encipherment,
1072
// - `CUPS_CREDUSAGE_DATA_ENCIPHERMENT`: data encipherment,
1073
// - `CUPS_CREDUSAGE_KEY_AGREEMENT`: key agreement,
1074
// - `CUPS_CREDUSAGE_KEY_CERT_SIGN`: key certicate signing,
1075
// - `CUPS_CREDUSAGE_CRL_SIGN`: certificate revocation list signing,
1076
// - `CUPS_CREDUSAGE_ENCIPHER_ONLY`: encipherment only,
1077
// - `CUPS_CREDUSAGE_DECIPHER_ONLY`: decipherment only,
1078
// - `CUPS_CREDUSAGE_DEFAULT_CA`: defaults for CA certificates,
1079
// - `CUPS_CREDUSAGE_DEFAULT_TLS`: defaults for TLS certificates, and/or
1080
// - `CUPS_CREDUSAGE_ALL`: all usages.
1081
//
1082
// The "cb" and "cb_data" arguments specify a function and its data that are
1083
// used to validate any subjectAltName values in the signing request:
1084
//
1085
// ```
1086
// bool san_cb(const char *common_name, const char *alt_name, void *cb_data) {
1087
//   ... return true if OK and false if not ...
1088
// }
1089
// ```
1090
//
1091
// If `NULL`, a default validation function is used that allows "localhost" and
1092
// variations of the common name.
1093
//
1094
// The "expiration_date" argument specifies the expiration date and time as a
1095
// Unix `time_t` value in seconds.
1096
//
1097
1098
bool          // O - `true` on success, `false` on failure
1099
cupsSignCredentialsRequest(
1100
    const char         *path,   // I - Directory path for certificate/key store or `NULL` for default
1101
    const char         *common_name,  // I - Common name to use
1102
    const char         *request,  // I - PEM-encoded CSR
1103
    const char         *root_name,  // I - Root certificate
1104
    cups_credpurpose_t allowed_purpose, // I - Allowed credential purpose(s)
1105
    cups_credusage_t   allowed_usage, // I - Allowed credential usage(s)
1106
    cups_cert_san_cb_t cb,    // I - subjectAltName callback or `NULL` to allow just .local
1107
    void               *cb_data,  // I - Callback data
1108
    time_t             expiration_date) // I - Certificate expiration date
1109
0
{
1110
0
  bool    result = false;    // Return value
1111
0
  X509    *cert = NULL;   // Certificate
1112
0
  X509_REQ  *crq = NULL;   // Certificate request
1113
0
  X509    *root_cert = NULL; // Root certificate, if any
1114
0
  EVP_PKEY  *root_key = NULL; // Root private key, if any
1115
0
  char    defpath[1024],    // Default path
1116
0
    crtfile[1024],    // Certificate filename
1117
0
    root_crtfile[1024], // Root certificate filename
1118
0
    root_keyfile[1024]; // Root private key filename
1119
0
  time_t  curtime;    // Current time
1120
0
  ASN1_INTEGER  *serial;    // Serial number
1121
0
  ASN1_TIME *notBefore,   // Initial date
1122
0
    *notAfter;    // Expiration date
1123
0
  BIO   *bio;     // Input/output file
1124
0
  char    temp[1024];   // Temporary string
1125
0
  int   i, j,     // Looping vars
1126
0
    num_exts;   // Number of extensions
1127
0
  STACK_OF(X509_EXTENSION) *exts = NULL;// Extensions
1128
0
  X509_EXTENSION *ext;      // Current extension
1129
0
  cups_credpurpose_t purpose;   // Current purpose
1130
0
  cups_credusage_t usage;   // Current usage
1131
0
  bool    saw_usage = false,  // Saw NID_key_usage?
1132
0
    saw_ext_usage = false,  // Saw NID_ext_key_usage?
1133
0
    saw_san = false;  // Saw NID_subject_alt_name?
1134
1135
1136
0
  DEBUG_printf("cupsSignCredentialsRequest(path=\"%s\", common_name=\"%s\", request=\"%s\", root_name=\"%s\", allowed_purpose=0x%x, allowed_usage=0x%x, cb=%p, cb_data=%p, expiration_date=%ld)", path, common_name, request, root_name, allowed_purpose, allowed_usage, (void *)cb, cb_data, (long)expiration_date);
1137
1138
  // Filenames...
1139
0
  if (!path)
1140
0
    path = http_default_path(defpath, sizeof(defpath));
1141
1142
0
  if (!path || !common_name || !request)
1143
0
  {
1144
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), false);
1145
0
    return (false);
1146
0
  }
1147
1148
0
  if (!cb)
1149
0
    cb = http_default_san_cb;
1150
1151
  // Import the X.509 certificate request...
1152
0
  DEBUG_puts("1cupsCreateCredentials: Importing X.509 certificate request.");
1153
0
  if ((bio = BIO_new_mem_buf(request, (int)strlen(request))) != NULL)
1154
0
  {
1155
0
    PEM_read_bio_X509_REQ(bio, &crq, /*cb*/NULL, /*u*/NULL);
1156
0
    BIO_free(bio);
1157
0
  }
1158
1159
0
  if (!crq)
1160
0
  {
1161
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to import X.509 certificate request."), 1);
1162
0
    return (false);
1163
0
  }
1164
1165
0
  if (X509_REQ_verify(crq, X509_REQ_get_pubkey(crq)) < 0)
1166
0
  {
1167
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to verify X.509 certificate request."), 1);
1168
0
    goto done;
1169
0
  }
1170
1171
  // Create the X.509 certificate...
1172
0
  DEBUG_puts("1cupsSignCredentialsRequest: Generating X.509 certificate.");
1173
1174
0
  if ((cert = X509_new()) == NULL)
1175
0
  {
1176
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create X.509 certificate."), 1);
1177
0
    goto done;
1178
0
  }
1179
1180
0
  curtime = time(NULL);
1181
1182
0
  notBefore = ASN1_TIME_new();
1183
0
  ASN1_TIME_set(notBefore, curtime);
1184
0
  X509_set_notBefore(cert, notBefore);
1185
0
  ASN1_TIME_free(notBefore);
1186
1187
0
  notAfter  = ASN1_TIME_new();
1188
0
  ASN1_TIME_set(notAfter, expiration_date);
1189
0
  X509_set_notAfter(cert, notAfter);
1190
0
  ASN1_TIME_free(notAfter);
1191
1192
0
  serial = ASN1_INTEGER_new();
1193
0
  ASN1_INTEGER_set(serial, (long)curtime);
1194
0
  X509_set_serialNumber(cert, serial);
1195
0
  ASN1_INTEGER_free(serial);
1196
1197
0
  X509_set_pubkey(cert, X509_REQ_get_pubkey(crq));
1198
1199
0
  X509_set_subject_name(cert, X509_REQ_get_subject_name(crq));
1200
0
  X509_set_version(cert, 2); // v3
1201
1202
  // Copy/verify extensions...
1203
0
  exts     = X509_REQ_get_extensions(crq);
1204
0
  num_exts = sk_X509_EXTENSION_num(exts);
1205
1206
0
  for (i = 0; i < num_exts; i ++)
1207
0
  {
1208
    // Get the extension object...
1209
0
    bool    add_ext = false;  // Add this extension?
1210
0
    ASN1_OBJECT   *obj;     // Extension object
1211
0
    ASN1_OCTET_STRING *extdata;   // Extension data string
1212
0
    unsigned char *data = NULL;   // Extension data bytes
1213
0
    int     datalen;    // Length of extension data
1214
1215
0
    ext     = sk_X509_EXTENSION_value(exts, i);
1216
0
    obj     = X509_EXTENSION_get_object(ext);
1217
0
    extdata = X509_EXTENSION_get_data(ext);
1218
0
    datalen = i2d_ASN1_OCTET_STRING(extdata, &data);
1219
1220
#ifdef DEBUG
1221
    char *tempptr;        // Pointer into string
1222
1223
    for (j = 0, tempptr = temp; j < datalen; j ++, tempptr += 2)
1224
      snprintf(tempptr, sizeof(temp) - (size_t)(tempptr - temp), "%02X", data[j]);
1225
1226
    DEBUG_printf("1cupsSignCredentialsRequest: EXT%d=%s", OBJ_obj2nid(obj), temp);
1227
#endif // DEBUG
1228
1229
0
    switch (OBJ_obj2nid(obj))
1230
0
    {
1231
0
      case NID_ext_key_usage :
1232
0
          add_ext       = true;
1233
0
          saw_ext_usage = true;
1234
1235
0
          if (datalen < 12 || data[2] != 0x30 || data[3] != (datalen - 4))
1236
0
          {
1237
0
            _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad keyUsage extension in X.509 certificate request."), 1);
1238
0
      goto done;
1239
0
          }
1240
1241
0
          for (purpose = 0, j = 4; j < datalen; j += data[j + 1] + 2)
1242
0
          {
1243
0
            if (data[j] != 0x06 || data[j + 1] != 8 || memcmp(data + j + 2, "+\006\001\005\005\007\003", 7))
1244
0
            {
1245
0
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad keyUsage extension in X.509 certificate request."), 1);
1246
0
        goto done;
1247
0
            }
1248
1249
0
            switch (data[j + 9])
1250
0
            {
1251
0
              case 1 :
1252
0
                  purpose |= CUPS_CREDPURPOSE_SERVER_AUTH;
1253
0
                  break;
1254
0
              case 2 :
1255
0
                  purpose |= CUPS_CREDPURPOSE_CLIENT_AUTH;
1256
0
                  break;
1257
0
              case 3 :
1258
0
                  purpose |= CUPS_CREDPURPOSE_CODE_SIGNING;
1259
0
                  break;
1260
0
              case 4 :
1261
0
                  purpose |= CUPS_CREDPURPOSE_EMAIL_PROTECTION;
1262
0
                  break;
1263
0
              case 8 :
1264
0
                  purpose |= CUPS_CREDPURPOSE_TIME_STAMPING;
1265
0
                  break;
1266
0
              case 9 :
1267
0
                  purpose |= CUPS_CREDPURPOSE_OCSP_SIGNING;
1268
0
                  break;
1269
0
        default :
1270
0
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad keyUsage extension in X.509 certificate request."), 1);
1271
0
      goto done;
1272
0
            }
1273
0
          }
1274
1275
0
          DEBUG_printf("1cupsSignCredentialsRequest: purpose=0x%04x", purpose);
1276
1277
0
          if (purpose & ~allowed_purpose)
1278
0
          {
1279
0
            _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad keyUsage extension in X.509 certificate request."), 1);
1280
0
      goto done;
1281
0
          }
1282
0
          break;
1283
1284
0
      case NID_key_usage :
1285
0
          add_ext   = true;
1286
0
          saw_usage = true;
1287
1288
0
          if (datalen < 6 || datalen > 7 || data[2] != 0x03 || data[3] != (datalen - 4))
1289
0
          {
1290
0
            _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad extKeyUsage extension in X.509 certificate request."), 1);
1291
0
      goto done;
1292
0
          }
1293
1294
0
          usage = 0;
1295
0
          if (data[5] & 0x80)
1296
0
      usage |= CUPS_CREDUSAGE_DIGITAL_SIGNATURE;
1297
0
          if (data[5] & 0x40)
1298
0
      usage |= CUPS_CREDUSAGE_NON_REPUDIATION;
1299
0
          if (data[5] & 0x20)
1300
0
      usage |= CUPS_CREDUSAGE_KEY_ENCIPHERMENT;
1301
0
          if (data[5] & 0x10)
1302
0
      usage |= CUPS_CREDUSAGE_DATA_ENCIPHERMENT;
1303
0
          if (data[5] & 0x08)
1304
0
      usage |= CUPS_CREDUSAGE_KEY_AGREEMENT;
1305
0
          if (data[5] & 0x04)
1306
0
      usage |= CUPS_CREDUSAGE_KEY_CERT_SIGN;
1307
0
          if (data[5] & 0x02)
1308
0
      usage |= CUPS_CREDUSAGE_CRL_SIGN;
1309
0
          if (data[5] & 0x01)
1310
0
      usage |= CUPS_CREDUSAGE_ENCIPHER_ONLY;
1311
0
          if (datalen == 7 && (data[6] & 0x80))
1312
0
      usage |= CUPS_CREDUSAGE_DECIPHER_ONLY;
1313
1314
0
          DEBUG_printf("1cupsSignCredentialsRequest: usage=0x%04x", usage);
1315
1316
0
          if (usage & ~allowed_usage)
1317
0
          {
1318
0
            _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad extKeyUsage extension in X.509 certificate request."), 1);
1319
0
      goto done;
1320
0
          }
1321
0
          break;
1322
1323
0
      case NID_subject_alt_name :
1324
0
          add_ext = true;
1325
0
          saw_san = true;
1326
1327
0
          if (datalen < 4 || data[2] != 0x30 || data[3] != (datalen - 4))
1328
0
          {
1329
0
            _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad subjectAltName extension in X.509 certificate request."), 1);
1330
0
      goto done;
1331
0
          }
1332
1333
          // Parse the SAN values (there should be an easier/standard OpenSSL API to do this!)
1334
0
          for (j = 4, datalen -= 2; j < datalen; j += data[j + 1] + 2)
1335
0
          {
1336
0
            if (data[j] == 0x82 && data[j + 1])
1337
0
            {
1338
              // GENERAL_STRING for DNS
1339
0
              memcpy(temp, data + j + 2, data[j + 1]);
1340
0
              temp[data[j + 1]] = '\0';
1341
1342
0
              DEBUG_printf("1cupsSignCredentialsRequest: SAN %s", temp);
1343
1344
0
              if (!(cb)(common_name, temp, cb_data))
1345
0
              {
1346
0
                _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Validation of subjectAltName in X.509 certificate request failed."), 1);
1347
0
                goto done;
1348
0
              }
1349
0
      }
1350
0
          }
1351
0
          break;
1352
0
    }
1353
1354
0
    OPENSSL_free(data);
1355
1356
    // If we get this far, the object is OK and we can add it...
1357
0
    if (add_ext && !X509_add_ext(cert, ext, -1))
1358
0
      goto done;
1359
0
  }
1360
1361
  // Add basic constraints for an "edge" certificate...
1362
0
  if ((ext = X509V3_EXT_conf_nid(/*conf*/NULL, /*ctx*/NULL, NID_basic_constraints, "critical,CA:FALSE,pathlen:0")) == NULL || !X509_add_ext(cert, ext, -1))
1363
0
  {
1364
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to add extension to X.509 certificate."), 1);
1365
0
    goto done;
1366
0
  }
1367
1368
  // Add key usage extensions as needed...
1369
0
  if (!saw_usage)
1370
0
  {
1371
0
    if ((ext = X509V3_EXT_conf_nid(/*conf*/NULL, /*ctx*/NULL, NID_key_usage, "critical,digitalSignature,keyEncipherment")) == NULL || !X509_add_ext(cert, ext, -1))
1372
0
    {
1373
0
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to add extension to X.509 certificate."), 1);
1374
0
      goto done;
1375
0
    }
1376
0
  }
1377
1378
0
  if (!saw_ext_usage)
1379
0
  {
1380
0
    if ((ext = X509V3_EXT_conf_nid(/*conf*/NULL, /*ctx*/NULL, NID_ext_key_usage, tls_usage_strings[0])) == NULL || !X509_add_ext(cert, ext, -1))
1381
0
    {
1382
0
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to add extension to X.509 certificate."), 1);
1383
0
      goto done;
1384
0
    }
1385
0
  }
1386
1387
0
  if (!saw_san)
1388
0
  {
1389
0
    if ((ext = openssl_create_san(common_name, /*num_alt_names*/0, /*alt_names*/NULL)) == NULL || !X509_add_ext(cert, ext, -1))
1390
0
    {
1391
0
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to add extension to X.509 certificate."), 1);
1392
0
      goto done;
1393
0
    }
1394
0
  }
1395
1396
  // Try loading a root certificate...
1397
0
  http_make_path(root_crtfile, sizeof(root_crtfile), path, root_name ? root_name : "_site_", "crt");
1398
0
  http_make_path(root_keyfile, sizeof(root_keyfile), path, root_name ? root_name : "_site_", "key");
1399
1400
0
  if (!access(root_crtfile, 0) && !access(root_keyfile, 0))
1401
0
  {
1402
0
    if ((bio = BIO_new_file(root_crtfile, "rb")) != NULL)
1403
0
    {
1404
0
      PEM_read_bio_X509(bio, &root_cert, /*cb*/NULL, /*u*/NULL);
1405
0
      BIO_free(bio);
1406
1407
0
      if ((bio = BIO_new_file(root_keyfile, "rb")) != NULL)
1408
0
      {
1409
0
  PEM_read_bio_PrivateKey(bio, &root_key, /*cb*/NULL, /*u*/NULL);
1410
0
  BIO_free(bio);
1411
0
      }
1412
1413
0
      if (!root_key)
1414
0
      {
1415
        // Only use root certificate if we have the key...
1416
0
        X509_free(root_cert);
1417
0
        root_cert = NULL;
1418
0
      }
1419
0
    }
1420
0
  }
1421
1422
0
  if (!root_cert || !root_key)
1423
0
  {
1424
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to load X.509 CA certificate and private key."), 1);
1425
0
    goto done;
1426
0
  }
1427
1428
0
  X509_set_issuer_name(cert, X509_get_subject_name(root_cert));
1429
0
  X509_sign(cert, root_key, EVP_sha256());
1430
1431
  // Save the certificate...
1432
0
  http_make_path(crtfile, sizeof(crtfile), path, common_name, "crt");
1433
1434
0
  if ((bio = BIO_new_file(crtfile, "wb")) == NULL)
1435
0
  {
1436
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
1437
0
    goto done;
1438
0
  }
1439
1440
0
  if (!PEM_write_bio_X509(bio, cert))
1441
0
  {
1442
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to write X.509 certificate."), 1);
1443
0
    BIO_free(bio);
1444
0
    goto done;
1445
0
  }
1446
1447
0
  PEM_write_bio_X509(bio, root_cert);
1448
1449
0
  BIO_free(bio);
1450
0
  result = true;
1451
0
  DEBUG_puts("1cupsSignRequest: Successfully created credentials.");
1452
1453
  // Cleanup...
1454
0
  done:
1455
1456
0
  if (exts)
1457
0
    sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
1458
0
  if (crq)
1459
0
    X509_REQ_free(crq);
1460
0
  if (cert)
1461
0
    X509_free(cert);
1462
0
  if (root_cert)
1463
0
    X509_free(root_cert);
1464
0
  if (root_key)
1465
0
    EVP_PKEY_free(root_key);
1466
1467
0
  return (result);
1468
0
}
1469
1470
1471
//
1472
// 'httpCopyPeerCredentials()' - Copy the credentials associated with the peer in an encrypted connection.
1473
//
1474
1475
char *          // O - PEM-encoded X.509 certificate chain or `NULL`
1476
httpCopyPeerCredentials(http_t *http) // I - Connection to server
1477
0
{
1478
0
  char    *credentials = NULL; // Return value
1479
0
  size_t  alloc_creds = 0;  // Allocated size
1480
0
  STACK_OF(X509) *chain;    // Certificate chain
1481
1482
1483
0
  DEBUG_printf("httpCopyPeerCredentials(http=%p)", (void *)http);
1484
1485
0
  if (http && http->tls)
1486
0
  {
1487
    // Get the chain of certificates for the remote end...
1488
0
    chain = SSL_get_peer_cert_chain(http->tls);
1489
1490
0
    DEBUG_printf("1httpCopyPeerCredentials: chain=%p", (void *)chain);
1491
1492
0
    if (chain)
1493
0
    {
1494
      // Loop through the certificates, adding them to the string...
1495
0
      int i,      // Looping var
1496
0
    count;      // Number of certs
1497
1498
0
      for (i = 0, count = sk_X509_num(chain); i < count; i ++)
1499
0
      {
1500
0
  X509  *cert = sk_X509_value(chain, i);
1501
            // Current certificate
1502
0
  BIO *bio = BIO_new(BIO_s_mem());
1503
            // Memory buffer for cert
1504
1505
0
        DEBUG_printf("1httpCopyPeerCredentials: chain[%d/%d]=%p", i + 1, count, (void *)cert);
1506
1507
#ifdef DEBUG
1508
  char subjectName[256], issuerName[256];
1509
  X509_NAME_get_text_by_NID(X509_get_subject_name(cert), NID_commonName, subjectName, sizeof(subjectName));
1510
  X509_NAME_get_text_by_NID(X509_get_issuer_name(cert), NID_commonName, issuerName, sizeof(issuerName));
1511
  DEBUG_printf("1httpCopyPeerCredentials: subjectName=\"%s\", issuerName=\"%s\"", subjectName, issuerName);
1512
1513
  STACK_OF(GENERAL_NAME) *names;  // subjectAltName values
1514
  names = X509_get_ext_d2i(cert, NID_subject_alt_name, /*crit*/NULL, /*idx*/NULL);
1515
  DEBUG_printf("1httpCopyPeerCredentials: subjectAltNames=%p(%d)", (void *)names, names ? sk_GENERAL_NAME_num(names) : 0);
1516
        if (names)
1517
          GENERAL_NAMES_free(names);
1518
#endif // DEBUG
1519
1520
0
  if (bio)
1521
0
  {
1522
0
    long  bytes;      // Number of bytes
1523
0
    char  *buffer;    // Pointer to bytes
1524
1525
0
    if (PEM_write_bio_X509(bio, cert))
1526
0
    {
1527
0
      if ((bytes = BIO_get_mem_data(bio, &buffer)) > 0)
1528
0
      {
1529
        // Expand credentials string...
1530
0
        if ((credentials = realloc(credentials, alloc_creds + (size_t)bytes + 1)) != NULL)
1531
0
        {
1532
          // Copy PEM-encoded data...
1533
0
          memcpy(credentials + alloc_creds, buffer, bytes);
1534
0
          credentials[alloc_creds + (size_t)bytes] = '\0';
1535
0
          alloc_creds += (size_t)bytes;
1536
0
        }
1537
0
      }
1538
0
    }
1539
1540
0
    BIO_free(bio);
1541
1542
0
    if (!credentials)
1543
0
      break;
1544
0
  }
1545
0
      }
1546
0
    }
1547
0
  }
1548
1549
0
  DEBUG_printf("1httpCopyPeerCredentials: Returning \"%s\".", credentials);
1550
1551
0
  return (credentials);
1552
0
}
1553
1554
1555
//
1556
// '_httpCreateCredentials()' - Create credentials in the internal format.
1557
//
1558
1559
_http_tls_credentials_t *   // O - Internal credentials
1560
_httpCreateCredentials(
1561
    const char *credentials,    // I - Credentials string
1562
    const char *key)      // I - Private key string
1563
0
{
1564
0
  _http_tls_credentials_t *hcreds;  // Credentials
1565
1566
1567
0
  DEBUG_printf("_httpCreateCredentials(credentials=\"%s\", key=\"%s\")", credentials, key);
1568
1569
0
  if (!credentials || !*credentials || !key || !*key)
1570
0
    return (NULL);
1571
1572
0
  if ((hcreds = calloc(1, sizeof(_http_tls_credentials_t))) == NULL)
1573
0
    return (NULL);
1574
1575
0
  hcreds->use = 1;
1576
1577
  // Load the certificates...
1578
0
  if ((hcreds->certs = openssl_load_x509(credentials)) == NULL)
1579
0
  {
1580
0
    _httpFreeCredentials(hcreds);
1581
0
    hcreds = NULL;
1582
0
  }
1583
0
  else
1584
0
  {
1585
    // Load the private key...
1586
0
    BIO *bio;       // Basic I/O for string
1587
1588
0
    if ((bio = BIO_new_mem_buf(key, strlen(key))) == NULL)
1589
0
    {
1590
0
      _httpFreeCredentials(hcreds);
1591
0
      hcreds = NULL;
1592
0
    }
1593
1594
0
    if (!PEM_read_bio_PrivateKey(bio, &hcreds->key, NULL, NULL))
1595
0
    {
1596
0
      _httpFreeCredentials(hcreds);
1597
0
      hcreds = NULL;
1598
0
    }
1599
0
  }
1600
1601
0
  DEBUG_printf("1_httpCreateCredentials: Returning %p.", (void *)hcreds);
1602
1603
0
  return (hcreds);
1604
0
}
1605
1606
1607
//
1608
// '_httpFreeCredentials()' - Free internal credentials.
1609
//
1610
1611
void
1612
_httpFreeCredentials(
1613
    _http_tls_credentials_t *hcreds)  // I - Internal credentials
1614
0
{
1615
0
  if (!hcreds)
1616
0
    return;
1617
1618
0
  if (hcreds->use)
1619
0
    hcreds->use --;
1620
1621
0
  if (hcreds->use)
1622
0
    return;
1623
1624
0
  sk_X509_free(hcreds->certs);
1625
0
  free(hcreds);
1626
0
}
1627
1628
1629
//
1630
// 'httpGetSecurity()' - Get the TLS version and cipher suite used by a connection.
1631
//
1632
// This function gets the TLS version and cipher suite being used by a
1633
// connection, if any.  The string is copied to "buffer" and is of the form
1634
// "TLS/major.minor CipherSuite".  If not encrypted, the buffer is cleared to
1635
// the empty string.
1636
//
1637
// @since CUPS 2.5@
1638
//
1639
1640
const char *        // O - Security information or `NULL` if not encrypted
1641
httpGetSecurity(http_t *http,   // I - HTTP connection
1642
                char   *buffer,   // I - String buffer
1643
                size_t bufsize)   // I - Size of buffer
1644
0
{
1645
0
  const char  *cipherName;    // Cipher suite name
1646
1647
1648
  // Range check input...
1649
0
  if (buffer)
1650
0
    *buffer = '\0';
1651
1652
0
  if (!http || !http->tls || !buffer || bufsize < 16)
1653
0
    return (NULL);
1654
1655
  // Record the TLS version and cipher suite...
1656
0
  cipherName = SSL_get_cipher_name(http->tls);
1657
1658
0
  switch (SSL_version(http->tls))
1659
0
  {
1660
0
    default :
1661
0
        snprintf(buffer, bufsize, "TLS/?.? %s", cipherName);
1662
0
        break;
1663
1664
0
    case TLS1_VERSION :
1665
0
        snprintf(buffer, bufsize, "TLS/1.0 %s", cipherName);
1666
0
        break;
1667
1668
0
    case TLS1_1_VERSION :
1669
0
        snprintf(buffer, bufsize, "TLS/1.1 %s", cipherName);
1670
0
        break;
1671
1672
0
    case TLS1_2_VERSION :
1673
0
        snprintf(buffer, bufsize, "TLS/1.2 %s", cipherName);
1674
0
        break;
1675
1676
0
#  ifdef TLS1_3_VERSION
1677
0
    case TLS1_3_VERSION :
1678
0
        snprintf(buffer, bufsize, "TLS/1.3 %s", cipherName);
1679
0
        break;
1680
0
#  endif // TLS1_3_VERSION
1681
0
  }
1682
1683
0
  return (buffer);
1684
0
}
1685
1686
1687
//
1688
// '_httpTLSInitialize()' - Initialize the TLS stack.
1689
//
1690
1691
void
1692
_httpTLSInitialize(void)
1693
0
{
1694
  // OpenSSL no longer requires explicit initialization...
1695
0
}
1696
1697
1698
//
1699
// '_httpTLSPending()' - Return the number of pending TLS-encrypted bytes.
1700
//
1701
1702
size_t          // O - Bytes available
1703
_httpTLSPending(http_t *http)   // I - HTTP connection
1704
0
{
1705
0
  return ((size_t)SSL_pending(http->tls));
1706
0
}
1707
1708
1709
//
1710
// '_httpTLSRead()' - Read from a SSL/TLS connection.
1711
//
1712
1713
int         // O - Bytes read
1714
_httpTLSRead(http_t *http,    // I - Connection to server
1715
       char   *buf,   // I - Buffer to store data
1716
       int    len)    // I - Length of buffer
1717
0
{
1718
0
  int bytes = SSL_read((SSL *)(http->tls), buf, len);
1719
          // Bytes read
1720
1721
0
  DEBUG_printf("7_httpTLSRead(http=%p, buf=%p, len=%d) returning %d", (void *)http, (void *)buf, len, bytes);
1722
1723
0
  if (bytes > 0)
1724
0
    return (bytes);
1725
1726
  // For now, make difference only for error after which we can retry, EPIPE otherwise...
1727
0
  if (SSL_get_error(http->tls, bytes) == SSL_ERROR_WANT_READ)
1728
0
    errno = EAGAIN;
1729
0
  else
1730
0
    errno = EPIPE;
1731
1732
0
  return (-1);
1733
0
}
1734
1735
1736
//
1737
// '_httpTLSStart()' - Set up SSL/TLS support on a connection.
1738
//
1739
1740
bool          // O - `true` on success, `false` on failure
1741
_httpTLSStart(http_t *http)   // I - Connection to server
1742
0
{
1743
0
  const char  *keypath;   // Certificate store path
1744
0
  BIO   *bio;     // Basic input/output context
1745
0
  SSL_CTX *context;   // Encryption context
1746
0
  char    hostname[256],    // Hostname
1747
0
    cipherlist[256];  // List of cipher suites
1748
0
  unsigned long error;      // Error code, if any
1749
0
  _cups_globals_t *cg = _cupsGlobals(); // Per-thread globals
1750
0
  static const uint16_t versions[] =  // SSL/TLS versions
1751
0
  {
1752
0
    TLS1_VERSION,     // No more SSL support in OpenSSL
1753
0
    TLS1_VERSION,     // TLS/1.0
1754
0
    TLS1_1_VERSION,     // TLS/1.1
1755
0
    TLS1_2_VERSION,     // TLS/1.2
1756
0
#ifdef TLS1_3_VERSION
1757
0
    TLS1_3_VERSION,     // TLS/1.3
1758
    TLS1_3_VERSION      // TLS/1.3 (max)
1759
#else
1760
    TLS1_2_VERSION,     // TLS/1.2
1761
    TLS1_2_VERSION      // TLS/1.2 (max)
1762
#endif // TLS1_3_VERSION
1763
0
  };
1764
1765
1766
0
  DEBUG_printf("3_httpTLSStart(http=%p)", (void *)http);
1767
1768
0
  if (!cg->client_conf_loaded)
1769
0
  {
1770
0
    DEBUG_puts("4_httpTLSStart: Setting defaults.");
1771
0
    _cupsSetDefaults();
1772
0
    DEBUG_printf("4_httpTLSStart: tls_options=%x", tls_options);
1773
0
  }
1774
1775
0
  cupsMutexLock(&tls_mutex);
1776
0
  keypath = tls_keypath;
1777
0
  cupsMutexUnlock(&tls_mutex);
1778
1779
0
  if (http->mode == _HTTP_MODE_SERVER && !keypath)
1780
0
  {
1781
0
    DEBUG_puts("4_httpTLSStart: cupsSetServerCredentials not called.");
1782
0
    http->error  = errno = EINVAL;
1783
0
    http->status = HTTP_STATUS_ERROR;
1784
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Server credentials not set."), 1);
1785
1786
0
    return (false);
1787
0
  }
1788
1789
0
  if (http->mode == _HTTP_MODE_CLIENT)
1790
0
  {
1791
    // Negotiate a TLS connection as a client...
1792
0
    context = SSL_CTX_new(TLS_client_method());
1793
0
    if (http->tls_credentials)
1794
0
    {
1795
0
      int i,      // Looping var
1796
0
    count;      // Number of certificates
1797
1798
0
      DEBUG_puts("4_httpTLSStart: Using client certificate.");
1799
0
      SSL_CTX_use_certificate(context, sk_X509_value(http->tls_credentials->certs, 0));
1800
0
      SSL_CTX_use_PrivateKey(context, http->tls_credentials->key);
1801
1802
0
      count = sk_X509_num(http->tls_credentials->certs);
1803
0
      for (i = 1; i < count; i ++)
1804
0
        SSL_CTX_add_extra_chain_cert(context, sk_X509_value(http->tls_credentials->certs, i));
1805
0
    }
1806
0
  }
1807
0
  else
1808
0
  {
1809
    // Negotiate a TLS connection as a server
1810
0
    char  crtfile[1024],    // Certificate file
1811
0
    keyfile[1024];    // Private key file
1812
0
    const char  *cn = NULL,   // Common name to lookup
1813
0
    *cnptr;     // Pointer into common name
1814
0
    bool  have_creds = false;  // Have credentials?
1815
1816
0
    context = SSL_CTX_new(TLS_server_method());
1817
1818
    // Find the TLS certificate...
1819
0
    cupsMutexLock(&tls_mutex);
1820
1821
0
    if (!tls_common_name)
1822
0
    {
1823
0
      cupsMutexUnlock(&tls_mutex);
1824
1825
0
      if (http->fields[HTTP_FIELD_HOST])
1826
0
      {
1827
  // Use hostname for TLS upgrade...
1828
0
  cupsCopyString(hostname, http->fields[HTTP_FIELD_HOST], sizeof(hostname));
1829
0
      }
1830
0
      else
1831
0
      {
1832
  // Resolve hostname from connection address...
1833
0
  http_addr_t addr;   // Connection address
1834
0
  socklen_t addrlen;  // Length of address
1835
1836
0
  addrlen = sizeof(addr);
1837
0
  if (getsockname(http->fd, (struct sockaddr *)&addr, &addrlen))
1838
0
  {
1839
    // Unable to get local socket address so use default...
1840
0
    DEBUG_printf("4_httpTLSStart: Unable to get socket address: %s", strerror(errno));
1841
0
    hostname[0] = '\0';
1842
0
  }
1843
0
  else if (httpAddrIsLocalhost(&addr))
1844
0
  {
1845
    // Local access top use default...
1846
0
    hostname[0] = '\0';
1847
0
  }
1848
0
  else
1849
0
  {
1850
    // Lookup the socket address...
1851
0
    httpAddrLookup(&addr, hostname, sizeof(hostname));
1852
0
    DEBUG_printf("4_httpTLSStart: Resolved socket address to \"%s\".", hostname);
1853
0
  }
1854
0
      }
1855
1856
0
      if (isdigit(hostname[0] & 255) || hostname[0] == '[')
1857
0
  hostname[0] = '\0';   // Don't allow numeric addresses
1858
1859
0
      if (hostname[0])
1860
0
  cn = hostname;
1861
1862
0
      cupsMutexLock(&tls_mutex);
1863
0
    }
1864
1865
0
    if (!cn)
1866
0
      cn = tls_common_name;
1867
1868
0
    DEBUG_printf("4_httpTLSStart: Using common name \"%s\"...", cn);
1869
1870
0
    if (cn)
1871
0
    {
1872
      // First look in the CUPS keystore...
1873
0
      http_make_path(crtfile, sizeof(crtfile), tls_keypath, cn, "crt");
1874
0
      http_make_path(keyfile, sizeof(keyfile), tls_keypath, cn, "key");
1875
1876
0
      if (access(crtfile, R_OK) || access(keyfile, R_OK))
1877
0
      {
1878
        // No CUPS-managed certs, look for CA certs...
1879
0
        char cacrtfile[1024], cakeyfile[1024];  // CA cert files
1880
1881
0
        snprintf(cacrtfile, sizeof(cacrtfile), "/etc/letsencrypt/live/%s/fullchain.pem", cn);
1882
0
        snprintf(cakeyfile, sizeof(cakeyfile), "/etc/letsencrypt/live/%s/privkey.pem", cn);
1883
1884
0
        if ((access(cacrtfile, R_OK) || access(cakeyfile, R_OK)) && (cnptr = strchr(cn, '.')) != NULL)
1885
0
        {
1886
          // Try just domain name...
1887
0
          cnptr ++;
1888
0
          if (strchr(cnptr, '.'))
1889
0
          {
1890
0
            snprintf(cacrtfile, sizeof(cacrtfile), "/etc/letsencrypt/live/%s/fullchain.pem", cnptr);
1891
0
            snprintf(cakeyfile, sizeof(cakeyfile), "/etc/letsencrypt/live/%s/privkey.pem", cnptr);
1892
0
          }
1893
0
        }
1894
1895
0
        if (!access(cacrtfile, R_OK) && !access(cakeyfile, R_OK))
1896
0
        {
1897
          // Use the CA certs...
1898
0
          cupsCopyString(crtfile, cacrtfile, sizeof(crtfile));
1899
0
          cupsCopyString(keyfile, cakeyfile, sizeof(keyfile));
1900
0
        }
1901
0
      }
1902
1903
0
      have_creds = !access(crtfile, R_OK) && !access(keyfile, R_OK);
1904
0
    }
1905
1906
0
    if (!have_creds && tls_auto_create && cn)
1907
0
    {
1908
0
      DEBUG_printf("4_httpTLSStart: Auto-create credentials for \"%s\".", cn);
1909
1910
0
      if (!cupsCreateCredentials(tls_keypath, false, CUPS_CREDPURPOSE_SERVER_AUTH, CUPS_CREDTYPE_DEFAULT, CUPS_CREDUSAGE_DEFAULT_TLS, NULL, NULL, NULL, NULL, NULL, cn, NULL, 0, NULL, NULL, time(NULL) + 3650 * 86400))
1911
0
      {
1912
0
  DEBUG_printf("4_httpTLSStart: cupsCreateCredentials failed: %s", cupsGetErrorString());
1913
0
  http->error  = errno = EINVAL;
1914
0
  http->status = HTTP_STATUS_ERROR;
1915
0
  SSL_CTX_free(context);
1916
0
        cupsMutexUnlock(&tls_mutex);
1917
1918
0
  return (false);
1919
0
      }
1920
0
    }
1921
1922
0
    cupsMutexUnlock(&tls_mutex);
1923
1924
0
    DEBUG_printf("4_httpTLSStart: Using private key file '%s'.", keyfile);
1925
0
    DEBUG_printf("4_httpTLSStart: Using certificate file '%s'.", crtfile);
1926
1927
0
    if (!SSL_CTX_use_PrivateKey_file(context, keyfile, SSL_FILETYPE_PEM) || !SSL_CTX_use_certificate_chain_file(context, crtfile))
1928
0
    {
1929
      // Unable to load private key or certificate...
1930
0
      DEBUG_puts("4_httpTLSStart: Unable to use private key or certificate chain file.");
1931
0
      if ((error = ERR_get_error()) != 0)
1932
0
        _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, ERR_error_string(error, NULL), 0);
1933
1934
0
      http->status = HTTP_STATUS_ERROR;
1935
0
      http->error  = EIO;
1936
1937
0
      SSL_CTX_free(context);
1938
1939
0
      return (false);
1940
0
    }
1941
0
  }
1942
1943
  // Set TLS options...
1944
0
  cupsCopyString(cipherlist, "HIGH:!DH:+DHE", sizeof(cipherlist));
1945
0
  if ((tls_options & _HTTP_TLS_ALLOW_RC4) && http->mode == _HTTP_MODE_CLIENT)
1946
0
    cupsConcatString(cipherlist, ":+RC4", sizeof(cipherlist));
1947
0
  else
1948
0
    cupsConcatString(cipherlist, ":!RC4", sizeof(cipherlist));
1949
0
  if (tls_options & _HTTP_TLS_DENY_CBC)
1950
0
    cupsConcatString(cipherlist, ":!SHA1:!SHA256:!SHA384", sizeof(cipherlist));
1951
0
  cupsConcatString(cipherlist, ":@STRENGTH", sizeof(cipherlist));
1952
1953
0
  DEBUG_printf("4_httpTLSStart: cipherlist='%s', tls_min_version=%d, tls_max_version=%d", cipherlist, tls_min_version, tls_max_version);
1954
1955
0
  SSL_CTX_set_min_proto_version(context, versions[tls_min_version]);
1956
0
  SSL_CTX_set_max_proto_version(context, versions[tls_max_version]);
1957
0
  SSL_CTX_set_cipher_list(context, cipherlist);
1958
1959
  // Setup a TLS session
1960
0
  cupsMutexLock(&tls_mutex);
1961
0
  if (!tls_bio_method)
1962
0
  {
1963
0
    tls_bio_method = BIO_meth_new(BIO_get_new_index(), "http");
1964
0
    BIO_meth_set_ctrl(tls_bio_method, http_bio_ctrl);
1965
0
    BIO_meth_set_create(tls_bio_method, http_bio_new);
1966
0
    BIO_meth_set_destroy(tls_bio_method, http_bio_free);
1967
0
    BIO_meth_set_read(tls_bio_method, http_bio_read);
1968
0
    BIO_meth_set_puts(tls_bio_method, http_bio_puts);
1969
0
    BIO_meth_set_write(tls_bio_method, http_bio_write);
1970
0
  }
1971
1972
0
  bio = BIO_new(tls_bio_method);
1973
0
  cupsMutexUnlock(&tls_mutex);
1974
1975
0
  BIO_ctrl(bio, BIO_C_SET_FILE_PTR, 0, (char *)http);
1976
1977
0
  http->tls = SSL_new(context);
1978
0
  SSL_set_bio(http->tls, bio, bio);
1979
1980
0
  if (http->mode == _HTTP_MODE_CLIENT)
1981
0
  {
1982
    // Negotiate as a client...
1983
0
    DEBUG_printf("4_httpTLSStart: Setting server name TLS extension to '%s'...", http->hostname);
1984
0
    SSL_set_tlsext_host_name(http->tls, http->hostname);
1985
1986
0
    DEBUG_puts("4_httpTLSStart: Calling SSL_connect...");
1987
0
    if (SSL_connect(http->tls) < 1)
1988
0
    {
1989
      // Failed
1990
0
      if ((error = ERR_get_error()) != 0)
1991
0
        _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, ERR_error_string(error, NULL), 0);
1992
1993
0
      http->status = HTTP_STATUS_ERROR;
1994
0
      http->error  = EPIPE;
1995
1996
0
      SSL_CTX_free(context);
1997
1998
0
      SSL_free(http->tls);
1999
0
      http->tls = NULL;
2000
2001
0
      DEBUG_printf("4_httpTLSStart: Returning false (%s)", ERR_error_string(error, NULL));
2002
2003
0
      return (false);
2004
0
    }
2005
0
  }
2006
0
  else
2007
0
  {
2008
    // Negotiate as a server...
2009
0
    DEBUG_puts("4_httpTLSStart: Calling SSL_accept...");
2010
0
    if (SSL_accept(http->tls) < 1)
2011
0
    {
2012
      // Failed
2013
0
      if ((error = ERR_get_error()) != 0)
2014
0
        _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, ERR_error_string(error, NULL), 0);
2015
2016
0
      http->status = HTTP_STATUS_ERROR;
2017
0
      http->error  = EPIPE;
2018
2019
0
      SSL_CTX_free(context);
2020
2021
0
      SSL_free(http->tls);
2022
0
      http->tls = NULL;
2023
2024
0
      DEBUG_printf("4_httpTLSStart: Returning false (%s)", ERR_error_string(error, NULL));
2025
2026
0
      return (false);
2027
0
    }
2028
0
  }
2029
2030
0
  DEBUG_puts("4_httpTLSStart: Returning true.");
2031
2032
0
  return (true);
2033
0
}
2034
2035
2036
//
2037
// '_httpTLSStop()' - Shut down SSL/TLS on a connection.
2038
//
2039
2040
void
2041
_httpTLSStop(http_t *http)    // I - Connection to server
2042
0
{
2043
0
  SSL_CTX *context;   // Context for encryption
2044
2045
2046
0
  context = SSL_get_SSL_CTX(http->tls);
2047
2048
0
  SSL_shutdown(http->tls);
2049
0
  SSL_CTX_free(context);
2050
0
  SSL_free(http->tls);
2051
2052
0
  http->tls = NULL;
2053
0
}
2054
2055
2056
//
2057
// '_httpTLSWrite()' - Write to a SSL/TLS connection.
2058
//
2059
2060
int         // O - Bytes written
2061
_httpTLSWrite(http_t     *http,   // I - Connection to server
2062
        const char *buf,    // I - Buffer holding data
2063
        int        len)   // I - Length of buffer
2064
0
{
2065
0
  int bytes;
2066
2067
0
  bytes = SSL_write(http->tls, buf, len);
2068
2069
0
  if (bytes > 0)
2070
0
    return (bytes);
2071
2072
  // For now, make difference only for error after which we can retry, EPIPE otherwise...
2073
0
  if (SSL_get_error(http->tls, bytes) == SSL_ERROR_WANT_WRITE)
2074
0
    errno = EAGAIN;
2075
0
  else
2076
0
    errno = EPIPE;
2077
2078
0
  return (-1);
2079
0
}
2080
2081
2082
//
2083
// '_httpUseCredentials()' - Increment the use count for internal credentials.
2084
//
2085
2086
_http_tls_credentials_t *   // O - Internal credentials
2087
_httpUseCredentials(
2088
    _http_tls_credentials_t *hcreds)  // I - Internal credentials
2089
0
{
2090
0
  if (hcreds)
2091
0
    hcreds->use ++;
2092
2093
0
  return (hcreds);
2094
0
}
2095
2096
2097
//
2098
// 'http_bio_ctrl()' - Control the HTTP connection.
2099
//
2100
2101
static long       // O - Result/data
2102
http_bio_ctrl(BIO  *h,      // I - BIO data
2103
              int  cmd,     // I - Control command
2104
        long arg1,    // I - First argument
2105
        void *arg2)   // I - Second argument
2106
0
{
2107
0
  DEBUG_printf("8http_bio_ctl(h=%p, cmd=%d, arg1=%ld, arg2=%p)", (void *)h, cmd, arg1, arg2);
2108
2109
0
  (void)arg1;
2110
2111
0
  switch (cmd)
2112
0
  {
2113
0
    default :
2114
0
        return (0);
2115
2116
0
    case BIO_CTRL_RESET :
2117
0
        BIO_set_data(h, NULL);
2118
0
  return (0);
2119
2120
0
    case BIO_C_SET_FILE_PTR :
2121
0
        BIO_set_data(h, arg2);
2122
0
        BIO_set_init(h, 1);
2123
0
  return (1);
2124
2125
0
    case BIO_C_GET_FILE_PTR :
2126
0
        if (arg2)
2127
0
  {
2128
0
    *((void **)arg2) = BIO_get_data(h);
2129
0
    return (1);
2130
0
  }
2131
0
  else
2132
0
    return (0);
2133
2134
0
    case BIO_CTRL_DUP :
2135
0
    case BIO_CTRL_FLUSH :
2136
0
        return (1);
2137
0
  }
2138
0
}
2139
2140
2141
//
2142
// 'http_bio_free()' - Free OpenSSL data.
2143
//
2144
2145
static int        // O - 1 on success, 0 on failure
2146
http_bio_free(BIO *h)     // I - BIO data
2147
0
{
2148
0
  DEBUG_printf("8http_bio_free(h=%p)", (void *)h);
2149
2150
0
  if (!h)
2151
0
    return (0);
2152
2153
0
  if (BIO_get_shutdown(h))
2154
0
    BIO_set_init(h, 0);
2155
2156
0
  return (1);
2157
0
}
2158
2159
2160
//
2161
// 'http_bio_new()' - Initialize an OpenSSL BIO structure.
2162
//
2163
2164
static int        // O - 1 on success, 0 on failure
2165
http_bio_new(BIO *h)      // I - BIO data
2166
0
{
2167
0
  DEBUG_printf("8http_bio_new(h=%p)", (void *)h);
2168
2169
0
  if (!h)
2170
0
    return (0);
2171
2172
0
  BIO_set_init(h, 0);
2173
0
  BIO_set_data(h, NULL);
2174
2175
0
  return (1);
2176
0
}
2177
2178
2179
//
2180
// 'http_bio_puts()' - Send a string for OpenSSL.
2181
//
2182
2183
static int        // O - Bytes written
2184
http_bio_puts(BIO        *h,    // I - BIO data
2185
              const char *str)    // I - String to write
2186
0
{
2187
0
  DEBUG_printf("8http_bio_puts(h=%p, str=\"%s\")", (void *)h, str);
2188
2189
#ifdef WIN32
2190
  return (send(((http_t *)BIO_get_data(h))->fd, str, (int)strlen(str), 0));
2191
#else
2192
0
  return ((int)send(((http_t *)BIO_get_data(h))->fd, str, strlen(str), 0));
2193
0
#endif // WIN32
2194
0
}
2195
2196
2197
//
2198
// 'http_bio_read()' - Read data for OpenSSL.
2199
//
2200
2201
static int        // O - Bytes read
2202
http_bio_read(BIO  *h,      // I - BIO data
2203
              char *buf,    // I - Buffer
2204
        int  size)    // I - Number of bytes to read
2205
0
{
2206
0
  http_t  *http;      // HTTP connection
2207
0
  int   bytes;      // Bytes read
2208
2209
2210
0
  DEBUG_printf("8http_bio_read(h=%p, buf=%p, size=%d)", (void *)h, (void *)buf, size);
2211
2212
0
  http = (http_t *)BIO_get_data(h);
2213
0
  DEBUG_printf("9http_bio_read: http=%p", (void *)http);
2214
2215
0
  if (!http->blocking || http->timeout_value > 0.0)
2216
0
  {
2217
    // Make sure we have data before we read...
2218
0
    while (!_httpWait(http, http->wait_value, 0))
2219
0
    {
2220
0
      if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
2221
0
  continue;
2222
2223
#ifdef WIN32
2224
      http->error = WSAETIMEDOUT;
2225
#else
2226
0
      http->error = ETIMEDOUT;
2227
0
#endif // WIN32
2228
2229
0
      DEBUG_puts("9http_bio_read: Timeout, returning -1.");
2230
0
      return (-1);
2231
0
    }
2232
0
  }
2233
2234
0
  bytes = (int)recv(http->fd, buf, (size_t)size, 0);
2235
0
  DEBUG_printf("9http_bio_read: Returning %d.", bytes);
2236
2237
0
  return (bytes);
2238
0
}
2239
2240
2241
//
2242
// 'http_bio_write()' - Write data for OpenSSL.
2243
//
2244
2245
static int        // O - Bytes written
2246
http_bio_write(BIO        *h,   // I - BIO data
2247
               const char *buf,   // I - Buffer to write
2248
         int        num)    // I - Number of bytes to write
2249
0
{
2250
0
  int bytes;        // Bytes written
2251
2252
2253
0
  DEBUG_printf("8http_bio_write(h=%p, buf=%p, num=%d)", (void *)h, (void *)buf, num);
2254
2255
0
  bytes = (int)send(((http_t *)BIO_get_data(h))->fd, buf, (size_t)num, 0);
2256
2257
0
  DEBUG_printf("9http_bio_write: Returning %d.", bytes);
2258
0
  return (bytes);
2259
0
}
2260
2261
2262
//
2263
// 'openssl_add_ext()' - Add an extension.
2264
//
2265
2266
static bool       // O - `true` on success, `false` on error
2267
openssl_add_ext(
2268
    STACK_OF(X509_EXTENSION) *exts, // I - Stack of extensions
2269
    int                      nid, // I - Extension ID
2270
    const char               *value)  // I - Value
2271
0
{
2272
0
  X509_EXTENSION *ext = NULL;   // Extension
2273
2274
2275
0
  DEBUG_printf("3openssl_add_ext(exts=%p, nid=%d, value=\"%s\")", (void *)exts, nid, value);
2276
2277
  // Create and add the extension...
2278
0
  if ((ext = X509V3_EXT_conf_nid(/*conf*/NULL, /*ctx*/NULL, nid, value)) == NULL)
2279
0
  {
2280
0
    DEBUG_puts("4openssl_add_ext: Unable to create extension, returning false.");
2281
0
    return (false);
2282
0
  }
2283
2284
0
  sk_X509_EXTENSION_push(exts, ext);
2285
2286
0
  return (true);
2287
0
}
2288
2289
2290
//
2291
// 'openssl_create_key()' - Create a suitable key pair for a certificate/signing request.
2292
//
2293
2294
static EVP_PKEY *     // O - Key pair
2295
openssl_create_key(
2296
    cups_credtype_t type)   // I - Type of key
2297
0
{
2298
0
  EVP_PKEY  *pkey;      // Key pair
2299
0
  EVP_PKEY_CTX  *ctx;     // Key generation context
2300
0
  int   algid;      // Algorithm NID
2301
0
  int   bits = 0;   // Bits
2302
0
  int   curveid = 0;    // Curve NID
2303
2304
2305
0
  switch (type)
2306
0
  {
2307
0
    case CUPS_CREDTYPE_ECDSA_P256_SHA256 :
2308
0
        algid   = EVP_PKEY_EC;
2309
0
        curveid = NID_secp256k1;
2310
0
  break;
2311
2312
0
    case CUPS_CREDTYPE_ECDSA_P384_SHA256 :
2313
0
        algid   = EVP_PKEY_EC;
2314
0
        curveid = NID_secp384r1;
2315
0
  break;
2316
2317
0
    case CUPS_CREDTYPE_ECDSA_P521_SHA256 :
2318
0
        algid   = EVP_PKEY_EC;
2319
0
        curveid = NID_secp521r1;
2320
0
  break;
2321
2322
0
    case CUPS_CREDTYPE_RSA_2048_SHA256 :
2323
0
        algid = EVP_PKEY_RSA;
2324
0
        bits  = 2048;
2325
0
  break;
2326
2327
0
    default :
2328
0
    case CUPS_CREDTYPE_RSA_3072_SHA256 :
2329
0
        algid = EVP_PKEY_RSA;
2330
0
        bits  = 3072;
2331
0
  break;
2332
2333
0
    case CUPS_CREDTYPE_RSA_4096_SHA256 :
2334
0
        algid = EVP_PKEY_RSA;
2335
0
        bits  = 4096;
2336
0
  break;
2337
0
  }
2338
2339
0
  pkey = NULL;
2340
2341
0
  if ((ctx = EVP_PKEY_CTX_new_id(algid, NULL)) == NULL)
2342
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create private key context."), 1);
2343
0
  else if (EVP_PKEY_keygen_init(ctx) <= 0)
2344
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to initialize private key context."), 1);
2345
0
  else if (bits && EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) <= 0)
2346
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to configure private key context."), 1);
2347
0
  else if (curveid && EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, curveid) <= 0)
2348
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to configure private key context."), 1);
2349
0
  else if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
2350
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create private key."), 1);
2351
2352
0
  EVP_PKEY_CTX_free(ctx);
2353
2354
0
  return (pkey);
2355
0
}
2356
2357
2358
//
2359
// 'openssl_create_name()' - Create an X.509 name value for a certificate/signing request.
2360
//
2361
2362
static X509_NAME *      // O - X.509 name value
2363
openssl_create_name(
2364
    const char      *organization,  // I - Organization or `NULL` to use common name
2365
    const char      *org_unit,    // I - Organizational unit or `NULL` for none
2366
    const char      *locality,    // I - City/town or `NULL` for "Unknown"
2367
    const char      *state_province,  // I - State/province or `NULL` for "Unknown"
2368
    const char      *country,   // I - Country or `NULL` for locale-based default
2369
    const char      *common_name, // I - Common name
2370
    const char      *email)   // I - Email address or `NULL` for none
2371
0
{
2372
0
  X509_NAME *name;      // Subject/issuer name
2373
0
  cups_lang_t *language;    // Default language info
2374
0
  const char  *langname;    // Language name
2375
2376
2377
0
  language = cupsLangDefault();
2378
0
  langname = language->language;
2379
0
  name     = X509_NAME_new();
2380
0
  if (country)
2381
0
    X509_NAME_add_entry_by_txt(name, SN_countryName, MBSTRING_ASC, (unsigned char *)country, -1, -1, 0);
2382
0
  else if (strlen(langname) == 5)
2383
0
    X509_NAME_add_entry_by_txt(name, SN_countryName, MBSTRING_ASC, (unsigned char *)langname + 3, -1, -1, 0);
2384
0
  else
2385
0
    X509_NAME_add_entry_by_txt(name, SN_countryName, MBSTRING_ASC, (unsigned char *)"US", -1, -1, 0);
2386
0
  X509_NAME_add_entry_by_txt(name, SN_commonName, MBSTRING_ASC, (unsigned char *)common_name, -1, -1, 0);
2387
0
  X509_NAME_add_entry_by_txt(name, SN_organizationName, MBSTRING_ASC, (unsigned char *)(organization ? organization : common_name), -1, -1, 0);
2388
0
  X509_NAME_add_entry_by_txt(name, SN_organizationalUnitName, MBSTRING_ASC, (unsigned char *)(org_unit ? org_unit : ""), -1, -1, 0);
2389
0
  X509_NAME_add_entry_by_txt(name, SN_stateOrProvinceName, MBSTRING_ASC, (unsigned char *)(state_province ? state_province : "Unknown"), -1, -1, 0);
2390
0
  X509_NAME_add_entry_by_txt(name, SN_localityName, MBSTRING_ASC, (unsigned char *)(locality ? locality : "Unknown"), -1, -1, 0);
2391
0
  if (email && *email)
2392
0
    X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_ASC, (unsigned char *)email, -1, -1, 0);
2393
2394
0
  return (name);
2395
0
}
2396
2397
2398
//
2399
// 'openssl_create_san()' - Create a list of subjectAltName values for a certificate/signing request.
2400
//
2401
2402
static X509_EXTENSION *     // O - Extension
2403
openssl_create_san(
2404
    const char         *common_name,  // I - Common name
2405
    size_t             num_alt_names, // I - Number of alternate names
2406
    const char * const *alt_names)  // I - List of alternate names
2407
0
{
2408
0
  char    temp[2048],   // Temporary string
2409
0
    *tempptr;   // Pointer into temporary string
2410
0
  size_t  i;      // Looping var
2411
2412
2413
  // Add the common name
2414
0
  snprintf(temp, sizeof(temp), "DNS:%s", common_name);
2415
0
  tempptr = temp + strlen(temp);
2416
2417
0
  if (strstr(common_name, ".local") == NULL)
2418
0
  {
2419
    // Add common_name.local to the list, too...
2420
0
    char  localname[256],   // hostname.local
2421
0
    *localptr;    // Pointer into localname
2422
2423
0
    cupsCopyString(localname, common_name, sizeof(localname));
2424
0
    if ((localptr = strchr(localname, '.')) != NULL)
2425
0
      *localptr = '\0';
2426
2427
0
    snprintf(tempptr, sizeof(temp) - (size_t)(tempptr - temp), ",DNS:%s.local", localname);
2428
0
    tempptr += strlen(tempptr);
2429
0
  }
2430
2431
  // Add any alternate names...
2432
0
  for (i = 0; i < num_alt_names; i ++)
2433
0
  {
2434
0
    if (strcmp(alt_names[i], "localhost"))
2435
0
    {
2436
0
      snprintf(tempptr, sizeof(temp) - (size_t)(tempptr - temp), ",DNS:%s", alt_names[i]);
2437
0
      tempptr += strlen(tempptr);
2438
0
    }
2439
0
  }
2440
2441
  // Return the stack
2442
0
  return (X509V3_EXT_conf_nid(/*conf*/NULL, /*ctx*/NULL, NID_subject_alt_name, temp));
2443
0
}
2444
2445
2446
//
2447
// 'openssl_get_date()' - Get the notBefore or notAfter date of a certificate.
2448
//
2449
2450
static time_t       // O - UNIX time in seconds
2451
openssl_get_date(X509 *cert,    // I - Certificate
2452
                 int  which)    // I - 0 for notBefore, 1 for notAfter
2453
0
{
2454
0
  struct tm exptm;      // Expiration date components
2455
2456
2457
0
  if (which)
2458
0
    ASN1_TIME_to_tm(X509_get0_notAfter(cert), &exptm);
2459
0
  else
2460
0
    ASN1_TIME_to_tm(X509_get0_notBefore(cert), &exptm);
2461
2462
0
  return (mktime(&exptm));
2463
0
}
2464
2465
2466
#if 0
2467
//
2468
// 'openssl_load_crl()' - Load the certificate revocation list, if any.
2469
//
2470
2471
static void
2472
openssl_load_crl(void)
2473
{
2474
  cupsMutexLock(&tls_mutex);
2475
2476
  if (!openssl_x509_crl_init(&tls_crl))
2477
  {
2478
    cups_file_t   *fp;    // CRL file
2479
    char    filename[1024], // site.crl
2480
      line[256];  // Base64-encoded line
2481
    unsigned char *data = NULL; // Buffer for cert data
2482
    size_t    alloc_data = 0, // Bytes allocated
2483
      num_data = 0; // Bytes used
2484
    int     decoded;  // Bytes decoded
2485
    openssl_datum_t datum;    // Data record
2486
2487
2488
    http_make_path(filename, sizeof(filename), CUPS_SERVERROOT, "site", "crl");
2489
2490
    if ((fp = cupsFileOpen(filename, "r")) != NULL)
2491
    {
2492
      while (cupsFileGets(fp, line, sizeof(line)))
2493
      {
2494
  if (!strcmp(line, "-----BEGIN X509 CRL-----"))
2495
  {
2496
    if (num_data)
2497
    {
2498
     /*
2499
      * Missing END X509 CRL...
2500
      */
2501
2502
      break;
2503
    }
2504
  }
2505
  else if (!strcmp(line, "-----END X509 CRL-----"))
2506
  {
2507
    if (!num_data)
2508
    {
2509
     /*
2510
      * Missing data...
2511
      */
2512
2513
      break;
2514
    }
2515
2516
          datum.data = data;
2517
    datum.size = num_data;
2518
2519
    openssl_x509_crl_import(tls_crl, &datum, GNUTLS_X509_FMT_PEM);
2520
2521
    num_data = 0;
2522
  }
2523
  else
2524
  {
2525
    if (alloc_data == 0)
2526
    {
2527
      data       = malloc(2048);
2528
      alloc_data = 2048;
2529
2530
      if (!data)
2531
        break;
2532
    }
2533
    else if ((num_data + strlen(line)) >= alloc_data)
2534
    {
2535
      unsigned char *tdata = realloc(data, alloc_data + 1024);
2536
              // Expanded buffer
2537
2538
      if (!tdata)
2539
        break;
2540
2541
      data       = tdata;
2542
      alloc_data += 1024;
2543
    }
2544
2545
    decoded = alloc_data - num_data;
2546
    httpDecode64((char *)data + num_data, &decoded, line, NULL);
2547
    num_data += (size_t)decoded;
2548
  }
2549
      }
2550
2551
      cupsFileClose(fp);
2552
2553
      if (data)
2554
  free(data);
2555
    }
2556
  }
2557
2558
  cupsMutexUnlock(&tls_mutex);
2559
}
2560
#endif // 0
2561
2562
2563
//
2564
// 'openssl_load_x509()' - Load a stack of X.509 certificates.
2565
//
2566
2567
static STACK_OF(X509) *     // O - Stack of X.509 certificates
2568
openssl_load_x509(
2569
    const char *credentials)    // I - Credentials string
2570
0
{
2571
0
  STACK_OF(X509)  *certs = NULL; // Certificate chain
2572
0
  X509      *cert = NULL; // Current certificate
2573
0
  BIO     *bio;   // Basic I/O for string
2574
2575
2576
  // Range check input...
2577
0
  if (!credentials || !*credentials)
2578
0
    return (NULL);
2579
2580
  // Make a BIO memory buffer for the string...
2581
0
  if ((bio = BIO_new_mem_buf(credentials, strlen(credentials))) == NULL)
2582
0
    return (NULL);
2583
2584
  // Read all the X509 certificates from the string...
2585
0
  while (PEM_read_bio_X509(bio, &cert, NULL, (void *)""))
2586
0
  {
2587
0
    if (!certs)
2588
0
    {
2589
      // Make a new stack of X509 certs...
2590
0
      certs = sk_X509_new_null();
2591
0
    }
2592
2593
0
    if (certs)
2594
0
    {
2595
      // Add the X509 certificate...
2596
0
      sk_X509_push(certs, cert);
2597
0
    }
2598
0
    else
2599
0
    {
2600
      // Unable to add, free and stop...
2601
0
      X509_free(cert);
2602
0
      break;
2603
0
    }
2604
2605
0
    cert = NULL;
2606
0
  }
2607
2608
0
  BIO_free(bio);
2609
2610
0
  return (certs);
2611
0
}