1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <gnutls/gnutls.h>
9 #include <gnutls/x509.h>
12 #include "alloc-util.h"
15 #include "microhttpd-util.h"
16 #include "string-util.h"
20 void microhttpd_logger(void *arg
, const char *fmt
, va_list ap
) {
23 f
= strjoina("microhttpd: ", fmt
);
25 DISABLE_WARNING_FORMAT_NONLITERAL
;
26 log_internalv(LOG_INFO
, 0, NULL
, 0, NULL
, f
, ap
);
30 static int mhd_respond_internal(struct MHD_Connection
*connection
,
31 enum MHD_RequestTerminationCode code
,
34 enum MHD_ResponseMemoryMode mode
) {
35 struct MHD_Response
*response
;
40 response
= MHD_create_response_from_buffer(size
, (char*) buffer
, mode
);
44 log_debug("Queueing response %u: %s", code
, buffer
);
45 MHD_add_response_header(response
, "Content-Type", "text/plain");
46 r
= MHD_queue_response(connection
, code
, response
);
47 MHD_destroy_response(response
);
52 int mhd_respond(struct MHD_Connection
*connection
,
53 enum MHD_RequestTerminationCode code
,
54 const char *message
) {
58 fmt
= strjoina(message
, "\n");
60 return mhd_respond_internal(connection
, code
,
61 fmt
, strlen(message
) + 1,
62 MHD_RESPMEM_PERSISTENT
);
65 int mhd_respond_oom(struct MHD_Connection
*connection
) {
66 return mhd_respond(connection
, MHD_HTTP_SERVICE_UNAVAILABLE
, "Out of memory.");
69 int mhd_respondf(struct MHD_Connection
*connection
,
71 enum MHD_RequestTerminationCode code
,
72 const char *format
, ...) {
85 fmt
= strjoina(format
, "\n");
87 #pragma GCC diagnostic push
88 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
89 r
= vasprintf(&m
, fmt
, ap
);
90 #pragma GCC diagnostic pop
94 return respond_oom(connection
);
96 return mhd_respond_internal(connection
, code
, m
, r
, MHD_RESPMEM_MUST_FREE
);
102 const char *const names
[4];
105 } gnutls_log_map
[] = {
106 { {"0"}, LOG_DEBUG
},
107 { {"1", "audit"}, LOG_WARNING
, true}, /* gnutls session audit */
108 { {"2", "assert"}, LOG_DEBUG
}, /* gnutls assert log */
109 { {"3", "hsk", "ext"}, LOG_DEBUG
}, /* gnutls handshake log */
110 { {"4", "rec"}, LOG_DEBUG
}, /* gnutls record log */
111 { {"5", "dtls"}, LOG_DEBUG
}, /* gnutls DTLS log */
112 { {"6", "buf"}, LOG_DEBUG
},
113 { {"7", "write", "read"}, LOG_DEBUG
},
114 { {"8"}, LOG_DEBUG
},
115 { {"9", "enc", "int"}, LOG_DEBUG
},
118 static void log_func_gnutls(int level
, const char *message
) {
121 if (0 <= level
&& level
< (int) ELEMENTSOF(gnutls_log_map
)) {
122 if (gnutls_log_map
[level
].enabled
)
123 log_internal(gnutls_log_map
[level
].level
, 0, NULL
, 0, NULL
, "gnutls %d/%s: %s", level
, gnutls_log_map
[level
].names
[1], message
);
125 log_debug("Received GNUTLS message with unknown level %d.", level
);
126 log_internal(LOG_DEBUG
, 0, NULL
, 0, NULL
, "gnutls: %s", message
);
130 static void log_reset_gnutls_level(void) {
133 for (i
= ELEMENTSOF(gnutls_log_map
) - 1; i
>= 0; i
--)
134 if (gnutls_log_map
[i
].enabled
) {
135 log_debug("Setting gnutls log level to %d", i
);
136 gnutls_global_set_log_level(i
);
141 static int log_enable_gnutls_category(const char *cat
) {
144 if (streq(cat
, "all")) {
145 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
146 gnutls_log_map
[i
].enabled
= true;
147 log_reset_gnutls_level();
150 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
151 if (strv_contains((char**)gnutls_log_map
[i
].names
, cat
)) {
152 gnutls_log_map
[i
].enabled
= true;
153 log_reset_gnutls_level();
156 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No such log category: %s", cat
);
159 int setup_gnutls_logger(char **categories
) {
163 gnutls_global_set_log_function(log_func_gnutls
);
166 STRV_FOREACH(cat
, categories
) {
167 r
= log_enable_gnutls_category(*cat
);
172 log_reset_gnutls_level();
177 static int verify_cert_authorized(gnutls_session_t session
) {
179 gnutls_certificate_type_t type
;
183 r
= gnutls_certificate_verify_peers2(session
, &status
);
185 return log_error_errno(r
, "gnutls_certificate_verify_peers2 failed: %m");
187 type
= gnutls_certificate_type_get(session
);
188 r
= gnutls_certificate_verification_status_print(status
, type
, &out
, 0);
190 return log_error_errno(r
, "gnutls_certificate_verification_status_print failed: %m");
192 log_debug("Certificate status: %s", out
.data
);
193 gnutls_free(out
.data
);
195 return status
== 0 ? 0 : -EPERM
;
198 static int get_client_cert(gnutls_session_t session
, gnutls_x509_crt_t
*client_cert
) {
199 const gnutls_datum_t
*pcert
;
201 gnutls_x509_crt_t cert
;
207 pcert
= gnutls_certificate_get_peers(session
, &listsize
);
208 if (!pcert
|| !listsize
)
209 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
210 "Failed to retrieve certificate chain");
212 r
= gnutls_x509_crt_init(&cert
);
214 log_error("Failed to initialize client certificate");
218 /* Note that by passing values between 0 and listsize here, you
219 can get access to the CA's certs */
220 r
= gnutls_x509_crt_import(cert
, &pcert
[0], GNUTLS_X509_FMT_DER
);
222 log_error("Failed to import client certificate");
223 gnutls_x509_crt_deinit(cert
);
231 static int get_auth_dn(gnutls_x509_crt_t client_cert
, char **buf
) {
236 assert(*buf
== NULL
);
238 r
= gnutls_x509_crt_get_dn(client_cert
, NULL
, &len
);
239 if (r
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
240 log_error("gnutls_x509_crt_get_dn failed");
248 gnutls_x509_crt_get_dn(client_cert
, *buf
, &len
);
252 static inline void gnutls_x509_crt_deinitp(gnutls_x509_crt_t
*p
) {
253 gnutls_x509_crt_deinit(*p
);
256 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
257 const union MHD_ConnectionInfo
*ci
;
258 gnutls_session_t session
;
259 _cleanup_(gnutls_x509_crt_deinitp
) gnutls_x509_crt_t client_cert
= NULL
;
260 _cleanup_free_
char *buf
= NULL
;
268 ci
= MHD_get_connection_info(connection
,
269 MHD_CONNECTION_INFO_GNUTLS_SESSION
);
271 log_error("MHD_get_connection_info failed: session is unencrypted");
272 *code
= mhd_respond(connection
, MHD_HTTP_FORBIDDEN
,
273 "Encrypted connection is required");
276 session
= ci
->tls_session
;
279 r
= get_client_cert(session
, &client_cert
);
281 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
282 "Authorization through certificate is required");
286 r
= get_auth_dn(client_cert
, &buf
);
288 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
289 "Failed to determine distinguished name from certificate");
293 log_debug("Connection from %s", buf
);
296 *hostname
= TAKE_PTR(buf
);
298 r
= verify_cert_authorized(session
);
300 log_warning("Client is not authorized");
301 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
302 "Client certificate not signed by recognized authority");
308 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
312 int setup_gnutls_logger(char **categories
) {
314 log_notice("Ignoring specified gnutls logging categories — gnutls not available.");