Skip to content

Commit 7fddf88

Browse files
steveharterkrwq
authored andcommitted
Merged PR 38843: [release/7.0] Fix unbound MAC work in GetCertContentType
Fix unbound MAC work in GetCertContentType
2 parents b9c54e2 + 1270b90 commit 7fddf88

File tree

4 files changed

+58
-38
lines changed

4 files changed

+58
-38
lines changed

src/libraries/System.Security.Cryptography.X509Certificates/tests/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("2233907", ce.Message);
52+
}, options).Dispose();
53+
}
2554
}
2655
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,6 +3372,26 @@ internal static DSAParameters GetDSA1024Params()
33723372
"04020105000420AD0EB570ACFB8357A8E99B17672353CFBA69C76FFE5B6BC113" +
33733373
"05577F12AE24040408D04E60444B79672302030927C1";
33743374

3375+
internal static readonly byte[] Pkcs12NoPassword100MRounds = Convert.FromBase64String(
3376+
"MIIDygIBAzCCA4QGCSqGSIb3DQEHAaCCA3UEggNxMIIDbTCCA2kGCSqGSIb3DQEHBqCCA1owggNW" +
3377+
"AgEAMIIDTwYJKoZIhvcNAQcBMF4GCSqGSIb3DQEFDTBRMDAGCSqGSIb3DQEFDDAjBBCNparJkj/3" +
3378+
"Uk8N7n0KCMeQAgEBMAwGCCqGSIb3DQILBQAwHQYJYIZIAWUDBAEqBBAcqpBrSDFcXYAWVWKcsEi9" +
3379+
"gIIC4P/ANdPYWI1vBH1U5sZGMIwLjY96pYaBelyZd0ZfKA8QfGHVNP9+E9hplBKGvRfIMiqmFutj" +
3380+
"RO4v7Ls8HZEk0hwBt9+6zXPWDJLxBDfSMHUd08+ZAH1yzEqq8aBMyIRVHOQkJFuFuCQJ9Ke5HzVi" +
3381+
"39S1rgHpnKYFvy+xZAhgI9OO1YxuFt4P9nhlEV/JCoyEQ/2iY99kKc3z7ArrV7BBFhfYGKhWQCBu" +
3382+
"kAmNBKweRldNWgDuW21WJEl5sByOmyDwpiK55Zxy1K1aIY8DYJTtIzzcX4CILaj6tClMH1G9w4jW" +
3383+
"BkQI2CG4vCsMl/28BbIP9EyH2C+gBAxvc1N32y3NSvO0/GPVenmQFF9KBMc4FVy4Z21syMKzUkBi" +
3384+
"PtIbDkcQbGAfyPgFk4SXCgn8OpIIvOOGI50/r+Hj14qex9VIrlwAAWCH8Y+YjwqFAQJYHQpb47zp" +
3385+
"B1fTwJFOrsXrBgLUzJLZKLR43yW2E9u6b8RsTuFHjh985naCHLuWPYOXS1zduBpHKpwoPUyCwD2r" +
3386+
"DAokCvA7RCsSXroUkpJarN4CAqsEB8COnzV1Dl2xcAYMerJxrTCKX6WIQUYo0/qeCoqTT38lDAlE" +
3387+
"7Ydjyx12iVM6eWejAdjORvlVtCQQtCxz8fZpdFGbMP8rf35A8hu++e4u0CLHnhTx38zPIm6H6YfN" +
3388+
"qj5h1Kz0xLzqnRfa7EGfDEERSHOy/DqNY4nUNG2DTjGOHy1QJelToG7Vo2L7CCZV+leX0nwLNExf" +
3389+
"hKEp+uQCiYSJe9iDm9fS9VymED79OJbr2bxdq3MggEGksLZv/H0ZT8Wsue0vq9jQ6J6YIEM+DKYr" +
3390+
"Zt2l4WgTBEKbpqmRvOqYRh9O8Sp+3IRNPzMC2ehzlYXqoPbtG4vxpoRsAMCM/W2x61jbsBSaNSFA" +
3391+
"eaUwcnKswRg30UonHUAIOJkqtadI57WE/Rat5eHVyya9f7ZN8bTFZjx0BQs6Bo8PK9yfqoidSN8w" +
3392+
"PTAfMAcGBSsOAwIaBBTt8zpgzygINykjoAwr2GKEywYFwgQUA+L1vfCVASwiE++gTfRgIScMGycC" +
3393+
"BAX14QA=");
3394+
33753395
internal const string Pkcs12OpenSslOneCertDefaultEmptyPassword =
33763396
"308209CF0201033082098506092A864886F70D010701A0820976048209723082" +
33773397
"096E308203E206092A864886F70D010706A08203D3308203CF020100308203C8" +

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public X509ContentType GetCertContentType(ReadOnlySpan<byte> rawData)
119119

