1+ import json
2+ from base64 import b64decode
3+ import base64
14from os import path
25from pathlib import Path
36from io import BytesIO
47from json import load
5- from PIL .Image import open as image_open
8+ from PIL .Image import NONE , open as image_open
69from base45 import b45decode
710from cbor2 import loads , CBORTag
8- from cose .algorithms import Es256 , Ps256
9- from cose .headers import KID
11+ from cose .algorithms import Es256 , Ps256 , Sha256
12+ from cose .headers import Algorithm , KID
1013from cose .keys import CoseKey
1114from cose .keys .curves import P256
1215from cose .keys .keyops import VerifyOp
2427from zlib import decompress
2528from cbor2 import loads , CBORTag
2629from datetime import date , datetime , timezone
30+ import requests
31+ from filecache import HOUR , MINUTE , filecache
32+ from json import load
33+ from cryptography .hazmat .primitives .hashes import SHA256
34+ from cryptography .hazmat .primitives .asymmetric import ec , rsa
35+ from cryptography .utils import int_to_bytes
36+
2737
2838TIMESTAMP_ISO8601_EXTENDED = "%Y-%m-%dT%H:%M:%S.%fZ"
2939CONFIG_ERROR = 'CONFIG_ERROR'
40+ ACC_KID_LIST = 'https://dgca-verifier-service-eu-acc.cfapps.eu10.hana.ondemand.com/signercertificateStatus'
41+ ACC_CERT_LIST = 'https://dgca-verifier-service-eu-acc.cfapps.eu10.hana.ondemand.com/signercertificateUpdate'
3042
3143def pytest_generate_tests (metafunc ):
3244 if "config_env" in metafunc .fixturenames :
3345 country_code = metafunc .config .getoption ("country_code" )
34- # file_name = metafunc.config.getoption("file_name")
35- # print(country_code, file_name)
46+ file_name = metafunc .config .getoption ("file_name" )
47+ print (country_code , file_name )
3648 test_dir = path .dirname (path .dirname (path .abspath (__file__ )))
3749 test_files = glob (str (Path (test_dir , country_code , "*.png" )), recursive = True )
3850 metafunc .parametrize ("config_env" , test_files , indirect = True )
3951
52+ def getKidList ():
53+ response = requests .get (ACC_KID_LIST )
54+ if not response .ok :
55+ fail ("KID List not reachable" )
56+ kidlist = dict ()
57+ for x in json .loads (response .text ):
58+ kidlist [x ]= ''
59+ return kidlist
60+
61+
62+
63+ def getCertificates (kidlist ):
64+ resume_token = 0
65+ abort = False
66+ while not abort :
67+ if resume_token == 0 :
68+ response = requests .get (ACC_CERT_LIST )
69+ else :
70+ headers = {"x-resume-token" :resume_token }
71+ response = requests .get (ACC_CERT_LIST ,headers = headers )
72+
73+ if not response .ok :
74+ fail ("Certificate List not reachable" )
75+ bytes = Sha256 .compute_hash (base64 .b64decode (response .text ))
76+
77+ kid = base64 .b64encode (bytes [0 :8 ]).decode ("ascii" )
78+
79+ if kid in kidlist :
80+ kidlist [kid ] = bytes
81+
82+ if "x-resume-token" in response .headers :
83+ resume_token = response .headers ["x-resume-token" ]
84+ else :
85+ abort = True
86+ return kidlist
87+
88+
89+ @filecache (HOUR )
90+ def downloadCertificates ():
91+ kidlist = getKidList ()
92+ kidlist = getCertificates (kidlist )
93+ return kidlist
94+
95+
4096@fixture
4197def config_env (request ):
4298 # noinspection PyBroadException
@@ -46,6 +102,7 @@ def config_env(request):
46102 except Exception :
47103 return {CONFIG_ERROR : format_exc ()}
48104
105+
49106def _readobject (png ):
50107 file = open (png ,mode = 'rb' )
51108 # read all lines at once
@@ -84,6 +141,9 @@ def _checkTags(cose):
84141 fail (f'QR Code not tagged as Sign1 Message. Tagged with { firstbyte } ({ type } )' )
85142
86143def test_issuer_quality (config_env : Dict ):
144+
145+ kidlist = downloadCertificates ()
146+
87147 _PREFIX = config_env
88148
89149 if (not _checkPrefix (_PREFIX )) :
@@ -99,6 +159,22 @@ def test_issuer_quality(config_env: Dict):
99159
100160 _CBOR = Sign1Message .decode (_COSE )
101161
162+ alg = _CBOR .phdr [Algorithm ]
163+
164+ if not alg in ["Es256" ,"Ps256" ] :
165+ fail ("Wrong Algorithm used" )
166+
167+ alg = _CBOR .uhdr [Algorithm ]
168+
169+ if not alg == NONE :
170+ fail ("Algorithm must be in Protected header" )
171+
172+ print (_CBOR )
102173
174+ kid = base64 .b64encode (_CBOR .phdr [KID ]).decode ("ascii" )
103175
176+ if not kid in kidlist :
177+ fail ("KID exist not on acceptance environment" )
104178
179+
180+
0 commit comments