Skip to content

Commit

Permalink
General exception fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeFZ committed Dec 4, 2022
1 parent 17466b6 commit a976a04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions LibConeshell/Coneshell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public static AsymmetricCipherKeyPair GenerateKeyPair()

var keypair = keygen.GenerateKeyPair();
if (keypair == null)
throw new InvalidDataException("Failed to generate x25519 keypair.");
throw new CryptographicException("Failed to generate X25519 keypair.");

return keypair;
}

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

if (iv.Length != 16)
throw new ArgumentException("The IV must be 16 bytes in length.", nameof(iv));
Expand Down
18 changes: 9 additions & 9 deletions LibConeshell/ConeshellV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected virtual byte[] DeriveDeviceSecret(byte[] sharedSecret)
X25519PrivateKeyParameters? clientPrivateKey = null, bool shouldCompress = false)
{
if (ServerPublicKey == null)
throw new InvalidDataException("No server public key provided");
throw new InvalidDataException("No server public key provided.");

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

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

if (inputReader.ReadUInt32() != HeaderMagic)
throw new InvalidDataException("Invalid message header.");
throw new IOException("Invalid message header.");

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

if (inputReader.ReadUInt32() != HeaderMagic)
throw new InvalidDataException("Invalid message header.");
throw new IOException("Invalid message header.");

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

if (!checksum.SequenceEqual(expectedChecksum))
throw new InvalidDataException("Body checksum mismatch.");
throw new CryptographicException("Body checksum mismatch.");

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

if (inputReader.ReadUInt32() != VfsHeaderMagic)
throw new InvalidDataException("Invalid database header.");
throw new IOException("Invalid database header.");

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

if (dbData.Length < headerSize)
throw new InvalidDataException("Encrypted database too short.");
throw new IOException("Encrypted database too short.");

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

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

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

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

var decompressedLength = decryptedData[0] | (decryptedData[1] << 8) | (decryptedData[2] << 16) | (decryptedData[3] << 24);
Expand Down
4 changes: 2 additions & 2 deletions LibConeshell/ConeshellV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ConeshellV3()
protected override byte[] DeriveDeviceSecret(byte[] sharedSecret)
{
if (sharedSecret.Length != SharedSecretLength)
throw new InvalidDataException($"The shared secret must be {SharedSecretLength} bytes in length.");
throw new ArgumentException($"The shared secret must be {SharedSecretLength} bytes in length.", nameof(sharedSecret));

var result = sharedSecret[..16];

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

if (inputReader.ReadUInt32() != VfsHeaderMagic)
throw new InvalidDataException("Invalid database header.");
throw new IOException("Invalid database header.");

var processedData = PreprocessVfs(inputReader, dbData.Length - 4);
inputReader.Dispose();
Expand Down

0 comments on commit a976a04

Please sign in to comment.