-
Notifications
You must be signed in to change notification settings - Fork 103
james/dt4a auth #2149
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
Merged
Merged
james/dt4a auth #2149
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2938f6b
basic zta middleware wip
James-Pickett 0706287
remove redundant b64 of pub keys, expect root key to be self signed, …
James-Pickett a94c420
add dt4a root keys
James-Pickett 1240662
use json instead of msg pack
James-Pickett 16e04ec
rough version of zta auth
James-Pickett 6cb07a4
Merge branch 'main' into james/zta-auth
James-Pickett a123746
update pub keys, marshall links instead of chain obj, comments
James-Pickett 6d581c0
tighten up tests
James-Pickett dee1581
add dt4a routing
James-Pickett 56e4021
comments, clean up
James-Pickett 7383014
Merge branch 'main' into james/zta-auth
James-Pickett 1025db5
add logging to dt4a middle ware
James-Pickett d593b39
pass noop logger to tests
James-Pickett 050774e
better payload json unmarshall
James-Pickett ace301f
leave zta unauthed until ready
James-Pickett 970e23a
b64 url encode signature to match server side
James-Pickett 29a5e5b
Merge branch 'main' into james/zta-auth
James-Pickett 7271398
feedback, better comments, better errors
James-Pickett 036d045
better errors, logging
James-Pickett b13d47b
Merge branch 'main' into james/zta-auth
James-Pickett 08cb40a
more tests, better var names
James-Pickett 89be2fc
parse pub keys in certs with jwk pkg
James-Pickett 58f90a0
better error messages
James-Pickett 3b2f94c
rename var to avoid shadowing
James-Pickett a542e4f
drop use of jwk pkg, update enpoint paths
James-Pickett d1cbd76
Merge branch 'main' into james/zta-auth
James-Pickett a70d56c
rename endpoint to v3
James-Pickett eea3e5c
removing shadowing, update comments, drop unneeded jwk features
James-Pickett cda9803
use consts for curve strings
James-Pickett 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 hidden or 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 hidden or 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,98 @@ | ||
package localserver | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
) | ||
|
||
// jwk is a JSON Web Key (JWK) structure for representing public keys, | ||
// this a partial implementation using the stdlib for only the bits we care about, | ||
// RFC https://datatracker.ietf.org/doc/html/rfc7517 | ||
type jwk struct { | ||
RebeccaMahany marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Curve string `json:"crv"` | ||
X string `json:"x"` | ||
Y string `json:"y"` | ||
KeyID string `json:"kid"` | ||
} | ||
|
||
const ( | ||
curveP256 string = "P-256" | ||
curveP384 string = "P-384" | ||
curveP521 string = "P-521" | ||
) | ||
|
||
func parseEllipticCurve(str string) (elliptic.Curve, error) { | ||
switch strings.ToUpper(str) { | ||
case curveP256: | ||
return elliptic.P256(), nil | ||
case curveP384: | ||
return elliptic.P384(), nil | ||
case curveP521: | ||
return elliptic.P521(), nil | ||
default: | ||
return &elliptic.CurveParams{}, fmt.Errorf("unsupported curve: %s", str) | ||
} | ||
} | ||
|
||
// ecdsaPubKey converts jwk in to ecdsa public key | ||
func (j *jwk) ecdsaPubKey() (*ecdsa.PublicKey, error) { | ||
curve, err := parseEllipticCurve(j.Curve) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Decode the x and y coordinates using base64 URL decoding (unpadded). | ||
xBytes, err := base64.RawURLEncoding.DecodeString(j.X) | ||
if err != nil { | ||
return nil, fmt.Errorf("error decoding x coordinate: %v", err) | ||
} | ||
yBytes, err := base64.RawURLEncoding.DecodeString(j.Y) | ||
if err != nil { | ||
return nil, fmt.Errorf("error decoding y coordinate: %v", err) | ||
} | ||
|
||
// Convert the bytes into big.Int values. | ||
x := new(big.Int).SetBytes(xBytes) | ||
y := new(big.Int).SetBytes(yBytes) | ||
|
||
// Construct the ECDSA public key. | ||
pubKey := &ecdsa.PublicKey{ | ||
Curve: curve, | ||
X: x, | ||
Y: y, | ||
} | ||
|
||
// this is a little weird, but it's the recommended way to validate a public key, | ||
// under the hood it calls Curve.IsOnCurve(...), but if you call that directly | ||
// you get deprecated warnings | ||
if _, err := pubKey.ECDH(); err != nil { | ||
return nil, fmt.Errorf("invalid public key: %w", err) | ||
} | ||
|
||
return pubKey, nil | ||
} | ||
|
||
// x25519PubKey converts jwk in to x25519 key (*[32]byte) | ||
func (j *jwk) x25519PubKey() (*[32]byte, error) { | ||
// Decode the "x" coordinate using base64 URL decoding (unpadded). | ||
xBytes, err := base64.RawURLEncoding.DecodeString(j.X) | ||
if err != nil { | ||
return nil, fmt.Errorf("error decoding x coordinate: %v", err) | ||
} | ||
|
||
// X25519 public keys should be 32 bytes. | ||
if len(xBytes) != 32 { | ||
return nil, errors.New("invalid x coordinate length for X25519, expected 32 bytes") | ||
} | ||
|
||
// Copy the bytes into a fixed size array. | ||
var pubKey [32]byte | ||
copy(pubKey[:], xBytes) | ||
|
||
return &pubKey, nil | ||
} |
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.