2024 年 PHP 日本研討會

openssl_pkcs7_verify

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

openssl_pkcs7_verify驗證 S/MIME 簽章訊息的簽章

描述

openssl_pkcs7_verify(
    字串 $input_filename,
    整數 $flags,
    ?字串 $signers_certificates_filename = null,
    陣列 $ca_info = [],
    ?字串 $untrusted_certificates_filename = null,
    ?字串 $content = null,
    ?字串 $output_filename = null
): 布林值|整數

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_filenameuntrusted_certificates_filenamecontentoutput_filename 現在可以為 null。
7.2.0 新增了 output_filename 參數。

注意事項

注意: 如 RFC 2045 中所述,input_filename 參數中的每一行不得超過 76 個字元。

新增備註

使用者貢獻的備註 1 則備註

reg1barclay at REMOVETHIS dot live dot it
6 年前
要使用 openssl_pkcs7_verify() 驗證 .p7m 檔案,您必須將其轉換為 S/MIME 格式。例如...
<?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);
}
?>
To Top