1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2012 Zbigniew Jędrzejewski-Szmek
11 #include <gnutls/gnutls.h>
12 #include <gnutls/x509.h>
15 #include "alloc-util.h"
18 #include "microhttpd-util.h"
19 #include "string-util.h"
23 void microhttpd_logger(void *arg
, const char *fmt
, va_list ap
) {
26 f
= strjoina("microhttpd: ", fmt
);
28 DISABLE_WARNING_FORMAT_NONLITERAL
;
29 log_internalv(LOG_INFO
, 0, NULL
, 0, NULL
, f
, ap
);
33 static int mhd_respond_internal(struct MHD_Connection
*connection
,
34 enum MHD_RequestTerminationCode code
,
37 enum MHD_ResponseMemoryMode mode
) {
38 struct MHD_Response
*response
;
43 response
= MHD_create_response_from_buffer(size
, (char*) buffer
, mode
);
47 log_debug("Queueing response %u: %s", code
, buffer
);
48 MHD_add_response_header(response
, "Content-Type", "text/plain");
49 r
= MHD_queue_response(connection
, code
, response
);
50 MHD_destroy_response(response
);
55 int mhd_respond(struct MHD_Connection
*connection
,
56 enum MHD_RequestTerminationCode code
,
57 const char *message
) {
61 fmt
= strjoina(message
, "\n");
63 return mhd_respond_internal(connection
, code
,
64 fmt
, strlen(message
) + 1,
65 MHD_RESPMEM_PERSISTENT
);
68 int mhd_respond_oom(struct MHD_Connection
*connection
) {
69 return mhd_respond(connection
, MHD_HTTP_SERVICE_UNAVAILABLE
, "Out of memory.");
72 int mhd_respondf(struct MHD_Connection
*connection
,
74 enum MHD_RequestTerminationCode code
,
75 const char *format
, ...) {
88 fmt
= strjoina(format
, "\n");
90 #pragma GCC diagnostic push
91 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
92 r
= vasprintf(&m
, fmt
, ap
);
93 #pragma GCC diagnostic pop
97 return respond_oom(connection
);
99 return mhd_respond_internal(connection
, code
, m
, r
, MHD_RESPMEM_MUST_FREE
);
105 const char *const names
[4];
108 } gnutls_log_map
[] = {
109 { {"0"}, LOG_DEBUG
},
110 { {"1", "audit"}, LOG_WARNING
, true}, /* gnutls session audit */
111 { {"2", "assert"}, LOG_DEBUG
}, /* gnutls assert log */
112 { {"3", "hsk", "ext"}, LOG_DEBUG
}, /* gnutls handshake log */
113 { {"4", "rec"}, LOG_DEBUG
}, /* gnutls record log */
114 { {"5", "dtls"}, LOG_DEBUG
}, /* gnutls DTLS log */
115 { {"6", "buf"}, LOG_DEBUG
},
116 { {"7", "write", "read"}, LOG_DEBUG
},
117 { {"8"}, LOG_DEBUG
},
118 { {"9", "enc", "int"}, LOG_DEBUG
},
121 static void log_func_gnutls(int level
, const char *message
) {
124 if (0 <= level
&& level
< (int) ELEMENTSOF(gnutls_log_map
)) {
125 if (gnutls_log_map
[level
].enabled
)
126 log_internal(gnutls_log_map
[level
].level
, 0, NULL
, 0, NULL
, "gnutls %d/%s: %s", level
, gnutls_log_map
[level
].names
[1], message
);
128 log_debug("Received GNUTLS message with unknown level %d.", level
);
129 log_internal(LOG_DEBUG
, 0, NULL
, 0, NULL
, "gnutls: %s", message
);
133 static void log_reset_gnutls_level(void) {
136 for (i
= ELEMENTSOF(gnutls_log_map
) - 1; i
>= 0; i
--)
137 if (gnutls_log_map
[i
].enabled
) {
138 log_debug("Setting gnutls log level to %d", i
);
139 gnutls_global_set_log_level(i
);
144 static int log_enable_gnutls_category(const char *cat
) {
147 if (streq(cat
, "all")) {
148 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
149 gnutls_log_map
[i
].enabled
= true;
150 log_reset_gnutls_level();
153 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
154 if (strv_contains((char**)gnutls_log_map
[i
].names
, cat
)) {
155 gnutls_log_map
[i
].enabled
= true;
156 log_reset_gnutls_level();
159 log_error("No such log category: %s", cat
);
163 int setup_gnutls_logger(char **categories
) {
167 gnutls_global_set_log_function(log_func_gnutls
);
170 STRV_FOREACH(cat
, categories
) {
171 r
= log_enable_gnutls_category(*cat
);
176 log_reset_gnutls_level();
181 static int verify_cert_authorized(gnutls_session_t session
) {
183 gnutls_certificate_type_t type
;
187 r
= gnutls_certificate_verify_peers2(session
, &status
);
189 return log_error_errno(r
, "gnutls_certificate_verify_peers2 failed: %m");
191 type
= gnutls_certificate_type_get(session
);
192 r
= gnutls_certificate_verification_status_print(status
, type
, &out
, 0);
194 return log_error_errno(r
, "gnutls_certificate_verification_status_print failed: %m");
196 log_debug("Certificate status: %s", out
.data
);
197 gnutls_free(out
.data
);
199 return status
== 0 ? 0 : -EPERM
;
202 static int get_client_cert(gnutls_session_t session
, gnutls_x509_crt_t
*client_cert
) {
203 const gnutls_datum_t
*pcert
;
205 gnutls_x509_crt_t cert
;
211 pcert
= gnutls_certificate_get_peers(session
, &listsize
);
212 if (!pcert
|| !listsize
) {
213 log_error("Failed to retrieve certificate chain");
217 r
= gnutls_x509_crt_init(&cert
);
219 log_error("Failed to initialize client certificate");
223 /* Note that by passing values between 0 and listsize here, you
224 can get access to the CA's certs */
225 r
= gnutls_x509_crt_import(cert
, &pcert
[0], GNUTLS_X509_FMT_DER
);
227 log_error("Failed to import client certificate");
228 gnutls_x509_crt_deinit(cert
);
236 static int get_auth_dn(gnutls_x509_crt_t client_cert
, char **buf
) {
241 assert(*buf
== NULL
);
243 r
= gnutls_x509_crt_get_dn(client_cert
, NULL
, &len
);
244 if (r
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
245 log_error("gnutls_x509_crt_get_dn failed");
253 gnutls_x509_crt_get_dn(client_cert
, *buf
, &len
);
257 static inline void gnutls_x509_crt_deinitp(gnutls_x509_crt_t
*p
) {
258 gnutls_x509_crt_deinit(*p
);
261 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
262 const union MHD_ConnectionInfo
*ci
;
263 gnutls_session_t session
;
264 _cleanup_(gnutls_x509_crt_deinitp
) gnutls_x509_crt_t client_cert
= NULL
;
265 _cleanup_free_
char *buf
= NULL
;
273 ci
= MHD_get_connection_info(connection
,
274 MHD_CONNECTION_INFO_GNUTLS_SESSION
);
276 log_error("MHD_get_connection_info failed: session is unencrypted");
277 *code
= mhd_respond(connection
, MHD_HTTP_FORBIDDEN
,
278 "Encrypted connection is required");
281 session
= ci
->tls_session
;
284 r
= get_client_cert(session
, &client_cert
);
286 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
287 "Authorization through certificate is required");
291 r
= get_auth_dn(client_cert
, &buf
);
293 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
294 "Failed to determine distinguished name from certificate");
298 log_debug("Connection from %s", buf
);
301 *hostname
= TAKE_PTR(buf
);
303 r
= verify_cert_authorized(session
);
305 log_warning("Client is not authorized");
306 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
307 "Client certificate not signed by recognized authority");
313 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
317 int setup_gnutls_logger(char **categories
) {
319 log_notice("Ignoring specified gnutls logging categories — gnutls not available.");