k2gl / dsse
Sign and verify DSSE (Dead Simple Signing Envelope) payloads in PHP, with pluggable keys
Requires
- php: >=8.1
Requires (Dev)
- k2gl/phpunit-fluent-assertions: ^12
- laravel/pint: ~1.20.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10|^11|^12
Suggests
- ext-openssl: Required by the ECDSA (P-256/384/521) and RSA (PKCS#1 v1.5) signers/verifiers.
- ext-sodium: Required by Ed25519Signer / Ed25519Verifier (Ed25519).
Provides
None
Conflicts
None
Replaces
None
README
Sign and verify DSSE (Dead Simple Signing Envelope) payloads in PHP with pluggable keys. It's the envelope Sigstore, in-toto, SLSA and npm provenance use to wrap a signed payload.
It gives you the three pieces of the spec and nothing else:
- PAE — the exact, binary-safe byte string that gets signed.
- Envelope — the JSON envelope (
payload/payloadType/signatures) with lossless (de)serialization. - Signer / Verifier — tiny interfaces so you can plug in any key (or a remote KMS/HSM). ECDSA (P-256/384/521), Ed25519, and RSA (PKCS#1 v1.5) implementations are included.
Install
composer require k2gl/dsse
Requires PHP 8.1+. The bundled signers use ext-openssl (ECDSA P-256/384/521, RSA) and
ext-sodium (Ed25519); both ship with PHP by default. The core (Pae, Envelope)
needs neither.
Usage
PAE — what actually gets signed
use K2gl\Dsse\Pae; Pae::encode('http://example.com/HelloWorld', 'hello world'); // "DSSEv1 29 http://example.com/HelloWorld 11 hello world"
Lengths are byte counts, so the encoding is unambiguous for any payload, including binary data.
Sign
use K2gl\Dsse\Envelope; use K2gl\Dsse\EcdsaP256Signer; $signer = EcdsaP256Signer::fromPem($privateKeyPem, keyId: 'k1'); $envelope = Envelope::sign('hello world', 'http://example.com/HelloWorld', $signer); echo $envelope->toJson(); // {"payload":"aGVsbG8gd29ybGQ=","payloadType":"http://example.com/HelloWorld","signatures":[{"keyid":"k1","sig":"..."}]}
Envelope::sign() accepts several signers to produce a multi-signature envelope.
Verify
use K2gl\Dsse\Envelope; use K2gl\Dsse\EcdsaP256Verifier; use K2gl\Dsse\Exception\SignatureVerificationFailed; $envelope = Envelope::fromJson($json); try { $payload = $envelope->verify(EcdsaP256Verifier::fromPem($publicKeyPem)); // $payload === 'hello world' } catch (SignatureVerificationFailed) { // no signature matched any supplied verifier }
The envelope is accepted if any signature verifies against any verifier you pass, mirroring the spec's verification model. Pass several verifiers to accept a set of trusted keys.
Ed25519
use K2gl\Dsse\Ed25519Signer; use K2gl\Dsse\Ed25519Verifier; $keypair = sodium_crypto_sign_keypair(); $signer = new Ed25519Signer(sodium_crypto_sign_secretkey($keypair), 'ed-1'); $verifier = new Ed25519Verifier(sodium_crypto_sign_publickey($keypair));
ECDSA P-384 / P-521 and RSA
The other bundled algorithms follow the same fromPem() pattern:
use K2gl\Dsse\EcdsaP384Signer; use K2gl\Dsse\EcdsaP384Verifier; use K2gl\Dsse\RsaSigner; use K2gl\Dsse\RsaVerifier; $signer = EcdsaP384Signer::fromPem($p384PrivateKeyPem); $verifier = EcdsaP384Verifier::fromPem($p384PublicKeyPem); // RSASSA-PKCS1-v1_5; hash algorithm defaults to sha256 (sha384/sha512 also supported) $rsaSigner = RsaSigner::fromPem($rsaPrivateKeyPem, hashAlgorithm: 'sha512'); $rsaVerifier = RsaVerifier::fromPem($rsaPublicKeyPem, hashAlgorithm: 'sha512'); // RSASSA-PSS (MGF1, salt as long as the hash) — what the Go and Python reference // implementations sign with; same hash choices $pssSigner = RsaPssSigner::fromPem($rsaPrivateKeyPem); $pssVerifier = RsaPssVerifier::fromPem($rsaPublicKeyPem);
EcdsaP521Signer / EcdsaP521Verifier work identically. ext-openssl has no PSS
padding, so RsaPssSigner / RsaPssVerifier run the RSA primitive bare and do
EMSA-PSS (RFC 8017) themselves — no extra dependency.
The ECDSA signers write raw r||s by default; ask for DER when the consumer is one
of the reference implementations, which verify only that:
use K2gl\Dsse\SignatureEncoding; $signer = EcdsaP256Signer::fromPem($privateKeyPem, encoding: SignatureEncoding::Der);
Loading a key without knowing its algorithm
When a key arrives as a PEM file or a JWK from a JWKS endpoint, PublicKey picks the
right verifier for you — RSA, ECDSA (P-256/384/521) or Ed25519 — so you don't have to
branch on the algorithm yourself:
use K2gl\Dsse\PublicKey; $verifier = PublicKey::fromPem($publicKeyPem); // detects the algorithm and curve $verifier = PublicKey::fromJwk($jwk); // EC / RSA / OKP (Ed25519) $payload = $envelope->verify($verifier);
An RSA PEM carries neither hash nor padding, so it verifies PKCS#1 v1.5 over SHA-256;
for another hash use RsaVerifier::fromPem($pem, hashAlgorithm: 'sha512'), for PSS
RsaPssVerifier. An RSA JWK usually says in alg what it is for, and that is honoured:
RS256/RS384/RS512 pick the hash, PS256/PS384/PS512 pick PSS.
KeyId computes the two identifiers commonly used for a signature's keyId:
use K2gl\Dsse\KeyId; KeyId::sha256Spki($publicKeyPem); // hex SHA-256 of the DER key (cosign / Sigstore style) KeyId::jwkThumbprint($jwk); // RFC 7638 base64url thumbprint
Plugging in your own key backend
Implement two methods to sign with a KMS/HSM or any other scheme:
use K2gl\Dsse\Signer; final class KmsSigner implements Signer { public function sign(string $message): string { /* sign PAE bytes, return raw signature */ } public function keyId(): ?string { return 'arn:aws:kms:...'; } }
Interoperability
Envelopes made by the two reference implementations —
go-securesystemslib and
securesystemslib (Python) — are
part of the test suite, one per scheme: ECDSA P-256/384/521, Ed25519, RSASSA-PSS and, from
Python, RSASSA-PKCS1-v1_5. The vectors under tests/fixtures/interop were produced by
the generators in tests/interop, and CI regenerates them with the upstream code on every
run and checks the other direction too: envelopes from every bundled signer are verified
by Go and by Python.
Two things the vectors settled. The reference implementations sign RSA with PSS
(Go has nothing else), hence RsaPssSigner / RsaPssVerifier. And they verify ECDSA
signatures in DER only, hence SignatureEncoding::Der on the ECDSA signers; the
verifiers here take either form.
Design
- Crypto-agnostic core.
PaeandEnvelopecarry no cryptography; signing is delegated toSigner/Verifier, so you control the algorithm and key storage. - Raw signatures by default. The bundled ECDSA signers emit raw
r||ssignatures (64/96/132 bytes for P-256/384/521), the form JOSE and WebCrypto use, converting from OpenSSL's DER internally;SignatureEncoding::Derkeeps the DER for consumers that want it (Sigstore bundles, the Go and Python reference implementations). The verifiers accept both, detecting the encoding automatically. - Strict and typed.
declare(strict_types=1)throughout, analysed at PHPStan level 9; every exception implementsDsseException.
License
MIT — see LICENSE.
Based on the DSSE specification (Apache-2.0) by the Secure Systems Lab; this is an independent, clean-room PHP implementation.