fa4a9e6003b5014fb8e222ad8813ce5954f9a38a
   1#include "cache.h"
   2#include "tag.h"
   3
   4/*
   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.
  10 *
  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
  21/* Some random size */
  22#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;
  30        char type[100];
  31        unsigned long size;
  32        void *buffer = read_sha1_file(sha1, type, &size);
  33
  34        if (buffer) {
  35                if (!strcmp(type, expected_type))
  36                        ret = check_sha1_signature(sha1, buffer, size, type);
  37                free(buffer);
  38        }
  39        return ret;
  40}
  41
  42static int verify_tag(char *buffer, unsigned long size)
  43{
  44        int typelen;
  45        char type[20];
  46        unsigned char sha1[20];
  47        const char *object, *type_line, *tag_line, *tagger_line;
  48
  49        if (size < 64)
  50                return error("wanna fool me ? you obviously got the size wrong !");
  51
  52        buffer[size] = 0;
  53
  54        /* Verify object line */
  55        object = buffer;
  56        if (memcmp(object, "object ", 7))
  57                return error("char%d: does not start with \"object \"", 0);
  58
  59        if (get_sha1_hex(object + 7, sha1))
  60                return error("char%d: could not get SHA1 hash", 7);
  61
  62        /* Verify type line */
  63        type_line = object + 48;
  64        if (memcmp(type_line - 1, "\ntype ", 6))
  65                return error("char%d: could not find \"\\ntype \"", 47);
  66
  67        /* Verify tag-line */
  68        tag_line = strchr(type_line, '\n');
  69        if (!tag_line)
  70                return error("char%td: could not find next \"\\n\"", type_line - buffer);
  71        tag_line++;
  72        if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
  73                return error("char%td: no \"tag \" found", tag_line - buffer);
  74
  75        /* Get the actual type */
  76        typelen = tag_line - type_line - strlen("type \n");
  77        if (typelen >= sizeof(type))
  78                return error("char%td: type too long", type_line+5 - buffer);
  79
  80        memcpy(type, type_line+5, typelen);
  81        type[typelen] = 0;
  82
  83        /* Verify that the object matches */
  84        if (verify_object(sha1, type))
  85                return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
  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;
  95                return error("char%td: could not verify tag name", tag_line - buffer);
  96        }
  97
  98        /* Verify the tagger line */
  99        tagger_line = tag_line;
 100
 101        if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n'))
 102                return error("char%td: could not find \"tagger\"", tagger_line - buffer);
 103
 104        /* TODO: check for committer info + blank line? */
 105        /* Also, the minimum length is probably + "tagger .", or 63+8=71 */
 106
 107        /* The actual stuff afterwards we don't care about.. */
 108        return 0;
 109}
 110
 111int main(int argc, char **argv)
 112{
 113        unsigned long size = 4096;
 114        char *buffer = malloc(size);
 115        unsigned char result_sha1[20];
 116
 117        if (argc != 1)
 118                usage("cat <signaturefile> | git-mktag");
 119
 120        setup_git_directory();
 121
 122        if (read_pipe(0, &buffer, &size)) {
 123                free(buffer);
 124                die("could not read from stdin");
 125        }
 126
 127        /* Verify it for some basic sanity: it needs to start with
 128           "object <sha1>\ntype\ntagger " */
 129        if (verify_tag(buffer, size) < 0)
 130                die("invalid tag signature file");
 131
 132        if (write_sha1_file(buffer, size, tag_type, result_sha1) < 0)
 133                die("unable to write tag file");
 134
 135        free(buffer);
 136
 137        printf("%s\n", sha1_to_hex(result_sha1));
 138        return 0;
 139}