| 1 | // gpgme supplies OpenPGP cryptography; policy and result ownership stay in V. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import os |
| 6 | import sync |
| 7 | import time |
| 8 | |
| 9 | #flag -lgpgme |
| 10 | #flag -lgpg-error |
| 11 | #include <gpgme.h> |
| 12 | #include <locale.h> |
| 13 | |
| 14 | struct C.gpgme_context {} |
| 15 | |
| 16 | struct C.gpgme_data {} |
| 17 | |
| 18 | struct C._gpgme_subkey { |
| 19 | next &C._gpgme_subkey |
| 20 | fpr &char |
| 21 | keyid &char |
| 22 | timestamp uint |
| 23 | expires uint |
| 24 | length u32 |
| 25 | revoked u32 |
| 26 | } |
| 27 | |
| 28 | struct C._gpgme_user_id { |
| 29 | uid &char |
| 30 | name &char |
| 31 | email &char |
| 32 | } |
| 33 | |
| 34 | struct C._gpgme_key { |
| 35 | subkeys &C._gpgme_subkey |
| 36 | uids &C._gpgme_user_id |
| 37 | disabled u32 |
| 38 | expired u32 |
| 39 | owner_trust i32 |
| 40 | } |
| 41 | |
| 42 | struct C._gpgme_signature { |
| 43 | next &C._gpgme_signature |
| 44 | fpr &char |
| 45 | status u32 |
| 46 | validity i32 |
| 47 | summary u32 |
| 48 | timestamp uint |
| 49 | exp_timestamp uint |
| 50 | validity_reason u32 |
| 51 | } |
| 52 | |
| 53 | struct C._gpgme_op_verify_result { |
| 54 | signatures &C._gpgme_signature |
| 55 | } |
| 56 | |
| 57 | fn C.gpgme_check_version(&char) &char |
| 58 | fn C.gpgme_engine_check_version(i32) u32 |
| 59 | fn C.gpgme_new(&&C.gpgme_context) u32 |
| 60 | fn C.gpgme_release(&C.gpgme_context) |
| 61 | fn C.gpgme_set_protocol(&C.gpgme_context, i32) u32 |
| 62 | fn C.gpgme_ctx_set_engine_info(&C.gpgme_context, i32, &char, &char) u32 |
| 63 | fn C.gpgme_set_locale(&C.gpgme_context, i32, &char) u32 |
| 64 | fn C.gpgme_strerror(u32) &char |
| 65 | fn C.gpgme_data_new_from_fd(&&C.gpgme_data, i32) u32 |
| 66 | fn C.gpgme_data_new_from_mem(&&C.gpgme_data, &char, uint, i32) u32 |
| 67 | fn C.gpgme_data_release(&C.gpgme_data) |
| 68 | fn C.gpgme_op_verify(&C.gpgme_context, &C.gpgme_data, &C.gpgme_data, &C.gpgme_data) u32 |
| 69 | fn C.gpgme_op_verify_result(&C.gpgme_context) &C._gpgme_op_verify_result |
| 70 | fn C.gpgme_get_key(&C.gpgme_context, &char, &&C._gpgme_key, i32) u32 |
| 71 | fn C.gpgme_key_unref(&C._gpgme_key) |
| 72 | fn C.gpgme_get_keylist_mode(&C.gpgme_context) u32 |
| 73 | fn C.gpgme_set_keylist_mode(&C.gpgme_context, u32) u32 |
| 74 | fn C.gpgme_op_import_keys(&C.gpgme_context, &&C._gpgme_key) u32 |
| 75 | fn C.gpgme_op_import_result(&C.gpgme_context) &C._gpgme_op_import_result |
| 76 | |
| 77 | struct C._gpgme_import_status { |
| 78 | result u32 |
| 79 | } |
| 80 | |
| 81 | struct C._gpgme_op_import_result { |
| 82 | considered i32 |
| 83 | imports &C._gpgme_import_status |
| 84 | } |
| 85 | |
| 86 | struct C._gpgme_engine_info { |
| 87 | next &C._gpgme_engine_info |
| 88 | protocol i32 |
| 89 | file_name &char |
| 90 | home_dir &char |
| 91 | } |
| 92 | |
| 93 | fn C.gpgme_get_engine_info(&&C._gpgme_engine_info) u32 |
| 94 | |
| 95 | fn C.setlocale(i32, &char) &char |
| 96 | |
| 97 | const gpgme_initialization = sync.new_mutex() |
| 98 | |
| 99 | fn gpg_error_code(status u32) u32 { return status & 0xffff } |
| 100 | |
| 101 | fn (mut handle Handle) check_gpg_error(status u32) ! { |
| 102 | if gpg_error_code(status) != 0 { |
| 103 | return handle.fail(.gpgme, unsafe { cstring_to_vstring(C.gpgme_strerror(status)) }) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | fn signature_library_version() ?string { |
| 108 | mut initialization := gpgme_initialization |
| 109 | initialization.@lock() |
| 110 | defer { initialization.unlock() } |
| 111 | version := C.gpgme_check_version(unsafe { nil }) |
| 112 | if version == unsafe { nil } { return none } |
| 113 | return gpg_string(version) |
| 114 | } |
| 115 | |
| 116 | fn (mut handle Handle) check_initial_keyring(home string) { |
| 117 | legacy := home + 'pubring.gpg' |
| 118 | modern := home + 'pubring.kbx' |
| 119 | // modern GnuPG keyboxes are valid public keyrings when no legacy ring exists. |
| 120 | public_ring := if !os.exists(legacy) && os.exists(modern) { modern } else { legacy } |
| 121 | if !handle.package_readable(public_ring) || !handle.package_readable(home + 'trustdb.gpg') { |
| 122 | handle.last_error = .not_a_file |
| 123 | handle.last_detail_valid = false |
| 124 | handle.log(.debug, 'Signature verification will fail!\n') |
| 125 | handle.log(.warning, "Public keyring not found; have you run 'pacman-key --init'?\n") |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | fn signature_log_string(pointer &char) string { |
| 130 | return if pointer == unsafe { nil } { '(null)' } else { gpg_string(pointer) } |
| 131 | } |
| 132 | |
| 133 | fn (mut handle Handle) initialize_signatures() ! { |
| 134 | if handle.signature_initialized { return } |
| 135 | home := handle.opts.strings[.gpgdir] |
| 136 | handle.check_initial_keyring(home) |
| 137 | version := signature_library_version() or { |
| 138 | return handle.fail(.gpgme, 'GPGME initialization failed') |
| 139 | } |
| 140 | // callbacks may verify another handle; never call them under the global setup mutex. |
| 141 | handle.log(.debug, 'GPGME version: ${version}\n') |
| 142 | mut initialization := gpgme_initialization |
| 143 | initialization.@lock() |
| 144 | mut held := true |
| 145 | defer { if held { initialization.unlock() } } |
| 146 | handle.check_gpg_error(C.gpgme_engine_check_version(C.GPGME_PROTOCOL_OpenPGP))! |
| 147 | mut info := &C._gpgme_engine_info(unsafe { nil }) |
| 148 | handle.check_gpg_error(C.gpgme_get_engine_info(&info))! |
| 149 | file := if info == unsafe { nil } { '(null)' } else { signature_log_string(info.file_name) } |
| 150 | directory := if home == '' { '(null)' } else { home } |
| 151 | held = false |
| 152 | initialization.unlock() |
| 153 | // initialization survives a later context allocation failure, as in signing.c. |
| 154 | handle.signature_initialized = true |
| 155 | handle.log(.debug, 'GPGME engine info: file=${file}, home=${directory}\n') |
| 156 | } |
| 157 | |
| 158 | fn (mut handle Handle) signature_context() !&C.gpgme_context { |
| 159 | return handle.signature_context_for_path(none) |
| 160 | } |
| 161 | |
| 162 | fn (mut handle Handle) signature_context_for_path(path ?string) !&C.gpgme_context { |
| 163 | handle.initialize_signatures()! |
| 164 | if filename := path { handle.log(.debug, 'checking signature for ${filename}\n') } |
| 165 | mut context := &C.gpgme_context(unsafe { nil }) |
| 166 | handle.check_gpg_error(C.gpgme_new(&context))! |
| 167 | mut ready := false |
| 168 | defer { if !ready { C.gpgme_release(context) } } |
| 169 | handle.check_gpg_error(C.gpgme_set_protocol(context, C.GPGME_PROTOCOL_OpenPGP))! |
| 170 | // global engine configuration leaks the first handle's keyring into later handles. |
| 171 | home := handle.opts.strings[.gpgdir] |
| 172 | home_pointer := if home == '' { unsafe { &char(nil) } } else { unsafe { &char(home.str) } } |
| 173 | handle.check_gpg_error(C.gpgme_ctx_set_engine_info(context, C.GPGME_PROTOCOL_OpenPGP, unsafe { nil }, home_pointer))! |
| 174 | handle.check_gpg_error(C.gpgme_set_locale(context, C.LC_CTYPE, C.setlocale(C.LC_CTYPE, unsafe { nil })))! |
| 175 | $if !windows { |
| 176 | handle.check_gpg_error(C.gpgme_set_locale(context, C.LC_MESSAGES, C.setlocale(C.LC_MESSAGES, unsafe { nil })))! |
| 177 | } |
| 178 | ready = true |
| 179 | return context |
| 180 | } |
| 181 | |
| 182 | fn gpg_string(pointer &char) string { |
| 183 | return if pointer == unsafe { nil } { '' } else { unsafe { cstring_to_vstring(pointer) } } |
| 184 | } |
| 185 | |
| 186 | fn signature_status(status u32, disabled bool) SigStatus { |
| 187 | if disabled { return .key_disabled } |
| 188 | return match int(gpg_error_code(status)) { |
| 189 | C.GPG_ERR_NO_ERROR { .valid } |
| 190 | C.GPG_ERR_KEY_EXPIRED { .key_expired } |
| 191 | C.GPG_ERR_SIG_EXPIRED { .sig_expired } |
| 192 | C.GPG_ERR_NO_PUBKEY { .key_unknown } |
| 193 | else { .invalid } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | fn signature_validity(validity i32) SigValidity { |
| 198 | return match validity { |
| 199 | C.GPGME_VALIDITY_ULTIMATE, C.GPGME_VALIDITY_FULL { .full } |
| 200 | C.GPGME_VALIDITY_MARGINAL { .marginal } |
| 201 | C.GPGME_VALIDITY_NEVER { .never } |
| 202 | else { .unknown } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | fn signature_validity_name(value i32) string { |
| 207 | return match value { |
| 208 | C.GPGME_VALIDITY_UNKNOWN { 'unknown' } |
| 209 | C.GPGME_VALIDITY_UNDEFINED { 'undefined' } |
| 210 | C.GPGME_VALIDITY_NEVER { 'never' } |
| 211 | C.GPGME_VALIDITY_MARGINAL { 'marginal' } |
| 212 | C.GPGME_VALIDITY_FULL { 'full' } |
| 213 | C.GPGME_VALIDITY_ULTIMATE { 'ultimate' } |
| 214 | else { '???' } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | fn signature_summaries(value u32) []string { |
| 219 | if value == 0 { return ['(empty)'] } |
| 220 | bits := [u32(C.GPGME_SIGSUM_VALID), u32(C.GPGME_SIGSUM_GREEN), u32(C.GPGME_SIGSUM_RED), |
| 221 | u32(C.GPGME_SIGSUM_KEY_REVOKED), u32(C.GPGME_SIGSUM_KEY_EXPIRED), |
| 222 | u32(C.GPGME_SIGSUM_SIG_EXPIRED), u32(C.GPGME_SIGSUM_KEY_MISSING), |
| 223 | u32(C.GPGME_SIGSUM_CRL_MISSING), u32(C.GPGME_SIGSUM_CRL_TOO_OLD), |
| 224 | u32(C.GPGME_SIGSUM_BAD_POLICY), u32(C.GPGME_SIGSUM_SYS_ERROR)] |
| 225 | labels := ['valid', 'green', 'red', 'key revoked', 'key expired', 'sig expired', 'key missing', |
| 226 | 'crl missing', 'crl too old', 'bad policy', 'sys error'] |
| 227 | mut values := []string{} |
| 228 | for index, bit in bits { if value & bit != 0 { values << labels[index] } } |
| 229 | return values |
| 230 | } |
| 231 | |
| 232 | fn (handle &Handle) log_signature_result(signature &C._gpgme_signature) { |
| 233 | fingerprint := if signature.fpr == unsafe { nil } { |
| 234 | '(null)' |
| 235 | } else { |
| 236 | gpg_string(signature.fpr) |
| 237 | } |
| 238 | handle.log(.debug, 'fingerprint: ${fingerprint}\n') |
| 239 | for summary in signature_summaries(signature.summary) { |
| 240 | handle.log(.debug, 'summary: ${summary}\n') |
| 241 | } |
| 242 | handle.log(.debug, 'status: ${gpg_string(C.gpgme_strerror(signature.status))}\n') |
| 243 | handle.log(.debug, 'timestamp: ${signature.timestamp}\n') |
| 244 | if i64(signature.timestamp) > time.now().unix() { |
| 245 | handle.log(.debug, 'signature timestamp is greater than system time.\n') |
| 246 | } |
| 247 | handle.log(.debug, 'exp_timestamp: ${signature.exp_timestamp}\n') |
| 248 | handle.log(.debug, 'validity: ${signature_validity_name(signature.validity)}; reason: ${gpg_string(C.gpgme_strerror(signature.validity_reason))}\n') |
| 249 | } |
| 250 | |
| 251 | fn (mut handle Handle) signature_result(context &C.gpgme_context, signature &C._gpgme_signature, retain bool) !(SigResult, voidptr) { |
| 252 | handle.log_signature_result(signature) |
| 253 | mut key := &C._gpgme_key(unsafe { nil }) |
| 254 | defer { if key != unsafe { nil } { C.gpgme_key_unref(key) } } |
| 255 | status := C.gpgme_get_key(context, signature.fpr, &key, 0) |
| 256 | mut info := PGPKey{} |
| 257 | mut disabled := false |
| 258 | if gpg_error_code(status) == C.GPG_ERR_EOF { |
| 259 | handle.log(.debug, 'key lookup failed, unknown key\n') |
| 260 | info = PGPKey{ fingerprint: gpg_string(signature.fpr) } |
| 261 | } else { |
| 262 | handle.check_gpg_error(status)! |
| 263 | if key != unsafe { nil } && key.uids != unsafe { nil } && key.subkeys != unsafe { nil } { |
| 264 | info = PGPKey{ |
| 265 | known: true |
| 266 | fingerprint: gpg_string(key.subkeys.fpr) |
| 267 | uid: gpg_string(key.uids.uid) |
| 268 | name: gpg_string(key.uids.name) |
| 269 | email: gpg_string(key.uids.email) |
| 270 | created: i64(key.subkeys.timestamp) |
| 271 | expires: i64(key.subkeys.expires) |
| 272 | } |
| 273 | disabled = key.disabled != 0 |
| 274 | handle.log(.debug, 'key: ${info.fingerprint}, ${info.uid}, owner_trust ${signature_validity_name(key.owner_trust)}, disabled ${key.disabled}\n') |
| 275 | } |
| 276 | } |
| 277 | data := if retain && info.known { voidptr(key) } else { unsafe { nil } } |
| 278 | if data != unsafe { nil } { C.gpgme_key_ref(key) } |
| 279 | return SigResult{ key: info, status: signature_status(signature.status, disabled), validity: signature_validity(signature.validity) }, data |
| 280 | } |
| 281 | |
| 282 | pub fn (mut handle Handle) check_signature(filename string, embedded ?string) ![]SigResult { |
| 283 | handle.check()! |
| 284 | verification := handle.verify_signature(filename, embedded, false) or { |
| 285 | handle.log_signature_error(err) |
| 286 | return err |
| 287 | } |
| 288 | return verification.results |
| 289 | } |
| 290 | |
| 291 | fn (mut handle Handle) verify_signature(filename string, embedded ?string, retain bool) !SignatureVerification { |
| 292 | path := c_string_prefix(filename) |
| 293 | if !handle.package_readable(path) { |
| 294 | return handle.fail(.not_a_file, path) |
| 295 | } |
| 296 | mut signature_file := os.File{} |
| 297 | defer { if signature_file.is_opened { signature_file.close() } } |
| 298 | if embedded == none { |
| 299 | sigpath := path + '.sig' |
| 300 | if !handle.package_readable(sigpath) { |
| 301 | handle.log(.debug, 'sig path ${sigpath} could not be opened\n') |
| 302 | return handle.fail(.sig_missing, sigpath) |
| 303 | } |
| 304 | signature_file = os.open(sigpath) or { |
| 305 | handle.log(.debug, 'sig path ${sigpath} could not be opened\n') |
| 306 | return handle.fail(.sig_missing, err.msg()) |
| 307 | } |
| 308 | } |
| 309 | mut file := os.open(path) or { return handle.fail(.not_a_file, err.msg()) } |
| 310 | defer { file.close() } |
| 311 | context := handle.signature_context_for_path(path)! |
| 312 | defer { C.gpgme_release(context) } |
| 313 | mut file_data := &C.gpgme_data(unsafe { nil }) |
| 314 | handle.check_gpg_error(C.gpgme_data_new_from_fd(&file_data, file.fd))! |
| 315 | defer { C.gpgme_data_release(file_data) } |
| 316 | mut signature_data := &C.gpgme_data(unsafe { nil }) |
| 317 | defer { if signature_data != unsafe { nil } { C.gpgme_data_release(signature_data) } } |
| 318 | if value := embedded { |
| 319 | decoded := decode_signature(value) or { return handle.fail(.sig_invalid, err.msg()) } |
| 320 | // GPGME copies the data and requires a non-null pointer for an empty signature. |
| 321 | data := if decoded.len == 0 { c'' } else { unsafe { &char(decoded.data) } } |
| 322 | handle.check_gpg_error(C.gpgme_data_new_from_mem(&signature_data, data, uint(decoded.len), 1))! |
| 323 | } else { |
| 324 | handle.check_gpg_error(C.gpgme_data_new_from_fd(&signature_data, signature_file.fd))! |
| 325 | } |
| 326 | handle.check_gpg_error(C.gpgme_op_verify(context, signature_data, file_data, unsafe { nil }))! |
| 327 | verification := C.gpgme_op_verify_result(context) |
| 328 | if verification == unsafe { nil } || verification.signatures == unsafe { nil } { |
| 329 | handle.log(.debug, 'no signatures returned\n') |
| 330 | return handle.fail(.sig_missing, path) |
| 331 | } |
| 332 | mut result := []SigResult{} |
| 333 | mut keys := []voidptr{} |
| 334 | mut complete := false |
| 335 | defer { if !complete { release_signature_keys(keys) } } |
| 336 | mut count := 0 |
| 337 | for signature := verification.signatures; signature != unsafe { nil }; signature = signature.next { |
| 338 | count++ |
| 339 | } |
| 340 | handle.log(.debug, '${count} signatures returned\n') |
| 341 | for signature := verification.signatures; signature != unsafe { nil }; signature = signature.next { |
| 342 | value, key := handle.signature_result(context, signature, retain)! |
| 343 | result << value |
| 344 | if retain { keys << key } |
| 345 | } |
| 346 | complete = true |
| 347 | return SignatureVerification{ results: result, key_data: keys } |
| 348 | } |
| 349 | |