nspkg / engine / signature_validation.v
110 lines · 104 sloc · 4.08 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// signature levels and checksum precedence follow be_package.c and be_sync.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5import os
6
7fn signature_policy(level u32, database bool) SignaturePolicy {
8 bits := if database { level >> 10 } else { level }
9 return SignaturePolicy{ optional: bits & 2 != 0, marginal: bits & 4 != 0, unknown: bits & 8 != 0 }
10}
11
12fn verify_database_signature(mut handle Handle, path string, identifier string, level u32) ! {
13 policy := signature_policy(level, true)
14 mut attempted := map[string]bool{}
15 for {
16 report := handle.check_signature_policy(path, none, policy) or {
17 handle.retry_signatures(identifier, [], policy, mut attempted)
18 return err
19 }
20 if report.valid { return }
21 if !handle.retry_signatures(identifier, report.results, policy, mut attempted) {
22 return handle.fail(.sig_invalid, path)
23 }
24 }
25}
26
27pub fn (mut handle Handle) validate_package(filename string, sync_pkg &Package, level u32) !u32 {
28 handle.check()!
29 path := c_string_prefix(filename)
30 if path == '' { return handle.fail(.wrong_args, 'empty package path') }
31 if !handle.package_readable(path) {
32 problem := os.last_error()
33 kind := match problem.code() {
34 2 { Errno.pkg_not_found }
35 13 { Errno.badperms }
36 else { Errno.pkg_open }
37 }
38 return handle.fail(kind, problem.msg())
39 }
40 mut embedded := ?string(none)
41 if sync_pkg != unsafe { nil } && sync_pkg.has_base64_sig { embedded = sync_pkg.base64_sig }
42 sigpath := path + '.sig'
43 has_sig := level & 1 != 0 && (embedded != none || handle.package_readable(sigpath))
44 mut validation := u32(0)
45 if sync_pkg != unsafe { nil } && (!has_sig || embedded == none) && sync_pkg.has_sha256sum {
46 handle.log(.debug, 'sha256sum: ${sync_pkg.sha256sum}\n')
47 handle.log(.debug, 'checking sha256sum for ${path}\n')
48 checksum := compute_sha256sum(path) or { return handle.fail(.pkg_invalid_checksum, path) }
49 if checksum != sync_pkg.sha256sum { return handle.fail(.pkg_invalid_checksum, path) }
50 validation |= 4
51 }
52 if level & 1 != 0 {
53 handle.log(.debug, 'sig data: ${embedded or { '<from .sig>' }}\n')
54 policy := signature_policy(level, false)
55 if !has_sig && !policy.optional { return handle.fail(.pkg_missing_sig, path) }
56 report := handle.check_signature_policy(path, embedded, policy) or {
57 return handle.fail(.pkg_invalid_sig, err.msg())
58 }
59 if !report.valid { return handle.fail(.pkg_invalid_sig, path) }
60 if has_sig { validation |= 8 }
61 }
62 return if validation == 0 { u32(1) } else { validation }
63}
64
65fn read_signature_file(path string) ![]u8 {
66 mut file := os.open(path)!
67 defer { file.close() }
68 // the source caps detached signatures at 16 KiB before issuer extraction.
69 mut data := []u8{len: 16385}
70 mut length := 0
71 for length < data.len {
72 n := file.read(mut data[length..]) or {
73 if err is os.Eof { break }
74 return err
75 }
76 if n == 0 { break }
77 length += n
78 }
79 if length > 16384 { return error('signature file exceeds 16 KiB') }
80 if length == 0 { return error('signature file is empty') }
81 return data[..length]
82}
83
84fn (mut handle Handle) import_package_keys(path string, full bool) ! {
85 sigpath := path + '.sig'
86 data := read_signature_file(sigpath) or {
87 handle.log(.error, localized_message('failed to read signature file: %s\n', sigpath))
88 return handle.fail(.pkg_invalid_sig, err.msg())
89 }
90 // armored and newer signatures may lack the v4 issuer packets this prepass reads.
91 keys := extract_key_ids(data) or {
92 handle.log_signature_format_error(path, err)
93 return
94 }
95 mut missing := false
96 for fingerprint in keys {
97 // this prepass is advisory; final signature verification still decides validity.
98 known := handle.key_in_keychain(fingerprint) or { continue }
99 if !known {
100 // be_package.c loads metadata before asking about an unknown or expired key.
101 handle.read_package_for_handle(path, full) or { handle.diagnostics << err.msg() }
102 imported := handle.import_key('', fingerprint) or { return handle.fail(.pkg_invalid_sig, err.msg()) }
103 if !imported { missing = true }
104 }
105 }
106 if missing {
107 handle.log(.error, localized_message('required key missing from keyring\n'))
108 return handle.fail(.pkg_invalid_sig, 'required key missing from keyring')
109 }
110}
111