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