note 125865 added to function.openssl-cms-encrypt
| From: | Sebastian@php.net | Date: | Sun, 28 Feb 2021 09:13:17 +0000 |
| Subject: | note 125865 added to function.openssl-cms-encrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219123@lists.php.net to get a copy of this message | ||
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);
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.247 (proxied: 217.110.80.32)
----
Manual Page -- http://php.net/manual/en/function.openssl-cms-encrypt.php
Edit -- https://master.php.net/note/edit/125865
Del: integrated -- https://master.php.net/note/delete/125865/integrated
Del: useless -- https://master.php.net/note/delete/125865/useless
Del: bad code -- https://master.php.net/note/delete/125865/bad+code
Del: spam -- https://master.php.net/note/delete/125865/spam
Del: non-english -- https://master.php.net/note/delete/125865/non-english
Del: in docs -- https://master.php.net/note/delete/125865/in+docs
Del: other reasons-- https://master.php.net/note/delete/125865
Reject -- https://master.php.net/note/reject/125865
Search -- https://master.php.net/manage/user-notes.php