Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP SSL Context Options

บทนำ

รายการ Context options สำหรับ transports ssl:// และ tls://

peer_namePeer name ที่จะใช้ หากไม่ตั้งค่า จะเดาจาก hostname ที่ใช้เปิด stream
verify_peerต้องการการตรวจสอบ SSL certificate ที่ใช้ ค่าเริ่มต้น TRUE
verify_peer_nameต้องการการตรวจสอบ peer name ค่าเริ่มต้น TRUE
allow_self_signedอนุญาต self-signed certificates ต้องการ verify_peer ค่าเริ่มต้น FALSE
cafileพาธของไฟล์ Certificate Authority บน local filesystem เพื่อยืนยันตัวตน remote peer
capathต้องเป็น certificate directory ที่ hash ถูกต้องแล้ว
local_certพาธไปยังไฟล์ local certificate บน filesystem
local_pkพาธไปยังไฟล์ local private key บน filesystem กรณีแยกไฟล์ certificate และ private key
passphrasePassphrase ที่ใช้เข้ารหัสไฟล์ local_cert
CN_matchCommon Name ที่คาดหวัง หาก Common Name ไม่ตรง การเชื่อมต่อจะล้มเหลว
verify_depthยกเลิกหาก certificate chain ลึกเกินไป
ciphersตั้งค่ารายการ ciphers ที่ใช้ได้ รูปแบบ string อธิบายใน ciphers(1)
capture_peer_certหาก TRUE จะสร้าง context option peer_certificate ที่มี peer certificate
capture_peer_cert_chainหาก TRUE จะสร้าง peer_certificate_chain context option ที่มี certificate chain
SNI_enabledหาก TRUE server name indication จะเปิดใช้งาน
SNI_server_nameหากตั้งค่า จะใช้เป็น server name สำหรับ SNI มิฉะนั้นจะเดาจาก hostname
disable_compressionหากตั้งค่า จะปิด TLS compression
peer_fingerprintยกเลิกการเชื่อมต่อเมื่อ remote certificate digest ไม่ตรงกับ hash ที่ระบุ
security_levelตั้งค่า security level หากไม่ระบุ ใช้ default security level พร้อมใช้งานตั้งแต่ PHP 7.2.0 และ OpenSSL 1.1.0

ตัวอย่าง

ตัวอย่างนี้แสดง SSL context settings

$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 ]]);