Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set salt when use pwd_hash #688

Merged
merged 2 commits into from
Sep 3, 2024
Merged
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
4 changes: 2 additions & 2 deletions common/password-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pbkdf2_sha256_derive_key (const char *data_in, int in_len,
{
int iteration = params->iteration;

unsigned char salt_bin[32];
unsigned char salt_bin[32] = {0};
hex_to_rawdata (salt, salt_bin, 32);

PKCS5_PBKDF2_HMAC (data_in, in_len,
Expand Down Expand Up @@ -110,7 +110,7 @@ argon2id_derive_key (const char *data_in, int in_len,
Argon2idParams *params,
unsigned char *key)
{
unsigned char salt_bin[32];
unsigned char salt_bin[32] = {0};
hex_to_rawdata (salt, salt_bin, 32);

argon2id_hash_raw(params->time_cost, params->memory_cost, params->parallelism,
Expand Down
4 changes: 2 additions & 2 deletions common/rpc-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ seafile_change_repo_passwd (const char *repo_id,
}

if (repo->pwd_hash_algo) {
if (seafile_pwd_hash_verify_repo_passwd (repo_id, old_passwd, repo->salt,
if (seafile_pwd_hash_verify_repo_passwd (repo->enc_version, repo_id, old_passwd, repo->salt,
repo->pwd_hash, repo->pwd_hash_algo, repo->pwd_hash_params) < 0) {
g_set_error (error, SEAFILE_DOMAIN, SEAF_ERR_BAD_ARGS, "Incorrect password");
return -1;
Expand All @@ -1072,7 +1072,7 @@ seafile_change_repo_passwd (const char *repo_id,
char new_magic[65], new_pwd_hash[65], new_random_key[97];

if (repo->pwd_hash_algo) {
seafile_generate_pwd_hash (repo_id, new_passwd, repo->salt,
seafile_generate_pwd_hash (repo->enc_version, repo_id, new_passwd, repo->salt,
repo->pwd_hash_algo, repo->pwd_hash_params, new_pwd_hash);
} else {
seafile_generate_magic (repo->enc_version, repo_id, new_passwd, repo->salt,
Expand Down
24 changes: 20 additions & 4 deletions common/seafile-crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ seafile_generate_magic (int version, const char *repo_id,
}

void
seafile_generate_pwd_hash (const char *repo_id,
seafile_generate_pwd_hash (int version,
const char *repo_id,
const char *passwd,
const char *repo_salt,
const char *algo,
Expand All @@ -174,7 +175,14 @@ seafile_generate_pwd_hash (const char *repo_id,
*/
g_string_append_printf (buf, "%s%s", repo_id, passwd);

pwd_hash_derive_key (buf->str, buf->len, repo_salt, algo, params_str, key);
if (version <= 2) {
// use fixed repo salt
char fixed_salt[64] = {0};
rawdata_to_hex(salt, fixed_salt, 8);
pwd_hash_derive_key (buf->str, buf->len, fixed_salt, algo, params_str, key);
} else {
pwd_hash_derive_key (buf->str, buf->len, repo_salt, algo, params_str, key);
}

g_string_free (buf, TRUE);
rawdata_to_hex (key, pwd_hash, 32);
Expand Down Expand Up @@ -214,7 +222,8 @@ seafile_verify_repo_passwd (const char *repo_id,
}

int
seafile_pwd_hash_verify_repo_passwd (const char *repo_id,
seafile_pwd_hash_verify_repo_passwd (int version,
const char *repo_id,
const char *passwd,
const char *repo_salt,
const char *pwd_hash,
Expand All @@ -227,7 +236,14 @@ seafile_pwd_hash_verify_repo_passwd (const char *repo_id,

g_string_append_printf (buf, "%s%s", repo_id, passwd);

pwd_hash_derive_key (buf->str, buf->len, repo_salt, algo, params_str, key);
if (version <= 2) {
// use fixed repo salt
char fixed_salt[64] = {0};
rawdata_to_hex(salt, fixed_salt, 8);
pwd_hash_derive_key (buf->str, buf->len, fixed_salt, algo, params_str, key);
} else {
pwd_hash_derive_key (buf->str, buf->len, repo_salt, algo, params_str, key);
}

g_string_free (buf, TRUE);
rawdata_to_hex (key, hex, 32);
Expand Down
6 changes: 4 additions & 2 deletions common/seafile-crypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ seafile_generate_magic (int version, const char *repo_id,
char *magic);

void
seafile_generate_pwd_hash (const char *repo_id,
seafile_generate_pwd_hash (int version,
const char *repo_id,
const char *passwd,
const char *repo_salt,
const char *algo,
Expand All @@ -93,7 +94,8 @@ seafile_verify_repo_passwd (const char *repo_id,
const char *repo_salt);

int
seafile_pwd_hash_verify_repo_passwd (const char *repo_id,
seafile_pwd_hash_verify_repo_passwd (int version,
const char *repo_id,
const char *passwd,
const char *repo_salt,
const char *pwd_hash,
Expand Down
2 changes: 1 addition & 1 deletion server/passwd-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ seaf_passwd_manager_set_passwd (SeafPasswdManager *mgr,
}

if (repo->pwd_hash_algo) {
if (seafile_pwd_hash_verify_repo_passwd (repo->id, passwd, repo->salt,
if (seafile_pwd_hash_verify_repo_passwd (repo->enc_version, repo->id, passwd, repo->salt,
repo->pwd_hash, repo->pwd_hash_algo, repo->pwd_hash_params) < 0) {
seaf_repo_unref (repo);
g_set_error (error, SEAFILE_DOMAIN, SEAF_ERR_GENERAL,
Expand Down
2 changes: 1 addition & 1 deletion server/repo-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3945,7 +3945,7 @@ seaf_repo_manager_create_new_repo (SeafRepoManager *mgr,
goto bad;
}
if (algo != NULL) {
seafile_generate_pwd_hash (repo_id, passwd, salt, algo, params, pwd_hash);
seafile_generate_pwd_hash (enc_version, repo_id, passwd, salt, algo, params, pwd_hash);
} else {
seafile_generate_magic (enc_version, repo_id, passwd, salt, magic);
}
Expand Down
5 changes: 3 additions & 2 deletions server/virtual-repo.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ do_create_virtual_repo (SeafRepoManager *mgr,
if (origin_repo->pwd_hash_params)
repo->pwd_hash_params = g_strdup (origin_repo->pwd_hash_params);
if (repo->pwd_hash_algo) {
seafile_generate_pwd_hash (repo_id, passwd, repo->salt,
seafile_generate_pwd_hash (repo->enc_version, repo_id, passwd, repo->salt,
repo->pwd_hash_algo, repo->pwd_hash_params, repo->pwd_hash);
memcpy (repo->magic, repo->pwd_hash, 32);
} else
Expand Down Expand Up @@ -231,7 +231,8 @@ create_virtual_repo_common (SeafRepoManager *mgr,
}

if (origin_repo->pwd_hash_algo) {
if (seafile_pwd_hash_verify_repo_passwd (origin_repo_id,
if (seafile_pwd_hash_verify_repo_passwd (origin_repo->enc_version,
origin_repo_id,
passwd,
origin_repo->salt,
origin_repo->pwd_hash,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_password/test_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,35 @@ def test_encrypted_repo(rpc, enc_version):
assert api.is_password_set(repo.id, USER) == 0

api.remove_repo(repo_id)

@pytest.mark.parametrize('rpc, enc_version, algo, params',
[('create_repo', 2, 'pbkdf2_sha256', '1000'), ('create_repo', 3, 'pbkdf2_sha256', '1000'), ('create_repo', 4, 'pbkdf2_sha256', '1000'),
('create_repo', 2, 'argon2id', '2,102400,8'), ('create_repo', 3, 'argon2id', '2,102400,8'), ('create_repo', 4, 'argon2id', '2,102400,8')])
def test_pwd_hash(rpc, enc_version, algo, params):
test_repo_name = 'test_enc_repo'
test_repo_desc = 'test_enc_repo'
test_repo_passwd = 'test_enc_repo'
repo_id = api.create_repo(test_repo_name, test_repo_desc, USER,
test_repo_passwd, enc_version, pwd_hash_algo=algo, pwd_hash_params=params)
assert repo_id

repo = api.get_repo(repo_id)
assert repo
assert repo.enc_version == enc_version
assert len(repo.pwd_hash) == 64
assert len(repo.random_key) == 96
if enc_version > 2:
assert len(repo.salt) == 64

new_passwd = 'new password'

assert api.set_passwd(repo.id, USER, test_repo_passwd) == 0
assert api.get_decrypt_key(repo.id, USER)
api.change_repo_passwd(repo.repo_id, test_repo_passwd, new_passwd, USER) == 0
assert api.set_passwd(repo.id, USER, new_passwd) == 0

assert api.is_password_set(repo.id, USER)
assert api.unset_passwd(repo.id, USER) == 0
assert api.is_password_set(repo.id, USER) == 0

api.remove_repo(repo_id)
Loading