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