1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2012 Lennart Poettering
4 Copyright 2012 Zbigniew Jędrzejewski-Szmek
12 #include <gnutls/gnutls.h>
13 #include <gnutls/x509.h>
16 #include "alloc-util.h"
19 #include "microhttpd-util.h"
20 #include "string-util.h"
24 void microhttpd_logger(void *arg
, const char *fmt
, va_list ap
) {
27 f
= strjoina("microhttpd: ", fmt
);
29 DISABLE_WARNING_FORMAT_NONLITERAL
;
30 log_internalv(LOG_INFO
, 0, NULL
, 0, NULL
, f
, ap
);
34 static int mhd_respond_internal(struct MHD_Connection
*connection
,
35 enum MHD_RequestTerminationCode code
,
38 enum MHD_ResponseMemoryMode mode
) {
39 struct MHD_Response
*response
;
44 response
= MHD_create_response_from_buffer(size
, (char*) buffer
, mode
);
48 log_debug("Queueing response %u: %s", code
, buffer
);
49 MHD_add_response_header(response
, "Content-Type", "text/plain");
50 r
= MHD_queue_response(connection
, code
, response
);
51 MHD_destroy_response(response
);
56 int mhd_respond(struct MHD_Connection
*connection
,
57 enum MHD_RequestTerminationCode code
,
58 const char *message
) {
62 fmt
= strjoina(message
, "\n");
64 return mhd_respond_internal(connection
, code
,
65 fmt
, strlen(message
) + 1,
66 MHD_RESPMEM_PERSISTENT
);
69 int mhd_respond_oom(struct MHD_Connection
*connection
) {
70 return mhd_respond(connection
, MHD_HTTP_SERVICE_UNAVAILABLE
, "Out of memory.");
73 int mhd_respondf(struct MHD_Connection
*connection
,
75 enum MHD_RequestTerminationCode code
,
76 const char *format
, ...) {
89 fmt
= strjoina(format
, "\n");
91 #pragma GCC diagnostic push
92 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
93 r
= vasprintf(&m
, fmt
, ap
);
94 #pragma GCC diagnostic pop
98 return respond_oom(connection
);
100 return mhd_respond_internal(connection
, code
, m
, r
, MHD_RESPMEM_MUST_FREE
);
106 const char *const names
[4];
109 } gnutls_log_map
[] = {
110 { {"0"}, LOG_DEBUG
},
111 { {"1", "audit"}, LOG_WARNING
, true}, /* gnutls session audit */
112 { {"2", "assert"}, LOG_DEBUG
}, /* gnutls assert log */
113 { {"3", "hsk", "ext"}, LOG_DEBUG
}, /* gnutls handshake log */
114 { {"4", "rec"}, LOG_DEBUG
}, /* gnutls record log */
115 { {"5", "dtls"}, LOG_DEBUG
}, /* gnutls DTLS log */
116 { {"6", "buf"}, LOG_DEBUG
},
117 { {"7", "write", "read"}, LOG_DEBUG
},
118 { {"8"}, LOG_DEBUG
},
119 { {"9", "enc", "int"}, LOG_DEBUG
},
122 static void log_func_gnutls(int level
, const char *message
) {
125 if (0 <= level
&& level
< (int) ELEMENTSOF(gnutls_log_map
)) {
126 if (gnutls_log_map
[level
].enabled
)
127 log_internal(gnutls_log_map
[level
].level
, 0, NULL
, 0, NULL
, "gnutls %d/%s: %s", level
, gnutls_log_map
[level
].names
[1], message
);
129 log_debug("Received GNUTLS message with unknown level %d.", level
);
130 log_internal(LOG_DEBUG
, 0, NULL
, 0, NULL
, "gnutls: %s", message
);
134 static void log_reset_gnutls_level(void) {
137 for (i
= ELEMENTSOF(gnutls_log_map
) - 1; i
>= 0; i
--)
138 if (gnutls_log_map
[i
].enabled
) {
139 log_debug("Setting gnutls log level to %d", i
);
140 gnutls_global_set_log_level(i
);
145 static int log_enable_gnutls_category(const char *cat
) {
148 if (streq(cat
, "all")) {
149 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
150 gnutls_log_map
[i
].enabled
= true;
151 log_reset_gnutls_level();
154 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
155 if (strv_contains((char**)gnutls_log_map
[i
].names
, cat
)) {
156 gnutls_log_map
[i
].enabled
= true;
157 log_reset_gnutls_level();
160 log_error("No such log category: %s", cat
);
164 int setup_gnutls_logger(char **categories
) {
168 gnutls_global_set_log_function(log_func_gnutls
);
171 STRV_FOREACH(cat
, categories
) {
172 r
= log_enable_gnutls_category(*cat
);
177 log_reset_gnutls_level();
182 static int verify_cert_authorized(gnutls_session_t session
) {
184 gnutls_certificate_type_t type
;
188 r
= gnutls_certificate_verify_peers2(session
, &status
);
190 return log_error_errno(r
, "gnutls_certificate_verify_peers2 failed: %m");
192 type
= gnutls_certificate_type_get(session
);
193 r
= gnutls_certificate_verification_status_print(status
, type
, &out
, 0);
195 return log_error_errno(r
, "gnutls_certificate_verification_status_print failed: %m");
197 log_debug("Certificate status: %s", out
.data
);
198 gnutls_free(out
.data
);
200 return status
== 0 ? 0 : -EPERM
;
203 static int get_client_cert(gnutls_session_t session
, gnutls_x509_crt_t
*client_cert
) {
204 const gnutls_datum_t
*pcert
;
206 gnutls_x509_crt_t cert
;
212 pcert
= gnutls_certificate_get_peers(session
, &listsize
);
213 if (!pcert
|| !listsize
) {
214 log_error("Failed to retrieve certificate chain");
218 r
= gnutls_x509_crt_init(&cert
);
220 log_error("Failed to initialize client certificate");
224 /* Note that by passing values between 0 and listsize here, you
225 can get access to the CA's certs */
226 r
= gnutls_x509_crt_import(cert
, &pcert
[0], GNUTLS_X509_FMT_DER
);
228 log_error("Failed to import client certificate");
229 gnutls_x509_crt_deinit(cert
);
237 static int get_auth_dn(gnutls_x509_crt_t client_cert
, char **buf
) {
242 assert(*buf
== NULL
);
244 r
= gnutls_x509_crt_get_dn(client_cert
, NULL
, &len
);
245 if (r
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
246 log_error("gnutls_x509_crt_get_dn failed");
254 gnutls_x509_crt_get_dn(client_cert
, *buf
, &len
);
258 static inline void gnutls_x509_crt_deinitp(gnutls_x509_crt_t
*p
) {
259 gnutls_x509_crt_deinit(*p
);
262 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
263 const union MHD_ConnectionInfo
*ci
;
264 gnutls_session_t session
;
265 _cleanup_(gnutls_x509_crt_deinitp
) gnutls_x509_crt_t client_cert
= NULL
;
266 _cleanup_free_
char *buf
= NULL
;
274 ci
= MHD_get_connection_info(connection
,
275 MHD_CONNECTION_INFO_GNUTLS_SESSION
);
277 log_error("MHD_get_connection_info failed: session is unencrypted");
278 *code
= mhd_respond(connection
, MHD_HTTP_FORBIDDEN
,
279 "Encrypted connection is required");
282 session
= ci
->tls_session
;
285 r
= get_client_cert(session
, &client_cert
);
287 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
288 "Authorization through certificate is required");
292 r
= get_auth_dn(client_cert
, &buf
);
294 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
295 "Failed to determine distinguished name from certificate");
299 log_debug("Connection from %s", buf
);
302 *hostname
= TAKE_PTR(buf
);
304 r
= verify_cert_authorized(session
);
306 log_warning("Client is not authorized");
307 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
308 "Client certificate not signed by recognized authority");
314 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
318 int setup_gnutls_logger(char **categories
) {
320 log_notice("Ignoring specified gnutls logging categories — gnutls not available.");