[BTLS]: Improve error handling. (#4317)
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsContext.cs
index 67b1a203cb81538e6cb77c6e498dd16544fcfe82..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 ()
@@ -212,17 +220,7 @@ namespace Mono.Btls
 
                void SetupCertificateStore ()
                {
-#if MONODROID
-                       ctx.CertificateStore.SetDefaultPaths ();
-                       ctx.CertificateStore.AddAndroidLookup ();
-#else
-                       var userPath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.UserTrustedRoots);
-                       if (Directory.Exists (userPath))
-                               ctx.CertificateStore.AddDirectoryLookup (userPath, MonoBtlsX509FileType.PEM);
-                       var machinePath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.MachineTrustedRoots);
-                       if (Directory.Exists (machinePath))
-                               ctx.CertificateStore.AddDirectoryLookup (machinePath, MonoBtlsX509FileType.PEM);
-#endif
+                       MonoBtlsProvider.SetupCertificateStore (ctx.CertificateStore);
 
                        if (Settings != null && Settings.TrustAnchors != null) {
                                var trust = IsServer ? MonoBtlsX509TrustKind.TRUST_CLIENT : MonoBtlsX509TrustKind.TRUST_SERVER;
@@ -246,7 +244,7 @@ namespace Mono.Btls
                        if (!IsServer)
                                ctx.SetSelectCallback (SelectCallback);
 
-                       ctx.SetVerifyParam (MonoBtlsProvider.GetVerifyParam (TargetHost, IsServer));
+                       ctx.SetVerifyParam (MonoBtlsProvider.GetVerifyParam (ServerName, IsServer));
 
                        TlsProtocolCode minProtocol, maxProtocol;
                        GetProtocolVersions (out minProtocol, out maxProtocol);
@@ -283,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
                        };
                }
 
@@ -371,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)