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

Binary signing of encryption. #404

Merged
merged 4 commits into from Nov 20, 2017
Merged

Binary signing of encryption. #404

merged 4 commits into from Nov 20, 2017

Conversation

elorest
Copy link
Member

@elorest elorest commented Nov 20, 2017

Description of the Change

  1. Creates signature from bytes and appends onto IO.
  • Reduces string operation count to zero in some cases and one in other cases.
  • When saving encryption to files there is no need to cast to string or base64 encode.
  • Signing encrypted file data adds security and prerequisite checks.
  • Makes encrypted cookies harder to gain any useful data from.
  1. Uses SHA256 instead of compromised SHA1.

Benefits

  1. Roughly 4% faster for encrypted cookies/session.
  2. More secure due to sha256 instead of sha1.

Copy link
Member

@drujensen drujensen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@@ -24,22 +28,30 @@ module Amber::Support
# Verify and Decrypt a message. We need to verify the message in order to
# avoid padding attacks. Reference: http://www.limited-entropy.com/padding-oracle-attacks.
def verify_and_decrypt(value : String) : Bytes
decrypt(verifier.verify_raw(value))
verify_and_decrypt(Base64.decode(value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base64 de-marshaling here doesn't match the marshaling that is happening when the cookie is written, does it make sense to have this class handle both Base64 encoding and decoding?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably makes more sense to remove that from here entirely and have this class just take and return slices. As it is this is just an overload where if a string is passed in it de-marshals it.

What do you mean by "it doesn't match"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm only trying to point out that by my reading, the code flow for decrypting a message is:

other->MessageEncryptor->base64.decode

and the code flow for encrypting is:

other->base64.encode->MessageEncryptor

I'd rather MessageEncryptor handle both base64 directions or neither.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither makes more sense, since method overloading doesn't work with return values, we'd have to create another method called encrypt_and_base64_encode or something.

I think removing it makes more sense.

@robacarp
Copy link
Member

Is there a simple way we can fingerprint the encryption method to make this more upgrade-able in the future?

@elorest
Copy link
Member Author

elorest commented Nov 20, 2017

@robacarp Not sure it understand your meaning on this. This isn't going to change all over the place. It will be a small breaking change with the 0.4.0 release but that's sort of the point of minor and major version releases.

One will just have to decrypt and save their production.yml before upgrading, and reencrypting.

@robacarp
Copy link
Member

@elorest I think it will also invalidate all cookies signed with the previous algorithm, but I'm not familiar enough with all the code at play to say that with confidence. I'm thinking more in terms of when sha256 is broken enough that it needs to be replaced, is there a way the cookies can be marked as sha256 so that they can be decrypted when this code is updated again.

Its not something necessary to put a ton of time into now, but simply prefixing the signed cookie with sha256: after it's been encrypted would give some future developer the opportunity to provide a seamless transition period.

@elorest
Copy link
Member Author

elorest commented Nov 20, 2017

Generally cookies aren't important enough that if they become invalid every year or two anyone worries about it. Rails has changed the way cookies worked many times in the last 12 years.
If they become invalid people will have to login again. Same as if they used a different browser or computer.

@elorest elorest merged commit b1c9f20 into master Nov 20, 2017
@robacarp
Copy link
Member

@elorest thanks, that's good background and context

@elorest elorest deleted the is/binary_sign_encryption branch November 20, 2017 19:47
eliasjpr pushed a commit that referenced this pull request Nov 23, 2017
* binarry signing of encryption

* added signature to encrypted secrets

* SHA256 Signature for environment files

* refactored a couple things to make it more readable.
@faustinoaq faustinoaq added this to Done in Framework 2018 May 5, 2018
@faustinoaq faustinoaq removed this from Done in Framework 2018 Jun 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants