Skip to content

Commit a976a04

Browse files
committed
General exception fixes
1 parent 17466b6 commit a976a04

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

LibConeshell/Coneshell.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public static AsymmetricCipherKeyPair GenerateKeyPair()
1818

1919
var keypair = keygen.GenerateKeyPair();
2020
if (keypair == null)
21-
throw new InvalidDataException("Failed to generate x25519 keypair.");
21+
throw new CryptographicException("Failed to generate X25519 keypair.");
2222

2323
return keypair;
2424
}
2525

2626
protected static byte[] AesCtrCryptInternal(byte[] message, byte[] key, byte[] iv)
2727
{
2828
if (key.Length != 16)
29-
throw new ArgumentException("The key must be 16 bytes in length", nameof(key));
29+
throw new ArgumentException("The key must be 16 bytes in length.", nameof(key));
3030

3131
if (iv.Length != 16)
3232
throw new ArgumentException("The IV must be 16 bytes in length.", nameof(iv));

LibConeshell/ConeshellV2.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected virtual byte[] DeriveDeviceSecret(byte[] sharedSecret)
6464
X25519PrivateKeyParameters? clientPrivateKey = null, bool shouldCompress = false)
6565
{
6666
if (ServerPublicKey == null)
67-
throw new InvalidDataException("No server public key provided");
67+
throw new InvalidDataException("No server public key provided.");
6868

6969
const int headerSize = 0x4 + 0x20 + 0x10;
7070

@@ -186,7 +186,7 @@ private byte[] EncryptMessageInternal(BinaryWriter encryptedWriter, byte[] messa
186186
using var inputReader = new BinaryReader(inputStream);
187187

188188
if (inputReader.ReadUInt32() != HeaderMagic)
189-
throw new InvalidDataException("Invalid message header.");
189+
throw new IOException("Invalid message header.");
190190

191191
var clientEncPubKey = inputReader.ReadBytes(0x20);
192192
var expectedChecksum = inputReader.ReadBytes(0x10);
@@ -215,7 +215,7 @@ public byte[] DecryptResponseMessage(byte[] encrypted, byte[] sharedSecret)
215215
using var inputReader = new BinaryReader(inputStream);
216216

217217
if (inputReader.ReadUInt32() != HeaderMagic)
218-
throw new InvalidDataException("Invalid message header.");
218+
throw new IOException("Invalid message header.");
219219

220220
var iv = inputReader.ReadBytes(16);
221221
var expectedChecksum = inputReader.ReadBytes(16);
@@ -244,7 +244,7 @@ private byte[] DecryptMessageInternal(byte[] encryptedBody, byte[] key, byte[] i
244244
var checksum = checksumHash.Hash!;
245245

246246
if (!checksum.SequenceEqual(expectedChecksum))
247-
throw new InvalidDataException("Body checksum mismatch.");
247+
throw new CryptographicException("Body checksum mismatch.");
248248

249249
var decompressedLength = body[0] | (body[1] << 8) | (body[2] << 16) | (body[3] << 24);
250250
var bodyData = body.Skip(4).ToArray();
@@ -285,7 +285,7 @@ public virtual byte[] DecryptVfs(byte[] dbData, bool skipVerification = false)
285285
var inputReader = new BinaryReader(inputStream);
286286

287287
if (inputReader.ReadUInt32() != VfsHeaderMagic)
288-
throw new InvalidDataException("Invalid database header.");
288+
throw new IOException("Invalid database header.");
289289

290290
return DecryptVfsInternal(dbData, inputReader, skipVerification, !skipVerification ? DeriveVfsPublicKey(VfsCertConstants) : "");
291291
}
@@ -296,7 +296,7 @@ protected static byte[] DecryptVfsInternal(byte[] dbData, BinaryReader inputRead
296296
var headerSize = fullHeaderSize - headerOffset;
297297

298298
if (dbData.Length < headerSize)
299-
throw new InvalidDataException("Encrypted database too short.");
299+
throw new IOException("Encrypted database too short.");
300300

301301
var gcmAdd1 = inputReader.ReadUInt32();
302302
var gcmKey = inputReader.ReadBytes(0x10);
@@ -310,7 +310,7 @@ protected static byte[] DecryptVfsInternal(byte[] dbData, BinaryReader inputRead
310310
var encryptedLength = dbData.Length - headerSize;
311311
var encryptedData = new byte[encryptedLength + gcmTag.Length];
312312
if (inputReader.Read(encryptedData, 0, encryptedLength) != encryptedLength)
313-
throw new InvalidDataException("Failed to read encrypted data from database.");
313+
throw new IOException("Failed to read encrypted data from database.");
314314

315315
Buffer.BlockCopy(gcmTag, 0, encryptedData, encryptedLength, gcmTag.Length);
316316

@@ -324,7 +324,7 @@ protected static byte[] DecryptVfsInternal(byte[] dbData, BinaryReader inputRead
324324
rsa.ImportFromPem(publicKey);
325325
var sigResult = rsa.VerifyHash(signedData, signature, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
326326
if (!sigResult)
327-
throw new InvalidDataException("Failed to verify VFS signature.");
327+
throw new CryptographicException("Failed to verify VFS signature.");
328328
}
329329

330330
var gcm = new GcmBlockCipher(new AesEngine());
@@ -339,7 +339,7 @@ protected static byte[] DecryptVfsInternal(byte[] dbData, BinaryReader inputRead
339339
}
340340
catch (Exception ex)
341341
{
342-
throw new InvalidDataException($"Failed to decrypt database: {ex.Message}");
342+
throw new CryptographicException($"Failed to decrypt database: {ex.Message}");
343343
}
344344

345345
var decompressedLength = decryptedData[0] | (decryptedData[1] << 8) | (decryptedData[2] << 16) | (decryptedData[3] << 24);

LibConeshell/ConeshellV3.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ConeshellV3()
6363
protected override byte[] DeriveDeviceSecret(byte[] sharedSecret)
6464
{
6565
if (sharedSecret.Length != SharedSecretLength)
66-
throw new InvalidDataException($"The shared secret must be {SharedSecretLength} bytes in length.");
66+
throw new ArgumentException($"The shared secret must be {SharedSecretLength} bytes in length.", nameof(sharedSecret));
6767

6868
var result = sharedSecret[..16];
6969

@@ -107,7 +107,7 @@ public override byte[] DecryptVfs(byte[] dbData, bool skipVerification = false /
107107
var inputReader = new BinaryReader(inputStream);
108108

109109
if (inputReader.ReadUInt32() != VfsHeaderMagic)
110-
throw new InvalidDataException("Invalid database header.");
110+
throw new IOException("Invalid database header.");
111111

112112
var processedData = PreprocessVfs(inputReader, dbData.Length - 4);
113113
inputReader.Dispose();

0 commit comments

Comments
 (0)