|
1 | | -using System; |
2 | | -using System.Security.Cryptography; |
3 | | -using System.Security.Cryptography.X509Certificates; |
4 | | -using Mastercard.Developer.ClientEncryption.Core.Utils; |
5 | | -#pragma warning disable 1591 // "Missing XML comment for publicly visible type or member." |
6 | | - |
7 | | -namespace Mastercard.Developer.ClientEncryption.Core.Encryption |
8 | | -{ |
9 | | - /// <summary> |
10 | | - /// Encryption parameters for computing field level encryption/decryption. |
11 | | - /// </summary> |
12 | | - public class FieldLevelEncryptionParams : EncryptionParams |
13 | | - { |
14 | | - private const int SymmetricKeySize = 128; |
15 | | - |
16 | | - /// <summary> |
17 | | - /// Initialization vector value. |
18 | | - /// </summary> |
19 | | - public string IvValue { get; private set; } |
20 | | - |
21 | | - /// <summary> |
22 | | - /// Encrypted key value. |
23 | | - /// </summary> |
24 | | - public string EncryptedKeyValue { get; private set; } |
25 | | - |
26 | | - /// <summary> |
27 | | - /// Digest algorithm to be used for the RSA OAEP padding. Example: "SHA-512". |
28 | | - /// </summary> |
29 | | - public string OaepPaddingDigestAlgorithmValue { get; private set; } |
30 | | - |
31 | | - private FieldLevelEncryptionConfig Config { get; set; } |
32 | | - private byte[] SecretKeyBytes { get; set; } |
33 | | - private byte[] IvBytes { get; set; } |
34 | | - |
35 | | - private FieldLevelEncryptionParams() {} |
36 | | - |
37 | | - public FieldLevelEncryptionParams(FieldLevelEncryptionConfig config, string ivValue, string encryptedKeyValue, string oaepPaddingDigestAlgorithmValue = null) |
38 | | - { |
39 | | - IvValue = ivValue; |
40 | | - EncryptedKeyValue = encryptedKeyValue; |
41 | | - OaepPaddingDigestAlgorithmValue = oaepPaddingDigestAlgorithmValue; |
42 | | - Config = config; |
43 | | - } |
44 | | - |
45 | | - /// <summary> |
46 | | - /// Generate encryption parameters. |
47 | | - /// </summary> |
48 | | - /// <exception cref="EncryptionException"/> |
49 | | - public static FieldLevelEncryptionParams Generate(FieldLevelEncryptionConfig config) |
50 | | - { |
51 | | - // Generate a random IV |
52 | | - var ivBytes = GenerateIv(); |
53 | | - var ivValue = EncodingUtils.EncodeBytes(ivBytes, config.ValueEncoding); |
54 | | - |
55 | | - // Generate an AES secret key |
56 | | - var secretKeyBytes = GenerateSecretKey(); |
57 | | - |
58 | | - // Encrypt the secret key |
59 | | - var encryptedSecretKeyBytes = WrapSecretKey(config, secretKeyBytes); |
60 | | - var encryptedKeyValue = EncodingUtils.EncodeBytes(encryptedSecretKeyBytes, config.ValueEncoding); |
61 | | - |
62 | | - // Compute the OAEP padding digest algorithm |
63 | | - var oaepPaddingDigestAlgorithmValue = config.OaepPaddingDigestAlgorithm.Replace("-", string.Empty); |
64 | | - |
65 | | - return new FieldLevelEncryptionParams |
66 | | - { |
67 | | - IvValue = ivValue, |
68 | | - EncryptedKeyValue = encryptedKeyValue, |
69 | | - OaepPaddingDigestAlgorithmValue = oaepPaddingDigestAlgorithmValue, |
70 | | - Config = config, |
71 | | - SecretKeyBytes = secretKeyBytes, |
72 | | - IvBytes = ivBytes |
73 | | - }; |
74 | | - } |
75 | | - |
76 | | - private static byte[] GenerateIv() |
77 | | - { |
78 | | - using (var aes = Aes.Create()) |
79 | | - { |
80 | | - if (aes == null) |
81 | | - { |
82 | | - throw new EncryptionException("Failed to generate IV, AES instance is null!"); |
83 | | - } |
84 | | - |
85 | | - aes.GenerateIV(); |
86 | | - return aes.IV; |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - private static byte[] GenerateSecretKey() |
91 | | - { |
92 | | - using (var aes = Aes.Create()) |
93 | | - { |
94 | | - if (aes == null) |
95 | | - { |
96 | | - throw new EncryptionException("Failed to generate secret key, AES instance is null!"); |
97 | | - } |
98 | | - |
99 | | - aes.KeySize = SymmetricKeySize; |
100 | | - aes.GenerateKey(); |
101 | | - return aes.Key; |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - internal byte[] GetSecretKeyBytes() |
106 | | - { |
107 | | - try |
108 | | - { |
109 | | - if (SecretKeyBytes != null) |
110 | | - { |
111 | | - return SecretKeyBytes; |
112 | | - } |
113 | | - // Decrypt the AES secret key |
114 | | - var encryptedSecretKeyBytes = EncodingUtils.DecodeValue(EncryptedKeyValue, Config.ValueEncoding); |
115 | | - SecretKeyBytes = UnwrapSecretKey(Config, encryptedSecretKeyBytes, OaepPaddingDigestAlgorithmValue); |
116 | | - return SecretKeyBytes; |
117 | | - } |
118 | | - catch (Exception e) |
119 | | - { |
120 | | - throw new EncryptionException("Failed to decode and unwrap the provided secret key value!", e); |
121 | | - } |
122 | | - } |
123 | | - |
124 | | - internal byte[] GetIvBytes() |
125 | | - { |
126 | | - try |
127 | | - { |
128 | | - if (IvBytes != null) |
129 | | - { |
130 | | - return IvBytes; |
131 | | - } |
132 | | - // Decode the IV |
133 | | - IvBytes = EncodingUtils.DecodeValue(IvValue, Config.ValueEncoding); |
134 | | - return IvBytes; |
135 | | - } |
136 | | - catch (Exception e) |
137 | | - { |
138 | | - throw new EncryptionException("Failed to decode the provided IV value!", e); |
139 | | - } |
140 | | - } |
141 | | - } |
142 | | -} |
| 1 | +using System; |
| 2 | +using System.Security.Cryptography; |
| 3 | +using System.Security.Cryptography.X509Certificates; |
| 4 | +using Mastercard.Developer.ClientEncryption.Core.Utils; |
| 5 | +#pragma warning disable 1591 // "Missing XML comment for publicly visible type or member." |
| 6 | + |
| 7 | +namespace Mastercard.Developer.ClientEncryption.Core.Encryption |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Encryption parameters for computing field level encryption/decryption. |
| 11 | + /// </summary> |
| 12 | + public class FieldLevelEncryptionParams |
| 13 | + { |
| 14 | + private const int SymmetricKeySize = 128; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Initialization vector value. |
| 18 | + /// </summary> |
| 19 | + public string IvValue { get; private set; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Encrypted key value. |
| 23 | + /// </summary> |
| 24 | + public string EncryptedKeyValue { get; private set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Digest algorithm to be used for the RSA OAEP padding. Example: "SHA-512". |
| 28 | + /// </summary> |
| 29 | + public string OaepPaddingDigestAlgorithmValue { get; private set; } |
| 30 | + |
| 31 | + private FieldLevelEncryptionConfig Config { get; set; } |
| 32 | + private byte[] SecretKeyBytes { get; set; } |
| 33 | + private byte[] IvBytes { get; set; } |
| 34 | + |
| 35 | + private FieldLevelEncryptionParams() {} |
| 36 | + |
| 37 | + public FieldLevelEncryptionParams(FieldLevelEncryptionConfig config, string ivValue, string encryptedKeyValue, string oaepPaddingDigestAlgorithmValue = null) |
| 38 | + { |
| 39 | + IvValue = ivValue; |
| 40 | + EncryptedKeyValue = encryptedKeyValue; |
| 41 | + OaepPaddingDigestAlgorithmValue = oaepPaddingDigestAlgorithmValue; |
| 42 | + Config = config; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Generate encryption parameters. |
| 47 | + /// </summary> |
| 48 | + /// <exception cref="EncryptionException"/> |
| 49 | + public static FieldLevelEncryptionParams Generate(FieldLevelEncryptionConfig config) |
| 50 | + { |
| 51 | + // Generate a random IV |
| 52 | + var ivBytes = GenerateIv(); |
| 53 | + var ivValue = EncodingUtils.EncodeBytes(ivBytes, config.ValueEncoding); |
| 54 | + |
| 55 | + // Generate an AES secret key |
| 56 | + var secretKeyBytes = GenerateSecretKey(); |
| 57 | + |
| 58 | + // Encrypt the secret key |
| 59 | + var encryptedSecretKeyBytes = RsaEncryption.WrapSecretKey(config.EncryptionCertificate.GetRSAPublicKey(), secretKeyBytes, config.OaepPaddingDigestAlgorithm); |
| 60 | + var encryptedKeyValue = EncodingUtils.EncodeBytes(encryptedSecretKeyBytes, config.ValueEncoding); |
| 61 | + |
| 62 | + // Compute the OAEP padding digest algorithm |
| 63 | + var oaepPaddingDigestAlgorithmValue = config.OaepPaddingDigestAlgorithm.Replace("-", string.Empty); |
| 64 | + |
| 65 | + return new FieldLevelEncryptionParams |
| 66 | + { |
| 67 | + IvValue = ivValue, |
| 68 | + EncryptedKeyValue = encryptedKeyValue, |
| 69 | + OaepPaddingDigestAlgorithmValue = oaepPaddingDigestAlgorithmValue, |
| 70 | + Config = config, |
| 71 | + SecretKeyBytes = secretKeyBytes, |
| 72 | + IvBytes = ivBytes |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + private static byte[] GenerateIv() |
| 77 | + { |
| 78 | + using (var aes = Aes.Create()) |
| 79 | + { |
| 80 | + if (aes == null) |
| 81 | + { |
| 82 | + throw new EncryptionException("Failed to generate IV, AES instance is null!"); |
| 83 | + } |
| 84 | + |
| 85 | + aes.GenerateIV(); |
| 86 | + return aes.IV; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static byte[] GenerateSecretKey() |
| 91 | + { |
| 92 | + using (var aes = Aes.Create()) |
| 93 | + { |
| 94 | + if (aes == null) |
| 95 | + { |
| 96 | + throw new EncryptionException("Failed to generate secret key, AES instance is null!"); |
| 97 | + } |
| 98 | + |
| 99 | + aes.KeySize = SymmetricKeySize; |
| 100 | + aes.GenerateKey(); |
| 101 | + return aes.Key; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + internal byte[] GetSecretKeyBytes() |
| 106 | + { |
| 107 | + try |
| 108 | + { |
| 109 | + if (SecretKeyBytes != null) |
| 110 | + { |
| 111 | + return SecretKeyBytes; |
| 112 | + } |
| 113 | + // Decrypt the AES secret key |
| 114 | + var encryptedSecretKeyBytes = EncodingUtils.DecodeValue(EncryptedKeyValue, Config.ValueEncoding); |
| 115 | + SecretKeyBytes = RsaEncryption.UnwrapSecretKey(Config, encryptedSecretKeyBytes, OaepPaddingDigestAlgorithmValue); |
| 116 | + return SecretKeyBytes; |
| 117 | + } |
| 118 | + catch (Exception e) |
| 119 | + { |
| 120 | + throw new EncryptionException("Failed to decode and unwrap the provided secret key value!", e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + internal byte[] GetIvBytes() |
| 125 | + { |
| 126 | + try |
| 127 | + { |
| 128 | + if (IvBytes != null) |
| 129 | + { |
| 130 | + return IvBytes; |
| 131 | + } |
| 132 | + // Decode the IV |
| 133 | + IvBytes = EncodingUtils.DecodeValue(IvValue, Config.ValueEncoding); |
| 134 | + return IvBytes; |
| 135 | + } |
| 136 | + catch (Exception e) |
| 137 | + { |
| 138 | + throw new EncryptionException("Failed to decode the provided IV value!", e); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments