Coverage Report

Created: 2026-09-03 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/iasecc-sm.c
Line
Count
Source
1
/*
2
 * iasecc.h Support for IAS/ECC smart cards
3
 *
4
 * Copyright (C) 2010  Viktor Tarasov <vtarasov@opentrust.com>
5
 *      OpenTrust <www.opentrust.com>
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include <string.h>
23
#include <stdint.h>
24
#include <stdlib.h>
25
26
#include "internal.h"
27
#include "asn1.h"
28
#include "cardctl.h"
29
#include "common/compat_strlcpy.h"
30
31
#include "sm.h"
32
#include "iasecc.h"
33
#include "authentic.h"
34
35
36
#ifdef ENABLE_SM
37
38
static int
39
sm_save_sc_context (struct sc_card *card, struct sm_info *sm_info)
40
0
{
41
0
  struct iasecc_private_data *prv;
42
0
  struct sc_context *ctx;
43
0
  struct sc_card_cache *cache;
44
45
0
  if (!card || !sm_info)
46
0
    return SC_ERROR_INVALID_ARGUMENTS;
47
48
0
  prv = (struct iasecc_private_data *)card->drv_data;
49
0
  ctx = card->ctx;
50
0
  cache = &prv->cache;
51
52
0
  sc_log(ctx, "SM save context: cache(valid:%i,current_df:%p)", cache->valid, cache->current_df);
53
0
  if (cache->valid && cache->current_df)   {
54
0
    sm_info->current_path_df = cache->current_df->path;
55
0
    if (cache->current_df->path.type == SC_PATH_TYPE_DF_NAME)   {
56
0
      if (cache->current_df->path.aid.len)   {
57
0
        sm_info->current_aid = cache->current_df->path.aid;
58
0
      }
59
0
      else   {
60
0
        memcpy(sm_info->current_aid.value, cache->current_df->path.value, cache->current_df->path.len);
61
0
        sm_info->current_aid.len = cache->current_df->path.len;
62
0
      }
63
0
    }
64
0
  }
65
66
0
  if (cache->valid && cache->current_ef)
67
0
    sm_info->current_path_ef = cache->current_ef->path;
68
69
0
  return SC_SUCCESS;
70
0
}
71
72
73
static int
74
sm_restore_sc_context(struct sc_card *card, struct sm_info *sm_info)
75
0
{
76
0
  int rv = SC_SUCCESS;
77
78
0
  if (sm_info->current_path_df.type == SC_PATH_TYPE_DF_NAME && sm_info->current_path_df.len)
79
0
    rv = sc_select_file(card, &sm_info->current_path_df, NULL);
80
81
0
  if (sm_info->current_path_ef.len && rv == SC_SUCCESS)
82
0
    rv = sc_select_file(card, &sm_info->current_path_ef, NULL);
83
84
0
  memset(&sm_info->current_path_df, 0, sizeof(sm_info->current_path_df));
85
0
  memset(&sm_info->current_path_ef, 0, sizeof(sm_info->current_path_ef));
86
87
0
  return rv;
88
0
}
89
90
static int
91
iasecc_sm_transmit_apdus(struct sc_card *card, struct sc_remote_data *rdata,
92
    unsigned char *out, size_t *out_len)
93
0
{
94
0
  struct sc_context *ctx = card->ctx;
95
0
  struct sc_remote_apdu *rapdu = rdata->data;
96
0
  int rv = SC_SUCCESS;
97
0
  size_t offs = 0;
98
99
0
  LOG_FUNC_CALLED(ctx);
100
0
  sc_log(ctx, "iasecc_sm_transmit_apdus() rdata-length %i", rdata->length);
101
102
0
  while (rapdu)   {
103
0
    sc_log(ctx, "iasecc_sm_transmit_apdus() rAPDU flags 0x%lX", rapdu->apdu.flags);
104
0
    rv = sc_transmit_apdu(card, &rapdu->apdu);
105
0
    LOG_TEST_RET(ctx, rv, "iasecc_sm_transmit_apdus() failed to execute r-APDU");
106
0
    rv = sc_check_sw(card, rapdu->apdu.sw1, rapdu->apdu.sw2);
107
0
    if (rv < 0 && !(rapdu->flags & SC_REMOTE_APDU_FLAG_NOT_FATAL))
108
0
      LOG_TEST_RET(ctx, rv, "iasecc_sm_transmit_apdus() fatal error %i");
109
110
0
    if (out && out_len && (rapdu->flags & SC_REMOTE_APDU_FLAG_RETURN_ANSWER))   {
111
0
      size_t len = rapdu->apdu.resplen > (*out_len - offs) ? (*out_len - offs) : rapdu->apdu.resplen;
112
113
0
      memcpy(out + offs, rapdu->apdu.resp, len);
114
0
      offs += len;
115
      /* TODO: decode and gather data answers */
