Coverage Report

Created: 2026-08-31 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-masktech.c
Line
Count
Source
1
/*
2
 * card-masktech.c: Support for Masktech smart cards using the MTCOS operating system.
3
 *
4
 * Copyright (C) 2011-2015 MaskTech GmbH Fischerstrasse 19, 87435 Kempten, Germany
5
 * Copyright (C) 2011 Andrey Uvarov (X-Infotech) <andrejs.uvarovs@x-infotech.com>
6
 * Copyright (C) 2015 Vincent Le Toux (My Smart Logon) <vincent.letoux@mysmartlogon.com>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
#endif
26
27
#include <stdlib.h>
28
#include <string.h>
29
30
#include "internal.h"
31
#include "cardctl.h"
32
#include "iso7816.h"
33
34
static struct sc_atr_table masktech_atrs[] = {
35
  {"3B:89:80:01:4D:54:43:4F:53:70:02:00:04:31",
36
   "FF:FF:FF:FF:FF:FF:FF:FF:FF:FC:FF:FC:F4:F5" , NULL,
37
   SC_CARD_TYPE_MASKTECH_GENERIC, 0, NULL},
38
  {"3B:88:80:01:00:00:00:00:77:81:80:00:6E", "FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:EE:FF:EE", NULL,
39
   SC_CARD_TYPE_MASKTECH_GENERIC, 0, NULL},
40
  {"3B:9D:13:81:31:60:35:80:31:C0:69:4D:54:43:4F:53:73:02:00:00:40",
41
   "FF:FF:FF:FF:FF:FF:FD:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FC:F0:F0", NULL,
42
   SC_CARD_TYPE_MASKTECH_GENERIC, 0, NULL},
43
  {NULL, NULL, NULL, 0, 0, NULL}
44
};
45
46
static struct sc_card_operations *iso_ops;
47
static struct sc_card_operations masktech_ops;
48
static struct sc_card_driver masktech_drv = {
49
  "MaskTech Smart Card",
50
  "MaskTech",
51
  &masktech_ops,
52
  masktech_atrs, 0, NULL
53
};
54
55
struct masktech_private_data {
56
  /* save the key reference set at set_masktech_set_security_env to recover it as the signature step */
57
  int rsa_key_ref;
58
59
};
60
61
static int masktech_match_card(sc_card_t * card)
62
8.59k
{
63
  /* check if the ATR is in the known ATR */
64
8.59k
  if (_sc_match_atr(card, masktech_atrs, &card->type) < 0)
65
8.57k
    return 0;
66
67
23
  return 1;
68
8.59k
}
69
70
static int masktech_init(sc_card_t * card)
71
23
{
72
23
  unsigned long flags;
73
23
  struct masktech_private_data *data;
74
75
23
  sc_log(card->ctx,  "masktech_init()\n");
76
77
  /* private data kept during the live of the driver */
78
23
  if (!(data = (struct masktech_private_data *) malloc(sizeof(*data))))
79
0
    return SC_ERROR_OUT_OF_MEMORY;
80
23
  card->drv_data = data;
81
82
  /* supported RSA keys and how padding is done */
83
23
  flags = SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_HASH_NONE;
84
23
  _sc_card_add_rsa_alg(card, 1024, flags, 0);
85
23
  _sc_card_add_rsa_alg(card, 2048, flags, 0);
86
23
  _sc_card_add_rsa_alg(card, 3072, flags, 0);
87
23
  card->caps |= SC_CARD_CAP_APDU_EXT;
88
23
  return SC_SUCCESS;
89
23
}
90
91
92
static int masktech_finish(sc_card_t *card)
93
23
{
94
  /* free the private data */
95
23
  if (card->drv_data) {
96
23
    free(card->drv_data);
97
23
    card->drv_data = NULL;
98
23
  }
99
23
  return 0;
100
23
}
101
102
static int masktech_set_security_env(sc_card_t *card,
103
                                     const sc_security_env_t *env,
104
                                     int se_num)
