Skip to content

Commit

Permalink
Merge branch 'ingela/public_key/implicit-pss-default/OTP-19179' into …
Browse files Browse the repository at this point in the history
…maint

* ingela/public_key/implicit-pss-default/OTP-19179:
  public_key: Add handling of implicit default for RSASSA-PSS keys
  • Loading branch information
IngelaAndin committed Aug 8, 2024
2 parents 4cff317 + b9d1950 commit ce5d6fc
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
42 changes: 30 additions & 12 deletions lib/public_key/src/public_key.erl
Original file line number Diff line number Diff line change
Expand Up @@ -620,19 +620,32 @@ der_priv_key_decode(#'OneAsymmetricKey'{
#'ECPrivateKey'{version = 2, parameters = {namedCurve, CurveOId}, privateKey = PrivKey,
attributes = Attr,
publicKey = PubKey};
der_priv_key_decode({'PrivateKeyInfo', v1,
{'PrivateKeyInfo_privateKeyAlgorithm', ?'rsaEncryption', _}, PrivKey, _}) ->
der_decode('RSAPrivateKey', PrivKey);
der_priv_key_decode({'PrivateKeyInfo', v1,
{'PrivateKeyInfo_privateKeyAlgorithm', ?'id-RSASSA-PSS',
{asn1_OPENTYPE, Parameters}}, PrivKey, _}) ->
der_priv_key_decode(#'PrivateKeyInfo'{version = v1,
privateKeyAlgorithm =
#'PrivateKeyInfo_privateKeyAlgorithm'{algorithm = ?'rsaEncryption'},
privateKey = PrivKey}) ->
der_decode('RSAPrivateKey', PrivKey);
der_priv_key_decode(#'PrivateKeyInfo'{version = v1,
privateKeyAlgorithm =
#'PrivateKeyInfo_privateKeyAlgorithm'{algorithm = ?'id-RSASSA-PSS',
parameters = {asn1_OPENTYPE, Parameters}},
privateKey = PrivKey}) ->
Key = der_decode('RSAPrivateKey', PrivKey),
Params = der_decode('RSASSA-PSS-params', Parameters),
{Key, Params};
der_priv_key_decode(#'PrivateKeyInfo'{version = v1,
privateKeyAlgorithm = #'PrivateKeyInfo_privateKeyAlgorithm'{algorithm = ?'id-dsa',
parameters =
{asn1_OPENTYPE, Parameters}},
privateKeyAlgorithm =
#'PrivateKeyInfo_privateKeyAlgorithm'{algorithm = ?'id-RSASSA-PSS',
parameters = asn1_NOVALUE},
privateKey = PrivKey}) ->
Key = der_decode('RSAPrivateKey', PrivKey),
#'RSASSA-AlgorithmIdentifier'{parameters = Params} = ?'rSASSA-PSS-Default-Identifier',
{Key, Params};
der_priv_key_decode(#'PrivateKeyInfo'{version = v1,
privateKeyAlgorithm =
#'PrivateKeyInfo_privateKeyAlgorithm'{algorithm = ?'id-dsa',
parameters =
{asn1_OPENTYPE, Parameters}},
privateKey = PrivKey}) ->
{params, #'Dss-Parms'{p=P, q=Q, g=G}} = der_decode('DSAParams', Parameters),
X = der_decode('Prime-p', PrivKey),
Expand Down Expand Up @@ -668,10 +681,15 @@ der_encode('PrivateKeyInfo', #'RSAPrivateKey'{} = PrivKey) ->
privateKeyAlgorithm = Alg,
privateKey = Key});
der_encode('PrivateKeyInfo', {#'RSAPrivateKey'{} = PrivKey, Parameters}) ->
Params = der_encode('RSASSA-PSS-params', Parameters),
#'RSASSA-AlgorithmIdentifier'{parameters = DefaultParams} = ?'rSASSA-PSS-Default-Identifier',
Params = case Parameters of
DefaultParams ->
asn1_NOVALUE;
_ ->
{asn1_OPENTYPE, der_encode('RSASSA-PSS-params', Parameters)}
end,
Alg = #'PrivateKeyInfo_privateKeyAlgorithm'{algorithm = ?'id-RSASSA-PSS',
parameters =
{asn1_OPENTYPE, Params}},
parameters = Params},
Key = der_encode('RSAPrivateKey', PrivKey),
der_encode('PrivateKeyInfo', #'PrivateKeyInfo'{version = v1,
privateKeyAlgorithm = Alg,
Expand Down
19 changes: 18 additions & 1 deletion lib/public_key/test/public_key_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
rsa_pem/1,
rsa_pss_pss_pem/0,
rsa_pss_pss_pem/1,
rsa_pss_default_pem/0,
rsa_pss_default_pem/1,
rsa_priv_pkcs8/0,
rsa_priv_pkcs8/1,
ec_pem/0,
Expand Down Expand Up @@ -191,7 +193,8 @@ all() ->
].

groups() ->
[{pem_decode_encode, [], [dsa_pem, rsa_pem, rsa_pss_pss_pem, ec_pem,
[{pem_decode_encode, [], [dsa_pem, rsa_pem, rsa_pss_pss_pem,
rsa_pss_default_pem, ec_pem,
encrypted_pem_pwdstring, encrypted_pem_pwdfun,
dh_pem, cert_pem, pkcs7_pem, pkcs10_pem, ec_pem2,
rsa_priv_pkcs8, dsa_priv_pkcs8, ec_priv_pkcs8,
Expand Down Expand Up @@ -375,6 +378,20 @@ rsa_pss_pss_pem(Config) when is_list(Config) ->
RSAPemNoEndNewLines = strip_superfluous_newlines(RsaPem),
RSAPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PrivEntry0])).

rsa_pss_default_pem() ->
[{doc, "RSA PKCS8 RSASSA-PSS private key with default params decode/encode"}].
rsa_pss_default_pem(Config) when is_list(Config) ->
Datadir = proplists:get_value(data_dir, Config),
{ok, RsaPem} = file:read_file(filename:join(Datadir, "pss_default.pem")),
[{'PrivateKeyInfo', DerRSAKey, not_encrypted} = Entry0 ] = public_key:pem_decode(RsaPem),
#'RSASSA-AlgorithmIdentifier'{parameters = Params} = ?'rSASSA-PSS-Default-Identifier',
{RSAKey, Params} = public_key:der_decode('PrivateKeyInfo', DerRSAKey),
{RSAKey, Parms} = public_key:pem_entry_decode(Entry0),
true = check_entry_type(RSAKey, 'RSAPrivateKey'),
PrivEntry0 = public_key:pem_entry_encode('PrivateKeyInfo', {RSAKey, Parms}),
RSAPemNoEndNewLines = strip_superfluous_newlines(RsaPem),
RSAPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PrivEntry0])).

rsa_priv_pkcs8() ->
[{doc, "RSA PKCS8 private key decode/encode"}].
rsa_priv_pkcs8(Config) when is_list(Config) ->
Expand Down
28 changes: 28 additions & 0 deletions lib/public_key/test/public_key_SUITE_data/pss_default.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEuwIBADALBgkqhkiG9w0BAQoEggSnMIIEowI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