Coverage Report

Created: 2026-12-03 06:15

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
10.2k
{
146
10.2k
  int r;
147
10.2k
  size_t ii;
148
10.2k
  u8 serial[128];
149
10.2k
  size_t serial_len = sizeof(serial);
150
10.2k
  u8 mnfid[SC_PKCS15_MAX_LABEL_SIZE];
151
10.2k
  size_t mnfid_len  = sizeof(mnfid) - 1;
152
10.2k
  u8 label[SC_PKCS15_MAX_LABEL_SIZE];
153
10.2k
  size_t label_len = sizeof(label) - 1;
154
10.2k
  u8 last_update[32], profile_indication[SC_PKCS15_MAX_LABEL_SIZE];
155
10.2k
  size_t lupdate_len = sizeof(last_update) - 1, pi_len = sizeof(profile_indication) - 1;
156
10.2k
  size_t flags_len   = sizeof(ti->flags);
157
10.2k
  u8 preferred_language[64];
158
10.2k
  size_t lang_length = sizeof(preferred_language) - 1;
159
10.2k
  struct sc_asn1_entry asn1_supported_algorithms[SC_MAX_SUPPORTED_ALGORITHMS + 1],
160
10.2k
      asn1_algo_infos[SC_MAX_SUPPORTED_ALGORITHMS][7],
161
10.2k
      asn1_algo_infos_parameters[SC_MAX_SUPPORTED_ALGORITHMS][3];
162
10.2k
  size_t reference_len = sizeof(ti->supported_algos[0].reference);
163
10.2k
  size_t mechanism_len = sizeof(ti->supported_algos[0].mechanism);
164
10.2k
  size_t parameter_len = sizeof(ti->supported_algos[0].parameters);
165
10.2k
  size_t operations_len = sizeof(ti->supported_algos[0].operations);
166
10.2k
  size_t algo_ref_len = sizeof(ti->supported_algos[0].algo_ref);
167
168
10.2k
  struct sc_asn1_entry asn1_last_update[C_ASN1_LAST_UPDATE_SIZE];
169
10.2k
  struct sc_asn1_entry asn1_profile_indication[C_ASN1_PROFILE_INDICATION_SIZE];
170
10.2k
  struct sc_asn1_entry asn1_toki_attrs[C_ASN1_TOKI_ATTRS_SIZE], asn1_tokeninfo[3], asn1_twlabel[3];
171
172
10.2k
  memset(last_update, 0, sizeof(last_update));
173
10.2k
  memset(label, 0, sizeof(label));
174
10.2k
  memset(profile_indication, 0, sizeof(profile_indication));
175
10.2k
  memset(mnfid, 0, sizeof(mnfid));
176
177
10.2k
  sc_copy_asn1_entry(c_asn1_twlabel, asn1_twlabel);
178
10.2k
  sc_copy_asn1_entry(c_asn1_toki_attrs, asn1_toki_attrs);
179
10.2k
  sc_copy_asn1_entry(c_asn1_tokeninfo, asn1_tokeninfo);
180
10.2k
  sc_copy_asn1_entry(c_asn1_last_update, asn1_last_update);
181
10.2k
  sc_format_asn1_entry(asn1_twlabel, label, &label_len, 0);
182
10.2k
  sc_copy_asn1_entry(c_asn1_profile_indication, asn1_profile_indication);
183
184
174k
  for (ii=0; ii<SC_MAX_SUPPORTED_ALGORITHMS; ii++) {
185
164k
    sc_copy_asn1_entry(c_asn1_algorithm_info, asn1_algo_infos[ii]);
186
164k
    sc_copy_asn1_entry(c_asn1_algorithm_info_parameters,
187
164k
      asn1_algo_infos_parameters[ii]);
188
164k
  }
189
10.2k
  sc_copy_asn1_entry(c_asn1_supported_algorithms, asn1_supported_algorithms);
190
191
174k
  for (ii=0; ii<SC_MAX_SUPPORTED_ALGORITHMS; ii++)   {
192
164k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 0, &ti->supported_algos[ii].reference, &reference_len, 0);
193
164k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 1, &ti->supported_algos[ii].mechanism, &mechanism_len, 0);
194
164k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 2,
195
164k
      asn1_algo_infos_parameters[ii], NULL, 0);
196
164k
    sc_format_asn1_entry(asn1_algo_infos_parameters[ii] + 0,
197
164k
      NULL, NULL, 0);
198
164k
    sc_format_asn1_entry(asn1_algo_infos_parameters[ii] + 1,
199
164k
      &ti->supported_algos[ii].parameters, &parameter_len, 0);
200
164k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 3, &ti->supported_algos[ii].operations, &operations_len, 0);
201
164k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 4, &ti->supported_algos[ii].algo_id, NULL, 1);
202
164k
    sc_format_asn1_entry(asn1_algo_infos[ii] + 5, &ti->supported_algos[ii].algo_ref, &algo_ref_len, 0);
203
164k
    sc_format_asn1_entry(asn1_supported_algorithms + ii, asn1_algo_infos[ii], NULL, 0);
204
164k
  }
205
206
10.2k
  sc_format_asn1_entry(asn1_last_update + 0, last_update, &lupdate_len, 0);
207
10.2k
  sc_format_asn1_entry(asn1_last_update + 1, &ti->last_update.path, NULL, 0);
208
209
10.2k
  sc_format_asn1_entry(asn1_profile_indication + 0, &ti->profile_indication.oid, NULL, 0);
210
10.2k
  sc_format_asn1_entry(asn1_profile_indication + 1, profile_indication, &pi_len, 0);
211
212
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 0, &ti->version, NULL, 0);
213
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 1, serial, &serial_len, 0);
214
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 2, mnfid, &mnfid_len, 0);
215
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 3, label, &label_len, 0);
216
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 4, asn1_twlabel, NULL, 0);
217
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 5, &ti->flags, &flags_len, 0);
218
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 6, &ti->seInfo, &ti->num_seInfo, 0);
219
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 7, NULL, NULL, 0);
220
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 8, asn1_supported_algorithms, NULL, 0);
221
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 9, NULL, NULL, 0);
222
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 10, NULL, NULL, 0);
223
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 11, asn1_last_update, NULL, 0);
224
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 12, preferred_language, &lang_length, 0);
225
10.2k
  sc_format_asn1_entry(asn1_toki_attrs + 13, asn1_profile_indication, NULL, 0);
226
10.2k
  sc_format_asn1_entry(asn1_tokeninfo, asn1_toki_attrs, NULL, 0);
227
228
10.2k
  r = sc_asn1_decode(ctx, asn1_tokeninfo, buf, blen, NULL, NULL);
229
10.2k
  if (r != SC_SUCCESS) {
230
    /* The decoding could have allocated something we need to free */
231
7.74k
    sc_pkcs15_clear_tokeninfo(ti);
232
7.74k
    LOG_TEST_RET(ctx, r, "ASN.1 parsing of EF(TokenInfo) failed");
233
7.74k
  }
234
235
2.51k
  if (asn1_toki_attrs[1].flags & SC_ASN1_PRESENT && serial_len > 0)   {
236
1.04k
    free(ti->serial_number);
237
1.04k
    ti->serial_number = malloc(serial_len * 2 + 1);
238
1.04k
    if (ti->serial_number == NULL)
239
0
      return SC_ERROR_OUT_OF_MEMORY;
240
1.04k
    sc_bin_to_hex(serial, serial_len, ti->serial_number, serial_len * 2 + 1, 0);
241
1.04k
    sc_log(ctx, "TokenInfo.serialNunmber '%s'", ti->serial_number);
242
1.04k
  }
243
244
2.51k
  if (ti->manufacturer_id == NULL) {
245
2.51k
    if (asn1_toki_attrs[2].flags & SC_ASN1_PRESENT)
246
1.10k
      ti->manufacturer_id = strdup((char *) mnfid);
247
1.41k
    else
248
1.41k
      ti->manufacturer_id = strdup("(unknown)");
249
2.51k
    if (ti->manufacturer_id == NULL)
250
0
      return SC_ERROR_OUT_OF_MEMORY;
251
2.51k
  }
252
2.51k
  if (ti->label == NULL) {
253
2.51k
    if (asn1_toki_attrs[3].flags & SC_ASN1_PRESENT ||
254
2.08k
        asn1_toki_attrs[4].flags & SC_ASN1_PRESENT)
255
432
      ti->label = strdup((char *) label);
256
2.08k
    else
257
2.08k
      ti->label = strdup("(unknown)");
258
2.51k
    if (ti->label == NULL)
259
0
      return SC_ERROR_OUT_OF_MEMORY;
260
2.51k
  }
261
2.51k
  if (asn1_toki_attrs[11].flags & SC_ASN1_PRESENT) {
262
499
    if (asn1_last_update[0].flags & SC_ASN1_PRESENT)   {
263
183
      sc_log(ctx, "LastUpdate.generalizedTime present");
264
183
      ti->last_update.gtime = strdup((char *)last_update);
265
183
      if (ti->last_update.gtime == NULL)
266
0
        return SC_ERROR_OUT_OF_MEMORY;
267
183
    }
268
316
    else if (asn1_last_update[1].flags & SC_ASN1_PRESENT)  {
269
230
      sc_log(ctx, "LastUpdate.referencedTime present");
270
230
    }
271
499
  }
272
2.51k
  if (asn1_toki_attrs[12].flags & SC_ASN1_PRESENT) {
273
26
    preferred_language[lang_length] = 0;
274
26
    ti->preferred_language = strdup((char *)preferred_language);
275
26
    if (ti->preferred_language == NULL)
276
0
      return SC_ERROR_OUT_OF_MEMORY;
277
26
  }
278
279
2.51k
  sc_init_oid(&ti->profile_indication.oid);
280
2.51k
  if (asn1_toki_attrs[13].flags & SC_ASN1_PRESENT) {
281
463
    if (asn1_profile_indication[0].flags & SC_ASN1_PRESENT)   {
282
10
      sc_log(ctx, "ProfileIndication.oid present");
283
10
    }
284
453
    else if (asn1_profile_indication[1].flags & SC_ASN1_PRESENT)  {
285
430
      sc_log(ctx, "ProfileIndication.name present");
286
430
      ti->profile_indication.name = strdup((char *)profile_indication);
287
430
      if (ti->profile_indication.name == NULL)
288
0
        return SC_ERROR_OUT_OF_MEMORY;
289
430
    }
290
463
  }
291
292
2.51k
  sc_log(ctx, "LastUpdate.path '%s'", sc_print_path(&ti->last_update.path));
293
2.51k
  sc_log(ctx, "ProfileIndication.name '%s'",  ti->profile_indication.name);
294
2.51k
  return SC_SUCCESS;
