From dbfb1fb3705f7d1f562f28ed0126933dd423f814 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 30 Apr 2017 22:22:13 -0400 Subject: add support for cert with password --- Emby.Server.Core/ApplicationHost.cs | 45 ++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'Emby.Server.Core/ApplicationHost.cs') diff --git a/Emby.Server.Core/ApplicationHost.cs b/Emby.Server.Core/ApplicationHost.cs index b7309de660..05f6c4309f 100644 --- a/Emby.Server.Core/ApplicationHost.cs +++ b/Emby.Server.Core/ApplicationHost.cs @@ -257,7 +257,7 @@ namespace Emby.Server.Core internal IPowerManagement PowerManagement { get; private set; } internal IImageEncoder ImageEncoder { get; private set; } - private readonly Action _certificateGenerator; + private readonly Action _certificateGenerator; private readonly Func _defaultUserNameFactory; /// @@ -274,7 +274,7 @@ namespace Emby.Server.Core ISystemEvents systemEvents, IMemoryStreamFactory memoryStreamFactory, INetworkManager networkManager, - Action certificateGenerator, + Action certificateGenerator, Func defaultUsernameFactory) : base(applicationPaths, logManager, @@ -609,8 +609,8 @@ namespace Emby.Server.Core RegisterSingleInstance(() => new SearchEngine(LogManager, LibraryManager, UserManager)); - CertificatePath = GetCertificatePath(true); - Certificate = GetCertificate(CertificatePath); + CertificateInfo = GetCertificateInfo(true); + Certificate = GetCertificate(CertificateInfo); HttpServer = HttpServerFactory.CreateServer(this, LogManager, ServerConfigurationManager, NetworkManager, MemoryStreamFactory, "Emby", "web/index.html", textEncoding, SocketFactory, CryptographyProvider, JsonSerializer, XmlSerializer, EnvironmentInfo, Certificate, FileSystemManager, SupportsDualModeSockets); HttpServer.GlobalResponse = LocalizationManager.GetLocalizedString("StartupEmbyServerIsLoading"); @@ -745,8 +745,10 @@ namespace Emby.Server.Core } } - private ICertificate GetCertificate(string certificateLocation) + private ICertificate GetCertificate(CertificateInfo info) { + var certificateLocation = info == null ? null : info.Path; + if (string.IsNullOrWhiteSpace(certificateLocation)) { return null; @@ -759,7 +761,7 @@ namespace Emby.Server.Core return null; } - X509Certificate2 localCert = new X509Certificate2(certificateLocation); + X509Certificate2 localCert = new X509Certificate2(certificateLocation, info.Password); //localCert.PrivateKey = PrivateKey.CreateFromFile(pvk_file).RSA; if (!localCert.HasPrivateKey) { @@ -1064,7 +1066,7 @@ namespace Emby.Server.Core SyncManager.AddParts(GetExports()); } - private string CertificatePath { get; set; } + private CertificateInfo CertificateInfo { get; set; } private ICertificate Certificate { get; set; } private IEnumerable GetUrlPrefixes() @@ -1080,7 +1082,7 @@ namespace Emby.Server.Core "http://"+i+":" + HttpPort + "/" }; - if (!string.IsNullOrWhiteSpace(CertificatePath)) + if (CertificateInfo != null) { prefixes.Add("https://" + i + ":" + HttpsPort + "/"); } @@ -1123,17 +1125,21 @@ namespace Emby.Server.Core } } - private string GetCertificatePath(bool generateCertificate) + private CertificateInfo GetCertificateInfo(bool generateCertificate) { if (!string.IsNullOrWhiteSpace(ServerConfigurationManager.Configuration.CertificatePath)) { // Custom cert - return ServerConfigurationManager.Configuration.CertificatePath; + return new CertificateInfo + { + Path = ServerConfigurationManager.Configuration.CertificatePath + }; } // Generate self-signed cert var certHost = GetHostnameFromExternalDns(ServerConfigurationManager.Configuration.WanDdns); var certPath = Path.Combine(ServerConfigurationManager.ApplicationPaths.ProgramDataPath, "ssl", "cert_" + (certHost + "1").GetMD5().ToString("N") + ".pfx"); + var password = "embycert"; if (generateCertificate) { @@ -1143,7 +1149,7 @@ namespace Emby.Server.Core try { - _certificateGenerator(certPath, certHost); + _certificateGenerator(certPath, certHost, password); } catch (Exception ex) { @@ -1153,7 +1159,11 @@ namespace Emby.Server.Core } } - return certPath; + return new CertificateInfo + { + Path = certPath, + Password = password + }; } /// @@ -1189,7 +1199,11 @@ namespace Emby.Server.Core requiresRestart = true; } - if (!string.Equals(CertificatePath, GetCertificatePath(false), StringComparison.OrdinalIgnoreCase)) + var currentCertPath = CertificateInfo == null ? null : CertificateInfo.Path; + var newCertInfo = GetCertificateInfo(false); + var newCertPath = newCertInfo == null ? null : newCertInfo.Path; + + if (!string.Equals(currentCertPath, newCertPath, StringComparison.OrdinalIgnoreCase)) { requiresRestart = true; } @@ -1779,6 +1793,11 @@ namespace Emby.Server.Core { Container.Register(typeInterface, typeImplementation); } + } + internal class CertificateInfo + { + public string Path { get; set; } + public string Password { get; set; } } } -- cgit v1.2.3 From fc788efa49e129eadbcc64a1b4fd7797e020d854 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 1 May 2017 16:03:27 -0400 Subject: add password to self signed cert --- Emby.Server.Core/ApplicationHost.cs | 2 +- Emby.Server.Implementations/Library/SearchEngine.cs | 5 ++++- MediaBrowser.Api/SearchService.cs | 5 ++++- MediaBrowser.Model/Search/SearchQuery.cs | 1 + .../Cryptography/CertificateGenerator.cs | 3 ++- 5 files changed, 12 insertions(+), 4 deletions(-) (limited to 'Emby.Server.Core/ApplicationHost.cs') diff --git a/Emby.Server.Core/ApplicationHost.cs b/Emby.Server.Core/ApplicationHost.cs index 05f6c4309f..994b942e92 100644 --- a/Emby.Server.Core/ApplicationHost.cs +++ b/Emby.Server.Core/ApplicationHost.cs @@ -1138,7 +1138,7 @@ namespace Emby.Server.Core // Generate self-signed cert var certHost = GetHostnameFromExternalDns(ServerConfigurationManager.Configuration.WanDdns); - var certPath = Path.Combine(ServerConfigurationManager.ApplicationPaths.ProgramDataPath, "ssl", "cert_" + (certHost + "1").GetMD5().ToString("N") + ".pfx"); + var certPath = Path.Combine(ServerConfigurationManager.ApplicationPaths.ProgramDataPath, "ssl", "cert_" + (certHost + "2").GetMD5().ToString("N") + ".pfx"); var password = "embycert"; if (generateCertificate) diff --git a/Emby.Server.Implementations/Library/SearchEngine.cs b/Emby.Server.Implementations/Library/SearchEngine.cs index a47a3322e6..51c9504c20 100644 --- a/Emby.Server.Implementations/Library/SearchEngine.cs +++ b/Emby.Server.Implementations/Library/SearchEngine.cs @@ -165,7 +165,10 @@ namespace Emby.Server.Implementations.Library ExcludeItemTypes = excludeItemTypes.ToArray(), IncludeItemTypes = includeItemTypes.ToArray(), Limit = query.Limit, - IncludeItemsByName = true + IncludeItemsByName = string.IsNullOrWhiteSpace(query.ParentId), + ParentId = string.IsNullOrWhiteSpace(query.ParentId) ? (Guid?)null : new Guid(query.ParentId), + SortBy = new[] { ItemSortBy.SortName }, + Recursive = true }); // Add search hints based on item name diff --git a/MediaBrowser.Api/SearchService.cs b/MediaBrowser.Api/SearchService.cs index d6fa4030de..b4ee923650 100644 --- a/MediaBrowser.Api/SearchService.cs +++ b/MediaBrowser.Api/SearchService.cs @@ -66,6 +66,8 @@ namespace MediaBrowser.Api [ApiMember(Name = "IncludeItemTypes", Description = "Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimeted.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)] public string IncludeItemTypes { get; set; } + public string ParentId { get; set; } + public GetSearchHints() { IncludeArtists = true; @@ -135,7 +137,8 @@ namespace MediaBrowser.Api IncludeStudios = request.IncludeStudios, StartIndex = request.StartIndex, UserId = request.UserId, - IncludeItemTypes = (request.IncludeItemTypes ?? string.Empty).Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray() + IncludeItemTypes = (request.IncludeItemTypes ?? string.Empty).Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray(), + ParentId = request.ParentId }).ConfigureAwait(false); diff --git a/MediaBrowser.Model/Search/SearchQuery.cs b/MediaBrowser.Model/Search/SearchQuery.cs index 678dfd39d7..11d2d97f7a 100644 --- a/MediaBrowser.Model/Search/SearchQuery.cs +++ b/MediaBrowser.Model/Search/SearchQuery.cs @@ -34,6 +34,7 @@ namespace MediaBrowser.Model.Search public bool IncludeArtists { get; set; } public string[] IncludeItemTypes { get; set; } + public string ParentId { get; set; } public SearchQuery() { diff --git a/MediaBrowser.Server.Startup.Common/Cryptography/CertificateGenerator.cs b/MediaBrowser.Server.Startup.Common/Cryptography/CertificateGenerator.cs index a362045d02..4f5b3d0042 100644 --- a/MediaBrowser.Server.Startup.Common/Cryptography/CertificateGenerator.cs +++ b/MediaBrowser.Server.Startup.Common/Cryptography/CertificateGenerator.cs @@ -44,7 +44,7 @@ namespace Emby.Common.Implementations.Security cb.NotAfter = notAfter; cb.SubjectName = subject; cb.SubjectPublicKey = subjectKey; - + // signature cb.Hash = "SHA256"; byte[] rawcert = cb.Sign(issuerKey); @@ -60,6 +60,7 @@ namespace Emby.Common.Implementations.Security attributes.Add(PKCS9.localKeyId, list); p12.AddCertificate(new X509Certificate(rawcert), attributes); + p12.Password = password; p12.AddPkcs8ShroudedKeyBag(subjectKey, attributes); p12.SaveToFile(fileName); -- cgit v1.2.3