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
) {
37 _cleanup_(MHD_destroy_responsep
) struct MHD_Response
*response
38 = MHD_create_response_from_buffer(size
, (char*) buffer
, mode
);
42 log_debug("Queueing response %u: %s", code
, buffer
);
43 MHD_add_response_header(response
, "Content-Type", "text/plain");
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 #pragma GCC diagnostic push
83 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
84 r
= vasprintf(&m
, fmt
, ap
);
85 #pragma GCC diagnostic pop
89 return respond_oom(connection
);
91 return mhd_respond_internal(connection
, code
, m
, r
, MHD_RESPMEM_MUST_FREE
);
97 const char *const names
[4];
100 } gnutls_log_map
[] = {
101 { {"0"}, LOG_DEBUG
},
102 { {"1", "audit"}, LOG_WARNING
, true}, /* gnutls session audit */
103 { {"2", "assert"}, LOG_DEBUG
}, /* gnutls assert log */
104 { {"3", "hsk", "ext"}, LOG_DEBUG
}, /* gnutls handshake log */
105 { {"4", "rec"}, LOG_DEBUG
}, /* gnutls record log */
106 { {"5", "dtls"}, LOG_DEBUG
}, /* gnutls DTLS log */
107 { {"6", "buf"}, LOG_DEBUG
},
108 { {"7", "write", "read"}, LOG_DEBUG
},
109 { {"8"}, LOG_DEBUG
},
110 { {"9", "enc", "int"}, LOG_DEBUG
},
113 static void log_func_gnutls(int level
, const char *message
) {
116 if (0 <= level
&& level
< (int) ELEMENTSOF(gnutls_log_map
)) {
117 if (gnutls_log_map
[level
].enabled
)
118 log_internal(gnutls_log_map
[level
].level
, 0, NULL
, 0, NULL
, "gnutls %d/%s: %s", level
, gnutls_log_map
[level
].names
[1], message
);
120 log_debug("Received GNUTLS message with unknown level %d.", level
);
121 log_internal(LOG_DEBUG
, 0, NULL
, 0, NULL
, "gnutls: %s", message
);
125 static void log_reset_gnutls_level(void) {
128 for (i
= ELEMENTSOF(gnutls_log_map
) - 1; i
>= 0; i
--)
129 if (gnutls_log_map
[i
].enabled
) {
130 log_debug("Setting gnutls log level to %d", i
);
131 gnutls_global_set_log_level(i
);
136 static int log_enable_gnutls_category(const char *cat
) {
139 if (streq(cat
, "all")) {
140 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
141 gnutls_log_map
[i
].enabled
= true;
142 log_reset_gnutls_level();
145 for (i
= 0; i
< ELEMENTSOF(gnutls_log_map
); i
++)
146 if (strv_contains((char**)gnutls_log_map
[i
].names
, cat
)) {
147 gnutls_log_map
[i
].enabled
= true;
148 log_reset_gnutls_level();
151 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No such log category: %s", cat
);
154 int setup_gnutls_logger(char **categories
) {
158 gnutls_global_set_log_function(log_func_gnutls
);
161 STRV_FOREACH(cat
, categories
) {
162 r
= log_enable_gnutls_category(*cat
);
167 log_reset_gnutls_level();
172 static int verify_cert_authorized(gnutls_session_t session
) {
174 gnutls_certificate_type_t type
;
178 r
= gnutls_certificate_verify_peers2(session
, &status
);
180 return log_error_errno(r
, "gnutls_certificate_verify_peers2 failed: %m");
182 type
= gnutls_certificate_type_get(session
);
183 r
= gnutls_certificate_verification_status_print(status
, type
, &out
, 0);
185 return log_error_errno(r
, "gnutls_certificate_verification_status_print failed: %m");
187 log_debug("Certificate status: %s", out
.data
);
188 gnutls_free(out
.data
);
190 return status
== 0 ? 0 : -EPERM
;
193 static int get_client_cert(gnutls_session_t session
, gnutls_x509_crt_t
*client_cert
) {
194 const gnutls_datum_t
*pcert
;
196 gnutls_x509_crt_t cert
;
202 pcert
= gnutls_certificate_get_peers(session
, &listsize
);
203 if (!pcert
|| !listsize
)
204 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
205 "Failed to retrieve certificate chain");
207 r
= gnutls_x509_crt_init(&cert
);
209 log_error("Failed to initialize client certificate");
213 /* Note that by passing values between 0 and listsize here, you
214 can get access to the CA's certs */
215 r
= gnutls_x509_crt_import(cert
, &pcert
[0], GNUTLS_X509_FMT_DER
);
217 log_error("Failed to import client certificate");
218 gnutls_x509_crt_deinit(cert
);
226 static int get_auth_dn(gnutls_x509_crt_t client_cert
, char **buf
) {
231 assert(*buf
== NULL
);
233 r
= gnutls_x509_crt_get_dn(client_cert
, NULL
, &len
);
234 if (r
!= GNUTLS_E_SHORT_MEMORY_BUFFER
) {
235 log_error("gnutls_x509_crt_get_dn failed");
243 gnutls_x509_crt_get_dn(client_cert
, *buf
, &len
);
247 static void gnutls_x509_crt_deinitp(gnutls_x509_crt_t
*p
) {
248 gnutls_x509_crt_deinit(*p
);
251 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
252 const union MHD_ConnectionInfo
*ci
;
253 gnutls_session_t session
;
254 _cleanup_(gnutls_x509_crt_deinitp
) gnutls_x509_crt_t client_cert
= NULL
;
255 _cleanup_free_
char *buf
= NULL
;
263 ci
= MHD_get_connection_info(connection
,
264 MHD_CONNECTION_INFO_GNUTLS_SESSION
);
266 log_error("MHD_get_connection_info failed: session is unencrypted");
267 *code
= mhd_respond(connection
, MHD_HTTP_FORBIDDEN
,
268 "Encrypted connection is required");
271 session
= ci
->tls_session
;
274 r
= get_client_cert(session
, &client_cert
);
276 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
277 "Authorization through certificate is required");
281 r
= get_auth_dn(client_cert
, &buf
);
283 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
284 "Failed to determine distinguished name from certificate");
288 log_debug("Connection from %s", buf
);
291 *hostname
= TAKE_PTR(buf
);
293 r
= verify_cert_authorized(session
);
295 log_warning("Client is not authorized");
296 *code
= mhd_respond(connection
, MHD_HTTP_UNAUTHORIZED
,
297 "Client certificate not signed by recognized authority");
303 int check_permissions(struct MHD_Connection
*connection
, int *code
, char **hostname
) {
307 int setup_gnutls_logger(char **categories
) {
309 log_notice("Ignoring specified gnutls logging categories — gnutls not available.");