Options de contexte SSL

Options de contexte SSLListe des options de contexte SSL

Description

Options de contexte pour les protocoles ssl:// et tls://.

Options

peer_name string

Nom de paires à utiliser. Si cette valeur n'est pas définie, alors le nom sera deviné en se basant sur le nom d'hôte utilisé lors de l'ouverture du flux.

verify_peer booléen

Nécessite une vérification du certificat SSL utilisé.

Par défaut, vaut true.

verify_peer_name bool

Nécessite la vérification du nom de paires.

Par défaut, vaut true.

allow_self_signed booléen

Permet les certificats autosignés. Requière le paramètre verify_peer.

Par défaut, vaut false

cafile string

Endroit où se trouve le fichier de l'autorité du certificat sur le système de fichiers local et qui devra être utilisé avec l'option de contexte verify_peer pour identifier le pair distant.

capath string

Si cafile n'est pas spécifié ou si le certificat n'a pas été trouvé, une recherche dans le dossier pointé par capath sera effectuée afin d'y trouver un certificat valide. capath doit être un dossier de certificat valide.

local_cert string

Chemin vers le certificat local, sur le système de fichiers. Ce doit être un fichier encodé PEM qui contient votre certificat et votre clé privée. Il peut, optionnellement, contenir la chaîne de certification de l'émetteur. La clé privée peut également être contenue dans un fichier distinct spécifié par local_pk.

local_pk string

Chemin d'accès au fichier de clé privée locale sur le système de fichiers dans le cas de fichiers distincts pour le certificat (local_cert) et la clé privée.

passphrase string

La phrase passe avec laquelle votre fichier local_cert a été encodé.

verify_depth int

Échoue si la chaîne de certification est trop profonde.

Par défaut, aucune vérification.

ciphers string

Définit la liste des chiffrements. Le format de la chaîne est décrite sur la page » ciphers(1).

Par défaut, vaut DEFAULT.

capture_peer_cert bool

Si définit à true, un option de contexte peer_certificate sera créée, contenant le certificat de l'émetteur.

capture_peer_cert_chain bool

Si définit à true, une option de contexte peer_certificate_chain sera créée, contenant la chaîne de certification.

SNI_enabled bool

Si vaut true, l'indication sur le nom du serveur sera activée. Le fait d'activer SNI permet d'utiliser plusieurs certificats sur la même adresse IP.

disable_compression bool

Si défini, la compression TLS sera désactivée. Cela peut aider à mitiger le vecteur d'attaque CRIME.

peer_fingerprint string | array

Stop lorsque le certificat digest distant ne correspond pas au hash spécifié.

Lorsqu'une chaîne de caractères est utilisée, la longueur va déterminer l'algorithme de hachage utilisé, soit "md5" (32), soit "sha1" (40).

Lorsqu'un tableau est utilisé, la clé indique le nom de l'algorithme de hachage, et chaque valeur correspondante représente le digest attendu.

security_level int

Définie le niveau de sécurité. Si non spécifié le niveau de sécurité par défaut est utilisé. Les niveaux de sécurités sont décrit dans » SSL_CTX_get_security_level(3).

Disponible à partir de PHP 7.2.0 et OpenSSL 1.1.0.

Historique

Version Description
7.2.0 Ajout de security_level. Requit OpenSSL >= 1.1.0.

Notes

Note: Vu que ssl:// est un protocole sous-jacent pour les gestionnaires https:// et ftps://, toutes les options de contexte appliquées à ssl:// seront également appliquées à https:// et ftps://.

Note: Afin que SNI (Server Name Indication) soit disponible, PHP doit être compilé avec OpenSSL 0.9.8j ou supérieur. Utilisez la constante OPENSSL_TLSEXT_SERVER_NAME pour déterminer si SNI est supporté ou non.

add a note add a note

User Contributed Notes 7 notes

up
4
website at meezaan dot net
7 years ago
There is also a crypto_type context. In older versions this was crypto_method. This is referenced on http://php.net/manual/en/function.stream-socket-enable-crypto.php
up
4
Botjan kufca
14 years ago
CN_match works contrary to intuitive thinking. I came across this when I was developing SSL server implemented in PHP. I stated (in code):

