-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Managed classes for AndroidCrypto SSL and X509Certificates (#48674)
- Loading branch information
1 parent
6a80a38
commit bb9a9d1
Showing
31 changed files
with
1,486 additions
and
941 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/libraries/Common/src/Interop/Android/Interop.JObjectLifetime.cs
This file contains 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,42 @@ | ||
// 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; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class JObjectLifetime | ||
{ | ||
[DllImport(Libraries.CryptoNative, EntryPoint = "AndroidCryptoNative_NewGlobalReference")] | ||
internal static extern IntPtr NewGlobalReference(IntPtr obj); | ||
|
||
[DllImport(Libraries.CryptoNative, EntryPoint = "AndroidCryptoNative_DeleteGlobalReference")] | ||
internal static extern void DeleteGlobalReference(IntPtr obj); | ||
|
||
internal class SafeJObjectHandle : SafeHandle | ||
{ | ||
public SafeJObjectHandle() | ||
: base(IntPtr.Zero, ownsHandle: true) | ||
{ | ||
} | ||
|
||
internal SafeJObjectHandle(IntPtr ptr) | ||
: base(IntPtr.Zero, ownsHandle: true) | ||
{ | ||
SetHandle(ptr); | ||
} | ||
|
||
protected override bool ReleaseHandle() | ||
{ | ||
Interop.JObjectLifetime.DeleteGlobalReference(handle); | ||
SetHandle(IntPtr.Zero); | ||
return true; | ||
} | ||
|
||
public override bool IsInvalid => handle == IntPtr.Zero; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../Common/src/Interop/Android/System.Security.Cryptography.Native.Android/Interop.Crypto.cs
This file contains 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,27 @@ | ||
// 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; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class AndroidCrypto | ||
{ | ||
private delegate int NegativeSizeReadMethod<in THandle>(THandle handle, byte[]? buf, int cBuf); | ||
private static byte[] GetDynamicBuffer<THandle>(NegativeSizeReadMethod<THandle> method, THandle handle) | ||
{ | ||
int negativeSize = method(handle, null, 0); | ||
if (negativeSize > 0) | ||
throw new CryptographicException(); | ||
|
||
byte[] bytes = new byte[-negativeSize]; | ||
int ret = method(handle, bytes, bytes.Length); | ||
if (ret != 1) | ||
throw new CryptographicException(); | ||
|
||
return bytes; | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ies/Common/src/Interop/Android/System.Security.Cryptography.Native.Android/Interop.Ssl.cs
This file contains 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,47 @@ | ||
// 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 Microsoft.Win32.SafeHandles; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class AndroidCrypto | ||
{ | ||
[DllImport(Interop.Libraries.CryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamRelease")] | ||
internal static extern void SSLStreamRelease(IntPtr ptr); | ||
|
||
internal class SslException : Exception | ||
{ | ||
internal SslException() | ||
{ | ||
} | ||
|
||
internal SslException(int errorCode) | ||
{ | ||
HResult = errorCode; | ||
} | ||
} | ||
} | ||
} | ||
|
||
namespace System.Net | ||
{ | ||
internal sealed class SafeSslHandle : SafeHandle | ||
{ | ||
public SafeSslHandle() | ||
: base(IntPtr.Zero, ownsHandle: true) | ||
{ | ||
} | ||
|
||
protected override bool ReleaseHandle() | ||
{ | ||
Interop.AndroidCrypto.SSLStreamRelease(handle); | ||
SetHandle(IntPtr.Zero); | ||
return true; | ||
} | ||
|
||
public override bool IsInvalid => handle == IntPtr.Zero; | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.