1*e7be843bSPierre Pronchery=pod 2*e7be843bSPierre Pronchery 3*e7be843bSPierre Pronchery=begin comment 4*e7be843bSPierre Pronchery 5*e7be843bSPierre ProncheryNB: Changes to the source code samples in this file should also be reflected in 6*e7be843bSPierre Proncherydemos/guide/tls-client-non-block.c 7*e7be843bSPierre Pronchery 8*e7be843bSPierre Pronchery=end comment 9*e7be843bSPierre Pronchery 10*e7be843bSPierre Pronchery=head1 NAME 11*e7be843bSPierre Pronchery 12*e7be843bSPierre Proncheryossl-guide-tls-client-non-block 13*e7be843bSPierre Pronchery- OpenSSL Guide: Writing a simple nonblocking TLS client 14*e7be843bSPierre Pronchery 15*e7be843bSPierre Pronchery=head1 SIMPLE NONBLOCKING TLS CLIENT EXAMPLE 16*e7be843bSPierre Pronchery 17*e7be843bSPierre ProncheryThis page will build on the example developed on the 18*e7be843bSPierre ProncheryL<ossl-guide-tls-client-block(7)> page which demonstrates how to write a simple 19*e7be843bSPierre Proncheryblocking TLS client. On this page we will amend that demo code so that it 20*e7be843bSPierre Proncherysupports a nonblocking socket. 21*e7be843bSPierre Pronchery 22*e7be843bSPierre ProncheryThe complete source code for this example nonblocking TLS client is available 23*e7be843bSPierre Proncheryin the B<demos/guide> directory of the OpenSSL source distribution in the file 24*e7be843bSPierre ProncheryB<tls-client-non-block.c>. It is also available online at 25*e7be843bSPierre ProncheryL<https://github.com/openssl/openssl/blob/master/demos/guide/tls-client-non-block.c>. 26*e7be843bSPierre Pronchery 27*e7be843bSPierre ProncheryAs we saw in the previous example a blocking socket is one which waits (blocks) 28*e7be843bSPierre Proncheryuntil data is available to read if you attempt to read from it when there is no 29*e7be843bSPierre Proncherydata yet. Similarly it waits when writing if the socket is currently unable to 30*e7be843bSPierre Proncherywrite at the moment. This can simplify the development of code because you do 31*e7be843bSPierre Proncherynot have to worry about what to do in these cases. The execution of the code 32*e7be843bSPierre Proncherywill simply stop until it is able to continue. However in many cases you do not 33*e7be843bSPierre Proncherywant this behaviour. Rather than stopping and waiting your application may need 34*e7be843bSPierre Proncheryto go and do other tasks whilst the socket is unable to read/write, for example 35*e7be843bSPierre Proncheryupdating a GUI or performing operations on some other socket. 36*e7be843bSPierre Pronchery 37*e7be843bSPierre ProncheryWith a nonblocking socket attempting to read or write to a socket that is 38*e7be843bSPierre Proncherycurrently unable to read or write will return immediately with a non-fatal 39*e7be843bSPierre Proncheryerror. Although OpenSSL does the reading/writing to the socket this nonblocking 40*e7be843bSPierre Proncherybehaviour is propagated up to the application so that OpenSSL I/O functions such 41*e7be843bSPierre Proncheryas L<SSL_read_ex(3)> or L<SSL_write_ex(3)> will not block. 42*e7be843bSPierre Pronchery 43*e7be843bSPierre ProncherySince this page is building on the example developed on the 44*e7be843bSPierre ProncheryL<ossl-guide-tls-client-block(7)> page we assume that you are familiar with it 45*e7be843bSPierre Proncheryand we only explain how this example differs. 46*e7be843bSPierre Pronchery 47*e7be843bSPierre Pronchery=head2 Setting the socket to be nonblocking 48*e7be843bSPierre Pronchery 49*e7be843bSPierre ProncheryThe first step in writing an application that supports nonblocking is to set 50*e7be843bSPierre Proncherythe socket into nonblocking mode. A socket will be default be blocking. The 51*e7be843bSPierre Proncheryexact details on how to do this can differ from one platform to another. 52*e7be843bSPierre ProncheryFortunately OpenSSL offers a portable function that will do this for you: 53*e7be843bSPierre Pronchery 54*e7be843bSPierre Pronchery /* Set to nonblocking mode */ 55*e7be843bSPierre Pronchery if (!BIO_socket_nbio(sock, 1)) { 56*e7be843bSPierre Pronchery sock = -1; 57*e7be843bSPierre Pronchery continue; 58*e7be843bSPierre Pronchery } 59*e7be843bSPierre Pronchery 60*e7be843bSPierre ProncheryYou do not have to use OpenSSL's function for this. You can of course directly 61*e7be843bSPierre Proncherycall whatever functions that your Operating System provides for this purpose on 62*e7be843bSPierre Proncheryyour platform. 63*e7be843bSPierre Pronchery 64*e7be843bSPierre Pronchery=head2 Performing work while waiting for the socket 65*e7be843bSPierre Pronchery 66*e7be843bSPierre ProncheryIn a nonblocking application you will need work to perform in the event that 67*e7be843bSPierre Proncherywe want to read or write to the socket, but we are currently unable to. In fact 68*e7be843bSPierre Proncherythis is the whole point of using a nonblocking socket, i.e. to give the 69*e7be843bSPierre Proncheryapplication the opportunity to do something else. Whatever it is that the 70*e7be843bSPierre Proncheryapplication has to do, it must also be prepared to come back and retry the 71*e7be843bSPierre Proncheryoperation that it previously attempted periodically to see if it can now 72*e7be843bSPierre Proncherycomplete. Ideally it would only do this in the event that the state of the 73*e7be843bSPierre Proncheryunderlying socket has actually changed (e.g. become readable where it wasn't 74*e7be843bSPierre Proncherybefore), but this does not have to be the case. It can retry at any time. 75*e7be843bSPierre Pronchery 76*e7be843bSPierre ProncheryNote that it is important that you retry exactly the same operation that you 77*e7be843bSPierre Proncherytried last time. You cannot start something new. For example if you were 78*e7be843bSPierre Proncheryattempting to write the text "Hello World" and the operation failed because the 79*e7be843bSPierre Proncherysocket is currently unable to write, then you cannot then attempt to write 80*e7be843bSPierre Proncherysome other text when you retry the operation. 81*e7be843bSPierre Pronchery 82*e7be843bSPierre ProncheryIn this demo application we will create a helper function which simulates doing 83*e7be843bSPierre Proncheryother work. In fact, for the sake of simplicity, it will do nothing except wait 84*e7be843bSPierre Proncheryfor the state of the socket to change. 85*e7be843bSPierre Pronchery 86*e7be843bSPierre ProncheryWe call our function C<wait_for_activity()> because all it does is wait until 87*e7be843bSPierre Proncherythe underlying socket has become readable or writeable when it wasn't before. 88*e7be843bSPierre Pronchery 89*e7be843bSPierre Pronchery static void wait_for_activity(SSL *ssl, int write) 90*e7be843bSPierre Pronchery { 91*e7be843bSPierre Pronchery fd_set fds; 92*e7be843bSPierre Pronchery int width, sock; 93*e7be843bSPierre Pronchery 94*e7be843bSPierre Pronchery /* Get hold of the underlying file descriptor for the socket */ 95*e7be843bSPierre Pronchery sock = SSL_get_fd(ssl); 96*e7be843bSPierre Pronchery 97*e7be843bSPierre Pronchery FD_ZERO(&fds); 98*e7be843bSPierre Pronchery FD_SET(sock, &fds); 99*e7be843bSPierre Pronchery width = sock + 1; 100*e7be843bSPierre Pronchery 101*e7be843bSPierre Pronchery /* 102*e7be843bSPierre Pronchery * Wait until the socket is writeable or readable. We use select here 103*e7be843bSPierre Pronchery * for the sake of simplicity and portability, but you could equally use 104*e7be843bSPierre Pronchery * poll/epoll or similar functions 105*e7be843bSPierre Pronchery * 106*e7be843bSPierre Pronchery * NOTE: For the purposes of this demonstration code this effectively 107*e7be843bSPierre Pronchery * makes this demo block until it has something more useful to do. In a 108*e7be843bSPierre Pronchery * real application you probably want to go and do other work here (e.g. 109*e7be843bSPierre Pronchery * update a GUI, or service other connections). 110*e7be843bSPierre Pronchery * 111*e7be843bSPierre Pronchery * Let's say for example that you want to update the progress counter on 112*e7be843bSPierre Pronchery * a GUI every 100ms. One way to do that would be to add a 100ms timeout 113*e7be843bSPierre Pronchery * in the last parameter to "select" below. Then, when select returns, 114*e7be843bSPierre Pronchery * you check if it did so because of activity on the file descriptors or 115*e7be843bSPierre Pronchery * because of the timeout. If it is due to the timeout then update the 116*e7be843bSPierre Pronchery * GUI and then restart the "select". 117*e7be843bSPierre Pronchery */ 118*e7be843bSPierre Pronchery if (write) 119*e7be843bSPierre Pronchery select(width, NULL, &fds, NULL, NULL); 120*e7be843bSPierre Pronchery else 121*e7be843bSPierre Pronchery select(width, &fds, NULL, NULL, NULL); 122*e7be843bSPierre Pronchery } 123*e7be843bSPierre Pronchery 124*e7be843bSPierre ProncheryIn this example we are using the C<select> function because it is very simple 125*e7be843bSPierre Proncheryto use and is available on most Operating Systems. However you could use any 126*e7be843bSPierre Proncheryother similar function to do the same thing. C<select> waits for the state of 127*e7be843bSPierre Proncherythe underlying socket(s) to become readable/writeable before returning. It also 128*e7be843bSPierre Proncherysupports a "timeout" (as do most other similar functions) so in your own 129*e7be843bSPierre Proncheryapplications you can make use of this to periodically wake up and perform work 130*e7be843bSPierre Proncherywhile waiting for the socket state to change. But we don't use that timeout 131*e7be843bSPierre Proncherycapability in this example for the sake of simplicity. 132*e7be843bSPierre Pronchery 133*e7be843bSPierre Pronchery=head2 Handling errors from OpenSSL I/O functions 134*e7be843bSPierre Pronchery 135*e7be843bSPierre ProncheryAn application that uses a nonblocking socket will need to be prepared to 136*e7be843bSPierre Proncheryhandle errors returned from OpenSSL I/O functions such as L<SSL_read_ex(3)> or 137*e7be843bSPierre ProncheryL<SSL_write_ex(3)>. Errors may be fatal (for example because the underlying 138*e7be843bSPierre Proncheryconnection has failed), or non-fatal (for example because we are trying to read 139*e7be843bSPierre Proncheryfrom the underlying socket but the data has not yet arrived from the peer). 140*e7be843bSPierre Pronchery 141*e7be843bSPierre ProncheryL<SSL_read_ex(3)> and L<SSL_write_ex(3)> will return 0 to indicate an error and 142*e7be843bSPierre ProncheryL<SSL_read(3)> and L<SSL_write(3)> will return 0 or a negative value to indicate 143*e7be843bSPierre Proncheryan error. L<SSL_shutdown(3)> will return a negative value to incidate an error. 144*e7be843bSPierre Pronchery 145*e7be843bSPierre ProncheryIn the event of an error an application should call L<SSL_get_error(3)> to find 146*e7be843bSPierre Proncheryout what type of error has occurred. If the error is non-fatal and can be 147*e7be843bSPierre Proncheryretried then L<SSL_get_error(3)> will return B<SSL_ERROR_WANT_READ> or 148*e7be843bSPierre ProncheryB<SSL_ERROR_WANT_WRITE> depending on whether OpenSSL wanted to read to or write 149*e7be843bSPierre Proncheryfrom the socket but was unable to. Note that a call to L<SSL_read_ex(3)> or 150*e7be843bSPierre ProncheryL<SSL_read(3)> can still generate B<SSL_ERROR_WANT_WRITE> because OpenSSL 151*e7be843bSPierre Proncherymay need to write protocol messages (such as to update cryptographic keys) even 152*e7be843bSPierre Proncheryif the application is only trying to read data. Similarly calls to 153*e7be843bSPierre ProncheryL<SSL_write_ex(3)> or L<SSL_write(3)> might generate B<SSL_ERROR_WANT_READ>. 154*e7be843bSPierre Pronchery 155*e7be843bSPierre ProncheryAnother type of non-fatal error that may occur is B<SSL_ERROR_ZERO_RETURN>. This 156*e7be843bSPierre Proncheryindicates an EOF (End-Of-File) which can occur if you attempt to read data from 157*e7be843bSPierre Proncheryan B<SSL> object but the peer has indicated that it will not send any more data 158*e7be843bSPierre Proncheryon it. In this case you may still want to write data to the connection but you 159*e7be843bSPierre Proncherywill not receive any more data. 160*e7be843bSPierre Pronchery 161*e7be843bSPierre ProncheryFatal errors that may occur are B<SSL_ERROR_SYSCALL> and B<SSL_ERROR_SSL>. These 162*e7be843bSPierre Proncheryindicate that the underlying connection has failed. You should not attempt to 163*e7be843bSPierre Proncheryshut it down with L<SSL_shutdown(3)>. B<SSL_ERROR_SYSCALL> indicates that 164*e7be843bSPierre ProncheryOpenSSL attempted to make a syscall that failed. You can consult B<errno> for 165*e7be843bSPierre Proncheryfurther details. B<SSL_ERROR_SSL> indicates that some OpenSSL error occurred. You 166*e7be843bSPierre Proncherycan consult the OpenSSL error stack for further details (for example by calling 167*e7be843bSPierre ProncheryL<ERR_print_errors(3)> to print out details of errors that have occurred). 168*e7be843bSPierre Pronchery 169*e7be843bSPierre ProncheryIn our demo application we will write a function to handle these errors from 170*e7be843bSPierre ProncheryOpenSSL I/O functions: 171*e7be843bSPierre Pronchery 172*e7be843bSPierre Pronchery static int handle_io_failure(SSL *ssl, int res) 173*e7be843bSPierre Pronchery { 174*e7be843bSPierre Pronchery switch (SSL_get_error(ssl, res)) { 175*e7be843bSPierre Pronchery case SSL_ERROR_WANT_READ: 176*e7be843bSPierre Pronchery /* Temporary failure. Wait until we can read and try again */ 177*e7be843bSPierre Pronchery wait_for_activity(ssl, 0); 178*e7be843bSPierre Pronchery return 1; 179*e7be843bSPierre Pronchery 180*e7be843bSPierre Pronchery case SSL_ERROR_WANT_WRITE: 181*e7be843bSPierre Pronchery /* Temporary failure. Wait until we can write and try again */ 182*e7be843bSPierre Pronchery wait_for_activity(ssl, 1); 183*e7be843bSPierre Pronchery return 1; 184*e7be843bSPierre Pronchery 185*e7be843bSPierre Pronchery case SSL_ERROR_ZERO_RETURN: 186*e7be843bSPierre Pronchery /* EOF */ 187*e7be843bSPierre Pronchery return 0; 188*e7be843bSPierre Pronchery 189*e7be843bSPierre Pronchery case SSL_ERROR_SYSCALL: 190*e7be843bSPierre Pronchery return -1; 191*e7be843bSPierre Pronchery 192*e7be843bSPierre Pronchery case SSL_ERROR_SSL: 193*e7be843bSPierre Pronchery /* 194*e7be843bSPierre Pronchery * If the failure is due to a verification error we can get more 195*e7be843bSPierre Pronchery * information about it from SSL_get_verify_result(). 196*e7be843bSPierre Pronchery */ 197*e7be843bSPierre Pronchery if (SSL_get_verify_result(ssl) != X509_V_OK) 198*e7be843bSPierre Pronchery printf("Verify error: %s\n", 199*e7be843bSPierre Pronchery X509_verify_cert_error_string(SSL_get_verify_result(ssl))); 200*e7be843bSPierre Pronchery return -1; 201*e7be843bSPierre Pronchery 202*e7be843bSPierre Pronchery default: 203*e7be843bSPierre Pronchery return -1; 204*e7be843bSPierre Pronchery } 205*e7be843bSPierre Pronchery } 206*e7be843bSPierre Pronchery 207*e7be843bSPierre ProncheryThis function takes as arguments the B<SSL> object that represents the 208*e7be843bSPierre Proncheryconnection, as well as the return code from the I/O function that failed. In 209*e7be843bSPierre Proncherythe event of a non-fatal failure, it waits until a retry of the I/O operation 210*e7be843bSPierre Proncherymight succeed (by using the C<wait_for_activity()> function that we developed 211*e7be843bSPierre Proncheryin the previous section). It returns 1 in the event of a non-fatal error 212*e7be843bSPierre Pronchery(except EOF), 0 in the event of EOF, or -1 if a fatal error occurred. 213*e7be843bSPierre Pronchery 214*e7be843bSPierre Pronchery=head2 Creating the SSL_CTX and SSL objects 215*e7be843bSPierre Pronchery 216*e7be843bSPierre ProncheryIn order to connect to a server we must create B<SSL_CTX> and B<SSL> objects for 217*e7be843bSPierre Proncherythis. The steps do this are the same as for a blocking client and are explained 218*e7be843bSPierre Proncheryon the L<ossl-guide-tls-client-block(7)> page. We won't repeat that information 219*e7be843bSPierre Proncheryhere. 220*e7be843bSPierre Pronchery 221*e7be843bSPierre Pronchery=head2 Performing the handshake 222*e7be843bSPierre Pronchery 223*e7be843bSPierre ProncheryAs in the demo for a blocking TLS client we use the L<SSL_connect(3)> function 224*e7be843bSPierre Proncheryto perform the TLS handshake with the server. Since we are using a nonblocking 225*e7be843bSPierre Proncherysocket it is very likely that calls to this function will fail with a non-fatal 226*e7be843bSPierre Proncheryerror while we are waiting for the server to respond to our handshake messages. 227*e7be843bSPierre ProncheryIn such a case we must retry the same L<SSL_connect(3)> call at a later time. 228*e7be843bSPierre ProncheryIn this demo we this in a loop: 229*e7be843bSPierre Pronchery 230*e7be843bSPierre Pronchery /* Do the handshake with the server */ 231*e7be843bSPierre Pronchery while ((ret = SSL_connect(ssl)) != 1) { 232*e7be843bSPierre Pronchery if (handle_io_failure(ssl, ret) == 1) 233*e7be843bSPierre Pronchery continue; /* Retry */ 234*e7be843bSPierre Pronchery printf("Failed to connect to server\n"); 235*e7be843bSPierre Pronchery goto end; /* Cannot retry: error */ 236*e7be843bSPierre Pronchery } 237*e7be843bSPierre Pronchery 238*e7be843bSPierre ProncheryWe continually call L<SSL_connect(3)> until it gives us a success response. 239*e7be843bSPierre ProncheryOtherwise we use the C<handle_io_failure()> function that we created earlier to 240*e7be843bSPierre Proncherywork out what we should do next. Note that we do not expect an EOF to occur at 241*e7be843bSPierre Proncherythis stage, so such a response is treated in the same way as a fatal error. 242*e7be843bSPierre Pronchery 243*e7be843bSPierre Pronchery=head2 Sending and receiving data 244*e7be843bSPierre Pronchery 245*e7be843bSPierre ProncheryAs with the blocking TLS client demo we use the L<SSL_write_ex(3)> function to 246*e7be843bSPierre Proncherysend data to the server. As with L<SSL_connect(3)> above, because we are using 247*e7be843bSPierre Proncherya nonblocking socket, this call could fail with a non-fatal error. In that case 248*e7be843bSPierre Proncherywe should retry exactly the same L<SSL_write_ex(3)> call again. Note that the 249*e7be843bSPierre Proncheryparameters must be I<exactly> the same, i.e. the same pointer to the buffer to 250*e7be843bSPierre Proncherywrite with the same length. You must not attempt to send different data on a 251*e7be843bSPierre Proncheryretry. An optional mode does exist (B<SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER>) 252*e7be843bSPierre Proncherywhich will configure OpenSSL to allow the buffer being written to change from 253*e7be843bSPierre Proncheryone retry to the next. However, in this case, you must still retry exactly the 254*e7be843bSPierre Proncherysame data - even though the buffer that contains that data may change location. 255*e7be843bSPierre ProncherySee L<SSL_CTX_set_mode(3)> for further details. As in the TLS client 256*e7be843bSPierre Proncheryblocking tutorial (L<ossl-guide-tls-client-block(7)>) we write the request 257*e7be843bSPierre Proncheryin three chunks. 258*e7be843bSPierre Pronchery 259*e7be843bSPierre Pronchery /* Write an HTTP GET request to the peer */ 260*e7be843bSPierre Pronchery while (!SSL_write_ex(ssl, request_start, strlen(request_start), &written)) { 261*e7be843bSPierre Pronchery if (handle_io_failure(ssl, 0) == 1) 262*e7be843bSPierre Pronchery continue; /* Retry */ 263*e7be843bSPierre Pronchery printf("Failed to write start of HTTP request\n"); 264*e7be843bSPierre Pronchery goto end; /* Cannot retry: error */ 265*e7be843bSPierre Pronchery } 266*e7be843bSPierre Pronchery while (!SSL_write_ex(ssl, hostname, strlen(hostname), &written)) { 267*e7be843bSPierre Pronchery if (handle_io_failure(ssl, 0) == 1) 268*e7be843bSPierre Pronchery continue; /* Retry */ 269*e7be843bSPierre Pronchery printf("Failed to write hostname in HTTP request\n"); 270*e7be843bSPierre Pronchery goto end; /* Cannot retry: error */ 271*e7be843bSPierre Pronchery } 272*e7be843bSPierre Pronchery while (!SSL_write_ex(ssl, request_end, strlen(request_end), &written)) { 273*e7be843bSPierre Pronchery if (handle_io_failure(ssl, 0) == 1) 274*e7be843bSPierre Pronchery continue; /* Retry */ 275*e7be843bSPierre Pronchery printf("Failed to write end of HTTP request\n"); 276*e7be843bSPierre Pronchery goto end; /* Cannot retry: error */ 277*e7be843bSPierre Pronchery } 278*e7be843bSPierre Pronchery 279*e7be843bSPierre ProncheryOn a write we do not expect to see an EOF response so we treat that case in the 280*e7be843bSPierre Proncherysame way as a fatal error. 281*e7be843bSPierre Pronchery 282*e7be843bSPierre ProncheryReading a response back from the server is similar: 283*e7be843bSPierre Pronchery 284*e7be843bSPierre Pronchery do { 285*e7be843bSPierre Pronchery /* 286*e7be843bSPierre Pronchery * Get up to sizeof(buf) bytes of the response. We keep reading until 287*e7be843bSPierre Pronchery * the server closes the connection. 288*e7be843bSPierre Pronchery */ 289*e7be843bSPierre Pronchery while (!eof && !SSL_read_ex(ssl, buf, sizeof(buf), &readbytes)) { 290*e7be843bSPierre Pronchery switch (handle_io_failure(ssl, 0)) { 291*e7be843bSPierre Pronchery case 1: 292*e7be843bSPierre Pronchery continue; /* Retry */ 293*e7be843bSPierre Pronchery case 0: 294*e7be843bSPierre Pronchery eof = 1; 295*e7be843bSPierre Pronchery continue; 296*e7be843bSPierre Pronchery case -1: 297*e7be843bSPierre Pronchery default: 298*e7be843bSPierre Pronchery printf("Failed reading remaining data\n"); 299*e7be843bSPierre Pronchery goto end; /* Cannot retry: error */ 300*e7be843bSPierre Pronchery } 301*e7be843bSPierre Pronchery } 302*e7be843bSPierre Pronchery /* 303*e7be843bSPierre Pronchery * OpenSSL does not guarantee that the returned data is a string or 304*e7be843bSPierre Pronchery * that it is NUL terminated so we use fwrite() to write the exact 305*e7be843bSPierre Pronchery * number of bytes that we read. The data could be non-printable or 306*e7be843bSPierre Pronchery * have NUL characters in the middle of it. For this simple example 307*e7be843bSPierre Pronchery * we're going to print it to stdout anyway. 308*e7be843bSPierre Pronchery */ 309*e7be843bSPierre Pronchery if (!eof) 310*e7be843bSPierre Pronchery fwrite(buf, 1, readbytes, stdout); 311*e7be843bSPierre Pronchery } while (!eof); 312*e7be843bSPierre Pronchery /* In case the response didn't finish with a newline we add one now */ 313*e7be843bSPierre Pronchery printf("\n"); 314*e7be843bSPierre Pronchery 315*e7be843bSPierre ProncheryThe main difference this time is that it is valid for us to receive an EOF 316*e7be843bSPierre Proncheryresponse when trying to read data from the server. This will occur when the 317*e7be843bSPierre Proncheryserver closes down the connection after sending all the data in its response. 318*e7be843bSPierre Pronchery 319*e7be843bSPierre ProncheryIn this demo we just print out all the data we've received back in the response 320*e7be843bSPierre Proncheryfrom the server. We continue going around the loop until we either encounter a 321*e7be843bSPierre Proncheryfatal error, or we receive an EOF (indicating a graceful finish). 322*e7be843bSPierre Pronchery 323*e7be843bSPierre Pronchery=head2 Shutting down the connection 324*e7be843bSPierre Pronchery 325*e7be843bSPierre ProncheryAs in the TLS blocking example we must shutdown the connection when we are 326*e7be843bSPierre Proncheryfinished with it. 327*e7be843bSPierre Pronchery 328*e7be843bSPierre ProncheryIf our application was initiating the shutdown then we would expect to see 329*e7be843bSPierre ProncheryL<SSL_shutdown(3)> give a return value of 0, and then we would continue to call 330*e7be843bSPierre Proncheryit until we received a return value of 1 (meaning we have successfully completed 331*e7be843bSPierre Proncherythe shutdown). In this particular example we don't expect SSL_shutdown() to 332*e7be843bSPierre Proncheryreturn 0 because we have already received EOF from the server indicating that it 333*e7be843bSPierre Proncheryhas shutdown already. So we just keep calling it until SSL_shutdown() returns 1. 334*e7be843bSPierre ProncherySince we are using a nonblocking socket we might expect to have to retry this 335*e7be843bSPierre Proncheryoperation several times. If L<SSL_shutdown(3)> returns a negative result then we 336*e7be843bSPierre Proncherymust call L<SSL_get_error(3)> to work out what to do next. We use our 337*e7be843bSPierre Proncheryhandle_io_failure() function that we developed earlier for this: 338*e7be843bSPierre Pronchery 339*e7be843bSPierre Pronchery /* 340*e7be843bSPierre Pronchery * The peer already shutdown gracefully (we know this because of the 341*e7be843bSPierre Pronchery * SSL_ERROR_ZERO_RETURN (i.e. EOF) above). We should do the same back. 342*e7be843bSPierre Pronchery */ 343*e7be843bSPierre Pronchery while ((ret = SSL_shutdown(ssl)) != 1) { 344*e7be843bSPierre Pronchery if (ret < 0 && handle_io_failure(ssl, ret) == 1) 345*e7be843bSPierre Pronchery continue; /* Retry */ 346*e7be843bSPierre Pronchery /* 347*e7be843bSPierre Pronchery * ret == 0 is unexpected here because that means "we've sent a 348*e7be843bSPierre Pronchery * close_notify and we're waiting for one back". But we already know 349*e7be843bSPierre Pronchery * we got one from the peer because of the SSL_ERROR_ZERO_RETURN 350*e7be843bSPierre Pronchery * (i.e. EOF) above. 351*e7be843bSPierre Pronchery */ 352*e7be843bSPierre Pronchery printf("Error shutting down\n"); 353*e7be843bSPierre Pronchery goto end; /* Cannot retry: error */ 354*e7be843bSPierre Pronchery } 355*e7be843bSPierre Pronchery 356*e7be843bSPierre Pronchery=head2 Final clean up 357*e7be843bSPierre Pronchery 358*e7be843bSPierre ProncheryAs with the blocking TLS client example, once our connection is finished with we 359*e7be843bSPierre Proncherymust free it. The steps to do this for this example are the same as for the 360*e7be843bSPierre Proncheryblocking example, so we won't repeat it here. 361*e7be843bSPierre Pronchery 362*e7be843bSPierre Pronchery=head1 FURTHER READING 363*e7be843bSPierre Pronchery 364*e7be843bSPierre ProncherySee L<ossl-guide-tls-client-block(7)> to read a tutorial on how to write a 365*e7be843bSPierre Proncheryblocking TLS client. See L<ossl-guide-quic-client-block(7)> to see how to do the 366*e7be843bSPierre Proncherysame thing for a QUIC client. 367*e7be843bSPierre Pronchery 368*e7be843bSPierre Pronchery=head1 SEE ALSO 369*e7be843bSPierre Pronchery 370*e7be843bSPierre ProncheryL<ossl-guide-introduction(7)>, L<ossl-guide-libraries-introduction(7)>, 371*e7be843bSPierre ProncheryL<ossl-guide-libssl-introduction(7)>, L<ossl-guide-tls-introduction(7)>, 372*e7be843bSPierre ProncheryL<ossl-guide-tls-client-block(7)>, L<ossl-guide-quic-client-block(7)> 373*e7be843bSPierre Pronchery 374*e7be843bSPierre Pronchery=head1 COPYRIGHT 375*e7be843bSPierre Pronchery 376*e7be843bSPierre ProncheryCopyright 2023 The OpenSSL Project Authors. All Rights Reserved. 377*e7be843bSPierre Pronchery 378*e7be843bSPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 379*e7be843bSPierre Proncherythis file except in compliance with the License. You can obtain a copy 380*e7be843bSPierre Proncheryin the file LICENSE in the source distribution or at 381*e7be843bSPierre ProncheryL<https://www.openssl.org/source/license.html>. 382*e7be843bSPierre Pronchery 383*e7be843bSPierre Pronchery=cut 384