|
| 1 | +/* |
| 2 | + * card-cedulauy-cache.h: MRZ cache of the Uruguayan eID card (cédula de identidad) |
| 3 | + * |
| 4 | + * Copyright (C) 2026 Nicolás Gutiérrez <ngutierreztassano@gmail.com> |
| 5 | + * |
| 6 | + * This library is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 2.1 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this library; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | + |
| 21 | +#ifndef _CARD_CEDULAUY_CACHE_H |
| 22 | +#define _CARD_CEDULAUY_CACHE_H |
| 23 | + |
| 24 | +#include <errno.h> |
| 25 | +#include <limits.h> |
| 26 | +#include <stdio.h> |
| 27 | +#include <string.h> |
| 28 | +#ifndef _WIN32 |
| 29 | +#include <fcntl.h> |
| 30 | +#include <sys/stat.h> |
| 31 | +#include <unistd.h> |
| 32 | +#endif |
| 33 | + |
| 34 | +#include "common/compat_strlcat.h" |
| 35 | +#include "libopensc/log.h" |
| 36 | +#include "libopensc/opensc.h" |
| 37 | + |
| 38 | +/* the three TD1 lines of 30 characters, concatenated */ |
| 39 | +#define CEDULAUY_MRZ_LEN 90 |
| 40 | +#define CEDULAUY_MRZ_CACHE_FILE "cedulauy_mrz" |
| 41 | + |
| 42 | +static inline int |
| 43 | +cedulauy_mrz_cache_path(sc_context_t *ctx, char *buf, size_t buflen) |
| 44 | +{ |
| 45 | + int r = sc_get_cache_dir(ctx, buf, buflen); |
| 46 | + if (r < 0) |
| 47 | + return r; |
| 48 | + |
| 49 | + if (strlcat(buf, "/" CEDULAUY_MRZ_CACHE_FILE, buflen) >= buflen) |
| 50 | + return SC_ERROR_BUFFER_TOO_SMALL; |
| 51 | + |
| 52 | + return SC_SUCCESS; |
| 53 | +} |
| 54 | + |
| 55 | +static inline FILE * |
| 56 | +cedulauy_create_mrz_cache(sc_context_t *ctx, const char *path) |
| 57 | +{ |
| 58 | +#ifdef _WIN32 |
| 59 | + (void)ctx; |
| 60 | + /* the profile directory ACL already restricts access to the user */ |
| 61 | + return fopen(path, "wb"); |
| 62 | +#else |
| 63 | + FILE *f; |
| 64 | + int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); |
| 65 | + |
| 66 | + if (fd < 0) |
| 67 | + return NULL; |
| 68 | + |
| 69 | + /* O_CREAT does not change the mode of an existing file */ |
| 70 | + if (fchmod(fd, S_IRUSR | S_IWUSR) < 0) |
| 71 | + sc_log(ctx, "Cannot restrict the permissions of %s", path); |
| 72 | + |
| 73 | + f = fdopen(fd, "wb"); |
| 74 | + if (f == NULL) |
| 75 | + close(fd); |
| 76 | + |
| 77 | + return f; |
| 78 | +#endif |
| 79 | +} |
| 80 | + |
| 81 | +/* returns 1 if a complete MRZ was read, 0 otherwise */ |
| 82 | +static inline int |
| 83 | +cedulauy_read_mrz_cache(sc_context_t *ctx, unsigned char mrz[CEDULAUY_MRZ_LEN]) |
| 84 | +{ |
| 85 | + char path[PATH_MAX]; |
| 86 | + FILE *f; |
| 87 | + size_t got; |
| 88 | + |
| 89 | + if (cedulauy_mrz_cache_path(ctx, path, sizeof path) < 0) |
| 90 | + return 0; |
| 91 | + |
| 92 | + f = fopen(path, "rb"); |
| 93 | + if (f == NULL) |
| 94 | + return 0; |
| 95 | + |
| 96 | + got = fread(mrz, 1, CEDULAUY_MRZ_LEN, f); |
| 97 | + fclose(f); |
| 98 | + |
| 99 | + if (got != CEDULAUY_MRZ_LEN) { |
| 100 | + sc_log(ctx, "Ignoring %s, it is not %d characters long", path, CEDULAUY_MRZ_LEN); |
| 101 | + return 0; |
| 102 | + } |
| 103 | + |
| 104 | + return 1; |
| 105 | +} |
| 106 | + |
| 107 | +static inline int |
| 108 | +cedulauy_write_mrz_cache(sc_context_t *ctx, const unsigned char mrz[CEDULAUY_MRZ_LEN]) |
| 109 | +{ |
| 110 | + char path[PATH_MAX]; |
| 111 | + FILE *f; |
| 112 | + size_t written; |
| 113 | + int r; |
| 114 | + |
| 115 | + LOG_FUNC_CALLED(ctx); |
| 116 | + |
| 117 | + r = cedulauy_mrz_cache_path(ctx, path, sizeof path); |
| 118 | + LOG_TEST_RET(ctx, r, "Cannot determine the cache directory"); |
| 119 | + |
| 120 | + f = cedulauy_create_mrz_cache(ctx, path); |
| 121 | + if (f == NULL && errno == ENOENT) { |
| 122 | + r = sc_make_cache_dir(ctx); |
| 123 | + LOG_TEST_RET(ctx, r, "Cannot create the cache directory"); |
| 124 | + f = cedulauy_create_mrz_cache(ctx, path); |
| 125 | + } |
| 126 | + if (f == NULL) { |
| 127 | + sc_log(ctx, "Cannot open %s: %s", path, strerror(errno)); |
| 128 | + LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL); |
| 129 | + } |
| 130 | + |
| 131 | + written = fwrite(mrz, 1, CEDULAUY_MRZ_LEN, f); |
| 132 | + if (fclose(f) != 0 || written != CEDULAUY_MRZ_LEN) { |
| 133 | + sc_log(ctx, "Cannot write %s: %s", path, strerror(errno)); |
| 134 | + LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL); |
| 135 | + } |
| 136 | + |
| 137 | + sc_log(ctx, "MRZ stored in %s", path); |
| 138 | + LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
| 139 | +} |
| 140 | + |
| 141 | +static inline int |
| 142 | +cedulauy_delete_mrz_cache(sc_context_t *ctx) |
| 143 | +{ |
| 144 | + char path[PATH_MAX]; |
| 145 | + int r; |
| 146 | + |
| 147 | + LOG_FUNC_CALLED(ctx); |
| 148 | + |
| 149 | + r = cedulauy_mrz_cache_path(ctx, path, sizeof path); |
| 150 | + LOG_TEST_RET(ctx, r, "Cannot determine the cache directory"); |
| 151 | + |
| 152 | + if (remove(path) != 0) { |
| 153 | + int err = errno; |
| 154 | + sc_log(ctx, "Cannot remove %s: %s", path, strerror(err)); |
| 155 | + LOG_FUNC_RETURN(ctx, err == ENOENT ? SC_ERROR_FILE_NOT_FOUND : SC_ERROR_INTERNAL); |
| 156 | + } |
| 157 | + |
| 158 | + sc_log(ctx, "MRZ removed from %s", path); |
| 159 | + LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
| 160 | +} |
| 161 | + |
| 162 | +#endif |
0 commit comments