note 125866 added to function.openssl-cms-decrypt
| From: | Sebastian@php.net | Date: | Sun, 28 Feb 2021 09:25:54 +0000 |
| Subject: | note 125866 added to function.openssl-cms-decrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219284@lists.php.net to get a copy of this message | ||
It took me a while to find out the correct way how to decrypt and verify 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 decryptAndVerify($signedAndEncryptedRawData): string
{
$tempDir = __DIR__ . '/tmp';
$originalFile = tempnam($tempDir, 'original');
$decryptedFile = tempnam($tempDir, 'decrypted');
$verifiedFile = tempnam($tempDir, 'verified');
file_put_contents($originalFile, $signedAndEncryptedRawData);
// One file with all possible certificates one after the other
// -----BEGIN CERTIFICATE----- ...-----END CERTIFICATE-----
$allPossibleSenderCertificates = __DIR__ . '/untrusted.pem';
// 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_decrypt(
input_filename: $originalFile,
output_filename: $decryptedFile,
certificate: $myCertificate,
private_key: $myPrivateKey,
encoding: OPENSSL_ENCODING_DER
);
openssl_cms_verify(
input_filename: $decryptedFile,
flags: OPENSSL_CMS_BINARY | OPENSSL_CMS_NOSIGS | OPENSSL_CMS_NOVERIFY,
ca_info: [],
untrusted_certificates_filename: $allPossibleSenderCertificates,
content: $verifiedFile,
encoding: OPENSSL_ENCODING_DER
);
return file_get_contents($verifiedFile);
}
----
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-decrypt.php
Edit -- https://master.php.net/note/edit/125866
Del: integrated -- https://master.php.net/note/delete/125866/integrated
Del: useless -- https://master.php.net/note/delete/125866/useless
Del: bad code -- https://master.php.net/note/delete/125866/bad+code
Del: spam -- https://master.php.net/note/delete/125866/spam
Del: non-english -- https://master.php.net/note/delete/125866/non-english
Del: in docs -- https://master.php.net/note/delete/125866/in+docs
Del: other reasons-- https://master.php.net/note/delete/125866
Reject -- https://master.php.net/note/reject/125866
Search -- https://master.php.net/manage/user-notes.php