blob: 7e10acfb6ef1673e783e0a42e1b8810a6e2eff06 [file] [log] [blame]
Daniel Barkalow2636f612005-04-28 07:46:33 -07001#include "cache.h"
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08002#include "tag.h"
Nicolas Pitre0ab17952007-02-26 14:56:00 -05003#include "commit.h"
4#include "tree.h"
5#include "blob.h"
Lukas Puehringer94240b92017-01-17 18:37:18 -05006#include "gpg-interface.h"
Daniel Barkalow2636f612005-04-28 07:46:33 -07007
8const char *tag_type = "tag";
9
Santiago Torres45a227e2016-04-22 10:52:04 -040010static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
11{
12 struct signature_check sigc;
13 size_t payload_size;
14 int ret;
15
16 memset(&sigc, 0, sizeof(sigc));
17
18 payload_size = parse_signature(buf, size);
19
20 if (size == payload_size) {
21 if (flags & GPG_VERIFY_VERBOSE)
22 write_in_full(1, buf, payload_size);
23 return error("no signature found");
24 }
25
26 ret = check_signature(buf, payload_size, buf + payload_size,
27 size - payload_size, &sigc);
Lukas Puehringer94240b92017-01-17 18:37:18 -050028
29 if (!(flags & GPG_VERIFY_OMIT_STATUS))
30 print_signature_buffer(&sigc, flags);
Santiago Torres45a227e2016-04-22 10:52:04 -040031
32 signature_check_clear(&sigc);
33 return ret;
34}
35
Stefan Beller84571762017-07-12 17:44:15 -070036int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
Santiago Torres45a227e2016-04-22 10:52:04 -040037 unsigned flags)
38{
39 enum object_type type;
40 char *buf;
41 unsigned long size;
42 int ret;
43
Stefan Beller84571762017-07-12 17:44:15 -070044 type = sha1_object_info(oid->hash, NULL);
Santiago Torres45a227e2016-04-22 10:52:04 -040045 if (type != OBJ_TAG)
46 return error("%s: cannot verify a non-tag object of type %s.",
47 name_to_report ?
48 name_to_report :
Stefan Beller84571762017-07-12 17:44:15 -070049 find_unique_abbrev(oid->hash, DEFAULT_ABBREV),
Santiago Torres45a227e2016-04-22 10:52:04 -040050 typename(type));
51
Stefan Beller84571762017-07-12 17:44:15 -070052 buf = read_sha1_file(oid->hash, &type, &size);
Santiago Torres45a227e2016-04-22 10:52:04 -040053 if (!buf)
54 return error("%s: unable to read file.",
55 name_to_report ?
56 name_to_report :
Stefan Beller84571762017-07-12 17:44:15 -070057 find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
Santiago Torres45a227e2016-04-22 10:52:04 -040058
59 ret = run_gpg_verify(buf, size, flags);
60
61 free(buf);
62 return ret;
63}
64
Junio C Hamano9534f402005-11-02 15:19:13 -080065struct object *deref_tag(struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 00:47:56 -070066{
Linus Torvalds19746322006-07-11 20:45:31 -070067 while (o && o->type == OBJ_TAG)
Martin Koegler24e8a3c2008-02-18 08:31:55 +010068 if (((struct tag *)o)->tagged)
brian m. carlsonc251c832017-05-06 22:10:38 +000069 o = parse_object(&((struct tag *)o)->tagged->oid);
Martin Koegler24e8a3c2008-02-18 08:31:55 +010070 else
71 o = NULL;
Junio C Hamano9534f402005-11-02 15:19:13 -080072 if (!o && warn) {
73 if (!warnlen)
74 warnlen = strlen(warn);
75 error("missing object referenced by '%.*s'", warnlen, warn);
76 }
Junio C Hamano37fde872005-08-05 00:47:56 -070077 return o;
78}
79
Jeff King90108a22012-01-06 14:18:01 -050080struct object *deref_tag_noverify(struct object *o)
81{
82 while (o && o->type == OBJ_TAG) {
brian m. carlsonc251c832017-05-06 22:10:38 +000083 o = parse_object(&o->oid);
Jeff King90108a22012-01-06 14:18:01 -050084 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
85 o = ((struct tag *)o)->tagged;
86 else
87 o = NULL;
88 }
89 return o;
90}
91
brian m. carlsond3101b52017-05-06 22:10:19 +000092struct tag *lookup_tag(const struct object_id *oid)
Daniel Barkalow2636f612005-04-28 07:46:33 -070093{
brian m. carlsond3101b52017-05-06 22:10:19 +000094 struct object *obj = lookup_object(oid->hash);
Linus Torvalds100c5f32007-04-16 22:11:43 -070095 if (!obj)
brian m. carlsond3101b52017-05-06 22:10:19 +000096 return create_object(oid->hash, alloc_tag_node());
Jeff King8ff226a2014-07-13 02:42:03 -040097 return object_as_type(obj, OBJ_TAG, 0);
Daniel Barkalow2636f612005-04-28 07:46:33 -070098}
99
Johannes Schindelindddbad72017-04-26 21:29:31 +0200100static timestamp_t parse_tag_date(const char *buf, const char *tail)
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700101{
102 const char *dateptr;
103
104 while (buf < tail && *buf++ != '>')
105 /* nada */;
106 if (buf >= tail)
107 return 0;
108 dateptr = buf;
109 while (buf < tail && *buf++ != '\n')
110 /* nada */;
111 if (buf >= tail)
112 return 0;
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200113 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
114 return parse_timestamp(dateptr, NULL, 10);
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700115}
116
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700117int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -0700118{
brian m. carlson1e4085a2017-05-06 22:10:02 +0000119 struct object_id oid;
Daniel Barkalow89e42022005-06-21 20:35:10 -0400120 char type[20];
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700121 const char *bufptr = data;
122 const char *tail = bufptr + size;
123 const char *nl;
Edgar Toernigae200ee2005-04-30 09:51:03 -0700124
Shawn O. Pearce2e0052a2010-04-12 16:25:25 -0700125 if (item->object.parsed)
126 return 0;
127 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700128
brian m. carlson1e4085a2017-05-06 22:10:02 +0000129 if (size < GIT_SHA1_HEXSZ + 24)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400130 return -1;
brian m. carlson1e4085a2017-05-06 22:10:02 +0000131 if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400132 return -1;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700133
Christian Couder59556542013-11-30 21:55:40 +0100134 if (!starts_with(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400135 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700136 bufptr += 5;
137 nl = memchr(bufptr, '\n', tail - bufptr);
138 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400139 return -1;
Jeff Kingeddda372015-09-24 17:08:26 -0400140 memcpy(type, bufptr, nl - bufptr);
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700141 type[nl - bufptr] = '\0';
142 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700143
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500144 if (!strcmp(type, blob_type)) {
brian m. carlson3aca1fc2017-05-06 22:10:14 +0000145 item->tagged = &lookup_blob(&oid)->object;
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500146 } else if (!strcmp(type, tree_type)) {
brian m. carlson740ee052017-05-06 22:10:17 +0000147 item->tagged = &lookup_tree(&oid)->object;
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500148 } else if (!strcmp(type, commit_type)) {
brian m. carlsonbc832662017-05-06 22:10:10 +0000149 item->tagged = &lookup_commit(&oid)->object;
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500150 } else if (!strcmp(type, tag_type)) {
brian m. carlsond3101b52017-05-06 22:10:19 +0000151 item->tagged = &lookup_tag(&oid)->object;
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500152 } else {
153 error("Unknown type %s", type);
154 item->tagged = NULL;
155 }
156
Christian Couder59556542013-11-30 21:55:40 +0100157 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
Nguyễn Thái Ngọc Duy85594252011-02-14 20:02:51 +0700158 ; /* good */
159 else
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700160 return -1;
161 bufptr += 4;
162 nl = memchr(bufptr, '\n', tail - bufptr);
163 if (!nl)
164 return -1;
165 item->tag = xmemdupz(bufptr, nl - bufptr);
166 bufptr = nl + 1;
167
Christian Couder59556542013-11-30 21:55:40 +0100168 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700169 item->date = parse_tag_date(bufptr, tail);
170 else
171 item->date = 0;
172
Daniel Barkalow2636f612005-04-28 07:46:33 -0700173 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400174}
Sergey Vlasov13019d42005-05-04 21:44:15 +0400175
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400176int parse_tag(struct tag *item)
177{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500178 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400179 void *data;
180 unsigned long size;
181 int ret;
182
183 if (item->object.parsed)
184 return 0;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000185 data = read_sha1_file(item->object.oid.hash, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400186 if (!data)
187 return error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000188 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500189 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400190 free(data);
191 return error("Object %s not a tag",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000192 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400193 }
194 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400195 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400196 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700197}