You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.7 KiB
PHTML

5 years ago
<?php
namespace Cijber;
4 years ago
use Cijber\OpenSSL\FFIWrapper;
5 years ago
use Cijber\OpenSSL\Instance;
4 years ago
use Cijber\OpenSSL\X509Store;
5 years ago
use FFI;
/**
* Class OpenSSL
* @package Cijber
*/
class OpenSSL
{
private static ?Instance $instance = null;
private static ?FFI $stdLib = null;
/**
* Get an OpenSSL instance which holds the FFI object,
* And initializes OpenSSL and frees when destructed
*
* @return Instance
*/
public static function getInstance(): Instance
5 years ago
{
if (static::$instance === null) {
static::$instance = new Instance();
static::$instance->init();
}
return static::$instance;
}
public static function getFFI(): FFI
{
return static::getInstance()->getFFI();
}
public static function getStdLib(): FFI
{
if (static::$stdLib === null) {
static::$stdLib = FFI::cdef("void* malloc (size_t size);", "libc.so.6");
}
return static::$stdLib;
}
public static function malloc(int $size): FFI\CData
{
return static::getStdLib()->malloc($size);
}
4 years ago
public static function consumeErrors(): array
{
$ffi = static::getFFI();
$errs = [];
while (0 !== ($code = $ffi->ERR_get_error())) {
4 years ago
$ptr = $ffi->ERR_error_string($code, null);
$errs[] = FFI::string($ptr);
4 years ago
}
return $errs;
}
public static function addressOf(FFI\CData $data): int
{
return FFI::cast("long long", $data)->cdata;
}
4 years ago
4 years ago
public static $caStore = null;
4 years ago
public static function CAStore()
{
4 years ago
if (static::$caStore === null) {
static::$caStore = X509Store::default();
}
return static::$caStore;
4 years ago
}
}