Skip to content

Commit

Permalink
public_key: Handle EDDSA public key decoding correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
IngelaAndin committed Nov 13, 2024
1 parent 0418c10 commit 7098b88
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/public_key/src/public_key.erl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pem_entry_decode({'SubjectPublicKeyInfo', Der, _}) ->
{params, DssParams} = der_decode('DSAParams', Params),
{der_decode(KeyType, Key0), DssParams};
'ECPoint' ->
ECCParams = der_decode('EcpkParameters', Params),
ECCParams = ec_normalize_params(AlgId, Params),
{#'ECPoint'{point = Key0}, ECCParams}
end;
pem_entry_decode({Asn1Type, Der, not_encrypted}) when is_atom(Asn1Type),
Expand Down Expand Up @@ -1755,6 +1755,12 @@ ec_generate_key(Params) ->
NormParams = ec_normalize_params(Params),
ec_key(Term, NormParams).

ec_normalize_params(AlgId, _) when AlgId == ?'id-Ed25519';
AlgId == ?'id-Ed448' ->
{namedCurve, AlgId};
ec_normalize_params(_, Params) ->
der_decode('EcpkParameters', Params).

-spec ec_normalize_params(ecpk_parameters_api()) -> ecpk_parameters().
ec_normalize_params({namedCurve, Name}) when is_atom(Name) ->
{namedCurve, pubkey_cert_records:namedCurves(Name)};
Expand Down
16 changes: 16 additions & 0 deletions lib/public_key/test/public_key_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
eddsa_priv_pkcs8/1,
eddsa_priv_rfc5958/0,
eddsa_priv_rfc5958/1,
eddsa_pub/0,
eddsa_pub/1,
eddsa_sign_verify_24_compat/1,
init_ec_pem_encode_generated/1,
ec_pem_encode_generated/0,
Expand Down Expand Up @@ -457,6 +459,20 @@ eddsa_priv_rfc5958(Config) when is_list(Config) ->
ECPemNoEndNewLines = strip_superfluous_newlines(ECPrivPem),
ECPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PrivEntry0])).

eddsa_pub() ->
[{doc, "EDDSA PKCS8 public key decode/encode"}].
eddsa_pub(Config) when is_list(Config) ->
Datadir = proplists:get_value(data_dir, Config),
{ok, EDDSAPubPem} = file:read_file(filename:join(Datadir, "public_eddsa.pem")),
[{'SubjectPublicKeyInfo', _, not_encrypted} = Key] = PemEntry =
public_key:pem_decode(EDDSAPubPem),
EDDSAPubKey = public_key:pem_entry_decode(PemEntry),
true = check_entry_type(EDDSAPubKey, 'ECPoint'),
{_, {namedCurve, ?'id-Ed25519'}} = EDDSAPubKey,
PrivEntry0 = public_key:pem_entry_encode('SubjectPublicKeyInfo', EDDSAPubKey),
ECPemNoEndNewLines = strip_superfluous_newlines(EDDSAPubPem),
ECPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PemEntry])).

eddsa_sign_verify_24_compat(_Config) ->
Key =
{'ECPrivateKey',1,
Expand Down
3 changes: 3 additions & 0 deletions lib/public_key/test/public_key_SUITE_data/public_eddsa.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAzVMFUvlbihtNisegppBVAct8qRH2Ql3KZ57JAxt8Gms=
-----END PUBLIC KEY-----

0 comments on commit 7098b88

Please sign in to comment.