Skip to content

Commit 0034146

Browse files
committed
Add httpGetSecurity API.
1 parent 2ced84a commit 0034146

8 files changed

Lines changed: 141 additions & 58 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changes in libcups
44
libcups v3.0.0 (YYYY-MM-DD)
55
---------------------------
66

7+
- Added `httpGetSecurity` API.
78
- Updated `ippfind` to use `cupsGetClock` API.
89
- Fixed return values of `ippDateToTime` when the timezone isn't GMT.
910
- Fixed a potential timing issue with `cupsEnumDests`.

cups/http.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Hyper-Text Transport Protocol definitions for CUPS.
33
//
4-
// Copyright © 2021-2024 by OpenPrinting.
4+
// Copyright © 2021-2025 by OpenPrinting.
55
// Copyright © 2007-2018 by Apple Inc.
66
// Copyright © 1997-2007 by Easy Software Products, all rights reserved.
77
//
@@ -469,6 +469,7 @@ extern off_t httpGetLength(http_t *http) _CUPS_PUBLIC;
469469
extern size_t httpGetPending(http_t *http) _CUPS_PUBLIC;
470470
extern size_t httpGetReady(http_t *http) _CUPS_PUBLIC;
471471
extern size_t httpGetRemaining(http_t *http) _CUPS_PUBLIC;
472+
extern const char *httpGetSecurity(http_t *http, char *buffer, size_t bufsize) _CUPS_PUBLIC;
472473
extern http_state_t httpGetState(http_t *http) _CUPS_PUBLIC;
473474
extern http_status_t httpGetStatus(http_t *http) _CUPS_PUBLIC;
474475
extern char *httpGetSubField(http_t *http, http_field_t field, const char *name, char *value, size_t valuelen) _CUPS_PUBLIC;

cups/libcups3.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ httpGetLength
387387
httpGetPending
388388
httpGetReady
389389
httpGetRemaining
390+
httpGetSecurity
390391
httpGetState
391392
httpGetStatus
392393
httpGetSubField

cups/tls-gnutls.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,60 @@ _httpFreeCredentials(
15201520
}
15211521

15221522

1523+
//
1524+
// 'httpGetSecurity()' - Get the TLS version and cipher suite used by a connection.
1525+
//
1526+
// This function gets the TLS version and cipher suite being used by a
1527+
// connection, if any. The string is copied to "buffer" and is of the form
1528+
// "TLS/major.minor CipherSuite". If not encrypted, the buffer is cleared to
1529+
// the empty string.
1530+
//
1531+
1532+
const char * // O - Security information or `NULL` if not encrypted
1533+
httpGetSecurity(http_t *http, // I - HTTP connection
1534+
char *buffer, // I - String buffer
1535+
size_t bufsize) // I - Size of buffer
1536+
{
1537+
const char *cipherName; // Cipher suite name
1538+
1539+
1540+
// Range check input...
1541+
if (buffer)
1542+
*buffer = '\0';
1543+
1544+
if (!http || !http->tls || !buffer || bufsize < 16)
1545+
return (NULL);
1546+
1547+
// Record the TLS version and cipher suite...
1548+
cipherName = gnutls_session_get_desc(http->tls);
1549+
1550+
switch (gnutls_protocol_get_version(http->tls))
1551+
{
1552+
default :
1553+
snprintf(buffer, bufsize, "TLS/?.? %s", cipherName);
1554+
break;
1555+
1556+
case GNUTLS_TLS1_0 :
1557+
snprintf(buffer, bufsize, "TLS/1.0 %s", cipherName);
1558+
break;
1559+
1560+
case GNUTLS_TLS1_1 :
1561+
snprintf(buffer, bufsize, "TLS/1.1 %s", cipherName);
1562+
break;
1563+
1564+
case GNUTLS_TLS1_2 :
1565+
snprintf(buffer, bufsize, "TLS/1.2 %s", cipherName);
1566+
break;
1567+
1568+
case GNUTLS_TLS1_3 :
1569+
snprintf(buffer, bufsize, "TLS/1.3 %s", cipherName);
1570+
break;
1571+
}
1572+
1573+
return (buffer);
1574+
}
1575+
1576+
15231577
//
15241578
// '_httpTLSInitialize()' - Initialize the TLS stack.
15251579
//

