Segmentation fault: what's wrong with this code?
Stef Bon <[email protected]> Fri, 5 Jun 2026 11:33:26 -0700 (PDT)
| Newsgroups | gmane.comp.encryption.openssl.user |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_234340_1372695241.1780684406744
Content-Type: multipart/alternative;
boundary="----=_Part_234341_860509091.1780684406744"
------=_Part_234341_860509091.1780684406744
Content-Type: text/plain; charset="UTF-8"
Hi,
I've written the following code, but I get a segmentation fault (when
calling EVP_PKEY_free).
I do not see what's causing this. Can you see it? Any help appreciated.
static void PK_read_spk_from_OSSL_DECODER(char *buffer, long unsigned int
size)
{
OSSL_DECODER_CTX *dctx=NULL;
EVP_PKEY *spk=NULL;
const unsigned char *start=(unsigned char *) buffer;
/* private key has a PEM encoded key */
dctx=OSSL_DECODER_CTX_new_for_pkey(&spk, "PEM", NULL, NULL,
EVP_PKEY_KEYPAIR, NULL, NULL);
if (dctx==NULL) {
fprintf(stdout, "cannot create decoder.\n");
return;
}
if (OSSL_DECODER_from_data(dctx, &start, &size)<=0) {
fprintf(stdout, "decoder error.\n");
} else {
fprintf(stdout, "decoder finished ok.\n");
EVP_PKEY_print_private_stdout(spk);
}
EVP_PKEY_free(spk);
OSSL_DECODER_CTX_free(dctx);
}
Thanks in advance,
Stef Bon
--
You received this message because you are subscribed to the Google Groups "openssl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openssl-users+unsubscribe-MCmKBN63+Bmbup2nOX2J7Q@public.gmane.org
To view this discussion visit https://groups.google.com/a/openssl.org/d/msgid/openssl-users/d37f0ed9-e315-4449-8e27-9cd1902d9190n%40openssl.org.
------=_Part_234341_860509091.1780684406744
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div>Hi,</div><div><br /></div><div>I've written the following code, but I =
get a segmentation fault (when calling EVP_PKEY_free).=C2=A0</div><div>I do=
not see what's causing this. Can you see it? Any help appreciated.</div><d=
iv><br /></div><div>
<span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); f=
ont-family: Courier New;">static void PK_read_spk_from_OSSL_DECODER(char *b=
uffer, long unsigned int size)</span><span style=3D"color: rgb(0, 0, 0); ba=
ckground-color: rgb(255, 255, 255); font-family: Courier New;">
</span><span style=3D"font-family: Courier New;"><br /><span style=3D"color=
: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">{</span><span style=
=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);"> =C2=A0=C2=A0=C2=A0OSSL_DECODER_CTX *dctx=3DNULL;</span><span =
style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);"> =C2=A0=C2=A0=C2=A0EVP_PKEY *spk=3DNULL;</span><span style=3D"=
color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);"> =C2=A0=C2=A0=C2=A0const unsigned char *start=3D(unsigned char=
*) buffer;</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb=
(255, 255, 255);"></span><br /><span style=3D"color: rgb(0, 0, 0); backgrou=
nd-color: rgb(255, 255, 255);"></span><span style=3D"color: rgb(0, 0, 0); b=
ackground-color: rgb(255, 255, 255);"><br /></span></span></div><div><span =
style=3D"font-family: Courier New;"><span style=3D"color: rgb(0, 0, 0); bac=
kground-color: rgb(255, 255, 255);">=C2=A0 =C2=A0/* private key has a PEM e=
ncoded key */</span><span style=3D"color: rgb(0, 0, 0); background-color: r=
gb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);"> =C2=A0=C2=A0=C2=A0dctx=3DOSSL_DECODER_CTX_new_for_pkey(&s=
pk, "PEM", NULL, NULL, EVP_PKEY_KEYPAIR, NULL, NULL);</span><span style=3D"=
color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);"> =C2=A0=C2=A0=C2=A0if (dctx=3D=3DNULL) {</span><span style=3D"=
color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, =
255);">=C2=A0 =C2=A0 =C2=A0 =C2=A0fprintf(stdout, "cannot create decoder.\n=
");</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 25=
5, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);"> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return;</span><span=
style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, =
255);">=C2=A0 =C2=A0}</span><span style=3D"color: rgb(0, 0, 0); background-=
color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);"> =C2=A0=C2=A0=C2=A0if (OSSL_DECODER_from_data(dctx, &start=
, &size)<=3D0) {</span><span style=3D"color: rgb(0, 0, 0); backgroun=
d-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, =
255);">=C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span style=3D"color: rgb(0, 0, 0)=
; background-color: rgb(255, 255, 255);">fprintf(stdout, "decoder error.\n"=
);</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255=
, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, =
255);">=C2=A0 =C2=A0} else {</span><span style=3D"color: rgb(0, 0, 0); back=
ground-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, =
255);">=C2=A0 =C2=A0 =C2=A0 =C2=A0fprintf(stdout, "decoder finished ok.\n")=
;</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255,=
255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);"> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0EVP_PKEY_print_priv=
ate_stdout(spk);</span><span style=3D"color: rgb(0, 0, 0); background-color=
: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, =
255);">=C2=A0 =C2=A0}</span><span style=3D"color: rgb(0, 0, 0); background-=
color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">=C2=A0 =C2=A0EVP_PKEY_free(spk);</span><span style=3D"color: r=
gb(0, 0, 0); background-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">=C2=A0 =C2=A0OSSL_DECODER_CTX_free(dctx);</span><span style=3D=
"color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
</span><br /><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255,=
255, 255);">
</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(255, 255, =
255);">}</span><span style=3D"color: rgb(0, 0, 0); background-color: rgb(25=
5, 255, 255);">
</span><br /></span><span style=3D"font-family: monospace;"><br /><br /></s=
pan>Thanks in advance,</div><div><br /></div><div>Stef Bon</div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;openssl-users" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:openssl-users+unsubscribe-MCmKBN63+BlAfugRpC6u6w@public.gmane.org">openssl-us=
[email protected]</a>.<br />
To view this discussion visit <a href=3D"https://groups.google.com/a/openss=
l.org/d/msgid/openssl-users/d37f0ed9-e315-4449-8e27-9cd1902d9190n%40openssl=
.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/a/op=
enssl.org/d/msgid/openssl-users/d37f0ed9-e315-4449-8e27-9cd1902d9190n%40ope=
nssl.org</a>.<br />
------=_Part_234341_860509091.1780684406744--
------=_Part_234340_1372695241.1780684406744--