116
0
    }
117
118
0
    rapdu = rapdu->next;
119
0
  }
120
121
0
  if (out_len)
122
0
    *out_len = offs;
123
124
0
  LOG_FUNC_RETURN(ctx, rv);
125
0
}
126
127
128
/* Big TODO: do SM release in all handles, clean the saved card context -- current DF, EF, etc. */
129
static int
130
sm_release (struct sc_card *card, struct sc_remote_data *rdata,
131
    unsigned char *out, size_t out_len)
132
0
{
133
0
  struct sc_context *ctx = card->ctx;
134
0
  struct sm_info *sm_info = &card->sm_ctx.info;
135
0
  int rv;
136
137
0
  LOG_FUNC_CALLED(ctx);
138
0
  if (!card->sm_ctx.module.ops.finalize)
139
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
140
141
0
  rv = card->sm_ctx.module.ops.finalize(ctx, sm_info, rdata, out, out_len);
142
143
0
  sm_restore_sc_context(card, sm_info);
144
0
  LOG_FUNC_RETURN(ctx, rv);
145
0
}
146
#endif
147
148
149
int
150
iasecc_sm_external_authentication(struct sc_card *card, unsigned skey_ref, int *tries_left)
151
0
{
152
0
  struct sc_context *ctx = card->ctx;
153
0
#ifdef ENABLE_SM
154
0
  struct sm_info *sm_info = &card->sm_ctx.info;
155
0
  struct sm_cwa_session *cwa_session = &sm_info->session.cwa;
156
0
  struct sc_remote_data rdata;
157
0
  struct sc_apdu apdu;
158
0
  unsigned char sbuf[0x100];
159
0
  int rv, offs;
160
161
0
  LOG_FUNC_CALLED(ctx);
162
0
  sc_log(ctx, "iasecc_sm_external_authentication(): SKey ref %i", skey_ref);
163
164
0
  if (card->sm_ctx.sm_mode == SM_MODE_NONE)
165
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Cannot do 'External Authentication' without SM activated ");
166
167
0
  strlcpy(sm_info->config_section, card->sm_ctx.config_section, sizeof(sm_info->config_section));
168
0
  sm_info->cmd = SM_CMD_EXTERNAL_AUTH;
169
0
  sm_info->serialnr = card->serialnr;
170
0
  sm_info->card_type = card->type;
171
0
  sm_info->sm_type = SM_TYPE_CWA14890;
172
0
  cwa_session->params.crt_at.usage = IASECC_UQB_AT_EXTERNAL_AUTHENTICATION;
173
0
  cwa_session->params.crt_at.algo = IASECC_ALGORITHM_ROLE_AUTH;
174
0
  cwa_session->params.crt_at.refs[0] = skey_ref;
175
176
0
  offs = 0;
177
0
  sbuf[offs++] = IASECC_CRT_TAG_ALGO;
178
0
  sbuf[offs++] = 0x01;
179
0
  sbuf[offs++] = IASECC_ALGORITHM_ROLE_AUTH;
180
0
  sbuf[offs++] = IASECC_CRT_TAG_REFERENCE;
181
0
  sbuf[offs++] = 0x01;
182
0
  sbuf[offs++] = skey_ref;
183
184
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x81, 0xA4);
185
0
  apdu.data = sbuf;
186
0
  apdu.datalen = offs;
187
0
  apdu.lc = offs;
188
189
0
  rv = sc_transmit_apdu(card, &apdu);
190
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_external_authentication(): APDU transmit failed");
191
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
192
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_external_authentication(): set SE error");
193
194
0
  rv = sc_get_challenge(card, cwa_session->card_challenge, sizeof(cwa_session->card_challenge));
195
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_external_authentication(): set SE error");
196
197
0
  sc_remote_data_init(&rdata);
198
199
0
  if (!card->sm_ctx.module.ops.initialize)
200
0
    LOG_TEST_RET(ctx, SC_ERROR_SM_NOT_INITIALIZED, "No SM module");
201
0
  rv = card->sm_ctx.module.ops.initialize(ctx, sm_info, &rdata);
202
0
  LOG_TEST_RET(ctx, rv, "SM: INITIALIZE failed");
203
204
0
  sc_log(ctx, "sm_iasecc_external_authentication(): rdata length %i\n", rdata.length);
205
206
0
  rv = iasecc_sm_transmit_apdus (card, &rdata, NULL, 0);
207
0
  if (rv == SC_ERROR_PIN_CODE_INCORRECT && tries_left)
208
0
    *tries_left = (rdata.data + rdata.length - 1)->apdu.sw2 & 0x0F;
209
0
  LOG_TEST_RET(ctx, rv, "sm_iasecc_external_authentication(): execute failed");
210
211
0
  LOG_FUNC_RETURN(ctx, rv);
212
#else
213
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of SM and External Authentication");
214
  return SC_ERROR_NOT_SUPPORTED;
215
#endif
216
0
}
217
218
219
#ifdef ENABLE_SM
220
static int
221
iasecc_sm_se_mutual_authentication(struct sc_card *card, unsigned se_num)
222
85
{
223
85
  struct sc_context *ctx = card->ctx;
224
85
  struct sm_info *sm_info = &card->sm_ctx.info;
225
85
  struct iasecc_se_info se;
226
85
  struct sc_crt *crt =  &sm_info->session.cwa.params.crt_at;
227
85
  struct sc_apdu apdu;
228
85
  unsigned char sbuf[0x100];
229
85
  int rv, offs;
230
231
85
  memset(&se, 0, sizeof(se));
232
233
85
  se.reference = se_num;
234
85
  crt->usage = IASECC_UQB_AT_MUTUAL_AUTHENTICATION;
235
85
  crt->tag = IASECC_CRT_TAG_AT;
236
237
85
  rv = iasecc_se_get_info(card, &se);
238
85
  LOG_TEST_RET(ctx, rv, "Get SE info error");
239
240
11
  rv = iasecc_se_get_crt(card, &se, crt);
241
11
  sc_file_free(se.df);
242
11
  LOG_TEST_RET(ctx, rv, "Cannot get authentication CRT");
243
244
  /* MSE SET Mutual Authentication SK scheme */
245
0
  offs = 0;
246
0
  sbuf[offs++] = IASECC_CRT_TAG_ALGO;
247
0
  sbuf[offs++] = 0x01;
248
0
  sbuf[offs++] = crt->algo;
249
0
  sbuf[offs++] = IASECC_CRT_TAG_REFERENCE;
250
0
  sbuf[offs++] = 0x01;
251
0
  sbuf[offs++] = crt->refs[0];
252
253
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0xC1, 0xA4);
254
0
  apdu.data = sbuf;
255
0
  apdu.datalen = offs;
256
0
  apdu.lc = offs;
257
258
0
  rv = sc_transmit_apdu(card, &apdu);
259
0
  LOG_TEST_RET(ctx, rv, "SM set SE mutual auth.: APDU transmit failed");
260
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
261
0
  LOG_TEST_RET(ctx, rv, "SM set SE mutual auth.: set SE error");
262
263
0
  LOG_FUNC_RETURN(ctx, rv);
