PHP mysqli和SSL

kh212irz  于 2023-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(113)

我正在尝试通过WAN配置加莱拉群集(VPN不是一个选项)。由于显而易见的原因,我正在设置连接以要求SSL密钥,并且很难让应用程序连接。我可以使用PDO成功连接,但无法让mysqli工作。设置将是:
美国地点:

  • mainWebServer:openSUSE Leap 15.2、Apache 2.4.33(命名为虚拟主机myserver.mycompany.com)
  • myServer1:Ubuntu 20.04,MySQL加莱拉Cluster 8.0.19
  • myDevBox:openSUSE Tumbleweed,运行PHP CLI调试连接问题

中国地区:

  • myServer2:Ubuntu 20.04,MySQL加莱拉Cluster 8.0.19,Apache 2.4 current
  • myServer 3:myServer 2的克隆

加莱拉集群名为myServer,节点名为myServerX,对应于它们的实际主机名。
PHP版本信息:

me@mydevbox:~> php --version
PHP 7.4.9 (cli) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies

MySQL服务器版本信息:

me@myServer1:~$ mysql --version
mysql  Ver 8.0.19-26.3 for Linux on x86_64 (MySQL Wsrep Server - GPL)

代码:

<?php

$conn = mysqli_init() or print("init failed\n");

mysqli_ssl_set(
    $conn,
    '/srv/www/vhosts/myProject/mysql_ssl_certs/client-key.pem',
    '/srv/www/vhosts/myProject/mysql_ssl_certs/client-cert.pem',
    '/srv/www/vhosts/myProject/mysql_ssl_certs/ca-cert.pem',
    NULL,
    NULL
);

mysqli_real_connect(
    $conn,
    'myServer1',
    'myUser',
    'myPassword',
    'myDB',
    3306,
    MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT
) or print("connect failed to myServer1 [{$conn->connect_errno}: {$conn->connect_error}]\n");

print_r($conn);

输出:

me@mydevbox:~> php sqlssltest.php
PHP Warning:  mysqli_real_connect(): Peer certificate CN=`myServer' did not match expected CN=`myServer1' in /home/me/sqlssltest.php on line 21
PHP Warning:  mysqli_real_connect(): Cannot connect to MySQL by using SSL in /home/me/sqlssltest.php on line 21
PHP Warning:  mysqli_real_connect(): [2002]  (trying to connect via (null)) in /home/me/sqlssltest.php on line 21
PHP Warning:  mysqli_real_connect(): (HY000/2002):  in /home/me/sqlssltest.php on line 21
connect failed to myServer1 [2002: ]
mysqli Object
(
    [client_info] => mysqlnd 7.4.9
    [client_version] => 70409
    [connect_errno] => 2002
    [connect_error] => 
    [errno] => 2002
    [error] => 
)

看起来mysqli忽略了我的MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT标志。我能够使用PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false成功连接PDO-有没有办法让mysqli工作?不幸的是,这是为了支持仍然使用mysqli的遗留应用程序代码,并且在需要启用时没有办法重构。我相信创建单独的CA证书会中断复制连接,因为它们都具有不同的主机名。

izkcnapc

izkcnapc1#

我们传递MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT作为第7个参数,但这是套接字参数,flags是第8个参数。尝试将null传递到第7位,并将MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT推到第8位。

332nm8kg

332nm8kg2#

麻烦的是为什么我需要改变我的新mysqli到另一个新的类型。下面的解决方案-代码用正确的方式ssl扩展了mysqli的overwrite __结构。

class ssl_mysqli extends \mysqli {

public function __construct($db_host, $db_user, $db_pass, $db_name, $port, $persistent = true, $ssl = false, $certpublic = "") {

    if($ssl) {

        parent::init();
        parent::options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, false);
        parent::ssl_set(NULL, NULL, $certpublic, NULL, NULL);
        parent::real_connect(($persistent ? 'p:' : '') . $db_host, $db_user, $db_pass, $db_name, $port, '', MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT );
        
        //https://bugs.php.net/bug.php?id=68344
    } else {
        parent::__construct($db_host, $db_user, $db_pass, $db_name);
    }

}

以上代码是在我和自己的证书下设置的。所以可信证书关闭了。挺酷的

相关问题