| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Reflection; |
| | 7 | | using System.Runtime.InteropServices; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Azure.Core.Cryptography; |
| | 11 | | using Metadata = System.Collections.Generic.IDictionary<string, string>; |
| | 12 | |
|
| | 13 | | namespace Azure.Storage.Cryptography.Models |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Represents the encryption data that is stored on the service. |
| | 17 | | /// </summary> |
| | 18 | | internal class EncryptionData |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// The blob encryption mode. |
| | 22 | | /// </summary> |
| 24 | 23 | | public string EncryptionMode { get; set; } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// A <see cref="KeyEnvelope"/> object that stores the wrapping algorithm, key identifier and the encrypted key. |
| | 27 | | /// </summary> |
| 24 | 28 | | public KeyEnvelope WrappedContentKey { get; set; } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// The encryption agent. |
| | 32 | | /// </summary> |
| 24 | 33 | | public EncryptionAgent EncryptionAgent { get; set; } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// The content encryption IV. |
| | 37 | | /// </summary> |
| 24 | 38 | | public byte[] ContentEncryptionIV { get; set; } |
| | 39 | |
|
| | 40 | | #pragma warning disable CA2227 // Collection properties should be read only |
| | 41 | | /// <summary> |
| | 42 | | /// Metadata for encryption. Currently used only for storing the encryption library, but may contain other data. |
| | 43 | | /// </summary> |
| 24 | 44 | | public Metadata KeyWrappingMetadata { get; set; } |
| | 45 | | #pragma warning restore CA2227 // Collection properties should be read only |
| | 46 | |
|
| | 47 | | internal static async Task<EncryptionData> CreateInternalV1_0( |
| | 48 | | byte[] contentEncryptionIv, |
| | 49 | | string keyWrapAlgorithm, |
| | 50 | | byte[] contentEncryptionKey, |
| | 51 | | IKeyEncryptionKey keyEncryptionKey, |
| | 52 | | bool async, |
| | 53 | | CancellationToken cancellationToken) |
| 6 | 54 | | => new EncryptionData() |
| 6 | 55 | | { |
| 6 | 56 | | EncryptionMode = Constants.ClientSideEncryption.EncryptionMode, |
| 6 | 57 | | ContentEncryptionIV = contentEncryptionIv, |
| 6 | 58 | | EncryptionAgent = new EncryptionAgent() |
| 6 | 59 | | { |
| 6 | 60 | | EncryptionAlgorithm = ClientSideEncryptionAlgorithm.AesCbc256, |
| 6 | 61 | | EncryptionVersion = ClientSideEncryptionVersion.V1_0 |
| 6 | 62 | | }, |
| 6 | 63 | | KeyWrappingMetadata = new Dictionary<string, string>() |
| 6 | 64 | | { |
| 6 | 65 | | { Constants.ClientSideEncryption.AgentMetadataKey, AgentString } |
| 6 | 66 | | }, |
| 6 | 67 | | WrappedContentKey = new KeyEnvelope() |
| 6 | 68 | | { |
| 6 | 69 | | Algorithm = keyWrapAlgorithm, |
| 6 | 70 | | EncryptedKey = async |
| 6 | 71 | | ? await keyEncryptionKey.WrapKeyAsync(keyWrapAlgorithm, contentEncryptionKey, cancellationToken) |
| 6 | 72 | | : keyEncryptionKey.WrapKey(keyWrapAlgorithm, contentEncryptionKey, cancellationToken), |
| 6 | 73 | | KeyId = keyEncryptionKey.KeyId |
| 6 | 74 | | } |
| 6 | 75 | | }; |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Singleton string identifying this encryption library. |
| | 79 | | /// </summary> |
| 8 | 80 | | private static string AgentString { get; } = GenerateAgentString(); |
| | 81 | |
|
| | 82 | | private static string GenerateAgentString() |
| | 83 | | { |
| 2 | 84 | | Assembly assembly = typeof(EncryptionData).Assembly; |
| 2 | 85 | | var platformInformation = $"({RuntimeInformation.FrameworkDescription}; {RuntimeInformation.OSDescription})" |
| 2 | 86 | | return $"azsdk-net-{assembly.GetName().Name}/{assembly.GetCustomAttribute<AssemblyInformationalVersionAttrib |
| | 87 | | } |
| | 88 | | } |
| | 89 | | } |