evilfreelancer / easyrsa-php
An easy way to use the official EasyRSA collection of shell scripts in your application
Fund package maintenance!
Requires
- php: ^7.2|^8.0
- ext-curl: *
- ext-json: *
- splitbrain/php-archive: ^1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- larapack/dd: ^1.1
- limedeck/phpunit-detailed-printer: ^5.0
- orchestra/testbench: ^4.0|^5.0
- phpunit/phpunit: ^8.0
- vlucas/phpdotenv: ^4.0
Suggests
- vlucas/phpdotenv: ^4.0
Provides
None
Conflicts
None
Replaces
None
README
EasyRSA wrapper for PHP
An easy way to use the official EasyRSA collection of shell scripts in your application.
composer require evilfreelancer/easyrsa-php
By the way, EasyRSA library support Laravel and Lumen frameworks, details here.
How to use
More examples you can find here.
Download the latest release of EasyRSA
Before you start use this script need to download the easy-rsa package.
require_once __DIR__ . '/../vendor/autoload.php'; use EasyRSA\Downloader; $dnl = new Downloader([ 'archive' => './easy-rsa.tar.gz', 'scripts' => './easy-rsa', ]); $dnl->getEasyRSA();
Result of this script will be in easy-rsa folder.
Generate certificates
require_once __DIR__ . '/../vendor/autoload.php'; use EasyRSA\Commands; $cmd = new Commands([ 'scripts' => './easy-rsa', 'certs' => './easy-rsa-certs', ]); // Load EASYRSA_* variables from a vars file into the subprocess environment if (file_exists(__DIR__ . '/../vars.example')) { $cmd->loadVars(__DIR__ . '/../vars.example'); } $cmd->initPKI(); $cmd->buildCA(true); $cmd->genDH(); $cmd->buildServerFull('server', true); $cmd->buildClientFull('client1', true); $cmd->buildClientFull('client2', true);
Result of this script will be in easy-rsa-certs folder.
Note:
Dotenv::createImmutable(...)->load()alone is not enough. Dotenv puts loaded values only into$_ENV/$_SERVERand does not export them to child processes, so the spawnedeasyrsascript does not see them. The worker exportsEASYRSA_*variables before each command (values found in$_ENVare synced too), butloadVars()orsetEnv()is the reliable way.
Password-protected CA (--passin)
Since EasyRSA 3.0.6 the easyrsa script supports the global --passin option,
which passes an OpenSSL password source instead of asking for the CA key
password interactively. Methods buildClientFull(), buildServerFull(),
signReqClient() and signReqServer() accept it as an optional last argument:
require_once __DIR__ . '/../vendor/autoload.php'; use EasyRSA\Commands; $cmd = new Commands([ 'scripts' => './easy-rsa', 'certs' => './easy-rsa-certs', ]); // Password source is an OpenSSL password argument, prefer indirections: // "env:CA_PASS" - password from the CA_PASS environment variable // "file:/run/secrets/ca.pass" - password from a file // Avoid the raw "pass:secret" form: it would be visible in the process list. putenv('CA_PASS=super-secret'); $cmd->buildClientFull('client1', false, 'env:CA_PASS'); $cmd->buildServerFull('server', false, 'file:/run/secrets/ca.pass'); $cmd->signReqClient('some.req', 'env:CA_PASS');
Generated command looks like this (global options are placed before the command, as required by the easyrsa script):
./easy-rsa/easyrsa3/easyrsa --batch --passin='env:CA_PASS' build-client-full 'client1'
For a password-protected CA built with buildCA() you need to provide both
password sources on the environment before the command runs, otherwise
EasyRSA asks for the passphrase interactively and fails in batch mode:
putenv('EASYRSA_PASSIN=file:/run/secrets/ca.pass'); putenv('EASYRSA_PASSOUT=file:/run/secrets/ca.pass'); $cmd->buildCA();
List of all available commands
| Method | Description |
|---|---|
| getContent(string $filename) | Show content of any certificate available in "certs" folder |
| initPKI() | Instantiate Public Key Infrastructure (PKI) |
| buildCA(bool $nopass = false) | Build Certificate Authority (CA) |
| genDH() | Generate Diffie-Hellman certificate (DH) |
| genReq() | Generate request for certificate |
| signReqClient(string $filename, ?string $passin = null) | Sign request for client certificate |
| signReqServer(string $filename, ?string $passin = null) | Sign request for server certificate |
| buildClientFull(string $name, bool $nopass = false, ?string $passin = null) | Build public and private key of client |
| buildServerFull(string $name, bool $nopass = false, ?string $passin = null) | Build public and private key of server |
| revoke(string $filename) | Revoke certificate |
| genCRL() | Generate Certificate Revocation List (CRL) |
| updateDB() | Update certificates database |
| showCert(string $filename) | Display information about certificate |
| showReq(string $filename) | Display information about request |
| importReq(string $filename) | Import request |
| exportP7(string $filename) | Export file in format of Public-Key Cryptography Standards (PKCS) v7 (P7) |
| exportP12(string $filename) | Export file in format of Public-Key Cryptography Standards (PKCS) v12 (P12) |
| setRSAPass(string $filename) | Set password in Rivest–Shamir–Adleman (RSA) format |
| setECPass(string $filename) | Set password in Elliptic Curve (EC) format |
You also can read content of generated certificate via getConfig($filename) method:
<?php require_once __DIR__ . '/../vendor/autoload.php'; use \EasyRSA\Commands; $cmd = new Commands([ 'scripts' => './easy-rsa', 'certs' => './easy-rsa-certs', ]); $file = $cmd->getContent('ca.crt'); echo "$file\n"; $file = $cmd->getContent('server.crt'); echo "$file\n"; $file = $cmd->getContent('server.key'); echo "$file\n";
Environment variables
You can set these variables via environment on host system or with help of vlucas/phpdotenv library or via any other way which you like.
EASYRSA_DN="cn_only" #EASYRSA_DN="org" EASYRSA_REQ_COUNTRY="DE" EASYRSA_REQ_PROVINCE="California" EASYRSA_REQ_CITY="San Francisco" EASYRSA_REQ_ORG="Copyleft Certificate Co" EASYRSA_REQ_EMAIL="me@example.net" EASYRSA_REQ_OU="My Organizational Unit" EASYRSA_REQ_CN="ChangeMe" EASYRSA_KEY_SIZE=2048 EASYRSA_ALGO=rsa EASYRSA_CA_EXPIRE=3650 EASYRSA_CERT_EXPIRE=3650 EASYRSA_DIGEST="sha256"
Example of environment variables configuration which should be used on certificate build stage can be fond here.
Frameworks support
Laravel
The package's service provider will automatically register its service provider.
Publish the easy-rsa.php configuration file:
php artisan vendor:publish --provider="EasyRSA\Laravel\ServiceProvider"
Alternative configuration method via .env file
After you publish the configuration file as suggested above, you may configure library
by adding the following to your application's .env file (with appropriate values):
EASYRSA_WORKER=default EASYRSA_ARCHIVE=./easy-rsa.tar.gz EASYRSA_SCRIPTS=./easy-rsa EASYRSA_CERTS=./easy-rsa-certs
Lumen
If you work with Lumen, please register the service provider and configuration in bootstrap/app.php:
$app->register(EasyRSA\Laravel\ServiceProvider::class); $app->configure('easy-rsa');
Manually copy the configuration file to your application.
Testing
This library can tested in multiple different ways
composer test:lint composer test:types composer test:unit
or just in one command
composer test