Skip to content

Commit 86ebf87

Browse files
jltoblergitster
authored andcommitted
gpg-interface: allow sign_buffer() to use default signing key
The `sign_commit_to_strbuf()` helper in "commit.c" provides fallback logic to get the default configured signing key when a key is not provided and handles generating the commit signature accordingly. This signing operation is not really specific to commits as any arbitrary buffer can be signed. Also, in a subsequent commit, this same logic is reused by git-fast-import(1) when signing commits with invalid signatures. Remove the `sign_commit_to_strbuf()` helper from "commit.c" and extend `sign_buffer()` in "gpg-interface.c" to support using the default key as a fallback when the `SIGN_BUFFER_USE_DEFAULT_KEY` flag is provided. Call sites are updated accordingly. Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 233545c commit 86ebf87

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

builtin/tag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static int do_sign(struct strbuf *buffer, struct object_id **compat_oid,
167167
char *keyid = get_signing_key();
168168
int ret = -1;
169169

170-
if (sign_buffer(buffer, &sig, keyid))
170+
if (sign_buffer(buffer, &sig, keyid, 0))
171171
goto out;
172172

173173
if (compat) {
@@ -176,7 +176,7 @@ static int do_sign(struct strbuf *buffer, struct object_id **compat_oid,
176176
if (convert_object_file(the_repository ,&compat_buf, algo, compat,
177177
buffer->buf, buffer->len, OBJ_TAG, 1))
178178
goto out;
179-
if (sign_buffer(&compat_buf, &compat_sig, keyid))
179+
if (sign_buffer(&compat_buf, &compat_sig, keyid, 0))
180180
goto out;
181181
add_header_signature(&compat_buf, &sig, algo);
182182
strbuf_addbuf(&compat_buf, &compat_sig);

commit.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,18 +1170,6 @@ int add_header_signature(struct strbuf *buf, struct strbuf *sig, const struct gi
11701170
return 0;
11711171
}
11721172

1173-
static int sign_commit_to_strbuf(struct strbuf *sig, struct strbuf *buf, const char *keyid)
1174-
{
1175-
char *keyid_to_free = NULL;
1176-
int ret = 0;
1177-
if (!keyid || !*keyid)
1178-
keyid = keyid_to_free = get_signing_key();
1179-
if (sign_buffer(buf, sig, keyid))
1180-
ret = -1;
1181-
free(keyid_to_free);
1182-
return ret;
1183-
}
1184-
11851173
int parse_signed_commit(const struct commit *commit,
11861174
struct strbuf *payload, struct strbuf *signature,
11871175
const struct git_hash_algo *algop)
@@ -1759,7 +1747,8 @@ int commit_tree_extended(const char *msg, size_t msg_len,
17591747
oidcpy(&parent_buf[i++], &p->item->object.oid);
17601748

17611749
write_commit_tree(&buffer, msg, msg_len, tree, parent_buf, nparents, author, committer, extra);
1762-
if (sign_commit && sign_commit_to_strbuf(&sig, &buffer, sign_commit)) {
1750+
if (sign_commit && sign_buffer(&buffer, &sig, sign_commit,
1751+
SIGN_BUFFER_USE_DEFAULT_KEY)) {
17631752
result = -1;
17641753
goto out;
17651754
}
@@ -1791,7 +1780,9 @@ int commit_tree_extended(const char *msg, size_t msg_len,
17911780
free_commit_extra_headers(compat_extra);
17921781
free(mapped_parents);
17931782

1794-
if (sign_commit && sign_commit_to_strbuf(&compat_sig, &compat_buffer, sign_commit)) {
1783+
if (sign_commit && sign_buffer(&compat_buffer, &compat_sig,
1784+
sign_commit,
1785+
SIGN_BUFFER_USE_DEFAULT_KEY)) {
17951786
result = -1;
17961787
goto out;
17971788
}

gpg-interface.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,11 +974,20 @@ const char *gpg_trust_level_to_str(enum signature_trust_level level)
974974
return sigcheck_gpg_trust_level[level].display_key;
975975
}
976976

977-
int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
977+
int sign_buffer(struct strbuf *buffer, struct strbuf *signature,
978+
const char *signing_key, enum sign_buffer_flags flags)
978979
{
980+
char *keyid_to_free = NULL;
981+
int ret = 0;
982+
979983
gpg_interface_lazy_init();
980984

981-
return use_format->sign_buffer(buffer, signature, signing_key);
985+
if ((flags & SIGN_BUFFER_USE_DEFAULT_KEY) && (!signing_key || !*signing_key))
986+
signing_key = keyid_to_free = get_signing_key();
987+
988+
ret = use_format->sign_buffer(buffer, signature, signing_key);
989+
free(keyid_to_free);
990+
return ret;
982991
}
983992

984993
/*

gpg-interface.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,23 @@ int parse_signature(const char *buf, size_t size, struct strbuf *payload, struct
7474
*/
7575
size_t parse_signed_buffer(const char *buf, size_t size);
7676

77+
/* Flags for sign_buffer(). */
78+
enum sign_buffer_flags {
79+
/*
80+
* Use the default configured signing key as returned by `get_signing_key()`
81+
* when the provided "signing_key" is NULL or empty.
82+
*/
83+
SIGN_BUFFER_USE_DEFAULT_KEY = (1 << 0),
84+
};
85+
7786
/*
7887
* Create a detached signature for the contents of "buffer" and append
7988
* it after "signature"; "buffer" and "signature" can be the same
8089
* strbuf instance, which would cause the detached signature appended
8190
* at the end. Returns 0 on success, non-zero on failure.
8291
*/
8392
int sign_buffer(struct strbuf *buffer, struct strbuf *signature,
84-
const char *signing_key);
85-
93+
const char *signing_key, enum sign_buffer_flags flags);
8694

8795
/*
8896
* Returns corresponding string in lowercase for a given member of

send-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ static int generate_push_cert(struct strbuf *req_buf,
391391
if (!update_seen)
392392
goto free_return;
393393

394-
if (sign_buffer(&cert, &cert, signing_key))
394+
if (sign_buffer(&cert, &cert, signing_key, 0))
395395
die(_("failed to sign the push certificate"));
396396

397397
packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);

0 commit comments

Comments
 (0)