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);
*
*/
- 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)
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)
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
[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 ();
* 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;
[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 ();
CheckStatusAndThrow (status, SslStatus.WouldBlock);
- wantMore = status == SslStatus.WouldBlock;
- return (int)processed;
+ var wantMore = status == SslStatus.WouldBlock;
+ return ((int)processed, wantMore);
} finally {
pendingIO = 0;
}
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