2

I am trying to create CSR using PHP and OpenSSL extension,
but when I do that it gives me an error which says:

Failed to generate private key: error:2006D080:BIO routines:BIO_new_file:no such file

also php.ini is edited correctly, I added extension=openssl.

This is my code:

<?php

$dn = array(
    "countryName" => "US",
    "stateOrProvinceName" => "California",
    "localityName" => "San Francisco",
    "organizationName" => "My Company",
    "organizationalUnitName" => "IT",
    "commonName" => "mycompany.com",
    "emailAddress" => "[email protected]"
);

// Generate a new private (and public) key pair
$privkey = openssl_pkey_new(array(
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
));

if ($privkey === false) {
    die('Failed to generate private key: ' . openssl_error_string());
}

// Generate a certificate signing request
$csr = openssl_csr_new($dn, $privkey);

if ($csr === false) {
    die('Failed to generate CSR: ' . openssl_error_string());
}

// Export the CSR as a string
$csrout = '';
if (!openssl_csr_export($csr, $csrout)) {
    die('Failed to export CSR: ' . openssl_error_string());
}

echo $csrout;

// Optionally, save the private key
$pkeyout = '';
if (!openssl_pkey_export($privkey, $pkeyout, 'your-passphrase')) {
    die('Failed to export private key: ' . openssl_error_string());
}
file_put_contents('private_key.pem', $pkeyout);

?>

I use Laragon as web server.

How can i solve this problem?

3
  • This seems to be an environment-specific problem. The posted PHP code can be successfully executed e.g. on paiza.io/projects/stQIvPF8_IwLQ-jzB_fV8A. The generated certificate and private key can be viewed in an ASN.1 parser, e.g. lapo.it/asn1js. Check your environment, e.g. php.net/manual/en/openssl.requirements.php. Commented Jun 13, 2024 at 6:35
  • @topaco this is a good news , but now i want to know what is the problem in my env , the second this i want to deploy this project on hostinger server , how can i make sure that it will work ? Commented Jun 13, 2024 at 6:37
  • @topaco i indeed looked at this source php.net/manual/en/openssl.requirements.php , but i could not install it Commented Jun 13, 2024 at 6:39

1 Answer 1

1

the problem is that I have to install OpenSSL on my system,

my system is Windows system then to install it I should go to this website :

OpenSSL Binaries

then after that choose the second choice in the table:

enter image description here

this is the link of it :

Second choice

then after that choose the completed version of OpenSSL :

enter image description here

after that, it will download the installer version of OpenSSL, just install it, and it will work.

thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.