public override int Write(byte[] buffer, int offset, int size, out bool wantMore)
        {
            Debug("Write: {0} {1} {2}", buffer.Length, offset, size);

            var data = Marshal.AllocHGlobal(size);

            if (data == IntPtr.Zero)
            {
                throw new OutOfMemoryException();
            }

            try {
                MonoBtlsError.ClearError();
                Marshal.Copy(buffer, offset, data, size);
                var status = ssl.Write(data, ref size);
                Debug("Write done: {0} {1}", status, size);

                if (status == MonoBtlsSslError.WantWrite)
                {
                    wantMore = true;
                    return(0);
                }
                else if (status != MonoBtlsSslError.None)
                {
                    throw GetException(status);
                }

                wantMore = false;
                return(size);
            } finally {
                Marshal.FreeHGlobal(data);
            }
        }
        static Exception GetException(MonoBtlsSslError status)
        {
            var error = MonoBtlsError.GetError();
            var text  = MonoBtlsError.GetErrorString(error);

            return(new MonoBtlsException("{0} {1}", status, text));
        }
Beispiel #3
0
        static Exception GetException(MonoBtlsSslError status)
        {
            string file;
            int    line;
            var    error = MonoBtlsError.GetError(out file, out line);

            if (error == 0)
            {
                return(new MonoBtlsException(status));
            }

            var reason = MonoBtlsError.GetErrorReason(error);

            if (reason > 0)
            {
                return(new TlsException((AlertDescription)reason));
            }

            var text = MonoBtlsError.GetErrorString(error);

            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));
        }
Beispiel #4
0
        public override (int ret, bool wantMore) Read(byte[] buffer, int offset, int size)
        {
            Debug("Read: {0} {1} {2}", buffer.Length, offset, size);

            var data = Marshal.AllocHGlobal(size);

            if (data == IntPtr.Zero)
            {
                throw new OutOfMemoryException();
            }

            try {
                MonoBtlsError.ClearError();
                var status = ssl.Read(data, ref size);
                Debug("Read done: {0} {1}", status, size);

                if (status == MonoBtlsSslError.WantRead)
                {
                    return(0, true);
                }
                if (status == MonoBtlsSslError.ZeroReturn)
                {
                    return(size, false);
                }
                if (status != MonoBtlsSslError.None)
                {
                    throw GetException(status);
                }

                if (size > 0)
                {
                    Marshal.Copy(data, buffer, offset, size);
                }

                return(size, false);
            } finally {
                Marshal.FreeHGlobal(data);
            }
        }
Beispiel #5
0
        public override bool ProcessHandshake()
        {
            var done = false;

            while (!done)
            {
                Debug("ProcessHandshake");
                MonoBtlsError.ClearError();
                var status = DoProcessHandshake();
                Debug("ProcessHandshake #1: {0}", status);

                switch (status)
                {
                case MonoBtlsSslError.None:
                    if (connected)
                    {
                        done = true;
                    }
                    else
                    {
                        connected = true;
                    }
                    break;

                case MonoBtlsSslError.WantRead:
                case MonoBtlsSslError.WantWrite:
                    return(false);

                default:
                    ctx.CheckLastError();
                    throw GetException(status);
                }
            }

            ssl.PrintErrors();

            return(true);
        }