|
5 | 5 | from pycose.keys import CoseKey, EC2Key
|
6 | 6 | from pycose.messages import Sign1Message
|
7 | 7 |
|
8 |
| -from typing import Union |
| 8 | +from typing import Union, Any |
9 | 9 |
|
10 | 10 | from pymdoccbor.exceptions import (
|
11 | 11 | MsoX509ChainNotFound,
|
@@ -75,15 +75,37 @@ def payload_as_dict(self):
|
75 | 75 | )
|
76 | 76 |
|
77 | 77 | @property
|
78 |
| - def raw_public_keys(self) -> bytes: |
| 78 | + def raw_public_keys(self) -> list[Union[bytes, dict]]: |
79 | 79 | """
|
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. |
82 | 91 | """
|
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() |
84 | 95 | for h, v in _mixed_heads:
|
85 | 96 | 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 |
87 | 109 |
|
88 | 110 | raise MsoX509ChainNotFound(
|
89 | 111 | "I can't find any valid X509certs, identified by label number 33, "
|
|
0 commit comments