$bits, "private_key_type" => $type, ]); // Generiamo i pem $rsa_pub = openssl_pkey_get_details($key)['key']; openssl_pkey_export($key, $rsa_pem); self::set($name, $domain, "public", $rsa_pub); self::set($name, $domain, "private", $rsa_pem); } public static function getKeyPath(string $kid, string $domain) { global $config; return realFolderPathOrCreate( $config["procurePlus"]["keyPath"] . DIRECTORY_SEPARATOR . $config["id-installazione"] . DIRECTORY_SEPARATOR . $domain ) . DIRECTORY_SEPARATOR . $kid; } /** * Elimina una chiave dal keystore * * @param string $name Nome della chiave * @param string $domain Dominio della chiave * @param string $kind Tipo di chiave (public/private) */ public static function delete(string $name, string $domain, string $kind) { // Cerchiamo il file $extension = ["public" => ".rsa.pub", "private" => ".rsa.pem"][$kind] ?? null; if($extension === null) throw new Exception("Tipo di chiave '$kind' non valido"); $path = self::getKeyPath($name, $domain) . $extension; // Creiamo un backup se esiste già if (file_exists($path)) { unlink($path); } } /** * Aggiunge una chiave al keystore * * @param string $name Nome della chiave * @param string $domain Dominio della chiave * @param string $kind Tipo di chiave (public/private) * @param string $data Contenuto della chiave */ public static function set(string $name, string $domain, string $kind, string $data) { // Cerchiamo il file $extension = ["public" => ".rsa.pub", "private" => ".rsa.pem"][$kind] ?? null; if($extension === null) throw new Exception("Tipo di chiave '$kind' non valido"); $path = self::getKeyPath($name, $domain) . $extension; // Creiamo un backup se esiste già if (file_exists($path)) { file_put_contents($path . ".bkp." . uniqid(date("Ymd")), file_get_contents($path)); } file_put_contents($path, $data); } /** * Ottiene una chiave dal keystore * * @param string $name Nome della chiave * @param string $domain Dominio della chiave * @param string $kind Tipo di chiave (public/private) * @return string Contenuto della chiave */ public static function get(string $name, string $domain, string $kind) { $extension = ["public" => ".rsa.pub", "private" => ".rsa.pem"][$kind] ?? null; if($extension === null) throw new Exception("Tipo di chiave '$kind' non valido"); $path = self::getKeyPath($name, $domain) . $extension; if (!file_exists($path)) throw new Exception("Impossibile trovare la chiave {$path}"); return file_get_contents($path); } } }