| | | 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> |
| | 0 | 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> |
| | 0 | 28 | | public KeyEnvelope WrappedContentKey { get; set; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The encryption agent. |
| | | 32 | | /// </summary> |
| | 0 | 33 | | public EncryptionAgent EncryptionAgent { get; set; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The content encryption IV. |
| | | 37 | | /// </summary> |
| | 0 | 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> |
| | 0 | 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) |
| | 0 | 54 | | => new EncryptionData() |
| | 0 | 55 | | { |
| | 0 | 56 | | EncryptionMode = Constants.ClientSideEncryption.EncryptionMode, |
| | 0 | 57 | | ContentEncryptionIV = contentEncryptionIv, |
| | 0 | 58 | | EncryptionAgent = new EncryptionAgent() |
| | 0 | 59 | | { |
| | 0 | 60 | | EncryptionAlgorithm = ClientSideEncryptionAlgorithm.AesCbc256, |
| | 0 | 61 | | EncryptionVersion = ClientSideEncryptionVersion.V1_0 |
| | 0 | 62 | | }, |
| | 0 | 63 | | KeyWrappingMetadata = new Dictionary<string, string>() |
| | 0 | 64 | | { |
| | 0 | 65 | | { Constants.ClientSideEncryption.AgentMetadataKey, AgentString } |
| | 0 | 66 | | }, |
| | 0 | 67 | | WrappedContentKey = new KeyEnvelope() |
| | 0 | 68 | | { |
| | 0 | 69 | | Algorithm = keyWrapAlgorithm, |
| | 0 | 70 | | EncryptedKey = async |
| | 0 | 71 | | ? await keyEncryptionKey.WrapKeyAsync(keyWrapAlgorithm, contentEncryptionKey, cancellationToken) |
| | 0 | 72 | | : keyEncryptionKey.WrapKey(keyWrapAlgorithm, contentEncryptionKey, cancellationToken), |
| | 0 | 73 | | KeyId = keyEncryptionKey.KeyId |
| | 0 | 74 | | } |
| | 0 | 75 | | }; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Singleton string identifying this encryption library. |
| | | 79 | | /// </summary> |
| | 0 | 80 | | private static string AgentString { get; } = GenerateAgentString(); |
| | | 81 | | |
| | | 82 | | private static string GenerateAgentString() |
| | | 83 | | { |
| | 0 | 84 | | Assembly assembly = typeof(EncryptionData).Assembly; |
| | 0 | 85 | | var platformInformation = $"({RuntimeInformation.FrameworkDescription}; {RuntimeInformation.OSDescription})" |
| | 0 | 86 | | return $"azsdk-net-{assembly.GetName().Name}/{assembly.GetCustomAttribute<AssemblyInformationalVersionAttrib |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |