Пример #1
0
        public static SecurityStatusPal DecryptMessage(
            SafeDeleteContext securityContext,
            byte[] buffer,
            ref int offset,
            ref int count)
        {
            try
            {
                SafeDeleteSslContext sslContext = (SafeDeleteSslContext)securityContext;
                SafeSslHandle        sslHandle  = sslContext.SslContext;

                sslContext.Write(buffer, offset, count);

                unsafe
                {
                    fixed(byte *offsetInput = &buffer[offset])
                    {
                        int       written;
                        PAL_TlsIo status;

                        lock (sslHandle)
                        {
                            status = Interop.AppleCrypto.SslRead(sslHandle, offsetInput, count, out written);
                        }

                        if (status < 0)
                        {
                            return(new SecurityStatusPal(
                                       SecurityStatusPalErrorCode.InternalError,
                                       Interop.AppleCrypto.CreateExceptionForOSStatus((int)status)));
                        }

                        count = written;

                        switch (status)
                        {
                        case PAL_TlsIo.Success:
                        case PAL_TlsIo.WouldBlock:
                            return(new SecurityStatusPal(SecurityStatusPalErrorCode.OK));

                        case PAL_TlsIo.ClosedGracefully:
                            return(new SecurityStatusPal(SecurityStatusPalErrorCode.ContextExpired));

                        case PAL_TlsIo.Renegotiate:
                            return(new SecurityStatusPal(SecurityStatusPalErrorCode.Renegotiate));

                        default:
                            Debug.Fail($"Unknown status value: {status}");
                            return(new SecurityStatusPal(SecurityStatusPalErrorCode.InternalError));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return(new SecurityStatusPal(SecurityStatusPalErrorCode.InternalError, e));
            }
        }
Пример #2
0
        public static SecurityStatusPal DecryptMessage(
            SafeDeleteSslContext securityContext,
            Span <byte> buffer,
            out int offset,
            out int count)
        {
            offset = 0;
            count  = 0;

            try
            {
                SafeSslHandle sslHandle = securityContext.SslContext;
                securityContext.Write(buffer);

                unsafe
                {
                    fixed(byte *ptr = buffer)
                    {
                        PAL_TlsIo status = Interop.AppleCrypto.SslRead(sslHandle, ptr, buffer.Length, out int written);

                        if (status < 0)
                        {
                            return(new SecurityStatusPal(
                                       SecurityStatusPalErrorCode.InternalError,
                                       Interop.AppleCrypto.CreateExceptionForOSStatus((int)status)));
                        }

                        count  = written;
                        offset = 0;

                        switch (status)
                        {
                        case PAL_TlsIo.Success:
                        case PAL_TlsIo.WouldBlock:
                            return(new SecurityStatusPal(SecurityStatusPalErrorCode.OK));

                        case PAL_TlsIo.ClosedGracefully:
                            return(new SecurityStatusPal(SecurityStatusPalErrorCode.ContextExpired));

                        case PAL_TlsIo.Renegotiate:
                            return(new SecurityStatusPal(SecurityStatusPalErrorCode.Renegotiate));

                        default:
                            Debug.Fail($"Unknown status value: {status}");
                            return(new SecurityStatusPal(SecurityStatusPalErrorCode.InternalError));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return(new SecurityStatusPal(SecurityStatusPalErrorCode.InternalError, e));
            }
        }
Пример #3
0
        public static SecurityStatusPal DecryptMessage(
            SafeDeleteSslContext securityContext,
            Span <byte> buffer,
            out int offset,
            out int count)
        {
            offset = 0;
            count  = 0;

            try
            {
                SafeSslHandle sslHandle = securityContext.SslContext;

                securityContext.Write(buffer);

                PAL_SSLStreamStatus ret = Interop.AndroidCrypto.SSLStreamRead(sslHandle, buffer, out int read);
                if (ret == PAL_SSLStreamStatus.Error)
                {
                    return(new SecurityStatusPal(SecurityStatusPalErrorCode.InternalError));
                }

                count = read;

                SecurityStatusPalErrorCode statusCode = ret switch
                {
                    PAL_SSLStreamStatus.OK => SecurityStatusPalErrorCode.OK,
                    PAL_SSLStreamStatus.NeedData => SecurityStatusPalErrorCode.OK,
                    PAL_SSLStreamStatus.Renegotiate => SecurityStatusPalErrorCode.Renegotiate,
                    PAL_SSLStreamStatus.Closed => SecurityStatusPalErrorCode.ContextExpired,
                    _ => SecurityStatusPalErrorCode.InternalError
                };

                return(new SecurityStatusPal(statusCode));
            }
            catch (Exception e)
            {
                return(new SecurityStatusPal(SecurityStatusPalErrorCode.InternalError, e));
            }
        }