Coverage Report

Created: 2026-10-17 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/pkcs15-coolkey.c
Line
Count
Source
1
/*
2
 * partial PKCS15 emulation for Coolkey cards
3
 *
4
 * Copyright (C) 2005,2006,2007,2008,2009,2010
5
 *               Douglas E. Engert <deengert@anl.gov>
6
 *               2004, Nils Larsch <larsch@trustcenter.de>
7
 * Copyright (C) 2006, Identity Alliance,
8
 *               Thomas Harning <thomas.harning@identityalliance.com>
9
 * Copyright (C) 2007, EMC, Russell Larner <rlarner@rsa.com>
10
 * Copyright (C) 2016, Red Hat, Inc.
11
 *
12
 * Coolkey driver author: Robert Relyea <rrelyea@redhat.com>
13
 *
14
 * This library is free software; you can redistribute it and/or
15
 * modify it under the terms of the GNU Lesser General Public
16
 * License as published by the Free Software Foundation; either
17
 * version 2.1 of the License, or (at your option) any later version.
18
 *
19
 * This library is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22
 * Lesser General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public
25
 * License along with this library; if not, write to the Free Software
26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27
 */
28
29
#ifdef HAVE_CONFIG_H
30
#include "config.h"
31
#endif
32
33
#include <stdlib.h>
34
#include <string.h>
35
#include <stdio.h>
36
#include <ctype.h>
37
38
#include "internal.h"
39
#include "cardctl.h"
40
#include "asn1.h"
41
#include "pkcs15.h"
42
#include "../pkcs11/pkcs11.h"
43
44
typedef struct pdata_st {
45
  const char *id;
46
  const char *label;
47
  const char *path;
48
  int         ref;
49
  int         type;
50
  unsigned int maxlen;
51
  unsigned int minlen;
52
  unsigned int storedlen;
53
  int         flags;
54
  int         tries_left;
55
  const unsigned char  pad_char;
56
  int         obj_flags;
57
} pindata;
58
59
static int coolkey_detect_card(sc_pkcs15_card_t *p15card)
60
12.3k
{
61
12.3k
  sc_card_t *card = p15card->card;
62
63
12.3k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
64
12.3k
  if (card->type < SC_CARD_TYPE_COOLKEY_GENERIC
65
1.48k
    || card->type >= SC_CARD_TYPE_COOLKEY_GENERIC+1000)
66
11.7k
    return SC_ERROR_INVALID_CARD;
67
686
  return SC_SUCCESS;
68
12.3k
}
69
70
static int
71
641
coolkey_get_object(sc_card_t *card, unsigned long object_id, sc_cardctl_coolkey_object_t **obj) {
72
641
  sc_cardctl_coolkey_find_object_t fobj;
73
641
  int r;
74
75
641
  fobj.type = SC_CARDCTL_COOLKEY_FIND_BY_ID;
76
641
  fobj.find_id = object_id;
77
641
  fobj.obj = NULL;
78
641
  r = sc_card_ctl(card, SC_CARDCTL_COOLKEY_FIND_OBJECT, &fobj);
79
641
  if (r < 0) {
80
168
    return r;
81
168
  }
82
473
  *obj = fobj.obj;
83
473
  return SC_SUCCESS;
84
641
}
85
86
87
/*
88
 * fetch attributes from an object
89
 */
