PHP: OpenSSL Functions - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

openssl_csr_export_to_file openal_stream

Last updated: Fri, 26 Jan 2007

view this page in

# CIX. OpenSSL Functions

# Введение

This module uses the functions of OpenSSL for generation and verification of signatures and for sealing (encrypting) and opening (decrypting) data. OpenSSL offers many features that this module currently doesn't support. Some of these may be added in the future.

# Требования

In order to use the OpenSSL functions you need to install the OpenSSL package. PHP between versions 4.0.5 and 4.3.1 will work with OpenSSL >= 0.9.5. Other versions (PHP <=4.0.4pl1 and >= 4.3.2) require OpenSSL >= 0.9.6.

Внимание
You are strongly encouraged to use the most recent OpenSSL version, otherwise your web server could be vulnerable to attack.

# Установка

To use PHP's OpenSSL support you must also compile PHP `--with-openssl[=DIR]`.

> Note to Win32 Users: In order to enable this module on a Windows environment, you must copy `libeay32.dll` from the DLL folder of the PHP/Win32 binary package to the SYSTEM32 folder of your windows machine. (Ex: `C:\WINNT\SYSTEM32` or `C:\WINDOWS\SYSTEM32`)

> Additionally, if you are planning to use the key generation and certificate signing functions, you will need to install a valid `openssl.cnf` on your system. As of PHP 4.3.0, we include a sample configuration file in the `openssl` folder of our win32 binary distribution. If you are using PHP 4.2.0 or later and are missing the file, you can obtain it from the OpenSSL home page or by downloading the PHP 4.3.0 release and using the configuration file from there.

> Note to Win32 Users: PHP will search for the `openssl.cnf` using the following logic:

> - the `OPENSSL_CONF` environmental variable, if set, will be used as the path (including filename) of the configuration file.

> - the `SSLEAY_CONF` environmental variable, if set, will be used as the path (including filename) of the configuration file.

> - The file `openssl.cnf` will be assumed to be found in the default certificate area, as configured at the time that the openssl DLL was compiled. This is usually means that the default filename is `c:\usr\local\ssl\openssl.cnf`.

> In your installation, you need to decide whether to install the configuration file at `c:\usr\local\ssl\openssl.cnf` or whether to install it someplace else and use environmental variables (possibly on a per-virtual-host basis) to locate the configuration file. Note that it is possible to override the default path from the script using the `configargs` of the functions that require a configuration file.

# Настройка во время выполнения

Данное расширение не определяет никакие директивы конфигурации в `php.ini`.

# Типы ресурсов

# Key/Certificate parameters

Quite a few of the openssl functions require a key or a certificate parameter. PHP 4.0.5 and earlier have to use a key or certificate resource returned by one of the openssl_get_xxx functions. Later versions may use one of the following methods:

- Certificates

  1. An X.509 resource returned from openssl_x509_read()

  2. A string having the format `file://path/to/cert.pem`; the named file must contain a PEM encoded certificate

  3. A string containing the content of a certificate, PEM encoded

- Public/Private Keys

  1. A key resource returned from openssl_get_publickey() or openssl_get_privatekey()

  2. For public keys only: an X.509 resource

  3. A string having the format `file://path/to/file.pem` - the named file must contain a PEM encoded certificate/private key (it may contain both)

  4. A string containing the content of a certificate/key, PEM encoded

  5. For private keys, you may also use the syntax array($key, $passphrase) where $key represents a key specified using the file:// or textual content notation above, and $passphrase represents a string containing the passphrase for that private key

# Certificate Verification

When calling a function that will verify a signature/certificate, the cainfo parameter is an array containing file and directory names that specify the locations of trusted CA files. If a directory is specified, then it must be a correctly formed hashed directory as the openssl command would use.

# Предопределенные константы

Перечисленные ниже константы определены данным расширением и могут быть доступны только в том случае, если PHP был собран с поддержкой этого расширения или же в том случае, если данное расширение подгружается во время выполнения.

## Purpose checking flags

`X509_PURPOSE_SSL_CLIENT` (integer)

