7
7
#include "tempfile.h"
8
8
9
9
static char * configured_signing_key ;
10
- static const char * gpg_format = "openpgp" ;
11
- static const char * gpg_program = "gpg" ;
10
+ struct gpg_format {
11
+ const char * name ;
12
+ const char * program ;
13
+ const char * * extra_args_verify ;
14
+ const char * * sigs ;
15
+ };
16
+
17
+ static const char * openpgp_verify_args [] = { "--keyid-format=long" , NULL };
18
+ static const char * openpgp_sigs [] = {
19
+ "-----BEGIN PGP SIGNATURE-----" ,
20
+ "-----BEGIN PGP MESSAGE-----" , NULL };
21
+
22
+ static struct gpg_format gpg_formats [] = {
23
+ { .name = "openpgp" , .program = "gpg" ,
24
+ .extra_args_verify = openpgp_verify_args ,
25
+ .sigs = openpgp_sigs
26
+ },
27
+ };
28
+ static struct gpg_format * current_format = & gpg_formats [0 ];
29
+
30
+ static struct gpg_format * get_format_by_name (const char * str )
31
+ {
32
+ int i ;
33
+
34
+ for (i = 0 ; i < ARRAY_SIZE (gpg_formats ); i ++ )
35
+ if (!strcasecmp (gpg_formats [i ].name , str ))
36
+ return gpg_formats + i ;
37
+ return NULL ;
38
+ }
12
39
13
- #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
14
- #define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
40
+ static struct gpg_format * get_format_by_sig (const char * sig )
41
+ {
42
+ int i , j ;
43
+
44
+ for (i = 0 ; i < ARRAY_SIZE (gpg_formats ); i ++ )
45
+ for (j = 0 ; gpg_formats [i ].sigs [j ]; j ++ )
46
+ if (starts_with (sig , gpg_formats [i ].sigs [j ]))
47
+ return gpg_formats + i ;
48
+ return NULL ;
49
+ }
15
50
16
51
void signature_check_clear (struct signature_check * sigc )
17
52
{
@@ -102,20 +137,14 @@ void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
102
137
fputs (output , stderr );
103
138
}
104
139
105
- static int is_gpg_start (const char * line )
106
- {
107
- return starts_with (line , PGP_SIGNATURE ) ||
108
- starts_with (line , PGP_MESSAGE );
109
- }
110
-
111
140
size_t parse_signature (const char * buf , size_t size )
112
141
{
113
142
size_t len = 0 ;
114
143
size_t match = size ;
115
144
while (len < size ) {
116
145
const char * eol ;
117
146
118
- if (is_gpg_start (buf + len ))
147
+ if (get_format_by_sig (buf + len ))
119
148
match = len ;
120
149
121
150
eol = memchr (buf + len , '\n' , size - len );
@@ -132,6 +161,9 @@ void set_signing_key(const char *key)
132
161
133
162
int git_gpg_config (const char * var , const char * value , void * cb )
134
163
{
164
+ struct gpg_format * fmt = NULL ;
165
+ char * fmtname = NULL ;
166
+
135
167
if (!strcmp (var , "user.signingkey" )) {
136
168
if (!value )
137
169
return config_error_nonbool (var );
@@ -140,18 +172,23 @@ int git_gpg_config(const char *var, const char *value, void *cb)
140
172
}
141
173
142
174
if (!strcmp (var , "gpg.format" )) {
143
- if (value && strcasecmp (value , "openpgp" ))
144
- return error ("malformed value for %s: %s" , var , value );
145
- return git_config_string (& gpg_format , var , value );
146
- }
147
-
148
- if (!strcmp (var , "gpg.program" )) {
149
175
if (!value )
150
176
return config_error_nonbool (var );
151
- gpg_program = xstrdup (value );
177
+ fmt = get_format_by_name (value );
178
+ if (!fmt )
179
+ return error ("malformed value for %s: %s" , var , value );
180
+ current_format = fmt ;
152
181
return 0 ;
153
182
}
154
183
184
+ if (!strcmp (var , "gpg.program" ))
185
+ fmtname = "openpgp" ;
186
+
187
+ if (fmtname ) {
188
+ fmt = get_format_by_name (fmtname );
189
+ return git_config_string (& fmt -> program , var , value );
190
+ }
191
+
155
192
return 0 ;
156
193
}
157
194
@@ -170,7 +207,7 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig
170
207
struct strbuf gpg_status = STRBUF_INIT ;
171
208
172
209
argv_array_pushl (& gpg .args ,
173
- gpg_program ,
210
+ current_format -> program ,
174
211
"--status-fd=2" ,
175
212
"-bsau" , signing_key ,
176
213
NULL );
@@ -208,6 +245,7 @@ int verify_signed_buffer(const char *payload, size_t payload_size,
208
245
struct strbuf * gpg_output , struct strbuf * gpg_status )
209
246
{
210
247
struct child_process gpg = CHILD_PROCESS_INIT ;
248
+ struct gpg_format * fmt ;
211
249
struct tempfile * temp ;
212
250
int ret ;
213
251
struct strbuf buf = STRBUF_INIT ;
@@ -223,10 +261,14 @@ int verify_signed_buffer(const char *payload, size_t payload_size,
223
261
return -1 ;
224
262
}
225
263
264
+ fmt = get_format_by_sig (signature );
265
+ if (!fmt )
266
+ BUG ("bad signature '%s'" , signature );
267
+
268
+ argv_array_push (& gpg .args , fmt -> program );
269
+ argv_array_pushv (& gpg .args , fmt -> extra_args_verify );
226
270
argv_array_pushl (& gpg .args ,
227
- gpg_program ,
228
271
"--status-fd=1" ,
229
- "--keyid-format=long" ,
230
272
"--verify" , temp -> filename .buf , "-" ,
231
273
NULL );
232
274
0 commit comments