295
2.51k
}
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
214
{
302
214
  int r, ii;
303
214
  size_t serial_len, mnfid_len, label_len, flags_len, last_upd_len, pi_len, lang_len;
304
305
214
  struct sc_asn1_entry asn1_toki_attrs[C_ASN1_TOKI_ATTRS_SIZE];
306
214
  struct sc_asn1_entry asn1_tokeninfo[2];
307
214
  struct sc_asn1_entry asn1_supported_algorithms[SC_MAX_SUPPORTED_ALGORITHMS + 1],
308
214
      asn1_algo_infos[SC_MAX_SUPPORTED_ALGORITHMS][7],
309
214
      asn1_algo_infos_parameters[SC_MAX_SUPPORTED_ALGORITHMS][3];
310
214
  size_t reference_len = sizeof(ti->supported_algos[0].reference);
311
214
  size_t mechanism_len = sizeof(ti->supported_algos[0].mechanism);
312
214
  size_t parameter_len = sizeof(ti->supported_algos[0].parameters);
313
214
  size_t operations_len = sizeof(ti->supported_algos[0].operations);
314
214
  size_t algo_ref_len = sizeof(ti->supported_algos[0].algo_ref);
315
214
  struct sc_asn1_entry asn1_last_update[C_ASN1_LAST_UPDATE_SIZE];
316
214
  struct sc_asn1_entry asn1_profile_indication[C_ASN1_PROFILE_INDICATION_SIZE];
317
214
  u8 serial[128];
318
319
214
  sc_copy_asn1_entry(c_asn1_toki_attrs, asn1_toki_attrs);
320
214
  sc_copy_asn1_entry(c_asn1_tokeninfo, asn1_tokeninfo);
321
214
  sc_copy_asn1_entry(c_asn1_last_update, asn1_last_update);
322
214
  sc_copy_asn1_entry(c_asn1_profile_indication, asn1_profile_indication);
323
324
623
  for (ii=0; ii<SC_MAX_SUPPORTED_ALGORITHMS && ti->supported_algos[ii].reference; ii++) {
325
409
    sc_copy_asn1_entry(c_asn1_algorithm_info, asn1_algo_infos[ii]);
326
409
    sc_copy_asn1_entry(c_asn1_algorithm_info_parameters,
327
409
      asn1_algo_infos_parameters[ii]);
328
409
  }
329
214
  sc_copy_asn1_entry(c_asn1_supported_algorithms, asn1_supported_algorithms);
330
331
623
  for (ii=0; ii<SC_MAX_SUPPORTED_ALGORITHMS && ti->supported_algos[ii].reference; ii++)   {
332
409
    sc_format_asn1_entry(asn1_algo_infos[ii] + 0, &ti->supported_algos[ii].reference, &reference_len, 1);
333
409
    sc_format_asn1_entry(asn1_algo_infos[ii] + 1, &ti->supported_algos[ii].mechanism, &mechanism_len, 1);
334
409
    sc_format_asn1_entry(asn1_algo_infos[ii] + 2,
335
409
      asn1_algo_infos_parameters[ii], NULL, 1);
336
409
    if (!sc_valid_oid(&ti->supported_algos[ii].parameters)) {
337
409
      sc_format_asn1_entry(asn1_algo_infos_parameters[ii] + 0,
338
409
        NULL, NULL, 1);
339
409
    }
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
409
    sc_format_asn1_entry(asn1_algo_infos[ii] + 3, &ti->supported_algos[ii].operations, &operations_len, 1);
345
409
    sc_format_asn1_entry(asn1_algo_infos[ii] + 4, &ti->supported_algos[ii].algo_id, NULL, 1);
346
409
    sc_format_asn1_entry(asn1_algo_infos[ii] + 5, &ti->supported_algos[ii].algo_ref, &algo_ref_len, 1);
347
409
    sc_format_asn1_entry(asn1_supported_algorithms + ii, asn1_algo_infos[ii], NULL, 1);
348
409
  }
349
350
214
  sc_format_asn1_entry(asn1_toki_attrs + 0, &ti->version, NULL, 1);
351
214
  if (ti->serial_number != NULL) {
352
74
    serial_len = 0;
353
74
    if (strlen(ti->serial_number)/2 > sizeof(serial))
354
0
      return SC_ERROR_BUFFER_TOO_SMALL;
355
74
    serial_len = sizeof(serial);
356
74
    if (sc_hex_to_bin(ti->serial_number, serial, &serial_len) < 0)
357
0
      return SC_ERROR_INVALID_ARGUMENTS;
358
74
    sc_format_asn1_entry(asn1_toki_attrs + 1, serial, &serial_len, 1);
359
74
  }
360
140
  else   {
361
140
    sc_format_asn1_entry(asn1_toki_attrs + 1, NULL, NULL, 0);
362
140
  }
363
364
214
  if (ti->manufacturer_id != NULL) {
365
74
    mnfid_len = strlen(ti->manufacturer_id);
366
74
    sc_format_asn1_entry(asn1_toki_attrs + 2, ti->manufacturer_id, &mnfid_len, 1);
367
74
  }
368
140
  else    {
369
140
    sc_format_asn1_entry(asn1_toki_attrs + 2, NULL, NULL, 0);
370
140
  }
371
372
214
  if (ti->label != NULL) {
373
74
    label_len = strlen(ti->label);
374
74
    sc_format_asn1_entry(asn1_toki_attrs + 3, ti->label, &label_len, 1);
375
74
  }
376
140
  else   {
377
140
    sc_format_asn1_entry(asn1_toki_attrs + 3, NULL, NULL, 0);
378
140
  }
379
380
214
  if (ti->flags) {
381
74
    flags_len = sizeof(ti->flags);
382
74
    sc_format_asn1_entry(asn1_toki_attrs + 5, &ti->flags, &flags_len, 1);
383
74
  }
384
140
  else   {
385
140
    sc_format_asn1_entry(asn1_toki_attrs + 5, NULL, NULL, 0);
386
140
  }
387
388
214
  if (ti->num_seInfo)
389
22
    sc_format_asn1_entry(asn1_toki_attrs + 6, ti->seInfo, &ti->num_seInfo, 1);
390
192
  else
391
192
    sc_format_asn1_entry(asn1_toki_attrs + 6, NULL, NULL, 0);
392
393
214
  sc_format_asn1_entry(asn1_toki_attrs + 7, NULL, NULL, 0);
394
395
214
  if (ti->supported_algos[0].reference)
396
164
    sc_format_asn1_entry(asn1_toki_attrs + 8, asn1_supported_algorithms, NULL, 1);
397
50
  else
398
50
    sc_format_asn1_entry(asn1_toki_attrs + 8, NULL, NULL, 0);
399
400
214
  sc_format_asn1_entry(asn1_toki_attrs + 9, NULL, NULL, 0);
401
214
  sc_format_asn1_entry(asn1_toki_attrs + 10, NULL, NULL, 0);
402
403
214
  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
214
  else if (ti->last_update.gtime != NULL) {
408
214
    last_upd_len = strlen(ti->last_update.gtime);
409
214
    sc_format_asn1_entry(asn1_last_update + 0, ti->last_update.gtime, &last_upd_len, 1);
410
214
    sc_format_asn1_entry(asn1_toki_attrs + 11, asn1_last_update, NULL, 1);
411
214
  }
412
0
  else   {
413
0
    sc_format_asn1_entry(asn1_toki_attrs + 11, NULL, NULL, 0);
414
0
  }
415
416
214
  if (ti->preferred_language != NULL) {
417
1
    lang_len = strlen(ti->preferred_language);
418
1
    sc_format_asn1_entry(asn1_toki_attrs + 12, ti->preferred_language, &lang_len, 1);
419
213
  } else {
420
213
    sc_format_asn1_entry(asn1_toki_attrs + 12, NULL, NULL, 0);
421
213
  }
422
423
214
  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
214
  else if (ti->profile_indication.name)   {
428
2
    pi_len = strlen(ti->profile_indication.name);
429
2
    sc_format_asn1_entry(asn1_profile_indication + 1, ti->profile_indication.name, &pi_len, 1);
430
2
    sc_format_asn1_entry(asn1_toki_attrs + 13, asn1_profile_indication, NULL, 1);
431
2
  }
432
212
  else    {
433
212
    sc_format_asn1_entry(asn1_toki_attrs + 13, NULL, NULL, 0);
434
212
  }
435
436
214
  sc_format_asn1_entry(asn1_tokeninfo, asn1_toki_attrs, NULL, 1);
437
438
214
  r = sc_asn1_encode(ctx, asn1_tokeninfo, buf, buflen);
439
214
  LOG_TEST_RET(ctx, r, "sc_asn1_encode() failed");
440
441
70
  return SC_SUCCESS;
442
214
}
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
980
{
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
980
  if (p15card->card->type == SC_CARD_TYPE_OBERTHUR_AUTHENTIC_3_2)   {
464
15
    sc_file_free(p15card->file_odf);
465
15
    p15card->file_odf = NULL;
466
15
    sc_file_free(p15card->file_tokeninfo);
467
15
    p15card->file_tokeninfo = NULL;
468
15
  }
469
980
}
470
471
static int
472
parse_ddo(struct sc_pkcs15_card *p15card, const u8 * buf, size_t buflen)
473
1.28k
{
474
1.28k
  struct sc_context *ctx = p15card->card->ctx;
475
1.28k
  struct sc_asn1_entry asn1_ddo[7];
476
1.28k
  sc_path_t odf_path, ti_path, us_path;
477
1.28k
  struct sc_iid iid;
478
1.28k
  struct sc_aid aid;
479
1.28k
  int r;
480
481
1.28k
  LOG_FUNC_CALLED(ctx);
482
483
1.28k
  iid.len = sizeof(iid.value);
484
1.28k
  aid.len = sizeof(aid.value);
485
486
1.28k
  sc_copy_asn1_entry(c_asn1_ddo, asn1_ddo);
487
1.28k
  sc_format_asn1_entry(asn1_ddo + 1, &odf_path, NULL, 0);
488
1.28k
  sc_format_asn1_entry(asn1_ddo + 2, &ti_path, NULL, 0);
489
1.28k
  sc_format_asn1_entry(asn1_ddo + 3, &us_path, NULL, 0);
490
1.28k
  sc_format_asn1_entry(asn1_ddo + 4, iid.value, &iid.len, 0);
491
1.28k
  sc_format_asn1_entry(asn1_ddo + 5, aid.value, &aid.len, 0);
492
493
1.28k
  r = sc_asn1_decode(ctx, asn1_ddo, buf, buflen, NULL, NULL);
494
1.28k
  LOG_TEST_RET(ctx, r, "DDO parsing failed");
495
496
980
  if (asn1_ddo[1].flags & SC_ASN1_PRESENT) {
497
192
    sc_file_free(p15card->file_odf);
498
192
    p15card->file_odf = sc_file_new();
499
192
    if (p15card->file_odf == NULL)
500
0
      goto mem_err;
501
192
    p15card->file_odf->path = odf_path;
502
192
  }
503
980
  if (asn1_ddo[2].flags & SC_ASN1_PRESENT) {
504
288
    sc_file_free(p15card->file_tokeninfo);
505
288
    p15card->file_tokeninfo = sc_file_new();
506
288
    if (p15card->file_tokeninfo == NULL)
507
0
      goto mem_err;
508
288
    p15card->file_tokeninfo->path = ti_path;
509
288
  }
510
980
  if (asn1_ddo[3].flags & SC_ASN1_PRESENT) {
511
54
    sc_file_free(p15card->file_unusedspace);
512
54
    p15card->file_unusedspace = sc_file_new();
513
54
    if (p15card->file_unusedspace == NULL)
514
0
      goto mem_err;
515
54
    p15card->file_unusedspace->path = us_path;
516
54
  }
517
980
  if (asn1_ddo[4].flags & SC_ASN1_PRESENT) {
518
53
    sc_debug(ctx, SC_LOG_DEBUG_ASN1, "DDO.IID '%s'", sc_dump_hex(iid.value, iid.len));
519
53
    memcpy(&p15card->app->ddo.iid, &iid, sizeof(struct sc_iid));
520
53
  }
521
980
  if (asn1_ddo[5].flags & SC_ASN1_PRESENT) {
522
63
    sc_debug(ctx, SC_LOG_DEBUG_ASN1, "DDO.AID '%s'", sc_dump_hex(aid.value, aid.len));
523
63
    memcpy(&p15card->app->ddo.aid, &aid, sizeof(struct sc_aid));
524
63
  }
525
526
980
  fix_authentic_ddo(p15card);
527
980
  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
16.6k
{
542
16.6k
  struct sc_context *ctx  = p15card->card->ctx;
543
16.6k
  struct sc_file *file = NULL;
544
16.6k
  struct sc_asn1_entry asn1_last_update[C_ASN1_LAST_UPDATE_SIZE];
545
16.6k
  unsigned char *content, last_update[32] = {0};
546
16.6k
  size_t lupdate_len = sizeof(last_update) - 1;
547
16.6k
  int r, content_len;
548
16.6k
  size_t size;
549
550
16.6k
  if (p15card->tokeninfo->last_update.gtime)
551
844
    goto done;
552
553
15.8k
  if (!p15card->tokeninfo->last_update.path.len)
554
13.5k
    return NULL;
555
556
2.27k
  r = sc_select_file(p15card->card, &p15card->tokeninfo->last_update.path, &file);
557
2.27k
  if (r < 0)
558
1.95k
    return NULL;
559
560
322
  size = file->size ? file->size : 1024;
561
322
  sc_file_free(file);
562
563
322
  content = calloc(1, size);
564
322
  if (!content)
565
0
    return NULL;
566
567
322
  r = sc_read_binary(p15card->card, 0, content, size, 0);
568
322
  if (r < 0) {
569
127
    free(content);
570
127
    return NULL;
571
127
  }
572
195
  content_len = r;
573
574
195
  sc_copy_asn1_entry(c_asn1_last_update, asn1_last_update);
575
195
  sc_format_asn1_entry(asn1_last_update + 0, last_update, &lupdate_len, 0);
576
577
195
  r = sc_asn1_decode(ctx, asn1_last_update, content, content_len, NULL, NULL);
578
195
  free(content);
579
195
  if (r < 0)
580
50
    return NULL;
581
582
145
  if (asn1_last_update[0].flags & SC_ASN1_PRESENT) {
583
7
    p15card->tokeninfo->last_update.gtime = strdup((char *)last_update);
584
7
    if (!p15card->tokeninfo->last_update.gtime)
585
0
      return NULL;
586
7
  }
587
989
done:
588
989
  sc_log(ctx, "lastUpdate.gtime '%s'", p15card->tokeninfo->last_update.gtime);
589
989
  return p15card->tokeninfo->last_update.gtime;
590
145
}
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
1.62k
{
622
1.62k
  const unsigned char *p = buf;
623
1.62k
  size_t left = buflen;
624
1.62k
  int r, i, type;
625
1.62k
  struct sc_path path;
626
1.62k
  struct sc_asn1_entry asn1_obj_or_path[] = {
627
1.62k
    { "path", SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_SEQUENCE, 0, &path, NULL },
628
1.62k
    { NULL, 0, 0, 0, NULL, NULL }
629
1.62k
  };
630
1.62k
  struct sc_asn1_entry asn1_odf[10];
631
632
1.62k
  sc_copy_asn1_entry(c_asn1_odf, asn1_odf);
633
16.2k
  for (i = 0; asn1_odf[i].name != NULL; i++)
634
14.5k
    sc_format_asn1_entry(asn1_odf + i, asn1_obj_or_path, NULL, 0);
635
9.54k
  while (left > 0) {
636
9.05k
    r = sc_asn1_decode_choice(p15card->card->ctx, asn1_odf, p, left, &p, &left);
637
9.05k
    if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
638
849
      break;
639
8.20k
    if (r < 0)
640
280
      return r;
641
7.92k
    type = r;
642
7.92k
    if (p15card->file_app) {
643
7.92k
      r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &path);
644
7.92k
      if (r < 0)
645
5
        return r;
646
7.92k
      r = sc_pkcs15_add_df(p15card, odf_indexes[type], &path);
647
7.92k
      if (r)
648
0
        return r;
649
7.92k
    }
650
7.92k
  }
651
1.33k
  return 0;
652
1.62k
}
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
1.08k
{
659
1.08k
  struct sc_path path;
660
1.08k
  struct sc_asn1_entry asn1_obj_or_path[] = {
661
1.08k
    { "path", SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_SEQUENCE, 0, &path, NULL },
662
1.08k
    { NULL, 0, 0, 0, NULL, NULL }
663
1.08k
  };
664
1.08k
  struct sc_asn1_entry *asn1_paths = NULL;
665
1.08k
  struct sc_asn1_entry *asn1_odf = NULL;
666
1.08k
  int df_count = 0, r, c = 0;
667
1.08k
  const int nr_indexes = sizeof(odf_indexes)/sizeof(odf_indexes[0]);
668
1.08k
  struct sc_pkcs15_df *df;
669
670
1.08k
  df = p15card->df_list;
671
4.79k
  while (df != NULL) {
672
3.70k
    df_count++;
673
3.70k
    df = df->next;
674
3.70k
  };
675
1.08k
  if (df_count == 0)
676
1.08k
    LOG_TEST_RET(ctx, SC_ERROR_OBJECT_NOT_FOUND, "No DF's found.");
677
678
1.08k
  asn1_odf = malloc(sizeof(struct sc_asn1_entry) * (df_count + 1));
679
1.08k
  if (asn1_odf == NULL) {
680
0
    r = SC_ERROR_OUT_OF_MEMORY;
681
0
    goto err;
682
0
  }
683
1.08k
  asn1_paths = malloc(sizeof(struct sc_asn1_entry) * (df_count * 2));
684
1.08k
  if (asn1_paths == NULL) {
685
0
    r = SC_ERROR_OUT_OF_MEMORY;
686
0
    goto err;
687
0
  }
688
4.79k
  for (df = p15card->df_list; df != NULL; df = df->next) {
689
3.70k
    int j, type = -1;
690
691
12.4k
    for (j = 0; j < nr_indexes; j++)
692
12.4k
      if (odf_indexes[j] == df->type) {
693
3.70k
        type = j;
694
3.70k
        break;
695
3.70k
      }
696
3.70k
    if (type == -1) {
697
0
      sc_log(ctx, "Unsupported DF type.");
698
0
      continue;
699
0
    }
700
3.70k
    asn1_odf[c] = c_asn1_odf[type];
701
3.70k
    sc_format_asn1_entry(asn1_odf + c, asn1_paths + 2*c, NULL, 1);
702
3.70k
    sc_copy_asn1_entry(asn1_obj_or_path, asn1_paths + 2*c);
703
3.70k
    sc_format_asn1_entry(asn1_paths + 2*c, &df->path, NULL, 1);
704
3.70k
    c++;
705
3.70k
  }
706
1.08k
  asn1_odf[c].name = NULL;
707
1.08k
  r = sc_asn1_encode(ctx, asn1_odf, buf, buflen);
708
1.08k
err:
709
1.08k
  if (asn1_paths != NULL)
710
1.08k
    free(asn1_paths);
711
1.08k
  if (asn1_odf != NULL)
712
1.08k
    free(asn1_odf);
713
1.08k
  return r;
714
1.08k
}
715
716
717
struct sc_pkcs15_card *
718
sc_pkcs15_card_new(void)
719
105k
{
720
105k
  struct sc_pkcs15_card *p15card;
721
722
105k
  p15card = calloc(1, sizeof(struct sc_pkcs15_card));
723
105k
  if (p15card == NULL)
724
0
    return NULL;
725
726
105k
  p15card->tokeninfo = calloc(1, sizeof(struct sc_pkcs15_tokeninfo));
727
105k
  if (p15card->tokeninfo == NULL) {
728
0
    free(p15card);
729
0
    return NULL;
730
0
  }
731
732
105k
  p15card->magic = SC_PKCS15_CARD_MAGIC;
733
105k
  return p15card;
734
105k
}
735
736
737
struct sc_pkcs15_tokeninfo *
738
sc_pkcs15_tokeninfo_new(void)
739
65.6k
{
740
65.6k
  struct sc_pkcs15_tokeninfo *tokeninfo;
741
742
65.6k
  tokeninfo = calloc(1, sizeof(struct sc_pkcs15_tokeninfo));
743
65.6k
  if (tokeninfo == NULL) {
744
0
    return NULL;
745
0
  }
746
747
65.6k
  sc_init_oid(&tokeninfo->profile_indication.oid);
748
749
65.6k
  return tokeninfo;
750
65.6k
}
751
752
static void
753
sc_pkcs15_clear_tokeninfo(struct sc_pkcs15_tokeninfo *tokeninfo)
754
179k
{
755
179k
  if (!tokeninfo)
756
0
    return;
757
758
179k
  free(tokeninfo->label);
759
179k
  tokeninfo->label = NULL;
760
179k
  free(tokeninfo->serial_number);
761
179k
  tokeninfo->serial_number = NULL;
762
179k
  free(tokeninfo->manufacturer_id);
763
179k
  tokeninfo->manufacturer_id = NULL;
764
179k
  free(tokeninfo->last_update.gtime);
765
179k
  tokeninfo->last_update.gtime = NULL;
766
179k
  free(tokeninfo->preferred_language);
767
179k
  tokeninfo->preferred_language = NULL;
768
179k
  free(tokeninfo->profile_indication.name);
769
179k
  tokeninfo->profile_indication.name = NULL;
770
179k
  if (tokeninfo->seInfo != NULL) {
771
81
    unsigned i;
772
169
    for (i = 0; i < tokeninfo->num_seInfo; i++)
773
88
      free(tokeninfo->seInfo[i]);
774
81
    free(tokeninfo->seInfo);
775
81
    tokeninfo->seInfo = NULL;
776
81
  }
777
179k
}
778
779
void
780
sc_pkcs15_free_tokeninfo(struct sc_pkcs15_tokeninfo *tokeninfo)
781
180k
{
782
180k
  if (!tokeninfo)
783
9.06k
    return;
784
785
170k
  sc_pkcs15_clear_tokeninfo(tokeninfo);
786
170k
  free(tokeninfo);
787
170k
}
788
789
void
790
sc_pkcs15_free_app(struct sc_pkcs15_card *p15card)
791
317k
{
792
317k
  if (p15card && p15card->app) {
793
9.30k
    free(p15card->app->label);
794
9.30k
    free(p15card->app->ddo.value);
795
9.30k
    free(p15card->app);
796
9.30k
    p15card->app = NULL;
797
9.30k
  }
798
317k
}
799
800
801
void
802
sc_pkcs15_card_free(struct sc_pkcs15_card *p15card)
803
126k
{
804
126k
  if (p15card == NULL || p15card->magic != SC_PKCS15_CARD_MAGIC)
805
21.0k
    return;
806
807
105k
  if (p15card->ops.clear)
808
4.77k
    p15card->ops.clear(p15card);
809
810
  /* For more complicated MD data a dedicated release procedure
811
   * has to be implemented. */
812
105k
  if (p15card->md_data)
813
0
    free(p15card->md_data);
814
815
105k
  sc_pkcs15_free_app(p15card);
816
105k
  sc_pkcs15_remove_objects(p15card);
817
105k
  sc_pkcs15_remove_dfs(p15card);
818
105k
  sc_pkcs15_free_unusedspace(p15card);
819
105k
  p15card->unusedspace_read = 0;
820
821
105k
  sc_file_free(p15card->file_app);
822
105k
  sc_file_free(p15card->file_tokeninfo);
823
105k
  sc_file_free(p15card->file_odf);
824
105k
  sc_file_free(p15card->file_unusedspace);
825
826
105k
  p15card->magic = 0;
827
105k
  sc_pkcs15_free_tokeninfo(p15card->tokeninfo);
828
105k
  sc_pkcs15_free_app(p15card);
829
105k
  free(p15card);
830
105k
}
831
832
833
void
834
sc_pkcs15_card_clear(struct sc_pkcs15_card *p15card)
835
100k
{
836
100k
  if (p15card == NULL)
837
0
    return;
838
839
100k
  if (p15card->ops.clear)
840
4.43k
    p15card->ops.clear(p15card);
841
842
100k
  p15card->flags = 0;
843
100k
  p15card->tokeninfo->version = 0;
844
100k
  p15card->tokeninfo->flags   = 0;
845
846
100k
  sc_pkcs15_remove_objects(p15card);
847
100k
  sc_pkcs15_remove_dfs(p15card);
848
849
100k
  p15card->df_list = NULL;
850
100k
  sc_file_free(p15card->file_app);
851
100k
  p15card->file_app = NULL;
852
100k
  sc_file_free(p15card->file_tokeninfo);
853
100k
  p15card->file_tokeninfo = NULL;
854
100k
  sc_file_free(p15card->file_odf);
855
100k
  p15card->file_odf = NULL;
856
100k
  sc_file_free(p15card->file_unusedspace);
857
100k
  p15card->file_unusedspace = NULL;
858
859
100k
  free(p15card->tokeninfo->label);
860
100k
  p15card->tokeninfo->label = NULL;
861
100k
  free(p15card->tokeninfo->serial_number);
862
100k
  p15card->tokeninfo->serial_number = NULL;
863
100k
  free(p15card->tokeninfo->manufacturer_id);
864
100k
  p15card->tokeninfo->manufacturer_id = NULL;
865
100k
  free(p15card->tokeninfo->last_update.gtime);
866
100k
  p15card->tokeninfo->last_update.gtime = NULL;
867
100k
  free(p15card->tokeninfo->preferred_language);
868
100k
  p15card->tokeninfo->preferred_language = NULL;
869
100k
  free(p15card->tokeninfo->profile_indication.name);
870
100k
  p15card->tokeninfo->profile_indication.name = NULL;
871
100k
  if (p15card->tokeninfo->seInfo != NULL) {
872
47
    size_t i;
873
111
    for (i = 0; i < p15card->tokeninfo->num_seInfo; i++)
874
64
      free(p15card->tokeninfo->seInfo[i]);
875
47
    free(p15card->tokeninfo->seInfo);
876
47
    p15card->tokeninfo->seInfo     = NULL;
877
47
    p15card->tokeninfo->num_seInfo = 0;
878
47
  }
879
880
100k
  sc_pkcs15_free_app(p15card);
881
100k
}
882
883
884
struct sc_app_info *
885
sc_find_app(struct sc_card *card, struct sc_aid *aid)
886
86.7k
{
887
86.7k
  int ii;
888
889
86.7k
  if (card->app_count <= 0)
890
79.6k
    return NULL;
891
892
7.00k
  if (!aid || !aid->len)
893
3.59k
    return card->app[0];
894
895
5.32k
  for (ii=0; ii < card->app_count; ii++) {
896
5.27k
    if (card->app[ii]->aid.len != aid->len)
897
491
      continue;
898
4.78k
    if (memcmp(card->app[ii]->aid.value, aid->value, aid->len))
899
1.42k
      continue;
900
3.36k
    return card->app[ii];
901
4.78k
  }
902
48
  return NULL;
903
3.41k
}
904
905
906
static struct sc_app_info *
907
sc_dup_app_info(const struct sc_app_info *info)
908
6.95k
{
909
6.95k
  struct sc_app_info *out = calloc(1, sizeof(struct sc_app_info));
910
911
6.95k
  if (!out)
912
0
    return NULL;
913
914
6.95k
  memcpy(out, info, sizeof(struct sc_app_info));
915
916
6.95k
  if (info->label) {
917
314
    out->label = strdup(info->label);
918
314
    if (!out->label) {
919
0
      free(out);
920
0
      return NULL;
921
0
    }
922
314
  } else
923
6.64k
    out->label = NULL;
924
925
6.95k
  out->ddo.value = malloc(info->ddo.len);
926
6.95k
  if (!out->ddo.value) {
927
0
    free(out->label);
928
0
    free(out);
929
0
    return NULL;
930
0
  }
931
6.95k
  memcpy(out->ddo.value, info->ddo.value, info->ddo.len);
932
6.95k
  out->ddo.len = info->ddo.len;
933
934
6.95k
  return out;
935
6.95k
}
936
937
938
struct sc_app_info *
939
sc_pkcs15_get_application_by_type(struct sc_card * card, char *app_type)
940
33.4k
{
941
33.4k
  struct sc_app_info *out = NULL;
942
33.4k
  scconf_block *conf_block = NULL;
943
33.4k
  int i, rv;
944
945
33.4k
  if (!card)
946
0
    return NULL;
947
948
33.4k
  if (card->app_count < 0)   {
949
33.4k
    rv = sc_enum_apps(card);
950
33.4k
    if (rv < 0 && rv != SC_ERROR_FILE_NOT_FOUND)
951
25.6k
      return NULL;
952
33.4k
  }
953
954
7.74k
  conf_block = sc_get_conf_block(card->ctx, "framework", "pkcs15", 1);
955
7.74k
  if (!conf_block)
956
7.74k
    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
7.74k
}
980
981
int
982
sc_pkcs15_bind_internal_odf(struct sc_pkcs15_card *p15card)
983
6.98k
{
984
6.98k
  struct sc_path tmppath;
985
6.98k
  struct sc_card    *card = p15card->card;
986
6.98k
  struct sc_context *ctx = card->ctx;
987
6.98k
  struct sc_pkcs15_df *df;
988
6.98k
  unsigned char *buf = NULL;
989
6.98k
  size_t len;
990
6.98k
  int    err, ok = 0;
991
992
6.98k
  LOG_FUNC_CALLED(ctx);
993
6.98k
  if (p15card->file_odf == NULL) {
994
    /* check if an ODF is present; we don't know yet whether we have a pkcs15 card */
995
6.94k
    sc_format_path("5031", &tmppath);
996
6.94k
    err = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &tmppath);
997
6.94k
    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
6.94k
    sc_log(ctx, "absolute path to EF(ODF) %s", sc_print_path(&tmppath));
1002
6.94k
    err = sc_select_file(card, &tmppath, &p15card->file_odf);
1003
6.94k
  }
