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