`X509_PURPOSE_SSL_SERVER` (integer)

`X509_PURPOSE_NS_SSL_SERVER` (integer)

`X509_PURPOSE_SMIME_SIGN` (integer)

`X509_PURPOSE_SMIME_ENCRYPT` (integer)

`X509_PURPOSE_CRL_SIGN` (integer)

`X509_PURPOSE_ANY` (integer)

## Padding flags

`OPENSSL_PKCS1_PADDING` (integer)

`OPENSSL_SSLV23_PADDING` (integer)

`OPENSSL_NO_PADDING` (integer)

`OPENSSL_PKCS1_OAEP_PADDING` (integer)

## Key types

`OPENSSL_KEYTYPE_RSA` (integer)

`OPENSSL_KEYTYPE_DSA` (integer)

`OPENSSL_KEYTYPE_DH` (integer)

## PKCS7 Flags/Constants

The S/MIME functions make use of flags which are specified using a bitfield which can include one or more of the following values:

Таблица 1. PKCS7 CONSTANTS

Constant | Description
--- | ---
PKCS7_TEXT | Adds text/plain content type headers to encrypted/signed message. If decrypting or verifying, it strips those headers from the output - if the decrypted or verified message is not of MIME type text/plain then an error will occur.
PKCS7_BINARY | Normally the input message is converted to "canonical" format which is effectively using CR and LF as end of line: as required by the S/MIME specification. When this options is present, no translation occurs. This is useful when handling binary data which may not be in MIME format.
PKCS7_NOINTERN | When verifying a message, certificates (if any) included in the message are normally searched for the signing certificate. With this option only the certificates specified in the `extracerts` parameter of openssl_pkcs7_verify() are used. The supplied certificates can still be used as untrusted CAs however.
PKCS7_NOVERIFY | Do not verify the signers certificate of a signed message.
PKCS7_NOCHAIN | Do not chain verification of signers certificates: that is don't use the certificates in the signed message as untrusted CAs.
PKCS7_NOCERTS | When signing a message the signer's certificate is normally included - with this option it is excluded. This will reduce the size of the signed message but the verifier must have a copy of the signers certificate available locally (passed using the `extracerts` to openssl_pkcs7_verify() for example).
PKCS7_NOATTR | Normally when a message is signed, a set of attributes are included which include the signing time and the supported symmetric algorithms. With this option they are not included.
PKCS7_DETACHED | When signing a message, use cleartext signing with the MIME type multipart/signed. This is the default if you do not specify any `flags` to openssl_pkcs7_sign(). If you turn this option off, the message will be signed using opaque signing, which is more resistant to translation by mail relays but cannot be read by mail agents that do not support S/MIME.
PKCS7_NOSIGS | Don't try and verify the signatures on a message

> Замечание: These constants were added in 4.0.6.

## Signature Algorithms

`OPENSSL_ALGO_SHA1` (integer)

  Used as default algorithm by openssl_sign() and openssl_verify().

`OPENSSL_ALGO_MD5` (integer)

`OPENSSL_ALGO_MD4` (integer)

`OPENSSL_ALGO_MD2` (integer)

> Замечание: These constants were added in 5.0.0.

## Ciphers

`OPENSSL_CIPHER_RC2_40` (integer)

`OPENSSL_CIPHER_RC2_128` (integer)

`OPENSSL_CIPHER_RC2_64` (integer)

`OPENSSL_CIPHER_DES` (integer)

`OPENSSL_CIPHER_3DES` (integer)

> Замечание: These constants were added in 4.3.0.

## Version constants

`OPENSSL_VERSION_TEXT` (string)

`OPENSSL_VERSION_NUMBER` (integer)

> Замечание: These constants were added in 5.2.0.

