[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
{
    [CCode (instance_pos = 1.1)]
    public int print_fp(GLib.FileStream fp, int offset);


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


    [CCode (instance_pos = 2.1)]
    public int public_encrypt([CCode (array_length_pos = 0)] uint8[] from, [CCode (array_length = 
false)] uint8[] to, int padding);
   
    [CCode (instance_pos = 2.1)]
    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
{
    outbuf_cipher = new uint8[inbuf_plain.length];
    outbuf_length = m_rsa_cipher.public_encrypt(inbuf_plain, outbuf_cipher, 
OpenSSL.RSA.PKCS1_PADDING);
    check_and_throw_error(outbuf_length >= 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
{
    outbuf_plain = new uint8[inbuf_cipher.length];
    outbuf_length = m_rsa_cipher.private_decrypt(inbuf_cipher, outbuf_plain, 
OpenSSL.RSA.PKCS1_PADDING);
    check_and_throw_error(outbuf_length >= 0, outbuf_length, _("Failed to decrypt data."));
}



// print rsa cipher  -----> 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);
-----> c code
_tmp6_ = self->priv->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);
-----> 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);
-----> 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)








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

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



&gt; On Saturday, 22 February 2020, 07:58:10 GMT, ???? via vala-list <vala-list gnome org&gt; wrote: 
&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


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