1004
44
  else {
1005
44
    tmppath = p15card->file_odf->path;
1006
44
    sc_file_free(p15card->file_odf);
1007
44
    p15card->file_odf = NULL;
1008
44
    err = sc_select_file(card, &tmppath, &p15card->file_odf);
1009
44
  }
1010
1011
6.98k
  if (err != SC_SUCCESS) {
1012
2.25k
    sc_log(ctx, "EF(ODF) not found in '%s'", sc_print_path(&tmppath));
1013
2.25k
    goto end;
1014
2.25k
  }
1015
1016
4.72k
  len = p15card->file_odf->size;
1017
4.72k
  if (!len) {
1018
1.70k
    sc_log(ctx, "EF(ODF) is empty");
1019
1.70k
    goto end;
1020
1.70k
  }
1021
3.02k
  if (len > MAX_FILE_SIZE) {
1022
917
    sc_log(ctx, "EF(ODF) too large");
1023
917
    goto end;
1024
917
  }
1025
2.10k
  buf = malloc(len);
1026
2.10k
  if(buf == NULL) {
1027
0
    err = SC_ERROR_OUT_OF_MEMORY;
1028
0
    goto end;
1029
0
  }
1030
1031
2.10k
  err = -1; /* file state: not in cache */
1032
2.10k
  if (p15card->opts.use_file_cache) {
1033
793
    err = sc_pkcs15_read_cached_file(p15card, &tmppath, &buf, &len);
1034
793
    if (err == SC_SUCCESS)
1035
15
      err = (int)len;
1036
793
  }
1037
2.10k
  if (err < 0) {
1038
2.08k
    err = sc_read_binary(card, 0, buf, len, 0);
1039
2.08k
    if (err < 2) {
1040
482
      if (err < 0) {
1041
404
        sc_log(ctx, "read EF(ODF) file error: %s", sc_strerror(err));
1042
404
      } else {
1043
78
        err = SC_ERROR_PKCS15_APP_NOT_FOUND;
1044
78
        sc_log(ctx, "Invalid content of EF(ODF): %s", sc_strerror(err));
1045
78
      }
1046
482
      goto end;
1047
482
    }
1048
    /* sc_read_binary may return less than requested */
1049
1.60k
    len = err;
1050
1051
1.60k
    if (p15card->opts.use_file_cache) {
1052
575
      sc_pkcs15_cache_file(p15card, &tmppath, buf, len);
1053
575
    }
1054
1.60k
  }
1055
1056
1.62k
  if (parse_odf(buf, len, p15card)) {
1057
285
    err = SC_ERROR_PKCS15_APP_NOT_FOUND;
1058
285
    sc_log(ctx, "Unable to parse ODF");
1059
285
    goto end;
1060
285
  }
1061
1.33k
  free(buf);
1062
1.33k
  buf = NULL;
1063
1064
1.33k
  sc_log(ctx, "The following DFs were found:");
1065
8.66k
  for (df = p15card->df_list; df; df = df->next)
1066
7.32k
    sc_log(ctx, "  DF type %u, path %s, index %u, count %d", df->type,
1067
1.33k
        sc_print_path(&df->path), df->path.index, df->path.count);
1068
1069
1.33k
  ok = 1;
1070
6.98k
end:
1071
6.98k
  if (buf != NULL)
1072
767
    free(buf);
1073
6.98k
  if (!ok) {
1074
5.64k
    LOG_FUNC_RETURN(ctx, err);
1075
5.64k
  }
1076
1077
1.33k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1078
1.33k
}
1079
1080
int
1081
sc_pkcs15_bind_internal_tokeninfo(struct sc_pkcs15_card *p15card)
1082
81.8k
{
1083
81.8k
  struct sc_path tmppath;
1084
81.8k
  struct sc_card *card = p15card->card;
1085
81.8k
  struct sc_context *ctx = card->ctx;
1086
81.8k
  struct sc_pkcs15_tokeninfo tokeninfo;
1087
81.8k
  unsigned char *buf = NULL;
1088
81.8k
  size_t len;
1089
81.8k
  int err, ok = 0;
1090
1091
81.8k
  LOG_FUNC_CALLED(ctx);
1092
1093
81.8k
  if (p15card->file_tokeninfo == NULL) {
1094
81.6k
    sc_format_path("5032", &tmppath);
1095
81.6k
    err = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &tmppath);
1096
81.6k
    if (err != SC_SUCCESS)   {
1097
18
      sc_log(ctx, "Cannot make absolute path to EF(TokenInfo); error:%i", err);
1098
18
      goto end;
1099
18
    }
1100
81.6k
    sc_log(ctx, "absolute path to EF(TokenInfo) %s", sc_print_path(&tmppath));
1101
81.6k
  }
1102
234
  else {
1103
234
    tmppath = p15card->file_tokeninfo->path;
1104
234
    sc_file_free(p15card->file_tokeninfo);
1105
234
    p15card->file_tokeninfo = NULL;
1106
234
  }
1107
1108
81.8k
  err = sc_select_file(card, &tmppath, &p15card->file_tokeninfo);
