Coverage Report

Created: 2026-11-12 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/sc.c
Line
Count
Source
1
/*
2
 * sc.c: 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 <stdio.h>
26
#include <ctype.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#ifdef HAVE_SYS_MMAN_H
30
#include <sys/mman.h>
31
#endif
32
#ifdef ENABLE_OPENSSL
33
#include <openssl/crypto.h>     /* for OPENSSL_cleanse */
34
#endif
35
36
37
#include "internal.h"
38
39
#ifdef PACKAGE_VERSION
40
static const char *sc_version = PACKAGE_VERSION;
41
#else
42
static const char *sc_version = "(undef)";
43
#endif
44
45
#ifdef _WIN32
46
#include <windows.h>
47
#define PAGESIZE 0
48
#else
49
#include <sys/mman.h>
50
#include <limits.h>
51
#include <unistd.h>
52
#ifndef PAGESIZE
53
#define PAGESIZE 0
54
#endif
55
#endif
56
57
const char *sc_get_version(void)
58
0
{
59
0
    return sc_version;
60
0
}
61
62
int sc_hex_to_bin(const char *in, u8 *out, size_t *outlen)
63
0
{
64
0
  const char *sc_hex_to_bin_separators = " :";
65
0
  if (in == NULL || out == NULL || outlen == NULL) {
66
0
    return SC_ERROR_INVALID_ARGUMENTS;
67
0
  }
68
69
0
  int byte_needs_nibble = 0;
70
0
  int r = SC_SUCCESS;
71
0
  size_t left = *outlen;
72
0
  u8 byte = 0;
73
0
  while (*in != '\0' && 0 != left) {
74
0
    char c = *in++;
75
0
    u8 nibble;
76
0
    if      ('0' <= c && c <= '9')
77
0
      nibble = c - '0';
78
0
    else if ('a' <= c && c <= 'f')
79
0
      nibble = c - 'a' + 10;
80
0
    else if ('A' <= c && c <= 'F')
81
0
      nibble = c - 'A' + 10;
82
0
    else {
83
0
      if (strchr(sc_hex_to_bin_separators, (int) c)) {
84
0
        if (byte_needs_nibble) {
85
0
          r = SC_ERROR_INVALID_ARGUMENTS;
86
0
          goto err;
87
0
        }
88
0
        continue;
89
0
      }
90
0
      r = SC_ERROR_INVALID_ARGUMENTS;
91
0
      goto err;
92
0
    }
93
94
0
    if (byte_needs_nibble) {
95
0
      byte |= nibble;
96
0
      *out++ = (u8) byte;
97
0
      left--;
98
0
      byte_needs_nibble = 0;
99
0
    } else {
100
0
      byte  = nibble << 4;
101
0
      byte_needs_nibble = 1;
102
0
    }
103
0
  }
104
105
0
  if (left == *outlen && 1 == byte_needs_nibble && 0 != left) {
106
    /* no output written so far, but we have a valid nibble in the upper
107
     * bits. Allow this special case. */
108
0
    *out = (u8) byte>>4;
109
0
    left--;
110
0
    byte_needs_nibble = 0;
111
0
  }
112
113
  /* for ease of implementation we only accept completely hexed bytes. */
114
0
  if (byte_needs_nibble) {
115
0
    r = SC_ERROR_INVALID_ARGUMENTS;
116
0
    goto err;
117
0
  }
118
119
  /* skip all trailing separators to see if we missed something */
120
0
  while (*in != '\0') {
121
0
    if (NULL == strchr(sc_hex_to_bin_separators, (int) *in))
122
0
      break;
123
0
    in++;
124
0
  }
125
0
  if (*in != '\0') {
126
0
    r = SC_ERROR_BUFFER_TOO_SMALL;
127
0
    goto err;
128
0
  }
129
130
0
err:
131
0
  *outlen -= left;
132
0
  return r;
133
0
}
134
135
int sc_bin_to_hex(const u8 *in, size_t in_len, char *out, size_t out_len,
136
          int in_sep)
137
0
{
138
0
  if (in == NULL || out == NULL) {
139
0
    return SC_ERROR_INVALID_ARGUMENTS;
140
0
  }
141
142
0
  if (in_sep > 0) {
143
0
    if (out_len < in_len*3 || out_len < 1)
144
0
      return SC_ERROR_BUFFER_TOO_SMALL;
145
0
  } else {
146
0
    if (out_len < in_len*2 + 1)
147
0
      return SC_ERROR_BUFFER_TOO_SMALL;
148
0
  }
149
150
0
  const char hex[] = "0123456789abcdef";
151
0
  while (in_len) {
152
0
    unsigned char value = *in++;
153
0
    *out++ = hex[(value >> 4) & 0xF];
154
0
    *out++ = hex[ value       & 0xF];
155
0
    in_len--;
156
0
    if (in_len && in_sep > 0)
157
0
      *out++ = (char)in_sep;
158
0
  }
159
0
  *out = '\0';
160
161
0
  return SC_SUCCESS;
162
0
}
163
164
/*
165
 * Right trim all non-printable characters
166
 */
