LCOV - code coverage report
Current view: top level - glib/gio - gtlsbackend.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 38 57 66.7 %
Date: 2024-04-23 05:16:05 Functions: 10 13 76.9 %
Branches: 16 36 44.4 %

           Branch data     Line data    Source code
       1                 :            : /* GIO - GLib Input, Output and Streaming Library
       2                 :            :  *
       3                 :            :  * Copyright © 2010 Red Hat, Inc
       4                 :            :  * Copyright © 2015 Collabora, Ltd.
       5                 :            :  *
       6                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       7                 :            :  *
       8                 :            :  * This library is free software; you can redistribute it and/or
       9                 :            :  * modify it under the terms of the GNU Lesser General Public
      10                 :            :  * License as published by the Free Software Foundation; either
      11                 :            :  * version 2.1 of the License, or (at your option) any later version.
      12                 :            :  *
      13                 :            :  * This library is distributed in the hope that it will be useful,
      14                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16                 :            :  * Lesser General Public License for more details.
      17                 :            :  *
      18                 :            :  * You should have received a copy of the GNU Lesser General
      19                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      20                 :            :  */
      21                 :            : 
      22                 :            : #include "config.h"
      23                 :            : #include "glib.h"
      24                 :            : 
      25                 :            : #include "gtlsbackend.h"
      26                 :            : #include "gtlsdatabase.h"
      27                 :            : #include "gdummytlsbackend.h"
      28                 :            : #include "gioenumtypes.h"
      29                 :            : #include "giomodule-priv.h"
      30                 :            : 
      31                 :            : /**
      32                 :            :  * GTlsBackend:
      33                 :            :  *
      34                 :            :  * TLS (Transport Layer Security, aka SSL) and DTLS backend. This is an
      35                 :            :  * internal type used to coordinate the different classes implemented
      36                 :            :  * by a TLS backend.
      37                 :            :  *
      38                 :            :  * Since: 2.28
      39                 :            :  */
      40                 :            : 
      41   [ +  +  +  -  :        325 : G_DEFINE_INTERFACE (GTlsBackend, g_tls_backend, G_TYPE_OBJECT)
                   +  + ]
      42                 :            : 
      43                 :            : static GTlsDatabase *default_database;
      44                 :            : G_LOCK_DEFINE_STATIC (default_database_lock);
      45                 :            : 
      46                 :            : static void
      47                 :          5 : g_tls_backend_default_init (GTlsBackendInterface *iface)
      48                 :            : {
      49                 :          5 : }
      50                 :            : 
      51                 :            : static GTlsBackend *tls_backend_default_singleton = NULL;  /* (owned) (atomic) */
      52                 :            : 
      53                 :            : /**
      54                 :            :  * g_tls_backend_get_default:
      55                 :            :  *
      56                 :            :  * Gets the default #GTlsBackend for the system.
      57                 :            :  *
      58                 :            :  * Returns: (not nullable) (transfer none): a #GTlsBackend, which will be a
      59                 :            :  *     dummy object if no TLS backend is available
      60                 :            :  *
      61                 :            :  * Since: 2.28
      62                 :            :  */
      63                 :            : GTlsBackend *
      64                 :         30 : g_tls_backend_get_default (void)
      65                 :            : {
      66   [ +  +  +  -  :         30 :   if (g_once_init_enter_pointer (&tls_backend_default_singleton))
                   +  + ]
      67                 :            :     {
      68                 :            :       GTlsBackend *singleton;
      69                 :            : 
      70                 :          3 :       singleton = _g_io_module_get_default (G_TLS_BACKEND_EXTENSION_POINT_NAME,
      71                 :            :                                             "GIO_USE_TLS",
      72                 :            :                                             NULL);
      73                 :            : 
      74                 :          3 :       g_once_init_leave_pointer (&tls_backend_default_singleton, singleton);
      75                 :            :     }
      76                 :            : 
      77                 :         30 :   return tls_backend_default_singleton;
      78                 :            : }
      79                 :            : 
      80                 :            : /**
      81                 :            :  * g_tls_backend_supports_tls:
      82                 :            :  * @backend: the #GTlsBackend
      83                 :            :  *
      84                 :            :  * Checks if TLS is supported; if this returns %FALSE for the default
      85                 :            :  * #GTlsBackend, it means no "real" TLS backend is available.
      86                 :            :  *
      87                 :            :  * Returns: whether or not TLS is supported
      88                 :            :  *
      89                 :            :  * Since: 2.28
      90                 :            :  */
      91                 :            : gboolean
      92                 :          0 : g_tls_backend_supports_tls (GTlsBackend *backend)
      93                 :            : {
      94         [ #  # ]:          0 :   if (G_TLS_BACKEND_GET_INTERFACE (backend)->supports_tls)
      95                 :          0 :     return G_TLS_BACKEND_GET_INTERFACE (backend)->supports_tls (backend);
      96   [ #  #  #  #  :          0 :   else if (G_IS_DUMMY_TLS_BACKEND (backend))
             #  #  #  # ]
      97                 :          0 :     return FALSE;
      98                 :            :   else
      99                 :          0 :     return TRUE;
     100                 :            : }
     101                 :            : 
     102                 :            : /**
     103                 :            :  * g_tls_backend_supports_dtls:
     104                 :            :  * @backend: the #GTlsBackend
     105                 :            :  *
     106                 :            :  * Checks if DTLS is supported. DTLS support may not be available even if TLS
     107                 :            :  * support is available, and vice-versa.
     108                 :            :  *
     109                 :            :  * Returns: whether DTLS is supported
     110                 :            :  *
     111                 :            :  * Since: 2.48
     112                 :            :  */
     113                 :            : gboolean
     114                 :          0 : g_tls_backend_supports_dtls (GTlsBackend *backend)
     115                 :            : {
     116         [ #  # ]:          0 :   if (G_TLS_BACKEND_GET_INTERFACE (backend)->supports_dtls)
     117                 :          0 :     return G_TLS_BACKEND_GET_INTERFACE (backend)->supports_dtls (backend);
     118                 :            : 
     119                 :          0 :   return FALSE;
     120                 :            : }
     121                 :            : 
     122                 :            : /**
     123                 :            :  * g_tls_backend_get_default_database:
     124                 :            :  * @backend: the #GTlsBackend
     125                 :            :  *
     126                 :            :  * Gets the default #GTlsDatabase used to verify TLS connections.
     127                 :            :  *
     128                 :            :  * Returns: (transfer full): the default database, which should be
     129                 :            :  *               unreffed when done.
     130                 :            :  *
     131                 :            :  * Since: 2.30
     132                 :            :  */
     133                 :            : GTlsDatabase *
     134                 :          3 : g_tls_backend_get_default_database (GTlsBackend *backend)
     135                 :            : {
     136                 :            :   GTlsDatabase *db;
     137                 :            : 
     138                 :          3 :   g_return_val_if_fail (G_IS_TLS_BACKEND (backend), NULL);
     139                 :            : 
     140                 :            :   /* This method was added later, so accept the (remote) possibility it can be NULL */
     141         [ -  + ]:          3 :   if (!G_TLS_BACKEND_GET_INTERFACE (backend)->get_default_database)
     142                 :          0 :     return NULL;
     143                 :            : 
     144                 :          3 :   G_LOCK (default_database_lock);
     145                 :            : 
     146         [ +  + ]:          3 :   if (!default_database)
     147                 :          2 :     default_database = G_TLS_BACKEND_GET_INTERFACE (backend)->get_default_database (backend);
     148         [ +  - ]:          3 :   db = default_database ? g_object_ref (default_database) : NULL;
     149                 :          3 :   G_UNLOCK (default_database_lock);
     150                 :            : 
     151                 :          3 :   return db;
     152                 :            : }
     153                 :            : 
     154                 :            : /**
     155                 :            :  * g_tls_backend_set_default_database:
     156                 :            :  * @backend: the #GTlsBackend
     157                 :            :  * @database: (nullable): the #GTlsDatabase
     158                 :            :  *
     159                 :            :  * Set the default #GTlsDatabase used to verify TLS connections
     160                 :            :  *
     161                 :            :  * Any subsequent call to g_tls_backend_get_default_database() will return
     162                 :            :  * the database set in this call.  Existing databases and connections are not
     163                 :            :  * modified.
     164                 :            :  *
     165                 :            :  * Setting a %NULL default database will reset to using the system default
     166                 :            :  * database as if g_tls_backend_set_default_database() had never been called.
     167                 :            :  *
     168                 :            :  * Since: 2.60
     169                 :            :  */
     170                 :            : void
     171                 :          2 : g_tls_backend_set_default_database (GTlsBackend  *backend,
     172                 :            :                                     GTlsDatabase *database)
     173                 :            : {
     174                 :          2 :   g_return_if_fail (G_IS_TLS_BACKEND (backend));
     175                 :          2 :   g_return_if_fail (database == NULL || G_IS_TLS_DATABASE (database));
     176                 :            : 
     177                 :          2 :   G_LOCK (default_database_lock);
     178                 :          2 :   g_set_object (&default_database, database);
     179                 :          2 :   G_UNLOCK (default_database_lock);
     180                 :            : }
     181                 :            : 
     182                 :            : /**
     183                 :            :  * g_tls_backend_get_certificate_type:
     184                 :            :  * @backend: the #GTlsBackend
     185                 :            :  *
     186                 :            :  * Gets the #GType of @backend's #GTlsCertificate implementation.
     187                 :            :  *
     188                 :            :  * Returns: the #GType of @backend's #GTlsCertificate
     189                 :            :  *   implementation.
     190                 :            :  *
     191                 :            :  * Since: 2.28
     192                 :            :  */
     193                 :            : GType
     194                 :         26 : g_tls_backend_get_certificate_type (GTlsBackend *backend)
     195                 :            : {
     196                 :         26 :   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_certificate_type ();
     197                 :            : }
     198                 :            : 
     199                 :            : /**
     200                 :            :  * g_tls_backend_get_client_connection_type:
     201                 :            :  * @backend: the #GTlsBackend
     202                 :            :  *
     203                 :            :  * Gets the #GType of @backend's #GTlsClientConnection implementation.
     204                 :            :  *
     205                 :            :  * Returns: the #GType of @backend's #GTlsClientConnection
     206                 :            :  *   implementation.
     207                 :            :  *
     208                 :            :  * Since: 2.28
     209                 :            :  */
     210                 :            : GType
     211                 :          1 : g_tls_backend_get_client_connection_type (GTlsBackend *backend)
     212                 :            : {
     213                 :          1 :   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_client_connection_type ();
     214                 :            : }
     215                 :            : 
     216                 :            : /**
     217                 :            :  * g_tls_backend_get_server_connection_type:
     218                 :            :  * @backend: the #GTlsBackend
     219                 :            :  *
     220                 :            :  * Gets the #GType of @backend's #GTlsServerConnection implementation.
     221                 :            :  *
     222                 :            :  * Returns: the #GType of @backend's #GTlsServerConnection
     223                 :            :  *   implementation.
     224                 :            :  *
     225                 :            :  * Since: 2.28
     226                 :            :  */
     227                 :            : GType
     228                 :         42 : g_tls_backend_get_server_connection_type (GTlsBackend *backend)
     229                 :            : {
     230                 :         42 :   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_server_connection_type ();
     231                 :            : }
     232                 :            : 
     233                 :            : /**
     234                 :            :  * g_tls_backend_get_dtls_client_connection_type:
     235                 :            :  * @backend: the #GTlsBackend
     236                 :            :  *
     237                 :            :  * Gets the #GType of @backend’s #GDtlsClientConnection implementation.
     238                 :            :  *
     239                 :            :  * Returns: the #GType of @backend’s #GDtlsClientConnection
     240                 :            :  *   implementation, or %G_TYPE_INVALID if this backend doesn’t support DTLS.
     241                 :            :  *
     242                 :            :  * Since: 2.48
     243                 :            :  */
     244                 :            : GType
     245                 :          1 : g_tls_backend_get_dtls_client_connection_type (GTlsBackend *backend)
     246                 :            : {
     247                 :            :   GTlsBackendInterface *iface;
     248                 :            : 
     249                 :          1 :   g_return_val_if_fail (G_IS_TLS_BACKEND (backend), G_TYPE_INVALID);
     250                 :            : 
     251                 :          1 :   iface = G_TLS_BACKEND_GET_INTERFACE (backend);
     252         [ -  + ]:          1 :   if (iface->get_dtls_client_connection_type == NULL)
     253                 :          0 :     return G_TYPE_INVALID;
     254                 :            : 
     255                 :          1 :   return iface->get_dtls_client_connection_type ();
     256                 :            : }
     257                 :            : 
     258                 :            : /**
     259                 :            :  * g_tls_backend_get_dtls_server_connection_type:
     260                 :            :  * @backend: the #GTlsBackend
     261                 :            :  *
     262                 :            :  * Gets the #GType of @backend’s #GDtlsServerConnection implementation.
     263                 :            :  *
     264                 :            :  * Returns: the #GType of @backend’s #GDtlsServerConnection
     265                 :            :  *   implementation, or %G_TYPE_INVALID if this backend doesn’t support DTLS.
     266                 :            :  *
     267                 :            :  * Since: 2.48
     268                 :            :  */
     269                 :            : GType
     270                 :          0 : g_tls_backend_get_dtls_server_connection_type (GTlsBackend *backend)
     271                 :            : {
     272                 :            :   GTlsBackendInterface *iface;
     273                 :            : 
     274                 :          0 :   g_return_val_if_fail (G_IS_TLS_BACKEND (backend), G_TYPE_INVALID);
     275                 :            : 
     276                 :          0 :   iface = G_TLS_BACKEND_GET_INTERFACE (backend);
     277         [ #  # ]:          0 :   if (iface->get_dtls_server_connection_type == NULL)
     278                 :          0 :     return G_TYPE_INVALID;
     279                 :            : 
     280                 :          0 :   return iface->get_dtls_server_connection_type ();
     281                 :            : }
     282                 :            : 
     283                 :            : /**
     284                 :            :  * g_tls_backend_get_file_database_type:
     285                 :            :  * @backend: the #GTlsBackend
     286                 :            :  *
     287                 :            :  * Gets the #GType of @backend's #GTlsFileDatabase implementation.
     288                 :            :  *
     289                 :            :  * Returns: the #GType of backend's #GTlsFileDatabase implementation.
     290                 :            :  *
     291                 :            :  * Since: 2.30
     292                 :            :  */
     293                 :            : GType
     294                 :          1 : g_tls_backend_get_file_database_type (GTlsBackend *backend)
     295                 :            : {
     296                 :          1 :   g_return_val_if_fail (G_IS_TLS_BACKEND (backend), 0);
     297                 :            : 
     298                 :            :   /* This method was added later, so accept the (remote) possibility it can be NULL */
     299         [ -  + ]:          1 :   if (!G_TLS_BACKEND_GET_INTERFACE (backend)->get_file_database_type)
     300                 :          0 :     return 0;
     301                 :            : 
     302                 :          1 :   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_file_database_type ();
     303                 :            : }

Generated by: LCOV version 1.14