Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

EVP and public key from pem file

2,109 views
Skip to first unread message

Marco Sommella

unread,
Dec 3, 2008, 3:27:34 PM12/3/08
to
------=_Part_12058_26001910.1228336035290
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi,
i tried to modify demos/sign, which reads private key from pem file and
public key from x509 certificate in pem file, to a version which instead
read public from pem file (not a certificate).

my 2 pem files are generate using RSA_generate_key, PEM_write_RSAPrivateKey,
PEM_write_RSAPublicKey fuctions.

here the code of sign.c changed by me:

#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>

int main ()
{
int err;
int sig_len;
unsigned char sig_buf [4096];
static char certfile[] = "pubkey.pem";
static char keyfile[] = "privkey.pem";
static char data[] = "I owe you...";
EVP_MD_CTX md_ctx;
EVP_PKEY * pkey;
FILE * fp;

/* Just load the crypto library error strings,
* SSL_load_error_strings() loads the crypto AND the SSL ones */
/* SSL_load_error_strings();*/
ERR_load_crypto_strings();

/* Read private key */

fp = fopen (keyfile, "r");
if (fp == NULL) exit (1);
pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
fclose (fp);

if (pkey == NULL) {
ERR_print_errors_fp (stderr);
exit (1);
}

/* Do the signature */

EVP_SignInit (&md_ctx, EVP_sha1());
EVP_SignUpdate (&md_ctx, data, strlen(data));
sig_len = sizeof(sig_buf);
err = EVP_SignFinal (&md_ctx, sig_buf, &sig_len, pkey);

if (err != 1) {
ERR_print_errors_fp(stderr);
exit (1);
}

EVP_PKEY_free (pkey);

/* Read public key */

fp = fopen (certfile, "r");
if (fp == NULL) exit (1);
pkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL);
fclose (fp);

if (pkey == NULL) {
ERR_print_errors_fp (stderr);
exit (1);
}

/* Verify the signature */

EVP_VerifyInit (&md_ctx, EVP_sha1());
EVP_VerifyUpdate (&md_ctx, data, strlen((char*)data));
err = EVP_VerifyFinal (&md_ctx, sig_buf, sig_len, pkey);
EVP_PKEY_free (pkey);

if (err != 1) {
ERR_print_errors_fp (stderr);
exit (1);
}
printf ("Signature Verified Ok.\n");
return(0);
}

it's return the following error:
1883:error:0906D06C:PEM routines:PEM_read_bio:no start
line:/export/builds/onnv_101a/usr/src/common/openssl/crypto/pem/pem_lib.c:644:Expecting:
PUBLIC KEY

Can somebody plz help me ?
TNX

--
------------------------------------------------------------------
Marco Sommella
marco.s...@gmail.com (E-Mail & MSN)
------------------------------------------------------------------

