forked from moov-io/signedxml
/
signedxml.go
executable file
·591 lines (507 loc) · 20.5 KB
/
signedxml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Package signedxml transforms and validates signedxml documents
package signedxml
import (
"crypto"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/beevik/etree"
)
var logger = log.New(os.Stdout, "DEBUG-SIGNEDXML: ", log.Ldate|log.Ltime|log.Lshortfile)
func init() {
hashAlgorithms = map[string]crypto.Hash{
"http://www.w3.org/2001/04/xmldsig-more#md5": crypto.MD5,
"http://www.w3.org/2000/09/xmldsig#sha1": crypto.SHA1,
"http://www.w3.org/2001/04/xmldsig-more#sha224": crypto.SHA224,
"http://www.w3.org/2001/04/xmlenc#sha256": crypto.SHA256,
"http://www.w3.org/2001/04/xmldsig-more#sha384": crypto.SHA384,
"http://www.w3.org/2001/04/xmlenc#sha512": crypto.SHA512,
"http://www.w3.org/2001/04/xmlenc#ripemd160": crypto.RIPEMD160,
}
signatureAlgorithms = map[string]x509.SignatureAlgorithm{
"http://www.w3.org/2001/04/xmldsig-more#rsa-md2": x509.MD2WithRSA,
"http://www.w3.org/2001/04/xmldsig-more#rsa-md5": x509.MD5WithRSA,
"http://www.w3.org/2000/09/xmldsig#rsa-sha1": x509.SHA1WithRSA,
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256": x509.SHA256WithRSA,
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha384": x509.SHA384WithRSA,
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha512": x509.SHA512WithRSA,
"http://www.w3.org/2000/09/xmldsig#dsa-sha1": x509.DSAWithSHA1,
"http://www.w3.org/2000/09/xmldsig#dsa-sha256": x509.DSAWithSHA256,
"http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha1": x509.ECDSAWithSHA1,
"http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256": x509.ECDSAWithSHA256,
"http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha384": x509.ECDSAWithSHA384,
"http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512": x509.ECDSAWithSHA512,
}
CanonicalizationAlgorithms = map[string]CanonicalizationAlgorithm{
"http://www.w3.org/2000/09/xmldsig#enveloped-signature": EnvelopedSignature{},
"http://www.w3.org/2001/10/xml-exc-c14n#": ExclusiveCanonicalization{},
"http://www.w3.org/2001/10/xml-exc-c14n#WithComments": ExclusiveCanonicalization{WithComments: true},
// xmllib2 canonicalizers, added:
// "http://www.w3.org/TR/xml-c14n": C14N10Canonicalizer{},
// "http://www.w3.org/TR/xml-c14n#WithComments": C14N10Canonicalizer{WithComments: true},
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315": C14N10Canonicalizer{},
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments": C14N10Canonicalizer{WithComments: true},
// "http://www.w3.org/TR/xml-exc-c14n": C14N10ExclusiveCanonicalizer{},
// "http://www.w3.org/TR/xml-exc-c14n#WithComments": C14N10ExclusiveCanonicalizer{WithComments: true},
"http://www.w3.org/2006/12/xml-c14n11": C14N11Canonicalizer{},
"http://www.w3.org/2006/12/xml-c14n11#WithComments": C14N11Canonicalizer{WithComments: true},
}
}
// CanonicalizationAlgorithm defines an interface for processing an XML
// document into a standard format.
//
// If any child elements are in the Transform node, the entire transform node
// will be passed to the Process method through the transformXML parameter as an
// XML string. This is necessary for transforms that need additional processing
// data, like XPath (http://www.w3.org/TR/xmldsig-core/#sec-XPath). If there are
// no child elements in Transform (or CanonicalizationMethod), then an empty
// string will be passed through.
type CanonicalizationAlgorithm interface {
Process(inputXML string, transformXML string) (outputXML string, err error)
}
// CanonicalizationAlgorithms maps the CanonicalizationMethod or
// Transform Algorithm URIs to a type that implements the
// CanonicalizationAlgorithm interface.
//
// Implementations are provided for the following transforms:
// http://www.w3.org/2001/10/xml-exc-c14n# (ExclusiveCanonicalization)
// http://www.w3.org/2001/10/xml-exc-c14n#WithComments (ExclusiveCanonicalizationWithComments)
// http://www.w3.org/2000/09/xmldsig#enveloped-signature (EnvelopedSignature)
//
// Custom implementations can be added to the map
var CanonicalizationAlgorithms map[string]CanonicalizationAlgorithm
var hashAlgorithms map[string]crypto.Hash
var signatureAlgorithms map[string]x509.SignatureAlgorithm
// signatureData provides options for verifying a signed XML document
type signatureData struct {
xml *etree.Document
signature *etree.Element
signedInfo *etree.Element
sigValue string
sigAlgorithm x509.SignatureAlgorithm
canonAlgorithm CanonicalizationAlgorithm
refIDAttribute string
}
// SetSignature can be used to assign an external signature for the XML doc
// that Validator will verify
func (s *signatureData) SetSignature(sig string) error {
doc := etree.NewDocument()
err := doc.ReadFromString(sig)
s.signature = doc.Root()
return err
}
func (s *signatureData) parseEnvelopedSignature() error {
sig := s.xml.FindElement(".//Signature")
if sig != nil {
s.signature = sig
} else {
return errors.New("signedxml: Unable to find a unique signature element " +
"in the xml document. The signature must either be enveloped in the " +
"xml doc or externally assigned to Validator.SetSignature")
}
return nil
}
func (s *signatureData) parseSignedInfo() error {
s.signedInfo = nil
s.signedInfo = s.signature.SelectElement("SignedInfo")
if s.signedInfo == nil {
return errors.New("signedxml: unable to find SignedInfo element")
}
// move the Signature level namespace down to SignedInfo so that the signature
// value will match up I.e: check if SignedInfo prefix is defined in Signature, copy it to SignInfo attrs
if s.signedInfo.Space != "" { // if SignedInfo has a prefix
attr := s.signature.SelectAttr(s.signedInfo.Space) // find prefix definition in Signature
if attr != nil {
s.signedInfo.Attr = []etree.Attr{*attr} // copy the definition to SignedInfo
}
} else { // if no prefix
attr := s.signature.SelectAttr("xmlns") // select any attribute with root namespace, if there is such
if attr != nil {
s.signedInfo.Attr = []etree.Attr{*attr}
}
}
// Copy SignedInfo xmlns: into itself if it does not exist and is defined as a root attribute
// i.e. check if SignedInfo prefix is defined in root, copy it to SignedInfo attrs
root := s.xml.Root()
if root != nil {
sigNS := root.SelectAttr("xmlns:" + s.signedInfo.Space)
if sigNS != nil {
if s.signedInfo.SelectAttr("xmlns:"+s.signedInfo.Space) == nil {
s.signedInfo.CreateAttr("xmlns:"+s.signedInfo.Space, sigNS.Value)
}
}
}
// It is adding <Root> tag namespaces, even if it wasn't used in SignedInfo - mistake.
// Solution: add all namespaces, which are used in the SignedInfo child tags
// signedInfoDoc, err := populateElementWithNameSpaces(s.signedInfo, s.xml.Copy())
// if err != nil {
// return err
// }
// s.signedInfo.Parent().AddChild(signedInfoDoc.Root())
// s.signedInfo.Parent().RemoveChildAt(0) // old signedInfo
return nil
}
func (s *signatureData) parseSigValue() error {
s.sigValue = ""
sigValueElement := s.signature.SelectElement("SignatureValue")
if sigValueElement != nil {
s.sigValue = sigValueElement.Text()
return nil
}
return errors.New("signedxml: unable to find SignatureValue")
}
func (s *signatureData) parseSigAlgorithm() error {
s.sigAlgorithm = x509.UnknownSignatureAlgorithm
sigMethod := s.signedInfo.SelectElement("SignatureMethod")
var sigAlgoURI string
if sigMethod == nil {
return errors.New("signedxml: Unable to find SignatureMethod element")
}
sigAlgoURI = sigMethod.SelectAttrValue("Algorithm", "")
if sigAlgoURI == "" {
return errors.New("signedxml: Unable to find Algorithm in " +
"SignatureMethod element")
}
sigAlgo, ok := signatureAlgorithms[sigAlgoURI]
if ok {
s.sigAlgorithm = sigAlgo
return nil
}
return errors.New("signedxml: Unsupported Algorithm " + sigAlgoURI + " in " +
"SignatureMethod")
}
func (s *signatureData) parseCanonAlgorithm() error {
s.canonAlgorithm = nil
canonMethod := s.signedInfo.SelectElement("CanonicalizationMethod")
var canonAlgoURI string
if canonMethod == nil {
return errors.New("signedxml: Unable to find CanonicalizationMethod element")
}
canonAlgoURI = canonMethod.SelectAttrValue("Algorithm", "")
if canonAlgoURI == "" {
return errors.New("signedxml: Unable to find Algorithm in " +
"CanonicalizationMethod element")
}
canonAlgo, ok := CanonicalizationAlgorithms[canonAlgoURI]
if ok {
s.canonAlgorithm = canonAlgo
return nil
}
return errors.New("signedxml: Unsupported Algorithm " + canonAlgoURI + " in " +
"CanonicalizationMethod")
}
func (s *signatureData) getReferencedXML(reference *etree.Element, inputDoc *etree.Document) (outputDoc *etree.Document, err error) {
uri := reference.SelectAttrValue("URI", "")
uri = strings.Replace(uri, "#", "", 1)
// populate doc with the referenced xml from the Reference URI
if uri == "" {
outputDoc = inputDoc
// // the above does not remove XML declarations from the root doc,
// // this fixes it, though it should be done by canonicalization:
// outputDoc = etree.NewDocument()
// outputDoc.SetRoot(inputDoc.Root())
} else {
refIDAttribute := "ID"
if s.refIDAttribute != "" {
refIDAttribute = s.refIDAttribute
}
// path := fmt.Sprintf(".//[@%s='%s']", refIDAttribute, uri)
// e := inputDoc.FindElement(path)
// if e != nil {
// outputDoc = etree.NewDocument()
// outputDoc.SetRoot(e.Copy())
if e := inputDoc.FindElement(fmt.Sprintf(".//[@%s='%s']", refIDAttribute, uri)); e != nil {
outputDoc = etree.NewDocument()
outputDoc.SetRoot(e.Copy())
} else if e := inputDoc.FindElement(fmt.Sprintf(".//[@%s='%s']", strings.ToLower(refIDAttribute), uri)); e != nil {
outputDoc = etree.NewDocument()
outputDoc.SetRoot(e.Copy())
} else if e := inputDoc.FindElement(fmt.Sprintf(".//[@%s='%s']", strings.Title(strings.ToLower(refIDAttribute)), uri)); e != nil {
outputDoc = etree.NewDocument()
outputDoc.SetRoot(e.Copy())
} else {
// SAML v1.1 Assertions use AssertionID
path := fmt.Sprintf(".//[@AssertionID='%s']", uri)
e := inputDoc.FindElement(path)
if e != nil {
outputDoc = etree.NewDocument()
outputDoc.SetRoot(e.Copy())
}
}
}
if outputDoc == nil {
return nil, errors.New("signedxml: unable to find refereced xml")
}
return outputDoc, nil
}
func LoadCertFromPEMString(pemString, pubKeyType string) (*x509.Certificate, error) {
var pubkey string
switch {
case strings.EqualFold("PUBLIC KEY", pubKeyType):
pubkey = fmt.Sprintf("-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----",
pemString)
case strings.EqualFold("CERTIFICATE", pubKeyType):
pubkey = fmt.Sprintf("-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----",
pemString)
}
pemBlock, _ := pem.Decode([]byte(pubkey))
if pemBlock == nil {
return &x509.Certificate{}, errors.New("Could not parse Public Key PEM")
}
if pemBlock.Type != "PUBLIC KEY" && pemBlock.Type != "CERTIFICATE" {
return &x509.Certificate{}, errors.New("Found wrong key type")
}
cert, err := x509.ParseCertificate(pemBlock.Bytes)
return cert, err
}
func processTransform(transform *etree.Element,
docIn *etree.Document) (docOut *etree.Document, err error) {
transformAlgoURI := transform.SelectAttrValue("Algorithm", "")
if transformAlgoURI == "" {
return nil, errors.New("signedxml: unable to find Algorithm in Transform")
}
transformAlgo, ok := CanonicalizationAlgorithms[transformAlgoURI]
if !ok {
return nil, fmt.Errorf("signedxml: unable to find matching transform"+
"algorithm for %s in CanonicalizationAlgorithms", transformAlgoURI)
}
var transformContent string
if transform.ChildElements() != nil {
tDoc := etree.NewDocument()
tDoc.SetRoot(transform.Copy())
transformContent, err = tDoc.WriteToString()
if err != nil {
return nil, err
}
}
docString, err := docIn.WriteToString()
if err != nil {
return nil, err
}
docString, err = transformAlgo.Process(docString, transformContent)
if err != nil {
return nil, err
}
docOut = etree.NewDocument()
docOut.ReadFromString(docString)
return docOut, nil
}
// func calculateHash(reference *etree.Element, doc *etree.Document) (string, error) {
// digestMethodElement := reference.SelectElement("DigestMethod")
// if digestMethodElement == nil {
// return "", errors.New("signedxml: unable to find DigestMethod")
// }
// digestMethodURI := digestMethodElement.SelectAttrValue("Algorithm", "")
// if digestMethodURI == "" {
// return "", errors.New("signedxml: unable to find Algorithm in DigestMethod")
// }
// digestAlgo, ok := hashAlgorithms[digestMethodURI]
// if !ok {
// return "", fmt.Errorf("signedxml: unable to find matching hash"+
// "algorithm for %s in hashAlgorithms", digestMethodURI)
// }
// doc.WriteSettings.CanonicalEndTags = true
// doc.WriteSettings.CanonicalText = true
// doc.WriteSettings.CanonicalAttrVal = true
// h := digestAlgo.New()
// docBytes, err := doc.WriteToBytes()
// if err != nil {
// return "", err
// }
// // ioutil.WriteFile("C:/Temp/SignedXML/Suspect.xml", docBytes, 0644)
// // s, _ := doc.WriteToString()
// // logger.Println(s)
// h.Write(docBytes)
// d := h.Sum(nil)
// calculatedValue := base64.StdEncoding.EncodeToString(d)
// return calculatedValue, nil
// }
// calculates a hash of a TargetToBeHashed (*etree.Document or []byte), detecting
// the hash algorithm in the reference element. If successful, hash digest value in
// base64 encoded string is written to the reference element/DigestValue tag.
func CalculateHashFromRef(reference *etree.Element, targetToBeHashed interface{}) (string, error) {
digestMethodElement := reference.SelectElement("DigestMethod")
if digestMethodElement == nil {
return "", errors.New("signedxml: unable to find DigestMethod")
}
digestMethodURI := digestMethodElement.SelectAttrValue("Algorithm", "")
if digestMethodURI == "" {
return "", errors.New("signedxml: unable to find Algorithm in DigestMethod")
}
digestAlgo, ok := hashAlgorithms[digestMethodURI]
if !ok {
return "", fmt.Errorf("signedxml: unable to find matching hash"+
"algorithm for %s in hashAlgorithms", digestMethodURI)
}
var targetBytes []byte
var err error
switch v := targetToBeHashed.(type) {
case *etree.Document:
v.WriteSettings.CanonicalEndTags = true
v.WriteSettings.CanonicalText = true
v.WriteSettings.CanonicalAttrVal = true
targetBytes, err = v.WriteToBytes()
if err != nil {
return "", err
}
case []byte:
targetBytes = v
}
// debug
// fn := strconv.FormatInt(time.Now().UnixNano(), 10) + ".xml" // unix-time based filename
// f, err := os.Create(fn)
// if err != nil {
// panic(err)
// }
// defer f.Close()
// _, err = f.Write(targetBytes)
// if err != nil {
// panic(err)
// }
h := digestAlgo.New() // hasher
h.Write(targetBytes) // calculate hash
d := h.Sum(nil) // digest
calculatedValue := base64.StdEncoding.EncodeToString(d) // digest in base64
return calculatedValue, nil
}
// calculates a hash of a targetToBeHashed ([]byte), detecting the hash algorithm
// by the URI string. The URI follows notation common for XML Signatures. If successfull,
// it outputs base64 encoded string of a target hash digest (fingerprint).
func CalculateHash(targetToBeHashed []byte, digestMethodURI string) (string, error) {
digestAlgo, ok := hashAlgorithms[digestMethodURI]
if !ok {
return "", fmt.Errorf("signedxml: unable to find matching hash"+
"algorithm for %s in hashAlgorithms", digestMethodURI)
}
h := digestAlgo.New() // hasher
_, err := h.Write(targetToBeHashed) // calculate hash
if err != nil {
return "", fmt.Errorf("signedxml: hashing error: %s", err)
}
d := h.Sum(nil) // digest
calculatedValue := base64.StdEncoding.EncodeToString(d) // digest in base64
return calculatedValue, nil
}
// Copies all namespaces that related to the targetElement. It must have the following namespaces:
// - own namespaces (if it defines such): nothing todo here, typically, they're defined in attributes of that element;
// - if the element has a prefix, but no definition for it, then parent has this namespace defined;
// - if any of the sub-elementas have a prefix, which is different from targetElement, then some parent must define it.
// Needed before canonicalizing and calculating hash of the target Element.
// TargetElem is always a sub-tag (child) of RootDoc
func PopulateElementWithNameSpaces(targetElem *etree.Element, rootDoc *etree.Document) (err error) { //(outputDoc *etree.Document, err error) {
// check that targetElem is a child of rootDoc
if rootDoc.FindElement(".//"+targetElem.Tag) != nil {
// Step 1: cycle through all prefixes used in the targetElement,
// these will be namespace definitions we'll have to have in the element
nsDefinitions := getUsedPrefixes(targetElem)
// Step1.5: check if namespace definitions has an empty value (indicicator of
// default namespace). If it doesn't exists, check if any parents above the element
// have xmlns defined - if so, add this used nsDefinitions
if _, ok := nsDefinitions[""]; !ok { // if no empty k name exists
if checkIfParentsUseDefaultNS(targetElem, rootDoc) {
nsDefinitions[""] = ""
}
}
// Step 2: starting with the targetElem, work up the path until all
// prefix keys (namespace names) have their corresponding definitions collected
nsDefinitions = getNameSpaceDefinitions(nsDefinitions, targetElem, rootDoc)
// Step 3: populate the targetElem with the namespaces, relevant for it
// setNSDefinitionsDynamically(targetElem, nsDefinitions, []string{})
setNSDefinitions(targetElem, nsDefinitions)
} else if targetElem.FullTag() == rootDoc.FullTag() {
targetElem = rootDoc.Root()
} else {
err = errors.New("targetElem is not in the rootDoc, cannot copy namespaces")
}
return
}
// MOD: setts namespaces on the element, given in nsdef
func setNSDefinitions(el *etree.Element, nsdef map[string]string) {
for k, v := range nsdef {
if k == "" {
el.CreateAttr("xmlns", v)
} else {
el.CreateAttr("xmlns:"+k, v)
}
}
}
// too complext, aimed at setting namespace where it is used
func setNSDefinitionsDynamically(el *etree.Element, nsdef map[string]string, parentPrefixes []string) {
if el.Space != "" && !isInArray(el.Space, parentPrefixes) {
el.CreateAttr("xmlns:"+el.Space, nsdef[el.Space])
parentPrefixes = append(parentPrefixes, el.Space)
}
for _, c := range el.ChildElements() {
setNSDefinitionsDynamically(c, nsdef, parentPrefixes)
}
}
// checks if items is in array
func isInArray(item string, array []string) bool {
for _, i := range array {
if item == i {
return true
}
}
return false
}
// returns a map, where its keys are the unique prefixes used in the
// element and its children
func getUsedPrefixes(el *etree.Element) (outMap map[string]string) {
// Space is element tag prefix. If it's emtpy, then this element has root namespace.
// if it's not empty, then it's defined somewhere up the element path.
outMap = map[string]string{}
outMap[el.Space] = "" // process element prefix
for _, c := range el.ChildElements() {
childMap := getUsedPrefixes(c) // process its children prefixes
for k, v := range childMap {
outMap[k] = v
}
}
return outMap
}
// checks if any of the parents above define default namespace (attribute "xmlns=...")
func checkIfParentsUseDefaultNS(el *etree.Element, rootDoc *etree.Document) bool {
if attr := el.SelectAttr("xmlns"); attr != nil {
return true
}
upNext := rootDoc.FindElement(".//" + el.Tag).Parent()
if upNext != nil {
return checkIfParentsUseDefaultNS(upNext, rootDoc)
}
return false
}
// takes a map of prefixes and cycles up the path from the element to
// collect its definitions
func getNameSpaceDefinitions(prefixMap map[string]string, el *etree.Element, rootDoc *etree.Document) (outMap map[string]string) {
var weHaveUnfilledValues bool
for k, v := range prefixMap {
if v == "" {
weHaveUnfilledValues = true
if attr := el.SelectAttr(k); attr != nil {
prefixMap[k] = attr.Value
} else if attr := el.SelectAttr("xmlns:" + k); attr != nil {
prefixMap[k] = attr.Value
} else if attr := el.SelectAttr("xmlns"); attr != nil { // root NS
if _, ok := prefixMap[""]; ok { // make sure non-prefixed tags were in element
prefixMap[""] = attr.Value
}
}
}
}
upNext := rootDoc.FindElement(".//" + el.Tag).Parent()
if weHaveUnfilledValues && upNext != nil {
parentMap := getNameSpaceDefinitions(prefixMap, upNext, rootDoc)
for k, v := range parentMap {
if prefixMap[k] == "" && v != "" {
prefixMap[k] = v
}
}
}
outMap = prefixMap
return
}