Skip to content

Commit

Permalink
Merge pull request #2031 from Unity-Technologies/2021.3/fix-uum-72433…
Browse files Browse the repository at this point in the history
…-ssl-memory-leak

[2021.3] Fix MemoryLeak in UnityTlsProvider
  • Loading branch information
scott-ferguson-unity authored Jun 21, 2024
2 parents 9505ce3 + ae7b09b commit 4a14a72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 1 addition & 2 deletions mcs/class/System/Mono.UnityTls/UnityTlsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ internal override bool ValidateCertificate (
}

chain?.Dispose();
var chainImpl = new X509ChainImplUnityTls(
UnityTls.NativeInterface.unitytls_x509list_get_ref (finalCertificateChainNative, &errorState),
var chainImpl = new X509ChainImplUnityTls(finalCertificateChainNative, &errorState,
reverseOrder: true // the verify callback starts with the root and ends with the leaf. That's the opposite of chain ordering.
);
chain = new X509Chain(chainImpl);
Expand Down
16 changes: 15 additions & 1 deletion mcs/class/System/Mono.UnityTls/X509ChainImplUnityTls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
namespace Mono.Unity
{
// Follows mostly X509ChainImplBtls
class X509ChainImplUnityTls : X509ChainImpl
unsafe class X509ChainImplUnityTls : X509ChainImpl
{
private X509ChainElementCollection elements;
private UnityTls.unitytls_x509list* ownedList;
private UnityTls.unitytls_x509list_ref nativeCertificateChain;
private X509ChainPolicy policy = new X509ChainPolicy ();
private List<X509ChainStatus> chainStatusList;
Expand All @@ -22,10 +23,19 @@ class X509ChainImplUnityTls : X509ChainImpl
internal X509ChainImplUnityTls (UnityTls.unitytls_x509list_ref nativeCertificateChain, bool reverseOrder = false)
{
this.elements = null;
this.ownedList = null;
this.nativeCertificateChain = nativeCertificateChain;
this.reverseOrder = reverseOrder;
}

internal X509ChainImplUnityTls (UnityTls.unitytls_x509list* ownedList, UnityTls.unitytls_errorstate* errorState, bool reverseOrder = false)
{
this.elements = null;
this.ownedList = ownedList;
this.nativeCertificateChain = UnityTls.NativeInterface.unitytls_x509list_get_ref(ownedList, errorState);
this.reverseOrder = reverseOrder;
}

public override bool IsValid {
get { return nativeCertificateChain.handle != UnityTls.NativeInterface.UNITYTLS_INVALID_HANDLE; }
}
Expand Down Expand Up @@ -97,6 +107,10 @@ public override void Reset ()
elements.Clear ();
elements = null;
}
if (ownedList != null) {
UnityTls.NativeInterface.unitytls_x509list_free (ownedList);
ownedList = null;
}
}

protected override void Dispose (bool disposing)
Expand Down

0 comments on commit 4a14a72

Please sign in to comment.