private SecurityStatus EncryptDecryptHelper(SafeDeleteContext securityContext, byte[] buffer, int offset, int size, int headerSize, int trailerSize, bool encrypt, out int resultSize) { bool gotReference = false; resultSize = 0; try { securityContext.DangerousAddRef(ref gotReference); Interop.libssl.SslErrorCode errorCode = Interop.libssl.SslErrorCode.SSL_ERROR_NONE; unsafe { fixed(byte *bufferPtr = buffer) { IntPtr inputPtr = new IntPtr(bufferPtr); IntPtr scHandle = securityContext.DangerousGetHandle(); resultSize = encrypt ? Interop.OpenSsl.Encrypt(scHandle, inputPtr, offset, size, buffer.Length, out errorCode) : Interop.OpenSsl.Decrypt(scHandle, inputPtr, size, out errorCode); } } switch (errorCode) { case Interop.libssl.SslErrorCode.SSL_ERROR_RENEGOTIATE: return(SecurityStatus.Renegotiate); case Interop.libssl.SslErrorCode.SSL_ERROR_ZERO_RETURN: return(SecurityStatus.ContextExpired); case Interop.libssl.SslErrorCode.SSL_ERROR_NONE: case Interop.libssl.SslErrorCode.SSL_ERROR_WANT_READ: return(SecurityStatus.OK); default: return(SecurityStatus.InternalError); } } catch (Exception ex) { Debug.Fail("Exception Caught. - " + ex); return(SecurityStatus.InternalError); } finally { if (gotReference) { securityContext.DangerousRelease(); } } }
private static SecurityStatusPal EncryptDecryptHelper(SafeDeleteContext securityContext, byte[] buffer, int offset, int size, int headerSize, int trailerSize, bool encrypt, out int resultSize) { resultSize = 0; try { Interop.libssl.SslErrorCode errorCode = Interop.libssl.SslErrorCode.SSL_ERROR_NONE; unsafe { fixed(byte *bufferPtr = buffer) { IntPtr inputPtr = new IntPtr(bufferPtr); Interop.libssl.SafeSslHandle scHandle = securityContext.SslContext; resultSize = encrypt ? Interop.OpenSsl.Encrypt(scHandle, inputPtr, offset, size, buffer.Length, out errorCode) : Interop.OpenSsl.Decrypt(scHandle, inputPtr, size, out errorCode); } } switch (errorCode) { case Interop.libssl.SslErrorCode.SSL_ERROR_RENEGOTIATE: return(SecurityStatusPal.Renegotiate); case Interop.libssl.SslErrorCode.SSL_ERROR_ZERO_RETURN: return(SecurityStatusPal.ContextExpired); case Interop.libssl.SslErrorCode.SSL_ERROR_NONE: case Interop.libssl.SslErrorCode.SSL_ERROR_WANT_READ: return(SecurityStatusPal.OK); default: return(SecurityStatusPal.InternalError); } } catch (Exception ex) { Debug.Fail("Exception Caught. - " + ex); return(SecurityStatusPal.InternalError); } }