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"
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"
39 #include "defs.h"
40
41 #define CHALLENGE_MD_ALGORITHM 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_t
53 challenge_generate (unsigned char **challenge, size_t *challenge_n)
54 {
55 gpg_error_t err = GPG_ERR_NO_ERROR;
56 unsigned char *challenge_new = NULL;
57 size_t challenge_new_n = gcry_md_get_algo_dlen (CHALLENGE_MD_ALGORITHM);
58
59 challenge_new = xtrymalloc (challenge_new_n);
60 if (! challenge_new)
61 err = gpg_err_code_from_errno (errno);
62 else
63 {
64 gcry_create_nonce (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_release (unsigned char *challenge)
76 {
77 if (challenge)
78 xfree (challenge);
79 }
80
81 static gpg_error_t
82 challenge_verify_sexp (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_t err = GPG_ERR_NO_ERROR;
87 gcry_sexp_t sexp_signature = NULL;
88 gcry_sexp_t sexp_data = NULL;
89 int algo = pk_algo (sexp_key);
90
91 if (algo == 0)
92 return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
93
94 err = challenge_data (&sexp_data, algo, challenge, challenge_n);
95
96 if (! err)
97 err = response_signature (&sexp_signature, algo,
98 response, response_n);
99
100 if (! err)
101 err = gcry_pk_verify (sexp_signature, sexp_data, sexp_key);
102
103 if (sexp_data)
104 gcry_sexp_release (sexp_data);
105 if (sexp_signature)
106 gcry_sexp_release (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_t
117 challenge_verify (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_t err;
122
123 err = challenge_verify_sexp (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_t
140 sexp_to_string (gcry_sexp_t sexp, char **sexp_string)
141 {
142 const int fmt = GCRYSEXP_FMT_ADVANCED;
143 gpg_error_t err;
144 size_t buffer_n;
145 char *buffer;
146
147 assert (sexp);
148
149 buffer = NULL;
150
151 /* Figure out amount of memory required for
152 string-representation. */
153 buffer_n = gcry_sexp_sprint (sexp, fmt, NULL, 0);
154 if (! buffer_n)
155 {
156 err = gpg_error (GPG_ERR_INV_SEXP); /* ? */
157 goto out;
158 }
159
160 /* Allocate memory. */
161 buffer = xtrymalloc (buffer_n);
162 if (!buffer)
163 {
164 err = gpg_error_from_errno (errno);
165 goto out;
166 }
167
168 /* And write string-representation into buffer. */
169 buffer_n = gcry_sexp_sprint (sexp, fmt, buffer, buffer_n);
170 if (!buffer_n)
171 {
172 err = gpg_error (GPG_ERR_INV_SEXP); /* ? */
173 goto out;
174 }
175
176 *sexp_string = buffer;
177 err = 0;
178
179 out:
180
181 if (err)
182 xfree (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_t
191 string_to_sexp (gcry_sexp_t *sexp, char *string)
192 {
193 gpg_error_t err;
194
195 err = gcry_sexp_sscan (sexp, NULL, string, strlen (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_t
207 file_to_string_internal (const char *filename, void **data, size_t *datalen)
208 {
209 struct stat statbuf;
210 unsigned char *data_new;
211 gpg_error_t err;
212 FILE *fp;
213 int ret;
214
215 data_new = NULL;
216 fp = NULL;
217
218 /* Retrieve file size. */
219 ret = stat (filename, &statbuf);
220 if (ret)
221 {
222 err = gpg_error_from_errno (errno);
223 goto out;
224 }
225
226 if (statbuf.st_size)
227 {
228 fp = fopen (filename, "r");
229 if (! fp)
230 {
231 err = gpg_error_from_errno (errno);
232 goto out;
233 }
234 data_new = xtrymalloc (statbuf.st_size + 1);
235 if (!data_new)
236 {
237 err = gpg_error_from_errno (errno);
238 goto out;
239 }
240 ret = fread (data_new, statbuf.st_size, 1, fp);
241 if (ret != 1)
242 {
243 err = gpg_error_from_errno (errno);
244 goto out;
245 }
246 data_new[statbuf.st_size] = 0;
247 }
248
249 err = 0;
250 *data = data_new;
251 if (datalen)
252 *datalen = statbuf.st_size;
253
254 out:
255
256 if (fp)
257 fclose (fp);
258
259 if (err)
260 xfree (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_t
271 file_to_binstring (const char *filename, void **data, size_t *datalen)
272 {
273 return file_to_string_internal (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_t
280 file_to_string (const char *filename, char **string)
281 {
282 gpg_error_t err;
283 void *data;
284
285 err = file_to_string_internal (filename, &data, NULL);
286 if (err)
287 goto out;
288
289 *string = data;
290
291 out:
292
293 return err;
294 }
295
296
297
298 gpg_error_t
299 char_vector_dup (int len, const char **a, char ***b)
300 {
301 char **c;
302 gpg_error_t err;
303 int i;
304
305 c = NULL;
306 err = 0;
307
308 c = xtrymalloc (sizeof (*c) * (len + 1));
309 if (!c)
310 {
311 err = gpg_error_from_errno (errno);
312 goto out;
313 }
314
315 for (i = 0; i < len + 1; i++)
316 c[i] = NULL;
317
318 for (i = 0; i < len; i++)
319 {
320 c[i] = xtrystrdup (a[i]);
321 if (!c[i])
322 {
323 err = gpg_error_from_errno (errno);
324 goto out;
325 }
326 }
327 c[i] = NULL;
328
329 out:
330
331 if (err)
332 {
333 if (c)
334 {
335 for (i = 0; c[i]; i++)
336 xfree (c[i]);
337 xfree (c);
338 }
339 *b = NULL;
340 }
341 else
342 *b = c;
343
344 return err;
345 }
346
347 void
348 char_vector_free (char **a)
349 {
350 int i;
351
352 if (a)
353 {
354 for (i = 0; a[i]; i++)
355 xfree (a[i]);
356 xfree (a);
357 }
358 }
359
360 int
361 my_strlen (const char *s)
362 {
363 int ret = 0;
364
365 while (*s)
366 {
367 if (ret == INT_MAX)
368 {
369 ret = -1;
370 break;
371 }
372 ret++;
373 s++;
374 }
375
376 return ret;
377 }
378
379 int
380 pk_algo (gcry_sexp_t sexp_key)
381 {
382 gcry_sexp_t sexp_data;
383 char *algoname;
384 int algo;
385
386 sexp_data = gcry_sexp_find_token (sexp_key, "public-key", 0);
387 if (!sexp_data)
388 return 0;
389
390 gcry_sexp_t sexp_tmp = gcry_sexp_cadr (sexp_data);
391 gcry_sexp_release (sexp_data);
392 sexp_data = sexp_tmp;
393
394 algoname = gcry_sexp_nth_string (sexp_data, 0);
395 gcry_sexp_release (sexp_data);
396 if (!algoname)
397 return 0;
398
399 algo = gcry_pk_map_name (algoname);
400 xfree(algoname);
401 return algo;
402 }
403
404 gpg_error_t
405 challenge_data (gcry_sexp_t *data, int algo,
406 unsigned char *challenge, size_t challenge_n)
407 {
408 if (algo == GCRY_PK_ECC)
409 {
410 return gcry_sexp_build (data, NULL,
411 "(data (flags eddsa) (hash-algo sha512) (value %b))",
412 challenge_n, challenge);
413 }
414
415 return gcry_sexp_build (data, NULL,
416 "(data (flags pkcs1) (hash sha1 %b))",
417 challenge_n, challenge);
418 }
419
420 gpg_error_t
421 response_signature (gcry_sexp_t *sig, int algo,
422 unsigned char *response, size_t response_n)
423 {
424 switch (algo)
425 {
426 case GCRY_PK_RSA:
427 return gcry_sexp_build (sig, NULL,
428 "(sig-val (rsa (s %b)))",
429 response_n, response);
430 case GCRY_PK_DSA:
431 return gcry_sexp_build (sig, NULL,
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_ECC:
436 return gcry_sexp_build (sig, NULL,
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_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
442 }
443 }
444 /* END */