| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text; |
| | 6 | | using System.Text.Json; |
| | 7 | | using Azure.Storage.Cryptography.Models; |
| | 8 | |
|
| | 9 | | namespace Azure.Storage.Queues.Specialized.Models |
| | 10 | | { |
| | 11 | | internal static class EncryptedMessageSerializer |
| | 12 | | { |
| | 13 | | private const string EncryptedMessage_EncryptedMessageTextName = "EncryptedMessageContents"; |
| | 14 | |
|
| | 15 | | #region Serialize |
| | 16 | | public static string Serialize(EncryptedMessage data) |
| | 17 | | { |
| 6 | 18 | | return Encoding.UTF8.GetString(SerializeEncryptedMessage(data).ToArray()); |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public static ReadOnlyMemory<byte> SerializeEncryptedMessage(EncryptedMessage message) |
| | 22 | | { |
| 6 | 23 | | var writer = new Core.ArrayBufferWriter<byte>(); |
| 6 | 24 | | using var json = new Utf8JsonWriter(writer); |
| | 25 | |
|
| 6 | 26 | | json.WriteStartObject(); |
| 6 | 27 | | WriteEncryptedMessage(json, message); |
| 6 | 28 | | json.WriteEndObject(); |
| | 29 | |
|
| 6 | 30 | | json.Flush(); |
| 6 | 31 | | return writer.WrittenMemory; |
| 6 | 32 | | } |
| | 33 | |
|
| | 34 | | public static void WriteEncryptedMessage(Utf8JsonWriter json, EncryptedMessage message) |
| | 35 | | { |
| 6 | 36 | | json.WriteString(EncryptedMessage_EncryptedMessageTextName, message.EncryptedMessageText); |
| | 37 | |
|
| 6 | 38 | | json.WriteStartObject(nameof(message.EncryptionData)); |
| 6 | 39 | | EncryptionDataSerializer.WriteEncryptionData(json, message.EncryptionData); |
| 6 | 40 | | json.WriteEndObject(); |
| 6 | 41 | | } |
| | 42 | | #endregion |
| | 43 | |
|
| | 44 | | #region Deserialize |
| | 45 | | public static bool TryDeserialize(string serializedData, out EncryptedMessage encryptedMessage) |
| | 46 | | { |
| | 47 | | try |
| | 48 | | { |
| 16 | 49 | | encryptedMessage = Deserialize(serializedData); |
| 2 | 50 | | return true; |
| | 51 | | } |
| | 52 | | // JsonException does not actually cover everything. InvalidOperationException can be thrown |
| | 53 | | // on some string inputs, as we can't assume input is even JSON. |
| 14 | 54 | | catch (Exception) |
| | 55 | | { |
| 14 | 56 | | encryptedMessage = default; |
| 14 | 57 | | return false; |
| | 58 | | } |
| 16 | 59 | | } |
| | 60 | |
|
| | 61 | | public static EncryptedMessage Deserialize(string serializedData) |
| | 62 | | { |
| 18 | 63 | | var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(serializedData)); |
| 18 | 64 | | return DeserializeEncryptedMessage(ref reader); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public static EncryptedMessage DeserializeEncryptedMessage(ref Utf8JsonReader reader) |
| | 68 | | { |
| 18 | 69 | | using JsonDocument json = JsonDocument.ParseValue(ref reader); |
| 12 | 70 | | JsonElement root = json.RootElement; |
| 12 | 71 | | return ReadEncryptionData(root); |
| 4 | 72 | | } |
| | 73 | |
|
| | 74 | | private static EncryptedMessage ReadEncryptionData(JsonElement root) |
| | 75 | | { |
| 12 | 76 | | var data = new EncryptedMessage(); |
| 52 | 77 | | foreach (var property in root.EnumerateObject()) |
| | 78 | | { |
| 18 | 79 | | ReadPropertyValue(data, property); |
| | 80 | | } |
| | 81 | |
|
| 4 | 82 | | if (data.EncryptionData == default || data.EncryptedMessageText == default) |
| | 83 | | { |
| 0 | 84 | | throw new FormatException($"Failed to find non-optional properties while deserializing `{typeof(Encrypte |
| | 85 | | } |
| | 86 | |
|
| 4 | 87 | | return data; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | private static void ReadPropertyValue(EncryptedMessage data, JsonProperty property) |
| | 91 | | { |
| 18 | 92 | | if (property.NameEquals(EncryptedMessage_EncryptedMessageTextName)) |
| | 93 | | { |
| 6 | 94 | | data.EncryptedMessageText = property.Value.GetString(); |
| | 95 | | } |
| 12 | 96 | | else if (property.NameEquals(nameof(data.EncryptionData))) |
| | 97 | | { |
| 6 | 98 | | data.EncryptionData = EncryptionDataSerializer.ReadEncryptionData(property.Value); |
| | 99 | | } |
| | 100 | | else |
| | 101 | | { |
| 6 | 102 | | throw new FormatException($"Failed to deserialize `{typeof(EncryptedMessage).FullName}`. Unrecognized pr |
| | 103 | | } |
| | 104 | | } |
| | 105 | | #endregion |
| | 106 | | } |
| | 107 | | } |