Skip to content

Commit

Permalink
crypto, bugfix: set EdDSA to pk.alg error in ed25519.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Apr 20, 2022
1 parent 3b4b396 commit c8d5fc0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
9 changes: 7 additions & 2 deletions fibjs/src/crypto/PKey.cpp
Expand Up @@ -1163,8 +1163,13 @@ result_t PKey::set_alg(exlib::string newVal)
}

if (type == MBEDTLS_PK_ECKEY) {
if (newVal != "ECDSA" && newVal != "ECSDSA")
return CHECK_ERROR(CALL_E_INVALIDARG);
if (mbedtls_pk_ec(m_key)->grp.id == MBEDTLS_ECP_DP_ED25519) {
if (newVal != "EdDSA")
return CHECK_ERROR(CALL_E_INVALIDARG);
} else {
if (newVal != "ECDSA" && newVal != "ECSDSA")
return CHECK_ERROR(CALL_E_INVALIDARG);
}
m_alg = newVal;
return 0;
}
Expand Down
23 changes: 18 additions & 5 deletions test/crypto_test.js
Expand Up @@ -1189,7 +1189,9 @@ MCowBQYDK2VwAyEA11qYAYKxCrfVS/7TyWQHOg7hcvPapiMlrwIaaPcHURo=
});

describe("alg", () => {
function test_alg(alg, pk) {
var all_algs = ['RSA', 'ECDSA', 'SM2', 'ECSDSA', 'EdDSA'];

function test_alg(alg, algs, pk) {
describe(alg, () => {
it("generateKey", () => {
assert.equal(pk.alg, alg);
Expand All @@ -1214,13 +1216,24 @@ MCowBQYDK2VwAyEA11qYAYKxCrfVS/7TyWQHOg7hcvPapiMlrwIaaPcHURo=
it("json import", () => {
assert.equal(new crypto.PKey(pk.json()).alg, alg);
});

it('set alg', () => {
all_algs.forEach(alg => {
if (algs.indexOf(alg) != -1)
pk.alg = alg;
else
assert.throws(() => {
pk.alg = alg;
});
});
});
});
}

test_alg("RSA", crypto.generateKey(512));
test_alg("ECDSA", crypto.generateKey());
test_alg("SM2", crypto.generateKey("SM2"));
test_alg("EdDSA", crypto.generateKey("ed25519"));
test_alg("RSA", ['RSA'], crypto.generateKey(512));
test_alg("ECDSA", ['ECDSA', 'ECSDSA'], crypto.generateKey());
test_alg("SM2", ['SM2', 'ECSDSA'], crypto.generateKey("SM2"));
test_alg("EdDSA", ['EdDSA'], crypto.generateKey("ed25519"));
});

describe("X509 Cert", () => {
Expand Down

0 comments on commit c8d5fc0

Please sign in to comment.