Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior when not providing key (nil) (alg: 'dir') #4

Closed
vmeyet opened this issue Aug 24, 2016 · 1 comment
Closed

Strange behavior when not providing key (nil) (alg: 'dir') #4

vmeyet opened this issue Aug 24, 2016 · 1 comment
Labels

Comments

@vmeyet
Copy link

vmeyet commented Aug 24, 2016

Issue

When using the algorithm "dir" and not giving a encryption key, the lib allow me to encrypt the message anyway (without knowing the encryption key)

pry(main)> JWE.encrypt('plain', nil, alg: 'dir')
=> "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..gOyySHAV-rYOmDd1.zfWUxIY.W7YvYlp7I01ZEJe3ZGntJw"

# How should I decipher it?

The issue is more pernicious and prod like in the following case:

pry(main)> cipher = JWE.encrypt('plain', ENV['ENCRYPTION_KEY'], alg: 'dir')
pry(main)> JWE.decrypt(cipher, ENV['ENCRYPTION_KEY'])
=> JWE::InvalidData: Invalid ciphertext or authentication tag

When using an ENV variable to hold the key, and forgetting to set that variable on the server.

Expected Behavior

An error raised, when not providing an encryption key when trying to encrypt the message

Reason

In https://github.com/jwt/ruby-jwe/blob/master/lib/jwe.rb#L31 we set the key directly if the alg is "dir"

cipher.cek = key if alg == 'dir'

In https://github.com/jwt/ruby-jwe/blob/master/lib/jwe/enc/aes_gcm.rb#L45-L47

the attr accessor set the variable @cek on the instance and when calling :cek afterward the variable @cek is set to random_bytes. That is used to encrypt the plain text.

   attr_accessor :cek
   ...
   def cek
     @cek ||= SecureRandom.random_bytes(key_length)
   end

When deciphering the cipher, the cipher key (random bytes) and the encryption key (nil) does not match an we get a CipherError

@excpt excpt added the bug label Aug 24, 2016
@francescobbo
Copy link
Member

Good catch! I'm fixing this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants