LCOV - code coverage report
Current view: top level - glib/gio/tests - tls-certificate.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 397 397 100.0 %
Date: 2024-04-23 05:16:05 Functions: 20 20 100.0 %
Branches: 3 4 75.0 %

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  *
       3                 :            :  * Copyright (C) 2011 Collabora Ltd.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  *
      20                 :            :  * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : 
      25                 :            : #include <gio/gio.h>
      26                 :            : 
      27                 :            : #include "gtesttlsbackend.h"
      28                 :            : 
      29                 :            : typedef struct
      30                 :            : {
      31                 :            :   gchar *cert_pems[3];
      32                 :            :   gchar *cert_crlf_pem;
      33                 :            :   gchar *key_pem;
      34                 :            :   gchar *key_crlf_pem;
      35                 :            :   gchar *key8_pem;
      36                 :            : } Reference;
      37                 :            : 
      38                 :            : static void
      39                 :          1 : pem_parser (const Reference *ref)
      40                 :            : {
      41                 :            :   GTlsCertificate *cert;
      42                 :            :   gchar *pem;
      43                 :          1 :   gsize pem_len = 0;
      44                 :          1 :   gchar *parsed_cert_pem = NULL;
      45                 :          1 :   gchar *parsed_key_pem = NULL;
      46                 :          1 :   GError *error = NULL;
      47                 :            : 
      48                 :            :   /* Check PEM parsing in certificate, private key order. */
      49                 :          1 :   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert-key.pem", NULL), &pem, &pem_len, &error);
      50                 :          1 :   g_assert_no_error (error);
      51                 :          1 :   g_assert_nonnull (pem);
      52                 :          1 :   g_assert_cmpuint (pem_len, >=, 10);
      53                 :            : 
      54                 :          1 :   cert = g_tls_certificate_new_from_pem (pem, -1, &error);
      55                 :          1 :   g_assert_no_error (error);
      56                 :          1 :   g_assert_nonnull (cert);
      57                 :            : 
      58                 :          1 :   g_object_get (cert,
      59                 :            :       "certificate-pem", &parsed_cert_pem,
      60                 :            :       "private-key-pem", &parsed_key_pem,
      61                 :            :       NULL);
      62                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
      63                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
      64                 :          1 :   g_assert_cmpstr (parsed_key_pem, ==, ref->key_pem);
      65                 :          1 :   g_clear_pointer (&parsed_key_pem, g_free);
      66                 :            : 
      67                 :          1 :   g_object_unref (cert);
      68                 :            : 
      69                 :            :   /* Make sure length is respected and parser detect invalid PEM
      70                 :            :    * when cert is truncated. */
      71                 :          1 :   cert = g_tls_certificate_new_from_pem (pem, 10, &error);
      72                 :          1 :   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
      73                 :          1 :   g_clear_error (&error);
      74                 :            : 
      75                 :            :   /* Make sure length is respected and parser detect invalid PEM
      76                 :            :    * when cert exists but key is truncated. */
      77                 :          1 :   cert = g_tls_certificate_new_from_pem (pem, pem_len - 10, &error);
      78                 :          1 :   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
      79                 :          1 :   g_clear_error (&error);
      80                 :          1 :   g_free (pem);
      81                 :            : 
      82                 :            :   /* Check PEM parsing in private key, certificate order */
      83                 :          1 :   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "key-cert.pem", NULL), &pem, NULL, &error);
      84                 :          1 :   g_assert_no_error (error);
      85                 :          1 :   g_assert_nonnull (pem);
      86                 :            : 
      87                 :          1 :   cert = g_tls_certificate_new_from_pem (pem, -1, &error);
      88                 :          1 :   g_assert_no_error (error);
      89                 :          1 :   g_assert_nonnull (cert);
      90                 :            : 
      91                 :          1 :   g_object_get (cert,
      92                 :            :       "certificate-pem", &parsed_cert_pem,
      93                 :            :       "private-key-pem", &parsed_key_pem,
      94                 :            :       NULL);
      95                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
      96                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
      97                 :          1 :   g_assert_cmpstr (parsed_key_pem, ==, ref->key_pem);
      98                 :          1 :   g_clear_pointer (&parsed_key_pem, g_free);
      99                 :            : 
     100                 :          1 :   g_free (pem);
     101                 :          1 :   g_object_unref (cert);
     102                 :            : 
     103                 :            :   /* Check certificate only PEM */
     104                 :          1 :   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL), &pem, NULL, &error);
     105                 :          1 :   g_assert_no_error (error);
     106                 :          1 :   g_assert_nonnull (pem);
     107                 :            : 
     108                 :          1 :   cert = g_tls_certificate_new_from_pem (pem, -1, &error);
     109                 :          1 :   g_assert_no_error (error);
     110                 :          1 :   g_assert_nonnull (cert);
     111                 :            : 
     112                 :          1 :   g_object_get (cert,
     113                 :            :       "certificate-pem", &parsed_cert_pem,
     114                 :            :       "private-key-pem", &parsed_key_pem,
     115                 :            :       NULL);
     116                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
     117                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
     118                 :          1 :   g_assert_null (parsed_key_pem);
     119                 :            : 
     120                 :          1 :   g_free (pem);
     121                 :          1 :   g_object_unref (cert);
     122                 :            : 
     123                 :            :   /* Check error with private key only PEM */
     124                 :          1 :   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL), &pem, NULL, &error);
     125                 :          1 :   g_assert_no_error (error);
     126                 :          1 :   g_assert_nonnull (pem);
     127                 :            : 
     128                 :          1 :   cert = g_tls_certificate_new_from_pem (pem, -1, &error);
     129                 :          1 :   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
     130                 :          1 :   g_clear_error (&error);
     131                 :          1 :   g_assert_null (cert);
     132                 :          1 :   g_free (pem);
     133                 :          1 : }
     134                 :            : 
     135                 :            : static void
     136                 :          1 : pem_parser_handles_chain (const Reference *ref)
     137                 :            : {
     138                 :            :   GTlsCertificate *cert;
     139                 :            :   GTlsCertificate *issuer;
     140                 :            :   GTlsCertificate *original_cert;
     141                 :            :   gchar *pem;
     142                 :          1 :   gchar *parsed_cert_pem = NULL;
     143                 :          1 :   gchar *parsed_key_pem = NULL;
     144                 :          1 :   GError *error = NULL;
     145                 :            : 
     146                 :            :   /* Check that a chain with exactly three certificates is returned */
     147                 :          1 :   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert-list.pem", NULL), &pem, NULL, &error);
     148                 :          1 :   g_assert_no_error (error);
     149                 :          1 :   g_assert_nonnull (pem);
     150                 :            : 
     151                 :          1 :   cert = original_cert = g_tls_certificate_new_from_pem (pem, -1, &error);
     152                 :          1 :   g_free (pem);
     153                 :          1 :   g_assert_no_error (error);
     154                 :          1 :   g_assert_nonnull (cert);
     155                 :            : 
     156                 :          1 :   g_object_get (cert,
     157                 :            :       "certificate-pem", &parsed_cert_pem,
     158                 :            :       "private-key-pem", &parsed_key_pem,
     159                 :            :       NULL);
     160                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
     161                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
     162                 :            : 
     163                 :            :   /* Make sure the private key was parsed */
     164                 :          1 :   g_assert_cmpstr (parsed_key_pem, ==, ref->key_pem);
     165                 :          1 :   g_clear_pointer (&parsed_key_pem, g_free);
     166                 :            : 
     167                 :            :   /* Now test the second cert */
     168                 :          1 :   issuer = g_tls_certificate_get_issuer (cert);
     169                 :          1 :   g_assert_nonnull (issuer);
     170                 :            : 
     171                 :          1 :   cert = issuer;
     172                 :          1 :   issuer = g_tls_certificate_get_issuer (cert);
     173                 :          1 :   g_assert_nonnull (issuer);
     174                 :            : 
     175                 :          1 :   g_object_get (cert,
     176                 :            :       "certificate-pem", &parsed_cert_pem,
     177                 :            :       "private-key-pem", &parsed_key_pem,
     178                 :            :       NULL);
     179                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[1]);
     180                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
     181                 :            : 
     182                 :            :   /* Only the first cert should have a private key */
     183                 :          1 :   g_assert_null (parsed_key_pem);
     184                 :            : 
     185                 :            :   /* Now test the final cert */
     186                 :          1 :   cert = issuer;
     187                 :          1 :   issuer = g_tls_certificate_get_issuer (cert);
     188                 :          1 :   g_assert_null (issuer);
     189                 :            : 
     190                 :          1 :   g_object_get (cert,
     191                 :            :       "certificate-pem", &parsed_cert_pem,
     192                 :            :       "private-key-pem", &parsed_key_pem,
     193                 :            :       NULL);
     194                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[2]);
     195                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
     196                 :            : 
     197                 :          1 :   g_assert_null (parsed_key_pem);
     198                 :            : 
     199                 :          1 :   g_object_unref (original_cert);
     200                 :          1 : }
     201                 :            : 
     202                 :            : static void
     203                 :          1 : pem_parser_no_sentinel (void)
     204                 :            : {
     205                 :            :   GTlsCertificate *cert;
     206                 :            :   gchar *pem;
     207                 :          1 :   gsize pem_len = 0;
     208                 :            :   gchar *pem_copy;
     209                 :          1 :   GError *error = NULL;
     210                 :            : 
     211                 :            :   /* Check certificate from not-nul-terminated PEM */
     212                 :          1 :   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL), &pem, &pem_len, &error);
     213                 :          1 :   g_assert_no_error (error);
     214                 :          1 :   g_assert_nonnull (pem);
     215                 :          1 :   g_assert_cmpuint (pem_len, >=, 10);
     216                 :            : 
     217                 :          1 :   pem_copy = g_new (char, pem_len);
     218                 :            :   /* Do not copy the terminating nul: */
     219                 :          1 :   memmove (pem_copy, pem, pem_len);
     220                 :          1 :   g_free (pem);
     221                 :            : 
     222                 :            :   /* Check whether the parser respects the @length parameter.
     223                 :            :    * pem_copy is allocated exactly pem_len bytes, so accessing memory
     224                 :            :    * outside its bounds will be detected by, for example, valgrind or
     225                 :            :    * asan. */
     226                 :          1 :   cert = g_tls_certificate_new_from_pem (pem_copy, pem_len, &error);
     227                 :          1 :   g_assert_no_error (error);
     228                 :          1 :   g_assert_nonnull (cert);
     229                 :            : 
     230                 :          1 :   g_free (pem_copy);
     231                 :          1 :   g_object_unref (cert);
     232                 :          1 : }
     233                 :            : 
     234                 :            : static void
     235                 :          1 : from_file (const Reference *ref)
     236                 :            : {
     237                 :            :   GTlsCertificate *cert;
     238                 :          1 :   gchar *parsed_cert_pem = NULL;
     239                 :          1 :   gchar *parsed_key_pem = NULL;
     240                 :          1 :   GError *error = NULL;
     241                 :            : 
     242                 :          1 :   cert = g_tls_certificate_new_from_file (g_test_get_filename (G_TEST_DIST, "cert-tests", "key-cert.pem", NULL),
     243                 :            :                                           &error);
     244                 :          1 :   g_assert_no_error (error);
     245                 :          1 :   g_assert_nonnull (cert);
     246                 :            : 
     247                 :          1 :   g_object_get (cert,
     248                 :            :       "certificate-pem", &parsed_cert_pem,
     249                 :            :       "private-key-pem", &parsed_key_pem,
     250                 :            :       NULL);
     251                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
     252                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
     253                 :          1 :   g_assert_cmpstr (parsed_key_pem, ==, ref->key_pem);
     254                 :          1 :   g_clear_pointer (&parsed_key_pem, g_free);
     255                 :            : 
     256                 :          1 :   g_object_unref (cert);
     257                 :          1 : }
     258                 :            : 
     259                 :            : static void
     260                 :          1 : from_files (const Reference *ref)
     261                 :            : {
     262                 :            :   GTlsCertificate *cert;
     263                 :          1 :   gchar *parsed_cert_pem = NULL;
     264                 :          1 :   gchar *parsed_key_pem = NULL;
     265                 :          1 :   GError *error = NULL;
     266                 :            : 
     267                 :          1 :   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL),
     268                 :            :                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL),
     269                 :            :                                            &error);
     270                 :          1 :   g_assert_no_error (error);
     271                 :          1 :   g_assert_nonnull (cert);
     272                 :            : 
     273                 :          1 :   g_object_get (cert,
     274                 :            :       "certificate-pem", &parsed_cert_pem,
     275                 :            :       "private-key-pem", &parsed_key_pem,
     276                 :            :       NULL);
     277                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
     278                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
     279                 :          1 :   g_assert_cmpstr (parsed_key_pem, ==, ref->key_pem);
     280                 :          1 :   g_clear_pointer (&parsed_key_pem, g_free);
     281                 :            : 
     282                 :          1 :   g_object_unref (cert);
     283                 :            : 
     284                 :            :   /* Missing private key */
     285                 :          1 :   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL),
     286                 :            :                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "cert2.pem", NULL),
     287                 :            :                                            &error);
     288                 :          1 :   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
     289                 :          1 :   g_clear_error (&error);
     290                 :          1 :   g_assert_null (cert);
     291                 :            : 
     292                 :            :   /* Missing header private key */
     293                 :          1 :   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL),
     294                 :            :                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key_missing-header.pem", NULL),
     295                 :            :                                            &error);
     296                 :          1 :   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
     297                 :          1 :   g_clear_error (&error);
     298                 :          1 :   g_assert_null (cert);
     299                 :            : 
     300                 :            :   /* Missing footer private key */
     301                 :          1 :   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL),
     302                 :            :                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key_missing-footer.pem", NULL),
     303                 :            :                                            &error);
     304                 :          1 :   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
     305                 :          1 :   g_clear_error (&error);
     306                 :          1 :   g_assert_null (cert);
     307                 :            : 
     308                 :            :   /* Missing certificate */
     309                 :          1 :   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL),
     310                 :            :                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL),
     311                 :            :                                            &error);
     312                 :          1 :   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
     313                 :          1 :   g_clear_error (&error);
     314                 :          1 :   g_assert_null (cert);
     315                 :            : 
     316                 :            :   /* Using this method twice with a file containing both private key and
     317                 :            :    * certificate as a way to enforce private key presence is a fair use
     318                 :            :    */
     319                 :          1 :   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "key-cert.pem", NULL),
     320                 :            :                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key-cert.pem", NULL),
     321                 :            :                                            &error);
     322                 :          1 :   g_assert_no_error (error);
     323                 :          1 :   g_assert_nonnull (cert);
     324                 :          1 :   g_object_unref (cert);
     325                 :          1 : }
     326                 :            : 
     327                 :            : static void
     328                 :          1 : from_files_crlf (const Reference *ref)
     329                 :            : {
     330                 :            :   GTlsCertificate *cert;
     331                 :          1 :   gchar *parsed_cert_pem = NULL;
     332                 :          1 :   gchar *parsed_key_pem = NULL;
     333                 :          1 :   GError *error = NULL;
     334                 :            : 
     335                 :          1 :   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert-crlf.pem", NULL),
     336                 :            :                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key-crlf.pem", NULL),
     337                 :            :                                            &error);
     338                 :          1 :   g_assert_no_error (error);
     339                 :          1 :   g_assert_nonnull (cert);
     340                 :            : 
     341                 :          1 :   g_object_get (cert,
     342                 :            :       "certificate-pem", &parsed_cert_pem,
     343                 :            :       "private-key-pem", &parsed_key_pem,
     344                 :            :       NULL);
     345                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_crlf_pem);
     346                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
     347                 :          1 :   g_assert_cmpstr (parsed_key_pem, ==, ref->key_crlf_pem);
     348                 :          1 :   g_clear_pointer (&parsed_key_pem, g_free);
     349                 :            : 
     350                 :          1 :   g_object_unref (cert);
     351                 :          1 : }
     352                 :            : 
     353                 :            : static void
     354                 :          1 : from_files_pkcs8 (const Reference *ref)
     355                 :            : {
     356                 :            :   GTlsCertificate *cert;
     357                 :          1 :   gchar *parsed_cert_pem = NULL;
     358                 :          1 :   gchar *parsed_key_pem = NULL;
     359                 :          1 :   GError *error = NULL;
     360                 :            : 
     361                 :          1 :   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL),
     362                 :            :                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key8.pem", NULL),
     363                 :            :                                            &error);
     364                 :          1 :   g_assert_no_error (error);
     365                 :          1 :   g_assert_nonnull (cert);
     366                 :            : 
     367                 :          1 :   g_object_get (cert,
     368                 :            :       "certificate-pem", &parsed_cert_pem,
     369                 :            :       "private-key-pem", &parsed_key_pem,
     370                 :            :       NULL);
     371                 :          1 :   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
     372                 :          1 :   g_clear_pointer (&parsed_cert_pem, g_free);
     373                 :          1 :   g_assert_cmpstr (parsed_key_pem, ==, ref->key8_pem);
     374                 :          1 :   g_clear_pointer (&parsed_key_pem, g_free);
     375                 :            : 
     376                 :          1 :   g_object_unref (cert);
     377                 :          1 : }
     378                 :            : 
     379                 :            : static void
     380                 :          1 : from_files_pkcs8enc (const Reference *ref)
     381                 :            : {
     382                 :            :   GTlsCertificate *cert;
     383                 :          1 :   GError *error = NULL;
     384                 :            : 
     385                 :            :   /* Mare sure an error is returned for encrypted key */
     386                 :          1 :   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL),
     387                 :            :                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key8enc.pem", NULL),
     388                 :            :                                            &error);
     389                 :          1 :   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
     390                 :          1 :   g_clear_error (&error);
     391                 :          1 :   g_assert_null (cert);
     392                 :          1 : }
     393                 :            : 
     394                 :            : static void
     395                 :          1 : list_from_file (const Reference *ref)
     396                 :            : {
     397                 :            :   GList *list, *l;
     398                 :          1 :   GError *error = NULL;
     399                 :            :   int i;
     400                 :            : 
     401                 :          1 :   list = g_tls_certificate_list_new_from_file (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert-list.pem", NULL),
     402                 :            :                                                &error);
     403                 :          1 :   g_assert_no_error (error);
     404                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 3);
     405                 :            : 
     406                 :          1 :   l = list;
     407         [ +  + ]:          4 :   for (i = 0; i < 3; i++)
     408                 :            :     {
     409                 :          3 :       GTlsCertificate *cert = l->data;
     410                 :          3 :       gchar *parsed_cert_pem = NULL;
     411                 :          3 :       g_object_get (cert,
     412                 :            :           "certificate-pem", &parsed_cert_pem,
     413                 :            :           NULL);
     414                 :          3 :       g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[i]);
     415                 :          3 :       g_free (parsed_cert_pem);
     416         [ +  - ]:          3 :       l = g_list_next (l);
     417                 :            :     }
     418                 :            : 
     419                 :          1 :   g_list_free_full (list, g_object_unref);
     420                 :            : 
     421                 :            :   /* Empty list is not an error */
     422                 :          1 :   list = g_tls_certificate_list_new_from_file (g_test_get_filename (G_TEST_DIST, "cert-tests", "nothing.pem", NULL),
     423                 :            :                                                &error);
     424                 :          1 :   g_assert_no_error (error);
     425                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 0);
     426                 :          1 : }
     427                 :            : 
     428                 :            : static void
     429                 :          1 : from_pkcs11_uri (void)
     430                 :            : {
     431                 :          1 :   GError *error = NULL;
     432                 :            :   GTlsCertificate *cert;
     433                 :          1 :   gchar *pkcs11_uri = NULL;
     434                 :            : 
     435                 :          1 :   cert = g_tls_certificate_new_from_pkcs11_uris ("pkcs11:model=p11-kit-trust;manufacturer=PKCS%2311%20Kit;serial=1;token=ca-bundle.crt", NULL, &error);
     436                 :          1 :   g_assert_no_error (error);
     437                 :          1 :   g_assert_nonnull (cert);
     438                 :            : 
     439                 :          1 :   g_object_get (cert, "pkcs11-uri", &pkcs11_uri, NULL);
     440                 :          1 :   g_assert_cmpstr ("pkcs11:model=p11-kit-trust;manufacturer=PKCS%2311%20Kit;serial=1;token=ca-bundle.crt", ==, pkcs11_uri);
     441                 :          1 :   g_free (pkcs11_uri);
     442                 :            : 
     443                 :          1 :   g_object_unref (cert);
     444                 :          1 : }
     445                 :            : 
     446                 :            : static void
     447                 :          1 : from_unsupported_pkcs11_uri (void)
     448                 :            : {
     449                 :          1 :   GError *error = NULL;
     450                 :            :   GTlsCertificate *cert;
     451                 :            : 
     452                 :            :   /* This is a magic value in gtesttlsbackend.c simulating an unsupported backend */
     453                 :          1 :   cert = g_tls_certificate_new_from_pkcs11_uris ("unsupported", NULL, &error);
     454                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
     455                 :          1 :   g_assert_null (cert);
     456                 :            : 
     457                 :          1 :   g_clear_error (&error);
     458                 :          1 : }
     459                 :            : 
     460                 :            : static void
     461                 :          1 : not_valid_before (void)
     462                 :            : {
     463                 :          1 :   const gchar *EXPECTED_NOT_VALID_BEFORE = "2020-10-12T17:49:44Z";
     464                 :            : 
     465                 :            :   GTlsCertificate *cert;
     466                 :          1 :   GError *error = NULL;
     467                 :            :   GDateTime *actual;
     468                 :            :   gchar *actual_str;
     469                 :            : 
     470                 :          1 :   cert = g_tls_certificate_new_from_pkcs11_uris ("pkcs11:model=p11-kit-trust;manufacturer=PKCS%2311%20Kit;serial=1;token=ca-bundle.crt", NULL, &error);
     471                 :          1 :   g_assert_no_error (error);
     472                 :          1 :   g_assert_nonnull (cert);
     473                 :            : 
     474                 :          1 :   actual = g_tls_certificate_get_not_valid_before (cert);
     475                 :          1 :   g_assert_nonnull (actual);
     476                 :          1 :   actual_str = g_date_time_format_iso8601 (actual);
     477                 :          1 :   g_assert_cmpstr (actual_str, ==, EXPECTED_NOT_VALID_BEFORE);
     478                 :          1 :   g_free (actual_str);
     479                 :          1 :   g_date_time_unref (actual);
     480                 :          1 :   g_object_unref (cert);
     481                 :          1 : }
     482                 :            : 
     483                 :            : static void
     484                 :          1 : not_valid_after (void)
     485                 :            : {
     486                 :          1 :   const gchar *EXPECTED_NOT_VALID_AFTER = "2045-10-06T17:49:44Z";
     487                 :            : 
     488                 :            :   GTlsCertificate *cert;
     489                 :          1 :   GError *error = NULL;
     490                 :            :   GDateTime *actual;
     491                 :            :   gchar *actual_str;
     492                 :            : 
     493                 :          1 :   cert = g_tls_certificate_new_from_pkcs11_uris ("pkcs11:model=p11-kit-trust;manufacturer=PKCS%2311%20Kit;serial=1;token=ca-bundle.crt", NULL, &error);
     494                 :          1 :   g_assert_no_error (error);
     495                 :          1 :   g_assert_nonnull (cert);
     496                 :            : 
     497                 :          1 :   actual = g_tls_certificate_get_not_valid_after (cert);
     498                 :          1 :   g_assert_nonnull (actual);
     499                 :          1 :   actual_str = g_date_time_format_iso8601 (actual);
     500                 :          1 :   g_assert_cmpstr (actual_str, ==, EXPECTED_NOT_VALID_AFTER);
     501                 :          1 :   g_free (actual_str);
     502                 :          1 :   g_date_time_unref (actual);
     503                 :          1 :   g_object_unref (cert);
     504                 :          1 : }
     505                 :            : 
     506                 :            : static void
     507                 :          1 : subject_name (void)
     508                 :            : {
     509                 :          1 :   const gchar *EXPECTED_SUBJECT_NAME = "DC=COM,DC=EXAMPLE,CN=server.example.com";
     510                 :            : 
     511                 :            :   GTlsCertificate *cert;
     512                 :          1 :   GError *error = NULL;
     513                 :            :   gchar *actual;
     514                 :            : 
     515                 :          1 :   cert = g_tls_certificate_new_from_pkcs11_uris ("pkcs11:model=p11-kit-trust;manufacturer=PKCS%2311%20Kit;serial=1;token=ca-bundle.crt", NULL, &error);
     516                 :          1 :   g_assert_no_error (error);
     517                 :          1 :   g_assert_nonnull (cert);
     518                 :            : 
     519                 :          1 :   actual = g_tls_certificate_get_subject_name (cert);
     520                 :          1 :   g_assert_nonnull (actual);
     521                 :          1 :   g_assert_cmpstr (actual, ==, EXPECTED_SUBJECT_NAME);
     522                 :          1 :   g_free (actual);
     523                 :          1 :   g_object_unref (cert);
     524                 :          1 : }
     525                 :            : 
     526                 :            : static void
     527                 :          1 : issuer_name (void)
     528                 :            : {
     529                 :          1 :   const gchar *EXPECTED_ISSUER_NAME = "DC=COM,DC=EXAMPLE,OU=Certificate Authority,CN=ca.example.com,emailAddress=ca@example.com";
     530                 :            : 
     531                 :            :   GTlsCertificate *cert;
     532                 :          1 :   GError *error = NULL;
     533                 :            :   gchar *actual;
     534                 :            : 
     535                 :          1 :   cert = g_tls_certificate_new_from_pkcs11_uris ("pkcs11:model=p11-kit-trust;manufacturer=PKCS%2311%20Kit;serial=1;token=ca-bundle.crt", NULL, &error);
     536                 :          1 :   g_assert_no_error (error);
     537                 :          1 :   g_assert_nonnull (cert);
     538                 :            : 
     539                 :          1 :   actual = g_tls_certificate_get_issuer_name (cert);
     540                 :          1 :   g_assert_nonnull (actual);
     541                 :          1 :   g_assert_cmpstr (actual, ==, EXPECTED_ISSUER_NAME);
     542                 :          1 :   g_free (actual);
     543                 :          1 :   g_object_unref (cert);
     544                 :          1 : }
     545                 :            : 
     546                 :            : static void
     547                 :          1 : dns_names (void)
     548                 :            : {
     549                 :            :   GTlsCertificate *cert;
     550                 :          1 :   GError *error = NULL;
     551                 :            :   GPtrArray *actual;
     552                 :          1 :   const gchar *dns_name = "a.example.com";
     553                 :          1 :   GBytes *expected = g_bytes_new_static (dns_name, strlen (dns_name));
     554                 :            : 
     555                 :          1 :   cert = g_tls_certificate_new_from_pkcs11_uris ("pkcs11:model=p11-kit-trust;manufacturer=PKCS%2311%20Kit;serial=1;token=ca-bundle.crt", NULL, &error);
     556                 :          1 :   g_assert_no_error (error);
     557                 :          1 :   g_assert_nonnull (cert);
     558                 :            : 
     559                 :          1 :   actual = g_tls_certificate_get_dns_names (cert);
     560                 :          1 :   g_assert_nonnull (actual);
     561                 :          1 :   g_assert_cmpuint (actual->len, ==, 1);
     562                 :          1 :   g_assert_true (g_ptr_array_find_with_equal_func (actual, expected, (GEqualFunc)g_bytes_equal, NULL));
     563                 :            : 
     564                 :          1 :   g_ptr_array_unref (actual);
     565                 :          1 :   g_bytes_unref (expected);
     566                 :          1 :   g_object_unref (cert);
     567                 :          1 : }
     568                 :            : 
     569                 :            : static void
     570                 :          1 : ip_addresses (void)
     571                 :            : {
     572                 :            :   GTlsCertificate *cert;
     573                 :          1 :   GError *error = NULL;
     574                 :            :   GPtrArray *actual;
     575                 :          1 :   GInetAddress *expected = g_inet_address_new_from_string ("192.0.2.1");
     576                 :            : 
     577                 :          1 :   cert = g_tls_certificate_new_from_pkcs11_uris ("pkcs11:model=p11-kit-trust;manufacturer=PKCS%2311%20Kit;serial=1;token=ca-bundle.crt", NULL, &error);
     578                 :          1 :   g_assert_no_error (error);
     579                 :          1 :   g_assert_nonnull (cert);
     580                 :            : 
     581                 :          1 :   actual = g_tls_certificate_get_ip_addresses (cert);
     582                 :          1 :   g_assert_nonnull (actual);
     583                 :          1 :   g_assert_cmpuint (actual->len, ==, 1);
     584                 :          1 :   g_assert_true (g_ptr_array_find_with_equal_func (actual, expected, (GEqualFunc)g_inet_address_equal, NULL));
     585                 :            : 
     586                 :          1 :   g_ptr_array_free (actual, TRUE);
     587                 :          1 :   g_object_unref (expected);
     588                 :          1 :   g_object_unref (cert);
     589                 :          1 : }
     590                 :            : 
     591                 :            : static void
     592                 :          1 : from_pkcs12 (void)
     593                 :            : {
     594                 :            :   GTlsCertificate *cert;
     595                 :          1 :   GError *error = NULL;
     596                 :          1 :   const guint8 data[1] = { 0 };
     597                 :            : 
     598                 :            :   /* This simply fails because our test backend doesn't support this
     599                 :            :    * property. This reflects using a backend that doesn't support it.
     600                 :            :    * The real test lives in glib-networking. */
     601                 :          1 :   cert = g_tls_certificate_new_from_pkcs12 (data, 1, NULL, &error);
     602                 :            : 
     603                 :          1 :   g_assert_null (cert);
     604                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
     605                 :          1 :   g_error_free (error);
     606                 :          1 : }
     607                 :            : 
     608                 :            : static void
     609                 :          1 : from_pkcs12_file (void)
     610                 :            : {
     611                 :            :   GTlsCertificate *cert;
     612                 :          1 :   GError *error = NULL;
     613                 :          1 :   char *path = g_test_build_filename (G_TEST_DIST, "cert-tests", "key-cert-password-123.p12", NULL);
     614                 :            : 
     615                 :            :   /* Fails on our test backend, see from_pkcs12() above. */
     616                 :          1 :   cert = g_tls_certificate_new_from_file_with_password (path, "123", &error);
     617                 :          1 :   g_assert_null (cert);
     618                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
     619                 :          1 :   g_clear_error (&error);
     620                 :            : 
     621                 :            :   /* Just for coverage. */
     622                 :          1 :   cert = g_tls_certificate_new_from_file (path, &error);
     623                 :          1 :   g_assert_null (cert);
     624                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
     625                 :          1 :   g_error_free (error);
     626                 :            : 
     627                 :          1 :   g_free (path);
     628                 :          1 : }
     629                 :            : 
     630                 :            : int
     631                 :          1 : main (int   argc,
     632                 :            :       char *argv[])
     633                 :            : {
     634                 :            :   int rtv;
     635                 :            :   Reference ref;
     636                 :          1 :   GError *error = NULL;
     637                 :            :   gchar *path;
     638                 :            : 
     639                 :          1 :   g_test_init (&argc, &argv, NULL);
     640                 :            : 
     641                 :          1 :   _g_test_tls_backend_get_type ();
     642                 :            : 
     643                 :            :   /* Load reference PEM */
     644                 :          1 :   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL);
     645                 :          1 :   g_file_get_contents (path, &ref.cert_pems[0], NULL, &error);
     646                 :          1 :   g_assert_no_error (error);
     647                 :          1 :   g_assert_nonnull (ref.cert_pems[0]);
     648                 :          1 :   g_free (path);
     649                 :          1 :   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "cert2.pem", NULL);
     650                 :          1 :   g_file_get_contents (path, &ref.cert_pems[1], NULL, &error);
     651                 :          1 :   g_assert_no_error (error);
     652                 :          1 :   g_assert_nonnull (ref.cert_pems[1]);
     653                 :          1 :   g_free (path);
     654                 :          1 :   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "cert3.pem", NULL);
     655                 :          1 :   g_file_get_contents (path, &ref.cert_pems[2], NULL, &error);
     656                 :          1 :   g_assert_no_error (error);
     657                 :          1 :   g_assert_nonnull (ref.cert_pems[2]);
     658                 :          1 :   g_free (path);
     659                 :          1 :   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "cert-crlf.pem", NULL);
     660                 :          1 :   g_file_get_contents (path, &ref.cert_crlf_pem, NULL, &error);
     661                 :          1 :   g_assert_no_error (error);
     662                 :          1 :   g_assert_nonnull (ref.cert_crlf_pem);
     663                 :          1 :   g_free (path);
     664                 :          1 :   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL);
     665                 :          1 :   g_file_get_contents (path, &ref.key_pem, NULL, &error);
     666                 :          1 :   g_assert_no_error (error);
     667                 :          1 :   g_assert_nonnull (ref.key_pem);
     668                 :          1 :   g_free (path);
     669                 :          1 :   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "key-crlf.pem", NULL);
     670                 :          1 :   g_file_get_contents (path, &ref.key_crlf_pem, NULL, &error);
     671                 :          1 :   g_assert_no_error (error);
     672                 :          1 :   g_assert_nonnull (ref.key_crlf_pem);
     673                 :          1 :   g_free (path);
     674                 :          1 :   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "key8.pem", NULL);
     675                 :          1 :   g_file_get_contents (path, &ref.key8_pem, NULL, &error);
     676                 :          1 :   g_assert_no_error (error);
     677                 :          1 :   g_assert_nonnull (ref.key8_pem);
     678                 :          1 :   g_free (path);
     679                 :            : 
     680                 :          1 :   g_test_add_data_func ("/tls-certificate/pem-parser",
     681                 :            :                         &ref, (GTestDataFunc)pem_parser);
     682                 :          1 :   g_test_add_data_func ("/tls-certificate/pem-parser-handles-chain",
     683                 :            :                         &ref, (GTestDataFunc)pem_parser_handles_chain);
     684                 :          1 :   g_test_add_data_func ("/tls-certificate/from_file",
     685                 :            :                         &ref, (GTestDataFunc)from_file);
     686                 :          1 :   g_test_add_data_func ("/tls-certificate/from_files",
     687                 :            :                         &ref, (GTestDataFunc)from_files);
     688                 :          1 :   g_test_add_data_func ("/tls-certificate/from_files_crlf",
     689                 :            :                         &ref, (GTestDataFunc)from_files_crlf);
     690                 :          1 :   g_test_add_data_func ("/tls-certificate/from_files_pkcs8",
     691                 :            :                         &ref, (GTestDataFunc)from_files_pkcs8);
     692                 :          1 :   g_test_add_data_func ("/tls-certificate/from_files_pkcs8enc",
     693                 :            :                         &ref, (GTestDataFunc)from_files_pkcs8enc);
     694                 :          1 :   g_test_add_data_func ("/tls-certificate/list_from_file",
     695                 :            :                         &ref, (GTestDataFunc)list_from_file);
     696                 :          1 :   g_test_add_func ("/tls-certificate/pkcs11-uri",
     697                 :            :                    from_pkcs11_uri);
     698                 :          1 :   g_test_add_func ("/tls-certificate/pkcs11-uri-unsupported",
     699                 :            :                    from_unsupported_pkcs11_uri);
     700                 :          1 :   g_test_add_func ("/tls-certificate/from_pkcs12",
     701                 :            :                    from_pkcs12);
     702                 :          1 :   g_test_add_func ("/tls-certificate/from_pkcs12_file",
     703                 :            :                    from_pkcs12_file);
     704                 :          1 :   g_test_add_func ("/tls-certificate/not-valid-before",
     705                 :            :                    not_valid_before);
     706                 :          1 :   g_test_add_func ("/tls-certificate/not-valid-after",
     707                 :            :                    not_valid_after);
     708                 :          1 :   g_test_add_func ("/tls-certificate/subject-name",
     709                 :            :                    subject_name);
     710                 :          1 :   g_test_add_func ("/tls-certificate/issuer-name",
     711                 :            :                    issuer_name);
     712                 :          1 :   g_test_add_func ("/tls-certificate/dns-names",
     713                 :            :                    dns_names);
     714                 :          1 :   g_test_add_func ("/tls-certificate/ip-addresses",
     715                 :            :                    ip_addresses);
     716                 :          1 :   g_test_add_func ("/tls-certificate/pem-parser-no-sentinel",
     717                 :            :                    pem_parser_no_sentinel);
     718                 :            : 
     719                 :          1 :   rtv = g_test_run();
     720                 :            : 
     721                 :          1 :   g_free (ref.cert_pems[0]);
     722                 :          1 :   g_free (ref.cert_pems[1]);
     723                 :          1 :   g_free (ref.cert_pems[2]);
     724                 :          1 :   g_free (ref.cert_crlf_pem);
     725                 :          1 :   g_free (ref.key_pem);
     726                 :          1 :   g_free (ref.key_crlf_pem);
     727                 :          1 :   g_free (ref.key8_pem);
     728                 :            : 
     729                 :          1 :   return rtv;
     730                 :            : }

Generated by: LCOV version 1.14