Skip to content

Commit

Permalink
Fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
dixyes committed Aug 1, 2024
1 parent 2c79cd1 commit 55cf8ba
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 345 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
__DIR__ . '/ext/tests/swow_closure/use.inc.inc',
__DIR__ . '/ext/tests/swow_closure/multiple_ns.inc',
__DIR__ . '/ext/tests/swow_closure/namespaced2.inc',
__DIR__ . '/ext/tests/include/lib/CertificateGenerator.php',
] as $excludeFilePath
) {
$GLOBALS['exclude_file_path_list'][] = substr($excludeFilePath, strlen(__DIR__) + strlen('/'));
Expand Down
348 changes: 175 additions & 173 deletions ext/tests/include/lib/CertificateGenerator.php
Original file line number Diff line number Diff line change
@@ -1,173 +1,175 @@
<?php
// from php-src ext/openssl/tests/CertificateGenerator.inc
class CertificateGenerator
{
const CONFIG = __DIR__. DIRECTORY_SEPARATOR . 'openssl.cnf';

/** @var OpenSSLCertificate */
private $ca;

/** @var resource */
private $caKey;

/** @var resource|null */
private $lastCert;

/** @var resource|null */
private $lastKey;

public function __construct()
{
if (!extension_loaded('openssl')) {
throw new RuntimeException(
'openssl extension must be loaded to generate certificates'
);
}
$this->generateCa();
}

/**
* @param int|null $keyLength
* @return resource
*/
private static function generateKey($keyLength = null)
{
if (null === $keyLength) {
$keyLength = 2048;
}

return openssl_pkey_new([
'private_key_bits' => $keyLength,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
'encrypt_key' => false,
]);
}

private function generateCa()
{
$this->caKey = self::generateKey();
$dn = [
'countryName' => 'GB',
'stateOrProvinceName' => 'Berkshire',
'localityName' => 'Newbury',
'organizationName' => 'Example Certificate Authority',
'commonName' => 'CA for PHP Tests'
];

$this->ca = openssl_csr_sign(
openssl_csr_new(
$dn,
$this->caKey,
[
'x509_extensions' => 'v3_ca',
'config' => self::CONFIG,
]
),
null,
$this->caKey,
2,
[
'config' => self::CONFIG,
]
);
}

public function getCaCert()
{
$output = '';
openssl_x509_export($this->ca, $output);

return $output;
}

public function saveCaCert($file)
{
openssl_x509_export_to_file($this->ca, $file);
}

private function generateCertAndKey(
$commonNameForCert, $file, $keyLength = null, $subjectAltName = null
) {
$dn = [
'countryName' => 'BY',
'stateOrProvinceName' => 'Minsk',
'localityName' => 'Minsk',
'organizationName' => 'Example Org',
];
if ($commonNameForCert !== null) {
$dn['commonName'] = $commonNameForCert;
}

$subjectAltNameConfig =
$subjectAltName ? "subjectAltName = $subjectAltName" : "";
$configCode = <<<CONFIG
[ req ]
distinguished_name = req_distinguished_name
default_md = sha256
default_bits = 1024
[ req_distinguished_name ]
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
$subjectAltNameConfig
[ usr_cert ]
basicConstraints = CA:FALSE
$subjectAltNameConfig
CONFIG;
$configFile = $file . '.cnf';
file_put_contents($configFile, $configCode);

$config = [
'config' => $configFile,
'req_extensions' => 'v3_req',
'x509_extensions' => 'usr_cert',
];

$this->lastKey = self::generateKey($keyLength);
$csr = openssl_csr_new($dn, $this->lastKey, $config);
$this->lastCert = openssl_csr_sign(
$csr,
$this->ca,
$this->caKey,
/* days */ 2,
$config,
);

return $config;
}

public function saveNewCertAsFileWithKey(
$commonNameForCert, $file, $keyLength = null, $subjectAltName = null
) {
$config = $this->generateCertAndKey($commonNameForCert, $file, $keyLength, $subjectAltName);

$certText = '';
openssl_x509_export($this->lastCert, $certText);

$keyText = '';
openssl_pkey_export($this->lastKey, $keyText, null, $config);

file_put_contents($file, $certText . PHP_EOL . $keyText);

unlink($config['config']);
}

public function saveNewCertAndKey(
$commonNameForCert, $certFile, $keyFile, $keyLength = null, $subjectAltName = null
) {
$config = $this->generateCertAndKey($commonNameForCert, $certFile, $keyLength, $subjectAltName);

openssl_x509_export_to_file($this->lastCert, $certFile);
openssl_pkey_export_to_file($this->lastKey, $keyFile, null, $config);

unlink($config['config']);
}

public function getCertDigest($algo)
{
return openssl_x509_fingerprint($this->lastCert, $algo);
}
}
<?php

// from php-src ext/openssl/tests/CertificateGenerator.inc

class CertificateGenerator
{
const CONFIG = __DIR__. DIRECTORY_SEPARATOR . 'openssl.cnf';

/** @var OpenSSLCertificate */
private $ca;

/** @var resource */
private $caKey;

/** @var resource|null */
private $lastCert;

/** @var resource|null */
private $lastKey;

public function __construct()
{
if (!extension_loaded('openssl')) {
throw new RuntimeException(
'openssl extension must be loaded to generate certificates'
);
}
$this->generateCa();
}

/**
* @param int|null $keyLength
* @return resource
*/
private static function generateKey($keyLength = null)
{
if (null === $keyLength) {
$keyLength = 2048;
}

return openssl_pkey_new([
'private_key_bits' => $keyLength,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
'encrypt_key' => false,
]);
}

private function generateCa()
{
$this->caKey = self::generateKey();
$dn = [
'countryName' => 'GB',
'stateOrProvinceName' => 'Berkshire',
'localityName' => 'Newbury',
'organizationName' => 'Example Certificate Authority',
'commonName' => 'CA for PHP Tests'
];

$this->ca = openssl_csr_sign(
openssl_csr_new(
$dn,
$this->caKey,
[
'x509_extensions' => 'v3_ca',
'config' => self::CONFIG,
]
),
null,
$this->caKey,
2,
[
'config' => self::CONFIG,
]
);
}

public function getCaCert()
{
$output = '';
openssl_x509_export($this->ca, $output);

return $output;
}

public function saveCaCert($file)
{
openssl_x509_export_to_file($this->ca, $file);
}

private function generateCertAndKey(
$commonNameForCert, $file, $keyLength = null, $subjectAltName = null
) {
$dn = [
'countryName' => 'BY',
'stateOrProvinceName' => 'Minsk',
'localityName' => 'Minsk',
'organizationName' => 'Example Org',
];
if ($commonNameForCert !== null) {
$dn['commonName'] = $commonNameForCert;
}

$subjectAltNameConfig =
$subjectAltName ? "subjectAltName = $subjectAltName" : "";
$configCode = <<<CONFIG
[ req ]
distinguished_name = req_distinguished_name
default_md = sha256
default_bits = 1024
[ req_distinguished_name ]
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
$subjectAltNameConfig
[ usr_cert ]
basicConstraints = CA:FALSE
$subjectAltNameConfig
CONFIG;
$configFile = $file . '.cnf';
file_put_contents($configFile, $configCode);

$config = [
'config' => $configFile,
'req_extensions' => 'v3_req',
'x509_extensions' => 'usr_cert',
];

$this->lastKey = self::generateKey($keyLength);
$csr = openssl_csr_new($dn, $this->lastKey, $config);
$this->lastCert = openssl_csr_sign(
$csr,
$this->ca,
$this->caKey,
/* days */ 2,
$config,
);

return $config;
}

public function saveNewCertAsFileWithKey(
$commonNameForCert, $file, $keyLength = null, $subjectAltName = null
) {
$config = $this->generateCertAndKey($commonNameForCert, $file, $keyLength, $subjectAltName);

$certText = '';
openssl_x509_export($this->lastCert, $certText);

$keyText = '';
openssl_pkey_export($this->lastKey, $keyText, null, $config);

file_put_contents($file, $certText . PHP_EOL . $keyText);

unlink($config['config']);
}

public function saveNewCertAndKey(
$commonNameForCert, $certFile, $keyFile, $keyLength = null, $subjectAltName = null
) {
$config = $this->generateCertAndKey($commonNameForCert, $certFile, $keyLength, $subjectAltName);

openssl_x509_export_to_file($this->lastCert, $certFile);
openssl_pkey_export_to_file($this->lastKey, $keyFile, null, $config);

unlink($config['config']);
}

public function getCertDigest($algo)
{
return openssl_x509_fingerprint($this->lastCert, $algo);
}
}
Loading

0 comments on commit 55cf8ba

Please sign in to comment.