Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 181 lines (149 sloc) 5.212 kb
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
1 #include "cache.h"
8e44025 Use blob_, commit_, tag_, and tree_type throughout.
Peter Eriksen authored
2 #include "tag.h"
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
3
4 /*
446c6fa New tests and en-passant modifications to mktag.
Ramsay Allan Jones authored
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.
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
10 *
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
11 * The first four lines are guaranteed to be at least 83 bytes:
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
12 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
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.
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
16 */
17
18 /*
19 * We refuse to tag something we can't verify. Just because.
20 */
21 static int verify_object(unsigned char *sha1, const char *expected_type)
22 {
23 int ret = -1;
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
24 enum object_type type;
91d7b8a [PATCH] delta read
Nicolas Pitre authored
25 unsigned long size;
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
26 void *buffer = read_sha1_file(sha1, &type, &size);
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
27
91d7b8a [PATCH] delta read
Nicolas Pitre authored
28 if (buffer) {
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
29 if (type == type_from_string(expected_type))
30 ret = check_sha1_signature(sha1, buffer, size, expected_type);
91d7b8a [PATCH] delta read
Nicolas Pitre authored
31 free(buffer);
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
32 }
33 return ret;
34 }
35
579d1fb Add NO_C99_FORMAT to support older compilers.
Ramsay Allan Jones authored
36 #ifdef NO_C99_FORMAT
37 #define PD_FMT "%d"
38 #else
39 #define PD_FMT "%td"
40 #endif
41
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
42 static int verify_tag(char *buffer, unsigned long size)
43 {
44 int typelen;
45 char type[20];
46 unsigned char sha1[20];
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
47 const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb;
ba26ab9 mktag.c: tweak validation of tagger field and adjust test script
Brandon Casey authored
48 size_t len;
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
49
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
50 if (size < 84)
446c6fa New tests and en-passant modifications to mktag.
Ramsay Allan Jones authored
51 return error("wanna fool me ? you obviously got the size wrong !");
cfba045 @whatever42 add more informative error messages to git-mktag
whatever42 authored
52
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
53 buffer[size] = 0;
54
55 /* Verify object line */
56 object = buffer;
57 if (memcmp(object, "object ", 7))
446c6fa New tests and en-passant modifications to mktag.
Ramsay Allan Jones authored
58 return error("char%d: does not start with \"object \"", 0);
cfba045 @whatever42 add more informative error messages to git-mktag
whatever42 authored
59
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
60 if (get_sha1_hex(object + 7, sha1))
446c6fa New tests and en-passant modifications to mktag.
Ramsay Allan Jones authored
61 return error("char%d: could not get SHA1 hash", 7);
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
62
63 /* Verify type line */
64 type_line = object + 48;
65 if (memcmp(type_line - 1, "\ntype ", 6))
446c6fa New tests and en-passant modifications to mktag.
Ramsay Allan Jones authored
66 return error("char%d: could not find \"\\ntype \"", 47);
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
67
68 /* Verify tag-line */
69 tag_line = strchr(type_line, '\n');
70 if (!tag_line)
579d1fb Add NO_C99_FORMAT to support older compilers.
Ramsay Allan Jones authored
71 return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
72 tag_line++;
73 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
579d1fb Add NO_C99_FORMAT to support older compilers.
Ramsay Allan Jones authored
74 return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
75
76 /* Get the actual type */
77 typelen = tag_line - type_line - strlen("type \n");
78 if (typelen >= sizeof(type))
579d1fb Add NO_C99_FORMAT to support older compilers.
Ramsay Allan Jones authored
79 return error("char" PD_FMT ": type too long", type_line+5 - buffer);
cfba045 @whatever42 add more informative error messages to git-mktag
whatever42 authored
80
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
81 memcpy(type, type_line+5, typelen);
82 type[typelen] = 0;
83
84 /* Verify that the object matches */
85 if (verify_object(sha1, type))
446c6fa New tests and en-passant modifications to mktag.
Ramsay Allan Jones authored
86 return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
87
88 /* Verify the tag-name: we don't allow control characters or spaces in it */
89 tag_line += 4;
90 for (;;) {
91 unsigned char c = *tag_line++;
92 if (c == '\n')
93 break;
94 if (c > ' ')
95 continue;
579d1fb Add NO_C99_FORMAT to support older compilers.
Ramsay Allan Jones authored
96 return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
97 }
98
c818566 @ebiederm [PATCH] Update tags to record who made them
ebiederm authored
99 /* Verify the tagger line */
100 tagger_line = tag_line;
101
ba26ab9 mktag.c: tweak validation of tagger field and adjust test script
Brandon Casey authored
102 if (memcmp(tagger_line, "tagger ", 7))
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
103 return error("char" PD_FMT ": could not find \"tagger \"",
104 tagger_line - buffer);
105
106 /*
107 * Check for correct form for name and email
108 * i.e. " <" followed by "> " on _this_ line
ba26ab9 mktag.c: tweak validation of tagger field and adjust test script
Brandon Casey authored
109 * No angle brackets within the name or email address fields.
110 * No spaces within the email address field.
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
111 */
112 tagger_line += 7;
113 if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
ba26ab9 mktag.c: tweak validation of tagger field and adjust test script
Brandon Casey authored
114 strpbrk(tagger_line, "<>\n") != lb+1 ||
115 strpbrk(lb+2, "><\n ") != rb)
116 return error("char" PD_FMT ": malformed tagger field",
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
117 tagger_line - buffer);
118
119 /* Check for author name, at least one character, space is acceptable */
120 if (lb == tagger_line)
121 return error("char" PD_FMT ": missing tagger name",
122 tagger_line - buffer);
123
ba26ab9 mktag.c: tweak validation of tagger field and adjust test script
Brandon Casey authored
124 /* timestamp, 1 or more digits followed by space */
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
125 tagger_line = rb + 2;
ba26ab9 mktag.c: tweak validation of tagger field and adjust test script
Brandon Casey authored
126 if (!(len = strspn(tagger_line, "0123456789")))
127 return error("char" PD_FMT ": missing tag timestamp",
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
128 tagger_line - buffer);
ba26ab9 mktag.c: tweak validation of tagger field and adjust test script
Brandon Casey authored
129 tagger_line += len;
130 if (*tagger_line != ' ')
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
131 return error("char" PD_FMT ": malformed tag timestamp",
132 tagger_line - buffer);
ba26ab9 mktag.c: tweak validation of tagger field and adjust test script
Brandon Casey authored
133 tagger_line++;
446c6fa New tests and en-passant modifications to mktag.
Ramsay Allan Jones authored
134
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
135 /* timezone, 5 digits [+-]hhmm, max. 1400 */
136 if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
ba26ab9 mktag.c: tweak validation of tagger field and adjust test script
Brandon Casey authored
137 strspn(tagger_line+1, "0123456789") == 4 &&
e0aaf78 mktag.c: improve verification of tagger field and tests
Brandon Casey authored
138 tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
139 return error("char" PD_FMT ": malformed tag timezone",
140 tagger_line - buffer);
141 tagger_line += 6;
142
143 /* Verify the blank line separating the header from the body */
144 if (*tagger_line != '\n')
145 return error("char" PD_FMT ": trailing garbage in tag header",
146 tagger_line - buffer);
c818566 @ebiederm [PATCH] Update tags to record who made them
ebiederm authored
147
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
148 /* The actual stuff afterwards we don't care about.. */
149 return 0;
150 }
151
579d1fb Add NO_C99_FORMAT to support older compilers.
Ramsay Allan Jones authored
152 #undef PD_FMT
153
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
154 int main(int argc, char **argv)
155 {
fd17f5b @MadCoder Replace all read_fd use with strbuf_read, and get rid of it.
MadCoder authored
156 struct strbuf buf;
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
157 unsigned char result_sha1[20];
158
159 if (argc != 1)
15e593e Fixup command names in some usage strings.
Ramsay Allan Jones authored
160 usage("git-mktag < signaturefile");
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
161
53228a5 Make the rest of commands work from a subdirectory.
Junio C Hamano authored
162 setup_git_directory();
163
fd17f5b @MadCoder Replace all read_fd use with strbuf_read, and get rid of it.
MadCoder authored
164 strbuf_init(&buf, 0);
165 if (strbuf_read(&buf, 0, 4096) < 0) {
e7332f9 @whatever42 remove the artificial restriction tagsize < 8kb
whatever42 authored
166 die("could not read from stdin");
b97e3df git-mktag: be more careful in reading the input.
Linus Torvalds authored
167 }
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
168
a9486b0 @proski Avoid C99 comments, use old-style C comments instead.
proski authored
169 /* Verify it for some basic sanity: it needs to start with
170 "object <sha1>\ntype\ntagger " */
fd17f5b @MadCoder Replace all read_fd use with strbuf_read, and get rid of it.
MadCoder authored
171 if (verify_tag(buf.buf, buf.len) < 0)
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
172 die("invalid tag signature file");
173
fd17f5b @MadCoder Replace all read_fd use with strbuf_read, and get rid of it.
MadCoder authored
174 if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
175 die("unable to write tag file");
e7332f9 @whatever42 remove the artificial restriction tagsize < 8kb
whatever42 authored
176
fd17f5b @MadCoder Replace all read_fd use with strbuf_read, and get rid of it.
MadCoder authored
177 strbuf_release(&buf);
ec4465a Add "tag" objects that can be used to sign other objects.
Linus Torvalds authored
178 printf("%s\n", sha1_to_hex(result_sha1));
179 return 0;
180 }
Something went wrong with that request. Please try again.