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