1109
81.8k
  if (err)   {
1110
71.5k
    sc_log(ctx, "cannot select EF(TokenInfo) file: %s", sc_strerror(err));
1111
71.5k
    goto end;
1112
71.5k
  }
1113
1114
10.3k
  len = p15card->file_tokeninfo->size;
1115
10.3k
  if (!len) {
1116
4.95k
    sc_log(ctx, "EF(TokenInfo) is empty");
1117
4.95k
    goto end;
1118
4.95k
  }
1119
5.35k
  if (len > MAX_FILE_SIZE) {
1120
1.26k
    sc_log(ctx, "EF(TokenInfo) too large");
1121
1.26k
    goto end;
1122
1.26k
  }
1123
4.09k
  buf = malloc(len);
1124
4.09k
  if(buf == NULL) {
1125
0
    err = SC_ERROR_OUT_OF_MEMORY;
1126
0
    goto end;
1127
0
  }
1128
1129
4.09k
  err = -1; /* file state: not in cache */
1130
4.09k
  if (p15card->opts.use_file_cache) {
1131
1.70k
    err = sc_pkcs15_read_cached_file(p15card, &tmppath, &buf, &len);
1132
1.70k
    if (err == SC_SUCCESS)
1133
1
      err = (int)len;
1134
1.70k
  }
1135
4.09k
  if (err < 0) {
1136
4.08k
    err = sc_read_binary(card, 0, buf, len, 0);
1137
4.08k
    if (err <= 2) {
1138
2.27k
      if (err < 0)   {
1139
1.91k
        sc_log(ctx, "read EF(TokenInfo) file error: %s", sc_strerror(err));
1140
1.91k
      } else {
1141
357
        err = SC_ERROR_PKCS15_APP_NOT_FOUND;
1142
357
        sc_log(ctx, "Invalid content of EF(TokenInfo): %s", sc_strerror(err));
1143
357
      }
1144
2.27k
      goto end;
1145
2.27k
    }
1146
    /* sc_read_binary may return less than requested */
1147
1.81k
    len = err;
1148
1149
1.81k
    if (p15card->opts.use_file_cache) {
1150
1.01k
      sc_pkcs15_cache_file(p15card, &tmppath, buf, len);
1151
1.01k
    }
1152
1.81k
  }
1153
1154
1.82k
  memset(&tokeninfo, 0, sizeof(tokeninfo));
1155
1.82k
  err = sc_pkcs15_parse_tokeninfo(ctx, &tokeninfo, buf, (size_t)err);
1156
1.82k
  if (err != SC_SUCCESS)   {
1157
1.05k
    sc_log(ctx, "cannot parse TokenInfo content: %s", sc_strerror(err));
1158
1.05k
    goto end;
1159
1.05k
  }
1160
1161
765
  sc_pkcs15_clear_tokeninfo(p15card->tokeninfo);
1162
765
  *(p15card->tokeninfo) = tokeninfo;
1163
1164
765
  if (!p15card->tokeninfo->serial_number && 0 == card->serialnr.len) {
1165
319
    sc_card_ctl(p15card->card, SC_CARDCTL_GET_SERIALNR, &card->serialnr);
1166
319
  }
1167
1168
765
  if (!p15card->tokeninfo->serial_number && card->serialnr.len)   {
1169
75
    char *serial = calloc(1, card->serialnr.len*2 + 1);
1170
75
    size_t ii;
1171
75
    if (!serial) {
1172
0
      err = SC_ERROR_OUT_OF_MEMORY;
1173
0
      goto end;
1174
0
    }
1175
1176
992
    for(ii=0;ii<card->serialnr.len;ii++)
1177
917
      sprintf(serial + ii*2, "%02X", *(card->serialnr.value + ii));
1178
1179
75
    p15card->tokeninfo->serial_number = serial;
1180
75
    sc_log(ctx, "p15card->tokeninfo->serial_number %s", p15card->tokeninfo->serial_number);
1181
75
  }
1182
1183
765
  ok = 1;
1184
81.8k
end:
1185
81.8k
  if (buf != NULL)
1186
4.09k
    free(buf);
1187
81.8k
  if (!ok) {
1188
81.1k
    LOG_FUNC_RETURN(ctx, err);
1189
81.1k
  }
1190
1191
765
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1192
765
}
1193
1194
int
1195
sc_pkcs15_bind_internal(struct sc_pkcs15_card *p15card, struct sc_aid *aid)
1196
86.7k
{
1197
86.7k
  struct sc_card *card = p15card->card;
1198
86.7k
  struct sc_context *ctx = card->ctx;
1199
86.7k
  const struct sc_app_info *info = NULL;
1200
86.7k
  int err, ok = 0;
1201
1202
86.7k
  LOG_FUNC_CALLED(ctx);
1203
  /* Enumerate apps now */
1204
86.7k
  if (card->app_count < 0) {
1205
42.1k
    err = sc_enum_apps(card);
1206
42.1k
    if (err != SC_SUCCESS)
1207
40.9k
      sc_log(ctx, "unable to enumerate apps: %s", sc_strerror(err));
1208
42.1k
  }
1209
86.7k
  sc_file_free(p15card->file_app);
1210
86.7k
  p15card->file_app = sc_file_new();
1211
86.7k
  if (p15card->file_app == NULL) {
1212
0
    err = SC_ERROR_OUT_OF_MEMORY;
1213
0
    goto end;
1214
0
  }
1215
1216
86.7k
  sc_format_path("3F005015", &p15card->file_app->path);
1217
1218
86.7k
  info = sc_find_app(card, aid);
1219
86.7k
  if (info) {
1220
6.95k
    sc_log(ctx, "bind to application('%s',aid:'%s')", info->label, sc_dump_hex(info->aid.value, info->aid.len));
1221
6.95k
    sc_pkcs15_free_app(p15card);
1222
6.95k
    p15card->app = sc_dup_app_info(info);
1223
6.95k
    if (!p15card->app) {
1224
0
      err = SC_ERROR_OUT_OF_MEMORY;
1225
0
      goto end;
1226
0
    }
1227
1228
6.95k
    if (info->path.len)
1229
5.70k
      p15card->file_app->path = info->path;
1230
1231
6.95k
    if (info->ddo.value && info->ddo.len)
1232
1.28k
      parse_ddo(p15card, info->ddo.value, info->ddo.len);
1233
1234
79.7k
  } else if (aid) {
1235
369
    sc_log(ctx, "Application '%s' not found", sc_dump_hex(aid->value, aid->len));
1236
369
    err = SC_ERROR_INVALID_ARGUMENTS;
1237
369
    goto end;
1238
369
  }
1239
86.3k
  sc_log(ctx, "application path '%s'", sc_print_path(&p15card->file_app->path));
1240
1241
  /* Check if pkcs15 directory exists */
1242
86.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
86.3k
  if ((err != SC_SUCCESS) && (card->app_count < 1)) {
1248
66.3k
    sc_format_path("3F00", &p15card->file_app->path);
1249
66.3k
    err = SC_SUCCESS;
1250
66.3k
  }
1251
1252
86.3k
  if (err < 0) {
1253
4.44k
    sc_log(ctx, "Cannot select application path");
1254
4.44k
    goto end;
1255
4.44k
  }
1256
1257
81.8k
  err = sc_pkcs15_bind_internal_tokeninfo(p15card);
1258
81.8k
  if (err < 0) {
1259
74.9k
    sc_log(ctx, "Cannot bind tokeninfo");
1260
74.9k
    goto end;
1261
74.9k
  }
1262
1263
6.98k
  err = sc_pkcs15_bind_internal_odf(p15card);
1264
6.98k
  if (err < 0) {
1265
3.02k
    sc_log(ctx, "Cannot bind ODF");
1266
3.02k
    goto end;
1267
3.02k
  }
1268
1269
3.96k
  ok = 1;
1270
86.7k
end:
1271
86.7k
  if (!ok) {
1272
82.7k
    sc_pkcs15_card_clear(p15card);
1273
82.7k
    if (err == SC_ERROR_FILE_NOT_FOUND)
1274
10.9k
      err = SC_ERROR_WRONG_CARD;
1275
82.7k
    LOG_FUNC_RETURN(ctx, err);
1276
82.7k
  }
1277
1278
3.96k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1279
3.96k
}
1280
1281
const char *pkcs15_get_default_use_file_cache(struct sc_card *card)
1282
85.6k
{
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
85.6k
  const char *card_drivers_with_file_cache[] = {
1289
85.6k
      "atrust-acos",
1290
85.6k
      "belpic",
1291
85.6k
      "cac1",
1292
85.6k
      "cac",
1293
85.6k
      "cedulauy",
1294
85.6k
      "coolkey",
1295
85.6k
      "edo",
1296
85.6k
      "esteid2018",
1297
85.6k
      "esteid2025",
1298
85.6k
      "flex",
1299
85.6k
      "cyberflex",
1300
85.6k
      "gemsafeV1",
1301
85.6k
      "idprime",
1302
85.6k
      "itacns",
1303
85.6k
      "jpki",
1304
85.6k
      "MaskTech",
1305
85.6k
      "mcrd",
1306
85.6k
      "myeid",
1307
85.6k
      "npa",
1308
85.6k
      "nqapplet",
1309
85.6k
      "tcos",
1310
85.6k
      "dtrust",
1311
85.6k
      "lteid",
1312
85.6k
  };
1313
1314
85.6k
  if (NULL == card || NULL == card->driver || NULL == card->driver->short_name)
1315
0
    return "no";
1316
1.65M
  for (size_t i = 0; i < (sizeof card_drivers_with_file_cache / sizeof *card_drivers_with_file_cache); i++) {
1317
1.60M
    if (0 == strcmp(card->driver->short_name, card_drivers_with_file_cache[i]))
1318
32.2k
      return "public";
1319
1.60M
  }
1320
1321
53.4k
  return "no";
1322
85.6k
}
1323
1324
int
1325
sc_pkcs15_bind(struct sc_card *card, struct sc_aid *aid,
1326
    struct sc_pkcs15_card **p15card_out)
1327
85.6k
{
1328
85.6k
  struct sc_pkcs15_card *p15card = NULL;
1329
85.6k
  struct sc_context *ctx;
1330
85.6k
  scconf_block *conf_block = NULL;
1331
85.6k
  int r, emu_first, enable_emu;
1332
85.6k
  const char *use_file_cache;
1333
85.6k
  const char *pin_protected_certificate, *private_certificate;
1334
1335
85.6k
  if (card == NULL || p15card_out == NULL) {
1336
0
    return SC_ERROR_INVALID_ARGUMENTS;
1337
0
  }
1338
85.6k
  ctx = card->ctx;
1339
1340
85.6k
  LOG_FUNC_CALLED(ctx);
1341
85.6k
  sc_log(ctx, "application(aid:'%s')", aid ? sc_dump_hex(aid->value, aid->len) : "empty");
1342
1343
85.6k
  p15card = sc_pkcs15_card_new();
1344
85.6k
  if (p15card == NULL)
1345
85.6k
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
1346
1347
85.6k
  p15card->card = card;
1348
85.6k
  p15card->opts.use_file_cache = SC_PKCS15_OPTS_CACHE_NO_FILES;
1349
85.6k
  use_file_cache = pkcs15_get_default_use_file_cache(card);
1350
85.6k
  p15card->opts.use_pin_cache = 1;
1351
85.6k
  p15card->opts.pin_cache_counter = 10;
1352
85.6k
  p15card->opts.pin_cache_ignore_user_consent = 0;
1353
85.6k
  pin_protected_certificate = "protect";
1354
85.6k
  private_certificate = "";
1355
1356
85.6k
  conf_block = sc_get_conf_block(ctx, "framework", "pkcs15", 1);
1357
85.6k
  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
85.6k
  if (0 == strcmp(use_file_cache, "yes")) {
1369
0
    p15card->opts.use_file_cache = SC_PKCS15_OPTS_CACHE_ALL_FILES;
1370
85.6k
  } else if (0 == strcmp(use_file_cache, "public")) {
1371
32.2k
    p15card->opts.use_file_cache = SC_PKCS15_OPTS_CACHE_PUBLIC_FILES;
1372
53.4k
  } else if (0 == strcmp(use_file_cache, "no")) {
1373
53.4k
    p15card->opts.use_file_cache = SC_PKCS15_OPTS_CACHE_NO_FILES;
1374
53.4k
  }
1375
1376
85.6k
  if (0 == strcmp(pin_protected_certificate, "protect")) {
1377
85.6k
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_PROTECT;
1378
85.6k
  } 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
85.6k
  if (0 == strcmp(private_certificate, "protect")) {
1385
0
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_PROTECT;
1386
85.6k
  } else if (0 == strcmp(private_certificate, "ignore")) {
1387
0
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_IGNORE;
1388
85.6k
  } else if (0 == strcmp(private_certificate, "declassify")) {
1389
0
    p15card->opts.pin_protected_certificate = SC_PKCS15_CARD_OPTS_PRIV_CERT_DECLASSIFY;
1390
0
  }
1391
85.6k
  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
85.6k
      p15card->opts.use_file_cache, p15card->opts.use_pin_cache, p15card->opts.pin_cache_counter,
1393
85.6k
      p15card->opts.pin_cache_ignore_user_consent, p15card->opts.pin_protected_certificate);
1394
1395
85.6k
  r = sc_lock(card);
1396
85.6k
  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
85.6k
  enable_emu = scconf_get_bool(conf_block, "enable_pkcs15_emulation", 1);
1403
85.6k
  if (enable_emu) {
1404
85.6k
    sc_log(ctx, "PKCS#15 emulation enabled");
1405
85.6k
    emu_first = scconf_get_bool(conf_block, "try_emulation_first", 0);
1406
85.6k
    if (emu_first || sc_pkcs15_is_emulation_only(card)) {
1407
15.3k
      r = sc_pkcs15_bind_synthetic(p15card, aid);
1408
15.3k
      if (r == SC_SUCCESS)
1409
5.88k
        goto done;
1410
9.49k
      r = sc_pkcs15_bind_internal(p15card, aid);
1411
9.49k
      if (r < 0)
1412
9.28k
        goto error;
1413
70.2k
    } else {
1414
70.2k
      r = sc_pkcs15_bind_internal(p15card, aid);
1415
70.2k
      if (r == SC_SUCCESS)
1416
3.55k
        goto done;
1417
66.7k
      r = sc_pkcs15_bind_synthetic(p15card, aid);
1418
66.7k
      if (r < 0)
1419
40.7k
        goto error;
1420
66.7k
    }
1421
85.6k
  }
1422
0
  else {
1423
0
    r = sc_pkcs15_bind_internal(p15card, aid);
1424
0
    if (r < 0)
1425
0
      goto error;
1426
0
  }
1427
35.6k
done:
1428
35.6k
  *p15card_out = p15card;
1429
35.6k
  sc_unlock(card);
1430
35.6k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1431
50.0k
error:
1432
50.0k
  sc_unlock(card);
1433
50.0k
  sc_pkcs15_card_free(p15card);
1434
50.0k
  LOG_FUNC_RETURN(ctx, r);
