test_xml.py (4303B)
1 # standard imports 2 import logging 3 import unittest 4 import os 5 from base64 import b64decode 6 from hashlib import sha256 7 8 # external imports 9 from fastecdsa import curve 10 from fastecdsa import ecdsa 11 12 # local imports 13 from funga.xml import SignatureParser 14 from funga.xml import SignatureAccept 15 from funga.xml import SignatureVerify 16 17 logging.basicConfig(level=logging.DEBUG) 18 logg = logging.getLogger() 19 20 test_dir = os.path.dirname(os.path.realpath(__file__)) 21 22 23 def verify_fail(v): 24 return False 25 26 27 def verify_sig_ok(v): 28 return len(v) == 65 29 30 verify_pub_ok = verify_sig_ok 31 32 def verify_digest_ok(v): 33 return len(v) == 32 34 35 36 class TestXmlSig(unittest.TestCase): 37 38 def setUp(self): 39 self.xml_file = os.path.join(test_dir, 'testdata', 'sign.xml') 40 self.parser = SignatureParser() 41 42 43 def test_base(self): 44 with self.assertRaises(AssertionError): 45 self.parser.process_file(self.xml_file) 46 self.parser.set(SignatureAccept.CANONICALIZATION, 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315') 47 48 with self.assertRaises(AssertionError): 49 self.parser.process_file(self.xml_file) 50 self.parser.set(SignatureAccept.SIGNING, 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256') 51 52 with self.assertRaises(AssertionError): 53 self.parser.process_file(self.xml_file) 54 self.parser.set(SignatureAccept.DIGEST, 'https://csrc.nist.gov/glossary/term/sha_256') 55 self.parser.process_file(self.xml_file) 56 57 self.parser.set(SignatureVerify.SIGNATURE, verify_fail) 58 with self.assertRaises(AssertionError): 59 self.parser.process_file(self.xml_file) 60 self.parser.set(SignatureVerify.SIGNATURE, verify_sig_ok) 61 self.parser.process_file(self.xml_file) 62 63 self.parser.set(SignatureVerify.DIGEST, verify_fail) 64 with self.assertRaises(AssertionError): 65 self.parser.process_file(self.xml_file) 66 self.parser.set(SignatureVerify.DIGEST, verify_sig_ok) 67 with self.assertRaises(AssertionError): 68 self.parser.process_file(self.xml_file) 69 self.parser.set(SignatureVerify.DIGEST, verify_digest_ok) 70 self.parser.process_file(self.xml_file) 71 72 self.parser.set(SignatureVerify.PUBLICKEY, verify_fail) 73 with self.assertRaises(AssertionError): 74 self.parser.process_file(self.xml_file) 75 self.parser.set(SignatureVerify.PUBLICKEY, verify_digest_ok) 76 with self.assertRaises(AssertionError): 77 self.parser.process_file(self.xml_file) 78 self.parser.set(SignatureVerify.PUBLICKEY, verify_pub_ok) 79 self.parser.process_file(self.xml_file) 80 81 self.assertEqual(self.parser.sig_r, 88049138661219786673382328446851442949676708152514327418479271802005342599993) 82 self.assertEqual(self.parser.sig_s, 15014121175306622951266769875074899597152611817081922798245423703943058669330) 83 self.assertEqual(self.parser.public_key.hex(), '049f6bb6a7e3f5b7ee71756a891233d1415658f8712bac740282e083dc9240f5368bdb3b256a5bf40a8f7f9753414cb447ee3f796c5f30f7eb40a7f5018fc7f02e') 84 self.assertEqual(self.parser.digest.hex(), 'e08f5c88dd7b076fe3e42f9146980a3c8223324ef7aa3b5b9a6103a6ca657b42') 85 86 h = sha256() 87 h.update(self.parser.sign_material.encode('utf-8')) 88 digest_outer = h.digest() 89 self.assertEqual(digest_outer.hex(), 'c34e546b70afeb6962d6faa59eb7f9316fc254cc96534cd181aff6958004c5ee') 90 91 c = curve.Curve( 92 'eth', 93 self.parser.prime, 94 self.parser.curve_a, 95 self.parser.curve_b, 96 self.parser.order, 97 self.parser.base_x, 98 self.parser.base_y, 99 ) 100 101 class Pubk: 102 def __init__(self, x, y): 103 self.x = x 104 self.y = y 105 106 x = int.from_bytes(self.parser.public_key[1:33], byteorder='big') 107 y = int.from_bytes(self.parser.public_key[33:65], byteorder='big') 108 pubk = Pubk(x, y) 109 110 r = ecdsa.verify( 111 (self.parser.sig_r, self.parser.sig_s), 112 self.parser.sign_material, 113 pubk, 114 curve=c, 115 hashfunc=sha256, 116 ) 117 self.assertTrue(r) 118 119 120 if __name__ == '__main__': 121 unittest.main()