@@ -352,10 +352,11 @@ httpConnect(
352352
353353bool // O - `true` on success, `false` on failure
354354httpConnectAgain (http_t * http , // I - HTTP connection
355- int msec , // I - Timeout in milliseconds
356- int * cancel ) // I - Pointer to "cancel" variable
355+ int msec , // I - Timeout in milliseconds
356+ int * cancel ) // I - Pointer to "cancel" variable
357357{
358358 http_addrlist_t * addr ; // Connected address
359+ char * orig_creds ; // Original peer credentials
359360#ifdef DEBUG
360361 http_addrlist_t * current ; // Current address
361362 char temp [256 ]; // Temporary address string
@@ -371,6 +372,8 @@ httpConnectAgain(http_t *http, // I - HTTP connection
371372 return (false);
372373 }
373374
375+ orig_creds = httpCopyPeerCredentials (http );
376+
374377 if (http -> tls )
375378 {
376379 DEBUG_puts ("2httpConnectAgain: Shutting down SSL/TLS..." );
@@ -415,6 +418,8 @@ httpConnectAgain(http_t *http, // I - HTTP connection
415418
416419 DEBUG_printf ("1httpConnectAgain: httpAddrConnect failed: %s" , strerror (http -> error ));
417420
421+ free (orig_creds );
422+
418423 return (false);
419424 }
420425
@@ -434,16 +439,42 @@ httpConnectAgain(http_t *http, // I - HTTP connection
434439 httpAddrClose (NULL , http -> fd );
435440 http -> fd = -1 ;
436441
442+ free (orig_creds );
443+
437444 return (false);
438445 }
439446 }
440447 else if (http -> encryption == HTTP_ENCRYPTION_REQUIRED && !http -> tls_upgrade )
441448 {
442- return (http_tls_upgrade (http ));
449+ if (!http_tls_upgrade (http ))
450+ {
451+ free (orig_creds );
452+
453+ return (false);
454+ }
443455 }
444456
445457 DEBUG_printf ("1httpConnectAgain: Connected to %s:%d..." , httpAddrGetString (http -> hostaddr , temp , sizeof (temp )), httpAddrGetPort (http -> hostaddr ));
446458
459+ if (orig_creds )
460+ {
461+ char * new_creds = httpCopyPeerCredentials (http );
462+ // New peer credentials
463+
464+ if (!new_creds || (strcmp (orig_creds , new_creds ) && cupsGetCredentialsTrust (/*path*/ NULL , http -> hostname , new_creds , /*require_ca*/ true) != HTTP_TRUST_OK ))
465+ {
466+ // New and old credentials don't match and the new cert doesn't validate...
467+ _httpDisconnect (http );
468+
469+ free (orig_creds );
470+ free (new_creds );
471+
472+ return (false);
473+ }
474+ }
475+
476+ free (orig_creds );
477+
447478 return (true);
448479}
449480
0 commit comments