Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrozsival committed Jun 19, 2024
1 parent a62afea commit 86327ef
Showing 1 changed file with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
using System.Text;
using Microsoft.Win32.SafeHandles;

using SafeJObjectHandle = Interop.JObjectLifetime.SafeJObjectHandle;

namespace System.Security.Cryptography.X509Certificates
{
internal sealed class AndroidCertificatePal : ICertificatePal
{
private SafeX509Handle _cert;
private SafeKeyHandle? _privateKey;
private Interop.JObjectLifetime.SafeJObjectHandle? _keyStorePrivateKeyEntry;
private SafeJObjectHandle? _keyStorePrivateKeyEntry;

private CertificateData _certData;

Expand All @@ -28,8 +30,7 @@ public static ICertificatePal FromHandle(IntPtr handle)

if (Interop.AndroidCrypto.IsKeyStorePrivateKeyEntry(handle))
{
var newPrivateKeyEntryHandle = new Interop.JObjectLifetime.SafeJObjectHandle();
Marshal.InitHandle(newPrivateKeyEntryHandle, Interop.JObjectLifetime.NewGlobalReference(handle));
SafeJObjectHandle newPrivateKeyEntryHandle = SafeJObjectHandle.CreateGlobalReferenceFromHandle(handle);
return new AndroidCertificatePal(newPrivateKeyEntryHandle);
}

Expand Down Expand Up @@ -160,7 +161,7 @@ private static AndroidCertificatePal ReadPkcs12(ReadOnlySpan<byte> rawData, Safe
}
}

internal AndroidCertificatePal(Interop.JObjectLifetime.SafeJObjectHandle handle)
internal AndroidCertificatePal(SafeJObjectHandle handle)
{
_cert = Interop.AndroidCrypto.GetPrivateKeyEntryCertificate(handle);
_keyStorePrivateKeyEntry = handle;
Expand All @@ -179,12 +180,9 @@ internal AndroidCertificatePal(SafeX509Handle handle, SafeKeyHandle privateKey)

public bool HasPrivateKey => _privateKey is not null || _keyStorePrivateKeyEntry is not null;

public IntPtr Handle => (_keyStorePrivateKeyEntry, _cert) switch
{
({} privateKeyEntry, _) => privateKeyEntry.DangerousGetHandle(),
(null, {} cert) => cert.DangerousGetHandle(),
_ => IntPtr.Zero,
};
public IntPtr Handle => _keyStorePrivateKeyEntry?.DangerousGetHandle()
?? _cert?.DangerousGetHandle()
?? IntPtr.Zero;

internal SafeX509Handle SafeHandle => _cert;

Expand Down

0 comments on commit 86327ef

Please sign in to comment.