-
Notifications
You must be signed in to change notification settings - Fork 9
/
scd.c
398 lines (336 loc) · 9.5 KB
/
scd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* Copyright (C) 2015-2018 SektionEins GmbH / Ben Fuhrmannek
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// for asprintf on linux
#define _GNU_SOURCE
#include "common.h"
#include "scd.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gcrypt.h>
#include <locale.h>
// for asprintf with not too old libgpg-error
#ifdef GPGRT_VERSION
#define asprintf gpgrt_asprintf
#endif
#ifdef _WIN32
static char* stpcpy(char *dst, char *src)
{
strcpy(dst, src);
return dst + strlen(src);
}
#endif
struct sec_signature {
uchar *pSignature;
unsigned long *pulSignatureLen;
};
static gpg_error_t find_gpg_socket(char *buf, size_t len)
{
char *tmp, *t, *ext = NULL;
// try GPG_AGENT_INFO which looks like /home/user/.gnupg/S.gpg-agent:1234:1
// note: GPG_AGENT_INFO is not set for gnupg >= 2.1
tmp = getenv("GPG_AGENT_INFO");
if (tmp) {
t = strchr(tmp, ':');
if (t) {
if (t-tmp + 1 > len)
return 1;
strncpy(buf, tmp, t-tmp);
buf[t-tmp] = '\0';
return 0;
}
}
#ifndef _WIN32
// use $GNUPGHOME/S.gpg-agent if $GNUPGHOME is set
tmp = getenv("GNUPGHOME");
if (tmp) {
ext = "/S.gpg-agent";
if (strlen(tmp) + strlen(ext) + 1 > len)
return 1;
t = stpcpy(buf, tmp);
stpcpy(t, ext);
return 0;
}
// fall back to $HOME/.gnupg/S.gpg-agent
tmp = getenv("HOME");
if (tmp) {
ext = "/.gnupg/S.gpg-agent";
if (strlen(tmp) + strlen(ext) + 1 > len)
return 1;
t = stpcpy(buf, tmp);
stpcpy(t, ext);
return 0;
}
#else // _WIN32
tmp = getenv("APPDATA");
if (tmp) {
ext = "/gnupg/S.gpg-agent";
if (strlen(tmp) + strlen(ext) + 1 > len)
return 1;
t = stpcpy(buf, tmp);
stpcpy(t, ext);
return 0;
}
#endif
return 1;
}
static inline int is_optstr_clean(char *str)
{
while (*str != 0) {
if (*str < 32) { return 0; }
str++;
}
return 1;
}
gpg_error_t scd_set_option(assuan_context_t ctx, char *key, char *value)
{
if (!key || *key == 0) {
SDEBUG("OPTION key not set");
return 1;
}
if (!value) {
SDEBUG("OPTION value not set for key '%s'", key);
return 1;
}
if (!is_optstr_clean(value) || !is_optstr_clean(key)) {
SDEBUG("OPTION key or value contains illegal characters for key '%s'", key);
return 1;
}
char *cmd;
if (asprintf(&cmd, "OPTION %s=%s", key, value) < 0) {
SDEBUG("OPITON out of memory for key '%s'", key);
return 1;
}
gpg_error_t err;
err = assuan_transact(ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL);
if (err) {
sec_log_err("error setting OPTION '%s=%s'", key, value);
}
free(cmd);
return err;
}
gpg_error_t scd_agent_connect(assuan_context_t *ctx)
{
gpg_error_t err;
char gpg_agent_socket_name[1024];
if (ctx == NULL) { return 1; }
if (*ctx != NULL) { return 0; }
err = find_gpg_socket(gpg_agent_socket_name, 1024);
if (err) {
SDEBUG("Could not find agent path, check env. variables");
return err;
}
err = assuan_new(ctx);
if (err) { return err; }
err = assuan_socket_connect(*ctx, gpg_agent_socket_name, ASSUAN_INVALID_PID, 0);
if (err) {
assuan_release(*ctx);
*ctx = NULL;
SDEBUG("Could not connect to agent via socket: '%s'", gpg_agent_socket_name);
return err;
}
#ifndef _WIN32
// set options. ignore errors - see debug log for debugging problems
char *val = NULL;
val = getenv("GPG_TTY");
if (!val) {
val = getenv("TTY");
}
if (!val) {
val = ttyname(0);
}
scd_set_option(*ctx, "ttyname", val);
#endif
scd_set_option(*ctx, "display", getenv("DISPLAY"));
scd_set_option(*ctx, "ttytype", getenv("TERM"));
scd_set_option(*ctx, "lc-ctype", setlocale(LC_CTYPE, NULL));
#ifndef _WIN32
scd_set_option(*ctx, "lc-messages", setlocale(LC_MESSAGES, NULL));
#endif
scd_set_option(*ctx, "xauthority", getenv("XAUTHORITY"));
scd_set_option(*ctx, "pinentry-user-data", getenv("PINENTRY_USER_DATA"));
scd_set_option(*ctx, "use-cache-for-signing", getenv("GPG_USE_CACHE_FOR_SIGNING"));
scd_set_option(*ctx, "allow-pinentry-notify", getenv("GPG_ALLOW_PINENTRY_NOTIFY"));
scd_set_option(*ctx, "pinentry-mode", getenv("PINENTRY_MODE"));
scd_set_option(*ctx, "cache-ttl-opt-preset", getenv("GPG_CACHE_TTL_OPT_PRESET"));
scd_set_option(*ctx, "s2k-count", getenv("GPG_S2K_COUNT"));
return 0;
}
void scd_agent_disconnect(assuan_context_t ctx)
{
assuan_release(ctx);
}
gpg_error_t scd_serialno_openpgp(assuan_context_t ctx)
{
gpg_error_t err;
err = assuan_transact(ctx, "SCD SERIALNO openpgp", NULL, NULL, NULL, NULL, NULL, NULL);
SDEBUG("[%u] token %spresent", err, err? "not ":"");
return err;
}
int scd_token_present(assuan_context_t ctx)
{
return !scd_serialno_openpgp(ctx);
}
#define ISHEX(p) ((*(p) >= '0' && *(p) <= '9') \
|| (*(p) >= 'a' && *(p) <= 'f') \
|| (*(p) >= 'F' && *(p) <= 'F'))
#define HEXC2I(p) (*(p) <= '9' ? *(p) - '0' :\
*(p) <= 'F' ? *(p) - 'A' + 10 :\
*(p) - 'a' + 10)
#define HEXC2I2(p) (16 * HEXC2I(p) + HEXC2I((p)+1))
gpg_error_t scd_unescape_data(uchar *out, size_t *poutlen, uchar *data, size_t datalen)
{
gpg_error_t rv = 0;
uchar *pin = data, *pout = out;
int countmode = 0; // if buffer is too short, return required buffer size in *poutlen
SDEBUG("maxlen=%zu datalen=%zu", *poutlen, datalen);
while (pin < data + datalen) {
if (!countmode && pout >= out + *poutlen) {
rv = GPG_ERR_BUFFER_TOO_SHORT;
countmode = 1;
// break;
}
if (*pin == '%' && pout + 2 < data + datalen && ISHEX(pout + 1) && ISHEX(pout + 2)) {
if (!countmode) {
*pout = HEXC2I2(pout+1);
SDEBUG("c=%c", *pout);
}
pin += 2;
} else {
if (!countmode) *pout = *pin;
}
pin++; pout++;
}
*poutlen = pout - out;
return rv;
}
gpg_error_t scd_copy_data(uchar *out, size_t *poutlen, uchar *data, size_t datalen)
{
gpg_error_t rv = 0;
if (*poutlen < datalen) {
rv = GPG_ERR_BUFFER_TOO_SHORT;
} else {
memcpy(out, data, datalen);
}
*poutlen = datalen;
return rv;
}
static gpg_error_t sign_data_cb(void *arg, const void *data, size_t datalen)
{
struct sec_signature *psig = (struct sec_signature*)arg;
gpg_error_t err = scd_copy_data(psig->pSignature, (size_t*)psig->pulSignatureLen, (unsigned char *)data, datalen);
return err;
}
gpg_error_t scd_sign_data(assuan_context_t ctx, uchar *pSignature, unsigned long *pulSignatureLen, uchar *pData, unsigned long ulDataLen)
{
gpg_error_t err = 0;
if (ulDataLen > SEC_SIGN_MAXLEN)
return 1;
char *cmdstart = "SCD SETDATA ";
int offset = strlen(cmdstart);
char *tmp = malloc(ulDataLen * 2 + offset + 1);
strcpy(tmp, cmdstart);
for (unsigned long i = 0; i < ulDataLen; i ++)
sprintf(tmp + offset + i*2, "%02X", pData[i]);
SDEBUG("cmd=%s", tmp);
err = assuan_transact(ctx, tmp, NULL, NULL, NULL, NULL, NULL, NULL);
free(tmp);
if (err) {
SDEBUG("ERROR: SETDATA failed [%x]: %s", err, gpg_strerror(err));
return err;
}
// if (g_state.sign.pSignature != NULL) {
// sec_free(g_state.sign.pSignature);
// }
struct sec_signature sig = {pSignature, pulSignatureLen};
err = assuan_transact(ctx, "SCD PKAUTH OPENPGP.3", sign_data_cb, &sig, NULL, NULL, NULL, NULL);
// if (err == GPG_ERR_BUFFER_TOO_SHORT) {}
if (err) {
SDEBUG("ERROR: PKAUTH failed [%x]: %s", err, gpg_strerror(err));
return err;
}
return err;
}
static inline gpg_error_t sec_sexp_strcmp(gcry_sexp_t sexp, int n, const char *v) {
gpg_error_t err = 0;
char *tmp;
tmp = gcry_sexp_nth_string(sexp, n);
if (tmp == NULL) { return 1; }
if (strcmp(tmp, v) != 0) {
SDEBUG("got '%s' instead of '%s'", tmp, v);
err = 1;
}
gcry_free(tmp);
return err;
}
gpg_error_t scd_unpack_pubkey(uchar **pn, size_t *pnlen,
uchar **pe, size_t *pelen,
uchar *pubkey, size_t pubkeylen)
{
// pubkey S-expression: (public-key (rsa (n ...) (e ...)))
// |-> sexp |-> sexp2 |->sexp3 |->sexp3
gpg_error_t err = 0;
size_t len;
char *tmp;
*pn = NULL;
*pe = NULL;
gcry_sexp_t sexp = NULL;
gcry_sexp_t sexp2 = NULL;
gcry_sexp_t sexp3 = NULL;
err = gcry_sexp_new(&sexp, pubkey, pubkeylen, 0);
if (err) goto pubkey_error;
// gcry_sexp_dump(sexp);
err = sec_sexp_strcmp(sexp, 0, "public-key");
if (err) goto pubkey_error;
sexp2 = gcry_sexp_nth(sexp, 1);
if (sexp2 == NULL) goto pubkey_error;
err = sec_sexp_strcmp(sexp2, 0, "rsa");
if (err) goto pubkey_error;
sexp3 = gcry_sexp_nth(sexp2, 1);
if (sexp3 == NULL) goto pubkey_error;
err = sec_sexp_strcmp(sexp3, 0, "n");
if (err) goto pubkey_error;
tmp = gcry_sexp_nth_buffer(sexp3, 1, &len);
if (tmp == NULL) goto pubkey_error;
*pn = malloc(len);
memcpy(*pn, tmp, len);
*pnlen = len;
gcry_free(tmp);
sexp3 = gcry_sexp_nth(sexp2, 2);
if (sexp3 == NULL) goto pubkey_error;
err = sec_sexp_strcmp(sexp3, 0, "e");
if (err) goto pubkey_error;
tmp = gcry_sexp_nth_buffer(sexp3, 1, &len);
if (tmp == NULL) goto pubkey_error;
*pe = malloc(len);
memcpy(*pe, tmp, len);
*pelen = len;
gcry_free(tmp);
if (sexp) gcry_sexp_release(sexp);
if (sexp2) gcry_sexp_release(sexp2);
if (sexp3) gcry_sexp_release(sexp3);
return 0;
pubkey_error:
if (err == 0) err = 1;
SDEBUG("error %d: %s\n", err, gcry_strerror(err));
if (sexp) gcry_sexp_release(sexp);
if (sexp2) gcry_sexp_release(sexp2);
if (sexp3) gcry_sexp_release(sexp3);
if (*pn) { free(*pn); *pn = NULL; *pnlen = 0; }
if (*pe) { free(*pe); *pe = NULL; *pelen = 0; }
return err;
}