CakeFest 2024: The Official CakePHP Conference

SSL コンテキストオプション

SSL コンテキストオプションSSL コンテキストオプションの一覧

説明

ssl:// および tls:// トランスポート用のコンテキストオプションです。

オプション

peer_name string

使用するピア名。省略した場合は、ストリームをオープンしたときに使ったホスト名をもとに、名前を推測します。

verify_peer bool

SSL サーバー証明書の検証を要求するかどうか。

デフォルトは true です。

verify_peer_name bool

ピア名の検証を要求するかどうか。

デフォルトは true です。

allow_self_signed bool

自己証明の証明書を許可するかどうか。 verify_peer が必要です。

デフォルトは false です。

cafile string

ローカルファイルシステム上の証明書ファイルの場所。 verify_peer オプションでリモートサーバーとの 認証の際に使用する。

capath string

cafile が指定されていなかったりその場所にファイルが 見つからなかったりした場合、capath が指す ディレクトリを検索して認証ファイルを探します。capath は認証ファイルのディレクトリを正確に指している必要があります。

local_cert string

ファイルシステム上のローカル証明書ファイルのパス。 あなたの証明書とプライベートキーを含み、PEM エンコードされた ファイルである必要があります。オプションで、発行者の 認証チェーンを含めることも可能です。 プライベートキーは、 local_pk で指定した別のファイルに含めることも可能です。

local_pk string

ローカルファイルシステム上のプライベートキーファイルの場所。 証明書 (local_cert) とプライベートキーを別のファイルに分けたい場合に用います。

passphrase string

local_cert ファイルをエンコードした際の パスフレーズ。

verify_depth int

証明書のチェインが深すぎる場合に終了するかどうか。

デフォルトでは検証を行いません。

ciphers string

使用可能な暗号化方式の一覧を設定します。設定できるフォーマットは » ciphers(1) の説明を参照ください。

デフォルトは DEFAULT です。

capture_peer_cert bool

true に設定すると、peer_certificate コンテキストオプションがピア証明書を含んで作成されます。

capture_peer_cert_chain bool

true に設定すると、peer_certificate_chain コンテキストオプションが証明書チェインを含んで作成されます。

SNI_enabled bool

true に設定すると、サーバー名の表示 (SNI) が有効になります。 これを有効にすると、同じ IP アドレスで複数の証明書を使えるようになります。

disable_compression bool

設定すると、TLS 圧縮を無効にします。これは、CRIME アタックベクターを軽減するのに役立ちます。

peer_fingerprint string | array

リモート証明書のダイジェストが指定したハッシュに一致しない場合に、異常終了させます。

string を指定する場合は、その長さでハッシュアルゴリズムを判断します。 "md5" (32) あるいは "sha1" (40) のいずれかです。

array を指定する場合は、キーがハッシュアルゴリズムで、 それに対応する値がダイジェストとなります。

security_level int

セキュリティレベルを設定します。 指定されない場合、ライブラリのデフォルトのセキュリティレベルが使われます。 セキュリティレベルについては、 » SSL_CTX_get_security_level(3) に説明があります。

PHP 7.2.0 および OpenSSL 1.1.0 以降が必要です。

変更履歴

バージョン 説明
7.2.0 security_level が追加されました。 OpenSSL >= 1.1.0 が必要です。

注意

注意: ssl://https:// および ftps:// のラッパーの 基盤となるものなので、ssl:// に適用可能なオプションは https:// および ftps:// にも 適用可能です。

注意: SNI (Server Name Indication) を使うには、PHP のコンパイル時に OpenSSL 0.9.8j 以降を使わなければなりません。SNI をサポートしているかどうかは OPENSSL_TLSEXT_SERVER_NAME で判定します。

add a note

User Contributed Notes 9 notes

up
6
tianyiw at vip dot qq dot com
1 year 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
7
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
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
3
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
1
consatangmail dot com
1 year 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
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
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
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
-2
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