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