Skip to content

Commit 12c4907

Browse files
committed
Fixed corrupted file name repair
1 parent 422f7c8 commit 12c4907

File tree

15 files changed

+64
-91
lines changed

15 files changed

+64
-91
lines changed

src/Core/SecureFolderFS.Core.Cryptography/Cipher/AesSiv128.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,6 @@ public byte[] Decrypt(ReadOnlySpan<byte> bytes, ReadOnlySpan<byte> associatedDat
4242
return _aesCmacSiv.Open(bytes.ToArray(), data: associatedData.ToArray());
4343
}
4444

45-
[MethodImpl(MethodImplOptions.Synchronized)]
46-
public byte[]? TryDecrypt(ReadOnlySpan<byte> bytes, ReadOnlySpan<byte> associatedData)
47-
{
48-
try
49-
{
50-
return Decrypt(bytes, associatedData);
51-
}
52-
catch (CryptographicException)
53-
{
54-
return null;
55-
}
56-
}
57-
5845
/// <inheritdoc/>
5946
public void Dispose()
6047
{

src/Core/SecureFolderFS.Core.Cryptography/Extensions/HeaderCryptExtensions/XChaChaHeaderExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ public static ReadOnlySpan<byte> GetHeaderContentKey(this ReadOnlySpan<byte> hea
1919
}
2020

2121
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22-
public static ReadOnlySpan<byte> GetHeaderTag(this ReadOnlySpan<byte> header)
22+
public static ReadOnlySpan<byte> SkipNonce(this ReadOnlySpan<byte> header)
2323
{
24-
return header.Slice(0, HEADER_TAG_SIZE);
24+
return header.Slice(HEADER_NONCE_SIZE);
25+
}
26+
27+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
28+
public static Span<byte> SkipNonce(this Span<byte> header)
29+
{
30+
return header.Slice(HEADER_NONCE_SIZE);
2531
}
2632
}
2733
}

src/Core/SecureFolderFS.Core.Cryptography/HeaderCrypt/XChaChaHeaderCrypt.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override void EncryptHeader(ReadOnlySpan<byte> plaintextHeader, Span<byte
4141
plaintextHeader.GetHeaderContentKey(),
4242
DekKey,
4343
plaintextHeader.GetHeaderNonce(),
44-
ciphertextHeader.Slice(HEADER_NONCE_SIZE),
44+
ciphertextHeader.SkipNonce(),
4545
default);
4646
}
4747

@@ -53,10 +53,10 @@ public override bool DecryptHeader(ReadOnlySpan<byte> ciphertextHeader, Span<byt
5353

5454
// Decrypt
5555
return XChaCha20Poly1305.Decrypt(
56-
ciphertextHeader.Slice(HEADER_NONCE_SIZE),
56+
ciphertextHeader.SkipNonce(),
5757
DekKey,
5858
ciphertextHeader.GetHeaderNonce(),
59-
plaintextHeader.Slice(HEADER_NONCE_SIZE),
59+
plaintextHeader.SkipNonce(),
6060
default);
6161
}
6262
}

src/Core/SecureFolderFS.Core.Cryptography/NameCrypt/BaseNameCrypt.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,28 @@ public virtual string EncryptName(ReadOnlySpan<char> plaintextName, ReadOnlySpan
4646
/// <inheritdoc/>
4747
public virtual string? DecryptName(ReadOnlySpan<char> ciphertextName, ReadOnlySpan<byte> directoryId)
4848
{
49-
// Decode buffer
50-
var ciphertextNameBuffer = fileNameEncodingId switch
49+
try
5150
{
52-
Constants.CipherId.ENCODING_BASE64URL => Base64Url.DecodeFromChars(ciphertextName),
53-
Constants.CipherId.ENCODING_BASE4K => Base4K.DecodeChainToNewBuffer(ciphertextName),
54-
_ => throw new ArgumentOutOfRangeException(nameof(fileNameEncodingId))
55-
};
51+
// Decode buffer
52+
var ciphertextNameBuffer = fileNameEncodingId switch
53+
{
54+
Constants.CipherId.ENCODING_BASE64URL => Base64Url.DecodeFromChars(ciphertextName),
55+
Constants.CipherId.ENCODING_BASE4K => Base4K.DecodeChainToNewBuffer(ciphertextName),
56+
_ => throw new ArgumentOutOfRangeException(nameof(fileNameEncodingId))
57+
};
5658

57-
// Decrypt
58-
var plaintextNameBuffer = DecryptFileName(ciphertextNameBuffer, directoryId);
59-
if (plaintextNameBuffer is null)
60-
return null;
59+
// Decrypt
60+
var plaintextNameBuffer = DecryptFileName(ciphertextNameBuffer, directoryId);
61+
if (plaintextNameBuffer is null)
62+
return null;
6163

62-
// Get string from plaintext buffer
63-
return Encoding.UTF8.GetString(plaintextNameBuffer);
64+
// Get string from plaintext buffer
65+
return Encoding.UTF8.GetString(plaintextNameBuffer);
66+
}
67+
catch (Exception)
68+
{
69+
return null;
70+
}
6471
}
6572