1435
50.0k
}
1436
1437
1438
int
1439
sc_pkcs15_unbind(struct sc_pkcs15_card *p15card)
1440
28.9k
{
1441
28.9k
  if (p15card == NULL || p15card->magic != SC_PKCS15_CARD_MAGIC) {
1442
8.35k
    return SC_ERROR_INVALID_ARGUMENTS;
1443
8.35k
  }
1444
1445
20.5k
  LOG_FUNC_CALLED(p15card->card->ctx);
1446
20.5k
  if (p15card->dll_handle)
1447
0
    sc_dlclose(p15card->dll_handle);
1448
20.5k
  sc_pkcs15_pincache_clear(p15card);
1449
20.5k
  sc_pkcs15_card_free(p15card);
1450
20.5k
  return 0;
1451
28.9k
}
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
1.81M
{
1459
1.81M
  struct sc_pkcs15_object *obj = NULL;
1460
1.81M
  struct sc_pkcs15_df *df = NULL;
1461
1.81M
  unsigned int  df_mask = 0;
1462
1.81M
  size_t    match_count = 0;
1463
1.81M
  int r;
1464
1465
1.81M
  if (type)
1466
1.01M
    class_mask |= SC_PKCS15_TYPE_TO_CLASS(type);
1467
1468
  /* Make sure the class mask we have makes sense */
1469
1.81M
  if (class_mask == 0
1470
1.81M
      || (class_mask & ~(
1471
1.81M
          SC_PKCS15_SEARCH_CLASS_PRKEY |
1472
1.81M
          SC_PKCS15_SEARCH_CLASS_PUBKEY |
1473
1.81M
          SC_PKCS15_SEARCH_CLASS_SKEY |
1474
1.81M
          SC_PKCS15_SEARCH_CLASS_CERT |
1475
1.81M
          SC_PKCS15_SEARCH_CLASS_DATA |
1476
1.81M
          SC_PKCS15_SEARCH_CLASS_AUTH))) {
1477
0
    LOG_FUNC_RETURN(p15card->card->ctx, SC_ERROR_INVALID_ARGUMENTS);
1478
0
  }
1479
1480
1.81M
  if (class_mask & SC_PKCS15_SEARCH_CLASS_PRKEY)
1481
843k
    df_mask |= (1 << SC_PKCS15_PRKDF);
1482
1.81M
  if (class_mask & SC_PKCS15_SEARCH_CLASS_PUBKEY)
1483
826k
    df_mask |= (1 << SC_PKCS15_PUKDF) | (1 << SC_PKCS15_PUKDF_TRUSTED);
1484
1.81M
  if (class_mask & SC_PKCS15_SEARCH_CLASS_CERT)
1485
809k
    df_mask |= (1 << SC_PKCS15_CDF) | (1 << SC_PKCS15_CDF_TRUSTED) | (1 << SC_PKCS15_CDF_USEFUL);
1486
1.81M
  if (class_mask & SC_PKCS15_SEARCH_CLASS_DATA)
1487
18.9k
    df_mask |= (1 << SC_PKCS15_DODF);
1488
1.81M
  if (class_mask & SC_PKCS15_SEARCH_CLASS_AUTH)
1489
60.8k
    df_mask |= (1 << SC_PKCS15_AODF);
1490
1.81M
  if (class_mask & SC_PKCS15_SEARCH_CLASS_SKEY)
1491
50.1k
    df_mask |= (1 << SC_PKCS15_SKDF);
1492
1493
  /* Make sure all the DFs we want to search have been
1494
   * enumerated. */
1495
3.41M
  for (df = p15card->df_list; df != NULL; df = df->next) {
1496
1.60M
    if (!(df_mask & (1 << df->type)))   {
1497
1.14M
      continue;
1498
1.14M
    }
1499
456k
    if (df->enumerated)
1500
224k
      continue;
1501
    /* Enumerate the DF's, so p15card->obj_list is populated. */
1502
231k
    if (p15card->ops.parse_df)
1503
5
      r = p15card->ops.parse_df(p15card, df);
1504
231k
    else
1505
231k
      r = sc_pkcs15_parse_df(p15card, df);
1506
231k
    if (r != SC_SUCCESS)
1507
231k
      continue;
1508
231k
  }
1509
1510
  /* And now loop over all objects */
1511
3.53M
  for (obj = p15card->obj_list; obj != NULL; obj = obj->next) {
1512
    /* Check object type */
1513
1.72M
    if (!(class_mask & SC_PKCS15_TYPE_TO_CLASS(obj->type)))
1514
1.40M
      continue;
1515
317k
    if (type != 0
1516
231k
     && obj->type != type
1517
123k
     && (obj->type & SC_PKCS15_TYPE_CLASS_MASK) != type)
1518
17.1k
      continue;
1519
1520
    /* Potential candidate, apply search function */
1521
300k
    if (func != NULL && func(obj, func_arg) <= 0)
1522
181k
      continue;
1523
    /* Okay, we have a match. */
1524
118k
    match_count++;
1525
118k
    if (!ret || ret_size <= 0)
1526
1.13k
      continue;
1527
117k
    ret[match_count-1] = obj;
1528
117k
    if (ret_size <= match_count)
1529
5.54k
      break;
1530
117k
  }
1531
1532
1.81M
  return (int)match_count;
1533
1.81M
}
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
169k
{
1540
169k
  return sc_pkcs15_get_objects_cond(p15card, type, NULL, NULL, ret, ret_size);
1541
169k
}
1542
1543
1544
static int
1545
compare_obj_id(struct sc_pkcs15_object *obj, const struct sc_pkcs15_id *id)
1546
185k
{
1547
185k
  void *data = obj->data;
1548
1549
185k
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1550
90.0k
  case SC_PKCS15_TYPE_CERT:
1551
90.0k
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_cert_info *) data)->id, id);
1552
88.9k
  case SC_PKCS15_TYPE_PRKEY:
1553
88.9k
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_prkey_info *) data)->id, id);
1554
1.02k
  case SC_PKCS15_TYPE_PUBKEY:
1555
1.02k
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_pubkey_info *) data)->id, id);
1556
4
  case SC_PKCS15_TYPE_SKEY:
1557
4
    return sc_pkcs15_compare_id(&((struct sc_pkcs15_skey_info *) data)->id, id);
1558
5.78k
  case SC_PKCS15_TYPE_AUTH:
1559
5.78k
    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
185k
  }
1563
0
  return 0;
1564
185k
}
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
1.04k
{
1579
1.04k
  void    *data = obj->data;
1580
1.04k
  unsigned int  usage;
1581
1582
1.04k
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1583
1.04k
  case SC_PKCS15_TYPE_PRKEY:
1584
1.04k
    usage = ((struct sc_pkcs15_prkey_info *) data)->usage;
1585
1.04k
    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
1.04k
  }
1592
1.04k
  return (usage & mask & value) != 0;
1593
1.04k
}
1594
1595
1596
static int
1597
compare_obj_flags(struct sc_pkcs15_object *obj, unsigned int mask, unsigned int value)
1598
1.02k
{
1599
1.02k
  struct sc_pkcs15_auth_info *auth_info;
1600
1.02k
  unsigned int  flags;
1601
1602
1.02k
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1603
1.02k
  case SC_PKCS15_TYPE_AUTH:
1604
1.02k
    auth_info = (struct sc_pkcs15_auth_info *) obj->data;
1605
1.02k
    if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1606
0
      return 0;
1607
1.02k
    flags = auth_info->attrs.pin.flags;
1608
1.02k
    break;
1609
0
  default:
1610
0
    return 0;
1611
1.02k
  }
1612
1.02k
  return !((flags ^ value) & mask);
1613
1.02k
}
1614
1615
1616
static int
1617
compare_obj_reference(struct sc_pkcs15_object *obj, int value)
1618
84
{
1619
84
  struct sc_pkcs15_auth_info *auth_info;
1620
84
  void    *data = obj->data;
1621
84
  int   reference;
1622
1623
84
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1624
15
  case SC_PKCS15_TYPE_AUTH:
1625
15
    auth_info = (struct sc_pkcs15_auth_info *) obj->data;
1626
15
    if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1627
0
      return 0;
1628
15
    reference = auth_info->attrs.pin.reference;
1629
15
    break;
1630
69
  case SC_PKCS15_TYPE_PRKEY:
1631
69
    reference = ((struct sc_pkcs15_prkey_info *) data)->key_reference;
1632
69
    break;
1633
0
  default:
1634
0
    return 0;
1635
84
  }
1636
84
  return reference == value;
1637
84
}
1638
1639
1640
static int
1641
compare_obj_path(struct sc_pkcs15_object *obj, const struct sc_path *path)
1642
2
{
1643
2
  void *data = obj->data;
1644
1645
2
  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
2
  case SC_PKCS15_TYPE_AUTH:
1655
2
    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
2
  }
1659
0
  return 0;
1660
2
}
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
188k
{
1679
188k
  struct sc_pkcs15_search_key *sk = (struct sc_pkcs15_search_key *) arg;
1680
1681
188k
  if (sk->id && !compare_obj_id(obj, sk->id))
1682
180k
    return 0;
1683
7.72k
  if (sk->app_oid && !sc_obj_app_oid(obj, sk->app_oid))
1684
0
    return 0;
1685
7.72k
  if (sk->usage_mask && !compare_obj_usage(obj, sk->usage_mask, sk->usage_value))
1686
206
    return 0;
1687
7.52k
  if (sk->flags_mask && !compare_obj_flags(obj, sk->flags_mask, sk->flags_value))
1688
936
    return 0;
1689
6.58k
  if (sk->match_reference && !compare_obj_reference(obj, sk->reference))
1690
82
    return 0;
1691
6.50k
  if (sk->path && !compare_obj_path(obj, sk->path))
1692
0
    return 0;
1693
6.50k
  if (sk->app_label) {
1694
0
    if (sk->label && !compare_obj_data_name(obj, sk->app_label, sk->label))
1695
0
      return 0;
1696
6.50k
  } else {
1697
6.50k
    if (sk->label && strncmp(sk->label, obj->label, sizeof(obj->label)) != 0)
1698
0
      return 0;
1699
6.50k
  }
1700
1701
6.50k
  return 1;
1702
6.50k
}
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
9.24k
{
1709
9.24k
  int r;
1710
1711
9.24k
  r = sc_pkcs15_get_objects_cond(p15card, type, compare_obj_key, sk, out, 1);
1712
9.24k
  if (r < 0)
1713
0
    return r;
1714
9.24k
  if (r == 0)
1715
8.31k
    return SC_ERROR_OBJECT_NOT_FOUND;
1716
924
  return 0;
1717
9.24k
}
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
795k
{
1724
795k
  return __sc_pkcs15_search_objects(p15card,
1725
795k
      sk->class_mask, sk->type,
1726
795k
      compare_obj_key, sk,
1727
795k
      ret, ret_size);
1728
795k
}
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
178k
{
1736
178k
  return __sc_pkcs15_search_objects(p15card, 0, type,
1737
178k
      func, func_arg, ret, ret_size);
1738
178k
}
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
840k
{
1745
840k
  struct sc_pkcs15_search_key sk;
1746
840k
  int r;
1747
1748
840k
  memset(&sk, 0, sizeof(sk));
1749
840k
  sk.id = id;
1750
1751
840k
  r = __sc_pkcs15_search_objects(p15card, 0, type, compare_obj_key, &sk, out, 1);
1752
840k
  if (r < 0)
1753
0
    return r;
1754
840k
  if (r == 0)
1755
835k
    return SC_ERROR_OBJECT_NOT_FOUND;
1756
5.03k
  return 0;
1757
840k
}
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
6.05k
{
1764
6.05k
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_CERT, id, out);
1765
6.05k
}
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
1.92k
{
1772
1.92k
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_PRKEY, id, out);
1773
1.92k
}
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
110
{
1780
110
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_PUBKEY, id, out);
1781
110
}
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
14.2k
{
1788
14.2k
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_SKEY, id, out);
1789
14.2k
}
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
7.96k
{
1796
7.96k
  return sc_pkcs15_find_object_by_id(p15card, SC_PKCS15_TYPE_AUTH, id, out);
1797
7.96k
}
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
2.17k
{
1804
2.17k
  struct sc_pkcs15_search_key sk;
1805
1806
2.17k
  memset(&sk, 0, sizeof(sk));
1807
2.17k
  sk.match_reference = 1;
1808
2.17k
  sk.reference = reference;
1809
2.17k
  sk.path = path;
1810
1811
2.17k
  return find_by_key(p15card, SC_PKCS15_TYPE_AUTH_PIN, &sk, out);
1812
2.17k
}
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
1.79k
{
1820
1.79k
  struct sc_context *ctx = p15card->card->ctx;
1821
1.79k
  struct sc_pkcs15_object *auth_objs[0x10];
1822
1.79k
  size_t nn_objs, ii;
1823
1.79k
  int r;
1824
1825
  /* Get all existing pkcs15 AUTH objects */
1826
1.79k
  r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, auth_objs, 0x10);
1827
1.79k
  LOG_TEST_RET(ctx, r, "Get PKCS#15 AUTH objects error");
1828
1.79k
  nn_objs = r;
1829
1830
3.27k
  for (ii=0; ii<nn_objs; ii++)   {
1831
1.64k
    struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)auth_objs[ii]->data;
1832
1833
1.64k
    if (auth_info->auth_method != auth_method)
1834
494
      continue;
1835
1.14k
    if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_PIN)
1836
1.14k
      if (auth_info->attrs.pin.reference != reference)
1837
699
        continue;
1838
1839
447
    if (path && !sc_compare_path(&auth_info->path, path))
1840
287
      continue;
1841
1842
160
    if (out)
1843
160
      *out = auth_objs[ii];
1844
1845
160
    return SC_SUCCESS;
1846
447
  }
1847
1848
1.63k
  return SC_ERROR_OBJECT_NOT_FOUND;
1849
1.79k
}
1850
1851
1852
int
1853
sc_pkcs15_find_so_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object **out)
1854
816
{
1855
816
  struct sc_pkcs15_search_key sk;
1856
1857
816
  memset(&sk, 0, sizeof(sk));
1858
816
  sk.flags_mask = sk.flags_value = SC_PKCS15_PIN_FLAG_SO_PIN;
1859
1860
816
  return find_by_key(p15card, SC_PKCS15_TYPE_AUTH_PIN, &sk, out);
1861
816
}
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
9.46k
{
1869
9.46k
  struct sc_context *ctx = p15card->card->ctx;
1870
9.46k
  struct sc_pkcs15_object *auths[SC_PKCS15_MAX_PINS];
1871
9.46k
  int r, i, num, idx = 0;
1872
1873
9.46k
  LOG_FUNC_CALLED(ctx);
1874
9.46k
  sc_log(ctx, "Find PIN flags:0x%X, mask:0x%X, index:%i", flags, mask, index ? *index : -1);
1875
9.46k
  if (index)
1876
0
    idx = *index;
1877
  /* Get authentication PKCS#15 objects that are present in the given application */
1878
9.46k
  r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, auths, SC_PKCS15_MAX_PINS);
1879
9.46k
  if (r < 0)
1880
0
    return r;
1881
9.46k
  num = r;
1882
1883
11.9k
  for (i=idx; i<num; i++)   {
1884
6.38k
    struct sc_pkcs15_auth_info *pin_info = (struct sc_pkcs15_auth_info *)(*(auths + i))->data;
1885
1886
6.38k
    if (!pin_info || pin_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1887
0
      continue;
1888
1889
6.38k
    if ((pin_info->attrs.pin.flags & mask) != flags)
1890
2.53k
      continue;
1891
1892
3.84k
    if (out)
1893
3.84k
      *out = *(auths + i);
1894
3.84k
    if (index)
1895
0
      *index = i;
1896
1897
3.84k
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1898
3.84k
  }
1899
1900
5.61k
  LOG_FUNC_RETURN(ctx, SC_ERROR_OBJECT_NOT_FOUND);
1901
5.61k
}
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
4.39k
{
1959
4.39k
  struct sc_pkcs15_search_key sk;
1960
1961
4.39k
  memset(&sk, 0, sizeof(sk));
1962
4.39k
  sk.usage_mask = sk.usage_value = usage;
1963
4.39k
  sk.id = id;
1964
1965
4.39k
  return find_by_key(p15card, SC_PKCS15_TYPE_PRKEY, &sk, out);
1966
4.39k
}
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
1.85k
{
1974
1.85k
  struct sc_pkcs15_search_key sk;
1975
1976
1.85k
  memset(&sk, 0, sizeof(sk));
1977
1.85k
  sk.match_reference = 1;
1978
1.85k
  sk.reference = reference;
1979
1.85k
  sk.path = path;
1980
1981
1.85k
  return find_by_key(p15card, SC_PKCS15_TYPE_PRKEY, &sk, out);
1982
1.85k
}
1983
1984
1985
int
1986
sc_pkcs15_add_object(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *obj)
1987
225k
{
1988
225k
  struct sc_pkcs15_object *p = p15card->obj_list;
1989
1990
225k
  if (!obj)
1991
0
    return 0;
1992
225k
  obj->next = obj->prev = NULL;
1993
225k
  if (p15card->obj_list == NULL) {
1994
29.8k
    p15card->obj_list = obj;
1995
29.8k
    return 0;
1996
29.8k
  }
1997
4.31M
  while (p->next != NULL)
1998
4.12M
    p = p->next;
1999
195k
  p->next = obj;
2000
195k
  obj->prev = p;
2001
2002
195k
  return 0;
2003
225k
}
2004
2005
2006
void
2007
sc_pkcs15_remove_object(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *obj)
2008
1.87k
{
2009
1.87k
  if (!obj)
2010
0
    return;
2011
1.87k
  else if (obj->prev == NULL)
2012
1.66k
    p15card->obj_list = obj->next;
2013
216
  else
2014
216
    obj->prev->next = obj->next;
2015
1.87k
  if (obj->next != NULL)
2016
57
    obj->next->prev = obj->prev;
2017
1.87k
}
2018
2019
2020
static void
2021
sc_pkcs15_remove_objects(struct sc_pkcs15_card *p15card)
2022
205k
{
2023
205k
  struct sc_pkcs15_object *cur = NULL, *next = NULL;
2024
2025
205k
  if (!p15card || !p15card->obj_list)
2026
177k
    return;
2027
251k
  for (cur = p15card->obj_list; cur; cur = next)   {
2028
223k
    next = cur->next;
2029
223k
    sc_pkcs15_free_object(cur);
2030
223k
  }
2031
2032
28.1k
  p15card->obj_list = NULL;
2033
28.1k
}
2034
2035
2036
void
2037
sc_pkcs15_free_object(struct sc_pkcs15_object *obj)
2038
296k
{
2039
296k
  if (!obj)
2040
16.0k
    return;
2041
280k
  switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
2042
21.8k
  case SC_PKCS15_TYPE_PRKEY:
2043
21.8k
    sc_pkcs15_free_prkey_info((sc_pkcs15_prkey_info_t *)obj->data);
2044
21.8k
    break;
2045
5.77k
  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
5.77k
    if (obj->emulated) {
2049
4.88k
      sc_pkcs15_free_pubkey(obj->emulated);
2050
4.88k
    }
2051
5.77k
    sc_pkcs15_free_pubkey_info((sc_pkcs15_pubkey_info_t *)obj->data);
2052
5.77k
    break;
2053
11.1k
  case SC_PKCS15_TYPE_CERT:
2054
11.1k
    sc_pkcs15_free_cert_info((sc_pkcs15_cert_info_t *)obj->data);
2055
11.1k
    break;
2056
14.3k
  case SC_PKCS15_TYPE_SKEY:
2057
14.3k
    sc_pkcs15_free_skey_info((sc_pkcs15_skey_info_t *)obj->data);
2058
14.3k
    break;
2059
158k
  case SC_PKCS15_TYPE_DATA_OBJECT:
2060
158k
    sc_pkcs15_free_data_info((sc_pkcs15_data_info_t *)obj->data);
2061
158k
    break;
2062
42.2k
  case SC_PKCS15_TYPE_AUTH:
2063
42.2k
    sc_pkcs15_free_auth_info((sc_pkcs15_auth_info_t *)obj->data);
2064
42.2k
    break;
2065
26.1k
  default:
2066
26.1k
    free(obj->data);
2067
280k
  }
2068
2069
280k
  sc_pkcs15_free_object_content(obj);
2070
2071
280k
  free(obj);
2072
280k
}
2073
2074
2075
int
2076
sc_pkcs15_add_df(struct sc_pkcs15_card *p15card, unsigned int type, const sc_path_t *path)
2077
64.9k
{
2078
64.9k
  struct sc_pkcs15_df *p, *newdf;
2079
2080
64.9k
  newdf = calloc(1, sizeof(struct sc_pkcs15_df));
2081
64.9k
  if (newdf == NULL)
2082
0
    return SC_ERROR_OUT_OF_MEMORY;
2083
64.9k
  newdf->path = *path;
2084
64.9k
  newdf->type = type;
2085
2086
64.9k
  if (p15card->df_list == NULL) {
2087
29.8k
    p15card->df_list = newdf;
2088
29.8k
    return 0;
2089
29.8k
  }
2090
2091
35.0k
  p = p15card->df_list;
2092
185k
  while (p->next != NULL)
2093
150k
    p = p->next;
2094
35.0k
  p->next = newdf;
2095
35.0k
  newdf->prev = p;
2096
2097
35.0k
  return 0;
2098
64.9k
}
2099
2100
2101
static void
2102
sc_pkcs15_remove_dfs(struct sc_pkcs15_card *p15card)
2103
205k
{
2104
205k
  struct sc_pkcs15_df *cur = NULL, *next = NULL;
2105
2106
205k
  if (!p15card || !p15card->df_list)
2107
175k
    return;
2108
2109
94.7k
  for (cur = p15card->df_list; cur; cur = next)   {
2110
64.9k
    next = cur->next;
2111
64.9k
    free(cur);
2112
64.9k
  }
2113
2114
29.8k
  p15card->df_list = NULL;
2115
29.8k
}
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
1.47k
{
2122
1.47k
  unsigned char *buf = NULL, *tmp = NULL, *p;
2123
1.47k
  size_t bufsize = 0, tmpsize;
2124
1.47k
  const struct sc_pkcs15_object *obj;
2125
1.47k
  int (* func)(struct sc_context *, const struct sc_pkcs15_object *nobj,
2126
1.47k
         unsigned char **nbuf, size_t *nbufsize) = NULL;
2127
1.47k
  int r;
2128
2129
1.47k
  if (p15card == NULL || p15card->magic != SC_PKCS15_CARD_MAGIC) {
2130
0
    return SC_ERROR_INVALID_ARGUMENTS;
2131
0
  }
2132
1.47k
  switch (df->type) {
2133
325
  case SC_PKCS15_PRKDF:
2134
325
    func = sc_pkcs15_encode_prkdf_entry;
2135
325
    break;
2136
202
  case SC_PKCS15_PUKDF:
2137
202
  case SC_PKCS15_PUKDF_TRUSTED:
2138
202
    func = sc_pkcs15_encode_pukdf_entry;
2139
202
    break;
2140
270
  case SC_PKCS15_SKDF:
2141
270
    func = sc_pkcs15_encode_skdf_entry;
2142
270
    break;
2143
162
  case SC_PKCS15_CDF:
2144
305
  case SC_PKCS15_CDF_TRUSTED:
2145
305
  case SC_PKCS15_CDF_USEFUL:
2146
305
    func = sc_pkcs15_encode_cdf_entry;
2147
305
    break;
2148
141
  case SC_PKCS15_DODF:
2149
141
    func = sc_pkcs15_encode_dodf_entry;
2150
141
    break;
2151
227
  case SC_PKCS15_AODF:
2152
227
    func = sc_pkcs15_encode_aodf_entry;
2153
227
    break;
2154
1.47k
  }
2155
1.47k
  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
3.69k
  for (obj = p15card->obj_list; obj != NULL; obj = obj->next) {
2162
2.23k
    if (obj->df != df)
2163
1.77k
      continue;
2164
462
    r = func(ctx, obj, &tmp, &tmpsize);
2165
462
    if (r) {
2166
10
      free(tmp);
2167
10
      free(buf);
2168
10
      return r;
2169
10
    }
2170
452
    if (!tmpsize)
2171
0
      continue;
2172
452
    p = (u8 *) realloc(buf, bufsize + tmpsize);
2173
452
    if (!p) {
2174
0
      free(tmp);
2175
0
      free(buf);
2176
0
      return SC_ERROR_OUT_OF_MEMORY;
2177
0
    }
2178
452
    buf = p;
2179
452
    memcpy(buf + bufsize, tmp, tmpsize);
2180
452
    free(tmp);
2181
452
    tmp = NULL;
2182
452
    bufsize += tmpsize;
2183
452
  }
2184
1.46k
  *buf_out = buf;
2185
1.46k
  *bufsize_out = bufsize;
2186
2187
1.46k
  return 0;
2188
1.47k
}
2189
2190
2191
int
2192
sc_pkcs15_parse_df(struct sc_pkcs15_card *p15card, struct sc_pkcs15_df *df)
2193
237k
{
2194
237k
  struct sc_context *ctx = p15card->card->ctx;
2195
237k
  unsigned char *buf;
2196
237k
  const unsigned char *p;
2197
237k
  size_t bufsize;
2198
237k
  int r;
2199
237k
  struct sc_pkcs15_object *obj = NULL;
2200
237k
  int (* func)(struct sc_pkcs15_card *, struct sc_pkcs15_object *,
2201
237k
         const u8 **nbuf, size_t *nbufsize) = NULL;
2202
2203
237k
  sc_log(ctx, "called; path=%s, type=%d, enum=%d", sc_print_path(&df->path), df->type, df->enumerated);
2204
2205
237k
  if (df->enumerated)
2206
237k
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
2207
2208
237k
  switch (df->type) {
2209
98.9k
  case SC_PKCS15_PRKDF:
2210
98.9k
    func = sc_pkcs15_decode_prkdf_entry;
2211
98.9k
    break;
2212
40.1k
  case SC_PKCS15_PUKDF:
2213
40.1k
    func = sc_pkcs15_decode_pukdf_entry;
2214
40.1k
    break;
2215
1.99k
  case SC_PKCS15_SKDF:
2216
1.99k
    func = sc_pkcs15_decode_skdf_entry;
2217
1.99k
    break;
2218
49.5k
  case SC_PKCS15_CDF:
2219
72.3k
  case SC_PKCS15_CDF_TRUSTED:
2220
91.1k
  case SC_PKCS15_CDF_USEFUL:
2221
91.1k
    func = sc_pkcs15_decode_cdf_entry;
2222
91.1k
    break;
2223
525
  case SC_PKCS15_DODF:
2224
525
    func = sc_pkcs15_decode_dodf_entry;
2225
525
    break;
2226
3.59k
  case SC_PKCS15_AODF:
2227
3.59k
    func = sc_pkcs15_decode_aodf_entry;
2228
3.59k
    break;
2229
237k
  }
2230
237k
  if (func == NULL) {
2231
1.16k
    sc_log(ctx, "unknown DF type: %d", df->type);
2232
1.16k
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
2233
1.16k
  }
2234
236k
  r = sc_pkcs15_read_file(p15card, &df->path, &buf, &bufsize, 0);
2235
236k
  LOG_TEST_RET(ctx, r, "pkcs15 read file failed");
2236
2237
2.39k
  p = buf;
2238
2.85k
  while (bufsize && *p != 0x00) {
2239
2240
2.10k
    obj = calloc(1, sizeof(struct sc_pkcs15_object));
2241
2.10k
    if (obj == NULL) {
2242
0
      r = SC_ERROR_OUT_OF_MEMORY;
2243
0
      goto ret;
2244
0
    }
2245
2.10k
    r = func(p15card, obj, &p, &bufsize);
2246
2.10k
    if (r) {
2247
1.64k
      free(obj);
2248
1.64k
      if (r == SC_ERROR_ASN1_END_OF_CONTENTS) {
2249
166
        r = 0;
2250
166
        break;
2251
166
      }
2252
1.48k
      sc_log(ctx, "%s: Error decoding DF entry", sc_strerror(r));
2253
1.48k
      goto ret;
2254
1.64k
    }
2255
2256
460
    obj->df = df;
2257
460
    r = sc_pkcs15_add_object(p15card, obj);
2258
460
    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
1.29k
    while (bufsize > 0 && *p == 00) {
2266
838
      bufsize--;
2267
838
      p++;
2268
838
    }
2269
912
  };
2270
2271
912
  if (r > 0)
2272
0
    r = 0;
2273
2.39k
ret:
2274
2.39k
  df->enumerated = 1;
2275
2.39k
  free(buf);
2276
2.39k
  LOG_FUNC_RETURN(ctx, r);
2277
2.39k
}
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
4.45k
{
2284
4.45k
  struct sc_context *ctx = p15card->card->ctx;
2285
4.45k
  struct sc_pkcs15_unusedspace *p = p15card->unusedspace_list, *new_unusedspace;
2286
2287
4.45k
  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
4.45k
  new_unusedspace = calloc(1, sizeof(sc_pkcs15_unusedspace_t));
2299
4.45k
  if (new_unusedspace == NULL)
2300
0
    return SC_ERROR_OUT_OF_MEMORY;
2301
4.45k
  new_unusedspace->path = *path;
2302
4.45k
  if (auth_id != NULL)
2303
9
    new_unusedspace->auth_id = *auth_id;
2304
2305
4.45k
  if (p15card->unusedspace_list == NULL) {
2306
4.45k
    p15card->unusedspace_list = new_unusedspace;
2307
4.45k
    return 0;
2308
4.45k
  }
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
4.45k
}
2316
2317
2318
void
2319
sc_pkcs15_remove_unusedspace(struct sc_pkcs15_card *p15card, struct sc_pkcs15_unusedspace *unusedspace)
2320
4.44k
{
2321
4.44k
  if (!unusedspace)
2322
0
    return;
2323
2324
4.44k
  if (!unusedspace->prev)
2325
4.44k
    p15card->unusedspace_list = unusedspace->next;
2326
0
  else
2327
0
    unusedspace->prev->next = unusedspace->next;
2328
2329
4.44k
  if (unusedspace->next)
2330
0
    unusedspace->next->prev = unusedspace->prev;
2331
2332
4.44k
  free(unusedspace);
2333
4.44k
}
2334
2335
2336
static void
2337
sc_pkcs15_free_unusedspace(struct sc_pkcs15_card *p15card)
2338
109k
{
2339
109k
  struct sc_pkcs15_unusedspace *cur = NULL, *next = NULL;
2340
2341
109k
  if (!p15card || !p15card->unusedspace_list)
2342
109k
    return;
2343
18
  for (cur = p15card->unusedspace_list; cur; cur = next)   {
2344
9
    next = cur->next;
2345
9
    free(cur);
2346
9
  }
2347
2348
9
  p15card->unusedspace_list = NULL;
2349
9
}
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
4.44k
{
2356
4.44k
  struct sc_path dummy_path;
2357
4.44k
  static const struct sc_asn1_entry c_asn1_unusedspace[] = {
2358
4.44k
    { "UnusedSpace", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
2359
4.44k
    { NULL, 0, 0, 0, NULL, NULL }
2360
4.44k
  };
2361
4.44k
  static const struct sc_asn1_entry c_asn1_unusedspace_values[] = {
2362
4.44k
    { "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
2363
4.44k
    { "authId", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_OPTIONAL, NULL, NULL },
2364
4.44k
    { NULL, 0, 0, 0, NULL, NULL }
2365
4.44k
  };
2366
4.44k
  struct sc_asn1_entry *asn1_unusedspace = NULL;
2367
4.44k
  struct sc_asn1_entry *asn1_values = NULL;
2368
4.44k
  int unusedspace_count = 0, r, c = 0;
2369
4.44k
  struct sc_pkcs15_unusedspace *unusedspace = NULL;
2370
2371
4.44k
  sc_format_path("3F00", &dummy_path);
2372
4.44k
  dummy_path.index = dummy_path.count = 0;
2373
2374
4.44k
  unusedspace = p15card->unusedspace_list;
2375
4.44k
  for ( ; unusedspace != NULL; unusedspace = unusedspace->next)
2376
0
    unusedspace_count++;
2377
4.44k
  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
4.44k
    r = sc_pkcs15_add_unusedspace(p15card, &dummy_path, NULL);
2381
4.44k
    if (r)
2382
0
      return r;
2383
4.44k
    unusedspace_count = 1;
2384
4.44k
  }
2385
2386
4.44k
  asn1_unusedspace = (struct sc_asn1_entry *)
2387
4.44k
    malloc(sizeof(struct sc_asn1_entry) * (unusedspace_count + 1));
2388
4.44k
  if (asn1_unusedspace == NULL) {
2389
0
    r = SC_ERROR_OUT_OF_MEMORY;
2390
0
    goto err;
2391
0
  }
2392
4.44k
  asn1_values = (struct sc_asn1_entry *)
2393
4.44k
    malloc(sizeof(struct sc_asn1_entry) * (unusedspace_count * 3));
2394
4.44k
  if (asn1_values == NULL) {
2395
0
    r = SC_ERROR_OUT_OF_MEMORY;
2396
0
    goto err;
2397
0
  }
2398
2399
8.89k
  for (unusedspace = p15card->unusedspace_list; unusedspace != NULL; unusedspace = unusedspace->next) {
2400
4.44k
    sc_copy_asn1_entry(c_asn1_unusedspace, asn1_unusedspace + c);
2401
4.44k
    sc_format_asn1_entry(asn1_unusedspace + c, asn1_values + 3*c, NULL, 1);
2402
4.44k
    sc_copy_asn1_entry(c_asn1_unusedspace_values, asn1_values + 3*c);
2403
4.44k
    sc_format_asn1_entry(asn1_values + 3*c, &unusedspace->path, NULL, 1);
2404
4.44k
    sc_format_asn1_entry(asn1_values + 3*c+1, &unusedspace->auth_id, NULL,
2405
4.44k
         unusedspace->auth_id.len > 0 ? 1 : 0);
2406
4.44k
    c++;
2407
4.44k
  }
2408
4.44k
  asn1_unusedspace[c].name = NULL;
2409
2410
4.44k
  r = sc_asn1_encode(ctx, asn1_unusedspace, buf, buflen);
2411
2412
4.44k
err:
2413
4.44k
  if (asn1_values != NULL)
2414
4.44k
    free(asn1_values);
2415
4.44k
  if (asn1_unusedspace != NULL)
2416
4.44k
    free(asn1_unusedspace);
2417
2418
  /* If we added the dummy entry, remove it now */
2419
4.44k
  if (unusedspace_count == 1 && sc_compare_path(&p15card->unusedspace_list->path, &dummy_path))
2420
4.44k
    sc_pkcs15_remove_unusedspace(p15card, p15card->unusedspace_list);
2421
2422
4.44k
  return r;
2423
4.44k
}
2424
2425
2426
int
2427
sc_pkcs15_parse_unusedspace(const unsigned char *buf, size_t buflen, struct sc_pkcs15_card *p15card)
2428
4.36k
{
2429
4.36k
  const unsigned char *p = buf;
2430
4.36k
  size_t left = buflen;
2431
4.36k
  int r;
2432
4.36k
  struct sc_path path;
2433
4.36k
  struct sc_pkcs15_id auth_id;
2434
4.36k
  struct sc_asn1_entry asn1_unusedspace[] = {
2435
4.36k
    { "UnusedSpace", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
2436
4.36k
    { NULL, 0, 0, 0, NULL, NULL }
2437
4.36k
  };
2438
4.36k
  struct sc_asn1_entry asn1_unusedspace_values[] = {
2439
4.36k
    { "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
2440
4.36k
    { "authId", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_OPTIONAL, NULL, NULL },
2441
4.36k
    { NULL, 0, 0, 0, NULL, NULL }
2442
4.36k
  };
2443
2444
  /* Clean the list if already present */
2445
4.36k
  sc_pkcs15_free_unusedspace(p15card);
2446
2447
4.36k
  sc_format_asn1_entry(asn1_unusedspace, asn1_unusedspace_values, NULL, 1);
2448
4.36k
  sc_format_asn1_entry(asn1_unusedspace_values, &path, NULL, 1);
2449
4.36k
  sc_format_asn1_entry(asn1_unusedspace_values+1, &auth_id, NULL, 0);
2450
2451
4.46k
  while (left > 0) {
2452
4.36k
    memset(&auth_id, 0, sizeof(auth_id));
2453
4.36k
    r = sc_asn1_decode(p15card->card->ctx, asn1_unusedspace, p, left, &p, &left);
2454
4.36k
    if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
2455
797
      break;
2456
3.57k
    if (r < 0)
2457
3.47k
      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
95
    if (path.count > 0 && p15card->file_app) {
2462
9
      r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &path);
2463
9
      if (r < 0)
2464
0
        return r;
2465
9
      r = sc_pkcs15_add_unusedspace(p15card, &path, &auth_id);
2466
9
      if (r)
2467
0
        return r;
2468
9
    }
2469
95
  }
2470
2471
890
  p15card->unusedspace_read = 1;
2472
2473
890
  return 0;
2474
4.36k
}
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
1.50k
{
2479
1.50k
  LOG_FUNC_CALLED(card->ctx);
2480
1.50k
#ifdef ENABLE_ZLIB
2481
1.50k
  int rv = SC_SUCCESS;
2482
1.50k
  int method = 0;
2483
2484
1.50k
  if (flags & SC_FILE_FLAG_COMPRESSED_GZIP) {
2485
0
    method = COMPRESSION_GZIP;
2486
1.50k
  } else if (flags & SC_FILE_FLAG_COMPRESSED_ZLIB) {
2487
1
    method = COMPRESSION_ZLIB;
2488
1.50k
  } else {
2489
1.50k
    method = COMPRESSION_AUTO;
2490
1.50k
  }
2491
2492
1.50k
  rv = sc_decompress_alloc(out, outlen, buf, buflen, method);
2493
1.50k
  if (rv != SC_SUCCESS) {
2494
1.24k
    sc_log(card->ctx,  "Decompression failed: %d", rv);
2495
1.24k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_DATA);
2496
1.24k
  }
2497
265
  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
265
}
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
329k
{
2530
329k
  struct sc_context *ctx;
2531
329k
  struct sc_file *file = NULL;
2532
329k
  unsigned char *data = NULL;
2533
329k
  size_t  len = 0, offset = 0;
2534
329k
  int r;
2535
2536
329k
  if (p15card == NULL || p15card->card == NULL || in_path == NULL || buf == NULL) {
2537
0
    return SC_ERROR_INVALID_ARGUMENTS;
2538
0
  }
2539
329k
  ctx = p15card->card->ctx;
2540
2541
329k
  LOG_FUNC_CALLED(ctx);
2542
329k
  sc_log(ctx, "path=%s, index=%u, count=%d", sc_print_path(in_path), in_path->index, in_path->count);
2543
2544
329k
  r = -1; /* file state: not in cache */
2545
329k
  if (p15card->opts.use_file_cache
2546
60.1k
      && ((p15card->opts.use_file_cache & SC_PKCS15_OPTS_CACHE_ALL_FILES) || !private_data)) {
2547
60.0k
    r = sc_pkcs15_read_cached_file(p15card, in_path, &data, &len);
2548
2549
60.0k
    if (!r && in_path->aid.len > 0 && in_path->len >= 2)   {
2550
123
      struct sc_path parent = *in_path;
2551
2552
123
      parent.len -= 2;
2553
123
      parent.type = SC_PATH_TYPE_PATH;
2554
123
      r = sc_select_file(p15card->card, &parent, NULL);
2555
123
    }
2556
60.0k
  }
2557
2558
329k
  if (r) {
2559
326k
    r = sc_lock(p15card->card);
2560
326k
    if (r)
2561
0
      goto fail;
2562
326k
    r = sc_select_file(p15card->card, in_path, &file);
2563
326k
    if (r)
2564
296k
      goto fail_unlock;
2565
2566
    /* Handle the case where the ASN.1 Path object specified
2567
     * index and length values */
2568
2569
30.5k
    if (file->ef_structure == SC_FILE_EF_LINEAR_VARIABLE) {
2570
2571
      // in_path->index: record_no
2572
      // in_path->count: ignored!
2573
2574
966
      if(file->record_length > 0) {
2575
373
        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
373
        } else {
2579
373
          len = file->record_length;
2580
373
        }
2581
593
      } else {
2582
593
        len = MAX_FILE_SIZE;
2583
593
      }
2584
2585
966
      if ((in_path->index <= 0) || (in_path->index > (int)(file->record_count))) {
2586
788
        sc_log(ctx, "  record number out of bounds: %d", in_path->index);
2587
788
        r = SC_ERROR_RECORD_NOT_FOUND;
2588
788
        goto fail_unlock;
2589
788
      }
2590
2591
29.6k
    } else {
2592
2593
29.6k
      if (in_path->count < 0) {
2594
22.1k
        if (file->size)
2595
15.2k
          len = (file->size > MAX_FILE_SIZE)? MAX_FILE_SIZE:file->size;
2596
6.88k
        else
2597
6.88k
          len = 1024;
2598
22.1k
        offset = 0;
2599
22.1k
      }
2600
7.48k
      else {
2601
7.48k
        offset = in_path->index;
2602
7.48k
        len = in_path->count;
2603
        /* Make sure we're within proper bounds */
2604
7.48k
        if (offset >= file->size || offset + len > file->size) {
2605
203
          r = SC_ERROR_INVALID_ASN1_OBJECT;
2606
203
          goto fail_unlock;
2607
203
        }
2608
7.48k
      }
2609
29.6k
    }
2610
2611
29.5k
    free(data);
2612
29.5k
    data = malloc(len);
2613
29.5k
    if (data == NULL) {
2614
0
      r = SC_ERROR_OUT_OF_MEMORY;
2615
0
      goto fail_unlock;
2616
0
    }
2617
2618
29.5k
    if (file->ef_structure == SC_FILE_EF_LINEAR_VARIABLE_TLV) {
2619
2.10k
      unsigned int i;
2620
2.10k
      size_t value_bytes_read = 0, record_len;
2621
2.10k
      size_t record_bytes, copied, missing;
2622
2.10k
      u8 record[256], tag;
2623
2.10k
      const u8 *value;
2624
2625
      /* Stop before the reserved ISO 7816-4 record number 255. */
2626
4.01k
      for (i = 1; i < 0xFF; i++) {
2627
        /* The aggregate size counts values, not record headers. */
2628
4.01k
        r = sc_read_record(p15card->card, i, 0, record, sizeof(record), SC_RECORD_BY_REC_NR);
2629
4.01k
        if (r == SC_ERROR_RECORD_NOT_FOUND)
2630
13
          break;
2631
4.00k
        if (r < 0) {
2632
1.68k
          goto fail_unlock;
2633
1.68k
        }
2634
2.31k
        if (r == 0)
2635
78
          break;
2636
2.23k
        if ((size_t)r > sizeof(record)) {
2637
0
          r = SC_ERROR_INVALID_TLV_OBJECT;
2638
0
          goto fail_unlock;
2639
0
        }
2640
2.23k
        record_bytes = (size_t)r;
2641
2.23k
        value = record;
2642
2.23k
        r = sc_simpletlv_read_tag(&value, record_bytes, &tag, &record_len,
2643
2.23k
            SC_SIMPLETLV_BIG_ENDIAN);
2644
2.23k
        if ((r != SC_SUCCESS && r != SC_ERROR_TLV_END_OF_CONTENTS) ||
2645
2.21k
            record_len > len - value_bytes_read) {
2646
120
          r = SC_ERROR_INVALID_TLV_OBJECT;
2647
120
          goto fail_unlock;
2648
120
        }
2649
2.11k
        copied = MIN(record_len, record_bytes - (size_t)(value - record));
2650
2.11k
        memcpy(data + value_bytes_read, value, copied);
2651
2.11k
        missing = record_len - copied;
2652
2.11k
        if (missing > 0) {
2653
213
          r = sc_read_record(p15card->card, i, (unsigned int)record_bytes,
2654
213
              data + value_bytes_read + copied, missing, SC_RECORD_BY_REC_NR);
2655
213
          if (r < 0)
2656
191
            goto fail_unlock;
2657
22
          if ((size_t)r != missing) {
2658
13
            r = SC_ERROR_INVALID_TLV_OBJECT;
2659
13
            goto fail_unlock;
2660
13
          }
2661
22
        }
2662
1.91k
        value_bytes_read += record_len;
2663
1.91k
      }
2664
91
      len = value_bytes_read;
2665
91
      if (len > 0) {
2666
38
        unsigned char *tmp = realloc(data, len);
2667
2668
38
        if (tmp != NULL)
2669
38
          data = tmp;
2670
38
      }
2671
91
    }
2672
27.4k
    else if (file->ef_structure == SC_FILE_EF_LINEAR_VARIABLE) {
2673
178
      r = sc_read_record(p15card->card, in_path->index, (unsigned)offset, data, len, SC_RECORD_BY_REC_NR);
2674
178
      if (r < 0) {
2675
54
        goto fail_unlock;
2676
54
      }
2677
      /* sc_read_record may return less than requested */
2678
124
      len = r;
2679
124
    }
2680
27.3k
    else {
2681
27.3k
      unsigned long flags = 0;
2682
27.3k
      r = sc_read_binary(p15card->card, (unsigned)offset, data, len, &flags);
2683
27.3k
      if (r < 0) {
2684
12.9k
        goto fail_unlock;
2685
12.9k
      }
2686
      /* sc_read_binary may return less than requested */
2687
14.3k
      len = r;
2688
2689
14.3k
      if (flags & SC_FILE_FLAG_COMPRESSED_AUTO
2690
12.8k
          || flags & SC_FILE_FLAG_COMPRESSED_ZLIB
2691
12.8k
          || flags & SC_FILE_FLAG_COMPRESSED_GZIP) {
2692
1.50k
        unsigned char *decompressed_buf = NULL;
2693
1.50k
        size_t decompressed_len = 0;
2694
1.50k
        r = decompress_file(p15card->card, data, len, &decompressed_buf, &decompressed_len, flags);
2695
1.50k
        if (r != SC_SUCCESS) {
2696
1.24k
          goto fail_unlock;
2697
1.24k
        }
2698
265
        free(data);
2699
265
        data = decompressed_buf;
2700
265
        len = decompressed_len;
2701
265
      }
2702
14.3k
    }
2703
13.3k
    sc_unlock(p15card->card);
2704
2705
13.3k
    sc_file_free(file);
2706
2707
13.3k
    if (p15card->opts.use_file_cache && ((p15card->opts.use_file_cache & SC_PKCS15_OPTS_CACHE_ALL_FILES) || !private_data)) {
2708
4.09k
      sc_pkcs15_cache_file(p15card, in_path, data, len);
2709
4.09k
    }
2710
13.3k
    if (len == 0) {
2711
1.49k
      free(data);
2712
1.49k
      data = NULL;
2713
1.49k
    }
2714
13.3k
  }
2715
16.0k
  *buf = data;
2716
16.0k
  *buflen = len;
2717
16.0k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
2718
2719
313k
fail_unlock:
2720
313k
  sc_unlock(p15card->card);
2721
313k
fail:
2722
313k
  free(data);
2723
313k
  sc_file_free(file);
2724
313k
  LOG_FUNC_RETURN(ctx, r);
2725
313k
}
2726
2727
2728
int
2729
sc_pkcs15_compare_id(const struct sc_pkcs15_id *id1, const struct sc_pkcs15_id *id2)
2730
241k
{
2731
241k
  if (id1 == NULL || id2 == NULL)
2732
0
    return 0;
2733
241k
  if (id1->len != id2->len)
2734
12.1k
    return 0;
2735
229k
  return memcmp(id1->value, id2->value, id1->len) == 0;
2736
241k
}
2737
2738
2739
void
2740
sc_pkcs15_format_id(const char *str, struct sc_pkcs15_id *id)
2741
687k
{
2742
687k
  size_t len;
2743
2744
687k
  if (!id)
2745
0
    return;
2746
687k
  len = sizeof(id->value);
2747
2748
687k
  if (sc_hex_to_bin(str, id->value, &len) != SC_SUCCESS)
2749
767
    id->len = 0;
2750
687k
  else
2751
687k
    id->len = len;
2752
687k
}
2753
2754
2755
const char *
2756
sc_pkcs15_print_id(const struct sc_pkcs15_id *id)
2757
38.4k
{
2758
38.4k
  static char buffer[256];
2759
2760
38.4k
  sc_bin_to_hex(id->value, id->len, buffer, sizeof(buffer), '\0');
2761
38.4k
  return buffer;
2762
38.4k
}
2763
2764
2765
int
2766
sc_pkcs15_hex_string_to_id(const char *in, struct sc_pkcs15_id *out)
2767
349
{
2768
349
  out->len = sizeof(out->value);
2769
349
  return sc_hex_to_bin(in, out->value, &out->len);
2770
349
}
2771
2772
2773
int
2774
sc_pkcs15_make_absolute_path(const struct sc_path *parent, struct sc_path *child)
2775
102k
{
2776
  /* nothing to do if child has valid 'aid' */
2777
102k
  if (child->aid.len)
2778
0
    return SC_SUCCESS;
2779
2780
102k
  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
102k
  else if (parent->type == SC_PATH_TYPE_DF_NAME)   {
2796
    /* child inherits parent's 'DF NAME' as 'aid' */
2797
2.81k
    if (parent->len > sizeof(child->aid.value))
2798
0
      return SC_ERROR_WRONG_LENGTH;
2799
2800
2.81k
    memcpy(child->aid.value, parent->value, parent->len);
2801
2.81k
    child->aid.len = parent->len;
2802
2803
2.81k
    return SC_SUCCESS;
2804
2.81k
  }
2805
2806
  /* a 0 length path stays a 0 length path */
2807
99.9k
  if (child->len == 0)
2808
1.43k
    return SC_SUCCESS;
2809
2810
98.5k
  if (sc_compare_path_prefix(sc_get_mf_path(), child))
2811
2.09k
    return SC_SUCCESS;
2812
2813
96.4k
  return sc_concatenate_path(child, parent, child);
2814
98.5k
}
2815
2816
2817
void sc_pkcs15_free_object_content(struct sc_pkcs15_object *obj)
2818
300k
{
2819
300k
  if (obj->content.value && obj->content.len) {
2820
1.90k
    if (obj->content_free) {
2821
1.80k
      obj->content_free(obj->content.value, obj->content.len);
2822
1.80k
    } else {
2823
101
      free(obj->content.value);
2824
101
    }
2825
1.90k
  }
2826
300k
  obj->content.value = NULL;
2827
300k
  obj->content.len = 0;
2828
300k
}
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
1.80k
{
2835
1.80k
  unsigned char *tmp_buf;
2836
2837
1.80k
  if (!obj)
2838
0
    return SC_ERROR_INVALID_ARGUMENTS;
2839
2840
1.80k
  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
1.80k
  if (SC_PKCS15_TYPE_AUTH & obj->type
2849
0
      || SC_PKCS15_TYPE_SKEY & obj->type
2850
1.80k
      || SC_PKCS15_TYPE_PRKEY & obj->type) {
2851
1.80k
    tmp_buf = sc_mem_secure_alloc(len);
2852
1.80k
    obj->content_free = sc_mem_secure_free;
2853
1.80k
  } else {
2854
0
    tmp_buf = malloc(len);
2855
0
  }
2856
1.80k
  if (!tmp_buf)
2857
0
    return SC_ERROR_OUT_OF_MEMORY;
2858
2859
1.80k
  memcpy(tmp_buf, value, len);
2860
2861
1.80k
  sc_pkcs15_free_object_content(obj);
2862
2863
1.80k
  obj->content.value = tmp_buf;
2864
1.80k
  obj->content.len = len;
2865
2866
1.80k
  return SC_SUCCESS;
2867
1.80k
}
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
2.71k
{
2894
2.71k
  struct sc_context *ctx = p15card->card->ctx;
2895
2.71k
  struct sc_supported_algo_info *info = NULL;
2896
2.71k
  int ii;
2897
2898
2.71k
  if (algo_oid == NULL)
2899
0
    return NULL;
2900
2901
5.53k
  for (ii=0;ii<SC_MAX_SUPPORTED_ALGORITHMS && p15card->tokeninfo->supported_algos[ii].reference; ii++)
2902
2.83k
    if ((p15card->tokeninfo->supported_algos[ii].operations & operation)
2903
2.80k
        && (p15card->tokeninfo->supported_algos[ii].mechanism == mechanism)
2904
729
        && sc_compare_oid(algo_oid, &p15card->tokeninfo->supported_algos[ii].algo_id) == 1)
2905
15
      break;
2906
2907
2.71k
  if (ii < SC_MAX_SUPPORTED_ALGORITHMS && p15card->tokeninfo->supported_algos[ii].reference)   {
2908
15
    info = &p15card->tokeninfo->supported_algos[ii];
2909
15
    sc_log(ctx, "found supported algorithm (ref:%X,mech:%X,ops:%X,algo_ref:%X)",
2910
15
        info->reference, info->mechanism, info->operations, info->algo_ref);
2911
15
  }
2912
2913
2.71k
  return info;
2914
2.71k
}
2915
2916
int
2917
sc_pkcs15_get_generalized_time(struct sc_context *ctx, char **out)
2918
767
{
2919
767
#ifdef HAVE_GETTIMEOFDAY
2920
767
  struct timeval tv;
2921
767
#endif
2922
767
  struct tm tm;
2923
767
  time_t t;
2924
2925
767
  if (!ctx || !out)
2926
0
    return SC_ERROR_INVALID_ARGUMENTS;
2927
767
  *out = NULL;
2928
2929
767
#ifdef HAVE_GETTIMEOFDAY
2930
767
  gettimeofday(&tv, NULL);
2931
767
  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
767
  if (NULL == gmtime_r(&t, &tm))
2941
767
    LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);
