|
1 | 1 | from base64 import b64decode |
2 | 2 | from dataclasses import dataclass |
3 | | -from typing import List, Optional |
| 3 | +from typing import Callable, List, Optional, Union |
4 | 4 |
|
5 | 5 | from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa, utils |
6 | 6 | from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 |
@@ -161,19 +161,19 @@ def _apply_transforms(self, payload, transforms_node, signature, c14n_algorithm) |
161 | 161 | def verify( |
162 | 162 | self, |
163 | 163 | data, |
164 | | - require_x509=True, |
165 | | - x509_cert=None, |
166 | | - cert_subject_name=None, |
167 | | - cert_resolver=None, |
168 | | - ca_pem_file=None, |
169 | | - ca_path=None, |
170 | | - hmac_key=None, |
171 | | - validate_schema=True, |
| 164 | + require_x509: bool = True, |
| 165 | + x509_cert: Optional[Union[str, X509]] = None, |
| 166 | + cert_subject_name: Optional[str] = None, |
| 167 | + cert_resolver: Optional[Callable] = None, |
| 168 | + ca_pem_file: Optional[Union[str, bytes]] = None, |
| 169 | + ca_path: Optional[str] = None, |
| 170 | + hmac_key: Optional[str] = None, |
| 171 | + validate_schema: bool = True, |
172 | 172 | parser=None, |
173 | | - uri_resolver=None, |
174 | | - id_attribute=None, |
175 | | - expect_references=1, |
176 | | - ignore_ambiguous_key_info=False, |
| 173 | + uri_resolver: Optional[Callable] = None, |
| 174 | + id_attribute: Optional[str] = None, |
| 175 | + expect_references: Union[int, bool] = 1, |
| 176 | + ignore_ambiguous_key_info: bool = False, |
177 | 177 | ) -> List[VerifyResult]: |
178 | 178 | """ |
179 | 179 | Verify the XML signature supplied in the data and return a list of **VerifyResult** data structures |
@@ -210,59 +210,47 @@ def verify( |
210 | 210 | :param require_x509: |
211 | 211 | If ``True``, a valid X.509 certificate-based signature with an established chain of trust is required to |
212 | 212 | pass validation. If ``False``, other types of valid signatures (e.g. HMAC or RSA public key) are accepted. |
213 | | - :type require_x509: boolean |
214 | 213 | :param x509_cert: |
215 | 214 | A trusted external X.509 certificate, given as a PEM-formatted string or OpenSSL.crypto.X509 object, to use |
216 | 215 | for verification. Overrides any X.509 certificate information supplied by the signature. If left set to |
217 | 216 | ``None``, requires that the signature supply a valid X.509 certificate chain that validates against the |
218 | 217 | known certificate authorities. Implies **require_x509=True**. |
219 | | - :type x509_cert: string or OpenSSL.crypto.X509 |
220 | 218 | :param cert_subject_name: |
221 | 219 | Subject Common Name to check the signing X.509 certificate against. Implies **require_x509=True**. |
222 | | - :type cert_subject_name: string |
223 | 220 | :param cert_resolver: |
224 | 221 | Function to use to resolve trusted X.509 certificates when X509IssuerSerial and X509Digest references are |
225 | 222 | found in the signature. The function is called with the keyword arguments ``x509_issuer_name``, |
226 | 223 | ``x509_serial_number`` and ``x509_digest``, and is expected to return an iterable of one or more |
227 | 224 | strings containing a PEM-formatted certificate and a chain of intermediate certificates, if needed. |
228 | 225 | Implies **require_x509=True**. |
229 | | - :type cert_resolver: callable |
230 | 226 | :param ca_pem_file: |
231 | 227 | Filename of a PEM file containing certificate authority information to use when verifying certificate-based |
232 | 228 | signatures. |
233 | | - :type ca_pem_file: string or bytes |
234 | 229 | :param ca_path: |
235 | 230 | Path to a directory containing PEM-formatted certificate authority files to use when verifying |
236 | 231 | certificate-based signatures. If neither **ca_pem_file** nor **ca_path** is given, the Mozilla CA bundle |
237 | 232 | provided by :py:mod:`certifi` will be loaded. |
238 | | - :type ca_path: string |
239 | 233 | :param hmac_key: If using HMAC, a string containing the shared secret. |
240 | | - :type hmac_key: string |
241 | 234 | :param validate_schema: Whether to validate **data** against the XML Signature schema. |
242 | | - :type validate_schema: boolean |
243 | 235 | :param parser: |
244 | 236 | Custom XML parser instance to use when parsing **data**. The default parser arguments used by SignXML are: |
245 | 237 | ``resolve_entities=False``. See https://lxml.de/FAQ.html#how-do-i-use-lxml-safely-as-a-web-service-endpoint. |
246 | 238 | :type parser: :py:class:`lxml.etree.XMLParser` compatible parser |
247 | 239 | :param uri_resolver: |
248 | 240 | Function to use to resolve reference URIs that don't start with "#". The function is called with a single |
249 | 241 | string argument containing the URI to be resolved, and is expected to return a lxml.etree node or string. |
250 | | - :type uri_resolver: callable |
251 | 242 | :param id_attribute: |
252 | 243 | Name of the attribute whose value ``URI`` refers to. By default, SignXML will search for "Id", then "ID". |
253 | | - :type id_attribute: string |
254 | 244 | :param expect_references: |
255 | 245 | Number of references to expect in the signature. If this is not 1, an array of VerifyResults is returned. |
256 | 246 | If set to a non-integer, any number of references is accepted (otherwise a mismatch raises an error). |
257 | | - :type expect_references: int or boolean |
258 | 247 | :param ignore_ambiguous_key_info: |
259 | 248 | Ignore the presence of a KeyValue element when X509Data is present in the signature and used for verifying. |
260 | 249 | The presence of both elements is an ambiguity and a security hazard. The public key used to sign the |
261 | 250 | document is already encoded in the certificate (which is in X509Data), so the verifier must either ignore |
262 | 251 | KeyValue or make sure it matches what's in the certificate. SignXML does not implement the functionality |
263 | 252 | necessary to match the keys, and throws an InvalidInput error instead. Set this to True to bypass the error |
264 | 253 | and validate the signature using X509Data only. |
265 | | - :type ignore_ambiguous_key_info: boolean |
266 | 254 |
|
267 | 255 | :raises: :py:class:`cryptography.exceptions.InvalidSignature` |
268 | 256 | """ |
|
0 commit comments