6673
protected abstract byte[] EncryptFileName(ReadOnlySpan<byte> plaintextFileNameBuffer, ReadOnlySpan<byte> directoryId);

src/Core/SecureFolderFS.Core.Dokany/Callbacks/OnDeviceDokany.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,7 @@ public override NtStatus FindFilesWithPattern(string fileName, string searchPatt
357357
continue;
358358

359359
var plaintextName = NativePathHelpers.GetPlaintextPath(item.FullName, Specifics, directoryId);
360-
plaintextName = plaintextName is not null
361-
? Path.GetFileName(plaintextName)
362-
: item.Name;
360+
plaintextName = plaintextName is not null ? Path.GetFileName(plaintextName) : null;
363361

364362
if (string.IsNullOrEmpty(plaintextName))
365363
continue;

src/Core/SecureFolderFS.Core.FileSystem/Helpers/Health/HealthHelpers.Directory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using OwlCore.Storage;
22
using SecureFolderFS.Core.Cryptography;
3+
using SecureFolderFS.Core.FileSystem.Helpers.Paths;
34
using SecureFolderFS.Shared.ComponentModel;
45
using SecureFolderFS.Shared.Models;
56
using SecureFolderFS.Storage.Extensions;
@@ -8,7 +9,6 @@
89
using System.IO;
910
using System.Threading;
1011
using System.Threading.Tasks;
11-
using SecureFolderFS.Core.FileSystem.Helpers.Paths;
1212

1313
namespace SecureFolderFS.Core.FileSystem.Helpers.Health
1414
{

src/Core/SecureFolderFS.Core.FileSystem/Validators/StructureValidator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public StructureValidator(FileSystemSpecifics specifics, IAsyncValidator<IFile,
2424
/// <inheritdoc/>
2525
public override Task ValidateAsync((IFolder, IProgress<IResult>?) value, CancellationToken cancellationToken = default)
2626
{
27-
// TODO: Implement
28-
throw new NotImplementedException();
27+
throw new NotSupportedException();
2928
}
3029

3130
/// <inheritdoc/>
@@ -55,7 +54,6 @@ public override async Task<IResult> ValidateResultAsync((IFolder, IProgress<IRes
5554
}
5655
}
5756

58-
// TODO: Return appropriate result
5957
return Result.Success;
6058
}
6159
}

src/Core/SecureFolderFS.Core.MobileFS/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static class FileSystem
1717
{
1818
public const string FS_ID = "ANDROID_DOCUMENTS_PROVIDER";
1919
public const string FS_NAME = "Android SAF";
20+
public const string AUTHORITY = "org.securefolderfs.securefolderfs.provider";
2021
}
2122

2223
internal static class Saf

src/Core/SecureFolderFS.Core.MobileFS/Platforms/Android/FileSystem/FileSystemProvider.Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace SecureFolderFS.Core.MobileFS.Platforms.Android.FileSystem
2121
{
2222
[ContentProvider(["${applicationId}.provider"],
23-
Name = "org.securefolderfs.securefolderfs.provider",
23+
Name = Constants.Android.FileSystem.AUTHORITY,
2424
Permission = "android.permission.MANAGE_DOCUMENTS",
2525
Enabled = true,
2626
Exported = true,

src/Core/SecureFolderFS.Core.MobileFS/Platforms/Android/FileSystem/RootCollection.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ private void FileSystemManager_CollectionChanged(object? sender, NotifyCollectio
6969

7070
void NotifySafChange()
7171
{
72-
// TODO: Make the authority change resistant to avoid unexpected errors
73-
// Authority taken from ContentProviderAttribute on FileSystemProvider
74-
var rootsUri = DocumentsContract.BuildRootsUri("org.securefolderfs.securefolderfs.provider");
72+
var rootsUri = DocumentsContract.BuildRootsUri(Constants.Android.FileSystem.AUTHORITY);
7573
if (rootsUri is not null)
7674
_context.ContentResolver?.NotifyChange(rootsUri, null);
7775
}

0 commit comments

Comments
 (0)