|
| 1 | +using System; |
| 2 | +using System.Security.Cryptography; |
| 3 | +using System.Text; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Unity.WalmartAuthRelay.Interfaces; |
| 7 | +using Unity.Services.CloudCode.Apis; |
| 8 | +using Unity.Services.CloudCode.Core; |
| 9 | + |
| 10 | +namespace Unity.WalmartAuthRelay.Services; |
| 11 | + |
| 12 | +public class AesEncryptionService : IEncryptionService |
| 13 | +{ |
| 14 | + private readonly ILogger<AesEncryptionService> _logger; |
| 15 | + private readonly ISecretService _secretService; |
| 16 | + |
| 17 | + private const string ENCRYPTION_PREFIX = "ENC:"; |
| 18 | + private const string ENCRYPTION_KEY_NAME = "LCID_ENCRYPTION_KEY"; |
| 19 | + private const int IV_SIZE = 12; // 96-bit IV for GCM |
| 20 | + private const int TAG_SIZE = 16; // 128-bit tag for GCM |
| 21 | + |
| 22 | + public AesEncryptionService(ILogger<AesEncryptionService> logger, ISecretService secretService) |
| 23 | + { |
| 24 | + _logger = logger; |
| 25 | + _secretService = secretService; |
| 26 | + } |
| 27 | + |
| 28 | + public async Task<string> EncryptAsync(IExecutionContext ctx, IGameApiClient client, string plainText) |
| 29 | + { |
| 30 | + if (string.IsNullOrEmpty(plainText)) |
| 31 | + { |
| 32 | + throw new ArgumentException("Plain text cannot be null or empty", nameof(plainText)); |
| 33 | + } |
| 34 | + |
| 35 | + try |
| 36 | + { |
| 37 | + var key = await GetEncryptionKeyAsync(ctx, client); |
| 38 | + var plainBytes = Encoding.UTF8.GetBytes(plainText); |
| 39 | + |
| 40 | + using var aes = new AesGcm(key, TAG_SIZE); |
| 41 | + var iv = new byte[IV_SIZE]; |
| 42 | + var ciphertext = new byte[plainBytes.Length]; |
| 43 | + var tag = new byte[TAG_SIZE]; |
| 44 | + |
| 45 | + RandomNumberGenerator.Fill(iv); |
| 46 | + aes.Encrypt(iv, plainBytes, ciphertext, tag); |
| 47 | + |
| 48 | + // Format: IV + Tag + Ciphertext |
| 49 | + var encryptedData = new byte[IV_SIZE + TAG_SIZE + ciphertext.Length]; |
| 50 | + Buffer.BlockCopy(iv, 0, encryptedData, 0, IV_SIZE); |
| 51 | + Buffer.BlockCopy(tag, 0, encryptedData, IV_SIZE, TAG_SIZE); |
| 52 | + Buffer.BlockCopy(ciphertext, 0, encryptedData, IV_SIZE + TAG_SIZE, ciphertext.Length); |
| 53 | + |
| 54 | + return ENCRYPTION_PREFIX + Convert.ToBase64String(encryptedData); |
| 55 | + } |
| 56 | + catch (Exception ex) |
| 57 | + { |
| 58 | + _logger.LogError(ex, "Failed to encrypt data"); |
| 59 | + throw new InvalidOperationException("Encryption failed", ex); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public async Task<string> DecryptAsync(IExecutionContext ctx, IGameApiClient client, string encryptedText) |
| 64 | + { |
| 65 | + if (string.IsNullOrEmpty(encryptedText)) |
| 66 | + { |
| 67 | + throw new ArgumentException("Encrypted text cannot be null or empty", nameof(encryptedText)); |
| 68 | + } |
| 69 | + |
| 70 | + if (!encryptedText.StartsWith(ENCRYPTION_PREFIX)) |
| 71 | + { |
| 72 | + throw new ArgumentException("Invalid encrypted data format", nameof(encryptedText)); |
| 73 | + } |
| 74 | + |
| 75 | + try |
| 76 | + { |
| 77 | + var key = await GetEncryptionKeyAsync(ctx, client); |
| 78 | + var base64Data = encryptedText.Substring(ENCRYPTION_PREFIX.Length); |
| 79 | + var encryptedData = Convert.FromBase64String(base64Data); |
| 80 | + |
| 81 | + if (encryptedData.Length < IV_SIZE + TAG_SIZE) |
| 82 | + { |
| 83 | + throw new ArgumentException("Invalid encrypted data length", nameof(encryptedText)); |
| 84 | + } |
| 85 | + |
| 86 | + var iv = new byte[IV_SIZE]; |
| 87 | + var tag = new byte[TAG_SIZE]; |
| 88 | + var ciphertext = new byte[encryptedData.Length - IV_SIZE - TAG_SIZE]; |
| 89 | + |
| 90 | + Buffer.BlockCopy(encryptedData, 0, iv, 0, IV_SIZE); |
| 91 | + Buffer.BlockCopy(encryptedData, IV_SIZE, tag, 0, TAG_SIZE); |
| 92 | + Buffer.BlockCopy(encryptedData, IV_SIZE + TAG_SIZE, ciphertext, 0, ciphertext.Length); |
| 93 | + |
| 94 | + using var aes = new AesGcm(key, TAG_SIZE); |
| 95 | + var plainBytes = new byte[ciphertext.Length]; |
| 96 | + aes.Decrypt(iv, ciphertext, tag, plainBytes); |
| 97 | + |
| 98 | + return Encoding.UTF8.GetString(plainBytes); |
| 99 | + } |
| 100 | + catch (Exception ex) |
| 101 | + { |
| 102 | + _logger.LogError(ex, "Failed to decrypt data"); |
| 103 | + throw new InvalidOperationException("Decryption failed", ex); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public Task<bool> IsEncryptedAsync(string value) |
| 108 | + { |
| 109 | + return Task.FromResult(!string.IsNullOrEmpty(value) && value.StartsWith(ENCRYPTION_PREFIX)); |
| 110 | + } |
| 111 | + |
| 112 | + private async Task<byte[]> GetEncryptionKeyAsync(IExecutionContext ctx, IGameApiClient client) |
| 113 | + { |
| 114 | + try |
| 115 | + { |
| 116 | + var keyBase64 = await _secretService.GetValueWithRetryAsync(ctx, client, ENCRYPTION_KEY_NAME); |
| 117 | + if (string.IsNullOrEmpty(keyBase64)) |
| 118 | + { |
| 119 | + throw new InvalidOperationException($"Encryption key '{ENCRYPTION_KEY_NAME}' not found in Secret Manager"); |
| 120 | + } |
| 121 | + |
| 122 | + var key = Convert.FromBase64String(keyBase64); |
| 123 | + if (key.Length != 32) // 256 bits = 32 bytes |
| 124 | + { |
| 125 | + throw new InvalidOperationException($"Encryption key must be 256 bits (32 bytes), but was {key.Length} bytes"); |
| 126 | + } |
| 127 | + |
| 128 | + return key; |
| 129 | + } |
| 130 | + catch (Exception ex) |
| 131 | + { |
| 132 | + _logger.LogError(ex, "Failed to retrieve encryption key from Secret Manager"); |
| 133 | + throw; |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments