Skip to content
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

Finish moving DSAOpenSsl to EVP_PKEY for the shim boundary. #55787

Closed
wants to merge 6 commits into from
Closed
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 @@ -13,6 +13,34 @@ namespace Internal.Cryptography
//
internal static partial class AsymmetricAlgorithmHelpers
{
internal static bool ValidateRfc3279DerSequence(ReadOnlySpan<byte> signature)
{
try
{
AsnValueReader reader = new AsnValueReader(signature, AsnEncodingRules.DER);
AsnValueReader payload = reader.ReadSequence();

if (reader.HasData)
{
return false;
}

payload.ReadIntegerBytes();
payload.ReadIntegerBytes();

if (payload.HasData)
{
return false;
}

return true;
}
catch (AsnContentException)
{
return false;
}
}

/// <summary>
/// Convert Ieee1363 format of (r, s) to Der format
/// </summary>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,40 @@ internal static ECParameters GetECCurveParameters(
key.DangerousRelease();
}
}

/// <summary>
/// Return the maximum value in the array; assumes non-negative values.
/// </summary>
private static int GetMax(int[] values)
{
int max = 0;

foreach (var i in values)
{
Debug.Assert(i >= 0);
if (i > max)
max = i;
}

return max;
}

/// <summary>
/// Return the maximum value in the array; assumes non-negative values.
/// </summary>
private static int GetMax(int value1, int value2)
{
Debug.Assert(value1 >= 0);
Debug.Assert(value2 >= 0);
return (value1 > value2 ? value1 : value2);
}

/// <summary>
/// Return the maximum value in the array; assumes non-negative values.
/// </summary>
private static int GetMax(int value1, int value2, int value3)
{
return GetMax(GetMax(value1, value2), value3);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;
Expand All @@ -9,11 +11,89 @@ internal static partial class Interop
{
internal static partial class Crypto
{
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeyGetDsa")]
internal static extern SafeDsaHandle EvpPkeyGetDsa(SafeEvpPKeyHandle pkey);
[DllImport(Libraries.CryptoNative)]
private static extern SafeEvpPKeyHandle CryptoNative_EvpPKeyCreateDsa(IntPtr dsa);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeySetDsa")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool EvpPkeySetDsa(SafeEvpPKeyHandle pkey, SafeDsaHandle key);
internal static SafeEvpPKeyHandle EvpPKeyCreateDsa(IntPtr dsa)
{
Debug.Assert(dsa != IntPtr.Zero);

SafeEvpPKeyHandle pkey = CryptoNative_EvpPKeyCreateDsa(dsa);

if (pkey.IsInvalid)
{
pkey.Dispose();
throw CreateOpenSslCryptographicException();
}

return pkey;
}

[DllImport(Libraries.CryptoNative)]
private static extern SafeEvpPKeyHandle CryptoNative_DsaGenerateKey(int keySize);

internal static SafeEvpPKeyHandle DsaGenerateKey(int keySize)
{
SafeEvpPKeyHandle handle = CryptoNative_DsaGenerateKey(keySize);

if (handle.IsInvalid)
{
handle.Dispose();
throw CreateOpenSslCryptographicException();
}

return handle;
}

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DsaSizeQ")]
private static extern int DsaSizeQ(SafeEvpPKeyHandle dsa);

/// <summary>
/// Return the size of the 'r' or 's' signature fields in bytes.
/// </summary>
internal static int DsaSignatureFieldSize(SafeEvpPKeyHandle dsa)
{
int size = DsaSizeQ(dsa);
Debug.Assert(size * 2 < EvpPKeySize(dsa));
return size;
}

[DllImport(Libraries.CryptoNative)]
private static extern unsafe int CryptoNative_DsaSignHash(
SafeEvpPKeyHandle key,
byte* hash,
int hashLen,
byte* destination,
int destinationLen);

internal static int DsaSignHash(
SafeEvpPKeyHandle key,
ReadOnlySpan<byte> hash,
Span<byte> destination)
{
int written;

unsafe
{
fixed (byte* hashPtr = hash)
fixed (byte* destPtr = destination)
{
written = CryptoNative_DsaSignHash(
key,
hashPtr,
hash.Length,
destPtr,
destination.Length);
}
}

if (written < 0)
{
Debug.Assert(written == -1);
throw CreateOpenSslCryptographicException();
}

return written;
}
}
}
Loading