| 1 | // v4 binary-document issuer extraction follows pacman signing.c and RFC 4880. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import encoding.hex |
| 6 | |
| 7 | fn (handle &Handle) log_signature_format_error(identifier string, problem IError) { |
| 8 | unsupported := if problem is AlpmError { |
| 9 | problem.detail in ['partial signature packets are unsupported', |
| 10 | 'indeterminate signature packets are unsupported', |
| 11 | 'only v4 binary-document signatures are supported'] |
| 12 | } else { |
| 13 | false |
| 14 | } |
| 15 | message := if unsupported { |
| 16 | '%s: unsupported signature format\n' |
| 17 | } else { |
| 18 | '%s: signature format error\n' |
| 19 | } |
| 20 | handle.log(.error, localized_message(message, c_string_prefix(identifier))) |
| 21 | } |
| 22 | |
| 23 | pub fn (mut handle Handle) extract_signature_key_ids(identifier string, data []u8) ![]string { |
| 24 | handle.require_live()! |
| 25 | return extract_key_ids(data) or { |
| 26 | handle.log_signature_format_error(identifier, err) |
| 27 | // signing.c reports parser failures without changing the handle's previous errno. |
| 28 | return AlpmError{ kind: handle.last_error } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn signature_number(data []u8, position int, width int, limit int) !u64 { |
| 33 | if width < 1 || position < 0 || position > limit || width > limit - position { |
| 34 | return AlpmError{ kind: .sig_invalid, detail: 'truncated signature packet' } |
| 35 | } |
| 36 | mut result := u64(0) |
| 37 | for offset in 0 .. width { result = (result << 8) | u64(data[position + offset]) } |
| 38 | return result |
| 39 | } |
| 40 | |
| 41 | fn signature_subpacket_keys(data []u8, start int, end int) ![]string { |
| 42 | mut position := start |
| 43 | for position < end { |
| 44 | first := int(data[position]) |
| 45 | position++ |
| 46 | mut length := u64(first) |
| 47 | if first >= 192 && first < 255 { |
| 48 | length = u64((first - 192) << 8) + signature_number(data, position, 1, end)! + 192 |
| 49 | position++ |
| 50 | } else if first == 255 { |
| 51 | length = signature_number(data, position, 4, end)! |
| 52 | position += 4 |
| 53 | } |
| 54 | if length == 0 || length > u64(end - position) { |
| 55 | return AlpmError{ kind: .sig_invalid, detail: 'invalid signature subpacket length' } |
| 56 | } |
| 57 | if data[position] == 16 { |
| 58 | if length < 9 { |
| 59 | return AlpmError{ kind: .sig_invalid, detail: 'truncated issuer key ID' } |
| 60 | } |
| 61 | return [hex.encode(data[position + 1..position + 9]).to_upper()] |
| 62 | } |
| 63 | position += int(length) |
| 64 | } |
| 65 | return []string{} |
| 66 | } |
| 67 | |
| 68 | pub fn extract_key_ids(data []u8) ![]string { |
| 69 | mut keys := []string{} |
| 70 | mut position := 0 |
| 71 | for position < data.len { |
| 72 | header := data[position] |
| 73 | position++ |
| 74 | if header & 0x80 == 0 { |
| 75 | return AlpmError{ kind: .sig_invalid, detail: 'invalid signature packet header' } |
| 76 | } |
| 77 | mut length := u64(0) |
| 78 | if header & 0x40 != 0 { |
| 79 | if header & 0x3f != 2 { |
| 80 | return AlpmError{ kind: .sig_invalid, detail: 'packet is not a signature' } |
| 81 | } |
| 82 | first := int(signature_number(data, position, 1, data.len)!) |
| 83 | position++ |
| 84 | if first < 192 { |
| 85 | length = u64(first) |
| 86 | } else if first < 224 { |
| 87 | length = u64((first - 192) << 8) + signature_number(data, position, 1, data.len)! + 192 |
| 88 | position++ |
| 89 | } else if first == 255 { |
| 90 | length = signature_number(data, position, 4, data.len)! |
| 91 | position += 4 |
| 92 | } else { |
| 93 | return AlpmError{ kind: .sig_invalid, detail: 'partial signature packets are unsupported' } |
| 94 | } |
| 95 | } else { |
| 96 | if (header >> 2) & 0xf != 2 { |
| 97 | return AlpmError{ kind: .sig_invalid, detail: 'packet is not a signature' } |
| 98 | } |
| 99 | width := match header & 3 { |
| 100 | 0 { 1 } |
| 101 | 1 { 2 } |
| 102 | 2 { 4 } |
| 103 | else { |
| 104 | return AlpmError{ kind: .sig_invalid, detail: 'indeterminate signature packets are unsupported' } |
| 105 | } |
| 106 | } |
| 107 | length = signature_number(data, position, width, data.len)! |
| 108 | position += width |
| 109 | } |
| 110 | if length < 10 || length > u64(data.len - position) { |
| 111 | return AlpmError{ kind: .sig_invalid, detail: 'invalid signature packet length' } |
| 112 | } |
| 113 | end := position + int(length) |
| 114 | if data[position] != 4 { |
| 115 | return AlpmError{ kind: .sig_invalid, detail: 'only v4 binary-document signatures are supported' } |
| 116 | } |
| 117 | if data[position + 1] != 0 { |
| 118 | return AlpmError{ kind: .sig_invalid, detail: 'signature is not a binary document' } |
| 119 | } |
| 120 | position += 4 |
| 121 | hashed_length := int(signature_number(data, position, 2, end)!) |
| 122 | position += 2 |
| 123 | if hashed_length > end - position - 4 { |
| 124 | return AlpmError{ kind: .sig_invalid, detail: 'invalid hashed subpacket area' } |
| 125 | } |
| 126 | keys << signature_subpacket_keys(data, position, position + hashed_length)! |
| 127 | position += hashed_length |
| 128 | unhashed_length := int(signature_number(data, position, 2, end)!) |
| 129 | position += 2 |
| 130 | if unhashed_length > end - position - 2 { |
| 131 | return AlpmError{ kind: .sig_invalid, detail: 'invalid unhashed subpacket area' } |
| 132 | } |
| 133 | keys << signature_subpacket_keys(data, position, position + unhashed_length)! |
| 134 | position = end |
| 135 | } |
| 136 | return keys |
| 137 | } |
| 138 | |