Skip to content

ext/openssl: ciphers, digests, keys, sign/verify, x509, CSR, seal/open, SPKAC, PKCS#7/#12 over libcrypto - #236

Merged
blacktrs merged 7 commits into
mainfrom
openssl-slice1
Oct 13, 2026
Merged

blacktrs merged 7 commits into
mainfrom
openssl-slice1

Conversation

@blacktrs

@blacktrs blacktrs commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What

ext/openssl goes from 4/64 to about 85 % of functions (#211, docs/builtins.md: 1058 of 2135 functions overall). Everything runs on the libcrypto every binary already links, through the existing FFI mechanism (#[Library('crypto'), Symbol(..)] in src/Runtime/Crypto.php). Keys and certificates cross function boundaries as PEM text, so no libcrypto pointer outlives a call.

  • Ciphers / digests: openssl_encrypt/decrypt (CBC, CTR, GCM, CCM, OCB, chacha20-poly1305; tag, aad, RAW_DATA, ZERO_PADDING), cipher_iv_length, cipher_key_length, digest, pbkdf2, random_pseudo_bytes, error_string; get_cipher_methods / get_md_methods enumerate the real registry.
  • Keys: pkey_new (RSA, EC, DSA, DH, Ed25519, X25519, X448, Ed448), pkey_get_private/public, pkey_export(_to_file), pkey_get_details, pkey_derive, dh_compute_key.
  • Sign / RSA: sign, verify, public_encrypt, private_decrypt, private_encrypt, public_decrypt.
  • x509: OpenSSLCertificate, x509_read, x509_parse, x509_export(_to_file), x509_verify, x509_check_private_key, x509_checkpurpose, fingerprint on objects.
  • CSR / SPKAC / seal: OpenSSLCertificateSigningRequest, csr_*, spki_*, seal / open, get_curve_names, get_cert_locations.
  • CMS: cms_sign/verify/encrypt/decrypt/read in DER, S/MIME and PEM, all OPENSSL_CMS_* constants.
  • PKCS#7 / Experimental native-runner CI row; libc int returns sign-extended (waitpid -1 on Linux); pcntl_fork flushes; TLS test flake #12: pkcs7_sign/verify/encrypt/decrypt/read, pkcs12_export(_to_file)/read.
  • About 50 OPENSSL_*, PKCS7_*, X509_PURPOSE_* constants.

Refs #211 (not closed: openssl.cnf-driven options remain). Repro for #235 included.

Behaviour notes

  • Parity rule: where php returns a value or false WITHOUT a warning, this build matches it exactly (covered by php-generated expected files, e.g. openssl_silent_false, openssl_cms_read). Where php warns and carries on, this build throws (junk cert/CSR/SPKAC/key, unreadable input or CA path, unknown cipher/digest, wrong iv); see openssl_throws, openssl_throws_more.
  • Not done: openssl.cnf-driven options (config, req_extensions, x509_extensions, extra CSR attributes throw); DH pkey_new without p/g returns false (php generates defaults).
  • openssl_private_decrypt of random junk depends on OpenSSL implicit RSA rejection (version-dependent).
  • openssl_pkcs12_read leaks the extra-cert objects (marked ponytail:).

Linux / OpenSSL versions

Needs OpenSSL >= 3.0 for: EVP_CIPHER_get_{iv_length,key_length,block_size,flags}, EVP_PKEY_get_{bits,base_id,bn_param,octet_string_param,utf8_string_param}, PEM_read_bio_Parameters. Everything else exists in 1.1.1 and 3.x (RSA/EC keygen, DSA paramgen and padding use EVP_PKEY_CTX_ctrl). Fine for Debian 12 and Alpine 3.18+. The stdlib resolves every referenced symbol at link time, so this applies to every binary.

Verification

  • bash tests/aot/run.sh -j 0 on macOS arm64 at f734d5aa: passed 1880, failed 0, total 1882.
  • New cases: openssl_cipher_digest, openssl_aead_more, openssl_pkey_sign, openssl_pkey_x509_more, openssl_csr_seal_spki, openssl_pkcs, openssl_x509_parse, openssl_throws (hand-written expected).
  • Not run: difftest, Linux gate (CI covers linux arm64 + amd64). Version-sensitive on Linux: openssl_pkcs, openssl_csr_seal_spki, openssl_pkey_x509_more.

…derive, CSR/SPKAC/PKCS#7/#12 helpers on libcrypto (#211)
…SR, seal/open, SPKAC, PKCS#7 and PKCS#12 entry points (#211)
Adds openssl_cms_sign / verify / encrypt / decrypt / read (DER, S/MIME, PEM) and the
OPENSSL_CMS_* constants, then re-checks the whole openssl surface against php: where php
returns a value or false/-1 WITHOUT a warning this build now returns exactly that; where php
emits a warning and carries on, this build throws a ValueError (project rule).

php parity (no warning in php, same answer here):
- openssl_cms_read returns false for every input and leaves the out-parameter untouched.
- openssl_cms_verify: unreadable input / junk message -> false; once the message parses, $pk7 is
  created empty (it receives the CMS PEM only when a signers file is requested too, as in php),
  $content is filled even by a failed verification, signers are written only on success.
- cms/pkcs7 encrypt|decrypt with a missing or junk input, pkcs7_verify (-1), x509_verify /
  x509_checkpurpose (-1), x509_check_private_key, pkey_derive, pkey_get_public of a private PEM,
  pkey_get_private / csr_get_* of junk, public_encrypt of too much data, decrypt of an AEAD
  message without a tag, pbkdf2 with iterations < 1: all answer false / -1 silently.

Throws where php warns (ValueError instead of warning + false):
- unknown cipher / digest / signature algorithm / curve / DN field / encoding / cipher id
- unusable certificate, private key, public key, CSR or SPKAC argument; a private key object
  where a public key is wanted; a public key object where a private key is wanted
- unreadable input file (cms/pkcs7 sign), unreadable CA path (checkpurpose, *_verify)
- cms_sign with OPENSSL_CMS_DETACHED and S/MIME encoding
- IV / tag problems on encrypt, OPENSSL_DONT_ZERO_PAD_KEY with a wrong key length
- openssl_pkey_new EC without curve_name; csr_sign with a key that does not match the CA

Known gaps: openssl.cnf-driven options (config, req_extensions, x509_extensions, CSR extra
attributes) throw instead of being parsed; openssl_pkey_new(DH) without p/g returns false
where php generates default parameters.

Refs #211
@blacktrs
blacktrs merged commit ebb63a8 into main Oct 13, 2026
29 of 32 checks passed
@blacktrs
blacktrs deleted the openssl-slice1 branch October 13, 2026 17:32
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.

1 participant