Files
duplicati/Duplicati/Library/Utility/Options/SslOptionsHelper.cs
T
Kenneth Skovhede 92d6d666ee Add OCSP failure handling option
This updates the SSL/TLS validation to optionally ignore failures in OCSP/CRL list. If the revocation servers are offline or unavailable, the SSL requests will fail.

With the new option, `--ignore-revocation-failure`, it is now possible to ignore this class of errors and let the connections work anyway.

By default, this is off so we have the strictest possible security.

As part of this PR, there were a few places that did validation/override of certificates. With this change all calls route into the single `SslCertificateValidator` class so all certificate validations are done the same from ServerUtil, reporting modules and backends.
2026-06-27 11:43:28 +02:00

135 lines
6.6 KiB
C#

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