264
0
}
265
#endif
266
267
268
int
269
iasecc_sm_initialize(struct sc_card *card, unsigned se_num, unsigned cmd)
270
85
{
271
85
  struct sc_context *ctx = card->ctx;
272
85
#ifdef ENABLE_SM
273
85
  struct sm_info *sm_info = &card->sm_ctx.info;
274
85
  struct sm_cwa_session *cwa_session = &sm_info->session.cwa;
275
85
  struct sc_remote_data rdata;
276
85
  int rv;
277
278
85
  LOG_FUNC_CALLED(ctx);
279
280
85
  strlcpy(sm_info->config_section, card->sm_ctx.config_section, sizeof(sm_info->config_section));
281
85
  sm_info->cmd = cmd;
282
85
  sm_info->serialnr = card->serialnr;
283
85
  sm_info->card_type = card->type;
284
85
  sm_info->sm_type = SM_TYPE_CWA14890;
285
286
85
  rv = iasecc_sm_se_mutual_authentication(card, se_num);
287
85
  LOG_TEST_RET(ctx, rv, "iasecc_sm_initialize() MUTUAL AUTHENTICATION failed");
288
289
0
  rv = sc_get_challenge(card, cwa_session->card_challenge, SM_SMALL_CHALLENGE_LEN);
290
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_initialize() GET CHALLENGE failed");
291
292
0
  sc_remote_data_init(&rdata);
293
294
0
  rv = sm_save_sc_context(card, sm_info);
295
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_initialize() cannot save current context");
296
297
0
  if (!card->sm_ctx.module.ops.initialize)
298
0
    LOG_TEST_RET(ctx, SC_ERROR_SM_NOT_INITIALIZED, "iasecc_sm_initialize() no SM module");
299
0
  rv = card->sm_ctx.module.ops.initialize(ctx, sm_info, &rdata);
300
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_initialize() INITIALIZE failed");
301
302
303
0
  if (rdata.length == 1)   {
304
0
    rdata.data->flags |= SC_REMOTE_APDU_FLAG_RETURN_ANSWER;
305
0
    rdata.data->apdu.flags &= ~SC_APDU_FLAGS_NO_GET_RESP;
306
0
  }
307
0
  else   {
308
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "TODO: SM init with more then one APDU");
309
0
  }
310
311
0
  cwa_session->mdata_len = sizeof(cwa_session->mdata);
312
0
  rv = iasecc_sm_transmit_apdus (card, &rdata, cwa_session->mdata, &cwa_session->mdata_len);
313
0
  if (rv == SC_ERROR_PIN_CODE_INCORRECT)
314
0
    sc_log(ctx, "SM initialization failed, %i tries left", (rdata.data + rdata.length - 1)->apdu.sw2 & 0x0F);
315
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_initialize() transmit APDUs failed");
316
317
0
  rdata.free(&rdata);
318
319
0
  sc_log(ctx, "MA data(len:%zu) '%s'", cwa_session->mdata_len,
320
0
      sc_dump_hex(cwa_session->mdata, cwa_session->mdata_len));
321
0
  if (cwa_session->mdata_len != 0x48)
322
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_DATA, "iasecc_sm_initialize() invalid MUTUAL AUTHENTICATE result data");
323
324
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
325
#else
326
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
327
  return SC_ERROR_NOT_SUPPORTED;
