// Copyright (C) 2026, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography.X509Certificates;
using Duplicati.Library.Certificates.Platform;
namespace Duplicati.Library.Certificates;
///
/// Factory for creating the appropriate CA trust installer for the current platform.
///
public static class CATrustInstallerFactory
{
///
/// Creates a CA trust installer appropriate for the current platform.
///
/// The store location to use (Windows only). If null, uses LocalMachine if admin, otherwise CurrentUser.
/// The certificate directory to use (Linux only). If null, uses the default system location.
/// The keychain path to use (macOS only). If null, uses the default login keychain.
/// An ICATrustInstaller instance for the current platform, or null if not supported.
public static ICATrustInstaller? CreateInstaller(StoreLocation? storeLocation, string? linuxCertDirectory, string? macOSKeychainPath)
{
if (OperatingSystem.IsWindows())
{
// If no store location specified, auto-detect based on privileges
var location = storeLocation ?? GetDefaultWindowsStoreLocation();
return new WindowsCATrustInstaller(location);
}
if (OperatingSystem.IsMacOS())
{
var installer = new MacOSCATrustInstaller();
if (!string.IsNullOrWhiteSpace(macOSKeychainPath))
installer.KeychainPath = macOSKeychainPath;
return installer;
}
if (OperatingSystem.IsLinux())
{
var installer = new LinuxCATrustInstaller();
if (!string.IsNullOrWhiteSpace(linuxCertDirectory))
installer.CertDirectory = linuxCertDirectory;
return installer;
}
return null;
}
///
/// Gets the default Windows store location based on current privileges.
/// Uses LocalMachine if running as admin/LocalSystem, otherwise CurrentUser.
///
/// The default store location for Windows.
[SupportedOSPlatform("windows")]
public static StoreLocation GetDefaultWindowsStoreLocation()
{
if (Utility.PermissionHelper.IsRunningAsAdministratorOrLocalSystem())
return StoreLocation.LocalMachine;
return StoreLocation.CurrentUser;
}
///
/// Determines whether the current platform is supported.
///
/// True if the platform is supported; otherwise, false.
public static bool IsPlatformSupported()
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
}