Skip to content

feat(build): add opt-in enable-lgpl21 (FFmpeg as LGPL-2.1+, Secure Transport TLS) - #88

Open
goozakdev wants to merge 3 commits into
mpvkit:mainfrom
goozakdev:lgpl21-securetransport
Open

goozakdev wants to merge 3 commits into
mpvkit:mainfrom
goozakdev:lgpl21-securetransport

Conversation

@goozakdev

Copy link
Copy Markdown

feat(build): add opt-in enable-lgpl21 (FFmpeg as LGPL-2.1+, Secure Transport TLS)

Closes #87

What this adds

A new opt-in build argument, enable-lgpl21, alongside enable-gpl:

make build enable-lgpl21

With it, FFmpeg is built as "LGPL version 2.1 or later":

  • gmp, nettle and gnutls are not built, and are not passed to FFmpeg or mpv.
  • --enable-version3 is removed (gmp is LGPLv3, which is what forced it).
  • --enable-securetransport is added, so TLS comes from Apple's Secure Transport.
  • It cannot be combined with enable-gpl (the option parser rejects the pair).
  • A small FFmpeg patch makes Secure Transport usable on iOS/tvOS (below).
  • The generated Package.swift is post-processed to match (below).

Nothing changes unless enable-lgpl21 is passed. With the option off, the generated
Package.swift is cmp-identical to the one produced by main before this PR.

Why

The default build links gmp and gnutls, so FFmpeg ends up LGPLv3 (--enable-version3). For an app
shipped through the App Store, LGPL-2.1 is a materially easier obligation than LGPLv3. This option
gives people who need that a supported path without forking the build scripts.

Why Secure Transport and not OpenSSL

  • --enable-openssl with OpenSSL 3.x does configure as LGPL 2.1 (configure only asks for
    --enable-version3 for OpenSSL >= 3 together with --enable-gpl).
  • But FFmpeg's LICENSE.md says Apache-2.0 libraries need --enable-version3 to combine with
    LGPL-2.1, and OpenSSL 3 is Apache-2.0. The configure check and the documentation disagree.
  • OpenSSL also rejected a trusted private-CA leaf certificate unless partial-chain verification was
    enabled; gnutls and Secure Transport accept it.

Secure Transport is part of the OS, adds no library to ship, and has no license question.

The FFmpeg patch

