@@ -51,9 +51,19 @@ internal static SslStreamCertificateContext Create(
51
51
{
52
52
if ( additionalCertificates != null )
53
53
{
54
- foreach ( X509Certificate cert in additionalCertificates )
54
+ chain . ChainPolicy . ExtraStore . AddRange ( additionalCertificates ) ;
55
+ }
56
+
57
+ if ( trust != null )
58
+ {
59
+ chain . ChainPolicy . TrustMode = X509ChainTrustMode . CustomRootTrust ;
60
+ if ( trust . _store != null )
61
+ {
62
+ chain . ChainPolicy . CustomTrustStore . AddRange ( trust . _store . Certificates ) ;
63
+ }
64
+ if ( trust . _trustList != null )
55
65
{
56
- chain . ChainPolicy . ExtraStore . Add ( cert ) ;
66
+ chain . ChainPolicy . CustomTrustStore . AddRange ( trust . _trustList ) ;
57
67
}
58
68
}
59
69
@@ -67,6 +77,20 @@ internal static SslStreamCertificateContext Create(
67
77
NetEventSource . Error ( null , $ "Failed to build chain for { target . Subject } ") ;
68
78
}
69
79
80
+ if ( ! chainStatus && ChainBuildNeedsTrustedRoot && additionalCertificates != null )
81
+ {
82
+ // Some platforms like Android may not be able to build the chain unless the chain root is trusted.
83
+ // We can try to rebuild the chain with making all extra certificates trused.
84
+ // We do not try to evaluate trust here, we jsut need to construct the chain so it should not matter.
85
+ chain . ChainPolicy . CustomTrustStore . AddRange ( additionalCertificates ) ;
86
+ chain . ChainPolicy . TrustMode = X509ChainTrustMode . CustomRootTrust ;
87
+ chainStatus = chain . Build ( target ) ;
88
+ if ( ! chainStatus && NetEventSource . Log . IsEnabled ( ) )
89
+ {
90
+ NetEventSource . Error ( null , $ "Failed to build chain for { target . Subject } while trusting additional certificates") ;
91
+ }
92
+ }
93
+
70
94
int count = chain . ChainElements . Count - 1 ;
71
95
72
96
// Some platforms (e.g. Android) can't ignore all verification and will return zero
0 commit comments