Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SSL Support #96

Merged
merged 16 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions libasynql/src/poggit/libasynql/mysqli/MysqlCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class MysqlCredentials implements JsonSerializable{
private $port;
/** @var string $socket */
private $socket;
/** @var MysqlSslCredentials|null */
private $sslCredentials;


/**
* Creates a new {@link MysqlCredentials} instance from an array (e.g. from Config), with the following defaults:
Expand All @@ -62,8 +65,15 @@ public static function fromArray(array $array, ?string $defaultSchema = null) :
if(!isset($defaultSchema, $array["schema"])){
throw new ConfigException("The attribute \"schema\" is missing in the MySQL settings");
}
return new MysqlCredentials($array["host"] ?? "127.0.0.1", $array["username"] ?? "root",
$array["password"] ?? "", $array["schema"] ?? $defaultSchema, $array["port"] ?? 3306, $array["socket"] ?? "");
return new MysqlCredentials(
$array["host"] ?? "127.0.0.1",
$array["username"] ?? "root",
$array["password"] ?? "",
$array["schema"] ?? $defaultSchema,
$array["port"] ?? 3306,
$array["socket"] ?? "",
isset($array["ssl"]) ? MysqlSslCredentials::fromArray($array["ssl"]) : null,
);
}

/**
Expand All @@ -75,14 +85,16 @@ public static function fromArray(array $array, ?string $defaultSchema = null) :
* @param string $schema
* @param int $port
* @param string $socket
* @param MysqlSslCredentials|null $sslCredentials
*/
public function __construct(string $host, string $username, string $password, string $schema, int $port = 3306, string $socket = ""){
public function __construct(string $host, string $username, string $password, string $schema, int $port = 3306, string $socket = "", ?MysqlSslCredentials $sslCredentials = null){
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->schema = $schema;
$this->port = $port;
$this->socket = $socket;
$this->sslCredentials = $sslCredentials;
}

/**
Expand All @@ -93,7 +105,14 @@ public function __construct(string $host, string $username, string $password, st
* @throws SqlError
*/
public function newMysqli() : mysqli{
$mysqli = @new mysqli($this->host, $this->username, $this->password, $this->schema, $this->port, $this->socket);
$mysqli = mysqli_init();
if($mysqli === false){
throw new SqlError(SqlError::STAGE_CONNECT, "Failed to initialize mysqli");
}
if($this->sslCredentials !== null){
$this->sslCredentials->applyToInstance($mysqli);
}
@mysqli_real_connect($mysqli, $this->host, $this->username, $this->password, $this->schema, $this->port, $this->socket);
if($mysqli->connect_error){
throw new SqlError(SqlError::STAGE_CONNECT, $mysqli->connect_error);
}
Expand All @@ -108,7 +127,7 @@ public function newMysqli() : mysqli{
* @throws SqlError
*/
public function reconnectMysqli(mysqli $mysqli) : void{
@$mysqli->connect($this->host, $this->username, $this->password, $this->schema, $this->port, $this->socket);
@mysqli_real_connect($mysqli, $this->host, $this->username, $this->password, $this->schema, $this->port, $this->socket);
if($mysqli->connect_error){
throw new SqlError(SqlError::STAGE_CONNECT, $mysqli->connect_error);
}
Expand All @@ -135,7 +154,8 @@ public function __debugInfo(){
"password" => str_repeat("*", strlen($this->password)),
"schema" => $this->schema,
"port" => $this->port,
"socket" => $this->socket
"socket" => $this->socket,
"sslCredentials" => $this->sslCredentials,
];
}

Expand All @@ -146,7 +166,8 @@ public function jsonSerialize() : array{
"password" => $this->password,
"schema" => $this->schema,
"port" => $this->port,
"socket" => $this->socket
"socket" => $this->socket,
"sslCredentials" => $this->sslCredentials,
];
}
}
97 changes: 97 additions & 0 deletions libasynql/src/poggit/libasynql/mysqli/MysqlSslCredentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* libasynql
*
* Copyright (C) 2018 SOFe
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace poggit\libasynql\mysqli;

use JsonSerializable;
use mysqli;

class MysqlSslCredentials implements JsonSerializable{
/** @var string|null $key */
private $key;
/** @var string $certificate */
private $certificate;
/** @var string|null $caCertificate */
private $caCertificate;
/** @var string|null $caPath */
private $caPath;
/** @var string|null $cipherAlgorithms */
private $cipherAlgorithms;


/**
* Creates a new {@link MysqlSslCredentials} instance from an array (e.g. from Config), with empty default values.
* @param array $array
* @return MysqlSslCredentials
*/
public static function fromArray(array $array) : MysqlSslCredentials{
return new MysqlSslCredentials(
$array["key"] ?? null,
$array["certificate"] ?? null,
$array["ca-certificate"] ?? null,
$array["ca-path"] ?? null,
$array["cipher-algorithms"] ?? null,
);
}

/**
* Constructs a new {@link MysqlSslCredentials} by passing parameters directly.
*
* @param string|null $key - The path name to the key file
* @param string|null $certificate - The path name to the certificate file
* @param string|null $caCertificate - The path name to the certificate authority file
* @param string|null $caPath - The path name to a directory that contains trusted SSL CA certificates in PEM format
* @param string|null $cipherAlgorithms - A list of allowable ciphers used for SSL encryption
*/
public function __construct(?string $key = null, ?string $certificate = null, ?string $caCertificate = null, ?string $caPath = null, ?string $cipherAlgorithms = null){
$this->key = $key;
$this->certificate = $certificate;
$this->caCertificate = $caCertificate;
$this->caPath = $caPath;
$this->cipherAlgorithms = $cipherAlgorithms;
}

/**
* Sets the SSL credentials for the given {@link mysqli} instance.
*
* @param mysqli $mysqli
*/
public function applyToInstance(mysqli $mysqli) : void{
$mysqli->ssl_set(
$this->key,
$this->certificate,
$this->caCertificate,
$this->caPath,
$this->cipherAlgorithms
);
}

public function jsonSerialize() : array{
return [
"key" => $this->key,
"certificate" => $this->certificate,
"caCertificate" => $this->caCertificate,
"caPath" => $this->caPath,
"cipherAlgorithms" => $this->cipherAlgorithms,
];
}
}