PHP Classes

PHP OpenSSL Proxy: Manage certificates and encrypt data using OpenSSL

Recommend this page to a friend!
  Info   View files Documentation   View files View files (18)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 53 This week: 1All time: 10,590 This week: 560Up
Version License PHP version Categories
php-openssl-proxy 1.0.0MIT/X Consortium ...7Tools, Cryptography, PHP 7
Description 

Author

This package can manage certificates and encrypt data using OpenSSL.

It can perform several operations with encrypted data using the PHP OpenSSL extension.

Currently, it can:

- Create self-signed certifications

- Export a certificate as a string or as an object

- Generate public and private keys from the details of a given institution

- Get a public key or a private key

- Sign data with a given key

- Verify if a data string encrypted with a given private key can be decrypted with the associated the public key

- Parse a certificate to extract its details as an object

- Check if a private key is from a given certificate

Picture of Adão Pedro
Name: Adão Pedro <contact>
Classes: 1 package by
Country: Angola Angola
Age: ???
All time rank: 44832 in Angola Angola
Week rank: 411 Up1 in Angola Angola Up

Documentation

php-openssl-proxy

About

A PHP wrapper around the OpenSSL extension that provides a user-friendly interface for dealing with OpenSSL.

What's up with the "proxy" name?

It is simply an analogy of the role of a proxy server - which acts as an intermediary.

Features

Create X.509, CSRs and CRLs certificates, Create RSA, HD and DSA keys, Generate and verify signatures, Encoding and decoding, Parsing x509 certificate.

Requirements

This library needs PHP 8 or greater, ext-openssl.

Installation

composer require adaopedro/php-openssl-proxy @dev

Example Usage

Creating a Self-Signed Certificate

use AdaoPedro\OpenSSLProxy\SSCertificate;

$ssCertificate = (new SSCertificate(
    days: 365, //expiration
))->setDistinguishNames(
        countryName: "AO",
        stateOrProvinceName: "Angola",
        localityName: "Luanda",
        organizationName: "A Pedro Developers (SU), Lda",
        organizationalUnitName: "AP",
        commonName: "apedrodevelopers",
        emailAddress: "contato@apdev.ao"
);

try {
    $ssCertificate->save();
} catch(\Exception $ex) {
    echo $ex->getMessage() . PHP_EOL;
}

Creating a CA-Signed Certificate

use AdaoPedro\OpenSSLProxy\CASCertificate;

$certificate = (new CASCertificate(
    days: 365, //expiration
    rootCertificate: $rootCertificate, //an instance of a Self-Signed Certificate, for example
))->setDistinguishNames(
        //...
);

try {
    $certificate->save();
} catch(\Exception $ex) {
    echo $ex->getMessage() . PHP_EOL;
}

Exporting a certificate as a string

//$certificate => an instance of SS or CAS Certificate
echo $certificate->getx509();

Exporting a certificate as an PHP OpenSSLCertificate object

//$certificate => an instance of SS or CAS Certificate
var_dump(
    $certificate->get()
);

Exporting public and private keys from a certificate

 //$certificate => an instance of SS or CAS Certificate
var_dump(
    $certificate->getPublicKey(),
);

//$certificate => an instance of SS or CAS Certificate
var_dump(
    $certificate->getPrivateKey(),
);

var_dump(
    $certificate->getPrivateKeyDecrypted() //in case we're working with encrypt_key
);

Generating public and private keys

$pKey = \AdaoPedro\OpenSSLProxy\generateNewPKey();

list($privKey, $pubKey) = \AdaoPedro\OpenSSLProxy\exportKeysFrom($pKey);

echo $pubKey . PHP_EOL;
echo $privKey . PHP_EOL;

Signing

$data = "Hello world!!";

$signature = \AdaoPedro\OpenSSLProxy\getSignatureFrom(
    $data,
    file_get_contents(".../private_key.pem"),
);

Signature verification

$data = "Hello world!!";

echo
\AdaoPedro\OpenSSLProxy\verifySignatureOf(
    $data,
    file_get_contents(".../hash.dat"),
    file_get_contents(".../public_key.pem"),
) === true
? "Verified"
: "Error. Data modified";

Parsing a PHP OpenSSLCertificate certificate object

//$certificate => an instance of SS or CAS Certificate
var_dump(
    $certificate()
);

Checks if a private key corresponds to a certificate

echo
\AdaoPedro\OpenSSLProxy\checkIfPrivateKey(
    file_get_contents(".../private_key.pem")
)->correspondsTo(
    file_get_contents(".../cert.pem")
) === true
? "Yes. It does"
: "No. It does not";

Customizing OpenSSL configurations (in case when we're using certificate generator classes)

use AdaoPedro\OpenSSLProxy\SSCertificate;

//you can find the initial config file in root of lib directory
/*
To customize, just pass the config filename as second parameter to SSCertificate constructor
or third parameter in case of CASCertificate
*/

$certificate = (new SSCertificate(
    days: 365, //expiration
    configFilename: __DIR__ . "/openssl_configs.php"
))->setDistinguishNames(
        //...
);

  Files folder image Files  
File Role Description
Files folder imagesrc (9 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file helpers_include.php Aux. Auxiliary script
Accessible without login Plain text file openssl_config.php Aux. Auxiliary script
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imagehelpers (4 files)
  Plain text file CASCertificate.php Class Class source
  Plain text file Certificate.php Class Class source
  Plain text file CertificateInterface.php Class Class source
  Plain text file CertificationAutho...ficateInterface.php Class Class source
  Plain text file OpenSSL.php Class Class source
  Plain text file ParsableCertificate.php Class Class source
  Plain text file SaveCertificateInterface.php Class Class source
  Plain text file SelfSignedCertificateInterface.php Class Class source
  Plain text file SSCertificate.php Class Class source

  Files folder image Files  /  src  /  helpers  
File Role Description
  Accessible without login Plain text file fromBase64.php Aux. Auxiliary script
  Accessible without login Plain text file getDateFromTimestamp.php Aux. Auxiliary script
  Accessible without login Plain text file printToFile.php Aux. Auxiliary script
  Accessible without login Plain text file toBase64.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:53
This week:1
All time:10,590
This week:560Up