328
#endif
329
0
}
330
331
332
#ifdef ENABLE_SM
333
static int
334
iasecc_sm_cmd(struct sc_card *card, struct sc_remote_data *rdata)
335
0
{
336
0
#define AUTH_SM_APDUS_MAX 12
337
0
#define ENCODED_APDUS_MAX_LENGTH (AUTH_SM_APDUS_MAX * (SC_MAX_APDU_BUFFER_SIZE * 2 + 64) + 32)
338
0
  struct sc_context *ctx = card->ctx;
339
0
  struct sm_info *sm_info = &card->sm_ctx.info;
340
0
  struct sm_cwa_session *session = &sm_info->session.cwa;
341
0
  struct sc_remote_apdu *rapdu = NULL;
342
0
  int rv;
343
344
0
  LOG_FUNC_CALLED(ctx);
345
0
  if (!card->sm_ctx.module.ops.get_apdus)
346
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
347
348
0
  rv =  card->sm_ctx.module.ops.get_apdus(ctx, sm_info, session->mdata, session->mdata_len, rdata);
349
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_cmd() 'GET APDUS' failed");
350
351
0
  sc_log(ctx, "iasecc_sm_cmd() %i remote APDUs to transmit", rdata->length);
352
0
  for (rapdu = rdata->data; rapdu; rapdu = rapdu->next)   {
353
0
    struct sc_apdu *apdu = &rapdu->apdu;
354
355
0
    sc_log(ctx, "iasecc_sm_cmd() apdu->ins:0x%X, resplen %zu", apdu->ins, apdu->resplen);
356
0
    if (!apdu->ins)
357
0
      break;
358
0
    rv = sc_transmit_apdu(card, apdu);
359
0
    if (rv < 0)   {
360
0
      sc_log(ctx, "iasecc_sm_cmd() APDU transmit error rv:%i", rv);
361
0
      break;
362
0
    }
363
364
0
    rv = sc_check_sw(card, apdu->sw1, apdu->sw2);
365
0
    if (rv < 0 && !(rapdu->flags & SC_REMOTE_APDU_FLAG_NOT_FATAL))   {
366
0
      sc_log(ctx, "iasecc_sm_cmd() APDU error rv:%i", rv);
367
0
      break;
368
0
    }
369
0
    sc_log(ctx, "iasecc_sm_cmd() apdu->resplen %zu", apdu->resplen);
370
0
  }
371
372
0
  LOG_FUNC_RETURN(ctx, rv);
373
0
}
374
#endif
375
376
377
int
378
iasecc_sm_rsa_generate(struct sc_card *card, unsigned se_num, struct iasecc_sdo *sdo)
379
0
{
380
0
  struct sc_context *ctx = card->ctx;
381
0
#ifdef ENABLE_SM
382
0
  struct sm_info *sm_info = &card->sm_ctx.info;
383
0
  struct sc_remote_data rdata;
384
0
  int rv;
385
386
0
  LOG_FUNC_CALLED(ctx);
387
0
  sc_log(ctx, "iasecc_sm_rsa_generate() SE#%i, SDO(class:%X,ref:%X)", se_num, sdo->sdo_class, sdo->sdo_ref);
388
389
0
  rv = iasecc_sm_initialize(card, se_num, SM_CMD_RSA_GENERATE);
390
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_rsa_generate() SM initialize failed");
391
392
0
  sm_info->cmd_data = sdo;
393
394
0
  sc_remote_data_init(&rdata);
395
0
  rv = iasecc_sm_cmd(card, &rdata);
396
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_rsa_generate() SM cmd failed");
397
398
0
  rv = sm_release (card, &rdata, NULL, 0);
399
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_rsa_generate() SM release failed");
400
401
0
  rdata.free(&rdata);
402
0
  LOG_FUNC_RETURN(ctx, rv);
403
#else
404
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
405
  return SC_ERROR_NOT_SUPPORTED;
406
#endif
407
0
}
408
409
410
int
411
iasecc_sm_rsa_update(struct sc_card *card, unsigned se_num, struct iasecc_sdo_rsa_update *udata)
412
0
{
413
0
  struct sc_context *ctx = card->ctx;
414
0
#ifdef ENABLE_SM
415
0
  struct sm_info *sm_info = &card->sm_ctx.info;
416
0
  struct sc_remote_data rdata;
417
0
  int rv;
418
419
0
  LOG_FUNC_CALLED(ctx);
420
0
  sc_log(ctx, "SM update RSA: SE#: 0x%X, SDO(class:0x%X:ref:%X)", se_num,
421
0
      udata->sdo_prv_key->sdo_class, udata->sdo_prv_key->sdo_ref);
422
423
0
  rv = iasecc_sm_initialize(card, se_num, SM_CMD_RSA_UPDATE);
424
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_rsa_update() SM initialize failed");
425
426
0
  sm_info->cmd_data = udata;
427
428
0
  sc_remote_data_init(&rdata);
429
0
  rv = iasecc_sm_cmd(card, &rdata);
430
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_rsa_update() SM cmd failed");
431
432
0
  rv = sm_release (card, &rdata, NULL, 0);
433
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_rsa_update() SM release failed");
434
435
0
  rdata.free(&rdata);
436
0
  LOG_FUNC_RETURN(ctx, rv);
437
#else
438
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
439
  return SC_ERROR_NOT_SUPPORTED;
440
#endif
441
0
}
442
443
444
int
445
iasecc_sm_pin_verify(struct sc_card *card, unsigned se_num, struct sc_pin_cmd_data *data)
446
0
{
447
0
  struct sc_context *ctx = card->ctx;
448
0
#ifdef ENABLE_SM
449
0
  struct sm_info *sm_info = &card->sm_ctx.info;
450
0
  struct sc_remote_data rdata;
451
0
  int rv;
452
453
0
  LOG_FUNC_CALLED(ctx);
454
0
  sc_log(ctx, "iasecc_sm_pin_verify() SE#%i, PIN(ref:%i,len:%zu)", se_num, data->pin_reference, data->pin1.len);
455
456
0
  rv = iasecc_sm_initialize(card, se_num, SM_CMD_PIN_VERIFY);
457
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_pin_verify() SM INITIALIZE failed");
458
459
0
  sm_info->cmd_data = data;
460
461
0
  sc_remote_data_init(&rdata);
462
0
  rv = iasecc_sm_cmd(card, &rdata);
463
0
  if (rv && rdata.length)
464
0
    if (rdata.data->apdu.sw1 == 0x63 && (rdata.data->apdu.sw2 & 0xF0) == 0xC0)
465
0
      data->pin1.tries_left = rdata.data->apdu.sw2 & 0x0F;
466
467
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_pin_verify() SM 'PIN VERIFY' failed");
468
469
0
  rv = sm_release (card, &rdata, NULL, 0);
470
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_pin_verify() SM release failed");
471
472
0
  rdata.free(&rdata);
473
0
  LOG_FUNC_RETURN(ctx, rv);
474
#else
475
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
476
  return SC_ERROR_NOT_SUPPORTED;
477
#endif
478
0
}
479
480
481
int
482
iasecc_sm_sdo_update(struct sc_card *card, unsigned se_num, struct iasecc_sdo_update *update)
483
0
{
484
0
  struct sc_context *ctx = card->ctx;
485
0
#ifdef ENABLE_SM
486
0
  struct sm_info *sm_info = &card->sm_ctx.info;
487
0
  struct sc_remote_data rdata;
488
0
  int rv;
489
490
0
  LOG_FUNC_CALLED(ctx);
491
0
  sc_log(ctx, "iasecc_sm_sdo_update() SE#%i, SDO(class:0x%X,ref:%i)", se_num, update->sdo_class, update->sdo_ref);
492
493
0
  rv = iasecc_sm_initialize(card, se_num, SM_CMD_SDO_UPDATE);
494
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_sdo_update() SM INITIALIZE failed");
495
496
0
  sc_log(ctx, "current DF '%s'", sc_print_path(&sm_info->current_path_df));
497
498
0
  sm_info->cmd_data = update;
499
500
0
  sc_remote_data_init(&rdata);
501
0
  rv = iasecc_sm_cmd(card, &rdata);
502
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_sdo_update() SM 'SDO UPDATE' failed");
503
504
0
  rv = sm_release (card, &rdata, NULL, 0);
505
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_sdo_update() SM release failed");
506
507
0
  rdata.free(&rdata);
508
0
  LOG_FUNC_RETURN(ctx, rv);
509
#else
510
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
511
  return SC_ERROR_NOT_SUPPORTED;
512
#endif
513
0
}
514
515
516
int
517
iasecc_sm_pin_reset(struct sc_card *card, unsigned se_num, struct sc_pin_cmd_data *data)
518
0
{
519
0
  struct sc_context *ctx = card->ctx;
520
0
#ifdef ENABLE_SM
521
0
  struct sm_info *sm_info = &card->sm_ctx.info;
522
0
  struct sc_remote_data rdata;
523
0
  int rv;
524
525
0
  LOG_FUNC_CALLED(ctx);
526
0
  sc_log(ctx, "iasecc_sm_pin_reset() SE#%i, PIN(ref:%i,len:%zu)", se_num, data->pin_reference, data->pin2.len);
527
528
0
  rv = iasecc_sm_initialize(card, se_num, SM_CMD_PIN_RESET);
529
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_pin_reset() SM INITIALIZE failed");
530
531
0
  sm_info->cmd_data = data;
532
533
0
  sc_remote_data_init(&rdata);
534
0
  rv = iasecc_sm_cmd(card, &rdata);
535
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_pin_reset() SM 'PIN RESET' failed");
536
537
0
  rv = sm_release (card, &rdata, NULL, 0);
538
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_pin_reset() SM release failed");
539
540
0
  rdata.free(&rdata);
541
0
  LOG_FUNC_RETURN(ctx, rv);
542
#else
543
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
544
  return SC_ERROR_NOT_SUPPORTED;
545
#endif
546
0
}
547
548
549
int
550
iasecc_sm_create_file(struct sc_card *card, unsigned se_num, unsigned char *fcp, size_t fcp_len)
551
1
{
552
1
  struct sc_context *ctx = card->ctx;
553
1
#ifdef ENABLE_SM
554
1
  struct sm_info *sm_info = &card->sm_ctx.info;
555
1
  struct sc_remote_data rdata;
556
1
  struct iasecc_sm_cmd_create_file cmd_data;
557
1
  int rv;
558
559
1
  LOG_FUNC_CALLED(ctx);
560
1
  sc_log(ctx, "iasecc_sm_create_file() SE#%i, fcp(%zu) '%s'", se_num, fcp_len, sc_dump_hex(fcp, fcp_len));
561
562
1
  rv = iasecc_sm_initialize(card, se_num, SM_CMD_FILE_CREATE);
563
1
  LOG_TEST_RET(ctx, rv, "iasecc_sm_create_file() SM INITIALIZE failed");
564
565
0
  cmd_data.data = fcp;
566
0
  cmd_data.size = fcp_len;
567
0
  sm_info->cmd_data = &cmd_data;
568
569
0
  sc_remote_data_init(&rdata);
570
0
  rv= iasecc_sm_cmd(card, &rdata);
571
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_create_file() SM 'UPDATE BINARY' failed");
572
573
0
  rv = sm_release (card, &rdata, NULL, 0);
574
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_create_file() SM release failed");
575
576
0
  rdata.free(&rdata);
577
0
  LOG_FUNC_RETURN(ctx, rv);
578
#else
579
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
580
  return SC_ERROR_NOT_SUPPORTED;
581
#endif
582
0
}
583
584
int
585
iasecc_sm_read_binary(struct sc_card *card, unsigned se_num, size_t offs, unsigned char *buff, size_t count)
586
83
{
587
83
  struct sc_context *ctx = card->ctx;
588
83
#ifdef ENABLE_SM
589
83
  struct sm_info *sm_info = &card->sm_ctx.info;
590
83
  struct sc_remote_data rdata;
591
83
  struct iasecc_sm_cmd_update_binary cmd_data;
592
83
  int rv;
593
594
83
  LOG_FUNC_CALLED(ctx);
595
83
  sc_log(ctx, "SM read binary: acl:%X, offs:%zu, count:%zu", se_num, offs, count);
596
597
83
  rv = iasecc_sm_initialize(card, se_num, SM_CMD_FILE_READ);
598
83
  LOG_TEST_RET(ctx, rv, "iasecc_sm_read_binary() SM INITIALIZE failed");
599
600
0
  cmd_data.offs = offs;
601
0
  cmd_data.count = count;
602
0
  sm_info->cmd_data = &cmd_data;
603
604
0
  sc_remote_data_init(&rdata);
605
0
  rv = iasecc_sm_cmd(card, &rdata);
606
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_read_binary() SM 'READ BINARY' failed");
607
608
0
  sc_log(ctx, "IAS/ECC decode answer() rdata length %i", rdata.length);
609
610
0
  rv = sm_release (card, &rdata, buff, count);
611
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_read_binary() SM release failed");
612
613
0
  rdata.free(&rdata);
614
0
  LOG_FUNC_RETURN(ctx, rv);
615
#else
616
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
617
  return SC_ERROR_NOT_SUPPORTED;
618
#endif
619
0
}
620
621
622
int
623
iasecc_sm_update_binary(struct sc_card *card, unsigned se_num, size_t offs,
624
    const unsigned char *buff, size_t count)
