selectForPdf method

Future<PdfTrustedRootsSelection> selectForPdf(
  1. Uint8List pdfBytes
)

Implementation

Future<PdfTrustedRootsSelection> selectForPdf(Uint8List pdfBytes) async {
  final cacheKey = validationSha256Hex(pdfBytes);
  final cached = _selectionCache[cacheKey];
  if (cached != null) return cached;

  final issuerNames = await _extractSignerIssuers(pdfBytes);
  if (issuerNames.isEmpty) {
    final result = _selectAll('No signer issuer found in CMS.');
    _selectionCache[cacheKey] = result;
    return result;
  }

  final unresolved = issuerNames.toSet();
  final selectedIds = <String>[];
  for (final sourceId in index.sourceIds) {
    final subjects = index.subjectsForSource(sourceId);
    if (_matchesAnyIssuer(unresolved, subjects)) {
      selectedIds.add(sourceId);
      unresolved.removeWhere(
        (issuer) => subjectMatchesValidationIssuer(issuer, subjects),
      );
    }
    if (unresolved.isEmpty) break;
  }

  if (selectedIds.isEmpty) {
    final result =
        _selectAll('No trusted-root source matched signer issuer.');
    _selectionCache[cacheKey] = result;
    return result;
  }

  if (requireFullIssuerCoverage && unresolved.isNotEmpty) {
    final result = fallbackToAllSources
        ? _selectAll('Partial issuer coverage; using all sources for safety.')
        : _fromSelection(
            selectedIds: selectedIds,
            fallback: false,
            reason: 'Partial issuer coverage without fallback.',
          );
    _selectionCache[cacheKey] = result;
    return result;
  }

  final result = _fromSelection(
    selectedIds: selectedIds,
    fallback: false,
    reason: 'Matched by signer issuer.',
  );
  _selectionCache[cacheKey] = result;
  return result;
}