/src/opensc/src/libopensc/pkcs15-cache.c
Line | Count | Source |
1 | | /* |
2 | | * pkcs15-cache.c: PKCS #15 file caching functions |
3 | | * |
4 | | * Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi> |
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 | | #ifdef HAVE_CONFIG_H |
22 | | #include "config.h" |
23 | | #endif |
24 | | |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | #ifdef HAVE_UNISTD_H |
29 | | #include <unistd.h> |
30 | | #endif |
31 | | #include <ctype.h> |
32 | | #include <errno.h> |
33 | | #include <limits.h> |
34 | | #include <sys/stat.h> |
35 | | |
36 | | #include "internal.h" |
37 | | #include "pkcs15.h" |
38 | | #include "common/compat_strlcpy.h" |
39 | | |
40 | 0 | #define RANDOM_UID_INDICATOR 0x08 |
41 | | |
42 | | static int |
43 | | is_safe_cache_component(const char *s) |
44 | 1.64k | { |
45 | 1.64k | const unsigned char *p = (const unsigned char *)s; |
46 | | |
47 | 1.64k | if (s == NULL || *s == '\0') |
48 | 104 | return 0; |
49 | 1.54k | if (strstr(s, "..") != NULL) |
50 | 10 | return 0; |
51 | 32.3k | for (; *p; p++) { |
52 | 31.0k | if (*p == '/' || *p == '\n') |
53 | 56 | return 0; |
54 | 30.9k | if (!isalnum(*p) && *p != '_' && *p != '-' && *p != '.') |
55 | 130 | return 0; |
56 | 30.9k | } |
57 | 1.34k | return 1; |
58 | 1.53k | } |
59 | | |
60 | | static int generate_cache_filename(struct sc_pkcs15_card *p15card, |
61 | | const sc_path_t *path, |
62 | | char *buf, size_t bufsize) |
63 | 4.28k | { |
64 | 4.28k | char dir[PATH_MAX]; |
65 | 4.28k | char *last_update = NULL; |
66 | 4.28k | char *serial_number = NULL; |
67 | 4.28k | int r; |
68 | 4.28k | unsigned u; |
69 | 4.28k | size_t change_counter; |
70 | | |
71 | 4.28k | if (p15card == NULL || p15card->tokeninfo == NULL || p15card->card == NULL || p15card->card->ctx == NULL) { |
72 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
73 | 0 | } |
74 | | |
75 | 4.28k | serial_number = p15card->tokeninfo->serial_number; |
76 | 4.28k | if (serial_number == NULL && |
77 | 2.64k | (p15card->card->uid.len == 0 || p15card->card->uid.value[0] == RANDOM_UID_INDICATOR)) |
78 | 2.64k | return SC_ERROR_INVALID_ARGUMENTS; |
79 | | |
80 | 1.64k | if (path->len > SC_MAX_PATH_SIZE) |
81 | 0 | return SC_ERROR_INTERNAL; |
82 | 1.64k | r = sc_get_cache_dir(p15card->card->ctx, dir, sizeof(dir)); |
83 | 1.64k | if (r) |
84 | 0 | return r; |
85 | 1.64k | snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "/"); |
86 | | |
87 | 1.64k | last_update = sc_pkcs15_get_lastupdate(p15card); |
88 | 1.64k | if (!last_update || !is_safe_cache_component(last_update)) |
89 | 1.64k | last_update = "NODATE"; |
90 | | |
91 | 1.64k | if (serial_number && is_safe_cache_component(serial_number)) { |
92 | 1.34k | snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), |
93 | 1.34k | "%s_%s", serial_number, last_update); |
94 | 1.34k | } else { |
95 | 297 | snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "uid-%s_%s", |
96 | 297 | sc_dump_hex(p15card->card->uid.value, p15card->card->uid.len), last_update); |
97 | 297 | } |
98 | | |
99 | 1.64k | if (SC_SUCCESS == sc_card_ctl(p15card->card, SC_CARDCTL_GET_CHANGE_COUNTER, &change_counter)) |
100 | 0 | snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "_%zu", change_counter); |
101 | | |
102 | 1.64k | if (path->aid.len && |
103 | 1.26k | (path->type == SC_PATH_TYPE_FILE_ID || path->type == SC_PATH_TYPE_PATH)) { |
104 | 998 | snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "_"); |
105 | 7.99k | for (u = 0; u < path->aid.len; u++) |
106 | 6.99k | snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), |
107 | 6.99k | "%02X", path->aid.value[u]); |
108 | 998 | } |
109 | 645 | else if (path->type != SC_PATH_TYPE_PATH) { |
110 | 299 | return SC_ERROR_INVALID_ARGUMENTS; |
111 | 299 | } |
112 | | |
113 | 1.34k | if (path->len) { |
114 | 1.34k | size_t offs = 0; |
115 | | |
116 | 1.34k | if (path->len > 2 && memcmp(path->value, "\x3F\x00", 2) == 0) |
117 | 346 | offs = 2; |
118 | 1.34k | snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "_"); |
119 | 4.72k | for (u = 0; u < path->len - offs; u++) |
120 | 3.38k | snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), |
121 | 3.38k | "%02X", path->value[u + offs]); |
122 | 1.34k | } |
123 | | |
124 | 1.34k | if (!buf) |
125 | 0 | return SC_ERROR_BUFFER_TOO_SMALL; |
126 | 1.34k | strlcpy(buf, dir, bufsize); |
127 | | |
128 | 1.34k | return SC_SUCCESS; |
129 | 1.34k | } |
130 | | |
131 | | int sc_pkcs15_read_cached_file(struct sc_pkcs15_card *p15card, |
132 | | const sc_path_t *path, |
133 | | u8 **buf, size_t *bufsize) |
134 | 7.31k | { |
135 | 7.31k | char fname[PATH_MAX]; |
136 | 7.31k | int rv; |
137 | 7.31k | FILE *f; |
138 | 7.31k | size_t count; |
139 | 7.31k | struct stat stbuf; |
140 | 7.31k | u8 *data = NULL; |
141 | | |
142 | 7.31k | if (path->len < 2) |
143 | 1.94k | return SC_ERROR_INVALID_ARGUMENTS; |
144 | | |
145 | | /* Accept full path or FILE-ID path with AID */ |
146 | 5.37k | if ((path->type != SC_PATH_TYPE_PATH) && (path->type != SC_PATH_TYPE_FILE_ID || path->aid.len == 0)) |
147 | 2.05k | return SC_ERROR_INVALID_ARGUMENTS; |
148 | | |
149 | 3.32k | sc_log(p15card->card->ctx, "try to read cache for %s", sc_print_path(path)); |
150 | 3.32k | rv = generate_cache_filename(p15card, path, fname, sizeof(fname)); |
151 | 3.32k | if (rv != SC_SUCCESS) |
152 | 2.06k | return rv; |
153 | 1.25k | sc_log(p15card->card->ctx, "read cached file %s", fname); |
154 | | |
155 | 1.25k | f = fopen(fname, "rb"); |
156 | 1.25k | if (!f) |
157 | 899 | return SC_ERROR_FILE_NOT_FOUND; |
158 | 360 | if (fstat(fileno(f), &stbuf)) { |
159 | 0 | fclose(f); |
160 | 0 | return SC_ERROR_FILE_NOT_FOUND; |
161 | 0 | } |
162 | | |
163 | 360 | if (path->count < 0) { |
164 | 228 | count = stbuf.st_size; |
165 | 228 | } |
166 | 132 | else { |
167 | 132 | count = path->count; |
168 | 132 | if (path->index + count > (size_t)stbuf.st_size) { |
169 | 132 | rv = SC_ERROR_FILE_NOT_FOUND; /* cache file bad? */ |
170 | 132 | goto err; |
171 | 132 | } |
172 | | |
173 | 0 | if (0 != fseek(f, (long)path->index, SEEK_SET)) { |
174 | 0 | rv = SC_ERROR_FILE_NOT_FOUND; |
175 | 0 | goto err; |
176 | 0 | } |
177 | 0 | } |
178 | | |
179 | 228 | if (*buf == NULL) { |
180 | 224 | if (count == 0) { |
181 | 197 | data = NULL; |
182 | 197 | } else { |
183 | 27 | data = malloc(count); |
184 | 27 | if (data == NULL) { |
185 | 0 | rv = SC_ERROR_OUT_OF_MEMORY; |
186 | 0 | goto err; |
187 | 0 | } |
188 | 27 | } |
189 | 224 | } |
190 | 4 | else { |
191 | 4 | if (count > *bufsize) { |
192 | 1 | rv = SC_ERROR_BUFFER_TOO_SMALL; |
193 | 1 | goto err; |
194 | 1 | } |
195 | 3 | data = *buf; |
196 | 3 | } |
197 | | |
198 | 227 | if (count > 0 && count != fread(data, 1, count, f)) { |
199 | 0 | rv = SC_ERROR_BUFFER_TOO_SMALL; |
200 | 0 | goto err; |
201 | 0 | } |
202 | 227 | *buf = data; |
203 | 227 | *bufsize = count; |
204 | | |
205 | 227 | rv = SC_SUCCESS; |
206 | | |
207 | 360 | err: |
208 | 360 | if (rv != SC_SUCCESS) { |
209 | 133 | if (data != *buf) { |
210 | 1 | free(data); |
211 | 1 | } |
212 | 133 | } |
213 | | |
214 | 360 | fclose(f); |
215 | 360 | return rv; |
216 | 227 | } |
217 | | |
218 | | int sc_pkcs15_cache_file(struct sc_pkcs15_card *p15card, |
219 | | const sc_path_t *path, |
220 | | const u8 *buf, size_t bufsize) |
221 | 964 | { |
222 | 964 | char fname[PATH_MAX]; |
223 | 964 | int r; |
224 | 964 | long len; |
225 | 964 | FILE *f; |
226 | 964 | size_t c; |
227 | | |
228 | 964 | r = generate_cache_filename(p15card, path, fname, sizeof(fname)); |
229 | 964 | if (r != 0) |
230 | 879 | return r; |
231 | | |
232 | 85 | f = fopen(fname, "ab"); |
233 | | /* If the open failed because the cache directory does |
234 | | * not exist, create it and a re-try the fopen() call. |
235 | | */ |
236 | 85 | if (f == NULL && errno == ENOENT) { |
237 | 2 | if ((r = sc_make_cache_dir(p15card->card->ctx)) < 0) |
238 | 0 | return r; |
239 | 2 | f = fopen(fname, "ab"); |
240 | 2 | } |
241 | 85 | if (f == NULL) |
242 | 0 | return 0; |
243 | | |
244 | | /* we opened the file for appending so we should be at the end of file. |
245 | | * The ftell() will give use the length of the file */ |
246 | 85 | len = ftell(f); |
247 | 85 | if (len > path->index) { |
248 | | /* override previous cache records on this location */ |
249 | 12 | r = fseek(f, path->index, SEEK_SET); |
250 | 12 | if (r != 0) { |
251 | 0 | fclose(f); |
252 | 0 | return 0; |
253 | 0 | } |
254 | 73 | } else if (path->index > len) { |
255 | | /* We miss some bytes so we will not cache this chunk */ |
256 | 1 | fclose(f); |
257 | 1 | return 0; |
258 | 1 | } |
259 | | |
260 | 84 | c = fwrite(buf, 1, bufsize, f); |
261 | 84 | fclose(f); |
262 | 84 | if (c != bufsize) { |
263 | 0 | sc_log(p15card->card->ctx, "fwrite() wrote only %zu bytes", c); |
264 | 0 | unlink(fname); |
265 | 0 | return SC_ERROR_INTERNAL; |
266 | 0 | } |
267 | 84 | return 0; |
268 | 84 | } |