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"
19 void microhttpd_logger(void *arg
, const char *fmt
, va_list ap
) {
22 f
= strjoina("microhttpd: ", fmt
);
24 DISABLE_WARNING_FORMAT_NONLITERAL
;
25 log_internalv(LOG_INFO
, 0, NULL
, 0, NULL
, f
, ap
);
29 static int mhd_respond_internal(struct MHD_Connection
*connection
,
30 enum MHD_RequestTerminationCode code
,
33 enum MHD_ResponseMemoryMode mode
) {
36 _cleanup_(MHD_destroy_responsep
) struct MHD_Response
*response
37 = MHD_create_response_from_buffer(size
, (char*) buffer
, mode
);
41 log_debug("Queueing response %u: %s", code
, buffer
);
42 if (MHD_add_response_header(response
, "Content-Type", "text/plain") == MHD_NO
)
44 return MHD_queue_response(connection
, code
, response
);
47 int mhd_respond(struct MHD_Connection
*connection
,
48 enum MHD_RequestTerminationCode code
,
49 const char *message
) {
53 fmt
= strjoina(message
, "\n");
55 return mhd_respond_internal(connection
, code
,
56 fmt
, strlen(message
) + 1,
57 MHD_RESPMEM_PERSISTENT
);
60 int mhd_respond_oom(struct MHD_Connection
*connection
) {
61 return mhd_respond(connection
, MHD_HTTP_SERVICE_UNAVAILABLE
, "Out of memory.");
64 int mhd_respondf(struct MHD_Connection
*connection
,
66 enum MHD_RequestTerminationCode code
,
67 const char *format
, ...) {
80 fmt
= strjoina(format
, "\n");
82 DISABLE_WARNING_FORMAT_NONLITERAL
;
83 r
= vasprintf(&m
, fmt
, ap
);
88 return respond_oom(connection
);
90 return mhd_respond_internal(connection
, code
, m
, r
, MHD_RESPMEM_MUST_FREE
);
96 const char *const names
[4];
99 } gnutls_log_map
[] = {
100 { {"0"}, LOG_DEBUG
},
101 { {"1", "audit"}, LOG_WARNING
, true}, /* gnutls session audit */
102 { {"2", "assert"}, LOG_DEBUG
}, /* gnutls assert log */
103 { {"3", "hsk", "ext"}, LOG_DEBUG
}, /* gnutls handshake log */
104 { {"4", "rec"}, LOG_DEBUG
}, /* gnutls record log */
105 { {"5", "dtls"}, LOG_DEBUG
}, /* gnutls DTLS log */
106 { {"6", "buf"}, LOG_DEBUG
},
107 { {"7", "write", "read"}, LOG_DEBUG
},
108 { {"8"}, LOG_DEBUG
},
109 { {"9", "enc", "int"}, LOG_DEBUG
},
112 static void log_func_gnutls(int level
, const char *message
) {
115 if (0 <= level
&& level
< (int) ELEMENTSOF(gnutls_log_map
)) {
116 if (gnutls_log_map
[level
].enabled
)
117 log_internal(gnutls_log_map
[level
].level
, 0, NULL
, 0, NULL
, "gnutls %d/%s: %s", level
, gnutls_log_map
[level
].names
[1], message
);
119 log_debug("Received GNUTLS message with unknown level %d.", level
);
120 log_internal(LOG_DEBUG
, 0, NULL
, 0, NULL
, "gnutls: %s", message
);
124 static void log_reset_gnutls_level(void) {
127 for (i
= ELEMENTSOF(gnutls_log_map
) - 1; i
>= 0; i
--)
128 if (gnutls_log_map
[i
].enabled
) {
129 log_debug("Setting gnutls log level to %d", i
);
130 gnutls_global_set_log_level(i
);
135 static int log_enable_gnutls_category(const char *cat
) {
138 if (streq(cat
, "all")) {
139 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
140 gnutls_log_map
[i
].enabled
= true;
141 log_reset_gnutls_level();
144 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
145 if (strv_contains((char**)gnutls_log_map
[i
].names
, cat
)) {
146 gnutls_log_map
[i
].enabled
= true;
147 log_reset_gnutls_level();
150 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No such log category: %s", cat
);
153 int setup_gnutls_logger(char **categories
) {
156 gnutls_global_set_log_function(log_func_gnutls
);
159 STRV_FOREACH(cat
, categories
) {
160 r
= log_enable_gnutls_category(*cat
);
165 log_reset_gnutls_level();
170 static int verify_cert_authorized(gnutls_session_t session
) {
172 gnutls_certificate_type_t type
;
176 r
= gnutls_certificate_verify_peers2(session
, &status
);
178 return log_error_errno(r
, "gnutls_certificate_verify_peers2 failed: %m");
180 type
= gnutls_certificate_type_get(session
);
181 r
= gnutls_certificate_verification_status_print(status
, type
, &out
, 0);
183 return log_error_errno(r
, "gnutls_certificate_verification_status_print failed: %m");
185 log_debug("Certificate status: %s", out
.data
);
186 gnutls_free(out
.data
);
188 return status
== 0 ? 0 : -EPERM
;
191 static int get_client_cert(gnutls_session_t session
, gnutls_x509_crt_t
*client_cert
) {
192 const gnutls_datum_t
*pcert
;
194 gnutls_x509_crt_t cert
;
200 pcert
= gnutls_certificate_get_peers(session
, &listsize
);
201 if (!pcert
|| !listsize
)
202 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
203 "Failed to retrieve certificate chain");
205 r
= gnutls_x509_crt_init(&cert
);
207 log_error("Failed to initialize client certificate");
211 /* Note that by passing values between 0 and listsize here, you
212 can get access to the CA's certs */
213 r
= gnutls_x509_crt_import(cert
, &pcert
[0], GNUTLS_X509_FMT_DER
);
215 log_error("Failed to import client certificate");
216 gnutls_x509_crt_deinit(cert
);
224 static int get_auth_dn(gnutls_x509_crt_t client_cert
, char **buf
) {
229 assert(*buf
== NULL
);
231 r
= gnutls_x509_crt_get_dn(client_cert
, NULL
, &len
);
232 if (r
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
233 log_error("gnutls_x509_crt_get_dn failed");
241 gnutls_x509_crt_get_dn(client_cert
, *buf
, &len
);
245 static void gnutls_x509_crt_deinitp(gnutls_x509_crt_t
*p
) {
246 gnutls_x509_crt_deinit(*p
);
249 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
250 const union MHD_ConnectionInfo
*ci
;
251 gnutls_session_t session
;
252 _cleanup_(gnutls_x509_crt_deinitp
) gnutls_x509_crt_t client_cert
= NULL
;
253 _cleanup_free_
char *buf
= NULL
;
261 ci
= MHD_get_connection_info(connection
,
262 MHD_CONNECTION_INFO_GNUTLS_SESSION
);
264 log_error("MHD_get_connection_info failed: session is unencrypted");
265 *code
= mhd_respond(connection
, MHD_HTTP_FORBIDDEN
,
266 "Encrypted connection is required");
269 session
= ci
->tls_session
;
272 r
= get_client_cert(session
, &client_cert
);
274 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
275 "Authorization through certificate is required");
279 r
= get_auth_dn(client_cert
, &buf
);
281 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
282 "Failed to determine distinguished name from certificate");
286 log_debug("Connection from %s", buf
);
289 *hostname
= TAKE_PTR(buf
);
291 r
= verify_cert_authorized(session
);
293 log_warning("Client is not authorized");
294 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
295 "Client certificate not signed by recognized authority");
301 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
305 int setup_gnutls_logger(char **categories
) {
307 log_notice("Ignoring specified gnutls logging categories — gnutls not available.");