ed25519-signature.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package signature
  2. import (
  3. "bytes"
  4. "crypto/ed25519"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "github.com/ncarlier/webhookd/pkg/truststore"
  11. )
  12. const (
  13. defaultKeyID = "default"
  14. xSignatureEd25519 = "X-Signature-Ed25519"
  15. xSignatureTimestamp = "X-Signature-Timestamp"
  16. )
  17. // IsEd25519SignatureRequest test if HTTP headers contains Ed25519 Signature
  18. func IsEd25519SignatureRequest(headers http.Header) bool {
  19. return headers.Get(xSignatureEd25519) != ""
  20. }
  21. // Ed25519SignatureHandler validate request HTTP signature
  22. func Ed25519SignatureHandler(r *http.Request, ts truststore.TrustStore) error {
  23. pubkey := ts.GetPublicKey(defaultKeyID)
  24. if pubkey == nil {
  25. return fmt.Errorf("public key not found: %s", defaultKeyID)
  26. }
  27. key, ok := pubkey.(ed25519.PublicKey)
  28. if !ok {
  29. return errors.New("invalid public key: verify the algorithm")
  30. }
  31. value := r.Header.Get(xSignatureEd25519)
  32. timestamp := r.Header.Get(xSignatureTimestamp)
  33. if value == "" || timestamp == "" {
  34. return errors.New("missing signature header")
  35. }
  36. sig, err := hex.DecodeString(value)
  37. if err != nil || len(sig) != ed25519.SignatureSize || sig[63]&224 != 0 {
  38. return fmt.Errorf("invalid signature format: %s", sig)
  39. }
  40. var msg bytes.Buffer
  41. msg.WriteString(timestamp)
  42. defer r.Body.Close()
  43. var body bytes.Buffer
  44. // Copy the original body back into the request after finishing.
  45. defer func() {
  46. r.Body = io.NopCloser(&body)
  47. }()
  48. // Copy body into buffers
  49. _, err = io.Copy(&msg, io.TeeReader(r.Body, &body))
  50. if err != nil {
  51. return err
  52. }
  53. if !ed25519.Verify(key, msg.Bytes(), sig) {
  54. return errors.New("invalid signature")
  55. }
  56. return nil
  57. }