libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

test_cert_tls.c (5952B)


      1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2016, 2024 Christian Grothoff & Evgeny Grin (Karlson2k)
      5 
      6   GNU libmicrohttpd is free software; you can redistribute it and/or
      7   modify it under the terms of the GNU Lesser General Public
      8   License as published by the Free Software Foundation; either
      9   version 2.1 of the License, or (at your option) any later version.
     10 
     11   GNU libmicrohttpd is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15 
     16   Alternatively, you can redistribute GNU libmicrohttpd and/or
     17   modify it under the terms of the GNU General Public License as
     18   published by the Free Software Foundation; either version 2 of
     19   the License, or (at your option) any later version, together
     20   with the eCos exception, as follows:
     21 
     22     As a special exception, if other files instantiate templates or
     23     use macros or inline functions from this file, or you compile this
     24     file and link it with other works to produce a work based on this
     25     file, this file does not by itself cause the resulting work to be
     26     covered by the GNU General Public License. However the source code
     27     for this file must still be made available in accordance with
     28     section (3) of the GNU General Public License v2.
     29 
     30     This exception does not invalidate any other reasons why a work
     31     based on this file might be covered by the GNU General Public
     32     License.
     33 
     34   You should have received copies of the GNU Lesser General Public
     35   License and the GNU General Public License along with this library;
     36   if not, see <https://www.gnu.org/licenses/>.
     37 */
     38 
     39 /**
     40  * @file test_tls.c
     41  * @brief  test with client against TLS server
     42  * @author Christian Grothoff
     43  */
     44 #include "libtest.h"
     45 
     46 
     47 int
     48 main (int argc, char *argv[])
     49 {
     50   char *srv_certs_chain
     51     = MHDT_load_pem ("chain.crt");
     52   char *srv_cert_key
     53     = MHDT_load_pem ("test-server-key.pem");
     54   struct MHD_DaemonOptionAndValue rca_options[] = {
     55     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     56     MHD_D_OPTION_WM_WORKER_THREADS (1),
     57     MHD_D_OPTION_TLS (MHD_TLS_BACKEND_ANY),
     58     MHD_D_OPTION_TLS_CERT_KEY (srv_certs_chain,
     59                                srv_cert_key,
     60                                NULL),
     61     MHD_D_OPTION_TERMINATE ()
     62   };
     63 #ifdef MHD_SUPPORT_GNUTLS
     64   struct MHD_DaemonOptionAndValue rca_options_gnu[] = {
     65     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     66     MHD_D_OPTION_WM_WORKER_THREADS (1),
     67     MHD_D_OPTION_TLS (MHD_TLS_BACKEND_GNUTLS),
     68     MHD_D_OPTION_TLS_CERT_KEY (srv_certs_chain,
     69                                srv_cert_key,
     70                                NULL),
     71     MHD_D_OPTION_TERMINATE ()
     72   };
     73 #endif
     74 #ifdef MHD_SUPPORT_OPENSSL
     75   struct MHD_DaemonOptionAndValue rca_options_open[] = {
     76     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     77     MHD_D_OPTION_WM_WORKER_THREADS (1),
     78     MHD_D_OPTION_TLS (MHD_TLS_BACKEND_OPENSSL),
     79     MHD_D_OPTION_TLS_CERT_KEY (srv_certs_chain,
     80                                srv_cert_key,
     81                                NULL),
     82     MHD_D_OPTION_TERMINATE ()
     83   };
     84 #endif
     85   struct ServerType
     86   {
     87     const char *label;
     88     MHDT_ServerSetup server_setup;
     89     void *server_setup_cls;
     90     MHDT_ServerRunner server_runner;
     91     void *server_runner_cls;
     92     struct MHDT_Phase phase;
     93   } configs[] = {
     94     {
     95       .label = "certs_chain",
     96       .server_setup = &MHDT_server_setup_minimal,
     97       .server_setup_cls = rca_options,
     98       .server_runner = &MHDT_server_run_minimal,
     99       .phase = {
    100         .label = "simple RCA get",
    101         .server_cb = &MHDT_server_reply_text,
    102         .server_cb_cls = (void *) "Hello world",
    103         .client_cb = &MHDT_client_get_host,
    104         .client_cb_cls = "localhost",
    105         .timeout_ms = 2500,
    106         .use_tls = true,
    107         .check_server_cert = true
    108       }
    109 
    110 
    111     },
    112 #ifdef MHD_SUPPORT_GNUTLS
    113     {
    114       .label = "certs_chain",
    115       .server_setup = &MHDT_server_setup_minimal,
    116       .server_setup_cls = rca_options_gnu,
    117       .server_runner = &MHDT_server_run_minimal,
    118       .phase = {
    119         .label = "simple RCA get",
    120         .server_cb = &MHDT_server_reply_text,
    121         .server_cb_cls = (void *) "Hello world",
    122         .client_cb = &MHDT_client_get_host,
    123         .client_cb_cls = "localhost",
    124         .timeout_ms = 2500,
    125         .use_tls = true,
    126         .check_server_cert = true
    127       }
    128 
    129 
    130     },
    131 #endif
    132 #ifdef MHD_SUPPORT_OPENSSL
    133     {
    134       .label = "certs_chain",
    135       .server_setup = &MHDT_server_setup_minimal,
    136       .server_setup_cls = rca_options_open,
    137       .server_runner = &MHDT_server_run_minimal,
    138       .phase = {
    139         .label = "simple RCA get",
    140         .server_cb = &MHDT_server_reply_text,
    141         .server_cb_cls = (void *) "Hello world",
    142         .client_cb = &MHDT_client_get_host,
    143         .client_cb_cls = "localhost",
    144         .timeout_ms = 2500,
    145         .use_tls = true,
    146         .check_server_cert = true
    147       }
    148 
    149 
    150     },
    151 #endif
    152     {
    153       .label = "END"
    154     }
    155   };
    156   unsigned int i;
    157   int ret = 0;
    158 
    159   (void) argc; /* Unused. Silence compiler warning. */
    160   (void) argv; /* Unused. Silence compiler warning. */
    161 
    162   for (i = 0; NULL != configs[i].server_setup; i++)
    163   {
    164     struct ServerType *st = &configs[i];
    165     struct MHDT_Phase phases[2] = {
    166       st->phase
    167     };
    168     fprintf (stderr,
    169              "Running TLS tests with server setup '%s'\n",
    170              st->label);
    171     ret = MHDT_test (st->server_setup,
    172                      st->server_setup_cls,
    173                      st->server_runner,
    174                      st->server_runner_cls,
    175                      phases);
    176     if (0 != ret)
    177     {
    178       fprintf (stderr,
    179                "Test failed with server of type '%s' (%u)\n",
    180                st->label,
    181                i);
    182       break;
    183     }
    184   }
    185   free (srv_cert_key);
    186   free (srv_certs_chain);
    187   return ret;
    188 }