mktag.c: improve verification of tagger field and tests
[git/git.git] / mktag.c
CommitLineData
ec4465ad 1#include "cache.h"
8e440259 2#include "tag.h"
ec4465ad
LT
3
4/*
446c6fae
RJ
5 * A signature file has a very simple fixed format: four lines
6 * of "object <sha1>" + "type <typename>" + "tag <tagname>" +
7 * "tagger <committer>", followed by a blank line, a free-form tag
8 * message and a signature block that git itself doesn't care about,
9 * but that can be verified with gpg or similar.
ec4465ad 10 *
e0aaf781 11 * The first four lines are guaranteed to be at least 83 bytes:
ec4465ad 12 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
e0aaf781
BC
13 * shortest possible type-line, "tag .\n" at 6 bytes is the shortest
14 * single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is
15 * the shortest possible tagger-line.
ec4465ad
LT
16 */
17
ec4465ad
LT
18/*
19 * We refuse to tag something we can't verify. Just because.
20 */
21static int verify_object(unsigned char *sha1, const char *expected_type)
22{
23 int ret = -1;
21666f1a 24 enum object_type type;
91d7b8af 25 unsigned long size;
21666f1a 26 void *buffer = read_sha1_file(sha1, &type, &size);
ec4465ad 27
91d7b8af 28 if (buffer) {
21666f1a
NP
29 if (type == type_from_string(expected_type))
30 ret = check_sha1_signature(sha1, buffer, size, expected_type);
91d7b8af 31 free(buffer);
ec4465ad
LT
32 }
33 return ret;
34}
35
579d1fbf
RJ
36#ifdef NO_C99_FORMAT
37#define PD_FMT "%d"
38#else
39#define PD_FMT "%td"
40#endif
41
ec4465ad
LT
42static int verify_tag(char *buffer, unsigned long size)
43{
44 int typelen;
45 char type[20];
46 unsigned char sha1[20];
e0aaf781 47 const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb;
ec4465ad 48
e0aaf781 49 if (size < 84)
446c6fae 50 return error("wanna fool me ? you obviously got the size wrong !");
cfba0459 51
ec4465ad
LT
52 buffer[size] = 0;
53
54 /* Verify object line */
55 object = buffer;
56 if (memcmp(object, "object ", 7))
446c6fae 57 return error("char%d: does not start with \"object \"", 0);
cfba0459 58
ec4465ad 59 if (get_sha1_hex(object + 7, sha1))
446c6fae 60 return error("char%d: could not get SHA1 hash", 7);
ec4465ad
LT
61
62 /* Verify type line */
63 type_line = object + 48;
64 if (memcmp(type_line - 1, "\ntype ", 6))
446c6fae 65 return error("char%d: could not find \"\\ntype \"", 47);
ec4465ad
LT
66
67 /* Verify tag-line */
68 tag_line = strchr(type_line, '\n');
69 if (!tag_line)
579d1fbf 70 return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
ec4465ad
LT
71 tag_line++;
72 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
579d1fbf 73 return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
ec4465ad
LT
74
75 /* Get the actual type */
76 typelen = tag_line - type_line - strlen("type \n");
77 if (typelen >= sizeof(type))
579d1fbf 78 return error("char" PD_FMT ": type too long", type_line+5 - buffer);
cfba0459 79
ec4465ad
LT
80 memcpy(type, type_line+5, typelen);
81 type[typelen] = 0;
82
83 /* Verify that the object matches */
ec4465ad 84 if (verify_object(sha1, type))
446c6fae 85 return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
ec4465ad
LT
86
87 /* Verify the tag-name: we don't allow control characters or spaces in it */
88 tag_line += 4;
89 for (;;) {
90 unsigned char c = *tag_line++;
91 if (c == '\n')
92 break;
93 if (c > ' ')
94 continue;
579d1fbf 95 return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
ec4465ad
LT
96 }
97
c818566d
EB
98 /* Verify the tagger line */
99 tagger_line = tag_line;
100
e0aaf781
BC
101 if (memcmp(tagger_line, "tagger ", 7) || (tagger_line[7] == '\n'))
102 return error("char" PD_FMT ": could not find \"tagger \"",
103 tagger_line - buffer);
104
105 /*
106 * Check for correct form for name and email
107 * i.e. " <" followed by "> " on _this_ line
108 */
109 tagger_line += 7;
110 if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
111 strchr(tagger_line, '\n') < rb)
112 return error("char" PD_FMT ": malformed tagger",
113 tagger_line - buffer);
114
115 /* Check for author name, at least one character, space is acceptable */
116 if (lb == tagger_line)
117 return error("char" PD_FMT ": missing tagger name",
118 tagger_line - buffer);
119
120 /* timestamp */
121 tagger_line = rb + 2;
122 if (*tagger_line == ' ')
123 return error("char" PD_FMT ": malformed tag timestamp",
124 tagger_line - buffer);
125 for (;;) {
126 unsigned char c = *tagger_line++;
127 if (c == ' ')
128 break;
129 if (isdigit(c))
130 continue;
131 return error("char" PD_FMT ": malformed tag timestamp",
132 tagger_line - buffer);
133 }
446c6fae 134
e0aaf781
BC
135 /* timezone, 5 digits [+-]hhmm, max. 1400 */
136 if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
137 isdigit(tagger_line[1]) && isdigit(tagger_line[2]) &&
138 isdigit(tagger_line[3]) && isdigit(tagger_line[4]) &&
139 tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
140 return error("char" PD_FMT ": malformed tag timezone",
141 tagger_line - buffer);
142 tagger_line += 6;
143
144 /* Verify the blank line separating the header from the body */
145 if (*tagger_line != '\n')
146 return error("char" PD_FMT ": trailing garbage in tag header",
147 tagger_line - buffer);
c818566d 148
ec4465ad
LT
149 /* The actual stuff afterwards we don't care about.. */
150 return 0;
151}
152
579d1fbf
RJ
153#undef PD_FMT
154
ec4465ad
LT
155int main(int argc, char **argv)
156{
fd17f5b5 157 struct strbuf buf;
ec4465ad
LT
158 unsigned char result_sha1[20];
159
160 if (argc != 1)
15e593e4 161 usage("git-mktag < signaturefile");
ec4465ad 162
53228a5f
JH
163 setup_git_directory();
164
fd17f5b5
PH
165 strbuf_init(&buf, 0);
166 if (strbuf_read(&buf, 0, 4096) < 0) {
e7332f96 167 die("could not read from stdin");
b97e3dfa 168 }
ec4465ad 169
a9486b02
PR
170 /* Verify it for some basic sanity: it needs to start with
171 "object <sha1>\ntype\ntagger " */
fd17f5b5 172 if (verify_tag(buf.buf, buf.len) < 0)
ec4465ad
LT
173 die("invalid tag signature file");
174
fd17f5b5 175 if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
ec4465ad 176 die("unable to write tag file");
e7332f96 177
fd17f5b5 178 strbuf_release(&buf);
ec4465ad
LT
179 printf("%s\n", sha1_to_hex(result_sha1));
180 return 0;
181}