29
#include "commit-reach.h"
30
#include "khash.h"
31
#include "date.h"
32
+#include "gpg-interface.h"
33
34
#define PACK_ID_BITS 16
35
#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
2719
return list;
2720
}
2721
2722
+struct signature_data {
2723
+ char *hash_algo; /* "sha1" or "sha256" */
2724
+ char *sig_format; /* "openpgp", "x509", "ssh", or "unknown" */
2725
+ struct strbuf data; /* The actual signature data */
2726
+};
2727
+
2728
+static void parse_one_signature(struct signature_data *sig, const char *v)
2729
+{
2730
+ char *args = xstrdup(v); /* Will be freed when sig->hash_algo is freed */
2731
+ char *space = strchr(args, ' ');
2732
+
2733
+ if (!space)
2734
+ die("Expected gpgsig format: 'gpgsig <hash-algo> <signature-format>', "
2735
+ "got 'gpgsig %s'", args);
2736
+ *space = '\0';
2737
+
2738
+ sig->hash_algo = args;
2739
+ sig->sig_format = space + 1;
2740
+
2741
+ /* Validate hash algorithm */
2742
+ if (strcmp(sig->hash_algo, "sha1") &&
2743
+ strcmp(sig->hash_algo, "sha256"))
2744
+ die("Unknown git hash algorithm in gpgsig: '%s'", sig->hash_algo);
2745
+
2746
+ /* Validate signature format */
2747
+ if (!valid_signature_format(sig->sig_format))
2748
+ die("Invalid signature format in gpgsig: '%s'", sig->sig_format);
2749
+ if (!strcmp(sig->sig_format, "unknown"))
2750
+ warning("'unknown' signature format in gpgsig");
2751
+
2752
+ /* Read signature data */
2753
+ read_next_command();
2754
+ parse_data(&sig->data, 0, NULL);
2755
+}
2756
+
2757
+static void add_gpgsig_to_commit(struct strbuf *commit_data,
2758
+ const char *header,
2759
+ struct signature_data *sig)
2760
+{
2761
+ struct string_list siglines = STRING_LIST_INIT_NODUP;
2762
+
2763
+ if (!sig->hash_algo)
2764
+ return;
2765
+
2766
+ strbuf_addstr(commit_data, header);
2767
+ string_list_split_in_place(&siglines, sig->data.buf, "\n", -1);
2768
+ strbuf_add_separated_string_list(commit_data, "\n ", &siglines);
2769
+ strbuf_addch(commit_data, '\n');
2770
+ string_list_clear(&siglines, 1);
2771
+ strbuf_release(&sig->data);
2772
+ free(sig->hash_algo);
2773
+}
2774
+
2775
+static void store_signature(struct signature_data *stored_sig,
2776
+ struct signature_data *new_sig,
2777
+ const char *hash_type)
2778
+{
2779
+ if (stored_sig->hash_algo) {
2780
+ warning("multiple %s signatures found, "
2781
+ "ignoring additional signature",
2782
+ hash_type);
2783
+ strbuf_release(&new_sig->data);
2784
+ free(new_sig->hash_algo);
2785
+ } else {
2786
+ *stored_sig = *new_sig;
2787
+ }
2788
+}
2789
+
2790
static void parse_new_commit(const char *arg)
2791
{
2723
- static struct strbuf sig = STRBUF_INIT;
2792
static struct strbuf msg = STRBUF_INIT;
2725
- struct string_list siglines = STRING_LIST_INIT_NODUP;
2793
+ struct signature_data sig_sha1 = { NULL, NULL, STRBUF_INIT };
2794
+ struct signature_data sig_sha256 = { NULL, NULL, STRBUF_INIT };
2795
struct branch *b;
2796
char *author = NULL;
2797
char *committer = NULL;
2729
- char *sig_alg = NULL;
2798
char *encoding = NULL;
2799
struct hash_list *merge_list = NULL;
2800
unsigned int merge_count;
2818
}
2819
if (!committer)
2820
die("Expected committer but didn't get one");
2753
- if (skip_prefix(command_buf.buf, "gpgsig ", &v)) {
2754
- sig_alg = xstrdup(v);
2755
- read_next_command();
2756
- parse_data(&sig, 0, NULL);
2821
+
2822
+ /* Process signatures (up to 2: one "sha1" and one "sha256") */
2823
+ while (skip_prefix(command_buf.buf, "gpgsig ", &v)) {
2824
+ struct signature_data sig = { NULL, NULL, STRBUF_INIT };
2825
+
2826
+ parse_one_signature(&sig, v);
2827
+
2828
+ if (!strcmp(sig.hash_algo, "sha1"))
2829
+ store_signature(&sig_sha1, &sig, "SHA-1");
2830
+ else if (!strcmp(sig.hash_algo, "sha256"))
2831
+ store_signature(&sig_sha256, &sig, "SHA-256");
2832
+ else
2833
+ BUG("parse_one_signature() returned unknown hash algo");
2834
+
2835
read_next_command();
2758
- } else
2759
- strbuf_setlen(&sig, 0);
2836
+ }
2837
+
2838
if (skip_prefix(command_buf.buf, "encoding ", &v)) {
2839
encoding = xstrdup(v);
2840
read_next_command();
2908
strbuf_addf(&new_data,
2909
"encoding %s\n",
2910
encoding);
2833
- if (sig_alg) {
2834
- if (!strcmp(sig_alg, "sha1"))
2835
- strbuf_addstr(&new_data, "gpgsig ");
2836
- else if (!strcmp(sig_alg, "sha256"))
2837
- strbuf_addstr(&new_data, "gpgsig-sha256 ");
2838
- else
2839
- die("Expected gpgsig algorithm sha1 or sha256, got %s", sig_alg);
2840
- string_list_split_in_place(&siglines, sig.buf, "\n", -1);
2841
- strbuf_add_separated_string_list(&new_data, "\n ", &siglines);
2842
- strbuf_addch(&new_data, '\n');
2843
- }
2911
+
2912
+ add_gpgsig_to_commit(&new_data, "gpgsig ", &sig_sha1);
2913
+ add_gpgsig_to_commit(&new_data, "gpgsig-sha256 ", &sig_sha256);
2914
+
2915
strbuf_addch(&new_data, '\n');
2916
strbuf_addbuf(&new_data, &msg);
2846
- string_list_clear(&siglines, 1);
2917
free(author);
2918
free(committer);
2849
- free(sig_alg);
2919
free(encoding);
2920
2921
if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark))