| Elijah Newren | d812c3b | 2023-04-11 07:41:56 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 | [diff] [blame] | 2 | #include "environment.h" |
| Junio C Hamano | 8f1d2e6 | 2006-01-07 09:33:54 | [diff] [blame] | 3 | #include "tag.h" |
| Elijah Newren | dabab1d | 2023-04-11 07:41:49 | [diff] [blame] | 4 | #include "object-name.h" |
| Elijah Newren | a034e91 | 2023-05-16 06:34:06 | [diff] [blame] | 5 | #include "object-store-ll.h" |
| Nicolas Pitre | 0ab1795 | 2007-02-26 19:56:00 | [diff] [blame] | 6 | #include "commit.h" |
| 7 | #include "tree.h" |
| 8 | #include "blob.h" |
| Stefan Beller | 14ba97f | 2018-05-15 21:48:42 | [diff] [blame] | 9 | #include "alloc.h" |
| Lukas Puehringer | 94240b9 | 2017-01-17 23:37:18 | [diff] [blame] | 10 | #include "gpg-interface.h" |
| Elijah Newren | 41771fa | 2023-02-24 00:09:27 | [diff] [blame] | 11 | #include "hex.h" |
| Jonathan Tan | 8c4cc32 | 2018-07-13 00:03:07 | [diff] [blame] | 12 | #include "packfile.h" |
| Daniel Barkalow | 2636f61 | 2005-04-28 14:46:33 | [diff] [blame] | 13 | |
| 14 | const char *tag_type = "tag"; |
| 15 | |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 16 | static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags) |
| 17 | { |
| 18 | struct signature_check sigc; |
| brian m. carlson | 482c119 | 2021-02-11 02:08:03 | [diff] [blame] | 19 | struct strbuf payload = STRBUF_INIT; |
| 20 | struct strbuf signature = STRBUF_INIT; |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 21 | int ret; |
| 22 | |
| 23 | memset(&sigc, 0, sizeof(sigc)); |
| 24 | |
| brian m. carlson | 482c119 | 2021-02-11 02:08:03 | [diff] [blame] | 25 | if (!parse_signature(buf, size, &payload, &signature)) { |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 26 | if (flags & GPG_VERIFY_VERBOSE) |
| brian m. carlson | 482c119 | 2021-02-11 02:08:03 | [diff] [blame] | 27 | write_in_full(1, buf, size); |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 28 | return error("no signature found"); |
| 29 | } |
| 30 | |
| Fabian Stelzer | dd3aa41 | 2021-12-09 08:52:47 | [diff] [blame] | 31 | sigc.payload_type = SIGNATURE_PAYLOAD_TAG; |
| Fabian Stelzer | 0276943 | 2021-12-09 08:52:43 | [diff] [blame] | 32 | sigc.payload = strbuf_detach(&payload, &sigc.payload_len); |
| 33 | ret = check_signature(&sigc, signature.buf, signature.len); |
| Lukas Puehringer | 94240b9 | 2017-01-17 23:37:18 | [diff] [blame] | 34 | |
| 35 | if (!(flags & GPG_VERIFY_OMIT_STATUS)) |
| 36 | print_signature_buffer(&sigc, flags); |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 37 | |
| 38 | signature_check_clear(&sigc); |
| brian m. carlson | 482c119 | 2021-02-11 02:08:03 | [diff] [blame] | 39 | strbuf_release(&payload); |
| 40 | strbuf_release(&signature); |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 41 | return ret; |
| 42 | } |
| 43 | |
| Stefan Beller | 8457176 | 2017-07-13 00:44:15 | [diff] [blame] | 44 | int gpg_verify_tag(const struct object_id *oid, const char *name_to_report, |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 45 | unsigned flags) |
| 46 | { |
| 47 | enum object_type type; |
| 48 | char *buf; |
| 49 | unsigned long size; |
| 50 | int ret; |
| 51 | |
| Stefan Beller | 0df8e96 | 2018-04-25 18:20:59 | [diff] [blame] | 52 | type = oid_object_info(the_repository, oid, NULL); |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 53 | if (type != OBJ_TAG) |
| 54 | return error("%s: cannot verify a non-tag object of type %s.", |
| 55 | name_to_report ? |
| 56 | name_to_report : |
| Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 13:58:46 | [diff] [blame] | 57 | repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV), |
| Brandon Williams | debca9d | 2018-02-14 18:59:24 | [diff] [blame] | 58 | type_name(type)); |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 59 | |
| Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 13:58:50 | [diff] [blame] | 60 | buf = repo_read_object_file(the_repository, oid, &type, &size); |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 61 | if (!buf) |
| 62 | return error("%s: unable to read file.", |
| 63 | name_to_report ? |
| 64 | name_to_report : |
| Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 13:58:46 | [diff] [blame] | 65 | repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV)); |
| Santiago Torres | 45a227e | 2016-04-22 14:52:04 | [diff] [blame] | 66 | |
| 67 | ret = run_gpg_verify(buf, size, flags); |
| 68 | |
| 69 | free(buf); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| Stefan Beller | 286d258 | 2018-06-29 01:22:20 | [diff] [blame] | 73 | struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen) |
| Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 74 | { |
| Jonathan Tan | 8c4cc32 | 2018-07-13 00:03:07 | [diff] [blame] | 75 | struct object_id *last_oid = NULL; |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 76 | while (o && o->type == OBJ_TAG) |
| Jonathan Tan | 8c4cc32 | 2018-07-13 00:03:07 | [diff] [blame] | 77 | if (((struct tag *)o)->tagged) { |
| 78 | last_oid = &((struct tag *)o)->tagged->oid; |
| Junio C Hamano | 09ca613 | 2018-08-02 22:30:46 | [diff] [blame] | 79 | o = parse_object(r, last_oid); |
| Jonathan Tan | 8c4cc32 | 2018-07-13 00:03:07 | [diff] [blame] | 80 | } else { |
| 81 | last_oid = NULL; |
| Martin Koegler | 24e8a3c | 2008-02-18 07:31:55 | [diff] [blame] | 82 | o = NULL; |
| Jonathan Tan | 8c4cc32 | 2018-07-13 00:03:07 | [diff] [blame] | 83 | } |
| Junio C Hamano | 9534f40 | 2005-11-02 23:19:13 | [diff] [blame] | 84 | if (!o && warn) { |
| Jonathan Tan | 8c4cc32 | 2018-07-13 00:03:07 | [diff] [blame] | 85 | if (last_oid && is_promisor_object(last_oid)) |
| 86 | return NULL; |
| Junio C Hamano | 9534f40 | 2005-11-02 23:19:13 | [diff] [blame] | 87 | if (!warnlen) |
| 88 | warnlen = strlen(warn); |
| 89 | error("missing object referenced by '%.*s'", warnlen, warn); |
| 90 | } |
| Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 91 | return o; |
| 92 | } |
| 93 | |
| Jeff King | 90108a2 | 2012-01-06 19:18:01 | [diff] [blame] | 94 | struct object *deref_tag_noverify(struct object *o) |
| 95 | { |
| 96 | while (o && o->type == OBJ_TAG) { |
| Stefan Beller | 109cd76 | 2018-06-29 01:21:51 | [diff] [blame] | 97 | o = parse_object(the_repository, &o->oid); |
| Jeff King | 90108a2 | 2012-01-06 19:18:01 | [diff] [blame] | 98 | if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged) |
| 99 | o = ((struct tag *)o)->tagged; |
| 100 | else |
| 101 | o = NULL; |
| 102 | } |
| 103 | return o; |
| 104 | } |
| 105 | |
| Stefan Beller | 8bde69b | 2018-06-29 01:22:11 | [diff] [blame] | 106 | struct tag *lookup_tag(struct repository *r, const struct object_id *oid) |
| Daniel Barkalow | 2636f61 | 2005-04-28 14:46:33 | [diff] [blame] | 107 | { |
| Jeff King | d0229ab | 2019-06-20 07:41:14 | [diff] [blame] | 108 | struct object *obj = lookup_object(r, oid); |
| Linus Torvalds | 100c5f3 | 2007-04-17 05:11:43 | [diff] [blame] | 109 | if (!obj) |
| Jeff King | a378509 | 2019-06-20 07:41:21 | [diff] [blame] | 110 | return create_object(r, oid, alloc_tag_node(r)); |
| Abhishek Kumar | 6da43d9 | 2020-06-17 09:14:08 | [diff] [blame] | 111 | return object_as_type(obj, OBJ_TAG, 0); |
| Daniel Barkalow | 2636f61 | 2005-04-28 14:46:33 | [diff] [blame] | 112 | } |
| 113 | |
| Johannes Schindelin | dddbad7 | 2017-04-26 19:29:31 | [diff] [blame] | 114 | static timestamp_t parse_tag_date(const char *buf, const char *tail) |
| Shawn O. Pearce | e451d06 | 2010-04-12 23:25:28 | [diff] [blame] | 115 | { |
| 116 | const char *dateptr; |
| 117 | |
| 118 | while (buf < tail && *buf++ != '>') |
| 119 | /* nada */; |
| 120 | if (buf >= tail) |
| 121 | return 0; |
| 122 | dateptr = buf; |
| 123 | while (buf < tail && *buf++ != '\n') |
| 124 | /* nada */; |
| 125 | if (buf >= tail) |
| 126 | return 0; |
| Johannes Schindelin | 1aeb7e7 | 2017-04-21 10:45:44 | [diff] [blame] | 127 | /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */ |
| 128 | return parse_timestamp(dateptr, NULL, 10); |
| Shawn O. Pearce | e451d06 | 2010-04-12 23:25:28 | [diff] [blame] | 129 | } |
| 130 | |
| Stefan Beller | 14ba97f | 2018-05-15 21:48:42 | [diff] [blame] | 131 | void release_tag_memory(struct tag *t) |
| 132 | { |
| 133 | free(t->tag); |
| 134 | t->tagged = NULL; |
| 135 | t->object.parsed = 0; |
| 136 | t->date = 0; |
| 137 | } |
| 138 | |
| Stefan Beller | 84f80cd | 2018-06-29 01:22:12 | [diff] [blame] | 139 | int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size) |
| Daniel Barkalow | 2636f61 | 2005-04-28 14:46:33 | [diff] [blame] | 140 | { |
| brian m. carlson | 1e4085a | 2017-05-06 22:10:02 | [diff] [blame] | 141 | struct object_id oid; |
| Daniel Barkalow | 89e4202 | 2005-06-22 00:35:10 | [diff] [blame] | 142 | char type[20]; |
| Shawn O. Pearce | 28de5b6 | 2010-04-12 23:25:27 | [diff] [blame] | 143 | const char *bufptr = data; |
| 144 | const char *tail = bufptr + size; |
| 145 | const char *nl; |
| Edgar Toernig | ae200ee | 2005-04-30 16:51:03 | [diff] [blame] | 146 | |
| Shawn O. Pearce | 2e0052a | 2010-04-12 23:25:25 | [diff] [blame] | 147 | if (item->object.parsed) |
| 148 | return 0; |
| Jeff King | 228c78f | 2019-10-25 21:20:20 | [diff] [blame] | 149 | |
| 150 | if (item->tag) { |
| 151 | /* |
| 152 | * Presumably left over from a previous failed parse; |
| 153 | * clear it out in preparation for re-parsing (we'll probably |
| 154 | * hit the same error, which lets us tell our current caller |
| 155 | * about the problem). |
| 156 | */ |
| 157 | FREE_AND_NULL(item->tag); |
| 158 | } |
| Daniel Barkalow | 2636f61 | 2005-04-28 14:46:33 | [diff] [blame] | 159 | |
| brian m. carlson | d8a3a69 | 2018-10-15 00:01:58 | [diff] [blame] | 160 | if (size < the_hash_algo->hexsz + 24) |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 161 | return -1; |
| brian m. carlson | 1e4085a | 2017-05-06 22:10:02 | [diff] [blame] | 162 | if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n') |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 163 | return -1; |
| Daniel Barkalow | 2636f61 | 2005-04-28 14:46:33 | [diff] [blame] | 164 | |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 165 | if (!starts_with(bufptr, "type ")) |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 166 | return -1; |
| Shawn O. Pearce | 28de5b6 | 2010-04-12 23:25:27 | [diff] [blame] | 167 | bufptr += 5; |
| 168 | nl = memchr(bufptr, '\n', tail - bufptr); |
| 169 | if (!nl || sizeof(type) <= (nl - bufptr)) |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 170 | return -1; |
| Jeff King | eddda37 | 2015-09-24 21:08:26 | [diff] [blame] | 171 | memcpy(type, bufptr, nl - bufptr); |
| Shawn O. Pearce | 28de5b6 | 2010-04-12 23:25:27 | [diff] [blame] | 172 | type[nl - bufptr] = '\0'; |
| 173 | bufptr = nl + 1; |
| Daniel Barkalow | 2636f61 | 2005-04-28 14:46:33 | [diff] [blame] | 174 | |
| Nicolas Pitre | 0ab1795 | 2007-02-26 19:56:00 | [diff] [blame] | 175 | if (!strcmp(type, blob_type)) { |
| Stefan Beller | 84f80cd | 2018-06-29 01:22:12 | [diff] [blame] | 176 | item->tagged = (struct object *)lookup_blob(r, &oid); |
| Nicolas Pitre | 0ab1795 | 2007-02-26 19:56:00 | [diff] [blame] | 177 | } else if (!strcmp(type, tree_type)) { |
| Stefan Beller | 84f80cd | 2018-06-29 01:22:12 | [diff] [blame] | 178 | item->tagged = (struct object *)lookup_tree(r, &oid); |
| Nicolas Pitre | 0ab1795 | 2007-02-26 19:56:00 | [diff] [blame] | 179 | } else if (!strcmp(type, commit_type)) { |
| Stefan Beller | 84f80cd | 2018-06-29 01:22:12 | [diff] [blame] | 180 | item->tagged = (struct object *)lookup_commit(r, &oid); |
| Nicolas Pitre | 0ab1795 | 2007-02-26 19:56:00 | [diff] [blame] | 181 | } else if (!strcmp(type, tag_type)) { |
| Stefan Beller | 84f80cd | 2018-06-29 01:22:12 | [diff] [blame] | 182 | item->tagged = (struct object *)lookup_tag(r, &oid); |
| Nicolas Pitre | 0ab1795 | 2007-02-26 19:56:00 | [diff] [blame] | 183 | } else { |
| Jeff King | 78d5014 | 2019-10-18 04:45:35 | [diff] [blame] | 184 | return error("unknown tag type '%s' in %s", |
| 185 | type, oid_to_hex(&item->object.oid)); |
| Nicolas Pitre | 0ab1795 | 2007-02-26 19:56:00 | [diff] [blame] | 186 | } |
| 187 | |
| Jeff King | 78d5014 | 2019-10-18 04:45:35 | [diff] [blame] | 188 | if (!item->tagged) |
| 189 | return error("bad tag pointer to %s in %s", |
| 190 | oid_to_hex(&oid), |
| 191 | oid_to_hex(&item->object.oid)); |
| 192 | |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 193 | if (bufptr + 4 < tail && starts_with(bufptr, "tag ")) |
| Nguyễn Thái Ngọc Duy | 8559425 | 2011-02-14 13:02:51 | [diff] [blame] | 194 | ; /* good */ |
| 195 | else |
| Shawn O. Pearce | 28de5b6 | 2010-04-12 23:25:27 | [diff] [blame] | 196 | return -1; |
| 197 | bufptr += 4; |
| 198 | nl = memchr(bufptr, '\n', tail - bufptr); |
| 199 | if (!nl) |
| 200 | return -1; |
| 201 | item->tag = xmemdupz(bufptr, nl - bufptr); |
| 202 | bufptr = nl + 1; |
| 203 | |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 204 | if (bufptr + 7 < tail && starts_with(bufptr, "tagger ")) |
| Shawn O. Pearce | e451d06 | 2010-04-12 23:25:28 | [diff] [blame] | 205 | item->date = parse_tag_date(bufptr, tail); |
| 206 | else |
| 207 | item->date = 0; |
| 208 | |
| Jeff King | 228c78f | 2019-10-25 21:20:20 | [diff] [blame] | 209 | item->object.parsed = 1; |
| Daniel Barkalow | 2636f61 | 2005-04-28 14:46:33 | [diff] [blame] | 210 | return 0; |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 211 | } |
| Sergey Vlasov | 13019d4 | 2005-05-04 17:44:15 | [diff] [blame] | 212 | |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 213 | int parse_tag(struct tag *item) |
| 214 | { |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 215 | enum object_type type; |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 216 | void *data; |
| 217 | unsigned long size; |
| 218 | int ret; |
| 219 | |
| 220 | if (item->object.parsed) |
| 221 | return 0; |
| Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 13:58:50 | [diff] [blame] | 222 | data = repo_read_object_file(the_repository, &item->object.oid, &type, |
| 223 | &size); |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 224 | if (!data) |
| 225 | return error("Could not read %s", |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 226 | oid_to_hex(&item->object.oid)); |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 227 | if (type != OBJ_TAG) { |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 228 | free(data); |
| 229 | return error("Object %s not a tag", |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 230 | oid_to_hex(&item->object.oid)); |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 231 | } |
| Stefan Beller | 0e740fe | 2018-06-29 01:22:04 | [diff] [blame] | 232 | ret = parse_tag_buffer(the_repository, item, data, size); |
| Sergey Vlasov | 13019d4 | 2005-05-04 17:44:15 | [diff] [blame] | 233 | free(data); |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 234 | return ret; |
| Daniel Barkalow | 2636f61 | 2005-04-28 14:46:33 | [diff] [blame] | 235 | } |
| René Scharfe | dad3f06 | 2019-09-05 19:55:55 | [diff] [blame] | 236 | |
| 237 | struct object_id *get_tagged_oid(struct tag *tag) |
| 238 | { |
| 239 | if (!tag->tagged) |
| 240 | die("bad tag"); |
| 241 | return &tag->tagged->oid; |
| 242 | } |