Skip to content

Commit fb52b69

Browse files
committed
X.509 updates:
- cupsCreateCredentialsRequest now stores the new private key separately - cupsSaveCredentials now uses the CSR private key when saving just the new certificate - cupsSaveCredentials now does some sanity checks on the input values. - cupsSaveCredentials now supports credential removal as documented.
1 parent fa8d43b commit fb52b69

4 files changed

Lines changed: 63 additions & 6 deletions

File tree

CHANGES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
Changes in libcups
22
==================
33

4+
libcups v3.0rc3 (YYYY-MM-DD)
5+
----------------------------
6+
7+
- Updated `cupsCreateCertificateRequest` to store the new private key
8+
separately.
9+
- Updated `cupsSaveCredentials` to validate the input credentials, support
10+
using a saved private key from `cupsCreateCertificateRequest`, and support
11+
credential removal as documented.
12+
13+
414
libcups v3.0rc2 (2024-10-15)
515
----------------------------
616

cups/tls-gnutls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ cupsCreateCredentialsRequest(
525525
}
526526

527527
http_make_path(csrfile, sizeof(csrfile), path, common_name, "csr");
528-
http_make_path(keyfile, sizeof(keyfile), path, common_name, "key");
528+
http_make_path(keyfile, sizeof(keyfile), path, common_name, "ktm");
529529

530530
// Create the encryption key...
531531
DEBUG_puts("1cupsCreateCredentialsRequest: Creating key pair.");

cups/tls-openssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ cupsCreateCredentialsRequest(
595595
}
596596

597597
http_make_path(csrfile, sizeof(csrfile), path, common_name, "csr");
598-
http_make_path(keyfile, sizeof(keyfile), path, common_name, "key");
598+
http_make_path(keyfile, sizeof(keyfile), path, common_name, "ktm");
599599

600600
// Create the encryption key...
601601
DEBUG_puts("1cupsCreateCredentialsRequest: Creating key pair.");

cups/tls.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,62 @@ cupsSaveCredentials(
130130
const char *credentials, // I - PEM-encoded certificate chain or `NULL` to remove
131131
const char *key) // I - PEM-encoded private key or `NULL` for none
132132
{
133-
if (http_save_file(path, common_name, "crt", credentials))
133+
bool ret = false; // Return value
134+
char crtfile[1024], // Certificate filename
135+
keyfile[1024], // Key filename
136+
ktmfile[1024]; // Temporary key filename
137+
138+
139+
// Validate input...
140+
if (credentials)
141+
{
142+
// Make sure it looks like a PEM-encoded cert...
143+
if (strncmp(credentials, "-----BEGIN CERTIFICATE-----", 27) || strstr(key, "-----END CERTIFICATE-----") == NULL)
144+
return (false);
145+
}
146+
147+
if (key)
134148
{
149+
// Make sure it looks like a PEM-encoded private key...
150+
if (strncmp(key, "-----BEGIN PRIVATE KEY-----", 27) || strstr(key, "-----END PRIVATE KEY-----") == NULL)
151+
return (false);
152+
}
153+
154+
// Save or delete credentials...
155+
http_make_path(crtfile, sizeof(crtfile), path, common_name, "crt");
156+
http_make_path(keyfile, sizeof(keyfile), path, common_name, "key");
157+
http_make_path(ktmfile, sizeof(ktmfile), path, common_name, "ktm");
158+
159+
if (!credentials && !key)
160+
{
161+
// Delete credentials...
162+
if (!unlink(crtfile) && !unlink(keyfile))
163+
ret = true;
164+
else
165+
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), false);
166+
}
167+
else if (!credentials && key)
168+
{
169+
// Bad arguments...
170+
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), false);
171+
}
172+
else if (!key && access(keyfile, 0) && access(ktmfile, 0))
173+
{
174+
// Missing key file...
175+
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), false);
176+
}
177+
else if (http_save_file(path, common_name, "crt", credentials))
178+
{
179+
// Certificate saved, save or rename key file as needed...
135180
if (key)
136-
return (http_save_file(path, common_name, "key", key));
181+
ret = http_save_file(path, common_name, "key", key);
182+
else if (!access(ktmfile, 0))
183+
ret = !rename(ktmfile, keyfile);
137184
else
138-
return (true);
185+
ret = true;
139186
}
140187

141-
return (false);
188+
return (ret);
142189
}
143190

144191

0 commit comments

Comments
 (0)