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