blob: 243d1fdbbcb1424b0082e1c999920f6a50b32d68 [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
36int gpg_verify_tag(const unsigned char *sha1, const char *name_to_report,
37 unsigned flags)
38{
39 enum object_type type;
40 char *buf;
41 unsigned long size;
42 int ret;
43
44 type = sha1_object_info(sha1, NULL);
45 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 :
49 find_unique_abbrev(sha1, DEFAULT_ABBREV),
50 typename(type));
51
52 buf = read_sha1_file(sha1, &type, &size);
53 if (!buf)
54 return error("%s: unable to read file.",
55 name_to_report ?
56 name_to_report :
57 find_unique_abbrev(sha1, DEFAULT_ABBREV));
58
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. carlsoned1c9972015-11-10 02:22:29 +000069 o = parse_object(((struct tag *)o)->tagged->oid.hash);
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. carlsoned1c9972015-11-10 02:22:29 +000083 o = parse_object(o->oid.hash);
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
Jason McMullan5d6ccf52005-06-03 11:05:39 -040092struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 07:46:33 -070093{
Linus Torvalds100c5f32007-04-16 22:11:43 -070094 struct object *obj = lookup_object(sha1);
95 if (!obj)
Jeff Kingd36f51c2014-07-13 02:41:55 -040096 return create_object(sha1, 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
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700100static unsigned long parse_tag_date(const char *buf, const char *tail)
101{
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;
113 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
114 return strtoul(dateptr, NULL, 10);
115}
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{
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500119 unsigned char sha1[20];
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
Daniel Barkalow2636f612005-04-28 07:46:33 -0700129 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400130 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700131 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400132 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700133 bufptr += 48; /* "object " + sha1 + "\n" */
Daniel Barkalow2636f612005-04-28 07:46:33 -0700134
Christian Couder59556542013-11-30 21:55:40 +0100135 if (!starts_with(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400136 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700137 bufptr += 5;
138 nl = memchr(bufptr, '\n', tail - bufptr);
139 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400140 return -1;
Jeff Kingeddda372015-09-24 17:08:26 -0400141 memcpy(type, bufptr, nl - bufptr);
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700142 type[nl - bufptr] = '\0';
143 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700144
Nicolas Pitre0ab17952007-02-26 14:56:00 -0500145 if (!strcmp(type, blob_type)) {
146 item->tagged = &lookup_blob(sha1)->object;
147 } else if (!strcmp(type, tree_type)) {
148 item->tagged = &lookup_tree(sha1)->object;
149 } else if (!strcmp(type, commit_type)) {
150 item->tagged = &lookup_commit(sha1)->object;
151 } else if (!strcmp(type, tag_type)) {
152 item->tagged = &lookup_tag(sha1)->object;
153 } else {
154 error("Unknown type %s", type);
155 item->tagged = NULL;
156 }
157
Christian Couder59556542013-11-30 21:55:40 +0100158 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
Nguyễn Thái Ngọc Duy85594252011-02-14 20:02:51 +0700159 ; /* good */
160 else
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700161 return -1;
162 bufptr += 4;
163 nl = memchr(bufptr, '\n', tail - bufptr);
164 if (!nl)
165 return -1;
166 item->tag = xmemdupz(bufptr, nl - bufptr);
167 bufptr = nl + 1;
168
Christian Couder59556542013-11-30 21:55:40 +0100169 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700170 item->date = parse_tag_date(bufptr, tail);
171 else
172 item->date = 0;
173
Daniel Barkalow2636f612005-04-28 07:46:33 -0700174 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400175}
Sergey Vlasov13019d42005-05-04 21:44:15 +0400176
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400177int parse_tag(struct tag *item)
178{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500179 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400180 void *data;
181 unsigned long size;
182 int ret;
183
184 if (item->object.parsed)
185 return 0;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000186 data = read_sha1_file(item->object.oid.hash, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400187 if (!data)
188 return error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000189 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500190 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400191 free(data);
192 return error("Object %s not a tag",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000193 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400194 }
195 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400196 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400197 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700198}