Codebase list golang-github-russellhaering-goxmldsig / dab0c85
Import Upstream version 1.1.1 Thorsten Alteholz 2 years ago
7 changed file(s) with 74 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
0 arch:
1 - amd64
2 - ppc64le
3
04 language: go
15
26 go:
3 - "1.13.x"
47 - "1.14.x"
58 - "1.15.x"
9 - "1.17.x"
610 - master
1010 type Canonicalizer interface {
1111 Canonicalize(el *etree.Element) ([]byte, error)
1212 Algorithm() AlgorithmID
13 }
14
15 type NullCanonicalizer struct {
16 }
17
18 func MakeNullCanonicalizer() Canonicalizer {
19 return &NullCanonicalizer{}
20 }
21
22 func (c *NullCanonicalizer) Algorithm() AlgorithmID {
23 return AlgorithmID("NULL")
24 }
25
26 func (c *NullCanonicalizer) Canonicalize(el *etree.Element) ([]byte, error) {
27 scope := make(map[string]struct{})
28 return canonicalSerialize(canonicalPrep(el, scope, false))
1329 }
1430
1531 type c14N10ExclusiveCanonicalizer struct {
4864 // Canonicalize transforms the input Element into a serialized XML document in canonical form.
4965 func (c *c14N11Canonicalizer) Canonicalize(el *etree.Element) ([]byte, error) {
5066 scope := make(map[string]struct{})
51 return canonicalSerialize(canonicalPrep(el, scope))
67 return canonicalSerialize(canonicalPrep(el, scope, true))
5268 }
5369
5470 func (c *c14N11Canonicalizer) Algorithm() AlgorithmID {
6581 // Canonicalize transforms the input Element into a serialized XML document in canonical form.
6682 func (c *c14N10RecCanonicalizer) Canonicalize(el *etree.Element) ([]byte, error) {
6783 scope := make(map[string]struct{})
68 return canonicalSerialize(canonicalPrep(el, scope))
84 return canonicalSerialize(canonicalPrep(el, scope, true))
6985 }
7086
7187 func (c *c14N10RecCanonicalizer) Algorithm() AlgorithmID {
8298 // Canonicalize transforms the input Element into a serialized XML document in canonical form.
8399 func (c *c14N10CommentCanonicalizer) Canonicalize(el *etree.Element) ([]byte, error) {
84100 scope := make(map[string]struct{})
85 return canonicalSerialize(canonicalPrep(el, scope))
101 return canonicalSerialize(canonicalPrep(el, scope, true))
86102 }
87103
88104 func (c *c14N10CommentCanonicalizer) Algorithm() AlgorithmID {
115131 //
116132 // TODO(russell_h): This is very similar to excCanonicalPrep - perhaps they should
117133 // be unified into one parameterized function?
118 func canonicalPrep(el *etree.Element, seenSoFar map[string]struct{}) *etree.Element {
134 func canonicalPrep(el *etree.Element, seenSoFar map[string]struct{}, strip bool) *etree.Element {
119135 _seenSoFar := make(map[string]struct{})
120136 for k, v := range seenSoFar {
121137 _seenSoFar[k] = v
140156 for i, token := range ne.Child {
141157 childElement, ok := token.(*etree.Element)
142158 if ok {
143 ne.Child[i] = canonicalPrep(childElement, _seenSoFar)
159 ne.Child[i] = canonicalPrep(childElement, _seenSoFar, strip)
144160 }
145161 }
146162
33
44 require (
55 github.com/beevik/etree v1.1.0
6 github.com/jonboulle/clockwork v0.2.0
6 github.com/jonboulle/clockwork v0.2.2
7 github.com/kr/pretty v0.3.0 // indirect
8 github.com/rogpeppe/go-internal v1.8.0 // indirect
79 github.com/stretchr/testify v1.6.1
10 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
11 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
812 )
00 github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
11 github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
2 github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
23 github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
34 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
45 github.com/jonboulle/clockwork v0.2.0 h1:J2SLSdy7HgElq8ekSl2Mxh6vrRNFxqbXGenYH2I02Vs=
56 github.com/jonboulle/clockwork v0.2.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
7 github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
8 github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
9 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
10 github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
11 github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
12 github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
13 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
14 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
15 github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
16 github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
17 github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
618 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
719 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
20 github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
21 github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
22 github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
823 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
924 github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
1025 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1226 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
13 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
27 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
28 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
29 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
30 gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
1431 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
32 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
33 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9191
9292 dataId := el.SelectAttrValue(ctx.IdAttribute, "")
9393 if dataId == "" {
94 return nil, errors.New("Missing data ID")
95 }
96
97 reference.CreateAttr(URIAttr, "#"+dataId)
94 reference.CreateAttr(URIAttr, "")
95 } else {
96 reference.CreateAttr(URIAttr, "#"+dataId)
97 }
98
9899
99100 // /SignedInfo/Reference/Transforms
100101 transforms := ctx.createNamespacedElement(reference, TransformsTag)
9595
9696 _, err := ctx.SignEnveloped(authnRequest)
9797 require.Error(t, err)
98
99 randomKeyStore = RandomKeyStoreForTest()
100 ctx = NewDefaultSigningContext(randomKeyStore)
101
102 authnRequest = &etree.Element{
103 Space: "samlp",
104 Tag: "AuthnRequest",
105 }
106
107 _, err = ctx.SignEnveloped(authnRequest)
108 require.Error(t, err)
10998 }
11099
111100 func TestSignNonDefaultID(t *testing.T) {
110110 ref *types.Reference) (*etree.Element, Canonicalizer, error) {
111111 transforms := ref.Transforms.Transforms
112112
113 if len(transforms) != 2 {
114 return nil, nil, errors.New("Expected Enveloped and C14N transforms")
115 }
116
117113 // map the path to the passed signature relative to the passed root, in
118114 // order to enable removal of the signature by an enveloped signature
119115 // transform
156152 }
157153
158154 if canonicalizer == nil {
159 return nil, nil, errors.New("Expected canonicalization transform")
155 canonicalizer = MakeNullCanonicalizer()
160156 }
161157
162158 return el, canonicalizer, nil
233229 }
234230
235231 func (ctx *ValidationContext) validateSignature(el *etree.Element, sig *types.Signature, cert *x509.Certificate) (*etree.Element, error) {
236 idAttr := el.SelectAttr(ctx.IdAttribute)
237 if idAttr == nil || idAttr.Value == "" {
238 return nil, errors.New("Missing ID attribute")
232 idAttrEl := el.SelectAttr(ctx.IdAttribute)
233 idAttr := ""
234 if idAttrEl != nil {
235 idAttr = idAttrEl.Value
239236 }
240237
241238 var ref *types.Reference
242239
243240 // Find the first reference which references the top-level element
244241 for _, _ref := range sig.SignedInfo.References {
245 if _ref.URI == "" || _ref.URI[1:] == idAttr.Value {
242 if _ref.URI == "" || _ref.URI[1:] == idAttr {
246243 ref = &_ref
247244 }
248245 }
268265 }
269266
270267 if !bytes.Equal(digest, decodedDigestValue) {
268 return nil, errors.New("Signature could not be verified")
269 }
270 if sig.SignatureValue == nil {
271271 return nil, errors.New("Signature could not be verified")
272272 }
273273
317317
318318 // findSignature searches for a Signature element referencing the passed root element.
319319 func (ctx *ValidationContext) findSignature(root *etree.Element) (*types.Signature, error) {
320 idAttr := root.SelectAttr(ctx.IdAttribute)
321 if idAttr == nil || idAttr.Value == "" {
322 return nil, errors.New("Missing ID attribute")
320 idAttrEl := root.SelectAttr(ctx.IdAttribute)
321 idAttr := ""
322 if idAttrEl != nil {
323 idAttr = idAttrEl.Value
323324 }
324325
325326 var sig *types.Signature
365366 canonicalSignedInfo = detachedSignedInfo
366367
367368 case CanonicalXML11AlgorithmId:
368 canonicalSignedInfo = canonicalPrep(detachedSignedInfo, map[string]struct{}{})
369 canonicalSignedInfo = canonicalPrep(detachedSignedInfo, map[string]struct{}{}, true)
369370
370371 case CanonicalXML10RecAlgorithmId:
371 canonicalSignedInfo = canonicalPrep(detachedSignedInfo, map[string]struct{}{})
372 canonicalSignedInfo = canonicalPrep(detachedSignedInfo, map[string]struct{}{}, true)
372373
373374 case CanonicalXML10CommentAlgorithmId:
374 canonicalSignedInfo = canonicalPrep(detachedSignedInfo, map[string]struct{}{})
375 canonicalSignedInfo = canonicalPrep(detachedSignedInfo, map[string]struct{}{}, true)
375376
376377 default:
377378 return fmt.Errorf("invalid CanonicalizationMethod on Signature: %s", c14NAlgorithm)
402403 // Traverse references in the signature to determine whether it has at least
403404 // one reference to the top level element. If so, conclude the search.
404405 for _, ref := range _sig.SignedInfo.References {
405 if ref.URI == "" || ref.URI[1:] == idAttr.Value {
406 if ref.URI == "" || ref.URI[1:] == idAttr {
406407 sig = _sig
407408 return etreeutils.ErrTraversalHalted
408409 }