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