Skip to content

Commit 57275c8

Browse files
steveharterkrwq
authored andcommitted
Merged PR 38844: [release/8.0] Fix unbound MAC work in GetCertContentType
Fix unbound MAC work in GetCertContentType
2 parents 173b4b8 + c031937 commit 57275c8

File tree

4 files changed

+59
-38
lines changed

4 files changed

+59
-38
lines changed

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Pal.macOS.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ private static DSA DecodeDsaPublicKey(byte[] encodedKeyValue, byte[] encodedPara
111111
public X509ContentType GetCertContentType(ReadOnlySpan<byte> rawData)
112112
{
113113
const int errSecUnknownFormat = -25257;
114+
114115
if (rawData.IsEmpty)
115116
{
116117
// Throw to match Windows and Unix behavior.
@@ -119,7 +120,7 @@ public X509ContentType GetCertContentType(ReadOnlySpan<byte> rawData)
119120

120121
X509ContentType contentType = Interop.AppleCrypto.X509GetContentType(rawData);
121122

122-
// Apple doesn't seem to recognize PFX files with no MAC, so try a quick maybe-it's-a-PFX test
123+
// Apple's native check can't check for PKCS12, so do a quick decode test to see if it is PKCS12 / PFX.
123124
if (contentType == X509ContentType.Unknown)
124125
{
125126
try
@@ -128,9 +129,11 @@ public X509ContentType GetCertContentType(ReadOnlySpan<byte> rawData)
128129
{
129130
fixed (byte* pin = rawData)
130131
{
132+
AsnValueReader reader = new AsnValueReader(rawData, AsnEncodingRules.BER);
133+
131134
using (var manager = new PointerMemoryManager<byte>(pin, rawData.Length))
132135
{
133-
PfxAsn.Decode(manager.Memory, AsnEncodingRules.BER);
136+
PfxAsn.Decode(ref reader, manager.Memory, out _);
134137
}
135138

136139
contentType = X509ContentType.Pkcs12;

src/libraries/System.Security.Cryptography/tests/X509Certificates/PfxIterationCountTests.X509Certificate2.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.DotNet.RemoteExecutor;
5+
using Microsoft.DotNet.XUnitExtensions;
6+
using Xunit;
7+
48
namespace System.Security.Cryptography.X509Certificates.Tests
59
{
10+
[SkipOnPlatform(TestPlatforms.Browser, "Browser doesn't support X.509 certificates")]
611
public class PfxIterationCountTests_X509Certificate2 : PfxIterationCountTests
712
{
813
internal override X509Certificate Import(byte[] blob)
@@ -22,5 +27,29 @@ internal override X509Certificate Import(string fileName, string password)
2227

2328
internal override X509Certificate Import(string fileName, SecureString password)
2429
=> new X509Certificate2(fileName, password);
30+
31+
32+
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
33+
public static void Import_IterationCountLimitExceeded_ThrowsInAllottedTime()
34+
{
35+
const int AllottedTime = 5000;
36+
37+
if (!PfxTests.Pkcs12PBES2Supported)
38+
{
39+
throw new SkipTestException("Pkcs12NoPassword100MRounds uses PBES2, which is not supported on this version.");
40+
}
41+
42+
RemoteInvokeOptions options = new()
43+
{
44+
TimeOut = AllottedTime
45+
};
46+
47+
RemoteExecutor.Invoke(static () =>
48+
{
49+
byte[] blob = TestData.Pkcs12NoPassword100MRounds;
50+
CryptographicException ce = Assert.Throws<CryptographicException>(() => new X509Certificate2(blob));
51+
Assert.Contains(FwlinkId, ce.Message);
52+
}, options).Dispose();
53+
}
2554
}
2655
}

src/libraries/System.Security.Cryptography/tests/X509Certificates/TestData.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,26 @@ internal static DSAParameters GetDSA1024Params()
34373437
"04020105000420AD0EB570ACFB8357A8E99B17672353CFBA69C76FFE5B6BC113" +
34383438
"05577F12AE24040408D04E60444B79672302030927C1").HexToByteArray();
34393439

3440+
internal static readonly byte[] Pkcs12NoPassword100MRounds = Convert.FromBase64String(
3441+
"MIIDygIBAzCCA4QGCSqGSIb3DQEHAaCCA3UEggNxMIIDbTCCA2kGCSqGSIb3DQEHBqCCA1owggNW" +
3442+
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T8Wsue0vq9jQ6J6YIEM+DKYr" +
3455+
"Zt2l4WgTBEKbpqmRvOqYRh9O8Sp+3IRNPzMC2ehzlYXqoPbtG4vxpoRsAMCM/W2x61jbsBSaNSFA" +
3456+
"eaUwcnKswRg30UonHUAIOJkqtadI57WE/Rat5eHVyya9f7ZN8bTFZjx0BQs6Bo8PK9yfqoidSN8w" +
3457+
"PTAfMAcGBSsOAwIaBBTt8zpgzygINykjoAwr2GKEywYFwgQUA+L1vfCVASwiE++gTfRgIScMGycC" +
3458+
"BAX14QA=");
3459+
34403460
internal static readonly byte[] Pkcs12OpenSslOneCertDefaultEmptyPassword =
34413461
("308209CF0201033082098506092A864886F70D010701A0820976048209723082" +
34423462
"096E308203E206092A864886F70D010706A08203D3308203CF020100308203C8" +

src/native/libs/System.Security.Cryptography.Native.Apple/pal_x509.c

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@ PAL_X509ContentType AppleCryptoNative_X509GetContentType(uint8_t* pbData, int32_
7070
// The sniffing order is:
7171
// * X509 DER
7272
// * PKCS7 PEM/DER
73-
// * PKCS12 DER (or PEM if Apple has non-standard support for that)
7473
// * X509 PEM or PEM aggregate (or DER, but that already matched)
7574
//
7675
// If the X509 PEM check is done first SecItemImport will erroneously match
7776
// some PKCS#7 blobs and say they were certificates.
7877
//
7978
// Likewise, if the X509 DER check isn't done first, Apple will report it as
8079
// being a PKCS#7.
80+
//
81+
// This does not attempt to open a PFX / PKCS12 as Apple does not provide
82+
// a suitable API to determine if it is PKCS12 without doing potentially
83+
// unbound MAC / KDF work. Instead, let that return Unknown and let the managed
84+
// decoding do the check.
8185
SecCertificateRef certref = SecCertificateCreateWithData(NULL, cfData);
8286

8387
if (certref != NULL)
@@ -104,41 +108,6 @@ PAL_X509ContentType AppleCryptoNative_X509GetContentType(uint8_t* pbData, int32_
104108
}
105109
}
106110

107-
dataFormat = kSecFormatPKCS12;
108-
actualFormat = dataFormat;
109-
itemType = kSecItemTypeAggregate;
110-
actualType = itemType;
111-
112-
osStatus = SecItemImport(cfData, NULL, &actualFormat, &actualType, 0, NULL, NULL, NULL);
113-
114-
if (osStatus == errSecPassphraseRequired)
115-
{
116-
dataFormat = kSecFormatPKCS12;
117-
actualFormat = dataFormat;
118-
itemType = kSecItemTypeAggregate;
119-
actualType = itemType;
120-
121-
SecItemImportExportKeyParameters importParams;
122-
memset(&importParams, 0, sizeof(SecItemImportExportKeyParameters));
123-
124-
importParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
125-
importParams.passphrase = CFSTR("");
126-
127-
osStatus = SecItemImport(cfData, NULL, &actualFormat, &actualType, 0, &importParams, NULL, NULL);
128-
129-
CFRelease(importParams.passphrase);
130-
importParams.passphrase = NULL;
131-
}
132-
133-
if (osStatus == noErr || osStatus == errSecPkcs12VerifyFailure)
134-
{
135-
if (actualType == itemType && actualFormat == dataFormat)
136-
{
137-
CFRelease(cfData);
138-
return PAL_Pkcs12;
139-
}
140-
}
141-
142111
dataFormat = kSecFormatX509Cert;
143112
actualFormat = dataFormat;
144113
itemType = kSecItemTypeCertificate;

0 commit comments

Comments
 (0)