| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.IO; |
| | 5 | | using System.Security.Cryptography; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Azure.Core.Cryptography; |
| | 9 | | using Azure.Storage.Cryptography.Models; |
| | 10 | |
|
| | 11 | | namespace Azure.Storage.Cryptography |
| | 12 | | { |
| | 13 | | internal class ClientSideEncryptor |
| | 14 | | { |
| | 15 | | private readonly IKeyEncryptionKey _keyEncryptionKey; |
| | 16 | | private readonly string _keyWrapAlgorithm; |
| | 17 | |
|
| 0 | 18 | | public ClientSideEncryptor(ClientSideEncryptionOptions options) |
| | 19 | | { |
| 0 | 20 | | _keyEncryptionKey = options.KeyEncryptionKey; |
| 0 | 21 | | _keyWrapAlgorithm = options.KeyWrapAlgorithm; |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Wraps the given read-stream in a CryptoStream and provides the metadata used to create |
| | 26 | | /// that stream. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="plaintext">Stream to wrap.</param> |
| | 29 | | /// <param name="async">Whether to wrap the content encryption key asynchronously.</param> |
| | 30 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | 31 | | /// <returns>The wrapped stream to read from and the encryption metadata for the wrapped stream.</returns> |
| | 32 | | public async Task<(Stream ciphertext, EncryptionData encryptionData)> EncryptInternal( |
| | 33 | | Stream plaintext, |
| | 34 | | bool async, |
| | 35 | | CancellationToken cancellationToken) |
| | 36 | | { |
| 0 | 37 | | if (_keyEncryptionKey == default || _keyWrapAlgorithm == default) |
| | 38 | | { |
| 0 | 39 | | throw Errors.ClientSideEncryption.MissingRequiredEncryptionResources(nameof(_keyEncryptionKey), nameof(_ |
| | 40 | | } |
| | 41 | |
|
| 0 | 42 | | var generatedKey = CreateKey(Constants.ClientSideEncryption.EncryptionKeySizeBits); |
| 0 | 43 | | EncryptionData encryptionData = default; |
| 0 | 44 | | Stream ciphertext = default; |
| | 45 | |
|
| 0 | 46 | | using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider() { Key = generatedKey }) |
| | 47 | | { |
| 0 | 48 | | encryptionData = await EncryptionData.CreateInternalV1_0( |
| 0 | 49 | | contentEncryptionIv: aesProvider.IV, |
| 0 | 50 | | keyWrapAlgorithm: _keyWrapAlgorithm, |
| 0 | 51 | | contentEncryptionKey: generatedKey, |
| 0 | 52 | | keyEncryptionKey: _keyEncryptionKey, |
| 0 | 53 | | async: async, |
| 0 | 54 | | cancellationToken: cancellationToken).ConfigureAwait(false); |
| | 55 | |
|
| 0 | 56 | | ciphertext = new CryptoStream( |
| 0 | 57 | | plaintext, |
| 0 | 58 | | aesProvider.CreateEncryptor(), |
| 0 | 59 | | CryptoStreamMode.Read); |
| 0 | 60 | | } |
| | 61 | |
|
| 0 | 62 | | return (ciphertext, encryptionData); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Encrypts the given stream and provides the metadata used to encrypt. This method writes to a memory stream, |
| | 67 | | /// optimized for known-size data that will already be buffered in memory. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="plaintext">Stream to encrypt.</param> |
| | 70 | | /// <param name="async">Whether to wrap the content encryption key asynchronously.</param> |
| | 71 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | 72 | | /// <returns>The encrypted data and the encryption metadata for the wrapped stream.</returns> |
| | 73 | | public async Task<(byte[] ciphertext, EncryptionData encryptionData)> BufferedEncryptInternal( |
| | 74 | | Stream plaintext, |
| | 75 | | bool async, |
| | 76 | | CancellationToken cancellationToken) |
| | 77 | | { |
| 0 | 78 | | if (_keyEncryptionKey == default || _keyWrapAlgorithm == default) |
| | 79 | | { |
| 0 | 80 | | throw Errors.ClientSideEncryption.MissingRequiredEncryptionResources(nameof(_keyEncryptionKey), nameof(_ |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | var generatedKey = CreateKey(Constants.ClientSideEncryption.EncryptionKeySizeBits); |
| 0 | 84 | | EncryptionData encryptionData = default; |
| 0 | 85 | | var ciphertext = new MemoryStream(); |
| 0 | 86 | | byte[] bufferedCiphertext = default; |
| | 87 | |
|
| 0 | 88 | | using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider() { Key = generatedKey }) |
| | 89 | | { |
| 0 | 90 | | encryptionData = await EncryptionData.CreateInternalV1_0( |
| 0 | 91 | | contentEncryptionIv: aesProvider.IV, |
| 0 | 92 | | keyWrapAlgorithm: _keyWrapAlgorithm, |
| 0 | 93 | | contentEncryptionKey: generatedKey, |
| 0 | 94 | | keyEncryptionKey: _keyEncryptionKey, |
| 0 | 95 | | async: async, |
| 0 | 96 | | cancellationToken: cancellationToken).ConfigureAwait(false); |
| | 97 | |
|
| 0 | 98 | | var transformStream = new CryptoStream( |
| 0 | 99 | | ciphertext, |
| 0 | 100 | | aesProvider.CreateEncryptor(), |
| 0 | 101 | | CryptoStreamMode.Write); |
| | 102 | |
|
| 0 | 103 | | if (async) |
| | 104 | | { |
| 0 | 105 | | await plaintext.CopyToAsync(transformStream).ConfigureAwait(false); |
| | 106 | | } |
| | 107 | | else |
| | 108 | | { |
| 0 | 109 | | plaintext.CopyTo(transformStream); |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | transformStream.FlushFinalBlock(); |
| | 113 | |
|
| 0 | 114 | | bufferedCiphertext = ciphertext.ToArray(); |
| 0 | 115 | | } |
| | 116 | |
|
| 0 | 117 | | return (bufferedCiphertext, encryptionData); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Securely generate a key. |
| | 122 | | /// </summary> |
| | 123 | | /// <param name="numBits">Key size.</param> |
| | 124 | | /// <returns>The generated key bytes.</returns> |
| | 125 | | private static byte[] CreateKey(int numBits) |
| | 126 | | { |
| 0 | 127 | | using (var secureRng = new RNGCryptoServiceProvider()) |
| | 128 | | { |
| 0 | 129 | | var buff = new byte[numBits / 8]; |
| 0 | 130 | | secureRng.GetBytes(buff); |
| 0 | 131 | | return buff; |
| | 132 | | } |
| 0 | 133 | | } |
| | 134 | | } |
| | 135 | | } |