openssl_pkcs7_verify

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

openssl_pkcs7_verifyПроверить подпись сообщения S/MIME

Описание

openssl_pkcs7_verify(
    string $input_filename,
    int $flags,
    string|null $signers_certificates_filename = null,
    array $ca_info = [],
    string|null $untrusted_certificates_filename = null,
    string|null $content = null,
    string|null $output_filename = null
): bool|int

openssl_pkcs7_verify() читает S/MIME сообщение из файла и проверяет его подпись.

Список параметров

input_filename

Путь к файлу с сообщением.

flags

flags можно использовать для модификации процесса проверки. Более подробно смотрите константы PKCS7.

signers_certificates_filename

Если задан параметр signers_certificates_filename, то в нем должна быть строка с именем файла, в который будут сохранены сертификаты, использованные при подписании, в формате PEM.

ca_info

Если задан параметр ca_info, то в нем должна содержаться информация о доверенных сертификатах CA, которые необходимо использовать в процессе проверки. Более подробно читайте на странице проверки сертификатов.

untrusted_certificates_filename

Если задан параметр untrusted_certificates_filename, в нем должно содержаться имя файла, содержащего набор недоверенных сертификатов CA.

content

В параметре content можно указать имя файла, в который будут записаны верифицированные данные без информации о подписи.

output_filename

Возвращаемые значения

Возвращает true, если проверка успешна, false, если нет и -1 в случае ошибки.

Список изменений

Версия Описание
8.0.0 signers_certificates_filename, untrusted_certificates_filename, content и output_filename теперь допускают значение null.
7.2.0 Добавлен параметр output_filename.

Примечания

Замечание: Как указано в RFC 2045, длина параметра input_filename не должна быть длиннее 76 символов.

add a note add a note

User Contributed Notes 2 notes

up
4
reg1barclay at REMOVETHIS dot live dot it
5 years ago
To verify a .p7m file with openssl_pkcs7_verify() you must convert it to S/MIME format. For example...
<?php
function der2smime($file)
{
   
$to=<<<TXT
MIME-Version: 1.0
Content-Disposition: attachment; filename="smime.p7m"
Content-Type: application/x-pkcs7-mime; smime-type=signed-data; name="smime.p7m"
Content-Transfer-Encoding: base64
\n
TXT;
   
$from=file_get_contents($file);
   
$to.=chunk_split(base64_encode($from));
    return
file_put_contents($file,$to);
}
?>
up
-2
Krzychu
10 years ago
To read signed message in base64 (not encrypted with priv&pub key):

You can just decode content by "base64_decode" or "imap_base64" functions and then erase by hand(regexp) sign from bottom of mail. Unfortunately  in my case (mail from Outlook) that  message (decoded by "base64_decode") has some additional special chars in some places (ie. before every attachment encoded base_64) what make message e-mail unable to parse.

After couple of hours I solved this:
It's needed to save single e-mail and use 2x "openssl_pkcs7_verify" function in row on original email (with headers and content in base64 ):
  1st use - extract sign (certificate) from e-mail and save to file *.cert
  2nd use - extract (with use that *.cert file) decoded message to  file*.out

Code:
  $handle  =  imap_open('mailbox.eml', '', '');

  $msg = 'home/john/tmp/email1.eml';
  imap_savebody($handle, $msg,  1);

  openssl_pkcs7_verify($msg, 0, $msg . '.cert');
  openssl_pkcs7_verify($msg, 0, $msg . '.cert', array(), $msg . '.cert', $msg.'.out');

  $email_content = file_get_contents($msg . '.out');
To Top