Skip to content

Commit 6d2a655

Browse files
committed
Merge branch 'bt/gpg-interface'
What is queued here is only the obviously correct and uncontroversial code clean-up part, which is an earlier 7 patches, of a larger series. The remainder that is not queued introduces a few configuration variables to deal with e-signature backends with different signature format. * bt/gpg-interface: gpg-interface: find the last gpg signature line gpg-interface: extract gpg line matching helper gpg-interface: fix const-correctness of "eol" pointer gpg-interface: use size_t for signature buffer size gpg-interface: modernize function declarations gpg-interface: handle bool user.signingkey t7004: fix mistaken tag name
2 parents 6c0110f + 8b44b2b commit 6d2a655

File tree

3 files changed

+71
-34
lines changed

3 files changed

+71
-34
lines changed

gpg-interface.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,26 @@ void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
101101
fputs(output, stderr);
102102
}
103103

104-
/*
105-
* Look at GPG signed content (e.g. a signed tag object), whose
106-
* payload is followed by a detached signature on it. Return the
107-
* offset where the embedded detached signature begins, or the end of
108-
* the data when there is no such signature.
109-
*/
110-
size_t parse_signature(const char *buf, unsigned long size)
104+
static int is_gpg_start(const char *line)
105+
{
106+
return starts_with(line, PGP_SIGNATURE) ||
107+
starts_with(line, PGP_MESSAGE);
108+
}
109+
110+
size_t parse_signature(const char *buf, size_t size)
111111
{
112-
char *eol;
113112
size_t len = 0;
114-
while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
115-
!starts_with(buf + len, PGP_MESSAGE)) {
113+
size_t match = size;
114+
while (len < size) {
115+
const char *eol;
116+
117+
if (is_gpg_start(buf + len))
118+
match = len;
119+
116120
eol = memchr(buf + len, '\n', size - len);
117121
len += eol ? eol - (buf + len) + 1 : size - len;
118122
}
119-
return len;
123+
return match;
120124
}
121125

122126
void set_signing_key(const char *key)
@@ -128,13 +132,19 @@ void set_signing_key(const char *key)
128132
int git_gpg_config(const char *var, const char *value, void *cb)
129133
{
130134
if (!strcmp(var, "user.signingkey")) {
135+
if (!value)
136+
return config_error_nonbool(var);
131137
set_signing_key(value);
138+
return 0;
132139
}
140+
133141
if (!strcmp(var, "gpg.program")) {
134142
if (!value)
135143
return config_error_nonbool(var);
136144
gpg_program = xstrdup(value);
145+
return 0;
137146
}
147+
138148
return 0;
139149
}
140150

@@ -145,12 +155,6 @@ const char *get_signing_key(void)
145155
return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
146156
}
147157

148-
/*
149-
* Create a detached signature for the contents of "buffer" and append
150-
* it after "signature"; "buffer" and "signature" can be the same
151-
* strbuf instance, which would cause the detached signature appended
152-
* at the end.
153-
*/
154158
int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
155159
{
156160
struct child_process gpg = CHILD_PROCESS_INIT;
@@ -192,11 +196,6 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig
192196
return 0;
193197
}
194198

195-
/*
196-
* Run "gpg" to see if the payload matches the detached signature.
197-
* gpg_output, when set, receives the diagnostic output from GPG.
198-
* gpg_status, when set, receives the status output from GPG.
199-
*/
200199
int verify_signed_buffer(const char *payload, size_t payload_size,
201200
const char *signature, size_t signature_size,
202201
struct strbuf *gpg_output, struct strbuf *gpg_status)

gpg-interface.h

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,43 @@ struct signature_check {
2323
char *key;
2424
};
2525

26-
extern void signature_check_clear(struct signature_check *sigc);
27-
extern size_t parse_signature(const char *buf, unsigned long size);
28-
extern void parse_gpg_output(struct signature_check *);
29-
extern int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key);
30-
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);
31-
extern int git_gpg_config(const char *, const char *, void *);
32-
extern void set_signing_key(const char *);
33-
extern const char *get_signing_key(void);
34-
extern int check_signature(const char *payload, size_t plen,
35-
const char *signature, size_t slen, struct signature_check *sigc);
36-
void print_signature_buffer(const struct signature_check *sigc, unsigned flags);
26+
void signature_check_clear(struct signature_check *sigc);
27+
28+
/*
29+
* Look at GPG signed content (e.g. a signed tag object), whose
30+
* payload is followed by a detached signature on it. Return the
31+
* offset where the embedded detached signature begins, or the end of
32+
* the data when there is no such signature.
33+
*/
34+
size_t parse_signature(const char *buf, size_t size);
35+
36+
void parse_gpg_output(struct signature_check *);
37+
38+
/*
39+
* Create a detached signature for the contents of "buffer" and append
40+
* it after "signature"; "buffer" and "signature" can be the same
41+
* strbuf instance, which would cause the detached signature appended
42+
* at the end.
43+
*/
44+
int sign_buffer(struct strbuf *buffer, struct strbuf *signature,
45+
const char *signing_key);
46+
47+
/*
48+
* Run "gpg" to see if the payload matches the detached signature.
49+
* gpg_output, when set, receives the diagnostic output from GPG.
50+
* gpg_status, when set, receives the status output from GPG.
51+
*/
52+
int verify_signed_buffer(const char *payload, size_t payload_size,
53+
const char *signature, size_t signature_size,
54+
struct strbuf *gpg_output, struct strbuf *gpg_status);
55+
56+
int git_gpg_config(const char *, const char *, void *);
57+
void set_signing_key(const char *);
58+
const char *get_signing_key(void);
59+
int check_signature(const char *payload, size_t plen,
60+
const char *signature, size_t slen,
61+
struct signature_check *sigc);
62+
void print_signature_buffer(const struct signature_check *sigc,
63+
unsigned flags);
3764

3865
#endif

t/t7004-tag.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,18 @@ test_expect_success GPG \
10561056
git tag -s -F sigblanknonlfile blanknonlfile-signed-tag &&
10571057
get_tag_msg blanknonlfile-signed-tag >actual &&
10581058
test_cmp expect actual &&
1059-
git tag -v signed-tag
1059+
git tag -v blanknonlfile-signed-tag
1060+
'
1061+
1062+
test_expect_success GPG 'signed tag with embedded PGP message' '
1063+
cat >msg <<-\EOF &&
1064+
-----BEGIN PGP MESSAGE-----
1065+
1066+
this is not a real PGP message
1067+
-----END PGP MESSAGE-----
1068+
EOF
1069+
git tag -s -F msg confusing-pgp-message &&
1070+
git tag -v confusing-pgp-message
10601071
'
10611072

10621073
# messages with commented lines for signed tags:

0 commit comments

Comments
 (0)