90
static int
91
44.7k
coolkey_get_attribute(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, CK_ATTRIBUTE_TYPE type, const u8 **val, size_t *val_len, u8 *data_type) {
92
44.7k
  sc_cardctl_coolkey_attribute_t attribute;
93
44.7k
  int r;
94
95
44.7k
  attribute.object = obj;
96
44.7k
  attribute.attribute_type = type;
97
98
44.7k
  r = sc_card_ctl(card, SC_CARDCTL_COOLKEY_GET_ATTRIBUTE, &attribute);
99
44.7k
  if (r < 0) {
100
24.1k
    return r;
101
24.1k
  }
102
20.6k
  *val = attribute.attribute_value;
103
20.6k
  *val_len = attribute.attribute_length;
104
20.6k
  if (data_type) {
105
2.04k
    *data_type = attribute.attribute_data_type;
106
2.04k
  }
107
20.6k
  return SC_SUCCESS;
108
44.7k
}
109
110
static int
111
1.84k
coolkey_find_matching_cert(sc_card_t *card, sc_cardctl_coolkey_object_t *in_obj, sc_cardctl_coolkey_object_t **cert_obj) {
112
1.84k
  sc_cardctl_coolkey_find_object_t fobj;
113
1.84k
  sc_cardctl_coolkey_attribute_t template[2];
114
1.84k
  u8 obj_class[4];
115
1.84k
  int r;
116
117
  /* we are searching for certs .. */
118
1.84k
  template[0].attribute_type = CKA_CLASS;
119
1.84k
  template[0].attribute_data_type = SC_CARDCTL_COOLKEY_ATTR_TYPE_ULONG;
120
1.84k
  template[0].attribute_length = sizeof(obj_class);
121
1.84k
  template[0].attribute_value = obj_class;
122
1.84k
  ulong2bebytes(obj_class, CKO_CERTIFICATE);
123
124
  /* fetch the current object's CKA_ID */
125
1.84k
  template[1].attribute_type = CKA_ID;
126
1.84k
  template[1].object = in_obj;
127
1.84k
  r = sc_card_ctl(card, SC_CARDCTL_COOLKEY_GET_ATTRIBUTE, &template[1]);
128
1.84k
  if (r < 0) {
129
12
    return r;
130
12
  }
131
1.83k
  template[0].object = NULL; /*paranoia */
132
1.83k
  template[1].object = NULL; /*paranoia */
133
134
  /* now find the cert that has the ID */
135
1.83k
  fobj.type = SC_CARDCTL_COOLKEY_FIND_BY_TEMPLATE;
136
1.83k
  fobj.obj = NULL;
137
1.83k
  fobj.coolkey_template = &template[0];
138
1.83k
  fobj.template_count=2;
139
1.83k
  r = sc_card_ctl(card, SC_CARDCTL_COOLKEY_FIND_OBJECT, &fobj);
140
1.83k
  if (r < 0) {
141
1.21k
    return r;
142
1.21k
  }
143
614
  *cert_obj = fobj.obj;
144
614
  return SC_SUCCESS;
145
1.83k
}
146
147
static int
148
coolkey_get_attribute_ulong(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, CK_ATTRIBUTE_TYPE type, CK_ULONG *value)
149
6.63k
{
150
6.63k
  const u8 *val = NULL;
151
6.63k
  size_t val_len = 0;
152
6.63k
  u8 data_type = 0;
153
6.63k
  int r;
154
155
6.63k
  r  = coolkey_get_attribute(card, obj, type, &val, &val_len, &data_type);
156
6.63k
  if (r < 0) {
157
4.59k
    return r;
158
4.59k
  }
159
2.04k
  if ((data_type != SC_CARDCTL_COOLKEY_ATTR_TYPE_ULONG) &&
160
85
      (val_len != sizeof(CK_ULONG))) {
161
41
    return SC_ERROR_CORRUPTED_DATA;
162
41
  }
163
2.00k
  *value = bebytes2ulong(val);
164
2.00k
  return SC_SUCCESS;
165
2.04k
}
166
167
static int
168
coolkey_get_attribute_boolean(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, CK_ATTRIBUTE_TYPE attr_type)
169
29.1k
{
170
29.1k
  int r;
171
29.1k
  const u8 *val = NULL;
172
29.1k
  size_t val_len = 0;
173
174
29.1k
  r = coolkey_get_attribute(card, obj, attr_type, &val, &val_len, NULL);
175
29.1k
  if (r < 0) {
176
    /* attribute not valid for this object, set boolean to false */
177
12.2k
    return 0;
178
12.2k
  }
179
16.8k
  if ((val_len == 1) && (*val == 1)) {
180
3.84k
    return 1;
181
3.84k
  }
182
12.9k
  return 0;
183
16.8k
}
184
185
static int
186
coolkey_get_attribute_bytes(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, CK_ATTRIBUTE_TYPE type, u8 *data, size_t *data_len, size_t max_data_len)
187
6.94k
{
188
6.94k
  const u8 *val;
189
6.94k
  size_t val_len = 0;
190
6.94k
  int r;
191
192
6.94k
  r = coolkey_get_attribute(card, obj, type, &val, &val_len, NULL);
193
6.94k
  if (r < 0) {
194
5.18k
    return r;
195
5.18k
  }
196
1.76k
  if (val_len > max_data_len) {
197
2
    val_len = max_data_len;
198
2
  }
199
1.76k
  memcpy(data, val, val_len);
200
1.76k
  *data_len = val_len;
201
1.76k
  return SC_SUCCESS;
202
6.94k
}
203
204
static int
205
coolkey_get_attribute_bytes_alloc(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, CK_ATTRIBUTE_TYPE type, u8 **data, size_t *data_len)
206
2.09k
{
207
2.09k
  const u8 *val;
208
2.09k
  size_t val_len;
209
2.09k
  int r;
210
211
2.09k
  r = coolkey_get_attribute(card, obj, type, &val, &val_len, NULL);
212
2.09k
  if (r < 0 || val_len < 1) {
213
2.08k
    return r;
214
2.08k
  }
215
7
  *data = malloc(val_len);
216
7
  if (*data == NULL) {
217
0
    return SC_ERROR_OUT_OF_MEMORY;
218
0
  }
219
7
  memcpy(*data, val, val_len);
220
7
  *data_len = val_len;
221
7
  return SC_SUCCESS;
222
7
}
223
224
static int
225
coolkey_get_id(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, struct sc_pkcs15_id *id)
226
1.77k
{
227
1.77k
  return coolkey_get_attribute_bytes(card, obj, CKA_ID, id->value , &id->len, sizeof(id->value));
228
1.77k
}
229
230
/*
231
 * A number of opensc structure have the same layout, use a common function to fill them
232
 * int:
233
 *     structure name      first parameter                 second parameter
234
 *     sc_lv_data          unsigned char * value           size_t len
235
 *     sc_pkcs15_data         u8 *data                     size_t data_len
236
 *     sc_pkcs15_bignum       u8 *data                     size_t len
237
 *     sc_pkcs15_der          u8 *value                    size_t len
238
 *     sc_pkcs15_u8           u8 *value                    size_t len
239
 *
240
 * The following can properly assign all of them
241
 */
