Skip to content

Commit 0fdf965

Browse files
committed
review fixups for quic-hq-interop
Reviewed-by: Sasa Nedvedicky <[email protected]> Reviewed-by: Viktor Dukhovni <[email protected]> Reviewed-by: Tomas Mraz <[email protected]> (Merged from openssl#25426)
1 parent a62fb94 commit 0fdf965

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

Diff for: demos/guide/quic-hq-interop.c

+39-24
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
static int handle_io_failure(SSL *ssl, int res);
5757
static int set_keylog_file(SSL_CTX *ctx, const char *keylog_file);
5858

59+
#define REQ_STRING_SZ 1024
60+
5961
/**
6062
* @brief A static pointer to a BIO object representing the session's BIO.
6163
*
@@ -147,8 +149,12 @@ static BIO *create_socket_bio(const char *hostname, const char *port,
147149
continue;
148150
}
149151

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) {
152158
BIO_closesocket(sock);
153159
sock = -1;
154160
continue;
@@ -186,7 +192,11 @@ static BIO *create_socket_bio(const char *hostname, const char *port,
186192
* case you must close the socket explicitly when it is no longer
187193
* needed.
188194
*/
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+
}
190200

191201
return bio;
192202
}
@@ -483,12 +493,17 @@ static int setup_session_cache(SSL *ssl, SSL_CTX *ctx, const char *filename)
483493
int rc = 0;
484494
int new_cache = 0;
485495

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))
492507
return rc;
493508

494509
/* open our cache file */
@@ -504,8 +519,6 @@ static int setup_session_cache(SSL *ssl, SSL_CTX *ctx, const char *filename)
504519
if (new_cache == 0) {
505520
/* read in our cached session */
506521
if (PEM_read_bio_SSL_SESSION(session_bio, &sess, NULL, NULL)) {
507-
if (!SSL_CTX_add_session(ctx, sess))
508-
goto err;
509522
/* set our session */
510523
if (!SSL_set_session(ssl, sess))
511524
goto err;
@@ -604,8 +617,8 @@ static size_t build_request_set(SSL *ssl)
604617
{
605618
size_t poll_idx;
606619
char *req;
607-
char outfilename[1024];
608-
char req_string[1024];
620+
char outfilename[REQ_STRING_SZ];
621+
char req_string[REQ_STRING_SZ];
609622
SSL *new_stream;
610623
size_t written;
611624

@@ -668,11 +681,11 @@ static size_t build_request_set(SSL *ssl)
668681
outnames[poll_idx] = req;
669682

670683
/* 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);
672685

673686
/* 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);
676689

677690
/* open a bio to write the file */
678691
outbiolist[poll_idx] = BIO_new_file(outfilename, "w+");
@@ -712,7 +725,6 @@ static size_t build_request_set(SSL *ssl)
712725
while (!SSL_write_ex2(poll_list[poll_idx].desc.value.ssl,
713726
req_string, strlen(req_string),
714727
SSL_WRITE_FLAG_CONCLUDE, &written)) {
715-
fprintf(stderr, "Write failed\n");
716728
if (handle_io_failure(poll_list[poll_idx].desc.value.ssl, 0) == 1)
717729
continue; /* Retry */
718730
fprintf(stderr, "Failed to write start of HTTP request\n");
@@ -786,7 +798,12 @@ static int setup_connection(char *hostname, char *port, int ipv6,
786798
*/
787799
SSL_CTX_set_verify(*ctx, SSL_VERIFY_PEER, NULL);
788800

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+
*/
790807
if (!SSL_CTX_set_default_verify_paths(*ctx)) {
791808
fprintf(stderr, "Failed to set the default trusted certificate store\n");
792809
goto end;
@@ -912,7 +929,6 @@ int main(int argc, char *argv[])
912929
BIO *req_bio = NULL;
913930
int res = EXIT_FAILURE;
914931
int ret;
915-
char req_string[1024];
916932
size_t readbytes = 0;
917933
char buf[160];
918934
int eof = 0;
@@ -931,13 +947,13 @@ int main(int argc, char *argv[])
931947
int ipv6 = 0;
932948

933949
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");
935951
goto end;
936952
}
937953

938954
if (!strcmp(argv[argnext], "-6")) {
939955
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");
941957
goto end;
942958
}
943959
ipv6 = 1;
@@ -947,7 +963,6 @@ int main(int argc, char *argv[])
947963
port = argv[argnext++];
948964
reqfile = argv[argnext];
949965

950-
memset(req_string, 0, 1024);
951966
req_bio = BIO_new_file(reqfile, "r");
952967
if (req_bio == NULL) {
953968
fprintf(stderr, "Failed to open request file %s\n", reqfile);
@@ -956,12 +971,12 @@ int main(int argc, char *argv[])
956971

957972
/* Get the list of requests */
958973
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)) {
960975
fprintf(stderr, "Failed to read some data from request file\n");
961976
goto end;
962977
}
963978
read_offset += bytes_read;
964-
reqnames = OPENSSL_realloc(reqnames, read_offset + 1024);
979+
reqnames = OPENSSL_realloc(reqnames, read_offset + REQ_STRING_SZ);
965980
if (reqnames == NULL) {
966981
fprintf(stderr, "Realloc failure\n");
967982
goto end;

0 commit comments

Comments
 (0)