56
56
static int handle_io_failure (SSL * ssl , int res );
57
57
static int set_keylog_file (SSL_CTX * ctx , const char * keylog_file );
58
58
59
+ #define REQ_STRING_SZ 1024
60
+
59
61
/**
60
62
* @brief A static pointer to a BIO object representing the session's BIO.
61
63
*
@@ -147,8 +149,12 @@ static BIO *create_socket_bio(const char *hostname, const char *port,
147
149
continue ;
148
150
}
149
151
150
- /* Set to nonblocking mode */
151
- if (!BIO_socket_nbio (sock , 1 )) {
152
+ /*
153
+ * Set to nonblocking mode
154
+ * Note: This function returns a range of errors
155
+ * <= 0 if something goes wrong, so catch them all here
156
+ */
157
+ if (BIO_socket_nbio (sock , 1 ) <= 0 ) {
152
158
BIO_closesocket (sock );
153
159
sock = -1 ;
154
160
continue ;
@@ -186,7 +192,11 @@ static BIO *create_socket_bio(const char *hostname, const char *port,
186
192
* case you must close the socket explicitly when it is no longer
187
193
* needed.
188
194
*/
189
- BIO_set_fd (bio , sock , BIO_CLOSE );
195
+ if (BIO_set_fd (bio , sock , BIO_CLOSE ) <= 0 ) {
196
+ BIO_closesocket (sock );
197
+ BIO_free (bio );
198
+ return NULL ;
199
+ }
190
200
191
201
return bio ;
192
202
}
@@ -483,12 +493,17 @@ static int setup_session_cache(SSL *ssl, SSL_CTX *ctx, const char *filename)
483
493
int rc = 0 ;
484
494
int new_cache = 0 ;
485
495
486
- /* make sure caching is enabled */
487
- if (!SSL_CTX_set_session_cache_mode (ctx , SSL_SESS_CACHE_BOTH ))
488
- return rc ;
489
-
490
- /* Don't use stateless session tickets */
491
- if (!SSL_CTX_set_options (ctx , SSL_OP_NO_TICKET ))
496
+ /*
497
+ * Because we cache sessions to a file in this client, we don't
498
+ * actualy need to internally store sessions, because we restore them
499
+ * from the file with SSL_set_session below, but we want to ensure
500
+ * that caching is enabled so that the session cache callbacks get called
501
+ * properly. The documentation is a bit unclear under what conditions
502
+ * the callback is made, so play it safe here, by enforcing enablement
503
+ */
504
+ if (!SSL_CTX_set_session_cache_mode (ctx , SSL_SESS_CACHE_CLIENT |
505
+ SSL_SESS_CACHE_NO_INTERNAL_STORE |
506
+ SSL_SESS_CACHE_NO_AUTO_CLEAR ))
492
507
return rc ;
493
508
494
509
/* open our cache file */
@@ -504,8 +519,6 @@ static int setup_session_cache(SSL *ssl, SSL_CTX *ctx, const char *filename)
504
519
if (new_cache == 0 ) {
505
520
/* read in our cached session */
506
521
if (PEM_read_bio_SSL_SESSION (session_bio , & sess , NULL , NULL )) {
507
- if (!SSL_CTX_add_session (ctx , sess ))
508
- goto err ;
509
522
/* set our session */
510
523
if (!SSL_set_session (ssl , sess ))
511
524
goto err ;
@@ -604,8 +617,8 @@ static size_t build_request_set(SSL *ssl)
604
617
{
605
618
size_t poll_idx ;
606
619
char * req ;
607
- char outfilename [1024 ];
608
- char req_string [1024 ];
620
+ char outfilename [REQ_STRING_SZ ];
621
+ char req_string [REQ_STRING_SZ ];
609
622
SSL * new_stream ;
610
623
size_t written ;
611
624
@@ -668,11 +681,11 @@ static size_t build_request_set(SSL *ssl)
668
681
outnames [poll_idx ] = req ;
669
682
670
683
/* Format the http request */
671
- sprintf (req_string , "GET /%s\r\n" , req );
684
+ BIO_snprintf (req_string , REQ_STRING_SZ , "GET /%s\r\n" , req );
672
685
673
686
/* build the outfile request path */
674
- memset (outfilename , 0 , 1024 );
675
- sprintf (outfilename , "/downloads/%s" , req );
687
+ memset (outfilename , 0 , REQ_STRING_SZ );
688
+ BIO_snprintf (outfilename , REQ_STRING_SZ , "/downloads/%s" , req );
676
689
677
690
/* open a bio to write the file */
678
691
outbiolist [poll_idx ] = BIO_new_file (outfilename , "w+" );
@@ -712,7 +725,6 @@ static size_t build_request_set(SSL *ssl)
712
725
while (!SSL_write_ex2 (poll_list [poll_idx ].desc .value .ssl ,
713
726
req_string , strlen (req_string ),
714
727
SSL_WRITE_FLAG_CONCLUDE , & written )) {
715
- fprintf (stderr , "Write failed\n" );
716
728
if (handle_io_failure (poll_list [poll_idx ].desc .value .ssl , 0 ) == 1 )
717
729
continue ; /* Retry */
718
730
fprintf (stderr , "Failed to write start of HTTP request\n" );
@@ -786,7 +798,12 @@ static int setup_connection(char *hostname, char *port, int ipv6,
786
798
*/
787
799
SSL_CTX_set_verify (* ctx , SSL_VERIFY_PEER , NULL );
788
800
789
- /* Use the default trusted certificate store */
801
+ /*
802
+ * Use the default trusted certificate store
803
+ * Note: The store is read from SSL_CERT_DIR and SSL_CERT_FILE
804
+ * environment variables in the default case, so users can set those
805
+ * When running this application to direct where the store is loaded from
806
+ */
790
807
if (!SSL_CTX_set_default_verify_paths (* ctx )) {
791
808
fprintf (stderr , "Failed to set the default trusted certificate store\n" );
792
809
goto end ;
@@ -912,7 +929,6 @@ int main(int argc, char *argv[])
912
929
BIO * req_bio = NULL ;
913
930
int res = EXIT_FAILURE ;
914
931
int ret ;
915
- char req_string [1024 ];
916
932
size_t readbytes = 0 ;
917
933
char buf [160 ];
918
934
int eof = 0 ;
@@ -931,13 +947,13 @@ int main(int argc, char *argv[])
931
947
int ipv6 = 0 ;
932
948
933
949
if (argc < 4 ) {
934
- fprintf (stderr , "Usage: quic-hq-interop [-6] hostname port file \n" );
950
+ fprintf (stderr , "Usage: quic-hq-interop [-6] hostname port reqfile \n" );
935
951
goto end ;
936
952
}
937
953
938
954
if (!strcmp (argv [argnext ], "-6" )) {
939
955
if (argc < 5 ) {
940
- fprintf (stderr , "Usage: quic-hq-interop [-6] hostname port\n" );
956
+ fprintf (stderr , "Usage: quic-hq-interop [-6] hostname port reqfile \n" );
941
957
goto end ;
942
958
}
943
959
ipv6 = 1 ;
@@ -947,7 +963,6 @@ int main(int argc, char *argv[])
947
963
port = argv [argnext ++ ];
948
964
reqfile = argv [argnext ];
949
965
950
- memset (req_string , 0 , 1024 );
951
966
req_bio = BIO_new_file (reqfile , "r" );
952
967
if (req_bio == NULL ) {
953
968
fprintf (stderr , "Failed to open request file %s\n" , reqfile );
@@ -956,12 +971,12 @@ int main(int argc, char *argv[])
956
971
957
972
/* Get the list of requests */
958
973
while (!BIO_eof (req_bio )) {
959
- if (!BIO_read_ex (req_bio , & reqnames [read_offset ], 1024 , & bytes_read )) {
974
+ if (!BIO_read_ex (req_bio , & reqnames [read_offset ], REQ_STRING_SZ , & bytes_read )) {
960
975
fprintf (stderr , "Failed to read some data from request file\n" );
961
976
goto end ;
962
977
}
963
978
read_offset += bytes_read ;
964
- reqnames = OPENSSL_realloc (reqnames , read_offset + 1024 );
979
+ reqnames = OPENSSL_realloc (reqnames , read_offset + REQ_STRING_SZ );
965
980
if (reqnames == NULL ) {
966
981
fprintf (stderr , "Realloc failure\n" );
967
982
goto end ;
0 commit comments