phpday 2027 - Call for Papers

Opzioni di contesto SSL

Opzioni di contesto SSL — Elenco delle opzioni di contesto SSL

Descrizione

Opzioni di contesto per i trasporti ssl:// e tls://.

Opzioni

peer_name string

Nome del peer da utilizzare. Se questo valore non è impostato, il nome viene dedotto in base al nome host utilizzato durante l'apertura dello stream.

verify_peer bool

Richiede la verifica del certificato SSL utilizzato.

Il valore predefinito è true.

verify_peer_name bool

Richiede la verifica del nome del peer.

Il valore predefinito è true.

allow_self_signed bool

Permette i certificati autofirmati. Richiede verify_peer.

Il valore predefinito è false

cafile string

Posizione del file dell'Autorità di Certificazione nel filesystem locale che dovrebbe essere utilizzato con l'opzione di contesto verify_peer per autenticare l'identità del peer remoto.

capath string

Se cafile non è specificato o se il certificato non viene trovato lì, la directory indicata da capath viene esaminata alla ricerca di un certificato adatto. capath deve essere una directory di certificati con hash corretto.

local_cert string

Percorso del file del certificato locale nel filesystem. Deve essere un file codificato in PEM che contiene il certificato e la chiave privata. Può opzionalmente contenere la catena di certificati degli emittenti. La chiave privata può anche essere contenuta in un file separato specificato da local_pk.

local_pk string

Percorso del file della chiave privata locale nel filesystem nel caso di file separati per il certificato (local_cert) e la chiave privata.

passphrase string

Passphrase con la quale il file local_cert è stato codificato.

verify_depth int

Interrompe se la catena di certificati è troppo profonda.

Per impostazione predefinita non viene effettuata alcuna verifica.

ciphers string

Imposta l'elenco dei cifrari disponibili. Il formato della stringa è descritto in » ciphers(1).

Il valore predefinito è DEFAULT.

capture_peer_cert bool

Se impostato a true verrà creata un'opzione di contesto peer_certificate contenente il certificato del peer.

capture_peer_cert_chain bool

Se impostato a true verrà creata un'opzione di contesto peer_certificate_chain contenente la catena di certificati.

SNI_enabled bool

Se impostato a true l'indicazione del nome del server verrà abilitata. L'abilitazione di SNI permette l'utilizzo di certificati multipli sullo stesso indirizzo IP.

disable_compression bool

Se impostato, disabilita la compressione TLS. Questo può aiutare a mitigare il vettore di attacco CRIME.

peer_fingerprint string | array

Interrompe quando il digest del certificato remoto non corrisponde all'hash specificato.

Quando viene utilizzata una string, la lunghezza determinerà quale algoritmo di hashing viene applicato, "md5" (32) o "sha1" (40).

Quando viene utilizzato un array, le chiavi indicano il nome dell'algoritmo di hashing e ogni valore corrispondente è il digest atteso.

security_level int

Imposta il livello di sicurezza. Se non specificato, viene utilizzato il livello di sicurezza predefinito della libreria. I livelli di sicurezza sono descritti in » SSL_CTX_get_security_level(3).

Disponibile a partire da PHP 7.2.0 e OpenSSL 1.1.0.

Log delle modifiche

Versione Descrizione
7.2.0 Aggiunto security_level. Richiede OpenSSL >= 1.1.0.

Note

Nota:

Poiché ssl:// è il trasporto sottostante per i wrapper https:// e ftps://, tutte le opzioni di contesto applicabili a ssl:// si applicano anche a https:// e ftps://.

Nota:

Affinché SNI (Server Name Indication) sia disponibile, PHP deve essere compilato con OpenSSL 0.9.8j o superiore. Utilizzare la costante OPENSSL_TLSEXT_SERVER_NAME per determinare se SNI è supportato.

+add a note

User Contributed Notes 9 notes

up
8
tianyiw at vip dot qq dot com
3 years ago
Enable SNI (Server Name Indication):
PEM must be contains certificate and private key.
<?php
$context = stream_context_create([
    'ssl' => [
        'SNI_enabled' => true,
        'SNI_server_certs' => [
            'host1.com' => '/path/host1.com.pem',
            'host2.com' => '/path/host2.com.pem',
        ],
    ]
]);
?>
up
9
website at meezaan dot net
10 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
5
gabri dot ns at gmail dot com
6 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
5
Charlie
9 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
4
Botjan kufca
16 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
2
mechtecs at gmail dot com
8 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
1
Charlie
9 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).
up
-1
consatangmail dot com
4 years ago
recommended use "ssl://" transport.

in php 5.5 ~ 7.1
ssl:// transport = ssl_v2|ssl_v3|tls_v1.0|tls_v1.1|tls_v1.2
tls:// transport = tls_v1.0

after 7.2 ssl:// and tls:// transports is same
php 7.2 ~ 7.3 = tls_v1.0|tls_v1.1|tls_v1.2
php 7.4 ~ 8.1 = tls_v1.0|tls_v1.1|tls_v1.2|tls_v1.3
up
-1
borbas dot geri at gmail dot com
12 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`.
To Top