forked from moov-io/signedxml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signedxml_test.go
454 lines (405 loc) · 16.9 KB
/
signedxml_test.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
package signedxml
import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/beevik/etree"
. "github.com/smartystreets/goconvey/convey"
)
func TestSign(t *testing.T) {
Convey("Given an XML, certificate, and RSA key", t, func() {
pemString, _ := ioutil.ReadFile("./testdata/rsa.crt")
pemBlock, _ := pem.Decode([]byte(pemString))
cert, _ := x509.ParseCertificate(pemBlock.Bytes)
pemString, _ = ioutil.ReadFile("./testdata/rsa.key")
pemBlock, _ = pem.Decode([]byte(pemString))
key, _ := x509.ParsePKCS1PrivateKey(pemBlock.Bytes)
xml, _ := ioutil.ReadFile("./testdata/nosignature.xml")
Convey("When generating the signature", func() {
signer, _ := NewSigner(string(xml))
xmlStr, err := signer.Sign(key)
Convey("Then no error occurs", func() {
So(err, ShouldBeNil)
})
Convey("And the signature should be valid", func() {
validator, _ := NewValidator(xmlStr)
validator.Certificates = append(validator.Certificates, *cert)
err := validator.Validate()
So(err, ShouldBeNil)
})
})
})
}
func TestValidate(t *testing.T) {
Convey("Given valid signed XML", t, func() {
cases := map[string]string{
"(WSFed BBAuth Metadata)": "./testdata/bbauth-metadata.xml",
"(SAML External NS)": "./testdata/saml-external-ns.xml",
"(Signature w/Inclusive NS)": "./testdata/signature-with-inclusivenamespaces.xml",
"(SAML)": "./testdata/valid-saml.xml",
// this one doesn't appear to follow the spec... ( http://webservices20.blogspot.co.il/2013/06/validating-windows-mobile-app-store.html)
//"(Windows Store Signature)": "./testdata/windows-store-signature.xml",
"(WSFed Generic Metadata)": "./testdata/wsfed-metadata.xml",
}
for description, test := range cases {
Convey(fmt.Sprintf("When Validate is called %s", description), func() {
xmlFile, err := os.Open(test)
if err != nil {
fmt.Println("Error opening file:", err)
}
defer xmlFile.Close()
xmlBytes, _ := ioutil.ReadAll(xmlFile)
validator, _ := NewValidator(string(xmlBytes))
err = validator.Validate()
Convey("Then no error occurs", func() {
So(err, ShouldBeNil)
So(validator.SigningCert().PublicKey, ShouldNotBeNil)
})
})
}
Convey("When Validate is called with an external Signature", func() {
xmlFile, _ := os.Open("./testdata/bbauth-metadata.xml")
sig := `<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/><SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><Reference URI="#_69b42076-409e-4476-af41-339962e49427"><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><DigestValue>LPHoiAkLmA/TGIuVbgpwlFLXL+ymEBc7TS0fC9/PTQU=</DigestValue></Reference></SignedInfo><SignatureValue>d2CXq9GEeDKvMxdpxtTRKQ8TGeSWhJOVPs8LMD0ObeE1t/YGiAm9keorMiki4laxbWqAuOmwHK3qNHogRFgkIYi3fnuFBzMrahXf0n3A5PRXXW1m768Z92GKV09pGuygKUXCtXzwq0seDi6PnzMCJFzFXGQWnum0paa8Oz+6425Sn0zO0fT3ttp3AXeXGyNXwYPYcX1iEMB7klUlyiz2hmn8ngCIbTkru7uIeyPmQ5WD4SS/qQaL4yb3FZibXoe/eRXrbkG1NAJCw9OWw0jsvWncE1rKFaqEMbz21fXSDhh3Ls+p9yVf+xbCrpkT0FMqjTHpNRvccMPZe/hDGrHV7Q==</SignatureValue><KeyInfo><X509Data><X509Certificate>MIIDNzCCAh+gAwIBAgIQQVK+d/vLK4ZNMDk15HGUoTANBgkqhkiG9w0BAQ0FADAoMSYwJAYDVQQDEx1CbGFja2JhdWQgQXV0aGVudGljYXRpb24gMjAyMjAeFw0wMDAxMDEwNDAwMDBaFw0yMjAxMDEwNDAwMDBaMCgxJjAkBgNVBAMTHUJsYWNrYmF1ZCBBdXRoZW50aWNhdGlvbiAyMDIyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArgByjSPVvP4DLf/l7QRz7G7Dhkdns0QjWslnWejHlFIezfkJ4NGPp0+5CRCFYBqAb7DhqyK77Ek5xdzmwgYb1X6GD6UDltWvN5BBFAw69I6/K0WjguFUxk19T7xdc8vTCNAMi+6Ys49O3EBNnI2fiqDoBdMjUTud1F04QY3N2rZWkjMrHV+CnzhoUwqsO/ABWrDbkPzBXdOOIbsKH0k0IP8q2+35pe1y2nxtB9f1fCyCmbUH2HINMHahDmxxanTW5Jy14yD/HSRTFQF9JMTeglomWq5q9VPx0NjsEJR+B5IkRCTf75LoYrrr/fvQm3aummmYPdHauXCBrcm0moX4ywIDAQABo10wWzBZBgNVHQEEUjBQgBDCHOfardZfhltQSbLqsukZoSowKDEmMCQGA1UEAxMdQmxhY2tiYXVkIEF1dGhlbnRpY2F0aW9uIDIwMjKCEEFSvnf7yyuGTTA5NeRxlKEwDQYJKoZIhvcNAQENBQADggEBADrOhfRiynRKGD7EHohpPrltFScJ9+QErYMhEvteqh3C48T99uKgDY8wTqv+PI08QUSZuhmmF2d+W7aRBo3t8ZZepIXCwDaKo/oUp2h5Y9O3vyGDguq5ptgDTmPNYDCwWtdt0TtQYeLtCQTJVbYByWL0eT+KdzQOkAi48cPEOObSc9Biga7LTCcbCVPeJlYzmHDQUhzBt2jcy5BGvmZloI5SsoZvve6ug74qNq8IJMyzJzUp3kRuB0ruKIioSDi1lc783LDT3LSXyIbOGw/vHBEBY4Ax7FK8CqXJ2TsYqVsyo8QypqXDnveLcgK+PNEAhezhxC9hyV8j1I8pfF72ABE=</X509Certificate></X509Data></KeyInfo></Signature>`
defer xmlFile.Close()
xmlBytes, _ := ioutil.ReadAll(xmlFile)
validator := Validator{}
validator.SetXML(string(xmlBytes))
validator.SetSignature(sig)
err := validator.Validate()
Convey("Then no error occurs", func() {
So(err, ShouldBeNil)
So(validator.SigningCert().PublicKey, ShouldNotBeNil)
})
})
Convey("When Validate is called with an external certificate and root xmlns", func() {
xmlFile, _ := os.Open("./testdata/rootxmlns.xml")
pemString, _ := ioutil.ReadFile("./testdata/rootxmlns.crt")
pemBlock, _ := pem.Decode([]byte(pemString))
cert, _ := x509.ParseCertificate(pemBlock.Bytes)
defer xmlFile.Close()
xmlBytes, _ := ioutil.ReadAll(xmlFile)
validator := Validator{}
validator.SetXML(string(xmlBytes))
validator.Certificates = append(validator.Certificates, *cert)
err := validator.Validate()
Convey("Then no error occurs", func() {
So(err, ShouldBeNil)
So(validator.SigningCert().PublicKey, ShouldNotBeNil)
})
})
})
Convey("Given invalid signed XML", t, func() {
cases := map[string]string{
"(Changed Content)": "./testdata/invalid-signature-changed content.xml",
"(Non-existing Reference)": "./testdata/invalid-signature-non-existing-reference.xml",
"(Wrong Sig Value)": "./testdata/invalid-signature-signature-value.xml",
}
for description, test := range cases {
Convey(fmt.Sprintf("When Validate is called %s", description), func() {
xmlFile, err := os.Open(test)
if err != nil {
fmt.Println("Error opening file:", err)
}
defer xmlFile.Close()
xmlBytes, _ := ioutil.ReadAll(xmlFile)
validator, _ := NewValidator(string(xmlBytes))
err = validator.Validate()
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
}
})
}
func TestEnvelopedSignatureProcess(t *testing.T) {
Convey("Given a document without a Signature elemement", t, func() {
doc := "<doc></doc>"
Convey("When Process is called", func() {
envSig := EnvelopedSignature{}
_, err := envSig.Process(doc, "")
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
})
Convey("Given an unparented signature element", t, func() {
doc := "<Signatrue></Signature>"
Convey("When Process is called", func() {
envSig := EnvelopedSignature{}
_, err := envSig.Process(doc, "")
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
})
}
func TestSignatureDataParsing(t *testing.T) {
Convey("Given a document without a Signature elemement", t, func() {
doc := etree.NewDocument()
doc.CreateElement("root")
Convey("When parseEnvelopedSignature is called", func() {
sigData := signatureData{xml: doc}
err := sigData.parseEnvelopedSignature()
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
})
Convey("Given a document without a SignedInfo elemement", t, func() {
doc := etree.NewDocument()
doc.CreateElement("root").CreateElement("Signature")
sigData := signatureData{xml: doc}
sigData.parseEnvelopedSignature()
Convey("When parseSignedInfo is called", func() {
err := sigData.parseSignedInfo()
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
})
Convey("Given a document without a SignatureValue elemement", t, func() {
doc := etree.NewDocument()
doc.CreateElement("root").CreateElement("Signature")
sigData := signatureData{xml: doc}
sigData.parseEnvelopedSignature()
Convey("When parseSigValue is called", func() {
err := sigData.parseSigValue()
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
})
Convey("Given a document without a SignatureMethod elemement", t, func() {
doc := etree.NewDocument()
doc.CreateElement("root").CreateElement("Signature").CreateElement("SignedInfo")
sigData := signatureData{xml: doc}
sigData.parseEnvelopedSignature()
sigData.parseSignedInfo()
Convey("When parseSigAlgorithm is called", func() {
err := sigData.parseSigAlgorithm()
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
})
Convey("Given a document without a SignatureMethod Algorithm element", t, func() {
doc := etree.NewDocument()
doc.CreateElement("root").CreateElement("Signature").CreateElement("SignedInfo").CreateElement("SignatureMethod")
sigData := signatureData{xml: doc}
sigData.parseEnvelopedSignature()
sigData.parseSignedInfo()
Convey("When parseSigAlgorithm is called", func() {
err := sigData.parseSigAlgorithm()
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
})
Convey("Given a document without a CanonicalizationMethod elemement", t, func() {
doc := etree.NewDocument()
doc.CreateElement("root").CreateElement("Signature").CreateElement("SignedInfo")
sigData := signatureData{xml: doc}
sigData.parseEnvelopedSignature()
sigData.parseSignedInfo()
Convey("When parseCanonAlgorithm is called", func() {
err := sigData.parseCanonAlgorithm()
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
})
Convey("Given a document without a CanonicalizationMethod Algorithm element", t, func() {
doc := etree.NewDocument()
doc.CreateElement("root").CreateElement("Signature").CreateElement("SignedInfo").CreateElement("CanonicalizationMethod")
sigData := signatureData{xml: doc}
sigData.parseEnvelopedSignature()
sigData.parseSignedInfo()
Convey("When parseCanonAlgorithm is called", func() {
err := sigData.parseCanonAlgorithm()
Convey("Then an error occurs", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "signedxml:")
})
})
})
}
var example31Input = `<?xml version="1.0"?>
<?xml-stylesheet href="doc.xsl" type="text/xsl" ?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc>Hello, world!<!-- Comment 1 --></doc>
<?pi-without-data ?>
<!-- Comment 2 -->
<!-- Comment 3 -->`
var example31Output = `<?xml-stylesheet href="doc.xsl" type="text/xsl" ?>
<doc>Hello, world!</doc>
<?pi-without-data?>`
var example31OutputWithComments = `<?xml-stylesheet href="doc.xsl" type="text/xsl" ?>
<doc>Hello, world!<!-- Comment 1 --></doc>
<?pi-without-data?>
<!-- Comment 2 -->
<!-- Comment 3 -->`
var example32Input = `<doc>
<clean> </clean>
<dirty> A B </dirty>
<mixed>
A
<clean> </clean>
B
<dirty> A B </dirty>
C
</mixed>
</doc>`
var example32Output = `<doc>
<clean> </clean>
<dirty> A B </dirty>
<mixed>
A
<clean> </clean>
B
<dirty> A B </dirty>
C
</mixed>
</doc>`
var example33Input = `<!DOCTYPE doc [<!ATTLIST e9 attr CDATA "default">]>
<doc>
<e1 />
<e2 ></e2>
<e3 name = "elem3" id="elem3" />
<e4 name="elem4" id="elem4" ></e4>
<e5 a:attr="out" b:attr="sorted" attr2="all" attr="I'm" xmlns:b="http://www.ietf.org" xmlns:a="http://www.w3.org" xmlns="http://example.org"/>
<e6 xmlns="" xmlns:a="http://www.w3.org">
<e7 xmlns="http://www.ietf.org">
<e8 xmlns="" xmlns:a="http://www.w3.org">
<e9 xmlns="" xmlns:a="http://www.ietf.org"/>
</e8>
</e7>
</e6>
</doc>`
var example33Output = `<doc>
<e1></e1>
<e2></e2>
<e3 id="elem3" name="elem3"></e3>
<e4 id="elem4" name="elem4"></e4>
<e5 xmlns="http://example.org" xmlns:a="http://www.w3.org" xmlns:b="http://www.ietf.org" attr="I'm" attr2="all" b:attr="sorted" a:attr="out"></e5>
<e6 xmlns:a="http://www.w3.org">
<e7 xmlns="http://www.ietf.org">
<e8 xmlns="">
<e9 xmlns:a="http://www.ietf.org" attr="default"></e9>
</e8>
</e7>
</e6>
</doc>`
var example34Input = `<!DOCTYPE doc [
<!ATTLIST normId id ID #IMPLIED>
<!ATTLIST normNames attr NMTOKENS #IMPLIED>
]>
<doc>
<text>First line
 Second line</text>
<value>2</value>
<compute><![CDATA[value>"0" && value<"10" ?"valid":"error"]]></compute>
<compute expr='value>"0" && value<"10" ?"valid":"error"'>valid</compute>
<norm attr=' '   
	 ' '/>
<normNames attr=' A   
	 B '/>
</doc>`
var example34Output = `<doc>
<text>First line
Second line</text>
<value>2</value>
<compute>value>"0" && value<"10" ?"valid":"error"</compute>
<compute expr="value>"0" && value<"10" ?"valid":"error"">valid</compute>
<norm attr=" ' 
	 ' "></norm>
<normNames attr="A 
	 B"></normNames>
</doc>`
// modified to not include DTD processing. still tests for whitespace treated as
// CDATA
var example34ModifiedOutput = `<doc>
<text>First line
Second line</text>
<value>2</value>
<compute>value>"0" && value<"10" ?"valid":"error"</compute>
<compute expr="value>"0" && value<"10" ?"valid":"error"">valid</compute>
<norm attr=" ' 
	 ' "></norm>
<normNames attr=" A 
	 B "></normNames>
</doc>`
var example35Input = `<!DOCTYPE doc [
<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>
<!ENTITY ent1 "Hello">
<!ENTITY ent2 SYSTEM "world.txt">
<!ENTITY entExt SYSTEM "earth.gif" NDATA gif>
<!NOTATION gif SYSTEM "viewgif.exe">
]>
<doc attrExtEnt="entExt">
&ent1;, &ent2;!
</doc>
<!-- Let world.txt contain "world" (excluding the quotes) -->`
var example35Output = `<doc attrExtEnt="entExt">
Hello, world!
</doc>`
var example36Input = `<?xml version="1.0" encoding="ISO-8859-1"?>
<doc>©</doc>`
var example36Output = "<doc>\u00A9</doc>"
var example37Input = `<!DOCTYPE doc [
<!ATTLIST e2 xml:space (default|preserve) 'preserve'>
<!ATTLIST e3 id ID #IMPLIED>
]>
<doc xmlns="http://www.ietf.org" xmlns:w3c="http://www.w3.org">
<e1>
<e2 xmlns="">
<e3 id="E3"/>
</e2>
</e1>
</doc>`
var example37SubsetExpression = `<!-- Evaluate with declaration xmlns:ietf="http://www.ietf.org" -->
(//. | //@* | //namespace::*)
[
self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2))
or
count(id("E3")|ancestor-or-self::node()) = count(ancestor-or-self::node())
]`
var example37Output = `<e1 xmlns="http://www.ietf.org" xmlns:w3c="http://www.w3.org"><e3 xmlns="" id="E3" xml:space="preserve"></e3></e1>`
type exampleXML struct {
input string
output string
withComments bool
expression string
}
// test examples from the spec (www.w3.org/TR/2001/REC-xml-c14n-20010315#Examples)
func TestCanonicalizationExamples(t *testing.T) {
Convey("Given XML Input", t, func() {
cases := map[string]exampleXML{
"(Example 3.1 w/o Comments)": exampleXML{input: example31Input, output: example31Output},
"(Example 3.1 w/Comments)": exampleXML{input: example31Input, output: example31OutputWithComments, withComments: true},
"(Example 3.2)": exampleXML{input: example32Input, output: example32Output},
// 3.3 is for Canonical NOT ExclusiveCanonical (one of the exceptions here: http://www.w3.org/TR/xml-exc-c14n/#sec-Specification)
//"(Example 3.3)": exampleXML{input: example33Input, output: example33Output},
"(Example 3.4)": exampleXML{input: example34Input, output: example34ModifiedOutput},
//"(Example 3.5)": exampleXML{input: example35Input, output: example35Output},
// 3.6 will work, but requires a change to the etree package first:
// http://stackoverflow.com/questions/6002619/unmarshal-an-iso-8859-1-xml-input-in-go
//"(Example 3.6)": exampleXML{input: example36Input, output: example36Output},
//"(Example 3.7)": exampleXML{input: example37Input, output: example37Output, expression: example37SubsetExpression},
}
for description, test := range cases {
Convey(fmt.Sprintf("When transformed %s", description), func() {
transform := ExclusiveCanonicalization{WithComments: test.withComments}
resultXML, err := transform.Process(test.input, "")
Convey("Then the resulting XML match the example output", func() {
So(err, ShouldBeNil)
So(resultXML, ShouldEqual, test.output)
})
})
}
})
}