| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Text.Json; |
| | | 6 | | |
| | | 7 | | namespace Azure.Security.KeyVault.Certificates |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// An Azure Key Vault certificate. |
| | | 11 | | /// </summary> |
| | | 12 | | public class KeyVaultCertificate : IJsonDeserializable |
| | | 13 | | { |
| | | 14 | | private const string KeyIdPropertyName = "kid"; |
| | | 15 | | private const string SecretIdPropertyName = "sid"; |
| | | 16 | | private const string CERPropertyName = "cer"; |
| | | 17 | | |
| | | 18 | | private string _keyId; |
| | | 19 | | private string _secretId; |
| | | 20 | | |
| | 76 | 21 | | internal KeyVaultCertificate(CertificateProperties properties = null) |
| | | 22 | | { |
| | 76 | 23 | | Properties = properties ?? new CertificateProperties(); |
| | 76 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the identifier of the certificate. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public Uri Id => Properties.Id; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the name of the certificate. |
| | | 33 | | /// </summary> |
| | 32 | 34 | | public string Name => Properties.Name; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the identifier of the Key Vault Key backing the certificate. |
| | | 38 | | /// </summary> |
| | | 39 | | public Uri KeyId |
| | | 40 | | { |
| | 0 | 41 | | get => new Uri(_keyId); |
| | 0 | 42 | | internal set => _keyId = value?.ToString(); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the identifier of the Key Vault Secret which contains the PEM of PFX formatted content of the certifica |
| | | 47 | | /// </summary> |
| | | 48 | | public Uri SecretId |
| | | 49 | | { |
| | 0 | 50 | | get => new Uri(_secretId); |
| | 0 | 51 | | internal set => _secretId = value?.ToString(); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets additional properties of the <see cref="KeyVaultCertificate"/>. |
| | | 56 | | /// </summary> |
| | 360 | 57 | | public CertificateProperties Properties { get; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the CER formatted public X509 certificate. |
| | | 61 | | /// </summary> |
| | 88 | 62 | | public byte[] Cer { get; internal set; } |
| | | 63 | | |
| | | 64 | | internal virtual void ReadProperty(JsonProperty prop) |
| | | 65 | | { |
| | 492 | 66 | | switch (prop.Name) |
| | | 67 | | { |
| | | 68 | | case KeyIdPropertyName: |
| | 68 | 69 | | _keyId = prop.Value.GetString(); |
| | 68 | 70 | | break; |
| | | 71 | | |
| | | 72 | | case SecretIdPropertyName: |
| | 68 | 73 | | _secretId = prop.Value.GetString(); |
| | 68 | 74 | | break; |
| | | 75 | | |
| | | 76 | | case CERPropertyName: |
| | 68 | 77 | | Cer = prop.Value.GetBytesFromBase64(); |
| | 68 | 78 | | break; |
| | | 79 | | |
| | | 80 | | default: |
| | 288 | 81 | | Properties.ReadProperty(prop); |
| | | 82 | | break; |
| | | 83 | | } |
| | 288 | 84 | | } |
| | | 85 | | |
| | | 86 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | | 87 | | { |
| | 1304 | 88 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 89 | | { |
| | 576 | 90 | | ReadProperty(prop); |
| | | 91 | | } |
| | 76 | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |