Skip to content

Commit b4b2052

Browse files
committed
Fix EC key conversion for OTP 28, resolves #180, resolves #181, resolves #182, resolves #179
1 parent b635b6d commit b4b2052

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/jose_public_key.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ der_decode(DER) when is_binary(DER) ->
144144
Other
145145
end.
146146

147+
der_decode('EcpkParameters', EcpkParameters = {namedCurve, _}) ->
148+
EcpkParameters;
147149
der_decode(ASN1Type, DER) when is_atom(ASN1Type) andalso is_binary(DER) ->
148150
public_key:der_decode(ASN1Type, DER).
149151

src/jwk/jose_jwk_kty_ec.erl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,26 @@
5050

5151
-export_type([key/0]).
5252

53+
%% Macros
54+
-ifdef(OTP_RELEASE).
55+
-if(?OTP_RELEASE >= 28).
56+
-define(EC_PRIVATE_KEY_VERSION, ecPrivkeyVer1).
57+
-define(EC_PRIVATE_KEY_VERSION_COMPAT, 1).
58+
-else.
59+
-define(EC_PRIVATE_KEY_VERSION, 1).
60+
-define(EC_PRIVATE_KEY_VERSION_COMPAT, ecPrivkeyVer1).
61+
-endif.
62+
-else.
63+
-define(EC_PRIVATE_KEY_VERSION, 1).
64+
-define(EC_PRIVATE_KEY_VERSION_COMPAT, ecPrivkeyVer1).
65+
-endif.
66+
5367
%%====================================================================
5468
%% jose_jwk callbacks
5569
%%====================================================================
5670

5771
from_map(F = #{ <<"kty">> := <<"EC">>, <<"d">> := _ }) ->
58-
from_map_ec_private_key(jose_jwa:ec_key_mode(), maps:remove(<<"kty">>, F), #'ECPrivateKey'{ version = 1 });
72+
from_map_ec_private_key(jose_jwa:ec_key_mode(), maps:remove(<<"kty">>, F), #'ECPrivateKey'{ version = ?EC_PRIVATE_KEY_VERSION });
5973
from_map(F = #{ <<"kty">> := <<"EC">> }) ->
6074
from_map_ec_public_key(maps:remove(<<"kty">>, F), {#'ECPoint'{}, undefined}).
6175

@@ -65,7 +79,7 @@ to_key(ECPublicKey={#'ECPoint'{}, _}) ->
6579
ECPublicKey.
6680

6781
to_map(#'ECPrivateKey'{
68-
version = 1,
82+
version = ?EC_PRIVATE_KEY_VERSION,
6983
privateKey = D,
7084
parameters = {namedCurve, Parameters},
7185
publicKey = PublicKey}, Fields) when is_binary(D) andalso is_binary(PublicKey) ->
@@ -88,13 +102,16 @@ to_map({#'ECPoint'{
88102
<<"y">> => jose_jwa_base64url:encode(Y)
89103
};
90104
to_map(ECPrivateKey0=#'ECPrivateKey'{
91-
version = 1,
105+
version = ?EC_PRIVATE_KEY_VERSION,
92106
privateKey = D,
93107
parameters = {namedCurve, _Parameters},
94108
publicKey = {_, PublicKey}}, Fields) when is_list(D) andalso is_binary(PublicKey) ->
95109
ECPrivateKey = ECPrivateKey0#'ECPrivateKey'{
96110
privateKey = list_to_binary(D),
97111
publicKey = PublicKey},
112+
to_map(ECPrivateKey, Fields);
113+
to_map(ECPrivateKey0=#'ECPrivateKey'{version = ?EC_PRIVATE_KEY_VERSION_COMPAT}, Fields) ->
114+
ECPrivateKey = ECPrivateKey0#'ECPrivateKey'{version = ?EC_PRIVATE_KEY_VERSION},
98115
to_map(ECPrivateKey, Fields).
99116

100117
to_public_map(K=#'ECPrivateKey'{}, F) ->

0 commit comments

Comments
 (0)