Skip to content

Commit

Permalink
Add support for raw ECDSA signatures. Keep backward compatibility wit… (
Browse files Browse the repository at this point in the history
#6)

* Add support for raw ECDSA signatures. Keep backward compatibility with DER-encoded signatures

* Go fmt

* Go fmt
  • Loading branch information
J3imip authored Dec 25, 2024
1 parent 43f5d0a commit a90a4f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/service/api/handlers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func Register(w http.ResponseWriter, r *http.Request) {
return
}

log.WithError(err).Error("failed to verify SOD")
log.WithError(sodError.VerboseError).Error("failed to verify SOD")

documentSOD.ErrorKind = sodError.KindPtr()
documentSOD.Error = sodError.VerboseErrorPtr()
Expand Down
22 changes: 19 additions & 3 deletions internal/types/signature_algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"encoding/asn1"
"hash"
"math/big"

"gitlab.com/distributed_lab/logan/v3/errors"
)
Expand Down Expand Up @@ -49,11 +51,25 @@ func GeneralVerify(publicKey interface{}, hash []byte, signature []byte, algo Al
}

func verifyECDSA(data, sig []byte, publicKey *ecdsa.PublicKey) error {
if ok := ecdsa.VerifyASN1(publicKey, data, sig); !ok {
return errors.New("failed to verify ECDSA signature")
// Attempt to parse the signature as ASN.1 DER format
if _, err := asn1.Unmarshal(sig, new(asn1.RawValue)); err == nil {
if ecdsa.VerifyASN1(publicKey, data, sig) {
return nil
}
return errors.New("failed to verify ECDSA signature in ASN.1 format")
}

// Handle raw (r || s) signature format
if len(sig) != 64 {
return errors.New("invalid ECDSA signature length")
}

r, s := new(big.Int).SetBytes(sig[:32]), new(big.Int).SetBytes(sig[32:])
if ecdsa.Verify(publicKey, data, r, s) {
return nil
}

return nil
return errors.New("failed to verify ECDSA signature in raw format")
}

func GeneralHash(algorithm HashAlgorithm) hash.Hash {
Expand Down

0 comments on commit a90a4f8

Please sign in to comment.