| | | 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 | | using Azure.Core; |
| | | 8 | | |
| | | 9 | | namespace Azure.Security.KeyVault.Keys |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// The properties needed to import a key. |
| | | 13 | | /// </summary> |
| | | 14 | | public class ImportKeyOptions : IJsonSerializable |
| | | 15 | | { |
| | | 16 | | private const string KeyPropertyName = "key"; |
| | | 17 | | private const string TagsPropertyName = "tags"; |
| | | 18 | | private const string HsmPropertyName = "hsm"; |
| | | 19 | | |
| | 2 | 20 | | private static readonly JsonEncodedText s_keyPropertyNameBytes = JsonEncodedText.Encode(KeyPropertyName); |
| | 2 | 21 | | private static readonly JsonEncodedText s_tagsPropertyNameBytes = JsonEncodedText.Encode(TagsPropertyName); |
| | 2 | 22 | | private static readonly JsonEncodedText s_hsmPropertyNameBytes = JsonEncodedText.Encode(HsmPropertyName); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="ImportKeyOptions"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="name">The name of the key to import.</param> |
| | | 28 | | /// <param name="keyMaterial">A <see cref="JsonWebKey"/> containing properties of the key to import.</param> |
| | | 29 | | /// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception> |
| | | 30 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="keyMaterial"/> is null.</ |
| | 36 | 31 | | public ImportKeyOptions(string name, JsonWebKey keyMaterial) |
| | | 32 | | { |
| | 36 | 33 | | Argument.AssertNotNull(keyMaterial, nameof(keyMaterial)); |
| | | 34 | | |
| | 36 | 35 | | Properties = new KeyProperties(name); |
| | 36 | 36 | | Key = keyMaterial; |
| | 36 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets the name of the key to import. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public string Name => Properties.Name; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets the cryptographic key, the key type, and the operations you can perform using the key. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <remarks> |
| | | 48 | | /// See http://tools.ietf.org/html/draft-ietf-jose-json-web-key-18 for specifications of a JSON web key. |
| | | 49 | | /// </remarks> |
| | 72 | 50 | | public JsonWebKey Key { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets or sets a value indicating whether to import the key into a hardware security module (HSM). |
| | | 54 | | /// </summary> |
| | 0 | 55 | | public bool? HardwareProtected { get; set; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets additional properties of the <see cref="KeyVaultKey"/>. |
| | | 59 | | /// </summary> |
| | 72 | 60 | | public KeyProperties Properties { get; } |
| | | 61 | | |
| | | 62 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) |
| | | 63 | | { |
| | 36 | 64 | | if (Key != null) |
| | | 65 | | { |
| | 36 | 66 | | json.WriteStartObject(s_keyPropertyNameBytes); |
| | | 67 | | |
| | 36 | 68 | | Key.WriteProperties(json); |
| | | 69 | | |
| | 36 | 70 | | json.WriteEndObject(); |
| | | 71 | | } |
| | | 72 | | |
| | 36 | 73 | | Properties.WriteAttributes(json); |
| | | 74 | | |
| | 36 | 75 | | if (Properties._tags != null && Properties._tags.Count > 0) |
| | | 76 | | { |
| | 0 | 77 | | json.WriteStartObject(s_tagsPropertyNameBytes); |
| | | 78 | | |
| | 0 | 79 | | foreach (KeyValuePair<string, string> kvp in Properties._tags) |
| | | 80 | | { |
| | 0 | 81 | | json.WriteString(kvp.Key, kvp.Value); |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | json.WriteEndObject(); |
| | | 85 | | } |
| | | 86 | | |
| | 36 | 87 | | if (HardwareProtected.HasValue) |
| | | 88 | | { |
| | 0 | 89 | | json.WriteBoolean(s_hsmPropertyNameBytes, HardwareProtected.Value); |
| | | 90 | | } |
| | 36 | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |