[Vala] ?????? ?????? About VAPI's instance position



more testing on windows and linux, found these message:
1. crashed at RSA_public_encrypt() on msys2 on windows 7
#8  0x0000000067e4fb2f in libcrypto-1_1-x64!BN_clear_free ()
   from C:\msys\mingw64\bin\libcrypto-1_1-x64.dll
#9  0x0000000067e474ce in libcrypto-1_1-x64!BN_CTX_free ()
   from C:\msys\mingw64\bin\libcrypto-1_1-x64.dll
#10 0x0000000067f618d8 in libcrypto-1_1-x64!RSA_padding_add_PKCS1_OAEP ()
   from C:\msys\mingw64\bin\libcrypto-1_1-x64.dll
#11 0x0000000000432014 in kangaroo_illuminate_supports_crypto_open_ssl_asymmetric_cipher_real_encrypt



2. crashed at RSA_private_decrypt() on archlinux:
---------------------------------------------------------
unmap_chunk(): invalid pointer


Program received signal SIGABRT, Aborted.
0x00007ffff37fece5 in raise () from /usr/lib/libc.so.6


(gdb) bt
#0  0x00007ffff37fece5 in raise () at /usr/lib/libc.so.6
#1  0x00007ffff37e8857 in abort () at /usr/lib/libc.so.6
#2  0x00007ffff38422b0 in __libc_message () at /usr/lib/libc.so.6
#3  0x00007ffff384974a in  () at /usr/lib/libc.so.6
#4  0x00007ffff38499fc in munmap_chunk () at /usr/lib/libc.so.6
#5  0x00007ffff6d1495d in BN_clear_free () at /usr/lib/libcrypto.so.1.1
#6  0x00007ffff6d0d02f in BN_CTX_free () at /usr/lib/libcrypto.so.1.1
#7  0x00007ffff6e07965 in  () at /usr/lib/libcrypto.so.1.1
#8  0x00005555555cff2c in kangaroo_illuminate_supports_crypto_open_ssl_asymmetric_cipher_real_decrypt



Could we make sure that vala code is correct and bug come from openssl?


??????:&nbsp;"vala-list"<vala-list gnome org&gt;;
????????:&nbsp;2020??2??23??(??????) ????12:35
??????:&nbsp;"vala-list"<vala-list gnome org&gt;;

????:&nbsp;[Vala] ??????  About VAPI's instance position



Hi Al,


thank you response quickly.


I follow your guide to update the openssl.vapi:
--------------------------------------------------------------------
[Compact]
[CCode (lower_case_cprefix = "RSA_", cprefix = "RSA_", cheader_filename = "openssl/rsa.h", free_function = 
"RSA_free")]
public class RSA
{
&amp;nbsp; &amp;nbsp; [CCode (instance_pos = 1.1)]
&amp;nbsp; &amp;nbsp; public int print_fp(GLib.FileStream fp, int offset);


&amp;nbsp; &amp;nbsp; [CCode (instance_pos = 1.1)]
&amp;nbsp; &amp;nbsp; public int print(BIO bp, int offset);


&amp;nbsp; &amp;nbsp; [CCode (instance_pos = 2.1)]
&amp;nbsp; &amp;nbsp; public int public_encrypt([CCode (array_length_pos = 0)] uint8[] from, [CCode 
(array_length = false)] uint8[] to, int padding);
&amp;nbsp; &amp;nbsp;
&amp;nbsp; &amp;nbsp; [CCode (instance_pos = 2.1)]
&amp;nbsp; &amp;nbsp; public int private_decrypt([CCode (array_length_pos = 0)] uint8[] from, [CCode 
(array_length = false)] uint8[] to, int padding);  
}



the calling code
------------------------------------------------------------------------------------------------------
public void encrypt(uint8[] inbuf_plain, out uint8[] outbuf_cipher = null, out int outbuf_length = null) 
throws GLib.Error
{
&amp;nbsp; &amp;nbsp; outbuf_cipher = new uint8[inbuf_plain.length];
&amp;nbsp; &amp;nbsp; outbuf_length = m_rsa_cipher.public_encrypt(inbuf_plain, outbuf_cipher, 
OpenSSL.RSA.PKCS1_PADDING);
&amp;nbsp; &amp;nbsp; check_and_throw_error(outbuf_length &amp;gt;= 0, outbuf_length, _("Failed to encrypt 
data."));
}


public void decrypt(uint8[] inbuf_cipher, out uint8[] outbuf_plain = null, out int outbuf_length = null) 
throws GLib.Error
{
&amp;nbsp; &amp;nbsp; outbuf_plain = new uint8[inbuf_cipher.length];
&amp;nbsp; &amp;nbsp; outbuf_length = m_rsa_cipher.private_decrypt(inbuf_cipher, outbuf_plain, 
OpenSSL.RSA.PKCS1_PADDING);
&amp;nbsp; &amp;nbsp; check_and_throw_error(outbuf_length &amp;gt;= 0, outbuf_length, _("Failed to decrypt 
data."));
}



// print rsa cipher&amp;nbsp; -----&amp;gt; correct, output successfully
var bio_stdout = new OpenSSL.BIO.with_stream(GLib.stdout, OpenSSL.BIO.NOCLOSE);
m_rsa_cipher.print(bio_stdout, 0);
bio_stdout.flush();



the code can compile and the follow code will crash again:
----------------------------------------------------------------------
m_rsa_cipher.print(GLib.stdout, 0);
-----&amp;gt; c code
_tmp6_ = self-&amp;gt;priv-&amp;gt;m_rsa_cipher;
_tmp7_ = stdout;
RSA_print_fp (_tmp7_, _tmp6_, 0);



outbuf_length = m_rsa_cipher.public_encrypt(inbuf_plain, outbuf_cipher, OpenSSL.RSA.PKCS1_PADDING);
-----&amp;gt; c code
_vala_outbuf_length = RSA_public_encrypt ((gint) inbuf_plain_length1, inbuf_plain, _vala_outbuf_cipher, 
_tmp1_, RSA_PKCS1_PADDING);


outbuf_length = m_rsa_cipher.private_decrypt(inbuf_cipher, outbuf_plain, OpenSSL.RSA.PKCS1_PADDING);
-----&amp;gt; c code
_vala_outbuf_length = RSA_private_decrypt ((gint) inbuf_cipher_length1, inbuf_cipher, _vala_outbuf_plain, 
_tmp1_, RSA_PKCS1_PADDING);


the context is:
1. create rsa engine (all done)
2. generate rsa key and print rsa engine (all done)
3. read rsa key and write rsa key to file (all done)
4. encrypt with public key and decrypt with private key (crashed)








??????:&amp;nbsp;"vala-list"<vala-list gnome org&amp;gt;;
????????:&amp;nbsp;2020??2??22??(??????) ????8:34
??????:&amp;nbsp;"vala-list"<vala-list gnome org&amp;gt;;

????:&amp;nbsp;Re: [Vala] About VAPI's instance position



&amp;gt; On Saturday, 22 February 2020, 07:58:10 GMT, ???? via vala-list <vala-list gnome org&amp;gt; wrote: 
&amp;gt; if set the instance_pos as 2.1, the code can compile and will crashed at function public_encrypt / 
private_decrypt,

2.1 sounds about right. That may not be the cause of the crash.

The documentation is helpful: https://www.openssl.org/docs/man1.1.1/man3/RSA_public_encrypt.html

`flen` should be the size of `from`. So you should use `array_length_pos` with `from` instead of 
`array_length = false`.

For improved code readability you can make `padding` a Vala enum called `Padding` that binds 
`RSA_PKCS1_PADDING`, etc.

A useful tip is to look at the C code generated from the Vala. Use `--ccode` or `--save-temps` with valac to 
get the intermediate C files. If you still have troubles please post the intermediate C code generated from 
Vala.

Best wishes,

Al
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]