1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.TlsConnection;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import gio.IOStream;
30 private import gio.TlsCertificate;
31 private import gio.TlsDatabase;
32 private import gio.TlsInteraction;
33 private import gio.c.functions;
34 public  import gio.c.types;
35 private import glib.ErrorG;
36 private import glib.GException;
37 private import gobject.ObjectG;
38 private import gobject.Signals;
39 public  import gtkc.giotypes;
40 private import std.algorithm;
41 
42 
43 /**
44  * #GTlsConnection is the base TLS connection class type, which wraps
45  * a #GIOStream and provides TLS encryption on top of it. Its
46  * subclasses, #GTlsClientConnection and #GTlsServerConnection,
47  * implement client-side and server-side TLS, respectively.
48  * 
49  * For DTLS (Datagram TLS) support, see #GDtlsConnection.
50  *
51  * Since: 2.28
52  */
53 public class TlsConnection : IOStream
54 {
55 	/** the main Gtk struct */
56 	protected GTlsConnection* gTlsConnection;
57 
58 	/** Get the main Gtk struct */
59 	public GTlsConnection* getTlsConnectionStruct(bool transferOwnership = false)
60 	{
61 		if (transferOwnership)
62 			ownedRef = false;
63 		return gTlsConnection;
64 	}
65 
66 	/** the main Gtk struct as a void* */
67 	protected override void* getStruct()
68 	{
69 		return cast(void*)gTlsConnection;
70 	}
71 
72 	/**
73 	 * Sets our main struct and passes it to the parent class.
74 	 */
75 	public this (GTlsConnection* gTlsConnection, bool ownedRef = false)
76 	{
77 		this.gTlsConnection = gTlsConnection;
78 		super(cast(GIOStream*)gTlsConnection, ownedRef);
79 	}
80 
81 
82 	/** */
83 	public static GType getType()
84 	{
85 		return g_tls_connection_get_type();
86 	}
87 
88 	/**
89 	 * Used by #GTlsConnection implementations to emit the
90 	 * #GTlsConnection::accept-certificate signal.
91 	 *
92 	 * Params:
93 	 *     peerCert = the peer's #GTlsCertificate
94 	 *     errors = the problems with @peer_cert
95 	 *
96 	 * Returns: %TRUE if one of the signal handlers has returned
97 	 *     %TRUE to accept @peer_cert
98 	 *
99 	 * Since: 2.28
100 	 */
101 	public bool emitAcceptCertificate(TlsCertificate peerCert, GTlsCertificateFlags errors)
102 	{
103 		return g_tls_connection_emit_accept_certificate(gTlsConnection, (peerCert is null) ? null : peerCert.getTlsCertificateStruct(), errors) != 0;
104 	}
105 
106 	/**
107 	 * Gets @conn's certificate, as set by
108 	 * g_tls_connection_set_certificate().
109 	 *
110 	 * Returns: @conn's certificate, or %NULL
111 	 *
112 	 * Since: 2.28
113 	 */
114 	public TlsCertificate getCertificate()
115 	{
116 		auto p = g_tls_connection_get_certificate(gTlsConnection);
117 
118 		if(p is null)
119 		{
120 			return null;
121 		}
122 
123 		return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) p);
124 	}
125 
126 	/**
127 	 * Gets the certificate database that @conn uses to verify
128 	 * peer certificates. See g_tls_connection_set_database().
129 	 *
130 	 * Returns: the certificate database that @conn uses or %NULL
131 	 *
132 	 * Since: 2.30
133 	 */
134 	public TlsDatabase getDatabase()
135 	{
136 		auto p = g_tls_connection_get_database(gTlsConnection);
137 
138 		if(p is null)
139 		{
140 			return null;
141 		}
142 
143 		return ObjectG.getDObject!(TlsDatabase)(cast(GTlsDatabase*) p);
144 	}
145 
146 	/**
147 	 * Get the object that will be used to interact with the user. It will be used
148 	 * for things like prompting the user for passwords. If %NULL is returned, then
149 	 * no user interaction will occur for this connection.
150 	 *
151 	 * Returns: The interaction object.
152 	 *
153 	 * Since: 2.30
154 	 */
155 	public TlsInteraction getInteraction()
156 	{
157 		auto p = g_tls_connection_get_interaction(gTlsConnection);
158 
159 		if(p is null)
160 		{
161 			return null;
162 		}
163 
164 		return ObjectG.getDObject!(TlsInteraction)(cast(GTlsInteraction*) p);
165 	}
166 
167 	/**
168 	 * Gets @conn's peer's certificate after the handshake has completed.
169 	 * (It is not set during the emission of
170 	 * #GTlsConnection::accept-certificate.)
171 	 *
172 	 * Returns: @conn's peer's certificate, or %NULL
173 	 *
174 	 * Since: 2.28
175 	 */
176 	public TlsCertificate getPeerCertificate()
177 	{
178 		auto p = g_tls_connection_get_peer_certificate(gTlsConnection);
179 
180 		if(p is null)
181 		{
182 			return null;
183 		}
184 
185 		return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) p);
186 	}
187 
188 	/**
189 	 * Gets the errors associated with validating @conn's peer's
190 	 * certificate, after the handshake has completed. (It is not set
191 	 * during the emission of #GTlsConnection::accept-certificate.)
192 	 *
193 	 * Returns: @conn's peer's certificate errors
194 	 *
195 	 * Since: 2.28
196 	 */
197 	public GTlsCertificateFlags getPeerCertificateErrors()
198 	{
199 		return g_tls_connection_get_peer_certificate_errors(gTlsConnection);
200 	}
201 
202 	/**
203 	 * Gets @conn rehandshaking mode. See
204 	 * g_tls_connection_set_rehandshake_mode() for details.
205 	 *
206 	 * Returns: @conn's rehandshaking mode
207 	 *
208 	 * Since: 2.28
209 	 */
210 	public GTlsRehandshakeMode getRehandshakeMode()
211 	{
212 		return g_tls_connection_get_rehandshake_mode(gTlsConnection);
213 	}
214 
215 	/**
216 	 * Tests whether or not @conn expects a proper TLS close notification
217 	 * when the connection is closed. See
218 	 * g_tls_connection_set_require_close_notify() for details.
219 	 *
220 	 * Returns: %TRUE if @conn requires a proper TLS close
221 	 *     notification.
222 	 *
223 	 * Since: 2.28
224 	 */
225 	public bool getRequireCloseNotify()
226 	{
227 		return g_tls_connection_get_require_close_notify(gTlsConnection) != 0;
228 	}
229 
230 	/**
231 	 * Gets whether @conn uses the system certificate database to verify
232 	 * peer certificates. See g_tls_connection_set_use_system_certdb().
233 	 *
234 	 * Deprecated: Use g_tls_connection_get_database() instead
235 	 *
236 	 * Returns: whether @conn uses the system certificate database
237 	 */
238 	public bool getUseSystemCertdb()
239 	{
240 		return g_tls_connection_get_use_system_certdb(gTlsConnection) != 0;
241 	}
242 
243 	/**
244 	 * Attempts a TLS handshake on @conn.
245 	 *
246 	 * On the client side, it is never necessary to call this method;
247 	 * although the connection needs to perform a handshake after
248 	 * connecting (or after sending a "STARTTLS"-type command) and may
249 	 * need to rehandshake later if the server requests it,
250 	 * #GTlsConnection will handle this for you automatically when you try
251 	 * to send or receive data on the connection. However, you can call
252 	 * g_tls_connection_handshake() manually if you want to know for sure
253 	 * whether the initial handshake succeeded or failed (as opposed to
254 	 * just immediately trying to write to @conn's output stream, in which
255 	 * case if it fails, it may not be possible to tell if it failed
256 	 * before or after completing the handshake).
257 	 *
258 	 * Likewise, on the server side, although a handshake is necessary at
259 	 * the beginning of the communication, you do not need to call this
260 	 * function explicitly unless you want clearer error reporting.
261 	 * However, you may call g_tls_connection_handshake() later on to
262 	 * renegotiate parameters (encryption methods, etc) with the client.
263 	 *
264 	 * #GTlsConnection::accept_certificate may be emitted during the
265 	 * handshake.
266 	 *
267 	 * Params:
268 	 *     cancellable = a #GCancellable, or %NULL
269 	 *
270 	 * Returns: success or failure
271 	 *
272 	 * Since: 2.28
273 	 *
274 	 * Throws: GException on failure.
275 	 */
276 	public bool handshake(Cancellable cancellable)
277 	{
278 		GError* err = null;
279 
280 		auto p = g_tls_connection_handshake(gTlsConnection, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
281 
282 		if (err !is null)
283 		{
284 			throw new GException( new ErrorG(err) );
285 		}
286 
287 		return p;
288 	}
289 
290 	/**
291 	 * Asynchronously performs a TLS handshake on @conn. See
292 	 * g_tls_connection_handshake() for more information.
293 	 *
294 	 * Params:
295 	 *     ioPriority = the [I/O priority][io-priority] of the request
296 	 *     cancellable = a #GCancellable, or %NULL
297 	 *     callback = callback to call when the handshake is complete
298 	 *     userData = the data to pass to the callback function
299 	 *
300 	 * Since: 2.28
301 	 */
302 	public void handshakeAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
303 	{
304 		g_tls_connection_handshake_async(gTlsConnection, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
305 	}
306 
307 	/**
308 	 * Finish an asynchronous TLS handshake operation. See
309 	 * g_tls_connection_handshake() for more information.
310 	 *
311 	 * Params:
312 	 *     result = a #GAsyncResult.
313 	 *
314 	 * Returns: %TRUE on success, %FALSE on failure, in which
315 	 *     case @error will be set.
316 	 *
317 	 * Since: 2.28
318 	 *
319 	 * Throws: GException on failure.
320 	 */
321 	public bool handshakeFinish(AsyncResultIF result)
322 	{
323 		GError* err = null;
324 
325 		auto p = g_tls_connection_handshake_finish(gTlsConnection, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
326 
327 		if (err !is null)
328 		{
329 			throw new GException( new ErrorG(err) );
330 		}
331 
332 		return p;
333 	}
334 
335 	/**
336 	 * This sets the certificate that @conn will present to its peer
337 	 * during the TLS handshake. For a #GTlsServerConnection, it is
338 	 * mandatory to set this, and that will normally be done at construct
339 	 * time.
340 	 *
341 	 * For a #GTlsClientConnection, this is optional. If a handshake fails
342 	 * with %G_TLS_ERROR_CERTIFICATE_REQUIRED, that means that the server
343 	 * requires a certificate, and if you try connecting again, you should
344 	 * call this method first. You can call
345 	 * g_tls_client_connection_get_accepted_cas() on the failed connection
346 	 * to get a list of Certificate Authorities that the server will
347 	 * accept certificates from.
348 	 *
349 	 * (It is also possible that a server will allow the connection with
350 	 * or without a certificate; in that case, if you don't provide a
351 	 * certificate, you can tell that the server requested one by the fact
352 	 * that g_tls_client_connection_get_accepted_cas() will return
353 	 * non-%NULL.)
354 	 *
355 	 * Params:
356 	 *     certificate = the certificate to use for @conn
357 	 *
358 	 * Since: 2.28
359 	 */
360 	public void setCertificate(TlsCertificate certificate)
361 	{
362 		g_tls_connection_set_certificate(gTlsConnection, (certificate is null) ? null : certificate.getTlsCertificateStruct());
363 	}
364 
365 	/**
366 	 * Sets the certificate database that is used to verify peer certificates.
367 	 * This is set to the default database by default. See
368 	 * g_tls_backend_get_default_database(). If set to %NULL, then
369 	 * peer certificate validation will always set the
370 	 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
371 	 * #GTlsConnection::accept-certificate will always be emitted on
372 	 * client-side connections, unless that bit is not set in
373 	 * #GTlsClientConnection:validation-flags).
374 	 *
375 	 * Params:
376 	 *     database = a #GTlsDatabase
377 	 *
378 	 * Since: 2.30
379 	 */
380 	public void setDatabase(TlsDatabase database)
381 	{
382 		g_tls_connection_set_database(gTlsConnection, (database is null) ? null : database.getTlsDatabaseStruct());
383 	}
384 
385 	/**
386 	 * Set the object that will be used to interact with the user. It will be used
387 	 * for things like prompting the user for passwords.
388 	 *
389 	 * The @interaction argument will normally be a derived subclass of
390 	 * #GTlsInteraction. %NULL can also be provided if no user interaction
391 	 * should occur for this connection.
392 	 *
393 	 * Params:
394 	 *     interaction = an interaction object, or %NULL
395 	 *
396 	 * Since: 2.30
397 	 */
398 	public void setInteraction(TlsInteraction interaction)
399 	{
400 		g_tls_connection_set_interaction(gTlsConnection, (interaction is null) ? null : interaction.getTlsInteractionStruct());
401 	}
402 
403 	/**
404 	 * Sets how @conn behaves with respect to rehandshaking requests.
405 	 *
406 	 * %G_TLS_REHANDSHAKE_NEVER means that it will never agree to
407 	 * rehandshake after the initial handshake is complete. (For a client,
408 	 * this means it will refuse rehandshake requests from the server, and
409 	 * for a server, this means it will close the connection with an error
410 	 * if the client attempts to rehandshake.)
411 	 *
412 	 * %G_TLS_REHANDSHAKE_SAFELY means that the connection will allow a
413 	 * rehandshake only if the other end of the connection supports the
414 	 * TLS `renegotiation_info` extension. This is the default behavior,
415 	 * but means that rehandshaking will not work against older
416 	 * implementations that do not support that extension.
417 	 *
418 	 * %G_TLS_REHANDSHAKE_UNSAFELY means that the connection will allow
419 	 * rehandshaking even without the `renegotiation_info` extension. On
420 	 * the server side in particular, this is not recommended, since it
421 	 * leaves the server open to certain attacks. However, this mode is
422 	 * necessary if you need to allow renegotiation with older client
423 	 * software.
424 	 *
425 	 * Params:
426 	 *     mode = the rehandshaking mode
427 	 *
428 	 * Since: 2.28
429 	 */
430 	public void setRehandshakeMode(GTlsRehandshakeMode mode)
431 	{
432 		g_tls_connection_set_rehandshake_mode(gTlsConnection, mode);
433 	}
434 
435 	/**
436 	 * Sets whether or not @conn expects a proper TLS close notification
437 	 * before the connection is closed. If this is %TRUE (the default),
438 	 * then @conn will expect to receive a TLS close notification from its
439 	 * peer before the connection is closed, and will return a
440 	 * %G_TLS_ERROR_EOF error if the connection is closed without proper
441 	 * notification (since this may indicate a network error, or
442 	 * man-in-the-middle attack).
443 	 *
444 	 * In some protocols, the application will know whether or not the
445 	 * connection was closed cleanly based on application-level data
446 	 * (because the application-level data includes a length field, or is
447 	 * somehow self-delimiting); in this case, the close notify is
448 	 * redundant and sometimes omitted. (TLS 1.1 explicitly allows this;
449 	 * in TLS 1.0 it is technically an error, but often done anyway.) You
450 	 * can use g_tls_connection_set_require_close_notify() to tell @conn
451 	 * to allow an "unannounced" connection close, in which case the close
452 	 * will show up as a 0-length read, as in a non-TLS
453 	 * #GSocketConnection, and it is up to the application to check that
454 	 * the data has been fully received.
455 	 *
456 	 * Note that this only affects the behavior when the peer closes the
457 	 * connection; when the application calls g_io_stream_close() itself
458 	 * on @conn, this will send a close notification regardless of the
459 	 * setting of this property. If you explicitly want to do an unclean
460 	 * close, you can close @conn's #GTlsConnection:base-io-stream rather
461 	 * than closing @conn itself, but note that this may only be done when no other
462 	 * operations are pending on @conn or the base I/O stream.
463 	 *
464 	 * Params:
465 	 *     requireCloseNotify = whether or not to require close notification
466 	 *
467 	 * Since: 2.28
468 	 */
469 	public void setRequireCloseNotify(bool requireCloseNotify)
470 	{
471 		g_tls_connection_set_require_close_notify(gTlsConnection, requireCloseNotify);
472 	}
473 
474 	/**
475 	 * Sets whether @conn uses the system certificate database to verify
476 	 * peer certificates. This is %TRUE by default. If set to %FALSE, then
477 	 * peer certificate validation will always set the
478 	 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
479 	 * #GTlsConnection::accept-certificate will always be emitted on
480 	 * client-side connections, unless that bit is not set in
481 	 * #GTlsClientConnection:validation-flags).
482 	 *
483 	 * Deprecated: Use g_tls_connection_set_database() instead
484 	 *
485 	 * Params:
486 	 *     useSystemCertdb = whether to use the system certificate database
487 	 */
488 	public void setUseSystemCertdb(bool useSystemCertdb)
489 	{
490 		g_tls_connection_set_use_system_certdb(gTlsConnection, useSystemCertdb);
491 	}
492 
493 	/**
494 	 * Emitted during the TLS handshake after the peer certificate has
495 	 * been received. You can examine @peer_cert's certification path by
496 	 * calling g_tls_certificate_get_issuer() on it.
497 	 *
498 	 * For a client-side connection, @peer_cert is the server's
499 	 * certificate, and the signal will only be emitted if the
500 	 * certificate was not acceptable according to @conn's
501 	 * #GTlsClientConnection:validation_flags. If you would like the
502 	 * certificate to be accepted despite @errors, return %TRUE from the
503 	 * signal handler. Otherwise, if no handler accepts the certificate,
504 	 * the handshake will fail with %G_TLS_ERROR_BAD_CERTIFICATE.
505 	 *
506 	 * For a server-side connection, @peer_cert is the certificate
507 	 * presented by the client, if this was requested via the server's
508 	 * #GTlsServerConnection:authentication_mode. On the server side,
509 	 * the signal is always emitted when the client presents a
510 	 * certificate, and the certificate will only be accepted if a
511 	 * handler returns %TRUE.
512 	 *
513 	 * Note that if this signal is emitted as part of asynchronous I/O
514 	 * in the main thread, then you should not attempt to interact with
515 	 * the user before returning from the signal handler. If you want to
516 	 * let the user decide whether or not to accept the certificate, you
517 	 * would have to return %FALSE from the signal handler on the first
518 	 * attempt, and then after the connection attempt returns a
519 	 * %G_TLS_ERROR_HANDSHAKE, you can interact with the user, and if
520 	 * the user decides to accept the certificate, remember that fact,
521 	 * create a new connection, and return %TRUE from the signal handler
522 	 * the next time.
523 	 *
524 	 * If you are doing I/O in another thread, you do not
525 	 * need to worry about this, and can simply block in the signal
526 	 * handler until the UI thread returns an answer.
527 	 *
528 	 * Params:
529 	 *     peerCert = the peer's #GTlsCertificate
530 	 *     errors = the problems with @peer_cert.
531 	 *
532 	 * Returns: %TRUE to accept @peer_cert (which will also
533 	 *     immediately end the signal emission). %FALSE to allow the signal
534 	 *     emission to continue, which will cause the handshake to fail if
535 	 *     no one else overrides it.
536 	 *
537 	 * Since: 2.28
538 	 */
539 	gulong addOnAcceptCertificate(bool delegate(TlsCertificate, GTlsCertificateFlags, TlsConnection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
540 	{
541 		return Signals.connect(this, "accept-certificate", dlg, connectFlags ^ ConnectFlags.SWAPPED);
542 	}
543 }