Skip Menu |
Report information
Id: 132668
Status: resolved
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: lloyd.fourn [at] gmail.com
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



From: Lloyd Fournier <lloyd.fourn [...] gmail.com>
Subject: [NATIVECALL][SEGV] OpenSSL PEM_write_bio_RSAPrivateKey
Date: Sat, 30 Dec 2017 04:34:33 +0000
To: "rakudobug [...] perl.org" <rakudobug [...] perl.org>
Download (untitled) / with headers
text/plain 1.3k
I was trying to add RSA key generation using nativecall and OpenSSL but kept getting non-deterministic segfaults. golfed example:

use NativeCall;

sub gen-lib { $*VM.platform-library-name('ssl'.IO).Str }

sub RSA_new(-->OpaquePointer) is native(&gen-lib) {...}
sub RSA_generate_key_ex(OpaquePointer $rsa, int32 $bits, OpaquePointer $e, OpaquePointer $cb --> OpaquePointer) is native(&gen-lib) {...}
sub BN_new(-->OpaquePointer) is native(&gen-lib) {...}
sub BN_set_word(OpaquePointer $bn, uint32 $e --> int32) is native(&gen-lib) {...}
sub BIO_s_mem(-->OpaquePointer) is native(&gen-lib) {...}
sub PEM_write_bio_RSAPrivateKey(OpaquePointer $bio, OpaquePointer $rsa --> int32) is native(&gen-lib){...}
sub BIO_new(OpaquePointer $method --> OpaquePointer) is native(&gen-lib){...};

{
    my uint32 $bits = 2048;
    my $bne = BN_new();
    BN_set_word($bne, 3) || die;
    my $rsa = RSA_new();
    RSA_generate_key_ex($rsa, $bits, $bne, Nil) || die;
    my $bio = BIO_new(BIO_s_mem());
    PEM_write_bio_RSAPrivateKey($bio, $rsa);
}

bash-3.2$ while perl6 nativessl.pl; do echo "worked"; done
Segmentation fault: 11
bash-3.2$ while perl6 nativessl.pl; do echo "worked"; done
worked
worked
worked
worked
worked
worked
Segmentation fault: 11
bash-3.2$ while perl6 nativessl.pl; do echo "worked"; done
worked
worked
worked
worked
Segmentation fault: 11
bash-3.2$ 
Download (untitled) / with headers
text/plain 456b
The problem is the PEM_write_bio_RSAPrivateKey signature used in the NativeCall declaration, it missed five arguments. From the manual: int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); So when called it received random values. My modernized example attached that succeds.
Subject: nativessl.p6
Download nativessl.p6
application/octet-stream 882b

Message body not shown because it is not plain text.

OK, so that's not an issue in Rakudo then. Closed.

On 2019-03-05 11:16:41, sortiz wrote:
Show quoted text
> The problem is the PEM_write_bio_RSAPrivateKey signature used in the
> NativeCall declaration, it missed five arguments.
>
> From the manual:
>
> int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER
> *enc,
> unsigned char *kstr, int
> klen,
> pem_password_cb *cb, void
> *u);
>
> So when called it received random values.
>
> My modernized example attached that succeds.




This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org