loadProvider method

OpenSslProvider? loadProvider(
  1. String name, {
  2. bool retainFallbacks = true,
  3. bool required = true,
  4. List<String> searchPaths = const [],
  5. bool searchModule = true,
})

Loads a provider into the library's default context.

name is the provider name (legacy, default, fips, base).

retainFallbacks preserves the implicit activation of the default provider. Keep it true: OpenSSL disables the fallback providers as soon as any provider is loaded explicitly, so loading only legacy with false would take AES, SHA-2, RSA and everything else down with it.

Calling it twice with the same name does not reload: it returns the provider already loaded.

If the module is not found in the MODULESDIR compiled into libcrypto (common with the Windows installer, which points at a directory that does not exist), the search is retried in searchPaths and in the usual platform locations, unless searchModule is false.

Throws OpenSslException when the provider cannot be loaded, unless required is false (in which case it returns null).

Implementation

OpenSslProvider? loadProvider(
  String name, {
  bool retainFallbacks = true,
  bool required = true,
  List<String> searchPaths = const [],
  bool searchModule = true,
}) {
  final existing = _loaded[name];
  if (existing != null) return existing;

  var handle = _tryLoad(name, retainFallbacks);

  if (handle == nullptr && searchModule) {
    final previousPath = providerSearchPath;
    for (final dir in _moduleCandidates(name, searchPaths)) {
      setProviderSearchPath(dir);
      handle = _tryLoad(name, retainFallbacks);
      if (handle != nullptr) break;
    }
    if (handle == nullptr && previousPath != null) {
      setProviderSearchPath(previousPath);
    }
  }

  if (handle == nullptr) {
    final code = bindings.ERR_get_error();
    OpenSslException.clearError(bindings);
    if (!required) return null;
    throw OpenSslException(
      'Failed to load the "$name" provider. Check that the '
      '${_moduleFileName(name)} module exists in the OpenSSL modules '
      'directory (MODULESDIR) and that the loaded libcrypto is 3.x. The '
      'directory can be given to setProviderSearchPath() or through the '
      'OPENSSL_MODULES environment variable.',
      code,
      retainFallbacks ? 'OSSL_PROVIDER_try_load' : 'OSSL_PROVIDER_load',
    );
  }

  final provider = OpenSslProvider(name, handle);
  _loaded[name] = provider;
  return provider;
}