openssl_pkcs7_verify

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

openssl_pkcs7_verifyS/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 は、nullable になりました。
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