I need to develop a payment gateway and I've just realized I need to store an HMAC_SHA256_V1 key for authentication operations. The way that this is done is: the browser calls a WCF service that responses with the information signed with my HMAC_SHA256_V1 key and then the browser sends by POST with a request with that info to open the gateway payment page (by redirect). And that key is the same i have to use to check the payment gateway response.

So, I think I can store that key in a configuration row inside a one row SqlServer table, so here is my question: do I need to take any measure such as encrypt the key or take any action in order to protect this information?

  • 2
    Can you add some more details? What is that key used for? And what exactly do you mean with sha256 key? sha is hashing, so is the key already hashed? Or is the hash itself the key (but then why even hash)? – tim Mar 8 '16 at 10:17
  • 1
    I've just added some details in the question, sorry about my english, i hope you can understad well – MirlvsMaximvs Mar 8 '16 at 11:11
  • English seems fine, but the question is missing some important details. I've made some guesses and tried to give you a good answer. Let me know if I got it all wrong. – Neil Smithline Mar 8 '16 at 22:50

I am a bit unclear as to the workflow, so let me know if I'm off.

I believe the situation is that you are communicating with a service. You make a request to the service and it responds, signing the reply with an HMAC. You then verify that the HMAC is correct to ensure both that the request originated from the service (authenticity) and that it hasn't been modified by an intermediary (integrity).

For an HMAC to be effective, the key must be secret. If the key is not secret, then an attacker can perform modify the message or pretend to be the service such that the recipient cannot tell that tell that they are receiving modified messages or not talking with their desired partner. And if you're willing to give up on both integrity and authenticity, there's no point in using the HMAC at all. So, the HMAC key must be secret to be effective.

Exactly what protections are appropriate to keep the key secret is application dependent. For example, storing it in a database table may lead to it getting exposed if your application has a SQL Injection vulnerability. That seems risky to me if you have an architecture that could be vulnerable to SQLi (I would consider any app that takes user input and uses it as part of a SQL query as potentially vulnerable to SQLi). Storing the key in a disk file could be vulnerable to path traversal attack.

In the end, no system will be perfect. You'll have to tune the amount of security to the sensitivity of your application. One trick is to combine multiple security measures by, for example, storing an encrypted HMAC key in the database and storing the key to decrypt it somewhere else (maybe a file outside of your web root). Then an attacker would need to get both the encrypted HMAC key and its decryption key to execute a successful attack. The further you can separate the HMAC key and its decryption key, the more secure you're app will be (and the less convenient it may become to implement and operate).

  • Thanks for the info, it's very useful. I hope not say something silly but if i'm not wrong I think I might cypher the key and cipher the value in my database, saving the key (simetric i supose because it's the same that decode) as a constant in the code as a private member of the type that manages that. So an atacker would need the assembly to decompile and the assembly it's not easy to get. Moreover that asembly might be also ofuscated in order to get more dificult to get. – MirlvsMaximvs Mar 9 '16 at 7:50
  • No. Not silly at all. Us infosec people don't tend to consider source code as a secure storage medium. Source code tends to be in source repos (sometimes public github repos!), gets seen by lots of people, and makes rotating the key difficult. That said, lots of people store secrets in the source. So it adds some security, but not less. As I don't know the sensitivity of your app or of this secret, you'll need to make the decision. – Neil Smithline Mar 9 '16 at 15:04
  • 1
    @MirlvsMaximvs - regarding the secrecy of the key in the binary, it's not likely as difficult to extract as you think. On Linux, there's the strings command that will extract all strings. There are similar apps on Windows. Still, the source code or the executable is a second item, besides the DB, that the attacker must obtain, so it is definitely more secure than storing an unencrypted key in the DB (something I really recommend against). – Neil Smithline Mar 9 '16 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.