105
0
{
106
0
  struct masktech_private_data *private_data;
107
0
  sc_log(card->ctx,  "masktech_set_security_env(), keyRef = 0x%0x, algo = 0x%0lx\n",
108
0
     *env->key_ref, env->algorithm_flags);
109
110
0
  private_data = (struct masktech_private_data *) card->drv_data;
111
0
  if (!private_data)
112
0
    return SC_ERROR_INTERNAL;
113
114
  /* save the key reference */
115
0
  if (env->flags & SC_SEC_ENV_KEY_REF_PRESENT) {
116
0
    if (env->key_ref_len != 1) {
117
0
      sc_log(card->ctx,  "Invalid key reference supplied.\n");
118
0
      return SC_ERROR_NOT_SUPPORTED;
119
0
    }
120
0
    private_data->rsa_key_ref = env->key_ref[0];
121
0
  }
122
123
0
  return iso_ops->set_security_env(card, env, se_num);
124
0
}
125
126
static int masktech_compute_signature(sc_card_t *card,
127
                                      const u8 * data,
128
                                      size_t datalen,
129
                                      u8 * out,
130
                                      size_t outlen)
131
0
{
132
133
0
  struct masktech_private_data *private_data;
134
0
  u8 sha256hash[32];
135
0
  static const u8 hdr_sha256[] = {
136
0
    0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
137
0
    0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
138
0
  };
139
0
  if (card == NULL || data == NULL || out == NULL)
140
0
    return SC_ERROR_INTERNAL;
141
0
  sc_log(card->ctx,  "masktech_compute_signature()\n");
142
143
  /* retrieve the key reference */
144
0
  private_data = (struct masktech_private_data *) card->drv_data;
145
0
  if (!private_data)
146
0
    return SC_ERROR_INTERNAL;
147
148
0
  if (private_data->rsa_key_ref == 0x88)
149
0
  {
150
    /* for this key reference, the card supports only SHA256 hash and the hash is computed using a digest info */
151
    /* check that it is a SHA256 with digest info*/
152
0
    if ((datalen != sizeof(hdr_sha256) + 32) || (memcmp(hdr_sha256, data, sizeof(hdr_sha256)) != 0))
153
0
    {
154
0
      sc_log(card->ctx,  "It is not a SHA256 with digestinfo\n");
155
0
      return SC_ERROR_NOT_SUPPORTED;
156
0
    }
157
    /* extract the SHA-256 hash */
158
0
    memcpy(sha256hash, (u8 *)(data+(datalen-32)), 32);//only last 32 byte => sha256
159
    /* default ISO 7816 functions */
160
0
    return iso_ops->compute_signature(card, sha256hash, 32, out, outlen);
161
0
  }
162
0
  else
163
0
  {
164
    /* default ISO 7816 functions */
165
0
    return iso_ops->compute_signature(card, data, datalen, out, outlen);
166
0
  }
167
0
}
168
169
static int masktech_decipher(sc_card_t *card,
170
                             const u8 * crgram,
171
                             size_t crgram_len,
172
                             u8 * out,
173
                             size_t outlen)
174
0
{
175
0
  int r;
176
0
  sc_apdu_t apdu;
177
0
  u8 rbuf[SC_MAX_EXT_APDU_BUFFER_SIZE];
178
179
0
  if (card == NULL || crgram == NULL || out == NULL)
180
0
    return SC_ERROR_INTERNAL;
181
0
  sc_log(card->ctx,  "masktech_decipher()\n");
182
183
0
  if (crgram_len > SC_MAX_EXT_APDU_BUFFER_SIZE) {
184
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
185
0
  }
186
187
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_EXT, 0x2A, 0x80, 0x86);
188
0
  apdu.resp = rbuf;
189
0
  apdu.resplen = sizeof(rbuf);
190
  /* the card doesn't support anything else here (+1 / -1 is not working) */
191
0
  apdu.le = 65536;
192
193
0
  apdu.data = crgram;
194
0
  apdu.lc = crgram_len;
195
0
  apdu.datalen = crgram_len;
