Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/cose/cwt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule COSE.CWT do
end

def peek_claims(token, custom_claims \\ %{}) do
msg = Sign1.decode(token)
{:ok, msg} = Sign1.decode_cbor(token)
{:ok, cbor_claims, ""} = CBOR.decode(msg.payload.value)

cbor_claims
Expand Down
8 changes: 4 additions & 4 deletions lib/cose/keys/ecc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule COSE.Keys.ECC do
{curve, cose_crv, key_len} = get_curve_info(alg)
{pub, priv} = :crypto.generate_key(:ecdh, curve)

<<4, x::binary-size(key_len), y::binary-size(key_len)>> = pub
<<4, x::binary-size(^key_len), y::binary-size(^key_len)>> = pub

%__MODULE__{
kty: :ecc,
Expand Down Expand Up @@ -70,7 +70,7 @@ defmodule COSE.Keys.ECC do
end

case final_pub do
<<4, x::binary-size(key_len), y::binary-size(key_len)>> ->
<<4, x::binary-size(^key_len), y::binary-size(^key_len)>> ->
%__MODULE__{
kty: :ecc,
crv: cose_crv,
Expand Down Expand Up @@ -128,7 +128,7 @@ defmodule COSE.Keys.ECC do

defp bitstring_to_binary(val) when is_bitstring(val) do
size = bit_size(val)
<<bin::binary-size(div(size, 8)), _::bitstring>> = val
<<bin::binary-size(div(^size, 8)), _::bitstring>> = val

bin
end
Expand Down Expand Up @@ -183,7 +183,7 @@ defimpl COSE.Keys.Key, for: COSE.Keys.ECC do

def raw_to_der(raw_sig, key_size) do
case raw_sig do
<<r::binary-size(key_size), s::binary-size(key_size)>> ->
<<r::binary-size(^key_size), s::binary-size(^key_size)>> ->
(encode_integer(r) <> encode_integer(s))
|> wrap_sequence()
|> then(&{:ok, &1})
Expand Down
4 changes: 0 additions & 4 deletions lib/cose/keys/keys.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ defmodule COSE.Keys do
alias COSE.Keys.RSA

@oid_rsa {1, 2, 840, 113_549, 1, 1, 1}
@oid_ec {1, 2, 840, 10045, 2, 1}

def sign(key, digest_type, to_be_signed), do: Key.sign(key, digest_type, to_be_signed)

Expand Down Expand Up @@ -68,9 +67,6 @@ defmodule COSE.Keys do
{:ECPrivateKey, _, _, _, _, _} ->
{:ok, ECC.from_record(pem_record)}

{:PrivateKeyInfo, _, {_, @oid_ec, _}, _, _} ->
{:ok, ECC.from_record(pem_record)}

_ ->
false
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cose/messages/encrypt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defmodule COSE.Messages.Encrypt do

def split_encrypted_tag(ciphertext, tag_len \\ 8) do
encrypted_len = byte_size(ciphertext) - tag_len
<<encrypted::binary-size(encrypted_len), tag::binary-size(tag_len)>> = ciphertext
<<encrypted::binary-size(^encrypted_len), tag::binary-size(^tag_len)>> = ciphertext
{encrypted, tag}
end
end
2 changes: 1 addition & 1 deletion lib/cose/messages/encrypt0.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ defmodule COSE.Messages.Encrypt0 do

def split_encrypted_tag(ciphertext, tag_len \\ 16) do
encrypted_len = byte_size(ciphertext) - tag_len
<<encrypted::binary-size(encrypted_len), tag::binary-size(tag_len)>> = ciphertext
<<encrypted::binary-size(^encrypted_len), tag::binary-size(^tag_len)>> = ciphertext
{encrypted, tag}
end
end
7 changes: 7 additions & 0 deletions test/cwt_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,12 @@ defmodule COSETest.CWT do
retrived_claims = cwt |> Map.delete(:issued_at)
assert ^claims = retrived_claims
end

test "peek claims without verifying the signature", %{key: key, claims: claims} do
{:ok, token} = CWT.sign_encode_cbor(claims, key)

retrived_claims = CWT.peek_claims(token) |> Map.delete(:issued_at)
assert ^claims = retrived_claims
end
end
end