You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ServerCertificateCustomValidationCallback has started returning a new value for SslPolicyErrors for the test code provided below.
Steps to Reproduce
Repro method. This used to pass and match behavior of HttpClientHandler and SocketsHttpHandler.
[TestMethod]publicasyncTaskRemoteCertificate_ForceFailure(){// Verifies that the default certificate validator is called for a normal request, and that it can be forced to failintcalls=0;ExceptioncallbackAssertFail=null;
#if __IOS__varhandler=newNSUrlSessionHandler();handler.ServerCertificateCustomValidationCallback=(sender,certificate,chain,sslPolicyErrors)=>
#else
var handler =newSystem.Net.Http.SocketsHttpHandler();handler.SslOptions.RemoteCertificateValidationCallback=(sender,certificate,chain,sslPolicyErrors)=>
#endif
{Interlocked.Increment(refcalls);try{Assert.AreEqual(SslPolicyErrors.None,sslPolicyErrors,"sslPolicyErrors");// Fails on iOS since 9.0.300. Now returns `RemoteCertificateChainErrors`}catch(Exceptionex){callbackAssertFail=ex;}returnfalse;};varclient=newHttpClient(handler);// Disable keep-alive and cache to force reconnection for each requestclient.DefaultRequestHeaders.ConnectionClose=true;client.DefaultRequestHeaders.CacheControl=newCacheControlHeaderValue{NoCache=true,NoStore=true};varresponseTask=client.GetAsync(newUri("https://sha256.badssl.com/"));// valid certificatetry{awaitresponseTask;}catch(HttpRequestException){/* expected */}// Expect our validator to be called onceAssert.AreEqual(1,calls,"numCalls");if(callbackAssertFail!=null){throwcallbackAssertFail;}}
I noticed the regression started on May 13, when 9.0.300 shipped, but from @mstefarov's dump above, that's also when the certificate was renewed. I rolled back to 9.0.202 and still seeing the issue :(
The phone browser doesn't reject this certificate though, so I wonder if there's something else that causes the existing code to not like this certificate?
The chain build in a previous comment has revocation checking enabled, by virtue of not turning it off.
Let's Encrypt stopped issuing certificates with registered OCSP endpoints, and the listed leaf certificate shows that it was issued after that point. So, partly, this is "Apple doesn't correctly process CRLs", with the "regression" inducing change having been external (the cert reissuance... as was already noted). (We specify kSecRevocationUseAnyAvailableMethod, but Apple's chain engine seems to decide that means "OCSP for the target cert, but CRL might be OK for CAs")
As far as I can tell NSUrlSessionHandler always has revocation checking enabled for custom callbacks, which is where this is coming from. For SocketsHttpHandler there are options regarding revocation. If NSUrlSessionHandler doesn't actually know what the underlying revocation check status is, but does know that the underlying system was willing to continue with the connection, then perhaps NSUrlSessionHandler wants to set chain.ChainPolicy.VerificationFlags |= X509ChainVerificationFlags.AllowEndRevocationUnknown; (which will make chain.Build() return true if that was the only problem) -- there are two other RevocationUnknown flags (intermediate CAs and the root), NSUrlSessionHandler may or may not want to set them, depending on what the underlying OS is doing.
@bartonjs thanks for your help diagnosing this! I've moved this back to dotnet/macios now.
So I guess there are a few options here:
Add an SslClientAuthenticationOptions SslOptions property to NSUrlSessionHandler, basically copying whatever SocketsHttpHandler does. There's quite a few properties in SslClientAuthenticationOptions though, and implementing them all doesn't look trivial, and might not even be possible to do in NSUrlSessionHandler due to API limitations in Apple's NSURLSessionHandler.
Add a CertificateChainPolicy property to NSUrlSessionHandler, and using that value here:
Just change the verification flag to |= AllowEndRevocationUnknown as @bartonjs suggests.
I'm leaning towards 2, because:
This is either a lot of work (which I'm hesitant to do when I'm not a security expert, nor do we have any on the team), or we ignore most of the settings in the SslClientAuthenticationOptions (which doesn't seem ideal either).
Fairly easy to implement, developers can opt-in as they see fit, and it's flexible.
Trivial to implement, but not very flexible, developer can't opt in or out of this new behavior.
@rolfbjarne I'd lean towards 3. My argument is it'll match behavior of other handlers, and thus you get consistent behavior across platforms you target. Possible with a combination with 2, so that you can opt-out of it, but defaults to matching SocketsHttpHandler behavior.
@rolfbjarne I'd lean towards 3. My argument is it'll match behavior of other handlers, and thus you get consistent behavior across platforms you target. Possible with a combination with 2, so that you can opt-out of it, but defaults to matching SocketsHttpHandler behavior.
I ended up doing 2 + 3, so the default behavior is consistent with other platforms + it's configurable, so you can get the old behavior if you want, or use other flags if Apple breaks something else.
Activity
dotMorten commentedon May 27, 2025
/CC @mandel-macaque
mstefarov commentedon May 27, 2025
The certificate from sha256.badssl.com
OpenSSL info dump
dotMorten commentedon May 27, 2025
I noticed the regression started on May 13, when 9.0.300 shipped, but from @mstefarov's dump above, that's also when the certificate was renewed. I rolled back to 9.0.202 and still seeing the issue :(
The phone browser doesn't reject this certificate though, so I wonder if there's something else that causes the existing code to not like this certificate?
rolfbjarne commentedon May 28, 2025
Moving to dotnet/runtime, because I've managed to distill this down to a console app: console-c460bb5.zip
Binlog:
msbuild.binlog.zip
Code:
prints:
Details
wfurt commentedon May 28, 2025
that like chain build change.
rolfbjarne commentedon Jul 4, 2025
Downloading sha256.badssl.com's certificate (https://crt.sh/?q=28b3bb3370523318551dbefb031d681022d9c0ae35004aad83783c1ad43e8884), and verifying using
security verify, shows that the certificate doesn't validate:However, passing the url to
security verifyresults in success:bartonjs commentedon Sep 5, 2025
The chain build in a previous comment has revocation checking enabled, by virtue of not turning it off.
Let's Encrypt stopped issuing certificates with registered OCSP endpoints, and the listed leaf certificate shows that it was issued after that point. So, partly, this is "Apple doesn't correctly process CRLs", with the "regression" inducing change having been external (the cert reissuance... as was already noted). (We specify
kSecRevocationUseAnyAvailableMethod, but Apple's chain engine seems to decide that means "OCSP for the target cert, but CRL might be OK for CAs")As far as I can tell NSUrlSessionHandler always has revocation checking enabled for custom callbacks, which is where this is coming from. For SocketsHttpHandler there are options regarding revocation. If NSUrlSessionHandler doesn't actually know what the underlying revocation check status is, but does know that the underlying system was willing to continue with the connection, then perhaps NSUrlSessionHandler wants to set
chain.ChainPolicy.VerificationFlags |= X509ChainVerificationFlags.AllowEndRevocationUnknown;(which will make chain.Build() return true if that was the only problem) -- there are two other RevocationUnknown flags (intermediate CAs and the root), NSUrlSessionHandler may or may not want to set them, depending on what the underlying OS is doing.bartonjs commentedon Sep 5, 2025
@rolfbjarne I don't seem to be able to transfer this back; but there's nothing that we can do for it in dotnet/runtime.
rolfbjarne commentedon Sep 8, 2025
@bartonjs thanks for your help diagnosing this! I've moved this back to dotnet/macios now.
So I guess there are a few options here:
SslClientAuthenticationOptions SslOptionsproperty to NSUrlSessionHandler, basically copying whateverSocketsHttpHandlerdoes. There's quite a few properties inSslClientAuthenticationOptionsthough, and implementing them all doesn't look trivial, and might not even be possible to do inNSUrlSessionHandlerdue to API limitations in Apple'sNSURLSessionHandler.CertificateChainPolicyproperty toNSUrlSessionHandler, and using that value here:macios/src/Foundation/NSUrlSessionHandler.cs
Lines 764 to 765 in ad507e3
|= AllowEndRevocationUnknownas @bartonjs suggests.I'm leaning towards 2, because:
SslClientAuthenticationOptions(which doesn't seem ideal either).[Foundation] Make it possible to customize the X509ChainPolicy when v…
rolfbjarne commentedon Sep 8, 2025
PR is up: #23767
dotMorten commentedon Sep 8, 2025
@rolfbjarne I'd lean towards 3. My argument is it'll match behavior of other handlers, and thus you get consistent behavior across platforms you target. Possible with a combination with 2, so that you can opt-out of it, but defaults to matching SocketsHttpHandler behavior.
rolfbjarne commentedon Sep 9, 2025
I ended up doing 2 + 3, so the default behavior is consistent with other platforms + it's configurable, so you can get the old behavior if you want, or use other flags if Apple breaks something else.
[Foundation] Make it possible to customize the X509ChainPolicy when v…
dotMorten commentedon Oct 21, 2025
I can confirm our tests just started passing for this on iOS too with the latest .NET 9 update! Thanks @rolfbjarne