| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.engine |
| 5 | import nspkg.tests.archivefixtures |
| 6 | import nspkg.tests.signaturefixtures |
| 7 | import encoding.base64 |
| 8 | import os |
| 9 | import time |
| 10 | |
| 11 | fn c_signature_results(list &SigList) []string { |
| 12 | mut output := []string{} |
| 13 | for index in 0 .. int(list.count) { |
| 14 | result := unsafe { list.results[index] } |
| 15 | key := result.key |
| 16 | output << '${int(result.status)}|${int(result.validity)}|${key.data != unsafe { nil }}|${cstring_to_vstring_or_none(key.fingerprint)}|${cstring_to_vstring_or_none(key.uid)}|${cstring_to_vstring_or_none(key.name)}|${cstring_to_vstring_or_none(key.email)}|${key.created}|${key.expires}|${key.length}|${key.revoked}' |
| 17 | } |
| 18 | return output |
| 19 | } |
| 20 | |
| 21 | fn native_signature_results(results []engine.SigResult) []string { |
| 22 | return results.map('${int(it.status)}|${int(it.validity)}|${it.key.known}|${it.key.fingerprint}|${it.key.uid}|${it.key.name}|${it.key.email}|${it.key.created}|${it.key.expires}|${it.key.length}|${int(it.key.revoked)}') |
| 23 | } |
| 24 | |
| 25 | fn compare_signature_check(mut native engine.Handle, reference &Handle, pkg &Package, path string, embedded ?string) ![]engine.SigResult { |
| 26 | if expected := pkg.check_pgp_signature() { |
| 27 | defer { expected.cleanup() or { eprintln(err) } } |
| 28 | actual := native.check_signature(path, embedded)! |
| 29 | assert native_signature_results(actual) == c_signature_results(expected) |
| 30 | return actual |
| 31 | } |
| 32 | want := reference.errno() |
| 33 | native.check_signature(path, embedded) or { |
| 34 | assert int(native.errno()) == int(want), '${want}: ${err.msg()}' |
| 35 | return []engine.SigResult{} |
| 36 | } |
| 37 | assert false, 'native check unexpectedly succeeded' |
| 38 | return []engine.SigResult{} |
| 39 | } |
| 40 | |
| 41 | fn compare_signature_load(mut native engine.Handle, reference &Handle, path string, level int) ! { |
| 42 | if expected := reference.pkg_load(path, 1, level) { |
| 43 | defer { expected.free_pkg() or { eprintln(err) } } |
| 44 | actual := native.load_package(path, true, u32(level))! |
| 45 | assert actual.validation == u32(expected.get_validation()) |
| 46 | assert actual.name == expected.get_name() |
| 47 | return |
| 48 | } |
| 49 | want := reference.errno() |
| 50 | native.load_package(path, true, u32(level)) or { |
| 51 | assert int(native.errno()) == int(want), 'level ${level}: ${want}: ${err.msg()}' |
| 52 | return |
| 53 | } |
| 54 | assert false, 'native package load unexpectedly succeeded at level ${level}' |
| 55 | } |
| 56 | |
| 57 | fn compare_database_signature_policy(mut native engine.Handle, reference &Handle, level int) ! { |
| 58 | expected := reference.register_syncdb('policy', level)! |
| 59 | defer { expected.unregister() or { eprintln(err) } } |
| 60 | want := reference.errno() |
| 61 | mut actual := native.register_syncdb('policy', u32(level))! |
| 62 | defer { actual.unregister() or { eprintln(err) } } |
| 63 | assert int(native.errno()) == int(want) |
| 64 | if valid := expected.get_valid() { |
| 65 | assert actual.get_valid()! == valid |
| 66 | return |
| 67 | } |
| 68 | want_cached := reference.errno() |
| 69 | actual.get_valid() or { |
| 70 | assert int(native.errno()) == int(want_cached) |
| 71 | return |
| 72 | } |
| 73 | assert false, 'native database validation unexpectedly succeeded at level ${level}' |
| 74 | } |
| 75 | |
| 76 | fn test_signature_decoding_and_valid_issuer_extraction_match_libalpm() ! { |
| 77 | for sample in ['', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9v\r\n', 'Zg\n\n', 'Zm8\n', 'AA==', 'AAA=', 'AAAA', |
| 78 | 'Zg', 'Zg=', 'Zg= ', 'Zg==\r', '=AAA', 'AAAA====', 'Zg==A', 'é', 'AAAA\x00ignored'] { |
| 79 | mut want_data := &u8(unsafe { nil }) |
| 80 | mut want_length := uint(0) |
| 81 | status := C.oracle_alpm_decode_signature(sample.str, &want_data, &want_length) |
| 82 | defer { C.free(want_data) } |
| 83 | mut actual_length := uint(999) |
| 84 | if actual := facade.decode_signature(sample, &actual_length) { |
| 85 | defer { C.free(actual) } |
| 86 | assert status == 0, sample |
| 87 | assert actual_length == want_length, sample |
| 88 | assert unsafe { actual.vbytes(int(actual_length)) } == unsafe { want_data.vbytes(int(want_length)) }, sample |
| 89 | } else { |
| 90 | assert status != 0, sample |
| 91 | assert actual_length == 0 |
| 92 | } |
| 93 | } |
| 94 | mut length := uint(999) |
| 95 | empty := facade.decode_signature('\n\n\n\n', &length)! |
| 96 | assert length == 0 |
| 97 | C.free(empty) |
| 98 | dir := os.join_path(os.vtmp_dir(), 'alpm-sig-packets-${os.getpid()}') |
| 99 | os.mkdir_all(dir)! |
| 100 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 101 | handle := initialize('/', dir)! |
| 102 | defer { handle.release() or { eprintln(err) } } |
| 103 | public := facade.initialize('/', dir)! |
| 104 | defer { public.release() or { eprintln(err) } } |
| 105 | body := [u8(4), 0, 1, 8, 0, 0, 0, 10, 9, 16, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0] |
| 106 | for header in [[u8(0xc2), 20], [u8(0xc2), 255, 0, 0, 0, 20], [u8(0x88), 20], [u8(0x89), 0, |
| 107 | 20], [u8(0x8a), 0, 0, 0, 20]] { |
| 108 | mut packet := header.clone() |
| 109 | packet << body |
| 110 | for data in [packet, packet.repeat(2)] { |
| 111 | mut expected := &List(unsafe { nil }) |
| 112 | assert C.oracle_alpm_extract_keyid(handle, c'fixture', data.data, uint(data.len), &expected) == 0 |
| 113 | mut actual := public.extract_keyid('fixture', data.data, uint(data.len))! |
| 114 | assert actual.strs() == expected.strs() |
| 115 | assert actual.strs() == engine.extract_key_ids(data)! |
| 116 | expected.free_inner(C.free) |
| 117 | expected.free() |
| 118 | actual.free_inner(C.free) |
| 119 | actual.free() |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn test_native_gpgme_verification_matches_libalpm() ! { |
| 125 | dir := os.join_path(os.vtmp_dir(), 'alpm-gpg-parity-${os.getpid()}') |
| 126 | home := os.join_path(dir, 'keys') |
| 127 | os.mkdir_all(os.join_path(dir, 'sync'))! |
| 128 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 129 | defer { signaturefixtures.cleanup(home) or { eprintln(err) } } |
| 130 | ring := signaturefixtures.create(home)! |
| 131 | path := os.join_path(dir, 'package.pkg.tar') |
| 132 | archivefixtures.write(path, [archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = signature-fixture\npkgver = 1-1\n' }], false)! |
| 133 | ring.sign(path, [ring.fingerprint], false)! |
| 134 | old := initialize('/', dir)! |
| 135 | defer { old.release() or { eprintln(err) } } |
| 136 | old.option_set_gpgdir(home)! |
| 137 | pkg := old.pkg_load(path, 0, 0)! |
| 138 | defer { pkg.free_pkg() or { eprintln(err) } } |
| 139 | mut native := engine.initialize('/', dir)! |
| 140 | defer { native.release() or { eprintln(err) } } |
| 141 | native.set_string_option(.gpgdir, home)! |
| 142 | policy_path := os.join_path(dir, 'sync', 'policy.db') |
| 143 | archivefixtures.write(policy_path, [archivefixtures.Entry{ name: 'app-1-1/desc', text: '%DESC%\nApp\n\n' }], false)! |
| 144 | ring.sign(policy_path, [ring.fingerprint], false)! |
| 145 | for trust in [2, 3, 4, 5, 6] { |
| 146 | ring.trust(ring.fingerprint, trust)! |
| 147 | results := compare_signature_check(mut native, old, pkg, path, none)! |
| 148 | assert results.len == 1 |
| 149 | for level in [0, 1, 3, 5, 7, 9, 11, 13, 15] { |
| 150 | compare_signature_load(mut native, old, path, level)! |
| 151 | compare_database_signature_policy(mut native, old, level << 10)! |
| 152 | } |
| 153 | } |
| 154 | data := os.read_bytes(path + '.sig')! |
| 155 | encoded := base64.encode(data) |
| 156 | archivefixtures.write(os.join_path(dir, 'sync', 'core.db'), [archivefixtures.Entry{ name: 'signature-fixture-1-1/desc', text: '%FILENAME%\npackage.pkg.tar\n\n%PGPSIG%\n${encoded}\n\n' }], false)! |
| 157 | old_dir := os.getwd() |
| 158 | os.chdir(dir)! |
| 159 | defer { os.chdir(old_dir) or { eprintln(err) } } |
| 160 | db := old.register_syncdb('core', 0)! |
| 161 | embedded_pkg := db.get_pkg('signature-fixture') or { return error('embedded signature package missing') } |
| 162 | compare_signature_check(mut native, old, embedded_pkg, path, encoded)! |
| 163 | second := ring.generate('Second ALPM Fixture <second-alpm@example.invalid>')! |
| 164 | ring.sign(path, [ring.fingerprint, second], false)! |
| 165 | assert compare_signature_check(mut native, old, pkg, path, none)!.len == 2 |
| 166 | compare_signature_load(mut native, old, path, 1)! |
| 167 | ring.sign(path, [ring.fingerprint], true)! |
| 168 | assert compare_signature_check(mut native, old, pkg, path, none)![0].status == .valid |
| 169 | compare_signature_load(mut native, old, path, 1)! |
| 170 | ring.gpg(['--yes', '--edit-key', ring.fingerprint, 'disable', 'save'])! |
| 171 | assert compare_signature_check(mut native, old, pkg, path, none)![0].status == .key_disabled |
| 172 | compare_signature_load(mut native, old, path, 15)! |
| 173 | ring.gpg(['--yes', '--edit-key', ring.fingerprint, 'enable', 'save'])! |
| 174 | past := time.now().unix() - 172800 |
| 175 | expired := ring.generate_at('Expired Fixture <expired@example.invalid>', past, '1d')! |
| 176 | ring.trust(expired, 6)! |
| 177 | ring.sign_at(path, expired, past + 3600, '0')! |
| 178 | expired_result := compare_signature_check(mut native, old, pkg, path, none)![0] |
| 179 | assert expired_result.status == .key_expired |
| 180 | assert expired_result.validity == .unknown, expired_result.str() |
| 181 | assert !native.check_signature_policy(path, none, engine.SignaturePolicy{})!.valid |
| 182 | assert native.check_signature_policy(path, none, engine.SignaturePolicy{ unknown: true })!.valid |
| 183 | timeless := ring.generate_at('Expired Signature Fixture <expired-signature@example.invalid>', past, '0')! |
| 184 | ring.trust(timeless, 6)! |
| 185 | ring.sign_at(path, timeless, past + 3600, '1d')! |
| 186 | assert compare_signature_check(mut native, old, pkg, path, none)![0].status == .sig_expired |
| 187 | assert !native.check_signature_policy(path, none, engine.SignaturePolicy{ marginal: true, unknown: true })!.valid |
| 188 | ring.sign(path, [ring.fingerprint], false)! |
| 189 | os.write_file(path, 'tampered')! |
| 190 | assert compare_signature_check(mut native, old, pkg, path, none)![0].status == .invalid |
| 191 | compare_signature_load(mut native, old, path, 1)! |
| 192 | compare_signature_load(mut native, old, path, 15)! |
| 193 | os.rm(path + '.sig')! |
| 194 | compare_signature_check(mut native, old, pkg, path, none)! |
| 195 | for level in [0, 1, 3, 15] { compare_signature_load(mut native, old, path, level)! } |
| 196 | os.rm(policy_path + '.sig')! |
| 197 | for level in [0, 1, 3, 15] { compare_database_signature_policy(mut native, old, level << 10)! } |
| 198 | os.write_file(path + '.sig', '')! |
| 199 | compare_signature_check(mut native, old, pkg, path, none)! |
| 200 | } |
| 201 | |