120120
X509ContentType contentType = Interop.AppleCrypto.X509GetContentType(rawData);
121121

122-
// Apple doesn't seem to recognize PFX files with no MAC, so try a quick maybe-it's-a-PFX test
122+
// Apple's native check can't check for PKCS12, so do a quick decode test to see if it is PKCS12 / PFX.
123123
if (contentType == X509ContentType.Unknown)
124124
{
125125
try
@@ -128,9 +128,11 @@ public X509ContentType GetCertContentType(ReadOnlySpan<byte> rawData)
128128
{
129129
fixed (byte* pin = rawData)
130130
{
131+
AsnValueReader reader = new AsnValueReader(rawData, AsnEncodingRules.BER);
132+
131133
using (var manager = new PointerMemoryManager<byte>(pin, rawData.Length))
132134
{
133-
PfxAsn.Decode(manager.Memory, AsnEncodingRules.BER);
135+
PfxAsn.Decode(ref reader, manager.Memory, out _);
134136
}
135137

136138
contentType = X509ContentType.Pkcs12;

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
@@ -106,14 +106,18 @@ PAL_X509ContentType AppleCryptoNative_X509GetContentType(uint8_t* pbData, int32_
106106
// The sniffing order is:
107107
// * X509 DER
108108
// * PKCS7 PEM/DER
109-
// * PKCS12 DER (or PEM if Apple has non-standard support for that)
110109
// * X509 PEM or PEM aggregate (or DER, but that already matched)
111110
//
112111
// If the X509 PEM check is done first SecItemImport will erroneously match
113112
// some PKCS#7 blobs and say they were certificates.
114113
//
115114
// Likewise, if the X509 DER check isn't done first, Apple will report it as
116115
// being a PKCS#7.
116+
//
117+
// This does not attempt to open a PFX / PKCS12 as Apple does not provide
118+
// a suitable API to determine if it is PKCS12 without doing potentially
119+
// unbound MAC / KDF work. Instead, let that return Unknown and let the managed
120+
// decoding do the check.
117121
SecCertificateRef certref = SecCertificateCreateWithData(NULL, cfData);
118122

119123
if (certref != NULL)
@@ -140,41 +144,6 @@ PAL_X509ContentType AppleCryptoNative_X509GetContentType(uint8_t* pbData, int32_
140144
}
141145
}
142146

143-
dataFormat = kSecFormatPKCS12;
144-
actualFormat = dataFormat;
145-
itemType = kSecItemTypeAggregate;
146-
actualType = itemType;
147-
148-
osStatus = SecItemImport(cfData, NULL, &actualFormat, &actualType, 0, NULL, NULL, NULL);
149-
150-
if (osStatus == errSecPassphraseRequired)
151-
{
152-
dataFormat = kSecFormatPKCS12;
153-
actualFormat = dataFormat;
154-
itemType = kSecItemTypeAggregate;
155-
actualType = itemType;
156-
157-
SecItemImportExportKeyParameters importParams;
158-
memset(&importParams, 0, sizeof(SecItemImportExportKeyParameters));
159-
160-
importParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
161-
importParams.passphrase = CFSTR("");
162-
163-
osStatus = SecItemImport(cfData, NULL, &actualFormat, &actualType, 0, &importParams, NULL, NULL);
164-
165-
CFRelease(importParams.passphrase);
166-
importParams.passphrase = NULL;
167-
}
168-
169-
if (osStatus == noErr || osStatus == errSecPkcs12VerifyFailure)
170-
{
171-
if (actualType == itemType && actualFormat == dataFormat)
172-
{
173-
CFRelease(cfData);
174-
return PAL_Pkcs12;
175-
}
176-
}
177-
178147
dataFormat = kSecFormatX509Cert;
179148
actualFormat = dataFormat;
180149
itemType = kSecItemTypeCertificate;

0 commit comments

Comments
 (0)