196
197
0
  r = sc_transmit_apdu(card, &apdu);
198
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
199
0
  if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
200
0
    size_t len = apdu.resplen > outlen ? outlen : apdu.resplen;
201
202
0
    memcpy(out, apdu.resp, len);
203
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, (int)len);
204
0
  }
205
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, sc_check_sw(card, apdu.sw1, apdu.sw2));
206
0
}
207
208
/* unblock pin cmd */
209
static int masktech_pin_unblock(sc_card_t *card,
210
                            struct sc_pin_cmd_data *data)
211
0
{
212
0
  int rv = 0;
213
0
  struct sc_pin_cmd_data verify_data;
214
0
  struct sc_pin_cmd_data reset_data;
215
216
  /* Build a SC_PIN_CMD_VERIFY APDU on PUK */
217
0
  memset(&verify_data, 0, sizeof(verify_data));
218
0
  verify_data.cmd = SC_PIN_CMD_VERIFY;
219
0
  verify_data.pin_type = 1;
220
0
  verify_data.pin_reference = 0x83;
221
0
  verify_data.pin1 = data->pin1;
222
0
  verify_data.flags = data->flags;
223
0
  verify_data.pin1.prompt = data->pin1.prompt;
224
225
0
  rv = iso_ops->pin_cmd(card, &verify_data);
226
0
  LOG_TEST_RET(card->ctx, rv, "APDU transmit failed - verify unblock PIN");
227
228
  /* Build a SC_PIN_CMD_UNBLOCK APDU */
229
0
  memset(&reset_data, 0, sizeof(reset_data));
230
0
  reset_data.cmd = SC_PIN_CMD_UNBLOCK;
231
0
  reset_data.pin_type = 1;
232
0
  reset_data.pin_reference = 0x91;
233
  /* pin1 is set to null on purpose and flag set to implicit change
234
   => if there is a pinpad reader, do not ask for pin1 */
235
0
  reset_data.pin2 = data->pin2;
236
0
  reset_data.flags = data->flags | SC_PIN_CMD_IMPLICIT_CHANGE;
237
0
  reset_data.pin2.prompt = data->pin2.prompt;
238
239
0
  rv = iso_ops->pin_cmd(card, &reset_data);
240
0
  LOG_TEST_RET(card->ctx, rv, "APDU transmit failed - reset unblock PIN");
241
242
0
  return 0;
243
0
}
244
245
static int masktech_pin_change(sc_card_t *card,
246
                            struct sc_pin_cmd_data *data)
247
0
{
248
0
  int rv = 0;
249
0
  struct sc_pin_cmd_data verify_data;
250
0
  struct sc_pin_cmd_data change_data;
251
252
  /* Build a SC_PIN_CMD_VERIFY APDU */
253
0
  memset(&verify_data, 0, sizeof(verify_data));
254
0
  verify_data.cmd = SC_PIN_CMD_VERIFY;
255
0
  verify_data.pin_type = 1;
256
0
  verify_data.pin_reference = data->pin_reference;
257
0
  verify_data.pin1 = data->pin1;
258
0
  verify_data.flags = data->flags;
259
0
  verify_data.pin1.prompt = data->pin1.prompt;
260
261
0
  rv = iso_ops->pin_cmd(card, &verify_data);
262
0
  LOG_TEST_RET(card->ctx, rv, "APDU transmit failed - verify change PIN");
263
264
  /* Build a SC_PIN_CMD_CHANGE APDU */
265
0
  memset(&change_data, 0, sizeof(change_data));
266
0
  change_data.cmd = SC_PIN_CMD_CHANGE;
267
0
  change_data.pin_type = 1;
268
0
  change_data.pin_reference = data->pin_reference;
269
  /* pin1 is set to null on purpose and flag set to implicit change
270
   => if there is a pinpad reader, do not ask for pin1 */
271
0
  change_data.pin2 = data->pin2;
272
0
  change_data.flags = data->flags | SC_PIN_CMD_IMPLICIT_CHANGE;
273
0
  change_data.pin2.prompt = data->pin2.prompt;
274
275
0
  rv = iso_ops->pin_cmd(card, &change_data);
276
0
  LOG_TEST_RET(card->ctx, rv, "APDU transmit failed - change PIN");
277
278
0
  return 0;
279
0
}
280
281
static int masktech_pin_cmd(sc_card_t *card,
282
                            struct sc_pin_cmd_data *data)
