1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include <gnutls/gnutls.h>
8 #include <gnutls/x509.h>
11 #include "alloc-util.h"
14 #include "microhttpd-util.h"
15 #include "string-util.h"
18 void microhttpd_logger(void *arg
, const char *fmt
, va_list ap
) {
21 f
= strjoina("microhttpd: ", fmt
);
23 DISABLE_WARNING_FORMAT_NONLITERAL
;
24 log_internalv(LOG_INFO
, 0, NULL
, 0, NULL
, f
, ap
);
28 int mhd_respond_internal(
29 struct MHD_Connection
*connection
,
30 enum MHD_RequestTerminationCode code
,
33 enum MHD_ResponseMemoryMode mode
) {
37 _cleanup_(MHD_destroy_responsep
) struct MHD_Response
*response
38 = MHD_create_response_from_buffer(size
, (char*) buffer
, mode
);
42 log_debug("Queueing response %u: %s", code
, buffer
);
43 if (MHD_add_response_header(response
, "Content-Type", "text/plain") == MHD_NO
)
45 return MHD_queue_response(connection
, code
, response
);
48 int mhd_respond_oom(struct MHD_Connection
*connection
) {
49 return mhd_respond(connection
, MHD_HTTP_SERVICE_UNAVAILABLE
, "Out of memory.");
52 int mhd_respondf_internal(
53 struct MHD_Connection
*connection
,
55 enum MHD_RequestTerminationCode code
,
56 const char *format
, ...) {
69 r
= vasprintf(&m
, format
, ap
);
73 return respond_oom(connection
);
75 return mhd_respond_internal(connection
, code
, m
, r
, MHD_RESPMEM_MUST_FREE
);
81 const char *const names
[4];
84 } gnutls_log_map
[] = {
86 { {"1", "audit"}, LOG_WARNING
, true}, /* gnutls session audit */
87 { {"2", "assert"}, LOG_DEBUG
}, /* gnutls assert log */
88 { {"3", "hsk", "ext"}, LOG_DEBUG
}, /* gnutls handshake log */
89 { {"4", "rec"}, LOG_DEBUG
}, /* gnutls record log */
90 { {"5", "dtls"}, LOG_DEBUG
}, /* gnutls DTLS log */
91 { {"6", "buf"}, LOG_DEBUG
},
92 { {"7", "write", "read"}, LOG_DEBUG
},
94 { {"9", "enc", "int"}, LOG_DEBUG
},
97 static void log_func_gnutls(int level
, const char *message
) {
100 if (0 <= level
&& level
< (int) ELEMENTSOF(gnutls_log_map
)) {
101 if (gnutls_log_map
[level
].enabled
)
102 log_internal(gnutls_log_map
[level
].level
, 0, NULL
, 0, NULL
, "gnutls %d/%s: %s", level
, gnutls_log_map
[level
].names
[1], message
);
104 log_debug("Received GNUTLS message with unknown level %d.", level
);
105 log_internal(LOG_DEBUG
, 0, NULL
, 0, NULL
, "gnutls: %s", message
);
109 static void log_reset_gnutls_level(void) {
112 for (i
= ELEMENTSOF(gnutls_log_map
) - 1; i
>= 0; i
--)
113 if (gnutls_log_map
[i
].enabled
) {
114 log_debug("Setting gnutls log level to %d", i
);
115 gnutls_global_set_log_level(i
);
120 static int log_enable_gnutls_category(const char *cat
) {
123 if (streq(cat
, "all")) {
124 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
125 gnutls_log_map
[i
].enabled
= true;
126 log_reset_gnutls_level();
129 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
130 if (strv_contains((char**)gnutls_log_map
[i
].names
, cat
)) {
131 gnutls_log_map
[i
].enabled
= true;
132 log_reset_gnutls_level();
135 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No such log category: %s", cat
);
138 int setup_gnutls_logger(char **categories
) {
141 gnutls_global_set_log_function(log_func_gnutls
);
144 STRV_FOREACH(cat
, categories
) {
145 r
= log_enable_gnutls_category(*cat
);
150 log_reset_gnutls_level();
155 static int verify_cert_authorized(gnutls_session_t session
) {
157 gnutls_certificate_type_t type
;
161 r
= gnutls_certificate_verify_peers2(session
, &status
);
163 return log_error_errno(r
, "gnutls_certificate_verify_peers2 failed: %m");
165 type
= gnutls_certificate_type_get(session
);
166 r
= gnutls_certificate_verification_status_print(status
, type
, &out
, 0);
168 return log_error_errno(r
, "gnutls_certificate_verification_status_print failed: %m");
170 log_debug("Certificate status: %s", out
.data
);
171 gnutls_free(out
.data
);
173 return status
== 0 ? 0 : -EPERM
;
176 static int get_client_cert(gnutls_session_t session
, gnutls_x509_crt_t
*client_cert
) {
177 const gnutls_datum_t
*pcert
;
179 gnutls_x509_crt_t cert
;
185 pcert
= gnutls_certificate_get_peers(session
, &listsize
);
186 if (!pcert
|| !listsize
)
187 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
188 "Failed to retrieve certificate chain");
190 r
= gnutls_x509_crt_init(&cert
);
192 log_error("Failed to initialize client certificate");
196 /* Note that by passing values between 0 and listsize here, you
197 can get access to the CA's certs */
198 r
= gnutls_x509_crt_import(cert
, &pcert
[0], GNUTLS_X509_FMT_DER
);
200 log_error("Failed to import client certificate");
201 gnutls_x509_crt_deinit(cert
);
209 static int get_auth_dn(gnutls_x509_crt_t client_cert
, char **buf
) {
214 assert(*buf
== NULL
);
216 r
= gnutls_x509_crt_get_dn(client_cert
, NULL
, &len
);
217 if (r
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
218 log_error("gnutls_x509_crt_get_dn failed");
226 gnutls_x509_crt_get_dn(client_cert
, *buf
, &len
);
230 static void gnutls_x509_crt_deinitp(gnutls_x509_crt_t
*p
) {
231 gnutls_x509_crt_deinit(*p
);
234 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
235 const union MHD_ConnectionInfo
*ci
;
236 gnutls_session_t session
;
237 _cleanup_(gnutls_x509_crt_deinitp
) gnutls_x509_crt_t client_cert
= NULL
;
238 _cleanup_free_
char *buf
= NULL
;
246 ci
= MHD_get_connection_info(connection
,
247 MHD_CONNECTION_INFO_GNUTLS_SESSION
);
249 log_error("MHD_get_connection_info failed: session is unencrypted");
250 *code
= mhd_respond(connection
, MHD_HTTP_FORBIDDEN
,
251 "Encrypted connection is required");
254 session
= ci
->tls_session
;
257 r
= get_client_cert(session
, &client_cert
);
259 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
260 "Authorization through certificate is required");
264 r
= get_auth_dn(client_cert
, &buf
);
266 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
267 "Failed to determine distinguished name from certificate");
271 log_debug("Connection from %s", buf
);
274 *hostname
= TAKE_PTR(buf
);
276 r
= verify_cert_authorized(session
);
278 log_warning("Client is not authorized");
279 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
280 "Client certificate not signed by recognized authority");
286 _noreturn_
int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
287 assert_not_reached();
290 int setup_gnutls_logger(char **categories
) {
292 log_notice("Ignoring specified gnutls logging categories — gnutls not available.");