Merge branch 'master' into bugfix/remove-login-button-on-register-view-when-already-authenticated

This commit is contained in:
BinaryAlley authored and GitHub committed 2024-11-14 11:45:33 +02:00
commit ced46ec440
1 file changed
+39 -17
@@ -3,6 +3,7 @@ using Lumina.Application.Common.Infrastructure.Security;
using Lumina.Infrastructure.Common.Models.Configuration; using Lumina.Infrastructure.Common.Models.Configuration;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
#endregion #endregion
@@ -10,7 +11,7 @@ using System.Text;
namespace Lumina.Infrastructure.Core.Security; namespace Lumina.Infrastructure.Core.Security;
/// <summary> /// <summary>
/// Service for cryptographic concerns. /// Provides encryption and decryption services using AES encryption.
/// </summary> /// </summary>
public sealed class CryptographyService : ICryptographyService, IDisposable public sealed class CryptographyService : ICryptographyService, IDisposable
{ {
@@ -26,17 +27,17 @@ public sealed class CryptographyService : ICryptographyService, IDisposable
} }
/// <summary> /// <summary>
/// Encrypts a plaintext string using AES encryption with a randomly generated initialization vector (IV). /// Encrypts a plaintext string using AES encryption with a randomly generated initialization vector (IV) and appends a SHA-256 checksum.
/// </summary> /// </summary>
/// <param name="plaintext">The plaintext string to encrypt.</param> /// <param name="plaintext">The plaintext string to encrypt.</param>
/// <returns>A Base64-encoded string of the combined IV and encrypted ciphertext.</returns> /// <returns>A Base64-encoded string containing the IV, encrypted ciphertext, and checksum.</returns>
/// <exception cref="ArgumentException">Thrown when <paramref name="plaintext"/> is <see langword="null"/> or empty.</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="plaintext"/> is <see langword="null"/> or empty.</exception>
public string Encrypt(string plaintext) public string Encrypt(string plaintext)
{ {
if (string.IsNullOrEmpty(plaintext)) if (string.IsNullOrEmpty(plaintext))
throw new ArgumentException("Value cannot be null or empty", nameof(plaintext)); throw new ArgumentException("Value cannot be null or empty", nameof(plaintext));
// generate a new, random 16-byte IV for each encryption to ensure unique ciphertexts // Generate a new, random 16-byte IV for each encryption to ensure unique ciphertexts
byte[] iv = new byte[16]; byte[] iv = new byte[16];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
rng.GetBytes(iv); rng.GetBytes(iv);
@@ -47,40 +48,51 @@ public sealed class CryptographyService : ICryptographyService, IDisposable
aes.Mode = CipherMode.CBC; aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7; aes.Padding = PaddingMode.PKCS7;
// Encrypt the plaintext
using ICryptoTransform encryptor = aes.CreateEncryptor(); using ICryptoTransform encryptor = aes.CreateEncryptor();
byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext);
byte[] ciphertext = encryptor.TransformFinalBlock(plaintextBytes, 0, plaintextBytes.Length); byte[] ciphertext = encryptor.TransformFinalBlock(plaintextBytes, 0, plaintextBytes.Length);
// combine IV and ciphertext // Compute SHA-256 checksum of the plaintext
byte[] combined = new byte[iv.Length + ciphertext.Length]; byte[] checksum = SHA256.HashData(plaintextBytes);
// Combine IV, ciphertext, and checksum
byte[] combined = new byte[iv.Length + ciphertext.Length + checksum.Length];
Buffer.BlockCopy(iv, 0, combined, 0, iv.Length); Buffer.BlockCopy(iv, 0, combined, 0, iv.Length);
Buffer.BlockCopy(ciphertext, 0, combined, iv.Length, ciphertext.Length); Buffer.BlockCopy(ciphertext, 0, combined, iv.Length, ciphertext.Length);
Buffer.BlockCopy(checksum, 0, combined, iv.Length + ciphertext.Length, checksum.Length);
// Return the combined data as a Base64-encoded string
return Convert.ToBase64String(combined); return Convert.ToBase64String(combined);
} }
/// <summary> /// <summary>
/// Decrypts a Base64-encoded string that contains both the IV and ciphertext. /// Decrypts a Base64-encoded string that contains the IV, encrypted ciphertext, and checksum.
/// </summary> /// </summary>
/// <param name="ciphertext">The Base64-encoded string containing both IV and ciphertext.</param> /// <param name="ciphertext">The Base64-encoded string containing IV, encrypted data, and checksum.</param>
/// <returns>The decrypted plaintext string.</returns> /// <returns>The decrypted plaintext string.</returns>
/// <exception cref="ArgumentException">Thrown when <paramref name="ciphertext"/> is <see langword="null"/>, empty, or invalid.</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="ciphertext"/> is <see langword="null"/>, empty, or invalid.</exception>
/// <exception cref="CryptographicException">Thrown when the checksum validation fails, indicating potential data corruption.</exception>
public string Decrypt(string ciphertext) public string Decrypt(string ciphertext)
{ {
if (string.IsNullOrEmpty(ciphertext)) if (string.IsNullOrEmpty(ciphertext))
throw new ArgumentException("Value cannot be null or empty", nameof(ciphertext)); throw new ArgumentException("Value cannot be null or empty", nameof(ciphertext));
// decode the combined data (IV + ciphertext) from Base64 // Decode the combined data (IV + ciphertext + checksum) from Base64
byte[] combined = Convert.FromBase64String(ciphertext); byte[] combined = Convert.FromBase64String(ciphertext);
if (combined.Length < 16) // Validate the minimum length (16 bytes for IV + 32 bytes for checksum)
if (combined.Length < 16 + 32)
throw new ArgumentException("Invalid ciphertext"); throw new ArgumentException("Invalid ciphertext");
// extract IV and ciphertext // Extract IV, ciphertext, and checksum from the combined data
byte[] iv = new byte[16]; byte[] iv = new byte[16];
byte[] encryptedData = new byte[combined.Length - 16]; byte[] checksum = new byte[32];
Buffer.BlockCopy(combined, 0, iv, 0, 16); byte[] encryptedData = new byte[combined.Length - 16 - 32];
Buffer.BlockCopy(combined, 16, encryptedData, 0, encryptedData.Length);
Buffer.BlockCopy(combined, 0, iv, 0, iv.Length);
Buffer.BlockCopy(combined, iv.Length, encryptedData, 0, encryptedData.Length);
Buffer.BlockCopy(combined, iv.Length + encryptedData.Length, checksum, 0, checksum.Length);
using Aes aes = Aes.Create(); using Aes aes = Aes.Create();
aes.Key = _key; aes.Key = _key;
@@ -88,13 +100,23 @@ public sealed class CryptographyService : ICryptographyService, IDisposable
aes.Mode = CipherMode.CBC; aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7; aes.Padding = PaddingMode.PKCS7;
// Decrypt the encrypted data
using ICryptoTransform decryptor = aes.CreateDecryptor(); using ICryptoTransform decryptor = aes.CreateDecryptor();
byte[] decrypted = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length); byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(decrypted);
// Compute the checksum of the decrypted data and validate against the provided checksum
byte[] computedChecksum = SHA256.HashData(decryptedBytes);
// If checksums do not match, throw a CryptographicException indicating potential corruption
if (!computedChecksum.SequenceEqual(checksum))
throw new CryptographicException("Checksum validation failed. The data may be corrupted.");
// Return the decrypted plaintext as a UTF-8 string
return Encoding.UTF8.GetString(decryptedBytes);
} }
/// <summary> /// <summary>
/// Clears sensitive key data when the service is disposed. /// Clears sensitive data from memory.
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {