diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ASN1.Print.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ASN1.Print.cs index c5589051c40aa..71168920c0508 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ASN1.Print.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ASN1.Print.cs @@ -28,7 +28,7 @@ internal static unsafe string DerStringToManagedString(byte[] derString) byte[] utf8Bytes; using (asn1String) - using (SafeBioHandle bio = libcrypto.BIO_new(libcrypto.BIO_s_mem())) + using (SafeBioHandle bio = CreateMemoryBio()) { int len = Asn1StringPrintEx(bio, asn1String, Asn1StringPrintFlags.ASN1_STRFLGS_UTF8_CONVERT); @@ -37,10 +37,10 @@ internal static unsafe string DerStringToManagedString(byte[] derString) throw Crypto.CreateOpenSslCryptographicException(); } - int bioSize = libcrypto.GetMemoryBioSize(bio); + int bioSize = GetMemoryBioSize(bio); utf8Bytes = new byte[bioSize + 1]; - int read = libcrypto.BIO_read(bio, utf8Bytes, utf8Bytes.Length); + int read = BioRead(bio, utf8Bytes, utf8Bytes.Length); if (read < 0) { diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.BIO.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.BIO.cs new file mode 100644 index 0000000000000..066a40df4539e --- /dev/null +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.BIO.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Crypto + { + [DllImport(Libraries.CryptoNative)] + internal static extern SafeBioHandle CreateMemoryBio(); + + [DllImport(Libraries.CryptoNative)] + internal static extern SafeBioHandle BioNewFile(string filename, string mode); + + [DllImport(Libraries.CryptoNative)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool BioDestroy(IntPtr a); + + [DllImport(Libraries.CryptoNative, CharSet = CharSet.Ansi)] + internal static extern int BioGets(SafeBioHandle b, [Out] StringBuilder buf, int size); + + [DllImport(Libraries.CryptoNative)] + internal static extern int BioRead(SafeBioHandle b, byte[] data, int len); + + [DllImport(Libraries.CryptoNative)] + internal static extern int BioWrite(SafeBioHandle b, byte[] data, int len); + + [DllImport(Libraries.CryptoNative)] + internal static extern int GetMemoryBioSize(SafeBioHandle bio); + } +} diff --git a/src/libraries/Common/src/Interop/Unix/libcrypto/Interop.Bignum.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs similarity index 64% rename from src/libraries/Common/src/Interop/Unix/libcrypto/Interop.Bignum.cs rename to src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs index 7ff2db31bac2e..0935a1b7187bf 100644 --- a/src/libraries/Common/src/Interop/Unix/libcrypto/Interop.Bignum.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Bignum.cs @@ -3,33 +3,23 @@ using System; using System.Runtime.InteropServices; - using Microsoft.Win32.SafeHandles; internal static partial class Interop { - internal static partial class libcrypto + internal static partial class Crypto { - [DllImport(Libraries.LibCrypto)] - internal static extern void BN_clear_free(IntPtr a); - - [DllImport(Libraries.LibCrypto)] - private static extern IntPtr BN_bin2bn(byte[] s, int len, IntPtr zero); + [DllImport(Libraries.CryptoNative)] + internal static extern void BigNumDestroy(IntPtr a); - [DllImport(Libraries.LibCrypto)] - private static extern unsafe int BN_bn2bin(SafeBignumHandle a, byte* to); + [DllImport(Libraries.CryptoNative)] + private static extern IntPtr BigNumFromBinary(byte[] s, int len); - [DllImport(Libraries.LibCrypto)] - private static extern int BN_num_bits(SafeBignumHandle a); + [DllImport(Libraries.CryptoNative)] + private static extern unsafe int BigNumToBinary(SafeBignumHandle a, byte* to); - /// - /// Returns the number of bytes needed to export a BIGNUM. - /// - /// This is a macro in bn.h, expanded here. - private static int BN_num_bytes(SafeBignumHandle a) - { - return (BN_num_bits(a) + 7) / 8; - } + [DllImport(Libraries.CryptoNative)] + private static extern int GetBigNumBytes(SafeBignumHandle a); internal static IntPtr CreateBignumPtr(byte[] bigEndianValue) { @@ -38,8 +28,7 @@ internal static IntPtr CreateBignumPtr(byte[] bigEndianValue) return IntPtr.Zero; } - IntPtr handle = BN_bin2bn(bigEndianValue, bigEndianValue.Length, IntPtr.Zero); - return handle; + return BigNumFromBinary(bigEndianValue, bigEndianValue.Length); } internal static SafeBignumHandle CreateBignum(byte[] bigEndianValue) @@ -48,7 +37,7 @@ internal static SafeBignumHandle CreateBignum(byte[] bigEndianValue) return new SafeBignumHandle(handle, true); } - private static byte[] ExtractBignum(IntPtr bignum, int targetSize) + internal static byte[] ExtractBignum(IntPtr bignum, int targetSize) { // Given that the only reference held to bignum is an IntPtr, create an unowned SafeHandle // to ensure that we don't destroy the key after extraction. @@ -65,7 +54,7 @@ private static unsafe byte[] ExtractBignum(SafeBignumHandle bignum, int targetSi return null; } - int compactSize = BN_num_bytes(bignum); + int compactSize = GetBigNumBytes(bignum); if (targetSize < compactSize) { @@ -85,7 +74,7 @@ private static unsafe byte[] ExtractBignum(SafeBignumHandle bignum, int targetSi fixed (byte* to = buf) { byte* start = to + offset; - BN_bn2bin(bignum, start); + BigNumToBinary(bignum, start); } return buf; diff --git a/src/libraries/Common/src/Interop/Unix/libcrypto/Interop.BIO.cs b/src/libraries/Common/src/Interop/Unix/libcrypto/Interop.BIO.cs deleted file mode 100644 index 2cb5dd35b5be3..0000000000000 --- a/src/libraries/Common/src/Interop/Unix/libcrypto/Interop.BIO.cs +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Runtime.InteropServices; -using System.Text; - -using Microsoft.Win32.SafeHandles; - -using NativeLong=System.IntPtr; - -internal static partial class Interop -{ - internal static partial class libcrypto - { - private const int BIO_CTRL_INFO = 3; - - [DllImport(Libraries.LibCrypto)] - internal static extern SafeBioHandle BIO_new(IntPtr type); - - [DllImport(Libraries.LibCrypto)] - internal static extern SafeBioHandle BIO_new_file(string filename, string mode); - - [DllImport(Libraries.LibCrypto)] - internal static extern IntPtr BIO_s_mem(); - - [DllImport(Libraries.LibCrypto)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool BIO_free(IntPtr a); - - [DllImport(Libraries.LibCrypto, CharSet = CharSet.Ansi)] - internal static extern int BIO_gets(SafeBioHandle b, [Out] StringBuilder buf, int size); - - [DllImport(Libraries.LibCrypto)] - internal static extern int BIO_read(SafeBioHandle b, byte[] data, int len); - - [DllImport(Libraries.LibCrypto)] - internal static extern int BIO_write(SafeBioHandle b, byte[] data, int len); - - [DllImport(Libraries.LibCrypto)] - private static extern NativeLong BIO_ctrl(SafeBioHandle bio, int cmd, NativeLong larg, IntPtr parg); - - internal static int GetMemoryBioSize(SafeBioHandle bio) - { - // This method is equivalent to BIO_get_mem_data(bio, NULL), except not a macro, - // and doesn't expose the NULL. - return BIO_ctrl(bio, BIO_CTRL_INFO, IntPtr.Zero, IntPtr.Zero).ToInt32(); - } - } -} diff --git a/src/libraries/Common/src/Interop/Unix/libcrypto/Interop.Rsa.cs b/src/libraries/Common/src/Interop/Unix/libcrypto/Interop.Rsa.cs index f729e4cbf0a83..00265194ce2c1 100644 --- a/src/libraries/Common/src/Interop/Unix/libcrypto/Interop.Rsa.cs +++ b/src/libraries/Common/src/Interop/Unix/libcrypto/Interop.Rsa.cs @@ -72,18 +72,18 @@ internal static unsafe RSAParameters ExportRsaParameters(SafeRsaHandle key, bool rsaParameters = new RSAParameters { - Modulus = ExtractBignum(rsaStructure->n, modulusSize), - Exponent = ExtractBignum(rsaStructure->e, 0), + Modulus = Crypto.ExtractBignum(rsaStructure->n, modulusSize), + Exponent = Crypto.ExtractBignum(rsaStructure->e, 0), }; if (includePrivateParameters) { - rsaParameters.D = ExtractBignum(rsaStructure->d, modulusSize); - rsaParameters.P = ExtractBignum(rsaStructure->p, halfModulus); - rsaParameters.DP = ExtractBignum(rsaStructure->dmp1, halfModulus); - rsaParameters.Q = ExtractBignum(rsaStructure->q, halfModulus); - rsaParameters.DQ = ExtractBignum(rsaStructure->dmq1, halfModulus); - rsaParameters.InverseQ = ExtractBignum(rsaStructure->iqmp, halfModulus); + rsaParameters.D = Crypto.ExtractBignum(rsaStructure->d, modulusSize); + rsaParameters.P = Crypto.ExtractBignum(rsaStructure->p, halfModulus); + rsaParameters.DP = Crypto.ExtractBignum(rsaStructure->dmp1, halfModulus); + rsaParameters.Q = Crypto.ExtractBignum(rsaStructure->q, halfModulus); + rsaParameters.DQ = Crypto.ExtractBignum(rsaStructure->dmq1, halfModulus); + rsaParameters.InverseQ = Crypto.ExtractBignum(rsaStructure->iqmp, halfModulus); } } finally diff --git a/src/libraries/Common/src/Interop/Unix/libssl/Interop.SafeSslHandle.cs b/src/libraries/Common/src/Interop/Unix/libssl/Interop.SafeSslHandle.cs index 6daf0e1dd429d..52c3a50c5bf06 100644 --- a/src/libraries/Common/src/Interop/Unix/libssl/Interop.SafeSslHandle.cs +++ b/src/libraries/Common/src/Interop/Unix/libssl/Interop.SafeSslHandle.cs @@ -59,15 +59,13 @@ public SafeBioHandle OutputBio public static SafeSslHandle Create(SafeSslContextHandle context, bool isServer) { - IntPtr memMethod = libcrypto.BIO_s_mem(); - - SafeBioHandle readBio = libcrypto.BIO_new(memMethod); + SafeBioHandle readBio = Crypto.CreateMemoryBio(); if (readBio.IsInvalid) { return new SafeSslHandle(); } - SafeBioHandle writeBio = libcrypto.BIO_new(memMethod); + SafeBioHandle writeBio = Crypto.CreateMemoryBio(); if (writeBio.IsInvalid) { readBio.Dispose(); diff --git a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeBignumHandle.Unix.cs b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeBignumHandle.Unix.cs index 12328635c58fc..3db8bf302f11a 100644 --- a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeBignumHandle.Unix.cs +++ b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeBignumHandle.Unix.cs @@ -17,7 +17,7 @@ internal SafeBignumHandle(IntPtr handle, bool ownsHandle) protected override bool ReleaseHandle() { - Interop.libcrypto.BN_clear_free(handle); + Interop.Crypto.BigNumDestroy(handle); SetHandle(IntPtr.Zero); return true; } diff --git a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeBioHandle.Unix.cs b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeBioHandle.Unix.cs index caf0cb7d0144c..303622bd21448 100644 --- a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeBioHandle.Unix.cs +++ b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeBioHandle.Unix.cs @@ -17,7 +17,8 @@ private SafeBioHandle() : protected override bool ReleaseHandle() { - Interop.libcrypto.BIO_free(handle); + Interop.Crypto.BioDestroy(handle); + SetHandle(IntPtr.Zero); return true; } diff --git a/src/libraries/Native/System.Security.Cryptography.Native/CMakeLists.txt b/src/libraries/Native/System.Security.Cryptography.Native/CMakeLists.txt index cd9fcf6cb5f0d..89b771a6efca3 100644 --- a/src/libraries/Native/System.Security.Cryptography.Native/CMakeLists.txt +++ b/src/libraries/Native/System.Security.Cryptography.Native/CMakeLists.txt @@ -21,6 +21,8 @@ set(NATIVECRYPTO_SOURCES openssl.c pal_asn1.cpp pal_asn1_print.cpp + pal_bignum.cpp + pal_bio.cpp pal_err.cpp pal_evp.cpp pal_evp_cipher.cpp diff --git a/src/libraries/Native/System.Security.Cryptography.Native/pal_bignum.cpp b/src/libraries/Native/System.Security.Cryptography.Native/pal_bignum.cpp new file mode 100644 index 0000000000000..aa09e6caa2cd3 --- /dev/null +++ b/src/libraries/Native/System.Security.Cryptography.Native/pal_bignum.cpp @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +#include "pal_bignum.h" + +extern "C" void BigNumDestroy(BIGNUM* a) +{ + if (a != nullptr) + { + BN_clear_free(a); + } +} + +extern "C" BIGNUM* BigNumFromBinary(const unsigned char* s, int32_t len) +{ + if (!s || !len) + { + return nullptr; + } + + return BN_bin2bn(s, len, nullptr); +} + +extern "C" int32_t BigNumToBinary(const BIGNUM* a, unsigned char* to) +{ + if (!a || !to) + { + return 0; + } + + return BN_bn2bin(a, to); +} + +extern "C" int32_t GetBigNumBytes(const BIGNUM* a) +{ + if (!a) + { + return 0; + } + + return BN_num_bytes(a); +} diff --git a/src/libraries/Native/System.Security.Cryptography.Native/pal_bignum.h b/src/libraries/Native/System.Security.Cryptography.Native/pal_bignum.h new file mode 100644 index 0000000000000..bd07d17945ce3 --- /dev/null +++ b/src/libraries/Native/System.Security.Cryptography.Native/pal_bignum.h @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +#include "pal_types.h" + +#include + +/* +Cleans up and deletes an BIGNUM instance. + +Implemented by: +1) Calling BN_clear_free + +No-op if a is null. +The given BIGNUM pointer is invalid after this call. +Always succeeds. +*/ +extern "C" void BigNumDestroy(BIGNUM* a); + +/* +Shims the BN_bin2bn method. +*/ +extern "C" BIGNUM* BigNumFromBinary(const unsigned char* s, int32_t len); + +/* +Shims the BN_bn2bin method. +*/ +extern "C" int32_t BigNumToBinary(const BIGNUM* a, unsigned char* to); + +/* +Returns the number of bytes needed to export a BIGNUM. +*/ +extern "C" int32_t GetBigNumBytes(const BIGNUM* a); diff --git a/src/libraries/Native/System.Security.Cryptography.Native/pal_bio.cpp b/src/libraries/Native/System.Security.Cryptography.Native/pal_bio.cpp new file mode 100644 index 0000000000000..d553d5e0d99d8 --- /dev/null +++ b/src/libraries/Native/System.Security.Cryptography.Native/pal_bio.cpp @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +#include "pal_bio.h" + +#include + +extern "C" BIO* CreateMemoryBio() +{ + return BIO_new(BIO_s_mem()); +} + +extern "C" BIO* BioNewFile(const char* filename, const char* mode) +{ + return BIO_new_file(filename, mode); +} + +extern "C" int32_t BioDestroy(BIO* a) +{ + return BIO_free(a); +} + +extern "C" int32_t BioGets(BIO* b, char* buf, int32_t size) +{ + return BIO_gets(b, buf, size); +} + +extern "C" int32_t BioRead(BIO* b, void* buf, int32_t len) +{ + return BIO_read(b, buf, len); +} + +extern "C" int32_t BioWrite(BIO* b, const void* buf, int32_t len) +{ + return BIO_write(b, buf, len); +} + +extern "C" int32_t GetMemoryBioSize(BIO* bio) +{ + long ret = BIO_get_mem_data(bio, nullptr); + + // BIO_get_mem_data returns the memory size, which will always be + // an int32. + assert(ret <= INT32_MAX); + return static_cast(ret); +} diff --git a/src/libraries/Native/System.Security.Cryptography.Native/pal_bio.h b/src/libraries/Native/System.Security.Cryptography.Native/pal_bio.h new file mode 100644 index 0000000000000..87067a4347d5d --- /dev/null +++ b/src/libraries/Native/System.Security.Cryptography.Native/pal_bio.h @@ -0,0 +1,49 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +#include "pal_types.h" + +#include + +/* +Creates a new memory-backed BIO instance. +*/ +extern "C" BIO* CreateMemoryBio(); + +/* +Direct shim to BIO_new_file. +*/ +extern "C" BIO* BioNewFile(const char* filename, const char* mode); + +/* +Cleans up and deletes a BIO instance. + +Implemented by: +1) Calling BIO_free + +No-op if a is null. +The given BIO pointer is invalid after this call. +*/ +extern "C" int32_t BioDestroy(BIO* a); + +/* +Direct shim to BIO_gets. +*/ +extern "C" int32_t BioGets(BIO* b, char* buf, int32_t size); + +/* +Direct shim to BIO_read. +*/ +extern "C" int32_t BioRead(BIO* b, void* buf, int32_t len); + +/* +Direct shim to BIO_write. +*/ +extern "C" int32_t BioWrite(BIO* b, const void* buf, int32_t len); + +/* +Gets the size of data available in the BIO. + +Shims the BIO_get_mem_data method. +*/ +extern "C" int32_t GetMemoryBioSize(BIO* bio); diff --git a/src/libraries/System.Net.Security/src/System.Net.Security.csproj b/src/libraries/System.Net.Security/src/System.Net.Security.csproj index ad5de7449ff3f..e55f2d35ff4ef 100644 --- a/src/libraries/System.Net.Security/src/System.Net.Security.csproj +++ b/src/libraries/System.Net.Security/src/System.Net.Security.csproj @@ -204,15 +204,15 @@ Interop\Unix\libcrypto\Interop.X509Ext.cs - - Interop\Unix\libcrypto\Interop.BIO.cs - Interop\Unix\libcrypto\Interop.Pkcs7.cs Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ASN1.cs + + Common\Interop\Unix\System.Security.Cryptography.Native\Interop.BIO.cs + Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ERR.cs diff --git a/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/OpenSslAsnFormatter.cs b/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/OpenSslAsnFormatter.cs index aa17b1634ba91..ffb32def09df2 100644 --- a/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/OpenSslAsnFormatter.cs +++ b/src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/OpenSslAsnFormatter.cs @@ -35,7 +35,7 @@ protected override string FormatNative(Oid oid, byte[] rawData, bool multiLine) return null; } - using (SafeBioHandle bio = Interop.libcrypto.BIO_new(Interop.libcrypto.BIO_s_mem())) + using (SafeBioHandle bio = Interop.Crypto.CreateMemoryBio()) using (SafeX509ExtensionHandle x509Ext = Interop.libcrypto.X509_EXTENSION_create_by_OBJ(IntPtr.Zero, asnOid, false, octetString)) { if (bio.IsInvalid || x509Ext.IsInvalid) @@ -48,11 +48,11 @@ protected override string FormatNative(Oid oid, byte[] rawData, bool multiLine) return null; } - int printLen = Interop.libcrypto.GetMemoryBioSize(bio); + int printLen = Interop.Crypto.GetMemoryBioSize(bio); // Account for the null terminator that it'll want to write. StringBuilder builder = new StringBuilder(printLen + 1); - Interop.libcrypto.BIO_gets(bio, builder, builder.Capacity); + Interop.Crypto.BioGets(bio, builder, builder.Capacity); return builder.ToString(); } diff --git a/src/libraries/System.Security.Cryptography.Encoding/src/System.Security.Cryptography.Encoding.csproj b/src/libraries/System.Security.Cryptography.Encoding/src/System.Security.Cryptography.Encoding.csproj index 8be4cf334aeec..7cbf1e55f9b2b 100644 --- a/src/libraries/System.Security.Cryptography.Encoding/src/System.Security.Cryptography.Encoding.csproj +++ b/src/libraries/System.Security.Cryptography.Encoding/src/System.Security.Cryptography.Encoding.csproj @@ -61,15 +61,15 @@ Common\Interop\Unix\Interop.Libraries.cs - - Common\Interop\Unix\libcrypto\Interop.BIO.cs - Common\Interop\Unix\libcrypto\Interop.X509Ext.cs Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ASN1.cs + + Common\Interop\Unix\System.Security.Cryptography.Native\Interop.BIO.cs + Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ERR.cs diff --git a/src/libraries/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj b/src/libraries/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj index 612651e129ca5..dcc1ae285bd24 100644 --- a/src/libraries/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj +++ b/src/libraries/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj @@ -25,9 +25,6 @@ Common\Interop\Unix\Interop.Libraries.cs - - Common\Interop\Unix\libcrypto\Interop.Bignum.cs - Common\Interop\Unix\libcrypto\Interop.EvpPkey.cs @@ -49,6 +46,9 @@ Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ASN1.cs" + + Common\Interop\Unix\System.Security.Cryptography.Native\Interop.Bignum.cs" + Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ERR.cs" diff --git a/src/libraries/System.Security.Cryptography.OpenSsl/src/System/Security/Cryptography/RSAOpenSsl.cs b/src/libraries/System.Security.Cryptography.OpenSsl/src/System/Security/Cryptography/RSAOpenSsl.cs index 854e145406911..4d2662988b8e3 100644 --- a/src/libraries/System.Security.Cryptography.OpenSsl/src/System/Security/Cryptography/RSAOpenSsl.cs +++ b/src/libraries/System.Security.Cryptography.OpenSsl/src/System/Security/Cryptography/RSAOpenSsl.cs @@ -288,14 +288,14 @@ public override unsafe void ImportParameters(RSAParameters parameters) // CreateBignumPtr returns IntPtr.Zero for null input, so this just does the right thing // on a public-key-only set of RSAParameters. - rsaStructure->n = Interop.libcrypto.CreateBignumPtr(parameters.Modulus); - rsaStructure->e = Interop.libcrypto.CreateBignumPtr(parameters.Exponent); - rsaStructure->d = Interop.libcrypto.CreateBignumPtr(parameters.D); - rsaStructure->p = Interop.libcrypto.CreateBignumPtr(parameters.P); - rsaStructure->dmp1 = Interop.libcrypto.CreateBignumPtr(parameters.DP); - rsaStructure->q = Interop.libcrypto.CreateBignumPtr(parameters.Q); - rsaStructure->dmq1 = Interop.libcrypto.CreateBignumPtr(parameters.DQ); - rsaStructure->iqmp = Interop.libcrypto.CreateBignumPtr(parameters.InverseQ); + rsaStructure->n = Interop.Crypto.CreateBignumPtr(parameters.Modulus); + rsaStructure->e = Interop.Crypto.CreateBignumPtr(parameters.Exponent); + rsaStructure->d = Interop.Crypto.CreateBignumPtr(parameters.D); + rsaStructure->p = Interop.Crypto.CreateBignumPtr(parameters.P); + rsaStructure->dmp1 = Interop.Crypto.CreateBignumPtr(parameters.DP); + rsaStructure->q = Interop.Crypto.CreateBignumPtr(parameters.Q); + rsaStructure->dmq1 = Interop.Crypto.CreateBignumPtr(parameters.DQ); + rsaStructure->iqmp = Interop.Crypto.CreateBignumPtr(parameters.InverseQ); imported = true; } @@ -410,7 +410,7 @@ private SafeRsaHandle GenerateKey() try { - using (SafeBignumHandle exponent = Interop.libcrypto.CreateBignum(s_defaultExponent)) + using (SafeBignumHandle exponent = Interop.Crypto.CreateBignum(s_defaultExponent)) { // The documentation for RSA_generate_key_ex does not say that it returns only // 0 or 1, so the call marshalls it back as a full Int32 and checks for a value diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificateAssetDownloader.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificateAssetDownloader.cs index 1f1d2c23bc2bd..584110c998e53 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificateAssetDownloader.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificateAssetDownloader.cs @@ -48,9 +48,9 @@ internal static SafeX509CrlHandle DownloadCrl(string uri, ref TimeSpan remaining return handle; } - using (SafeBioHandle bio = Interop.libcrypto.BIO_new(Interop.libcrypto.BIO_s_mem())) + using (SafeBioHandle bio = Interop.Crypto.CreateMemoryBio()) { - Interop.libcrypto.BIO_write(bio, data, data.Length); + Interop.Crypto.BioWrite(bio, data, data.Length); handle = Interop.libcrypto.PEM_read_bio_X509_CRL(bio); diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificatePal.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificatePal.cs index e34cdd5708a87..0765593ead4a4 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificatePal.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificatePal.cs @@ -45,7 +45,7 @@ public static ICertificatePal FromBlob(byte[] rawData, string password, X509KeyS public static ICertificatePal FromFile(string fileName, string password, X509KeyStorageFlags keyStorageFlags) { // If we can't open the file, fail right away. - using (SafeBioHandle fileBio = Interop.libcrypto.BIO_new_file(fileName, "rb")) + using (SafeBioHandle fileBio = Interop.Crypto.BioNewFile(fileName, "rb")) { Interop.Crypto.CheckValidOpenSslHandle(fileBio); @@ -152,11 +152,11 @@ internal static bool TryReadX509Pem(SafeBioHandle bio, out ICertificatePal certP internal static bool TryReadX509Pem(byte[] rawData, out ICertificatePal certPal) { SafeX509Handle certHandle; - using (SafeBioHandle bio = Interop.libcrypto.BIO_new(Interop.libcrypto.BIO_s_mem())) + using (SafeBioHandle bio = Interop.Crypto.CreateMemoryBio()) { Interop.Crypto.CheckValidOpenSslHandle(bio); - Interop.libcrypto.BIO_write(bio, rawData, rawData.Length); + Interop.Crypto.BioWrite(bio, rawData, rawData.Length); certHandle = Interop.libcrypto.PEM_read_bio_X509_AUX(bio, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CrlCache.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CrlCache.cs index fbf6b9917a9f5..a7c82a1fefb06 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CrlCache.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CrlCache.cs @@ -44,7 +44,7 @@ private static bool AddCachedCrl(X509Certificate2 cert, SafeX509StoreHandle stor { string crlFile = GetCachedCrlPath(cert); - using (SafeBioHandle bio = Interop.libcrypto.BIO_new_file(crlFile, "rb")) + using (SafeBioHandle bio = Interop.Crypto.BioNewFile(crlFile, "rb")) { if (bio.IsInvalid) { @@ -120,7 +120,7 @@ private static void DownloadAndAddCrl( { string crlFile = GetCachedCrlPath(cert, mkDir: true); - using (SafeBioHandle bio = Interop.libcrypto.BIO_new_file(crlFile, "wb")) + using (SafeBioHandle bio = Interop.Crypto.BioNewFile(crlFile, "wb")) { if (!bio.IsInvalid) { diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509CertificateReader.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509CertificateReader.cs index 0592edf5e034e..95f811b9ef968 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509CertificateReader.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509CertificateReader.cs @@ -281,10 +281,10 @@ public string GetNameInfo(X509NameType nameType, bool forIssuer) return ""; } - int bioSize = Interop.libcrypto.GetMemoryBioSize(bioHandle); + int bioSize = Interop.Crypto.GetMemoryBioSize(bioHandle); // Ensure space for the trailing \0 StringBuilder builder = new StringBuilder(bioSize + 1); - int read = Interop.libcrypto.BIO_gets(bioHandle, builder, builder.Capacity); + int read = Interop.Crypto.BioGets(bioHandle, builder, builder.Capacity); if (read < 0) { diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509Encoder.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509Encoder.cs index 53278fec719d9..a75a36ecddd29 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509Encoder.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509Encoder.cs @@ -39,7 +39,7 @@ internal static string X500DistinguishedNameDecode(byte[] encodedDistinguishedNa { Interop.Crypto.CheckValidOpenSslHandle(x509Name); - using (SafeBioHandle bioHandle = Interop.libcrypto.BIO_new(Interop.libcrypto.BIO_s_mem())) + using (SafeBioHandle bioHandle = Interop.Crypto.CreateMemoryBio()) { Interop.Crypto.CheckValidOpenSslHandle(bioHandle); @@ -53,7 +53,7 @@ internal static string X500DistinguishedNameDecode(byte[] encodedDistinguishedNa // BIO_gets wants to ensure that the response is NULL-terminated. // So add one to leave space for the NULL. StringBuilder builder = new StringBuilder(written + 1); - int read = Interop.libcrypto.BIO_gets(bioHandle, builder, builder.Capacity); + int read = Interop.Crypto.BioGets(bioHandle, builder, builder.Capacity); if (read < 0) { diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/PkcsFormatReader.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/PkcsFormatReader.cs index 1f603cbce3c31..7b8f68ffd0b4a 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/PkcsFormatReader.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/PkcsFormatReader.cs @@ -114,11 +114,11 @@ private static bool TryReadPkcs7Pem( out ICertificatePal certPal, out List certPals) { - using (SafeBioHandle bio = Interop.libcrypto.BIO_new(Interop.libcrypto.BIO_s_mem())) + using (SafeBioHandle bio = Interop.Crypto.CreateMemoryBio()) { Interop.Crypto.CheckValidOpenSslHandle(bio); - Interop.libcrypto.BIO_write(bio, rawData, rawData.Length); + Interop.Crypto.BioWrite(bio, rawData, rawData.Length); SafePkcs7Handle pkcs7 = Interop.libcrypto.PEM_read_bio_PKCS7(bio, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/StorePal.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/StorePal.cs index 811141aa28938..05f17b6fc59b6 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/StorePal.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/StorePal.cs @@ -47,7 +47,7 @@ public static IStorePal FromBlob(byte[] rawData, string password, X509KeyStorage public static IStorePal FromFile(string fileName, string password, X509KeyStorageFlags keyStorageFlags) { - using (SafeBioHandle bio = Interop.libcrypto.BIO_new_file(fileName, "rb")) + using (SafeBioHandle bio = Interop.Crypto.BioNewFile(fileName, "rb")) { Interop.Crypto.CheckValidOpenSslHandle(bio); diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj b/src/libraries/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj index 8bfe987ad3b46..bc79dc0e60a90 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj @@ -162,12 +162,6 @@ Common\Interop\Unix\System.Native\Interop.Permissions.cs - - Common\Interop\Unix\libcrypto\Interop.Bignum.cs - - - Common\Interop\Unix\libcrypto\Interop.BIO.cs - Common\Interop\Unix\libcrypto\Interop.CURLcode.cs @@ -195,6 +189,12 @@ Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ASN1.Print.cs + + Common\Interop\Unix\System.Security.Cryptography.Native\Interop.Bignum.cs + + + Common\Interop\Unix\System.Security.Cryptography.Native\Interop.BIO.cs + Common\Interop\Unix\System.Security.Cryptography.Native\Interop.ERR.cs