Coverage Report

Created: 2026-11-30 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/ef-gdo.c
Line
Count
Source
1
/*
2
 * ef-atr.c: Stuff for handling EF(GDO)
3
 *
4
 * Copyright (C) 2017  Frank Morgner <frankmorgner@gmail.com>
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
#ifdef HAVE_CONFIG_H
21
#include "config.h"
22
#endif
23
24
#include "asn1.h"
25
#include "internal.h"
26
#include <stdlib.h>
27
#include <string.h>
28
29
static int
30
sc_parse_ef_gdo_content(const unsigned char *gdo, size_t gdo_len,
31
    unsigned char *iccsn, size_t *iccsn_len,
32
    unsigned char *chn, size_t *chn_len)
33
210
{
34
210
  int r = SC_SUCCESS, iccsn_found = 0, chn_found = 0;
35
210
  const unsigned char *p = gdo;
36
210
  size_t left = gdo_len;
37
38
210
  if (!gdo)
39
0
    return SC_ERROR_INVALID_ARGUMENTS;
40
41
511
  while (left >= 2) {
42
320
    unsigned int cla, tag;
43
320
    size_t tag_len;
44
45
320
    r = sc_asn1_read_tag(&p, left, &cla, &tag, &tag_len);
46
320
    if (r != SC_SUCCESS) {
47
12
      if (r == SC_ERROR_ASN1_END_OF_CONTENTS) {
48
        /* not enough data */
49
8
        r = SC_SUCCESS;
50
8
      }
51
12
      break;
52
12
    }
53
308
    if (p == NULL) {
54
      /* done parsing */
55
7
      break;
56
7
    }
57
301
    if (tag_len > (size_t)(gdo + gdo_len - p)) {
58
0
      r = SC_ERROR_INVALID_DATA;
59
0
      break;
60
0
    }
61
62
301
    if (cla == SC_ASN1_TAG_APPLICATION) {
63
205
      switch (tag) {
64
56
        case 0x1A:
65
56
          iccsn_found = 1;
66
56
          if (iccsn && iccsn_len) {
67
56
            memcpy(iccsn, p, MIN(tag_len, *iccsn_len));
68
56
            *iccsn_len = MIN(tag_len, *iccsn_len);
69
56
          }
70
56
          break;
71
56
        case 0x1F20:
72
56
          chn_found = 1;
73
56
          if (chn && chn_len) {
74
0
            memcpy(chn, p, MIN(tag_len, *chn_len));
75
0
            *chn_len = MIN(tag_len, *chn_len);
76
0
          }
77
56
          break;
78
205
      }
79
205
    }
80
81
301
    p += tag_len;
82
301
    left = gdo_len - (p - gdo);
83
301
  }
84
85
210
  if (!iccsn_found && iccsn_len)
86
194
    *iccsn_len = 0;
87
210
  if (!chn_found && chn_len)
88
0
    *chn_len = 0;
89
90
210
  return r;
91
210
}
92
93
94
95
int
96
sc_parse_ef_gdo(struct sc_card *card,
97
    unsigned char *iccsn, size_t *iccsn_len,
98
    unsigned char *chn, size_t *chn_len)
99
769
{
100
769
  struct sc_context *ctx;
101
769
  struct sc_path path;
102
769
  struct sc_file *file = NULL;
103
769
  unsigned char *gdo = NULL;
104
769
  size_t gdo_len = 0;
105
769
  int r;
106
107
769
  if (!card)
108
0
    return SC_ERROR_INVALID_ARGUMENTS;
109
110
769
  ctx = card->ctx;
111
112
769
  LOG_FUNC_CALLED(ctx);
113
114
769
  sc_format_path("3F002F02", &path);
115
769
  r = sc_select_file(card, &path, &file);
116
769
  LOG_TEST_GOTO_ERR(ctx, r, "Cannot select EF(GDO) file");
117
118
221
  if (file && file->size) {
119
1
    gdo_len = file->size;
120
220
  } else {
121
220
    gdo_len = 64;
122
220
  }
123
221
  gdo = malloc(gdo_len);
124
221
  if (!gdo) {
125
0
    r = SC_ERROR_OUT_OF_MEMORY;
126
0
    goto err;
127
0
  }
128
129
221
  r = sc_read_binary(card, 0, gdo, gdo_len, 0);
130
221
  LOG_TEST_GOTO_ERR(ctx, r, "Cannot read EF(GDO) file");
131
132
210
  r = sc_parse_ef_gdo_content(gdo, r, iccsn, iccsn_len, chn, chn_len);
133
134
769
err:
135
769
  sc_file_free(file);
136
769
  free(gdo);
137
138
769
  LOG_FUNC_RETURN(ctx, r);
139
769
}