| | | 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.Certificates |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// A policy which governs the lifecycle a properties of a certificate managed by Azure Key Vault. |
| | | 13 | | /// </summary> |
| | | 14 | | public class CertificatePolicy : IJsonSerializable, IJsonDeserializable |
| | | 15 | | { |
| | | 16 | | private const string DefaultSubject = "CN=DefaultPolicy"; |
| | | 17 | | private const string DefaultIssuerName = "Self"; |
| | | 18 | | |
| | | 19 | | private const string KeyTypePropertyName = "kty"; |
| | | 20 | | private const string ReuseKeyPropertyName = "reuse_key"; |
| | | 21 | | private const string ExportablePropertyName = "exportable"; |
| | | 22 | | private const string CurveNamePropertyName = "crv"; |
| | | 23 | | private const string KeySizePropertyName = "key_size"; |
| | | 24 | | private const string KeyPropsPropertyName = "key_props"; |
| | | 25 | | private const string SecretPropsPropertyName = "secret_props"; |
| | | 26 | | private const string X509PropsPropertyName = "x509_props"; |
| | | 27 | | private const string LifetimeActionsPropertyName = "lifetime_actions"; |
| | | 28 | | private const string IssuerPropertyName = "issuer"; |
| | | 29 | | private const string AttributesPropertyName = "attributes"; |
| | | 30 | | private const string ContentTypePropertyName = "contentType"; |
| | | 31 | | private const string SubjectPropertyName = "subject"; |
| | | 32 | | private const string SansPropertyName = "sans"; |
| | | 33 | | private const string KeyUsagePropertyName = "key_usage"; |
| | | 34 | | private const string EkusPropertyName = "ekus"; |
| | | 35 | | private const string ValidityMonthsPropertyName = "validity_months"; |
| | | 36 | | private const string EnabledPropertyName = "enabled"; |
| | | 37 | | private const string CreatedPropertyName = "created"; |
| | | 38 | | private const string UpdatedPropertyName = "updated"; |
| | | 39 | | |
| | 2 | 40 | | private static readonly JsonEncodedText s_keyTypePropertyNameBytes = JsonEncodedText.Encode(KeyTypePropertyName) |
| | 2 | 41 | | private static readonly JsonEncodedText s_reuseKeyPropertyNameBytes = JsonEncodedText.Encode(ReuseKeyPropertyNam |
| | 2 | 42 | | private static readonly JsonEncodedText s_exportablePropertyNameBytes = JsonEncodedText.Encode(ExportablePropert |
| | 2 | 43 | | private static readonly JsonEncodedText s_curveNamePropertyNameBytes = JsonEncodedText.Encode(CurveNamePropertyN |
| | 2 | 44 | | private static readonly JsonEncodedText s_keySizePropertyNameBytes = JsonEncodedText.Encode(KeySizePropertyName) |
| | 2 | 45 | | private static readonly JsonEncodedText s_lifetimeActionsPropertyNameBytes = JsonEncodedText.Encode(LifetimeActi |
| | 2 | 46 | | private static readonly JsonEncodedText s_issuerPropertyNameBytes = JsonEncodedText.Encode(IssuerPropertyName); |
| | 2 | 47 | | private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode(AttributesPropert |
| | 2 | 48 | | private static readonly JsonEncodedText s_keyPropsPropertyNameBytes = JsonEncodedText.Encode(KeyPropsPropertyNam |
| | 2 | 49 | | private static readonly JsonEncodedText s_secretPropsPropertyNameBytes = JsonEncodedText.Encode(SecretPropsPrope |
| | 2 | 50 | | private static readonly JsonEncodedText s_x509PropsPropertyNameBytes = JsonEncodedText.Encode(X509PropsPropertyN |
| | 2 | 51 | | private static readonly JsonEncodedText s_contentTypePropertyNameBytes = JsonEncodedText.Encode(ContentTypePrope |
| | 2 | 52 | | private static readonly JsonEncodedText s_subjectPropertyNameBytes = JsonEncodedText.Encode(SubjectPropertyName) |
| | 2 | 53 | | private static readonly JsonEncodedText s_sansPropertyNameBytes = JsonEncodedText.Encode(SansPropertyName); |
| | 2 | 54 | | private static readonly JsonEncodedText s_keyUsagePropertyNameBytes = JsonEncodedText.Encode(KeyUsagePropertyNam |
| | 2 | 55 | | private static readonly JsonEncodedText s_ekusPropertyNameBytes = JsonEncodedText.Encode(EkusPropertyName); |
| | 2 | 56 | | private static readonly JsonEncodedText s_validityMonthsPropertyNameBytes = JsonEncodedText.Encode(ValidityMonth |
| | 2 | 57 | | private static readonly JsonEncodedText s_enabledPropertyNameBytes = JsonEncodedText.Encode(EnabledPropertyName) |
| | | 58 | | |
| | | 59 | | private IssuerParameters _issuer; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Initializes a new instance of the <see cref="CertificatePolicy"/> class. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="issuerName">The name of an issuer for the certificate, including values from <see cref="WellKno |
| | | 65 | | /// <param name="subject">The subject name of the certificate, such as "CN=contoso.com".</param> |
| | | 66 | | /// <exception cref="ArgumentException"><paramref name="subject"/> or <paramref name="issuerName"/> is empty.</e |
| | | 67 | | /// <exception cref="ArgumentNullException"><paramref name="subject"/> or <paramref name="issuerName"/> is null. |
| | 38 | 68 | | public CertificatePolicy(string issuerName, string subject) |
| | | 69 | | { |
| | 38 | 70 | | Argument.AssertNotNullOrEmpty(issuerName, nameof(issuerName)); |
| | 32 | 71 | | Argument.AssertNotNullOrEmpty(subject, nameof(subject)); |
| | | 72 | | |
| | 28 | 73 | | IssuerName = issuerName; |
| | 28 | 74 | | Subject = subject; |
| | 28 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Initializes a new instance of the <see cref="CertificatePolicy"/> class. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="issuerName">The name of an issuer for the certificate, including values from <see cref="WellKno |
| | | 81 | | /// <param name="subjectAlternativeNames">The subject alternative names (SANs) of the certificate.</param> |
| | | 82 | | /// <exception cref="ArgumentException"><paramref name="issuerName"/> is empty or <paramref name="subjectAlterna |
| | | 83 | | /// <exception cref="ArgumentNullException"><paramref name="subjectAlternativeNames"/> or <paramref name="issuer |
| | 10 | 84 | | public CertificatePolicy(string issuerName, SubjectAlternativeNames subjectAlternativeNames) |
| | | 85 | | { |
| | 10 | 86 | | Argument.AssertNotNullOrEmpty(issuerName, nameof(issuerName)); |
| | 8 | 87 | | Argument.AssertNotNull(subjectAlternativeNames, nameof(subjectAlternativeNames)); |
| | 4 | 88 | | if (subjectAlternativeNames.IsEmpty) |
| | | 89 | | { |
| | 4 | 90 | | throw new ArgumentException("Value cannot contain empty collection properties.", nameof(subjectAlternati |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | SubjectAlternativeNames = subjectAlternativeNames; |
| | 0 | 94 | | IssuerName = issuerName; |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Initializes a new instance of the <see cref="CertificatePolicy"/> class. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="issuerName">The name of an issuer for the certificate, including values from <see cref="WellKno |
| | | 101 | | /// <param name="subject">The subject name of the certificate, such as "CN=contoso.com".</param> |
| | | 102 | | /// <param name="subjectAlternativeNames">The subject alternative names (SANs) of the certificate.</param> |
| | | 103 | | /// <exception cref="ArgumentException"><paramref name="subject"/> or <paramref name="issuerName"/> is empty, or |
| | | 104 | | /// <exception cref="ArgumentNullException"><paramref name="subject"/>, <paramref name="issuerName"/>, or <param |
| | 8 | 105 | | public CertificatePolicy(string issuerName, string subject, SubjectAlternativeNames subjectAlternativeNames) |
| | | 106 | | { |
| | 8 | 107 | | Argument.AssertNotNullOrEmpty(issuerName, nameof(issuerName)); |
| | 4 | 108 | | Argument.AssertNotNullOrEmpty(subject, nameof(subject)); |
| | 0 | 109 | | Argument.AssertNotNull(subjectAlternativeNames, nameof(subjectAlternativeNames)); |
| | 0 | 110 | | if (subjectAlternativeNames.IsEmpty) |
| | | 111 | | { |
| | 0 | 112 | | throw new ArgumentException("Value cannot contain empty collection properties.", nameof(subjectAlternati |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | Subject = subject; |
| | 0 | 116 | | IssuerName = issuerName; |
| | 0 | 117 | | SubjectAlternativeNames = subjectAlternativeNames; |
| | 0 | 118 | | } |
| | | 119 | | |
| | 146 | 120 | | internal CertificatePolicy() |
| | | 121 | | { |
| | 146 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Gets a new <see cref="CertificatePolicy"/> suitable for self-signed certificate requests. |
| | | 126 | | /// You should change the <see cref="Subject"/> before passing this policy to create a certificate. |
| | | 127 | | /// </summary> |
| | 2 | 128 | | public static CertificatePolicy Default => new CertificatePolicy(DefaultIssuerName, DefaultSubject); |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Gets or sets the type of backing key to be generated when issuing new certificates. |
| | | 132 | | /// </summary> |
| | 362 | 133 | | public CertificateKeyType? KeyType { get; set; } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Gets or sets a value indicating whether the certificate key should be reused when rotating the certificate. |
| | | 137 | | /// </summary> |
| | 314 | 138 | | public bool? ReuseKey { get; set; } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets or sets a value indicating whether the certificate key is exportable from the vault or secure certifica |
| | | 142 | | /// </summary> |
| | 304 | 143 | | public bool? Exportable { get; set; } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets or sets the curve which back an Elliptic Curve (EC) key. |
| | | 147 | | /// </summary> |
| | 108 | 148 | | public CertificateKeyCurveName? KeyCurveName { get; set; } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Gets or sets the size of the RSA key. The value must be a valid RSA key length such as 2048 or 4092. |
| | | 152 | | /// </summary> |
| | 196 | 153 | | public int? KeySize { get; set; } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Gets the subject name of a certificate. |
| | | 157 | | /// </summary> |
| | 412 | 158 | | public string Subject { get; internal set; } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Gets the subject alternative names (SANs) of a certificate. |
| | | 162 | | /// </summary> |
| | 0 | 163 | | public SubjectAlternativeNames SubjectAlternativeNames { get; internal set; } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Gets the name of an issuer for a certificate. |
| | | 167 | | /// </summary> |
| | | 168 | | public string IssuerName |
| | | 169 | | { |
| | 102 | 170 | | get => _issuer.IssuerName; |
| | 112 | 171 | | internal set => _issuer.IssuerName = value; |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Gets or sets the <see cref="CertificateContentType"/> of the certificate when downloaded from GetSecret. |
| | | 176 | | /// </summary> |
| | 400 | 177 | | public CertificateContentType? ContentType { get; set; } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Gets or sets the certificate type of a certificate. |
| | | 181 | | /// </summary> |
| | | 182 | | public string CertificateType |
| | | 183 | | { |
| | 18 | 184 | | get => _issuer.CertificateType; |
| | 0 | 185 | | set => _issuer.CertificateType = value; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Gets or sets a value indicating whether a certificate should be published to the certificate transparency li |
| | | 190 | | /// </summary> |
| | | 191 | | public bool? CertificateTransparency |
| | | 192 | | { |
| | 26 | 193 | | get => _issuer.CertificateTransparency; |
| | 72 | 194 | | set => _issuer.CertificateTransparency = value; |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Gets or sets the validity period for a certificate in months. |
| | | 199 | | /// </summary> |
| | 166 | 200 | | public int? ValidityInMonths { get; set; } |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Gets or sets a value indicating whether the certificate is currently enabled. If null, the server default wi |
| | | 204 | | /// </summary> |
| | 174 | 205 | | public bool? Enabled { get; set; } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate was updated. |
| | | 209 | | /// </summary> |
| | 80 | 210 | | public DateTimeOffset? UpdatedOn { get; internal set; } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate was created. |
| | | 214 | | /// </summary> |
| | 76 | 215 | | public DateTimeOffset? CreatedOn { get; internal set; } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Gets the allowed usages for the key of the certificate. |
| | | 219 | | /// </summary> |
| | 1070 | 220 | | public IList<CertificateKeyUsage> KeyUsage { get; } = new List<CertificateKeyUsage>(); |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Gets the allowed enhanced key usages (EKUs) of the certificate. |
| | | 224 | | /// </summary> |
| | 418 | 225 | | public IList<string> EnhancedKeyUsage { get; } = new List<string>(); |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Gets the actions to be executed at specified times in the certificates lifetime. |
| | | 229 | | /// </summary> |
| | 374 | 230 | | public IList<LifetimeAction> LifetimeActions { get; } = new List<LifetimeAction>(); |
| | | 231 | | |
| | | 232 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | | 233 | | { |
| | 1120 | 234 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 235 | | { |
| | 490 | 236 | | switch (prop.Name) |
| | | 237 | | { |
| | | 238 | | case KeyPropsPropertyName: |
| | 70 | 239 | | ReadKeyProperties(prop.Value); |
| | 70 | 240 | | break; |
| | | 241 | | |
| | | 242 | | case SecretPropsPropertyName: |
| | 70 | 243 | | ReadSecretProperties(prop.Value); |
| | 70 | 244 | | break; |
| | | 245 | | |
| | | 246 | | case X509PropsPropertyName: |
| | 70 | 247 | | ReadX509CertificateProperties(prop.Value); |
| | 70 | 248 | | break; |
| | | 249 | | |
| | | 250 | | case IssuerPropertyName: |
| | 70 | 251 | | _issuer.ReadProperties(prop.Value); |
| | 70 | 252 | | break; |
| | | 253 | | |
| | | 254 | | case AttributesPropertyName: |
| | 70 | 255 | | ReadAttributesProperties(prop.Value); |
| | 70 | 256 | | break; |
| | | 257 | | |
| | | 258 | | case LifetimeActionsPropertyName: |
| | 280 | 259 | | foreach (JsonElement actionElem in prop.Value.EnumerateArray()) |
| | | 260 | | { |
| | 70 | 261 | | LifetimeActions.Add(LifetimeAction.FromJsonObject(actionElem)); |
| | | 262 | | } |
| | | 263 | | break; |
| | | 264 | | |
| | | 265 | | } |
| | | 266 | | } |
| | 70 | 267 | | } |
| | | 268 | | |
| | | 269 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) |
| | | 270 | | { |
| | | 271 | | // Key Props |
| | 88 | 272 | | if (KeyType.HasValue || KeyCurveName.HasValue || KeySize.HasValue || ReuseKey.HasValue || Exportable.HasValu |
| | | 273 | | { |
| | 68 | 274 | | json.WriteStartObject(s_keyPropsPropertyNameBytes); |
| | | 275 | | |
| | 68 | 276 | | WriteKeyProperties(json); |
| | | 277 | | |
| | 68 | 278 | | json.WriteEndObject(); |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | // Secret Props |
| | 88 | 282 | | if (ContentType.HasValue) |
| | | 283 | | { |
| | 70 | 284 | | json.WriteStartObject(s_secretPropsPropertyNameBytes); |
| | | 285 | | |
| | 70 | 286 | | WriteSecretProperties(json); |
| | | 287 | | |
| | 70 | 288 | | json.WriteEndObject(); |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | // X509 Props |
| | 88 | 292 | | if (Subject != null || (SubjectAlternativeNames != null && !SubjectAlternativeNames.IsEmpty) || !KeyUsage.Is |
| | | 293 | | { |
| | 74 | 294 | | json.WriteStartObject(s_x509PropsPropertyNameBytes); |
| | | 295 | | |
| | 74 | 296 | | WriteX509CertificateProperties(json); |
| | | 297 | | |
| | 74 | 298 | | json.WriteEndObject(); |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | // Issuer Props |
| | 88 | 302 | | if (IssuerName != null || CertificateType != null || CertificateTransparency.HasValue) |
| | | 303 | | { |
| | 74 | 304 | | json.WriteStartObject(s_issuerPropertyNameBytes); |
| | | 305 | | |
| | 74 | 306 | | _issuer.WriteProperties(json); |
| | | 307 | | |
| | 74 | 308 | | json.WriteEndObject(); |
| | | 309 | | } |
| | | 310 | | |
| | 88 | 311 | | if (Enabled.HasValue) |
| | | 312 | | { |
| | 4 | 313 | | json.WriteStartObject(s_attributesPropertyNameBytes); |
| | | 314 | | |
| | 4 | 315 | | WriteAttributesProperties(json); |
| | | 316 | | |
| | 4 | 317 | | json.WriteEndObject(); |
| | | 318 | | } |
| | | 319 | | |
| | 88 | 320 | | if (!LifetimeActions.IsNullOrEmpty()) |
| | | 321 | | { |
| | 2 | 322 | | json.WriteStartArray(s_lifetimeActionsPropertyNameBytes); |
| | | 323 | | |
| | 8 | 324 | | foreach (LifetimeAction action in LifetimeActions) |
| | | 325 | | { |
| | 2 | 326 | | if (action != null) |
| | | 327 | | { |
| | 2 | 328 | | json.WriteStartObject(); |
| | | 329 | | |
| | 2 | 330 | | ((IJsonSerializable)action).WriteProperties(json); |
| | | 331 | | |
| | 2 | 332 | | json.WriteEndObject(); |
| | | 333 | | } |
| | | 334 | | } |
| | | 335 | | |
| | 2 | 336 | | json.WriteEndArray(); |
| | | 337 | | } |
| | 88 | 338 | | } |
| | | 339 | | |
| | | 340 | | private void ReadKeyProperties(JsonElement json) |
| | | 341 | | { |
| | 700 | 342 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 343 | | { |
| | 280 | 344 | | switch (prop.Name) |
| | | 345 | | { |
| | | 346 | | case KeyTypePropertyName: |
| | 70 | 347 | | KeyType = prop.Value.GetString(); |
| | 70 | 348 | | break; |
| | | 349 | | |
| | | 350 | | case ReuseKeyPropertyName: |
| | 70 | 351 | | ReuseKey = prop.Value.GetBoolean(); |
| | 70 | 352 | | break; |
| | | 353 | | |
| | | 354 | | case ExportablePropertyName: |
| | 70 | 355 | | Exportable = prop.Value.GetBoolean(); |
| | 70 | 356 | | break; |
| | | 357 | | |
| | | 358 | | case CurveNamePropertyName: |
| | 0 | 359 | | KeyCurveName = prop.Value.GetString(); |
| | 0 | 360 | | break; |
| | | 361 | | |
| | | 362 | | case KeySizePropertyName: |
| | 70 | 363 | | KeySize = prop.Value.GetInt32(); |
| | | 364 | | break; |
| | | 365 | | } |
| | | 366 | | } |
| | 70 | 367 | | } |
| | | 368 | | |
| | | 369 | | private void WriteKeyProperties(Utf8JsonWriter json) |
| | | 370 | | { |
| | 68 | 371 | | if (KeyType.HasValue) |
| | | 372 | | { |
| | 56 | 373 | | json.WriteString(s_keyTypePropertyNameBytes, KeyType.ToString()); |
| | | 374 | | } |
| | | 375 | | |
| | 68 | 376 | | if (ReuseKey.HasValue) |
| | | 377 | | { |
| | 60 | 378 | | json.WriteBoolean(s_reuseKeyPropertyNameBytes, ReuseKey.Value); |
| | | 379 | | } |
| | | 380 | | |
| | 68 | 381 | | if (Exportable.HasValue) |
| | | 382 | | { |
| | 60 | 383 | | json.WriteBoolean(s_exportablePropertyNameBytes, Exportable.Value); |
| | | 384 | | } |
| | | 385 | | |
| | 68 | 386 | | if (KeyCurveName.HasValue) |
| | | 387 | | { |
| | 2 | 388 | | json.WriteString(s_curveNamePropertyNameBytes, KeyCurveName.ToString()); |
| | | 389 | | } |
| | | 390 | | |
| | 68 | 391 | | if (KeySize.HasValue) |
| | | 392 | | { |
| | 8 | 393 | | json.WriteNumber(s_keySizePropertyNameBytes, KeySize.Value); |
| | | 394 | | } |
| | 68 | 395 | | } |
| | | 396 | | |
| | | 397 | | private void ReadSecretProperties(JsonElement json) |
| | | 398 | | { |
| | 70 | 399 | | if (json.TryGetProperty(ContentTypePropertyName, out JsonElement contentTypeProp)) |
| | | 400 | | { |
| | 70 | 401 | | ContentType = contentTypeProp.GetString(); |
| | | 402 | | } |
| | 70 | 403 | | } |
| | | 404 | | |
| | | 405 | | private void WriteSecretProperties(Utf8JsonWriter json) |
| | | 406 | | { |
| | 70 | 407 | | if (ContentType.HasValue) |
| | | 408 | | { |
| | 70 | 409 | | json.WriteString(s_contentTypePropertyNameBytes, ContentType.ToString()); |
| | | 410 | | } |
| | 70 | 411 | | } |
| | | 412 | | |
| | | 413 | | private void ReadX509CertificateProperties(JsonElement json) |
| | | 414 | | { |
| | 840 | 415 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 416 | | { |
| | 350 | 417 | | switch (prop.Name) |
| | | 418 | | { |
| | | 419 | | case SubjectPropertyName: |
| | 70 | 420 | | Subject = prop.Value.GetString(); |
| | 70 | 421 | | break; |
| | | 422 | | |
| | | 423 | | case SansPropertyName: |
| | 0 | 424 | | SubjectAlternativeNames = new SubjectAlternativeNames(); |
| | 0 | 425 | | ((IJsonDeserializable)SubjectAlternativeNames).ReadProperties(prop.Value); |
| | 0 | 426 | | break; |
| | | 427 | | |
| | | 428 | | case KeyUsagePropertyName: |
| | 796 | 429 | | foreach (JsonElement usageElem in prop.Value.EnumerateArray()) |
| | | 430 | | { |
| | 328 | 431 | | KeyUsage.Add(usageElem.GetString()); |
| | | 432 | | } |
| | | 433 | | break; |
| | | 434 | | |
| | | 435 | | case EkusPropertyName: |
| | 380 | 436 | | foreach (JsonElement usageElem in prop.Value.EnumerateArray()) |
| | | 437 | | { |
| | 120 | 438 | | EnhancedKeyUsage.Add(usageElem.GetString()); |
| | | 439 | | } |
| | | 440 | | break; |
| | | 441 | | |
| | | 442 | | case ValidityMonthsPropertyName: |
| | 70 | 443 | | ValidityInMonths = prop.Value.GetInt32(); |
| | | 444 | | break; |
| | | 445 | | } |
| | | 446 | | } |
| | 70 | 447 | | } |
| | | 448 | | |
| | | 449 | | private void WriteX509CertificateProperties(Utf8JsonWriter json) |
| | | 450 | | { |
| | 74 | 451 | | if (Subject != null) |
| | | 452 | | { |
| | 74 | 453 | | json.WriteString(s_subjectPropertyNameBytes, Subject); |
| | | 454 | | } |
| | | 455 | | |
| | 74 | 456 | | if (SubjectAlternativeNames != null && !SubjectAlternativeNames.IsEmpty) |
| | | 457 | | { |
| | 0 | 458 | | json.WriteStartObject(s_sansPropertyNameBytes); |
| | | 459 | | |
| | 0 | 460 | | ((IJsonSerializable)SubjectAlternativeNames).WriteProperties(json); |
| | | 461 | | |
| | 0 | 462 | | json.WriteEndObject(); |
| | | 463 | | } |
| | | 464 | | |
| | 74 | 465 | | if (!KeyUsage.IsNullOrEmpty()) |
| | | 466 | | { |
| | 56 | 467 | | json.WriteStartArray(s_keyUsagePropertyNameBytes); |
| | 744 | 468 | | foreach (CertificateKeyUsage usage in KeyUsage) |
| | | 469 | | { |
| | 316 | 470 | | json.WriteStringValue(usage.ToString()); |
| | | 471 | | } |
| | 56 | 472 | | json.WriteEndArray(); |
| | | 473 | | } |
| | | 474 | | |
| | 74 | 475 | | if (!EnhancedKeyUsage.IsNullOrEmpty()) |
| | | 476 | | { |
| | 0 | 477 | | json.WriteStartArray(s_ekusPropertyNameBytes); |
| | 0 | 478 | | foreach (var usage in EnhancedKeyUsage) |
| | | 479 | | { |
| | 0 | 480 | | json.WriteStringValue(usage); |
| | | 481 | | } |
| | 0 | 482 | | json.WriteEndArray(); |
| | | 483 | | } |
| | | 484 | | |
| | 74 | 485 | | if (ValidityInMonths.HasValue) |
| | | 486 | | { |
| | 2 | 487 | | json.WriteNumber(s_validityMonthsPropertyNameBytes, ValidityInMonths.Value); |
| | | 488 | | } |
| | 74 | 489 | | } |
| | | 490 | | |
| | | 491 | | private void ReadAttributesProperties(JsonElement json) |
| | | 492 | | { |
| | 560 | 493 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 494 | | { |
| | 210 | 495 | | switch (prop.Name) |
| | | 496 | | { |
| | | 497 | | case EnabledPropertyName: |
| | 70 | 498 | | Enabled = prop.Value.GetBoolean(); |
| | 70 | 499 | | break; |
| | | 500 | | |
| | | 501 | | case CreatedPropertyName: |
| | 70 | 502 | | CreatedOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| | 70 | 503 | | break; |
| | | 504 | | |
| | | 505 | | case UpdatedPropertyName: |
| | 70 | 506 | | UpdatedOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| | | 507 | | break; |
| | | 508 | | } |
| | | 509 | | } |
| | 70 | 510 | | } |
| | | 511 | | |
| | | 512 | | private void WriteAttributesProperties(Utf8JsonWriter json) |
| | | 513 | | { |
| | 4 | 514 | | if (Enabled.HasValue) |
| | | 515 | | { |
| | 4 | 516 | | json.WriteBoolean(s_enabledPropertyNameBytes, Enabled.Value); |
| | | 517 | | } |
| | 4 | 518 | | } |
| | | 519 | | } |
| | | 520 | | } |