Sources/BuildScripts/patch/FFmpeg/0001-tls_securetransport-load-ca_file-without-SecItemImport.patch
(applies to FFmpeg n9.0.2, which this branch builds, and also applied to n8.1.2).

  1. SecItemImport does not exist on iOS/tvOS. configure sets HAVE_SECITEMIMPORT 0 there, so
    import_pem() returns AVERROR_PATCHWELCOME and any https open with a CA file (mpv's tls-ca-file)
    fails. When SecItemImport is unavailable the patch parses the PEM CERTIFICATE blocks itself:
    whitespace stripped, av_base64_decode, then SecCertificateCreateWithData. Other block types are
    skipped; a malformed block, or a file with no certificate, is AVERROR_INVALIDDATA. Behaviour with
    SecItemImport is unchanged.
  2. Whitelist gotcha, on every platform. The CA file was opened with the stream's protocol whitelist.
    FFmpeg's own https protocol sets a default_whitelist of http,https,tls,rtp,tcp,udp,crypto,httpproxy
    (libavformat/http.c), which has no file, so loading the CA file failed with
    Protocol 'file' not on whitelist and so did every https open with ca_file. A plain avio_open2 of an
    https URL with ca_file fails this way on pristine FFmpeg; mpv is not involved. It is not only an
    iOS/tvOS problem: on macOS (HAVE_SECITEMIMPORT=1) any https open with ca_file fails too, so the
    problem is live there, not latent. The patch opens the CA, certificate and key files with a whitelist of
    exactly file. This permits local-file reads of the configured CA/cert/key path even when the
    caller's whitelist excludes file; it still honours the caller's blacklist.
    Those paths are
    configuration, not stream data. The gnutls and OpenSSL backends read them straight from disk and never
    hit the whitelist.

Generated manifest

docs/Package.template.swift is post-processed when enable-lgpl21 is set
(applyLGPL21Manifest, plus early returns in generatePackageManagerFile):

  • gmp, nettle, hogweed, gnutls, Libssl and Libcrypto are removed from the _FFmpeg dependencies, and
    .linkedFramework("Security") is added.
  • The MPVKit-GPL product and the _MPVKit-GPL / _FFmpeg-GPL targets with their -GPL binary targets
    are removed (an LGPL-2.1 build cannot produce them).
  • The binaryTargets for OpenSSL (Libssl/Libcrypto) and the GPLv3 Libsmbclient are not emitted, since
    nothing links them in this mode.
  • If any text it expects is missing from the template, it throws with a message naming what is missing,
    rather than quietly emitting a manifest that still needs the removed libraries.

How it was verified

On 1.0.0 (FFmpeg n8.1.2, mpv v0.41.0), in a fork, with this feature fully built:

  • Built for ios, isimulator, tvos, tvsimulator and macos; every FFmpeg library reports
    LGPL version 2.1 or later.
  • Used in iOS and tvOS simulator app builds and in App Store archives of both. Symbols and strings in the
    archives show no gnutls, nettle, gmp or OpenSSL code, and Secure Transport plus the patched
    import_ca_pem present.
  • HTTPS playback was runtime-tested on the iOS simulator only, with mpv, tls-verify=yes and tls-ca-file
    against a local HTTPS server: a self-signed certificate added to the CA file plays; a private-CA leaf
    added to the CA file plays; a certificate not in the CA file is rejected (-9807,
    errSSLXCertChainInvalid).
  • The PEM parser was run separately under ASan/UBSan and leaks: a Mozilla bundle parses 119/119, CRLF
    input works, malformed input is rejected.

On this branch (FFmpeg n9.0.2) only; no full build and no runtime test of the network path:

  • git apply --check of the patch on pristine n9.0.2 passes (the build script applies it with git apply).
  • An enable-lgpl21 configure of n9.0.2 for iOS arm64 (using this branch's build script) reports
    CONFIG_SECURETRANSPORT 1, CONFIG_GNUTLS 0, CONFIG_VERSION3 0, CONFIG_GPL 0, and license
    "LGPL version 2.1 or later". libavformat/tls_securetransport.o compiles for arm64.
  • A macOS runtime test of the patch with HAVE_SECITEMIMPORT forced to 0, under ASan/UBSan, covering 15
    CA-file cases.
  • Generator output, run with dummy checksums: option off is cmp-identical to main's; option on parses
    with swift package dump-package, has the single product MPVKit, no GPL product or target, no gmp /
    nettle / hogweed / gnutls / Libssl / Libcrypto / Libsmbclient, links Security, and every target dependency
    resolves. Mutating the template (renamed dependency, product, target, marker) makes it fail loudly.
  • swiftc -typecheck of the build script sources passes at each of the three commits.

Known limits

  • Secure Transport is deprecated by Apple (the compile emits deprecation warnings). It still works on current
    OSes, but this option inherits that.
  • Without a tls-ca-file, Secure Transport verifies against the system roots (still verified, hostname
    checked). The gnutls build behaves differently: with no ca_file it falls back to
    gnutls_certificate_set_x509_system_trust, which is unimplemented on iOS/tvOS (it returned -1250 when
    measured), and a ca_file path that does not exist is only logged, not an error.
  • Mac Catalyst and visionOS slices are untested in this mode.
  • The runtime checks above were on 1.0.0 / n8.1.2, not n9.0.2.
  • The enable-lgpl21 build still downloads OpenSSL and Smbclient during the build, but they are not linked
    and not shipped in the generated manifest.
  • The Libbluray, Libuchardet and Libluajit binaryTargets are still emitted unchanged in this mode;
    they are not licence issues for this option.

On iOS and tvOS HAVE_SECITEMIMPORT is 0, so import_pem() returns
AVERROR_PATCHWELCOME and any tls_ca_file makes the TLS open fail,
although the later verification through SecTrustSetAnchorCertificates()
works on those platforms.

When SecItemImport() is unavailable, parse the PEM CERTIFICATE blocks
of the CA file directly: strip whitespace, decode them with
av_base64_decode() and build the anchor array with
SecCertificateCreateWithData(). Other PEM block types are skipped, a
malformed block is AVERROR_INVALIDDATA, and so is a file without any
certificate. Behaviour with SecItemImport() is unchanged.

Separately, open the CA, certificate and key files with a protocol
whitelist of exactly "file" instead of inheriting the stream's
whitelist. FFmpeg's https protocol sets a default_whitelist of
"http,https,tls,rtp,tcp,udp,crypto,httpproxy" (libavformat/http.c),
which has no "file", so the inherited whitelist made loading the CA
file, and so every https open with ca_file, fail with "Protocol 'file'
not on whitelist". This is not specific to iOS/tvOS: it also fails on
the macOS SecItemImport path, on pristine FFmpeg.

This permits local-file reads of the configured CA/cert/key path even
when the caller's whitelist excludes "file". It still honours the
caller's blacklist. The GnuTLS and OpenSSL backends read these paths
straight from disk and never hit the whitelist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option for an LGPL-2.1+ FFmpeg build: drop gmp/--enable-version3, allow securetransport for TLS

1 participant