Debian Codegraph @

lib/xmldsig/canonicalize.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package xmldsigscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/
18
19 import (
20 "sortscip-go gomod github.com/golang/go/src go1.18 sort/"
21
22 "github.com/beevik/etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/"
23 )
24
25 // Canonicalize a document starting from the given element and return the
26 // serialized bytes. Implements something vaguely like xml-exc-c14n. Namespaces
27 // declared in parent nodes are pulled in, and namespaces not used in the
28 // element where they are declared are pushed further down to the elements that
29 // use them.
30 //
31 // This is not a standards-conforming implementation. Use at your own peril.
32 func SerializeCanonicalscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SerializeCanonical().(oldroot *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#) ([]byte, error) {
33 // make a deep copy before mangling things
34 root := oldroot.Copyscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Copy().()
35 // remake the document without any xml declarations etc.
36 doc := etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.NewDocumentscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/NewDocument().()
37 doc.SetRootscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Document#SetRoot().(root)
38 doc.WriteSettingsscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Document#WriteSettings..CanonicalEndTagsscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/WriteSettings#CanonicalEndTags. = true
39 doc.WriteSettingsscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Document#WriteSettings..CanonicalTextscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/WriteSettings#CanonicalText. = true
40 doc.WriteSettingsscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Document#WriteSettings..CanonicalAttrValscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/WriteSettings#CanonicalAttrVal. = true
41 pullDownscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/pullDown().(oldroot, root)
42 walkAttributesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/walkAttributes().(root)
43 return doc.WriteToBytesscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Document#WriteToBytes().()
44 }
45
46 // if the attribute is a namespace declaration then return the namespace
47 func getDeclscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/getDecl().(attr etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#) (string, bool) {
48 if attr.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. == "" && attr.Keyscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Key. == "xmlns" {
49 return "", true
50 } else if attr.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. == "xmlns" {
51 return attr.Keyscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Key., true
52 } else {
53 return "", false
54 }
55 }
56
57 // attribute name for declaring the given namespace
58 func putDeclscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/putDecl().(space string) string {
59 if space == "" {
60 return "xmlns"
61 }
62 return "xmlns:" + space
63 }
64
65 func walkAttributesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/walkAttributes().(elem *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#) {
66 // remove unused spaces and push ones this element doesn't use down to child elements
67 for i := 0; i < len(elem.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr.); {
68 attr := elem.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr.[i]
69 if space, isDecl := getDeclscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/getDecl().(attr); isDecl && !usesSpacescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/usesSpace().(elem, space) {
70 pushDownscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/pushDown().(elem, elem, space, putDeclscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/putDecl().(space), attr.Valuescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Value.)
71 elem.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr. = append(elem.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr.[:i], elem.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr.[i+1:]...)
72 continue
73 }
74 i++
75 }
76 sortscip-go gomod github.com/golang/go/src go1.18 sort/.Slicescip-go gomod github.com/golang/go/src go1.18 sort/Slice().(elem.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr., func(i, j int) bool {
77 x := elem.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr.[i]
78 y := elem.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr.[j]
79 // default namespace node sorts first
80 if x.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. == "" && x.Keyscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Key. == "xmlns" {
81 return true
82 } else if y.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. == "" && y.Keyscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Key. == "xmlns" {
83 return false
84 }
85 // then all other namespace nodes
86 if x.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. == "xmlns" && y.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. != "xmlns" {
87 return true
88 } else if y.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. == "xmlns" && x.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. != "xmlns" {
89 return false
90 }
91 // then order by namespace and finally by key
92 if x.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. != y.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. {
93 return x.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. < y.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space.
94 }
95 return x.Keyscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Key. < y.Keyscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Key.
96 })
97 for i := 0; i < len(elem.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child.); {
98 token := elem.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child.[i]
99 switch t := token.(type) {
100 case *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#:
101 walkAttributesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/walkAttributes().(t)
102 case *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.CharDatascip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/CharData#:
103 // keep
104 default:
105 // remove
106 elem.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child. = append(elem.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child.[:i], elem.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child.[i+1:]...)
107 continue
108 }
109 i++
110 }
111 }
112
113 // does this element or its attributes reference the given namespace?
114 func usesSpacescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/usesSpace().(elem *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, space string) bool {
115 if elem.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Space. == space {
116 return true
117 } else if space == "" {
118 // if the element doesn't use the default namespace, then neither do the attributes
119 return false
120 }
121 for _, attr := range elem.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr. {
122 if attr.Spacescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Space. == space {
123 return true
124 }
125 }
126 return false
127 }
128
129 // if the root element used to be the child of another element, pull down
130 // namespaces that were declared in its ancestors
131 func pullDownscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/pullDown().(oldroot, newroot *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#) {
132 spaces := make(map[string]string)
133 for p := oldroot.Parentscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Parent().(); p != nil; p = p.Parentscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Parent().() {
134 for _, attr := range p.Attrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Attr. {
135 space, isDecl := getDeclscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/getDecl().(attr)
136 if !isDecl {
137 continue
138 }
139 if spaces[space] != "" {
140 continue
141 }
142 spaces[space] = attr.Valuescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Attr#Value.
143 }
144 }
145 for space, value := range spaces {
146 pushDownscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/pushDown().(nil, newroot, space, putDeclscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/putDecl().(space), value)
147 }
148 }
149
150 // add a namespace to child elements that need it
151 func pushDownscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/pushDown().(top, elem *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, space, key, value string) {
152 if elem != top && elem.SelectAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#SelectAttr().(key) != nil {
153 // redeclared here already
154 return
155 } else if usesSpacescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/usesSpace().(elem, space) {
156 // used here, declare it here
157 elem.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().(key, value)
158 } else {
159 // recurse further
160 for _, elem := range elem.ChildElementsscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#ChildElements().() {
161 pushDownscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/pushDown().(top, elem, space, key, value)
162 }
163 }
164 }