-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Change iOS PEM searcher to use PemEncoding.TryFindUtf8 #109564
Merged
Merged
Changes from all commits
Commits
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 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
53 changes: 31 additions & 22 deletions
53
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/PemEnumerator.cs
This file contains 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 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 |
---|---|---|
|
@@ -2,59 +2,48 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using System.Buffers.Text; | ||
using System.Diagnostics; | ||
using System.Text; | ||
using Internal.Cryptography; | ||
|
||
namespace System.Security.Cryptography.X509Certificates | ||
{ | ||
internal sealed partial class AppleCertificatePal : ICertificatePal | ||
{ | ||
internal delegate bool DerCallback(ReadOnlySpan<byte> derData, X509ContentType contentType); | ||
|
||
internal static bool TryDecodePem(ReadOnlySpan<byte> rawData, DerCallback derCallback) | ||
internal static void TryDecodePem(ReadOnlySpan<byte> rawData, Func<ReadOnlySpan<byte>, X509ContentType, bool> derCallback) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return value from this method was always ignored and seemed to have no purpose any more. So it became |
||
{ | ||
// If the character is a control character that isn't whitespace, then we're probably using a DER encoding | ||
// and not using a PEM encoding in ASCII. | ||
// and not using a PEM encoding in UTF8. | ||
if (char.IsControl((char)rawData[0]) && !char.IsWhiteSpace((char)rawData[0])) | ||
{ | ||
return false; | ||
} | ||
|
||
// Look for the PEM marker. This doesn't guarantee it will be a valid PEM since we don't check whether | ||
// the marker is at the beginning of line or whether the line is a complete marker. It's just a quick | ||
// check to avoid conversion from bytes to characters if the content is DER encoded. | ||
if (rawData.IndexOf("-----BEGIN "u8) < 0) | ||
{ | ||
return false; | ||
return; | ||
} | ||
|
||
char[] certPem = ArrayPool<char>.Shared.Rent(rawData.Length); | ||
byte[]? certBytes = null; | ||
|
||
try | ||
{ | ||
Encoding.ASCII.GetChars(rawData, certPem); | ||
|
||
foreach ((ReadOnlySpan<char> contents, PemFields fields) in new PemEnumerator(certPem.AsSpan(0, rawData.Length))) | ||
foreach ((ReadOnlySpan<byte> contents, PemFields fields) in PemEnumerator.Utf8(rawData)) | ||
{ | ||
ReadOnlySpan<char> label = contents[fields.Label]; | ||
ReadOnlySpan<byte> label = contents[fields.Label]; | ||
bool isCertificate = label.SequenceEqual(PemLabels.X509CertificateUtf8); | ||
|
||
if (label.SequenceEqual(PemLabels.X509Certificate) || label.SequenceEqual(PemLabels.Pkcs7Certificate)) | ||
if (isCertificate || label.SequenceEqual(PemLabels.Pkcs7CertificateUtf8)) | ||
{ | ||
certBytes = CryptoPool.Rent(fields.DecodedDataLength); | ||
|
||
if (!Convert.TryFromBase64Chars(contents[fields.Base64Data], certBytes, out int bytesWritten) | ||
|| bytesWritten != fields.DecodedDataLength) | ||
OperationStatus decodeResult = Base64.DecodeFromUtf8( | ||
contents[fields.Base64Data], | ||
certBytes, | ||
out _, | ||
out int bytesWritten); | ||
|
||
if (decodeResult != OperationStatus.Done || bytesWritten != fields.DecodedDataLength) | ||
{ | ||
Debug.Fail("The contents should have already been validated by the PEM reader."); | ||
throw new CryptographicException(SR.Cryptography_X509_NoPemCertificate); | ||
} | ||
|
||
X509ContentType contentType = | ||
label.SequenceEqual(PemLabels.X509Certificate) ? | ||
X509ContentType.Cert : | ||
X509ContentType.Pkcs7; | ||
X509ContentType contentType = isCertificate ? X509ContentType.Cert : X509ContentType.Pkcs7; | ||
bool cont = derCallback(certBytes.AsSpan(0, bytesWritten), contentType); | ||
|
||
byte[] toReturn = certBytes; | ||
|
@@ -63,22 +52,18 @@ internal static bool TryDecodePem(ReadOnlySpan<byte> rawData, DerCallback derCal | |
|
||
if (!cont) | ||
{ | ||
return true; | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
ArrayPool<char>.Shared.Return(certPem, clearArray: true); | ||
|
||
if (certBytes != null) | ||
{ | ||
CryptoPool.Return(certBytes, clearSize: 0); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need a dedicated delegate anymore with
allows ref struct
on Func now, so use Func.