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 @@ -214,14 +214,6 @@ internal static int EvpPKeyGetEcKeySize(SafeEvpPKeyHandle pkey)
return CryptoNative_EvpPKeyGetEcKeySize(pkey);
}

[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_GetECKeyParameters(
SafeEcKeyHandle key,
[MarshalAs(UnmanagedType.Bool)] bool includePrivate,
out SafeBignumHandle qx_bn, out int x_cb,
out SafeBignumHandle qy_bn, out int y_cb,
out IntPtr d_bn_not_owned, out int d_cb);

[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_EvpPKeyGetEcKeyParameters(
SafeEvpPKeyHandle key,
Expand All @@ -230,57 +222,6 @@ private static partial int CryptoNative_EvpPKeyGetEcKeyParameters(
out SafeBignumHandle qy_bn, out int y_cb,
out SafeBignumHandle d_bn, out int d_cb);

internal static ECParameters GetECKeyParameters(
SafeEcKeyHandle key,
bool includePrivate)
{
SafeBignumHandle qx_bn, qy_bn, d_bn;
IntPtr d_bn_not_owned;
int qx_cb, qy_cb, d_cb;

bool refAdded = false;
try
{
key.DangerousAddRef(ref refAdded); // Protect access to d_bn_not_owned
int rc = CryptoNative_GetECKeyParameters(
key,
includePrivate,
out qx_bn, out qx_cb,
out qy_bn, out qy_cb,
out d_bn_not_owned, out d_cb);

using (qx_bn)
using (qy_bn)
{
if (rc == -1)
{
throw new CryptographicException(SR.Cryptography_CSP_NoPrivateKey);
}
else if (rc != 1)
{
throw Interop.Crypto.CreateOpenSslCryptographicException();
}

using (d_bn = new SafeBignumHandle(d_bn_not_owned, false))
{
// Match Windows semantics where qx, qy, and d have same length
int keySizeBits = EcKeyGetSize(key);
int expectedSize = (keySizeBits + 7) / 8;
return GetEcParameters(
expectedSize,
qx_bn, qx_cb,
qy_bn, qy_cb,
d_bn, d_cb);
}
}
}
finally
{
if (refAdded)
key.DangerousRelease();
}
}

internal static ECParameters EvpPKeyGetEcKeyParameters(
SafeEvpPKeyHandle key,
bool includePrivate)
Expand Down Expand Up @@ -346,23 +287,6 @@ private static ECParameters GetEcParameters(
return parameters;
}

[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_GetECCurveParameters(
SafeEcKeyHandle key,
[MarshalAs(UnmanagedType.Bool)] bool includePrivate,
out ECCurve.ECCurveType curveType,
out SafeBignumHandle qx, out int x_cb,
out SafeBignumHandle qy, out int y_cb,
out IntPtr d_bn_not_owned, out int d_cb,
out SafeBignumHandle p, out int P_cb,
out SafeBignumHandle a, out int A_cb,
out SafeBignumHandle b, out int B_cb,
out SafeBignumHandle gx, out int Gx_cb,
out SafeBignumHandle gy, out int Gy_cb,
out SafeBignumHandle order, out int order_cb,
out SafeBignumHandle cofactor, out int cofactor_cb,
out SafeBignumHandle seed, out int seed_cb);

[LibraryImport(Libraries.CryptoNative)]
private static unsafe partial int CryptoNative_EvpPKeyGetEcCurveParameters(
SafeEvpPKeyHandle key,
Expand All @@ -380,80 +304,6 @@ private static unsafe partial int CryptoNative_EvpPKeyGetEcCurveParameters(
out SafeBignumHandle cofactor, out int cofactor_cb,
out SafeBignumHandle seed, out int seed_cb);

internal static ECParameters GetECCurveParameters(
SafeEcKeyHandle key,
bool includePrivate)
{
ECCurve.ECCurveType curveType;
SafeBignumHandle qx_bn, qy_bn, p_bn, a_bn, b_bn, gx_bn, gy_bn, order_bn, cofactor_bn, seed_bn;
IntPtr d_bn_not_owned;
int qx_cb, qy_cb, p_cb, a_cb, b_cb, gx_cb, gy_cb, order_cb, cofactor_cb, seed_cb, d_cb;

bool refAdded = false;
try
{
key.DangerousAddRef(ref refAdded); // Protect access to d_bn_not_owned
int rc = CryptoNative_GetECCurveParameters(
key,
includePrivate,
out curveType,
out qx_bn, out qx_cb,
out qy_bn, out qy_cb,
out d_bn_not_owned, out d_cb,
out p_bn, out p_cb,
out a_bn, out a_cb,
out b_bn, out b_cb,
out gx_bn, out gx_cb,
out gy_bn, out gy_cb,
out order_bn, out order_cb,
out cofactor_bn, out cofactor_cb,
out seed_bn, out seed_cb);

using (qx_bn)
using (qy_bn)
using (p_bn)
using (a_bn)
using (b_bn)
using (gx_bn)
using (gy_bn)
using (order_bn)
using (cofactor_bn)
using (seed_bn)
{
if (rc == -1)
{
throw new CryptographicException(SR.Cryptography_CSP_NoPrivateKey);
}
else if (rc != 1)
{
throw Interop.Crypto.CreateOpenSslCryptographicException();
}

using (var d_h = new SafeBignumHandle(d_bn_not_owned, false))
{
return GetEcCurveParameters(
curveType,
qx_bn, qx_cb,
qy_bn, qy_cb,
d_h, d_cb,
p_bn, p_cb,
a_bn, a_cb,
b_bn, b_cb,
gx_bn, gx_cb,
gy_bn, gy_cb,
order_bn, order_cb,
cofactor_bn, cofactor_cb,
seed_bn, seed_cb);
}
}
}
finally
{
if (refAdded)
key.DangerousRelease();
}
}

internal static unsafe ECParameters EvpPKeyGetEcCurveParameters(
SafeEvpPKeyHandle key,
bool includePrivate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,11 @@

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Crypto
{
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EcKeyCreateByOid", StringMarshalling = StringMarshalling.Utf8)]
private static partial SafeEcKeyHandle CryptoNative_EcKeyCreateByOid(string oid);
internal static SafeEcKeyHandle? EcKeyCreateByOid(string oid)
{
SafeEcKeyHandle handle = CryptoNative_EcKeyCreateByOid(oid);
if (handle == null || handle.IsInvalid)
{
ErrClearError();
}

return handle;
}

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EcKeyDestroy")]
internal static partial void EcKeyDestroy(IntPtr a);

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EcKeyGenerateKey")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool EcKeyGenerateKey(SafeEcKeyHandle eckey);

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EcKeyUpRef")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool EcKeyUpRef(IntPtr r);

[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_EcKeyGetSize(SafeEcKeyHandle ecKey, out int keySize);
internal static int EcKeyGetSize(SafeEcKeyHandle key)
{
int keySize;
int rc = CryptoNative_EcKeyGetSize(key, out keySize);
if (rc == 1)
{
return keySize;
}

throw Interop.Crypto.CreateOpenSslCryptographicException();
}

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EcKeyGetCurveName2")]
private static partial int CryptoNative_EcKeyGetCurveName(SafeEcKeyHandle ecKey, out int nid);

internal static string EcKeyGetCurveName(SafeEcKeyHandle key)
{
int rc = CryptoNative_EcKeyGetCurveName(key, out int nidCurveName);
if (rc == 1)
{
return CurveNidToOidValue(nidCurveName);
}

throw Interop.Crypto.CreateOpenSslCryptographicException();
}

internal static bool EcKeyHasCurveName(SafeEcKeyHandle key)
{
int rc = CryptoNative_EcKeyGetCurveName(key, out int nidCurveName);
if (rc == 1)
{
// Key is invalid or doesn't have a curve
return (nidCurveName != Interop.Crypto.NID_undef);
}

throw Interop.Crypto.CreateOpenSslCryptographicException();
}

internal static string CurveNidToOidValue(int nidCurveName)
{
if (nidCurveName == Interop.Crypto.NID_undef)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;
Expand All @@ -9,18 +10,19 @@ internal static partial class Interop
{
internal static partial class Crypto
{
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeyGetEcKey")]
internal static partial SafeEcKeyHandle EvpPkeyGetEcKey(SafeEvpPKeyHandle pkey);

[LibraryImport(Libraries.CryptoNative)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool CryptoNative_EvpPkeySetEcKey(SafeEvpPKeyHandle pkey, SafeEcKeyHandle key);
private static partial SafeEvpPKeyHandle CryptoNative_CreateEvpPkeyFromEcKey(IntPtr ecKey, out int keySize);

// Calls EVP_PKEY_set1_EC_KEY therefore the key will be duplicated
internal static SafeEvpPKeyHandle CreateEvpPkeyFromEcKey(SafeEcKeyHandle key)
/// <summary>
/// Creates a new EVP_PKEY from a raw EC_KEY pointer (IntPtr).
/// The EC_KEY is duplicated (up-ref'd) so the caller retains ownership.
/// Also returns the EC key size. Returns NULL (invalid handle) on failure.
/// </summary>
internal static SafeEvpPKeyHandle CreateEvpPkeyFromEcKey(IntPtr ecKeyHandle, out int keySize)
{
SafeEvpPKeyHandle pkey = Interop.Crypto.EvpPkeyCreate();
if (!CryptoNative_EvpPkeySetEcKey(pkey, key))
SafeEvpPKeyHandle pkey = CryptoNative_CreateEvpPkeyFromEcKey(ecKeyHandle, out keySize);

if (pkey.IsInvalid)
{
pkey.Dispose();
throw Interop.Crypto.CreateOpenSslCryptographicException();
Expand Down

This file was deleted.

Loading
Loading