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

Repair EC x/y coordinates when importing JWK #585

Merged
merged 8 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Fix key base equality and spaceship operators [#569](https://github.com/jwt/ruby-jwt/pull/569) - [@magneland](https://github.com/magneland).
- Remove explicit base64 require from x5c_key_finder [#580](https://github.com/jwt/ruby-jwt/pull/580) - [@anakinj](https://github.com/anakinj).
- Performance improvements and cleanup of tests [#581](https://github.com/jwt/ruby-jwt/pull/581) - [@anakinj](https://github.com/anakinj).
- Repair EC x/y coordinates when importing JWK [#585](https://github.com/jwt/ruby-jwt/pull/585) - [@julik](https://github.com/julik).
- Your contribution here

## [v2.7.1](https://github.com/jwt/ruby-jwt/tree/v2.8.0) (2023-06-09)
Expand Down
29 changes: 22 additions & 7 deletions lib/jwt/jwk/ec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class EC < KeyBase # rubocop:disable Metrics/ClassLength
EC_PUBLIC_KEY_ELEMENTS = %i[kty crv x y].freeze
EC_PRIVATE_KEY_ELEMENTS = %i[d].freeze
EC_KEY_ELEMENTS = (EC_PRIVATE_KEY_ELEMENTS + EC_PUBLIC_KEY_ELEMENTS).freeze
ZERO_BYTE = "\0".b.freeze

def initialize(key, params = nil, options = {})
params ||= {}
Expand Down Expand Up @@ -143,7 +144,6 @@ def parse_ec_key(key)
if ::JWT.openssl_3?
def create_ec_key(jwk_crv, jwk_x, jwk_y, jwk_d) # rubocop:disable Metrics/MethodLength
curve = EC.to_openssl_curve(jwk_crv)

x_octets = decode_octets(jwk_x)
y_octets = decode_octets(jwk_y)

Expand Down Expand Up @@ -205,12 +205,27 @@ def create_ec_key(jwk_crv, jwk_x, jwk_y, jwk_d)
end
end

def decode_octets(jwk_data)
::JWT::Base64.url_decode(jwk_data)
end

def decode_open_ssl_bn(jwk_data)
OpenSSL::BN.new(::JWT::Base64.url_decode(jwk_data), BINARY)
def decode_octets(base64_encoded_coordinate)
bytes = ::JWT::Base64.url_decode(base64_encoded_coordinate)
# Some base64 encoders on some platform omit a single 0-byte at
# the start of either Y or X coordinate of the elliptic curve point.
# This leads to an encoding error when data is passed to OpenSSL BN.
# It is know to have happend to exported JWKs on a Java application and
# on a Flutter/Dart application (both iOS and Android). All that is
# needed to fix the problem is adding a leading 0-byte. We know the
# required byte is 0 because with any other byte the point is no longer
# on the curve - and OpenSSL will actually communicate this via another
# exception. The indication of a stripped byte will be the fact that the
# coordinates - once decoded into bytes - should always be an even
# bytesize. For example, with a P-521 curve, both x and y must be 66 bytes.
# With a P-256 curve, both x and y must be 32 and so on. The simplest way
# to check for this truncation is thus to check whether the number of bytes
# is odd, and restore the leading 0-byte if it is.
if bytes.bytesize.odd?
ZERO_BYTE + bytes
else
bytes
end
end

class << self
Expand Down
23 changes: 23 additions & 0 deletions spec/jwt/jwk/ec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@
end
end
end

context 'with missing 0-byte at the start of EC coordinates' do
let(:example_keysets) do
[
'{"kty":"EC","crv":"P-256","x":"0Nv5IKAlkvXuAKmOmFgmrwXKR7qGePOzu_7RXg5msw","y":"FqnPSNutcjfvXNlufwb7nLJuUEnBkbMdZ3P79nY9c3k"}',
'{"kty":"EC","crv":"P-256","x":"xGjPg-7meZamM_yfkGeBUB2eJ5c82Y8vQdXwi5cVGw","y":"9FwKAuJacVyEy71yoVn1u1ETsQoiwF7QfkfXURGxg14"}',
'{"kty":"EC","crv":"P-256","x":"yTvy0bwt5s29mIg1DMq-IjZH4pDgZIN9keEEaSuWZhk","y":"a0nrmd8qz8jpZDgpY82Rgv3vZ5xiJuiAoMIuRlGnaw"}',
'{"kty":"EC","crv":"P-256","x":"yJen7AW4lLUTMH4luDj0wlMNSGCuOBB5R-ZoxlAU_g","y":"aMbA-M6ORHePSatiPVz_Pzu7z2XRnKMzK-HIscpfud8"}',
'{"kty":"EC","crv":"P-256","x":"p_D00Z1ydC7mBIpSKPUUrzVzY9Fr5NMhhGfnf4P9guw","y":"lCqM3B_s04uhm7_91oycBvoWzuQWJCbMoZc46uqHXA"}',
'{"kty":"EC","crv":"P-256","x":"hKS-vxV1bvfZ2xOuHv6Qt3lmHIiArTnhWac31kXw3w","y":"f_UWjrTpmq_oTdfss7YJ-9dEiYw_JC90kwAE-y0Yu-w"}',
'{"kty":"EC","crv":"P-256","x":"3W22hN16OJN1XPpUQuCxtwoBRlf-wGyBNIihQiTmSdI","y":"eUaveaPQ4CpyfY7sfCqEF1DCOoxHdMpPHW15BmUF0w"}',
'{"kty":"EC","crv":"P-256","x":"oq_00cGL3SxUZTA-JvcXALhfQya7elFuC7jcJScN7Bs","y":"1nNPIinv_gQiwStfx7vqs7Vt_MSyzoQDy9sCnZlFfg"}',
'{"crv":"P-521","kty":"EC","x":"AMNQr/q+YGv4GfkEjrXH2N0+hnGes4cCqahJlV39m3aJpqSK+uiAvkRE5SDm2bZBc3YHGzhDzfMTUpnvXwjugUQP","y":"fIwouWsnp44Fjh2gBmO8ZafnpXZwLOCoaT5itu/Q4Z6j3duRfqmDsqyxZueDA3Gaac2LkbWGplT7mg4j7vCuGsw="}'
]
end

it 'prepends a 0-byte to either X or Y coordinate so that the keys decode correctly' do
example_keysets.each do |keyset_json|
jwk = described_class.import(JSON.parse(keyset_json))
expect(jwk).to be_kind_of(JWT::JWK::EC)
end
end
end
end
end
end
Loading