------=_Part_12058_26001910.1228336035290
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi,<br>i tried to modify demos/sign, which reads private key from pem file and public key from x509 certificate in pem file, to a version which instead read public from pem file (not a certificate).<br><br>my 2 pem files are generate using RSA_generate_key, PEM_write_RSAPrivateKey, PEM_write_RSAPublicKey fuctions.<br>
<br>here the code of sign.c changed by me:<br><br>#include &lt;stdio.h&gt;<br>#include &lt;openssl/rsa.h&gt;<br>#include &lt;openssl/evp.h&gt;<br>#include &lt;openssl/objects.h&gt;<br>#include &lt;openssl/x509.h&gt;<br>#include &lt;openssl/err.h&gt;<br>
#include &lt;openssl/pem.h&gt;<br>#include &lt;openssl/ssl.h&gt;<br><br>int main ()<br>{<br>&nbsp; int err;<br>&nbsp; int sig_len;<br>&nbsp; unsigned char sig_buf [4096];<br>&nbsp; static char certfile[] = &quot;pubkey.pem&quot;;<br>&nbsp; static char keyfile[]&nbsp; = &quot;privkey.pem&quot;;<br>
&nbsp; static char data[]&nbsp;&nbsp;&nbsp;&nbsp; = &quot;I owe you...&quot;;<br>&nbsp; EVP_MD_CTX&nbsp;&nbsp;&nbsp;&nbsp; md_ctx;<br>&nbsp; EVP_PKEY *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pkey;<br>&nbsp; FILE *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fp;<br><br>&nbsp; /* Just load the crypto library error strings,<br>&nbsp;&nbsp; * SSL_load_error_strings() loads the crypto AND the SSL ones */<br>
&nbsp; /* SSL_load_error_strings();*/<br>&nbsp; ERR_load_crypto_strings();<br>&nbsp; <br>&nbsp; /* Read private key */<br>&nbsp; <br>&nbsp; fp = fopen (keyfile, &quot;r&quot;);<br>&nbsp; if (fp == NULL) exit (1);<br>&nbsp; pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);<br>
&nbsp; fclose (fp);<br><br>&nbsp; if (pkey == NULL) { <br>&nbsp;&nbsp;&nbsp; ERR_print_errors_fp (stderr);<br>&nbsp;&nbsp;&nbsp; exit (1);<br>&nbsp; }<br>&nbsp; <br>&nbsp; /* Do the signature */<br>&nbsp; <br>&nbsp; EVP_SignInit&nbsp;&nbsp; (&amp;md_ctx, EVP_sha1());<br>&nbsp; EVP_SignUpdate (&amp;md_ctx, data, strlen(data));<br>
&nbsp; sig_len = sizeof(sig_buf);<br>&nbsp; err = EVP_SignFinal (&amp;md_ctx, sig_buf, &amp;sig_len, pkey);<br><br>&nbsp; if (err != 1) {<br>&nbsp;&nbsp;&nbsp; ERR_print_errors_fp(stderr);<br>&nbsp;&nbsp;&nbsp; exit (1);<br>&nbsp; }<br><br>&nbsp; EVP_PKEY_free (pkey);<br>&nbsp; <br>
&nbsp; /* Read public key */<br>&nbsp; <br>&nbsp; fp = fopen (certfile, &quot;r&quot;);<br>&nbsp; if (fp == NULL) exit (1);<br>&nbsp; pkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL);<br>&nbsp; fclose (fp);<br>&nbsp; <br>&nbsp; if (pkey == NULL) { <br>&nbsp;&nbsp;&nbsp; ERR_print_errors_fp (stderr);<br>
&nbsp;&nbsp;&nbsp; exit (1);<br>&nbsp; }<br><br>&nbsp; /* Verify the signature */<br>&nbsp; <br>&nbsp; EVP_VerifyInit&nbsp;&nbsp; (&amp;md_ctx, EVP_sha1());<br>&nbsp; EVP_VerifyUpdate (&amp;md_ctx, data, strlen((char*)data));<br>&nbsp; err = EVP_VerifyFinal (&amp;md_ctx, sig_buf, sig_len, pkey);<br>
&nbsp; EVP_PKEY_free (pkey);<br><br>&nbsp; if (err != 1) {<br>&nbsp;&nbsp;&nbsp; ERR_print_errors_fp (stderr);<br>&nbsp;&nbsp;&nbsp; exit (1);<br>&nbsp; }<br>&nbsp; printf (&quot;Signature Verified Ok.\n&quot;);<br>&nbsp; return(0);<br>}<br><br>it&#39;s return the following error:<br>
1883:error:0906D06C:PEM routines:PEM_read_bio:no start line:/export/builds/onnv_101a/usr/src/common/openssl/crypto/pem/pem_lib.c:644:Expecting: PUBLIC KEY<br><br>Can somebody plz help me ?<br>TNX<br clear="all"><br>-- <br>
------------------------------------------------------------------<br>Marco Sommella<br><a href="mailto:marco.s...@gmail.com">marco.s...@gmail.com</a> (E-Mail &amp; MSN)<br>------------------------------------------------------------------<br>


------=_Part_12058_26001910.1228336035290--
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List openss...@openssl.org
Automated List Manager majo...@openssl.org

Klarth

unread,
Dec 4, 2008, 4:35:10 AM12/4/08
to
At this code fragment:

> fp = fopen (certfile, "r");
> if (fp == NULL) exit (1);
> pkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL);
> fclose (fp);

Is certfile actually a X.509 certificate file or a public key file?
What are the contents of your certfile?
--- Kah

> marco.somme...@gmail.com (E-Mail & MSN)


