Signed push
v2 cover Signed push
The first round is found at $gmane/255520
While signed tags and commits assert that the objects thusly signed came from you, who signed these objects, there is not a good way to assert that you wanted to have a particular object at the tip of a particular branch. My signing v2.0.1 tag only means I want to call the version v2.0.1, and it does not mean I want to push it out to my 'master' branch---it is likely that I only want it in 'maint', so the signature on the object alone is insufficient.
The only assurance to you that 'maint' points at what I wanted to place there comes from your trust on the hosting site and my authentication with it, which cannot easily audited later.
This series introduces a cryptographic assurance for ref updates done by "git push" by introducing a mechanism that allows you to sign a "push certificate" (for the lack of better name) every time you push. Think of it as working on an axis orthogonal to the traditional "signed tags".
Notable changes from the first iteration are:
- "push --signed" against a receiver that does not support push certificates used to downgrade to an unsigned push with a warning; this round makes it die.
- The push-cert capability the receiver sends now with a value, <nonce>; the certificate must include this value on the "nonce" header to prevent a valid push certificate that was used to push elsewhere from being replayed to push to an unrelated repository.
And several typofixes here and there.
Junio C Hamano (19):
receive-pack: do not overallocate command structure
receive-pack: parse feature request a bit earlier
receive-pack: do not reuse old_sha1[] for other things
receive-pack: factor out queueing of command
send-pack: move REF_STATUS_REJECT_NODELETE logic a bit higher
send-pack: refactor decision to send update per ref
send-pack: always send capabilities
send-pack: factor out capability string generation
send-pack: rename "new_refs" to "need_pack_data"
send-pack: refactor inspecting and resetting status and sending
commands
send-pack: clarify that cmds_sent is a boolean
gpg-interface: move parse_gpg_output() to where it should be
gpg-interface: move parse_signature() to where it should be pack-protocol doc: typofix for PKT-LINE
the beginning of the signed push
receive-pack: GPG-validate push certificates
send-pack: send feature request on push-cert packet signed push: signed push: remove duplicated protocol info signed push: fortify against replay attacks
Documentation/git-push.txt | 9 +- Documentation/git-receive-pack.txt | 41 ++++- Documentation/technical/pack-protocol.txt | 25 ++- Documentation/technical/protocol-capabilities.txt | 13 +- builtin/push.c | 1 + builtin/receive-pack.c | 199 ++++++++++++++++++---- commit.c | 36 ---- gpg-interface.c | 57 +++++++ gpg-interface.h | 18 +- send-pack.c | 197 ++++++++++++++++----- send-pack.h | 1 + t/t5534-push-signed.sh | 82 +++++++++ tag.c | 20 --- tag.h | 1 - transport.c | 4 + transport.h | 5 + 16 files changed, 564 insertions(+), 145 deletions(-) create mode 100755 t/t5534-push-signed.sh
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 1/19 receive-pack: do not overallocate command structure
An "update" command in the protocol exchange consists of 40-hex old object name, SP, 40-hex new object name, SP, and a refname, but the first instance is further followed by a NUL with feature requests.
The command structure, which has a flex-array member that stores the refname at the end, was allocated based on the whole length of the update command, without excluding the trailing feature requests.
--- builtin/receive-pack.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
builtin/receive-pack.c +3 -2
@@ -872,10 +872,11 @@ static struct command *read_head_info(struct sha1_array *shallow)if (parse_feature_request (feature_list,"quiet" )) quiet =1 ; } - cmd =xcalloc (1 ,sizeof (struct command ) + len -80 ); + cmd =xcalloc (1 ,sizeof (struct command ) + reflen +1 );hashcpy (cmd->old_sha1, old_sha1);hashcpy (cmd->new_sha1, new_sha1); -memcpy (cmd->ref_name, line +82 , len -81 ); +memcpy (cmd->ref_name, refname, reflen); + cmd->ref_name[reflen] ='\0' ; *p = cmd; p = &cmd->next; }
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 2/19 receive-pack: parse feature request a bit earlier
In reply to Junio C HamanoIdeally, we should have also allowed the first "shallow" to carry the feature request trailer, but that is water under the bridge now. This makes the next step to factor out the queuing of commands easier to review.
--- builtin/receive-pack.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-)
builtin/receive-pack.c +14 -12
@@ -840,7 +840,7 @@ static struct command *read_head_info(struct sha1_array *shallow)unsigned char old_sha1[20 ], new_sha1[20 ];struct command *cmd;char *refname; -int len, reflen; +int len, reflen, linelen; line =packet_read_line (0 , &len);if (!line)
@@ -853,7 +853,18 @@ static struct command *read_head_info(struct sha1_array *shallow)continue ; } - if (len < 83 || + linelen =strlen (line); +if (linelen < len) { +const char *feature_list = line + linelen +1 ; +if (parse_feature_request (feature_list,"report-status" )) + report_status =1 ; +if (parse_feature_request (feature_list,"side-band-64k" )) + use_sideband =LARGE_PACKET_MAX ; +if (parse_feature_request (feature_list,"quiet" )) + quiet =1 ; + } + + if (linelen < 83 || line[40] != ' ' || line[81] != ' ' || get_sha1_hex(line, old_sha1) ||
@@ -862,16 +873,7 @@ static struct command *read_head_info(struct sha1_array *shallow) line); refname = line +82 ; - reflen =strlen (refname); -if (reflen +82 < len) { -const char *feature_list = refname + reflen +1 ; -if (parse_feature_request (feature_list,"report-status" )) - report_status =1 ; -if (parse_feature_request (feature_list,"side-band-64k" )) - use_sideband =LARGE_PACKET_MAX ; -if (parse_feature_request (feature_list,"quiet" )) - quiet =1 ; - } + reflen = linelen -82 ; cmd =xcalloc (1 ,sizeof (struct command ) + reflen +1 );hashcpy (cmd->old_sha1, old_sha1);hashcpy (cmd->new_sha1, new_sha1);
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 3/19 receive-pack: do not reuse old_sha1[] for other things
In reply to Junio C HamanoThis piece of code reads object names of shallow boundaries, not old_sha1[], i.e. the current value the ref points at, which is to be replaced by what is in new_sha1[].
--- builtin/receive-pack.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
builtin/receive-pack.c +5 -3
@@ -847,9 +847,11 @@ static struct command *read_head_info(struct sha1_array *shallow)break ;if (len ==48 &&starts_with (line,"shallow " )) { -if (get_sha1_hex (line +8 , old_sha1)) -die ("protocol error: expected shallow sha, got '%s'" , line +8 ); -sha1_array_append (shallow, old_sha1); +unsigned char sha1[20 ]; +if (get_sha1_hex (line +8 , sha1)) +die ("protocol error: expected shallow sha, got '%s'" , + line +8 ); +sha1_array_append (shallow, sha1);continue ; }
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 4/19 receive-pack: factor out queueing of command
In reply to Junio C HamanoMake a helper function to accept a line of a protocol message and queue an update command out of the code from read_head_info().
--- builtin/receive-pack.c | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-)
builtin/receive-pack.c +29 -21
@@ -831,16 +831,40 @@ static void execute_commands(struct command *commands, "the reported refs above"); } +static struct command **queue_command (struct command **p, +const char *line, +int linelen) +{ +unsigned char old_sha1[20 ], new_sha1[20 ]; +struct command *cmd; +const char *refname; +int reflen; + +if (linelen <83 || + line[40 ] !=' ' || + line[81 ] !=' ' || +get_sha1_hex (line, old_sha1) || +get_sha1_hex (line +41 , new_sha1)) +die ("protocol error: expected old/new/ref, got '%s'" , line); + + refname = line +82 ; + reflen = linelen -82 ; + cmd =xcalloc (1 ,sizeof (struct command ) + reflen +1 ); +hashcpy (cmd->old_sha1, old_sha1); +hashcpy (cmd->new_sha1, new_sha1); +memcpy (cmd->ref_name, refname, reflen); + cmd->ref_name[reflen] ='\0' ; + *p = cmd; +return &cmd->next; +} +static struct command *read_head_info (struct sha1_array *shallow) {struct command *commands =NULL ;struct command **p = &commands;for (;;) {char *line; -unsigned char old_sha1[20 ], new_sha1[20 ]; -struct command *cmd; -char *refname; -int len, reflen, linelen; +int len, linelen; line =packet_read_line (0 , &len);if (!line)
@@ -866,23 +890,7 @@ static struct command *read_head_info(struct sha1_array *shallow) quiet =1 ; } -if (linelen <83 || - line[40 ] !=' ' || - line[81 ] !=' ' || -get_sha1_hex (line, old_sha1) || -get_sha1_hex (line +41 , new_sha1)) -die ("protocol error: expected old/new/ref, got '%s'" , - line); - - refname = line +82 ; - reflen = linelen -82 ; - cmd =xcalloc (1 ,sizeof (struct command ) + reflen +1 ); -hashcpy (cmd->old_sha1, old_sha1); -hashcpy (cmd->new_sha1, new_sha1); -memcpy (cmd->ref_name, refname, reflen); - cmd->ref_name[reflen] ='\0' ; - *p = cmd; - p = &cmd->next; + p =queue_command (p, line, linelen); }return commands; }
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 5/19 send-pack: move REF_STATUS_REJECT_NODELETE logic a bit higher
In reply to Junio C Hamano20e8b465 (refactor ref status logic for pushing, 2010-01-08) restructured the code to set status for each ref to be pushed, but did not quite go far enough. We inspect the status set earlier by set_refs_status_for_push() and then perform yet another update to the status of a ref with an otherwise OK status to be deleted to mark it with REF_STATUS_REJECT_NODELETE when the protocol tells us never to delete.
Split the latter into a separate loop that comes before we enter the per-ref loop. This way we would have one less condition to check in the main loop.
--- send-pack.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
send-pack.c +10 -5
@@ -231,6 +231,15 @@ int send_pack(struct send_pack_args *args,return 0 ; } +/* +* NEEDSWORK: why is delete-refs so specific to send-pack +* machinery that set_ref_status_for_push() cannot set this +* bit for us??? +*/ +for (ref = remote_refs; ref; ref = ref->next) +if (ref->deletion && !allow_deleting_refs) + ref->status =REF_STATUS_REJECT_NODELETE ; +if (!args->dry_run)advertise_shallow_grafts_buf (&req_buf);
@@ -249,17 +258,13 @@ int send_pack(struct send_pack_args *args,case REF_STATUS_REJECT_FETCH_FIRST :case REF_STATUS_REJECT_NEEDS_FORCE :case REF_STATUS_REJECT_STALE : +case REF_STATUS_REJECT_NODELETE :case REF_STATUS_UPTODATE :continue ;default : ;/* do nothing */ } -if (ref->deletion && !allow_deleting_refs) { - ref->status =REF_STATUS_REJECT_NODELETE ; -continue ; - } -if (!ref->deletion) new_refs++;
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 6/19 send-pack: refactor decision to send update per ref
In reply to Junio C HamanoA new helper function ref_update_to_be_sent() decides for each ref if the update is to be sent based on the status previously set by set_ref_status_for_push() and also if this is a mirrored push.
--- send-pack.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-)
send-pack.c +21 -15
@@ -190,6 +190,26 @@ static void advertise_shallow_grafts_buf(struct strbuf *sb)for_each_commit_graft (advertise_shallow_grafts_cb, sb); } +static int ref_update_to_be_sent (const struct ref *ref,const struct send_pack_args *args) +{ +if (!ref->peer_ref && !args->send_mirror) +return 0 ; + +/* Check for statuses set by set_ref_status_for_push() */ +switch (ref->status) { +case REF_STATUS_REJECT_NONFASTFORWARD : +case REF_STATUS_REJECT_ALREADY_EXISTS : +case REF_STATUS_REJECT_FETCH_FIRST : +case REF_STATUS_REJECT_NEEDS_FORCE : +case REF_STATUS_REJECT_STALE : +case REF_STATUS_REJECT_NODELETE : +case REF_STATUS_UPTODATE : +return 0 ; +default : +return 1 ; + } +} + int send_pack(struct send_pack_args *args, int fd[], struct child_process *conn, struct ref *remote_refs,
@@ -248,23 +268,9 @@ int send_pack(struct send_pack_args *args,*/ new_refs =0 ;for (ref = remote_refs; ref; ref = ref->next) { -if (!ref->peer_ref && !args->send_mirror) +if (!ref_update_to_be_sent (ref, args))continue ; -/* Check for statuses set by set_ref_status_for_push() */ -switch (ref->status) { -case REF_STATUS_REJECT_NONFASTFORWARD : -case REF_STATUS_REJECT_ALREADY_EXISTS : -case REF_STATUS_REJECT_FETCH_FIRST : -case REF_STATUS_REJECT_NEEDS_FORCE : -case REF_STATUS_REJECT_STALE : -case REF_STATUS_REJECT_NODELETE : -case REF_STATUS_UPTODATE : -continue ; -default : - ;/* do nothing */ - } -if (!ref->deletion) new_refs++;
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 7/19 send-pack: always send capabilities
In reply to Junio C HamanoWe tried to avoid sending one extra byte, NUL and nothing behind it to signal there is no protocol capabilities being sent, on the first command packet on the wire, but it just made the code look ugly.
--- send-pack.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
send-pack.c +1 -3
@@ -281,8 +281,7 @@ int send_pack(struct send_pack_args *args,char *new_hex =sha1_to_hex (ref->new_sha1);int quiet = quiet_supported && (args->quiet || !args->progress); -if (!cmds_sent && (status_report || use_sideband || - quiet || agent_supported)) { +if (!cmds_sent) packet_buf_write(&req_buf, "%s %s %s%c%s%s%s%s%s", old_hex, new_hex, ref->name, 0,
@@ -292,7 +291,6 @@ int send_pack(struct send_pack_args *args, agent_supported ?" agent=" :"" , agent_supported ?git_user_agent_sanitized () :"" ); - } else packet_buf_write(&req_buf, "%s %s %s", old_hex, new_hex, ref->name);
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 8/19 send-pack: factor out capability string generation
In reply to Junio C HamanoA run of 'var ? " var" : ""' fed to a long printf string in a deeply nested block was hard to read. Move it outside the loop and format it into a strbuf.
As an added bonus, the trick to add "agent=<agent-name>" by using two conditionals is replaced by a more readable version.
--- send-pack.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
send-pack.c +13 -8
@@ -218,6 +218,7 @@ int send_pack(struct send_pack_args *args,int in = fd[0 ];int out = fd[1 ];struct strbuf req_buf =STRBUF_INIT ; +struct strbuf cap_buf =STRBUF_INIT ;struct ref *ref;int new_refs;int allow_deleting_refs =0 ;
@@ -251,6 +252,15 @@ int send_pack(struct send_pack_args *args,return 0 ; } +if (status_report) +strbuf_addstr (&cap_buf," report-status" ); +if (use_sideband) +strbuf_addstr (&cap_buf," side-band-64k" ); +if (quiet_supported && (args->quiet || !args->progress)) +strbuf_addstr (&cap_buf," quiet" ); +if (agent_supported) +strbuf_addf (&cap_buf," agent=%s" ,git_user_agent_sanitized ()); +/* * NEEDSWORK: why is delete-refs so specific to send-pack * machinery that set_ref_status_for_push() cannot set this
@@ -279,18 +289,12 @@ int send_pack(struct send_pack_args *args, }else {char *old_hex =sha1_to_hex (ref->old_sha1);char *new_hex =sha1_to_hex (ref->new_sha1); -int quiet = quiet_supported && (args->quiet || !args->progress);if (!cmds_sent)packet_buf_write (&req_buf, -"%s %s %s%c%s%s%s%s%s" , +"%s %s %s%c%s" , old_hex, new_hex, ref->name,0 , - status_report ?" report-status" :"" , - use_sideband ?" side-band-64k" :"" , - quiet ?" quiet" :"" , - agent_supported ?" agent=" :"" , - agent_supported ?git_user_agent_sanitized () :"" - ); + cap_buf.buf);else packet_buf_write (&req_buf,"%s %s %s" , old_hex, new_hex, ref->name);
@@ -311,6 +315,7 @@ int send_pack(struct send_pack_args *args,packet_flush (out); }strbuf_release (&req_buf); +strbuf_release (&cap_buf);if (use_sideband && cmds_sent) {memset (&demux,0 ,sizeof (demux));
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 9/19 send-pack: rename "new_refs" to "need_pack_data"
In reply to Junio C HamanoThe variable counts how many non-deleting command is being sent, but is only checked with 0-ness to decide if we need to send the pack data.
--- send-pack.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
send-pack.c +3 -4
@@ -220,7 +220,7 @@ int send_pack(struct send_pack_args *args,struct strbuf req_buf =STRBUF_INIT ;struct strbuf cap_buf =STRBUF_INIT ;struct ref *ref; -int new_refs; +int need_pack_data =0 ;int allow_deleting_refs =0 ;int status_report =0 ;int use_sideband =0 ;
@@ -276,13 +276,12 @@ int send_pack(struct send_pack_args *args,/* * Finally, tell the other end! */ - new_refs =0 ;for (ref = remote_refs; ref; ref = ref->next) {if (!ref_update_to_be_sent (ref, args))continue ;if (!ref->deletion) - new_refs++; + need_pack_data =1 ;if (args->dry_run) { ref->status =REF_STATUS_OK ;
@@ -327,7 +326,7 @@ int send_pack(struct send_pack_args *args, in = demux.out; } -if (new_refs && cmds_sent) { +if (need_pack_data && cmds_sent) {if (pack_objects (out, remote_refs, extra_have, args) <0 ) {for (ref = remote_refs; ref; ref = ref->next) ref->status =REF_STATUS_NONE ;
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 10/19 send-pack: refactor inspecting and resetting status and sending commands
In reply to Junio C HamanoThe main loop over remote_refs list inspects the ref status to see if we need to generate pack data (i.e. a delete-only push does not need to send any additional data), resets it to "expecting the status report" state, and formats the actual update commands to be sent.
Split the former two out of the main loop, as it will become conditional in later steps.
Besides, we should have code that does real thing here, before the "Finally, tell the other end!" part ;-)
--- send-pack.c | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-)
send-pack.c +30 -19
@@ -274,7 +274,8 @@ int send_pack(struct send_pack_args *args,advertise_shallow_grafts_buf (&req_buf);/* -* Finally, tell the other end! +* Clear the status for each ref and see if we need to send +* the pack data. */ for (ref = remote_refs; ref; ref = ref->next) {if (!ref_update_to_be_sent (ref, args))
@@ -283,25 +284,35 @@ int send_pack(struct send_pack_args *args,if (!ref->deletion) need_pack_data =1 ; -if (args->dry_run) { +if (args->dry_run || !status_report) ref->status =REF_STATUS_OK ; - }else { -char *old_hex =sha1_to_hex (ref->old_sha1); -char *new_hex =sha1_to_hex (ref->new_sha1); - -if (!cmds_sent) -packet_buf_write (&req_buf, -"%s %s %s%c%s" , - old_hex, new_hex, ref->name,0 , - cap_buf.buf); -else -packet_buf_write (&req_buf,"%s %s %s" , - old_hex, new_hex, ref->name); - ref->status = status_report ? -REF_STATUS_EXPECTING_REPORT : -REF_STATUS_OK ; - cmds_sent++; - } +else + ref->status =REF_STATUS_EXPECTING_REPORT ; + } + +/* +* Finally, tell the other end! +*/ +for (ref = remote_refs; ref; ref = ref->next) { +char *old_hex, *new_hex; + +if (args->dry_run) +continue ; + +if (!ref_update_to_be_sent (ref, args)) +continue ; + + old_hex =sha1_to_hex (ref->old_sha1); + new_hex =sha1_to_hex (ref->new_sha1); +if (!cmds_sent) +packet_buf_write (&req_buf, +"%s %s %s%c%s" , + old_hex, new_hex, ref->name,0 , + cap_buf.buf); +else +packet_buf_write (&req_buf,"%s %s %s" , + old_hex, new_hex, ref->name); + cmds_sent++; }if (args->stateless_rpc) {
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 11/19 send-pack: clarify that cmds_sent is a boolean
In reply to Junio C HamanoWe use it to make sure that the feature request is sent only once on the very first request packet (ignoring the "shallow " line, which was an unfortunate mistake we cannot retroactively fix with existing receive-pack already deployed in the field) and we set it to "true" with cmds_sent++, not because we care about the actual number of updates sent but because it is merely an old idiomatic way.
Set it explicitly to one to clarify that the code that uses this variable only cares about its zero-ness.
--- send-pack.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
send-pack.c +4 -3
@@ -304,15 +304,16 @@ int send_pack(struct send_pack_args *args, old_hex =sha1_to_hex (ref->old_sha1); new_hex =sha1_to_hex (ref->new_sha1); -if (!cmds_sent) +if (!cmds_sent) {packet_buf_write (&req_buf,"%s %s %s%c%s" , old_hex, new_hex, ref->name,0 , cap_buf.buf); -else + cmds_sent =1 ; + }else {packet_buf_write (&req_buf,"%s %s %s" , old_hex, new_hex, ref->name); - cmds_sent++; + } }if (args->stateless_rpc) {
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 12/19 gpg-interface: move parse_gpg_output() to where it should be
In reply to Junio C HamanoEarlier, ffb6d7d5 (Move commit GPG signature verification to commit.c, 2013-03-31) moved this helper that used to be in pretty.c (i.e. the output code path) to commit.c for better reusability.
It was a good first step in the right direction, but still suffers a myopic view that commits will be the only thing we would ever want to sign---we would actually want to be able to reuse it even wider.
The function interprets what GPG said; gpg-interface is obviously a better place. Move it there.
--- commit.c | 36 ------------------------------------ gpg-interface.c | 36 ++++++++++++++++++++++++++++++++++++ gpg-interface.h | 17 ++++++++++++----- 3 files changed, 48 insertions(+), 41 deletions(-)
commit.c +0 -36
@@ -1220,42 +1220,6 @@ free_return:free (buf); } -static struct { -char result; -const char *check; -} sigcheck_gpg_status[] = { - {'G' ,"\n[GNUPG:] GOODSIG " }, - {'B' ,"\n[GNUPG:] BADSIG " }, - {'U' ,"\n[GNUPG:] TRUST_NEVER" }, - {'U' ,"\n[GNUPG:] TRUST_UNDEFINED" }, -}; - -static void parse_gpg_output (struct signature_check *sigc) -{ -const char *buf = sigc->gpg_status; -int i; - -/* Iterate over all search strings */ -for (i =0 ; i <ARRAY_SIZE (sigcheck_gpg_status); i++) { -const char *found, *next; - -if (!skip_prefix (buf, sigcheck_gpg_status[i].check +1 , &found)) { - found =strstr (buf, sigcheck_gpg_status[i].check); -if (!found) -continue ; - found +=strlen (sigcheck_gpg_status[i].check); - } - sigc->result = sigcheck_gpg_status[i].result; -/* The trust messages are not followed by key/signer information */ -if (sigc->result !='U' ) { - sigc->key =xmemdupz (found,16 ); - found +=17 ; - next =strchrnul (found,'\n' ); - sigc->signer =xmemdupz (found, next - found); - } - } -} -void check_commit_signature (const struct commit * commit,struct signature_check *sigc) {struct strbuf payload =STRBUF_INIT ;
gpg-interface.c +36 -0
@@ -21,6 +21,42 @@ void signature_check_clear(struct signature_check *sigc) sigc->key =NULL ; } +static struct { +char result; +const char *check; +} sigcheck_gpg_status[] = { + {'G' ,"\n[GNUPG:] GOODSIG " }, + {'B' ,"\n[GNUPG:] BADSIG " }, + {'U' ,"\n[GNUPG:] TRUST_NEVER" }, + {'U' ,"\n[GNUPG:] TRUST_UNDEFINED" }, +}; + +void parse_gpg_output (struct signature_check *sigc) +{ +const char *buf = sigc->gpg_status; +int i; + +/* Iterate over all search strings */ +for (i =0 ; i <ARRAY_SIZE (sigcheck_gpg_status); i++) { +const char *found, *next; + +if (!skip_prefix (buf, sigcheck_gpg_status[i].check +1 , &found)) { + found =strstr (buf, sigcheck_gpg_status[i].check); +if (!found) +continue ; + found +=strlen (sigcheck_gpg_status[i].check); + } + sigc->result = sigcheck_gpg_status[i].result; +/* The trust messages are not followed by key/signer information */ +if (sigc->result !='U' ) { + sigc->key =xmemdupz (found,16 ); + found +=17 ; + next =strchrnul (found,'\n' ); + sigc->signer =xmemdupz (found, next - found); + } + } +} +void set_signing_key (const char *key) {free (configured_signing_key);
gpg-interface.h +12 -5
@@ -5,16 +5,23 @@ struct signature_check {char *payload;char *gpg_output;char *gpg_status; -char result;/* 0 (not checked), -* N (checked but no further result), -* U (untrusted good), -* G (good) -* B (bad) */ + +/* +* possible "result": +* 0 (not checked) +* N (checked but no further result) +* U (untrusted good) +* G (good) +* B (bad) +*/ +char result;char *signer;char *key; };extern void signature_check_clear (struct signature_check *sigc); +extern void parse_gpg_output (struct signature_check *); +extern int sign_buffer (struct strbuf *buffer,struct strbuf *signature,const char *signing_key);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);extern int git_gpg_config (const char *,const char *,void *);
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 13/19 gpg-interface: move parse_signature() to where it should be
In reply to Junio C HamanoOur signed-tag objects set the standard format used by Git to store GPG-signed payload (i.e. the payload followed by its detached signature), and it made sense to have a helper to find the boundary between the payload and its signature in tag.c back then.
Newer code added later to parse other kinds of objects that learned to use the same format to store GPG-signed payload (e.g. signed commits), however, kept using the helper from the same location.
Move it to gpg-interface; the helper is no longer about signed tag, but it is how our code and data interact with GPG.
--- gpg-interface.c | 21 +++++++++++++++++++++ gpg-interface.h | 1 + tag.c | 20 -------------------- tag.h | 1 - 4 files changed, 22 insertions(+), 21 deletions(-)
gpg-interface.c +21 -0
@@ -7,6 +7,9 @@static char *configured_signing_key;static const char *gpg_program ="gpg" ; +#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----" +#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----" +void signature_check_clear (struct signature_check *sigc) {free (sigc->payload);
@@ -57,6 +60,24 @@ void parse_gpg_output(struct signature_check *sigc) } } +/* +* Look at GPG signed content (e.g. a signed tag object), whose +* payload is followed by a detached signature on it. Return the +* offset where the embedded detached signature begins, or the end of +* the data when there is no such signature. +*/ +size_t parse_signature (const char *buf,unsigned long size) +{ +char *eol; +size_t len =0 ; +while (len < size && !starts_with (buf + len,PGP_SIGNATURE ) && + !starts_with (buf + len,PGP_MESSAGE )) { + eol =memchr (buf + len,'\n' , size - len); + len += eol ? eol - (buf + len) +1 : size - len; + } +return len; +} +void set_signing_key (const char *key) {free (configured_signing_key);
gpg-interface.h +1 -0
@@ -20,6 +20,7 @@ struct signature_check { };extern void signature_check_clear (struct signature_check *sigc); +extern size_t parse_signature (const char *buf,unsigned long size);extern void parse_gpg_output (struct signature_check *);extern int sign_buffer (struct strbuf *buffer,struct strbuf *signature,const char *signing_key);
tag.c +0 -20
@@ -4,9 +4,6 @@#include "tree.h" #include "blob.h" -#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----" -#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----" -const char *tag_type ="tag" ;struct object *deref_tag (struct object *o,const char *warn,int warnlen)
@@ -143,20 +140,3 @@ int parse_tag(struct tag *item)free (data);return ret; } - -/* -* Look at a signed tag object, and return the offset where -* the embedded detached signature begins, or the end of the -* data when there is no such signature. -*/ -size_t parse_signature (const char *buf,unsigned long size) -{ -char *eol; -size_t len =0 ; -while (len < size && !starts_with (buf + len,PGP_SIGNATURE ) && - !starts_with (buf + len,PGP_MESSAGE )) { - eol =memchr (buf + len,'\n' , size - len); - len += eol ? eol - (buf + len) +1 : size - len; - } -return len; -}
tag.h +0 -1
@@ -17,6 +17,5 @@ extern int parse_tag_buffer(struct tag *item, const void *data, unsigned long siextern int parse_tag (struct tag *item);extern struct object *deref_tag (struct object *,const char *,int );extern struct object *deref_tag_noverify (struct object *); -extern size_t parse_signature (const char *buf,unsigned long size);#endif /* TAG_H */
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 14/19 pack-protocol doc: typofix for PKT-LINE
In reply to Junio C HamanoEverywhere else we use PKT-LINE to denote the pkt-line formatted data, but "shallow/deepen" messages are described with PKT_LINE().
Fix them.
--- Documentation/technical/pack-protocol.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/technical/pack-protocol.txt +2 -2
@@ -212,9 +212,9 @@ out of what the server said it could do with the first 'want' line. want-list = first-want *additional-want - shallow-line = PKT_LINE("shallow" SP obj-id) + shallow-line = PKT-LINE("shallow" SP obj-id) - depth-request = PKT_LINE("deepen" SP depth) + depth-request = PKT-LINE("deepen" SP depth) first-want = PKT-LINE("want" SP obj-id SP capability-list LF) additional-want = PKT-LINE("want" SP obj-id LF)
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 15/19 the beginning of the signed push
In reply to Junio C HamanoWhile signed tags and commits assert that the objects thusly signed came from you, who signed these objects, there is not a good way to assert that you wanted to have a particular object at the tip of a particular branch. My signing v2.0.1 tag only means I want to call the version v2.0.1, and it does not mean I want to push it out to my 'master' branch---it is likely that I only want it in 'maint'.
Introduce a mechanism that allows you to sign a "push certificate" (for the lack of better name) every time you push, asserting that what object you are pushing to update which ref that used to point at what other object. Think of it as a cryptographic protection for ref updates, similar to signed tags/commits but working on an orthogonal axis.
The basic flow based on this mechanism goes like this:
1. You push out your work with "git push --signed".
2. The sending side learns where the remote refs are as usual, together with what protocol extension the receiving end supports. If the receiving end does not advertise the protocol extension "push-cert", an attempt to "git push --signed" fails.
Otherwise, a text file, that looks like the following, is prepared in core:
certificate version 0.1
pusher Junio C Hamano <[email protected]> 1315427886 -0700
7339ca65... 21580ecb... refs/heads/master
3793ac56... 12850bec... refs/heads/next
The file begins with a few header lines, which may grow as we gain more experience. The 'pusher' header records the name of the signer (the value of user.signingkey configuration variable, falling back to GIT_COMMITTER_{NAME|EMAIL}) and the time of the certificate generation. After the header, a blank line follows, followed by a copy of the protocol message lines.
Each line shows the old and the new object name at the tip of the ref this push tries to update, in the way identical to how the underlying "git push" protocol exchange tells the ref updates to the receiving end (by recording the "old" object name, the push certificate also protects against replaying). It is expected that new command packet types other than the old-new-refname kind will be included in push certificate in the same way as would appear in the plain vanilla command packets in unsigned pushes.
The user then is asked to sign this push certificate using GPG, formatted in a way similar to how signed tag objects are signed, and the result is sent to the other side (i.e. receive-pack).
In the protocol exchange, this step comes immediately before the sender tells what the result of the push should be, which in turn comes before it sends the pack data.
3. When the receiving end sees a push certificate, the certificate is written out as a blob. The pre-receive hook can learn about the certificate by checking GIT_PUSH_CERT environment variable, which, if present, tells the object name of this blob, and make the decision to allow or reject this push. Additionally, the post-receive hook can also look at the certificate, which may be a good place to log all the received certificates for later audits.
Because a push certificate carry the same information as the usual command packets in the protocol exchange, we can omit the latter when a push certificate is in use and reduce the protocol overhead. This however is not included in this patch to make it easier to review (in other words, the series at this step should never be released without the remainder of the series, as it implements an interim protocol that will be incompatible with the final one, merely for reviewing purposes). As such, the documentation update for the protocol is left out of this step.
--- Documentation/git-push.txt | 9 +++++- Documentation/git-receive-pack.txt | 16 +++++++++- builtin/push.c | 1 + builtin/receive-pack.c | 43 ++++++++++++++++++++++++- send-pack.c | 64 ++++++++++++++++++++++++++++++++++++++ send-pack.h | 1 + t/t5534-push-signed.sh | 63 +++++++++++++++++++++++++++++++++++++ transport.c | 4 +++ transport.h | 5 +++ 9 files changed, 203 insertions(+), 3 deletions(-) create mode 100755 t/t5534-push-signed.sh
Documentation/git-push.txt +8 -1
@@ -10,7 +10,8 @@ SYNOPSIS -------- [verse] 'git push' [--all | --mirror | --tags] [--follow-tags] [-n | --dry-run] [--receive-pack=<git-receive-pack>] - [--repo=<repository>] [-f | --force] [--prune] [-v | --verbose] [-u | --set-upstream] + [--repo=<repository>] [-f | --force] [--prune] [-v | --verbose] + [-u | --set-upstream] [--signed] [--force-with-lease[=<refname>[:<expect>]]] [--no-verify] [<repository> [<refspec>...]]
@@ -129,6 +130,12 @@ already exists on the remote side. from the remote but are pointing at commit-ish that are reachable from the refs being pushed. +--signed:: + GPG-sign the push request to update refs on the receiving + side, to allow it to be checked by the hooks and/or be + logged. See linkgit:git-receive-pack[1] for the details + on the receiving end. + --receive-pack=<git-receive-pack>:: --exec=<git-receive-pack>:: Path to the 'git-receive-pack' program on the remote
Documentation/git-receive-pack.txt +15 -1
@@ -53,6 +53,11 @@ the update. Refs to be created will have sha1-old equal to 0\{40}, while refs to be deleted will have sha1-new equal to 0\{40}, otherwise sha1-old and sha1-new should be valid objects in the repository. +When accepting a signed push (see linkgit:git-push[1]), the signed +push certificate is stored in a blob and an environment variable +`GIT_PUSH_CERT` can be consulted for its object name. See the +description of `post-receive` hook for an example. + This hook is called before any refname is updated and before any fast-forward checks are performed.
@@ -101,9 +106,13 @@ the update. Refs that were created will have sha1-old equal to 0\{40}, otherwise sha1-old and sha1-new should be valid objects in the repository. +The `GIT_PUSH_CERT` environment variable can be inspected, just as +in `pre-receive` hook, after accepting a signed push. + Using this hook, it is easy to generate mails describing the updates to the repository. This example script sends one mail message per -ref listing the commits pushed to the repository: +ref listing the commits pushed to the repository, and logs the push +certificates of signed pushes to a file: #!/bin/sh # mail out commit update information.
@@ -119,6 +128,11 @@ ref listing the commits pushed to the repository: fi | mail -s "Changes to ref $ref" commit-list@mydomain done + # log signed push certificate, if any + if test -n "${GIT_PUSH_CERT-}" + then + git cat-file blob ${GIT_PUSH_CERT} >>/var/log/push-log + fi exit 0 The exit code from this hook invocation is ignored, however a
builtin/push.c +1 -0
@@ -506,6 +506,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)OPT_BIT (0 ,"no-verify" , &flags,N_ ("bypass pre-push hook" ),TRANSPORT_PUSH_NO_HOOK ),OPT_BIT (0 ,"follow-tags" , &flags,N_ ("push missing but relevant tags" ),TRANSPORT_PUSH_FOLLOW_TAGS ), +OPT_BIT (0 ,"signed" , &flags,N_ ("GPG sign the push" ),TRANSPORT_PUSH_CERT ), OPT_END() };
builtin/receive-pack.c +42 -1
73 lines not on this page · raw message
send-pack.c +64 -0
88 lines not on this page · raw message
send-pack.h +1 -0
@@ -11,6 +11,7 @@ struct send_pack_args { use_thin_pack:1, use_ofs_delta:1, dry_run:1, + push_cert:1, stateless_rpc:1; };
t/t5534-push-signed.sh new file +63 -0
63 lines not on this page · raw message
transport.c +4 -0
@@ -480,6 +480,9 @@ static int set_git_option(struct git_transport_options *opts,die ("transport: invalid depth option '%s'" , value); }return 0 ; + }else if (!strcmp (name,TRANS_OPT_PUSH_CERT )) { + opts->push_cert = !!value; +return 0 ; }return 1 ; }
@@ -823,6 +826,7 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re args.progress = transport->progress; args.dry_run = !!(flags &TRANSPORT_PUSH_DRY_RUN ); args.porcelain = !!(flags &TRANSPORT_PUSH_PORCELAIN ); + args.push_cert = !!(flags &TRANSPORT_PUSH_CERT ); ret =send_pack (&args, data->fd, data->conn, remote_refs, &data->extra_have);
transport.h +5 -0
@@ -12,6 +12,7 @@ struct git_transport_options { unsigned check_self_contained_and_connected : 1; unsigned self_contained_and_connected : 1; unsigned update_shallow : 1; + unsigned push_cert : 1;int depth;const char *uploadpack;const char *receivepack;
@@ -123,6 +124,7 @@ struct transport {#define TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND 256#define TRANSPORT_PUSH_NO_HOOK 512#define TRANSPORT_PUSH_FOLLOW_TAGS 1024 +#define TRANSPORT_PUSH_CERT 2048#define TRANSPORT_SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)#define TRANSPORT_SUMMARY (x) (int)(TRANSPORT_SUMMARY_WIDTH + strlen(x) - gettext_width(x)), (x)
@@ -156,6 +158,9 @@ struct transport *transport_get(struct remote *, const char *);/* Accept refs that may update .git/shallow without --depth */ #define TRANS_OPT_UPDATE_SHALLOW "updateshallow" +/* Send push certificates */ +#define TRANS_OPT_PUSH_CERT "pushcert" +/** * Returns 0 if the option was used, non-zero otherwise. Prints a * message to stderr if the option is not used.
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 16/19 receive-pack: GPG-validate push certificates
In reply to Junio C HamanoReusing the GPG signature check helpers we already have, verify the signature in receive-pack and give the results to the hooks via GIT_PUSH_CERT_{SIGNER,KEY,STATUS} environment variables.
Policy decisions, such as accepting or rejecting a good signature by a key that is not fully trusted, is left to the hook and kept outside of the core.
--- Documentation/git-receive-pack.txt | 27 ++++++++++++++++++++++----- builtin/receive-pack.c | 29 +++++++++++++++++++++++++++++ t/t5534-push-signed.sh | 18 ++++++++++++++++-- 3 files changed, 67 insertions(+), 7 deletions(-)
Documentation/git-receive-pack.txt +22 -5
@@ -56,7 +56,21 @@ sha1-old and sha1-new should be valid objects in the repository. When accepting a signed push (see linkgit:git-push[1]), the signed push certificate is stored in a blob and an environment variable `GIT_PUSH_CERT` can be consulted for its object name. See the -description of `post-receive` hook for an example. +description of `post-receive` hook for an example. In addition, the +certificate is verified using GPG and the result is exported with +the following environment variables: + +GIT_PUSH_CERT_SIGNER:: + The name and the e-mail address of the owner of the key that + signed the push certificate. + +GIT_PUSH_CERT_KEY:: + The GPG key ID of the key that signed the push certificate. + +GIT_PUSH_CERT_STATUS:: + The status of GPG verification of the push certificate, + using the same mnemonic as used in `%G?` format of `git log` + family of commands (see linkgit:git-log[1]). This hook is called before any refname is updated and before any fast-forward checks are performed.
@@ -106,13 +120,14 @@ the update. Refs that were created will have sha1-old equal to 0\{40}, otherwise sha1-old and sha1-new should be valid objects in the repository. -The `GIT_PUSH_CERT` environment variable can be inspected, just as +The `GIT_PUSH_CERT*` environment variables can be inspected, just as in `pre-receive` hook, after accepting a signed push. Using this hook, it is easy to generate mails describing the updates to the repository. This example script sends one mail message per ref listing the commits pushed to the repository, and logs the push -certificates of signed pushes to a file: +certificates of signed pushes with good signatures to a logger +service: #!/bin/sh # mail out commit update information.
@@ -129,9 +144,11 @@ certificates of signed pushes to a file: mail -s "Changes to ref $ref" commit-list@mydomain done # log signed push certificate, if any - if test -n "${GIT_PUSH_CERT-}" + if test -n "${GIT_PUSH_CERT-}" && test ${GIT_PUSH_CERT_STATUS} = G then - git cat-file blob ${GIT_PUSH_CERT} >>/var/log/push-log + ( + git cat-file blob ${GIT_PUSH_CERT} + ) | mail -s "push from $GIT_PUSH_CERT_SIGNER" push-log@mydomain fi exit 0
builtin/receive-pack.c +29 -0
@@ -15,6 +15,8 @@#include "connected.h" #include "argv-array.h" #include "version.h" +#include "tag.h" +#include "gpg-interface.h" static const char receive_pack_usage[] ="git receive-pack <git-dir>" ;
@@ -48,6 +50,7 @@ static int shallow_update;static const char *alt_shallow_file;static struct strbuf push_cert =STRBUF_INIT ;static unsigned char push_cert_sha1[20 ]; +static struct signature_check sigcheck;static enum deny_action parse_deny_action (const char *var,const char *value) {
@@ -260,12 +263,38 @@ static void prepare_push_cert_sha1(struct child_process *proc)struct argv_array env =ARGV_ARRAY_INIT ;if (!already_done) { +struct strbuf gpg_output =STRBUF_INIT ; +struct strbuf gpg_status =STRBUF_INIT ; +int bogs/* beginning_of_gpg_sig */ ; + already_done =1 ;if (write_sha1_file (push_cert.buf, push_cert.len,"blob" , push_cert_sha1))hashclr (push_cert_sha1); + +memset (&sigcheck,'\0' ,sizeof (sigcheck)); + sigcheck.result ='N' ; + + bogs =parse_signature (push_cert.buf, push_cert.len); +if (verify_signed_buffer (push_cert.buf, bogs, + push_cert.buf + bogs, push_cert.len - bogs, + &gpg_output, &gpg_status) <0 ) { + ;/* error running gpg */ + }else { + sigcheck.payload = push_cert.buf; + sigcheck.gpg_output = gpg_output.buf; + sigcheck.gpg_status = gpg_status.buf; +parse_gpg_output (&sigcheck); + } + +strbuf_release (&gpg_output); +strbuf_release (&gpg_status); }if (!is_null_sha1 (push_cert_sha1)) {argv_array_pushf (&env,"GIT_PUSH_CERT=%s" ,sha1_to_hex (push_cert_sha1)); +argv_array_pushf (&env,"GIT_PUSH_CERT_SIGNER=%s" , sigcheck.signer); +argv_array_pushf (&env,"GIT_PUSH_CERT_KEY=%s" , sigcheck.key); +argv_array_pushf (&env,"GIT_PUSH_CERT_STATUS=%c" , sigcheck.result); + proc->env = env.argv; } }
t/t5534-push-signed.sh +16 -2
@@ -52,12 +52,26 @@ test_expect_success GPG 'signed push sends push certificate' ' if test -n "${GIT_PUSH_CERT-}" then git cat-file blob $GIT_PUSH_CERT >../push-cert - fi + fi && + + cat >../push-cert-status <<E_O_F + SIGNER=${GIT_PUSH_CERT_SIGNER-nobody} + KEY=${GIT_PUSH_CERT_KEY-nokey} + STATUS=${GIT_PUSH_CERT_STATUS-nostatus} + E_O_F + + EOF + + cat >expect <<-\EOF && + SIGNER=C O Mitter <[email protected]> + KEY=13B6F51ECDDE430D + STATUS=G EOF git push --signed dst noop ff +noff && grep "$(git rev-parse noop ff) refs/heads/ff" dst/push-cert && - grep "$(git rev-parse noop noff) refs/heads/noff" dst/push-cert + grep "$(git rev-parse noop noff) refs/heads/noff" dst/push-cert && + test_cmp expect dst/push-cert-status ' test_done
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 17/19 send-pack: send feature request on push-cert packet
In reply to Junio C HamanoWe would want to update the interim protocol so that we do not send the usual update commands when the push certificate feature is in use, as the same information is in the certificate. Once that happens, the push-cert packet may become the only protocol command, but then there is no packet to put the feature request behind, like we always did.
As we have prepared the receiving end that understands the push-cert feature to accept the feature request on the first protocol packet (other than "shallow ", which was an unfortunate historical mistake that has to come before everything else), we can give the feature request on the push-cert packet instead of the first update protocol packet, in preparation for the next step to actually update to the final protocol.
--- send-pack.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
send-pack.c +8 -5
@@ -225,9 +225,10 @@ static const char *next_line(const char *line, size_t len)return nl +1 ; } -static void generate_push_cert (struct strbuf *req_buf, -const struct ref *remote_refs, -struct send_pack_args *args) +static int generate_push_cert (struct strbuf *req_buf, +const struct ref *remote_refs, +struct send_pack_args *args, +const char *cap_string) {const struct ref *ref;char stamp[60 ];
@@ -256,7 +257,7 @@ static void generate_push_cert(struct strbuf *req_buf,if (sign_buffer (&cert, &cert, signing_key))die (_ ("failed to sign the push certificate" )); -packet_buf_write (req_buf,"push-cert\n" ); +packet_buf_write (req_buf,"push-cert%c%s" ,0 , cap_string);for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) { np =next_line (cp, cert.buf + cert.len - cp); packet_buf_write(req_buf,
@@ -267,6 +268,7 @@ static void generate_push_cert(struct strbuf *req_buf, free_return:free (signing_key);strbuf_release (&cert); +return 1 ; } int send_pack(struct send_pack_args *args,
@@ -335,7 +337,8 @@ int send_pack(struct send_pack_args *args,advertise_shallow_grafts_buf (&req_buf);if (!args->dry_run && args->push_cert) -generate_push_cert (&req_buf, remote_refs, args); + cmds_sent =generate_push_cert (&req_buf, remote_refs, args, + cap_buf.buf);/* * Clear the status for each ref and see if we need to send
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 18/19 signed push: remove duplicated protocol info
In reply to Junio C HamanoWith the interim protocol, we used to send the update commands even though we already send a signed copy of the same information when push certificate is in use. Update the send-pack/receive-pack pair not to do so.
The notable thing on the receive-pack side is that it makes sure that there is no command sent over the traditional protocol packet outside the push certificate. Otherwise a pusher can claim to be pushing one set of ref updates in the signed certificate while issuing commands to update unrelated refs, and such an update will evade later audits.
--- Documentation/technical/pack-protocol.txt | 20 ++++++++++++++- Documentation/technical/protocol-capabilities.txt | 12 +++++++-- builtin/receive-pack.c | 30 +++++++++++++++++++++-- send-pack.c | 2 +- 4 files changed, 58 insertions(+), 6 deletions(-)
Documentation/technical/pack-protocol.txt +19 -1
@@ -465,7 +465,7 @@ contain all the objects that the server will need to complete the new references. ---- - update-request = *shallow command-list [pack-file] + update-request = *shallow ( command-list | push-cert ) [pack-file] shallow = PKT-LINE("shallow" SP obj-id)
@@ -481,12 +481,30 @@ references. old-id = obj-id new-id = obj-id + push-cert = PKT-LINE("push-cert" NUL capability-list LF) + PKT-LINE("certificate version 0.1" LF) + PKT-LINE("pusher" ident LF) + PKT-LINE(LF) + *PKT-LINE(command LF) + *PKT-LINE(GPG signature lines LF) + PKT-LINE("push-cert-end" LF) + pack-file = "PACK" 28*(OCTET) ---- If the receiving end does not support delete-refs, the sending end MUST NOT ask for delete command. +If the receiving end does not support push-cert, the sending end MUST +NOT send a push-cert command. + +When a push-cert command is sent, command-list MUST NOT be sent; the +commands recorded in the push certificate is used instead. The GPG +signature lines are a detached signature for the contents recorded in +the push certificate before the signature block begins and are used +to certify that the commands were given by the pusher, who must be +the signer. + The pack-file MUST NOT be sent if the only command used is 'delete'. A pack-file MUST be sent if either create or update command is used,
Documentation/technical/protocol-capabilities.txt +10 -2
@@ -18,8 +18,8 @@ was sent. Server MUST NOT ignore capabilities that client requested and server advertised. As a consequence of these rules, server MUST NOT advertise capabilities it does not understand. -The 'report-status', 'delete-refs', and 'quiet' capabilities are sent and -recognized by the receive-pack (push to server) process. +The 'report-status', 'delete-refs', 'quiet', and 'push-cert' capabilities +are sent and recognized by the receive-pack (push to server) process. The 'ofs-delta' and 'side-band-64k' capabilities are sent and recognized by both upload-pack and receive-pack protocols. The 'agent' capability
@@ -250,3 +250,11 @@ allow-tip-sha1-in-want If the upload-pack server advertises this capability, fetch-pack may send "want" lines with SHA-1s that exist at the server but are not advertised by upload-pack. + +push-cert +--------- + +The receive-pack server that advertises this capability is willing +to accept a signed push certificate. A send-pack client MUST NOT +send a push-cert packet unless the receive-pack server advertises +this capability.
builtin/receive-pack.c +28 -2
@@ -880,7 +880,7 @@ static void execute_commands(struct command *commands, "the reported refs above"); } -static struct command **queue_command (struct command **p, +static struct command **queue_command (struct command **tail,const char *line,int linelen) {
@@ -903,10 +903,32 @@ static struct command **queue_command(struct command **p,hashcpy (cmd->new_sha1, new_sha1);memcpy (cmd->ref_name, refname, reflen); cmd->ref_name[reflen] ='\0' ; - *p = cmd; + *tail = cmd;return &cmd->next; } +static void queue_commands_from_cert (struct command **tail, +struct strbuf *push_cert) +{ +const char *boc, *eoc; + +if (*tail) +die ("protocol error: got both push certificate and unsigned commands" ); + + boc =strstr (push_cert->buf,"\n\n" ); +if (!boc) +die ("malformed push certificate %.*s" ,100 , push_cert->buf); +else + boc +=2 ; + eoc = push_cert->buf +parse_signature (push_cert->buf, push_cert->len); + +while (boc < eoc) { +const char *eol =memchr (boc,'\n' , eoc - boc); + tail =queue_command (tail, boc, eol ? eol - boc : eoc - eol); + boc = eol ? eol +1 : eoc; + } +} +static struct command *read_head_info (struct sha1_array *shallow) {struct command *commands =NULL ;
@@ -962,6 +984,10 @@ static struct command *read_head_info(struct sha1_array *shallow) p =queue_command (p, line, linelen); } + +if (push_cert.len) +queue_commands_from_cert (p, &push_cert); +return commands; }
send-pack.c +1 -1
@@ -363,7 +363,7 @@ int send_pack(struct send_pack_args *args,for (ref = remote_refs; ref; ref = ref->next) {char *old_hex, *new_hex; -if (args->dry_run) +if (args->dry_run || args->push_cert)continue ;if (!ref_update_to_be_sent (ref, args))
Signature · 1 line
2.1.0-304-g950f846
PATCH v2 19/19 signed push: fortify against replay attacks
In reply to Junio C HamanoIn order to prevent a valid push certificate for pushing into an repository from getting replayed to push to an unrelated one, send a nonce string from the receive-pack process and have the signer include it in the push certificate. The original nonce is exported as GIT_PUSH_CERT_NONCE for the hooks to examine and match against the value on the "nonce" header in the certificate to notice a replay.
Because the built-in nonce generation may not be suitable for all situations, allow the server to invoke receive-pack with pregenerated nonce from the command line argument.
--- Documentation/git-receive-pack.txt | 8 +++++ Documentation/technical/pack-protocol.txt | 5 +-- Documentation/technical/protocol-capabilities.txt | 7 ++-- builtin/receive-pack.c | 42 ++++++++++++++++++++--- send-pack.c | 19 +++++++--- t/t5534-push-signed.sh | 17 +++++---- 6 files changed, 78 insertions(+), 20 deletions(-)
Documentation/git-receive-pack.txt +8 -0
@@ -72,6 +72,13 @@ GIT_PUSH_CERT_STATUS:: using the same mnemonic as used in `%G?` format of `git log` family of commands (see linkgit:git-log[1]). +GIT_PUSH_CERT_NONCE:: + The nonce string the process asked the signer to include + in the push certificate. If this does not match the value + recorded on the "nonce" header in the push certificate, it + may indicate that the certificate is a valid one that is + being replayed from a separate "git push" session. + This hook is called before any refname is updated and before any fast-forward checks are performed.
@@ -147,6 +154,7 @@ service: if test -n "${GIT_PUSH_CERT-}" && test ${GIT_PUSH_CERT_STATUS} = G then ( + echo expected nonce is ${GIT_PUSH_NONCE} git cat-file blob ${GIT_PUSH_CERT} ) | mail -s "push from $GIT_PUSH_CERT_SIGNER" push-log@mydomain fi
Documentation/technical/pack-protocol.txt +3 -2
@@ -483,10 +483,11 @@ references. push-cert = PKT-LINE("push-cert" NUL capability-list LF) PKT-LINE("certificate version 0.1" LF) - PKT-LINE("pusher" ident LF) + PKT-LINE("pusher" SP ident LF) + PKT-LINE("nonce" SP nonce LF) PKT-LINE(LF) *PKT-LINE(command LF) - *PKT-LINE(GPG signature lines LF) + *PKT-LINE(detached GPG signature lines LF) PKT-LINE("push-cert-end" LF) pack-file = "PACK" 28*(OCTET)
Documentation/technical/protocol-capabilities.txt +4 -3
@@ -251,10 +251,11 @@ If the upload-pack server advertises this capability, fetch-pack may send "want" lines with SHA-1s that exist at the server but are not advertised by upload-pack. -push-cert ---------- +push-cert=<nonce> +----------------- The receive-pack server that advertises this capability is willing -to accept a signed push certificate. A send-pack client MUST NOT +to accept a signed push certificate, and asks the <nonce> to be +included in the push certificate. A send-pack client MUST NOT send a push-cert packet unless the receive-pack server advertises this capability.
builtin/receive-pack.c +37 -5
93 lines not on this page · raw message
send-pack.c +15 -4
@@ -228,7 +228,8 @@ static const char *next_line(const char *line, size_t len)static int generate_push_cert (struct strbuf *req_buf,const struct ref *remote_refs,struct send_pack_args *args, -const char *cap_string) +const char *cap_string, +const char *push_cert_nonce) {const struct ref *ref;char stamp[60 ];
@@ -240,6 +241,8 @@ static int generate_push_cert(struct strbuf *req_buf,datestamp (stamp,sizeof (stamp));strbuf_addf (&cert,"certificate version 0.1\n" );strbuf_addf (&cert,"pusher %s %s\n" , signing_key, stamp); +if (push_cert_nonce[0 ]) +strbuf_addf (&cert,"nonce %s\n" , push_cert_nonce);strbuf_addstr (&cert,"\n" );for (ref = remote_refs; ref; ref = ref->next) {
@@ -290,6 +293,8 @@ int send_pack(struct send_pack_args *args,unsigned cmds_sent =0 ;int ret;struct async demux; +const char *push_cert_nonce =NULL ; +/* Does the other end support the reporting? */ if (server_supports ("report-status" ))
@@ -306,8 +311,14 @@ int send_pack(struct send_pack_args *args, agent_supported =1 ;if (server_supports ("no-thin" )) args->use_thin_pack =0 ; -if (args->push_cert && !server_supports ("push-cert" )) -die (_ ("the receiving end does not support --signed push" )); +if (args->push_cert) { +int len; + + push_cert_nonce =server_feature_value ("push-cert" , &len); +if (!push_cert_nonce) +die (_ ("the receiving end does not support --signed push" )); + push_cert_nonce =xmemdupz (push_cert_nonce, len); + }if (!remote_refs) { fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
@@ -338,7 +349,7 @@ int send_pack(struct send_pack_args *args,if (!args->dry_run && args->push_cert) cmds_sent =generate_push_cert (&req_buf, remote_refs, args, - cap_buf.buf); + cap_buf.buf, push_cert_nonce);/* * Clear the status for each ref and see if we need to send
t/t5534-push-signed.sh +11 -6
@@ -58,17 +58,22 @@ test_expect_success GPG 'signed push sends push certificate' ' SIGNER=${GIT_PUSH_CERT_SIGNER-nobody} KEY=${GIT_PUSH_CERT_KEY-nokey} STATUS=${GIT_PUSH_CERT_STATUS-nostatus} + NONCE=${GIT_PUSH_CERT_NONCE-nononce} E_O_F EOF - cat >expect <<-\EOF && - SIGNER=C O Mitter <[email protected]> - KEY=13B6F51ECDDE430D - STATUS=G - EOF - git push --signed dst noop ff +noff && + + ( + cat <<-\EOF && + SIGNER=C O Mitter <[email protected]> + KEY=13B6F51ECDDE430D + STATUS=G + EOF + sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" dst/push-cert + ) >expect && + grep "$(git rev-parse noop ff) refs/heads/ff" dst/push-cert && grep "$(git rev-parse noop noff) refs/heads/noff" dst/push-cert && test_cmp expect dst/push-cert-status
Signature · 1 line
2.1.0-304-g950f846
Re: [PATCH v2 19/19] signed push: fortify against replay attacks
Quoted 18 lines · Junio C Hamano
In order to prevent a valid push certificate for pushing into an repository from getting replayed to push to an unrelated one, send a nonce string from the receive-pack process and have the signer include it in the push certificate. The original nonce is exported as
GIT_PUSH_CERT_NONCEfor the hooks to examine and match against the value on the "nonce" header in the certificate to notice a replay.Because the built-in nonce generation may not be suitable for all situations, allow the server to invoke receive-pack with pregenerated nonce from the command line argument.
Signed-off-by Junio C Hamano <[email protected]>---
builtin/receive-pack.c +0
-0
builtin/receive-pack.c static int delete_only(struct command *commands) · line 1241
Quoted 10 lines
builtin/receive-pack.c +7
-0@@ -1226,12 +1232,28 @@ static int delete_only(struct command *commands)return 1 ; } +static char *prepare_push_cert_nonce (const char *sitename,const char *dir) +{ +struct strbuf buf =STRBUF_INIT ; +unsigned char sha1[20 ]; + +if (!sitename) { +static char buf[1024 ];
Potentially confusing 'buf' shadows 'buf' in outer scope.
builtin/receive-pack.c static int delete_only(struct command *commands) · line 1246
Quoted 5 lines
builtin/receive-pack.c +5
-0+gethostname (buf,sizeof (buf)); + sitename = buf; + } +strbuf_addf (&buf,"%s:%s:%lu" , sitename, dir,time (NULL )); +hash_sha1_file (buf.buf, buf.len,"blob" , sha1);
strbuf_release(&buf);
Quoted 141 lines
builtin/receive-pack.c +14
-0+return xstrdup (sha1_to_hex (sha1)); +} +int cmd_receive_pack (int argc,const char **argv,const char *prefix) {int advertise_refs =0 ;int stateless_rpc =0 ;int i;const char *dir =NULL ; +const char *sitename =NULL ;struct command *commands;struct sha1_array shallow =SHA1_ARRAY_INIT ;struct sha1_array ref =SHA1_ARRAY_INIT ;@@ -1261,6 +1283,13 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) fix_thin =0 ;continue ; } +if (skip_prefix (arg,"--sitename=" , &sitename)) { +continue ; + } +if (skip_prefix (arg,"--push-cert-nonce=" , &push_cert_nonce)) { + push_cert_nonce =xstrdup (push_cert_nonce); +continue ; + }usage (receive_pack_usage); }@@ -1277,6 +1306,8 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)die ("'%s' does not appear to be a git repository" , dir);git_config (receive_pack_config,NULL ); +if (!push_cert_nonce) + push_cert_nonce =prepare_push_cert_nonce (sitename, dir);if (0 <= transfer_unpack_limit) unpack_limit = transfer_unpack_limit;@@ -1321,5 +1352,6 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)packet_flush (1 );sha1_array_clear (&shallow);sha1_array_clear (&ref); +free ((void *)push_cert_nonce);return 0 ; }send-pack.c +15
-4@@ -228,7 +228,8 @@ static const char *next_line(const char *line, size_t len)static int generate_push_cert (struct strbuf *req_buf,const struct ref *remote_refs,struct send_pack_args *args, -const char *cap_string) +const char *cap_string, +const char *push_cert_nonce) {const struct ref *ref;char stamp[60 ];@@ -240,6 +241,8 @@ static int generate_push_cert(struct strbuf *req_buf,datestamp (stamp,sizeof (stamp));strbuf_addf (&cert,"certificate version 0.1\n" );strbuf_addf (&cert,"pusher %s %s\n" , signing_key, stamp); +if (push_cert_nonce[0 ]) +strbuf_addf (&cert,"nonce %s\n" , push_cert_nonce);strbuf_addstr (&cert,"\n" );for (ref = remote_refs; ref; ref = ref->next) {@@ -290,6 +293,8 @@ int send_pack(struct send_pack_args *args,unsigned cmds_sent =0 ;int ret;struct async demux; +const char *push_cert_nonce =NULL ; +/* Does the other end support the reporting? */ if (server_supports ("report-status" ))@@ -306,8 +311,14 @@ int send_pack(struct send_pack_args *args, agent_supported =1 ;if (server_supports ("no-thin" )) args->use_thin_pack =0 ; -if (args->push_cert && !server_supports ("push-cert" )) -die (_ ("the receiving end does not support --signed push" )); +if (args->push_cert) { +int len; + + push_cert_nonce =server_feature_value ("push-cert" , &len); +if (!push_cert_nonce) +die (_ ("the receiving end does not support --signed push" )); + push_cert_nonce =xmemdupz (push_cert_nonce, len); + }if (!remote_refs) { fprintf(stderr, "No refs in common and none specified; doing nothing.\n"@@ -338,7 +349,7 @@ int send_pack(struct send_pack_args *args,if (!args->dry_run && args->push_cert) cmds_sent =generate_push_cert (&req_buf, remote_refs, args, - cap_buf.buf); + cap_buf.buf, push_cert_nonce);/* * Clear the status for each ref and see if we need to send t/t5534-push-signed.sh +11
-7@@ -58,17 +58,22 @@ test_expect_success GPG 'signed push sends push certificate' ' SIGNER=${GIT_PUSH_CERT_SIGNER-nobody} KEY=${GIT_PUSH_CERT_KEY-nokey} STATUS=${GIT_PUSH_CERT_STATUS-nostatus} + NONCE=${GIT_PUSH_CERT_NONCE-nononce} E_O_F EOF - cat >expect <<-\EOF && - SIGNER=C O Mitter <[email protected]> - KEY=13B6F51ECDDE430D - STATUS=G - EOF - git push --signed dst noop ff +noff && + + ( + cat <<-\EOF && + SIGNER=C O Mitter <[email protected]> + KEY=13B6F51ECDDE430D + STATUS=G + EOF + sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" dst/push-cert + ) >expect && + grep "$(git rev-parse noop ff) refs/heads/ff" dst/push-cert && grep "$(git rev-parse noop noff) refs/heads/noff" dst/push-cert && test_cmp expect dst/push-cert-status --2.1.0-304-g950f846
--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v2 19/19] signed push: fortify against replay attacks
In reply to Junio C HamanoQuoted 9 lines · Junio C Hamano
builtin/receive-pack.c +5
-0@@ -1226,12 +1232,28 @@ static int delete_only(struct command *commands)return 1 ; } +static char *prepare_push_cert_nonce (const char *sitename,const char *dir) +{ +struct strbuf buf =STRBUF_INIT ; +unsigned char sha1[20 ]; +
builtin/receive-pack.c static int delete_only(struct command *commands) · line 1249
Quoted 10 lines
builtin/receive-pack.c +10
-0+if (!sitename) { +static char buf[1024 ]; +gethostname (buf,sizeof (buf)); + sitename = buf; + } +strbuf_addf (&buf,"%s:%s:%lu" , sitename, dir,time (NULL )); +hash_sha1_file (buf.buf, buf.len,"blob" , sha1); +return xstrdup (sha1_to_hex (sha1)); +} +
On every other use of gethostname within git.git we're checking the return code. And if gethostname fails, we're either copying in 'localhost' or 'unknown' instead.
Does that make sense here as well?
Found by coverity scan.
Thanks,
Stefan
Re: [PATCH v2 19/19] signed push: fortify against replay attacks
Quoted 25 lines · Stefan Beller
Quoted 9 lines · Junio C Hamano
builtin/receive-pack.c +5
-0@@ -1226,12 +1232,28 @@ static int delete_only(struct command *commands)return 1 ; } +static char *prepare_push_cert_nonce (const char *sitename,const char *dir) +{ +struct strbuf buf =STRBUF_INIT ; +unsigned char sha1[20 ]; +builtin/receive-pack.c static int delete_only(struct command *commands) · line 1249
Quoted 10 lines
builtin/receive-pack.c +10
-0+if (!sitename) { +static char buf[1024 ]; +gethostname (buf,sizeof (buf)); + sitename = buf; + } +strbuf_addf (&buf,"%s:%s:%lu" , sitename, dir,time (NULL )); +hash_sha1_file (buf.buf, buf.len,"blob" , sha1); +return xstrdup (sha1_to_hex (sha1)); +} +On every other use of gethostname within git.git we're checking the return code. And if gethostname fails, we're either copying in 'localhost' or 'unknown' instead.
Quoted 1 line
Does that make sense here as well?
It does, but I think this code will have to change quite a lot before it gets ready even for 'next'.
Thanks.