| | 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; |
| | 7 | | using System.Text.Json; |
| | 8 | | using System.Threading; |
| | 9 | | using Azure.Core; |
| | 10 | |
|
| | 11 | | namespace Azure.Security.KeyVault.Certificates |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Options for a certificate to be imported into Azure Key Vault. |
| | 15 | | /// </summary> |
| | 16 | | public class ImportCertificateOptions : IJsonSerializable |
| | 17 | | { |
| 2 | 18 | | private static readonly JsonEncodedText s_valuePropertyNameBytes = JsonEncodedText.Encode("value"); |
| 2 | 19 | | private static readonly JsonEncodedText s_policyPropertyNameBytes = JsonEncodedText.Encode("policy"); |
| 2 | 20 | | private static readonly JsonEncodedText s_passwordPropertyNameBytes = JsonEncodedText.Encode("pwd"); |
| 2 | 21 | | private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode("attributes"); |
| 2 | 22 | | private static readonly JsonEncodedText s_enabledPropertyNameBytes = JsonEncodedText.Encode("enabled"); |
| 2 | 23 | | private static readonly JsonEncodedText s_tagsPropertyNameBytes = JsonEncodedText.Encode("tags"); |
| | 24 | |
|
| | 25 | | private Dictionary<string, string> _tags; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Initializes a new instance of the <see cref="ImportCertificateOptions"/> class. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="name">A name for the imported certificate.</param> |
| | 31 | | /// <param name="certificate">The PFX or ASCII PEM formatted value of the certificate containing both the X.509 |
| | 32 | | /// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception> |
| | 33 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="certificate"/> is null.</ |
| 8 | 34 | | public ImportCertificateOptions(string name, byte[] certificate) |
| | 35 | | { |
| 8 | 36 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| 8 | 37 | | Argument.AssertNotNull(certificate, nameof(certificate)); |
| | 38 | |
|
| 8 | 39 | | Name = name; |
| 8 | 40 | | Certificate = certificate; |
| 8 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets the name of the certificate to import. |
| | 45 | | /// </summary> |
| 24 | 46 | | public string Name { get; } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets the PFX or PEM formatted value of the certificate containing both the X.509 certificates and the privat |
| | 50 | | /// </summary> |
| 16 | 51 | | public byte[] Certificate { get; } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Gets the policy which governs the lifecycle of the imported certificate and its properties when it is rotate |
| | 55 | | /// </summary> |
| 40 | 56 | | public CertificatePolicy Policy { get; set; } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Gets or sets the password protecting the certificate specified in the Value. |
| | 60 | | /// </summary> |
| 0 | 61 | | public string Password { get; set; } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Gets or sets a value indicating whether the merged certificate should be enabled. If null, the server defaul |
| | 65 | | /// </summary> |
| 0 | 66 | | public bool? Enabled { get; set; } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Gets the tags to be applied to the imported certificate. Although this collection cannot be set, it can be m |
| | 70 | | /// or initialized with a <see href="https://docs.microsoft.com/dotnet/csharp/programming-guide/classes-and-str |
| | 71 | | /// </summary> |
| 0 | 72 | | public IDictionary<string, string> Tags => LazyInitializer.EnsureInitialized(ref _tags); |
| | 73 | |
|
| | 74 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) |
| | 75 | | { |
| 8 | 76 | | if (Certificate != null) |
| | 77 | | { |
| 8 | 78 | | if (Policy != null && Policy.ContentType == CertificateContentType.Pem) |
| | 79 | | { |
| 4 | 80 | | string value = Encoding.ASCII.GetString(Certificate); |
| 4 | 81 | | json.WriteString(s_valuePropertyNameBytes, value); |
| | 82 | | } |
| | 83 | | else |
| | 84 | | { |
| 4 | 85 | | json.WriteBase64String(s_valuePropertyNameBytes, Certificate); |
| | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| 8 | 89 | | if (!string.IsNullOrEmpty(Password)) |
| | 90 | | { |
| 0 | 91 | | json.WriteString(s_passwordPropertyNameBytes, Password); |
| | 92 | | } |
| | 93 | |
|
| 8 | 94 | | if (Policy != null) |
| | 95 | | { |
| 8 | 96 | | json.WriteStartObject(s_policyPropertyNameBytes); |
| | 97 | |
|
| 8 | 98 | | ((IJsonSerializable)Policy).WriteProperties(json); |
| | 99 | |
|
| 8 | 100 | | json.WriteEndObject(); |
| | 101 | | } |
| | 102 | |
|
| 8 | 103 | | if (Enabled.HasValue) |
| | 104 | | { |
| 0 | 105 | | json.WriteStartObject(s_attributesPropertyNameBytes); |
| | 106 | |
|
| 0 | 107 | | json.WriteBoolean(s_enabledPropertyNameBytes, Enabled.Value); |
| | 108 | |
|
| 0 | 109 | | json.WriteEndObject(); |
| | 110 | | } |
| | 111 | |
|
| 8 | 112 | | if (!_tags.IsNullOrEmpty()) |
| | 113 | | { |
| 0 | 114 | | json.WriteStartObject(s_tagsPropertyNameBytes); |
| | 115 | |
|
| 0 | 116 | | foreach (KeyValuePair<string, string> kvp in _tags) |
| | 117 | | { |
| 0 | 118 | | json.WriteString(kvp.Key, kvp.Value); |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | json.WriteEndObject(); |
| | 122 | | } |
| 8 | 123 | | } |
| | 124 | | } |
| | 125 | | } |