@@ -158,6 +158,15 @@ ngx_http_apisix_cleanup_cert_and_key(ngx_http_apisix_ctx_t *ctx)
158158 ctx -> upstream_pkey = NULL ;
159159 }
160160}
161+
162+ static void
163+ ngx_http_apisix_cleanup_trusted_store (ngx_http_apisix_ctx_t * ctx )
164+ {
165+ if (ctx -> upstream_trusted_store != NULL ) {
166+ X509_STORE_free (ctx -> upstream_trusted_store );
167+ ctx -> upstream_trusted_store = NULL ;
168+ }
169+ }
161170#endif
162171
163172
@@ -168,6 +177,7 @@ ngx_http_apisix_cleanup(void *data)
168177
169178#if (NGX_HTTP_SSL )
170179 ngx_http_apisix_cleanup_cert_and_key (ctx );
180+ ngx_http_apisix_cleanup_trusted_store (ctx );
171181#endif
172182}
173183
@@ -250,6 +260,41 @@ ngx_http_apisix_upstream_set_cert_and_key(ngx_http_request_t *r,
250260 return NGX_ERROR ;
251261}
252262
263+ ngx_int_t
264+ ngx_http_apisix_upstream_set_ssl_trusted_store (ngx_http_request_t * r , void * data )
265+ {
266+ X509_STORE * store = data ;
267+ ngx_http_apisix_ctx_t * ctx ;
268+
269+ if (store == NULL ) {
270+ return NGX_ERROR ;
271+ }
272+
273+ ctx = ngx_http_apisix_get_module_ctx (r );
274+
275+ if (ctx == NULL ) {
276+ return NGX_ERROR ;
277+ }
278+
279+ if (ctx -> upstream_trusted_store != NULL ) {
280+ ngx_http_apisix_cleanup_trusted_store (ctx );
281+ }
282+
283+ if (X509_STORE_up_ref (store ) == 0 ) {
284+ goto failed ;
285+ }
286+
287+ ctx -> upstream_trusted_store = store ;
288+
289+ return NGX_OK ;
290+
291+ failed :
292+
293+ ngx_http_apisix_flush_ssl_error ();
294+
295+ return NGX_ERROR ;
296+ }
297+
253298
254299void
255300ngx_http_apisix_set_upstream_ssl (ngx_http_request_t * r , ngx_connection_t * c )
@@ -258,6 +303,7 @@ ngx_http_apisix_set_upstream_ssl(ngx_http_request_t *r, ngx_connection_t *c)
258303 ngx_http_apisix_ctx_t * ctx ;
259304 STACK_OF (X509 ) * cert ;
260305 EVP_PKEY * pkey ;
306+ X509_STORE * store ;
261307 X509 * x509 ;
262308#ifdef OPENSSL_IS_BORINGSSL
263309 size_t i ;
@@ -275,8 +321,9 @@ ngx_http_apisix_set_upstream_ssl(ngx_http_request_t *r, ngx_connection_t *c)
275321 }
276322
277323 if (ctx -> upstream_cert != NULL ) {
278- cert = ctx -> upstream_cert ;
279- pkey = ctx -> upstream_pkey ;
324+ cert = ctx -> upstream_cert ;
325+ pkey = ctx -> upstream_pkey ;
326+ store = ctx -> upstream_trusted_store ;
280327
281328 if (sk_X509_num (cert ) < 1 ) {
282329 ngx_ssl_error (NGX_LOG_ERR , c -> log , 0 ,
@@ -317,6 +364,17 @@ ngx_http_apisix_set_upstream_ssl(ngx_http_request_t *r, ngx_connection_t *c)
317364 "SSL_use_PrivateKey() failed" );
318365 goto failed ;
319366 }
367+
368+ if (store != NULL ) {
369+ ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , c -> log , 0 ,
370+ "overriding upstream SSL trusted store" );
371+
372+ if (SSL_set1_verify_cert_store (sc , store ) == 0 ) {
373+ ngx_ssl_error (NGX_LOG_ALERT , c -> log , 0 ,
374+ "SSL_set1_verify_cert_store() failed" );
375+ goto failed ;
376+ }
377+ }
320378 }
321379
322380 return ;
@@ -325,6 +383,42 @@ ngx_http_apisix_set_upstream_ssl(ngx_http_request_t *r, ngx_connection_t *c)
325383
326384 ngx_http_apisix_flush_ssl_error ();
327385}
386+
387+
388+ int
389+ ngx_http_apisix_upstream_set_ssl_verify (ngx_http_request_t * r , int verify )
390+ {
391+ ngx_http_apisix_ctx_t * ctx ;
392+
393+ ctx = ngx_http_apisix_get_module_ctx (r );
394+
395+ if (ctx == NULL ) {
396+ return NGX_ERROR ;
397+ }
398+
399+ ctx -> upstream_ssl_verify_set = 1 ;
400+ ctx -> upstream_ssl_verify = verify ;
401+
402+ return NGX_OK ;
403+ }
404+
405+ ngx_flag_t
406+ ngx_http_apisix_get_upstream_ssl_verify (ngx_http_request_t * r , ngx_flag_t proxy_ssl_verify )
407+ {
408+ ngx_http_apisix_ctx_t * ctx ;
409+
410+ ctx = ngx_http_apisix_get_module_ctx (r );
411+
412+ if (ctx == NULL ) {
413+ return proxy_ssl_verify ;
414+ }
415+
416+ if (!ctx -> upstream_ssl_verify_set ) {
417+ return proxy_ssl_verify ;
418+ }
419+
420+ return ctx -> upstream_ssl_verify ;
421+ }
328422#endif
329423
330424
0 commit comments