This commit is contained in:
2024-08-05 22:57:28 +08:00
commit 0efda6c02a
1779 changed files with 171774 additions and 0 deletions
@@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Acme PHP project.
*
* (c) Titouan Galopin <galopintitouan@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AcmePhp\Ssl\Generator\DhKey;
use AcmePhp\Ssl\Generator\KeyOption;
use AcmePhp\Ssl\Generator\OpensslPrivateKeyGeneratorTrait;
use AcmePhp\Ssl\Generator\PrivateKeyGeneratorInterface;
use AcmePhp\Ssl\PrivateKey;
use Webmozart\Assert\Assert;
/**
* Generate random DH private key using OpenSSL.
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class DhKeyGenerator implements PrivateKeyGeneratorInterface
{
use OpensslPrivateKeyGeneratorTrait;
public function generatePrivateKey(KeyOption $keyOption): PrivateKey
{
Assert::isInstanceOf($keyOption, DhKeyOption::class);
return $this->generatePrivateKeyFromOpensslOptions([
'private_key_type' => OPENSSL_KEYTYPE_DH,
'dh' => [
'p' => $keyOption->getPrime(),
'g' => $keyOption->getGenerator(),
],
]);
}
public function supportsKeyOption(KeyOption $keyOption): bool
{
return $keyOption instanceof DhKeyOption;
}
}
@@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Acme PHP project.
*
* (c) Titouan Galopin <galopintitouan@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AcmePhp\Ssl\Generator\DhKey;
use AcmePhp\Ssl\Generator\KeyOption;
class DhKeyOption implements KeyOption
{
/** @var string */
private $generator;
/** @var string */
private $prime;
/**
* @param string $prime Hexadecimal representation of the prime
* @param string $generator Hexadecimal representation of the generator: ie. 02
*
* @see https://tools.ietf.org/html/rfc3526 how to choose a prime and generator numbers
*/
public function __construct(string $prime, string $generator = '02')
{
$this->generator = pack('H*', $generator);
$this->prime = pack('H*', $prime);
}
public function getGenerator(): string
{
return $this->generator;
}
public function getPrime(): string
{
return $this->prime;
}
}