forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
119 lines (97 loc) · 4 KB
/
Copy pathProgram.cs
File metadata and controls
119 lines (97 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.Extensions.Hosting;
using TlsFeaturesObserve.HttpSys;
HttpSysConfigurator.ConfigureCacheTlsClientHello();
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseHttpSys(options =>
{
options.UrlPrefixes.Add("https://*:6000");
options.Authentication.Schemes = AuthenticationSchemes.None;
options.Authentication.AllowAnonymous = true;
});
var app = builder.Build();
// Example middleware using TryGetTlsClientHello API to query TLS Client Hello raw bytes.
app.Use(async (context, next) =>
{
var connectionFeature = context.Features.GetRequiredFeature<IHttpConnectionFeature>();
var httpSysPropFeature = context.Features.GetRequiredFeature<IHttpSysRequestPropertyFeature>();
var tlsHandshakeFeature = context.Features.GetRequiredFeature<ITlsHandshakeFeature>();
// first time invocation to find out required size
var success = httpSysPropFeature.TryGetTlsClientHello(Array.Empty<byte>(), out var bytesReturned);
Debug.Assert(!success);
Debug.Assert(bytesReturned > 0);
// rent with enough memory span and invoke
var bytes = ArrayPool<byte>.Shared.Rent(bytesReturned);
success = httpSysPropFeature.TryGetTlsClientHello(bytes, out _);
Debug.Assert(success);
await context.Response.WriteAsync(
$"""
TryGetTlsClientHello
--------------------
connectionId = {connectionFeature.ConnectionId};
negotiated cipher suite = {tlsHandshakeFeature.NegotiatedCipherSuite};
tlsClientHello.length = {bytesReturned};
tlsclienthello start = {string.Join(' ', bytes.AsSpan(0, 30).ToArray())}
""");
await next(context);
});
// Example middleware exercising the generic IHttpSysRequestPropertyFeature.TryGetRequestProperty API.
app.Use(async (context, next) =>
{
// From Win SDK http.h
const int HttpRequestPropertyTlsClientHello = 11;
var httpSysPropFeature = context.Features.GetRequiredFeature<IHttpSysRequestPropertyFeature>();
try
{
// probe required size with empty output buffer and empty qualifier
var success = httpSysPropFeature.TryGetRequestProperty(
HttpRequestPropertyTlsClientHello,
qualifier: default,
output: default,
out var requiredSize);
Debug.Assert(!success);
Debug.Assert(requiredSize > 0);
var rented = ArrayPool<byte>.Shared.Rent(requiredSize);
try
{
success = httpSysPropFeature.TryGetRequestProperty(
HttpRequestPropertyTlsClientHello,
qualifier: default,
output: rented.AsSpan(0, requiredSize),
out var written);
Debug.Assert(success);
await context.Response.WriteAsync(
$"""
TryGetRequestProperty(HttpRequestPropertyTlsClientHello)
--------------------------------------------------------
requiredSize = {requiredSize}
bytesReturned = {written}
first 30 bytes = {string.Join(' ', rented.AsSpan(0, Math.Min(30, written)).ToArray())}
""");
}
finally
{
ArrayPool<byte>.Shared.Return(rented);
}
}
catch (Exception ex)
{
await context.Response.WriteAsync(
$"""
TryGetRequestProperty(HttpRequestPropertyTlsClientHello) threw: {ex.GetType().Name}: {ex.Message}
""");
}
await next(context);
});
app.Run();