242
int
243
coolkey_get_attribute_lv(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, CK_ATTRIBUTE_TYPE type, void *ptr)
244
2.08k
{
245
2.08k
  struct sc_pkcs15_data *item = (struct sc_pkcs15_data *)ptr;
246
2.08k
  return coolkey_get_attribute_bytes_alloc(card, obj, type, &item->data, &item->data_len);
247
2.08k
}
248
249
1.88k
#define COOLKEY_ID_CERT ((unsigned long)'c')
250
#define COOLKEY_ID_KEY ((unsigned long)'k')
251
641
#define COOLKEY_ID_CERT_DATA ((unsigned long)'C')
252
253
static unsigned long
254
1.88k
coolkey_get_object_type(unsigned long object_id) { return ((object_id >> 24 ) & 0xff); }
255
256
static unsigned long
257
coolkey_make_new_id(unsigned long object_id, unsigned long id_type)
258
641
{ return ((object_id  & 0x00ffffffUL)|(id_type << 24)); }
259
260
261
/*
262
 * We need cert data to fill in some of our keys. Also, some older tokens store the cert data in a separate
263
 * object from the rest of the cert attributes. This function handles both of these complications
264
 */
265
static int
266
coolkey_get_certificate(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, struct sc_pkcs15_der *cert)
267
1.88k
{
268
1.88k
  sc_cardctl_coolkey_object_t *cert_obj;
269
1.88k
  unsigned long object_id;
270
1.88k
  int r;
271
272
1.88k
  cert_obj = obj;
273
1.88k
  if (coolkey_get_object_type(obj->id) != COOLKEY_ID_CERT) {
274
1.84k
    r = coolkey_find_matching_cert(card, obj, &cert_obj);
275
1.84k
    if (r < 0) {
276
1.23k
      return r;
277
1.23k
    }
278
1.84k
  }
279
654
  r = coolkey_get_attribute_lv(card, cert_obj, CKA_VALUE, cert);
280
654
  if (r == SC_ERROR_DATA_OBJECT_NOT_FOUND) {
281
641
    object_id = coolkey_make_new_id(cert_obj->id, COOLKEY_ID_CERT_DATA);
282
641
    r = coolkey_get_object(card, object_id, &cert_obj);
283
641
    if (r < 0) {
284
168
      return r;
285
168
    }
286
    /* fill in cert data */
287
473
    cert->value = malloc(cert_obj->length);
288
473
    if (cert->value == NULL) {
289
0
      return SC_ERROR_OUT_OF_MEMORY;
290
0
    }
291
473
    memcpy(cert->value, cert_obj->data, cert_obj->length);
292
473
    cert->len = cert_obj->length;
293
473
    return SC_SUCCESS;
294
473
  }
295
13
  return r;
296
654
}
297
298
299
300
struct coolkey_attr_flags {
301
  CK_ATTRIBUTE_TYPE attribute_type;
302
  unsigned int pkcs15_flags;
303
};
304
305
static struct coolkey_attr_flags usage_table[] = {
306
  { CKA_ENCRYPT,          SC_PKCS15_PRKEY_USAGE_ENCRYPT       },
307
  { CKA_DECRYPT,          SC_PKCS15_PRKEY_USAGE_DECRYPT       },
308
  { CKA_SIGN,             SC_PKCS15_PRKEY_USAGE_SIGN          },
309
  { CKA_SIGN_RECOVER,     SC_PKCS15_PRKEY_USAGE_SIGNRECOVER   },
310
  { CKA_WRAP,             SC_PKCS15_PRKEY_USAGE_WRAP          },
311
  { CKA_UNWRAP,           SC_PKCS15_PRKEY_USAGE_UNWRAP        },
312
  { CKA_VERIFY,           SC_PKCS15_PRKEY_USAGE_VERIFY        },
313
  { CKA_VERIFY_RECOVER,   SC_PKCS15_PRKEY_USAGE_VERIFYRECOVER },
314
  { CKA_DERIVE,           SC_PKCS15_PRKEY_USAGE_DERIVE        }
315
};
316
317
static struct coolkey_attr_flags access_table[] = {
318
  { CKA_SENSITIVE,        SC_PKCS15_PRKEY_ACCESS_SENSITIVE       },
319
    { CKA_EXTRACTABLE,      SC_PKCS15_PRKEY_ACCESS_EXTRACTABLE     },
320
    { CKA_ALWAYS_SENSITIVE, SC_PKCS15_PRKEY_ACCESS_ALWAYSSENSITIVE },
321
    { CKA_NEVER_EXTRACTABLE,SC_PKCS15_PRKEY_ACCESS_NEVEREXTRACTABLE},
322
    { CKA_LOCAL,            SC_PKCS15_PRKEY_ACCESS_LOCAL           }
323
};
324
325
static struct coolkey_attr_flags flag_table[] = {
326
  { CKA_PRIVATE,          SC_PKCS15_CO_FLAG_PRIVATE       },
327
  { CKA_MODIFIABLE,       SC_PKCS15_CO_FLAG_MODIFIABLE    }
328
};
329
330
static int usage_table_size = sizeof(usage_table)/sizeof(usage_table[0]);
331
static int access_table_size = sizeof(access_table)/sizeof(access_table[0]);
332
static int flag_table_size = sizeof(flag_table)/sizeof(flag_table[0]);
333
334
static int
335
coolkey_set_bool_flags(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, unsigned int *flags_ptr, struct coolkey_attr_flags *table, int table_size)
336
7.96k
{
337
7.96k
  unsigned int flags = 0;
338
7.96k
  int i;
339
340
37.0k
  for (i=0; i< table_size; i++) {
341
29.1k
    if (coolkey_get_attribute_boolean(card, obj, table[i].attribute_type)) {
342
3.84k
      flags |= table[i].pkcs15_flags;
343
3.84k
    }
344
29.1k
  }
345
7.96k
  *flags_ptr = flags;
346
7.96k
  return SC_SUCCESS;
347
7.96k
}
348
349
/* map a cert usage and algorithm to public and private key usages */
350
static int
351
coolkey_get_usage(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, unsigned int *usage_ptr)
352
1.32k
{
353
1.32k
  return coolkey_set_bool_flags(card, obj, usage_ptr, usage_table, usage_table_size);
354
1.32k
}
355
356
/* map a cert usage and algorithm to public and private key usages */
357
static int
358
coolkey_get_flags(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, unsigned int *flag_ptr)
359
5.32k
{
360
5.32k
  return coolkey_set_bool_flags(card, obj, flag_ptr, flag_table, flag_table_size);
361
5.32k
}
362
363
static int
364
coolkey_get_access(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, unsigned int *access_ptr)
365
1.32k
{
366
1.32k
  return coolkey_set_bool_flags(card, obj, access_ptr, access_table, access_table_size);
367
1.32k
}
368
369
370
/*
371
 * turn a coolkey object into a pkcss 15 pubkey. object should already be type
372
 * CKO_PUBLIC_KEY */
