Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,29 @@ public void TestNamedImportValidationNegative()
}
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
[PlatformSpecific(TestPlatforms.Windows | TestPlatforms.Linux)]
public void ImportParameters_MismatchedKeyPair_Throws(bool explicitCurve)
{
if (explicitCurve)
{
SkipTestException.ThrowUnless(ECDiffieHellmanFactory.ExplicitCurvesSupported);
}

ECParameters parameters = explicitCurve ?
EccTestData.GetNistP256ReferenceKeyExplicit() :
EccTestData.GetNistP256ReferenceKey();

parameters.D[0] ^= 1;

using (ECDiffieHellman ec = ECDiffieHellmanFactory.Create())
{
Assert.ThrowsAny<CryptographicException>(() => ec.ImportParameters(parameters));
}
}

[Fact]
public void TestGeneralExportWithExplicitParameters()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,29 @@ public void TestNamedImportValidationNegative()
}
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
[PlatformSpecific(TestPlatforms.Windows | TestPlatforms.Linux)]
public void ImportParameters_MismatchedKeyPair_Throws(bool explicitCurve)
{
if (explicitCurve)
{
SkipTestException.ThrowUnless(ECExplicitCurvesSupported);
}

ECParameters parameters = explicitCurve ?
EccTestData.GetNistP256ReferenceKeyExplicit() :
EccTestData.GetNistP256ReferenceKey();

parameters.D[0] ^= 1;

using (ECDsa ec = ECDsaFactory.Create())
{
Assert.ThrowsAny<CryptographicException>(() => ec.ImportParameters(parameters));
}
}

[ConditionalFact]
public void TestGeneralExportWithExplicitParameters()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ static int PushPublicKeyParam(

return 1;
}

static int CheckEcKey(EVP_PKEY* key, int hasPrivateKey)
{
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(key, NULL);
if (ctx == NULL)
return 0;

int ret = hasPrivateKey ? EVP_PKEY_check(ctx) : EVP_PKEY_public_check(ctx);
EVP_PKEY_CTX_free(ctx);
return ret == 1;
}
#endif

static ECCurveType MethodToCurveType(const EC_METHOD* method)
Expand Down Expand Up @@ -1835,6 +1846,9 @@ static int32_t EvpPKeyCreateByEcParameters(
goto error;
}

if (!CheckEcKey(*pkey, hasPrivateKey))
goto error;

if (keySize != NULL)
{
*keySize = CryptoNative_EvpPKeyGetEcKeySize(*pkey);
Expand Down Expand Up @@ -2178,6 +2192,9 @@ static int32_t EvpPKeyCreateByEcExplicitParameters(
int selection = hasPrivateKey ? EVP_PKEY_KEYPAIR : EVP_PKEY_PUBLIC_KEY;
if (EVP_PKEY_fromdata(ctx, pkey, selection, params) != 1)
goto error;

if (!CheckEcKey(*pkey, hasPrivateKey))
goto error;
}

if (keySize != NULL)
Expand Down
Loading