(
&self,
args: LoadVerifyLocationsArgs,
vm: &VirtualMachine,
)
| 1468 | |
| 1469 | #[pymethod] |
| 1470 | fn load_verify_locations( |
| 1471 | &self, |
| 1472 | args: LoadVerifyLocationsArgs, |
| 1473 | vm: &VirtualMachine, |
| 1474 | ) -> PyResult<()> { |
| 1475 | if let (None, None, None) = (&args.cafile, &args.capath, &args.cadata) { |
| 1476 | return Err(vm.new_type_error("cafile, capath and cadata cannot be all omitted")); |
| 1477 | } |
| 1478 | |
| 1479 | #[cold] |
| 1480 | fn invalid_cadata(vm: &VirtualMachine) -> PyBaseExceptionRef { |
| 1481 | vm.new_type_error("cadata should be an ASCII string or a bytes-like object") |
| 1482 | } |
| 1483 | |
| 1484 | let mut ctx = self.builder(); |
| 1485 | |
| 1486 | // validate cadata type and load cadata |
| 1487 | if let Some(cadata) = args.cadata { |
| 1488 | let (certs, is_pem) = match cadata { |
| 1489 | Either::A(s) => { |
| 1490 | if !s.as_str().is_ascii() { |
| 1491 | return Err(invalid_cadata(vm)); |
| 1492 | } |
| 1493 | (X509::stack_from_pem(s.as_bytes()), true) |
| 1494 | } |
| 1495 | Either::B(b) => (b.with_ref(x509_stack_from_der), false), |
| 1496 | }; |
| 1497 | let certs = certs.map_err(|e| convert_openssl_error(vm, e))?; |
| 1498 | |
| 1499 | // If no certificates were loaded, raise an error |
| 1500 | if certs.is_empty() { |
| 1501 | let msg = if is_pem { |
| 1502 | "no start line: cadata does not contain a certificate" |
| 1503 | } else { |
| 1504 | "not enough data: cadata does not contain a certificate" |
| 1505 | }; |
| 1506 | return Err(vm |
| 1507 | .new_os_subtype_error( |
| 1508 | PySSLError::class(&vm.ctx).to_owned(), |
| 1509 | None, |
| 1510 | msg.to_owned(), |
| 1511 | ) |
| 1512 | .upcast()); |
| 1513 | } |
| 1514 | |
| 1515 | let store = ctx.cert_store_mut(); |
| 1516 | for cert in certs { |
| 1517 | store |
| 1518 | .add_cert(cert) |
| 1519 | .map_err(|e| convert_openssl_error(vm, e))?; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | if args.cafile.is_some() || args.capath.is_some() { |
| 1524 | let cafile_path = args.cafile.map(|p| p.to_path_buf(vm)).transpose()?; |
| 1525 | let capath_path = args.capath.map(|p| p.to_path_buf(vm)).transpose()?; |
| 1526 | // Check file/directory existence before calling OpenSSL to get proper errno |
| 1527 | if let Some(ref path) = cafile_path |
nothing calls this directly
no test coverage detected