Skip to content

Commit 63834b1

Browse files
committed
add dcc signature verification check
1 parent 6c341af commit 63834b1

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

tests/test_qrqualitycheck.py

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
from cryptography.hazmat.primitives.hashes import SHA256
3434
from cryptography.hazmat.primitives.asymmetric import ec, rsa
3535
from cryptography.utils import int_to_bytes
36+
from cryptography import x509
37+
from cryptography.x509 import ExtensionNotFound
3638

39+
COSE = 'COSE'
3740
TIMESTAMP_ISO8601_EXTENDED = "%Y-%m-%dT%H:%M:%S.%fZ"
3841
CONFIG_ERROR = 'CONFIG_ERROR'
3942
X_RESUME_TOKEN = 'x-resume-token'
@@ -66,10 +69,10 @@ def getKidList():
6669
def getCertificates(kidlist):
6770
r = requests.get(ACC_CERT_LIST)
6871
while X_RESUME_TOKEN in r.headers and r.status_code == 200:
69-
print(r.headers[X_RESUME_TOKEN])
72+
# print(r.headers[X_RESUME_TOKEN])
7073
bytes = Sha256.compute_hash(base64.b64decode(r.text))
71-
kid = base64.b64encode(bytes[0:8]).decode("ascii")
72-
kidlist[r.headers[X_KID]] = bytes
74+
#kid = base64.b64encode(bytes[0:8]).decode("ascii")
75+
kidlist[r.headers[X_KID]] = r.text # bytes
7376
r = requests.get(ACC_CERT_LIST, headers={
7477
X_RESUME_TOKEN: r.headers[X_RESUME_TOKEN]})
7578
return kidlist
@@ -160,7 +163,63 @@ def test_issuer_quality(config_env: Dict):
160163
if Algorithm in _CBOR.uhdr:
161164
fail("Algorithm must be in Protected header")
162165

163-
kid = base64.b64encode(_CBOR.uhdr[KID]).decode("ascii")
166+
if KID in _CBOR.phdr:
167+
kid = _CBOR.phdr[KID]
168+
else:
169+
kid = _CBOR.uhdr[KID]
170+
171+
kid = base64.b64encode(kid).decode("ascii")
172+
print(kid)
164173

165174
if not kid in kidlist:
166-
fail("KID exist not on acceptance environment")
175+
fail("KID exist not on acceptance environment")
176+
177+
x = y = e = n = None
178+
cert = x509.load_pem_x509_certificate(
179+
f'-----BEGIN CERTIFICATE-----\n{kidlist[kid]}\n-----END CERTIFICATE-----'.encode())
180+
fingerprint = cert.fingerprint(SHA256())
181+
keyid = fingerprint[0:8]
182+
183+
if isinstance(cert.public_key(), rsa.RSAPublicKey):
184+
e = int_to_bytes(cert.public_key().public_numbers().e)
185+
n = int_to_bytes(cert.public_key().public_numbers().n)
186+
elif isinstance(cert.public_key(), ec.EllipticCurvePublicKey):
187+
x = int_to_bytes(cert.public_key().public_numbers().x)
188+
y = int_to_bytes(cert.public_key().public_numbers().y)
189+
else:
190+
raise Exception(
191+
f'Unsupported Certificate Algorithm: {cert.signature_algorithm_oid} for verification.'
192+
)
193+
try:
194+
dsc_supported_operations = {eku.dotted_string for eku in
195+
cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value}
196+
except ExtensionNotFound:
197+
dsc_supported_operations = set()
198+
199+
key = None
200+
if x and y:
201+
key = CoseKey.from_dict(
202+
{
203+
KpKeyOps: [VerifyOp],
204+
KpKty: KtyEC2,
205+
EC2KpCurve: P256, # Ought o be pk.curve - but the two libs clash
206+
KpAlg: Es256, # ECDSA using P-256 and SHA-256
207+
EC2KpX: x,
208+
EC2KpY: y,
209+
}
210+
)
211+
elif e and n:
212+
key = CoseKey.from_dict(
213+
{
214+
KpKeyOps: [VerifyOp],
215+
KpKty: KtyRSA,
216+
KpAlg: Ps256, # RSASSA-PSS using SHA-256 and MGF1 with SHA-256
217+
RSAKpE: e,
218+
RSAKpN: n,
219+
}
220+
)
221+
_CBOR.key = key
222+
223+
if not _CBOR.verify_signature():
224+
fail("Signature could not be verified with signing certificate {}".format(
225+
kidlist[kid]))

0 commit comments

Comments
 (0)