• PHP Video Tutorials

PHP - OpenSSL Functions



What is OpenSSL?

OpenSSL is a free open source module that is meant to take care of communications that takes place over computer networks. OpenSSL is a tool for Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols.

What is an SSL Certificate?

A Secure Sockets Layer i.e SSL is used by website.The SSL certificate takes care of protecting the data between two computers by using ecryption. The two computers involved can be the data sharing between client and server. When you share data like password, credit card details, home address, social security number it has to be protected and the same is taken care by the SSL certificate.The SSL certificates makes sure the identity of both computers involved is authenticated for safe connection.

OpenSSL installation in PHP

OpenSSL module is by default added to PHP. You can activate the same by removing the (;) ;extension=php_openssl.dll added at the start of the extension in php.ini. After that restart apache and to confirm if the changes are reflecting save below code as .php and execute the .php in browser.

<?php
   phpinfo();
?>
You should see the openssl enabled in the browser as shown below: openssl

OpenSSL configuration

The openssl.cnf is the configuration file and has all the default configuration required for openssl to work.To execute openssl the first thing is that php will try to locate the config file.To get the same you will have to add the php folder to environment variable.

If you are Windows user following are the steps to setup environment variable for php folder:

1. Right click My Computer and go to Properties.

2. Go to Advanced system Settings.

3. Click on the 'Environment Variables' button.

4. Edit the path variable and click on Edit button.

5. Now add the Php folder path at the end.I am using xampp so my php folder is C:\xampp\php;

6.Once done click on Ok button.

7.Now open your command prompt and enter the command: openssl version -a.

C:\Windows\system32>openssl version -a
OpenSSL 1.0.2l  25 May 2017
built on: reproducible build, date unspecified
platform: mingw64
options:  bn(64,64) rc4(16x,int) des(idx,cisc,2,long) idea(int) blowfish(idx)
compiler: x86_64-w64-mingw32-gcc -I. -I.. -I../include  -D_WINDLL -DOPENSSL_PIC
-DOPENSSL_THREADS -D_MT -DDSO_WIN32 -static-libgcc -DL_ENDIAN -O3 -Wall -DWIN32_
LEAN_AND_MEAN -DUNICODE -D_UNICODE -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DO
PENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DRC4_ASM -DSHA1_ASM -DSHA256_ASM -DSH
A512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM
 -DECP_NISTZ256_ASM
OPENSSLDIR: "/etc/ssl"

Now php will be able to locate the openssl.cnf configuration file.

OpenSSL functions

Following table lists down all the functions related to PHP OpenSSL. Here column version indicates the earliest version of PHP that supports the function.
Sr.No Function & Description Version
1 openssl_pkey_new()

Returns a resource identifier that has new private and public key pair

5.0.0
2 openssl_pkey_get_private()

Returns the private key

5.0.0
3 openssl_pkey_get_public()

Returns the public key

5.0.0
4 openssl_​pkey_​export_​to_​file()

Exports the key to a file

5.0.0
5 openssl_private_encrypt()

Encrypts the data with the private key

5.0.0
6 openssl_public_encrypt()

Encrypts the data with public key

5.0.0
7 openssl_public_decrypt()

Decrypts the data with the public key

5.0.0
8 openssl_private_decrypt()

Decrypts the data with the private key

5.0.0
php_function_reference.htm
Advertisements