Skip to content

Commit 4b007be

Browse files
authored
Merge pull request #24 from dizme/main
Support Issuer Signed object with Certificate Chain
2 parents d6c148d + d01aae5 commit 4b007be

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,4 @@ cython_debug/
157157
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160-
#.idea/
160+
.idea/

pymdoccbor/mso/verifier.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pycose.keys import CoseKey, EC2Key
66
from pycose.messages import Sign1Message
77

8-
from typing import Union
8+
from typing import Union, Any
99

1010
from pymdoccbor.exceptions import (
1111
MsoX509ChainNotFound,
@@ -75,15 +75,37 @@ def payload_as_dict(self):
7575
)
7676

7777
@property
78-
def raw_public_keys(self) -> bytes:
78+
def raw_public_keys(self) -> list[Union[bytes, dict]]:
7979
"""
80-
it returns the public key extract from x509 certificates
81-
looking to both phdr and uhdr
80+
Extracts public keys from x509 certificates found in the MSO.
81+
This method searches for x509 certificates in both the protected header (phdr)
82+
and unprotected header (uhdr) of the COSE_Sign1 object. It handles certificate
83+
data in various formats, including:
84+
- `bytes`: Returns a list containing the raw bytes of the certificate.
85+
- `list`: Returns the list of certificates as-is.
86+
- `dict`: Wraps the dictionary in a list and returns it.
87+
If no valid x509 certificates are found, an `MsoX509ChainNotFound` exception
88+
is raised. Unexpected types are logged as warnings.
89+
:return: list[Any]: A list of certificates in their respective formats.
90+
:raises MsoX509ChainNotFound: If no x509 certificates are found.
8291
"""
83-
_mixed_heads = self.object.phdr.items() | self.object.uhdr.items()
92+
merged = self.object.phdr.copy()
93+
merged.update(self.object.uhdr)
94+
_mixed_heads = merged.items()
8495
for h, v in _mixed_heads:
8596
if h.identifier == 33:
86-
return list(self.object.uhdr.values())
97+
if isinstance(v, bytes):
98+
return [v]
99+
elif isinstance(v, list):
100+
return v
101+
elif isinstance(v, dict):
102+
return [v]
103+
else:
104+
logger.warning(
105+
f"Unexpected type for public key: {type(v)}. "
106+
"Expected bytes, list or dict."
107+
)
108+
continue
87109

88110
raise MsoX509ChainNotFound(
89111
"I can't find any valid X509certs, identified by label number 33, "

requirements-dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ isort
66
autoflake
77
bandit
88
autopep8
9-
pycose>=1.0.1
9+
pycose>=1.0.1
10+
cbor2>=5.4.0,<5.5.0

0 commit comments

Comments
 (0)