| 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> |
| 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" |
| 30 | #include "util/filenames.h" |
| 31 | #include "key-lookup.h" |
| 32 | #include "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_t |
| 41 | key_filename_construct (char **filename, const char *serialno) |
| 42 | { |
| 43 | return make_filename (filename, POLDI_KEY_DIRECTORY, serialno, NULL); |
| 44 | } |
| 45 | |
| 46 | /* Lookup the key belonging to the card specified by SERIALNO. |
| 47 | Returns a proper error code. */ |
| 48 | gpg_error_t |
| 49 | key_lookup_by_serialno (poldi_ctx_t ctx, const char *serialno, gcry_sexp_t *key) |
| 50 | { |
| 51 | gcry_sexp_t key_sexp; |
| 52 | char *key_string; |
| 53 | char *key_path; |
| 54 | gpg_error_t err; |
| 55 | |
| 56 | key_path = NULL; |
| 57 | key_string = NULL; |
| 58 | |
| 59 | err = key_filename_construct (&key_path, serialno); |
| 60 | if (err) |
| 61 | { |
| 62 | log_msg_error (ctx->loghandle, |
| 63 | "failed to construct key file path " |
| 64 | "for serial number `%s': %s\n", |
| 65 | serialno, gpg_strerror (err)); |
| 66 | goto out; |
| 67 | } |
| 68 | |
| 69 | err = file_to_string (key_path, &key_string); |
| 70 | if ((! err) && (! key_string)) |
| 71 | err = gpg_error (GPG_ERR_NO_PUBKEY); |
| 72 | if (err) |
| 73 | { |
| 74 | log_msg_error (ctx->loghandle, |
| 75 | "failed to retrieve key from key file `%s': %s\n", |
| 76 | key_path, gpg_strerror (err)); |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | err = string_to_sexp (&key_sexp, key_string); |
| 81 | if (err) |
| 82 | { |
| 83 | log_msg_error (ctx->loghandle, |
| 84 | "failed to convert key " |
| 85 | "from `%s' into S-Expression: %s\n", |
| 86 | key_path, gpg_strerror (err)); |
| 87 | goto out; |
| 88 | } |
| 89 | |
| 90 | *key = key_sexp; |
| 91 | |
| 92 | out: |
| 93 | |
| 94 | xfree (key_path); |
| 95 | xfree (key_string); |
| 96 | |
| 97 | return err; |
| 98 | } |
| 99 | |
| 100 |