blob: 96dbd5b2d5501e31b7a7bb75d7630347bfdc24c0 [file] [log] [blame]
Elijah Newrend812c3b2023-04-11 00:41:56 -07001#include "git-compat-util.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00002#include "environment.h"
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08003#include "tag.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -07004#include "object-name.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07005#include "object-store.h"
Nicolas Pitre0ab17952007-02-26 14:56:00 -05006#include "commit.h"
7#include "tree.h"
8#include "blob.h"
Stefan Beller14ba97f2018-05-15 14:48:42 -07009#include "alloc.h"
Lukas Puehringer94240b92017-01-17 18:37:18 -050010#include "gpg-interface.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000011#include "hex.h"
Jonathan Tan8c4cc322018-07-12 17:03:07 -070012#include "packfile.h"
Elijah Newrend5ebb502023-03-21 06:26:01 +000013#include "wrapper.h"
Daniel Barkalow2636f612005-04-28 07:46:33 -070014
15const char *tag_type = "tag";
16
Santiago Torres45a227e2016-04-22 10:52:04 -040017static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
18{
19 struct signature_check sigc;
brian m. carlson482c1192021-02-11 02:08:03 +000020 struct strbuf payload = STRBUF_INIT;
21 struct strbuf signature = STRBUF_INIT;
Santiago Torres45a227e2016-04-22 10:52:04 -040022 int ret;
23
24 memset(&sigc, 0, sizeof(sigc));
25
brian m. carlson482c1192021-02-11 02:08:03 +000026 if (!parse_signature(buf, size, &payload, &signature)) {
Santiago Torres45a227e2016-04-22 10:52:04 -040027 if (flags & GPG_VERIFY_VERBOSE)
brian m. carlson482c1192021-02-11 02:08:03 +000028 write_in_full(1, buf, size);
Santiago Torres45a227e2016-04-22 10:52:04 -040029 return error("no signature found");
30 }
31
Fabian Stelzerdd3aa412021-12-09 09:52:47 +010032 sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
Fabian Stelzer02769432021-12-09 09:52:43 +010033 sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
34 ret = check_signature(&sigc, signature.buf, signature.len);
Lukas Puehringer94240b92017-01-17 18:37:18 -050035
36 if (!(flags & GPG_VERIFY_OMIT_STATUS))
37 print_signature_buffer(&sigc, flags);
Santiago Torres45a227e2016-04-22 10:52:04 -040038
39 signature_check_clear(&sigc);
brian m. carlson482c1192021-02-11 02:08:03 +000040 strbuf_release(&payload);
41 strbuf_release(&signature);
Santiago Torres45a227e2016-04-22 10:52:04 -040042 return ret;
43}
44
Stefan Beller84571762017-07-12 17:44:15 -070045int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
Santiago Torres45a227e2016-04-22 10:52:04 -040046 unsigned flags)
47{
48 enum object_type type;
49 char *buf;
50 unsigned long size;
51 int ret;
52
Stefan Beller0df8e962018-04-25 11:20:59 -070053 type = oid_object_info(the_repository, oid, NULL);
Santiago Torres45a227e2016-04-22 10:52:04 -040054 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ð Bjarmasond850b7a2023-03-28 15:58:46 +020058 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV),
Brandon Williamsdebca9d2018-02-14 10:59:24 -080059 type_name(type));
Santiago Torres45a227e2016-04-22 10:52:04 -040060
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +020061 buf = repo_read_object_file(the_repository, oid, &type, &size);
Santiago Torres45a227e2016-04-22 10:52:04 -040062 if (!buf)
63 return error("%s: unable to read file.",
64 name_to_report ?
65 name_to_report :
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +020066 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV));
Santiago Torres45a227e2016-04-22 10:52:04 -040067
68 ret = run_gpg_verify(buf, size, flags);
69
70 free(buf);
71 return ret;
72}
73
Stefan Beller286d2582018-06-28 18:22:20 -070074struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 00:47:56 -070075{
Jonathan Tan8c4cc322018-07-12 17:03:07 -070076 struct object_id *last_oid = NULL;
Linus Torvalds19746322006-07-11 20:45:31 -070077 while (o && o->type == OBJ_TAG)
Jonathan Tan8c4cc322018-07-12 17:03:07 -070078 if (((struct tag *)o)->tagged) {
79 last_oid = &((struct tag *)o)->tagged->oid;
Junio C Hamano09ca6132018-08-02 15:30:46 -070080 o = parse_object(r, last_oid);
Jonathan Tan8c4cc322018-07-12 17:03:07 -070081 } else {
82 last_oid = NULL;
Martin Koegler24e8a3c2008-02-18 08:31:55 +010083 o = NULL;
Jonathan Tan8c4cc322018-07-12 17:03:07 -070084 }
Junio C Hamano9534f402005-11-02 15:19:13 -080085 if (!o && warn) {
Jonathan Tan8c4cc322018-07-12 17:03:07 -070086 if (last_oid && is_promisor_object(last_oid))
87 return NULL;
Junio C Hamano9534f402005-11-02 15:19:13 -080088 if (!warnlen)
89 warnlen = strlen(warn);
90 error("missing object referenced by '%.*s'", warnlen, warn);
91 }
Junio C Hamano37fde872005-08-05 00:47:56 -070092 return o;
93}
94
Jeff King90108a22012-01-06 14:18:01 -050095struct object *deref_tag_noverify(struct object *o)
96{
97 while (o && o->type == OBJ_TAG) {
Stefan Beller109cd762018-06-28 18:21:51 -070098 o = parse_object(the_repository, &o->oid);
Jeff King90108a22012-01-06 14:18:01 -050099 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 Beller8bde69b2018-06-28 18:22:11 -0700107struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
Daniel Barkalow2636f612005-04-28 07:46:33 -0700108{
Jeff Kingd0229ab2019-06-20 03:41:14 -0400109 struct object *obj = lookup_object(r, oid);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700110 if (!obj)
Jeff Kinga3785092019-06-20 03:41:21 -0400111 return create_object(r, oid, alloc_tag_node(r));
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530112 return object_as_type(obj, OBJ_TAG, 0);
Daniel Barkalow2636f612005-04-28 07:46:33 -0700113}
114
Johannes Schindelindddbad72017-04-26 21:29:31 +0200115static timestamp_t parse_tag_date(const char *buf, const char *tail)
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700116{
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 Schindelin1aeb7e72017-04-21 12:45:44 +0200128 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
129 return parse_timestamp(dateptr, NULL, 10);
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700130}
131
Stefan Beller14ba97f2018-05-15 14:48:42 -0700132void 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 Beller84f80cd2018-06-28 18:22:12 -0700140int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -0700141{
brian m. carlson1e4085a2017-05-06 22:10:02 +0000142 struct object_id oid;
Daniel Barkalow89e42022005-06-21 20:35:10 -0400143 char type[20];
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700144 const char *bufptr = data;
145 const char *tail = bufptr + size;
146 const char *nl;
Edgar Toernigae200ee2005-04-30 09:51:03 -0700147
Shawn O. Pearce2e0052a2010-04-12 16:25:25 -0700148 if (item->object.parsed)
149 return 0;
Jeff King228c78f2019-10-25 17:20:20 -0400150
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 Barkalow2636f612005-04-28 07:46:33 -0700160
brian m. carlsond8a3a692018-10-15 00:01:58 +0000161 if (size < the_hash_algo->hexsz + 24)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400162 return -1;
brian m. carlson1e4085a2017-05-06 22:10:02 +0000163 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400164 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700165
Christian Couder59556542013-11-30 21:55:40 +0100166 if (!starts_with(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400167 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700168 bufptr += 5;
169 nl = memchr(bufptr, '\n', tail - bufptr);
170 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400171 return -1;
Jeff Kingeddda372015-09-24 17:08:26 -0400172 memcpy(type, bufptr, nl - bufptr);
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700173 type[nl - bufptr] = '\0';
174 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700175
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500176 if (!strcmp(type, blob_type)) {
Stefan Beller84f80cd2018-06-28 18:22:12 -0700177 item->tagged = (struct object *)lookup_blob(r, &oid);
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500178 } else if (!strcmp(type, tree_type)) {
Stefan Beller84f80cd2018-06-28 18:22:12 -0700179 item->tagged = (struct object *)lookup_tree(r, &oid);
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500180 } else if (!strcmp(type, commit_type)) {
Stefan Beller84f80cd2018-06-28 18:22:12 -0700181 item->tagged = (struct object *)lookup_commit(r, &oid);
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500182 } else if (!strcmp(type, tag_type)) {
Stefan Beller84f80cd2018-06-28 18:22:12 -0700183 item->tagged = (struct object *)lookup_tag(r, &oid);
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500184 } else {
Jeff King78d50142019-10-18 00:45:35 -0400185 return error("unknown tag type '%s' in %s",
186 type, oid_to_hex(&item->object.oid));
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500187 }
188
Jeff King78d50142019-10-18 00:45:35 -0400189 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 Couder59556542013-11-30 21:55:40 +0100194 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
Nguyễn Thái Ngọc Duy85594252011-02-14 20:02:51 +0700195 ; /* good */
196 else
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700197 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 Couder59556542013-11-30 21:55:40 +0100205 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700206 item->date = parse_tag_date(bufptr, tail);
207 else
208 item->date = 0;
209
Jeff King228c78f2019-10-25 17:20:20 -0400210 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700211 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400212}
Sergey Vlasov13019d42005-05-04 21:44:15 +0400213
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400214int parse_tag(struct tag *item)
215{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500216 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400217 void *data;
218 unsigned long size;
219 int ret;
220
221 if (item->object.parsed)
222 return 0;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200223 data = repo_read_object_file(the_repository, &item->object.oid, &type,
224 &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400225 if (!data)
226 return error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000227 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500228 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400229 free(data);
230 return error("Object %s not a tag",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000231 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400232 }
Stefan Beller0e740fe2018-06-28 18:22:04 -0700233 ret = parse_tag_buffer(the_repository, item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400234 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400235 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700236}
René Scharfedad3f062019-09-05 21:55:55 +0200237
238struct object_id *get_tagged_oid(struct tag *tag)
239{
240 if (!tag->tagged)
241 die("bad tag");
242 return &tag->tagged->oid;
243}