Jump to content

openssl x509 certificate problems


sookyboo

Recommended Posts

I have a .cer file (generated from a jsp server I think).

 

I need to extract it's public key and use the public key to encrypt a value...

 

I get these errors:

Warning: openssl_x509_read() [function.openssl-x509-read]: supplied parameter cannot be coerced into an X509 certificate! in /public_html/test/cert.php on line 26

 

Warning: openssl_x509_free() expects parameter 1 to be resource, string given in /public_html/test/cert.php on line 34

 

    $fp = fopen("/home/x.cer", "r");

    $cert = fread($fp, 8192);

 

    fclose($fp);

openssl_x509_read( $cert);

$data = openssl_x509_parse($cert);

 

print_r($data);

 

openssl_x509_free( $cert );

 

 

Any ideas what is wrong...

Starting to get frustrated...

Thanks in advance

 

 

Link to comment
Share on other sites

  • 1 year later...

Hi , it's my fisrt post in here, I hope this will help.

 

2 Do:

 

1. Generate a *.PEM certificate (Privacy Enhanced Mail) via Openssl command:

 

openssl pkcs12 -in  YOUR_pkcs12_certificate.pfx -out YOUR_NEW_CERTIFICATE.pem

 

2.The source code of PHP must be compiled with openssl option:

 

./configure --with-openssl=/usr

 

 

3.

<?php

 

$cert = file_get_contents("OUR_NEW_CERTIFICATE.pem");

$ssl = openssl_x509_parse($cert);

print_r($ssl);

?>

 

4. Cheers

 

 

 

 

Link to comment
Share on other sites

  • 4 months later...

I had the same issue and it turned out to be UTF-8 BOM.

In case you don't knoew what it means - it's 2 hidden characters at the beginning of a file that indicates it is encoded with UTF-8.

Notepad and other applications put it there by default.

To remove it:

If the file has only english characters (like certificates should have) - Open the file with notepad and click file>save as.

In the 'encoding' field select 'ANSI', and save the file.

 

Another option is to use HEX editor (I use xvi32 - free and good), and delete the first 2 characters.

 

I hope this helps someone...

 

Cheers,

Dan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.