Merge remote branch 'gvdb/master'
[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 static gpointer
84 get_default_tls_backend (gpointer arg)
85 {
86   const char *use_this;
87   GList *extensions;
88   GIOExtensionPoint *ep;
89   GIOExtension *extension;
90
91   _g_io_modules_ensure_loaded ();
92
93   ep = g_io_extension_point_lookup (G_TLS_BACKEND_EXTENSION_POINT_NAME);
94
95   use_this = g_getenv ("GIO_USE_TLS");
96   if (use_this)
97     {
98       extension = g_io_extension_point_get_extension_by_name (ep, use_this);
99       if (extension)
100         return g_object_new (g_io_extension_get_type (extension), NULL);
101     }
102
103   extensions = g_io_extension_point_get_extensions (ep);
104   if (extensions)
105     {
106       extension = extensions->data;
107       return g_object_new (g_io_extension_get_type (extension), NULL);
108     }
109
110   return NULL;
111 }
112
113 /**
114  * g_tls_backend_get_default:
115  *
116  * Gets the default #GTlsBackend for the system.
117  *
118  * Returns: a #GTlsBackend
119  *
120  * Since: 2.28
121  */
122 GTlsBackend *
123 g_tls_backend_get_default (void)
124 {
125   static GOnce once_init = G_ONCE_INIT;
126
127   return g_once (&once_init, get_default_tls_backend, NULL);
128 }
129
130 /**
131  * g_tls_backend_supports_tls:
132  * @backend: the #GTlsBackend
133  *
134  * Checks if TLS is supported; if this returns %FALSE for the default
135  * #GTlsBackend, it means no "real" TLS backend is available.
136  *
137  * Return value: whether or not TLS is supported
138  *
139  * Since: 2.28
140  */
141 gboolean
142 g_tls_backend_supports_tls (GTlsBackend *backend)
143 {
144   if (G_TLS_BACKEND_GET_INTERFACE (backend)->supports_tls)
145     return G_TLS_BACKEND_GET_INTERFACE (backend)->supports_tls (backend);
146   else if (G_IS_DUMMY_TLS_BACKEND (backend))
147     return FALSE;
148   else
149     return TRUE;
150 }
151
152 /**
153  * g_tls_backend_get_certificate_type:
154  * @backend: the #GTlsBackend
155  *
156  * Gets the #GType of @backend's #GTlsCertificate implementation.
157  *
158  * Return value: the #GType of @backend's #GTlsCertificate
159  *   implementation.
160  *
161  * Since: 2.28
162  */
163 GType
164 g_tls_backend_get_certificate_type (GTlsBackend *backend)
165 {
166   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_certificate_type ();
167 }
168
169 /**
170  * g_tls_backend_get_client_connection_type:
171  * @backend: the #GTlsBackend
172  *
173  * Gets the #GType of @backend's #GTlsClientConnection implementation.
174  *
175  * Return value: the #GType of @backend's #GTlsClientConnection
176  *   implementation.
177  *
178  * Since: 2.28
179  */
180 GType
181 g_tls_backend_get_client_connection_type (GTlsBackend *backend)
182 {
183   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_client_connection_type ();
184 }
185
186 /**
187  * g_tls_backend_get_server_connection_type:
188  * @backend: the #GTlsBackend
189  *
190  * Gets the #GType of @backend's #GTlsServerConnection implementation.
191  *
192  * Return value: the #GType of @backend's #GTlsServerConnection
193  *   implementation.
194  *
195  * Since: 2.28
196  */
197 GType
198 g_tls_backend_get_server_connection_type (GTlsBackend *backend)
199 {
200   return G_TLS_BACKEND_GET_INTERFACE (backend)->get_server_connection_type ();
201 }