沃梦达 / 编程问答 / php问题 / 正文

如何在 Web 服务器中安全地从 PHP 建立 TLS 连接

How to make TLS connection from PHP in web server, and safely(如何在 Web 服务器中安全地从 PHP 建立 TLS 连接)

本文介绍了如何在 Web 服务器中安全地从 PHP 建立 TLS 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些 PHP 代码在 Web 服务器中运行,例如,运行一个简单的 CakePHP 应用程序.通过这个应用程序,我想偶尔与某个服务器建立 TLS 连接以交换一些数据.这通常是如何完成的?(我对 PHP 的经验很少.)

Suppose I have some PHP code running inside a web server, for example, running a simple CakePHP app. From this app, I want to occasionally make a TLS connection out to some server to exchange some data. How is this typically done? (I have little experience with PHP.)

建议使用哪些 PHP 插件或库或其他什么来完成 TLS 连接?从哪里开始寻找一些好地方?(我最初的 Google 搜索给了我很大的噪声信号比.)

What PHP plug-ins or libraries or whatever, are recommended to accomplish TLS connections? Where are some good places to start looking? (My initial Google searches give me a huge ratio of noise to signal.)

将 X509 私钥放在 Web 服务器上会涉及哪些安全问题?要建立 TLS 连接,我将需要 X509 证书和相应的私钥,例如 PEM 文件、DER 文件、Java 密钥库或其他文件.那个私钥会被放在网络根目录下的某个易受攻击的地方吗?这通常是如何处理的?具体来说,保护用于建立 TLS 连接的私钥有哪些安全问题?最佳做法是什么?

What are the security issues involved with having the X509 private key sitting on the web server? To make a TLS connection out, I'm going to need both the X509 certificate and the corresponding private key, as perhaps PEM files, or DER files, or in a Java keystore, or whatever. Is that private key going to be sitting somewhere vulnerable under the web root? How is this typically handled? What are the security issues, specifically, of securing the private key that is to be used to make a TLS connection out? What are the best practices?

我忘了提到 PHP 应用程序连接到的服务器需要提供客户端证书.客户端确实需要私钥来连接.此外,我要连接的服务器不是 HTTP 服务器——我只需要在与服务器的普通套接字连接上读取/写入(通过 SSL/TLS 连接).

I forgot to mention that the server that the PHP app connects to requires a client certificate to be presented. The client does need the private key to connect. Also, the server I am connecting to is not an HTTP server -- I just need to read/write on a plain socket connection to the server (over the SSL/TLS connection).

推荐答案

TLS 是正确的名称,但大多数人仍然称它为 SSL.在 PHP 中,您可以建立此连接 使用 CURL.

TLS is the proper name, however most people still call it SSL. In PHP you can make this connection using CURL.

使用 TLS/SSL 客户端,您只需要公钥来验证远程主机.这个公钥就是那个公钥,它是否被泄露给攻击者并不重要.在服务器端,Apache 可以访问公钥和私钥.这些密钥使用正常的文件访问权限进行保护.在 *nix 系统下,这些密钥通常存储在 /etc/ 的某个位置,归 Apache 进程所有,最好使用 chmod 400.

With a TLS/SSL client you only need the public key to verify a remote host. This public key is just that public, it doesn't matter if it gets leaked to an attacker. On the server side Apache has access both the public and private key. These keys are protected using normal file access privileges. Under *nix systems these keys are commonly stored somewhere in /etc/, owned by the Apache process and chmod 400 is best.

客户端最简单的身份验证方法是简单的用户名/密码.您可以使用 SSL 对客户端和服务器进行身份验证.这将要求您将私钥存储在您的 PHP 应用程序可以访问的某个位置,最好是使用 chmod 400 在 webroot 之外,但您可以轻松地将其重命名为 .php 或将其放在一个文件夹中包含 deny from all 的 .htaccess.在服务器端,您可以使用 这些环境变量 验证客户端证书.

The easiest authentication method for clients would be a simple username/password. You can use SSL to authenticate both the client and server. This would require you to store the private key somewhere where your PHP app has access, ideally outside of the webroot with a chmod 400, but you could easily rename it to .php or put it in a folder with a .htaccess that contains deny from all. On the server side you can verify the clients certificate using these environmental variables.

如果您只想要一个 TLS 连接而不是一个 HTTPs.然后你可以通过设置上下文选项来使用stream_context_set_option:

If you just want a TLS connection and not an HTTPs. Then you can use stream_context_set_option by setting the Context Options:

<?php 
$context = stream_context_create(); 
$result = stream_context_set_option($context, 'ssl', 'local_cert', '/path/to/keys.pem'); 
// This line is needed if your cert has a passphrase
$result = stream_context_set_option($context, 'ssl', 'passphrase', 'pass_to_access_keys'); 

$socket = stream_socket_client('tls://'.$host.':443', $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context); 
?>

这篇关于如何在 Web 服务器中安全地从 PHP 建立 TLS 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 Web 服务器中安全地从 PHP 建立 TLS 连接

基础教程推荐