-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add X509 SAN extension and RFC6125 MatchesHostname #72304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d18fb13
Add X509 SAN extension and RFC6125 MatchesHostname
bartonjs ba03c57
Address feedback
bartonjs fa94579
Make macOS happy
bartonjs 0a462d0
Make macOS happier
bartonjs 4207504
Small test enhancements
bartonjs 0719d7b
Add new X.509 extensions into CryptoConfig tests
bartonjs 6aca049
Apply feedback
bartonjs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
...curity.Cryptography.X509Certificates/tests/ExtensionsTests/SubjectAlternativeNameTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net; | ||
| using Xunit; | ||
|
|
||
| namespace System.Security.Cryptography.X509Certificates.Tests.ExtensionsTests | ||
| { | ||
| public static class SubjectAlternativeNameTests | ||
| { | ||
| [Fact] | ||
| public static void DefaultConstructor() | ||
| { | ||
| X509SubjectAlternativeNameExtension ext = new X509SubjectAlternativeNameExtension(); | ||
| Assert.Empty(ext.RawData); | ||
| Assert.Equal("2.5.29.17", ext.Oid.Value); | ||
| Assert.Empty(ext.EnumerateDnsNames()); | ||
| Assert.Empty(ext.EnumerateIPAddresses()); | ||
| Assert.False(ext.Critical, "ext.Critical"); | ||
| } | ||
bartonjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [Fact] | ||
| public static void ArrayCtorRejectsNull() | ||
| { | ||
| Assert.Throws<ArgumentNullException>( | ||
| "rawData", | ||
| () => new X509SubjectAlternativeNameExtension((byte[])null)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(LoadMode.CopyFrom)] | ||
| [InlineData(LoadMode.Array)] | ||
| [InlineData(LoadMode.Span)] | ||
| public static void EnumerateDnsNames(LoadMode loadMode) | ||
| { | ||
| SubjectAlternativeNameBuilder builder = new SubjectAlternativeNameBuilder(); | ||
| builder.AddDnsName("foo"); | ||
| builder.AddIpAddress(IPAddress.Loopback); | ||
| builder.AddUserPrincipalName("[email protected]"); | ||
| builder.AddIpAddress(IPAddress.IPv6Loopback); | ||
| builder.AddDnsName("*.foo"); | ||
| X509Extension built = builder.Build(true); | ||
|
|
||
| X509SubjectAlternativeNameExtension ext; | ||
|
|
||
| switch (loadMode) | ||
| { | ||
| case LoadMode.CopyFrom: | ||
| ext = new X509SubjectAlternativeNameExtension(); | ||
| ext.CopyFrom(built); | ||
| break; | ||
| case LoadMode.Array: | ||
| ext = new X509SubjectAlternativeNameExtension(built.RawData); | ||
| break; | ||
| case LoadMode.Span: | ||
| byte[] tmp = new byte[built.RawData.Length + 2]; | ||
| built.RawData.AsSpan().CopyTo(tmp.AsSpan(1)); | ||
| ext = new X509SubjectAlternativeNameExtension(tmp.AsSpan()[1..^1]); | ||
| tmp.AsSpan().Clear(); | ||
| break; | ||
| default: | ||
| throw new ArgumentOutOfRangeException(nameof(loadMode), loadMode, "Unexpected mode"); | ||
| } | ||
|
|
||
| Assert.Equal(new[] { "foo", "*.foo" }, ext.EnumerateDnsNames()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(LoadMode.CopyFrom)] | ||
| [InlineData(LoadMode.Array)] | ||
| [InlineData(LoadMode.Span)] | ||
| public static void EnumerateIPAddresses(LoadMode loadMode) | ||
| { | ||
| SubjectAlternativeNameBuilder builder = new SubjectAlternativeNameBuilder(); | ||
| builder.AddDnsName("foo"); | ||
| builder.AddIpAddress(IPAddress.Loopback); | ||
| builder.AddUserPrincipalName("[email protected]"); | ||
| builder.AddIpAddress(IPAddress.IPv6Loopback); | ||
| builder.AddDnsName("*.foo"); | ||
| X509Extension built = builder.Build(true); | ||
|
|
||
| X509SubjectAlternativeNameExtension ext; | ||
|
|
||
| switch (loadMode) | ||
| { | ||
| case LoadMode.CopyFrom: | ||
| ext = new X509SubjectAlternativeNameExtension(); | ||
| ext.CopyFrom(built); | ||
| break; | ||
| case LoadMode.Array: | ||
| ext = new X509SubjectAlternativeNameExtension(built.RawData); | ||
| break; | ||
| case LoadMode.Span: | ||
| byte[] tmp = new byte[built.RawData.Length + 2]; | ||
| built.RawData.AsSpan().CopyTo(tmp.AsSpan(1)); | ||
| ext = new X509SubjectAlternativeNameExtension(tmp.AsSpan()[1..^1]); | ||
| tmp.AsSpan().Clear(); | ||
| break; | ||
| default: | ||
| throw new ArgumentOutOfRangeException(nameof(loadMode), loadMode, "Unexpected mode"); | ||
| } | ||
|
|
||
| Assert.Equal(new[] { IPAddress.Loopback, IPAddress.IPv6Loopback }, ext.EnumerateIPAddresses()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(LoadMode.CopyFrom)] | ||
| [InlineData(LoadMode.Array)] | ||
| [InlineData(LoadMode.Span)] | ||
| public static void CopyFromAfterLoaded(LoadMode originalLoadMode) | ||
| { | ||
| SubjectAlternativeNameBuilder builder = new SubjectAlternativeNameBuilder(); | ||
| builder.AddDnsName("foo"); | ||
| builder.AddIpAddress(IPAddress.Loopback); | ||
| builder.AddUserPrincipalName("[email protected]"); | ||
| builder.AddIpAddress(IPAddress.IPv6Loopback); | ||
| builder.AddDnsName("*.foo"); | ||
| X509Extension built = builder.Build(true); | ||
|
|
||
| X509SubjectAlternativeNameExtension ext; | ||
|
|
||
| switch (originalLoadMode) | ||
| { | ||
| case LoadMode.CopyFrom: | ||
| ext = new X509SubjectAlternativeNameExtension(); | ||
| ext.CopyFrom(built); | ||
| break; | ||
| case LoadMode.Array: | ||
| ext = new X509SubjectAlternativeNameExtension(built.RawData, critical: true); | ||
| break; | ||
| case LoadMode.Span: | ||
| byte[] tmp = new byte[built.RawData.Length + 2]; | ||
| built.RawData.AsSpan().CopyTo(tmp.AsSpan(1)); | ||
| ext = new X509SubjectAlternativeNameExtension(tmp.AsSpan()[1..^1], critical: true); | ||
| tmp.AsSpan().Clear(); | ||
| break; | ||
| default: | ||
| throw new ArgumentOutOfRangeException(nameof(originalLoadMode), originalLoadMode, "Unexpected mode"); | ||
| } | ||
|
|
||
| Assert.True(ext.Critical, "ext.Critical"); | ||
| Assert.Equal(new[] { "foo", "*.foo" }, ext.EnumerateDnsNames()); | ||
| Assert.Equal(new[] { IPAddress.Loopback, IPAddress.IPv6Loopback }, ext.EnumerateIPAddresses()); | ||
|
|
||
| builder = new SubjectAlternativeNameBuilder(); | ||
| builder.AddDnsName("a"); | ||
| builder.AddDnsName("b"); | ||
| builder.AddDnsName("c"); | ||
| builder.AddIpAddress(IPAddress.IPv6Loopback); | ||
| ext.CopyFrom(builder.Build()); | ||
|
|
||
| Assert.False(ext.Critical, "ext.Critical"); | ||
| Assert.Equal(new[] { "a", "b", "c" }, ext.EnumerateDnsNames()); | ||
| Assert.Equal(new[] { IPAddress.IPv6Loopback }, ext.EnumerateIPAddresses()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(LoadMode.CopyFrom)] | ||
| [InlineData(LoadMode.Array)] | ||
| [InlineData(LoadMode.Span)] | ||
| public static void VerifyDecodeFailureBehavior(LoadMode loadMode) | ||
| { | ||
| byte[] invalidEncoding = { 0x05, 0x00 }; | ||
|
|
||
| VerifyDecodeFailure(invalidEncoding, loadMode); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(LoadMode.CopyFrom)] | ||
| [InlineData(LoadMode.Array)] | ||
| [InlineData(LoadMode.Span)] | ||
| public static void VerifyInvalidIPAddressBehavior(LoadMode loadMode) | ||
| { | ||
| // dNSName: foo | ||
| // iPAddress: 127.0.0.1 | ||
| // iPAddress: 7F 00 00 01 00 (127.0.0.1 with a trailing 0) | ||
| // UPN: [email protected] | ||
| // iPAddress: ::1 | ||
| // dNSName: *.foo | ||
| byte[] invalidEncoding = | ||
| { | ||
| 0x30, 0x4D, 0x82, 0x03, 0x66, 0x6F, 0x6F, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x87, 0x05, 0x7F, | ||
| 0x00, 0x00, 0x01, 0x00, 0xA0, 0x20, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x14, | ||
| 0x02, 0x03, 0xA0, 0x12, 0x0C, 0x10, 0x75, 0x73, 0x65, 0x72, 0x40, 0x73, 0x6F, 0x6D, 0x65, 0x2E, | ||
| 0x64, 0x6F, 0x6D, 0x61, 0x69, 0x6E, 0x87, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x82, 0x05, 0x2A, 0x2E, 0x66, 0x6F, 0x6F, | ||
| }; | ||
|
|
||
| VerifyDecodeFailure(invalidEncoding, loadMode); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(LoadMode.CopyFrom)] | ||
| [InlineData(LoadMode.Array)] | ||
| [InlineData(LoadMode.Span)] | ||
| public static void VerifyInvalidDnsNameBehavior(LoadMode loadMode) | ||
| { | ||
| // dNSName: foo | ||
| // iPAddress: 127.0.0.1 | ||
| // UPN: [email protected] | ||
| // dNSName: 86 6F 6F ("foo" with the f changed from 66 to 86) | ||
| // iPAddress: ::1 | ||
| // dNSName: *.foo | ||
| byte[] invalidEncoding = | ||
| { | ||
| 0x30, 0x4B, 0x82, 0x03, 0x66, 0x6F, 0x6F, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0xA0, 0x20, 0x06, | ||
| 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x14, 0x02, 0x03, 0xA0, 0x12, 0x0C, 0x10, 0x75, | ||
| 0x73, 0x65, 0x72, 0x40, 0x73, 0x6F, 0x6D, 0x65, 0x2E, 0x64, 0x6F, 0x6D, 0x61, 0x69, 0x6E, 0x82, | ||
| 0x03, 0x86, 0x6F, 0x6F, 0x87, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x82, 0x05, 0x2A, 0x2E, 0x66, 0x6F, 0x6F, | ||
| }; | ||
|
|
||
| VerifyDecodeFailure(invalidEncoding, loadMode); | ||
| } | ||
|
|
||
| private static void VerifyDecodeFailure(byte[] invalidEncoding, LoadMode loadMode) | ||
| { | ||
| switch (loadMode) | ||
| { | ||
| case LoadMode.CopyFrom: | ||
| X509Extension untyped = new X509Extension("0.0", invalidEncoding, true); | ||
| X509SubjectAlternativeNameExtension ext = new X509SubjectAlternativeNameExtension(); | ||
|
|
||
| // The pattern for X509Extension is that CopyFrom doesn't validate data, | ||
| // and it blindly accepts the incoming OID. The semantic properties then throw late. | ||
| ext.CopyFrom(untyped); | ||
| Assert.True(ext.Critical); | ||
| Assert.Equal("0.0", ext.Oid.Value); | ||
| AssertExtensions.SequenceEqual(invalidEncoding, ext.RawData); | ||
| Assert.Throws<CryptographicException>(ext.EnumerateDnsNames); | ||
| Assert.Throws<CryptographicException>(ext.EnumerateIPAddresses); | ||
| break; | ||
| case LoadMode.Array: | ||
| // The ctors don't need to be so forgiving, through. | ||
| Assert.Throws<CryptographicException>( | ||
| () => new X509SubjectAlternativeNameExtension(invalidEncoding)); | ||
| break; | ||
| case LoadMode.Span: | ||
| Assert.Throws<CryptographicException>( | ||
| () => new X509SubjectAlternativeNameExtension(new ReadOnlySpan<byte>(invalidEncoding))); | ||
| break; | ||
| default: | ||
| throw new ArgumentOutOfRangeException(nameof(loadMode), loadMode, "Unexpected mode"); | ||
| } | ||
| } | ||
bartonjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public enum LoadMode | ||
| { | ||
| CopyFrom, | ||
| Array, | ||
| Span, | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.