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