cups/tls-openssl.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,62 @@ _httpFreeCredentials(
16191619
}
16201620

16211621

1622+
//
1623+
// 'httpGetSecurity()' - Get the TLS version and cipher suite used by a connection.
1624+
//
1625+
// This function gets the TLS version and cipher suite being used by a
1626+
// connection, if any. The string is copied to "buffer" and is of the form
1627+
// "TLS/major.minor CipherSuite". If not encrypted, the buffer is cleared to
1628+
// the empty string.
1629+
//
1630+
1631+
const char * // O - Security information or `NULL` if not encrypted
1632+
httpGetSecurity(http_t *http, // I - HTTP connection
1633+
char *buffer, // I - String buffer
1634+
size_t bufsize) // I - Size of buffer
1635+
{
1636+
const char *cipherName; // Cipher suite name
1637+
1638+
1639+
// Range check input...
1640+
if (buffer)
1641+
*buffer = '\0';
1642+
1643+
if (!http || !http->tls || !buffer || bufsize < 16)
1644+
return (NULL);
1645+
1646+
// Record the TLS version and cipher suite...
1647+
cipherName = SSL_get_cipher_name(http->tls);
1648+
1649+
switch (SSL_version(http->tls))
1650+
{
1651+
default :
1652+
snprintf(buffer, bufsize, "TLS/?.? %s", cipherName);
1653+
break;
1654+
1655+
case TLS1_VERSION :
1656+
snprintf(buffer, bufsize, "TLS/1.0 %s", cipherName);
1657+
break;
1658+
1659+
case TLS1_1_VERSION :
1660+
snprintf(buffer, bufsize, "TLS/1.1 %s", cipherName);
1661+
break;
1662+
1663+
case TLS1_2_VERSION :
1664+
snprintf(buffer, bufsize, "TLS/1.2 %s", cipherName);
1665+
break;
1666+
1667+
# ifdef TLS1_3_VERSION
1668+
case TLS1_3_VERSION :
1669+
snprintf(buffer, bufsize, "TLS/1.3 %s", cipherName);
1670+
break;
1671+
# endif // TLS1_3_VERSION
1672+
}
1673+
1674+
return (buffer);
1675+
}
1676+
1677+
16221678
//
16231679
// '_httpTLSInitialize()' - Initialize the TLS stack.
16241680
//

