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" |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 3 | #include "commit.h" |
| 4 | #include "tree.h" |
| 5 | #include "blob.h" |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 6 | |
| 7 | const char *tag_type = "tag"; |
| 8 | |
Santiago Torres | 45a227e | 2016-04-22 10:52:04 -0400 | [diff] [blame] | 9 | static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags) |
| 10 | { |
| 11 | struct signature_check sigc; |
| 12 | size_t payload_size; |
| 13 | int ret; |
| 14 | |
| 15 | memset(&sigc, 0, sizeof(sigc)); |
| 16 | |
| 17 | payload_size = parse_signature(buf, size); |
| 18 | |
| 19 | if (size == payload_size) { |
| 20 | if (flags & GPG_VERIFY_VERBOSE) |
| 21 | write_in_full(1, buf, payload_size); |
| 22 | return error("no signature found"); |
| 23 | } |
| 24 | |
| 25 | ret = check_signature(buf, payload_size, buf + payload_size, |
| 26 | size - payload_size, &sigc); |
| 27 | print_signature_buffer(&sigc, flags); |
| 28 | |
| 29 | signature_check_clear(&sigc); |
| 30 | return ret; |
| 31 | } |
| 32 | |
| 33 | int gpg_verify_tag(const unsigned char *sha1, const char *name_to_report, |
| 34 | unsigned flags) |
| 35 | { |
| 36 | enum object_type type; |
| 37 | char *buf; |
| 38 | unsigned long size; |
| 39 | int ret; |
| 40 | |
| 41 | type = sha1_object_info(sha1, NULL); |
| 42 | if (type != OBJ_TAG) |
| 43 | return error("%s: cannot verify a non-tag object of type %s.", |
| 44 | name_to_report ? |
| 45 | name_to_report : |
| 46 | find_unique_abbrev(sha1, DEFAULT_ABBREV), |
| 47 | typename(type)); |
| 48 | |
| 49 | buf = read_sha1_file(sha1, &type, &size); |
| 50 | if (!buf) |
| 51 | return error("%s: unable to read file.", |
| 52 | name_to_report ? |
| 53 | name_to_report : |
| 54 | find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
| 55 | |
| 56 | ret = run_gpg_verify(buf, size, flags); |
| 57 | |
| 58 | free(buf); |
| 59 | return ret; |
| 60 | } |
| 61 | |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 62 | struct object *deref_tag(struct object *o, const char *warn, int warnlen) |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 63 | { |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 64 | while (o && o->type == OBJ_TAG) |
Martin Koegler | 24e8a3c | 2008-02-18 08:31:55 +0100 | [diff] [blame] | 65 | if (((struct tag *)o)->tagged) |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 66 | o = parse_object(((struct tag *)o)->tagged->oid.hash); |
Martin Koegler | 24e8a3c | 2008-02-18 08:31:55 +0100 | [diff] [blame] | 67 | else |
| 68 | o = NULL; |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 69 | if (!o && warn) { |
| 70 | if (!warnlen) |
| 71 | warnlen = strlen(warn); |
| 72 | error("missing object referenced by '%.*s'", warnlen, warn); |
| 73 | } |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 74 | return o; |
| 75 | } |
| 76 | |
Jeff King | 90108a2 | 2012-01-06 14:18:01 -0500 | [diff] [blame] | 77 | struct object *deref_tag_noverify(struct object *o) |
| 78 | { |
| 79 | while (o && o->type == OBJ_TAG) { |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 80 | o = parse_object(o->oid.hash); |
Jeff King | 90108a2 | 2012-01-06 14:18:01 -0500 | [diff] [blame] | 81 | if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged) |
| 82 | o = ((struct tag *)o)->tagged; |
| 83 | else |
| 84 | o = NULL; |
| 85 | } |
| 86 | return o; |
| 87 | } |
| 88 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 89 | struct tag *lookup_tag(const unsigned char *sha1) |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 90 | { |
Linus Torvalds | 100c5f3 | 2007-04-16 22:11:43 -0700 | [diff] [blame] | 91 | struct object *obj = lookup_object(sha1); |
| 92 | if (!obj) |
Jeff King | d36f51c | 2014-07-13 02:41:55 -0400 | [diff] [blame] | 93 | return create_object(sha1, alloc_tag_node()); |
Jeff King | 8ff226a | 2014-07-13 02:42:03 -0400 | [diff] [blame] | 94 | return object_as_type(obj, OBJ_TAG, 0); |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Shawn O. Pearce | e451d06 | 2010-04-12 16:25:28 -0700 | [diff] [blame] | 97 | static unsigned long parse_tag_date(const char *buf, const char *tail) |
| 98 | { |
| 99 | const char *dateptr; |
| 100 | |
| 101 | while (buf < tail && *buf++ != '>') |
| 102 | /* nada */; |
| 103 | if (buf >= tail) |
| 104 | return 0; |
| 105 | dateptr = buf; |
| 106 | while (buf < tail && *buf++ != '\n') |
| 107 | /* nada */; |
| 108 | if (buf >= tail) |
| 109 | return 0; |
| 110 | /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */ |
| 111 | return strtoul(dateptr, NULL, 10); |
| 112 | } |
| 113 | |
Nguyễn Thái Ngọc Duy | cf7b1ca | 2011-02-05 17:52:20 +0700 | [diff] [blame] | 114 | int parse_tag_buffer(struct tag *item, const void *data, unsigned long size) |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 115 | { |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 116 | unsigned char sha1[20]; |
Daniel Barkalow | 89e4202 | 2005-06-21 20:35:10 -0400 | [diff] [blame] | 117 | char type[20]; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 118 | const char *bufptr = data; |
| 119 | const char *tail = bufptr + size; |
| 120 | const char *nl; |
Edgar Toernig | ae200ee | 2005-04-30 09:51:03 -0700 | [diff] [blame] | 121 | |
Shawn O. Pearce | 2e0052a | 2010-04-12 16:25:25 -0700 | [diff] [blame] | 122 | if (item->object.parsed) |
| 123 | return 0; |
| 124 | item->object.parsed = 1; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 125 | |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 126 | if (size < 64) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 127 | return -1; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 128 | if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n') |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 129 | return -1; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 130 | bufptr += 48; /* "object " + sha1 + "\n" */ |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 131 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 132 | if (!starts_with(bufptr, "type ")) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 133 | return -1; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 134 | bufptr += 5; |
| 135 | nl = memchr(bufptr, '\n', tail - bufptr); |
| 136 | if (!nl || sizeof(type) <= (nl - bufptr)) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 137 | return -1; |
Jeff King | eddda37 | 2015-09-24 17:08:26 -0400 | [diff] [blame] | 138 | memcpy(type, bufptr, nl - bufptr); |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 139 | type[nl - bufptr] = '\0'; |
| 140 | bufptr = nl + 1; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 141 | |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 142 | if (!strcmp(type, blob_type)) { |
| 143 | item->tagged = &lookup_blob(sha1)->object; |
| 144 | } else if (!strcmp(type, tree_type)) { |
| 145 | item->tagged = &lookup_tree(sha1)->object; |
| 146 | } else if (!strcmp(type, commit_type)) { |
| 147 | item->tagged = &lookup_commit(sha1)->object; |
| 148 | } else if (!strcmp(type, tag_type)) { |
| 149 | item->tagged = &lookup_tag(sha1)->object; |
| 150 | } else { |
| 151 | error("Unknown type %s", type); |
| 152 | item->tagged = NULL; |
| 153 | } |
| 154 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 155 | 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] | 156 | ; /* good */ |
| 157 | else |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 158 | return -1; |
| 159 | bufptr += 4; |
| 160 | nl = memchr(bufptr, '\n', tail - bufptr); |
| 161 | if (!nl) |
| 162 | return -1; |
| 163 | item->tag = xmemdupz(bufptr, nl - bufptr); |
| 164 | bufptr = nl + 1; |
| 165 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 166 | if (bufptr + 7 < tail && starts_with(bufptr, "tagger ")) |
Shawn O. Pearce | e451d06 | 2010-04-12 16:25:28 -0700 | [diff] [blame] | 167 | item->date = parse_tag_date(bufptr, tail); |
| 168 | else |
| 169 | item->date = 0; |
| 170 | |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 171 | return 0; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 172 | } |
Sergey Vlasov | 13019d4 | 2005-05-04 21:44:15 +0400 | [diff] [blame] | 173 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 174 | int parse_tag(struct tag *item) |
| 175 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 176 | enum object_type type; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 177 | void *data; |
| 178 | unsigned long size; |
| 179 | int ret; |
| 180 | |
| 181 | if (item->object.parsed) |
| 182 | return 0; |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 183 | data = read_sha1_file(item->object.oid.hash, &type, &size); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 184 | if (!data) |
| 185 | return error("Could not read %s", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 186 | oid_to_hex(&item->object.oid)); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 187 | if (type != OBJ_TAG) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 188 | free(data); |
| 189 | return error("Object %s not a tag", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 190 | oid_to_hex(&item->object.oid)); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 191 | } |
| 192 | ret = parse_tag_buffer(item, data, size); |
Sergey Vlasov | 13019d4 | 2005-05-04 21:44:15 +0400 | [diff] [blame] | 193 | free(data); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 194 | return ret; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 195 | } |