|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Security.Cryptography; |
| 8 | +using System.Security.Cryptography.X509Certificates; |
| 9 | +using Microsoft.AspNetCore.Server.Kestrel.Https; |
| 10 | +using Microsoft.Extensions.Hosting; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | + |
| 13 | +namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Certificates |
| 14 | +{ |
| 15 | + internal class CertificateConfigLoader : ICertificateConfigLoader |
| 16 | + { |
| 17 | + public CertificateConfigLoader(IHostEnvironment hostEnvironment, ILogger<KestrelServer> logger) |
| 18 | + { |
| 19 | + HostEnvironment = hostEnvironment; |
| 20 | + Logger = logger; |
| 21 | + } |
| 22 | + |
| 23 | + public IHostEnvironment HostEnvironment { get; } |
| 24 | + public ILogger<KestrelServer> Logger { get; } |
| 25 | + |
| 26 | + public bool IsTestMock => false; |
| 27 | + |
| 28 | + public X509Certificate2 LoadCertificate(CertificateConfig certInfo, string endpointName) |
| 29 | + { |
| 30 | + if (certInfo is null) |
| 31 | + { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + if (certInfo.IsFileCert && certInfo.IsStoreCert) |
| 36 | + { |
| 37 | + throw new InvalidOperationException(CoreStrings.FormatMultipleCertificateSources(endpointName)); |
| 38 | + } |
| 39 | + else if (certInfo.IsFileCert) |
| 40 | + { |
| 41 | + var certificatePath = Path.Combine(HostEnvironment.ContentRootPath, certInfo.Path); |
| 42 | + if (certInfo.KeyPath != null) |
| 43 | + { |
| 44 | + var certificateKeyPath = Path.Combine(HostEnvironment.ContentRootPath, certInfo.KeyPath); |
| 45 | + var certificate = GetCertificate(certificatePath); |
| 46 | + |
| 47 | + if (certificate != null) |
| 48 | + { |
| 49 | + certificate = LoadCertificateKey(certificate, certificateKeyPath, certInfo.Password); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + Logger.FailedToLoadCertificate(certificateKeyPath); |
| 54 | + } |
| 55 | + |
| 56 | + if (certificate != null) |
| 57 | + { |
| 58 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 59 | + { |
| 60 | + return PersistKey(certificate); |
| 61 | + } |
| 62 | + |
| 63 | + return certificate; |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + Logger.FailedToLoadCertificateKey(certificateKeyPath); |
| 68 | + } |
| 69 | + |
| 70 | + throw new InvalidOperationException(CoreStrings.InvalidPemKey); |
| 71 | + } |
| 72 | + |
| 73 | + return new X509Certificate2(Path.Combine(HostEnvironment.ContentRootPath, certInfo.Path), certInfo.Password); |
| 74 | + } |
| 75 | + else if (certInfo.IsStoreCert) |
| 76 | + { |
| 77 | + return LoadFromStoreCert(certInfo); |
| 78 | + } |
| 79 | + |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + private static X509Certificate2 PersistKey(X509Certificate2 fullCertificate) |
| 84 | + { |
| 85 | + // We need to force the key to be persisted. |
| 86 | + // See https://github.com/dotnet/runtime/issues/23749 |
| 87 | + var certificateBytes = fullCertificate.Export(X509ContentType.Pkcs12, ""); |
| 88 | + return new X509Certificate2(certificateBytes, "", X509KeyStorageFlags.DefaultKeySet); |
| 89 | + } |
| 90 | + |
| 91 | + private static X509Certificate2 LoadCertificateKey(X509Certificate2 certificate, string keyPath, string password) |
| 92 | + { |
| 93 | + // OIDs for the certificate key types. |
| 94 | + const string RSAOid = "1.2.840.113549.1.1.1"; |
| 95 | + const string DSAOid = "1.2.840.10040.4.1"; |
| 96 | + const string ECDsaOid = "1.2.840.10045.2.1"; |
| 97 | + |
| 98 | + var keyText = File.ReadAllText(keyPath); |
| 99 | + return certificate.PublicKey.Oid.Value switch |
| 100 | + { |
| 101 | + RSAOid => AttachPemRSAKey(certificate, keyText, password), |
| 102 | + ECDsaOid => AttachPemECDSAKey(certificate, keyText, password), |
| 103 | + DSAOid => AttachPemDSAKey(certificate, keyText, password), |
| 104 | + _ => throw new InvalidOperationException(string.Format(CoreStrings.UnrecognizedCertificateKeyOid, certificate.PublicKey.Oid.Value)) |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + private static X509Certificate2 GetCertificate(string certificatePath) |
| 109 | + { |
| 110 | + if (X509Certificate2.GetCertContentType(certificatePath) == X509ContentType.Cert) |
| 111 | + { |
| 112 | + return new X509Certificate2(certificatePath); |
| 113 | + } |
| 114 | + |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + private static X509Certificate2 AttachPemRSAKey(X509Certificate2 certificate, string keyText, string password) |
| 119 | + { |
| 120 | + using var rsa = RSA.Create(); |
| 121 | + if (password == null) |
| 122 | + { |
| 123 | + rsa.ImportFromPem(keyText); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + rsa.ImportFromEncryptedPem(keyText, password); |
| 128 | + } |
| 129 | + |
| 130 | + return certificate.CopyWithPrivateKey(rsa); |
| 131 | + } |
| 132 | + |
| 133 | + private static X509Certificate2 AttachPemDSAKey(X509Certificate2 certificate, string keyText, string password) |
| 134 | + { |
| 135 | + using var dsa = DSA.Create(); |
| 136 | + if (password == null) |
| 137 | + { |
| 138 | + dsa.ImportFromPem(keyText); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + dsa.ImportFromEncryptedPem(keyText, password); |
| 143 | + } |
| 144 | + |
| 145 | + return certificate.CopyWithPrivateKey(dsa); |
| 146 | + } |
| 147 | + |
| 148 | + private static X509Certificate2 AttachPemECDSAKey(X509Certificate2 certificate, string keyText, string password) |
| 149 | + { |
| 150 | + using var ecdsa = ECDsa.Create(); |
| 151 | + if (password == null) |
| 152 | + { |
| 153 | + ecdsa.ImportFromPem(keyText); |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + ecdsa.ImportFromEncryptedPem(keyText, password); |
| 158 | + } |
| 159 | + |
| 160 | + return certificate.CopyWithPrivateKey(ecdsa); |
| 161 | + } |
| 162 | + |
| 163 | + private static X509Certificate2 LoadFromStoreCert(CertificateConfig certInfo) |
| 164 | + { |
| 165 | + var subject = certInfo.Subject; |
| 166 | + var storeName = string.IsNullOrEmpty(certInfo.Store) ? StoreName.My.ToString() : certInfo.Store; |
| 167 | + var location = certInfo.Location; |
| 168 | + var storeLocation = StoreLocation.CurrentUser; |
| 169 | + if (!string.IsNullOrEmpty(location)) |
| 170 | + { |
| 171 | + storeLocation = (StoreLocation)Enum.Parse(typeof(StoreLocation), location, ignoreCase: true); |
| 172 | + } |
| 173 | + var allowInvalid = certInfo.AllowInvalid ?? false; |
| 174 | + |
| 175 | + return CertificateLoader.LoadFromStoreCert(subject, storeName, storeLocation, allowInvalid); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments