Coverage Report

Created: 2026-11-30 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/pkcs15.c
Line
Count
Source
1
/*
2
 * pkcs15.c: PKCS #15 general 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 <stdlib.h>
26
#include <string.h>
27
#include <stdio.h>
28
#include <ctype.h>
29
30
#include "asn1.h"
31
#include "cardctl.h"
32
#include "common/compat_strlcat.h"
33
#include "common/compat_strlcpy.h"
34
#include "common/libscdl.h"
35
#include "internal.h"
36
#include "pkcs15.h"
37
#include "simpletlv.h"
38
39
#ifdef ENABLE_OPENSSL
40
#include <openssl/sha.h>
41
#endif
42
43
#ifdef HAVE_SYS_TIME_H
44
#include <sys/time.h>
45
#endif
46
47
#ifdef ENABLE_ZLIB
48
#include "compression.h"
49
#endif
50
51
static const struct sc_asn1_entry c_asn1_twlabel[] = {
52
  { "twlabel", SC_ASN1_UTF8STRING, SC_ASN1_TAG_UTF8STRING, 0, NULL, NULL },
53
  { NULL, 0, 0, 0, NULL, NULL }
54
};
55
56
// clang-format off
57
static const struct sc_asn1_entry c_asn1_algorithm_info[7] = {
58
  { "reference",    SC_ASN1_INTEGER,  SC_ASN1_TAG_INTEGER,  0,                NULL, NULL },
59
  { "algorithmPKCS#11", SC_ASN1_INTEGER,  SC_ASN1_TAG_INTEGER,  0,                NULL, NULL },
60
  { "parameters",   SC_ASN1_CHOICE,   0,      SC_ASN1_OPTIONAL, NULL, NULL },
61
  { "supportedOperations",SC_ASN1_BIT_FIELD,  SC_ASN1_TAG_BIT_STRING, 0,                NULL, NULL },
62
  { "objId",    SC_ASN1_OBJECT,   SC_ASN1_TAG_OBJECT, SC_ASN1_OPTIONAL, NULL, NULL },
63
  { "algRef",   SC_ASN1_INTEGER,  SC_ASN1_TAG_INTEGER,  SC_ASN1_OPTIONAL, NULL, NULL },
64
  { NULL, 0, 0, 0, NULL, NULL }
65
};
66
// clang-format on
67
68
static const struct sc_asn1_entry c_asn1_algorithm_info_parameters[3] = {
69
  { "PKCS15RSAParameters",SC_ASN1_NULL,   SC_ASN1_TAG_NULL, 0, NULL, NULL },
70
  { "PKCS15ECParameters", SC_ASN1_OBJECT,   SC_ASN1_TAG_OBJECT, 0, NULL, NULL },
71
  { NULL, 0, 0, 0, NULL, NULL }
72
};
73
74
/*
75
 * in src/libopensc/types.h SC_MAX_SUPPORTED_ALGORITHMS  defined as 16
76
 */
77
static const struct sc_asn1_entry c_asn1_supported_algorithms[SC_MAX_SUPPORTED_ALGORITHMS + 1] = {
78
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
79
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
80
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
81
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
82
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
83
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
84
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
85
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
86
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
87
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
88
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
89
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
90
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
91
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
92
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
93
  { "algorithmInfo", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
94
  { NULL, 0, 0, 0, NULL, NULL }
95
};
96
97
#define C_ASN1_LAST_UPDATE_SIZE 3
98
static const struct sc_asn1_entry c_asn1_last_update[C_ASN1_LAST_UPDATE_SIZE] = {
99
  { "generalizedTime",  SC_ASN1_GENERALIZEDTIME, SC_ASN1_TAG_GENERALIZEDTIME, SC_ASN1_OPTIONAL, NULL, NULL },
100
  { "referencedTime", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS,  SC_ASN1_OPTIONAL, NULL, NULL },
101
  { NULL, 0, 0, 0, NULL, NULL }
102
};
103
104
#define C_ASN1_PROFILE_INDICATION_SIZE 3
105
static const struct sc_asn1_entry c_asn1_profile_indication[C_ASN1_PROFILE_INDICATION_SIZE] = {
106
  { "profileOID",   SC_ASN1_OBJECT,   SC_ASN1_TAG_OBJECT,     SC_ASN1_OPTIONAL, NULL, NULL },
107
  { "profileName",  SC_ASN1_UTF8STRING, SC_ASN1_TAG_UTF8STRING, SC_ASN1_OPTIONAL, NULL, NULL },
108
  { NULL, 0, 0, 0, NULL, NULL }
109
};
110
111
#define C_ASN1_TOKI_ATTRS_SIZE 15
112
static const struct sc_asn1_entry c_asn1_toki_attrs[C_ASN1_TOKI_ATTRS_SIZE] = {
113
  { "version",      SC_ASN1_INTEGER,    SC_ASN1_TAG_INTEGER, 0, NULL, NULL },
114
  { "serialNumber",   SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_OPTIONAL, NULL, NULL },
115
  { "manufacturerID", SC_ASN1_UTF8STRING,   SC_ASN1_TAG_UTF8STRING, SC_ASN1_OPTIONAL, NULL, NULL },
116
  { "label",      SC_ASN1_UTF8STRING,   SC_ASN1_CTX | 0, SC_ASN1_OPTIONAL, NULL, NULL },
117
  /* XXX the Taiwanese ID card erroneously uses explicit tagging */
118
  { "label-tw",       SC_ASN1_STRUCT,   SC_ASN1_CTX | 0 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
119
  { "tokenflags",     SC_ASN1_BIT_FIELD,    SC_ASN1_TAG_BIT_STRING, 0, NULL, NULL },
120
  { "seInfo",     SC_ASN1_SE_INFO,    SC_ASN1_CONS | SC_ASN1_TAG_SEQUENCE, SC_ASN1_OPTIONAL, NULL, NULL },
121
  { "recordInfo",     SC_ASN1_STRUCT,   SC_ASN1_CONS | SC_ASN1_CTX | 1, SC_ASN1_OPTIONAL, NULL, NULL },
122
  { "supportedAlgorithms", SC_ASN1_STRUCT,  SC_ASN1_CONS | SC_ASN1_CTX | 2, SC_ASN1_OPTIONAL, NULL, NULL },
123
  { "issuerId",       SC_ASN1_UTF8STRING,   SC_ASN1_CTX | 3, SC_ASN1_OPTIONAL, NULL, NULL },
124
  { "holderId",       SC_ASN1_UTF8STRING,   SC_ASN1_CTX | 4, SC_ASN1_OPTIONAL, NULL, NULL },
125
  { "lastUpdate",     SC_ASN1_STRUCT,   SC_ASN1_CONS | SC_ASN1_CTX | 5, SC_ASN1_OPTIONAL, NULL, NULL },
126
  { "preferredLanguage", SC_ASN1_PRINTABLESTRING, SC_ASN1_TAG_PRINTABLESTRING, SC_ASN1_OPTIONAL, NULL, NULL },
127
  { "profileIndication", SC_ASN1_STRUCT,    SC_ASN1_CONS | SC_ASN1_CTX | 6, SC_ASN1_OPTIONAL, NULL, NULL },
128
  { NULL, 0, 0, 0, NULL, NULL }
129
};
130
131
static const struct sc_asn1_entry c_asn1_tokeninfo[] = {
132
  { "TokenInfo", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_TAG_SEQUENCE, 0, NULL, NULL },
133
  { NULL, 0, 0, 0, NULL, NULL }
134
};
135
136
static void sc_pkcs15_free_unusedspace(struct sc_pkcs15_card *);
137
static void sc_pkcs15_remove_dfs(struct sc_pkcs15_card *);
138
static void sc_pkcs15_remove_objects(struct sc_pkcs15_card *);
139
static int sc_pkcs15_aux_get_md_guid(struct sc_pkcs15_card *, const struct sc_pkcs15_object *,
140
    unsigned, unsigned char *, size_t *);
141
static void sc_pkcs15_clear_tokeninfo(struct sc_pkcs15_tokeninfo *tokeninfo);
142
143
int sc_pkcs15_parse_tokeninfo(sc_context_t *ctx,
144
  sc_pkcs15_tokeninfo_t *ti, const u8 *buf, size_t blen)
145
938
{
146
938
  int r;
147
938
  size_t ii;
148
938
  u8 serial[128];
149
938
  size_t serial_len = sizeof(serial);
150
938
  u8 mnfid[SC_PKCS15_MAX_LABEL_SIZE];
151
938
  size_t mnfid_len  = sizeof(mnfid) - 1;
152
938
  u8 label[SC_PKCS15_MAX_LABEL_SIZE];
153
938
  size_t label_len = sizeof(label) - 1;
154
938
  u8 last_update[32], profile_indication[SC_PKCS15_MAX_LABEL_SIZE];
155
938
  size_t lupdate_len = sizeof(last_update) - 1, pi_len = sizeof(profile_indication) - 1;
156
938
  size_t flags_len   = sizeof(ti->flags);
157
938
  u8 preferred_language[64];
158
938
  size_t lang_length = sizeof(preferred_language) - 1;
159
938
  struct sc_asn1_entry asn1_supported_algorithms[SC_MAX_SUPPORTED_ALGORITHMS + 1],
160
938
      asn1_algo_infos[SC_MAX_SUPPORTED_ALGORITHMS][7],
161
938
      asn1_algo_infos_parameters[SC_MAX_SUPPORTED_ALGORITHMS][3];
162
938
  size_t reference_len = sizeof(ti->supported_algos[0].reference);
163
938
  size_t mechanism_len = sizeof(ti->supported_algos[0].mechanism);
164
938
  size_t parameter_len = sizeof(ti->supported_algos[0].parameters);
165
938
  size_t operations_len = sizeof(ti->supported_algos[0].operations);
166
938
  size_t algo_ref_len = sizeof(ti->supported_algos[0].algo_ref);
167
168
938
  struct sc_asn1_entry asn1_last_update[C_ASN1_LAST_UPDATE_SIZE];
169
938
  struct sc_asn1_entry asn1_profile_indication[C_ASN1_PROFILE_INDICATION_SIZE];
170
938
  struct sc_asn1_entry asn1_toki_attrs[C_ASN1_TOKI_ATTRS_SIZE], asn1_tokeninfo[3], asn1_twlabel[3];
171
172
938
  memset(last_update, 0, sizeof(last_update));
173
938
  memset(label, 0, sizeof(label));
174
938
  memset(profile_indication, 0, sizeof(profile_indication));
175
938
  memset(mnfid, 0, sizeof(mnfid));
176
177
938
  sc_copy_asn1_entry(c_asn1_twlabel, asn1_twlabel);
178
938
  sc_copy_asn1_entry(c_asn1_toki_attrs, asn1_toki_attrs);
179
938
  sc_copy_asn1_entry(c_asn1_tokeninfo, asn1_tokeninfo);
180
938
  sc_copy_asn1_entry(c_asn1_last_update, asn1_last_update);
181
938
  sc_format_asn1_entry(asn1_twlabel, label, &label_len, 0);
182
938
  sc_copy_asn1_entry(c_asn1_profile_indication, asn1_profile_indication);
183
184
15.9k
  for (ii=0; ii<SC_MAX_SUPPORTED_ALGORITHMS; ii++) {
185
15.0k
    sc_copy_asn1_entry(c_asn1_algorithm_info, asn1_algo_infos[ii]);
186
15.0k
    sc_copy_asn1_entry(c_asn1_algorithm_info_parameters,
187
15.0k
      asn1_algo_infos_parameters[ii]);
188
15.0k
  }
189
938
  sc_copy_asn1_entry(c_asn1_supported_algorithms, asn1_supported_algorithms);
190
191
15.9k
  for (ii=0; ii<SC_MAX_SUPPORTED_ALGORITHMS; ii++)   {
192
15.0k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 0, &ti->supported_algos[ii].reference, &reference_len, 0);
193
15.0k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 1, &ti->supported_algos[ii].mechanism, &mechanism_len, 0);
194
15.0k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 2,
195
15.0k
      asn1_algo_infos_parameters[ii], NULL, 0);
196
15.0k
    sc_format_asn1_entry(asn1_algo_infos_parameters[ii] + 0,
197
15.0k
      NULL, NULL, 0);
198
15.0k
    sc_format_asn1_entry(asn1_algo_infos_parameters[ii] + 1,
199
15.0k
      &ti->supported_algos[ii].parameters, &parameter_len, 0);
200
15.0k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 3, &ti->supported_algos[ii].operations, &operations_len, 0);
201
15.0k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 4, &ti->supported_algos[ii].algo_id, NULL, 1);
202
15.0k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 5, &ti->supported_algos[ii].algo_ref, &algo_ref_len, 0);
203
15.0k
    sc_format_asn1_entry(asn1_supported_algorithms + ii, asn1_algo_infos[ii], NULL, 0);
204
15.0k
  }
205
206
938
  sc_format_asn1_entry(asn1_last_update + 0, last_update, &lupdate_len, 0);
207
938
  sc_format_asn1_entry(asn1_last_update + 1, &ti->last_update.path, NULL, 0);
208
209
938
  sc_format_asn1_entry(asn1_profile_indication + 0, &ti->profile_indication.oid, NULL, 0);
210
938
  sc_format_asn1_entry(asn1_profile_indication + 1, profile_indication, &pi_len, 0);
211
212
938
  sc_format_asn1_entry(asn1_toki_attrs + 0, &ti->version, NULL, 0);
213
938
  sc_format_asn1_entry(asn1_toki_attrs + 1, serial, &serial_len, 0);
214
938
  sc_format_asn1_entry(asn1_toki_attrs + 2, mnfid, &mnfid_len, 0);
215
938
  sc_format_asn1_entry(asn1_toki_attrs + 3, label, &label_len, 0);
216
938
  sc_format_asn1_entry(asn1_toki_attrs + 4, asn1_twlabel, NULL, 0);
217
938
  sc_format_asn1_entry(asn1_toki_attrs + 5, &ti->flags, &flags_len, 0);
218
938
  sc_format_asn1_entry(asn1_toki_attrs + 6, &ti->seInfo, &ti->num_seInfo, 0);
219
938
  sc_format_asn1_entry(asn1_toki_attrs + 7, NULL, NULL, 0);
220
938
  sc_format_asn1_entry(asn1_toki_attrs + 8, asn1_supported_algorithms, NULL, 0);
221
938
  sc_format_asn1_entry(asn1_toki_attrs + 9, NULL, NULL, 0);
222
938
  sc_format_asn1_entry(asn1_toki_attrs + 10, NULL, NULL, 0);
223
938
  sc_format_asn1_entry(asn1_toki_attrs + 11, asn1_last_update, NULL, 0);
224
938
  sc_format_asn1_entry(asn1_toki_attrs + 12, preferred_language, &lang_length, 0);
225
938
  sc_format_asn1_entry(asn1_toki_attrs + 13, asn1_profile_indication, NULL, 0);
226
938
  sc_format_asn1_entry(asn1_tokeninfo, asn1_toki_attrs, NULL, 0);
227
228
938
  r = sc_asn1_decode(ctx, asn1_tokeninfo, buf, blen, NULL, NULL);
229
938
  if (r != SC_SUCCESS) {
230
    /* The decoding could have allocated something we need to free */
231
483
    sc_pkcs15_clear_tokeninfo(ti);
232
483
    LOG_TEST_RET(ctx, r, "ASN.1 parsing of EF(TokenInfo) failed");
233
483
  }
234
235
455
  if (asn1_toki_attrs[1].flags & SC_ASN1_PRESENT && serial_len > 0)   {
236
259
    free(ti->serial_number);
237
259
    ti->serial_number = malloc(serial_len * 2 + 1);
238
259
    if (ti->serial_number == NULL)
239
0
      return SC_ERROR_OUT_OF_MEMORY;
240
259
    sc_bin_to_hex(serial, serial_len, ti->serial_number, serial_len * 2 + 1, 0);
241
259
    sc_log(ctx, "TokenInfo.serialNunmber '%s'", ti->serial_number);
242
259
  }
243
244
455
  if (ti->manufacturer_id == NULL) {
245
455
    if (asn1_toki_attrs[2].flags & SC_ASN1_PRESENT)
246
105
      ti->manufacturer_id = strdup((char *) mnfid);
247
350
    else
248
350
      ti->manufacturer_id = strdup("(unknown)");
249
455
    if (ti->manufacturer_id == NULL)
250
0
      return SC_ERROR_OUT_OF_MEMORY;
251
455
  }
252
455
  if (ti->label == NULL) {
253
455
    if (asn1_toki_attrs[3].flags & SC_ASN1_PRESENT ||
254
448
        asn1_toki_attrs[4].flags & SC_ASN1_PRESENT)
255
7
      ti->label = strdup((char *) label);
256
448
    else
257
448
      ti->label = strdup("(unknown)");
258
455
    if (ti->label == NULL)
259
0
      return SC_ERROR_OUT_OF_MEMORY;
260
455
  }
261
455
  if (asn1_toki_attrs[11].flags & SC_ASN1_PRESENT) {
262
172
    if (asn1_last_update[0].flags & SC_ASN1_PRESENT)   {
263
38
      sc_log(ctx, "LastUpdate.generalizedTime present");
264
38
      ti->last_update.gtime = strdup((char *)last_update);
265
38
      if (ti->last_update.gtime == NULL)
266
0
        return SC_ERROR_OUT_OF_MEMORY;
267
38
    }
268
134
    else if (asn1_last_update[1].flags & SC_ASN1_PRESENT)  {
269
99
      sc_log(ctx, "LastUpdate.referencedTime present");
270
99
    }
271
172
  }
272
455
  if (asn1_toki_attrs[12].flags & SC_ASN1_PRESENT) {
273
5
    preferred_language[lang_length] = 0;
274
5
    ti->preferred_language = strdup((char *)preferred_language);
275
5
    if (ti->preferred_language == NULL)
276
0
      return SC_ERROR_OUT_OF_MEMORY;
277
5
  }
278
279
455
  sc_init_oid(&ti->profile_indication.oid);
280
455
  if (asn1_toki_attrs[13].flags & SC_ASN1_PRESENT) {
281
106
    if (asn1_profile_indication[0].flags & SC_ASN1_PRESENT)   {
282
3
      sc_log(ctx, "ProfileIndication.oid present");
283
3
    }
284
103
    else if (asn1_profile_indication[1].flags & SC_ASN1_PRESENT)  {
285
100
      sc_log(ctx, "ProfileIndication.name present");
286
100
      ti->profile_indication.name = strdup((char *)profile_indication);
287
100
      if (ti->profile_indication.name == NULL)
288
0
        return SC_ERROR_OUT_OF_MEMORY;
289
100
    }
290
106
  }
291
292
455
  sc_log(ctx, "LastUpdate.path '%s'", sc_print_path(&ti->last_update.path));
293
455
  sc_log(ctx, "ProfileIndication.name '%s'",  ti->profile_indication.name);
294
455
  return SC_SUCCESS;
295
455
}
296
297
298
int
299
sc_pkcs15_encode_tokeninfo(sc_context_t *ctx, sc_pkcs15_tokeninfo_t *ti,
300
    u8 **buf, size_t *buflen)
301
0
{
302
0
  int r, ii;
303
0
  size_t serial_len, mnfid_len, label_len, flags_len, last_upd_len, pi_len, lang_len;
304
305
0
  struct sc_asn1_entry asn1_toki_attrs[C_ASN1_TOKI_ATTRS_SIZE];
306
0
  struct sc_asn1_entry asn1_tokeninfo[2];
307
0
  struct sc_asn1_entry asn1_supported_algorithms[SC_MAX_SUPPORTED_ALGORITHMS + 1],
308
0
      asn1_algo_infos[SC_MAX_SUPPORTED_ALGORITHMS][7],
309
0
      asn1_algo_infos_parameters[SC_MAX_SUPPORTED_ALGORITHMS][3];
310
0
  size_t reference_len = sizeof(ti->supported_algos[0].reference);
311
0
  size_t mechanism_len = sizeof(ti->supported_algos[0].mechanism);
312
0
  size_t parameter_len = sizeof(ti->supported_algos[0].parameters);
313
0
  size_t operations_len = sizeof(ti->supported_algos[0].operations);
314
0
  size_t algo_ref_len = sizeof(ti->supported_algos[0].algo_ref);
315
0
  struct sc_asn1_entry asn1_last_update[C_ASN1_LAST_UPDATE_SIZE];
316
0
  struct sc_asn1_entry asn1_profile_indication[C_ASN1_PROFILE_INDICATION_SIZE];
317
0
  u8 serial[128];
318
319
0
  sc_copy_asn1_entry(c_asn1_toki_attrs, asn1_toki_attrs);
320
0
  sc_copy_asn1_entry(c_asn1_tokeninfo, asn1_tokeninfo);
321
0
  sc_copy_asn1_entry(c_asn1_last_update, asn1_last_update);
322
0
  sc_copy_asn1_entry(c_asn1_profile_indication, asn1_profile_indication);
323
324
0
  for (ii=0; ii<SC_MAX_SUPPORTED_ALGORITHMS && ti->supported_algos[ii].reference; ii++) {
325
0
    sc_copy_asn1_entry(c_asn1_algorithm_info, asn1_algo_infos[ii]);
326
0
    sc_copy_asn1_entry(c_asn1_algorithm_info_parameters,
327
0
      asn1_algo_infos_parameters[ii]);
328
0
  }
329
0
  sc_copy_asn1_entry(c_asn1_supported_algorithms, asn1_supported_algorithms);
330
331
0
  for (ii=0; ii<SC_MAX_SUPPORTED_ALGORITHMS && ti->supported_algos[ii].reference; ii++)   {
332
0
    sc_format_asn1_entry(asn1_algo_infos[ii] + 0, &ti->supported_algos[ii].reference, &reference_len, 1);
333
0
    sc_format_asn1_entry(asn1_algo_infos[ii] + 1, &ti->supported_algos[ii].mechanism, &mechanism_len, 1);
334
0
    sc_format_asn1_entry(asn1_algo_infos[ii] + 2,
335
0
      asn1_algo_infos_parameters[ii], NULL, 1);
336
0
    if (!sc_valid_oid(&ti->supported_algos[ii].parameters)) {
337
0
      sc_format_asn1_entry(asn1_algo_infos_parameters[ii] + 0,
338
0
        NULL, NULL, 1);
339
0
    }
340
0
    else {
341
0
      sc_format_asn1_entry(asn1_algo_infos_parameters[ii] + 1,
342
0
        &ti->supported_algos[ii].parameters, &parameter_len, 0);
343
0
    }
344
0
    sc_format_asn1_entry(asn1_algo_infos[ii] + 3, &ti->supported_algos[ii].operations, &operations_len, 1);
345
0
    sc_format_asn1_entry(asn1_algo_infos[ii] + 4, &ti->supported_algos[ii].algo_id, NULL, 1);
346
0
    sc_format_asn1_entry(asn1_algo_infos[ii] + 5, &ti->supported_algos[ii].algo_ref, &algo_ref_len, 1);
347
0
    sc_format_asn1_entry(asn1_supported_algorithms + ii, asn1_algo_infos[ii], NULL, 1);
348
0
  }
349
350
0
  sc_format_asn1_entry(asn1_toki_attrs + 0, &ti->version, NULL, 1);
351
0
  if (ti->serial_number != NULL) {
352
0
    serial_len = 0;
353
0
    if (strlen(ti->serial_number)/2 > sizeof(serial))
354
0
      return SC_ERROR_BUFFER_TOO_SMALL;
355
0
    serial_len = sizeof(serial);
356
0
    if (sc_hex_to_bin(ti->serial_number, serial, &serial_len) < 0)
357
0
      return SC_ERROR_INVALID_ARGUMENTS;
358
0
    sc_format_asn1_entry(asn1_toki_attrs + 1, serial, &serial_len, 1);
359
0
  }
360
0
  else   {
361
0
    sc_format_asn1_entry(asn1_toki_attrs + 1, NULL, NULL, 0);
362
0
  }
363
364
0
  if (ti->manufacturer_id != NULL) {
365
0
    mnfid_len = strlen(ti->manufacturer_id);
366
0
    sc_format_asn1_entry(asn1_toki_attrs + 2, ti->manufacturer_id, &mnfid_len, 1);
367
0
  }
368
0
  else    {
369
0
    sc_format_asn1_entry(asn1_toki_attrs + 2, NULL, NULL, 0);
370
0
  }
371
372
0
  if (ti->label != NULL) {
373
0
    label_len = strlen(ti->label);
374
0
    sc_format_asn1_entry(asn1_toki_attrs + 3, ti->label, &label_len, 1);
375
0
  }
376
0
  else   {
377
0
    sc_format_asn1_entry(asn1_toki_attrs + 3, NULL, NULL, 0);
378
0
  }
379
380
0
  if (ti->flags) {
381
0
    flags_len = sizeof(ti->flags);
382
0
    sc_format_asn1_entry(asn1_toki_attrs + 5, &ti->flags, &flags_len, 1);
383
0
  }
384
0
  else   {
385
0
    sc_format_asn1_entry(asn1_toki_attrs + 5, NULL, NULL, 0);
386
0
  }
387
388
0
  if (ti->num_seInfo)
389
0
    sc_format_asn1_entry(asn1_toki_attrs + 6, ti->seInfo, &ti->num_seInfo, 1);
390
0
  else
391
0
    sc_format_asn1_entry(asn1_toki_attrs + 6, NULL, NULL, 0);
392
393
0
  sc_format_asn1_entry(asn1_toki_attrs + 7, NULL, NULL, 0);
394
395
0
  if (ti->supported_algos[0].reference)
396
0
    sc_format_asn1_entry(asn1_toki_attrs + 8, asn1_supported_algorithms, NULL, 1);
397
0
  else
398
0
    sc_format_asn1_entry(asn1_toki_attrs + 8, NULL, NULL, 0);
399
400
0
  sc_format_asn1_entry(asn1_toki_attrs + 9, NULL, NULL, 0);
401
0
  sc_format_asn1_entry(asn1_toki_attrs + 10, NULL, NULL, 0);
402
403
0
  if (ti->last_update.path.len) {
404
0
    sc_format_asn1_entry(asn1_last_update + 0, &ti->last_update.path, NULL, 1);
405
0
    sc_format_asn1_entry(asn1_toki_attrs + 11, asn1_last_update, NULL, 1);
406
0
  }
407
0
  else if (ti->last_update.gtime != NULL) {
408
0
    last_upd_len = strlen(ti->last_update.gtime);
409
0
    sc_format_asn1_entry(asn1_last_update + 0, ti->last_update.gtime, &last_upd_len, 1);
410
0
    sc_format_asn1_entry(asn1_toki_attrs + 11, asn1_last_update, NULL, 1);
411
0
  }
412
0
  else   {
413
0
    sc_format_asn1_entry(asn1_toki_attrs + 11, NULL, NULL, 0);
414
0
  }
415
416
0
  if (ti->preferred_language != NULL) {
417
0
    lang_len = strlen(ti->preferred_language);
418
0
    sc_format_asn1_entry(asn1_toki_attrs + 12, ti->preferred_language, &lang_len, 1);
419
0
  } else {
420
0
    sc_format_asn1_entry(asn1_toki_attrs + 12, NULL, NULL, 0);
421
0
  }
422
423
0
  if (sc_valid_oid(&ti->profile_indication.oid))   {
424
0
    sc_format_asn1_entry(asn1_profile_indication + 0, &ti->profile_indication.oid, NULL, 1);
425
0
    sc_format_asn1_entry(asn1_toki_attrs + 13, asn1_profile_indication, NULL, 1);
426
0
  }
427
0
  else if (ti->profile_indication.name)   {
428
0
    pi_len = strlen(ti->profile_indication.name);
429
0
    sc_format_asn1_entry(asn1_profile_indication + 1, ti->profile_indication.name, &pi_len, 1);
430
0
    sc_format_asn1_entry(asn1_toki_attrs + 13, asn1_profile_indication, NULL, 1);
431
0
  }
432
0
  else    {
433
0
    sc_format_asn1_entry(asn1_toki_attrs + 13, NULL, NULL, 0);
434
0
  }
435
436
0
  sc_format_asn1_entry(asn1_tokeninfo, asn1_toki_attrs, NULL, 1);
437
438
0
  r = sc_asn1_encode(ctx, asn1_tokeninfo, buf, buflen);
439
0
  LOG_TEST_RET(ctx, r, "sc_asn1_encode() failed");
440
441
0
  return SC_SUCCESS;
442
0
}
443
444
static const struct sc_asn1_entry c_asn1_ddo[] = {
445
  { "oid",     SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, SC_ASN1_OPTIONAL, NULL, NULL },
446
  { "odfPath",     SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_TAG_SEQUENCE, SC_ASN1_OPTIONAL, NULL, NULL },
447
  { "tokenInfoPath", SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_CTX | 0, SC_ASN1_OPTIONAL, NULL, NULL },
448
  { "unusedPath",    SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_CTX | 1, SC_ASN1_OPTIONAL, NULL, NULL },
449
/* According to PKCS#15 v1.1 here is the place for the future extensions.
450
 * The following data are used when ODF record points to the xDF files in a different application.
451
 */
452
  { "ddoIIN",    SC_ASN1_OCTET_STRING, SC_ASN1_APP | 0x02, SC_ASN1_OPTIONAL, NULL, NULL },
453
  { "ddoAID",    SC_ASN1_OCTET_STRING, SC_ASN1_APP | 0x0F, SC_ASN1_OPTIONAL, NULL, NULL },
454
  { NULL, 0, 0, 0, NULL, NULL }
455
};
456
457
static void
458
fix_authentic_ddo(struct sc_pkcs15_card *p15card)
459
36
{
460
  /* AuthentIC v3.2 card has invalid ODF and tokenInfo paths encoded into DDO.
461
   * Cleanup this attributes -- default values must be OK.
462
   */
463
36
  if (p15card->card->type == SC_CARD_TYPE_OBERTHUR_AUTHENTIC_3_2)   {
464
0
    sc_file_free(p15card->file_odf);
465
0
    p15card->file_odf = NULL;
466
0
    sc_file_free(p15card->file_tokeninfo);
467
0
    p15card->file_tokeninfo = NULL;
468
0
  }
469
36
}
470
471
static int
472
parse_ddo(struct sc_pkcs15_card *p15card, const u8 * buf, size_t buflen)
473
48
{
474
48
  struct sc_context *ctx = p15card->card->ctx;
475
48
  struct sc_asn1_entry asn1_ddo[7];
476
48
  sc_path_t odf_path, ti_path, us_path;
477
48
  struct sc_iid iid;
478
48
  struct sc_aid aid;
479
48
  int r;
480
481
48
  LOG_FUNC_CALLED(ctx);
482
483
48
  iid.len = sizeof(iid.value);
484
48
  aid.len = sizeof(aid.value);
485
486
48
  sc_copy_asn1_entry(c_asn1_ddo, asn1_ddo);
487
48
  sc_format_asn1_entry(asn1_ddo + 1, &odf_path, NULL, 0);
488
48
  sc_format_asn1_entry(asn1_ddo + 2, &ti_path, NULL, 0);
489
48
  sc_format_asn1_entry(asn1_ddo + 3, &us_path, NULL, 0);
490
48
  sc_format_asn1_entry(asn1_ddo + 4, iid.value, &iid.len, 0);
491
48
  sc_format_asn1_entry(asn1_ddo + 5, aid.value, &aid.len, 0);
492
493
48
  r = sc_asn1_decode(ctx, asn1_ddo, buf, buflen, NULL, NULL);
494
48
  LOG_TEST_RET(ctx, r, "DDO parsing failed");
495
496
36
  if (asn1_ddo[1].flags & SC_ASN1_PRESENT) {
497
5
    sc_file_free(p15card->file_odf);
498
5
    p15card->file_odf = sc_file_new();
499
5
    if (p15card->file_odf == NULL)
500
0
      goto mem_err;
501
5
    p15card->file_odf->path = odf_path;
502
5
  }
503
36
  if (asn1_ddo[2].flags & SC_ASN1_PRESENT) {
504
11
    sc_file_free(p15card->file_tokeninfo);
505
11
    p15card->file_tokeninfo = sc_file_new();
506
11
    if (p15card->file_tokeninfo == NULL)
507
0
      goto mem_err;
508
11
    p15card->file_tokeninfo->path = ti_path;
509
11
  }
510
36
  if (asn1_ddo[3].flags & SC_ASN1_PRESENT) {
511
3
    sc_file_free(p15card->file_unusedspace);
512
3
    p15card->file_unusedspace = sc_file_new();
513
3
    if (p15card->file_unusedspace == NULL)
514
0
      goto mem_err;
515
3
    p15card->file_unusedspace->path = us_path;
516
3
  }
517
36
  if (asn1_ddo[4].flags & SC_ASN1_PRESENT) {
518
3
    sc_debug(ctx, SC_LOG_DEBUG_ASN1, "DDO.IID '%s'", sc_dump_hex(iid.value, iid.len));
519
3
    memcpy(&p15card->app->ddo.iid, &iid, sizeof(struct sc_iid));
520
3
  }
521
36
  if (asn1_ddo[5].flags & SC_ASN1_PRESENT) {
522
4
    sc_debug(ctx, SC_LOG_DEBUG_ASN1, "DDO.AID '%s'", sc_dump_hex(aid.value, aid.len));
523
4
    memcpy(&p15card->app->ddo.aid, &aid, sizeof(struct sc_aid));
524
4
  }
525
526
36
  fix_authentic_ddo(p15card);
527
36
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
528
0
mem_err:
529
0
  sc_file_free(p15card->file_odf);
530
0
  p15card->file_odf = NULL;
531
0
  sc_file_free(p15card->file_tokeninfo);
532
0
  p15card->file_tokeninfo = NULL;
533
0
  sc_file_free(p15card->file_unusedspace);
534
0
  p15card->file_unusedspace = NULL;
535
0
  LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
536
0
}
537
538
539
char *
540
sc_pkcs15_get_lastupdate(struct sc_pkcs15_card *p15card)
541
3.80k
{
542
3.80k
  struct sc_context *ctx  = p15card->card->ctx;
543
3.80k
  struct sc_file *file = NULL;
544
3.80k
  struct sc_asn1_entry asn1_last_update[C_ASN1_LAST_UPDATE_SIZE];
545
3.80k
  unsigned char *content, last_update[32] = {0};
546
3.80k
  size_t lupdate_len = sizeof(last_update) - 1;
547
3.80k
  int r, content_len;
548
3.80k
  size_t size;
549
550
3.80k
  if (p15card->tokeninfo->last_update.gtime)
551
334
    goto done;
552
553
3.46k
  if (!p15card->tokeninfo->last_update.path.len)
554
2.79k
    return NULL;
555
556
671
  r = sc_select_file(p15card->card, &p15card->tokeninfo->last_update.path, &file);
557
671
  if (r < 0)
558
583
    return NULL;
559
560
88
  size = file->size ? file->size : 1024;
561
88
  sc_file_free(file);
562
563
88
  content = calloc(1, size);
564
88
  if (!content)
565
0
    return NULL;
566
567
88
  r = sc_read_binary(p15card->card, 0, content, size, 0);
568
88
  if (r < 0) {
569
36
    free(content);
570
36
    return NULL;
571
36
  }
572
52
  content_len = r;
573
574
52
  sc_copy_asn1_entry(c_asn1_last_update, asn1_last_update);
575
52
  sc_format_asn1_entry(asn1_last_update + 0, last_update, &lupdate_len, 0);
576
577
52
  r = sc_asn1_decode(ctx, asn1_last_update, content, content_len, NULL, NULL);
578
52
  free(content);
579
52
  if (r < 0)
580
24
    return NULL;
581
582
28
  if (asn1_last_update[0].flags & SC_ASN1_PRESENT) {
583
2
    p15card->tokeninfo->last_update.gtime = strdup((char *)last_update);
584
2
    if (!p15card->tokeninfo->last_update.gtime)
585
0
      return NULL;
586
2
  }
587
362
done:
588
362
  sc_log(ctx, "lastUpdate.gtime '%s'", p15card->tokeninfo->last_update.gtime);
589
362
  return p15card->tokeninfo->last_update.gtime;
590
28
}
591
592
593
static const struct sc_asn1_entry c_asn1_odf[] = {
594
  { "privateKeys",   SC_ASN1_STRUCT, SC_ASN1_CTX | 0 | SC_ASN1_CONS, 0, NULL, NULL },
595
  { "publicKeys",    SC_ASN1_STRUCT, SC_ASN1_CTX | 1 | SC_ASN1_CONS, 0, NULL, NULL },
596
  { "trustedPublicKeys",   SC_ASN1_STRUCT, SC_ASN1_CTX | 2 | SC_ASN1_CONS, 0, NULL, NULL },
597
  { "secretKeys",    SC_ASN1_STRUCT, SC_ASN1_CTX | 3 | SC_ASN1_CONS, 0, NULL, NULL },
598
  { "certificates",  SC_ASN1_STRUCT, SC_ASN1_CTX | 4 | SC_ASN1_CONS, 0, NULL, NULL },
599
  { "trustedCertificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 5 | SC_ASN1_CONS, 0, NULL, NULL },
600
  { "usefulCertificates",  SC_ASN1_STRUCT, SC_ASN1_CTX | 6 | SC_ASN1_CONS, 0, NULL, NULL },
601
  { "dataObjects",   SC_ASN1_STRUCT, SC_ASN1_CTX | 7 | SC_ASN1_CONS, 0, NULL, NULL },
602
  { "authObjects",   SC_ASN1_STRUCT, SC_ASN1_CTX | 8 | SC_ASN1_CONS, 0, NULL, NULL },
603
  { NULL, 0, 0, 0, NULL, NULL }
604
};
605
606
static const unsigned int odf_indexes[] = {
607
  SC_PKCS15_PRKDF,
608
  SC_PKCS15_PUKDF,
609
  SC_PKCS15_PUKDF_TRUSTED,
610
  SC_PKCS15_SKDF,
611
  SC_PKCS15_CDF,
612
  SC_PKCS15_CDF_TRUSTED,
613
  SC_PKCS15_CDF_USEFUL,
614
  SC_PKCS15_DODF,
615
  SC_PKCS15_AODF,
616
};
617
618
619
static int
620
parse_odf(const unsigned char * buf, size_t buflen, struct sc_pkcs15_card *p15card)
621
34
{
622
34
  const unsigned char *p = buf;
623
34
  size_t left = buflen;
624
34
  int r, i, type;
625
34
  struct sc_path path;
626
34
  struct sc_asn1_entry asn1_obj_or_path[] = {
627
34
    { "path", SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_SEQUENCE, 0, &path, NULL },
628
34
    { NULL, 0, 0, 0, NULL, NULL }
629
34
  };
630
34
  struct sc_asn1_entry asn1_odf[10];
631
632
34
  sc_copy_asn1_entry(c_asn1_odf, asn1_odf);
633
340
  for (i = 0; asn1_odf[i].name != NULL; i++)
634
306
    sc_format_asn1_entry(asn1_odf + i, asn1_obj_or_path, NULL, 0);
635
119
  while (left > 0) {
636
114
    r = sc_asn1_decode_choice(p15card->card->ctx, asn1_odf, p, left, &p, &left);
637
114
    if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
638
11
      break;
639
103
    if (r < 0)
640
18
      return r;
641
85
    type = r;
642
85
    if (p15card->file_app) {
643
85
      r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &path);
644
85
      if (r < 0)
645
0
        return r;
646
85
      r = sc_pkcs15_add_df(p15card, odf_indexes[type], &path);
647
85
      if (r)
648
0
        return r;
649
85
    }
650
85
  }
651
16
  return 0;
652
34
}
653
654
655
int
656
sc_pkcs15_encode_odf(struct sc_context *ctx, struct sc_pkcs15_card *p15card,
657
       unsigned char **buf, size_t *buflen)
658
0
{
659
0
  struct sc_path path;
660
0
  struct sc_asn1_entry asn1_obj_or_path[] = {
661
0
    { "path", SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_SEQUENCE, 0, &path, NULL },
662
0
    { NULL, 0, 0, 0, NULL, NULL }
663
0
  };
664
0
  struct sc_asn1_entry *asn1_paths = NULL;
665
0
  struct sc_asn1_entry *asn1_odf = NULL;
666
0
  int df_count = 0, r, c = 0;
667
0
  const int nr_indexes = sizeof(odf_indexes)/sizeof(odf_indexes[0]);
668
0
  struct sc_pkcs15_df *df;
669
670
0
  df = p15card->df_list;
671
0
  while (df != NULL) {
672
0
    df_count++;
673
0
    df = df->next;
674
0
  };
675
0
  if (df_count == 0)
676
0
    LOG_TEST_RET(ctx, SC_ERROR_OBJECT_NOT_FOUND, "No DF's found.");
677
678
0
  asn1_odf = malloc(sizeof(struct sc_asn1_entry) * (df_count + 1));
679
0
  if (asn1_odf == NULL) {
680
0
    r = SC_ERROR_OUT_OF_MEMORY;
681
0
    goto err;
682
0
  }
683
0
  asn1_paths = malloc(sizeof(struct sc_asn1_entry) * (df_count * 2));
684
0
  if (asn1_paths == NULL) {
685
0
    r = SC_ERROR_OUT_OF_MEMORY;
686
0
    goto err;
687
0
  }
688
0
  for (df = p15card->df_list; df != NULL; df = df->next) {
689
0
    int j, type = -1;
690
691
0
    for (j = 0; j < nr_indexes; j++)
692
0
      if (odf_indexes[j] == df->type) {
693
0
        type = j;
694
0
        break;
695
0
      }
696
0
    if (type == -1) {
697
0
      sc_log(ctx, "Unsupported DF type.");
698
0
      continue;
699
0
    }
700
0
    asn1_odf[c] = c_asn1_odf[type];
701
0
    sc_format_asn1_entry(asn1_odf + c, asn1_paths + 2*c, NULL, 1);
702
0
    sc_copy_asn1_entry(asn1_obj_or_path, asn1_paths + 2*c);
703
0
    sc_format_asn1_entry(asn1_paths + 2*c, &df->path, NULL, 1);
704
0
    c++;
705
0
  }
706
0
  asn1_odf[c].name = NULL;
707
0
  r = sc_asn1_encode(ctx, asn1_odf, buf, buflen);
708
0
err:
709
0
  if (asn1_paths != NULL)
710
0
    free(asn1_paths);
711
0
  if (asn1_odf != NULL)
712
0
    free(asn1_odf);
713
0
  return r;
714
0
}
715
716
717
struct sc_pkcs15_card *
718
sc_pkcs15_card_new(void)
719
13.0k
{
720
13.0k
  struct sc_pkcs15_card *p15card;
721
722
13.0k
  p15card = calloc(1, sizeof(struct sc_pkcs15_card));
723
13.0k
  if (p15card == NULL)
724
0
    return NULL;
725
726
13.0k
  p15card->tokeninfo = calloc(1, sizeof(struct sc_pkcs15_tokeninfo));
727
13.0k
  if (p15card->tokeninfo == NULL) {
728
0
    free(p15card);
729
0
    return NULL;
730
0
  }
731
732
13.0k
  p15card->magic = SC_PKCS15_CARD_MAGIC;
733
13.0k
  return p15card;
734
13.0k
}
735
736
737
struct sc_pkcs15_tokeninfo *
738
sc_pkcs15_tokeninfo_new(void)
739
9.14k
{
740
9.14k
  struct sc_pkcs15_tokeninfo *tokeninfo;
741
742
9.14k
  tokeninfo = calloc(1, sizeof(struct sc_pkcs15_tokeninfo));
743
9.14k
  if (tokeninfo == NULL) {
744
0
    return NULL;
745
0
  }
746
747
9.14k
  sc_init_oid(&tokeninfo->profile_indication.oid);
748
749
9.14k
  return tokeninfo;
750
9.14k
}
751
752
static void
753
sc_pkcs15_clear_tokeninfo(struct sc_pkcs15_tokeninfo *tokeninfo)
754
22.7k
{
755
22.7k
  if (!tokeninfo)
756
0
    return;
757
758
22.7k
  free(tokeninfo->label);
759
22.7k
  tokeninfo->label = NULL;
760
22.7k
  free(tokeninfo->serial_number);
761
22.7k
  tokeninfo->serial_number = NULL;
762
22.7k
  free(tokeninfo->manufacturer_id);
763
22.7k
  tokeninfo->manufacturer_id = NULL;
764
22.7k
  free(tokeninfo->last_update.gtime);
765
22.7k
  tokeninfo->last_update.gtime = NULL;
766
22.7k
  free(tokeninfo->preferred_language);
767
22.7k
  tokeninfo->preferred_language = NULL;
768
22.7k
  free(tokeninfo->profile_indication.name);
769
22.7k
  tokeninfo->profile_indication.name = NULL;
770
22.7k
  if (tokeninfo->seInfo != NULL) {
771
22
    unsigned i;
772
30
    for (i = 0; i < tokeninfo->num_seInfo; i++)
773
8
      free(tokeninfo->seInfo[i]);
774
22
    free(tokeninfo->seInfo);
775
22
    tokeninfo->seInfo = NULL;
776
22
  }
777
22.7k
}
778
779
void
780
sc_pkcs15_free_tokeninfo(struct sc_pkcs15_tokeninfo *tokeninfo)
781
23.6k
{
782
23.6k
  if (!tokeninfo)
783
1.41k
    return;
784
785
22.1k
  sc_pkcs15_clear_tokeninfo(tokeninfo);
786
22.1k
  free(tokeninfo);
787
22.1k
}
788
789
void
790
sc_pkcs15_free_app(struct sc_pkcs15_card *p15card)
791
41.3k
{
792
41.3k
  if (p15card && p15card->app) {
793
720
    free(p15card->app->label);
794
720
    free(p15card->app->ddo.value);
795
720
    free(p15card->app);
796
720
    p15card->app = NULL;
797
720
  }
798
41.3k
}
799
800
801
void
802
sc_pkcs15_card_free(struct sc_pkcs15_card *p15card)
803
13.0k
{
804
13.0k
  if (p15card == NULL || p15card->magic != SC_PKCS15_CARD_MAGIC)
805
0
    return;
806
807
13.0k
  if (p15card->ops.clear)
808
837
    p15card->ops.clear(p15card);
809
810
  /* For more complicated MD data a dedicated release procedure
811
   * has to be implemented. */
812
13.0k
  if (p15card->md_data)
813
0
    free(p15card->md_data);
814
815
13.0k
  sc_pkcs15_free_app(p15card);
816
13.0k
  sc_pkcs15_remove_objects(p15card);
817
13.0k
  sc_pkcs15_remove_dfs(p15card);
818
13.0k
  sc_pkcs15_free_unusedspace(p15card);
819
13.0k
  p15card->unusedspace_read = 0;
820
821
13.0k
  sc_file_free(p15card->file_app);
822
13.0k
  sc_file_free(p15card->file_tokeninfo);
823
13.0k
  sc_file_free(p15card->file_odf);
824
13.0k
  sc_file_free(p15card->file_unusedspace);
825
826
13.0k
  p15card->magic = 0;
827
13.0k
  sc_pkcs15_free_tokeninfo(p15card->tokeninfo);
828
13.0k
  sc_pkcs15_free_app(p15card);
829
13.0k
  free(p15card);
830
13.0k
}
831
832
833
void
834
sc_pkcs15_card_clear(struct sc_pkcs15_card *p15card)
835
14.8k
{
836
14.8k
  if (p15card == NULL)
837
0
    return;
838
839
14.8k
  if (p15card->ops.clear)
840
683
    p15card->ops.clear(p15card);
841
842
14.8k
  p15card->flags = 0;
843
14.8k
  p15card->tokeninfo->version = 0;
844
14.8k
  p15card->tokeninfo->flags   = 0;
845
846
14.8k
  sc_pkcs15_remove_objects(p15card);
847
14.8k
  sc_pkcs15_remove_dfs(p15card);
848
849
14.8k
  p15card->df_list = NULL;
850
14.8k
  sc_file_free(p15card->file_app);
851
14.8k
  p15card->file_app = NULL;
852
14.8k
  sc_file_free(p15card->file_tokeninfo);
853
14.8k
  p15card->file_tokeninfo = NULL;
854
14.8k
  sc_file_free(p15card->file_odf);
855
14.8k
  p15card->file_odf = NULL;
856
14.8k
  sc_file_free(p15card->file_unusedspace);
857
14.8k
  p15card->file_unusedspace = NULL;
858
859
14.8k
  free(p15card->tokeninfo->label);
860
14.8k
  p15card->tokeninfo->label = NULL;
861
14.8k
  free(p15card->tokeninfo->serial_number);
862
14.8k
  p15card->tokeninfo->serial_number = NULL;
863
14.8k
  free(p15card->tokeninfo->manufacturer_id);
864
14.8k
  p15card->tokeninfo->manufacturer_id = NULL;
865
14.8k
  free(p15card->tokeninfo->last_update.gtime);
866
14.8k
  p15card->tokeninfo->last_update.gtime = NULL;
867
14.8k
  free(p15card->tokeninfo->preferred_language);
868
14.8k
  p15card->tokeninfo->preferred_language = NULL;
869
14.8k
  free(p15card->tokeninfo->profile_indication.name);
870
14.8k
  p15card->tokeninfo->profile_indication.name = NULL;
871
14.8k
  if (p15card->tokeninfo->seInfo != NULL) {
872
4
    size_t i;
873
10
    for (i = 0; i < p15card->tokeninfo->num_seInfo; i++)
874
6
      free(p15card->tokeninfo->seInfo[i]);
875
4
    free(p15card->tokeninfo->seInfo);
876
4
    p15card->tokeninfo->seInfo     = NULL;
877
4
    p15card->tokeninfo->num_seInfo = 0;
878
4
  }
879
880
14.8k
  sc_pkcs15_free_app(p15card);
881
14.8k
}
882
883
884
struct sc_app_info *
885
sc_find_app(struct sc_card *card, struct sc_aid *aid)
886
12.3k
{
887
12.3k
  int ii;
888
889
12.3k
  if (card->app_count <= 0)
890
11.9k
    return NULL;
891
892
422
  if (!aid || !aid->len)
893
422
    return card->app[0];
894
895
0
  for (ii=0; ii < card->app_count; ii++) {
896
0
    if (card->app[ii]->aid.len != aid->len)
897
0
      continue;
898
0
    if (memcmp(card->app[ii]->aid.value, aid->value, aid->len))
899
0
      continue;
900
0
    return card->app[ii];
901
0
  }
902
0
  return NULL;
903
0
}
904
905
906
static struct sc_app_info *
907
sc_dup_app_info(const struct sc_app_info *info)
908
422
{
909
422
  struct sc_app_info *out = calloc(1, sizeof(struct sc_app_info));
910
911
422
  if (!out)
912
0
    return NULL;
913
914
422
  memcpy(out, info, sizeof(struct sc_app_info));
915
916
422
  if (info->label) {
917
18
    out->label = strdup(info->label);
918
18
    if (!out->label) {
919
0
      free(out);
920
0
      return NULL;
921
0
    }
922
18
  } else
923
404
    out->label = NULL;
924
925
422
  out->ddo.value = malloc(info->ddo.len);
926
422
  if (!out->ddo.value) {
927
0
    free(out->label);
928
0
    free(out);
929
0
    return NULL;
930
0
  }
931
422
  memcpy(out->ddo.value, info->ddo.value, info->ddo.len);
932
422
  out->ddo.len = info->ddo.len;
933
934
422
  return out;
935
422
}
936
937
938
struct sc_app_info *
939
sc_pkcs15_get_application_by_type(struct sc_card * card, char *app_type)
940
0
{
941
0
  struct sc_app_info *out = NULL;
942
0
  scconf_block *conf_block = NULL;
943
0
  int i, rv;
944
945
0
  if (!card)
946
0
    return NULL;
947
948
0
  if (card->app_count < 0)   {
949
0
    rv = sc_enum_apps(card);
950
0
    if (rv < 0 && rv != SC_ERROR_FILE_NOT_FOUND)
951
0
      return NULL;
952
0
  }
953
954
0
  conf_block = sc_get_conf_block(card->ctx, "framework", "pkcs15", 1);
955
0
  if (!conf_block)
956
0
    return NULL;
957
958
0
  for (i = 0; i < card->app_count; i++)   {
959
0
    struct sc_app_info *app_info = card->app[i];
960
0
    scconf_block **blocks = NULL;
961
0
    char str_path[SC_MAX_AID_STRING_SIZE];
962
963
0
    sc_bin_to_hex(app_info->aid.value, app_info->aid.len, str_path, sizeof(str_path), 0);
964
0
    blocks = scconf_find_blocks(card->ctx->conf, conf_block, "application", str_path);
965
0
    if (blocks)   {
966
0
      if (blocks[0])   {
967
0
        char *type = (char *)scconf_get_str(blocks[0], "type", app_type);
968
0
        if (!strcmp(type, app_type))   {
969
0
          out = app_info;
970
0
          free(blocks);
971
0
          break;
972
0
        }
973
0
      }
974
0
      free(blocks);
975
0
    }
976
0
  }
977
978
0
  return out;
979
0
}
980
981
int
982
sc_pkcs15_bind_internal_odf(struct sc_pkcs15_card *p15card)
983
586
{
984
586
  struct sc_path tmppath;
985
586
  struct sc_card    *card = p15card->card;
986
586
  struct sc_context *ctx = card->ctx;
987
586
  struct sc_pkcs15_df *df;
988
586
  unsigned char *buf = NULL;
989
586
  size_t len;
990
586
  int    err, ok = 0;
991
992
586
  LOG_FUNC_CALLED(ctx);
993
586
  if (p15card->file_odf == NULL) {
994
    /* check if an ODF is present; we don't know yet whether we have a pkcs15 card */
995
586
    sc_format_path("5031", &tmppath);
996
586
    err = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &tmppath);
997
586
    if (err != SC_SUCCESS)   {
998
0
      sc_log(ctx, "Cannot make absolute path to EF(ODF); error:%i", err);
999
0
      goto end;
1000
0
    }
1001
586
    sc_log(ctx, "absolute path to EF(ODF) %s", sc_print_path(&tmppath));
1002
586
    err = sc_select_file(card, &tmppath, &p15card->file_odf);
1003
586
  }
1004
0
  else {
1005
0
    tmppath = p15card->file_odf->path;
1006
0
    sc_file_free(p15card->file_odf);
1007
0
    p15card->file_odf = NULL;
1008
0
    err = sc_select_file(card, &tmppath, &p15card->file_odf);
1009
0
  }
1010
1011
586
  if (err != SC_SUCCESS) {
1012
344
    sc_log(ctx, "EF(ODF) not found in '%s'", sc_print_path(&tmppath));
1013
344
    goto end;
1014
344
  }
1015
1016
242
  len = p15card->file_odf->size;
1017
242
  if (!len) {
1018
108
    sc_log(ctx, "EF(ODF) is empty");
1019
108
    goto end;
1020
108
  }
1021
134
  if (len > MAX_FILE_SIZE) {
1022
31
    sc_log(ctx, "EF(ODF) too large");
1023
31
    goto end;
1024
31
  }
1025
103
  buf = malloc(len);
1026
103
  if(buf == NULL) {
1027
0
    err = SC_ERROR_OUT_OF_MEMORY;
1028
0
    goto end;
1029
0
  }
1030
1031
103
  err = -1; /* file state: not in cache */
1032
103
  if (p15card->opts.use_file_cache) {
1033
60
    err = sc_pkcs15_read_cached_file(p15card, &tmppath, &buf, &len);
1034
60
    if (err == SC_SUCCESS)
1035
2
      err = (int)len;
1036
60
  }
1037
103
  if (err < 0) {
1038
101
    err = sc_read_binary(card, 0, buf, len, 0);
1039
101
    if (err < 2) {
1040
69
      if (err < 0) {
1041
57
        sc_log(ctx, "read EF(ODF) file error: %s", sc_strerror(err));
1042
57
      } else {
1043
12
        err = SC_ERROR_PKCS15_APP_NOT_FOUND;
1044
12
        sc_log(ctx, "Invalid content of EF(ODF): %s", sc_strerror(err));
1045
12
      }
1046
69
      goto end;
1047
69
    }
1048
    /* sc_read_binary may return less than requested */
1049
32
    len = err;
1050
1051
32
    if (p15card->opts.use_file_cache) {
1052
22
      sc_pkcs15_cache_file(p15card, &tmppath, buf, len);
1053
22
    }
1054
32
  }
1055
1056
34
  if (parse_odf(buf, len, p15card)) {
1057
18
    err = SC_ERROR_PKCS15_APP_NOT_FOUND;
1058
18
    sc_log(ctx, "Unable to parse ODF");
1059
18
    goto end;
1060
18
  }
1061
16
  free(buf);
1062
16
  buf = NULL;
1063
1064
16
  sc_log(ctx, "The following DFs were found:");
1065
101
  for (df = p15card->df_list; df; df = df->next)
1066
85
    sc_log(ctx, "  DF type %u, path %s, index %u, count %d", df->type,
1067
16
        sc_print_path(&df->path), df->path.index, df->path.count);
1068
1069
16
  ok = 1;
1070
586
end:
1071
586
  if (buf != NULL)
1072
87
    free(buf);
1073
586
  if (!ok) {
1074
570
    LOG_FUNC_RETURN(ctx, err);
1075
570
  }
1076
1077
16
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1078
16
}
1079
1080
int
1081
sc_pkcs15_bind_internal_tokeninfo(struct sc_pkcs15_card *p15card)
1082
12.0k
{
1083
12.0k
  struct sc_path tmppath;
1084
12.0k
  struct sc_card *card = p15card->card;
1085
12.0k
  struct sc_context *ctx = card->ctx;
1086
12.0k
  struct sc_pkcs15_tokeninfo tokeninfo;
1087
12.0k
  unsigned char *buf = NULL;
1088
12.0k
  size_t len;
1089
12.0k
  int err, ok = 0;
1090
1091
12.0k
  LOG_FUNC_CALLED(ctx);
1092
1093
12.0k
  if (p15card->file_tokeninfo == NULL) {
1094
12.0k
    sc_format_path("5032", &tmppath);
1095
12.0k
    err = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &tmppath);
1096
12.0k
    if (err != SC_SUCCESS)   {
1097
3
      sc_log(ctx, "Cannot make absolute path to EF(TokenInfo); error:%i", err);
1098
3
      goto end;
1099
3
    }
1100
12.0k
    sc_log(ctx, "absolute path to EF(TokenInfo) %s", sc_print_path(&tmppath));
1101
12.0k
  }
1102
6
  else {
1103
6
    tmppath = p15card->file_tokeninfo->path;
1104
6
    sc_file_free(p15card->file_tokeninfo);
1105
6
    p15card->file_tokeninfo = NULL;
1106
6
  }
1107
1108
12.0k
  err = sc_select_file(card, &tmppath, &p15card->file_tokeninfo);
1109
12.0k
  if (err)   {
1110
10.9k
    sc_log(ctx, "cannot select EF(TokenInfo) file: %s", sc_strerror(err));
1111
10.9k
    goto end;
1112
10.9k
  }
1113
1114
1.06k
  len = p15card->file_tokeninfo->size;
1115
1.06k
  if (!len) {
1116
436
    sc_log(ctx, "EF(TokenInfo) is empty");
1117
436
    goto end;
1118
436
  }
1119
626
  if (len > MAX_FILE_SIZE) {
1120
65
    sc_log(ctx, "EF(TokenInfo) too large");
1121
65
    goto end;
1122
65
  }
1123
561
  buf = malloc(len);
1124
561
  if(buf == NULL) {
1125
0
    err = SC_ERROR_OUT_OF_MEMORY;
1126
0
    goto end;
1127
0
  }
1128
1129
561
  err = -1; /* file state: not in cache */
1130
561
  if (p15card->opts.use_file_cache) {
1131
199
    err = sc_pkcs15_read_cached_file(p15card, &tmppath, &buf, &len);
1132
199
    if (err == SC_SUCCESS)
1133
1
      err = (int)len;
1134
199
  }
1135
561
  if (err < 0) {
1136
560
    err = sc_read_binary(card, 0, buf, len, 0);
1137
560
    if (err <= 2) {
1138
342
      if (err < 0)   {
1139
301
        sc_log(ctx, "read EF(TokenInfo) file error: %s", sc_strerror(err));
1140
301
      } else {
1141
41
        err = SC_ERROR_PKCS15_APP_NOT_FOUND;
1142
41
        sc_log(ctx, "Invalid content of EF(TokenInfo): %s", sc_strerror(err));
1143
41
      }
1144
342
      goto end;
1145
342
    }
1146
    /* sc_read_binary may return less than requested */
1147
218
    len = err;
1148
1149
218
    if (p15card->opts.use_file_cache) {
1150
120
      sc_pkcs15_cache_file(p15card, &tmppath, buf, len);
1151
120
    }
1152
218
  }
1153
1154
219
  memset(&tokeninfo, 0, sizeof(tokeninfo));
1155
219
  err = sc_pkcs15_parse_tokeninfo(ctx, &tokeninfo, buf, (size_t)err);
1156
219
  if (err != SC_SUCCESS)   {
1157
134
    sc_log(ctx, "cannot parse TokenInfo content: %s", sc_strerror(err));
1158
134
    goto end;
1159
134
  }
1160
1161
85
  sc_pkcs15_clear_tokeninfo(p15card->tokeninfo);
1162
85
  *(p15card->tokeninfo) = tokeninfo;
1163
1164
85
  if (!p15card->tokeninfo->serial_number && 0 == card->serialnr.len) {
1165
46
    sc_card_ctl(p15card->card, SC_CARDCTL_GET_SERIALNR, &card->serialnr);
1166
46
  }
1167
1168
85
  if (!p15card->tokeninfo->serial_number && card->serialnr.len)   {
1169
15
    char *serial = calloc(1, card->serialnr.len*2 + 1);
1170
15
    size_t ii;
1171
15
    if (!serial) {
1172
0
      err = SC_ERROR_OUT_OF_MEMORY;
1173
0
      goto end;
1174
0
    }
1175
1176
191
    for(ii=0;ii<card->serialnr.len;ii++)
1177
176
      sprintf(serial + ii*2, "%02X", *(card->serialnr.value + ii));
1178
1179
15
    p15card->tokeninfo->serial_number = serial;
1180
15
    sc_log(ctx, "p15card->tokeninfo->serial_number %s", p15card->tokeninfo->serial_number);
1181
15
  }
1182
1183
85
  ok = 1;
1184
12.0k
end:
1185
12.0k
  if (buf != NULL)
1186
561
    free(buf);
1187
12.0k
  if (!ok) {
1188
11.9k
    LOG_FUNC_RETURN(ctx, err);
1189
11.9k
  }
1190
1191
85
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1192
85
}
1193
1194
int
1195
sc_pkcs15_bind_internal(struct sc_pkcs15_card *p15card, struct sc_aid *aid)
1196
12.3k
{
1197
12.3k
  struct sc_card *card = p15card->card;
1198
12.3k
  struct sc_context *ctx = card->ctx;
1199
12.3k
  const struct sc_app_info *info = NULL;
1200
12.3k
  int err, ok = 0;
1201
1202
12.3k
  LOG_FUNC_CALLED(ctx);
1203
  /* Enumerate apps now */
1204
12.3k
  if (card->app_count < 0) {
1205
11.4k
    err = sc_enum_apps(card);
1206
11.4k
    if (err != SC_SUCCESS)
1207
11.1k
      sc_log(ctx, "unable to enumerate apps: %s", sc_strerror(err));
1208
11.4k
  }
1209
12.3k
  sc_file_free(p15card->file_app);
1210
12.3k
  p15card->file_app = sc_file_new();
1211
12.3k
  if (p15card->file_app == NULL) {
1212
0
    err = SC_ERROR_OUT_OF_MEMORY;
1213
0
    goto end;
1214
0
  }
1215
1216
12.3k
  sc_format_path("3F005015", &p15card->file_app->path);
1217
1218
12.3k
  info = sc_find_app(card, aid);
1219
12.3k
  if (info) {
1220
422
    sc_log(ctx, "bind to application('%s',aid:'%s')", info->label, sc_dump_hex(info->aid.value, info->aid.len));
1221
422
    sc_pkcs15_free_app(p15card);
1222
422
    p15card->app = sc_dup_app_info(info);
1223
422
    if (!p15card->app) {
1224
0
      err = SC_ERROR_OUT_OF_MEMORY;
1225
0
      goto end;
1226
0
    }
1227
1228
422
    if (info->path.len)
1229
374
      p15card->file_app->path = info->path;
1230
1231
422
    if (info->ddo.value && info->ddo.len)
1232
48
      parse_ddo(p15card, info->ddo.value, info->ddo.len);
1233
1234
11.9k
  } else if (aid) {
1235
0
    sc_log(ctx, "Application '%s' not found", sc_dump_hex(aid->value, aid->len));
1236
0
    err = SC_ERROR_INVALID_ARGUMENTS;
1237
0
    goto end;
1238
0
  }
1239
12.3k
  sc_log(ctx, "application path '%s'", sc_print_path(&p15card->file_app->path));
1240
1241
  /* Check if pkcs15 directory exists */
1242
12.3k
  err = sc_select_file(card, &p15card->file_app->path, NULL);
1243
1244
  /* If the above test failed on cards without EF(DIR),
1245
   * try to continue read ODF from 3F005031. -aet
1246
   */
1247
12.3k
  if ((err != SC_SUCCESS) && (card->app_count < 1)) {
1248
10.2k
    sc_format_path("3F00", &p15card->file_app->path);
1249
10.2k
    err = SC_SUCCESS;
1250
10.2k
  }
1251
1252
12.3k
  if (err < 0) {
1253
309
    sc_log(ctx, "Cannot select application path");
1254
309
    goto end;
1255
309
  }
1256
1257
12.0k
  err = sc_pkcs15_bind_internal_tokeninfo(p15card);
1258
12.0k
  if (err < 0) {
1259
11.4k
    sc_log(ctx, "Cannot bind tokeninfo");
1260
11.4k
    goto end;
1261
11.4k
  }
1262
1263
586
  err = sc_pkcs15_bind_internal_odf(p15card);
1264
586
  if (err < 0) {
1265
431
    sc_log(ctx, "Cannot bind ODF");
1266
431
    goto end;
1267
431
  }
1268
1269
155
  ok = 1;
1270
12.3k
end:
1271
12.3k
  if (!ok) {
1272
12.2k
    sc_pkcs15_card_clear(p15card);
1273
12.2k
    if (err == SC_ERROR_FILE_NOT_FOUND)
1274
1.29k
      err = SC_ERROR_WRONG_CARD;
1275
12.2k
    LOG_FUNC_RETURN(ctx, err);
1276
12.2k
  }
1277
1278
155
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1279
155
}
1280
1281
const char *pkcs15_get_default_use_file_cache(struct sc_card *card)
1282
13.0k
{
1283
  /* enable file caching by default for cards with static content to avoid
1284
   * synchronization problems.
1285
   *
1286
   * The following list was initialized with the cards that can't be modified
1287
   * with OpenSC i.e. which don't have a profile/driver for pkcs15-init. */
1288
13.0k
  const char *card_drivers_with_file_cache[] = {
1289
13.0k
      "atrust-acos",
1290
13.0k
      "belpic",
1291
13.0k
      "cac1",
1292
13.0k
      "cac",
1293
13.0k
      "cedulauy",
1294
13.0k
      "coolkey",
1295
13.0k
      "edo",
1296
13.0k
      "esteid2018",
1297
13.0k
      "esteid2025",
1298
13.0k
      "flex",
1299
13.0k
      "cyberflex",
1300
13.0k
      "gemsafeV1",
1301
13.0k
      "idprime",
1302
13.0k
      "itacns",
1303
13.0k
      "jpki",
1304
13.0k
      "MaskTech",
1305
13.0k
      "mcrd",
1306
13.0k
      "myeid",
1307
13.0k
      "npa",
1308
13.0k
      "nqapplet",
1309
13.0k
      "tcos",
1310
13.0k
      "dtrust",
1311
13.0k
      "lteid",
1312
13.0k
  };
1313
1314
13.0k
  if (NULL == card || NULL == card->driver || NULL == card->driver->short_name)
1315
0
    return "no";
1316
246k
  for (size_t i = 0; i < (sizeof card_drivers_with_file_cache / sizeof *card_drivers_with_file_cache); i++) {
1317
239k
    if (0 == strcmp(card->driver->short_name, card_drivers_with_file_cache[i]))
1318
5.51k
      return "public";
1319
239k
  }
1320
1321
7.52k
  return "no";
1322
13.0k
}
1323
1324
int
1325
sc_pkcs15_bind(struct sc_card *card, struct sc_aid *aid,
1326
    struct sc_pkcs15_card **p15card_out)
1327
13.0k
{
1328
13.0k
  struct sc_pkcs15_card *p15card = NULL;
1329
13.0k
  struct sc_context *ctx;
1330
13.0k
  scconf_block *conf_block = NULL;
1331
13.0k
  int r, emu_first, enable_emu;
1332
13.0k
  const char *use_file_cache;
1333
13.0k
  const char *pin_protected_certificate, *private_certificate;
1334
1335
13.0k
  if (card == NULL || p15card_out == NULL) {
1336
0
    return SC_ERROR_INVALID_ARGUMENTS;
1337
0
  }
1338
13.0k
  ctx = card->ctx;
1339
1340
13.0k
  LOG_FUNC_CALLED(ctx);
1341
13.0k
  sc_log(ctx, "application(aid:'%s')", aid ? sc_dump_hex(aid->value, aid->len) : "empty");
1342
1343
13.0k
  p15card = sc_pkcs15_card_new();
1344
13.0k
  if (p15card == NULL)
1345
13.0k
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
1346
1347
13.0k
  p15card->card = card;
1348
13.0k
  p15card->opts.use_file_cache = SC_PKCS15_OPTS_CACHE_NO_FILES;
1349
13.0k
  use_file_cache = pkcs15_get_default_use_file_cache(card);
1350
13.0k
  p15card->opts.use_pin_cache = 1;
1351
13.0k
  p15card->opts.pin_cache_counter = 10;
1352
13.0k
  p15card->opts.pin_cache_ignore_user_consent = 0;
1353
13.0k
  pin_protected_certificate = "protect";
1354
13.0k
  private_certificate = "";
1355
1356
13.0k
  conf_block = sc_get_conf_block(ctx, "framework", "pkcs15", 1);
1357
13.0k
  if (conf_block) {
1358
0
    use_file_cache = scconf_get_str(conf_block, "use_file_caching", use_file_cache);
1359
0
    p15card->opts.use_pin_cache = scconf_get_bool(conf_block, "use_pin_caching", p15card->opts.use_pin_cache);
1360
0
    p15card->opts.pin_cache_counter = scconf_get_int(conf_block, "pin_cache_counter", p15card->opts.pin_cache_counter);
1361
0
    p15card->opts.pin_cache_ignore_user_consent = scconf_get_bool(conf_block, "pin_cache_ignore_user_consent",
1362
0
        p15card->opts.pin_cache_ignore_user_consent);
1363
0
    pin_protected_certificate = scconf_get_str(conf_block, "pin_protected_certificate", pin_protected_certificate);
1364
    /* read also the old value to keep backward compatibility */
1365
0
    private_certificate = scconf_get_str(conf_block, "private_certificate", private_certificate);
1366
0
  }
1367
1368
13.0k
  if (0 == strcmp(use_file_cache, "yes")) {
1369
0
    p15card->opts.use_file_cache = SC_PKCS15_OPTS_CACHE_ALL_FILES;
1370
13.0k
  } else if (0 == strcmp(use_file_cache, "public")) {
1371
5.51k
    p15card->opts.use_file_cache = SC_PKCS15_OPTS_CACHE_PUBLIC_FILES;
1372
7.52k
  } else if (0 == strcmp(use_file_cache, "no")) {
1373
7.52k
    p15card->opts.use_file_cache = SC_PKCS15_OPTS_CACHE_NO_FILES;
1374
7.52k
  }
1375
1376
13.0k
  if (0 == strcmp(pin_protected_certificate, "protect")) {
1377
13.0k
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_PROTECT;
1378
13.0k
  } else if (0 == strcmp(pin_protected_certificate, "ignore")) {
1379
0
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_IGNORE;
1380
0
  } else if (0 == strcmp(pin_protected_certificate, "declassify")) {
1381
0
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_DECLASSIFY;
1382
0
  }
1383
  /* overwrite pin_protected_certificate when private_certificate set */
1384
13.0k
  if (0 == strcmp(private_certificate, "protect")) {
1385
0
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_PROTECT;
1386
13.0k
  } else if (0 == strcmp(private_certificate, "ignore")) {
1387
0
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_IGNORE;
1388
13.0k
  } else if (0 == strcmp(private_certificate, "declassify")) {
1389
0
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_DECLASSIFY;
1390
0
  }
1391
13.0k
  sc_log(ctx, "PKCS#15 options: use_file_cache=%d use_pin_cache=%d pin_cache_counter=%d pin_cache_ignore_user_consent=%d pin_protected_certificate=%d",
1392
13.0k
      p15card->opts.use_file_cache, p15card->opts.use_pin_cache, p15card->opts.pin_cache_counter,
1393
13.0k
      p15card->opts.pin_cache_ignore_user_consent, p15card->opts.pin_protected_certificate);
1394
1395
13.0k
  r = sc_lock(card);
1396
13.0k
  if (r) {
1397
0
    sc_log(ctx, "sc_lock() failed: %s", sc_strerror(r));
1398
0
    sc_pkcs15_card_free(p15card);
1399
0
    LOG_FUNC_RETURN(ctx, r);
1400
0
  }
1401
1402
13.0k
  enable_emu = scconf_get_bool(conf_block, "enable_pkcs15_emulation", 1);
1403
13.0k
  if (enable_emu) {
1404
13.0k
    sc_log(ctx, "PKCS#15 emulation enabled");
1405
13.0k
    emu_first = scconf_get_bool(conf_block, "try_emulation_first", 0);
1406
13.0k
    if (emu_first || sc_pkcs15_is_emulation_only(card)) {
1407
2.86k
      r = sc_pkcs15_bind_synthetic(p15card, aid);
1408
2.86k
      if (r == SC_SUCCESS)
1409
1.61k
        goto done;
1410
1.25k
      r = sc_pkcs15_bind_internal(p15card, aid);
1411
1.25k
      if (r < 0)
1412
1.24k
        goto error;
1413
10.1k
    } else {
1414
10.1k
      r = sc_pkcs15_bind_internal(p15card, aid);
1415
10.1k
      if (r == SC_SUCCESS)
1416
129
        goto done;
1417
10.0k
      r = sc_pkcs15_bind_synthetic(p15card, aid);
1418
10.0k
      if (r < 0)
1419
5.81k
        goto error;
1420
10.0k
    }
1421
13.0k
  }
1422
0
  else {
1423
0
    r = sc_pkcs15_bind_internal(p15card, aid);
1424
0
    if (r < 0)
1425
0
      goto error;
1426
0
  }
1427
5.98k
done:
1428
5.98k
  *p15card_out = p15card;
1429
5.98k
  sc_unlock(card);
1430
5.98k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1431
7.05k
error:
1432
7.05k
  sc_unlock(card);
1433
7.05k
  sc_pkcs15_card_free(p15card);
1434
7.05k
  LOG_FUNC_RETURN(ctx, r);
1435
7.05k
}
1436
1437
1438
int
1439
sc_pkcs15_unbind(struct sc_pkcs15_card *p15card)
1440
0
{
1441
0
  if (p15card == NULL || p15card->magic != SC_PKCS15_CARD_MAGIC) {
1442
0
    return SC_ERROR_INVALID_ARGUMENTS;
1443
0
  }
1444
1445
0
  LOG_FUNC_CALLED(p15card->card->ctx);
1446
0
  if (p15card->dll_handle)
1447
0
    sc_dlclose(p15card->dll_handle);
1448
0
  sc_pkcs15_pincache_clear(p15card);
1449
0
  sc_pkcs15_card_free(p15card);
1450
0
  return 0;
1451
0
}
1452
1453
1454
static int
1455
__sc_pkcs15_search_objects(struct sc_pkcs15_card *p15card, unsigned int class_mask, unsigned int type,
1456
      int (*func)(sc_pkcs15_object_t *, void *), void *func_arg,
1457
      sc_pkcs15_object_t **ret, size_t ret_size)
1458
2.91k
{
1459
2.91k
  struct sc_pkcs15_object *obj = NULL;
1460
2.91k
  struct sc_pkcs15_df *df = NULL;
1461
2.91k
  unsigned int  df_mask = 0;
1462
2.91k
  size_t    match_count = 0;
1463
2.91k
  int r;
1464
1465
2.91k
  if (type)
1466
2.90k
    class_mask |= SC_PKCS15_TYPE_TO_CLASS(type);
1467
1468
  /* Make sure the class mask we have makes sense */
1469
2.91k
  if (class_mask == 0
1470
2.91k
      || (class_mask & ~(
1471
2.91k
          SC_PKCS15_SEARCH_CLASS_PRKEY |
1472
2.91k
          SC_PKCS15_SEARCH_CLASS_PUBKEY |
1473
2.91k
          SC_PKCS15_SEARCH_CLASS_SKEY |
1474
2.91k
          SC_PKCS15_SEARCH_CLASS_CERT |
1475
2.91k
          SC_PKCS15_SEARCH_CLASS_DATA |
1476
2.91k
          SC_PKCS15_SEARCH_CLASS_AUTH))) {
1477
0
    LOG_FUNC_RETURN(p15card->card->ctx, SC_ERROR_INVALID_ARGUMENTS);
1478
0
  }
1479
1480
2.91k
  if (class_mask & SC_PKCS15_SEARCH_CLASS_PRKEY)
1481
5
    df_mask |= (1 << SC_PKCS15_PRKDF);
1482
2.91k
  if (class_mask & SC_PKCS15_SEARCH_CLASS_PUBKEY)
1483
0
    df_mask |= (1 << SC_PKCS15_PUKDF) | (1 << SC_PKCS15_PUKDF_TRUSTED);
1484
2.91k
  if (class_mask & SC_PKCS15_SEARCH_CLASS_CERT)
1485
1.79k
    df_mask |= (1 << SC_PKCS15_CDF) | (1 << SC_PKCS15_CDF_TRUSTED) | (1 << SC_PKCS15_CDF_USEFUL);
1486
2.91k
  if (class_mask & SC_PKCS15_SEARCH_CLASS_DATA)
1487
155
    df_mask |= (1 << SC_PKCS15_DODF);
1488
2.91k
  if (class_mask & SC_PKCS15_SEARCH_CLASS_AUTH)
1489
961
    df_mask |= (1 << SC_PKCS15_AODF);
1490
2.91k
  if (class_mask & SC_PKCS15_SEARCH_CLASS_SKEY)
1491
0
    df_mask |= (1 << SC_PKCS15_SKDF);
1492
1493
  /* Make sure all the DFs we want to search have been
1494
   * enumerated. */
1495
10.6k
  for (df = p15card->df_list; df != NULL; df = df->next) {
1496
7.77k
    if (!(df_mask & (1 << df->type)))   {
1497
5.74k
      continue;
1498
5.74k
    }
1499
2.03k
    if (df->enumerated)
1500
2.02k
      continue;
1501
    /* Enumerate the DF's, so p15card->obj_list is populated. */
1502
10
    if (p15card->ops.parse_df)
1503
0
      r = p15card->ops.parse_df(p15card, df);
1504
10
    else
1505
10
      r = sc_pkcs15_parse_df(p15card, df);
1506
10
    if (r != SC_SUCCESS)
1507
10
      continue;
1508
10
  }
1509
1510
  /* And now loop over all objects */
1511
25.8k
  for (obj = p15card->obj_list; obj != NULL; obj = obj->next) {
1512
    /* Check object type */
1513
24.3k
    if (!(class_mask & SC_PKCS15_TYPE_TO_CLASS(obj->type)))
1514
19.2k
      continue;
1515
5.13k
    if (type != 0
1516
5.13k
     && obj->type != type
1517
3.39k
     && (obj->type & SC_PKCS15_TYPE_CLASS_MASK) != type)
1518
0
      continue;
1519
1520
    /* Potential candidate, apply search function */
1521
5.13k
    if (func != NULL && func(obj, func_arg) <= 0)
1522
1.97k
      continue;
1523
    /* Okay, we have a match. */
1524
3.15k
    match_count++;
1525
3.15k
    if (!ret || ret_size <= 0)
1526
0
      continue;
1527
3.15k
    ret[match_count-1] = obj;
1528
3.15k
    if (ret_size <= match_count)
1529
1.41k
      break;
1530
3.15k
  }
1531
1532
2.91k
  return (int)match_count;
1533
2.91k
}
1534
1535
1536
int
1537
sc_pkcs15_get_objects(struct sc_pkcs15_card *p15card, unsigned int type,
1538
    struct sc_pkcs15_object **ret, size_t ret_size)
1539
162
{
1540
162
  return sc_pkcs15_get_objects_cond(p15card, type, NULL, NULL, ret, ret_size);
1541
162
}
1542
1543
1544
static int
1545
compare_obj_id(struct sc_pkcs15_object *obj, const struct sc_pkcs15_id *id)
1546
3.39k
{
1547
3.39k
  void *data = obj->data;
1548
1549
3.39k
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1550
1.95k
  case SC_PKCS15_TYPE_CERT:
1551
1.95k
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_cert_info *) data)->id, id);
1552
0
  case SC_PKCS15_TYPE_PRKEY:
1553
0
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_prkey_info *) data)->id, id);
1554
0
  case SC_PKCS15_TYPE_PUBKEY:
1555
0
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_pubkey_info *) data)->id, id);
1556
0
  case SC_PKCS15_TYPE_SKEY:
1557
0
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_skey_info *) data)->id, id);
1558
1.43k
  case SC_PKCS15_TYPE_AUTH:
1559
1.43k
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_auth_info *) data)->auth_id, id);
1560
0
  case SC_PKCS15_TYPE_DATA_OBJECT:
1561
0
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_data_info *) data)->id, id);
1562
3.39k
  }
1563
0
  return 0;
1564
3.39k
}
1565
1566
1567
static int
1568
sc_obj_app_oid(struct sc_pkcs15_object *obj, const struct sc_object_id *app_oid)
1569
0
{
1570
0
  if ((obj->type & SC_PKCS15_TYPE_CLASS_MASK) == SC_PKCS15_TYPE_DATA_OBJECT)
1571
0
    return sc_compare_oid(&((struct sc_pkcs15_data_info *) obj->data)->app_oid, app_oid);
1572
0
  return 0;
1573
0
}
1574
1575
1576
static int
1577
compare_obj_usage(struct sc_pkcs15_object *obj, unsigned int mask, unsigned int value)
1578
0
{
1579
0
  void    *data = obj->data;
1580
0
  unsigned int  usage;
1581
1582
0
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1583
0
  case SC_PKCS15_TYPE_PRKEY:
1584
0
    usage = ((struct sc_pkcs15_prkey_info *) data)->usage;
1585
0
    break;
1586
0
  case SC_PKCS15_TYPE_PUBKEY:
1587
0
    usage = ((struct sc_pkcs15_pubkey_info *) data)->usage;
1588
0
    break;
1589
0
  default:
1590
0
    return 0;
1591
0
  }
1592
0
  return (usage & mask & value) != 0;
1593
0
}
1594
1595
1596
static int
1597
compare_obj_flags(struct sc_pkcs15_object *obj, unsigned int mask, unsigned int value)
1598
0
{
1599
0
  struct sc_pkcs15_auth_info *auth_info;
1600
0
  unsigned int  flags;
1601
1602
0
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1603
0
  case SC_PKCS15_TYPE_AUTH:
1604
0
    auth_info = (struct sc_pkcs15_auth_info *) obj->data;
1605
0
    if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1606
0
      return 0;
1607
0
    flags = auth_info->attrs.pin.flags;
1608
0
    break;
1609
0
  default:
1610
0
    return 0;
1611
0
  }
1612
0
  return !((flags ^ value) & mask);
1613
0
}
1614
1615
1616
static int
1617
compare_obj_reference(struct sc_pkcs15_object *obj, int value)
1618
0
{
1619
0
  struct sc_pkcs15_auth_info *auth_info;
1620
0
  void    *data = obj->data;
1621
0
  int   reference;
1622
1623
0
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1624
0
  case SC_PKCS15_TYPE_AUTH:
1625
0
    auth_info = (struct sc_pkcs15_auth_info *) obj->data;
1626
0
    if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1627
0
      return 0;
1628
0
    reference = auth_info->attrs.pin.reference;
1629
0
    break;
1630
0
  case SC_PKCS15_TYPE_PRKEY:
1631
0
    reference = ((struct sc_pkcs15_prkey_info *) data)->key_reference;
1632
0
    break;
1633
0
  default:
1634
0
    return 0;
1635
0
  }
1636
0
  return reference == value;
1637
0
}
1638
1639
1640
static int
1641
compare_obj_path(struct sc_pkcs15_object *obj, const struct sc_path *path)
1642
0
{
1643
0
  void *data = obj->data;
1644
1645
0
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1646
0
  case SC_PKCS15_TYPE_PRKEY:
1647
0
    return sc_compare_path(&((struct sc_pkcs15_prkey_info *) data)->path, path);
1648
0
  case SC_PKCS15_TYPE_PUBKEY:
1649
0
    return sc_compare_path(&((struct sc_pkcs15_pubkey_info *) data)->path, path);
1650
0
  case SC_PKCS15_TYPE_SKEY:
1651
0
    return sc_compare_path(&((struct sc_pkcs15_skey_info *) data)->path, path);
1652
0
  case SC_PKCS15_TYPE_CERT:
1653
0
    return sc_compare_path(&((struct sc_pkcs15_cert_info *) data)->path, path);
1654
0
  case SC_PKCS15_TYPE_AUTH:
1655
0
    return sc_compare_path(&((struct sc_pkcs15_auth_info *) data)->path, path);
1656
0
  case SC_PKCS15_TYPE_DATA_OBJECT:
1657
0
    return sc_compare_path(&((struct sc_pkcs15_data_info *) data)->path, path);
1658
0
  }
1659
0
  return 0;
1660
0
}
1661
1662
1663
static int
1664
compare_obj_data_name(struct sc_pkcs15_object *obj, const char *app_label, const char *label)
1665
0
{
1666
0
  struct sc_pkcs15_data_info *cinfo = (struct sc_pkcs15_data_info *) obj->data;
1667
1668
0
  if (obj->type != SC_PKCS15_TYPE_DATA_OBJECT)
1669
0
    return 0;
1670
1671
0
  return !strncmp(cinfo->app_label, app_label, sizeof cinfo->app_label) &&
1672
0
    !strncmp(obj->label, label, sizeof obj->label);
1673
0
}
1674
1675
1676
static int
1677
compare_obj_key(struct sc_pkcs15_object *obj, void *arg)
1678
3.39k
{
1679
3.39k
  struct sc_pkcs15_search_key *sk = (struct sc_pkcs15_search_key *) arg;
1680
1681
3.39k
  if (sk->id && !compare_obj_id(obj, sk->id))
1682
1.97k
    return 0;
1683
1.41k
  if (sk->app_oid && !sc_obj_app_oid(obj, sk->app_oid))
1684
0
    return 0;
1685
1.41k
  if (sk->usage_mask && !compare_obj_usage(obj, sk->usage_mask, sk->usage_value))
1686
0
    return 0;
1687
1.41k
  if (sk->flags_mask && !compare_obj_flags(obj, sk->flags_mask, sk->flags_value))
1688
0
    return 0;
1689
1.41k
  if (sk->match_reference && !compare_obj_reference(obj, sk->reference))
1690
0
    return 0;
1691
1.41k
  if (sk->path && !compare_obj_path(obj, sk->path))
1692
0
    return 0;
1693
1.41k
  if (sk->app_label) {
1694
0
    if (sk->label && !compare_obj_data_name(obj, sk->app_label, sk->label))
1695
0
      return 0;
1696
1.41k
  } else {
1697
1.41k
    if (sk->label && strncmp(sk->label, obj->label, sizeof(obj->label)) != 0)
1698
0
      return 0;
1699
1.41k
  }
1700
1701
1.41k
  return 1;
1702
1.41k
}
1703
1704
1705
static int
1706
find_by_key(struct sc_pkcs15_card *p15card, unsigned int type, struct sc_pkcs15_search_key *sk,
1707
    struct sc_pkcs15_object **out)
1708
0
{
1709
0
  int r;
1710
1711
0
  r = sc_pkcs15_get_objects_cond(p15card, type, compare_obj_key, sk, out, 1);
1712
0
  if (r < 0)
1713
0
    return r;
1714
0
  if (r == 0)
1715
0
    return SC_ERROR_OBJECT_NOT_FOUND;
1716
0
  return 0;
1717
0
}
1718
1719
1720
int
1721
sc_pkcs15_search_objects(struct sc_pkcs15_card *p15card, struct sc_pkcs15_search_key *sk,
1722
      struct sc_pkcs15_object **ret, size_t ret_size)
1723
10
{
1724
10
  return __sc_pkcs15_search_objects(p15card,
1725
10
      sk->class_mask, sk->type,
1726
10
      compare_obj_key, sk,
1727
10
      ret, ret_size);
1728
10
}
1729
1730
1731
int
1732
sc_pkcs15_get_objects_cond(struct sc_pkcs15_card *p15card, unsigned int type,
1733
    int (* func)(struct sc_pkcs15_object *, void *),
1734
    void *func_arg, struct sc_pkcs15_object **ret, size_t ret_size)
1735
162
{
1736
162
  return __sc_pkcs15_search_objects(p15card, 0, type,
1737
162
      func, func_arg, ret, ret_size);
1738
162
}
1739
1740
1741
int sc_pkcs15_find_object_by_id(struct sc_pkcs15_card *p15card,
1742
        unsigned int type, const struct sc_pkcs15_id *id,
1743
        struct sc_pkcs15_object **out)
1744
2.73k
{
1745
2.73k
  struct sc_pkcs15_search_key sk;
1746
2.73k
  int r;
1747
1748
2.73k
  memset(&sk, 0, sizeof(sk));
1749
2.73k
  sk.id = id;
1750
1751
2.73k
  r = __sc_pkcs15_search_objects(p15card, 0, type, compare_obj_key, &sk, out, 1);
1752
2.73k
  if (r < 0)
1753
0
    return r;
1754
2.73k
  if (r == 0)
1755
1.32k
    return SC_ERROR_OBJECT_NOT_FOUND;
1756
1.41k
  return 0;
1757
2.73k
}
1758
1759
1760
int
1761
sc_pkcs15_find_cert_by_id(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_id *id,
1762
    struct sc_pkcs15_object **out)
1763
1.78k
{
1764
1.78k
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_CERT, id, out);
1765
1.78k
}
1766
1767
1768
int
1769
sc_pkcs15_find_prkey_by_id(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_id *id,
1770
    struct sc_pkcs15_object **out)
1771
0
{
1772
0
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_PRKEY, id, out);
1773
0
}
1774
1775
1776
int
1777
sc_pkcs15_find_pubkey_by_id(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_id *id,
1778
    struct sc_pkcs15_object **out)
1779
0
{
1780
0
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_PUBKEY, id, out);
1781
0
}
1782
1783
1784
int
1785
sc_pkcs15_find_skey_by_id(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_id *id,
1786
    struct sc_pkcs15_object **out)
1787
0
{
1788
0
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_SKEY, id, out);
1789
0
}
1790
1791
1792
int
1793
sc_pkcs15_find_pin_by_auth_id(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_id *id,
1794
    struct sc_pkcs15_object **out)
1795
954
{
1796
954
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_AUTH, id, out);
1797
954
}
1798
1799
1800
int
1801
sc_pkcs15_find_pin_by_reference(struct sc_pkcs15_card *p15card, const sc_path_t *path, int reference,
1802
    struct sc_pkcs15_object **out)
1803
0
{
1804
0
  struct sc_pkcs15_search_key sk;
1805
1806
0
  memset(&sk, 0, sizeof(sk));
1807
0
  sk.match_reference = 1;
1808
0
  sk.reference = reference;
1809
0
  sk.path = path;
1810
1811
0
  return find_by_key(p15card, SC_PKCS15_TYPE_AUTH_PIN, &sk, out);
1812
0
}
1813
1814
1815
int
1816
sc_pkcs15_find_pin_by_type_and_reference(struct sc_pkcs15_card *p15card, const struct sc_path *path,
1817
        unsigned auth_method, int reference,
1818
        struct sc_pkcs15_object **out)
1819
0
{
1820
0
  struct sc_context *ctx = p15card->card->ctx;
1821
0
  struct sc_pkcs15_object *auth_objs[0x10];
1822
0
  size_t nn_objs, ii;
1823
0
  int r;
1824
1825
  /* Get all existing pkcs15 AUTH objects */
1826
0
  r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, auth_objs, 0x10);
1827
0
  LOG_TEST_RET(ctx, r, "Get PKCS#15 AUTH objects error");
1828
0
  nn_objs = r;
1829
1830
0
  for (ii=0; ii<nn_objs; ii++)   {
1831
0
    struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)auth_objs[ii]->data;
1832
1833
0
    if (auth_info->auth_method != auth_method)
1834
0
      continue;
1835
0
    if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_PIN)
1836
0
      if (auth_info->attrs.pin.reference != reference)
1837
0
        continue;
1838
1839
0
    if (path && !sc_compare_path(&auth_info->path, path))
1840
0
      continue;
1841
1842
0
    if (out)
1843
0
      *out = auth_objs[ii];
1844
1845
0
    return SC_SUCCESS;
1846
0
  }
1847
1848
0
  return SC_ERROR_OBJECT_NOT_FOUND;
1849
0
}
1850
1851
1852
int
1853
sc_pkcs15_find_so_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object **out)
1854
0
{
1855
0
  struct sc_pkcs15_search_key sk;
1856
1857
0
  memset(&sk, 0, sizeof(sk));
1858
0
  sk.flags_mask = sk.flags_value = SC_PKCS15_PIN_FLAG_SO_PIN;
1859
1860
0
  return find_by_key(p15card, SC_PKCS15_TYPE_AUTH_PIN, &sk, out);
1861
0
}
1862
1863
1864
int
1865
sc_pkcs15_find_pin_by_flags(struct sc_pkcs15_card *p15card,
1866
    unsigned flags, unsigned mask, int *index,
1867
    struct sc_pkcs15_object **out)
1868
0
{
1869
0
  struct sc_context *ctx = p15card->card->ctx;
1870
0
  struct sc_pkcs15_object *auths[SC_PKCS15_MAX_PINS];
1871
0
  int r, i, num, idx = 0;
1872
1873
0
  LOG_FUNC_CALLED(ctx);
1874
0
  sc_log(ctx, "Find PIN flags:0x%X, mask:0x%X, index:%i", flags, mask, index ? *index : -1);
1875
0
  if (index)
1876
0
    idx = *index;
1877
  /* Get authentication PKCS#15 objects that are present in the given application */
1878
0
  r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, auths, SC_PKCS15_MAX_PINS);
1879
0
  if (r < 0)
1880
0
    return r;
1881
0
  num = r;
1882
1883
0
  for (i=idx; i<num; i++)   {
1884
0
    struct sc_pkcs15_auth_info *pin_info = (struct sc_pkcs15_auth_info *)(*(auths + i))->data;
1885
1886
0
    if (!pin_info || pin_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1887
0
      continue;
1888
1889
0
    if ((pin_info->attrs.pin.flags & mask) != flags)
1890
0
      continue;
1891
1892
0
    if (out)
1893
0
      *out = *(auths + i);
1894
0
    if (index)
1895
0
      *index = i;
1896
1897
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1898
0
  }
1899
1900
0
  LOG_FUNC_RETURN(ctx, SC_ERROR_OBJECT_NOT_FOUND);
1901
0
}
1902
1903
1904
int
1905
sc_pkcs15_find_data_object_by_id(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_id *id,
1906
    struct sc_pkcs15_object **out)
1907
0
{
1908
0
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_DATA_OBJECT, id, out);
1909
0
}
1910
1911
1912
int
1913
sc_pkcs15_find_data_object_by_app_oid(struct sc_pkcs15_card *p15card, const struct sc_object_id *app_oid,
1914
    struct sc_pkcs15_object **out)
1915
0
{
1916
0
  struct sc_pkcs15_search_key sk;
1917
0
  int r;
1918
1919
0
  memset(&sk, 0, sizeof(sk));
1920
0
  sk.app_oid = app_oid;
1921
1922
0
  r = __sc_pkcs15_search_objects(p15card, 0, SC_PKCS15_TYPE_DATA_OBJECT,
1923
0
        compare_obj_key, &sk,
1924
0
        out, 1);
1925
0
  if (r < 0)
1926
0
    return r;
1927
0
  if (r == 0)
1928
0
    return SC_ERROR_OBJECT_NOT_FOUND;
1929
0
  return 0;
1930
0
}
1931
1932
1933
int
1934
sc_pkcs15_find_data_object_by_name(struct sc_pkcs15_card *p15card, const char *app_label, const char *label,
1935
    struct sc_pkcs15_object **out)
1936
0
{
1937
0
  struct sc_pkcs15_search_key sk;
1938
0
  int r;
1939
1940
0
  memset(&sk, 0, sizeof(sk));
1941
0
  sk.app_label = app_label;
1942
0
  sk.label = label;
1943
1944
0
  r = __sc_pkcs15_search_objects(p15card, 0, SC_PKCS15_TYPE_DATA_OBJECT,
1945
0
        compare_obj_key, &sk,
1946
0
        out, 1);
1947
0
  if (r < 0)
1948
0
    return r;
1949
0
  if (r == 0)
1950
0
    return SC_ERROR_OBJECT_NOT_FOUND;
1951
0
  return 0;
1952
0
}
1953
1954
1955
int
1956
sc_pkcs15_find_prkey_by_id_usage(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_id *id,
1957
    unsigned int usage, struct sc_pkcs15_object **out)
1958
0
{
1959
0
  struct sc_pkcs15_search_key sk;
1960
1961
0
  memset(&sk, 0, sizeof(sk));
1962
0
  sk.usage_mask = sk.usage_value = usage;
1963
0
  sk.id = id;
1964
1965
0
  return find_by_key(p15card, SC_PKCS15_TYPE_PRKEY, &sk, out);
1966
0
}
1967
1968
1969
int
1970
sc_pkcs15_find_prkey_by_reference(struct sc_pkcs15_card *p15card, const struct sc_path *path,
1971
        int reference,
1972
        struct sc_pkcs15_object **out)
1973
0
{
1974
0
  struct sc_pkcs15_search_key sk;
1975
1976
0
  memset(&sk, 0, sizeof(sk));
1977
0
  sk.match_reference = 1;
1978
0
  sk.reference = reference;
1979
0
  sk.path = path;
1980
1981
0
  return find_by_key(p15card, SC_PKCS15_TYPE_PRKEY, &sk, out);
1982
0
}
1983
1984
1985
int
1986
sc_pkcs15_add_object(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *obj)
1987
47.0k
{
1988
47.0k
  struct sc_pkcs15_object *p = p15card->obj_list;
1989
1990
47.0k
  if (!obj)
1991
0
    return 0;
1992
47.0k
  obj->next = obj->prev = NULL;
1993
47.0k
  if (p15card->obj_list == NULL) {
1994
5.61k
    p15card->obj_list = obj;
1995
5.61k
    return 0;
1996
5.61k
  }
1997
759k
  while (p->next != NULL)
1998
718k
    p = p->next;
1999
41.4k
  p->next = obj;
2000
41.4k
  obj->prev = p;
2001
2002
41.4k
  return 0;
2003
47.0k
}
2004
2005
2006
void
2007
sc_pkcs15_remove_object(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *obj)
2008
0
{
2009
0
  if (!obj)
2010
0
    return;
2011
0
  else if (obj->prev == NULL)
2012
0
    p15card->obj_list = obj->next;
2013
0
  else
2014
0
    obj->prev->next = obj->next;
2015
0
  if (obj->next != NULL)
2016
0
    obj->next->prev = obj->prev;
2017
0
}
2018
2019
2020
static void
2021
sc_pkcs15_remove_objects(struct sc_pkcs15_card *p15card)
2022
27.9k
{
2023
27.9k
  struct sc_pkcs15_object *cur = NULL, *next = NULL;
2024
2025
27.9k
  if (!p15card || !p15card->obj_list)
2026
22.2k
    return;
2027
52.7k
  for (cur = p15card->obj_list; cur; cur = next)   {
2028
47.0k
    next = cur->next;
2029
47.0k
    sc_pkcs15_free_object(cur);
2030
47.0k
  }
2031
2032
5.61k
  p15card->obj_list = NULL;
2033
5.61k
}
2034
2035
2036
void
2037
sc_pkcs15_free_object(struct sc_pkcs15_object *obj)
2038
47.0k
{
2039
47.0k
  if (!obj)
2040
0
    return;
2041
47.0k
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
2042
5.63k
  case SC_PKCS15_TYPE_PRKEY:
2043
5.63k
    sc_pkcs15_free_prkey_info((sc_pkcs15_prkey_info_t *)obj->data);
2044
5.63k
    break;
2045
2.30k
  case SC_PKCS15_TYPE_PUBKEY:
2046
    /* This is normally passed to framework-pkcs15,
2047
     * but if something fails on the way, it would not get freed */
2048
2.30k
    if (obj->emulated) {
2049
2.08k
      sc_pkcs15_free_pubkey(obj->emulated);
2050
2.08k
    }
2051
2.30k
    sc_pkcs15_free_pubkey_info((sc_pkcs15_pubkey_info_t *)obj->data);
2052
2.30k
    break;
2053
3.98k
  case SC_PKCS15_TYPE_CERT:
2054
3.98k
    sc_pkcs15_free_cert_info((sc_pkcs15_cert_info_t *)obj->data);
2055
3.98k
    break;
2056
0
  case SC_PKCS15_TYPE_SKEY:
2057
0
    sc_pkcs15_free_skey_info((sc_pkcs15_skey_info_t *)obj->data);
2058
0
    break;
2059
27.5k
  case SC_PKCS15_TYPE_DATA_OBJECT:
2060
27.5k
    sc_pkcs15_free_data_info((sc_pkcs15_data_info_t *)obj->data);
2061
27.5k
    break;
2062
7.63k
  case SC_PKCS15_TYPE_AUTH:
2063
7.63k
    sc_pkcs15_free_auth_info((sc_pkcs15_auth_info_t *)obj->data);
2064
7.63k
    break;
2065
0
  default:
2066
0
    free(obj->data);
2067
47.0k
  }
2068
2069
47.0k
  sc_pkcs15_free_object_content(obj);
2070
2071
47.0k
  free(obj);
2072
47.0k
}
2073
2074
2075
int
2076
sc_pkcs15_add_df(struct sc_pkcs15_card *p15card, unsigned int type, const sc_path_t *path)
2077
13.6k
{
2078
13.6k
  struct sc_pkcs15_df *p, *newdf;
2079
2080
13.6k
  newdf = calloc(1, sizeof(struct sc_pkcs15_df));
2081
13.6k
  if (newdf == NULL)
2082
0
    return SC_ERROR_OUT_OF_MEMORY;
2083
13.6k
  newdf->path = *path;
2084
13.6k
  newdf->type = type;
2085
2086
13.6k
  if (p15card->df_list == NULL) {
2087
5.64k
    p15card->df_list = newdf;
2088
5.64k
    return 0;
2089
5.64k
  }
2090
2091
8.05k
  p = p15card->df_list;
2092
28.9k
  while (p->next != NULL)
2093
20.8k
    p = p->next;
2094
8.05k
  p->next = newdf;
2095
8.05k
  newdf->prev = p;
2096
2097
8.05k
  return 0;
2098
13.6k
}
2099
2100
2101
static void
2102
sc_pkcs15_remove_dfs(struct sc_pkcs15_card *p15card)
2103
27.9k
{
2104
27.9k
  struct sc_pkcs15_df *cur = NULL, *next = NULL;
2105
2106
27.9k
  if (!p15card || !p15card->df_list)
2107
22.2k
    return;
2108
2109
19.3k
  for (cur = p15card->df_list; cur; cur = next)   {
2110
13.6k
    next = cur->next;
2111
13.6k
    free(cur);
2112
13.6k
  }
2113
2114
5.64k
  p15card->df_list = NULL;
2115
5.64k
}
2116
2117
2118
int
2119
sc_pkcs15_encode_df(struct sc_context *ctx, struct sc_pkcs15_card *p15card, struct sc_pkcs15_df *df,
2120
    unsigned char **buf_out, size_t *bufsize_out)
2121
0
{
2122
0
  unsigned char *buf = NULL, *tmp = NULL, *p;
2123
0
  size_t bufsize = 0, tmpsize;
2124
0
  const struct sc_pkcs15_object *obj;
2125
0
  int (* func)(struct sc_context *, const struct sc_pkcs15_object *nobj,
2126
0
         unsigned char **nbuf, size_t *nbufsize) = NULL;
2127
0
  int r;
2128
2129
0
  if (p15card == NULL || p15card->magic != SC_PKCS15_CARD_MAGIC) {
2130
0
    return SC_ERROR_INVALID_ARGUMENTS;
2131
0
  }
2132
0
  switch (df->type) {
2133
0
  case SC_PKCS15_PRKDF:
2134
0
    func = sc_pkcs15_encode_prkdf_entry;
2135
0
    break;
2136
0
  case SC_PKCS15_PUKDF:
2137
0
  case SC_PKCS15_PUKDF_TRUSTED:
2138
0
    func = sc_pkcs15_encode_pukdf_entry;
2139
0
    break;
2140
0
  case SC_PKCS15_SKDF:
2141
0
    func = sc_pkcs15_encode_skdf_entry;
2142
0
    break;
2143
0
  case SC_PKCS15_CDF:
2144
0
  case SC_PKCS15_CDF_TRUSTED:
2145
0
  case SC_PKCS15_CDF_USEFUL:
2146
0
    func = sc_pkcs15_encode_cdf_entry;
2147
0
    break;
2148
0
  case SC_PKCS15_DODF:
2149
0
    func = sc_pkcs15_encode_dodf_entry;
2150
0
    break;
2151
0
  case SC_PKCS15_AODF:
2152
0
    func = sc_pkcs15_encode_aodf_entry;
2153
0
    break;
2154
0
  }
2155
0
  if (func == NULL) {
2156
0
    sc_log(ctx, "unknown DF type: %d", df->type);
2157
0
    *buf_out = NULL;
2158
0
    *bufsize_out = 0;
2159
0
    return 0;
2160
0
  }
2161
0
  for (obj = p15card->obj_list; obj != NULL; obj = obj->next) {
2162
0
    if (obj->df != df)
2163
0
      continue;
2164
0
    r = func(ctx, obj, &tmp, &tmpsize);
2165
0
    if (r) {
2166
0
      free(tmp);
2167
0
      free(buf);
2168
0
      return r;
2169
0
    }
2170
0
    if (!tmpsize)
2171
0
      continue;
2172
0
    p = (u8 *) realloc(buf, bufsize + tmpsize);
2173
0
    if (!p) {
2174
0
      free(tmp);
2175
0
      free(buf);
2176
0
      return SC_ERROR_OUT_OF_MEMORY;
2177
0
    }
2178
0
    buf = p;
2179
0
    memcpy(buf + bufsize, tmp, tmpsize);
2180
0
    free(tmp);
2181
0
    tmp = NULL;
2182
0
    bufsize += tmpsize;
2183
0
  }
2184
0
  *buf_out = buf;
2185
0
  *bufsize_out = bufsize;
2186
2187
0
  return 0;
2188
0
}
2189
2190
2191
int
2192
sc_pkcs15_parse_df(struct sc_pkcs15_card *p15card, struct sc_pkcs15_df *df)
2193
1.73k
{
2194
1.73k
  struct sc_context *ctx = p15card->card->ctx;
2195
1.73k
  unsigned char *buf;
2196
1.73k
  const unsigned char *p;
2197
1.73k
  size_t bufsize;
2198
1.73k
  int r;
2199
1.73k
  struct sc_pkcs15_object *obj = NULL;
2200
1.73k
  int (* func)(struct sc_pkcs15_card *, struct sc_pkcs15_object *,
2201
1.73k
         const u8 **nbuf, size_t *nbufsize) = NULL;
2202
2203
1.73k
  sc_log(ctx, "called; path=%s, type=%d, enum=%d", sc_print_path(&df->path), df->type, df->enumerated);
2204
2205
1.73k
  if (df->enumerated)
2206
1.73k
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
2207
2208
1.73k
  switch (df->type) {
2209
147
  case SC_PKCS15_PRKDF:
2210
147
    func = sc_pkcs15_decode_prkdf_entry;
2211
147
    break;
2212
1.16k
  case SC_PKCS15_PUKDF:
2213
1.16k
    func = sc_pkcs15_decode_pukdf_entry;
2214
1.16k
    break;
2215
0
  case SC_PKCS15_SKDF:
2216
0
    func = sc_pkcs15_decode_skdf_entry;
2217
0
    break;
2218
351
  case SC_PKCS15_CDF:
2219
351
  case SC_PKCS15_CDF_TRUSTED:
2220
351
  case SC_PKCS15_CDF_USEFUL:
2221
351
    func = sc_pkcs15_decode_cdf_entry;
2222
351
    break;
2223
0
  case SC_PKCS15_DODF:
2224
0
    func = sc_pkcs15_decode_dodf_entry;
2225
0
    break;
2226
65
  case SC_PKCS15_AODF:
2227
65
    func = sc_pkcs15_decode_aodf_entry;
2228
65
    break;
2229
1.73k
  }
2230
1.73k
  if (func == NULL) {
2231
0
    sc_log(ctx, "unknown DF type: %d", df->type);
2232
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
2233
0
  }
2234
1.73k
  r = sc_pkcs15_read_file(p15card, &df->path, &buf, &bufsize, 0);
2235
1.73k
  LOG_TEST_RET(ctx, r, "pkcs15 read file failed");
2236
2237
210
  p = buf;
2238
210
  while (bufsize && *p != 0x00) {
2239
2240
94
    obj = calloc(1, sizeof(struct sc_pkcs15_object));
2241
94
    if (obj == NULL) {
2242
0
      r = SC_ERROR_OUT_OF_MEMORY;
2243
0
      goto ret;
2244
0
    }
2245
94
    r = func(p15card, obj, &p, &bufsize);
2246
94
    if (r) {
2247
94
      free(obj);
2248
94
      if (r == SC_ERROR_ASN1_END_OF_CONTENTS) {
2249
19
        r = 0;
2250
19
        break;
2251
19
      }
2252
75
      sc_log(ctx, "%s: Error decoding DF entry", sc_strerror(r));
2253
75
      goto ret;
2254
94
    }
2255
2256
0
    obj->df = df;
2257
0
    r = sc_pkcs15_add_object(p15card, obj);
2258
0
    if (r) {
2259
0
      if (obj->data)
2260
0
        free(obj->data);
2261
0
      free(obj);
2262
0
      sc_log(ctx, "%s: Error adding object", sc_strerror(r));
2263
0
      goto ret;
2264
0
    }
2265
0
    while (bufsize > 0 && *p == 00) {
2266
0
      bufsize--;
2267
0
      p++;
2268
0
    }
2269
135
  };
2270
2271
135
  if (r > 0)
2272
0
    r = 0;
2273
210
ret:
2274
210
  df->enumerated = 1;
2275
210
  free(buf);
2276
210
  LOG_FUNC_RETURN(ctx, r);
2277
210
}
2278
2279
2280
int
2281
sc_pkcs15_add_unusedspace(struct sc_pkcs15_card *p15card, const struct sc_path *path,
2282
    const struct sc_pkcs15_id *auth_id)
2283
0
{
2284
0
  struct sc_context *ctx = p15card->card->ctx;
2285
0
  struct sc_pkcs15_unusedspace *p = p15card->unusedspace_list, *new_unusedspace;
2286
2287
0
  if (path->count == -1) {
2288
0
    char pbuf[SC_MAX_PATH_STRING_SIZE];
2289
2290
0
    int r = sc_path_print(pbuf, sizeof(pbuf), path);
2291
0
    if (r != SC_SUCCESS)
2292
0
      pbuf[0] = '\0';
2293
2294
0
    sc_log(ctx, "No offset and length present in path %s", pbuf);
2295
0
    return SC_ERROR_INVALID_ARGUMENTS;
2296
0
  }
2297
2298
0
  new_unusedspace = calloc(1, sizeof(sc_pkcs15_unusedspace_t));
2299
0
  if (new_unusedspace == NULL)
2300
0
    return SC_ERROR_OUT_OF_MEMORY;
2301
0
  new_unusedspace->path = *path;
2302
0
  if (auth_id != NULL)
2303
0
    new_unusedspace->auth_id = *auth_id;
2304
2305
0
  if (p15card->unusedspace_list == NULL) {
2306
0
    p15card->unusedspace_list = new_unusedspace;
2307
0
    return 0;
2308
0
  }
2309
0
  while (p->next != NULL)
2310
0
    p = p->next;
2311
0
  p->next = new_unusedspace;
2312
0
  new_unusedspace->prev = p;
2313
2314
0
  return 0;
2315
0
}
2316
2317
2318
void
2319
sc_pkcs15_remove_unusedspace(struct sc_pkcs15_card *p15card, struct sc_pkcs15_unusedspace *unusedspace)
2320
0
{
2321
0
  if (!unusedspace)
2322
0
    return;
2323
2324
0
  if (!unusedspace->prev)
2325
0
    p15card->unusedspace_list = unusedspace->next;
2326
0
  else
2327
0
    unusedspace->prev->next = unusedspace->next;
2328
2329
0
  if (unusedspace->next)
2330
0
    unusedspace->next->prev = unusedspace->prev;
2331
2332
0
  free(unusedspace);
2333
0
}
2334
2335
2336
static void
2337
sc_pkcs15_free_unusedspace(struct sc_pkcs15_card *p15card)
2338
13.0k
{
2339
13.0k
  struct sc_pkcs15_unusedspace *cur = NULL, *next = NULL;
2340
2341
13.0k
  if (!p15card || !p15card->unusedspace_list)
2342
13.0k
    return;
2343
0
  for (cur = p15card->unusedspace_list; cur; cur = next)   {
2344
0
    next = cur->next;
2345
0
    free(cur);
2346
0
  }
2347
2348
0
  p15card->unusedspace_list = NULL;
2349
0
}
2350
2351
2352
int
2353
sc_pkcs15_encode_unusedspace(struct sc_context *ctx, struct sc_pkcs15_card *p15card,
2354
       unsigned char **buf, size_t *buflen)
2355
0
{
2356
0
  struct sc_path dummy_path;
2357
0
  static const struct sc_asn1_entry c_asn1_unusedspace[] = {
2358
0
    { "UnusedSpace", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
2359
0
    { NULL, 0, 0, 0, NULL, NULL }
2360
0
  };
2361
0
  static const struct sc_asn1_entry c_asn1_unusedspace_values[] = {
2362
0
    { "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
2363
0
    { "authId", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_OPTIONAL, NULL, NULL },
2364
0
    { NULL, 0, 0, 0, NULL, NULL }
2365
0
  };
2366
0
  struct sc_asn1_entry *asn1_unusedspace = NULL;
2367
0
  struct sc_asn1_entry *asn1_values = NULL;
2368
0
  int unusedspace_count = 0, r, c = 0;
2369
0
  struct sc_pkcs15_unusedspace *unusedspace = NULL;
2370
2371
0
  sc_format_path("3F00", &dummy_path);
2372
0
  dummy_path.index = dummy_path.count = 0;
2373
2374
0
  unusedspace = p15card->unusedspace_list;
2375
0
  for ( ; unusedspace != NULL; unusedspace = unusedspace->next)
2376
0
    unusedspace_count++;
2377
0
  if (unusedspace_count == 0) {
2378
    /* The standard says there has to be at least 1 entry,
2379
     * so we use a path with a length of 0 bytes */
2380
0
    r = sc_pkcs15_add_unusedspace(p15card, &dummy_path, NULL);
2381
0
    if (r)
2382
0
      return r;
2383
0
    unusedspace_count = 1;
2384
0
  }
2385
2386
0
  asn1_unusedspace = (struct sc_asn1_entry *)
2387
0
    malloc(sizeof(struct sc_asn1_entry) * (unusedspace_count + 1));
2388
0
  if (asn1_unusedspace == NULL) {
2389
0
    r = SC_ERROR_OUT_OF_MEMORY;
2390
0
    goto err;
2391
0
  }
2392
0
  asn1_values = (struct sc_asn1_entry *)
2393
0
    malloc(sizeof(struct sc_asn1_entry) * (unusedspace_count * 3));
2394
0
  if (asn1_values == NULL) {
2395
0
    r = SC_ERROR_OUT_OF_MEMORY;
2396
0
    goto err;
2397
0
  }
2398
2399
0
  for (unusedspace = p15card->unusedspace_list; unusedspace != NULL; unusedspace = unusedspace->next) {
2400
0
    sc_copy_asn1_entry(c_asn1_unusedspace, asn1_unusedspace + c);
2401
0
    sc_format_asn1_entry(asn1_unusedspace + c, asn1_values + 3*c, NULL, 1);
2402
0
    sc_copy_asn1_entry(c_asn1_unusedspace_values, asn1_values + 3*c);
2403
0
    sc_format_asn1_entry(asn1_values + 3*c, &unusedspace->path, NULL, 1);
2404
0
    sc_format_asn1_entry(asn1_values + 3*c+1, &unusedspace->auth_id, NULL,
2405
0
         unusedspace->auth_id.len > 0 ? 1 : 0);
2406
0
    c++;
2407
0
  }
2408
0
  asn1_unusedspace[c].name = NULL;
2409
2410
0
  r = sc_asn1_encode(ctx, asn1_unusedspace, buf, buflen);
2411
2412
0
err:
2413
0
  if (asn1_values != NULL)
2414
0
    free(asn1_values);
2415
0
  if (asn1_unusedspace != NULL)
2416
0
    free(asn1_unusedspace);
2417
2418
  /* If we added the dummy entry, remove it now */
2419
0
  if (unusedspace_count == 1 && sc_compare_path(&p15card->unusedspace_list->path, &dummy_path))
2420
0
    sc_pkcs15_remove_unusedspace(p15card, p15card->unusedspace_list);
2421
2422
0
  return r;
2423
0
}
2424
2425
2426
int
2427
sc_pkcs15_parse_unusedspace(const unsigned char *buf, size_t buflen, struct sc_pkcs15_card *p15card)
2428
0
{
2429
0
  const unsigned char *p = buf;
2430
0
  size_t left = buflen;
2431
0
  int r;
2432
0
  struct sc_path path;
2433
0
  struct sc_pkcs15_id auth_id;
2434
0
  struct sc_asn1_entry asn1_unusedspace[] = {
2435
0
    { "UnusedSpace", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
2436
0
    { NULL, 0, 0, 0, NULL, NULL }
2437
0
  };
2438
0
  struct sc_asn1_entry asn1_unusedspace_values[] = {
2439
0
    { "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
2440
0
    { "authId", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_OPTIONAL, NULL, NULL },
2441
0
    { NULL, 0, 0, 0, NULL, NULL }
2442
0
  };
2443
2444
  /* Clean the list if already present */
2445
0
  sc_pkcs15_free_unusedspace(p15card);
2446
2447
0
  sc_format_asn1_entry(asn1_unusedspace, asn1_unusedspace_values, NULL, 1);
2448
0
  sc_format_asn1_entry(asn1_unusedspace_values, &path, NULL, 1);
2449
0
  sc_format_asn1_entry(asn1_unusedspace_values+1, &auth_id, NULL, 0);
2450
2451
0
  while (left > 0) {
2452
0
    memset(&auth_id, 0, sizeof(auth_id));
2453
0
    r = sc_asn1_decode(p15card->card->ctx, asn1_unusedspace, p, left, &p, &left);
2454
0
    if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
2455
0
      break;
2456
0
    if (r < 0)
2457
0
      return r;
2458
    /* If the path length is 0, it's a dummy path then don't add it.
2459
     * If the path length isn't included (-1) then it's against the standard
2460
     *   but we'll just ignore it instead of returning an error. */
2461
0
    if (path.count > 0 && p15card->file_app) {
2462
0
      r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &path);
2463
0
      if (r < 0)
2464
0
        return r;
2465
0
      r = sc_pkcs15_add_unusedspace(p15card, &path, &auth_id);
2466
0
      if (r)
2467
0
        return r;
2468
0
    }
2469
0
  }
2470
2471
0
  p15card->unusedspace_read = 1;
2472
2473
0
  return 0;
2474
0
}
2475
2476
static int decompress_file(sc_card_t *card, unsigned char *buf, size_t buflen,
2477
    unsigned char **out, size_t *outlen, unsigned long flags)
2478
363
{
2479
363
  LOG_FUNC_CALLED(card->ctx);
2480
363
#ifdef ENABLE_ZLIB
2481
363
  int rv = SC_SUCCESS;
2482
363
  int method = 0;
2483
2484
363
  if (flags & SC_FILE_FLAG_COMPRESSED_GZIP) {
2485
0
    method = COMPRESSION_GZIP;
2486
363
  } else if (flags & SC_FILE_FLAG_COMPRESSED_ZLIB) {
2487
0
    method = COMPRESSION_ZLIB;
2488
363
  } else {
2489
363
    method = COMPRESSION_AUTO;
2490
363
  }
2491
2492
363
  rv = sc_decompress_alloc(out, outlen, buf, buflen, method);
2493
363
  if (rv != SC_SUCCESS) {
2494
222
    sc_log(card->ctx,  "Decompression failed: %d", rv);
2495
222
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_DATA);
2496
222
  }
2497
141
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
2498
#else
2499
  sc_log(card->ctx, "Compression not supported, no zlib");
2500
  LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
2501
#endif
2502
141
}
2503
2504
int
2505
sc_decode_do53(sc_context_t *ctx, u8 **data, size_t *data_len,
2506
    const u8 *buf, size_t buflen)
2507
0
{
2508
0
  struct sc_asn1_entry c_asn1_do53[] = {
2509
0
    { "do53", SC_ASN1_OCTET_STRING, SC_ASN1_APP|0x13, SC_ASN1_ALLOC|SC_ASN1_UNSIGNED, NULL, NULL },
2510
0
    { NULL, 0, 0, 0, NULL, NULL }
2511
0
  };
2512
0
  struct sc_asn1_entry asn1_do53[2];
2513
0
  int r;
2514
2515
0
  LOG_FUNC_CALLED(ctx);
2516
0
  sc_copy_asn1_entry(c_asn1_do53, asn1_do53);
2517
0
  sc_format_asn1_entry(asn1_do53, data, data_len, 0);
2518
2519
0
  r = sc_asn1_decode(ctx, asn1_do53, buf, buflen, NULL, NULL);
2520
0
  LOG_TEST_RET(ctx, r, "ASN.1 parsing of do-53 failed");
2521
2522
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
2523
0
}
2524
2525
2526
int
2527
sc_pkcs15_read_file(struct sc_pkcs15_card *p15card, const struct sc_path *in_path,
2528
    unsigned char **buf, size_t *buflen, int private_data)
2529
16.7k
{
2530
16.7k
  struct sc_context *ctx;
2531
16.7k
  struct sc_file *file = NULL;
2532
16.7k
  unsigned char *data = NULL;
2533
16.7k
  size_t  len = 0, offset = 0;
2534
16.7k
  int r;
2535
2536
16.7k
  if (p15card == NULL || p15card->card == NULL || in_path == NULL || buf == NULL) {
2537
0
    return SC_ERROR_INVALID_ARGUMENTS;
2538
0
  }
2539
16.7k
  ctx = p15card->card->ctx;
2540
2541
16.7k
  LOG_FUNC_CALLED(ctx);
2542
16.7k
  sc_log(ctx, "path=%s, index=%u, count=%d", sc_print_path(in_path), in_path->index, in_path->count);
2543
2544
16.7k
  r = -1; /* file state: not in cache */
2545
16.7k
  if (p15card->opts.use_file_cache
2546
7.07k
      && ((p15card->opts.use_file_cache & SC_PKCS15_OPTS_CACHE_ALL_FILES) || !private_data)) {
2547
7.07k
    r = sc_pkcs15_read_cached_file(p15card, in_path, &data, &len);
2548
2549
7.07k
    if (!r && in_path->aid.len > 0 && in_path->len >= 2)   {
2550
2
      struct sc_path parent = *in_path;
2551
2552
2
      parent.len -= 2;
2553
2
      parent.type = SC_PATH_TYPE_PATH;
2554
2
      r = sc_select_file(p15card->card, &parent, NULL);
2555
2
    }
2556
7.07k
  }
2557
2558
16.7k
  if (r) {
2559
16.3k
    r = sc_lock(p15card->card);
2560
16.3k
    if (r)
2561
0
      goto fail;
2562
16.3k
    r = sc_select_file(p15card->card, in_path, &file);
2563
16.3k
    if (r)
2564
11.9k
      goto fail_unlock;
2565
2566
    /* Handle the case where the ASN.1 Path object specified
2567
     * index and length values */
2568
2569
4.42k
    if (file->ef_structure == SC_FILE_EF_LINEAR_VARIABLE) {
2570
2571
      // in_path->index: record_no
2572
      // in_path->count: ignored!
2573
2574
74
      if(file->record_length > 0) {
2575
64
        if(file->record_length > MAX_FILE_SIZE) {
2576
0
          len = MAX_FILE_SIZE;
2577
0
          sc_log(ctx, "  record size truncated, encoded length: %zu", file->record_length);
2578
64
        } else {
2579
64
          len = file->record_length;
2580
64
        }
2581
64
      } else {
2582
10
        len = MAX_FILE_SIZE;
2583
10
      }
2584
2585
74
      if ((in_path->index <= 0) || (in_path->index > (int)(file->record_count))) {
2586
33
        sc_log(ctx, "  record number out of bounds: %d", in_path->index);
2587
33
        r = SC_ERROR_RECORD_NOT_FOUND;
2588
33
        goto fail_unlock;
2589
33
      }
2590
2591
4.34k
    } else {
2592
2593
4.34k
      if (in_path->count < 0) {
2594
2.72k
        if (file->size)
2595
2.60k
          len = (file->size > MAX_FILE_SIZE)? MAX_FILE_SIZE:file->size;
2596
129
        else
2597
129
          len = 1024;
2598
2.72k
        offset = 0;
2599
2.72k
      }
2600
1.61k
      else {
2601
1.61k
        offset = in_path->index;
2602
1.61k
        len = in_path->count;
2603
        /* Make sure we're within proper bounds */
2604
1.61k
        if (offset >= file->size || offset + len > file->size) {
2605
16
          r = SC_ERROR_INVALID_ASN1_OBJECT;
2606
16
          goto fail_unlock;
2607
16
        }
2608
1.61k
      }
2609
4.34k
    }
2610
2611
4.37k
    free(data);
2612
4.37k
    data = malloc(len);
2613
4.37k
    if (data == NULL) {
2614
0
      r = SC_ERROR_OUT_OF_MEMORY;
2615
0
      goto fail_unlock;
2616
0
    }
2617
2618
4.37k
    if (file->ef_structure == SC_FILE_EF_LINEAR_VARIABLE_TLV) {
2619
69
      unsigned int i;
2620
69
      size_t value_bytes_read = 0, record_len;
2621
69
      size_t record_bytes, copied, missing;
2622
69
      u8 record[256], tag;
2623
69
      const u8 *value;
2624
2625
      /* Stop before the reserved ISO 7816-4 record number 255. */
2626
194
      for (i = 1; i < 0xFF; i++) {
2627
        /* The aggregate size counts values, not record headers. */
2628
194
        r = sc_read_record(p15card->card, i, 0, record, sizeof(record), SC_RECORD_BY_REC_NR);
2629
194
        if (r == SC_ERROR_RECORD_NOT_FOUND)
2630
1
          break;
2631
193
        if (r < 0) {
2632
39
          goto fail_unlock;
2633
39
        }
2634
154
        if (r == 0)
2635
3
          break;
2636
151
        if ((size_t)r > sizeof(record)) {
2637
0
          r = SC_ERROR_INVALID_TLV_OBJECT;
2638
0
          goto fail_unlock;
2639
0
        }
2640
151
        record_bytes = (size_t)r;
2641
151
        value = record;
2642
151
        r = sc_simpletlv_read_tag(&value, record_bytes, &tag, &record_len,
2643
151
            SC_SIMPLETLV_BIG_ENDIAN);
2644
151
        if ((r != SC_SUCCESS && r != SC_ERROR_TLV_END_OF_CONTENTS) ||
2645
150
            record_len > len - value_bytes_read) {
2646
6
          r = SC_ERROR_INVALID_TLV_OBJECT;
2647
6
          goto fail_unlock;
2648
6
        }
2649
145
        copied = MIN(record_len, record_bytes - (size_t)(value - record));
2650
145
        memcpy(data + value_bytes_read, value, copied);
2651
145
        missing = record_len - copied;
2652
145
        if (missing > 0) {
2653
21
          r = sc_read_record(p15card->card, i, (unsigned int)record_bytes,
2654
21
              data + value_bytes_read + copied, missing, SC_RECORD_BY_REC_NR);
2655
21
          if (r < 0)
2656
19
            goto fail_unlock;
2657
2
          if ((size_t)r != missing) {
2658
1
            r = SC_ERROR_INVALID_TLV_OBJECT;
2659
1
            goto fail_unlock;
2660
1
          }
2661
2
        }
2662
125
        value_bytes_read += record_len;
2663
125
      }
2664
4
      len = value_bytes_read;
2665
4
      if (len > 0) {
2666
1
        unsigned char *tmp = realloc(data, len);
2667
2668
1
        if (tmp != NULL)
2669
1
          data = tmp;
2670
1
      }
2671
4
    }
2672
4.30k
    else if (file->ef_structure == SC_FILE_EF_LINEAR_VARIABLE) {
2673
41
      r = sc_read_record(p15card->card, in_path->index, (unsigned)offset, data, len, SC_RECORD_BY_REC_NR);
2674
41
      if (r < 0) {
2675
9
        goto fail_unlock;
2676
9
      }
2677
      /* sc_read_record may return less than requested */
2678
32
      len = r;
2679
32
    }
2680
4.26k
    else {
2681
4.26k
      unsigned long flags = 0;
2682
4.26k
      r = sc_read_binary(p15card->card, (unsigned)offset, data, len, &flags);
2683
4.26k
      if (r < 0) {
2684
926
        goto fail_unlock;
2685
926
      }
2686
      /* sc_read_binary may return less than requested */
2687
3.33k
      len = r;
2688
2689
3.33k
      if (flags & SC_FILE_FLAG_COMPRESSED_AUTO
2690
2.97k
          || flags & SC_FILE_FLAG_COMPRESSED_ZLIB
2691
2.97k
          || flags & SC_FILE_FLAG_COMPRESSED_GZIP) {
2692
363
        unsigned char *decompressed_buf = NULL;
2693
363
        size_t decompressed_len = 0;
2694
363
        r = decompress_file(p15card->card, data, len, &decompressed_buf, &decompressed_len, flags);
2695
363
        if (r != SC_SUCCESS) {
2696
222
          goto fail_unlock;
2697
222
        }
2698
141
        free(data);
2699
141
        data = decompressed_buf;
2700
141
        len = decompressed_len;
2701
141
      }
2702
3.33k
    }
2703
3.15k
    sc_unlock(p15card->card);
2704
2705
3.15k
    sc_file_free(file);
2706
2707
3.15k
    if (p15card->opts.use_file_cache && ((p15card->opts.use_file_cache & SC_PKCS15_OPTS_CACHE_ALL_FILES) || !private_data)) {
2708
697
      sc_pkcs15_cache_file(p15card, in_path, data, len);
2709
697
    }
2710
3.15k
    if (len == 0) {
2711
276
      free(data);
2712
276
      data = NULL;
2713
276
    }
2714
3.15k
  }
2715
3.48k
  *buf = data;
2716
3.48k
  *buflen = len;
2717
3.48k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
2718
2719
13.2k
fail_unlock:
2720
13.2k
  sc_unlock(p15card->card);
2721
13.2k
fail:
2722
13.2k
  free(data);
2723
13.2k
  sc_file_free(file);
2724
13.2k
  LOG_FUNC_RETURN(ctx, r);
2725
13.2k
}
2726
2727
2728
int
2729
sc_pkcs15_compare_id(const struct sc_pkcs15_id *id1, const struct sc_pkcs15_id *id2)
2730
9.35k
{
2731
9.35k
  if (id1 == NULL || id2 == NULL)
2732
0
    return 0;
2733
9.35k
  if (id1->len != id2->len)
2734
4.16k
    return 0;
2735
5.18k
  return memcmp(id1->value, id2->value, id1->len) == 0;
2736
9.35k
}
2737
2738
2739
void
2740
sc_pkcs15_format_id(const char *str, struct sc_pkcs15_id *id)
2741
115k
{
2742
115k
  size_t len;
2743
2744
115k
  if (!id)
2745
0
    return;
2746
115k
  len = sizeof(id->value);
2747
2748
115k
  if (sc_hex_to_bin(str, id->value, &len) != SC_SUCCESS)
2749
244
    id->len = 0;
2750
115k
  else
2751
115k
    id->len = len;
2752
115k
}
2753
2754
2755
const char *
2756
sc_pkcs15_print_id(const struct sc_pkcs15_id *id)
2757
592
{
2758
592
  static char buffer[256];
2759
2760
592
  sc_bin_to_hex(id->value, id->len, buffer, sizeof(buffer), '\0');
2761
592
  return buffer;
2762
592
}
2763
2764
2765
int
2766
sc_pkcs15_hex_string_to_id(const char *in, struct sc_pkcs15_id *out)
2767
0
{
2768
0
  out->len = sizeof(out->value);
2769
0
  return sc_hex_to_bin(in, out->value, &out->len);
2770
0
}
2771
2772
2773
int
2774
sc_pkcs15_make_absolute_path(const struct sc_path *parent, struct sc_path *child)
2775
14.5k
{
2776
  /* nothing to do if child has valid 'aid' */
2777
14.5k
  if (child->aid.len)
2778
0
    return SC_SUCCESS;
2779
2780
14.5k
  if (parent->aid.len)   {
2781
0
    sc_path_t ppath;
2782
2783
    /* child inherits parent's 'aid' */
2784
0
    child->aid = parent->aid;
2785
0
    if (!parent->len)
2786
0
      return SC_SUCCESS;
2787
2788
    /* parent has valid 'path' -- concatenate it with the child's one */
2789
0
    memcpy(&ppath, parent, sizeof(sc_path_t));
2790
0
    ppath.aid.len = 0;
2791
0
    ppath.type = SC_PATH_TYPE_FROM_CURRENT;
2792
0
    return sc_concatenate_path(child, &ppath, child);
2793
2794
0
  }
2795
14.5k
  else if (parent->type == SC_PATH_TYPE_DF_NAME)   {
2796
    /* child inherits parent's 'DF NAME' as 'aid' */
2797
74
    if (parent->len > sizeof(child->aid.value))
2798
0
      return SC_ERROR_WRONG_LENGTH;
2799
2800
74
    memcpy(child->aid.value, parent->value, parent->len);
2801
74
    child->aid.len = parent->len;
2802
2803
74
    return SC_SUCCESS;
2804
74
  }
2805
2806
  /* a 0 length path stays a 0 length path */
2807
14.4k
  if (child->len == 0)
2808
125
    return SC_SUCCESS;
2809
2810
14.3k
  if (sc_compare_path_prefix(sc_get_mf_path(), child))
2811
16
    return SC_SUCCESS;
2812
2813
14.3k
  return sc_concatenate_path(child, parent, child);
2814
14.3k
}
2815
2816
2817
void sc_pkcs15_free_object_content(struct sc_pkcs15_object *obj)
2818
48.1k
{
2819
48.1k
  if (obj->content.value && obj->content.len) {
2820
985
    if (obj->content_free) {
2821
985
      obj->content_free(obj->content.value, obj->content.len);
2822
985
    } else {
2823
0
      free(obj->content.value);
2824
0
    }
2825
985
  }
2826
48.1k
  obj->content.value = NULL;
2827
48.1k
  obj->content.len = 0;
2828
48.1k
}
2829
2830
2831
int
2832
sc_pkcs15_allocate_object_content(struct sc_context *ctx, struct sc_pkcs15_object *obj,
2833
    const unsigned char *value, size_t len)
2834
985
{
2835
985
  unsigned char *tmp_buf;
2836
2837
985
  if (!obj)
2838
0
    return SC_ERROR_INVALID_ARGUMENTS;
2839
2840
985
  if (!value || !len)   {
2841
0
    sc_pkcs15_free_object_content(obj);
2842
0
    return SC_SUCCESS;
2843
0
  }
2844
2845
  /* Need to pass by temporary variable,
2846
   * because 'value' and 'content.value' pointers can be the sames.
2847
   */
2848
985
  if (SC_PKCS15_TYPE_AUTH & obj->type
2849
0
      || SC_PKCS15_TYPE_SKEY & obj->type
2850
985
      || SC_PKCS15_TYPE_PRKEY & obj->type) {
2851
985
    tmp_buf = sc_mem_secure_alloc(len);
2852
985
    obj->content_free = sc_mem_secure_free;
2853
985
  } else {
2854
0
    tmp_buf = malloc(len);
2855
0
  }
2856
985
  if (!tmp_buf)
2857
0
    return SC_ERROR_OUT_OF_MEMORY;
2858
2859
985
  memcpy(tmp_buf, value, len);
2860
2861
985
  sc_pkcs15_free_object_content(obj);
2862
2863
985
  obj->content.value = tmp_buf;
2864
985
  obj->content.len = len;
2865
2866
985
  return SC_SUCCESS;
2867
985
}
2868
2869
2870
struct sc_supported_algo_info *
2871
sc_pkcs15_get_supported_algo(struct sc_pkcs15_card *p15card, unsigned operation, unsigned mechanism)
2872
0
{
2873
0
  struct sc_context *ctx = p15card->card->ctx;
2874
0
  struct sc_supported_algo_info *info = NULL;
2875
0
  int ii;
2876
2877
0
  for (ii=0;ii<SC_MAX_SUPPORTED_ALGORITHMS && p15card->tokeninfo->supported_algos[ii].reference; ii++)
2878
0
    if ((p15card->tokeninfo->supported_algos[ii].operations & operation)
2879
0
        && (p15card->tokeninfo->supported_algos[ii].mechanism == mechanism))
2880
0
      break;
2881
2882
0
  if (ii < SC_MAX_SUPPORTED_ALGORITHMS && p15card->tokeninfo->supported_algos[ii].reference)   {
2883
0
    info = &p15card->tokeninfo->supported_algos[ii];
2884
0
    sc_log(ctx, "found supported algorithm (ref:%X,mech:%X,ops:%X,algo_ref:%X)",
2885
0
        info->reference, info->mechanism, info->operations, info->algo_ref);
2886
0
  }
2887
2888
0
  return info;
2889
0
}
2890
2891
struct sc_supported_algo_info *
2892
sc_pkcs15_get_specific_supported_algo(struct sc_pkcs15_card *p15card, unsigned operation, unsigned mechanism, const struct sc_object_id *algo_oid)
2893
0
{
2894
0
  struct sc_context *ctx = p15card->card->ctx;
2895
0
  struct sc_supported_algo_info *info = NULL;
2896
0
  int ii;
2897
2898
0
  if (algo_oid == NULL)
2899
0
    return NULL;
2900
2901
0
  for (ii=0;ii<SC_MAX_SUPPORTED_ALGORITHMS && p15card->tokeninfo->supported_algos[ii].reference; ii++)
2902
0
    if ((p15card->tokeninfo->supported_algos[ii].operations & operation)
2903
0
        && (p15card->tokeninfo->supported_algos[ii].mechanism == mechanism)
2904
0
        && sc_compare_oid(algo_oid, &p15card->tokeninfo->supported_algos[ii].algo_id) == 1)
2905
0
      break;
2906
2907
0
  if (ii < SC_MAX_SUPPORTED_ALGORITHMS && p15card->tokeninfo->supported_algos[ii].reference)   {
2908
0
    info = &p15card->tokeninfo->supported_algos[ii];
2909
0
    sc_log(ctx, "found supported algorithm (ref:%X,mech:%X,ops:%X,algo_ref:%X)",
2910
0
        info->reference, info->mechanism, info->operations, info->algo_ref);
2911
0
  }
2912
2913
0
  return info;
2914
0
}
2915
2916
int
2917
sc_pkcs15_get_generalized_time(struct sc_context *ctx, char **out)
2918
0
{
2919
0
#ifdef HAVE_GETTIMEOFDAY
2920
0
  struct timeval tv;
2921
0
#endif
2922
0
  struct tm tm;
2923
0
  time_t t;
2924
2925
0
  if (!ctx || !out)
2926
0
    return SC_ERROR_INVALID_ARGUMENTS;
2927
0
  *out = NULL;
2928
2929
0
#ifdef HAVE_GETTIMEOFDAY
2930
0
  gettimeofday(&tv, NULL);
2931
0
  t = tv.tv_sec;
2932
#else
2933
  t = time(NULL);
2934
#endif
2935
2936
#ifdef _WIN32
2937
  if (0 != gmtime_s(&tm, &t))
2938
    LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);
2939
#else
2940
0
  if (NULL == gmtime_r(&t, &tm))
2941
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);
2942
0
#endif
2943
2944
0
  *out = calloc(1, 16);
2945
0
  if (*out == NULL)
2946
0
    LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "memory failure");
2947
2948
  /* print time in generalized time format */
2949
0
  if (!strftime(*out, 16, "%Y%m%d%H%M%SZ", &tm)) {
2950
0
    free(*out);
2951
0
    LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "strftime failed");
2952
0
  }
2953
2954
0
  return SC_SUCCESS;
2955
0
}
2956
2957
2958
int
2959
sc_pkcs15_add_supported_algo_ref(struct sc_pkcs15_object *obj, struct sc_supported_algo_info *algo)
2960
0
{
2961
0
  unsigned int ii, *algo_refs = NULL;
2962
2963
0
  if (!algo)
2964
0
    return SC_SUCCESS;
2965
2966
0
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
2967
0
  case SC_PKCS15_TYPE_PRKEY:
2968
0
    algo_refs = ((struct sc_pkcs15_prkey_info *)obj->data)->algo_refs;
2969
0
    break;
2970
0
  case SC_PKCS15_TYPE_PUBKEY:
2971
0
    algo_refs = ((struct sc_pkcs15_pubkey_info *)obj->data)->algo_refs;
2972
0
    break;
2973
0
  case SC_PKCS15_TYPE_SKEY:
2974
0
    algo_refs = ((struct sc_pkcs15_skey_info *)obj->data)->algo_refs;
2975
0
    break;
2976
0
  }
2977
0
  if (!algo_refs)
2978
0
    return SC_ERROR_NOT_SUPPORTED;
2979
2980
0
  for (ii=0;ii<SC_MAX_SUPPORTED_ALGORITHMS && *(algo_refs + ii);ii++)
2981
0
    if (*(algo_refs + ii) == algo->reference)
2982
0
      return SC_SUCCESS;
2983
2984
0
  for (ii=0;ii<SC_MAX_SUPPORTED_ALGORITHMS;ii++)   {
2985
0
    if (*(algo_refs + ii) == 0)   {
2986
0
      *(algo_refs + ii) = algo->reference;
2987
0
      return SC_SUCCESS;
2988
0
    }
2989
0
  }
2990
2991
0
  return SC_ERROR_TOO_MANY_OBJECTS;
2992
0
}
2993
2994
2995
int
2996
sc_pkcs15_get_object_id(const struct sc_pkcs15_object *obj, struct sc_pkcs15_id *out)
2997
0
{
2998
0
  if (!obj || !out)
2999
0
    return SC_ERROR_INVALID_ARGUMENTS;
3000
3001
0
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
3002
0
  case SC_PKCS15_TYPE_CERT:
3003
0
    *out = ((struct sc_pkcs15_cert_info *) obj->data)->id;
3004
0
    break;
3005
0
  case SC_PKCS15_TYPE_PRKEY:
3006
0
    *out = ((struct sc_pkcs15_prkey_info *) obj->data)->id;
3007
0
    break;
3008
0
  case SC_PKCS15_TYPE_PUBKEY:
3009
0
    *out = ((struct sc_pkcs15_pubkey_info *) obj->data)->id;
3010
0
    break;
3011
0
  case SC_PKCS15_TYPE_SKEY:
3012
0
    *out = ((struct sc_pkcs15_skey_info *) obj->data)->id;
3013
0
    break;
3014
0
  case SC_PKCS15_TYPE_AUTH:
3015
0
    *out = ((struct sc_pkcs15_auth_info *) obj->data)->auth_id;
3016
0
    break;
3017
0
  case SC_PKCS15_TYPE_DATA_OBJECT:
3018
0
    *out = ((struct sc_pkcs15_data_info *) obj->data)->id;
3019
0
    break;
3020
0
  default:
3021
0
    return SC_ERROR_NOT_SUPPORTED;
3022
0
  }
3023
3024
0
  return SC_SUCCESS;
3025
0
}
3026
3027
/*
3028
 * Simplified GUID serializing.
3029
 * Ex. {3F2504E0-4F89-11D3-9A0C-0305E82C3301}
3030
 *
3031
 * There is no variant, version number and other special meaning fields
3032
 *  that are described in RFC-4122 .
3033
 */
3034
int
3035
sc_pkcs15_serialize_guid(unsigned char *in, size_t in_size, unsigned flags,
3036
    char *out, size_t out_size)
3037
0
{
3038
0
  if (in_size < 16)
3039
0
    return SC_ERROR_BUFFER_TOO_SMALL;
3040
0
  if (out_size < 39)
3041
0
    return SC_ERROR_BUFFER_TOO_SMALL;
3042
3043
0
  snprintf(out, out_size,
3044
0
      "%s%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%s",
3045
0
      flags ? "" : "{",
3046
0
      in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7],
3047
0
      in[8], in[9], in[10], in[11], in[12], in[13], in[14], in[15],
3048
0
      flags ? "" : "}");
3049
3050
0
  return SC_SUCCESS;
3051
0
}
3052
3053
int
3054
sc_pkcs15_get_object_guid(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_object *obj,
3055
    unsigned flags, unsigned char *out, size_t *out_size)
3056
0
{
3057
0
  struct sc_context *ctx = p15card->card->ctx;
3058
0
  struct sc_serial_number serialnr;
3059
0
  struct sc_pkcs15_id  id;
3060
0
  unsigned char guid_bin[SC_PKCS15_MAX_ID_SIZE + SC_MAX_SERIALNR];
3061
0
  int rv;
3062
0
  size_t guid_bin_size;
3063
3064
0
  LOG_FUNC_CALLED(ctx);
3065
0
  if(!out || !out_size)
3066
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
3067
3068
0
  if (p15card->ops.get_guid)   {
3069
0
    rv = p15card->ops.get_guid(p15card, obj, out, out_size);
3070
0
    LOG_FUNC_RETURN(ctx, rv);
3071
0
  }
3072
3073
0
  rv = sc_pkcs15_aux_get_md_guid(p15card, obj, flags, out, out_size);
3074
0
  if (rv == SC_SUCCESS)
3075
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
3076
0
  else if (rv != SC_ERROR_NOT_SUPPORTED)
3077
0
    LOG_TEST_RET(ctx, rv, "Failed to get alternative object GUID");
3078
3079
0
  memset(out, 0, *out_size);
3080
3081
0
  rv = sc_pkcs15_get_object_id(obj, &id);
3082
0
  LOG_TEST_RET(ctx, rv, "Cannot get object's ID");
3083
3084
0
  if (p15card->tokeninfo && p15card->tokeninfo->serial_number)   {
3085
    /* The serial from EF(TokenInfo) is preferred because of the
3086
     * "--serial" parameter of pkcs15-init. */
3087
0
    serialnr.len = SC_MAX_SERIALNR;
3088
0
    rv = sc_hex_to_bin(p15card->tokeninfo->serial_number, serialnr.value, &serialnr.len);
3089
0
    if (rv) {
3090
      /* Fallback in case hex_to_bin fails due to unexpected characters */
3091
0
      serialnr.len = strlen(p15card->tokeninfo->serial_number);
3092
0
      if (serialnr.len > SC_MAX_SERIALNR)
3093
0
        serialnr.len = SC_MAX_SERIALNR;
3094
3095
0
      memcpy(serialnr.value, p15card->tokeninfo->serial_number, serialnr.len);
3096
0
    }
3097
0
  } else if (p15card->card->serialnr.len)   {
3098
0
    serialnr = p15card->card->serialnr;
3099
0
  } else   {
3100
0
    rv = sc_card_ctl(p15card->card, SC_CARDCTL_GET_SERIALNR, &serialnr);
3101
0
    LOG_TEST_RET(ctx, rv, "'GET_SERIALNR' CTL failed and other serial numbers not present");
3102
0
  }
3103
3104
0
  memset(guid_bin, 0, sizeof(guid_bin));
3105
0
  memcpy(guid_bin, id.value, id.len);
3106
0
  memcpy(guid_bin + id.len, serialnr.value, serialnr.len);
3107
0
  guid_bin_size = id.len + serialnr.len;
3108
3109
  /*
3110
   * If OpenSSL is available (SHA1), then rather use the hash of the data
3111
   * - this also protects against data being too short
3112
   */
3113
0
#ifdef ENABLE_OPENSSL
3114
0
  SHA1(guid_bin, guid_bin_size, guid_bin);
3115
0
  guid_bin_size = SHA_DIGEST_LENGTH;
3116
#else
3117
  /* If guid_bin has a size larger than 16 bytes
3118
   * force the remaining bytes up to 16 bytes to be zero
3119
   * so sc_pkcs15_serialize_guid won't fail because the size is less than 16
3120
   */
3121
  if (guid_bin_size < 16)
3122
    guid_bin_size = 16;
3123
#endif
3124
3125
0
  rv = sc_pkcs15_serialize_guid(guid_bin, guid_bin_size, flags, (char *)out, *out_size);
3126
0
  LOG_TEST_RET(ctx, rv, "Serialize GUID error");
3127
3128
0
  *out_size = strlen((char *)out);
3129
0
  LOG_FUNC_RETURN(ctx, rv);
3130
0
}
3131
3132
3133
static int
3134
sc_pkcs15_aux_get_md_guid(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_object *obj,
3135
    unsigned flags,
3136
    unsigned char *out, size_t *out_size)
3137
0
{
3138
0
  struct sc_context *ctx = p15card->card->ctx;
3139
0
  struct sc_pkcs15_prkey_info *prkey_info = NULL;
3140
0
  int rv;
3141
3142
0
  LOG_FUNC_CALLED(ctx);
3143
0
  if(!out || !out_size)
3144
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
3145
3146
0
  if ((obj->type & SC_PKCS15_TYPE_CLASS_MASK) != SC_PKCS15_TYPE_PRKEY)
3147
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
3148
3149
0
  prkey_info = (struct sc_pkcs15_prkey_info *)obj->data;
3150
0
  if (!prkey_info->aux_data || prkey_info->aux_data->type != SC_AUX_DATA_TYPE_MD_CMAP_RECORD)
3151
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
3152
3153
0
  rv = sc_aux_data_get_md_guid(ctx, prkey_info->aux_data, flags, out, out_size);
3154
0
  LOG_FUNC_RETURN(ctx, rv);
3155
0
}
3156
3157
3158
void
3159
sc_pkcs15_free_key_params(struct sc_pkcs15_key_params *params)
3160
7.99k
{
3161
7.99k
  if (!params)
3162
0
    return;
3163
7.99k
  if (params->data && params->free_params)
3164
0
    params->free_params(params->data);
3165
7.99k
  else if (params->data)
3166
0
    free(params->data);
3167
3168
  params->data = NULL;
3169
7.99k
}
3170