625
0
{
626
0
  struct sc_context *ctx = card->ctx;
627
0
#ifdef ENABLE_SM
628
0
  struct sm_info *sm_info = &card->sm_ctx.info;
629
0
  struct sc_remote_data rdata;
630
0
  struct iasecc_sm_cmd_update_binary cmd_data;
631
0
  int rv;
632
633
0
  LOG_FUNC_CALLED(ctx);
634
0
  sc_log(ctx, "SM update binary: acl:%X, offs:%zu, count:%zu", se_num, offs, count);
635
636
0
  rv = iasecc_sm_initialize(card, se_num, SM_CMD_FILE_UPDATE);
637
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_update_binary() SM INITIALIZE failed");
638
639
0
  cmd_data.offs = offs;
640
0
  cmd_data.count = count;
641
0
  cmd_data.data = buff;
642
0
  sm_info->cmd_data = &cmd_data;
643
644
0
  sc_remote_data_init(&rdata);
645
0
  rv = iasecc_sm_cmd(card, &rdata);
646
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_update_binary() SM 'UPDATE BINARY' failed");
647
648
0
  rv = sm_release (card, &rdata, NULL, 0);
649
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_update_binary() SM release failed");
650
651
0
  rdata.free(&rdata);
652
0
  LOG_FUNC_RETURN(ctx, (int)count);
653
#else
654
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
655
  return SC_ERROR_NOT_SUPPORTED;
656
#endif
657
0
}
658
659
660
int
661
iasecc_sm_delete_file(struct sc_card *card, unsigned se_num, unsigned int file_id)
662
1
{
663
1
  struct sc_context *ctx = card->ctx;
664
1
#ifdef ENABLE_SM
665
1
  struct sm_info *sm_info = &card->sm_ctx.info;
666
1
  struct sc_remote_data rdata;
667
1
  int rv;
668
669
1
  LOG_FUNC_CALLED(ctx);
670
1
  sc_log(ctx, "SM delete file: SE#:%X, file-id:%X", se_num, file_id);
671
672
1
  rv = iasecc_sm_initialize(card, se_num, SM_CMD_FILE_DELETE);
673
1
  LOG_TEST_RET(ctx, rv, "iasecc_sm_delete_file() SM INITIALIZE failed");
674
675
0
  sm_info->cmd_data = (void *)(uintptr_t)file_id;
676
677
0
  sc_remote_data_init(&rdata);
678
0
  rv = iasecc_sm_cmd(card, &rdata);
679
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_delete_file() SM 'FILE DELETE' failed");
680
681
0
  rv = sm_release (card, &rdata, NULL, 0);
682
0
  LOG_TEST_RET(ctx, rv, "iasecc_sm_delete_file() SM release failed");
683
684
0
  rdata.free(&rdata);
685
0
  LOG_FUNC_RETURN(ctx, rv);
686
#else
687
  LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "built without support of Secure-Messaging");
688
  return SC_ERROR_NOT_SUPPORTED;
689
#endif
690
0
}
691
692