Ensure that local and remote keys have secure permissions

Merge after #403

Fixes #382