2942
767
#endif
2943
2944
767
  *out = calloc(1, 16);
2945
767
  if (*out == NULL)
2946
767
    LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "memory failure");
2947
2948
  /* print time in generalized time format */
2949
767
  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
767
  return SC_SUCCESS;
2955
767
}
2956
2957
2958
int
2959
sc_pkcs15_add_supported_algo_ref(struct sc_pkcs15_object *obj, struct sc_supported_algo_info *algo)
2960
1.36k
{
2961
1.36k
  unsigned int ii, *algo_refs = NULL;
2962
2963
1.36k
  if (!algo)
2964
0
    return SC_SUCCESS;
2965
2966
1.36k
  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
1.36k
  case SC_PKCS15_TYPE_SKEY:
2974
1.36k
    algo_refs = ((struct sc_pkcs15_skey_info *)obj->data)->algo_refs;
2975
1.36k
    break;
2976
1.36k
  }
2977
1.36k
  if (!algo_refs)
2978
0
    return SC_ERROR_NOT_SUPPORTED;
2979
2980
2.03k
  for (ii=0;ii<SC_MAX_SUPPORTED_ALGORITHMS && *(algo_refs + ii);ii++)
2981
680
    if (*(algo_refs + ii) == algo->reference)
2982
1
      return SC_SUCCESS;
2983
2984
2.03k
  for (ii=0;ii<SC_MAX_SUPPORTED_ALGORITHMS;ii++)   {
2985
2.03k
    if (*(algo_refs + ii) == 0)   {
2986
1.35k
      *(algo_refs + ii) = algo->reference;
2987
1.35k
      return SC_SUCCESS;
2988
1.35k
    }
2989
2.03k
  }
2990
2991
0
  return SC_ERROR_TOO_MANY_OBJECTS;
2992
1.35k
}
2993
2994
2995
int
2996
sc_pkcs15_get_object_id(const struct sc_pkcs15_object *obj, struct sc_pkcs15_id *out)
2997
1.14k
{
2998
1.14k
  if (!obj || !out)
2999
0
    return SC_ERROR_INVALID_ARGUMENTS;
3000
3001
1.14k
  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
1.14k
  case SC_PKCS15_TYPE_PRKEY:
3006
1.14k
    *out = ((struct sc_pkcs15_prkey_info *) obj->data)->id;
3007
1.14k
    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
1.14k
  }
3023
3024
1.14k
  return SC_SUCCESS;
3025
1.14k
}
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
167
{
3038
167
  if (in_size < 16)
3039
0
    return SC_ERROR_BUFFER_TOO_SMALL;
3040
167
  if (out_size < 39)
3041
0
    return SC_ERROR_BUFFER_TOO_SMALL;
3042
3043
167
  snprintf(out, out_size,
3044
167
      "%s%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%s",
3045
167
      flags ? "" : "{",
3046
167
      in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7],
3047
167
      in[8], in[9], in[10], in[11], in[12], in[13], in[14], in[15],
3048
167
      flags ? "" : "}");
3049
3050
167
  return SC_SUCCESS;
3051
167
}
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
1.14k
{
3057
1.14k
  struct sc_context *ctx = p15card->card->ctx;
3058
1.14k
  struct sc_serial_number serialnr;
3059
1.14k
  struct sc_pkcs15_id  id;
3060
1.14k
  unsigned char guid_bin[SC_PKCS15_MAX_ID_SIZE + SC_MAX_SERIALNR];
3061
1.14k
  int rv;
3062
1.14k
  size_t guid_bin_size;
3063
3064
1.14k
  LOG_FUNC_CALLED(ctx);
3065
1.14k
  if(!out || !out_size)
3066
1.14k
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
3067
3068
1.14k
  if (p15card->ops.get_guid)   {
3069
840
    rv = p15card->ops.get_guid(p15card, obj, out, out_size);
3070
840
    LOG_FUNC_RETURN(ctx, rv);
3071
840
  }
3072
3073
302
  rv = sc_pkcs15_aux_get_md_guid(p15card, obj, flags, out, out_size);
3074
302
  if (rv == SC_SUCCESS)
3075
302
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
3076
302
  else if (rv != SC_ERROR_NOT_SUPPORTED)
3077
0
    LOG_TEST_RET(ctx, rv, "Failed to get alternative object GUID");
3078
3079
302
  memset(out, 0, *out_size);
3080
3081
302
  rv = sc_pkcs15_get_object_id(obj, &id);
3082
302
  LOG_TEST_RET(ctx, rv, "Cannot get object's ID");
3083
3084
302
  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
156
    serialnr.len = SC_MAX_SERIALNR;
3088
156
    rv = sc_hex_to_bin(p15card->tokeninfo->serial_number, serialnr.value, &serialnr.len);
3089
156
    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
156
  } else if (p15card->card->serialnr.len)   {
3098
6
    serialnr = p15card->card->serialnr;
3099
140
  } else   {
3100
140
    rv = sc_card_ctl(p15card->card, SC_CARDCTL_GET_SERIALNR, &serialnr);
3101
140
    LOG_TEST_RET(ctx, rv, "'GET_SERIALNR' CTL failed and other serial numbers not present");
3102
140
  }
3103
3104
167
  memset(guid_bin, 0, sizeof(guid_bin));
3105
167
  memcpy(guid_bin, id.value, id.len);
3106
167
  memcpy(guid_bin + id.len, serialnr.value, serialnr.len);
3107
167
  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
167
#ifdef ENABLE_OPENSSL
3114
167
  SHA1(guid_bin, guid_bin_size, guid_bin);
3115
167
  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
167
  rv = sc_pkcs15_serialize_guid(guid_bin, guid_bin_size, flags, (char *)out, *out_size);
3126
167
  LOG_TEST_RET(ctx, rv, "Serialize GUID error");
3127
3128
167
  *out_size = strlen((char *)out);
3129
167
  LOG_FUNC_RETURN(ctx, rv);
3130
167
}
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
302
{
3138
302
  struct sc_context *ctx = p15card->card->ctx;
3139
302
  struct sc_pkcs15_prkey_info *prkey_info = NULL;
3140
302
  int rv;
3141
3142
302
  LOG_FUNC_CALLED(ctx);
3143
302
  if(!out || !out_size)
3144
302
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
3145
3146
302
  if ((obj->type & SC_PKCS15_TYPE_CLASS_MASK) != SC_PKCS15_TYPE_PRKEY)
3147
302
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
3148
3149
302
  prkey_info = (struct sc_pkcs15_prkey_info *)obj->data;
3150
302
  if (!prkey_info->aux_data || prkey_info->aux_data->type != SC_AUX_DATA_TYPE_MD_CMAP_RECORD)
3151
302
    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
37.2k
{
3161
37.2k
  if (!params)
3162
0
    return;
3163
37.2k
  if (params->data && params->free_params)
3164
1.28k
    params->free_params(params->data);
3165
35.9k
  else if (params->data)
3166
21
    free(params->data);
3167
3168
  params->data = NULL;
3169
37.2k
}
3170