Skip to content

Commit

Permalink
Merge pull request #225 from dgarske/csharp_cleanup
Browse files Browse the repository at this point in the history
Implement `IDisposable` for CSharp classes for cleanup of unmanaged resources
  • Loading branch information
anhu authored Jul 15, 2022
2 parents bbecd2b + 6b44458 commit 63be95d
Showing 1 changed file with 74 additions and 23 deletions.
97 changes: 74 additions & 23 deletions wrapper/CSharp/wolfTPM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public enum X509_Format : int
DER = 2,
}

public class KeyBlob
public class KeyBlob : IDisposable
{
const string DLLNAME = "wolftpm";

Expand Down Expand Up @@ -211,16 +211,24 @@ public KeyBlob()
{
keyblob = wolfTPM2_NewKeyBlob();
}
~KeyBlob() => Dispose(false);

~KeyBlob()
public void Dispose()
{
if (keyblob != IntPtr.Zero)
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
/* free un-managed objects */
if (keyblob != IntPtr.Zero) {
/* ignore return code */
wolfTPM2_FreeKeyBlob(keyblob);
keyblob = IntPtr.Zero;
}
}


public int GetKeyBlobAsBuffer(byte[] buffer)
{
int rc = wolfTPM2_GetKeyBlobAsBuffer(buffer, buffer.Length,
Expand Down Expand Up @@ -250,7 +258,7 @@ public IntPtr GetHandle()
}
}

public class Key
public class Key : IDisposable
{
const string DLLNAME = "wolftpm";

Expand Down Expand Up @@ -279,16 +287,24 @@ public Key()
{
key = wolfTPM2_NewKey();
}
~Key() => Dispose(false);

~Key()
public void Dispose()
{
if (key != IntPtr.Zero)
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
/* free un-managed objects */
if (key != IntPtr.Zero) {
/* ignore return code */
wolfTPM2_FreeKey(key);
key = IntPtr.Zero;
}
}


public IntPtr GetHandle()
{
return wolfTPM2_GetHandleRefFromKey(key);
Expand All @@ -311,10 +327,9 @@ public int SetKeyAuthPassword(string auth)
}
return rc;
}

}

public class Template
public class Template : IDisposable
{
const string DLLNAME = "wolftpm";

Expand All @@ -329,12 +344,24 @@ public Template()
{
template = wolfTPM2_NewPublicTemplate();
}
~Template() => Dispose(false);

~Template()
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
wolfTPM2_FreePublicTemplate(template);
/* free un-managed objects */
if (template != IntPtr.Zero) {
/* ignore return code */
wolfTPM2_FreePublicTemplate(template);
template = IntPtr.Zero;
}
}


/* non-device functions: template and auth */
[DllImport(DLLNAME, EntryPoint = "wolfTPM2_GetKeyTemplate_RSA")]
private static extern int wolfTPM2_GetKeyTemplate_RSA(IntPtr publicTemplate,
Expand Down Expand Up @@ -476,7 +503,7 @@ public int SetKeyTemplate_Unique(string unique)
}
}

public class Session
public class Session : IDisposable
{
const string DLLNAME = "wolftpm";

Expand All @@ -497,18 +524,25 @@ public Session()
session = wolfTPM2_NewSession();
sessionIdx = 1; /* for most commands the index is 1 */
}

public Session(int index)
{
session = wolfTPM2_NewSession();
sessionIdx = index;
}
~Session() => Dispose(false);

~Session()
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
/* free un-managed objects */
if (session != IntPtr.Zero) {
/* ignore return code on free */
/* ignore return code */
wolfTPM2_FreeSession(session);
session = IntPtr.Zero;
}
}

Expand Down Expand Up @@ -556,7 +590,7 @@ public int StopAuth(Device device)
}
}

public class Csr
public class Csr : IDisposable
{
const string DLLNAME = "wolftpm";

Expand All @@ -572,15 +606,24 @@ public Csr()
{
csr = wolfTPM2_NewCSR();
}
~Csr() => Dispose(false);

~Csr()
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
/* free un-managed objects */
if (csr != IntPtr.Zero) {
/* ignore return code on free */
/* ignore return code */
wolfTPM2_FreeCSR(csr);
csr = IntPtr.Zero;
}
}


[DllImport(DLLNAME, EntryPoint = "wolfTPM2_CSR_SetCustomExt")]
private static extern int wolfTPM2_CSR_SetCustomExt(IntPtr dev,
IntPtr csr,
Expand Down Expand Up @@ -679,7 +722,7 @@ public int MakeAndSign(Device device,
}
}

public class Device
public class Device : IDisposable
{
/* ================================================================== */
/* Constants */
Expand All @@ -696,12 +739,20 @@ public Device()
{
device = wolfTPM2_New();
}
~Device() => Dispose(false);

~Device()
public void Dispose()
{
if (device != IntPtr.Zero)
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
/* free un-managed objects */
if (device != IntPtr.Zero) {
/* ignore return code */
wolfTPM2_Free(device);
device = IntPtr.Zero;
}
}

Expand Down

0 comments on commit 63be95d

Please sign in to comment.