-
Notifications
You must be signed in to change notification settings - Fork 36
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
implement stateless resets #141
Merged
bifurcation
merged 5 commits into
bifurcation:master
from
marten-seemann:stateless-reset
Dec 6, 2017
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1e8da32
implement stateless resets
marten-seemann cf5746e
also save the negotiated cipher suite in the cookie
marten-seemann 570ac8d
increase nonce size and derive a separate key for every nonce
marten-seemann 3da3cc7
return AlertStatelessRetry after sending a HelloRetryRequest
marten-seemann f9e1617
rename the CookieSource to CookieProtector
marten-seemann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package mint | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"fmt" | ||
"io" | ||
|
||
"golang.org/x/crypto/hkdf" | ||
) | ||
|
||
// CookieProtector is used to create and verify a cookie | ||
type CookieProtector interface { | ||
// NewToken creates a new token | ||
NewToken([]byte) ([]byte, error) | ||
// DecodeToken decodes a token | ||
DecodeToken([]byte) ([]byte, error) | ||
} | ||
|
||
const cookieSecretSize = 32 | ||
const cookieNonceSize = 32 | ||
|
||
// The DefaultCookieProtector is a simple implementation for the CookieProtector. | ||
type DefaultCookieProtector struct { | ||
secret []byte | ||
} | ||
|
||
var _ CookieProtector = &DefaultCookieProtector{} | ||
|
||
// NewDefaultCookieProtector creates a source for source address tokens | ||
func NewDefaultCookieProtector() (CookieProtector, error) { | ||
secret := make([]byte, cookieSecretSize) | ||
if _, err := rand.Read(secret); err != nil { | ||
return nil, err | ||
} | ||
return &DefaultCookieProtector{secret: secret}, nil | ||
} | ||
|
||
// NewToken encodes data into a new token. | ||
func (s *DefaultCookieProtector) NewToken(data []byte) ([]byte, error) { | ||
nonce := make([]byte, cookieNonceSize) | ||
if _, err := rand.Read(nonce); err != nil { | ||
return nil, err | ||
} | ||
aead, aeadNonce, err := s.createAEAD(nonce) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return append(nonce, aead.Seal(nil, aeadNonce, data, nil)...), nil | ||
} | ||
|
||
// DecodeToken decodes a token. | ||
func (s *DefaultCookieProtector) DecodeToken(p []byte) ([]byte, error) { | ||
if len(p) < cookieNonceSize { | ||
return nil, fmt.Errorf("Token too short: %d", len(p)) | ||
} | ||
nonce := p[:cookieNonceSize] | ||
aead, aeadNonce, err := s.createAEAD(nonce) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return aead.Open(nil, aeadNonce, p[cookieNonceSize:], nil) | ||
} | ||
|
||
func (s *DefaultCookieProtector) createAEAD(nonce []byte) (cipher.AEAD, []byte, error) { | ||
h := hkdf.New(sha256.New, s.secret, nonce, []byte("mint cookie source")) | ||
key := make([]byte, 32) // use a 32 byte key, in order to select AES-256 | ||
if _, err := io.ReadFull(h, key); err != nil { | ||
return nil, nil, err | ||
} | ||
aeadNonce := make([]byte, 12) | ||
if _, err := io.ReadFull(h, aeadNonce); err != nil { | ||
return nil, nil, err | ||
} | ||
c, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
aead, err := cipher.NewGCM(c) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return aead, aeadNonce, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package mint | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestCookieProtector(t *testing.T) { | ||
cs, err := NewDefaultCookieProtector() | ||
assertNotError(t, err, "creating the cookie source failed") | ||
|
||
t.Run("handling valid tokens", func(t *testing.T) { | ||
cookie := []byte("foobar") | ||
token, err := cs.NewToken(cookie) | ||
assertNotError(t, err, "creating new token failed") | ||
decoded, err := cs.DecodeToken(token) | ||
assertNotError(t, err, "decoding the token failed") | ||
assertDeepEquals(t, cookie, decoded) | ||
}) | ||
|
||
t.Run("handling invalid tokens", func(t *testing.T) { | ||
_, err := cs.DecodeToken([]byte("too short")) | ||
assertError(t, err, "it should reject too short tokens") | ||
_, err = cs.DecodeToken(append(bytes.Repeat([]byte{0}, cookieNonceSize), []byte("invalid token")...)) | ||
assertError(t, err, "it should reject invalid tokens") | ||
// create a valid and modify the nonce | ||
token, err := cs.NewToken([]byte("foobar")) | ||
assertNotError(t, err, "creating new token failed") | ||
token[0]++ | ||
_, err = cs.DecodeToken(token) | ||
assertError(t, err, "it should reject a token with the wrong nonce") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't your fault, but I wonder if we should give these non-alerts a special name so we don't acccidentally try to send them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #149