Skip to content
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

added implementation of the openssl_x509_verify function #917

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtin-functions/_functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ function openssl_sign ($data ::: string, &$signature ::: string, $priv_key_id ::
function openssl_verify ($data ::: string, $signature ::: string, $pub_key_id ::: string, $signature_alg ::: int = 1) ::: int;
function openssl_random_pseudo_bytes ($length ::: int) ::: string | false;
function openssl_x509_parse ($x509cert ::: string, $shortnames ::: bool = true) ::: mixed[] | false;
function openssl_x509_verify ($x509cert ::: string, $public_key ::: string) ::: int;
function openssl_x509_checkpurpose ($x509cert ::: string, $purpose ::: int) ::: mixed;

define('PKCS7_TEXT', 0x1);
Expand Down
24 changes: 24 additions & 0 deletions runtime/openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,26 @@ class X509_parser {
return static_cast<bool>(ret);
}

int64_t verify(const string &public_key) const noexcept {
if (!x509_) {
return -1;
}
dl::CriticalSectionSmartGuard critical_section;

bool from_cache = false;
EVP_PKEY *pkey = openssl_get_public_evp(public_key, from_cache);
if (pkey == nullptr) {
critical_section.leave_critical_section();
DrDet marked this conversation as resolved.
Show resolved Hide resolved
php_warning("Parameter key is not a valid public key");
return -1;
}
int64_t result = X509_verify(x509_, pkey);
if (!from_cache) {
EVP_PKEY_free(pkey);
}
return result;
}

~X509_parser() noexcept {
dl::CriticalSectionGuard critical_section;
stop_x509_processing(x509_);
Expand Down Expand Up @@ -1393,6 +1413,10 @@ Optional<array<mixed>> f$openssl_x509_parse(const string &data, bool shortnames
return X509_parser{data}.parse(shortnames);
}

int64_t f$openssl_x509_verify(const string &signature, const string &public_key) {
return X509_parser{signature}.verify(public_key);
}

mixed f$openssl_x509_checkpurpose(const string &data, int64_t purpose) {
return X509_parser{data}.check_purpose(purpose);
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Optional<string> f$openssl_random_pseudo_bytes(int64_t length);

Optional<array<mixed>> f$openssl_x509_parse(const string &data, bool shortnames = true);

int64_t f$openssl_x509_verify(const string &certificate, const string &public_key);

mixed f$openssl_x509_checkpurpose(const string &data, int64_t purpose);

bool f$openssl_pkcs7_sign(const string &infile, const string &outfile,
Expand Down
73 changes: 73 additions & 0 deletions tests/phpt/openssl/8_openssl_x509_verify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
@ok
<?php

$certificate = <<<EOF
-----BEGIN CERTIFICATE-----
MIIDazCCAlOgAwIBAgIUM2N/ghpbi9B4S2PNcmKylVu6qNAwDQYJKoZIhvcNAQEL
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDEwMjExMzM3MTlaFw0yMTEw
MjExMzM3MTlaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQDJ4L96idNQxd0zIcbHnAm0TAD+P9j1j/aMBW/Xqq/N
wdnUzCaj5sXP7wPemXOa1adyZZPf+y6N/BRmROQTHQ5ACq5aC8bj9j+iIh1sr8wy
q2ulv2I5ya+EijEdI+aV9PPaw4ZjwFVLhsimpCqUGg6xewRIE1bfpvkFDTh3t+x+
934xeZrLQjybq8jhyGUxpPVmm7qIgMg3h29ubJs+dEn8tnf+cM9tk3gZXcpnUmm7
hhyvH+mf3cGHsnW/WZ+/Di1IUPqJPw744bpPAZfSAqOWypu2/XZRedvwa9KBfY/N
EPqp2fD3sKNURhxPg4nrjIu24zDyJY4rAchhLDB/j67dAgMBAAGjUzBRMB0GA1Ud
DgQWBBRBB1A15ReS/WLe8mkhRRLmt0fMkzAfBgNVHSMEGDAWgBRBB1A15ReS/WLe
8mkhRRLmt0fMkzAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQB1
5c1r8ys83G4ZPbDijmDAYMiTn6dpj5QZVQzJ6+Ii0sHQC3G81dKVwi+kjv4wd8Qx
sr/xhhEGYz/HXNsic1kNJesem0eiqEadj2I4l/VKPpF7k/mfVdJRMmmSSoQjeHBD
Bh2s3EGrZKzs0iEUl6/dzWoCv0xcXB5/g1nsTFEpF6rH8OQg+tNxpzEGz7gYBg6w
dWA/yDkCSRBAhGRlxJ9UQVpLr/tDn0x53QqfC6mq4wH73ghlHlWNrDDmAQLcF1ZK
mcbr89vkJ9wrPacySkwkMRAdO0+lWaznwjbEwTUK/0QZWysJPUw0phqsYyTn0KFi
nvYpS4Nc4toUevFuCxqv
-----END CERTIFICATE-----
EOF;

$private_key = <<<EOF
DrDet marked this conversation as resolved.
Show resolved Hide resolved
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDJ4L96idNQxd0z
IcbHnAm0TAD+P9j1j/aMBW/Xqq/NwdnUzCaj5sXP7wPemXOa1adyZZPf+y6N/BRm
ROQTHQ5ACq5aC8bj9j+iIh1sr8wyq2ulv2I5ya+EijEdI+aV9PPaw4ZjwFVLhsim
pCqUGg6xewRIE1bfpvkFDTh3t+x+934xeZrLQjybq8jhyGUxpPVmm7qIgMg3h29u
bJs+dEn8tnf+cM9tk3gZXcpnUmm7hhyvH+mf3cGHsnW/WZ+/Di1IUPqJPw744bpP
AZfSAqOWypu2/XZRedvwa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pcooJpassAAgwzbAU51aBxb
sfxIz+fIN8f+I1hEMRsf9/FBsc6CCc3UqxGplmVjAoGAbsfPjBWeZwnvXJKit/7m
zq/Yb5nPZaOj3+ThE3lkj1YbLi0uikwBSq6qOH7FAlU4dNFw3lDi3Dv+B/bkCQHV
X0PiNUXzW3XI5upkKFEywZoalo1I0PbnQ8O6xPJtr/qwU/epH6IvNe2VH57F6zHt
F8Q1HP10FI4YYEnQ3a7JK18=
-----END PRIVATE KEY-----
EOF;

$public_key = <<<EOF
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyeC/eonTUMXdMyHGx5wJ
tEwA/j/Y9Y/2jAVv16qvzcHZ1Mwmo+bFz+8D3plzmtWncmWT3/sujfwUZkTkEx0O
QAquWgvG4/Y/oiIdbK/MMqtrpb9iOcmvhIoxHSPmlfTz2sOGY8BVS4bIpqQqlBoO
sXsESBNW36b5BQ04d7fsfvd+MXmay0I8m6vI4chlMaT1Zpu6iIDIN4dvbmybPnRJ
/LZ3/nDPbZN4GV3KZ1Jpu4Ycrx/pn93Bh7J1v1mfvw4tSFD6iT8O+OG6TwGX0gKj
lsqbtv12UXnb8GvSgX2PzRD6qdnw97CjVEYcT4OJ64yLtuMw8iWOKwHIYSwwf4+u
3QIDAQAB
-----END PUBLIC KEY-----
EOF;

$res = openssl_x509_verify($certificate, $public_key);

var_dump($res);