// Copyright (C) 2025, 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. #nullable enable using System; using System.Collections.Generic; using System.Net.Http; using Duplicati.Library.Interface; namespace Duplicati.Library.Utility.Options; /// /// Helper class for unified use of SSL certificate options /// public static class SslOptionsHelper { /// /// The name of the ssl option without a prefix /// public const string UseSSLOption = "use-ssl"; /// /// The name of the accept any ssl certificate option without a prefix /// public const string AcceptAnySslCertificateOption = "accept-any-ssl-certificate"; /// /// The name of the accept specified ssl hash option without a prefix /// public const string AcceptSpecifiedSslHashOption = "accept-specified-ssl-hash"; /// /// Gets the SSL certificate options for certificate only /// /// An optional prefix for the options /// The SSL certificate options public static CommandLineArgument[] GetCertOnlyOptions(string? prefix = null) => [ new CommandLineArgument($"{prefix}{AcceptAnySslCertificateOption}", CommandLineArgument.ArgumentType.Boolean, Strings.SslOptionsHelper.DescriptionAcceptAnyCertificateShort, Strings.SslOptionsHelper.DescriptionAcceptAnyCertificateLong, "false"), new CommandLineArgument($"{prefix}{AcceptSpecifiedSslHashOption}", CommandLineArgument.ArgumentType.String, Strings.SslOptionsHelper.DescriptionAcceptHashShort, Strings.SslOptionsHelper.DescriptionAcceptHashLong) ]; /// /// Gets the SSL certificate options for SSL only /// /// An optional prefix for the options /// The SSL certificate options public static CommandLineArgument[] GetSslOnlyOption(string? prefix = null) => [ new CommandLineArgument($"{prefix}{UseSSLOption}", CommandLineArgument.ArgumentType.Boolean, Strings.SslOptionsHelper.DescriptionUseSSLShort, Strings.SslOptionsHelper.DescriptionUseSSLLong, "false") ]; /// /// Gets the SSL certificate options /// /// An optional prefix for the options /// The SSL certificate options public static CommandLineArgument[] GetOptions(string? prefix = null) => [ .. GetSslOnlyOption(prefix), .. GetCertOnlyOptions(prefix) ]; /// /// Parses the SSL certificate options from a dictionary /// /// The dictionary to parse /// An optional prefix for the options /// The parsed SSL certificate options public static SslCertificateOptions Parse(IReadOnlyDictionary options, string? prefix = null) { var acceptSpecificCertificatesString = options.TryGetValue($"{prefix}{AcceptSpecifiedSslHashOption}", out var value) ? value : null; return new SslCertificateOptions( Utility.ParseBoolOption(options, $"{prefix}{UseSSLOption}"), Utility.ParseBoolOption(options, $"{prefix}{AcceptAnySslCertificateOption}"), string.IsNullOrWhiteSpace(acceptSpecificCertificatesString) ? [] : acceptSpecificCertificatesString.Split([",", ";"], StringSplitOptions.RemoveEmptyEntries) ); } /// /// Structure to hold the SSL certificate options /// /// Flag to indicate if SSL is used /// Flag to accept all certificates /// Array of specific certificate hashes to accept public sealed record SslCertificateOptions(bool UseSSL, bool AcceptAllCertificates, string[] AcceptSpecificCertificateHashes) { /// /// Creates a handler with the SSL certificate options /// /// The created handler public HttpClientHandler CreateHandler() { var handler = new HttpClientHandler(); return ConfigureHandler(handler); } /// /// Configures an existing handler with the SSL certificate options /// /// The handler to configure /// The configured handler public HttpClientHandler ConfigureHandler(HttpClientHandler handler) { HttpClientHelper.ConfigureHandlerCertificateValidator(handler, AcceptAllCertificates, AcceptSpecificCertificateHashes); return handler; } } }