Skip to content

Commit d5fec92

Browse files
committed
Merge branch 'sg/gpg-sig'
Teach "merge/pull" to optionally verify and reject commits that are not signed properly. * sg/gpg-sig: pretty printing: extend %G? to include 'N' and 'U' merge/pull Check for untrusted good GPG signatures merge/pull: verify GPG signatures of commits being merged commit.c/GPG signature verification: Also look at the first GPG status line Move commit GPG signature verification to commit.c
2 parents 7b72ec5 + e290c4b commit d5fec92

File tree

13 files changed

+219
-82
lines changed

13 files changed

+219
-82
lines changed

Documentation/merge-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ option can be used to override --squash.
8484
Pass merge strategy specific option through to the merge
8585
strategy.
8686

87+
--verify-signatures::
88+
--no-verify-signatures::
89+
Verify that the commits being merged have good and trusted GPG signatures
90+
and abort the merge in case they do not.
91+
8792
--summary::
8893
--no-summary::
8994
Synonyms to --stat and --no-stat; these are deprecated and will be

Documentation/pretty-formats.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ The placeholders are:
131131
- '%B': raw body (unwrapped subject and body)
132132
- '%N': commit notes
133133
- '%GG': raw verification message from GPG for a signed commit
134-
- '%G?': show either "G" for Good or "B" for Bad for a signed commit
134+
- '%G?': show "G" for a Good signature, "B" for a Bad signature, "U" for a good,
135+
untrusted signature and "N" for no signature
135136
- '%GS': show the name of the signer for a signed commit
136137
- '%GK': show the key used to sign a signed commit
137138
- '%gD': reflog selector, e.g., `refs/stash@{1}`

builtin/merge.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static const char * const builtin_merge_usage[] = {
4949
static int show_diffstat = 1, shortlog_len = -1, squash;
5050
static int option_commit = 1, allow_fast_forward = 1;
5151
static int fast_forward_only, option_edit = -1;
52-
static int allow_trivial = 1, have_message;
52+
static int allow_trivial = 1, have_message, verify_signatures;
5353
static int overwrite_ignore = 1;
5454
static struct strbuf merge_msg = STRBUF_INIT;
5555
static struct strategy **use_strategies;
@@ -199,6 +199,8 @@ static struct option builtin_merge_options[] = {
199199
OPT_BOOLEAN(0, "ff-only", &fast_forward_only,
200200
N_("abort if fast-forward is not possible")),
201201
OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
202+
OPT_BOOL(0, "verify-signatures", &verify_signatures,
203+
N_("Verify that the named commit has a valid GPG signature")),
202204
OPT_CALLBACK('s', "strategy", &use_strategies, N_("strategy"),
203205
N_("merge strategy to use"), option_parse_strategy),
204206
OPT_CALLBACK('X', "strategy-option", &xopts, N_("option=value"),
@@ -1246,6 +1248,39 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12461248
usage_with_options(builtin_merge_usage,
12471249
builtin_merge_options);
12481250

1251+
if (verify_signatures) {
1252+
for (p = remoteheads; p; p = p->next) {
1253+
struct commit *commit = p->item;
1254+
char hex[41];
1255+
struct signature_check signature_check;
1256+
memset(&signature_check, 0, sizeof(signature_check));
1257+
1258+
check_commit_signature(commit, &signature_check);
1259+
1260+
strcpy(hex, find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
1261+
switch (signature_check.result) {
1262+
case 'G':
1263+
break;
1264+
case 'U':
1265+
die(_("Commit %s has an untrusted GPG signature, "
1266+
"allegedly by %s."), hex, signature_check.signer);
1267+
case 'B':
1268+
die(_("Commit %s has a bad GPG signature "
1269+
"allegedly by %s."), hex, signature_check.signer);
1270+
default: /* 'N' */
1271+
die(_("Commit %s does not have a GPG signature."), hex);
1272+
}
1273+
if (verbosity >= 0 && signature_check.result == 'G')
1274+
printf(_("Commit %s has a good GPG signature by %s\n"),
1275+
hex, signature_check.signer);
1276+
1277+
free(signature_check.gpg_output);
1278+
free(signature_check.gpg_status);
1279+
free(signature_check.signer);
1280+
free(signature_check.key);
1281+
}
1282+
}
1283+
12491284
strbuf_addstr(&buf, "merge");
12501285
for (p = remoteheads; p; p = p->next)
12511286
strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name);

commit.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,76 @@ static void handle_signed_tag(struct commit *parent, struct commit_extra_header
10411041
free(buf);
10421042
}
10431043

1044+
static struct {
1045+
char result;
1046+
const char *check;
1047+
} sigcheck_gpg_status[] = {
1048+
{ 'G', "\n[GNUPG:] GOODSIG " },
1049+
{ 'B', "\n[GNUPG:] BADSIG " },
1050+
{ 'U', "\n[GNUPG:] TRUST_NEVER" },
1051+
{ 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
1052+
};
1053+
1054+
static void parse_gpg_output(struct signature_check *sigc)
1055+
{
1056+
const char *buf = sigc->gpg_status;
1057+
int i;
1058+
1059+
/* Iterate over all search strings */
1060+
for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
1061+
const char *found, *next;
1062+
1063+
if (!prefixcmp(buf, sigcheck_gpg_status[i].check + 1)) {
1064+
/* At the very beginning of the buffer */
1065+
found = buf + strlen(sigcheck_gpg_status[i].check + 1);
1066+
} else {
1067+
found = strstr(buf, sigcheck_gpg_status[i].check);
1068+
if (!found)
1069+
continue;
1070+
found += strlen(sigcheck_gpg_status[i].check);
1071+
}
1072+
sigc->result = sigcheck_gpg_status[i].result;
1073+
/* The trust messages are not followed by key/signer information */
1074+
if (sigc->result != 'U') {
1075+
sigc->key = xmemdupz(found, 16);
1076+
found += 17;
1077+
next = strchrnul(found, '\n');
1078+
sigc->signer = xmemdupz(found, next - found);
1079+
}
1080+
}
1081+
}
1082+
1083+
void check_commit_signature(const struct commit* commit, struct signature_check *sigc)
1084+
{
1085+
struct strbuf payload = STRBUF_INIT;
1086+
struct strbuf signature = STRBUF_INIT;
1087+
struct strbuf gpg_output = STRBUF_INIT;
1088+
struct strbuf gpg_status = STRBUF_INIT;
1089+
int status;
1090+
1091+
sigc->result = 'N';
1092+
1093+
if (parse_signed_commit(commit->object.sha1,
1094+
&payload, &signature) <= 0)
1095+
goto out;
1096+
status = verify_signed_buffer(payload.buf, payload.len,
1097+
signature.buf, signature.len,
1098+
&gpg_output, &gpg_status);
1099+
if (status && !gpg_output.len)
1100+
goto out;
1101+
sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
1102+
sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
1103+
parse_gpg_output(sigc);
1104+
1105+
out:
1106+
strbuf_release(&gpg_status);
1107+
strbuf_release(&gpg_output);
1108+
strbuf_release(&payload);
1109+
strbuf_release(&signature);
1110+
}
1111+
1112+
1113+
10441114
void append_merge_tag_headers(struct commit_list *parents,
10451115
struct commit_extra_header ***tail)
10461116
{

commit.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "tree.h"
66
#include "strbuf.h"
77
#include "decorate.h"
8+
#include "gpg-interface.h"
89

910
struct commit_list {
1011
struct commit *item;
@@ -232,4 +233,13 @@ extern void print_commit_list(struct commit_list *list,
232233
const char *format_cur,
233234
const char *format_last);
234235

236+
/*
237+
* Check the signature of the given commit. The result of the check is stored
238+
* in sig->check_result, 'G' for a good signature, 'U' for a good signature
239+
* from an untrusted signer, 'B' for a bad signature and 'N' for no signature
240+
* at all. This may allocate memory for sig->gpg_output, sig->gpg_status,
241+
* sig->signer and sig->key.
242+
*/
243+
extern void check_commit_signature(const struct commit* commit, struct signature_check *sigc);
244+
235245
#endif /* COMMIT_H */

git-pull.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test -z "$(git ls-files -u)" || die_conflict
3939
test -f "$GIT_DIR/MERGE_HEAD" && die_merge
4040

4141
strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
42-
log_arg= verbosity= progress= recurse_submodules=
42+
log_arg= verbosity= progress= recurse_submodules= verify_signatures=
4343
merge_args= edit=
4444
curr_branch=$(git symbolic-ref -q HEAD)
4545
curr_branch_short="${curr_branch#refs/heads/}"
@@ -125,6 +125,12 @@ do
125125
--no-recurse-submodules)
126126
recurse_submodules=--no-recurse-submodules
127127
;;
128+
--verify-signatures)
129+
verify_signatures=--verify-signatures
130+
;;
131+
--no-verify-signatures)
132+
verify_signatures=--no-verify-signatures
133+
;;
128134
--d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
129135
dry_run=--dry-run
130136
;;
@@ -283,7 +289,7 @@ true)
283289
eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
284290
;;
285291
*)
286-
eval="git-merge $diffstat $no_commit $edit $squash $no_ff $ff_only"
292+
eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
287293
eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
288294
eval="$eval \"\$merge_name\" HEAD $merge_head"
289295
;;

gpg-interface.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#ifndef GPG_INTERFACE_H
22
#define GPG_INTERFACE_H
33

4+
struct signature_check {
5+
char *gpg_output;
6+
char *gpg_status;
7+
char result; /* 0 (not checked),
8+
* N (checked but no further result),
9+
* U (untrusted good),
10+
* G (good)
11+
* B (bad) */
12+
char *signer;
13+
char *key;
14+
};
15+
416
extern int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key);
517
extern int verify_signed_buffer(const char *payload, size_t payload_size, const char *signature, size_t signature_size, struct strbuf *gpg_output, struct strbuf *gpg_status);
618
extern int git_gpg_config(const char *, const char *, void *);

pretty.c

Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -766,14 +766,7 @@ struct format_commit_context {
766766
const struct pretty_print_context *pretty_ctx;
767767
unsigned commit_header_parsed:1;
768768
unsigned commit_message_parsed:1;
769-
unsigned commit_signature_parsed:1;
770-
struct {
771-
char *gpg_output;
772-
char *gpg_status;
773-
char good_bad;
774-
char *signer;
775-
char *key;
776-
} signature;
769+
struct signature_check signature_check;
777770
char *message;
778771
size_t width, indent1, indent2;
779772

@@ -956,64 +949,6 @@ static void rewrap_message_tail(struct strbuf *sb,
956949
c->indent2 = new_indent2;
957950
}
958951

959-
static struct {
960-
char result;
961-
const char *check;
962-
} signature_check[] = {
963-
{ 'G', "\n[GNUPG:] GOODSIG " },
964-
{ 'B', "\n[GNUPG:] BADSIG " },
965-
};
966-
967-
static void parse_signature_lines(struct format_commit_context *ctx)
968-
{
969-
const char *buf = ctx->signature.gpg_status;
970-
int i;
971-
972-
for (i = 0; i < ARRAY_SIZE(signature_check); i++) {
973-
const char *found = strstr(buf, signature_check[i].check);
974-
const char *next;
975-
if (!found)
976-
continue;
977-
ctx->signature.good_bad = signature_check[i].result;
978-
found += strlen(signature_check[i].check);
979-
ctx->signature.key = xmemdupz(found, 16);
980-
found += 17;
981-
next = strchrnul(found, '\n');
982-
ctx->signature.signer = xmemdupz(found, next - found);
983-
break;
984-
}
985-
}
986-
987-
static void parse_commit_signature(struct format_commit_context *ctx)
988-
{
989-
struct strbuf payload = STRBUF_INIT;
990-
struct strbuf signature = STRBUF_INIT;
991-
struct strbuf gpg_output = STRBUF_INIT;
992-
struct strbuf gpg_status = STRBUF_INIT;
993-
int status;
994-
995-
ctx->commit_signature_parsed = 1;
996-
997-
if (parse_signed_commit(ctx->commit->object.sha1,
998-
&payload, &signature) <= 0)
999-
goto out;
1000-
status = verify_signed_buffer(payload.buf, payload.len,
1001-
signature.buf, signature.len,
1002-
&gpg_output, &gpg_status);
1003-
if (status && !gpg_output.len)
1004-
goto out;
1005-
ctx->signature.gpg_output = strbuf_detach(&gpg_output, NULL);
1006-
ctx->signature.gpg_status = strbuf_detach(&gpg_status, NULL);
1007-
parse_signature_lines(ctx);
1008-
1009-
out:
1010-
strbuf_release(&gpg_status);
1011-
strbuf_release(&gpg_output);
1012-
strbuf_release(&payload);
1013-
strbuf_release(&signature);
1014-
}
1015-
1016-
1017952
static int format_reflog_person(struct strbuf *sb,
1018953
char part,
1019954
struct reflog_walk_info *log,
@@ -1199,27 +1134,29 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
11991134
}
12001135

12011136
if (placeholder[0] == 'G') {
1202-
if (!c->commit_signature_parsed)
1203-
parse_commit_signature(c);
1137+
if (!c->signature_check.result)
1138+
check_commit_signature(c->commit, &(c->signature_check));
12041139
switch (placeholder[1]) {
12051140
case 'G':
1206-
if (c->signature.gpg_output)
1207-
strbuf_addstr(sb, c->signature.gpg_output);
1141+
if (c->signature_check.gpg_output)
1142+
strbuf_addstr(sb, c->signature_check.gpg_output);
12081143
break;
12091144
case '?':
1210-
switch (c->signature.good_bad) {
1145+
switch (c->signature_check.result) {
12111146
case 'G':
12121147
case 'B':
1213-
strbuf_addch(sb, c->signature.good_bad);
1148+
case 'U':
1149+
case 'N':
1150+
strbuf_addch(sb, c->signature_check.result);
12141151
}
12151152
break;
12161153
case 'S':
1217-
if (c->signature.signer)
1218-
strbuf_addstr(sb, c->signature.signer);
1154+
if (c->signature_check.signer)
1155+
strbuf_addstr(sb, c->signature_check.signer);
12191156
break;
12201157
case 'K':
1221-
if (c->signature.key)
1222-
strbuf_addstr(sb, c->signature.key);
1158+
if (c->signature_check.key)
1159+
strbuf_addstr(sb, c->signature_check.key);
12231160
break;
12241161
}
12251162
return 2;
@@ -1357,8 +1294,8 @@ void format_commit_message(const struct commit *commit,
13571294
rewrap_message_tail(sb, &context, 0, 0, 0);
13581295

13591296
logmsg_free(context.message, commit);
1360-
free(context.signature.gpg_output);
1361-
free(context.signature.signer);
1297+
free(context.signature_check.gpg_output);
1298+
free(context.signature_check.signer);
13621299
}
13631300

13641301
static void pp_header(const struct pretty_print_context *pp,

t/lib-gpg/pubring.gpg

1.17 KB
Binary file not shown.

t/lib-gpg/random_seed

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)