// signature levels and checksum precedence follow be_package.c and be_sync.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os fn signature_policy(level u32, database bool) SignaturePolicy { bits := if database { level >> 10 } else { level } return SignaturePolicy{ optional: bits & 2 != 0, marginal: bits & 4 != 0, unknown: bits & 8 != 0 } } fn verify_database_signature(mut handle Handle, path string, identifier string, level u32) ! { policy := signature_policy(level, true) mut attempted := map[string]bool{} for { report := handle.check_signature_policy(path, none, policy) or { handle.retry_signatures(identifier, [], policy, mut attempted) return err } if report.valid { return } if !handle.retry_signatures(identifier, report.results, policy, mut attempted) { return handle.fail(.sig_invalid, path) } } } pub fn (mut handle Handle) validate_package(filename string, sync_pkg &Package, level u32) !u32 { handle.check()! path := c_string_prefix(filename) if path == '' { return handle.fail(.wrong_args, 'empty package path') } if !handle.package_readable(path) { problem := os.last_error() kind := match problem.code() { 2 { Errno.pkg_not_found } 13 { Errno.badperms } else { Errno.pkg_open } } return handle.fail(kind, problem.msg()) } mut embedded := ?string(none) if sync_pkg != unsafe { nil } && sync_pkg.has_base64_sig { embedded = sync_pkg.base64_sig } sigpath := path + '.sig' has_sig := level & 1 != 0 && (embedded != none || handle.package_readable(sigpath)) mut validation := u32(0) if sync_pkg != unsafe { nil } && (!has_sig || embedded == none) && sync_pkg.has_sha256sum { handle.log(.debug, 'sha256sum: ${sync_pkg.sha256sum}\n') handle.log(.debug, 'checking sha256sum for ${path}\n') checksum := compute_sha256sum(path) or { return handle.fail(.pkg_invalid_checksum, path) } if checksum != sync_pkg.sha256sum { return handle.fail(.pkg_invalid_checksum, path) } validation |= 4 } if level & 1 != 0 { handle.log(.debug, 'sig data: ${embedded or { '' }}\n') policy := signature_policy(level, false) if !has_sig && !policy.optional { return handle.fail(.pkg_missing_sig, path) } report := handle.check_signature_policy(path, embedded, policy) or { return handle.fail(.pkg_invalid_sig, err.msg()) } if !report.valid { return handle.fail(.pkg_invalid_sig, path) } if has_sig { validation |= 8 } } return if validation == 0 { u32(1) } else { validation } } fn read_signature_file(path string) ![]u8 { mut file := os.open(path)! defer { file.close() } // the source caps detached signatures at 16 KiB before issuer extraction. mut data := []u8{len: 16385} mut length := 0 for length < data.len { n := file.read(mut data[length..]) or { if err is os.Eof { break } return err } if n == 0 { break } length += n } if length > 16384 { return error('signature file exceeds 16 KiB') } if length == 0 { return error('signature file is empty') } return data[..length] } fn (mut handle Handle) import_package_keys(path string, full bool) ! { sigpath := path + '.sig' data := read_signature_file(sigpath) or { handle.log(.error, localized_message('failed to read signature file: %s\n', sigpath)) return handle.fail(.pkg_invalid_sig, err.msg()) } // armored and newer signatures may lack the v4 issuer packets this prepass reads. keys := extract_key_ids(data) or { handle.log_signature_format_error(path, err) return } mut missing := false for fingerprint in keys { // this prepass is advisory; final signature verification still decides validity. known := handle.key_in_keychain(fingerprint) or { continue } if !known { // be_package.c loads metadata before asking about an unknown or expired key. handle.read_package_for_handle(path, full) or { handle.diagnostics << err.msg() } imported := handle.import_key('', fingerprint) or { return handle.fail(.pkg_invalid_sig, err.msg()) } if !imported { missing = true } } } if missing { handle.log(.error, localized_message('required key missing from keyring\n')) return handle.fail(.pkg_invalid_sig, 'required key missing from keyring') } }