Skip to content

Commit

Permalink
add REC-xml-c14n-20010315 canonical algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
danikarik committed Oct 4, 2017
1 parent b7efc62 commit 81ebcdc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
34 changes: 34 additions & 0 deletions canonicalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ func (c *c14N11Canonicalizer) Algorithm() AlgorithmID {
return CanonicalXML11AlgorithmId
}

type c14N10RecCanonicalizer struct{}

// MakeC14N10RecCanonicalizer constructs an inclusive canonicalizer.
func MakeC14N10RecCanonicalizer() Canonicalizer {
return &c14N10RecCanonicalizer{}
}

// Canonicalize transforms the input Element into a serialized XML document in canonical form.
func (c *c14N10RecCanonicalizer) Canonicalize(el *etree.Element) ([]byte, error) {
scope := make(map[string]struct{})
return canonicalSerialize(canonicalPrep(el, scope))
}

func (c *c14N10RecCanonicalizer) Algorithm() AlgorithmID {
return CanonicalXML10RecAlgorithmId
}

type c14N10CommentCanonicalizer struct{}

// MakeC14N10CommentCanonicalizer constructs an inclusive canonicalizer.
func MakeC14N10CommentCanonicalizer() Canonicalizer {
return &c14N10CommentCanonicalizer{}
}

// Canonicalize transforms the input Element into a serialized XML document in canonical form.
func (c *c14N10CommentCanonicalizer) Canonicalize(el *etree.Element) ([]byte, error) {
scope := make(map[string]struct{})
return canonicalSerialize(canonicalPrep(el, scope))
}

func (c *c14N10CommentCanonicalizer) Algorithm() AlgorithmID {
return CanonicalXML10CommentAlgorithmId
}

func composeAttr(space, key string) string {
if space != "" {
return space + ":" + key
Expand Down
12 changes: 12 additions & 0 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ func (ctx *ValidationContext) transform(
case CanonicalXML11AlgorithmId:
canonicalizer = MakeC14N11Canonicalizer()

case CanonicalXML10RecAlgorithmId:
canonicalizer = MakeC14N10RecCanonicalizer()

case CanonicalXML10CommentAlgorithmId:
canonicalizer = MakeC14N10CommentCanonicalizer()

default:
return nil, nil, errors.New("Unknown Transform Algorithm: " + algo)
}
Expand Down Expand Up @@ -303,6 +309,12 @@ func (ctx *ValidationContext) findSignature(el *etree.Element) (*types.Signature
case CanonicalXML11AlgorithmId:
canonicalSignedInfo = canonicalPrep(detachedSignedInfo, map[string]struct{}{})

case CanonicalXML10RecAlgorithmId:
canonicalSignedInfo = canonicalPrep(detachedSignedInfo, map[string]struct{}{})

case CanonicalXML10CommentAlgorithmId:
canonicalSignedInfo = canonicalPrep(detachedSignedInfo, map[string]struct{}{})

default:
return fmt.Errorf("invalid CanonicalizationMethod on Signature: %s", c14NAlgorithm)
}
Expand Down
3 changes: 3 additions & 0 deletions xml_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const (
CanonicalXML10ExclusiveAlgorithmId AlgorithmID = "http://www.w3.org/2001/10/xml-exc-c14n#"
CanonicalXML11AlgorithmId AlgorithmID = "http://www.w3.org/2006/12/xml-c14n11"

CanonicalXML10RecAlgorithmId AlgorithmID = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
CanonicalXML10CommentAlgorithmId AlgorithmID = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"

EnvelopedSignatureAltorithmId AlgorithmID = "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
)

Expand Down

0 comments on commit 81ebcdc

Please sign in to comment.