nov / json-jwt

JSON Web Token and its family (JSON Web Signature, JSON Web Encryption and JSON Web Key) in Ruby
MIT License
298 stars 80 forks source link

A128CBC-HS256 #14

Closed jondeandres closed 10 years ago

jondeandres commented 10 years ago

Hi,

in the draft in http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-14 i can read this:

Replaced "A128CBC+HS256" and "A256CBC+HS512" with "A128CBC-HS256" and "A256CBC-HS512". The new algorithms perform the same cryptographic computations as [I-D.mcgrew-aead-aes-cbc-hmac-sha2], but with the Initialization Vector and Authentication Tag values remaining separate from the Ciphertext value in the output representation. Also deleted the header parameters "epu"(encryption PartyUInfo) and "epv" (encryption PartyVInfo), since they are no longer used.

Will you update the gem to work properly with this change in the draft? If you know what changes should be made in JSON::JWE perhaps i'll be able to code them.

An example of the problem i have. Trying to decode with A128CBC+HS256 i have this error when calling JSON::JWE#decrypt! method:

OpenSSL::Cipher::CipherError Exception: wrong final block length

Thanks.

nov commented 10 years ago

JWE implementation isn't following the latest spec, since JWE spec isn't solid yet.

nov commented 10 years ago

GCM now working. CBC isn't yet.

I need to read the latest JWE spec to understand what I need to update. It seems CBC flow changed a lot.

jondeandres commented 10 years ago

Thanks for the effort Nov.

I'm looking at the specification and your code, trying to understand something. Really i don't know what to change, but i'm researching.

This seems to be the key perhaps?