373
static sc_pkcs15_pubkey_t *
374
coolkey_make_public_key(sc_card_t *card, sc_cardctl_coolkey_object_t *obj, CK_KEY_TYPE key_type)
375
1.44k
{
376
1.44k
  sc_pkcs15_pubkey_t *key;
377
1.44k
  int r;
378
379
1.44k
  key = calloc(1, sizeof(struct sc_pkcs15_pubkey));
380
1.44k
  if (!key)
381
0
    return NULL;
382
1.44k
  switch (key_type) {
383
1.43k
  case CKK_RSA:
384
1.43k
    key->algorithm = SC_ALGORITHM_RSA;
385
1.43k
    r = coolkey_get_attribute_lv(card, obj, CKA_MODULUS, &key->u.rsa.modulus);
386
1.43k
    if (r != SC_SUCCESS) {
387
1.43k
      goto fail;
388
1.43k
    }
389
2
    r = coolkey_get_attribute_lv(card, obj, CKA_PUBLIC_EXPONENT, &key->u.rsa.exponent);
390
2
    if (r != SC_SUCCESS) {
391
2
      goto fail;
392
2
    }
393
0
    break;
394
1
  case CKK_EC:
395
1
    key->algorithm = SC_ALGORITHM_EC;
396
1
    r = coolkey_get_attribute_bytes_alloc(card, obj, CKA_EC_POINT, &key->u.ec.ecpointQ.value, &key->u.ec.ecpointQ.len);
397
1
      if(r < 0) {
398
1
      goto fail;
399
1
    }
400
0
    r = coolkey_get_attribute_bytes_alloc(card, obj, CKA_EC_PARAMS,
401
0
        &key->u.ec.params.der.value, &key->u.ec.params.der.len);
402
0
    if (r < 0) {
403
0
      goto fail;
404
0
    }
405
0
    r = sc_pkcs15_fix_ec_parameters(card->ctx, &key->u.ec.params);
406
0
    if (r < 0) {
407
0
      goto fail;
408
0
    }
409
0
    break;
410
1.44k
  }
411
15
  return key;
412
1.43k
fail:
413
1.43k
  sc_pkcs15_free_pubkey(key);
414
415
  /* now parse the DER cert */
416
1.43k
  return NULL;
417
1.44k
}
418
419
420
static sc_pkcs15_pubkey_t *
421
coolkey_get_public_key_from_certificate(sc_pkcs15_card_t *p15card, sc_cardctl_coolkey_object_t *obj)
422
1.43k
{
423
1.43k
  sc_pkcs15_cert_info_t cert_info;
424
1.43k
  sc_pkcs15_cert_t *cert_out = NULL;
425
1.43k
  sc_pkcs15_pubkey_t *key = NULL;
426
1.43k
  int r, private_obj;
427
1.43k
  unsigned int flags;
428
429
1.43k
  memset(&cert_info, 0, sizeof(cert_info));
430
431
1.43k
  r = coolkey_get_certificate(p15card->card, obj, &cert_info.value);
432
1.43k
  if (r < 0) {
433
1.28k
    goto fail;
434
1.28k
  }
435
436
145
  coolkey_get_flags(p15card->card, obj, &flags);
437
145
  private_obj = flags & SC_PKCS15_CO_FLAG_PRIVATE;
438
145
  r = sc_pkcs15_read_certificate(p15card, &cert_info, private_obj, &cert_out);
439
145
  if (r < 0) {
440
145
    goto fail;
441
145
  }
442
0
  key = cert_out->key;
443
0
  cert_out->key = NULL; /* adopt the key from the cert */
444
1.43k
fail:
445
1.43k
  if (cert_out) {
446
0
    sc_pkcs15_free_certificate(cert_out);
447
0
  }
448
1.43k
  if (cert_info.value.value) {
449
142
    free(cert_info.value.value);
450
142
  }
451
1.43k
  return key;
452
0
}
453
454
static sc_pkcs15_pubkey_t *
455
coolkey_get_public_key(sc_pkcs15_card_t *p15card, sc_cardctl_coolkey_object_t *obj, CK_KEY_TYPE key_type)
456
1.44k
{
457
1.44k
  sc_pkcs15_pubkey_t *key;
458
459
1.44k
  key = coolkey_make_public_key(p15card->card, obj, key_type);
460
1.44k
  if (key) {
461
15
    return key;
462
15
  }
463
1.43k
  return coolkey_get_public_key_from_certificate(p15card, obj);
464
1.44k
}
465
466
static int sc_pkcs15emu_coolkey_init(sc_pkcs15_card_t *p15card)
467
686
{
468
686
  int use_pin_cache_backup = p15card->opts.use_pin_cache;
469
686
  static const pindata pins[] = {
470
686
    { "1", NULL, "", 0x00,
471
686
      SC_PKCS15_PIN_TYPE_ASCII_NUMERIC,
472
686
      32, 4, 32,
473
686
      SC_PKCS15_PIN_FLAG_INITIALIZED,
474
686
      -1, 0xFF,
475
686
      SC_PKCS15_CO_FLAG_PRIVATE },
476
686
    { NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0}
477
686
  };
478
479
  /*
480
   * The size of the key or the algid is not really known
481
   * but can be derived from the certificates.
482
   * the cert, pubkey and privkey are a set.
483
   * Key usages bits taken from certificate key usage extension.
484
   */
485
486
686
  int    r, i;
487
686
  sc_card_t *card = p15card->card;
488
686
  sc_serial_number_t serial;
489
686
  int count;
490
686
  struct sc_pkcs15_object *obj;
491
492
686
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
493
494
686
  memset(&serial, 0, sizeof(serial));
495
496
  /* coolkey caches a nonce once it logs in, don't keep the pin around. The card will
497
   * stay logged in until it's been pulled from the reader, in which case you want to reauthenticate
498
   * anyway */
499
686
  p15card->opts.use_pin_cache = 0;
500
501
  /* get the token info from the card */
502
686
  r = sc_card_ctl(card, SC_CARDCTL_COOLKEY_GET_TOKEN_INFO, p15card->tokeninfo);
503
686
  if (r < 0) {
504
    /* put some defaults in if we didn't succeed */
505
0
    set_string(&p15card->tokeninfo->label, "Coolkey");
506
0
    set_string(&p15card->tokeninfo->manufacturer_id, "Unknown");
507
0
    set_string(&p15card->tokeninfo->serial_number, "00000000");
508
0
  }
509
510
  /* set pins */
511
686
  sc_log(card->ctx,  "Coolkey adding pins...");
512
1.37k
  for (i = 0; pins[i].id; i++) {
513
686
    struct sc_pkcs15_auth_info pin_info;
514
686
    struct sc_pkcs15_object   pin_obj;
515
686
    const char * label;
516
517
686
    memset(&pin_info, 0, sizeof(pin_info));
518
686
    memset(&pin_obj,  0, sizeof(pin_obj));
519
520
686
    pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
521
686
    sc_pkcs15_format_id(pins[i].id, &pin_info.auth_id);
522
686
    pin_info.attrs.pin.reference     = pins[i].ref;
523
686
    pin_info.attrs.pin.flags         = pins[i].flags;
524
686
    pin_info.attrs.pin.type          = pins[i].type;
525
686
    pin_info.attrs.pin.min_length    = pins[i].minlen;
526
686
    pin_info.attrs.pin.stored_length = pins[i].storedlen;
527
686
    pin_info.attrs.pin.max_length    = pins[i].maxlen;
528
686
    pin_info.attrs.pin.pad_char      = pins[i].pad_char;
529
686
    sc_format_path(pins[i].path, &pin_info.path);
530
686
    pin_info.tries_left    = -1;
531
532
686
    label = pins[i].label? pins[i].label : p15card->tokeninfo->label;
533
686
    sc_log(card->ctx,  "Coolkey Adding pin %d label=%s",i, label);
534
686
    strncpy(pin_obj.label, label, SC_PKCS15_MAX_LABEL_SIZE - 1);
535
686
    pin_obj.flags = pins[i].obj_flags;
536
537
686
    r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
538
686
    LOG_TEST_GOTO_ERR(card->ctx, r, "Can not add pin object.");
539
686
  }
540
541
  /* set other objects */
542
686
  r = (card->ops->card_ctl)(card, SC_CARDCTL_COOLKEY_INIT_GET_OBJECTS, &count);
543
686
  LOG_TEST_GOTO_ERR(card->ctx, r, "Can not initiate objects.");
544
545
686
  sc_log(card->ctx,  "Iterating over %d objects", count);
546
5.86k
  for (i = 0; i < count; i++) {
547
5.17k
    struct sc_cardctl_coolkey_object     coolkey_obj;
548
5.17k
    struct sc_pkcs15_object    obj_obj;
549
5.17k
    struct sc_pkcs15_cert_info cert_info = {0};
550
5.17k
    struct sc_pkcs15_pubkey_info pubkey_info = {0};
551
5.17k
    struct sc_pkcs15_prkey_info prkey_info = {0};
552
5.17k
    sc_pkcs15_pubkey_t *key = NULL;
553
5.17k
    void *obj_info = NULL;
554
5.17k
    int obj_type = 0;
555
5.17k
    CK_KEY_TYPE key_type;
556
5.17k
    CK_OBJECT_CLASS obj_class;
557
5.17k
    size_t len;
558
559
5.17k
    r = (card->ops->card_ctl)(card, SC_CARDCTL_COOLKEY_GET_NEXT_OBJECT, &coolkey_obj);
560
5.17k
    LOG_TEST_GOTO_ERR(card->ctx, r, "Can not get next object from card.");
561
5.17k
    sc_log(card->ctx, "Loading object %d", i);
562
5.17k
    memset(&obj_obj, 0, sizeof(obj_obj));
563
    /* coolkey applets have label only on the certificates,
564
     * but we should copy it also to the keys matching the same ID */
565
5.17k
    coolkey_get_attribute_bytes(card, &coolkey_obj, CKA_LABEL, (u8 *)obj_obj.label, &len, sizeof(obj_obj.label));
566
5.17k
    coolkey_get_flags(card, &coolkey_obj, &obj_obj.flags);
567
5.17k
    if (obj_obj.flags & SC_PKCS15_CO_FLAG_PRIVATE) {
568
1.09k
      sc_pkcs15_format_id(pins[0].id, &obj_obj.auth_id);
569
1.09k
    }
570
571
5.17k
    r = coolkey_get_attribute_ulong(card, &coolkey_obj, CKA_CLASS, &obj_class);
572
5.17k
    if (r < 0) {
573
3.18k
      goto fail;
574
3.18k
    }
575
1.99k
    switch (obj_class) {
576
1.33k
    case CKO_PRIVATE_KEY:
577
1.33k
      sc_log(card->ctx, "Processing private key object %d", i);
578
1.33k
      r = coolkey_get_attribute_ulong(card, &coolkey_obj, CKA_KEY_TYPE, &key_type);
579
      /* default to CKK_RSA */
580
1.33k
      if (r == SC_ERROR_DATA_OBJECT_NOT_FOUND) {
581
1.30k
        key_type = CKK_RSA;
582
1.30k
        r = SC_SUCCESS;
583
1.30k
      }
584
1.33k
      if (r < 0) {
585
12
        goto fail;
586
12
      }
587
      /* set the info values */
588
1.32k
      obj_info = &prkey_info;
589
1.32k
      coolkey_get_id(card, &coolkey_obj, &prkey_info.id);
590
1.32k
      prkey_info.path = coolkey_obj.path;
591
1.32k
      prkey_info.key_reference = (int)coolkey_obj.id;
592
1.32k
      prkey_info.native = 1;
593
1.32k
      coolkey_get_usage(card, &coolkey_obj, &prkey_info.usage);
594
1.32k
      coolkey_get_access(card, &coolkey_obj, &prkey_info.access_flags);
595
1.32k
      key = coolkey_get_public_key(p15card, &coolkey_obj, key_type);
596
1.32k
      if (key_type == CKK_RSA) {
597
1.30k
        obj_type = SC_PKCS15_TYPE_PRKEY_RSA;
598
1.30k
        if (key) {
599
0
          prkey_info.modulus_length = key->u.rsa.modulus.len*8;
600
0
        }
601
1.30k
      } else if (key_type == CKK_EC) {
602
1
        obj_type = SC_PKCS15_TYPE_PRKEY_EC;
603
1
        if (key) {
604
0
          prkey_info.field_length = key->u.ec.params.field_length;
605
0
        }
606
14
      } else {
607
14
        goto fail;
608
14
      }
609
1.30k
      break;
610
611
1.30k
    case CKO_PUBLIC_KEY:
612
131
      sc_log(card->ctx, "Processing public key object %d", i);
613
131
      r = coolkey_get_attribute_ulong(card, &coolkey_obj, CKA_KEY_TYPE, &key_type);
614
      /* default to CKK_RSA */
615
131
      if (r == SC_ERROR_DATA_OBJECT_NOT_FOUND) {
616
128
        key_type = CKK_RSA;
617
128
        r = SC_SUCCESS;
618
128
      }
619
131
      if (r < 0) {
620
2
        goto fail;
621
2
      }
622
129
      key = coolkey_get_public_key(p15card, &coolkey_obj, key_type);
623
129
      if (key == NULL) {
624
128
        goto fail;
625
128
      }
626
      /* set the info values */
627
1
      obj_info = &pubkey_info;
628
1
      r = sc_pkcs15_encode_pubkey_as_spki(card->ctx, key, &pubkey_info.direct.spki.value,
629
1
        &pubkey_info.direct.spki.len);
630
1
      if (r < 0)
631
1
        goto fail;
632
0
      coolkey_get_id(card, &coolkey_obj, &pubkey_info.id);
633
0
      pubkey_info.path = coolkey_obj.path;
634
0
      pubkey_info.native = 1;
635
0
      pubkey_info.key_reference = (int)coolkey_obj.id;
636
0
      coolkey_get_usage(card, &coolkey_obj, &pubkey_info.usage);
637
0
      coolkey_get_access(card, &coolkey_obj, &pubkey_info.access_flags);
638
0
      if (key_type == CKK_RSA) {
639
0
        obj_type = SC_PKCS15_TYPE_PUBKEY_RSA;
640
0
        pubkey_info.modulus_length = key->u.rsa.modulus.len*8;
641
0
      } else if (key_type == CKK_EC) {
642
0
        obj_type = SC_PKCS15_TYPE_PUBKEY_EC;
643
0
        pubkey_info.field_length = key->u.ec.params.field_length;
644
0
      } else {
645
0
        goto fail;
646
0
      }
647
      /* set the obj values */
648
0
      obj_obj.emulated = key;
649
0
      key = NULL;
650
0
      break;
651
652
450
    case CKO_CERTIFICATE:
653
450
      sc_log(card->ctx, "Processing certificate object %d", i);
654
450
      obj_info = &cert_info;
655
450
      coolkey_get_id(card, &coolkey_obj, &cert_info.id);
656
450
      cert_info.path = coolkey_obj.path;
657
450
      obj_type = SC_PKCS15_TYPE_CERT_X509;
658
659
      /* following will find the cached cert in cert_info */
660
450
      r = coolkey_get_certificate(card, &coolkey_obj, &cert_info.value);
661
450
      if (r < 0) {
662
111
        goto fail;
663
111
      }
664
339
      break;
665
666
667
339
    default:
668
      /* no other recognized types which are stored 'on card' */
669
77
      sc_log(card->ctx, "Unknown object type %lu, skipping", obj_class);
670
77
      continue;
671
1.99k
    }
672
673
1.64k
    r = sc_pkcs15emu_object_add(p15card, obj_type, &obj_obj, obj_info);
674
1.64k
    if (r != SC_SUCCESS)
675
0
      sc_log(card->ctx, "sc_pkcs15emu_object_add() returned %d", r);
676
5.09k
fail:
677
5.09k
    if (key) {
678
15
      sc_pkcs15_free_pubkey(key);
679
15
    }
680
5.09k
    free(cert_info.value.value);
681
5.09k
    free(pubkey_info.direct.spki.value);
682
5.09k
  }
683
686
  r = (card->ops->card_ctl)(card, SC_CARDCTL_COOLKEY_FINAL_GET_OBJECTS, &count);
684
686
  LOG_TEST_GOTO_ERR(card->ctx, r, "Can not finalize objects.");
685
686
  /* Iterate over all the created objects and fill missing labels */
687
3.01k
  for (obj = p15card->obj_list; obj != NULL; obj = obj->next) {
688
2.33k
    struct sc_pkcs15_id *id = NULL;
689
2.33k
    struct sc_pkcs15_object *cert_object;
690
691
    /* label non-empty -- do not overwrite */
692
2.33k
    if (obj->label[0] != '\0')
693
687
      continue;
694
695
1.64k
    switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
696
0
    case SC_PKCS15_TYPE_PUBKEY:
697
0
      id = &((struct sc_pkcs15_pubkey_info *)obj->data)->id;
698
0
      break;
699
1.30k
    case SC_PKCS15_TYPE_PRKEY:
700
1.30k
      id = &((struct sc_pkcs15_prkey_info *)obj->data)->id;
701
1.30k
      break;
702
339
    default:
703
      /* We do not care about other objects */
704
339
      continue;
705
1.64k
    }
706
1.30k
    r = sc_pkcs15_find_cert_by_id(p15card, id, &cert_object);
707
1.30k
    if (r != 0)
708
1.17k
      continue;
709
710
135
    sc_log(card->ctx, "Copy label \"%s\" from cert to key object",
711
135
      cert_object->label);
712
135
    memcpy(obj->label, cert_object->label, SC_PKCS15_MAX_LABEL_SIZE);
713
135
  }
714
715
686
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
716
717
0
err:
718
0
  sc_pkcs15_card_clear(p15card);
719
0
  p15card->opts.use_pin_cache = use_pin_cache_backup;
720
721
0
  LOG_FUNC_RETURN(card->ctx, r);
722
0
}
723
724
int
725
sc_pkcs15emu_coolkey_init_ex(sc_pkcs15_card_t *p15card,
726
    struct sc_aid *aid)
727
12.3k
{
728
12.3k
  sc_card_t      *card = p15card->card;
729
12.3k
  sc_context_t    *ctx = card->ctx;
730
12.3k
  int rv;
731
732
12.3k
  LOG_FUNC_CALLED(ctx);
733
734
12.3k
  rv = coolkey_detect_card(p15card);
735
12.3k
  if (rv)
736
12.3k
    LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_CARD);
737
686
  rv = sc_pkcs15emu_coolkey_init(p15card);
738
739
686
  LOG_FUNC_RETURN(ctx, rv);
740
686
}