8000 signature: add OpenPGP signing mechanism based on Sequoia by ueno · Pull Request #2569 · containers/image · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

signature: add OpenPGP signing mechanism based on Sequoia #2569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ueno
Copy link
@ueno ueno commented Sep 17, 2024

This adds a new OpenPGP signing mechanism based Sequoia-PGP[1]. As
Sequoia-PGP is written in Rust and doesn't provide a stable C FFI,
this integration includes a minimal shared library as a "point
solution".

To build, first follow the instruction in signature/sequoia/README.md
and install libimage_sequoia.so* into the library path, and then
build with the following command from the top-level directory:

  $ make BUILDTAGS="btrfs_noversion containers_image_sequoia"

Note also that, for testing on Fedora, crypto-policies settings might
need to be modified to explictly allow SHA-1 and 1024 bit RSA, as the
testing keys in signature/fixtures are using those legacy algorithms.

  1. https://sequoia-pgp.org/

@ueno ueno force-pushed the wip/signature-sequoia branch 5 times, most recently from 0f4142d to 5003e75 Compare September 17, 2024 10:24
@cgwalters
Copy link
Contributor

(I'm not an "official" reviewer here, FWIW)

I skimmed your code and I have to say it looks generally great.

But very similarly to your PR in ostreedev/ostree#3278 it'd be quite nice I think to list out why we're making this change. Especially questions like: Do we still need the openpgp backend too going forward?

https://github.com/ueno/podman-sequoia

I do wonder whether it makes sense for such a thing to live separately from this repository. Would it have a stable API/ABI? Of course adding it to this repository suddenly grows the scope of things here from "Go" to "Go, Rust and C" which is not at all a small thing.

But, I personally also do like the idea of opening up to thinking about how we use Rust code in this stack too. (To be clear, I am not speaking in any way for the other people who have done 99% of the work in this repository that are not me)

@mtrmac
Copy link
Collaborator
mtrmac commented Sep 17, 2024

Hey @ueno , I’m very sorry I couldn’t get back to this discussion since … April?, due to a different RHEL priority (… possibly relevant here: sigstore signatures, using the Go standard-library crypto implementation).

The code in this PR is all good.


We do need to figure out packaging, and RH package ownership.

Do we still need the openpgp backend too going forward?

There’s a product discussion motivating the inclusion of this — and I assume product concerns would drive the continued existence of OpenPGP for a number of years. The default … should be kept up to date, but also needs to be somewhat practical to use.

Of course adding it to this repository suddenly grows the scope of things here from "Go" to "Go, Rust and C" which is not at all a small thing.

This repository is currently consumed by referencing it in go.mod in Podman & friends, and using go mod vendor; i.e. (apart from some configuration files), it mostly does not directly participate in builds.

*shrug* That means that adding an entirely new built artifact into this repo would probably not hurt any existing packaging.

But one other implication is that different versions of “Podman & friends” can be concurrently installed and include different versions of c/image, i.e. the C-API shared library (in a single system-wide location) will have to maintain a stable ABI to support all of those versions; directly including the Rust+C part in the c/image repo would not allow us to develop the server and client in immediate lockstep, so there’s no direct benefit of using one repo for all.

One thing I’m unsure about is assumptions of the Go tooling about repository contents — we have a go.mod at the top level of this Git repo, it’s conceivable that compilers/linters/… are going to assume that subdirectories primarily (exclusively?) contain Go code.


Podman etc. developers who don’t directly work on signing do need some reasonably convenient way to get an environment which doesn’t panic during process start. For GPGME, we tell them to install existing packages, on both Linux and macOS: https://github.com/containers/image/tree/main?tab=readme-ov-file#building . Something at about that level of hassle (or less) should exist for https://github.com/containers/image/tree/main?tab=readme-ov-file#building , or those developers are going to start sticking containers_image_openpgp in Makefiles.

@mtrmac mtrmac added the kind/feature A request for, or a PR adding, new functionality label Sep 17, 2024
@ueno
Copy link
Author
ueno commented Sep 18, 2024

[...] it'd be quite nice I think to list out why we're making this change. Especially questions like: Do we still need the openpgp backend too going forward?

In the discussion in April, a couple of motivations raised are:

As for the future of the openpgp backend, I agree with @mtrmac that it still has some value to maintain it as a legacy/standalone backend, even if sigstore signatures are preferred.

Podman etc. developers who don’t directly work on signing do need some reasonably convenient way to get an environment which doesn’t panic during process start. For GPGME, we tell them to install existing packages, on both Linux and macOS: https://github.com/containers/image/tree/main?tab=readme-ov-file#building . Something at about that level of hassle (or less) should exist for https://github.com/containers/image/tree/main?tab=readme-ov-file#building , or those developers are going to start sticking containers_image_openpgp in Makefiles.

Not sure if this is an ideal solution, but one option might be to merge mechanism_sequoia.go into mechanism_gpgme.go (rename it accordingly), and make it use the existing GPGME backend as a fallback if it fails to load libpodman_sequoia.so.0. The current Go binding uses dlwrap to enable this kind of run-time detection of Sequoia through sequoia.Init.

@ueno ueno force-pushed the wip/signature-sequoia branch 3 times, most recently from fa356ee to 23fec2d Compare September 19, 2024 02:30
@mtrmac
Copy link
Collaborator
mtrmac commented Sep 19, 2024

Not sure if this is an ideal solution, but one option might be to merge mechanism_sequoia.go into mechanism_gpgme.go (rename it accordingly), and make it use the existing GPGME backend as a fallback if it fails to load libpodman_sequoia.so.0.

That’s interesting … that would certainly eliminate concerns about developers not being able to work on other things. OTOH we’d then need some other mode to run in tests, and in production, to ensure we are using the right backend.

@ueno
Copy link
Author
ueno commented Apr 15, 2025

@mtrmac I've reworked this and incorporated all the required code into the same tree, instead of vendoring an external repository. The rust code is now hosted in signatures/sequoia/rust, which can be packaged something like rust-rpm-sequoia; we (RHEL Crypto team) are happy to own the downstream package.

As for the CI coverage, the base CI image needs to be updated to include the Rust toolchain and OpenSSL development files. Could you help with that?

@ueno ueno force-pushed the wip/signature-sequoia branch from 995e8aa to f8df8c8 Compare April 15, 2025 02:00
@ueno ueno marked this pull request as ready for review April 15, 2025 02:00
@ueno ueno force-pushed the wip/signature-sequoia branch from f8df8c8 to bdfcafc Compare April 15, 2025 21:58
@ueno
Copy link
Author
ueno commented Apr 15, 2025

To test it locally, I currently do the following on Fedora 41:

cd signature/sequoia/rust
PREFIX=/usr LIBDIR="\${prefix}/lib64" cargo build --release
cd -
sudo update-crypto-policies --set LEGACY
LD_LIBRARY_PATH=$PWD/signature/sequoia/rust/target/release make BUILDTAGS="btrfs_noversion containers_image_sequoia"

@ueno ueno force-pushed the wip/signature-sequoia branch from bdfcafc to 344c8ea Compare May 13, 2025 01:54
@mtrmac
Copy link
Collaborator
mtrmac commented May 21, 2025

Cc: @jnovy, I’ll probably come asking questions.

Copy link
Collaborator
@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a very first look at the code, filing this without trying this or a full understanding.

One highlight is the key migration … and I don’t know what happens about passphrases / workflows that currently assume gpg-agent. We might have to give up on that?!

Comment on lines +51 to +52
for _, keyring := range []string{"pubring.gpg", "secring.gpg"} {
blob, err := os.ReadFile(path.Join(gpgHome, keyring))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be copying all keys from ~/.gnupg into ~/.local/share/sequoia, on every single podman push --sign-by? That seems unexpected.

I don’t know what to do here… at a first glance, maybe, if we find such keys, we need to load them only into an ephemeral store.

Or maybe this is fine, for products where users / product owners opt in — probably implying that Sequoia wouldn’t be the default. Well, it isn’t a default in this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out ( https://www.gnupg.org/faq/whats-new-in-2.1.html#nosecring ) that recent GPG implementations are not writing to secring.gpg at all, so this would not necessarily import all relevant keys.


And after I generated a new ed25519 key pair using the default settings of GnuPG 2.4.8, the sequoia backend imported it successfully, but using it failed with Malformed MPI: Details omitted, parsing secret; that seems to be a message that applies also when an incorrect passphrase is used ?!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

… a rsa3072 key works.

⚠️ If the Sequoia backend isn’t compatible enough to be a drop-in replacement, I think that’s a significant problem, and we might be forced to keep the GPG implementation around and to only use Sequoia with an opt-in. [Well, that would also resolve the difficulties with import, and gpg-agent compatibility. But I don’t know how/where we would add the option on the verification path.]

Is there something non-obvious I need to do to support the GnuPG-default ed25519 keys?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,228 @@
//go:build containers_image_sequoia
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME: I didn’t read this yet.

let bindings_dir = build_dir.join("bindings");
let include = bindings_dir.join("sequoia.h");

// Generate ${CARGO_TARGET_DIR}/${PROFILE}/bindings/sequoia.h
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s unclear to me how this relates to the code in this PR — nothing uses bindings/…. Is the sequoia.h included in this PR auto-generated by this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the *.[ch] files are, originally, generated: https://github.com/ueno/podman-sequoia/blob/ffc01d5b6fdb6032e36e32f899652d9134d3a62a/go/GNUmakefile#L15-L24 .

We should generate them as a part of the build (forcing users to use make???`), or (preferably??) ensure in CI that the files have not been modified automatically.

Or are we going to freeze the C API, in order to have a stable shared library interface across independently-updated packages?

return nil, err
}
} else if _, err := mech.ImportKeys(blob); err != nil {
return nil, err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably wrap with something that indicates this is a failure to import a specific file.

@mtrmac
Copy link
Collaborator
mtrmac commented Jun 9, 2025

To test it locally, I currently do the following on Fedora 41:

macOS:

(cd signature/sequoia/rust; DYLD_FALLBACK_LIBRARY_PATH="$(xcode-select --print-path)/Toolchains/XcodeDefault.xctoolchain/usr/lib/" LIBDIR="$(pwd)" cargo build --release)

where DYLD_FALLBACK_LIBRARY_PATH is required to find a libclang.dylib

… and more modifications are required in the caller.

@mtrmac
Copy link
Collaborator
mtrmac commented Jun 16, 2025

A high-level note: during a personal discussion at DevConf.CZ, it was agreed that the preferred design is to use Sequoia for all signature verifications; but for signing, to expose signing using GnuPG-maintained secret keys, and Sequoia-maintained secret keys, as two separate features in the UI.

That way, existing GnuPG keys, and existing gpg-agent-reliant workflows will continue to work, while the Sequoia implementation, including its new algorithm support, can be used by those who generate new keys or can migrate; and the c/image implementation will not need to worry about transparently/automatically migrating users’ secret keys.

@ueno
Copy link
Author
ueno commented Jun 18, 2025

@nwalfield pointed out that, sq can transparently use gpg-agent, so the migration of user's secret keys shouldn't be needed as long as gpg-agent is installed. The feature is implemented in the sequoia-keystore crate, which this PR already uses, but currently it excludes gpg-agent support. Maybe we could enable it and remove all the manual migration logic from this PR, which might be simpler than exposing two separate UI for signing. @mtrmac What do you think?

@mtrmac
Copy link
Collaborator
mtrmac commented Jun 18, 2025

@ueno I think that might well make sense to do, but not as the first step.

I don’t think full transparency is achievable: if we want to support PQC algorithms using the Sequoia implementations, users should be manually generating keys using Sequoia directly, without GnuPG. So, users will need to use sq and deal with Sequoia key storage mechanisms directly; asking them to use GNUPGHOME or some other GnuPG-primary mechanism to point “not-actually-GnuPG” at the Sequoia directory would only be confusing.

So, I think the best UI will be to add new CLI features (skopeo --sign-by-sq or something like that), with the Sequoia key store configuration a new option, not interacting with GNUPGHOME at all. By using a new CLI option, users explicitly opt-in into a behavior change, so we don’t need to try to interact with gpg-agent in that flow.

In the first version of this, it would be less work to keep the existing gpgme backend (for signing, not verification — hopefully all verification can be done using Sequoia) for the currently-existing CLI flows: that will guarantee full compatibility.

And afterwards, after this time-sensitive work is time-sensitive is done, we can consider replacing the gpgme calls with the Sequoia gpg-agent. Certainly removing the dependency on GnuPG would be nice, but if it is transparent, it’s probably not as urgent. And I think it is somewhat blocked on understanding of things like https://gitlab.com/sequoia-pgp/sequoia/-/issues/1194 — it’s quite possible that this would not be an issue if the signing happened in gpg-agent, but let’s see what that investigation shows.


@ueno I can work on the Go / c/image-side work to introduce the new Sequoia-only signing features, and handle the other small comments from the earlier review. (I’d be also happy to work on improvements to the Rust side, with the disclaimer that I’m very new to Rust and completely unfamiliar with the Sequoia codebase and API.)

The primary thing I’d need help with is for you and @jnovy to come to an agreement on packaging / ownership. Does the offer that the RHEL Crypto team could own and maintain the Rust-side package still stand?

In that case, it would probably make most sense for the Rust code to live in its own repository (with c/image containing just a static copy of the generated headers and CGo stubs); and I think it would be simpler for Podman/Buildah/Skopeo to link directly to a shared library, without dlopen, so that the usual linker dependencies and tools like nm can help with symbol lookups.

@ueno ueno force-pushed the wip/signature-sequoia branch from 344c8ea to 1e352d0 Compare June 18, 2025 14:41
@ueno
Copy link
Author
ueno commented Jun 18, 2025

@ueno I can work on the Go / c/image-side work to introduce the new Sequoia-only signing features, and handle the other small comments from the earlier review. (I’d be also happy to work on improvements to the Rust side, with the disclaimer that I’m very new to Rust and completely unfamiliar with the Sequoia codebase and API.)

That would be great! I just pushed some fixes (not all) to the issues pointed in the review, FWIW.

The primary thing I’d need help with is for you and @jnovy to come to an agreement on packaging / ownership. Does the offer that the RHEL Crypto team could own and maintain the Rust-side package still stand?

Yes, it does.

In that case, it would probably make most sense for the Rust code to live in its own repository (with c/image containing just a static copy of the generated headers and CGo stubs); and I think it would be simpler for Podman/Buildah/Skopeo to link directly to a shared library, without dlopen, so that the usual linker dependencies and tools like nm can help with symbol lookups.

That would be simpler, though I actually chose this dlopen design in case you want to keep both GnuPG and Sequoia interfaces (and do not always want to link to Sequoia, as the binary size is not small). If it is not a problem, we can certainly make it as a shared library.

@mtrmac
Copy link
Collaborator
mtrmac commented Jun 18, 2025

I actually chose this dlopen design in case you want to keep both GnuPG and Sequoia interfaces (and do not always want to link to Sequoia, as the binary size is not small). If it is not a problem, …

I don’t know. @jnovy ?

@ueno ueno force-pushed the wip/signature-sequoia branch from 1e352d0 to 19cc16f Compare June 18, 2025 15:13
This adds a new OpenPGP signing mechanism based Sequoia-PGP[1]. As
Sequoia-PGP is written in Rust and doesn't provide a stable C FFI,
this integration includes a minimal shared library as a "point
solution".

To build, first follow the instruction in signature/sequoia/README.md
and install `libimage_sequoia.so*` into the library path, and then
build with the following command from the top-level directory:

  $ make BUILDTAGS="btrfs_noversion containers_image_sequoia"

Note also that, for testing on Fedora, crypto-policies settings might
need to be modified to explictly allow SHA-1 and 1024 bit RSA, as the
testing keys in signature/fixtures are using those legacy algorithms.

1. https://sequoia-pgp.org/

Signed-off-by: Daiki Ueno <dueno@redhat.com>
@ueno ueno force-pushed the wip/signature-sequoia branch from 19cc16f to a83a195 Compare June 19, 2025 08:47
Copy link
@nwalfield nwalfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ueno, this looks really good! In my review, I focused on the Rust code, as I don't feel qualified to comment on the rest.

fn from_directory(dir: impl AsRef<Path>) -> Result<Self, anyhow::Error> {
let home_dir = dir.as_ref().to_path_buf();

let keystore_dir = home_dir.join("data").join("keystore");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your goal is to be compatible with sq, then this is corrent when SEQUOIA_HOME is set. If SEQUOIA_HOME is not set, then we use the xdg paths. The sequoia-directories does most of this automatically.

let primary_key_handle: KeyHandle = key_handle.parse()?;
let certs = self
.certstore
.lookup_by_cert_or_subkey(&primary_key_handle)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you know you have a primary key handle, then you should use lookup_by_cert instead of lookup_by_cert_or_subkey.


let mut signing_key_handles: Vec<KeyHandle> = vec![];
for cert in certs {
for ka in cert.keys().with_policy(&self.policy, None).for_signing() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check:

  • The cert is not revoked
  • The cert is live (not expired)
  • The subkey is not revoked
  • The subkey is live (not expired)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: Do we need more test coverage?


The primary caller does verification in a ~stateless manner, given a signature and a set of acceptable public keys. That input might contain top-level revocations, but that seems unlikely. Expired keys are definitely a possibility, it’s a bit hard to infer user intent in that case.

Also there’s some earlier history in containers/podman#16406 , which says subkeys don’t work with GPGME at all — and some speculation how key revocation / expiration without a trusted timestamp can be problematic.

};
let mut policy = ConfiguredStandardPolicy::new();
policy.parse_default_config()?;
let policy = policy.build();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you reuse the policy in self.policy?

MessageLayer::SignatureGroup { ref results } => {
let result = results.iter().find(|r| r.is_ok());
if let Some(result) = result {
self.signer = Some(result.as_ref().unwrap().ka.cert().to_owned());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, you need to check that the certificate and subkey are not revoked and live.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for, or a PR adding, new functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0