-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[OSX][TLS 1.3] Network Framework Native Layer + Interop Classes Implementations #117016
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
Closed
liveans
wants to merge
25
commits into
dotnet:main
from
liveans:network_framework_integration_native_interop
Closed
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
c062e18
Native Interop Layer
liveans ecba3f7
Native Layer Compilation fix for Mono + NativeAOT + templates
liveans bf130f3
First shape of new native + interop
liveans 0dcc599
Newlines at the end of files
liveans 44b3597
Default constructor ownsHandle to true
liveans 3cbcea7
Delete couple of unsafe keyword in Interop
liveans 3defe85
Update src/native/libs/System.Net.Security.Native.Apple/pal_networkfr…
liveans 57a7069
Merge branch 'main' into network_framework_integration_native_interop
liveans c1a2b6b
Fix PlatformManifestFileEntry
liveans 0977679
Review feedback
liveans fe343b0
Apply suggestions from code review
liveans 0fff060
Update src/libraries/Common/src/Interop/OSX/Interop.Network.Tls.cs
liveans 3441093
Review feedbacks
liveans 3e547c5
Merge branch 'main' into network_framework_integration_native_interop
liveans 25b8950
Further review feedback
liveans 91238c5
Add new library name to nativeaot build target file
liveans a250b67
Merge branch 'main' into network_framework_integration_native_interop
liveans beb5f93
Merge System.Net.Security.Native.Apple with System.Security.Cryptogra…
rzikm 0ab06b2
fixup! Merge System.Net.Security.Native.Apple with System.Security.Cr…
rzikm 39bef6d
Shared OSStatus
rzikm 8267454
Correctly release some handles
rzikm 4bf6eb9
Remove printf
rzikm f771ea9
Add comments
rzikm 6ab3942
Fix build
rzikm cc950a1
Merge branch 'main' into network_framework_integration_native_interop
liveans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // 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; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class NetworkFramework | ||
| { | ||
| // Network Framework reference counting functions | ||
| [LibraryImport(Libraries.NetworkFramework, EntryPoint = "nw_retain")] | ||
| internal static partial IntPtr Retain(IntPtr obj); | ||
|
|
||
| [LibraryImport(Libraries.NetworkFramework, EntryPoint = "nw_release")] | ||
| internal static partial void Release(IntPtr obj); | ||
| } | ||
| } |
153 changes: 153 additions & 0 deletions
153
src/libraries/Common/src/Interop/OSX/Interop.NetworkTls.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| // 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.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Net.Security; | ||
| using System.Runtime.InteropServices; | ||
| using System.Security.Authentication; | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| // TLS 1.3 specific Network Framework implementation for macOS | ||
| internal static partial class NetworkFramework | ||
| { | ||
| internal static partial class Tls | ||
| { | ||
| // Core TLS functions for Network Framework integration | ||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwInit")] | ||
| [return: MarshalAs(UnmanagedType.I4)] | ||
| internal static unsafe partial bool Init(delegate* unmanaged<IntPtr, StatusUpdates, IntPtr, IntPtr, void> statusCallback, | ||
| delegate* unmanaged<IntPtr, byte*, void**, int> readCallback, | ||
| delegate* unmanaged<IntPtr, byte*, void**, int> writeCallback); | ||
|
|
||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwCreateContext")] | ||
| internal static partial SafeNetworkFrameworkHandle CreateContext([MarshalAs(UnmanagedType.I4)] bool isServer); | ||
|
|
||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwSetTlsOptions", StringMarshalling = StringMarshalling.Utf8)] | ||
| private static partial int SetTlsOptions(SafeNetworkFrameworkHandle connection, IntPtr gcHandle, | ||
| string targetName, Span<byte> alpnBuffer, int alpnLength, | ||
| SslProtocols minTlsProtocol, SslProtocols maxTlsProtocol); | ||
|
|
||
| internal static int SetTlsOptions(SafeNetworkFrameworkHandle nwHandle, IntPtr gcHandle, string targetName, List<SslApplicationProtocol>? applicationProtocols, SslProtocols minTlsVersion, SslProtocols maxTlsVersion) | ||
| { | ||
| int alpnLength = GetAlpnProtocolListSerializedLength(applicationProtocols); | ||
| Span<byte> alpn = alpnLength <= 256 ? stackalloc byte[256].Slice(0, alpnLength) : new byte[alpnLength]; | ||
| SerializeAlpnProtocolList(applicationProtocols, alpn); | ||
|
|
||
| return SetTlsOptions(nwHandle, gcHandle, targetName, alpn, alpnLength, minTlsVersion, maxTlsVersion); | ||
| } | ||
|
|
||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwStartTlsHandshake")] | ||
| internal static partial int StartTlsHandshake(SafeNetworkFrameworkHandle connection, IntPtr gcHandle); | ||
|
|
||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwProcessInputData")] | ||
| internal static unsafe partial int ProcessInputData(SafeNetworkFrameworkHandle connection, | ||
| SafeNetworkFrameworkHandle framer, | ||
| byte* buffer, int bufferLength); | ||
|
|
||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwSendToConnection")] | ||
| internal static unsafe partial int SendToConnection(SafeNetworkFrameworkHandle connection, IntPtr gcHandle, | ||
| void* buffer, int bufferLength); | ||
|
|
||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwReadFromConnection")] | ||
| internal static partial int ReadFromConnection(SafeNetworkFrameworkHandle connection, IntPtr gcHandle); | ||
|
|
||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwCancelConnection")] | ||
| internal static partial int CancelConnection(SafeNetworkFrameworkHandle connection); | ||
|
|
||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwGetConnectionInfo")] | ||
| internal static unsafe partial int GetConnectionInfo(SafeNetworkFrameworkHandle connection, | ||
| out SslProtocols pProtocol, out TlsCipherSuite pCipherSuiteOut, | ||
| ref void* negotiatedAlpn, out uint alpnLength); | ||
|
|
||
| [LibraryImport(Interop.Libraries.AppleNetworkNative, EntryPoint = "AppleNetNative_NwCopyCertChain")] | ||
| internal static partial int CopyCertChain(SafeNetworkFrameworkHandle connection, | ||
| out SafeCFArrayHandle certificates, | ||
| out int count); | ||
|
|
||
| internal static int GetAlpnProtocolListSerializedLength(List<SslApplicationProtocol>? applicationProtocols) | ||
| { | ||
| if (applicationProtocols is null) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| int protocolSize = 0; | ||
|
|
||
| foreach (SslApplicationProtocol protocol in applicationProtocols) | ||
| { | ||
| if (protocol.Protocol.Length == 0 || protocol.Protocol.Length > byte.MaxValue) | ||
| { | ||
| throw new ArgumentException(SR.net_ssl_app_protocols_invalid, nameof(applicationProtocols)); | ||
| } | ||
|
|
||
| protocolSize += protocol.Protocol.Length + 2; | ||
| } | ||
|
|
||
| return protocolSize; | ||
| } | ||
|
|
||
| private static void SerializeAlpnProtocolList(List<SslApplicationProtocol>? applicationProtocols, Span<byte> buffer) | ||
| { | ||
| if (applicationProtocols is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Debug.Assert(GetAlpnProtocolListSerializedLength(applicationProtocols) == buffer.Length); | ||
|
|
||
| int offset = 0; | ||
| foreach (SslApplicationProtocol protocol in applicationProtocols) | ||
| { | ||
| buffer[offset++] = (byte)protocol.Protocol.Length; | ||
| protocol.Protocol.Span.CopyTo(buffer.Slice(offset)); | ||
| offset += protocol.Protocol.Length; | ||
| buffer[offset++] = 0; | ||
| } | ||
| } | ||
| } | ||
| // Status enumeration for Network Framework TLS operations | ||
| internal enum StatusUpdates | ||
| { | ||
| UnknownError = 0, | ||
| FramerStart = 1, | ||
| FramerStop = 2, | ||
| HandshakeFinished = 3, | ||
| HandshakeFailed = 4, | ||
| ConnectionReadFinished = 100, | ||
| ConnectionWriteFinished = 101, | ||
| ConnectionWriteFailed = 102, | ||
| ConnectionCancelled = 103, | ||
| } | ||
|
|
||
| internal enum OSStatus | ||
| { | ||
| NoError = 0, | ||
| ReadError = -19, | ||
| WriteError = -20, | ||
| EOFError = -39, | ||
| SecUserCanceled = -128, | ||
| WouldBlock = -9803 | ||
| } | ||
| } | ||
|
|
||
| // Safe handle classes for Network Framework TLS resources | ||
| internal sealed class SafeNetworkFrameworkHandle : SafeHandleZeroOrMinusOneIsInvalid | ||
liveans marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| public SafeNetworkFrameworkHandle() : base(ownsHandle: true) { } | ||
|
|
||
| public SafeNetworkFrameworkHandle(IntPtr handle, bool ownsHandle) : base(ownsHandle) | ||
| { | ||
| SetHandle(NetworkFramework.Retain(handle)); | ||
| } | ||
liveans marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| protected override bool ReleaseHandle() | ||
| { | ||
| NetworkFramework.Release(handle); | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/native/libs/System.Net.Security.Native.Apple/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| project(System.Net.Security.Native.Apple C) | ||
liveans marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| set(CMAKE_INCLUDE_CURRENT_DIR ON) | ||
|
|
||
| add_compile_options(-Wno-deprecated-declarations) | ||
|
|
||
| include(${CMAKE_CURRENT_LIST_DIR}/extra_libs.cmake) | ||
| append_extra_networking_apple_libs(NATIVE_LIBS_EXTRA) | ||
|
|
||
| set(NATIVENETWORKING_SOURCES | ||
| pal_networkframework.m | ||
| entrypoints.c | ||
| ) | ||
|
|
||
| if (GEN_SHARED_LIB) | ||
| add_library(System.Net.Security.Native.Apple | ||
| SHARED | ||
| ${NATIVENETWORKING_SOURCES} | ||
| ${VERSION_FILE_PATH} | ||
| ) | ||
| endif() | ||
|
|
||
| add_library(System.Net.Security.Native.Apple-Static | ||
| STATIC | ||
| ${NATIVENETWORKING_SOURCES} | ||
| ) | ||
|
|
||
| set_target_properties(System.Net.Security.Native.Apple-Static PROPERTIES OUTPUT_NAME System.Net.Security.Native.Apple CLEAN_DIRECT_OUTPUT 1) | ||
|
|
||
| if (GEN_SHARED_LIB) | ||
| target_link_libraries(System.Net.Security.Native.Apple | ||
| PRIVATE | ||
| ${NATIVE_LIBS_EXTRA} | ||
| ) | ||
|
|
||
| add_custom_command(TARGET System.Net.Security.Native.Apple POST_BUILD | ||
| COMMENT "Verifying System.Net.Security.Native.Apple entry points" | ||
| COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../verify-entrypoints.sh | ||
| $<TARGET_FILE:System.Net.Security.Native.Apple> | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/entrypoints.c | ||
| ${CMAKE_NM} | ||
| VERBATIM | ||
| ) | ||
| endif() | ||
|
|
||
| if (GEN_SHARED_LIB) | ||
| install_with_stripped_symbols (System.Net.Security.Native.Apple PROGRAMS .) | ||
| endif() | ||
|
|
||
| install (TARGETS System.Net.Security.Native.Apple-Static DESTINATION ${STATIC_LIB_DESTINATION} COMPONENT libs) | ||
28 changes: 28 additions & 0 deletions
28
src/native/libs/System.Net.Security.Native.Apple/entrypoints.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #include <minipal/entrypoints.h> | ||
|
|
||
| // Include System.Net.Security.Native.Apple headers | ||
| #include "pal_networkframework.h" | ||
|
|
||
| static const Entry s_netSecurityAppleNative[] = | ||
| { | ||
| DllImportEntry(AppleNetNative_NwInit) | ||
| DllImportEntry(AppleNetNative_NwCreateContext) | ||
| DllImportEntry(AppleNetNative_NwSetTlsOptions) | ||
| DllImportEntry(AppleNetNative_NwStartTlsHandshake) | ||
| DllImportEntry(AppleNetNative_NwProcessInputData) | ||
| DllImportEntry(AppleNetNative_NwSendToConnection) | ||
| DllImportEntry(AppleNetNative_NwReadFromConnection) | ||
| DllImportEntry(AppleNetNative_NwCancelConnection) | ||
| DllImportEntry(AppleNetNative_NwGetConnectionInfo) | ||
| DllImportEntry(AppleNetNative_NwCopyCertChain) | ||
| }; | ||
|
|
||
| EXTERN_C const void* NetSecurityAppleResolveDllImport(const char* name); | ||
|
|
||
| EXTERN_C const void* NetSecurityAppleResolveDllImport(const char* name) | ||
| { | ||
| return minipal_resolve_dllimport(s_netSecurityAppleNative, ARRAY_SIZE(s_netSecurityAppleNative), name); | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/native/libs/System.Net.Security.Native.Apple/extra_libs.cmake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| function(append_extra_networking_apple_libs NativeLibsExtra) | ||
| find_library(COREFOUNDATION CoreFoundation) | ||
| find_library(SECURITY Security) | ||
| find_library(NETWORK Network) | ||
| find_library(FOUNDATION Foundation) | ||
|
|
||
| set(${NativeLibsExtra} ${${NativeLibsExtra}} ${COREFOUNDATION} ${SECURITY} ${NETWORK} ${FOUNDATION} PARENT_SCOPE) | ||
| endfunction() |
68 changes: 68 additions & 0 deletions
68
src/native/libs/System.Net.Security.Native.Apple/pal_networkframework.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "pal_compiler.h" | ||
| #include <pal_ssl_types.h> | ||
| #include <stdint.h> | ||
| #include <stddef.h> | ||
| #include <sys/types.h> // for intptr_t | ||
|
|
||
| #ifdef __OBJC__ | ||
| #import <Network/Network.h> | ||
| #else | ||
| #include <Network/Network.h> | ||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| // Handshake state enumeration matching other Apple SSL implementations | ||
| typedef enum | ||
| { | ||
| PAL_TlsHandshakeState_Unknown = 0, | ||
| PAL_TlsHandshakeState_Complete = 1, | ||
| PAL_TlsHandshakeState_WouldBlock = 2, | ||
| PAL_TlsHandshakeState_ServerAuthCompleted = 3, | ||
| PAL_TlsHandshakeState_ClientAuthCompleted = 4, | ||
| PAL_TlsHandshakeState_ClientCertRequested = 5, | ||
| PAL_TlsHandshakeState_ClientHelloReceived = 6, | ||
| } PAL_TlsHandshakeState; | ||
|
|
||
| // Status update enumeration for TLS operations | ||
| typedef enum | ||
| { | ||
| PAL_NwStatusUpdates_UnknownError = 0, | ||
| PAL_NwStatusUpdates_FramerStart = 1, | ||
| PAL_NwStatusUpdates_FramerStop = 2, | ||
| PAL_NwStatusUpdates_HandshakeFinished = 3, | ||
| PAL_NwStatusUpdates_HandshakeFailed = 4, | ||
|
|
||
| PAL_NwStatusUpdates_ConnectionReadFinished = 100, | ||
| PAL_NwStatusUpdates_ConnectionWriteFinished = 101, | ||
| PAL_NwStatusUpdates_ConnectionWriteFailed = 102, | ||
| PAL_NwStatusUpdates_ConnectionCancelled = 103, | ||
| } PAL_NwStatusUpdates; | ||
|
|
||
| // Callback type definitions that match the implementation usage | ||
| typedef void (*StatusUpdateCallback)(size_t context, PAL_NwStatusUpdates status, size_t data1, size_t data2); | ||
| typedef int32_t (*ReadCallback)(void* context, uint8_t* buffer, size_t* length); | ||
| typedef int32_t (*WriteCallback)(void* context, uint8_t* buffer, size_t length); | ||
|
|
||
| // Only TLS-specific Network Framework functions are exported | ||
| PALEXPORT nw_connection_t AppleNetNative_NwCreateContext(int32_t isServer); | ||
| PALEXPORT int32_t AppleNetNative_NwStartTlsHandshake(nw_connection_t connection, size_t gcHandle); | ||
| PALEXPORT int32_t AppleNetNative_NwInit(StatusUpdateCallback statusFunc, ReadCallback readFunc, WriteCallback writeFunc); | ||
| PALEXPORT int32_t AppleNetNative_NwSendToConnection(nw_connection_t connection, size_t gcHandle, uint8_t* buffer, int length); | ||
| PALEXPORT int32_t AppleNetNative_NwReadFromConnection(nw_connection_t connection, size_t gcHandle); | ||
| PALEXPORT int32_t AppleNetNative_NwProcessInputData(nw_connection_t connection, nw_framer_t framer, const uint8_t * data, int dataLength); | ||
| PALEXPORT int32_t AppleNetNative_NwSetTlsOptions(nw_connection_t connection, size_t gcHandle, char* targetName, const uint8_t* alpnBuffer, int alpnLength, PAL_SslProtocol minTlsProtocol, PAL_SslProtocol maxTlsProtocol); | ||
| PALEXPORT int32_t AppleNetNative_NwGetConnectionInfo(nw_connection_t connection, PAL_SslProtocol* pProtocol, uint16_t* pCipherSuiteOut, const char** negotiatedAlpn, uint32_t* alpnLength); | ||
| PALEXPORT int32_t AppleNetNative_NwCopyCertChain(nw_connection_t connection, CFArrayRef* certificates, int* count); | ||
| PALEXPORT int32_t AppleNetNative_NwCancelConnection(nw_connection_t connection); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.