<?php
// CertKey.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Lib;use Exception;class CertKey{public static function GenerateKeyFiles($privateKeyFile,$publicKeyFile,$opensslConfigFile,$passphrase=null,$overwriteExisting=false):bool{$bOk=false;if(!empty($privateKeyFile)&&($overwriteExisting||!file_exists($privateKeyFile))&&!empty($publicKeyFile)&&($overwriteExisting||!file_exists($publicKeyFile))&&!empty($opensslConfigFile)){if(empty($passphrase))$passphrase=null;try{$passphrase=empty($passphrase)?null:$passphrase;$config=['config'=>$opensslConfigFile,'private_key_bits'=>4096,'private_key_type'=>OPENSSL_KEYTYPE_RSA,'default_alg'=>'sha256','encrypt_key'=>$passphrase!==null,];$res=openssl_pkey_new($config);if($res!==false){$privateKey='';if(openssl_pkey_export($res,$privateKey,$passphrase,$config)===true){$publicKey=openssl_pkey_get_details($res);if($publicKey!==false){$publicKey=$publicKey['key'];if(!empty($publicKey)){file_put_contents($privateKeyFile,$privateKey);file_put_contents($publicKeyFile,$publicKey);$bOk=true;}}}}}catch(Exception $e){$bOk=false;}}return $bOk;}}