cups/tlscheck.c

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// TLS check program for CUPS.
33
//
4-
// Copyright © 2021-2023 by OpenPrinting.
4+
// Copyright © 2021-2025 by OpenPrinting.
55
// Copyright © 2007-2017 by Apple Inc.
66
// Copyright © 1997-2006 by Easy Software Products.
77
//
@@ -31,11 +31,10 @@ main(int argc, // I - Number of command-line arguments
3131
http_t *http = NULL; // HTTP connection
3232
const char *server = NULL; // Hostname from command-line
3333
int port = 0; // Port number
34-
char *creds; // Server credentials
35-
char creds_str[2048]; // Credentials string
36-
const char *cipherName; // Cipher suite name
37-
int tlsVersion = 0; // TLS version number
38-
char uri[1024], // Printer URI
34+
char *creds, // Server credentials
35+
creds_str[2048], // Credentials string
36+
security[256], // Security string
37+
uri[1024], // Printer URI
3938
scheme[32], // URI scheme
4039
host[256], // Hostname
4140
userpass[256], // Username/password
@@ -184,57 +183,7 @@ main(int argc, // I - Number of command-line arguments
184183
free(creds);
185184
}
186185

187-
#ifdef HAVE_OPENSSL
188-
switch (SSL_version(http->tls))
189-
{
190-
default :
191-
tlsVersion = 0;
192-
break;
193-
194-
case TLS1_VERSION :
195-
tlsVersion = 10;
196-
break;
197-
198-
case TLS1_1_VERSION :
199-
tlsVersion = 11;
200-
break;
201-
202-
case TLS1_2_VERSION :
203-
tlsVersion = 12;
204-
break;
205-
206-
# ifdef TLS1_3_VERSION
207-
case TLS1_3_VERSION :
208-
tlsVersion = 13;
209-
break;
210-
# endif // TLS1_3_VERSION
211-
}
212-
213-
cipherName = SSL_get_cipher_name(http->tls);
214-
215-
#else // HAVE_GNUTLS
216-
switch (gnutls_protocol_get_version(http->tls))
217-
{
218-
default :
219-
tlsVersion = 0;
220-
break;
221-
case GNUTLS_TLS1_0 :
222-
tlsVersion = 10;
223-
break;
224-
case GNUTLS_TLS1_1 :
225-
tlsVersion = 11;
226-
break;
227-
case GNUTLS_TLS1_2 :
228-
tlsVersion = 12;
229-
break;
230-
case GNUTLS_TLS1_3 :
231-
tlsVersion = 13;
232-
break;
233-
}
234-
cipherName = gnutls_session_get_desc(http->tls);
235-
#endif // HAVE_OPENSSL
236-
237-
printf("%s: OK (TLS: %d.%d, %s)\n", server, tlsVersion / 10, tlsVersion % 10, cipherName);
186+
printf("%s: OK (%s)\n", server, httpGetSecurity(http, security, sizeof(security)));
238187
printf(" %s\n", creds_str);
239188

240189
if (verbose)

doc/cupspm.epub

201 Bytes
Binary file not shown.

doc/cupspm.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ <h2 class="title">Contents</h2>
619619
<li><a href="#httpGetPending">httpGetPending</a></li>
620620
<li><a href="#httpGetReady">httpGetReady</a></li>
621621
<li><a href="#httpGetRemaining">httpGetRemaining</a></li>
622+
<li><a href="#httpGetSecurity">httpGetSecurity</a></li>
622623
<li><a href="#httpGetState">httpGetState</a></li>
623624
<li><a href="#httpGetStatus">httpGetStatus</a></li>
624625
<li><a href="#httpGetSubField">httpGetSubField</a></li>
@@ -7940,6 +7941,26 @@ <h4 class="returnvalue">Return Value</h4>
79407941
<h4 class="discussion">Discussion</h4>
79417942
<p class="discussion">The <a href="#httpIsChunked"><code>httpIsChunked</code></a> function can be used to determine whether the
79427943
message body is chunked or fixed-length.</p>
7944+
<h3 class="function"><a id="httpGetSecurity">httpGetSecurity</a></h3>
7945+
<p class="description">Get the TLS version and cipher suite used by a connection.</p>
7946+
<p class="code">
7947+
<span class="reserved">const</span> <span class="reserved">char</span> *httpGetSecurity(<a href="#http_t">http_t</a> *http, <span class="reserved">char</span> *buffer, size_t bufsize);</p>
7948+
<h4 class="parameters">Parameters</h4>
7949+
<table class="list"><tbody>
7950+
<tr><th>http</th>
7951+
<td class="description">HTTP connection</td></tr>
7952+
<tr><th>buffer</th>
7953+
<td class="description">String buffer</td></tr>
7954+
<tr><th>bufsize</th>
7955+
<td class="description">Size of buffer</td></tr>
7956+
</tbody></table>
7957+
<h4 class="returnvalue">Return Value</h4>
7958+
<p class="description">Security information or <code>NULL</code> if not encrypted</p>
7959+
<h4 class="discussion">Discussion</h4>
7960+
<p class="discussion">This function gets the TLS version and cipher suite being used by a
7961+
connection, if any. The string is copied to &quot;buffer&quot; and is of the form
7962+
&quot;TLS/major.minor CipherSuite&quot;. If not encrypted, the buffer is cleared to
7963+
the empty string.</p>
79437964
<h3 class="function"><a id="httpGetState">httpGetState</a></h3>
79447965
<p class="description">Get the current state of the HTTP request.</p>
79457966
<p class="code">

0 commit comments

Comments
 (0)