283
17
{
284
17
  int       rv;
285
17
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
286
287
17
  switch(data->cmd)
288
17
  {
289
0
  case SC_PIN_CMD_UNBLOCK:
290
0
    rv = masktech_pin_unblock(card, data);
291
0
    break;
292
0
  case SC_PIN_CMD_CHANGE:
293
0
    rv = masktech_pin_change(card, data);
294
0
    break;
295
17
  default:
296
17
    rv = iso_ops->pin_cmd(card, data);
297
17
    break;
298
17
  }
299
17
  return rv;
300
301
302
17
}
303
304
static int masktech_get_serialnr(sc_card_t * card, sc_serial_number_t * serial)
305
17
{
306
17
  struct sc_apdu apdu;
307
17
  unsigned char apdu_resp[SC_MAX_APDU_BUFFER_SIZE-2];
308
17
  int rv;
309
310
17
  if (!serial)
311
17
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
312
313
  /* Get smart card serial number */
314
17
  card->cla = 0x80;
315
17
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0x08, 0x00, 0x00);
316
17
  apdu.resplen = sizeof(apdu_resp);
317
17
  apdu.resp = apdu_resp;
318
319
17
  rv = sc_transmit_apdu(card, &apdu);
320
17
  card->cla = 0x00;
321
322
17
  LOG_TEST_RET(card->ctx, rv, "APDU transmit failed");
323
324
16
  if (apdu.sw1 != 0x90 || apdu.sw2 != 0x00)
325
9
    return SC_ERROR_INTERNAL;
326
327
7
  if (SC_MAX_SERIALNR < apdu.resplen)
328
1
  {
329
1
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
330
1
  }
331
  /* cache serial number */
332
6
  card->serialnr.len = apdu.resplen;
333
6
  memcpy(card->serialnr.value, apdu.resp, card->serialnr.len);
334
335
  /* copy and return serial number */
336
6
  if (serial)
337
6
    memcpy(serial, &card->serialnr, sizeof(*serial));
338
339
6
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
340
6
}
341
342
343
static int masktech_card_ctl(sc_card_t * card, unsigned long cmd, void *ptr)
344
38
{
345
38
  sc_log(card->ctx,  "masktech_card_ctl()\n");
346
38
  switch (cmd) {
347
17
    case SC_CARDCTL_GET_SERIALNR:
348
17
      return masktech_get_serialnr(card, (sc_serial_number_t *) ptr);
349
21
    default:
350
21
      return SC_ERROR_NOT_SUPPORTED;
351
38
  }
352
38
}
353
354
static struct sc_card_driver *sc_get_driver(void)
355
15.7k
{
356
357
15.7k
  if (iso_ops == NULL)
358
1
    iso_ops = sc_get_iso7816_driver()->ops;
359
360
15.7k
  masktech_ops = *iso_ops;
361
362
15.7k
  masktech_ops.match_card = masktech_match_card;
363
15.7k
  masktech_ops.init = masktech_init;
364
15.7k
  masktech_ops.finish = masktech_finish;
365
15.7k
  masktech_ops.set_security_env = masktech_set_security_env;
366
15.7k
  masktech_ops.compute_signature = masktech_compute_signature;
367
15.7k
  masktech_ops.decipher = masktech_decipher;
368
15.7k
  masktech_ops.pin_cmd = masktech_pin_cmd;
369
15.7k
  masktech_ops.card_ctl = masktech_card_ctl;
370
15.7k
  return &masktech_drv;
371
15.7k
}
372
373
struct sc_card_driver *sc_get_masktech_driver(void)
374
15.7k
{
375
15.7k
  return sc_get_driver();
376
15.7k
}