Skip to content

Better error handling in SubtleCrypto workers #71693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@
<data name="Unknown_Error" xml:space="preserve">
<value>Unknown error.</value>
</data>
<data name="Unknown_SubtleCrypto_Error" xml:space="preserve">
<value>SubtleCrypto returned an unknown error: '{0}'.</value>
</data>
<data name="PlatformNotSupported_CipherModeBrowser" xml:space="preserve">
<value>Only CipherMode.CBC is supported on this platform.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private unsafe int EncryptDecrypt(ReadOnlySpan<byte> input, Span<byte> output)
pOutput, output.Length);

if (bytesWritten < 0)
throw new Exception(SR.Unknown_Error);
throw new CryptographicException(SR.Format(SR.Unknown_SubtleCrypto_Error, bytesWritten));

return bytesWritten;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,42 +43,35 @@ public override int GetCurrentHash(Span<byte> destination)
{
Debug.Assert(destination.Length >= _hashSizeInBytes);

byte[] srcArray = Array.Empty<byte>();
int srcLength = 0;
if (_buffer != null)
{
srcArray = _buffer.GetBuffer();
srcLength = (int)_buffer.Length;
}
ReadOnlySpan<byte> source = _buffer != null ?
new ReadOnlySpan<byte>(_buffer.GetBuffer(), 0, (int)_buffer.Length) :
default;

unsafe
{
fixed (byte* key = _key)
fixed (byte* src = srcArray)
fixed (byte* dest = destination)
{
int res = Interop.BrowserCrypto.Sign(_hashAlgorithm, key, _key.Length, src, srcLength, dest, destination.Length);
Debug.Assert(res != 0);
}
}
Sign(_hashAlgorithm, _key, source, destination);

return _hashSizeInBytes;
}

public static unsafe int MacDataOneShot(string hashAlgorithmId, ReadOnlySpan<byte> key, ReadOnlySpan<byte> data, Span<byte> destination)
public static int MacDataOneShot(string hashAlgorithmId, ReadOnlySpan<byte> key, ReadOnlySpan<byte> data, Span<byte> destination)
{
(SimpleDigest hashName, int hashSizeInBytes) = SHANativeHashProvider.HashAlgorithmToPal(hashAlgorithmId);
Debug.Assert(destination.Length >= hashSizeInBytes);

Sign(hashName, key, data, destination);

return hashSizeInBytes;
}

private static unsafe void Sign(SimpleDigest hashName, ReadOnlySpan<byte> key, ReadOnlySpan<byte> data, Span<byte> destination)
{
fixed (byte* k = key)
fixed (byte* src = data)
fixed (byte* dest = destination)
{
int res = Interop.BrowserCrypto.Sign(hashName, k, key.Length, src, data.Length, dest, destination.Length);
Debug.Assert(res != 0);
if (res != 0)
throw new CryptographicException(SR.Format(SR.Unknown_SubtleCrypto_Error, res));
}

return hashSizeInBytes;
}

public override int HashSizeInBytes => _hashSizeInBytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,34 @@ public override int GetCurrentHash(Span<byte> destination)
{
Debug.Assert(destination.Length >= _hashSizeInBytes);

byte[] srcArray = Array.Empty<byte>();
int srcLength = 0;
if (_buffer != null)
{
srcArray = _buffer.GetBuffer();
srcLength = (int)_buffer.Length;
}
ReadOnlySpan<byte> source = _buffer != null ?
new ReadOnlySpan<byte>(_buffer.GetBuffer(), 0, (int)_buffer.Length) :
default;

unsafe
{
fixed (byte* src = srcArray)
fixed (byte* dest = destination)
{
int res = Interop.BrowserCrypto.SimpleDigestHash(_impl, src, srcLength, dest, destination.Length);
Debug.Assert(res != 0);
}
}
SimpleDigestHash(_impl, source, destination);

return _hashSizeInBytes;
}

public static unsafe int HashOneShot(string hashAlgorithmId, ReadOnlySpan<byte> data, Span<byte> destination)
public static int HashOneShot(string hashAlgorithmId, ReadOnlySpan<byte> data, Span<byte> destination)
{
(SimpleDigest impl, int hashSizeInBytes) = HashAlgorithmToPal(hashAlgorithmId);
Debug.Assert(destination.Length >= hashSizeInBytes);

SimpleDigestHash(impl, data, destination);

return hashSizeInBytes;
}

private static unsafe void SimpleDigestHash(SimpleDigest hashName, ReadOnlySpan<byte> data, Span<byte> destination)
{
fixed (byte* src = data)
fixed (byte* dest = destination)
{
int res = Interop.BrowserCrypto.SimpleDigestHash(impl, src, data.Length, dest, destination.Length);
Debug.Assert(res != 0);
int res = Interop.BrowserCrypto.SimpleDigestHash(hashName, src, data.Length, dest, destination.Length);
if (res != 0)
throw new CryptographicException(SR.Format(SR.Unknown_SubtleCrypto_Error, res));
}

return hashSizeInBytes;
}

public override int HashSizeInBytes => _hashSizeInBytes;
Expand Down
Loading