- do not allow self signed certs (works)
- verify peer certs against CA cert (works)
- verify the client's CN against CN_match (does not work), like this:

stream_context_set_option($context, 'ssl', 'CN_match', '*.example.org');

I presumed this would match any client with CN below .example.org domain.
Unfortunately this is NOT the case. The option above does not do that.

What it really does is this:
- it takes client's CN and compares it to CN_match
- IF CLIENT's CN CONTAINS AN ASTERISK like *.example.org, then it is matched against CN_match in wildcard matching fashion

Examples to illustrate behaviour:
(CNM = server's CN_match)
(CCN = client's CN)

- CNM=host.example.org, CCN=host.example.org ---> OK
- CNM=host.example.org, CCN=*.example.org ---> OK
- CNM=.example.org, CCN=*.example.org ---> OK
- CNM=example.org, CCN=*.example.org ---> ERROR

- CNM=*.example.org, CCN=host.example.org ---> ERROR
- CNM=*.example.org, CCN=*.example.org ---> OK

According to PHP sources I believe that the same applies if you are trying to act as Client and the server contains a wildcard certificate. If you set CN_match to myserver.example.org and server presents itself with *.example.org, the connection is allowed.

Everything above applies to PHP version 5.2.12.
I will supply a patch to support CN_match starting with asterisk.
up
3
Charlie
7 years ago
I am unable to load a PEM that was generated with the stunnel tools. However, I am able to use PHP calls to generate a working PEM that is recognized both by stunnel and php, as outlined here:

http://www.devdungeon.com/content/how-use-ssl-sockets-php

This code fragment is now working for me, and with stunnel verify=4, both sides confirm the fingerprint. Oddly, if "tls://" is set below, then TLSv1 is forced, but using "ssl://" allows TLSv1.2:

$stream_context = stream_context_create([ 'ssl' => [
'local_cert'        => '/path/to/key.pem',
'peer_fingerprint'  => openssl_x509_fingerprint(file_get_contents('/path/to/key.crt')),
'verify_peer'       => false,
'verify_peer_name'  => false,
'allow_self_signed' => true,
'verify_depth'      => 0 ]]);

$fp = stream_socket_client('ssl://ssl.server.com:12345',
   $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $stream_context);
fwrite($fp, "foo bar\n");
while($line = fgets($fp, 8192)) echo $line;
up
0
borbas dot geri at gmail dot com
10 years ago
I used this for Apple Push Notification Service.
Passed in a local certificate filename `cert.pem` trough local_cert option.
Worked fine, when invoked the script directly.

But when I included/required the script from a different location, it stopped working, without any explicit error message.

Resolved by passed in the full path for the file `<FullPathTo>cert.pem`.
up
-1
gabri dot ns at gmail dot com
4 years ago
i usually download root CA certificate from https://curl.haxx.se/docs/caextract.html then put it as 'cafile' and it work almost all of the time.

the only problem i'v ever found is when the server does not properly sending intermediete CA certificate, then, you must add it manually to the file.
up
-1
mechtecs at gmail dot com
5 years ago
If you want to validate the server against a local certificate, which you already saved, to further validate the target server, you have to use a fullchain.pem. Then the verify_peer option will work. So just get the server certificate, and search the root CA's pem's and copy everything into a single file. For example:

My certificate has the "GeoTrust TLS RSA CA G1" certificate in the chain, so you google that string. Go to the official digicert Geotrust page and download the "GeoTrustTLSRSACAG1.crt" certificate. Then you can use the following command to convert it into the pem format:
openssl x509 -inform DER -in GeoTrustTLSRSACAG1.crt -out GeoTrustTLSRSACAG1.crt.pem -outform PEM
up
-3
Charlie
7 years ago
It appears that "allow_self_signed" does not and cannot apply to the local_cert option.

The stunnel verify=4 option, which verifies but ignores a CA, has no analog in these settings, which is unfortunate.

Even more perplexingly, while the "openssl verify -CAfile" is successful, PHP appears unable to use the new ca/crt pair in any configuration.

I did actually link my PHP against a copy of LibreSSL 2.3.8, but PHP oddly is unable to use TLS1.1 or 1.2. It does, however, enable EC secp521r1 (of which my native OpenSSL 0.9.8e is incapable).
To Top