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; |
| 16 | size_t payload_size; |
| 17 | int ret; |
| 18 | |
| 19 | memset(&sigc, 0, sizeof(sigc)); |
| 20 | |
| 21 | payload_size = parse_signature(buf, size); |
| 22 | |
| 23 | if (size == payload_size) { |
| 24 | if (flags & GPG_VERIFY_VERBOSE) |
| 25 | write_in_full(1, buf, payload_size); |
| 26 | return error("no signature found"); |
| 27 | } |
| 28 | |
| 29 | ret = check_signature(buf, payload_size, buf + payload_size, |
| 30 | size - payload_size, &sigc); |
Lukas Puehringer | 94240b9 | 2017-01-17 18:37:18 -0500 | [diff] [blame] | 31 | |
| 32 | if (!(flags & GPG_VERIFY_OMIT_STATUS)) |
| 33 | print_signature_buffer(&sigc, flags); |
Santiago Torres | 45a227e | 2016-04-22 10:52:04 -0400 | [diff] [blame] | 34 | |
| 35 | signature_check_clear(&sigc); |
| 36 | return ret; |
| 37 | } |
| 38 | |
Stefan Beller | 8457176 | 2017-07-12 17:44:15 -0700 | [diff] [blame] | 39 | 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] | 40 | unsigned flags) |
| 41 | { |
| 42 | enum object_type type; |
| 43 | char *buf; |
| 44 | unsigned long size; |
| 45 | int ret; |
| 46 | |
Stefan Beller | 0df8e96 | 2018-04-25 11:20:59 -0700 | [diff] [blame] | 47 | type = oid_object_info(the_repository, oid, NULL); |
Santiago Torres | 45a227e | 2016-04-22 10:52:04 -0400 | [diff] [blame] | 48 | if (type != OBJ_TAG) |
| 49 | return error("%s: cannot verify a non-tag object of type %s.", |
| 50 | name_to_report ? |
| 51 | name_to_report : |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 52 | find_unique_abbrev(oid, DEFAULT_ABBREV), |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 53 | type_name(type)); |
Santiago Torres | 45a227e | 2016-04-22 10:52:04 -0400 | [diff] [blame] | 54 | |
brian m. carlson | b4f5aca | 2018-03-12 02:27:53 +0000 | [diff] [blame] | 55 | buf = read_object_file(oid, &type, &size); |
Santiago Torres | 45a227e | 2016-04-22 10:52:04 -0400 | [diff] [blame] | 56 | if (!buf) |
| 57 | return error("%s: unable to read file.", |
| 58 | name_to_report ? |
| 59 | name_to_report : |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 60 | find_unique_abbrev(oid, DEFAULT_ABBREV)); |
Santiago Torres | 45a227e | 2016-04-22 10:52:04 -0400 | [diff] [blame] | 61 | |
| 62 | ret = run_gpg_verify(buf, size, flags); |
| 63 | |
| 64 | free(buf); |
| 65 | return ret; |
| 66 | } |
| 67 | |
Stefan Beller | 286d258 | 2018-06-28 18:22:20 -0700 | [diff] [blame] | 68 | 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] | 69 | { |
Jonathan Tan | 8c4cc32 | 2018-07-12 17:03:07 -0700 | [diff] [blame] | 70 | struct object_id *last_oid = NULL; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 71 | while (o && o->type == OBJ_TAG) |
Jonathan Tan | 8c4cc32 | 2018-07-12 17:03:07 -0700 | [diff] [blame] | 72 | if (((struct tag *)o)->tagged) { |
| 73 | last_oid = &((struct tag *)o)->tagged->oid; |
Junio C Hamano | 09ca613 | 2018-08-02 15:30:46 -0700 | [diff] [blame] | 74 | o = parse_object(r, last_oid); |
Jonathan Tan | 8c4cc32 | 2018-07-12 17:03:07 -0700 | [diff] [blame] | 75 | } else { |
| 76 | last_oid = NULL; |
Martin Koegler | 24e8a3c | 2008-02-18 08:31:55 +0100 | [diff] [blame] | 77 | o = NULL; |
Jonathan Tan | 8c4cc32 | 2018-07-12 17:03:07 -0700 | [diff] [blame] | 78 | } |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 79 | if (!o && warn) { |
Jonathan Tan | 8c4cc32 | 2018-07-12 17:03:07 -0700 | [diff] [blame] | 80 | if (last_oid && is_promisor_object(last_oid)) |
| 81 | return NULL; |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 82 | if (!warnlen) |
| 83 | warnlen = strlen(warn); |
| 84 | error("missing object referenced by '%.*s'", warnlen, warn); |
| 85 | } |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 86 | return o; |
| 87 | } |
| 88 | |
Jeff King | 90108a2 | 2012-01-06 14:18:01 -0500 | [diff] [blame] | 89 | struct object *deref_tag_noverify(struct object *o) |
| 90 | { |
| 91 | while (o && o->type == OBJ_TAG) { |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 92 | o = parse_object(the_repository, &o->oid); |
Jeff King | 90108a2 | 2012-01-06 14:18:01 -0500 | [diff] [blame] | 93 | if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged) |
| 94 | o = ((struct tag *)o)->tagged; |
| 95 | else |
| 96 | o = NULL; |
| 97 | } |
| 98 | return o; |
| 99 | } |
| 100 | |
Stefan Beller | 8bde69b | 2018-06-28 18:22:11 -0700 | [diff] [blame] | 101 | struct tag *lookup_tag(struct repository *r, const struct object_id *oid) |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 102 | { |
Stefan Beller | 8bde69b | 2018-06-28 18:22:11 -0700 | [diff] [blame] | 103 | struct object *obj = lookup_object(r, oid->hash); |
Linus Torvalds | 100c5f3 | 2007-04-16 22:11:43 -0700 | [diff] [blame] | 104 | if (!obj) |
Stefan Beller | 8bde69b | 2018-06-28 18:22:11 -0700 | [diff] [blame] | 105 | return create_object(r, oid->hash, |
| 106 | alloc_tag_node(r)); |
| 107 | return object_as_type(r, 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; |
| 145 | item->object.parsed = 1; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 146 | |
brian m. carlson | 1e4085a | 2017-05-06 22:10:02 +0000 | [diff] [blame] | 147 | if (size < GIT_SHA1_HEXSZ + 24) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 148 | return -1; |
brian m. carlson | 1e4085a | 2017-05-06 22:10:02 +0000 | [diff] [blame] | 149 | 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] | 150 | return -1; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 151 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 152 | if (!starts_with(bufptr, "type ")) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 153 | return -1; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 154 | bufptr += 5; |
| 155 | nl = memchr(bufptr, '\n', tail - bufptr); |
| 156 | if (!nl || sizeof(type) <= (nl - bufptr)) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 157 | return -1; |
Jeff King | eddda37 | 2015-09-24 17:08:26 -0400 | [diff] [blame] | 158 | memcpy(type, bufptr, nl - bufptr); |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 159 | type[nl - bufptr] = '\0'; |
| 160 | bufptr = nl + 1; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 161 | |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 162 | if (!strcmp(type, blob_type)) { |
Stefan Beller | 84f80cd | 2018-06-28 18:22:12 -0700 | [diff] [blame] | 163 | item->tagged = (struct object *)lookup_blob(r, &oid); |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 164 | } else if (!strcmp(type, tree_type)) { |
Stefan Beller | 84f80cd | 2018-06-28 18:22:12 -0700 | [diff] [blame] | 165 | item->tagged = (struct object *)lookup_tree(r, &oid); |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 166 | } else if (!strcmp(type, commit_type)) { |
Stefan Beller | 84f80cd | 2018-06-28 18:22:12 -0700 | [diff] [blame] | 167 | item->tagged = (struct object *)lookup_commit(r, &oid); |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 168 | } else if (!strcmp(type, tag_type)) { |
Stefan Beller | 84f80cd | 2018-06-28 18:22:12 -0700 | [diff] [blame] | 169 | item->tagged = (struct object *)lookup_tag(r, &oid); |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 170 | } else { |
| 171 | error("Unknown type %s", type); |
| 172 | item->tagged = NULL; |
| 173 | } |
| 174 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 175 | 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] | 176 | ; /* good */ |
| 177 | else |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 178 | return -1; |
| 179 | bufptr += 4; |
| 180 | nl = memchr(bufptr, '\n', tail - bufptr); |
| 181 | if (!nl) |
| 182 | return -1; |
| 183 | item->tag = xmemdupz(bufptr, nl - bufptr); |
| 184 | bufptr = nl + 1; |
| 185 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 186 | if (bufptr + 7 < tail && starts_with(bufptr, "tagger ")) |
Shawn O. Pearce | e451d06 | 2010-04-12 16:25:28 -0700 | [diff] [blame] | 187 | item->date = parse_tag_date(bufptr, tail); |
| 188 | else |
| 189 | item->date = 0; |
| 190 | |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 191 | return 0; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 192 | } |
Sergey Vlasov | 13019d4 | 2005-05-04 21:44:15 +0400 | [diff] [blame] | 193 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 194 | int parse_tag(struct tag *item) |
| 195 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 196 | enum object_type type; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 197 | void *data; |
| 198 | unsigned long size; |
| 199 | int ret; |
| 200 | |
| 201 | if (item->object.parsed) |
| 202 | return 0; |
brian m. carlson | b4f5aca | 2018-03-12 02:27:53 +0000 | [diff] [blame] | 203 | data = read_object_file(&item->object.oid, &type, &size); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 204 | if (!data) |
| 205 | return error("Could not read %s", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 206 | oid_to_hex(&item->object.oid)); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 207 | if (type != OBJ_TAG) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 208 | free(data); |
| 209 | return error("Object %s not a tag", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 210 | oid_to_hex(&item->object.oid)); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 211 | } |
Stefan Beller | 0e740fe | 2018-06-28 18:22:04 -0700 | [diff] [blame] | 212 | ret = parse_tag_buffer(the_repository, item, data, size); |
Sergey Vlasov | 13019d4 | 2005-05-04 21:44:15 +0400 | [diff] [blame] | 213 | free(data); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 214 | return ret; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 215 | } |