Add ability to set custom key length

This commit is contained in:
Rob Desilets
2022-02-10 15:05:18 -06:00
parent c25ba7fab9
commit bd83867dcf
2 changed files with 29 additions and 24 deletions

View File

@@ -138,6 +138,11 @@ class Client
$this->init(); $this->init();
} }
public function getKeyLength(): int
{
return $config['key_length'] ?? 4096;
}
/** /**
* Get an existing order by ID * Get an existing order by ID
* *
@@ -316,7 +321,7 @@ class Client
*/ */
public function getCertificate(Order $order): Certificate public function getCertificate(Order $order): Certificate
{ {
$privateKey = Helper::getNewKey(); $privateKey = Helper::getNewKey($this->getKeyLength());
$csr = Helper::getCsr($order->getDomains(), $privateKey); $csr = Helper::getCsr($order->getDomains(), $privateKey);
$der = Helper::toDer($csr); $der = Helper::toDer($csr);
@@ -493,7 +498,7 @@ class Client
{ {
//Make sure a private key is in place //Make sure a private key is in place
if ($this->getFilesystem()->has($this->getPath('account.pem')) === false) { if ($this->getFilesystem()->has($this->getPath('account.pem')) === false) {
$this->getFilesystem()->write($this->getPath('account.pem'), Helper::getNewKey()); $this->getFilesystem()->write($this->getPath('account.pem'), Helper::getNewKey($this->getKeyLength()));
} }
$privateKey = $this->getFilesystem()->read($this->getPath('account.pem')); $privateKey = $this->getFilesystem()->read($this->getPath('account.pem'));
$privateKey = openssl_pkey_get_private($privateKey); $privateKey = openssl_pkey_get_private($privateKey);

View File

@@ -51,11 +51,11 @@ class Helper
* *
* @return string * @return string
*/ */
public static function getNewKey(): string public static function getNewKey(int $keyLength): string
{ {
$key = openssl_pkey_new([ $key = openssl_pkey_new([
'private_key_bits' => 2048, 'private_key_bits' => $keyLength,
'private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_type' => OPENSSL_KEYTYPE_RSA,
]); ]);
openssl_pkey_export($key, $pem); openssl_pkey_export($key, $pem);