HttpsConnectionMiddlewareTests.cs 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System.Globalization;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Net.Security;
  7. using System.Security.Authentication;
  8. using System.Security.Cryptography.X509Certificates;
  9. using System.Text;
  10. using Microsoft.AspNetCore.Connections.Features;
  11. using Microsoft.AspNetCore.Hosting;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Http.Features;
  14. using Microsoft.AspNetCore.Server.Kestrel.Core;
  15. using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
  16. using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
  17. using Microsoft.AspNetCore.Server.Kestrel.Https;
  18. using Microsoft.AspNetCore.Server.Kestrel.Https.Internal;
  19. using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
  20. using Microsoft.AspNetCore.Testing;
  21. using Microsoft.Extensions.Configuration;
  22. using Microsoft.Extensions.DependencyInjection;
  23. using Microsoft.Extensions.Hosting;
  24. using Microsoft.Extensions.Logging;
  25. using Microsoft.Extensions.Metrics;
  26. using Moq;
  27. namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests;
  28. public class HttpsConnectionMiddlewareTests : LoggedTest
  29. {
  30. private static readonly X509Certificate2 _x509Certificate2 = TestResources.GetTestCertificate();
  31. private static readonly X509Certificate2 _x509Certificate2NoExt = TestResources.GetTestCertificate("no_extensions.pfx");
  32. private static KestrelServerOptions CreateServerOptions()
  33. {
  34. var env = new Mock<IHostEnvironment>();
  35. env.SetupGet(e => e.ContentRootPath).Returns(Directory.GetCurrentDirectory());
  36. var serverOptions = new KestrelServerOptions();
  37. serverOptions.ApplicationServices = new ServiceCollection()
  38. .AddLogging()
  39. .AddSingleton<IHttpsConfigurationService, HttpsConfigurationService>()
  40. .AddSingleton<HttpsConfigurationService.IInitializer, HttpsConfigurationService.Initializer>()
  41. .AddSingleton(env.Object)
  42. .AddSingleton(new KestrelMetrics(new TestMeterFactory()))
  43. .BuildServiceProvider();
  44. return serverOptions;
  45. }
  46. [Fact]
  47. public async Task CanReadAndWriteWithHttpsConnectionMiddleware()
  48. {
  49. void ConfigureListenOptions(ListenOptions listenOptions)
  50. {
  51. listenOptions.UseHttps(new HttpsConnectionAdapterOptions { ServerCertificate = _x509Certificate2 });
  52. };
  53. await using (var server = new TestServer(App, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  54. {
  55. var result = await server.HttpClientSlim.PostAsync($"https://localhost:{server.Port}/",
  56. new FormUrlEncodedContent(new[] {
  57. new KeyValuePair<string, string>("content", "Hello World?")
  58. }),
  59. validateCertificate: false);
  60. Assert.Equal("content=Hello+World%3F", result);
  61. }
  62. }
  63. [Fact]
  64. public async Task CanReadAndWriteWithHttpsConnectionMiddlewareWithPemCertificate()
  65. {
  66. var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string>
  67. {
  68. ["Certificates:Default:Path"] = Path.Combine("shared", "TestCertificates", "https-aspnet.crt"),
  69. ["Certificates:Default:KeyPath"] = Path.Combine("shared", "TestCertificates", "https-aspnet.key"),
  70. ["Certificates:Default:Password"] = "aspnetcore",
  71. }).Build();
  72. var options = CreateServerOptions();
  73. var loader = new KestrelConfigurationLoader(options, configuration, options.ApplicationServices.GetRequiredService<IHttpsConfigurationService>(), reloadOnChange: false);
  74. options.ConfigurationLoader = loader; // Since we're constructing it explicitly, we have to hook it up explicitly
  75. loader.Load();
  76. void ConfigureListenOptions(ListenOptions listenOptions)
  77. {
  78. listenOptions.KestrelServerOptions = options;
  79. listenOptions.UseHttps();
  80. };
  81. await using (var server = new TestServer(App, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  82. {
  83. var result = await server.HttpClientSlim.PostAsync($"https://localhost:{server.Port}/",
  84. new FormUrlEncodedContent(new[] {
  85. new KeyValuePair<string, string>("content", "Hello World?")
  86. }),
  87. validateCertificate: false);
  88. Assert.Equal("content=Hello+World%3F", result);
  89. }
  90. }
  91. [Fact]
  92. public async Task SslStreamIsAvailable()
  93. {
  94. void ConfigureListenOptions(ListenOptions listenOptions)
  95. {
  96. listenOptions.UseHttps(new HttpsConnectionAdapterOptions { ServerCertificate = _x509Certificate2 });
  97. };
  98. await using (var server = new TestServer(context =>
  99. {
  100. var feature = context.Features.Get<ISslStreamFeature>();
  101. Assert.NotNull(feature);
  102. Assert.NotNull(feature.SslStream);
  103. return context.Response.WriteAsync("hello world");
  104. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  105. {
  106. var result = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false);
  107. Assert.Equal("hello world", result);
  108. }
  109. }
  110. [Fact]
  111. public async Task HandshakeDetailsAreAvailable()
  112. {
  113. void ConfigureListenOptions(ListenOptions listenOptions)
  114. {
  115. listenOptions.UseHttps(new HttpsConnectionAdapterOptions { ServerCertificate = _x509Certificate2 });
  116. };
  117. await using (var server = new TestServer(context =>
  118. {
  119. var tlsFeature = context.Features.Get<ITlsHandshakeFeature>();
  120. Assert.NotNull(tlsFeature);
  121. Assert.True(tlsFeature.Protocol > SslProtocols.None, "Protocol");
  122. Assert.True(tlsFeature.NegotiatedCipherSuite >= TlsCipherSuite.TLS_NULL_WITH_NULL_NULL, "NegotiatedCipherSuite");
  123. Assert.True(tlsFeature.CipherAlgorithm > CipherAlgorithmType.Null, "Cipher");
  124. Assert.True(tlsFeature.CipherStrength > 0, "CipherStrength");
  125. Assert.True(tlsFeature.HashAlgorithm >= HashAlgorithmType.None, "HashAlgorithm"); // May be None on Linux.
  126. Assert.True(tlsFeature.HashStrength >= 0, "HashStrength"); // May be 0 for some algorithms
  127. Assert.True(tlsFeature.KeyExchangeAlgorithm >= ExchangeAlgorithmType.None, "KeyExchangeAlgorithm"); // Maybe None on Windows 7
  128. Assert.True(tlsFeature.KeyExchangeStrength >= 0, "KeyExchangeStrength"); // May be 0 on mac
  129. return context.Response.WriteAsync("hello world");
  130. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  131. {
  132. var result = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false);
  133. Assert.Equal("hello world", result);
  134. }
  135. }
  136. [Fact]
  137. public async Task HandshakeDetailsAreAvailableAfterAsyncCallback()
  138. {
  139. void ConfigureListenOptions(ListenOptions listenOptions)
  140. {
  141. listenOptions.UseHttps(async (stream, clientHelloInfo, state, cancellationToken) =>
  142. {
  143. await Task.Yield();
  144. return new SslServerAuthenticationOptions
  145. {
  146. ServerCertificate = _x509Certificate2,
  147. };
  148. }, state: null);
  149. }
  150. await using (var server = new TestServer(context =>
  151. {
  152. var tlsFeature = context.Features.Get<ITlsHandshakeFeature>();
  153. Assert.NotNull(tlsFeature);
  154. Assert.True(tlsFeature.Protocol > SslProtocols.None, "Protocol");
  155. Assert.True(tlsFeature.CipherAlgorithm > CipherAlgorithmType.Null, "Cipher");
  156. Assert.True(tlsFeature.CipherStrength > 0, "CipherStrength");
  157. Assert.True(tlsFeature.HashAlgorithm >= HashAlgorithmType.None, "HashAlgorithm"); // May be None on Linux.
  158. Assert.True(tlsFeature.HashStrength >= 0, "HashStrength"); // May be 0 for some algorithms
  159. Assert.True(tlsFeature.KeyExchangeAlgorithm >= ExchangeAlgorithmType.None, "KeyExchangeAlgorithm"); // Maybe None on Windows 7
  160. Assert.True(tlsFeature.KeyExchangeStrength >= 0, "KeyExchangeStrength"); // May be 0 on mac
  161. return context.Response.WriteAsync("hello world");
  162. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  163. {
  164. var result = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false);
  165. Assert.Equal("hello world", result);
  166. }
  167. }
  168. [Fact]
  169. public async Task RequireCertificateFailsWhenNoCertificate()
  170. {
  171. await using (var server = new TestServer(App, new TestServiceContext(LoggerFactory), listenOptions =>
  172. {
  173. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  174. {
  175. ServerCertificate = _x509Certificate2,
  176. ClientCertificateMode = ClientCertificateMode.RequireCertificate
  177. });
  178. }))
  179. {
  180. await Assert.ThrowsAnyAsync<Exception>(
  181. () => server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/"));
  182. }
  183. }
  184. [Fact]
  185. public async Task AllowCertificateContinuesWhenNoCertificate()
  186. {
  187. void ConfigureListenOptions(ListenOptions listenOptions)
  188. {
  189. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  190. {
  191. ServerCertificate = _x509Certificate2,
  192. ClientCertificateMode = ClientCertificateMode.AllowCertificate
  193. });
  194. }
  195. await using (var server = new TestServer(context =>
  196. {
  197. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  198. Assert.NotNull(tlsFeature);
  199. Assert.Null(tlsFeature.ClientCertificate);
  200. return context.Response.WriteAsync("hello world");
  201. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  202. {
  203. var result = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false);
  204. Assert.Equal("hello world", result);
  205. }
  206. }
  207. [Fact]
  208. public async Task AsyncCallbackSettingClientCertificateRequiredContinuesWhenNoCertificate()
  209. {
  210. void ConfigureListenOptions(ListenOptions listenOptions)
  211. {
  212. listenOptions.UseHttps((stream, clientHelloInfo, state, cancellationToken) =>
  213. new ValueTask<SslServerAuthenticationOptions>(new SslServerAuthenticationOptions
  214. {
  215. ServerCertificate = _x509Certificate2,
  216. ClientCertificateRequired = true,
  217. RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true,
  218. CertificateRevocationCheckMode = X509RevocationMode.NoCheck
  219. }), state: null);
  220. }
  221. await using (var server = new TestServer(context =>
  222. {
  223. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  224. Assert.NotNull(tlsFeature);
  225. Assert.Null(tlsFeature.ClientCertificate);
  226. return context.Response.WriteAsync("hello world");
  227. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  228. {
  229. var result = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false);
  230. Assert.Equal("hello world", result);
  231. }
  232. }
  233. [Fact]
  234. public void ThrowsWhenNoServerCertificateIsProvided()
  235. {
  236. Assert.Throws<ArgumentException>(() => CreateMiddleware(new HttpsConnectionAdapterOptions()));
  237. }
  238. [Fact]
  239. public async Task UsesProvidedServerCertificate()
  240. {
  241. void ConfigureListenOptions(ListenOptions listenOptions)
  242. {
  243. listenOptions.UseHttps(new HttpsConnectionAdapterOptions { ServerCertificate = _x509Certificate2 });
  244. };
  245. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  246. {
  247. using (var connection = server.CreateConnection())
  248. {
  249. var stream = OpenSslStream(connection.Stream);
  250. await stream.AuthenticateAsClientAsync("localhost");
  251. Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2));
  252. }
  253. }
  254. }
  255. [Fact]
  256. public async Task UsesProvidedServerCertificateSelector()
  257. {
  258. var selectorCalled = 0;
  259. void ConfigureListenOptions(ListenOptions listenOptions)
  260. {
  261. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  262. {
  263. ServerCertificateSelector = (connection, name) =>
  264. {
  265. Assert.NotNull(connection);
  266. Assert.NotNull(connection.Features.Get<SslStream>());
  267. Assert.Equal("localhost", name);
  268. selectorCalled++;
  269. return _x509Certificate2;
  270. }
  271. });
  272. }
  273. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  274. {
  275. using (var connection = server.CreateConnection())
  276. {
  277. var stream = OpenSslStream(connection.Stream);
  278. await stream.AuthenticateAsClientAsync("localhost");
  279. Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2));
  280. Assert.Equal(1, selectorCalled);
  281. }
  282. }
  283. }
  284. [Fact]
  285. public async Task UsesProvidedAsyncCallback()
  286. {
  287. var selectorCalled = 0;
  288. void ConfigureListenOptions(ListenOptions listenOptions)
  289. {
  290. listenOptions.UseHttps(async (stream, clientHelloInfo, state, cancellationToken) =>
  291. {
  292. await Task.Yield();
  293. Assert.NotNull(stream);
  294. Assert.Equal("localhost", clientHelloInfo.ServerName);
  295. selectorCalled++;
  296. return new SslServerAuthenticationOptions
  297. {
  298. ServerCertificate = _x509Certificate2
  299. };
  300. }, state: null);
  301. }
  302. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  303. {
  304. using (var connection = server.CreateConnection())
  305. {
  306. var stream = OpenSslStream(connection.Stream);
  307. await stream.AuthenticateAsClientAsync("localhost");
  308. Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2));
  309. Assert.Equal(1, selectorCalled);
  310. }
  311. }
  312. }
  313. [Fact]
  314. public async Task UsesProvidedServerCertificateSelectorEachTime()
  315. {
  316. var selectorCalled = 0;
  317. void ConfigureListenOptions(ListenOptions listenOptions)
  318. {
  319. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  320. {
  321. ServerCertificateSelector = (connection, name) =>
  322. {
  323. Assert.NotNull(connection);
  324. Assert.NotNull(connection.Features.Get<SslStream>());
  325. Assert.Equal("localhost", name);
  326. selectorCalled++;
  327. if (selectorCalled == 1)
  328. {
  329. return _x509Certificate2;
  330. }
  331. return _x509Certificate2NoExt;
  332. }
  333. });
  334. }
  335. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  336. {
  337. using (var connection = server.CreateConnection())
  338. {
  339. var stream = OpenSslStream(connection.Stream);
  340. await stream.AuthenticateAsClientAsync("localhost");
  341. Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2));
  342. Assert.Equal(1, selectorCalled);
  343. }
  344. using (var connection = server.CreateConnection())
  345. {
  346. var stream = OpenSslStream(connection.Stream);
  347. await stream.AuthenticateAsClientAsync("localhost");
  348. Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2NoExt));
  349. Assert.Equal(2, selectorCalled);
  350. }
  351. }
  352. }
  353. [Fact]
  354. public async Task UsesProvidedServerCertificateSelectorValidatesEkus()
  355. {
  356. var selectorCalled = 0;
  357. void ConfigureListenOptions(ListenOptions listenOptions)
  358. {
  359. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  360. {
  361. ServerCertificateSelector = (features, name) =>
  362. {
  363. selectorCalled++;
  364. return TestResources.GetTestCertificate("eku.code_signing.pfx");
  365. }
  366. });
  367. }
  368. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  369. {
  370. using (var connection = server.CreateConnection())
  371. {
  372. var stream = OpenSslStream(connection.Stream);
  373. #pragma warning disable SYSLIB0039 // TLS 1.0 and 1.1 are obsolete
  374. await Assert.ThrowsAsync<IOException>(() =>
  375. stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls12 | SslProtocols.Tls11, false));
  376. #pragma warning restore SYSLIB0039
  377. Assert.Equal(1, selectorCalled);
  378. }
  379. }
  380. }
  381. [Fact]
  382. public async Task UsesProvidedServerCertificateSelectorOverridesServerCertificate()
  383. {
  384. var selectorCalled = 0;
  385. void ConfigureListenOptions(ListenOptions listenOptions)
  386. {
  387. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  388. {
  389. ServerCertificate = _x509Certificate2NoExt,
  390. ServerCertificateSelector = (connection, name) =>
  391. {
  392. Assert.NotNull(connection);
  393. Assert.NotNull(connection.Features.Get<SslStream>());
  394. Assert.Equal("localhost", name);
  395. selectorCalled++;
  396. return _x509Certificate2;
  397. }
  398. });
  399. }
  400. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  401. {
  402. using (var connection = server.CreateConnection())
  403. {
  404. var stream = OpenSslStream(connection.Stream);
  405. await stream.AuthenticateAsClientAsync("localhost");
  406. Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2));
  407. Assert.Equal(1, selectorCalled);
  408. }
  409. }
  410. }
  411. [Fact]
  412. public async Task UsesProvidedServerCertificateSelectorFailsIfYouReturnNull()
  413. {
  414. var selectorCalled = 0;
  415. void ConfigureListenOptions(ListenOptions listenOptions)
  416. {
  417. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  418. {
  419. ServerCertificateSelector = (features, name) =>
  420. {
  421. selectorCalled++;
  422. return null;
  423. }
  424. });
  425. }
  426. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  427. {
  428. using (var connection = server.CreateConnection())
  429. {
  430. var stream = OpenSslStream(connection.Stream);
  431. #pragma warning disable SYSLIB0039 // TLS 1.0 and 1.1 are obsolete
  432. await Assert.ThrowsAsync<IOException>(() =>
  433. stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls12 | SslProtocols.Tls11, false));
  434. #pragma warning restore SYSLIB0039
  435. Assert.Equal(1, selectorCalled);
  436. }
  437. }
  438. }
  439. [Theory]
  440. [InlineData(HttpProtocols.Http1)]
  441. [InlineData(HttpProtocols.Http1AndHttp2)] // Make sure Http/1.1 doesn't regress with Http/2 enabled.
  442. public async Task CertificatePassedToHttpContext(HttpProtocols httpProtocols)
  443. {
  444. void ConfigureListenOptions(ListenOptions listenOptions)
  445. {
  446. listenOptions.Protocols = httpProtocols;
  447. listenOptions.UseHttps(options =>
  448. {
  449. options.ServerCertificate = _x509Certificate2;
  450. options.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
  451. options.AllowAnyClientCertificate();
  452. });
  453. }
  454. await using (var server = new TestServer(context =>
  455. {
  456. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  457. Assert.NotNull(tlsFeature);
  458. Assert.NotNull(tlsFeature.ClientCertificate);
  459. Assert.NotNull(context.Connection.ClientCertificate);
  460. return context.Response.WriteAsync("hello world");
  461. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  462. {
  463. using (var connection = server.CreateConnection())
  464. {
  465. // SslStream is used to ensure the certificate is actually passed to the server
  466. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  467. // of the certificate authorities sent by the server in the SSL handshake.
  468. // Use a random host name to avoid the TLS session resumption cache.
  469. var stream = OpenSslStreamWithCert(connection.Stream);
  470. await stream.AuthenticateAsClientAsync(Guid.NewGuid().ToString());
  471. await AssertConnectionResult(stream, true);
  472. }
  473. }
  474. }
  475. [Fact]
  476. public async Task RenegotiateForClientCertificateOnHttp1DisabledByDefault()
  477. {
  478. void ConfigureListenOptions(ListenOptions listenOptions)
  479. {
  480. listenOptions.Protocols = HttpProtocols.Http1;
  481. listenOptions.UseHttps(options =>
  482. {
  483. options.ServerCertificate = _x509Certificate2;
  484. options.ClientCertificateMode = ClientCertificateMode.NoCertificate;
  485. options.AllowAnyClientCertificate();
  486. });
  487. }
  488. await using var server = new TestServer(async context =>
  489. {
  490. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  491. Assert.NotNull(tlsFeature);
  492. Assert.Null(tlsFeature.ClientCertificate);
  493. Assert.Null(context.Connection.ClientCertificate);
  494. var clientCert = await context.Connection.GetClientCertificateAsync();
  495. Assert.Null(clientCert);
  496. Assert.Null(tlsFeature.ClientCertificate);
  497. Assert.Null(context.Connection.ClientCertificate);
  498. await context.Response.WriteAsync("hello world");
  499. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  500. using var connection = server.CreateConnection();
  501. // SslStream is used to ensure the certificate is actually passed to the server
  502. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  503. // of the certificate authorities sent by the server in the SSL handshake.
  504. // Use a random host name to avoid the TLS session resumption cache.
  505. var stream = OpenSslStreamWithCert(connection.Stream);
  506. await stream.AuthenticateAsClientAsync(Guid.NewGuid().ToString());
  507. await AssertConnectionResult(stream, true);
  508. }
  509. [ConditionalTheory]
  510. [InlineData(HttpProtocols.Http1)]
  511. [InlineData(HttpProtocols.Http1AndHttp2)] // Make sure turning on Http/2 doesn't regress HTTP/1
  512. [TlsAlpnSupported]
  513. [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Missing platform support.")]
  514. [SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/33566#issuecomment-892031659", Queues = HelixConstants.RedhatAmd64)] // Outdated OpenSSL client
  515. public async Task CanRenegotiateForClientCertificate(HttpProtocols httpProtocols)
  516. {
  517. void ConfigureListenOptions(ListenOptions listenOptions)
  518. {
  519. listenOptions.Protocols = httpProtocols;
  520. listenOptions.UseHttps(options =>
  521. {
  522. options.ServerCertificate = _x509Certificate2;
  523. options.ClientCertificateMode = ClientCertificateMode.DelayCertificate;
  524. options.AllowAnyClientCertificate();
  525. });
  526. }
  527. await using var server = new TestServer(async context =>
  528. {
  529. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  530. Assert.NotNull(tlsFeature);
  531. Assert.Null(tlsFeature.ClientCertificate);
  532. Assert.Null(context.Connection.ClientCertificate);
  533. var clientCert = await context.Connection.GetClientCertificateAsync();
  534. Assert.NotNull(clientCert);
  535. Assert.NotNull(tlsFeature.ClientCertificate);
  536. Assert.NotNull(context.Connection.ClientCertificate);
  537. await context.Response.WriteAsync("hello world");
  538. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  539. using var connection = server.CreateConnection();
  540. // SslStream is used to ensure the certificate is actually passed to the server
  541. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  542. // of the certificate authorities sent by the server in the SSL handshake.
  543. // Use a random host name to avoid the TLS session resumption cache.
  544. var stream = OpenSslStreamWithCert(connection.Stream);
  545. await stream.AuthenticateAsClientAsync(Guid.NewGuid().ToString());
  546. await AssertConnectionResult(stream, true);
  547. }
  548. [ConditionalFact]
  549. [TlsAlpnSupported]
  550. [OSSkipCondition(OperatingSystems.Windows | OperatingSystems.Linux, SkipReason = "MacOS only test.")]
  551. public async Task CanRenegotiateForClientCertificate_MacOS_PlatformNotSupportedException()
  552. {
  553. void ConfigureListenOptions(ListenOptions listenOptions)
  554. {
  555. listenOptions.Protocols = HttpProtocols.Http1;
  556. listenOptions.UseHttps(options =>
  557. {
  558. options.ServerCertificate = _x509Certificate2;
  559. options.ClientCertificateMode = ClientCertificateMode.DelayCertificate;
  560. options.AllowAnyClientCertificate();
  561. });
  562. }
  563. await using var server = new TestServer(async context =>
  564. {
  565. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  566. Assert.NotNull(tlsFeature);
  567. Assert.Null(tlsFeature.ClientCertificate);
  568. Assert.Null(context.Connection.ClientCertificate);
  569. await Assert.ThrowsAsync<PlatformNotSupportedException>(() => context.Connection.GetClientCertificateAsync());
  570. var lifetimeNotificationFeature = context.Features.Get<IConnectionLifetimeNotificationFeature>();
  571. Assert.False(
  572. lifetimeNotificationFeature.ConnectionClosedRequested.IsCancellationRequested,
  573. "GetClientCertificateAsync shouldn't cause the connection to be closed.");
  574. await context.Response.WriteAsync("hello world");
  575. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  576. using var connection = server.CreateConnection();
  577. // SslStream is used to ensure the certificate is actually passed to the server
  578. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  579. // of the certificate authorities sent by the server in the SSL handshake.
  580. // Use a random host name to avoid the TLS session resumption cache.
  581. var stream = OpenSslStreamWithCert(connection.Stream);
  582. await stream.AuthenticateAsClientAsync(Guid.NewGuid().ToString());
  583. await AssertConnectionResult(stream, true);
  584. }
  585. [Fact]
  586. public async Task Renegotiate_ServerOptionsSelectionCallback_NotSupported()
  587. {
  588. void ConfigureListenOptions(ListenOptions listenOptions)
  589. {
  590. listenOptions.UseHttps((SslStream stream, SslClientHelloInfo clientHelloInfo, object state, CancellationToken cancellationToken) =>
  591. {
  592. return ValueTask.FromResult(new SslServerAuthenticationOptions()
  593. {
  594. ServerCertificate = _x509Certificate2,
  595. ClientCertificateRequired = false,
  596. RemoteCertificateValidationCallback = (_, _, _, _) => true,
  597. });
  598. }, state: null);
  599. }
  600. await using var server = new TestServer(async context =>
  601. {
  602. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  603. Assert.NotNull(tlsFeature);
  604. Assert.Null(tlsFeature.ClientCertificate);
  605. Assert.Null(context.Connection.ClientCertificate);
  606. var clientCert = await context.Connection.GetClientCertificateAsync();
  607. Assert.Null(clientCert);
  608. Assert.Null(tlsFeature.ClientCertificate);
  609. Assert.Null(context.Connection.ClientCertificate);
  610. await context.Response.WriteAsync("hello world");
  611. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  612. using var connection = server.CreateConnection();
  613. // SslStream is used to ensure the certificate is actually passed to the server
  614. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  615. // of the certificate authorities sent by the server in the SSL handshake.
  616. // Use a random host name to avoid the TLS session resumption cache.
  617. var stream = OpenSslStreamWithCert(connection.Stream);
  618. await stream.AuthenticateAsClientAsync(Guid.NewGuid().ToString());
  619. await AssertConnectionResult(stream, true);
  620. }
  621. [ConditionalFact]
  622. [TlsAlpnSupported]
  623. [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Missing platform support.")]
  624. [SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/33566#issuecomment-892031659", Queues = HelixConstants.RedhatAmd64)] // Outdated OpenSSL client
  625. public async Task CanRenegotiateForTlsCallbackOptions()
  626. {
  627. void ConfigureListenOptions(ListenOptions listenOptions)
  628. {
  629. listenOptions.UseHttps(new TlsHandshakeCallbackOptions()
  630. {
  631. OnConnection = context =>
  632. {
  633. context.AllowDelayedClientCertificateNegotation = true;
  634. return ValueTask.FromResult(new SslServerAuthenticationOptions()
  635. {
  636. ServerCertificate = _x509Certificate2,
  637. ClientCertificateRequired = false,
  638. RemoteCertificateValidationCallback = (_, _, _, _) => true,
  639. });
  640. }
  641. });
  642. }
  643. await using var server = new TestServer(async context =>
  644. {
  645. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  646. Assert.NotNull(tlsFeature);
  647. Assert.Null(tlsFeature.ClientCertificate);
  648. Assert.Null(context.Connection.ClientCertificate);
  649. var clientCert = await context.Connection.GetClientCertificateAsync();
  650. Assert.NotNull(clientCert);
  651. Assert.NotNull(tlsFeature.ClientCertificate);
  652. Assert.NotNull(context.Connection.ClientCertificate);
  653. await context.Response.WriteAsync("hello world");
  654. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  655. using var connection = server.CreateConnection();
  656. // SslStream is used to ensure the certificate is actually passed to the server
  657. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  658. // of the certificate authorities sent by the server in the SSL handshake.
  659. // Use a random host name to avoid the TLS session resumption cache.
  660. var stream = OpenSslStreamWithCert(connection.Stream);
  661. await stream.AuthenticateAsClientAsync(Guid.NewGuid().ToString());
  662. await AssertConnectionResult(stream, true);
  663. }
  664. [ConditionalFact]
  665. [TlsAlpnSupported]
  666. [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Missing platform support.")]
  667. [SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/33566#issuecomment-892031659", Queues = HelixConstants.RedhatAmd64)] // Outdated OpenSSL client
  668. public async Task CanRenegotiateForClientCertificateOnHttp1CanReturnNoCert()
  669. {
  670. void ConfigureListenOptions(ListenOptions listenOptions)
  671. {
  672. listenOptions.Protocols = HttpProtocols.Http1;
  673. listenOptions.UseHttps(options =>
  674. {
  675. options.ServerCertificate = _x509Certificate2;
  676. options.ClientCertificateMode = ClientCertificateMode.DelayCertificate;
  677. options.AllowAnyClientCertificate();
  678. });
  679. }
  680. await using var server = new TestServer(async context =>
  681. {
  682. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  683. Assert.NotNull(tlsFeature);
  684. Assert.Null(tlsFeature.ClientCertificate);
  685. Assert.Null(context.Connection.ClientCertificate);
  686. var clientCert = await context.Connection.GetClientCertificateAsync();
  687. Assert.Null(clientCert);
  688. Assert.Null(tlsFeature.ClientCertificate);
  689. Assert.Null(context.Connection.ClientCertificate);
  690. await context.Response.WriteAsync("hello world");
  691. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  692. using var connection = server.CreateConnection();
  693. // SslStream is used to ensure the certificate is actually passed to the server
  694. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  695. // of the certificate authorities sent by the server in the SSL handshake.
  696. // Use a random host name to avoid the TLS session resumption cache.
  697. var stream = new SslStream(connection.Stream);
  698. var clientOptions = new SslClientAuthenticationOptions()
  699. {
  700. TargetHost = Guid.NewGuid().ToString(),
  701. #pragma warning disable SYSLIB0039 // TLS 1.0 and 1.1 are obsolete
  702. EnabledSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12,
  703. #pragma warning restore SYSLIB0039
  704. };
  705. clientOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
  706. await stream.AuthenticateAsClientAsync(clientOptions);
  707. await AssertConnectionResult(stream, true);
  708. }
  709. [ConditionalFact]
  710. [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Fails on OSX.")]
  711. public async Task ServerCertificateChainInExtraStore()
  712. {
  713. var streams = new List<SslStream>();
  714. CertHelper.CleanupCertificates(nameof(ServerCertificateChainInExtraStore));
  715. (var clientCertificate, var clientChain) = CertHelper.GenerateCertificates(nameof(ServerCertificateChainInExtraStore), longChain: true, serverCertificate: false);
  716. using (var store = new X509Store(StoreName.CertificateAuthority, StoreLocation.CurrentUser))
  717. {
  718. // add chain certificate so we can construct chain since there is no way how to pass intermediates directly.
  719. store.Open(OpenFlags.ReadWrite);
  720. store.AddRange(clientChain);
  721. store.Close();
  722. }
  723. using (var chain = new X509Chain())
  724. {
  725. chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
  726. chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
  727. chain.ChainPolicy.DisableCertificateDownloads = true;
  728. var chainStatus = chain.Build(clientCertificate);
  729. }
  730. void ConfigureListenOptions(ListenOptions listenOptions)
  731. {
  732. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  733. {
  734. ServerCertificate = _x509Certificate2,
  735. ServerCertificateChain = clientChain,
  736. OnAuthenticate = (con, so) =>
  737. {
  738. so.ClientCertificateRequired = true;
  739. so.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
  740. {
  741. Assert.NotEmpty(chain.ChainPolicy.ExtraStore);
  742. Assert.Contains(clientChain[0], chain.ChainPolicy.ExtraStore);
  743. return true;
  744. };
  745. so.CertificateRevocationCheckMode = X509RevocationMode.NoCheck;
  746. }
  747. });
  748. }
  749. await using (var server = new TestServer(
  750. context => context.Response.WriteAsync("hello world"),
  751. new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  752. {
  753. using (var connection = server.CreateConnection())
  754. {
  755. var stream = OpenSslStreamWithCert(connection.Stream, clientCertificate);
  756. await stream.AuthenticateAsClientAsync("localhost");
  757. await AssertConnectionResult(stream, true);
  758. }
  759. }
  760. CertHelper.CleanupCertificates(nameof(ServerCertificateChainInExtraStore));
  761. clientCertificate.Dispose();
  762. var list = (System.Collections.IList)clientChain;
  763. for (var i = 0; i < list.Count; i++)
  764. {
  765. var c = (X509Certificate)list[i];
  766. c.Dispose();
  767. }
  768. foreach (var s in streams)
  769. {
  770. s.Dispose();
  771. }
  772. }
  773. [ConditionalFact]
  774. // TLS 1.2 and lower have to renegotiate the whole connection to get a client cert, and if that hits an error
  775. // then the connection is aborted.
  776. [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Missing platform support.")]
  777. [TlsAlpnSupported]
  778. [SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/33566#issuecomment-892031659", Queues = HelixConstants.RedhatAmd64)] // Outdated OpenSSL client
  779. public async Task RenegotiateForClientCertificateOnPostWithoutBufferingThrows()
  780. {
  781. void ConfigureListenOptions(ListenOptions listenOptions)
  782. {
  783. listenOptions.Protocols = HttpProtocols.Http1;
  784. listenOptions.UseHttps(options =>
  785. {
  786. options.ServerCertificate = _x509Certificate2;
  787. options.ClientCertificateMode = ClientCertificateMode.DelayCertificate;
  788. options.AllowAnyClientCertificate();
  789. });
  790. }
  791. // Under 4kb can sometimes work because it fits into Kestrel's header parsing buffer.
  792. var expectedBody = new string('a', 1024 * 4);
  793. await using var server = new TestServer(async context =>
  794. {
  795. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  796. Assert.NotNull(tlsFeature);
  797. Assert.Null(tlsFeature.ClientCertificate);
  798. Assert.Null(context.Connection.ClientCertificate);
  799. var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => context.Connection.GetClientCertificateAsync());
  800. Assert.Equal("Client stream needs to be drained before renegotiation.", ex.Message);
  801. Assert.Null(tlsFeature.ClientCertificate);
  802. Assert.Null(context.Connection.ClientCertificate);
  803. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  804. using var connection = server.CreateConnection();
  805. // SslStream is used to ensure the certificate is actually passed to the server
  806. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  807. // of the certificate authorities sent by the server in the SSL handshake.
  808. // Use a random host name to avoid the TLS session resumption cache.
  809. var stream = OpenSslStreamWithCert(connection.Stream);
  810. await stream.AuthenticateAsClientAsync(Guid.NewGuid().ToString());
  811. await AssertConnectionResult(stream, true, expectedBody);
  812. }
  813. [ConditionalFact]
  814. [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10)] // HTTP/2 requires Win10
  815. [TlsAlpnSupported]
  816. public async Task ServerOptionsSelectionCallback_SetsALPN()
  817. {
  818. static void ConfigureListenOptions(ListenOptions listenOptions)
  819. {
  820. listenOptions.UseHttps((_, _, _, _) =>
  821. ValueTask.FromResult(new SslServerAuthenticationOptions()
  822. {
  823. ServerCertificate = _x509Certificate2,
  824. }), state: null);
  825. }
  826. await using var server = new TestServer(context => Task.CompletedTask,
  827. new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  828. using var connection = server.CreateConnection();
  829. var stream = OpenSslStream(connection.Stream);
  830. await stream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions()
  831. {
  832. // Use a random host name to avoid the TLS session resumption cache.
  833. TargetHost = Guid.NewGuid().ToString(),
  834. ApplicationProtocols = new() { SslApplicationProtocol.Http2, SslApplicationProtocol.Http11, },
  835. });
  836. Assert.Equal(SslApplicationProtocol.Http2, stream.NegotiatedApplicationProtocol);
  837. }
  838. [ConditionalFact]
  839. [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10)] // HTTP/2 requires Win10
  840. [TlsAlpnSupported]
  841. public async Task TlsHandshakeCallbackOptionsOverload_SetsALPN()
  842. {
  843. static void ConfigureListenOptions(ListenOptions listenOptions)
  844. {
  845. listenOptions.UseHttps(new TlsHandshakeCallbackOptions()
  846. {
  847. OnConnection = context =>
  848. {
  849. return ValueTask.FromResult(new SslServerAuthenticationOptions()
  850. {
  851. ServerCertificate = _x509Certificate2,
  852. });
  853. }
  854. });
  855. }
  856. await using var server = new TestServer(context => Task.CompletedTask,
  857. new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  858. using var connection = server.CreateConnection();
  859. var stream = OpenSslStream(connection.Stream);
  860. await stream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions()
  861. {
  862. // Use a random host name to avoid the TLS session resumption cache.
  863. TargetHost = Guid.NewGuid().ToString(),
  864. ApplicationProtocols = new() { SslApplicationProtocol.Http2, SslApplicationProtocol.Http11, },
  865. });
  866. Assert.Equal(SslApplicationProtocol.Http2, stream.NegotiatedApplicationProtocol);
  867. }
  868. [ConditionalFact]
  869. [TlsAlpnSupported]
  870. public async Task TlsHandshakeCallbackOptionsOverload_EmptyAlpnList_DisablesAlpn()
  871. {
  872. static void ConfigureListenOptions(ListenOptions listenOptions)
  873. {
  874. listenOptions.UseHttps(new TlsHandshakeCallbackOptions()
  875. {
  876. OnConnection = context =>
  877. {
  878. return ValueTask.FromResult(new SslServerAuthenticationOptions()
  879. {
  880. ServerCertificate = _x509Certificate2,
  881. ApplicationProtocols = new(),
  882. });
  883. }
  884. });
  885. }
  886. await using var server = new TestServer(context => Task.CompletedTask,
  887. new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  888. using var connection = server.CreateConnection();
  889. var stream = OpenSslStream(connection.Stream);
  890. await stream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions()
  891. {
  892. // Use a random host name to avoid the TLS session resumption cache.
  893. TargetHost = Guid.NewGuid().ToString(),
  894. ApplicationProtocols = new() { SslApplicationProtocol.Http2, SslApplicationProtocol.Http11, },
  895. });
  896. Assert.Equal(default, stream.NegotiatedApplicationProtocol);
  897. }
  898. [ConditionalFact]
  899. [TlsAlpnSupported]
  900. [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Missing platform support.")]
  901. [SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/33566#issuecomment-892031659", Queues = HelixConstants.RedhatAmd64)] // Outdated OpenSSL client
  902. public async Task CanRenegotiateForClientCertificateOnPostIfDrained()
  903. {
  904. void ConfigureListenOptions(ListenOptions listenOptions)
  905. {
  906. listenOptions.Protocols = HttpProtocols.Http1;
  907. listenOptions.UseHttps(options =>
  908. {
  909. options.ServerCertificate = _x509Certificate2;
  910. options.ClientCertificateMode = ClientCertificateMode.DelayCertificate;
  911. options.AllowAnyClientCertificate();
  912. });
  913. }
  914. var expectedBody = new string('a', 1024 * 4);
  915. await using var server = new TestServer(async context =>
  916. {
  917. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  918. Assert.NotNull(tlsFeature);
  919. Assert.Null(tlsFeature.ClientCertificate);
  920. Assert.Null(context.Connection.ClientCertificate);
  921. // Read the body before requesting the client cert
  922. var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
  923. Assert.Equal(expectedBody, body);
  924. var clientCert = await context.Connection.GetClientCertificateAsync();
  925. Assert.NotNull(clientCert);
  926. Assert.NotNull(tlsFeature.ClientCertificate);
  927. Assert.NotNull(context.Connection.ClientCertificate);
  928. await context.Response.WriteAsync("hello world");
  929. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  930. using var connection = server.CreateConnection();
  931. // SslStream is used to ensure the certificate is actually passed to the server
  932. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  933. // of the certificate authorities sent by the server in the SSL handshake.
  934. // Use a random host name to avoid the TLS session resumption cache.
  935. var stream = OpenSslStreamWithCert(connection.Stream);
  936. await stream.AuthenticateAsClientAsync(Guid.NewGuid().ToString());
  937. await AssertConnectionResult(stream, true, expectedBody);
  938. }
  939. [ConditionalFact]
  940. [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Missing platform support.")]
  941. [TlsAlpnSupported]
  942. [SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/33566#issuecomment-892031659", Queues = HelixConstants.RedhatAmd64)] // Outdated OpenSSL client
  943. public async Task RenegotationFailureCausesConnectionClose()
  944. {
  945. void ConfigureListenOptions(ListenOptions listenOptions)
  946. {
  947. listenOptions.Protocols = HttpProtocols.Http1;
  948. listenOptions.UseHttps(options =>
  949. {
  950. options.ServerCertificate = _x509Certificate2;
  951. options.ClientCertificateMode = ClientCertificateMode.DelayCertificate;
  952. options.AllowAnyClientCertificate();
  953. });
  954. }
  955. var expectedBody = new string('a', 1024 * 4);
  956. await using var server = new TestServer(async context =>
  957. {
  958. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  959. Assert.NotNull(tlsFeature);
  960. Assert.Null(tlsFeature.ClientCertificate);
  961. Assert.Null(context.Connection.ClientCertificate);
  962. // Request the client cert while there's still body data in the buffers
  963. var ioe = await Assert.ThrowsAsync<InvalidOperationException>(() => context.Connection.GetClientCertificateAsync());
  964. Assert.Equal("Client stream needs to be drained before renegotiation.", ioe.Message);
  965. context.Response.ContentLength = 11;
  966. await context.Response.WriteAsync("hello world");
  967. }, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  968. using var connection = server.CreateConnection();
  969. // SslStream is used to ensure the certificate is actually passed to the server
  970. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  971. // of the certificate authorities sent by the server in the SSL handshake.
  972. // Use a random host name to avoid the TLS session resumption cache.
  973. var stream = OpenSslStreamWithCert(connection.Stream);
  974. await stream.AuthenticateAsClientAsync(Guid.NewGuid().ToString());
  975. var request = Encoding.UTF8.GetBytes($"POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: {expectedBody.Length}\r\n\r\n{expectedBody}");
  976. await stream.WriteAsync(request, 0, request.Length).DefaultTimeout();
  977. var reader = new StreamReader(stream);
  978. Assert.Equal("HTTP/1.1 200 OK", await reader.ReadLineAsync().DefaultTimeout());
  979. Assert.Equal("Content-Length: 11", await reader.ReadLineAsync().DefaultTimeout());
  980. Assert.Equal("Connection: close", await reader.ReadLineAsync().DefaultTimeout());
  981. Assert.StartsWith("Date: ", await reader.ReadLineAsync().DefaultTimeout());
  982. Assert.Equal("", await reader.ReadLineAsync().DefaultTimeout());
  983. Assert.Equal("hello world", await reader.ReadLineAsync().DefaultTimeout());
  984. Assert.Null(await reader.ReadLineAsync().DefaultTimeout());
  985. }
  986. [Fact]
  987. public async Task HttpsSchemePassedToRequestFeature()
  988. {
  989. void ConfigureListenOptions(ListenOptions listenOptions)
  990. {
  991. listenOptions.UseHttps(new HttpsConnectionAdapterOptions { ServerCertificate = _x509Certificate2 });
  992. }
  993. await using (var server = new TestServer(context => context.Response.WriteAsync(context.Request.Scheme), new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  994. {
  995. var result = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false);
  996. Assert.Equal("https", result);
  997. }
  998. }
  999. [Fact]
  1000. public async Task Tls10CanBeDisabled()
  1001. {
  1002. void ConfigureListenOptions(ListenOptions listenOptions)
  1003. {
  1004. listenOptions.UseHttps(options =>
  1005. {
  1006. #pragma warning disable SYSLIB0039 // TLS 1.0 and 1.1 are obsolete
  1007. options.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11;
  1008. #pragma warning restore SYSLIB0039
  1009. options.ServerCertificate = _x509Certificate2;
  1010. options.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
  1011. options.AllowAnyClientCertificate();
  1012. });
  1013. }
  1014. await using (var server = new TestServer(context => context.Response.WriteAsync("hello world"), new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  1015. {
  1016. // SslStream is used to ensure the certificate is actually passed to the server
  1017. // HttpClient might not send the certificate because it is invalid or it doesn't match any
  1018. // of the certificate authorities sent by the server in the SSL handshake.
  1019. using (var connection = server.CreateConnection())
  1020. {
  1021. var stream = OpenSslStreamWithCert(connection.Stream);
  1022. #pragma warning disable SYSLIB0039 // TLS 1.0 and 1.1 are obsolete
  1023. var ex = await Assert.ThrowsAnyAsync<Exception>(
  1024. async () => await stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls, false));
  1025. #pragma warning restore SYSLIB0039
  1026. }
  1027. }
  1028. }
  1029. [Theory]
  1030. [InlineData(ClientCertificateMode.AllowCertificate)]
  1031. [InlineData(ClientCertificateMode.RequireCertificate)]
  1032. public async Task ClientCertificateValidationGetsCalledWithNotNullParameters(ClientCertificateMode mode)
  1033. {
  1034. var clientCertificateValidationCalled = false;
  1035. void ConfigureListenOptions(ListenOptions listenOptions)
  1036. {
  1037. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  1038. {
  1039. ServerCertificate = _x509Certificate2,
  1040. ClientCertificateMode = mode,
  1041. ClientCertificateValidation = (certificate, chain, sslPolicyErrors) =>
  1042. {
  1043. clientCertificateValidationCalled = true;
  1044. Assert.NotNull(certificate);
  1045. Assert.NotNull(chain);
  1046. return true;
  1047. }
  1048. });
  1049. }
  1050. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  1051. {
  1052. using (var connection = server.CreateConnection())
  1053. {
  1054. var stream = OpenSslStreamWithCert(connection.Stream);
  1055. await stream.AuthenticateAsClientAsync("localhost");
  1056. await AssertConnectionResult(stream, true);
  1057. Assert.True(clientCertificateValidationCalled);
  1058. }
  1059. }
  1060. }
  1061. [ConditionalTheory]
  1062. [InlineData(ClientCertificateMode.AllowCertificate)]
  1063. [InlineData(ClientCertificateMode.RequireCertificate)]
  1064. public async Task ValidationFailureRejectsConnection(ClientCertificateMode mode)
  1065. {
  1066. void ConfigureListenOptions(ListenOptions listenOptions)
  1067. {
  1068. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  1069. {
  1070. ServerCertificate = _x509Certificate2,
  1071. ClientCertificateMode = mode,
  1072. ClientCertificateValidation = (certificate, chain, sslPolicyErrors) => false
  1073. });
  1074. }
  1075. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  1076. {
  1077. using (var connection = server.CreateConnection())
  1078. {
  1079. var stream = OpenSslStreamWithCert(connection.Stream);
  1080. await stream.AuthenticateAsClientAsync("localhost");
  1081. await AssertConnectionResult(stream, false);
  1082. }
  1083. }
  1084. }
  1085. [Theory]
  1086. [InlineData(ClientCertificateMode.AllowCertificate)]
  1087. [InlineData(ClientCertificateMode.RequireCertificate)]
  1088. public async Task RejectsConnectionOnSslPolicyErrorsWhenNoValidation(ClientCertificateMode mode)
  1089. {
  1090. void ConfigureListenOptions(ListenOptions listenOptions)
  1091. {
  1092. listenOptions.UseHttps(new HttpsConnectionAdapterOptions
  1093. {
  1094. ServerCertificate = _x509Certificate2,
  1095. ClientCertificateMode = mode
  1096. });
  1097. }
  1098. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  1099. {
  1100. using (var connection = server.CreateConnection())
  1101. {
  1102. var stream = OpenSslStreamWithCert(connection.Stream);
  1103. await stream.AuthenticateAsClientAsync("localhost");
  1104. await AssertConnectionResult(stream, false);
  1105. }
  1106. }
  1107. }
  1108. [Fact]
  1109. public async Task AllowAnyCertOverridesCertificateValidation()
  1110. {
  1111. void ConfigureListenOptions(ListenOptions listenOptions)
  1112. {
  1113. listenOptions.UseHttps(options =>
  1114. {
  1115. options.ServerCertificate = _x509Certificate2;
  1116. options.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
  1117. options.ClientCertificateValidation = (certificate, x509Chain, sslPolicyErrors) => false;
  1118. options.AllowAnyClientCertificate();
  1119. });
  1120. }
  1121. await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  1122. {
  1123. using (var connection = server.CreateConnection())
  1124. {
  1125. var stream = OpenSslStreamWithCert(connection.Stream);
  1126. await stream.AuthenticateAsClientAsync("localhost");
  1127. await AssertConnectionResult(stream, true);
  1128. }
  1129. }
  1130. }
  1131. [Fact]
  1132. public async Task CertificatePassedToHttpContextIsNotDisposed()
  1133. {
  1134. void ConfigureListenOptions(ListenOptions listenOptions)
  1135. {
  1136. listenOptions.UseHttps(options =>
  1137. {
  1138. options.ServerCertificate = _x509Certificate2;
  1139. options.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
  1140. options.AllowAnyClientCertificate();
  1141. });
  1142. }
  1143. RequestDelegate app = context =>
  1144. {
  1145. var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
  1146. Assert.NotNull(tlsFeature);
  1147. Assert.NotNull(tlsFeature.ClientCertificate);
  1148. Assert.NotNull(context.Connection.ClientCertificate);
  1149. Assert.NotNull(context.Connection.ClientCertificate.PublicKey);
  1150. return context.Response.WriteAsync("hello world");
  1151. };
  1152. await using (var server = new TestServer(app, new TestServiceContext(LoggerFactory), ConfigureListenOptions))
  1153. {
  1154. using (var connection = server.CreateConnection())
  1155. {
  1156. var stream = OpenSslStreamWithCert(connection.Stream);
  1157. await stream.AuthenticateAsClientAsync("localhost");
  1158. await AssertConnectionResult(stream, true);
  1159. }
  1160. }
  1161. }
  1162. [Theory]
  1163. [InlineData("no_extensions.pfx")]
  1164. public void AcceptsCertificateWithoutExtensions(string testCertName)
  1165. {
  1166. var certPath = TestResources.GetCertPath(testCertName);
  1167. TestOutputHelper.WriteLine("Loading " + certPath);
  1168. var cert = new X509Certificate2(certPath, "testPassword");
  1169. Assert.Empty(cert.Extensions.OfType<X509EnhancedKeyUsageExtension>());
  1170. CreateMiddleware(cert);
  1171. }
  1172. [Theory]
  1173. [InlineData("eku.server.pfx")]
  1174. [InlineData("eku.multiple_usages.pfx")]
  1175. public void ValidatesEnhancedKeyUsageOnCertificate(string testCertName)
  1176. {
  1177. var certPath = TestResources.GetCertPath(testCertName);
  1178. TestOutputHelper.WriteLine("Loading " + certPath);
  1179. var cert = new X509Certificate2(certPath, "testPassword");
  1180. Assert.NotEmpty(cert.Extensions);
  1181. var eku = Assert.Single(cert.Extensions.OfType<X509EnhancedKeyUsageExtension>());
  1182. Assert.NotEmpty(eku.EnhancedKeyUsages);
  1183. CreateMiddleware(new HttpsConnectionAdapterOptions
  1184. {
  1185. ServerCertificate = cert,
  1186. });
  1187. }
  1188. [Theory]
  1189. [InlineData("eku.code_signing.pfx")]
  1190. [InlineData("eku.client.pfx")]
  1191. public void ThrowsForCertificatesMissingServerEku(string testCertName)
  1192. {
  1193. var certPath = TestResources.GetCertPath(testCertName);
  1194. TestOutputHelper.WriteLine("Loading " + certPath);
  1195. var cert = new X509Certificate2(certPath, "testPassword");
  1196. Assert.NotEmpty(cert.Extensions);
  1197. var eku = Assert.Single(cert.Extensions.OfType<X509EnhancedKeyUsageExtension>());
  1198. Assert.NotEmpty(eku.EnhancedKeyUsages);
  1199. var ex = Assert.Throws<InvalidOperationException>(() =>
  1200. CreateMiddleware(new HttpsConnectionAdapterOptions
  1201. {
  1202. ServerCertificate = cert,
  1203. }));
  1204. Assert.Equal(CoreStrings.FormatInvalidServerCertificateEku(cert.Thumbprint), ex.Message);
  1205. }
  1206. [ConditionalTheory]
  1207. [InlineData(HttpProtocols.Http1)]
  1208. [InlineData(HttpProtocols.Http2)]
  1209. [InlineData(HttpProtocols.Http1AndHttp2)]
  1210. [TlsAlpnSupported]
  1211. [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10)]
  1212. public async Task ListenOptionsProtolsCanBeSetAfterUseHttps(HttpProtocols httpProtocols)
  1213. {
  1214. void ConfigureListenOptions(ListenOptions listenOptions)
  1215. {
  1216. listenOptions.UseHttps(_x509Certificate2);
  1217. listenOptions.Protocols = httpProtocols;
  1218. }
  1219. await using var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), ConfigureListenOptions);
  1220. using var connection = server.CreateConnection();
  1221. var sslOptions = new SslClientAuthenticationOptions
  1222. {
  1223. TargetHost = "localhost",
  1224. EnabledSslProtocols = SslProtocols.None,
  1225. ApplicationProtocols = new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 },
  1226. };
  1227. using var stream = OpenSslStream(connection.Stream);
  1228. await stream.AuthenticateAsClientAsync(sslOptions);
  1229. Assert.Equal(
  1230. httpProtocols.HasFlag(HttpProtocols.Http2) ?
  1231. SslApplicationProtocol.Http2 :
  1232. SslApplicationProtocol.Http11,
  1233. stream.NegotiatedApplicationProtocol);
  1234. }
  1235. [ConditionalFact]
  1236. [OSSkipCondition(OperatingSystems.MacOSX | OperatingSystems.Linux, SkipReason = "Downgrade logic only applies on Windows")]
  1237. [MaximumOSVersion(OperatingSystems.Windows, WindowsVersions.Win81)]
  1238. public void Http1AndHttp2DowngradeToHttp1ForHttpsOnIncompatibleWindowsVersions()
  1239. {
  1240. var httpConnectionAdapterOptions = new HttpsConnectionAdapterOptions
  1241. {
  1242. ServerCertificate = _x509Certificate2,
  1243. HttpProtocols = HttpProtocols.Http1AndHttp2
  1244. };
  1245. CreateMiddleware(httpConnectionAdapterOptions);
  1246. Assert.Equal(HttpProtocols.Http1, httpConnectionAdapterOptions.HttpProtocols);
  1247. }
  1248. [ConditionalFact]
  1249. [OSSkipCondition(OperatingSystems.MacOSX | OperatingSystems.Linux, SkipReason = "Downgrade logic only applies on Windows")]
  1250. [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10)]
  1251. public void Http1AndHttp2DoesNotDowngradeOnCompatibleWindowsVersions()
  1252. {
  1253. var httpConnectionAdapterOptions = new HttpsConnectionAdapterOptions
  1254. {
  1255. ServerCertificate = _x509Certificate2,
  1256. HttpProtocols = HttpProtocols.Http1AndHttp2
  1257. };
  1258. CreateMiddleware(httpConnectionAdapterOptions);
  1259. Assert.Equal(HttpProtocols.Http1AndHttp2, httpConnectionAdapterOptions.HttpProtocols);
  1260. }
  1261. [ConditionalFact]
  1262. [OSSkipCondition(OperatingSystems.MacOSX | OperatingSystems.Linux, SkipReason = "Error logic only applies on Windows")]
  1263. [MaximumOSVersion(OperatingSystems.Windows, WindowsVersions.Win81)]
  1264. public void Http2ThrowsOnIncompatibleWindowsVersions()
  1265. {
  1266. var httpConnectionAdapterOptions = new HttpsConnectionAdapterOptions
  1267. {
  1268. ServerCertificate = _x509Certificate2,
  1269. HttpProtocols = HttpProtocols.Http2
  1270. };
  1271. Assert.Throws<NotSupportedException>(() => CreateMiddleware(httpConnectionAdapterOptions));
  1272. }
  1273. [ConditionalFact]
  1274. [OSSkipCondition(OperatingSystems.MacOSX | OperatingSystems.Linux, SkipReason = "Error logic only applies on Windows")]
  1275. [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10)]
  1276. public void Http2DoesNotThrowOnCompatibleWindowsVersions()
  1277. {
  1278. var httpConnectionAdapterOptions = new HttpsConnectionAdapterOptions
  1279. {
  1280. ServerCertificate = _x509Certificate2,
  1281. HttpProtocols = HttpProtocols.Http2
  1282. };
  1283. // Does not throw
  1284. CreateMiddleware(httpConnectionAdapterOptions);
  1285. }
  1286. private static HttpsConnectionMiddleware CreateMiddleware(X509Certificate2 serverCertificate)
  1287. {
  1288. return CreateMiddleware(new HttpsConnectionAdapterOptions
  1289. {
  1290. ServerCertificate = serverCertificate,
  1291. });
  1292. }
  1293. private static HttpsConnectionMiddleware CreateMiddleware(HttpsConnectionAdapterOptions options)
  1294. {
  1295. return new HttpsConnectionMiddleware(context => Task.CompletedTask, options, new KestrelMetrics(new TestMeterFactory()));
  1296. }
  1297. private static async Task App(HttpContext httpContext)
  1298. {
  1299. var request = httpContext.Request;
  1300. var response = httpContext.Response;
  1301. while (true)
  1302. {
  1303. var buffer = new byte[8192];
  1304. var count = await request.Body.ReadAsync(buffer, 0, buffer.Length);
  1305. if (count == 0)
  1306. {
  1307. break;
  1308. }
  1309. await response.Body.WriteAsync(buffer, 0, count);
  1310. }
  1311. }
  1312. private static SslStream OpenSslStream(Stream rawStream)
  1313. {
  1314. return new SslStream(rawStream, false, (sender, certificate, chain, errors) => true);
  1315. }
  1316. /// <summary>
  1317. /// SslStream is used to ensure the certificate is actually passed to the server
  1318. /// HttpClient might not send the certificate because it is invalid or it doesn't match any
  1319. /// of the certificate authorities sent by the server in the SSL handshake.
  1320. /// </summary>
  1321. private static SslStream OpenSslStreamWithCert(Stream rawStream, X509Certificate2 clientCertificate = null)
  1322. {
  1323. return new SslStream(rawStream, false, (sender, certificate, chain, errors) => true,
  1324. (sender, host, certificates, certificate, issuers) => clientCertificate ?? _x509Certificate2);
  1325. }
  1326. private static async Task AssertConnectionResult(SslStream stream, bool success, string body = null)
  1327. {
  1328. var request = body == null ? Encoding.UTF8.GetBytes("GET / HTTP/1.0\r\n\r\n")
  1329. : Encoding.UTF8.GetBytes($"POST / HTTP/1.0\r\nContent-Length: {body.Length}\r\n\r\n{body}");
  1330. await stream.WriteAsync(request, 0, request.Length);
  1331. var reader = new StreamReader(stream);
  1332. string line = null;
  1333. if (success)
  1334. {
  1335. line = await reader.ReadLineAsync();
  1336. Assert.Equal("HTTP/1.1 200 OK", line);
  1337. }
  1338. else
  1339. {
  1340. try
  1341. {
  1342. line = await reader.ReadLineAsync();
  1343. }
  1344. catch (IOException) { }
  1345. Assert.Null(line);
  1346. }
  1347. }
  1348. }