Skip to content

Commit 0276943

Browse files
FStelzergitster
authored andcommitted
ssh signing: use sigc struct to pass payload
To be able to extend the payload metadata with things like its creation timestamp or the creators ident we remove the payload parameters to check_signature() and use the already existing sigc->payload field instead, only adding the length field to the struct. This also allows us to get rid of the xmemdupz() calls in the verify functions. Since sigc is now used to input data as well as output the result move it to the front of the function list. - Add payload_length to struct signature_check - Populate sigc.payload/payload_len on all call sites - Remove payload parameters to check_signature() - Remove payload parameters to internal verify_* functions and use sigc instead - Remove xmemdupz() used for verbose output since payload is now already populated. Signed-off-by: Fabian Stelzer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cafd345 commit 0276943

File tree

7 files changed

+35
-35
lines changed

7 files changed

+35
-35
lines changed

builtin/receive-pack.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,10 @@ static void prepare_push_cert_sha1(struct child_process *proc)
769769
memset(&sigcheck, '\0', sizeof(sigcheck));
770770

771771
bogs = parse_signed_buffer(push_cert.buf, push_cert.len);
772-
check_signature(push_cert.buf, bogs, push_cert.buf + bogs,
773-
push_cert.len - bogs, &sigcheck);
772+
sigcheck.payload = xmemdupz(push_cert.buf, bogs);
773+
sigcheck.payload_len = bogs;
774+
check_signature(&sigcheck, push_cert.buf + bogs,
775+
push_cert.len - bogs);
774776

775777
nonce_status = check_nonce(push_cert.buf, bogs);
776778
}

commit.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,8 +1212,9 @@ int check_commit_signature(const struct commit *commit, struct signature_check *
12121212

12131213
if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
12141214
goto out;
1215-
ret = check_signature(payload.buf, payload.len, signature.buf,
1216-
signature.len, sigc);
1215+
1216+
sigc->payload = strbuf_detach(&payload, &sigc->payload_len);
1217+
ret = check_signature(sigc, signature.buf, signature.len);
12171218

12181219
out:
12191220
strbuf_release(&payload);

fmt-merge-msg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
533533
else {
534534
buf = payload.buf;
535535
len = payload.len;
536-
if (check_signature(payload.buf, payload.len, sig.buf,
537-
sig.len, &sigc) &&
536+
sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
537+
if (check_signature(&sigc, sig.buf, sig.len) &&
538538
!sigc.output)
539539
strbuf_addstr(&sig, "gpg verification failed.\n");
540540
else

gpg-interface.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ struct gpg_format {
1919
const char **verify_args;
2020
const char **sigs;
2121
int (*verify_signed_buffer)(struct signature_check *sigc,
22-
struct gpg_format *fmt, const char *payload,
23-
size_t payload_size, const char *signature,
22+
struct gpg_format *fmt,
23+
const char *signature,
2424
size_t signature_size);
2525
int (*sign_buffer)(struct strbuf *buffer, struct strbuf *signature,
2626
const char *signing_key);
@@ -53,12 +53,12 @@ static const char *ssh_sigs[] = {
5353
};
5454

5555
static int verify_gpg_signed_buffer(struct signature_check *sigc,
56-
struct gpg_format *fmt, const char *payload,
57-
size_t payload_size, const char *signature,
56+
struct gpg_format *fmt,
57+
const char *signature,
5858
size_t signature_size);
5959
static int verify_ssh_signed_buffer(struct signature_check *sigc,
60-
struct gpg_format *fmt, const char *payload,
61-
size_t payload_size, const char *signature,
60+
struct gpg_format *fmt,
61+
const char *signature,
6262
size_t signature_size);
6363
static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
6464
const char *signing_key);
@@ -314,8 +314,8 @@ static void parse_gpg_output(struct signature_check *sigc)
314314
}
315315

316316
static int verify_gpg_signed_buffer(struct signature_check *sigc,
317-
struct gpg_format *fmt, const char *payload,
318-
size_t payload_size, const char *signature,
317+
struct gpg_format *fmt,
318+
const char *signature,
319319
size_t signature_size)
320320
{
321321
struct child_process gpg = CHILD_PROCESS_INIT;
@@ -343,14 +343,13 @@ static int verify_gpg_signed_buffer(struct signature_check *sigc,
343343
NULL);
344344

345345
sigchain_push(SIGPIPE, SIG_IGN);
346-
ret = pipe_command(&gpg, payload, payload_size, &gpg_stdout, 0,
346+
ret = pipe_command(&gpg, sigc->payload, sigc->payload_len, &gpg_stdout, 0,
347347
&gpg_stderr, 0);
348348
sigchain_pop(SIGPIPE);
349349

350350
delete_tempfile(&temp);
351351

352352
ret |= !strstr(gpg_stdout.buf, "\n[GNUPG:] GOODSIG ");
353-
sigc->payload = xmemdupz(payload, payload_size);
354353
sigc->output = strbuf_detach(&gpg_stderr, NULL);
355354
sigc->gpg_status = strbuf_detach(&gpg_stdout, NULL);
356355

@@ -426,8 +425,8 @@ static void parse_ssh_output(struct signature_check *sigc)
426425
}
427426

428427
static int verify_ssh_signed_buffer(struct signature_check *sigc,
429-
struct gpg_format *fmt, const char *payload,
430-
size_t payload_size, const char *signature,
428+
struct gpg_format *fmt,
429+
const char *signature,
431430
size_t signature_size)
432431
{
433432
struct child_process ssh_keygen = CHILD_PROCESS_INIT;
@@ -480,7 +479,7 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
480479
"-n", "git",
481480
"-s", buffer_file->filename.buf,
482481
NULL);
483-
pipe_command(&ssh_keygen, payload, payload_size,
482+
pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len,
484483
&ssh_keygen_out, 0, &ssh_keygen_err, 0);
485484

486485
/*
@@ -526,7 +525,7 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
526525
}
527526

528527
sigchain_push(SIGPIPE, SIG_IGN);
529-
ret = pipe_command(&ssh_keygen, payload, payload_size,
528+
ret = pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len,
530529
&ssh_keygen_out, 0, &ssh_keygen_err, 0);
531530
sigchain_pop(SIGPIPE);
532531

@@ -540,7 +539,6 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
540539
}
541540
}
542541

543-
sigc->payload = xmemdupz(payload, payload_size);
544542
strbuf_stripspace(&ssh_keygen_out, 0);
545543
strbuf_stripspace(&ssh_keygen_err, 0);
546544
/* Add stderr outputs to show the user actual ssh-keygen errors */
@@ -562,8 +560,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
562560
return ret;
563561
}
564562

565-
int check_signature(const char *payload, size_t plen, const char *signature,
566-
size_t slen, struct signature_check *sigc)
563+
int check_signature(struct signature_check *sigc,
564+
const char *signature, size_t slen)
567565
{
568566
struct gpg_format *fmt;
569567
int status;
@@ -575,8 +573,7 @@ int check_signature(const char *payload, size_t plen, const char *signature,
575573
if (!fmt)
576574
die(_("bad/incompatible signature '%s'"), signature);
577575

578-
status = fmt->verify_signed_buffer(sigc, fmt, payload, plen, signature,
579-
slen);
576+
status = fmt->verify_signed_buffer(sigc, fmt, signature, slen);
580577

581578
if (status && !sigc->output)
582579
return !!status;
@@ -593,7 +590,7 @@ void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
593590
sigc->output;
594591

595592
if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
596-
fputs(sigc->payload, stdout);
593+
fwrite(sigc->payload, 1, sigc->payload_len, stdout);
597594

598595
if (output)
599596
fputs(output, stderr);

gpg-interface.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum signature_trust_level {
1717

1818
struct signature_check {
1919
char *payload;
20+
size_t payload_len;
2021
char *output;
2122
char *gpg_status;
2223

@@ -70,9 +71,8 @@ const char *get_signing_key(void);
7071
* Either a GPG KeyID or a SSH Key Fingerprint
7172
*/
7273
const char *get_signing_key_id(void);
73-
int check_signature(const char *payload, size_t plen,
74-
const char *signature, size_t slen,
75-
struct signature_check *sigc);
74+
int check_signature(struct signature_check *sigc,
75+
const char *signature, size_t slen);
7676
void print_signature_buffer(const struct signature_check *sigc,
7777
unsigned flags);
7878

log-tree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ static void show_signature(struct rev_info *opt, struct commit *commit)
513513
if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
514514
goto out;
515515

516-
status = check_signature(payload.buf, payload.len, signature.buf,
517-
signature.len, &sigc);
516+
sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
517+
status = check_signature(&sigc, signature.buf, signature.len);
518518
if (status && !sigc.output)
519519
show_sig_lines(opt, status, "No signature\n");
520520
else
@@ -583,8 +583,8 @@ static int show_one_mergetag(struct commit *commit,
583583
status = -1;
584584
if (parse_signature(extra->value, extra->len, &payload, &signature)) {
585585
/* could have a good signature */
586-
status = check_signature(payload.buf, payload.len,
587-
signature.buf, signature.len, &sigc);
586+
sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
587+
status = check_signature(&sigc, signature.buf, signature.len);
588588
if (sigc.output)
589589
strbuf_addstr(&verify_message, sigc.output);
590590
else

tag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
2525
return error("no signature found");
2626
}
2727

28-
ret = check_signature(payload.buf, payload.len, signature.buf,
29-
signature.len, &sigc);
28+
sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
29+
ret = check_signature(&sigc, signature.buf, signature.len);
3030

3131
if (!(flags & GPG_VERIFY_OMIT_STATUS))
3232
print_signature_buffer(&sigc, flags);

0 commit comments

Comments
 (0)