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