| 1 | /* key-lookup.c - Lookup keys for localdb authentication |
| 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 3 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, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <poldi.h>cxx . . $ `<file>/src/poldi.h`/ |
| 21 | |
| 22 | #include <unistd.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | #include <gpg-error.h> |
| 27 | #include <gcrypt.h> |
| 28 | |
| 29 | #include "util/support.h"cxx . . $ `<file>/src/util/support.h`/ |
| 30 | #include "util/filenames.h"cxx . . $ `<file>/src/util/filenames.h`/ |
| 31 | #include "key-lookup.h"cxx . . $ `<file>/src/pam/auth-method-localdb/key-lookup.h`/ |
| 32 | #include "defs-localdb.h"cxx . . $ `<file>/src/pam/auth-method-localdb/defs-localdb.h`/ |
| 33 | |
| 34 | |
| 35 | |
| 36 | /* This functions construct a new C-string containing the absolute |
| 37 | path for the file, which is to expected to contain the public key |
| 38 | for the card identified by SERIALNO. Returns proper error |
| 39 | code. */ |
| 40 | static gpg_error_tcxx . . $ gpg_error_t# |
| 41 | key_filename_constructcxx . . $ key_filename_construct(d091b773ddfd6a93). (char **filename, const char *serialno) |
| 42 | { |
| 43 | return make_filenamecxx . . $ make_filename(6db52936afcb0040). (filename, POLDI_KEY_DIRECTORYcxx . . $ `src/pam/auth-method-localdb/defs-localdb.h:28:9`!, serialno, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 44 | } |
| 45 | |
| 46 | /* Lookup the key belonging to the card specified by SERIALNO. |
| 47 | Returns a proper error code. */ |
| 48 | gpg_error_tcxx . . $ gpg_error_t# |
| 49 | key_lookup_by_serialnocxx . . $ key_lookup_by_serialno(94ccc6ed6e7e12d). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, const char *serialno, gcry_sexp_tcxx . . $ gcry_sexp_t# *key) |
| 50 | { |
| 51 | gcry_sexp_tcxx . . $ gcry_sexp_t# key_sexp; |
| 52 | char *key_string; |
| 53 | char *key_path; |
| 54 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 55 | |
| 56 | key_path = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 57 | key_string = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 58 | |
| 59 | err = key_filename_constructcxx . . $ key_filename_construct(d091b773ddfd6a93). (&key_path, serialno); |
| 60 | if (err) |
| 61 | { |
| 62 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 63 | "failed to construct key file path " |
| 64 | "for serial number `%s': %s\n", |
| 65 | serialno, gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 66 | goto out; |
| 67 | } |
| 68 | |
| 69 | err = file_to_stringcxx . . $ file_to_string(a9c69b9d9ab9e4b8). (key_path, &key_string); |
| 70 | if ((! err) && (! key_string)) |
| 71 | err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_NO_PUBKEYcxx . . $ GPG_ERR_NO_PUBKEY.); |
| 72 | if (err) |
| 73 | { |
| 74 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 75 | "failed to retrieve key from key file `%s': %s\n", |
| 76 | key_path, gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | err = string_to_sexpcxx . . $ string_to_sexp(4a5ea71d31321a6c). (&key_sexp, key_string); |
| 81 | if (err) |
| 82 | { |
| 83 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 84 | "failed to convert key " |
| 85 | "from `%s' into S-Expression: %s\n", |
| 86 | key_path, gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 87 | goto out; |
| 88 | } |
| 89 | |
| 90 | *key = key_sexp; |
| 91 | |
| 92 | out: |
| 93 | |
| 94 | xfreecxx . . $ `src/poldi.h:39:9`! (key_path); |
| 95 | xfreecxx . . $ `src/poldi.h:39:9`! (key_string); |
| 96 | |
| 97 | return err; |
| 98 | } |
| 99 | |
| 100 |