Convert DER-encoded certificate to Python dict (for getpeercert with binary_form=False) Returns a dict with fields: subject, issuer, version, serialNumber, notBefore, notAfter, subjectAltName (if present)
(
vm: &VirtualMachine,
cert: &x509_parser::certificate::X509Certificate<'_>,
)
| 315 | /// Returns a dict with fields: subject, issuer, version, serialNumber, |
| 316 | /// notBefore, notAfter, subjectAltName (if present) |
| 317 | pub fn cert_to_dict( |
| 318 | vm: &VirtualMachine, |
| 319 | cert: &x509_parser::certificate::X509Certificate<'_>, |
| 320 | ) -> PyResult { |
| 321 | let dict = vm.ctx.new_dict(); |
| 322 | |
| 323 | // Subject and Issuer |
| 324 | dict.set_item("subject", name_to_py(vm, cert.subject())?, vm)?; |
| 325 | dict.set_item("issuer", name_to_py(vm, cert.issuer())?, vm)?; |
| 326 | |
| 327 | // Version (X.509 v3 = version 2 in the cert, but Python uses 3) |
| 328 | dict.set_item( |
| 329 | "version", |
| 330 | vm.ctx.new_int(cert.version().0 as i32 + 1).into(), |
| 331 | vm, |
| 332 | )?; |
| 333 | |
| 334 | // Serial number - hex format with even length |
| 335 | let serial = format_serial_number(&cert.serial); |
| 336 | dict.set_item("serialNumber", vm.ctx.new_str(serial).into(), vm)?; |
| 337 | |
| 338 | // Validity dates - format with GMT using chrono |
| 339 | dict.set_item( |
| 340 | "notBefore", |
| 341 | vm.ctx |
| 342 | .new_str(format_asn1_time(&cert.validity().not_before)) |
| 343 | .into(), |
| 344 | vm, |
| 345 | )?; |
| 346 | dict.set_item( |
| 347 | "notAfter", |
| 348 | vm.ctx |
| 349 | .new_str(format_asn1_time(&cert.validity().not_after)) |
| 350 | .into(), |
| 351 | vm, |
| 352 | )?; |
| 353 | |
| 354 | // Subject Alternative Names (if present) |
| 355 | if let Ok(Some(san_ext)) = cert.subject_alternative_name() { |
| 356 | let san_list = process_san_general_names(vm, &san_ext.value.general_names); |
| 357 | |
| 358 | if !san_list.is_empty() { |
| 359 | dict.set_item("subjectAltName", vm.ctx.new_tuple(san_list).into(), vm)?; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | Ok(dict.into()) |
| 364 | } |
| 365 | |
| 366 | /// Convert DER-encoded certificate to Python dict (for get_ca_certs) |
| 367 | /// |
no test coverage detected