Skip to content
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

Tweaked code for Cpix signature support #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.test
.idea/
39 changes: 26 additions & 13 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@ func (ctx *ValidationContext) transform(
el *etree.Element,
sig *types.Signature,
ref *types.Reference) (*etree.Element, Canonicalizer, error) {
transforms := ref.Transforms.Transforms

if len(transforms) != 2 {
return nil, nil, errors.New("Expected Enveloped and C14N transforms")
}
// Transforms is an optional element https://www.w3.org/TR/xmldsig-core/#sec-CanonicalizationMethod 4.4.3.4
transforms := ref.Transforms.Transforms

// map the path to the passed signature relative to the passed root, in
// order to enable removal of the signature by an enveloped signature
Expand Down Expand Up @@ -155,6 +152,25 @@ func (ctx *ValidationContext) transform(
}
}

if canonicalizer == nil {
// Canonicalization Algorithm found outside the transform node
canonicalAlg := sig.SignedInfo.CanonicalizationMethod.Algorithm

switch AlgorithmID(canonicalAlg) {

case CanonicalXML11AlgorithmId:
canonicalizer = MakeC14N11Canonicalizer()

case CanonicalXML10RecAlgorithmId:
canonicalizer = MakeC14N10RecCanonicalizer()

case CanonicalXML10CommentAlgorithmId:
canonicalizer = MakeC14N10CommentCanonicalizer()
default:
return nil, nil, errors.New("Unknown Canonicalizer Algorithm: " + canonicalAlg)
}
}

if canonicalizer == nil {
return nil, nil, errors.New("Expected canonicalization transform")
}
Expand Down Expand Up @@ -233,16 +249,14 @@ func (ctx *ValidationContext) verifySignedInfo(sig *types.Signature, canonicaliz
}

func (ctx *ValidationContext) validateSignature(el *etree.Element, sig *types.Signature, cert *x509.Certificate) (*etree.Element, error) {
//Id attr is optional for CPIX element https://dashif.org/docs/CPIX2.1/HTML/Index.html#schema-cpix
idAttr := el.SelectAttr(ctx.IdAttribute)
if idAttr == nil || idAttr.Value == "" {
return nil, errors.New("Missing ID attribute")
}

var ref *types.Reference

// Find the first reference which references the top-level element
for _, _ref := range sig.SignedInfo.References {
if _ref.URI == "" || _ref.URI[1:] == idAttr.Value {
if _ref.URI == "" || (idAttr != nil && _ref.URI[1:] == idAttr.Value) {
ref = &_ref
}
}
Expand Down Expand Up @@ -298,10 +312,8 @@ func contains(roots []*x509.Certificate, cert *x509.Certificate) bool {

// findSignature searches for a Signature element referencing the passed root element.
func (ctx *ValidationContext) findSignature(el *etree.Element) (*types.Signature, error) {
//Id attr is optional for CPIX element https://dashif.org/docs/CPIX2.1/HTML/Index.html#schema-cpix
idAttr := el.SelectAttr(ctx.IdAttribute)
if idAttr == nil || idAttr.Value == "" {
return nil, errors.New("Missing ID attribute")
}

var sig *types.Signature

Expand Down Expand Up @@ -380,7 +392,7 @@ func (ctx *ValidationContext) findSignature(el *etree.Element) (*types.Signature
// Traverse references in the signature to determine whether it has at least
// one reference to the top level element. If so, conclude the search.
for _, ref := range _sig.SignedInfo.References {
if ref.URI == "" || ref.URI[1:] == idAttr.Value {
if ref.URI == "" || (idAttr != nil && ref.URI[1:] == idAttr.Value) {
sig = _sig
return etreeutils.ErrTraversalHalted
}
Expand Down Expand Up @@ -465,3 +477,4 @@ func (ctx *ValidationContext) Validate(el *etree.Element) (*etree.Element, error

return ctx.validateSignature(el, sig, cert)
}