1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
6 Copyright 2012 Zbigniew Jędrzejewski-Szmek
14 #include <gnutls/gnutls.h>
15 #include <gnutls/x509.h>
18 #include "alloc-util.h"
21 #include "microhttpd-util.h"
22 #include "string-util.h"
26 void microhttpd_logger(void *arg
, const char *fmt
, va_list ap
) {
29 f
= strjoina("microhttpd: ", fmt
);
31 DISABLE_WARNING_FORMAT_NONLITERAL
;
32 log_internalv(LOG_INFO
, 0, NULL
, 0, NULL
, f
, ap
);
36 static int mhd_respond_internal(struct MHD_Connection
*connection
,
37 enum MHD_RequestTerminationCode code
,
40 enum MHD_ResponseMemoryMode mode
) {
41 struct MHD_Response
*response
;
46 response
= MHD_create_response_from_buffer(size
, (char*) buffer
, mode
);
50 log_debug("Queueing response %u: %s", code
, buffer
);
51 MHD_add_response_header(response
, "Content-Type", "text/plain");
52 r
= MHD_queue_response(connection
, code
, response
);
53 MHD_destroy_response(response
);
58 int mhd_respond(struct MHD_Connection
*connection
,
59 enum MHD_RequestTerminationCode code
,
60 const char *message
) {
64 fmt
= strjoina(message
, "\n");
66 return mhd_respond_internal(connection
, code
,
67 fmt
, strlen(message
) + 1,
68 MHD_RESPMEM_PERSISTENT
);
71 int mhd_respond_oom(struct MHD_Connection
*connection
) {
72 return mhd_respond(connection
, MHD_HTTP_SERVICE_UNAVAILABLE
, "Out of memory.");
75 int mhd_respondf(struct MHD_Connection
*connection
,
77 enum MHD_RequestTerminationCode code
,
78 const char *format
, ...) {
91 fmt
= strjoina(format
, "\n");
93 #pragma GCC diagnostic push
94 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
95 r
= vasprintf(&m
, fmt
, ap
);
96 #pragma GCC diagnostic pop
100 return respond_oom(connection
);
102 return mhd_respond_internal(connection
, code
, m
, r
, MHD_RESPMEM_MUST_FREE
);
108 const char *const names
[4];
111 } gnutls_log_map
[] = {
112 { {"0"}, LOG_DEBUG
},
113 { {"1", "audit"}, LOG_WARNING
, true}, /* gnutls session audit */
114 { {"2", "assert"}, LOG_DEBUG
}, /* gnutls assert log */
115 { {"3", "hsk", "ext"}, LOG_DEBUG
}, /* gnutls handshake log */
116 { {"4", "rec"}, LOG_DEBUG
}, /* gnutls record log */
117 { {"5", "dtls"}, LOG_DEBUG
}, /* gnutls DTLS log */
118 { {"6", "buf"}, LOG_DEBUG
},
119 { {"7", "write", "read"}, LOG_DEBUG
},
120 { {"8"}, LOG_DEBUG
},
121 { {"9", "enc", "int"}, LOG_DEBUG
},
124 static void log_func_gnutls(int level
, const char *message
) {
127 if (0 <= level
&& level
< (int) ELEMENTSOF(gnutls_log_map
)) {
128 if (gnutls_log_map
[level
].enabled
)
129 log_internal(gnutls_log_map
[level
].level
, 0, NULL
, 0, NULL
, "gnutls %d/%s: %s", level
, gnutls_log_map
[level
].names
[1], message
);
131 log_debug("Received GNUTLS message with unknown level %d.", level
);
132 log_internal(LOG_DEBUG
, 0, NULL
, 0, NULL
, "gnutls: %s", message
);
136 static void log_reset_gnutls_level(void) {
139 for (i
= ELEMENTSOF(gnutls_log_map
) - 1; i
>= 0; i
--)
140 if (gnutls_log_map
[i
].enabled
) {
141 log_debug("Setting gnutls log level to %d", i
);
142 gnutls_global_set_log_level(i
);
147 static int log_enable_gnutls_category(const char *cat
) {
150 if (streq(cat
, "all")) {
151 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
152 gnutls_log_map
[i
].enabled
= true;
153 log_reset_gnutls_level();
156 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
157 if (strv_contains((char**)gnutls_log_map
[i
].names
, cat
)) {
158 gnutls_log_map
[i
].enabled
= true;
159 log_reset_gnutls_level();
162 log_error("No such log category: %s", cat
);
166 int setup_gnutls_logger(char **categories
) {
170 gnutls_global_set_log_function(log_func_gnutls
);
173 STRV_FOREACH(cat
, categories
) {
174 r
= log_enable_gnutls_category(*cat
);
179 log_reset_gnutls_level();
184 static int verify_cert_authorized(gnutls_session_t session
) {
186 gnutls_certificate_type_t type
;
190 r
= gnutls_certificate_verify_peers2(session
, &status
);
192 return log_error_errno(r
, "gnutls_certificate_verify_peers2 failed: %m");
194 type
= gnutls_certificate_type_get(session
);
195 r
= gnutls_certificate_verification_status_print(status
, type
, &out
, 0);
197 return log_error_errno(r
, "gnutls_certificate_verification_status_print failed: %m");
199 log_debug("Certificate status: %s", out
.data
);
200 gnutls_free(out
.data
);
202 return status
== 0 ? 0 : -EPERM
;
205 static int get_client_cert(gnutls_session_t session
, gnutls_x509_crt_t
*client_cert
) {
206 const gnutls_datum_t
*pcert
;
208 gnutls_x509_crt_t cert
;
214 pcert
= gnutls_certificate_get_peers(session
, &listsize
);
215 if (!pcert
|| !listsize
) {
216 log_error("Failed to retrieve certificate chain");
220 r
= gnutls_x509_crt_init(&cert
);
222 log_error("Failed to initialize client certificate");
226 /* Note that by passing values between 0 and listsize here, you
227 can get access to the CA's certs */
228 r
= gnutls_x509_crt_import(cert
, &pcert
[0], GNUTLS_X509_FMT_DER
);
230 log_error("Failed to import client certificate");
231 gnutls_x509_crt_deinit(cert
);
239 static int get_auth_dn(gnutls_x509_crt_t client_cert
, char **buf
) {
244 assert(*buf
== NULL
);
246 r
= gnutls_x509_crt_get_dn(client_cert
, NULL
, &len
);
247 if (r
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
248 log_error("gnutls_x509_crt_get_dn failed");
256 gnutls_x509_crt_get_dn(client_cert
, *buf
, &len
);
260 static inline void gnutls_x509_crt_deinitp(gnutls_x509_crt_t
*p
) {
261 gnutls_x509_crt_deinit(*p
);
264 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
265 const union MHD_ConnectionInfo
*ci
;
266 gnutls_session_t session
;
267 _cleanup_(gnutls_x509_crt_deinitp
) gnutls_x509_crt_t client_cert
= NULL
;
268 _cleanup_free_
char *buf
= NULL
;
276 ci
= MHD_get_connection_info(connection
,
277 MHD_CONNECTION_INFO_GNUTLS_SESSION
);
279 log_error("MHD_get_connection_info failed: session is unencrypted");
280 *code
= mhd_respond(connection
, MHD_HTTP_FORBIDDEN
,
281 "Encrypted connection is required");
284 session
= ci
->tls_session
;
287 r
= get_client_cert(session
, &client_cert
);
289 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
290 "Authorization through certificate is required");
294 r
= get_auth_dn(client_cert
, &buf
);
296 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
297 "Failed to determine distinguished name from certificate");
301 log_debug("Connection from %s", buf
);
304 *hostname
= TAKE_PTR(buf
);
306 r
= verify_cert_authorized(session
);
308 log_warning("Client is not authorized");
309 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
310 "Client certificate not signed by recognized authority");
316 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
320 int setup_gnutls_logger(char **categories
) {
322 log_notice("Ignoring specified gnutls logging categories — gnutls not available.");