> ------------------------------------------------------------------
>
> ------=_Part_12058_26001910.1228336035290
> Content-Type: text/html; charset=ISO-8859-1
> Content-Transfer-Encoding: 7bit
> Content-Disposition: inline
>
> Hi,<br>i tried to modify demos/sign, which reads private key from pem file and public key from x509 certificate in pem file, to a version which instead read public from pem file (not a certificate).<br><br>my 2 pem files are generate using RSA_generate_key, PEM_write_RSAPrivateKey, PEM_write_RSAPublicKey fuctions.<br>
> <br>here the code of sign.c changed by me:<br><br>#include &lt;stdio.h&gt;<br>#include &lt;openssl/rsa.h&gt;<br>#include &lt;openssl/evp.h&gt;<br>#include &lt;openssl/objects.h&gt;<br>#include &lt;openssl/x509.h&gt;<br>#include &lt;openssl/err.h&gt;<br>
> #include &lt;openssl/pem.h&gt;<br>#include &lt;openssl/ssl.h&gt;<br><br>int main ()<br>{<br>&nbsp; int err;<br>&nbsp; int sig_len;<br>&nbsp; unsigned char sig_buf [4096];<br>&nbsp; static char certfile[] = &quot;pubkey.pem&quot;;<br>&nbsp; static char keyfile[]&nbsp; = &quot;privkey.pem&quot;;<br>
> &nbsp; static char data[]&nbsp;&nbsp;&nbsp;&nbsp; = &quot;I owe you...&quot;;<br>&nbsp; EVP_MD_CTX&nbsp;&nbsp;&nbsp;&nbsp; md_ctx;<br>&nbsp; EVP_PKEY *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pkey;<br>&nbsp; FILE *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fp;<br><br>&nbsp; /* Just load the crypto library error strings,<br>&nbsp;&nbsp; * SSL_load_error_strings() loads the crypto AND the SSL ones */<br>
> &nbsp; /* SSL_load_error_strings();*/<br>&nbsp; ERR_load_crypto_strings();<br>&nbsp; <br>&nbsp; /* Read private key */<br>&nbsp; <br>&nbsp; fp = fopen (keyfile, &quot;r&quot;);<br>&nbsp; if (fp == NULL) exit (1);<br>&nbsp; pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);<br>
> &nbsp; fclose (fp);<br><br>&nbsp; if (pkey == NULL) { <br>&nbsp;&nbsp;&nbsp; ERR_print_errors_fp (stderr);<br>&nbsp;&nbsp;&nbsp; exit (1);<br>&nbsp; }<br>&nbsp; <br>&nbsp; /* Do the signature */<br>&nbsp; <br>&nbsp; EVP_SignInit&nbsp;&nbsp; (&amp;md_ctx, EVP_sha1());<br>&nbsp; EVP_SignUpdate (&amp;md_ctx, data, strlen(data));<br>
> &nbsp; sig_len = sizeof(sig_buf);<br>&nbsp; err = EVP_SignFinal (&amp;md_ctx, sig_buf, &amp;sig_len, pkey);<br><br>&nbsp; if (err != 1) {<br>&nbsp;&nbsp;&nbsp; ERR_print_errors_fp(stderr);<br>&nbsp;&nbsp;&nbsp; exit (1);<br>&nbsp; }<br><br>&nbsp; EVP_PKEY_free (pkey);<br>&nbsp; <br>
> &nbsp; /* Read public key */<br>&nbsp; <br>&nbsp; fp = fopen (certfile, &quot;r&quot;);<br>&nbsp; if (fp == NULL) exit (1);<br>&nbsp; pkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL);<br>&nbsp; fclose (fp);<br>&nbsp; <br>&nbsp; if (pkey == NULL) { <br>&nbsp;&nbsp;&nbsp; ERR_print_errors_fp (stderr);<br>
> &nbsp;&nbsp;&nbsp; exit (1);<br>&nbsp; }<br><br>&nbsp; /* Verify the signature */<br>&nbsp; <br>&nbsp; EVP_VerifyInit&nbsp;&nbsp; (&amp;md_ctx, EVP_sha1());<br>&nbsp; EVP_VerifyUpdate (&amp;md_ctx, data, strlen((char*)data));<br>&nbsp; err = EVP_VerifyFinal (&amp;md_ctx, sig_buf, sig_len, pkey);<br>
> &nbsp; EVP_PKEY_free (pkey);<br><br>&nbsp; if (err != 1) {<br>&nbsp;&nbsp;&nbsp; ERR_print_errors_fp (stderr);<br>&nbsp;&nbsp;&nbsp; exit (1);<br>&nbsp; }<br>&nbsp; printf (&quot;Signature Verified Ok.\n&quot;);<br>&nbsp; return(0);<br>}<br><br>it&#39;s return the following error:<br>
> 1883:error:0906D06C:PEM routines:PEM_read_bio:no start line:/export/builds/onnv_101a/usr/src/common/openssl/crypto/pem/pem_lib.c:644:Expecting: PUBLIC KEY<br><br>Can somebody plz help me ?<br>TNX<br clear="all"><br>-- <br>

> ------------------------------------------------------------------<br>Marco Sommella<br><a href="mailto:marco.somme...@gmail.com">marco.somme...@gmail.com</a> (E-Mail &amp; MSN)<br>------------------------------------------------------------------<br>


>
> ------=_Part_12058_26001910.1228336035290--
> ______________________________________________________________________
> OpenSSL Project                                http://www.openssl.org

> User Support Mailing List                    openssl-us...@openssl.org
> Automated List Manager                           majord...@openssl.org

0 new messages