167
0
size_t sc_right_trim(u8 *buf, size_t len) {
168
169
0
  size_t i;
170
171
0
  if (!buf)
172
0
    return 0;
173
174
0
  if (len > 0) {
175
0
    for(i = len-1; i > 0; i--) {
176
0
      if(!isprint(buf[i])) {
177
0
        buf[i] = '\0';
178
0
        len--;
179
0
        continue;
180
0
      }
181
0
      break;
182
0
    }
183
0
  }
184
0
  return len;
185
0
}
186
187
u8 *ulong2bebytes(u8 *buf, unsigned long x)
188
0
{
189
0
  if (buf != NULL) {
190
0
    buf[3] = (u8) (x & 0xff);
191
0
    buf[2] = (u8) ((x >> 8) & 0xff);
192
0
    buf[1] = (u8) ((x >> 16) & 0xff);
193
0
    buf[0] = (u8) ((x >> 24) & 0xff);
194
0
  }
195
0
  return buf;
196
0
}
197
198
u8 *ushort2bebytes(u8 *buf, unsigned short x)
199
0
{
200
0
  if (buf != NULL) {
201
0
    buf[1] = (u8) (x & 0xff);
202
0
    buf[0] = (u8) ((x >> 8) & 0xff);
203
0
  }
204
0
  return buf;
205
0
}
206
207
unsigned long bebytes2ulong(const u8 *buf)
208
0
{
209
0
  if (buf == NULL)
210
0
    return 0UL;
211
0
  return (unsigned long)buf[0] << 24
212
0
    | (unsigned long)buf[1] << 16
213
0
    | (unsigned long)buf[2] << 8
214
0
    | (unsigned long)buf[3];
215
0
}
216
217
unsigned short bebytes2ushort(const u8 *buf)
218
0
{
219
0
  if (buf == NULL)
220
0
    return 0U;
221
0
  return (unsigned short)buf[0] << 8
222
0
    | (unsigned short)buf[1];
223
0
}
224
225
unsigned short lebytes2ushort(const u8 *buf)
226
0
{
227
0
  if (buf == NULL)
228
0
    return 0U;
229
0
  return (unsigned short)buf[1] << 8
230
0
    | (unsigned short)buf[0];
231
0
}
232
233
unsigned long lebytes2ulong(const u8 *buf)
234
0
{
235
0
  if (buf == NULL)
236
0
    return 0UL;
237
0
  return (unsigned long)buf[3] << 24
238
0
    | (unsigned long)buf[2] << 16
239
0
    | (unsigned long)buf[1] << 8
240
0
    | (unsigned long)buf[0];
241
0
}
242
243
void set_string(char **strp, const char *value)
244
0
{
245
0
  if (strp == NULL) {
246
0
    return;
247
0
  }
248
249
0
  free(*strp);
250
0
  *strp = value ? strdup(value) : NULL;
251
0
}
252
253
void sc_init_oid(struct sc_object_id *oid)
254
13.7k
{
255
13.7k
  int ii;
256
257
13.7k
  if (!oid)
258
0
    return;
259
232k
  for (ii=0; ii<SC_MAX_OBJECT_ID_OCTETS; ii++)
260
219k
    oid->value[ii] = -1;
261
13.7k
}
262
263
int sc_format_oid(struct sc_object_id *oid, const char *in)
264
0
{
265
0
  int        ii, ret = SC_ERROR_INVALID_ARGUMENTS;
266
0
  const char *p;
267
0
  char       *q;
268
269
0
  if (oid == NULL || in == NULL)
270
0
    return SC_ERROR_INVALID_ARGUMENTS;
271
272
0
  sc_init_oid(oid);
273
274
0
  p = in;
275
0
  for (ii=0; ii < SC_MAX_OBJECT_ID_OCTETS; ii++)   {
276
0
    oid->value[ii] = (int)strtol(p, &q, 10);
277
0
    if (!*q)
278
0
      break;
279
280
0
    if (!(q[0] == '.' && isdigit((unsigned char)q[1])))
281
0
      goto out;
282
283
0
    p = q + 1;
284
0
  }
285
286
0
  if (!sc_valid_oid(oid))
287
0
    goto out;
288
289
0
  ret = SC_SUCCESS;
290
0
out:
291
0
  if (ret)
292
0
    sc_init_oid(oid);
293
294
0
  return ret;
295
0
}
296
297
int sc_compare_oid(const struct sc_object_id *oid1, const struct sc_object_id *oid2)
298
0
{
299
0
  int i;
300
301
0
  if (oid1 == NULL || oid2 == NULL) {
302
0
    return SC_ERROR_INVALID_ARGUMENTS;
303
0
  }
304
305
0
  for (i = 0; i < SC_MAX_OBJECT_ID_OCTETS; i++)   {
306
0
    if (oid1->value[i] != oid2->value[i])
307
0
      return 0;
308
0
    if (oid1->value[i] == -1)
309
0
      break;
310
0
  }
311
312
0
  return 1;
313
0
}
314
315
316
int sc_valid_oid(const struct sc_object_id *oid)
317
0
{
318
0
  int ii;
319
320
0
  if (!oid)
321
0
    return 0;
322
0
  if (oid->value[0] == -1 || oid->value[1] == -1)
323
0
    return 0;
324
0
  if (oid->value[0] > 2 || oid->value[1] > 39)
325
0
    return 0;
326
0
  for (ii=0;ii<SC_MAX_OBJECT_ID_OCTETS;ii++)
327
0
    if (oid->value[ii])
328
0
      break;
329
0
  if (ii==SC_MAX_OBJECT_ID_OCTETS)
330
0
    return 0;
331
0
  return 1;
332
0
}
333
334
335
int sc_detect_card_presence(sc_reader_t *reader)
336
0
{
337
0
  int r;
338
0
  LOG_FUNC_CALLED(reader->ctx);
339
0
  if (reader->ops->detect_card_presence == NULL)
340
0
    LOG_FUNC_RETURN(reader->ctx, SC_ERROR_NOT_SUPPORTED);
341
342
0
  r = reader->ops->detect_card_presence(reader);
343
344
  // Check that we get sane return value from backend
345
  // detect_card_presence should return 0 if no card is present.
346
0
  if (r && !(r & SC_READER_CARD_PRESENT))
347
0
    LOG_FUNC_RETURN(reader->ctx, SC_ERROR_INTERNAL);
348
349
0
  LOG_FUNC_RETURN(reader->ctx, r);
350
0
}
351
352
int sc_path_set(sc_path_t *path, int type, const u8 *id, size_t id_len,
353
  int idx, int count)
