| | | 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 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Net.Security; |
| | | 6 | | using System.Security.Authentication; |
| | | 7 | | |
| | | 8 | | namespace DotnetFuzzing.Fuzzers |
| | | 9 | | { |
| | | 10 | | internal sealed class SslStreamClientHelloFuzzer : IFuzzer |
| | | 11 | | { |
| | 0 | 12 | | public string[] TargetAssemblies => ["System.Net.Security"]; |
| | | 13 | | |
| | 0 | 14 | | public string[] TargetCoreLibPrefixes => []; |
| | | 15 | | |
| | | 16 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 38 | 17 | | { |
| | 38 | 18 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(bytes.Length); |
| | | 19 | | try |
| | 38 | 20 | | { |
| | 38 | 21 | | bytes.CopyTo(buffer); |
| | 38 | 22 | | using MemoryStream ms = new MemoryStream(buffer, 0, bytes.Length); |
| | 38 | 23 | | using SslStream sslStream = new SslStream(ms); |
| | 38 | 24 | | sslStream.AuthenticateAsServerAsync((stream, clientHelloInfo, b, token) => |
| | 2 | 25 | | { |
| | 38 | 26 | | // This callback should be called when ClientHello is |
| | 38 | 27 | | // received, Since we don't parse any other TLS messages, |
| | 38 | 28 | | // we can terminate the handshake here. Fuzzing the rest of |
| | 38 | 29 | | // the handshake would fuzz the platform TLS implementation |
| | 38 | 30 | | // which is not instrumented and is out of scope for this |
| | 38 | 31 | | // fuzzer. |
| | 2 | 32 | | throw new MyCustomException(); |
| | 38 | 33 | | }, null).GetAwaiter().GetResult(); |
| | 0 | 34 | | } |
| | 38 | 35 | | catch (Exception ex) when (ex is AuthenticationException or IOException or MyCustomException) |
| | 38 | 36 | | { |
| | 38 | 37 | | } |
| | | 38 | | finally |
| | 38 | 39 | | { |
| | 38 | 40 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 38 | 41 | | } |
| | 38 | 42 | | } |
| | | 43 | | |
| | | 44 | | internal class MyCustomException : Exception |
| | | 45 | | { |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | } |