Debian Codegraph @

src/util/support.c

raw symbols SCIP
1 /* support.c - PAM authentication via OpenPGP smartcards.
2 Copyright (C) 2004, 2005, 2007, 2008 g10 Code GmbH
3
4 This file is part of Poldi.
5
6 Poldi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Poldi is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21 #include "util-local.h"cxx . . $ `<file>/src/util/util-local.h`/
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <sys/mman.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31 #include <stdarg.h>
32 #include <pwd.h>
33 #include <dirent.h>
34 #include <limits.h>
35
36 #include <gcrypt.h>
37
38 #include "support.h"cxx . . $ `<file>/src/util/support.h`/
39 #include "defs.h"cxx . . $ `<file>/src/util/defs.h`/
40
41 #define CHALLENGE_MD_ALGORITHMcxx . . $ `src/util/support.c:41:9`! GCRY_MD_SHA1
42
43
44
45 /* Note: it's expected that xtrymalloc, xtrystrdup and xfree are
46 defined in util-local.h. */
47
48 /* This function generates a challenge; the challenge will be stored
49 in newly allocated memory, which is to be stored in *CHALLENGE;
50 it's length in bytes is to be stored in *CHALLENGE_N. Returns
51 proper error code. */
52 gpg_error_tcxx . . $ gpg_error_t#
53 challenge_generatecxx . . $ challenge_generate(fee6e208587138e9). (unsigned char **challenge, size_t *challenge_n)
54 {
55 gpg_error_tcxx . . $ gpg_error_t# err = GPG_ERR_NO_ERRORcxx . . $ GPG_ERR_NO_ERROR.;
56 unsigned char *challenge_new = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
57 size_t challenge_new_n = gcry_md_get_algo_dlencxx . . $ gcry_md_get_algo_dlen(46cc46841198e427). (CHALLENGE_MD_ALGORITHMcxx . . $ `src/util/support.c:41:9`!);
58
59 challenge_new = xtrymalloccxx . . $ `src/poldi.h:35:9`! (challenge_new_n);
60 if (! challenge_new)
61 err = gpg_err_code_from_errnocxx . . $ gpg_err_code_from_errno(53350dad864c0c3e). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
62 else
63 {
64 gcry_create_noncecxx . . $ gcry_create_nonce(5500c4293b1bbef0). (challenge_new, challenge_new_n);
65 *challenge = challenge_new;
66 *challenge_n = challenge_new_n;
67 }
68
69 return err;
70 }
71
72 /* Releases the challenge contained in CHALLENGE generated by
73 challenge_generate(). */
74 void
75 challenge_releasecxx . . $ challenge_release(f25546b61a629de7). (unsigned char *challenge)
76 {
77 if (challenge)
78 xfreecxx . . $ `src/poldi.h:39:9`! (challenge);
79 }
80
81 static gpg_error_tcxx . . $ gpg_error_t#
82 challenge_verify_sexpcxx . . $ challenge_verify_sexp(ffed3e639787afb3). (gcry_sexp_tcxx . . $ gcry_sexp_t# sexp_key,
83 unsigned char *challenge, size_t challenge_n,
84 unsigned char *response, size_t response_n)
85 {
86 gpg_error_tcxx . . $ gpg_error_t# err = GPG_ERR_NO_ERRORcxx . . $ GPG_ERR_NO_ERROR.;
87 gcry_sexp_tcxx . . $ gcry_sexp_t# sexp_signature = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
88 gcry_sexp_tcxx . . $ gcry_sexp_t# sexp_data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
89 int algo = pk_algocxx . . $ pk_algo(809ac41603e1f3b8). (sexp_key);
90
91 if (algo == 0)
92 return gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_UNSUPPORTED_ALGORITHMcxx . . $ GPG_ERR_UNSUPPORTED_ALGORITHM.);
93
94 err = challenge_datacxx . . $ challenge_data(9f0327bd59ab19da). (&sexp_data, algo, challenge, challenge_n);
95
96 if (! err)
97 err = response_signaturecxx . . $ response_signature(9f0327bd59ab19da). (&sexp_signature, algo,
98 response, response_n);
99
100 if (! err)
101 err = gcry_pk_verifycxx . . $ gcry_pk_verify(67151c5137046d44). (sexp_signature, sexp_data, sexp_key);
102
103 if (sexp_data)
104 gcry_sexp_releasecxx . . $ gcry_sexp_release(c5bb25d0f29ec38). (sexp_data);
105 if (sexp_signature)
106 gcry_sexp_releasecxx . . $ gcry_sexp_release(c5bb25d0f29ec38). (sexp_signature);
107
108 return err;
109 }
110
111 /* This functions verifies that the signature contained in RESPONSE of
112 size RESPONSE_N (in bytes) is indeed the result of signing the
113 challenge given in CHALLENGE of size CHALLENGE_N (in bytes) with
114 the secret key belonging to the public key given as PUBLIC_KEY.
115 Returns proper error code. */
116 gpg_error_tcxx . . $ gpg_error_t#
117 challenge_verifycxx . . $ challenge_verify(ffed3e639787afb3). (gcry_sexp_tcxx . . $ gcry_sexp_t# public_key,
118 unsigned char *challenge, size_t challenge_n,
119 unsigned char *response, size_t response_n)
120 {
121 gpg_error_tcxx . . $ gpg_error_t# err;
122
123 err = challenge_verify_sexpcxx . . $ challenge_verify_sexp(ffed3e639787afb3). (public_key,
124 challenge, challenge_n, response, response_n);
125
126 return err;
127 }
128
129
130
131 /*
132 * S-Expression conversion.
133 */
134
135 /* This function converts the given S-Expression SEXP into it's
136 `ADVANCED' string representation, using newly-allocated memory,
137 storing the resulting NUL-terminated string in *SEXP_STRING.
138 Returns a proper error code. */
139 gpg_error_tcxx . . $ gpg_error_t#
140 sexp_to_stringcxx . . $ sexp_to_string(dfc1b8d2b6a0e0f). (gcry_sexp_tcxx . . $ gcry_sexp_t# sexp, char **sexp_string)
141 {
142 const int fmt = GCRYSEXP_FMT_ADVANCEDcxx . . $ GCRYSEXP_FMT_ADVANCED.;
143 gpg_error_tcxx . . $ gpg_error_t# err;
144 size_t buffer_n;
145 char *buffer;
146
147 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (sexp);
148
149 buffer = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
150
151 /* Figure out amount of memory required for
152 string-representation. */
153 buffer_n = gcry_sexp_sprintcxx . . $ gcry_sexp_sprint(2992c33cee38ac18). (sexp, fmt, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
154 if (! buffer_n)
155 {
156 err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_INV_SEXPcxx . . $ GPG_ERR_INV_SEXP.); /* ? */
157 goto out;
158 }
159
160 /* Allocate memory. */
161 buffer = xtrymalloccxx . . $ `src/poldi.h:35:9`! (buffer_n);
162 if (!buffer)
163 {
164 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
165 goto out;
166 }
167
168 /* And write string-representation into buffer. */
169 buffer_n = gcry_sexp_sprintcxx . . $ gcry_sexp_sprint(2992c33cee38ac18). (sexp, fmt, buffer, buffer_n);
170 if (!buffer_n)
171 {
172 err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_INV_SEXPcxx . . $ GPG_ERR_INV_SEXP.); /* ? */
173 goto out;
174 }
175
176 *sexp_string = buffer;
177 err = 0;
178
179 out:
180
181 if (err)
182 xfreecxx . . $ `src/poldi.h:39:9`! (buffer);
183
184 return err;
185 }
186
187 /* This functions converts the given string-representation of an
188 S-Expression into a new S-Expression object, which is to be stored
189 in *SEXP. Returns proper error code. */
190 gpg_error_tcxx . . $ gpg_error_t#
191 string_to_sexpcxx . . $ string_to_sexp(4a5ea71d31321a6c). (gcry_sexp_tcxx . . $ gcry_sexp_t# *sexp, char *string)
192 {
193 gpg_error_tcxx . . $ gpg_error_t# err;
194
195 err = gcry_sexp_sscancxx . . $ gcry_sexp_sscan(98bdf0951f34d12f). (sexp, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, string, strlencxx . . $ strlen(751346f8406fd082). (string));
196
197 return err;
198 }
199
200 /* This function retrieves the content from the file specified by
201 FILENAMED and writes it into a newly allocated chunk of memory,
202 which is then stored in *DATA and *DATALEN. This functions adds a
203 NUL termination to the actual file data but does not include that
204 additional NUL character in DATALEN! Returns proper error
205 code. */
206 static gpg_error_tcxx . . $ gpg_error_t#
207 file_to_string_internalcxx . . $ file_to_string_internal(e0db3a0091505537). (const char *filename, void **data, size_t *datalen)
208 {
209 struct statcxx . . $ stat# statbuf;
210 unsigned char *data_new;
211 gpg_error_tcxx . . $ gpg_error_t# err;
212 FILEcxx . . $ FILE# *fp;
213 int ret;
214
215 data_new = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
216 fp = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
217
218 /* Retrieve file size. */
219 ret = statcxx . . $ stat(a9cd1e23e96f423c). (filename, &statbuf);
220 if (ret)
221 {
222 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
223 goto out;
224 }
225
226 if (statbuf.st_sizecxx . . $ stat#st_size.)
227 {
228 fp = fopencxx . . $ fopen(82ec0787cd37e0a3). (filename, "r");
229 if (! fp)
230 {
231 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
232 goto out;
233 }
234 data_new = xtrymalloccxx . . $ `src/poldi.h:35:9`! (statbuf.st_size + 1);
235 if (!data_new)
236 {
237 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
238 goto out;
239 }
240 ret = freadcxx . . $ fread(738fc8ac1d711e58). (data_new, statbuf.st_sizecxx . . $ stat#st_size., 1, fp);
241 if (ret != 1)
242 {
243 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
244 goto out;
245 }
246 data_new[statbuf.st_sizecxx . . $ stat#st_size.] = 0;
247 }
248
249 err = 0;
250 *data = data_new;
251 if (datalen)
252 *datalen = statbuf.st_sizecxx . . $ stat#st_size.;
253
254 out:
255
256 if (fp)
257 fclosecxx . . $ fclose(89bc1efcdac0ca22). (fp);
258
259 if (err)
260 xfreecxx . . $ `src/poldi.h:39:9`! (data_new);
261
262 return err;
263 }
264
265
266 /* This function retrieves the content from the file specified by
267 FILENAMED and writes it into a newly allocated chunk of memory,
268 which is then stored in *DATA and *DATALEN. Returns proper error
269 code. */
270 gpg_error_tcxx . . $ gpg_error_t#
271 file_to_binstringcxx . . $ file_to_binstring(e0db3a0091505537). (const char *filename, void **data, size_t *datalen)
272 {
273 return file_to_string_internalcxx . . $ file_to_string_internal(e0db3a0091505537). (filename, data, datalen);
274 }
275
276 /* This function retrieves the content from the file specified by
277 FILENAMED and writes it into a newly allocated C-string, which is
278 then stored in *STRING. Returns proper error code. */
279 gpg_error_tcxx . . $ gpg_error_t#
280 file_to_stringcxx . . $ file_to_string(a9c69b9d9ab9e4b8). (const char *filename, char **string)
281 {
282 gpg_error_tcxx . . $ gpg_error_t# err;
283 void *data;
284
285 err = file_to_string_internalcxx . . $ file_to_string_internal(e0db3a0091505537). (filename, &data, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
286 if (err)
287 goto out;
288
289 *string = data;
290
291 out:
292
293 return err;
294 }
295
296
297
298 gpg_error_tcxx . . $ gpg_error_t#
299 char_vector_dupcxx . . $ char_vector_dup(9354c60f7c8ad8bb). (int len, const char **a, char ***b)
300 {
301 char **c;
302 gpg_error_tcxx . . $ gpg_error_t# err;
303 int i;
304
305 c = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
306 err = 0;
307
308 c = xtrymalloccxx . . $ `src/poldi.h:35:9`! (sizeof (*c) * (len + 1));
309 if (!c)
310 {
311 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
312 goto out;
313 }
314
315 for (i = 0; i < len + 1; i++)
316 c[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
317
318 for (i = 0; i < len; i++)
319 {
320 c[i] = xtrystrdupcxx . . $ `src/poldi.h:37:9`! (a[i]);
321 if (!c[i])
322 {
323 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
324 goto out;
325 }
326 }
327 c[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
328
329 out:
330
331 if (err)
332 {
333 if (c)
334 {
335 for (i = 0; c[i]; i++)
336 xfreecxx . . $ `src/poldi.h:39:9`! (c[i]);
337 xfreecxx . . $ `src/poldi.h:39:9`! (c);
338 }
339 *b = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
340 }
341 else
342 *b = c;
343
344 return err;
345 }
346
347 void
348 char_vector_freecxx . . $ char_vector_free(3ed91490e421178f). (char **a)
349 {
350 int i;
351
352 if (a)
353 {
354 for (i = 0; a[i]; i++)
355 xfreecxx . . $ `src/poldi.h:39:9`! (a[i]);
356 xfreecxx . . $ `src/poldi.h:39:9`! (a);
357 }
358 }
359
360 int
361 my_strlencxx . . $ my_strlen(5c4306d8553b3696). (const char *s)
362 {
363 int ret = 0;
364
365 while (*s)
366 {
367 if (ret == INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!)
368 {
369 ret = -1;
370 break;
371 }
372 ret++;
373 s++;
374 }
375
376 return ret;
377 }
378
379 int
380 pk_algocxx . . $ pk_algo(809ac41603e1f3b8). (gcry_sexp_tcxx . . $ gcry_sexp_t# sexp_key)
381 {
382 gcry_sexp_tcxx . . $ gcry_sexp_t# sexp_data;
383 char *algoname;
384 int algo;
385
386 sexp_data = gcry_sexp_find_tokencxx . . $ gcry_sexp_find_token(fc5ff166fcd9a563). (sexp_key, "public-key", 0);
387 if (!sexp_data)
388 return 0;
389
390 gcry_sexp_tcxx . . $ gcry_sexp_t# sexp_tmp = gcry_sexp_cadrcxx . . $ gcry_sexp_cadr(8063094aeeb0c111). (sexp_data);
391 gcry_sexp_releasecxx . . $ gcry_sexp_release(c5bb25d0f29ec38). (sexp_data);
392 sexp_data = sexp_tmp;
393
394 algoname = gcry_sexp_nth_stringcxx . . $ gcry_sexp_nth_string(cd74b69df9f85a56). (sexp_data, 0);
395 gcry_sexp_releasecxx . . $ gcry_sexp_release(c5bb25d0f29ec38). (sexp_data);
396 if (!algoname)
397 return 0;
398
399 algo = gcry_pk_map_namecxx . . $ gcry_pk_map_name(5c4306d8553b3696). (algoname);
400 xfreecxx . . $ `src/poldi.h:39:9`!(algoname);
401 return algo;
402 }
403
404 gpg_error_tcxx . . $ gpg_error_t#
405 challenge_datacxx . . $ challenge_data(9f0327bd59ab19da). (gcry_sexp_tcxx . . $ gcry_sexp_t# *data, int algo,
406 unsigned char *challenge, size_t challenge_n)
407 {
408 if (algo == GCRY_PK_ECCcxx . . $ GCRY_PK_ECC.)
409 {
410 return gcry_sexp_buildcxx . . $ gcry_sexp_build(82809d3d423c9b48). (data, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
411 "(data (flags eddsa) (hash-algo sha512) (value %b))",
412 challenge_n, challenge);
413 }
414
415 return gcry_sexp_buildcxx . . $ gcry_sexp_build(82809d3d423c9b48). (data, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
416 "(data (flags pkcs1) (hash sha1 %b))",
417 challenge_n, challenge);
418 }
419
420 gpg_error_tcxx . . $ gpg_error_t#
421 response_signaturecxx . . $ response_signature(9f0327bd59ab19da). (gcry_sexp_tcxx . . $ gcry_sexp_t# *sig, int algo,
422 unsigned char *response, size_t response_n)
423 {
424 switch (algo)
425 {
426 case GCRY_PK_RSAcxx . . $ GCRY_PK_RSA.:
427 return gcry_sexp_buildcxx . . $ gcry_sexp_build(82809d3d423c9b48). (sig, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
428 "(sig-val (rsa (s %b)))",
429 response_n, response);
430 case GCRY_PK_DSAcxx . . $ GCRY_PK_DSA.:
431 return gcry_sexp_buildcxx . . $ gcry_sexp_build(82809d3d423c9b48). (sig, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
432 "(sig-val (dsa (r %b) (s %b)))",
433 response_n / 2, response,
434 response_n / 2, response + response_n / 2);
435 case GCRY_PK_ECCcxx . . $ GCRY_PK_ECC.:
436 return gcry_sexp_buildcxx . . $ gcry_sexp_build(82809d3d423c9b48). (sig, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
437 "(sig-val (eddsa (r %b) (s %b)))",
438 response_n / 2, response,
439 response_n / 2, response + response_n / 2);
440 default:
441 return gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_UNSUPPORTED_ALGORITHMcxx . . $ GPG_ERR_UNSUPPORTED_ALGORITHM.);
442 }
443 }
444 /* END */