Browse Source

pin the C# verify callback

Jacob Barthelmeh 3 years ago
parent
commit
3773d33070
1 changed files with 56 additions and 2 deletions
  1. 56 2
      wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs

+ 56 - 2
wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs

@@ -58,6 +58,7 @@ namespace wolfSSL.CSharp {
             private GCHandle rec_cb;
             private GCHandle snd_cb;
             private GCHandle psk_cb;
+            private GCHandle vrf_cb;
             private IntPtr ctx;
 
             public void set_receive(GCHandle input)
@@ -87,6 +88,19 @@ namespace wolfSSL.CSharp {
                 return this.psk_cb;
             }
 
+            public void set_vrf(GCHandle input)
+            {
+                if (!Object.Equals(this.vrf_cb, default(GCHandle)))
+                {
+                    this.vrf_cb.Free();
+                }
+                this.vrf_cb = input;
+            }
+            public GCHandle get_vrf()
+            {
+                return this.vrf_cb;
+            }
+
             public void set_ctx(IntPtr input)
             {
                 this.ctx = input;
@@ -114,6 +128,10 @@ namespace wolfSSL.CSharp {
                 {
                     this.psk_cb.Free();
                 }
+                if (!Object.Equals(this.vrf_cb, default(GCHandle)))
+                {
+                    this.vrf_cb.Free();
+                }
             }
         }
 
@@ -125,6 +143,7 @@ namespace wolfSSL.CSharp {
         {
             private GCHandle fd_pin;
             private GCHandle psk_cb;
+            private GCHandle vrf_cb;
             private IntPtr ssl;
 
             public void set_fd(GCHandle input)
@@ -145,6 +164,19 @@ namespace wolfSSL.CSharp {
                 return this.psk_cb;
             }
 
+            public void set_vrf(GCHandle input)
+            {
+                if (!Object.Equals(this.vrf_cb, default(GCHandle)))
+                {
+                    this.vrf_cb.Free();
+                }
+                this.vrf_cb = input;
+            }
+            public GCHandle get_vrf()
+            {
+                return this.vrf_cb;
+            }
+
             public void set_ssl(IntPtr input)
             {
                 this.ssl = input;
@@ -165,6 +197,10 @@ namespace wolfSSL.CSharp {
                 {
                     this.psk_cb.Free();
                 }
+                if (!Object.Equals(this.vrf_cb, default(GCHandle)))
+                {
+                    this.vrf_cb.Free();
+                }
             }
         }
 
@@ -1837,13 +1873,22 @@ namespace wolfSSL.CSharp {
         {
             try
             {
-                IntPtr local_ctx = unwrap_ctx(ctx);
+                GCHandle   gch;
+                ctx_handle handles;
+                IntPtr     local_ctx = unwrap_ctx(ctx);
                 if (local_ctx == IntPtr.Zero)
                 {
                     log(ERROR_LOG, "CTX set_verify error");
                     return FAILURE;
                 }
 
+                /* pin the verify callback to protect from garbage collection */
+                if (!vc.Equals(null)) {
+                    gch = GCHandle.FromIntPtr(ctx);
+                    handles = (ctx_handle)gch.Target;
+                    handles.set_vrf(GCHandle.Alloc(vc));
+                }
+
                 wolfSSL_CTX_set_verify(local_ctx, mode, vc);
                 return SUCCESS;
             }
@@ -1864,13 +1909,22 @@ namespace wolfSSL.CSharp {
         {
             try
             {
-                IntPtr local_ssl = unwrap_ssl(ssl);
+                GCHandle   gch;
+                ssl_handle handles;
+                IntPtr     local_ssl = unwrap_ssl(ssl);
                 if (local_ssl == IntPtr.Zero)
                 {
                     log(ERROR_LOG, "set_verify error");
                     return FAILURE;
                 }
 
+                /* pin the verify callback to protect from garbage collection */
+                if (!vc.Equals(null)) {
+                    gch = GCHandle.FromIntPtr(ssl);
+                    handles = (ssl_handle)gch.Target;
+                    handles.set_vrf(GCHandle.Alloc(vc));
+                }
+
                 wolfSSL_set_verify(local_ssl, mode, vc);
                 return SUCCESS;
             }