2d6913ab4d0428d796645e890348f926dde09c2b
[platform/upstream/glib.git] / gio / gtlsbackend.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2010 Red Hat, Inc
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include "glib.h"
23
24 #include "gtlsbackend.h"
25 #include "gdummytlsbackend.h"
26 #include "gioenumtypes.h"
27 #include "giomodule-priv.h"
28
29 /**
30  * SECTION:gtls
31  * @title: TLS Overview
32  * @short_description: TLS (aka SSL) support for GSocketConnection
33  * @include: gio/gio.h
34  *
35  * #GTlsConnection and related classes provide TLS (Transport Layer
36  * Security, previously known as SSL, Secure Sockets Layer) support for
37  * gio-based network streams.
38  *
39  * In the simplest case, for a client connection, you can just set the
40  * #GSocketClient:tls flag on a #GSocketClient, and then any
41  * connections created by that client will have TLS negotiated
42  * automatically, using appropriate default settings, and rejecting
43  * any invalid or self-signed certificates (unless you change that
44  * default by setting the #GSocketClient:tls-validation-flags
45  * property). The returned object will be a #GTcpWrapperConnection,
46  * which wraps the underlying #GTlsClientConnection.
47  *
48  * For greater control, you can create your own #GTlsClientConnection,
49  * wrapping a #GSocketConnection (or an arbitrary #GIOStream with
50  * pollable input and output streams) and then connect to its signals,
51  * such as #GTlsConnection::accept-certificate, before starting the
52  * handshake.
53  *
54  * Server-side TLS is similar, using #GTlsServerConnection. At the
55  * moment, there is no support for automatically wrapping server-side
56  * connections in the way #GSocketClient does for client-side
57  * connections.
58  */
59
60 /**
61  * SECTION:gtlsbackend
62  * @title: GTlsBackend
63  * @short_description: TLS backend implementation
64  * @include: gio/gio.h
65  */
66
67 /**
68  * GTlsBackend:
69  *
70  * Type implemented by TLS #GIOModules to provide access to additional
71  * TLS-related types.
72  *
73  * Since: 2.28
74  */
75
76 G_DEFINE_INTERFACE (GTlsBackend, g_tls_backend, G_TYPE_OBJECT);
77
78 static void
79 g_tls_backend_default_init (GTlsBackendInterface *iface)
80 {
81 }
82
83 /**
84  * g_tls_backend_get_default:
85  *
86  * Gets the default #GTlsBackend for the system.
87  *
88  * Returns: (transfer none): a #GTlsBackend
89  *
90  * Since: 2.28
91  */
92 GTlsBackend *
93 g_tls_backend_get_default (void)
94 {
95   return _g_io_module_get_default (G_TLS_BACKEND_EXTENSION_POINT_NAME,
96                                    "GIO_USE_TLS", NULL);
97 }
98
99 /**
100  * g_tls_backend_supports_tls:
101  * @backend: the #GTlsBackend
102  *
103  * Checks if TLS is supported; if this returns %FALSE for the default
104  * #GTlsBackend, it means no "real" TLS backend is available.
105  *
106  * Return value: whether or not TLS is supported
107  *
108  * Since: 2.28
109  */
110 gboolean
111 g_tls_backend_supports_tls (GTlsBackend *backend)
112 {
113   if (G_TLS_BACKEND_GET_INTERFACE (backend)->supports_tls)
114     return G_TLS_BACKEND_GET_INTERFACE (backend)->supports_tls (backend);
115   else if (G_IS_DUMMY_TLS_BACKEND (backend))
116     return FALSE;
117   else
118     return TRUE;
119 }
120
121 /**
122  * g_tls_backend_get_default_database:
123  * @backend: the #GTlsBackend
124  *
125  * Gets the default #GTlsDatabase used to verify TLS connections.
126  *
127  * Return value: (transfer full): the default database, which should be
128  *               unreffed when done.
129  *
130  * Since: 2.30
131  */
132 GTlsDatabase *
133 g_tls_backend_get_default_database (GTlsBackend *backend)
134 {
135   g_return_val_if_fail (G_IS_TLS_BACKEND (backend), NULL);
136
137   /* This method was added later, so accept the (remote) possibility it can be NULL */
138   if (!G_TLS_BACKEND_GET_INTERFACE (backend)->get_default_database)
139     return NULL;
140
141   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_default_database (backend);
142 }
143
144 /**
145  * g_tls_backend_get_certificate_type:
146  * @backend: the #GTlsBackend
147  *
148  * Gets the #GType of @backend's #GTlsCertificate implementation.
149  *
150  * Return value: the #GType of @backend's #GTlsCertificate
151  *   implementation.
152  *
153  * Since: 2.28
154  */
155 GType
156 g_tls_backend_get_certificate_type (GTlsBackend *backend)
157 {
158   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_certificate_type ();
159 }
160
161 /**
162  * g_tls_backend_get_client_connection_type:
163  * @backend: the #GTlsBackend
164  *
165  * Gets the #GType of @backend's #GTlsClientConnection implementation.
166  *
167  * Return value: the #GType of @backend's #GTlsClientConnection
168  *   implementation.
169  *
170  * Since: 2.28
171  */
172 GType
173 g_tls_backend_get_client_connection_type (GTlsBackend *backend)
174 {
175   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_client_connection_type ();
176 }
177
178 /**
179  * g_tls_backend_get_server_connection_type:
180  * @backend: the #GTlsBackend
181  *
182  * Gets the #GType of @backend's #GTlsServerConnection implementation.
183  *
184  * Return value: the #GType of @backend's #GTlsServerConnection
185  *   implementation.
186  *
187  * Since: 2.28
188  */
189 GType
190 g_tls_backend_get_server_connection_type (GTlsBackend *backend)
191 {
192   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_server_connection_type ();
193 }
194
195 /**
196  * g_tls_backend_get_file_database_type:
197  * @backend: the #GTlsBackend
198  *
199  * Gets the #GTyep of @backend's #GTlsFileDatabase implementation.
200  *
201  * Return value: the #GType of backend's #GTlsFileDatabase implementation.
202  *
203  * Since: 2.30
204  */
205 GType
206 g_tls_backend_get_file_database_type (GTlsBackend *backend)
207 {
208   g_return_val_if_fail (G_IS_TLS_BACKEND (backend), 0);
209
210   /* This method was added later, so accept the (remote) possibility it can be NULL */
211   if (!G_TLS_BACKEND_GET_INTERFACE (backend)->get_file_database_type)
212     return 0;
213
214   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_file_database_type ();
215 }