17

I'm new to backend and currently don't know best practices of storing sensitive data in safe.

I configured node.js server with JWT. I signed JWT with RSA key. Each time server receive request it verifies integrity of JWT.

For now I have 2 files private.key and public.key. The first file is used for signing JWT and the second one is used for verifing JWT.

So the question is.

Where should I store these keys (files)? Currently, I store them beside the project source files but do not push them to the remote source control system. Whenever I need content of the keys I just use readFileSync so it requires files to be present in the project.

Do I need to encrypt these files too? or...
Do I need to push these files to the remove service and use them via HTTP? or..
Is there any service where I can put my files and use some URL to get access to the files?

2 Answers 2

14

Public key: that's the easy part, it can be almost anywhere (known URL, some type of file store like S3/GCS/..., even source control). The only concern is to make sure it is not modified, but it can be read by anyone.

Private key: this is where it gets tricky. The private key should be as safe as possible with access as limited as possible. If someone reads your private key, they can pretend to be your service. You are correct that it must not be in source control.

You'll want to make sure the private key is always encrypted. The issue now becomes how to give your service the passphrase to decode the key. The encrypted file itself can be in a cloud storage system (S3, GCS, etc..). Some access controls on the encrypted file is still a good idea.

Some ways to access the passphrase (definitely not exhaustive). Each has pros and cons. Which to use depends a lot on your environment and balancing automation vs security.

  • environment variable: many deployment systems allow you to specify environment variables in a way that is not easily read by anyone but administrators of the system. This might not be safe depending on the production environment and who has access to the VM/orchestration system.

  • manage your own: use your own infrastructure to distribute secrets. Things like keywhiz and Vault can be very powerful, but these are not simple systems to deploy and manage.

  • cloud KMS: make use of the cloud-specific Key Management System (eg: Google/AWS KMS, Azure Vault, etc...). They usually cannot store a file, but they can store a key with which to encrypt a file. The permissions on key usage should be very restricted, this can usually be done per role account and assigned only to your service.

  • human interaction required: require that someone manually login to the system and decrypt the private key for use by the backend. The obvious downside is that there is no automation there, if your VM goes down it cannot be automatically restarted.

3

One option to add to Marc's answer would be a hardware security module (HSM). These are expensive and not always practical (cloud providers may offer HSM-backed key storage but you can't just attach your own HSM). However, they can be made more secure than a key file. The HSM need not ever divulge the key; instead, when some program (your server) wants to use the key, it authenticates to the HSM (which means you may still have to provide a credential to the program) and then passes some data (such as the JWT body) to the HSM and says "Sign this, please". Obviously, any other program running on the same hardware that can spoof your program's authentication could also sign forged JWTs (or other data), but they couldn't abscond with the key itself. The HSM could be physically removed, but physical security is usually easier than software security.

A simpler option, available with some operating systems and/or hardware, is to use a highly secure software security module (which may be backed by hardware features). There are various examples of such things, from pure-software implementations (Windows has one accessible via its default crypto API, where your key is stored in a highly privileged process that other processes can only access via authenticated channels) to hardware-backed options using a "secure enclave" or similar where a highly-trusted program can run and store data in such a way that even a fully-compromised OS cannot, in theory, extract secrets from it.

2
  • As a comment, 'can't just attach your own HSM', yes you can. You can use an HSM on-premise and talk to it from your cloud applications. You can use lease an HSM from the Cloud Service Provider, or you can lease an "HSMaaS" box from a 3d party provider and use that.
    – rip...
    May 7, 2020 at 2:51
  • Leasing an HSM means you're not attaching your own HSM, but assuming you trust the lessor it's a valid option. Remote HSMs miss most of the benefits of using an HSM (instead of a software solution) in the first place, though; the HSM server becomes its own target for attacks, the software securing the connection to the HSM becomes a weaker link than the HSM instead so you might as well just use local software-based security, and you need to store a key to authenticate to the HSM but where do you put that? Still, yes, you can combine HSM with cloud services if you really want to.
    – CBHacking
    May 7, 2020 at 8:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .