Class: TC_TSIK

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
ext/xmlsig/t/tc_tsik.rb

Instance Method Summary collapse

Instance Method Details

#HMACKeyObject

Port of the TSIK org.apache.tsik.xmlsig.test.XmlSigTestMerlin23 class



459
460
461
462
463
# File 'ext/xmlsig/t/tc_tsik.rb', line 459

def HMACKey
    key = Xmlsig::Key.new
    key.loadHMACFromString('secret')
    return key
end

#loadDoc(filename) ⇒ Object



49
50
51
52
53
54
# File 'ext/xmlsig/t/tc_tsik.rb', line 49

def loadDoc(filename)
    doc = Xmlsig::XmlDoc.new
    fullfilename = resdir() + filename
    assert_equal(0, doc.loadFromFile(fullfilename), "loadFromFile of a XmlDoc")
    return doc
end

#privateKeyObject



41
42
43
44
45
46
47
# File 'ext/xmlsig/t/tc_tsik.rb', line 41

def privateKey
    key = Xmlsig::Key.new
    assert_equal(0, key.loadFromFile(resdir() + 'mypriv.pem', 'pem', ''), "loadFromFile of a key")
    assert_equal(1, key.isValid(), "isValid key")
    key.setName("")
    return key
end

#publicKeyObject



34
35
36
37
38
39
# File 'ext/xmlsig/t/tc_tsik.rb', line 34

def publicKey
    key = Xmlsig::Key.new
    assert_equal(0, key.loadFromFile(resdir() + 'mypub.pem', 'pem', ''), "loadFromFile of a key")
    assert_equal(1, key.isValid(), "isValid key")
    return key
end

#resdirObject

def teardown end



30
31
32
# File 'ext/xmlsig/t/tc_tsik.rb', line 30

def resdir
    return 't/res/tsik_tcport/'
end

#sign1(inFileName, privateKey, publicKey, cert) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'ext/xmlsig/t/tc_tsik.rb', line 56

def sign1(inFileName, privateKey, publicKey, cert)
    doc = loadDoc(inFileName)
    if (cert != NIL)
        signer = Xmlsig::Signer.new(doc, privateKey)
        signer.addCertFromFile(cert, 'pem')
        return signer
    elsif (publicKey != NIL)
        return Xmlsig::Signer.new(doc, privateKey, publicKey)
    else
  return Xmlsig::Signer.new(doc, privateKey)
    end
end

#sign2(signer, type, outFileName) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'ext/xmlsig/t/tc_tsik.rb', line 69

def sign2(signer, type, outFileName)
    if (type == "")
        d = signer.sign()
    else
        d = signer.sign(Xmlsig::XPath.new(type))
        if (outFileName != NIL)
            d.toFile(outFileName)
        end
    end
    return d
end

#test_BadXPath1Object



338
339
340
341
342
343
344
345
346
# File 'ext/xmlsig/t/tc_tsik.rb', line 338

def test_BadXPath1
    ### TSIK XmlSigTest.testBadXPath1
    signer = sign1('in.xml', privateKey(), publicKey(), NIL)
    xpath = Xmlsig::XPath.new('bad xpath')
    signer.addReference(xpath)
    d = sign2(signer, '/', NIL)
    rescue
    assert_equal(true, d.nil?)
end

#test_BadXPath2Object



348
349
350
351
352
353
354
355
356
# File 'ext/xmlsig/t/tc_tsik.rb', line 348

def test_BadXPath2
    ### TSIK XmlSigTest.testBadXPath2
    signer = sign1('in.xml', privateKey(), publicKey(), NIL)
    xpath = Xmlsig::XPath.new('here()')
    signer.addReference(xpath)
    d = sign2(signer, '/', NIL)
    rescue
    assert_equal(true, d.nil?)
end

#test_CertObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'ext/xmlsig/t/tc_tsik.rb', line 252

def test_Cert
    ### TSIK XmlSigTest.testCert
    signer = sign1('in.xml', privateKey(), NIL, resdir() + 'mycert.x509')
    signer.attachPublicKey(1)
    assert_equal(false, signer.nil?)
    doc = sign2(signer, '/', NIL)
    assert_equal(false, doc.nil?)
    sigLoc = '/books/ds:Signature'

    assert_equal(true, verify(doc, sigLoc, publicKey(), 0, 0), "Verify")
    assert_equal(true, verify(doc, sigLoc, NIL, 1, 0), "Verify")
    assert_equal(true, verify(doc, sigLoc, NIL, 1, 1), "Verify")
    xpath = Xmlsig::XPath.new(sigLoc)
    xpath.addNamespace("ds", "http://www.w3.org/2000/09/xmldsig#")
    verifier = Xmlsig::Verifier.new(doc, xpath)
    l = verifier.getCertificateChain()
    assert_equal(1, l.length, "Chain length")
end

#test_CertOnlyObject



200
201
202
203
204
205
206
207
# File 'ext/xmlsig/t/tc_tsik.rb', line 200

def test_CertOnly
    ### TSIK XmlSigTest.testCertOnly
    doc = loadDoc('testCertOnly_in.xml')
    xpath = Xmlsig::XPath.new('//ds:Signature')
    xpath.addNamespace('ds', "http://www.w3.org/2000/09/xmldsig#")
    v = Xmlsig::Verifier.new(doc, xpath)
    assert_equal(1, v.verify(), "Verify")
end

#test_DetachedObject



313
314
315
316
317
318
319
320
# File 'ext/xmlsig/t/tc_tsik.rb', line 313

def test_Detached
    ### TSIK XmlSigTest.testDetached
    signer = sign1('in.xml', privateKey(), publicKey(), NIL)
    doc = sign2(signer, '/books/book[1]', NIL)
    assert_equal(false, doc.nil?)
    assert_equal(true, verify(doc, '/books/book[1]/ds:Signature', publicKey(), 0, 0), "Verify")
    assert_equal(true, verify(doc, '/books/book[1]/ds:Signature', NIL, 1, 0), "Verify")
end

#test_EmptyListObject

Port of the TSIK org.apache.tsik.xmlsig.test.XmlSigTestExcC14n class



404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'ext/xmlsig/t/tc_tsik.rb', line 404

def test_EmptyList
    ### TSIK XmlSigTestExcC14n.testEmptyList
    s = sign1('testStele.xml', privateKey(), publicKey(), NIL)
    s.useExclusiveCanonicalizer('')
    xp = Xmlsig::XPath.new("//*[@Id='ID1']")
    # Unused in TSIK, if we leave this out our Reference URIs match
    #xp.addNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/")
    s.addReference(xp)
    doc = sign2(s, '/', NIL)
    assert_equal(false, doc.nil?)
    assert_equal(true, verify(doc, '//ds:Signature', publicKey(), 0, 0), "Verify")
    assert_equal(true, verify(doc, '//ds:Signature', NIL, 1, 0), "Verify")
end

#test_EmptyNamespaceObject



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ext/xmlsig/t/tc_tsik.rb', line 227

def test_EmptyNamespace
    ### TSIK XmlSigTest.testEmptyNamespace
    # original testEmptyNamespace.xml was
    #  <elem xmlns="default namespace"/>
    # changed to
    #  <elem xmlns="http://default/namespace"/>
    # because the former was being flagged by the libxml2 parser as
    # having an invalid URI
    signer = sign1('testEmptyNamespace.xml', privateKey(), publicKey(), NIL)
    doc = sign2(signer, '/', NIL)
    assert_equal(false, doc.nil?)
    assert_equal(true, verify(doc, '//ds:Signature', publicKey(), 0, 0), "Verify")
end

#test_EnvelopedObject



296
297
298
299
300
301
302
# File 'ext/xmlsig/t/tc_tsik.rb', line 296

def test_Enveloped
    ### TSIK XmlSigTest.testEnveloped
    signer = sign1('in.xml', privateKey(), publicKey(), NIL)
    doc = sign2(signer, '/', NIL)
    assert_equal(false, doc.nil?)
    assert_equal(true, verify(doc, '/books/ds:Signature', publicKey(), 0, 0), "Verify")
end

#test_EnvelopingObject



304
305
306
307
308
309
310
311
# File 'ext/xmlsig/t/tc_tsik.rb', line 304

def test_Enveloping
    ### TSIK XmlSigTest.testEnveloping
    signer = sign1('in.xml', privateKey(), publicKey(), NIL)
    doc = sign2(signer, '', NIL)
    assert_equal(false, doc.nil?)
    assert_equal(true, verify(doc, '/ds:Signature', publicKey(), 0, 0), "Verify")
    assert_equal(true, verify(doc, '/ds:Signature', NIL, 1, 0), "Verify")
end

#test_HmacObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/xmlsig/t/tc_tsik.rb', line 209

def test_Hmac
    ### TSIK XmlSigTest.testHmac
    doc = loadDoc('in.xml')
    sigKey = Xmlsig::Key.new
    sigKey.loadHMACFromString('ab')
    signer = Xmlsig::Signer.new(doc, sigKey)
    xpath = Xmlsig::XPath.new('/')
    d = signer.sign(xpath)
    xpath = Xmlsig::XPath.new('//ds:Signature')
    xpath.addNamespace('ds', "http://www.w3.org/2000/09/xmldsig#")
    v = Xmlsig::Verifier.new(d, xpath)
    verKey = Xmlsig::Key.new
    verKey.loadHMACFromString('ab')
    assert_equal(1, v.verify(verKey), "Verify")
    verKey.loadHMACFromString('bb')
    assert_equal(0, v.verify(verKey), "Verify")
end

#test_Merlin2Object



448
449
450
451
452
453
# File 'ext/xmlsig/t/tc_tsik.rb', line 448

def test_Merlin2
    ### TSIK XmlSigTestDigsig.testMerlin2
    doc = loadDoc("merlin-xmldsig-fifteen/signature-enveloping-rsa.xml")
    sigLoc = "//ds:Signature"
    assert_equal(true, verify(doc, sigLoc, NIL, 1, 0), "Verify")
end

#test_Merlin23Object



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
# File 'ext/xmlsig/t/tc_tsik.rb', line 465

def test_Merlin23
       ### TSIK XmlSigTestMerlin23
       xpath = Xmlsig::XPath.new("//ds:Signature")
       xpath.addNamespace("ds", "http://www.w3.org/2000/09/xmldsig#")
       for res in ['signature-enveloped-dsa',
                    'signature-enveloping-b64-dsa',
                    'signature-enveloping-rsa',
                    'signature-external-b64-dsa',
                    'signature-external-dsa',
                    'signature-x509-crt-crl',
                    'signature-x509-crt',
                    'signature-enveloping-hmac-sha1']
           fullfilename = resdir() + "merlin-xmldsig-twenty-three/" + res + ".xml"
           doc = Xmlsig::XmlDoc.new
           if doc.loadFromFile(fullfilename) < 0
               raise "IOError - Couldn't open XML file " + fullfilename
           end
           verifier = Xmlsig::Verifier.new(doc, xpath)
           if res == 'signature-enveloping-hmac-sha1'
               assert_equal(1, verifier.verify(HMACKey()), "Verify Merlin32")
           else 
               assert_equal(1, verifier.verify(), "Verify Merlin32")
           end
       end
end

#test_MerlinEnvelopedDsaObject



383
384
385
386
387
# File 'ext/xmlsig/t/tc_tsik.rb', line 383

def test_MerlinEnvelopedDsa
    ### TSIK XmlSigTest.testMerlinEnvelopedDsa
    doc = loadDoc("merlin-xmldsig-fifteen/signature-enveloped-dsa.xml")
    assert_equal(true, verify(doc, '//ds:Signature', NIL, 1, 0), "Verify")
end

#test_MerlinEnvelopingBase64DsaObject



395
396
397
398
399
# File 'ext/xmlsig/t/tc_tsik.rb', line 395

def test_MerlinEnvelopingBase64Dsa
    ### TSIK XmlSigTest.testMerlinEnvelopingBase64Dsa
    doc = loadDoc("merlin-xmldsig-fifteen/signature-enveloping-b64-dsa.xml")
    assert_equal(true, verify(doc, '//ds:Signature', NIL, 1, 0), "Verify")
end

#test_MerlinEnvelopingDsaObject



389
390
391
392
393
# File 'ext/xmlsig/t/tc_tsik.rb', line 389

def test_MerlinEnvelopingDsa
    ### TSIK XmlSigTest.testMerlinEnvelopingDsa
    doc = loadDoc("merlin-xmldsig-fifteen/signature-enveloping-dsa.xml")
    assert_equal(true, verify(doc, '//ds:Signature', NIL, 1, 0), "Verify")
end

#test_MultipleCertObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'ext/xmlsig/t/tc_tsik.rb', line 271

def test_MultipleCert
       ### TSIK XmlSigTest.testMultipleCert
       cert = Xmlsig::X509Certificate.new
       if (cert.loadFromFile(resdir() +  'mycert.x509', 'cert_pem') < 0)
           raise "IOException - Couldn't load 'mycert.x509'"
       end
       verifyingKey = cert.getKey()
       doc = loadDoc('in.xml')
       signer = Xmlsig::Signer.new(doc, privateKey(), verifyingKey)
       signer.attachPublicKey(1)
       signer.addReference(Xmlsig::XPath.new("/books/book[2]"))
       d = signer.sign(Xmlsig::XPath.new("/books/book[1]"))
	assert_equal(false, d.nil?)
       sigLoc = '//ds:Signature'

       assert_equal(true, verify(d, sigLoc, publicKey(), 0, 0), "Verify")
       assert_equal(true, verify(d, sigLoc, NIL, 1, 0), "Verify")
       assert_equal(true, verify(d, sigLoc, NIL, 1, 1), "Verify")
       xpath = Xmlsig::XPath.new(sigLoc)
       xpath.addNamespace("ds", "http://www.w3.org/2000/09/xmldsig#")
       verifier = Xmlsig::Verifier.new(d, xpath)
       l = verifier.getCertificateChain()
       assert_equal(1, l.length, "Chain length")
end

#test_MultipleRefsDetachedObject



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'ext/xmlsig/t/tc_tsik.rb', line 322

def test_MultipleRefsDetached
    ### TSIK XmlSigTest.testMultipleRefsDetached
    signer = sign1('in.xml', privateKey(), publicKey(), NIL)

    for expr in ['/', '/books', '/books/book[2]', '/books/book[1]',
                "/books/book[@name='Professional XML']"]
        xpath = Xmlsig::XPath.new(expr)
        signer.addReference(xpath)
    end

    doc = sign2(signer, '/books/book[1]', NIL)
    assert_equal(false, doc.nil?)
    assert_equal(true, verify(doc, '/books/book[1]/ds:Signature', publicKey(), 0, 0), "Verify")
    assert_equal(true, verify(doc, '/books/book[1]/ds:Signature', NIL, 1, 0), "Verify")
end

#test_MultipleSignaturesObject



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'ext/xmlsig/t/tc_tsik.rb', line 358

def test_MultipleSignatures
    ### TSIK XmlSigTest.testMultipleSignatures
    signer = sign1('in.xml', privateKey(), publicKey(), NIL)
    signer.addReference(Xmlsig::XPath.new('/books/book[2]'))
    doc = signer.sign(Xmlsig::XPath.new('/books/book[1]'), 1)
    assert_equal(false, doc.nil?)
    signer = Xmlsig::Signer.new(doc, privateKey(), publicKey())
    # TSIK uses /books/ds:Signature for this reference, which
    # matches multiple Signature elements. This causes problems
    # because a Signature should not refer to itself, so we've
    # changed it to refer to the first Signature only which was
    # presumably the intent. (modified TSIK output reference xml)
    xpath = Xmlsig::XPath.new('/books/ds:Signature[1]')
    xpath.addNamespace('ds', 'http://www.w3.org/2000/09/xmldsig#')
    signer.addReference(xpath)
    doc = signer.sign(Xmlsig::XPath.new('/books/book[2]'), 1)
    assert_equal(false, doc.nil?)
    sigLoc1 = '/books/ds:Signature[1]'
    sigLoc2 = '/books/ds:Signature[2]'
    assert_equal(true, verify(doc, sigLoc1, publicKey(), 0, 0), "Verify")
    assert_equal(true, verify(doc, sigLoc2, publicKey(), 0, 0), "Verify")
    assert_equal(true, verify(doc, sigLoc1, NIL, 1, 0), "Verify")
    assert_equal(true, verify(doc, sigLoc2, NIL, 1, 0), "Verify")
end

#test_SigningAndVerifyingKeyObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'ext/xmlsig/t/tc_tsik.rb', line 184

def test_SigningAndVerifyingKey
    ### TSIK XmlSigTest.testSigningAndVerifyingKey
    doc = loadDoc('in.xml')
    # Sign document
    signer = Xmlsig::Signer.new(doc, privateKey(), publicKey())
    xpath = Xmlsig::XPath.new('/books')
    signer.attachPublicKey(1)
    d = signer.sign(xpath)
    # Verify document
    xpath = Xmlsig::XPath.new('//ds:Signature')
    xpath.addNamespace('ds', "http://www.w3.org/2000/09/xmldsig#")
    v = Xmlsig::Verifier.new(d, xpath)
    assert_equal(1, v.verify(publicKey()), "Verify")
    assert_equal(1, v.verify(), "Verify")
end

#test_SignInPlaceObject

Port of the TSIK org.apache.tsik.xmlsig.test.XmlSigTest class



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/xmlsig/t/tc_tsik.rb', line 112

def test_SignInPlace
       ### TSIK XmlSigTest.testSignInPlace
       # Load document
       doc = loadDoc("SomeTest.xml")

       # Sign document
       signer = Xmlsig::Signer.new(doc, privateKey(), publicKey())
       xpath = Xmlsig::XPath.new('/')
       assert_equal(0, signer.signInPlace(xpath), "signInPlace")

       # Verify document
       xpath = Xmlsig::XPath.new('//ds:Signature')
       xpath.addNamespace('ds', "http://www.w3.org/2000/09/xmldsig#")
       v = Xmlsig::Verifier.new(doc, xpath)
       assert_equal(1, v.verify(publicKey()), "Verify")
end

#test_SignInPlace2Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/xmlsig/t/tc_tsik.rb', line 129

def test_SignInPlace2
    ### TSIK XmlSigTest.testSignInPlace2
    # Load document
    doc = loadDoc("Test2.xml")

    # Sign document
    signer = Xmlsig::Signer.new(doc, privateKey(), publicKey())
    xpath = Xmlsig::XPath.new('/test/test2')
    assert_equal(0, signer.signInPlace(xpath, 1), "signInPlace")

    # Verify document
    xpath = Xmlsig::XPath.new('//ds:Signature')
    xpath.addNamespace('ds', "http://www.w3.org/2000/09/xmldsig#")
    v = Xmlsig::Verifier.new(doc, xpath)
    assert_equal(1, v.verify(publicKey()), "Verify")
end

#test_SignInPlaceEnvelopingObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/xmlsig/t/tc_tsik.rb', line 146

def test_SignInPlaceEnveloping
    ### TSIK XmlSigTest.testSignInPlaceEnveloping
    # Load document
    doc = loadDoc("Test2.xml")

    # Sign document
    signer = Xmlsig::Signer.new(doc, privateKey(), publicKey())
    assert_equal(0, signer.signInPlace(), "signInPlace")

    # Verify document
    xpath = Xmlsig::XPath.new('//ds:Signature')
    xpath.addNamespace('ds', "http://www.w3.org/2000/09/xmldsig#")
    v = Xmlsig::Verifier.new(doc, xpath)
    assert_equal(1, v.verify(publicKey()), "Verify")
end

#test_SteleObject



241
242
243
244
245
246
247
248
249
250
# File 'ext/xmlsig/t/tc_tsik.rb', line 241

def test_Stele
    ### TSIK XmlSigTest.testStele
    signer = sign1('testStele.xml', privateKey(), publicKey(), NIL)
    xpath = Xmlsig::XPath.new("/SOAP-ENV:Envelope/SOAP-ENV:Body")
    xpath.addNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
    signer.addReference(xpath)
    doc = sign2(signer, '/', NIL)
    assert_equal(false, doc.nil?)
    assert_equal(true, verify(doc, '//ds:Signature', publicKey(), 0, 0), "Verify")
end

#test_VerifyingKeyWithCertsObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'ext/xmlsig/t/tc_tsik.rb', line 162

def test_VerifyingKeyWithCerts
    ### TSIK XmlSigTest.testVerifyingKeyWithCerts
    doc = loadDoc('in.xml')
    cert = Xmlsig::X509Certificate.new
    if (cert.loadFromFile(resdir() +  'mycert.x509', 'cert_pem') < 0)
        raise "IOException - Couldn't load 'mycert.x509'"
    end
    verifyingKey = cert.getKey()
    # Sign document
    signer = Xmlsig::Signer.new(doc, privateKey(), verifyingKey)
    xpath = Xmlsig::XPath.new('/')
    d = signer.sign(xpath)

    # Verify document
    xpath = Xmlsig::XPath.new('//ds:Signature')
    xpath.addNamespace('ds', "http://www.w3.org/2000/09/xmldsig#")
    v = Xmlsig::Verifier.new(d, xpath)
    v.setKeyStore(Xmlsig::KeyStore.new)
    assert_equal(1, v.verify(publicKey()), "Verify")
    assert_equal(1, v.verify(), "Verify")
end

#test_XPathObject

Port of the TSIK org.apache.tsik.xmlsig.test.XmlSigTestDigsig class



421
422
423
424
425
426
# File 'ext/xmlsig/t/tc_tsik.rb', line 421

def test_XPath
    ### TSIK XmlSigTestDigsig.testXPath
    doc = loadDoc("digsig-ratified/xpath_out.xml")
    sigLoc = "//ds:Signature"
    assert_equal(true, verify(doc, sigLoc, NIL, 1, 0), "Verify")
end

#test_XPathEnvelopedObject



438
439
440
441
442
443
444
445
446
# File 'ext/xmlsig/t/tc_tsik.rb', line 438

def test_XPathEnveloped
    ### TSIK XmlSigTestDigsig.testXPathEnveloped
    doc = loadDoc("digsig-ratified/envelopedsignature_out.xml")
    # Id attribute must be set, see
    # http://www.aleksey.com/xmlsec/faq.html#section_3_2
    doc.addIdAttr('Id', 'RegisterResult', 'http://www.xkms.org/schema/xkms-2001-01-20')
    sigLoc = "//ds:Signature"
    assert_equal(true, verify(doc, sigLoc, NIL, 1, 0), "Verify")
end

#test_XPointerObject



428
429
430
431
432
433
434
435
436
# File 'ext/xmlsig/t/tc_tsik.rb', line 428

def test_XPointer
    ### TSIK XmlSigTestDigsig.testXPointer
    doc = loadDoc("digsig-ratified/xpointer_out.xml")
    # Id attribute must be set, see
    # http://www.aleksey.com/xmlsec/faq.html#section_3_2
    doc.addIdAttr('Id', 'elem', '')
    sigLoc = "//ds:Signature"
    assert_equal(true, verify(doc, sigLoc, NIL, 1, 0), "Verify")
end

#verify(inDoc, signatureLocation, publicKey, mustHavePublicKey, mustHaveCert) ⇒ Object



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
# File 'ext/xmlsig/t/tc_tsik.rb', line 81

def verify(inDoc, signatureLocation, publicKey, mustHavePublicKey, mustHaveCert)
    xpath = Xmlsig::XPath.new(signatureLocation)
    xpath.addNamespace("ds", "http://www.w3.org/2000/09/xmldsig#")
    xpath.addNamespace("s2", "http://ns.s2ml.org/s2ml")
    xpath.addNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
    xpath.addNamespace("EMS", "http://ems.verisign.com/2001/05/ems-s2ml#")

    verifier = Xmlsig::Verifier.new(inDoc, xpath)
    pubKey = publicKey
    if (mustHavePublicKey)
        pubKey = verifier.getVerifyingKey()
        if (pubKey == NIL)
            print("Cannot find public key")
            return 0
        end
    end
    if (mustHaveCert)
        cert = verifier.getCertificate()
        if (cert == NIL)
            print("Cannot find certificate")
            return 0
        end
    end
    verified = 0
    verified = verifier.verify(pubKey) == 1
    return verified
end