rescue'ing OpenSSL errors

R

Robert

Hi! I'm having some issues with the OpenSSL library:

I'm trying to rescue whatever errors the library wants to throw,
however, it's not working:

def encrypt
require 'openssl'
begin
aes = OpenSSL::Cipher::Cipher.new('AES-128-CBC')
aes.encrypt('SOME_KEY')
cipher = aes.update(password)
cipher << aes.final
return cipher
rescue
return false
end
end

Now I go into irb:

require 'encrypt'

encrypt('test') # -> gives encrypted output
encrypt('') # -> gives error "evp_enc.c(332): OpenSSL internal error,
assertion failed: inl > 0 \n Abort trap"

Seems like I can't rescue that because the error comes from a C
library. Is that right?


Are there any solutions to this, except, for example, checking that
"not password.empty?" ?




Thanks, Rob
 
E

Edward Faulkner

--neYutvxvOLaeuPCA
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Seems like I can't rescue that because the error comes from a C =20
library. Is that right?

By default, rescue only catches exceptions that are derived from
StandardError (not Exception). It's possible that OpenSSL is raising
an exception derived directly from Exception.

Try using "rescue Exception" instead of just "rescue".

regards,
Ed

--neYutvxvOLaeuPCA
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDVXU3nhUz11p9MSARAvxMAJwKL81UPkTTJI+Btwcrz4WGdgb8bACgqi3t
NkJYlO+NK/mD1aC7BWWFWrg=
=Ixff
-----END PGP SIGNATURE-----

--neYutvxvOLaeuPCA--
 
R

Robert

Hello Edward,

thanks about pointing that out. Another thing learned :)

However, it does not work. It still throws that same error (and also
quits irb).

Anyway, for me that's not a problem anymore, because I just added:
raise ArgumentError if password.empty?
to my script. But I'm still curious why a "rescue Exception" doesn't
work here -- if anyone knows, that'd be cool :)


roob
 
T

Toby DiPasquale

Hello Edward,

thanks about pointing that out. Another thing learned :)

However, it does not work. It still throws that same error (and also
quits irb).

Anyway, for me that's not a problem anymore, because I just added:
raise ArgumentError if password.empty?
to my script. But I'm still curious why a "rescue Exception" doesn't
work here -- if anyone knows, that'd be cool :)

Your clue is the last part of the error. It appears as if the OpenSSL
library has generated a SIGABRT. Try this:

require 'encrypt'
trap( "ABRT") { puts "abort trapped... dumping out" }
encrypt( 'test')
encrypt( '')

See if that works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top