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