| | 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.Text.Json; |
| | 7 | |
|
| | 8 | | namespace Azure.Security.KeyVault.Keys |
| | 9 | | { |
| | 10 | | internal class KeyRequestParameters : IJsonSerializable |
| | 11 | | { |
| | 12 | | private const string KeyTypePropertyName = "kty"; |
| | 13 | | private const string KeySizePropertyName = "key_size"; |
| | 14 | | private const string KeyOpsPropertyName = "key_ops"; |
| | 15 | | private const string CurveNamePropertyName = "crv"; |
| | 16 | | private const string AttributesPropertyName = "attributes"; |
| | 17 | | private const string TagsPropertyName = "tags"; |
| | 18 | |
|
| 2 | 19 | | private static readonly JsonEncodedText s_keyTypePropertyNameBytes = JsonEncodedText.Encode(KeyTypePropertyName) |
| 2 | 20 | | private static readonly JsonEncodedText s_keySizePropertyNameBytes = JsonEncodedText.Encode(KeySizePropertyName) |
| 2 | 21 | | private static readonly JsonEncodedText s_keyOpsPropertyNameBytes = JsonEncodedText.Encode(KeyOpsPropertyName); |
| 2 | 22 | | private static readonly JsonEncodedText s_curveNamePropertyNameBytes = JsonEncodedText.Encode(CurveNamePropertyN |
| 2 | 23 | | private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode(AttributesPropert |
| 2 | 24 | | private static readonly JsonEncodedText s_tagsPropertyNameBytes = JsonEncodedText.Encode(TagsPropertyName); |
| | 25 | |
|
| | 26 | | private KeyAttributes _attributes; |
| | 27 | |
|
| 1032 | 28 | | public KeyType KeyType { get; set; } |
| 400 | 29 | | public int? KeySize { get; set; } |
| 0 | 30 | | public KeyAttributes Attributes { get; set; } |
| 496 | 31 | | public IList<KeyOperation> KeyOperations { get; set; } |
| 468 | 32 | | public bool? Enabled { get => _attributes.Enabled; set => _attributes.Enabled = value; } |
| 320 | 33 | | public DateTimeOffset? NotBefore { get => _attributes.NotBefore; set => _attributes.NotBefore = value; } |
| 356 | 34 | | public DateTimeOffset? Expires { get => _attributes.ExpiresOn; set => _attributes.ExpiresOn = value; } |
| 464 | 35 | | public IDictionary<string, string> Tags { get; set; } |
| 512 | 36 | | public KeyCurveName? Curve { get; set; } |
| | 37 | |
|
| 72 | 38 | | internal KeyRequestParameters(KeyProperties key, IEnumerable<KeyOperation> operations) |
| | 39 | | { |
| 72 | 40 | | if (key.Enabled.HasValue) |
| | 41 | | { |
| 72 | 42 | | Enabled = key.Enabled.Value; |
| | 43 | | } |
| 72 | 44 | | if (key.ExpiresOn.HasValue) |
| | 45 | | { |
| 36 | 46 | | Expires = key.ExpiresOn.Value; |
| | 47 | | } |
| 72 | 48 | | if (key.NotBefore.HasValue) |
| | 49 | | { |
| 0 | 50 | | NotBefore = key.NotBefore.Value; |
| | 51 | | } |
| 72 | 52 | | if (key.Tags != null && key.Tags.Count > 0) |
| | 53 | | { |
| 24 | 54 | | Tags = new Dictionary<string, string>(key.Tags); |
| | 55 | | } |
| 72 | 56 | | if (operations != null) |
| | 57 | | { |
| 36 | 58 | | KeyOperations = new List<KeyOperation>(operations); |
| | 59 | | } |
| 72 | 60 | | } |
| | 61 | |
|
| 320 | 62 | | internal KeyRequestParameters(KeyType type, CreateKeyOptions options = default) |
| | 63 | | { |
| 320 | 64 | | KeyType = type; |
| 320 | 65 | | if (options != null) |
| | 66 | | { |
| 156 | 67 | | if (options.Enabled.HasValue) |
| | 68 | | { |
| 4 | 69 | | Enabled = options.Enabled.Value; |
| | 70 | | } |
| 156 | 71 | | if (options.ExpiresOn.HasValue) |
| | 72 | | { |
| 4 | 73 | | Expires = options.ExpiresOn.Value; |
| | 74 | | } |
| 156 | 75 | | if (options.NotBefore.HasValue) |
| | 76 | | { |
| 4 | 77 | | NotBefore = options.NotBefore.Value; |
| | 78 | | } |
| 156 | 79 | | if (options.KeyOperations != null && options.KeyOperations.Count > 0) |
| | 80 | | { |
| 16 | 81 | | KeyOperations = new List<KeyOperation>(options.KeyOperations); |
| | 82 | | } |
| 156 | 83 | | if (options.Tags != null && options.Tags.Count > 0) |
| | 84 | | { |
| 12 | 85 | | Tags = new Dictionary<string, string>(options.Tags); |
| | 86 | | } |
| | 87 | | } |
| 320 | 88 | | } |
| | 89 | |
|
| | 90 | | internal KeyRequestParameters(CreateEcKeyOptions ecKey) |
| 108 | 91 | | : this(ecKey.KeyType, ecKey) |
| | 92 | | { |
| 108 | 93 | | if (ecKey.CurveName.HasValue) |
| | 94 | | { |
| 60 | 95 | | Curve = ecKey.CurveName.Value; |
| | 96 | | } |
| 108 | 97 | | } |
| | 98 | |
|
| | 99 | | internal KeyRequestParameters(CreateRsaKeyOptions rsaKey) |
| 44 | 100 | | : this(rsaKey.KeyType, rsaKey) |
| | 101 | | { |
| 44 | 102 | | if (rsaKey.KeySize.HasValue) |
| | 103 | | { |
| 4 | 104 | | KeySize = rsaKey.KeySize.Value; |
| | 105 | | } |
| 44 | 106 | | } |
| | 107 | |
|
| | 108 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) |
| | 109 | | { |
| 392 | 110 | | if (KeyType != default) |
| | 111 | | { |
| 320 | 112 | | json.WriteString(s_keyTypePropertyNameBytes, KeyType.ToString()); |
| | 113 | | } |
| 392 | 114 | | if (KeySize.HasValue) |
| | 115 | | { |
| 4 | 116 | | json.WriteNumber(s_keySizePropertyNameBytes, KeySize.Value); |
| | 117 | | } |
| 392 | 118 | | if (Curve.HasValue) |
| | 119 | | { |
| 60 | 120 | | json.WriteString(s_curveNamePropertyNameBytes, Curve.Value.ToString()); |
| | 121 | | } |
| 392 | 122 | | if (Enabled.HasValue || NotBefore.HasValue || Expires.HasValue) |
| | 123 | | { |
| 76 | 124 | | json.WriteStartObject(s_attributesPropertyNameBytes); |
| | 125 | |
|
| 76 | 126 | | _attributes.WriteProperties(json); |
| | 127 | |
|
| 76 | 128 | | json.WriteEndObject(); |
| | 129 | | } |
| 392 | 130 | | if (!KeyOperations.IsNullOrEmpty()) |
| | 131 | | { |
| 52 | 132 | | json.WriteStartArray(s_keyOpsPropertyNameBytes); |
| 344 | 133 | | foreach (KeyOperation operation in KeyOperations) |
| | 134 | | { |
| 120 | 135 | | json.WriteStringValue(operation.ToString()); |
| | 136 | | } |
| 52 | 137 | | json.WriteEndArray(); |
| | 138 | | } |
| 392 | 139 | | if (!Tags.IsNullOrEmpty()) |
| | 140 | | { |
| 36 | 141 | | json.WriteStartObject(s_tagsPropertyNameBytes); |
| | 142 | |
|
| 216 | 143 | | foreach (KeyValuePair<string, string> kvp in Tags) |
| | 144 | | { |
| 72 | 145 | | json.WriteString(kvp.Key, kvp.Value); |
| | 146 | | } |
| | 147 | |
|
| 36 | 148 | | json.WriteEndObject(); |
| | 149 | | } |
| 392 | 150 | | } |
| | 151 | | } |
| | 152 | | } |