21
21
#include <openssl/buffer.h>
22
22
#include <openssl/http.h>
23
23
#include "internal/sockets.h"
24
- #include "internal/cryptlib.h"
24
+ #include "internal/cryptlib.h" /* for ossl_assert() */
25
25
26
26
#include "http_local.h"
27
27
@@ -157,7 +157,7 @@ int OSSL_HTTP_REQ_CTX_header(OSSL_HTTP_REQ_CTX *rctx, const char *server,
157
157
* Section 5.1.2 of RFC 1945 states that the absoluteURI form is only
158
158
* allowed when using a proxy
159
159
*/
160
- if (BIO_printf (rctx -> mem , "http:// %s" , server ) <= 0 )
160
+ if (BIO_printf (rctx -> mem , OSSL_HTTP_PREFIX " %s" , server ) <= 0 )
161
161
return 0 ;
162
162
if (port != NULL && BIO_printf (rctx -> mem , ":%s" , port ) <= 0 )
163
163
return 0 ;
@@ -701,10 +701,8 @@ static BIO *HTTP_new_bio(const char *server /* optionally includes ":port" */,
701
701
const char * port = server_port ;
702
702
BIO * cbio ;
703
703
704
- if (server == NULL ) {
705
- HTTPerr (0 , ERR_R_PASSED_NULL_PARAMETER );
704
+ if (!ossl_assert (server != NULL ))
706
705
return NULL ;
707
- }
708
706
709
707
if (proxy != NULL ) {
710
708
host = proxy ;
@@ -714,7 +712,7 @@ static BIO *HTTP_new_bio(const char *server /* optionally includes ":port" */,
714
712
host_end = strchr (host , '/' );
715
713
if (host_end != NULL && (size_t )(host_end - host ) < sizeof (host_name )) {
716
714
/* chop trailing string starting with '/' */
717
- strncpy (host_name , host , host_end - host );
715
+ strncpy (host_name , host , host_end - host + 1 );
718
716
host = host_name ;
719
717
}
720
718
@@ -849,18 +847,28 @@ BIO *OSSL_HTTP_transfer(const char *server, const char *port, const char *path,
849
847
HTTPerr (0 , ERR_R_PASSED_INVALID_ARGUMENT );
850
848
return NULL ;
851
849
}
852
- /* remaining parameters are checked indirectly by the functions called */
853
850
854
- proxy = http_adapt_proxy (proxy , no_proxy , server , use_ssl );
855
- if (bio != NULL )
851
+ if (bio != NULL ) {
856
852
cbio = bio ;
857
- else
853
+ } else {
858
854
#ifndef OPENSSL_NO_SOCK
855
+ if (server == NULL ) {
856
+ HTTPerr (0 , ERR_R_PASSED_NULL_PARAMETER );
857
+ return NULL ;
858
+ }
859
+ if (* port == '\0' )
860
+ port = NULL ;
861
+ if (port == NULL && strchr (server , ':' ) == NULL )
862
+ port = use_ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT ;
863
+ proxy = http_adapt_proxy (proxy , no_proxy , server , use_ssl );
859
864
if ((cbio = HTTP_new_bio (server , port , proxy )) == NULL )
860
865
return NULL ;
861
866
#else
867
+ HTTPerr (0 , HTTP_R_SOCK_NOT_SUPPORTED );
862
868
return NULL ;
863
869
#endif
870
+ }
871
+ /* remaining parameters are checked indirectly by the functions called */
864
872
865
873
(void )ERR_set_mark (); /* prepare removing any spurious libssl errors */
866
874
if (rbio == NULL && BIO_connect_retry (cbio , timeout ) <= 0 )
@@ -902,10 +910,10 @@ BIO *OSSL_HTTP_transfer(const char *server, const char *port, const char *path,
902
910
if (lib == ERR_LIB_SSL || lib == ERR_LIB_HTTP
903
911
|| (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_TIMEOUT )
904
912
|| (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_ERROR )
905
- # ifndef OPENSSL_NO_CMP
913
+ #ifndef OPENSSL_NO_CMP
906
914
|| (lib == ERR_LIB_CMP
907
915
&& reason == CMP_R_POTENTIALLY_INVALID_CERTIFICATE )
908
- # endif
916
+ #endif
909
917
) {
910
918
BIO_snprintf (buf , 200 , "server=%s:%s" , server , port );
911
919
ERR_add_error_data (1 , buf );
@@ -949,17 +957,16 @@ BIO *OSSL_HTTP_transfer(const char *server, const char *port, const char *path,
949
957
950
958
static int redirection_ok (int n_redir , const char * old_url , const char * new_url )
951
959
{
952
- static const char https [] = "https:" ;
953
- int https_len = 6 ; /* strlen(https) */
960
+ size_t https_len = strlen (OSSL_HTTPS_NAME ":" );
954
961
955
962
if (n_redir >= HTTP_VERSION_MAX_REDIRECTIONS ) {
956
963
HTTPerr (0 , HTTP_R_TOO_MANY_REDIRECTIONS );
957
964
return 0 ;
958
965
}
959
966
if (* new_url == '/' ) /* redirection to same server => same protocol */
960
967
return 1 ;
961
- if (strncmp (old_url , https , https_len ) == 0 &&
962
- strncmp (new_url , https , https_len ) != 0 ) {
968
+ if (strncmp (old_url , OSSL_HTTPS_NAME ":" , https_len ) == 0 &&
969
+ strncmp (new_url , OSSL_HTTPS_NAME ":" , https_len ) != 0 ) {
963
970
HTTPerr (0 , HTTP_R_REDIRECTION_FROM_HTTPS_TO_HTTP );
964
971
return 0 ;
965
972
}
@@ -1122,8 +1129,8 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
1122
1129
const char * proxyuser , const char * proxypass ,
1123
1130
int timeout , BIO * bio_err , const char * prog )
1124
1131
{
1125
- # undef BUF_SIZE
1126
- # define BUF_SIZE (8 * 1024)
1132
+ #undef BUF_SIZE
1133
+ #define BUF_SIZE (8 * 1024)
1127
1134
char * mbuf = OPENSSL_malloc (BUF_SIZE );
1128
1135
char * mbufp ;
1129
1136
int read_len = 0 ;
@@ -1132,11 +1139,13 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
1132
1139
int rv ;
1133
1140
time_t max_time = timeout > 0 ? time (NULL ) + timeout : 0 ;
1134
1141
1135
- if (bio == NULL || server == NULL || port == NULL
1142
+ if (bio == NULL || server == NULL
1136
1143
|| (bio_err != NULL && prog == NULL )) {
1137
1144
HTTPerr (0 , ERR_R_PASSED_NULL_PARAMETER );
1138
1145
goto end ;
1139
1146
}
1147
+ if (port == NULL || * port == '\0' )
1148
+ port = OSSL_HTTPS_PORT ;
1140
1149
1141
1150
if (mbuf == NULL || fbio == NULL ) {
1142
1151
BIO_printf (bio_err /* may be NULL */ , "%s: out of memory" , prog );
@@ -1256,6 +1265,5 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
1256
1265
}
1257
1266
OPENSSL_free (mbuf );
1258
1267
return ret ;
1259
- # undef BUF_SIZE
1268
+ #undef BUF_SIZE
1260
1269
}
1261
-
0 commit comments