diff --git a/LibConeshell/Coneshell.cs b/LibConeshell/Coneshell.cs index d3a6823..76ee422 100644 --- a/LibConeshell/Coneshell.cs +++ b/LibConeshell/Coneshell.cs @@ -18,7 +18,7 @@ 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; } @@ -26,7 +26,7 @@ public static AsymmetricCipherKeyPair GenerateKeyPair() 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)); diff --git a/LibConeshell/ConeshellV2.cs b/LibConeshell/ConeshellV2.cs index cdfdf7e..0bbff2d 100644 --- a/LibConeshell/ConeshellV2.cs +++ b/LibConeshell/ConeshellV2.cs @@ -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; @@ -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); @@ -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); @@ -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(); @@ -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) : ""); } @@ -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); @@ -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); @@ -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()); @@ -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); diff --git a/LibConeshell/ConeshellV3.cs b/LibConeshell/ConeshellV3.cs index e9d2a98..98e9136 100644 --- a/LibConeshell/ConeshellV3.cs +++ b/LibConeshell/ConeshellV3.cs @@ -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]; @@ -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();