354
0
{
355
0
  if (path == NULL || id == NULL || id_len == 0 || id_len > SC_MAX_PATH_SIZE)
356
0
    return SC_ERROR_INVALID_ARGUMENTS;
357
358
0
  memset(path, 0, sizeof(*path));
359
0
  memcpy(path->value, id, id_len);
360
0
  path->len   = id_len;
361
0
  path->type  = type;
362
0
  path->index = idx;
363
0
  path->count = count;
364
365
0
  return SC_SUCCESS;
366
0
}
367
368
void sc_format_path(const char *str, sc_path_t *path)
369
0
{
370
0
  int type = SC_PATH_TYPE_PATH;
371
372
0
  if (path) {
373
0
    memset(path, 0, sizeof(*path));
374
0
    if (*str == 'i' || *str == 'I') {
375
0
      type = SC_PATH_TYPE_FILE_ID;
376
0
      str++;
377
0
    }
378
0
    path->len = sizeof(path->value);
379
0
    if (sc_hex_to_bin(str, path->value, &path->len) >= 0) {
380
0
      path->type = type;
381
0
    }
382
0
    path->count = -1;
383
0
  }
384
0
}
385
386
int sc_append_path(sc_path_t *dest, const sc_path_t *src)
387
0
{
388
0
  return sc_concatenate_path(dest, dest, src);
389
0
}
390
391
int sc_append_path_id(sc_path_t *dest, const u8 *id, size_t idlen)
392
0
{
393
0
  if (dest->len + idlen > SC_MAX_PATH_SIZE)
394
0
    return SC_ERROR_INVALID_ARGUMENTS;
395
0
  memcpy(dest->value + dest->len, id, idlen);
396
0
  dest->len += idlen;
397
0
  return SC_SUCCESS;
398
0
}
399
400
int sc_append_file_id(sc_path_t *dest, unsigned int fid)
401
0
{
402
0
  u8 id[2] = { fid >> 8, fid & 0xff };
403
404
0
  return sc_append_path_id(dest, id, 2);
405
0
}
406
407
int sc_concatenate_path(sc_path_t *d, const sc_path_t *p1, const sc_path_t *p2)
408
0
{
409
0
  sc_path_t tpath;
410
411
0
  if (d == NULL || p1 == NULL || p2 == NULL)
412
0
    return SC_ERROR_INVALID_ARGUMENTS;
413
414
0
  if (p1->type == SC_PATH_TYPE_DF_NAME || p2->type == SC_PATH_TYPE_DF_NAME)
415
    /* we do not support concatenation of AIDs at the moment */
416
0
    return SC_ERROR_NOT_SUPPORTED;
417
418
0
  if (p1->len + p2->len > SC_MAX_PATH_SIZE)
419
0
    return SC_ERROR_INVALID_ARGUMENTS;
420
421
0
  memset(&tpath, 0, sizeof(sc_path_t));
422
0
  memcpy(tpath.value, p1->value, p1->len);
423
0
  memcpy(tpath.value + p1->len, p2->value, p2->len);
424
0
  tpath.len  = p1->len + p2->len;
425
0
  tpath.type = SC_PATH_TYPE_PATH;
426
  /* use 'index' and 'count' entry of the second path object */
427
0
  tpath.index = p2->index;
428
0
  tpath.count = p2->count;
429
  /* the result is currently always as path */
430
0
  tpath.type  = SC_PATH_TYPE_PATH;
431
432
0
  *d = tpath;
433
434
0
  return SC_SUCCESS;
435
0
}
436
437
const char *sc_print_path(const sc_path_t *path)
438
0
{
439
0
  static char buffer[SC_MAX_PATH_STRING_SIZE + SC_MAX_AID_STRING_SIZE];
440
441
0
  if (sc_path_print(buffer, sizeof(buffer), path) != SC_SUCCESS)
442
0
    buffer[0] = '\0';
443
444
0
  return buffer;
445
0
}
446
447
int sc_path_print(char *buf, size_t buflen, const sc_path_t *path)
448
0
{
449
0
  size_t i;
450
451
0
  if (buf == NULL || path == NULL)
452
0
    return SC_ERROR_INVALID_ARGUMENTS;
453
454
0
  if (buflen < path->len * 2 + path->aid.len * 2 + 3)
455
0
    return SC_ERROR_BUFFER_TOO_SMALL;
456
457
0
  buf[0] = '\0';
458
0
  if (path->aid.len)   {
459
0
    for (i = 0; i < path->aid.len; i++)
460
0
      snprintf(buf + strlen(buf), buflen - strlen(buf), "%02x", path->aid.value[i]);
461
0
    snprintf(buf + strlen(buf), buflen - strlen(buf), "::");
462
0
  }
463
464
0
  for (i = 0; i < path->len; i++)
465
0
    snprintf(buf + strlen(buf), buflen - strlen(buf), "%02x", path->value[i]);
466
0
  if (!path->aid.len && path->type == SC_PATH_TYPE_DF_NAME)
467
0
    snprintf(buf + strlen(buf), buflen - strlen(buf), "::");
468
469
0
  return SC_SUCCESS;
470
0
}
471
472
int sc_compare_path(const sc_path_t *path1, const sc_path_t *path2)
473
0
{
474
0
  return path1->len == path2->len
475
0
    && !memcmp(path1->value, path2->value, path1->len);
476
0
}
477
478
int sc_compare_path_prefix(const sc_path_t *prefix, const sc_path_t *path)
479
0
{
480
0
  sc_path_t tpath;
481
482
0
  if (prefix->len > path->len)
483
0
    return 0;
484
485
0
  tpath     = *path;
486
0
  tpath.len = prefix->len;
487
488
0
  return sc_compare_path(&tpath, prefix);
489
0
}
490
491
const sc_path_t *sc_get_mf_path(void)
492
0
{
493
0
  static const sc_path_t mf_path = {
494
0
    {0x3f, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 2,
495
0
    0,
496
0
    0,
497
0
    SC_PATH_TYPE_PATH,
498
0
    {{0},0}
499
0
  };
500
0
  return &mf_path;
501
0
}
502
503
int sc_file_add_acl_entry(sc_file_t *file, unsigned int operation,
504
                          unsigned int method, unsigned long key_ref)
505
0
{
506
0
  sc_acl_entry_t *p, *_new;
507
508
0
  if (file == NULL || operation >= SC_MAX_AC_OPS) {
509
0
    return SC_ERROR_INVALID_ARGUMENTS;
510
0
  }
511
512
0
  switch (method) {
513
0
  case SC_AC_NEVER:
514
0
    sc_file_clear_acl_entries(file, operation);
515
0
    file->acl[operation] = (sc_acl_entry_t *) 1;
516
0
    return SC_SUCCESS;
517
0
  case SC_AC_NONE:
518
0
    sc_file_clear_acl_entries(file, operation);
519
0
    file->acl[operation] = (sc_acl_entry_t *) 2;
520
0
    return SC_SUCCESS;
521
0
  case SC_AC_UNKNOWN:
522
0
    sc_file_clear_acl_entries(file, operation);
523
0
    file->acl[operation] = (sc_acl_entry_t *) 3;
524
0
    return SC_SUCCESS;
525
0
  default:
526
    /* NONE and UNKNOWN get zapped when a new AC is added.
527
     * If the ACL is NEVER, additional entries will be
528
     * dropped silently. */
529
0
    if (file->acl[operation] == (sc_acl_entry_t *) 1)
530
0
      return SC_SUCCESS;
531
0
    if (file->acl[operation] == (sc_acl_entry_t *) 2
532
0
     || file->acl[operation] == (sc_acl_entry_t *) 3)
533
0
      file->acl[operation] = NULL;
534
0
  }
535
536
  /* If the entry is already present (e.g. due to the mapping)
537
   * of the card's AC with OpenSC's), don't add it again. */
538
0
  for (p = file->acl[operation]; p != NULL; p = p->next) {
539
0
    if ((p->method == method) && (p->key_ref == key_ref))
540
0
      return SC_SUCCESS;
541
0
  }
542
543
0
  _new = malloc(sizeof(sc_acl_entry_t));
544
0
  if (_new == NULL)
545
0
    return SC_ERROR_OUT_OF_MEMORY;
546
0
  _new->method = method;
547
0
  _new->key_ref = (unsigned)key_ref;
548
0
  _new->next = NULL;
549
550
0
  p = file->acl[operation];
551
0
  if (p == NULL) {
552
0
    file->acl[operation] = _new;
553
0
    return SC_SUCCESS;
554
0
  }
555
0
  while (p->next != NULL)
556
0
    p = p->next;
557
0
  p->next = _new;
558
559
0
  return SC_SUCCESS;
560
0
}
561
562
const sc_acl_entry_t * sc_file_get_acl_entry(const sc_file_t *file,
563
              unsigned int operation)
564
0
{
565
0
  sc_acl_entry_t *p;
566
0
  static const sc_acl_entry_t e_never = {
567
0
    SC_AC_NEVER, SC_AC_KEY_REF_NONE, NULL
568
0
  };
569
0
  static const sc_acl_entry_t e_none = {
570
0
    SC_AC_NONE, SC_AC_KEY_REF_NONE, NULL
571
0
  };
572
0
  static const sc_acl_entry_t e_unknown = {
573
0
    SC_AC_UNKNOWN, SC_AC_KEY_REF_NONE, NULL
574
0
  };
575
576
0
  if (file == NULL || operation >= SC_MAX_AC_OPS) {
577
0
    return NULL;
578
0
  }
579
580
0
  p = file->acl[operation];
581
0
  if (p == (sc_acl_entry_t *) 1)
582
0
    return &e_never;
583
0
  if (p == (sc_acl_entry_t *) 2)
584
0
    return &e_none;
585
0
  if (p == (sc_acl_entry_t *) 3)
586
0
    return &e_unknown;
587
588
0
  return file->acl[operation];
589
0
}
590
591
void sc_file_clear_acl_entries(sc_file_t *file, unsigned int operation)
592
0
{
593
0
  sc_acl_entry_t *e;
594
595
0
  if (file == NULL || operation >= SC_MAX_AC_OPS) {
596
0
    return;
597
0
  }
598
599
0
  e = file->acl[operation];
600
0
  if (e == (sc_acl_entry_t *) 1 ||
601
0
      e == (sc_acl_entry_t *) 2 ||
602
0
      e == (sc_acl_entry_t *) 3) {
603
0
    file->acl[operation] = NULL;
604
0
    return;
605
0
  }
606
607
0
  while (e != NULL) {
608
0
    sc_acl_entry_t *tmp = e->next;
609
0
    free(e);
610
0
    e = tmp;
611
0
  }
612
0
  file->acl[operation] = NULL;
613
0
}
614
615
sc_file_t * sc_file_new(void)
616
0
{
617
0
  sc_file_t *file = (sc_file_t *)calloc(1, sizeof(sc_file_t));
618
0
  if (file == NULL)
619
0
    return NULL;
620
621
0
  file->magic = SC_FILE_MAGIC;
622
0
  return file;
623
0
}
624
625
void sc_file_free(sc_file_t *file)
626
0
{
627
0
  unsigned int i;
628
0
  if (file == NULL || !sc_file_valid(file))
629
0
    return;
630
0
  file->magic = 0;
631
0
  for (i = 0; i < SC_MAX_AC_OPS; i++)
632
0
    sc_file_clear_acl_entries(file, i);
633
0
  if (file->sec_attr)
634
0
    free(file->sec_attr);
635
0
  if (file->prop_attr)
636
0
    free(file->prop_attr);
637
0
  if (file->type_attr)
638
0
    free(file->type_attr);
639
0
  if (file->encoded_content)
640
0
    free(file->encoded_content);
641
0
  free(file);
642
0
}
643
644
void sc_file_dup(sc_file_t **dest, const sc_file_t *src)
645
0
{
646
0
  sc_file_t *newf;
647
0
  const sc_acl_entry_t *e;
648
0
  unsigned int op;
649
650
0
  if (!dest)
651
0
    return;
652
0
  *dest = NULL;
653
0
  if (!sc_file_valid(src))
654
0
    return;
655
0
  newf = sc_file_new();
656
0
  if (newf == NULL)
657
0
    return;
658
0
  *dest = newf;
659
660
0
  memcpy(&newf->path, &src->path, sizeof(struct sc_path));
661
0
  memcpy(&newf->name, &src->name, sizeof(src->name));
662
0
  newf->namelen = src->namelen;
663
0
  newf->type    = src->type;
664
0
  newf->shareable    = src->shareable;
665
0
  newf->ef_structure = src->ef_structure;
666
0
  newf->size    = src->size;
667
0
  newf->id      = src->id;
668
0
  newf->status  = src->status;
669
0
  for (op = 0; op < SC_MAX_AC_OPS; op++) {
670
0
    newf->acl[op] = NULL;
671
0
    e = sc_file_get_acl_entry(src, op);
672
0
    if (e != NULL) {
673
0
      if (sc_file_add_acl_entry(newf, op, e->method, e->key_ref) < 0)
674
0
        goto err;
675
0
    }
676
0
  }
677
0
  newf->record_length = src->record_length;
678
0
  newf->record_count  = src->record_count;
679
680
0
  if (sc_file_set_sec_attr(newf, src->sec_attr, src->sec_attr_len) < 0)
681
0
    goto err;
682
0
  if (sc_file_set_prop_attr(newf, src->prop_attr, src->prop_attr_len) < 0)
683
0
    goto err;
684
0
  if (sc_file_set_type_attr(newf, src->type_attr, src->type_attr_len) < 0)
685
0
    goto err;
686
0
  if (sc_file_set_content(newf, src->encoded_content, src->encoded_content_len) < 0)
687
0
    goto err;
688
0
  return;
689
0
err:
690
0
  sc_file_free(newf);
691
0
  *dest = NULL;
692
0
}
693
694
int sc_file_set_sec_attr(sc_file_t *file, const u8 *sec_attr,
695
       size_t sec_attr_len)
696
0
{
697
0
  u8 *tmp;
698
0
  if (!sc_file_valid(file)) {
699
0
    return SC_ERROR_INVALID_ARGUMENTS;
700
0
  }
701
702
0
  if (sec_attr == NULL || sec_attr_len == 0) {
703
0
    if (file->sec_attr != NULL)
704
0
      free(file->sec_attr);
705
0
    file->sec_attr = NULL;
706
0
    file->sec_attr_len = 0;
707
0
    return 0;
708
0
   }
709
0
  tmp = (u8 *) realloc(file->sec_attr, sec_attr_len);
710
0
  if (!tmp) {
711
0
    if (file->sec_attr)
712
0
      free(file->sec_attr);
713
0
    file->sec_attr     = NULL;
714
0
    file->sec_attr_len = 0;
715
0
    return SC_ERROR_OUT_OF_MEMORY;
716
0
  }
717
0
  file->sec_attr = tmp;
718
0
  memcpy(file->sec_attr, sec_attr, sec_attr_len);
719
0
  file->sec_attr_len = sec_attr_len;
720
721
0
  return 0;
722
0
}
723
724
int sc_file_set_prop_attr(sc_file_t *file, const u8 *prop_attr,
725
       size_t prop_attr_len)
726
0
{
727
0
  u8 *tmp;
728
0
  if (!sc_file_valid(file)) {
729
0
    return SC_ERROR_INVALID_ARGUMENTS;
730
0
  }
731
732
0
  if (prop_attr == NULL || prop_attr_len == 0) {
733
0
    if (file->prop_attr != NULL)
734
0
      free(file->prop_attr);
735
0
    file->prop_attr = NULL;
736
0
    file->prop_attr_len = 0;
737
0
    return SC_SUCCESS;
738
0
   }
739
0
  tmp = (u8 *) realloc(file->prop_attr, prop_attr_len);
740
0
  if (!tmp) {
741
0
    if (file->prop_attr)
742
0
      free(file->prop_attr);
743
0
    file->prop_attr = NULL;
744
0
    file->prop_attr_len = 0;
745
0
    return SC_ERROR_OUT_OF_MEMORY;
746
0
  }
747
0
  file->prop_attr = tmp;
748
0
  memcpy(file->prop_attr, prop_attr, prop_attr_len);
749
0
  file->prop_attr_len = prop_attr_len;
750
751
0
  return SC_SUCCESS;
752
0
}
753
754
int sc_file_set_type_attr(sc_file_t *file, const u8 *type_attr,
755
       size_t type_attr_len)
756
0
{
757
0
  u8 *tmp;
758
0
  if (!sc_file_valid(file)) {
759
0
    return SC_ERROR_INVALID_ARGUMENTS;
760
0
  }
761
762
0
  if (type_attr == NULL || type_attr_len == 0) {
763
0
    if (file->type_attr != NULL)
764
0
      free(file->type_attr);
765
0
    file->type_attr = NULL;
766
0
    file->type_attr_len = 0;
767
0
    return SC_SUCCESS;
768
0
   }
769
0
  tmp = (u8 *) realloc(file->type_attr, type_attr_len);
770
0
  if (!tmp) {
771
0
    if (file->type_attr)
772
0
      free(file->type_attr);
773
0
    file->type_attr = NULL;
774
0
    file->type_attr_len = 0;
775
0
    return SC_ERROR_OUT_OF_MEMORY;
776
0
  }
777
0
  file->type_attr = tmp;
778
0
  memcpy(file->type_attr, type_attr, type_attr_len);
779
0
  file->type_attr_len = type_attr_len;
780
781
0
  return SC_SUCCESS;
782
0
}
783
784
785
int sc_file_set_content(sc_file_t *file, const u8 *content,
786
       size_t content_len)
787
0
{
788
0
  u8 *tmp;
789
0
  if (!sc_file_valid(file)) {
790
0
    return SC_ERROR_INVALID_ARGUMENTS;
791
0
  }
792
793
0
  if (content == NULL || content_len == 0) {
794
0
    if (file->encoded_content != NULL)
795
0
      free(file->encoded_content);
796
0
    file->encoded_content = NULL;
797
0
    file->encoded_content_len = 0;
798
0
    return SC_SUCCESS;
799
0
  }
800
801
0
  tmp = (u8 *) realloc(file->encoded_content, content_len);
802
0
  if (!tmp) {
803
0
    if (file->encoded_content)
804
0
      free(file->encoded_content);
805
0
    file->encoded_content = NULL;
806
0
    file->encoded_content_len = 0;
807
0
    return SC_ERROR_OUT_OF_MEMORY;
808
0
  }
809
810
0
  file->encoded_content = tmp;
811
0
  memcpy(file->encoded_content, content, content_len);
812
0
  file->encoded_content_len = content_len;
813
814
0
  return SC_SUCCESS;
815
0
}
816
817
818
0
int sc_file_valid(const sc_file_t *file) {
819
0
  if (file == NULL)
820
0
    return 0;
821
0
  return file->magic == SC_FILE_MAGIC;
822
0
}
823
824
int _sc_parse_atr(sc_reader_t *reader)
825
0
{
826
0
  u8 *p = reader->atr.value;
827
0
  int atr_len = (int) reader->atr.len;
828
0
  int n_hist, x;
829
0
  int tx[4] = {-1, -1, -1, -1};
830
0
  int i, FI, DI;
831
0
  const int Fi_table[] = {
832
0
    372, 372, 558, 744, 1116, 1488, 1860, -1,
833
0
    -1, 512, 768, 1024, 1536, 2048, -1, -1 };
834
0
  const int f_table[] = {
835
0
    40, 50, 60, 80, 120, 160, 200, -1,
836
0
    -1, 50, 75, 100, 150, 200, -1, -1 };
837
0
  const int Di_table[] = {
838
0
    -1, 1, 2, 4, 8, 16, 32, -1,
839
0
    12, 20, -1, -1, -1, -1, -1, -1 };
840
841
0
  reader->atr_info.hist_bytes_len = 0;
842
0
  reader->atr_info.hist_bytes = NULL;
843
844
0
  if (atr_len == 0) {
845
0
    sc_log(reader->ctx, "empty ATR - card not present?\n");
846
0
    return SC_ERROR_INTERNAL;
847
0
  }
848
849
0
  if (p[0] != 0x3B && p[0] != 0x3F) {
850
0
    sc_log(reader->ctx, "invalid sync byte in ATR: 0x%02X\n", p[0]);
851
0
    return SC_ERROR_INTERNAL;
852
0
  }
853
0
  n_hist = p[1] & 0x0F;
854
0
  x = p[1] >> 4;
855
0
  p += 2;
856
0
  atr_len -= 2;
857
0
  for (i = 0; i < 4 && atr_len > 0; i++) {
858
0
                if (x & (1 << i)) {
859
0
                        tx[i] = *p;
860
0
                        p++;
861
0
                        atr_len--;
862
0
                } else
863
0
                        tx[i] = -1;
864
0
        }
865
0
  if (tx[0] >= 0) {
866
0
    reader->atr_info.FI = FI = tx[0] >> 4;
867
0
    reader->atr_info.DI = DI = tx[0] & 0x0F;
868
0
    reader->atr_info.Fi = Fi_table[FI];
869
0
    reader->atr_info.f = f_table[FI];
870
0
    reader->atr_info.Di = Di_table[DI];
871
0
  } else {
872
0
    reader->atr_info.Fi = -1;
873
0
    reader->atr_info.f = -1;
874
0
    reader->atr_info.Di = -1;
875
0
  }
876
0
  if (tx[2] >= 0)
877
0
    reader->atr_info.N = tx[3];
878
0
  else
879
0
    reader->atr_info.N = -1;
880
0
  while (tx[3] > 0 && tx[3] & 0xF0 && atr_len > 0) {
881
0
    x = tx[3] >> 4;
882
0
    for (i = 0; i < 4 && atr_len > 0; i++) {
883
0
                  if (x & (1 << i)) {
884
0
                          tx[i] = *p;
885
0
                          p++;
886
0
                          atr_len--;
887
0
                  } else
888
0
                          tx[i] = -1;
889
0
    }
890
0
  }
891
0
  if (atr_len <= 0)
892
0
    return SC_SUCCESS;
893
0
  if (n_hist > atr_len)
894
0
    n_hist = atr_len;
895
0
  reader->atr_info.hist_bytes_len = n_hist;
896
0
  reader->atr_info.hist_bytes = p;
897
0
  return SC_SUCCESS;
898
0
}
899
900
void *sc_mem_secure_alloc(size_t len)
901
0
{
902
0
  void *p;
903
904
#ifdef _WIN32
905
  p = VirtualAlloc(NULL, len, MEM_COMMIT, PAGE_READWRITE);
906
  if (p != NULL) {
907
    VirtualLock(p, len);
908
  }
909
#else
910
0
  p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
911
0
  if (p != NULL) {
912
0
    mlock(p, len);
913
0
  }
914
0
#endif
915
916
0
  return p;
917
0
}
918
919
void sc_mem_secure_free(void *ptr, size_t len)
920
0
{
921
#ifdef _WIN32
922
  VirtualUnlock(ptr, len);
923
  VirtualFree(ptr, 0, MEM_RELEASE);
924
#else
925
0
  munlock(ptr, len);
926
0
  munmap(ptr, len);
927
0
#endif
928
0
}
929
930
void sc_mem_clear(void *ptr, size_t len)
931
0
{
932
0
  if (len > 0)   {
933
#ifdef HAVE_MEMSET_S
934
    memset_s(ptr, len, 0, len);
935
#elif _WIN32
936
    SecureZeroMemory(ptr, len);
937
#elif HAVE_EXPLICIT_BZERO
938
    explicit_bzero(ptr, len);
939
#elif ENABLE_OPENSSL
940
    OPENSSL_cleanse(ptr, len);
941
#else
942
    memset(ptr, 0, len);
943
#endif
944
0
  }
945
0
}
946
947
int sc_mem_reverse(unsigned char *buf, size_t len)
948
0
{
949
0
  unsigned char ch;
950
0
  size_t ii;
951
952
0
  if (!buf || !len)
953
0
    return SC_ERROR_INVALID_ARGUMENTS;
954
955
0
  for (ii = 0; ii < len / 2; ii++)   {
956
0
    ch = *(buf + ii);
957
0
    *(buf + ii) = *(buf + len - 1 - ii);
958
0
    *(buf + len - 1 - ii) = ch;
959
0
  }
960
961
0
  return SC_SUCCESS;
962
0
}
963
964
static int
965
sc_remote_apdu_allocate(struct sc_remote_data *rdata,
966
    struct sc_remote_apdu **new_rapdu)
967
0
{
968
0
  struct sc_remote_apdu *rapdu = NULL, *rr;
969
970
0
  if (!rdata)
971
0
    return SC_ERROR_INVALID_ARGUMENTS;
972
973
0
  rapdu = calloc(1, sizeof(struct sc_remote_apdu));
974
0
  if (rapdu == NULL)
975
0
    return SC_ERROR_OUT_OF_MEMORY;
976
977
0
  rapdu->apdu.data = &rapdu->sbuf[0];
978
0
  rapdu->apdu.resp = &rapdu->rbuf[0];
979
0
  rapdu->apdu.resplen = sizeof(rapdu->rbuf);
980
981
0
  if (new_rapdu)
982
0
    *new_rapdu = rapdu;
983
984
0
  if (rdata->data == NULL)   {
985
0
    rdata->data = rapdu;
986
0
    rdata->length = 1;
987
0
    return SC_SUCCESS;
988
0
  }
989
990
0
  for (rr = rdata->data; rr->next; rr = rr->next)
991
0
    ;
992
0
  rr->next = rapdu;
993
0
  rdata->length++;
994
995
0
  return SC_SUCCESS;
996
0
}
997
998
static void
999
sc_remote_apdu_free (struct sc_remote_data *rdata)
1000
0
{
1001
0
  struct sc_remote_apdu *rapdu = NULL;
1002
1003
0
  if (!rdata)
1004
0
    return;
1005
1006
0
  rapdu = rdata->data;
1007
0
  while(rapdu)   {
1008
0
    struct sc_remote_apdu *rr = rapdu->next;
1009
1010
0
    free(rapdu);
1011
0
    rapdu = rr;
1012
0
  }
1013
0
}
1014
1015
void sc_remote_data_init(struct sc_remote_data *rdata)
1016
0
{
1017
0
  if (!rdata)
1018
0
    return;
1019
0
  memset(rdata, 0, sizeof(struct sc_remote_data));
1020
1021
0
  rdata->alloc = sc_remote_apdu_allocate;
1022
0
  rdata->free = sc_remote_apdu_free;
1023
0
}
1024
1025
static unsigned long  sc_CRC_tab32[256];
1026
static int sc_CRC_tab32_initialized = 0;
1027
unsigned sc_crc32(const unsigned char *value, size_t len)
1028
0
{
1029
0
  size_t ii, jj;
1030
0
  unsigned long crc;
1031
0
  unsigned long index, long_c;
1032
1033
0
  if (!sc_CRC_tab32_initialized)   {
1034
0
    for (ii=0; ii<256; ii++) {
1035
0
      crc = (unsigned long) ii;
1036
0
      for (jj=0; jj<8; jj++) {
1037
0
        if ( crc & 0x00000001L )
1038
0
          crc = ( crc >> 1 ) ^ 0xEDB88320l;
1039
0
        else
1040
0
          crc =   crc >> 1;
1041
0
      }
1042
0
      sc_CRC_tab32[ii] = crc;
1043
0
    }
1044
0
    sc_CRC_tab32_initialized = 1;
1045
0
  }
1046
1047
0
  crc = 0xffffffffL;
1048
0
  for (ii=0; ii<len; ii++)   {
1049
0
    long_c = 0x000000ffL & (unsigned long) (*(value + ii));
1050
0
    index = crc ^ long_c;
1051
0
    crc = (crc >> 8) ^ sc_CRC_tab32[ index & 0xff ];
1052
0
  }
1053
1054
0
  crc ^= 0xffffffff;
1055
0
  return  crc%0xffff;
1056
0
}
1057
1058
const u8 *sc_compacttlv_find_tag(const u8 *buf, size_t len, u8 tag, size_t *outlen)
1059
0
{
1060
0
  if (buf != NULL) {
1061
0
    size_t idx;
1062
0
    u8 plain_tag = tag & 0xF0;
1063
0
    size_t expected_len = tag & 0x0F;
1064
1065
0
    for (idx = 0; idx < len; idx++) {
1066
0
      u8 ctag = buf[idx] & 0xF0;
1067
0
      size_t ctag_len = buf[idx] & 0x0F;
1068
0
      if (ctag == plain_tag && idx + ctag_len < len &&
1069
0
          (expected_len == 0 || expected_len == ctag_len)) {
1070
0
        if (outlen != NULL)
1071
0
          *outlen = ctag_len;
1072
0
        return buf + (idx + 1);
1073
0
      }
1074
0
      idx += ctag_len;
1075
0
    }
1076
0
  }
1077
0
  return NULL;
1078
0
}
1079
1080
/**************************** mutex functions ************************/
1081
1082
int sc_mutex_create(const sc_context_t *ctx, void **mutex)
1083
0
{
1084
0
  if (ctx == NULL)
1085
0
    return SC_ERROR_INVALID_ARGUMENTS;
1086
0
  if (ctx->thread_ctx != NULL && ctx->thread_ctx->create_mutex != NULL)
1087
0
    return ctx->thread_ctx->create_mutex(mutex);
1088
0
  else
1089
0
    return SC_SUCCESS;
1090
0
}
1091
1092
int sc_mutex_lock(const sc_context_t *ctx, void *mutex)
1093
0
{
1094
0
  if (ctx == NULL)
1095
0
    return SC_ERROR_INVALID_ARGUMENTS;
1096
0
  if (ctx->thread_ctx != NULL && ctx->thread_ctx->lock_mutex != NULL)
1097
0
    return ctx->thread_ctx->lock_mutex(mutex);
1098
0
  else
1099
0
    return SC_SUCCESS;
1100
0
}
1101
1102
int sc_mutex_unlock(const sc_context_t *ctx, void *mutex)
1103
0
{
1104
0
  if (ctx == NULL)
1105
0
    return SC_ERROR_INVALID_ARGUMENTS;
1106
0
  if (ctx->thread_ctx != NULL && ctx->thread_ctx->unlock_mutex != NULL)
1107
0
    return ctx->thread_ctx->unlock_mutex(mutex);
1108
0
  else
1109
0
    return SC_SUCCESS;
1110
0
}
1111
1112
int sc_mutex_destroy(const sc_context_t *ctx, void *mutex)
1113
0
{
1114
0
  if (ctx == NULL)
1115
0
    return SC_ERROR_INVALID_ARGUMENTS;
1116
0
  if (ctx->thread_ctx != NULL && ctx->thread_ctx->destroy_mutex != NULL)
1117
0
    return ctx->thread_ctx->destroy_mutex(mutex);
1118
0
  else
1119
0
    return SC_SUCCESS;
1120
0
}
1121
1122
unsigned long sc_thread_id(const sc_context_t *ctx)
1123
0
{
1124
0
  if (ctx == NULL || ctx->thread_ctx == NULL ||
1125
0
      ctx->thread_ctx->thread_id == NULL)
1126
0
    return 0UL;
1127
0
  else
1128
0
    return ctx->thread_ctx->thread_id();
1129
0
}
1130
1131
void sc_free(void *p)
1132
0
{
1133
0
  free(p);
1134
0
}