add a note User Contributed Notes
OpenSSL Functions
Richard Ablewhite
04-Dec-2006 02:23
`Windows users be warned that you need the following file in system32:
msvcr71.dll
It has to go in system32, is not picked up from php/dlls`
yabba dabba
26-Jul-2006 11:23
`The php4 distribution for Windows/IIS has a README-SSL.txt which strongly implies that just the path needs to be added to the OPENSLL_CONF variable in the server's environment variables. Be sure to add the file name and extension too.
E.g.: c:\php-4.3.11\openssl\openssl.cnf`
peter dot mescalchin @ geemail dot com
16-May-2006 01:34
`For w32 users to enable OpenSSL support. As well as copying "libeay32.dll" to the windows system32 folder you also need to copy "ssleay32.dll". The documentation above should probably be updated to note this.
This requirement was documented at the libcurl pages:
http://curl.haxx.se/libcurl/php/install.html#windows`
php ~at~ wwwcrm dot komm
16-Nov-2005 08:47
`If you want to use PHP for public / private key encryption jobs without needing to know the ins and outs of the Open SSL extension, the following may be of interest:
http://www.karenandalex.com/php_stuff/_class_OpenSSL.phps
This class was unavailable for a long while (server problems) but is now back up. Apologies to those who clicked through and got a 404
I hope it is useful to you...
Alex`
beckman at purplecow dot com
08-Nov-2005 12:07
`FreeBSD Ports tree php5-openssl uses openssl-0.9.8a. This is a problem, as if you install these two ports and attempt to open an HTTPS URL within PHP, it will fail with this error from openssl_error_string(): error:140A90A1:SSL routines:func(169):reason(161) which is SSL_R_LIBRARY_HAS_NO_CIPHERS or "library has no ciphers"
This is because the openssl library now requires you to load your ciphers manually -- all ciphers are not automatically loaded for you.
I don't believe the php5-openssl module has been updated to do this before opening an SSL connection (as of 5.0.5).
Using openssl-0.9.7i seems to work; symlinking libcrypto.so.3 to libcrypto.so.4 prevents the php5-openssl port from trying to install openssl-0.9.8a. So install openssl-stable (0.9.7i) from ports first, symlink 2nd, then install php5-openssl 3rd, and you should be OK.`
matt at NOSPAMopenflowsPLEASE dot org
08-Nov-2005 10:42
`The openssl functions were disabled in Debian release 3.0 (woody), but as of release 3.1 (sarge) they're available again.`
greensweater
31-Aug-2005 04:21
`Sorry, the code in my previous note doesn't work... the last line should read:
$csr = openssl_csr_new(array('commonName'=>'MyCSR'),$pkey,$config);`
greensweater
30-Aug-2005 09:54
`"You need to have a valid openssl.cnf installed for this function to operate correctly" includes most openssl functions. You can force php to find your openssl.cnf file as follows:
$config = array('config'=>'/path/to/openssl.cnf');
$pkey = openssl_pkey_new($config);
$csr = openssl_csr_new('MyCSR',$pkey,$config);`
skippy zuavra net
20-Oct-2004 05:38
`In case you're wondering what's a "correctly hashed" directory for the use with cainfo: it's simply a directory which contains CA public certificates in PEM/X.509 format. You can get such certificates either from the CA's website (they advertise it in visible places) or from your browser. In Explorer for instance you can click on the little yellow padlock, go to the CA entry and export it.
The only trick with the directory is that file names must be in the form "hash.#". The "hash" part is the 8-digit hex hash of the certificate, while the # part is a number which serves to differentiate certificates which give the same hash (yes, it can happen with certificates coming from the same CA). Usually # is 0, but you also can use 1, 2 and so on when having more certs with the same hash.
In order to obtain the hash of a certificate you can use the openssl command line utility like this:
openssl x509 -hash -in certfile.cer | head -1`
jaz at ensn dot net
16-Sep-2004 11:18
`For newbies (as me):
If you want to try at home on win32, you can learn how to install apache+ssl on this url: http://tud.at/programm/apache-ssl-win32-howto.php3
Versions on English, Spanish and French.
Just I have read and install and run perfectly.`
norman at rasmussen dot org
02-Feb-2004 10:43
`Debian maintainers have disabled the openssl support because it seems to help break apache on startup. (http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=193343 and http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=165699)
- Norman`
add a note
openssl_csr_export_to_file openal_stream

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 11:33:19 2007 PST