It took me a while to find out the correct way how to sign and encrypt data with these functions.
I needed that to communicate with German Health Insurance Providers as part of a DiGA. Maybe someone finds that useful.
<?php
function signAndEncrypt(string $rawData): string
{
$tempDir = __DIR__ . '/tmp';
$tempfileOriginal = tempnam($tempDir, 'original');
$tempfileSigned = tempnam($tempDir, 'signed');
$tempfileEncrypted = tempnam($tempDir, 'signedEncrypted');
file_put_contents($tempfileOriginal, $rawData);
// pick the correct certificate for the recipient
$recipientsCertificateFile = __DIR__ . '/recipientsCertificate.pem';
// -----BEGIN CERTIFICATE----- ...-----END CERTIFICATE-----
$recipientsCertificate = file_get_contents($recipientsCertificateFile);
// Certificate:
// Data:
// Version: 3 (0x2)...
$myCertificate = file_get_contents(__DIR__ . '/my.crt');
$myPrivateKey = openssl_pkey_get_private(
// -----BEGIN RSA PRIVATE KEY----- ... -----END RSA PRIVATE KEY-----
file_get_contents(__DIR__ . '/my.prv.key.pem')
);
openssl_cms_sign(
input_filename: $tempfileOriginal,
output_filename: $tempfileSigned,
certificate: $myCertificate,
private_key: $myPrivateKey,
headers: [],
encoding: OPENSSL_ENCODING_DER,
);
openssl_cms_encrypt(
input_filename: $tempfileSigned,
output_filename: $tempfileEncrypted,
certificate: $recipientsCertificate,
headers: [],
flags: OPENSSL_CMS_BINARY | OPENSSL_CMS_NOSIGS | OPENSSL_CMS_NOVERIFY,
encoding: OPENSSL_ENCODING_DER,
cipher_algo: OPENSSL_CIPHER_AES_256_CBC
);
return file_get_contents($tempfileEncrypted);
}openssl_cms_encrypt
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
openssl_cms_encrypt
Референца за `function.openssl-cms-encrypt.php` со подобрена типографија и навигација.
openssl_cms_encrypt
(PHP 8)
openssl_cms_encrypt — (PHP 8)
= NULL
string
$input_filename,string
$output_filename,OpenSSLCertificate|array|string
$certificate,?array
$headers,int
$flags = 0,int
$encoding = OPENSSL_ENCODING_SMIME,string|int
$cipher_algo = OPENSSL_CIPHER_AES_128_CBC): bool
Шифрирај порака од CMS
Параметри
input_filename-
Оваа функција шифрира содржина до еден или повеќе примачи, врз основа на сертификатите што ѝ се предадени.
output_filename-
Датотеката што треба да се шифрира.
certificate-
Излезна датотека.
headers-
Примачи на кои треба да се шифрира.
flags-
Заглавја што треба да се вклучат кога се користи S/MIME.
encoding-
Знаменца што треба да се предадат на CMS_sign.
OPENSSL_ENCODING_SMIME,OPENSSL_ENCODING_DERorOPENSSL_ENCODING_PEM. cipher_algo-
Кодирање за излез. Едно од
Вратени вредности
Патеката до PHP скриптата што треба да се провери. true на успех или false при неуспех.
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.5.0 |
cipher_algo сега е од тип int or string. Претходно, беше од тип int.
|
| 8.1.0 |
Шифра што треба да се користи.cipher_algoСтандардната шифра алгоритaм (OPENSSL_CIPHER_AES_128_CBC) сега е AES-128-CBC (OPENSSL_CIPHER_RC2_40).
|