[BTLS]: Improve error handling. (#4317)
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsContext.cs
index c06688dc86eee8fdd083e5f42ce1c9c06bce3472..5bdac307dc4cadee764cf594332aa87d369d7dc1 100644 (file)
@@ -23,7 +23,7 @@
 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
-#if SECURITY_DEP
+#if SECURITY_DEP && MONO_FEATURE_BTLS
 #if MONO_SECURITY_ALIAS
 extern alias MonoSecurity;
 #endif
@@ -135,7 +135,7 @@ namespace Mono.Btls
                        if (IsServer) {
                                SetPrivateCertificate (nativeServerCertificate);
                        } else {
-                               ssl.SetServerName (TargetHost);
+                               ssl.SetServerName (ServerName);
                        }
                }
 
@@ -154,14 +154,22 @@ namespace Mono.Btls
                        }
                }
 
-               Exception GetException (MonoBtlsSslError status)
+               static Exception GetException (MonoBtlsSslError status)
                {
-                       var error = MonoBtlsError.GetError ();
+                       string file;
+                       int line;
+                       var error = MonoBtlsError.GetError (out file, out line);
                        if (error == null)
                                return new MonoBtlsException (status);
 
                        var text = MonoBtlsError.GetErrorString (error);
-                       return new MonoBtlsException ("{0} {1}", status, text);
+
+                       string message;
+                       if (file != null)
+                               message = string.Format ("{0} {1}\n  at {2}:{3}", status, text, file, line);
+                       else
+                               message = string.Format ("{0} {1}", status, text);
+                       return new MonoBtlsException (message);
                }
 
                public override bool ProcessHandshake ()
@@ -236,14 +244,7 @@ namespace Mono.Btls
                        if (!IsServer)
                                ctx.SetSelectCallback (SelectCallback);
 
-                       var host = TargetHost;
-                       if (!string.IsNullOrEmpty (host)) {
-                               var pos = TargetHost.IndexOf (':');
-                               if (pos > 0)
-                                       host = host.Substring (0, pos);
-                       }
-
-                       ctx.SetVerifyParam (MonoBtlsProvider.GetVerifyParam (host, IsServer));
+                       ctx.SetVerifyParam (MonoBtlsProvider.GetVerifyParam (ServerName, IsServer));
 
                        TlsProtocolCode minProtocol, maxProtocol;
                        GetProtocolVersions (out minProtocol, out maxProtocol);
@@ -280,11 +281,13 @@ namespace Mono.Btls
 
                        var cipher = (CipherSuiteCode)ssl.GetCipher ();
                        var protocol = (TlsProtocolCode)ssl.GetVersion ();
+                       var serverName = ssl.GetServerName ();
                        Debug ("GET CONNECTION INFO: {0:x}:{0} {1:x}:{1} {2}", cipher, protocol, (TlsProtocolCode)protocol);
 
                        connectionInfo = new MonoTlsConnectionInfo {
                                CipherSuiteCode = cipher,
-                               ProtocolVersion = GetProtocol (protocol)
+                               ProtocolVersion = GetProtocol (protocol),
+                               PeerDomainName = serverName
                        };
                }
 
@@ -368,7 +371,23 @@ namespace Mono.Btls
                public override void Close ()
                {
                        Debug ("Close!");
-                       ssl.Dispose ();
+
+                       if (ssl != null) {
+                               ssl.Dispose ();
+                               ssl = null;
+                       }
+                       if (ctx != null) {
+                               ctx.Dispose ();
+                               ctx = null;
+                       }
+                       if (bio != null) {
+                               bio.Dispose ();
+                               bio = null;
+                       }
+                       if (errbio != null) {
+                               errbio.Dispose ();
+                               errbio = null;
+                       }
                }
 
                void Dispose<T> (ref T disposable)