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

Sqlite rebased #21

Merged
merged 18 commits into from
Jun 24, 2020
Merged

Sqlite rebased #21

merged 18 commits into from
Jun 24, 2020

Conversation

duijf
Copy link
Collaborator

@duijf duijf commented Jun 19, 2020

This is #15 rebased on the latest master.

I tried to preserve the original git history as much as possible, while also making the following changes:

  • Use Fido2.PublicKey instead of Fido2.Ec2Key
  • Move all HTTP logic into the individual handler functions that we have.
  • Add 4d0597a to make the SQLite changes work nicely with the assertion module that I authored in Implement assertion #14
  • Add minor tweaks to .gitignore and the Cabal file
  • Clean up a few warnings that resulted from rebasing.
  • Use Data.Binary.{decode,encode} before storing the public key parameters in the DB. (Otherwise, these can overflow).
  • Abort database transactions when exceptions occur. (Otherwise the connection remains in the transaction state causing subsequent requests to fail)

This now uses SQLite for the user state. Everything still seems to work now.

duijf and others added 14 commits June 19, 2020 11:44
This is just a toy example database where we can store users and
associated credentials, as an example for the server, don't use
this in production.
I was running into a few compile errors which didn't show up because the
Main module wasn't touched. The cause was that cabal didn't want to
recompile the Database module every time I touched it. That is also what
caused some of Ruud's commits to not build.
This way we still have access to the display name and username.
This ensures that we always finish our transactions whenever any errors
occur (like UNIQUE constraint violations). Without this, the server
could keep transactions opened meaning subsequent requests would not
work correctly.
@duijf duijf requested review from ruuda and arianvp June 19, 2020 11:49
@arianvp
Copy link
Owner

arianvp commented Jun 19, 2020

Perhaps we can store the EC2 Points in compressed form (x coordinate plus sign) instead of x/y coordinates. This eliminates a whole bunch of cryptographic security vulnerabilities by construction

@arianvp
Copy link
Owner

arianvp commented Jun 19, 2020

lets not make that a blocker. I'll open a new issue for it

fido/Crypto/Fido2/Protocol.hs Outdated Show resolved Hide resolved
server/Database.hs Show resolved Hide resolved
@ruuda
Copy link
Collaborator

ruuda commented Jun 19, 2020

Use Data.Binary.{decode,encode} before storing the public key parameters in the DB. (Otherwise, these can overflow).

Overflow in what sense? They were ByteString and they were stored as BLOB, and the coordinates have a fixed size, so what is there to overflow?

fido/Crypto/Fido2/Protocol.hs Outdated Show resolved Hide resolved
server/Database.hs Outdated Show resolved Hide resolved
@@ -234,7 +220,9 @@ completeLogin sessions users = do
verifyLogin :: SessionId -> Session -> Fido2.UserId -> Fido2.Challenge -> Scotty.ActionM ()
verifyLogin sessionId session userId challenge = do
credential <- Scotty.jsonData @(Fido2.PublicKeyCredential Fido2.AuthenticatorAssertionResponse)
credentials <- (liftIO $ getUserCredentials userId users) >>= handleError
credentials <- liftIO $ Database.withTransaction db $ \tx -> do
Copy link
Collaborator

Choose a reason for hiding this comment

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

We already have the credential here, so we can look up only that one by id, right?

Copy link
Collaborator Author

@duijf duijf Jun 20, 2020

Choose a reason for hiding this comment

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

That would require us to trust that the user actually respected the list of allowedCredentials that we passed to them. I'm not sure whether that is safe (but I suspect it isn't).

This code ensures that we only check the challenge against the list of credentials that are actually associated with the current user in the DB.

We can also change this to a lookup by both the user ID and the credential ID of course, but I'm not sure if there is a reason for choosing one over the other.

Do you have a preference?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We can also change this to a lookup by both the user ID and the credential ID of course

Yes let's do this.

@duijf
Copy link
Collaborator Author

duijf commented Jun 20, 2020

Overflow in what sense? They were ByteString and they were stored as BLOB, and the coordinates have a fixed size, so what is there to overflow?

I observed that the signature verification during login didn't work anymore after I got things to compile. When I printed the public keys after reading them back from the DB, they seemed to have overflowed.

But you're right: these fields were ByteStrings in your branch (I wrongly assumed they were Integers there too).

Seems like the ToField instance for Integer always truncates to SQLite's integer type even though the column is a BLOB (source). This caused the signature verification to fail after rebasing (which is why I added the call to Binary.encode).

This allows us to avoid partiality in the publicKeyX and publicKeyY
accessors, as well as expose a smart constructor which always checks
whether the point is valid.
@duijf duijf requested review from arianvp and ruuda June 20, 2020 08:53
@arianvp
Copy link
Owner

arianvp commented Jun 24, 2020

Lets merge this "as is" for now. I'm planning to do some major refactoring and I dont want this to become stale again

@arianvp arianvp merged commit b289d5d into master Jun 24, 2020
@arianvp arianvp deleted the sqlite-rebased branch June 24, 2020 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants