BIO_new_mem_buf() doesn't work after RSA_private_decrypt() failure.
Hi all, I have written a program which does RSA_private_decrypt() repeatedly with a private key given with PEM format. I also derive private key with BIO_new_mem_buf() and PEM_read_bio_RSAPrivateKey() functions, so the brief code is as follows. bioPtr = BIO_new_mem_buf( InputPEMstring, -1 ); : prvkey = PEM_read_bio_RSAPrivateKey( bioPtr, NULL, NULL, NULL ); : RSA_private_decrypt( ..., prvkey, RSA_PKCS1_OAEP_PADDING ); /* We use OAEP */ It does work well as long as providing appropriate private keys. Buf if I provide wrong private key, RSA_private_decrypt() fails of course, and then next BIO_new_mem_buf() also fails with the error code 0x407A079 by ERR_get_error(). Do I need to call some initializing api after private decryption failure, before calling BIO_new_mem_buf() again, or is there any work around ? I'm using OpenSSL 0.9.8a on 2.4.21-4 Best regards, Tatsuya Tsurukawa __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager [EMAIL PROTECTED]
Re: BIO_new_mem_buf() doesn't work after RSA_private_decrypt() failure.
Dear Richard, Thank you for your quick answer. I did nothing after RSA_private_decrypt() error(-1) with a wrong private key and called ERR_get_error() right after BIO_new_mem_buf() returned NULL as follows. iRet = RSA_private_decrypt(); // provide wrong private key if( iRet == -1 ){ // Do nothing } bioPtr = BIO_new_mem_buf(); if( bioPtr == NULL ){ ERR_get_error(); : } As you've mentioned, it got _not_ BIO_new_mem_buf() error but RSA_private_decrypt() error instead. I don't know why it didn't get BIO_new_mem_buf() error although it failed (returned NULL). By the way, I've also found if I call ERR_clear_error() between RSA_private_decrypt() and BIO_new_mem_buf(), BIO_new_mem_buf() doesn't fail as follows. iRet = RSA_private_decrypt(); // provide wrong private key if( iRet == -1 ){ // Do nothing } ERR_clear_error(); bioPtr = BIO_new_mem_buf(); if( bioPtr == NULL ){ ERR_get_error(); : } It seems I need to clear error queue after private decryption error, but does the status of error queue actually affect to the behaviour of following apis ? Best regards, Tatsuya Tsurukawa Richard Levitte - VMS Whacker wrote: In message [EMAIL PROTECTED] on Tue, 20 Jun 2006 21:16:49 +0900, Tatsuya Tsurukawa [EMAIL PROTECTED] said: Tsurukawa.Tatsuya bioPtr = BIO_new_mem_buf( InputPEMstring, -1 ); Tsurukawa.Tatsuya : Tsurukawa.Tatsuya prvkey = PEM_read_bio_RSAPrivateKey( bioPtr, NULL, NULL, NULL ); Tsurukawa.Tatsuya : Tsurukawa.Tatsuya RSA_private_decrypt( ..., prvkey, RSA_PKCS1_OAEP_PADDING ); /* We use OAEP */ Tsurukawa.Tatsuya Tsurukawa.Tatsuya It does work well as long as providing appropriate Tsurukawa.Tatsuya private keys. Buf if I provide wrong private key, Tsurukawa.Tatsuya RSA_private_decrypt() fails of course, and then Tsurukawa.Tatsuya next BIO_new_mem_buf() also fails with the error Tsurukawa.Tatsuya code 0x407A079 by ERR_get_error(). Exactly how do you check for errors? You see, the error codes are stored in a queue until you either print it (ERR_print_errors()) or clear it (ERR_clear_error()). (yeah, of course, there are other ERR_* functions that you can use as well to manipulate the queue, but those I mention are probably the more useful most of the times) To check *if* a specific function returned with an error, you have to check the returned value, and *if* it returned with a value indicating an error, *then* you check the error code. Same thing as you do with errno, basically. Since BIO_new_mem_buf() returns a pointer, it's quite natural to check if returns NULL to see if there was an error at all with it. For RSA_private_decrypt(), the manual says the following about the returned value: RSA_public_encrypt() returns the size of the encrypted data (i.e., RSA_size(rsa)). RSA_private_decrypt() returns the size of the recovered plaintext. On error, -1 is returned; the error codes can be obtained by ERR_get_error(3). The code 0x407A079 is easily decoded, btw: : ; openssl errstr 407A079 error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error Cheers, Richard __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager [EMAIL PROTECTED]
Re: How to get To-Be-Signed portion of certificate with openssl api ?
Dear Steve, I've tried it with the following code, but I couldn't get the correct data yet. Could you please point out the wrong point of the following code. // variables int iResult = 0; unsigned char cert[2000]; BIO *bioPtr; X509 *certPtr; unsigned char *tbs; : // make X509 structure bioPtr = BIO_new_mem_buf(cert, -1); certPtr = PEM_read_bio_X509(bioPtr, NULL, NULL, NULL); // get binary data size of tbs iResult = i2d_X509_CINF(certPtr-cert_info, NULL); // prepare buffer for tbs tbs = (unsigned char *)malloc(iResult); // get der binary data of tbs iResult = i2d_X509_CINF(certPtr-cert_info, tbs); tbs is filled with data, but it is different from correct data. Best regards, Tatsuya Tsurukawa Dr. Stephen Henson wrote: On Wed, Mar 01, 2006, Tatsuya Tsurukawa wrote: Hi All, I have a quick question. How can I get To-Be-Signed portion of certificate with openssl api ? In case of using JDK, X509Certificat class and getTBSCertificate() method seem to be available for the same purpose. I'm not familiar with the openssl api, and I couldn't find the appropriate api due to the luck of sample codes on the internet. Decode the certificate into an X509 structure using d2i_X509(). The TBS portion in OpenSSL is called X509_CINF and the relevant fields are available in the structure. If you want to reencode it call i2d_X509_CINF. Information about the d2i/i2d functions is in the FAQ. Steve. -- Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage OpenSSL project core developer and freelance consultant. Funding needed! Details on homepage. Homepage: http://www.drh-consultancy.demon.co.uk __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager [EMAIL PROTECTED] [EMAIL PROTECTED] __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager [EMAIL PROTECTED]
How to get To-Be-Signed portion of certificate with openssl api ?
Hi All, I have a quick question. How can I get To-Be-Signed portion of certificate with openssl api ? In case of using JDK, X509Certificat class and getTBSCertificate() method seem to be available for the same purpose. I'm not familiar with the openssl api, and I couldn't find the appropriate api due to the luck of sample codes on the internet. Regards, Tatsuya Tsurukawa [EMAIL PROTECTED] __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager [EMAIL PROTECTED]