Skip to content

Commit

Permalink
Merge pull request #18 from muzzammilshahid/cra-fixes
Browse files Browse the repository at this point in the history
Fix CRA signature verification
  • Loading branch information
om26er authored Jun 30, 2024
2 parents de554a4 + bb60a58 commit cccda84
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
24 changes: 19 additions & 5 deletions cmd/wampproto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,20 +447,34 @@ func Run(args []string) (string, error) {
case c.signCRAChallenge.FullCommand():
craKey, err := wampprotocli.DecodeHexOrBase64(*c.craKey)
if err != nil {
return "", fmt.Errorf("invalid cra-key: %s", err.Error())
craKey = []byte(*c.craKey)
}

signedChallenge := auth.SignCRAChallenge(*c.craChallenge, craKey)
var craChallenge = *c.craChallenge
craChallengeBytes, err := wampprotocli.DecodeHexOrBase64(craChallenge)
if err == nil {
craChallenge = string(craChallengeBytes)
}

signedChallenge := auth.SignCRAChallengeBytes(craChallenge, craKey)

return wampprotocli.FormatOutputBytes(*c.output, []byte(signedChallenge))
return wampprotocli.FormatOutputBytes(*c.output, signedChallenge)

case c.verifyCRASignature.FullCommand():
craKey, err := wampprotocli.DecodeHexOrBase64(*c.verifyCRAKey)
if err != nil {
return "", fmt.Errorf("invalid cra-key: %s", err.Error())
craKey = []byte(*c.verifyCRAKey)
}

isVerified := auth.VerifyCRASignature(*c.verifyCRASign, *c.verifyCRAChallenge, craKey)
var craChallenge = *c.verifyCRAChallenge
craChallengeBytes, err := wampprotocli.DecodeHexOrBase64(craChallenge)
if err == nil {
craChallenge = string(craChallengeBytes)
}

craSignature := wampprotocli.EnsureBase64(*c.verifyCRASign)

isVerified := auth.VerifyCRASignature(craSignature, craChallenge, craKey)
if !isVerified {
return "", fmt.Errorf("signature verification failed")
}
Expand Down
14 changes: 14 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ func HexToBase64(hexStr string) (string, error) {
return base64Str, nil
}

func EnsureBase64(str string) string {
base64Str, err := HexToBase64(str)
if err == nil {
return base64Str
}

_, err = base64.StdEncoding.DecodeString(str)
if err == nil {
return str
}

return base64.StdEncoding.EncodeToString([]byte(str))
}

func DecodeHexOrBase64(str string) ([]byte, error) {
bytes, err := hex.DecodeString(str)
if err == nil {
Expand Down

0 comments on commit cccda84

Please sign in to comment.