blob: 3448a5dde7cbd795b08db872180f03dcfb66b99a [file] [log] [blame]
Linus Torvaldsec4465a2005-04-25 12:07:44 -07001#include "cache.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02002#include "tag.h"
Linus Torvaldsec4465a2005-04-25 12:07:44 -07003
4/*
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +01005 * 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.
Linus Torvaldsec4465a2005-04-25 12:07:44 -070010 *
11 * The first three lines are guaranteed to be at least 63 bytes:
12 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
13 * shortest possible type-line, and "tag .\n" at 6 bytes is the
14 * shortest single-character-tag line.
15 *
16 * We also artificially limit the size of the full object to 8kB.
17 * Just because I'm a lazy bastard, and if you can't fit a signature
18 * in that size, you're doing something wrong.
19 */
20
Pavel Roskina9486b02006-07-10 02:57:51 -040021/* Some random size */
Linus Torvaldsec4465a2005-04-25 12:07:44 -070022#define MAXSIZE (8192)
23
24/*
25 * We refuse to tag something we can't verify. Just because.
26 */
27static int verify_object(unsigned char *sha1, const char *expected_type)
28{
29 int ret = -1;
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040030 char type[100];
31 unsigned long size;
32 void *buffer = read_sha1_file(sha1, type, &size);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070033
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040034 if (buffer) {
35 if (!strcmp(type, expected_type))
36 ret = check_sha1_signature(sha1, buffer, size, type);
37 free(buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070038 }
39 return ret;
40}
41
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010042#ifdef NO_C99_FORMAT
43#define PD_FMT "%d"
44#else
45#define PD_FMT "%td"
46#endif
47
Linus Torvaldsec4465a2005-04-25 12:07:44 -070048static int verify_tag(char *buffer, unsigned long size)
49{
50 int typelen;
51 char type[20];
52 unsigned char sha1[20];
Eric W. Biedermanc8185662005-07-14 19:02:10 -060053 const char *object, *type_line, *tag_line, *tagger_line;
Linus Torvaldsec4465a2005-04-25 12:07:44 -070054
Björn Engelmanne7332f92006-05-23 20:19:04 +020055 if (size < 64)
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010056 return error("wanna fool me ? you obviously got the size wrong !");
Björn Engelmanncfba0452006-05-23 20:20:09 +020057
Linus Torvaldsec4465a2005-04-25 12:07:44 -070058 buffer[size] = 0;
59
60 /* Verify object line */
61 object = buffer;
62 if (memcmp(object, "object ", 7))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010063 return error("char%d: does not start with \"object \"", 0);
Björn Engelmanncfba0452006-05-23 20:20:09 +020064
Linus Torvaldsec4465a2005-04-25 12:07:44 -070065 if (get_sha1_hex(object + 7, sha1))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010066 return error("char%d: could not get SHA1 hash", 7);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070067
68 /* Verify type line */
69 type_line = object + 48;
70 if (memcmp(type_line - 1, "\ntype ", 6))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010071 return error("char%d: could not find \"\\ntype \"", 47);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070072
73 /* Verify tag-line */
74 tag_line = strchr(type_line, '\n');
75 if (!tag_line)
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010076 return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070077 tag_line++;
78 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010079 return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070080
81 /* Get the actual type */
82 typelen = tag_line - type_line - strlen("type \n");
83 if (typelen >= sizeof(type))
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010084 return error("char" PD_FMT ": type too long", type_line+5 - buffer);
Björn Engelmanncfba0452006-05-23 20:20:09 +020085
Linus Torvaldsec4465a2005-04-25 12:07:44 -070086 memcpy(type, type_line+5, typelen);
87 type[typelen] = 0;
88
89 /* Verify that the object matches */
Linus Torvaldsec4465a2005-04-25 12:07:44 -070090 if (verify_object(sha1, type))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010091 return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
Linus Torvaldsec4465a2005-04-25 12:07:44 -070092
93 /* Verify the tag-name: we don't allow control characters or spaces in it */
94 tag_line += 4;
95 for (;;) {
96 unsigned char c = *tag_line++;
97 if (c == '\n')
98 break;
99 if (c > ' ')
100 continue;
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100101 return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700102 }
103
Eric W. Biedermanc8185662005-07-14 19:02:10 -0600104 /* Verify the tagger line */
105 tagger_line = tag_line;
106
107 if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n'))
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100108 return error("char" PD_FMT ": could not find \"tagger\"", tagger_line - buffer);
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +0100109
110 /* TODO: check for committer info + blank line? */
111 /* Also, the minimum length is probably + "tagger .", or 63+8=71 */
Eric W. Biedermanc8185662005-07-14 19:02:10 -0600112
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700113 /* The actual stuff afterwards we don't care about.. */
114 return 0;
115}
116
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100117#undef PD_FMT
118
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700119int main(int argc, char **argv)
120{
Björn Engelmanne7332f92006-05-23 20:19:04 +0200121 unsigned long size = 4096;
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200122 char *buffer = xmalloc(size);
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700123 unsigned char result_sha1[20];
124
125 if (argc != 1)
Ramsay Allan Jones15e593e2006-08-03 16:38:39 +0100126 usage("git-mktag < signaturefile");
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700127
Junio C Hamano53228a52005-11-26 00:50:02 -0800128 setup_git_directory();
129
Björn Engelmanne7332f92006-05-23 20:19:04 +0200130 if (read_pipe(0, &buffer, &size)) {
131 free(buffer);
132 die("could not read from stdin");
Linus Torvaldsb97e3df2005-05-29 12:06:32 -0700133 }
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700134
Pavel Roskina9486b02006-07-10 02:57:51 -0400135 /* Verify it for some basic sanity: it needs to start with
136 "object <sha1>\ntype\ntagger " */
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700137 if (verify_tag(buffer, size) < 0)
138 die("invalid tag signature file");
139
Peter Eriksen8e440252006-04-02 14:44:09 +0200140 if (write_sha1_file(buffer, size, tag_type, result_sha1) < 0)
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700141 die("unable to write tag file");
Björn Engelmanne7332f92006-05-23 20:19:04 +0200142
143 free(buffer);
144
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700145 printf("%s\n", sha1_to_hex(result_sha1));
146 return 0;
147}