Merge pull request #5428 from kumpera/wasm-support-p2
[mono.git] / mcs / class / System / Mono.AppleTls / AppleTlsContext.cs
index 563e35f01d15e9f6583699ec2273c4a641919b82..0cc69e47648db2ed9fc45b7bc8af47bd83713b5d 100644 (file)
@@ -95,12 +95,6 @@ namespace Mono.AppleTls
                        get { return !disposed && context != IntPtr.Zero; }
                }
 
-               [System.Diagnostics.Conditional ("APPLE_TLS_DEBUG")]
-               protected new void Debug (string message, params object[] args)
-               {
-                       Console.Error.WriteLine ("MobileTlsStream({0}): {1}", Parent.ID, string.Format (message, args));
-               }
-
                void CheckStatusAndThrow (SslStatus status, params SslStatus[] acceptable)
                {
                        var last = Interlocked.Exchange (ref lastException, null);
@@ -234,32 +228,41 @@ namespace Mono.AppleTls
                         * 
                         */
 
-                       var trust = GetPeerTrust (!IsServer);
-                       X509CertificateCollection certificates;
+                       bool ok;
+                       SecTrust trust = null;
+                       X509CertificateCollection certificates = null;
 
-                       if (trust == null || trust.Count == 0) {
-                               remoteCertificate = null;
-                               if (!IsServer)
-                                       throw new TlsException (AlertDescription.CertificateUnknown);
-                               certificates = null;
-                       } else {
-                               if (trust.Count > 1)
-                                       Debug ("WARNING: Got multiple certificates in SecTrust!");
+                       try {
+                               trust = GetPeerTrust (!IsServer);
 
-                               certificates = new X509CertificateCollection ();
-                               for (int i = 0; i < trust.Count; i++)
-                                       certificates.Add (trust [(IntPtr)i].ToX509Certificate ());
+                               if (trust == null || trust.Count == 0) {
+                                       remoteCertificate = null;
+                                       if (!IsServer)
+                                               throw new TlsException (AlertDescription.CertificateUnknown);
+                                       certificates = null;
+                               } else {
+                                       if (trust.Count > 1)
+                                               Debug ("WARNING: Got multiple certificates in SecTrust!");
 
-                               remoteCertificate = certificates [0];
-                               Debug ("Got peer trust: {0}", remoteCertificate);
-                       }
+                                       certificates = new X509CertificateCollection ();
+                                       for (int i = 0; i < trust.Count; i++)
+                                               certificates.Add (trust.GetCertificate (i));
+
+                                       remoteCertificate = new X509Certificate (certificates [0]);
+                                       Debug ("Got peer trust: {0}", remoteCertificate);
+                               }
 
-                       bool ok;
-                       try {
                                ok = ValidateCertificate (certificates);
                        } catch (Exception ex) {
                                Debug ("Certificate validation failed: {0}", ex);
                                throw new TlsException (AlertDescription.CertificateUnknown, "Certificate validation threw exception.");
+                       } finally {
+                               if (trust != null)
+                                       trust.Dispose ();
+                               if (certificates != null) {
+                                       for (int i = 0; i < certificates.Count; i++)
+                                               certificates [i].Dispose ();
+                               }
                        }
 
                        if (!ok)
@@ -290,11 +293,6 @@ namespace Mono.AppleTls
                        else
                                MaxProtocol = SslProtocol.Tls_1_0;
 
-#if APPLE_TLS_DEBUG
-                       foreach (var c in GetSupportedCiphers ())
-                               Debug ("  {0} SslCipherSuite.{1} {2:x} {3}", IsServer ? "Server" : "Client", c, (int)c, (CipherSuiteCode)c);
-#endif
-
                        if (Settings != null && Settings.EnabledCiphers != null) {
                                SslCipherSuite [] ciphers = new SslCipherSuite [Settings.EnabledCiphers.Length];
                                for (int i = 0 ; i < Settings.EnabledCiphers.Length; ++i)
@@ -664,7 +662,7 @@ namespace Mono.AppleTls
                                if (value == IntPtr.Zero)
                                        throw new TlsException (AlertDescription.CertificateUnknown);
                        }
-                       return (value == IntPtr.Zero) ? null : new SecTrust (value);
+                       return (value == IntPtr.Zero) ? null : new SecTrust (value, true);
                }
 
                #endregion
@@ -777,7 +775,7 @@ namespace Mono.AppleTls
                [DllImport (SecurityLibrary)]
                extern unsafe static /* OSStatus */ SslStatus SSLRead (/* SSLContextRef */ IntPtr context, /* const void* */ byte* data, /* size_t */ IntPtr dataLength, /* size_t* */ out IntPtr processed);
 
-               public override unsafe int Read (byte[] buffer, int offset, int count, out bool wantMore)
+               public override unsafe (int ret, bool wantMore) Read (byte[] buffer, int offset, int count)
                {
                        if (Interlocked.Exchange (ref pendingIO, 1) == 1)
                                throw new InvalidOperationException ();
@@ -801,13 +799,12 @@ namespace Mono.AppleTls
                                         * when the first inner Read() returns 0.  MobileAuthenticatedStream.InnerRead() attempts
                                         * to distinguish between a graceful close and abnormal termination of connection.
                                         */
-                                       wantMore = false;
-                                       return 0;
+                                       return (0, false);
                                }
 
                                CheckStatusAndThrow (status, SslStatus.WouldBlock, SslStatus.ClosedGraceful);
-                               wantMore = status == SslStatus.WouldBlock;
-                               return (int)processed;
+                               var wantMore = status == SslStatus.WouldBlock;
+                               return ((int)processed, wantMore);
                        } catch (Exception ex) {
                                Debug ("Read error: {0}", ex);
                                throw;
@@ -819,7 +816,7 @@ namespace Mono.AppleTls
                [DllImport (SecurityLibrary)]
                extern unsafe static /* OSStatus */ SslStatus SSLWrite (/* SSLContextRef */ IntPtr context, /* const void* */ byte* data, /* size_t */ IntPtr dataLength, /* size_t* */ out IntPtr processed);
 
-               public override unsafe int Write (byte[] buffer, int offset, int count, out bool wantMore)
+               public override unsafe (int ret, bool wantMore) Write (byte[] buffer, int offset, int count)
                {
                        if (Interlocked.Exchange (ref pendingIO, 1) == 1)
                                throw new InvalidOperationException ();
@@ -839,8 +836,8 @@ namespace Mono.AppleTls
 
                                CheckStatusAndThrow (status, SslStatus.WouldBlock);
 
-                               wantMore = status == SslStatus.WouldBlock;
-                               return (int)processed;
+                               var wantMore = status == SslStatus.WouldBlock;
+                               return ((int)processed, wantMore);
                        } finally {
                                pendingIO = 0;
                        }
@@ -851,24 +848,7 @@ namespace Mono.AppleTls
 
                public override void Shutdown ()
                {
-                       if (Interlocked.Exchange (ref pendingIO, 1) == 1)
-                               throw new InvalidOperationException ();
-
-                       Debug ("Shutdown");
-
-                       lastException = null;
-
-                       try {
-                               if (closed || disposed)
-                                       return;
-
-                               var status = SSLClose (Handle);
-                               Debug ("Shutdown done: {0}", status);
-                               CheckStatusAndThrow (status);
-                       } finally {
-                               closed = true;
-                               pendingIO = 0;
-                       }
+                       closed = true;
                }
 
                #endregion