o Replaced "A128CBC+HS256" and "A256CBC+HS512" with "A128CBC-HS256" and "A256CBC-HS512". The new algorithms perform the same cryptographic computations as [I-D.mcgrew-aead-aes-cbc-hmac-sha2 http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-17#ref-I-D.mcgrew-aead-aes-cbc-hmac-sha2], but with the Initialization Vector and Authentication Tag values remaining separate from the Ciphertext value in the output representation. Also deleted the header parameters "epu" (encryption PartyUInfo) and "epv" (encryption PartyVInfo), since they are no longer used.

o Changed from using the term "Integrity Value" to "Authentication Tag".

On Thu, Oct 24, 2013 at 11:41 AM, Nov Matake notifications@github.comwrote:

GCM now working. CBC isn't yet.

I need to read the latest JWE spec to understand what I need to update. It seems CBC flow changed a lot.

— Reply to this email directly or view it on GitHubhttps://github.com/nov/json-jwt/issues/14#issuecomment-26978850 .

Jon de Andrés

nov commented 10 years ago

I've managed to follow the latest spec. Now all tests passed, so json-jwt is Nimbus JOSE+JWT compatible. (Nimbus JOSE+JWT is the most stable JOSE implementation I know)

Please try json-jwt v0.6.0 and tell me if you have any troubles. Since I'm also not crypto expert, I need more feedback on JWE implementation.

nov commented 10 years ago

BTW, for what are you using JWE?

jondeandres commented 10 years ago

Nov,

i work in Wuaki.tv, www.wuaki.tv, a video on demand service in Spain and UK, we are part of the Rakuten group, i supose you know. We are developing the Wuaki.tv application for Microsoft Xbox One and they use JWT to send us some user authentication tokens in a JWT claim.

Thank you very much cause the changes!! I'm sure it can be useful for us.

I'm having still some problems with this, perhaps we have an error in our code. Do you have any inconvenient to review this code?

We have this class that we use to decode the token we receive:

module XboxOne class JWT attr_reader :private_key

def initialize(private_key = nil)
  @private_key = private_key || default_private_key
end

# Decrypts the received data and returns a
# {JSON::JWE} object. The data received should be
 # a JWT token from the Xbox console
#
# @param [String] data the data to decrypt
# @return [JSON::JWE] the decrypted object
def decrypt!(data)
  jwe = JSON::JWE.new(data)
  jwe.enc = encryption_type
  jwe.alg =  algorithm

  jwe.decrypt!(private_key)
end

# This is the encryption type defined by the xbox documentation
def encryption_type
  :'A128CBC-HS256'
end

def algorithm
  :'RSA-OAEP'
end

def default_private_key
  OpenSSL::PKey::RSA.new(File.new(default_private_key_path))
end

def default_private_key_path
  ::Rails.application.config.wuaki_private_key_path
end

end end

And i'm trying to test this:

  let(:encrypted_data) do

"eyJlbmMiOiJBMTI4Q0JDK0hTMjU2IiwiYWxnIjoiUlNBLU9BRVAiLCJjdHkiOiJKV1QiLCJ6aXAiOiJERUYiLCJ4NXQiOiJfTXU5V3JJb2Yyb0w5b3BzUFhTYjd0RE1IMzAifQ.Pnzzn7bLi6_MGPRJzoS60Z248QNWK6CBCVTdv_XhHMQ3a9rCaKGVHDw_llavnbRo3RP5qT5E-QXuK8NACwDET6Jm1ZDdJ1v1YhJwZoULzHarWDY15W23gkAjCQDkJfqG4OG252Yj8Qy5xSC1HYf01NKJNHj1MgLyizAEgUrRRWFbrXxhCbLOHNabgs4kLsE5Zph-TtvAGH6Fieu1bkYYo3v0MT2jWsqhYKnumTzvuRespNg1GQioBo8-Os5VwkmveeTpn7KlxjDS4HeD-czC4KzrgxaUWu9desxpgK_ft8irxL_HArNfVdm8kg3dFvcaruaPrtNfeP_aUZ9fKB8Lbg.eTtVTOf6wPeKNkk6ABOPRA.3xTYtlHrryXrK4iBURxIS1WaBq9Lu1fn7V6e9D5D8XRPcE0T9DIyRLkhZS1bVDMFu_vr5cjqxBKh4z3MChV0yQsIKES6EXlsDmFpBg7dKloiIlAuQdDBra8h_3dTna8Tmxn9iJh-nbSVnn80lq8YC44iFB9neKZUg0IO5Tm6fpsTlk-09NIhq16v4Wm5_c1_CUs0kfQxLg9ACPEQD2Il_wTWCSVfyS1vCY081EuV51RyIZQH0U9TdvpSj6JgkxO-wv1NnuQUUQ2DKLqsbgr7hXhXNM5WbulkPtNk7I3hQmUybLPC5sQO8QrLvGn61zfvx-xwKj0qTVg0uBECvamyliChYgV5KbpW3NIkMjljZvh9VZuQ0_sa37YFGTpMWbmz1p7aDQ_bbupWfM2SkLlCPDQ6OJYw_QkqkCuQOz7FuKxCj4mMBi0vxUxGCgoIdXqV_EyQ-4wehu7185JxyvNFxhEXM8YG8CFi6XX6YqZx9LQ5lOIDhvMq6uXjan7JPXT8kNIjt32TmyL7PDGloMCItDAs_GGcT_Zqu0JMgeTpvWB4LhQcIBigifQOk8an6PcOyQLN-SNmuCjmS-CxJvv_dhtrxuiMIQjcAHZqbuWytsZOCDdB8v83aaQWU-hMTl1Zy7ZOXO" end

  subject { XboxOne::JWT.new }

  it 'decrypts the data' do
    subject.decrypt!(encrypted_data).to_s
  end

I have this error when calling cipher.final in your code:

OpenSSL::Cipher::CipherError Exception: wrong final block length

Thank you Nov! Regards.

On Thu, Oct 24, 2013 at 1:32 PM, Nov Matake notifications@github.comwrote:

BTW, for what are you using JWE?

— Reply to this email directly or view it on GitHubhttps://github.com/nov/json-jwt/issues/14#issuecomment-26984741 .

Jon de Andrés

jondeandres commented 10 years ago

Nov,

the Microsoft guys tell me that the token we receive should be first signed and last crypted. Do you have any idea of how can I use your gem to do this?

On Thu, Oct 24, 2013 at 1:47 PM, Jon de Andrés jondeandres@gmail.comwrote:

Nov,

i work in Wuaki.tv, www.wuaki.tv, a video on demand service in Spain and UK, we are part of the Rakuten group, i supose you know. We are developing the Wuaki.tv application for Microsoft Xbox One and they use JWT to send us some user authentication tokens in a JWT claim.

Thank you very much cause the changes!! I'm sure it can be useful for us.

I'm having still some problems with this, perhaps we have an error in our code. Do you have any inconvenient to review this code?

We have this class that we use to decode the token we receive:

module XboxOne class JWT attr_reader :private_key

def initialize(private_key = nil)
  @private_key = private_key || default_private_key
end

# Decrypts the received data and returns a
# {JSON::JWE} object. The data received should be
 # a JWT token from the Xbox console
#
# @param [String] data the data to decrypt
# @return [JSON::JWE] the decrypted object
def decrypt!(data)
  jwe = JSON::JWE.new(data)
  jwe.enc = encryption_type
  jwe.alg =  algorithm

  jwe.decrypt!(private_key)
end

# This is the encryption type defined by the xbox documentation
def encryption_type
  :'A128CBC-HS256'
end

def algorithm
  :'RSA-OAEP'
end

def default_private_key
  OpenSSL::PKey::RSA.new(File.new(default_private_key_path))
end

def default_private_key_path
  ::Rails.application.config.wuaki_private_key_path
end

end end

And i'm trying to test this:

  let(:encrypted_data) do

"eyJlbmMiOiJBMTI4Q0JDK0hTMjU2IiwiYWxnIjoiUlNBLU9BRVAiLCJjdHkiOiJKV1QiLCJ6aXAiOiJERUYiLCJ4NXQiOiJfTXU5V3JJb2Yyb0w5b3BzUFhTYjd0RE1IMzAifQ.Pnzzn7bLi6_MGPRJzoS60Z248QNWK6CBCVTdv_XhHMQ3a9rCaKGVHDw_llavnbRo3RP5qT5E-QXuK8NACwDET6Jm1ZDdJ1v1YhJwZoULzHarWDY15W23gkAjCQDkJfqG4OG252Yj8Qy5xSC1HYf01NKJNHj1MgLyizAEgUrRRWFbrXxhCbLOHNabgs4kLsE5Zph-TtvAGH6Fieu1bkYYo3v0MT2jWsqhYKnumTzvuRespNg1GQioBo8-Os5VwkmveeTpn7KlxjDS4HeD-czC4KzrgxaUWu9desxpgK_ft8irxL_HArNfVdm8kg3dFvcaruaPrtNfeP_aUZ9fKB8Lbg.eTtVTOf6wPeKNkk6ABOPRA.3xTYtlHrryXrK4iBURxIS1WaBq9Lu1fn7V6e9D5D8XRPcE0T9DIyRLkhZS1bVDMFu_vr5cjqxBKh4z3MChV0yQsIKES6EXlsDmFpBg7dKloiIlAuQdDBra8h_3dTna8Tmxn9iJh-nbSVnn80lq8YC44iFB9neKZUg0IO5Tm6fpsTlk-09NIhq16v4Wm5_c1_CUs0kfQxLg9ACPEQD2Il_wTWCSVfyS1vCY081EuV51RyIZQH0U9TdvpSj6JgkxO-wv1NnuQUUQ2DKLqsbgr7hXhXNM5WbulkPtNk7I3hQmUybLPC5sQO8QrLvGn61zfvx-xwKj0qTVg0uBECvamyliChYgV5KbpW3NIkMjljZvh9VZuQ0_sa37YFGTpMWbmz1p7aDQ_bbupWfM2SkLlCPDQ6OJYw_QkqkCuQOz7FuKxCj4mMBi0vxUxGCgoIdXqV_EyQ-4wehu7185JxyvNFxhEXM8YG8CFi6XX6YqZx9LQ5lOIDhvMq6uXjan7JPXT8kNIjt32TmyL7PDGloMCItDAs_GGcT_Zqu0JMgeTpvWB4LhQcIBigifQOk8an6PcOyQLN-SNmuCjmS-CxJvv_dhtrxuiMIQjcAHZqbuWytsZOCDdB8v83aaQWU-hMTl1Zy7ZOXO" end

  subject { XboxOne::JWT.new }

  it 'decrypts the data' do
    subject.decrypt!(encrypted_data).to_s
  end

I have this error when calling cipher.final in your code:

OpenSSL::Cipher::CipherError Exception: wrong final block length

Thank you Nov! Regards.

On Thu, Oct 24, 2013 at 1:32 PM, Nov Matake notifications@github.comwrote:

BTW, for what are you using JWE?

— Reply to this email directly or view it on GitHubhttps://github.com/nov/json-jwt/issues/14#issuecomment-26984741 .

Jon de Andrés

Jon de Andrés

nov commented 10 years ago

Your encrypted_data has only 3 dots, though the latest format of JWE should have 4 dots. So what's wrong is your fixture data.

nov commented 10 years ago

For signed and encrypted JWT, you can find exact sample code in README.

nov commented 10 years ago
require 'json/jwt'

claim = {
  iss: 'nov',
  exp: 1.week.from_now,
  nbf: Time.now
}

# No signature, no encryption
jwt = JSON::JWT.new(claim).to_s

# With signiture, no encryption
jws = JSON::JWT.new(claim).sign(key, algorithm) # algorithm is optional. default HS256
jws.to_s # => header.payload.signature

# With signature & encryption
jwe = jws.encrypt(key, algorithm, encryption_method) # algorithm & encryption_method are optional. default RSA1_5 & A128CBC-HS256
jws.to_s # => header.encrypted_key.iv.cipher_text.integrity_value
nov commented 10 years ago

BTW, it's great to hear Xbox uses JOSE so deeply :)

jondeandres commented 10 years ago

Nov, thanks for your example, but that's the way to encrypt the data, no?

How should I decrypt signed and encrypted data?

Thank you!

On Fri, Oct 25, 2013 at 3:47 AM, Nov Matake notifications@github.comwrote:

BTW, it's great to hear Xbox uses JOSE so deeply :)

— Reply to this email directly or view it on GitHubhttps://github.com/nov/json-jwt/issues/14#issuecomment-27050179 .

Jon de Andrés

nov commented 10 years ago

Ah, then try

# decrypt here
jwe = JSON::JWE.decode(jwe_string, your_private_key)

# plain text of JWE is a JWS
jws_string = jwe.to_s

# verify the JWS signature
jws = JSON::JWS.decode(jws_string, microsoft_public_key) 
jondeandres commented 10 years ago

Hi Nov,

we've been able to capture what we think that is the whole packet, but when I decode the token to get the jwe string i get this data: "=S\xD7\x96\xAB8\x10\xFC\xA2\xB9\x87\x1C\x1Em\xA2\x18$\x86 \xD2\xCB=\x06a,a0\x1E\a\xC2\xD7\xAF\xC4\xEE\xD9\xC7\x92ZU\xD5\xD5\xADzv\xA5:;R\x9F\xBA\xDF\x89\x1CPOw\xAF\xA5]\t\x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xD7\xEE\xDB]\x1F\xC4\x1F\xFD\xFD\xF8\xBD'\x8F\xAC\x18\xE1\xF8\xADL\xA5\e1h\xC8\xA7K\xB9\xBB\xEC/\x8CN\xCF\xF1\xC6\xC2\xE1\x94\xECI\xA8\xA1\xCC0\xCBB\x01l\xEA\xEC\xE6\xFEe\a\xC5\a\xFB\xD7\xF6\x8D\xDC\x9AO9-\xF6\xBA:\x05\xFAr\x88\xF0p\xC5\xBF\xFB\xCF\x03\x97\xF7\xC3\xF1\x1F"

Something is wrong here...but i don't know if it's a problem with encoding or what. I got this output with JSON::JWT.decode(encrypted_data, subject.default_private_key).to_s.

Thanks!

On Fri, Oct 25, 2013 at 10:45 AM, Nov Matake notifications@github.comwrote:

Ah,

jwe = JSON::JWE.decode(jwe_string, your_private_key)jws_string = jwe.to_s jws = JSON::JWS.decode(jws_string, microsoft_public_key)

— Reply to this email directly or view it on GitHubhttps://github.com/nov/json-jwt/issues/14#issuecomment-27074376 .

Jon de Andrés

nov commented 10 years ago

Hum... is the JWE input binary data? Without the raw JWE nor the private key, debugging is almost impossible :(

jondeandres commented 10 years ago

The token we receive is this one: eyJlbmMiOiJBMTI4Q0JDK0hTMjU2IiwiYWxnIjoiUlNBLU9BRVAiLCJjdHkiOiJKV1QiLCJ6aXAiOiJERUYiLCJ4NXQiOiJfTXU5V3JJb2Yyb0w5b3BzUFhTYjd0RE1IMzAifQ.zblV1FW_6U_G_aEgvtL1cSGpZIc21WxiOMZWtwRgC-OLIWgzsCjYqQF5Cec8LBjU1DJixgwA2Mn9uO8c3rPxmVRC4nhFXHbr14zPnM0V3jY8YQE2nPX2gbGVAjEbB2cmRDhQkddJWeNQK8_RgD3pDviFsavWuGFWker1xS9Fn3n3pR21tIZgQnj6_ZFNzXTzWcIsCn6UExq0zghe7ztWohstK5_U30c_uB17lrrOVevk1TTs8B489wxMyNtsc0mn4SY0Rxk3yFoV541gWHQTmqKxe7KPakaY_1KkQk8S30Tn35EYavzoVktfu7SU5a51ZENBc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8z30b5rhlo7TkO4Q86P09lFbVk_QHFRDYiYjSq4MKGyWNEKUEjMlPpQHXcoMAbuGLXeNSUpjcseCCbGUCPuBY2oGW3ThMvwSJYyzbgjs4jE9aj6iOcf77qyLkCi7wWW7SPWnUwdLvfXyhJaHmuaaGuVzxi4vy7YkPuNz6Ew95GLL1RR.ESaY6ZcilPYcm3Mpz3AyKzmqK4JLl3JDhx2ZCZuXygo

It seems to be correct, although finally they are not using A128CBC-HS256 and they are using A128CBC+HS256, so i'm doing this tests with your 0.5.6 version of the gem. It's strange cause the output should be in base64 no?

Thanks for this Nov ;-).

On Wed, Oct 30, 2013 at 1:36 PM, Nov Matake notifications@github.comwrote:

Hum... is the JWE input binary data? Without the raw JWE nor the private key, debugging is almost impossible :(

— Reply to this email directly or view it on GitHubhttps://github.com/nov/json-jwt/issues/14#issuecomment-27385095 .

Jon de Andrés

nov commented 10 years ago

Ah, it's compressed. This gem isn't supporting compressed JWE yet.

nov@tov ~$ irb
irb(main):001:0> require 'url_safe_base64'
=> true
irb(main):002:0> UrlSafeBase64.decode64 'eyJlbmMiOiJBMTI4Q0JDK0hTMjU2IiwiYWxnIjoiUlNBLU9BRVAiLCJjdHkiOiJKV1QiLCJ6aXAiOiJERUYiLCJ4NXQiOiJfTXU5V3JJb2Yyb0w5b3BzUFhTYjd0RE1IMzAifQ'
=> "{\"enc\":\"A128CBC+HS256\",\"alg\":\"RSA-OAEP\",\"cty\":\"JWT\",\"zip\":\"DEF\",\"x5t\":\"_Mu9WrIof2oL9opsPXSb7tDMH30\"}"
nov commented 10 years ago

Probably you can get the unzipped data by just simply unzip jwe.to_s output.

jondeandres commented 10 years ago

Hi!

I'll try to unzip the data, thank you Nov!!!! I tell you if it works ;-). On Oct 30, 2013 1:59 PM, "Nov Matake" notifications@github.com wrote:

Probably you can get the unzipped data by just simply unzip jwe.to_soutput.

— Reply to this email directly or view it on GitHubhttps://github.com/nov/json-jwt/issues/14#issuecomment-27386524 .

bensw commented 10 years ago

Figured out the solution to being able to unzip the data.

From the spec, JWE's use DEFLATE, which are headerless and checksumless. In order to have Zlib ignore these things you need to set the window size of the inflate stream to -15.

def inflate_jwe(jwe_string)
  zstream = Zlib::Inflate.new(-15)
  buf = zstream.inflate(string)
  zstream.finish
  zstream.close
  buf
end

inflate_jwe(jwe.to_s)

In this particular case (MSFT tokens) it is still necessary to use version 0.5.6 of this library (because of the dependency on A128CBC+HS256).

Hopefully this helps others who run into this specific use case!

nov commented 10 years ago

:+1:

kkarras commented 9 years ago

@bensw I believe I'm running into the same thing with a MSFT JWE.

Been trying to decrypt but not having success.

Should I still try and use older version of this gem and decompress as mentioned above?

alexandru-calinoiu commented 9 years ago

I was facing a similar problem, using version 0.5.6 of the gem, my code is:

def inflate_jwe(jwe_string)
  zstream = Zlib::Inflate.new(-15)
  buf = zstream.inflate(jwe_string)
  zstream.finish
  zstream.close

  buf
end

def decrypt!(data)
  jwe = JSON::JWT.decode(data, default_private_key).to_s
  inflate_jwe(jwe)
end

output = UrlSafeBase64.decode64(decrypt!(data))

The part that I was missing was to decode64 the data after it was inflated.