Coverage Report

Created: 2026-01-10 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-myeid.c
Line
Count
Source
1
/*
2
 * card-myeid.c
3
 *
4
 * Copyright (C) 2008-2019 Aventra Ltd.
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 <string.h>
26
#include <stdlib.h>
27
28
#include "internal.h"
29
#include "asn1.h"
30
#include "cardctl.h"
31
#include "types.h"
32
33
/* Low byte is the MyEID card's key type specific component ID. High byte is used
34
 * internally for key type, so myeid_loadkey() is aware of the exact component. */
35
142
#define LOAD_KEY_MODULUS    0x0080
36
71
#define LOAD_KEY_PUBLIC_EXPONENT  0x0081
37
0
#define LOAD_KEY_PRIME_P    0x0083
38
0
#define LOAD_KEY_PRIME_Q    0x0084
39
0
#define LOAD_KEY_DP1      0x0085
40
0
#define LOAD_KEY_DQ1      0x0086
41
0
#define LOAD_KEY_INVQ     0x0087
42
0
#define LOAD_KEY_EC_PUBLIC    0x1086
43
0
#define LOAD_KEY_EC_PRIVATE   0x1087
44
71
#define LOAD_KEY_SYMMETRIC    0x20a0
45
46
#define MYEID_CARD_NAME_MAX_LEN   100
47
48
/* The following flags define the features supported by the card currently in use.
49
  They are used in 'card_supported_features' field in myeid_card_caps struct */
50
741
#define MYEID_CARD_CAP_RSA    0x01
51
741
#define MYEID_CARD_CAP_3DES   0x02
52
741
#define MYEID_CARD_CAP_AES    0x04
53
#define MYEID_CARD_CAP_ECC    0x08
54
#define MYEID_CARD_CAP_GRIDPIN    0x10
55
#define MYEID_CARD_CAP_PIV_EMU    0x20
56
57
0
#define MYEID_MAX_APDU_DATA_LEN   0xFF
58
0
#define MYEID_MAX_RSA_KEY_LEN   4096
59
60
#define MYEID_MAX_EXT_APDU_BUFFER_SIZE  (MYEID_MAX_RSA_KEY_LEN/8+16)
61
62
static const char *myeid_card_name = "MyEID";
63
static const char *oseid_card_name = "OsEID";
64
static char card_name_buf[MYEID_CARD_NAME_MAX_LEN];
65
66
static struct sc_card_operations myeid_ops;
67
static struct sc_card_driver myeid_drv = {
68
  "MyEID cards with PKCS#15 applet",
69
  "myeid",
70
  &myeid_ops,
71
  NULL,
72
  0,
73
  NULL
74
};
75
76
typedef struct myeid_private_data {
77
  int card_state;
78
79
  unsigned short change_counter;
80
  unsigned char cap_chaining;
81
  /* the driver sets sec_env pointer in myeid_set_security_env and
82
   it is used immediately in myeid_decipher to differentiate between RSA decryption and
83
   ECDH key agreement. Note that this pointer is usually not valid
84
   after this pair of calls and must not be used elsewhere. */
85
  const struct sc_security_env* sec_env;
86
  int disable_hw_pkcs1_padding;
87
  /* buffers for AES(DES) block cipher */
88
  uint8_t sym_crypt_buffer[16];
89
  uint8_t sym_plain_buffer[16];
90
  uint8_t sym_crypt_buffer_len;
91
  uint8_t sym_plain_buffer_len;
92
  /* PSO for AES/DES need algo+flags from sec env */
93
  unsigned long algorithm, algorithm_flags;
94
} myeid_private_data_t;
95
96
typedef struct myeid_card_caps {
97
  unsigned char card_caps_ver;
98
  unsigned short card_supported_features;
99
  unsigned short max_rsa_key_length;
100
  unsigned short max_des_key_length;
101
  unsigned short max_aes_key_length;
102
  unsigned short max_ecc_key_length;
103
} myeid_card_caps_t;
104
105
static struct myeid_supported_ec_curves {
106
  char *curve_name;
107
  struct sc_object_id curve_oid;
108
  size_t size;
109
} ec_curves[] = {
110
  {"secp192r1", {{1, 2, 840, 10045, 3, 1, 1, -1}},192},
111
  /* {"secp224r1", {{1, 3, 132, 0, 33, -1}},    224}, */
112
  {"secp256r1", {{1, 2, 840, 10045, 3, 1, 7, -1}},256},
113
  {"secp384r1", {{1, 3, 132, 0, 34, -1}},   384},
114
  {"secp521r1", {{1, 3, 132, 0, 35, -1}},   521},
115
  {NULL, {{-1}}, 0},
116
};
117
118
static int myeid_get_info(struct sc_card *card, u8 *rbuf, size_t buflen);
119
static int myeid_get_card_caps(struct sc_card *card, myeid_card_caps_t* card_caps);
120
121
static int myeid_match_card(struct sc_card *card)
122
13.6k
{
123
13.6k
  size_t len = card->reader->atr_info.hist_bytes_len;
124
  /* Normally the historical bytes are exactly "MyEID", but there might
125
   * be some historic units which have a small prefix byte sequence. */
126
13.6k
  if (len >= 5) {
127
3.77k
    if (!memcmp(&card->reader->atr_info.hist_bytes[len - 5], "MyEID", 5)) {
128
1.41k
      sc_log(card->ctx, "Matched MyEID card");
129
1.41k
      card->type = SC_CARD_TYPE_MYEID_GENERIC;
130
1.41k
      return 1;
131
1.41k
    }
132
    /* The software implementation of MyEID is identified by OsEID bytes */
133
2.35k
    if (!memcmp(&card->reader->atr_info.hist_bytes[len - 5], "OsEID", 5)) {
134
172
      sc_log(card->ctx, "Matched OsEID card");
135
172
      card->type = SC_CARD_TYPE_MYEID_OSEID;
136
172
      return 1;
137
172
    }
138
2.35k
  }
139
12.0k
  return 0;
140
13.6k
}
141
142
static int myeid_load_options(sc_context_t *ctx, myeid_private_data_t *priv)
143
1.59k
{
144
1.59k
  int r;
145
1.59k
  size_t i, j;
146
1.59k
  scconf_block **found_blocks, *block;
147
148
1.59k
  if (!ctx || !priv) {
149
0
    r = SC_ERROR_INTERNAL;
150
0
    goto err;
151
0
  }
152
1.59k
  priv->disable_hw_pkcs1_padding = 0;
153
3.18k
  for (i = 0; ctx->conf_blocks[i]; i++) {
154
1.59k
    found_blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i],
155
1.59k
        "card_driver", "myeid");
156
1.59k
    if (!found_blocks)
157
0
      continue;
158
1.59k
    for (j = 0, block = found_blocks[j]; block; j++, block = found_blocks[j]) {
159
0
      priv->disable_hw_pkcs1_padding = scconf_get_int(block, "disable_hw_pkcs1_padding", 0);
160
0
      sc_log(ctx,"Found config option: disable_hw_pkcs1_padding = %d\n", priv->disable_hw_pkcs1_padding);
161
0
    }
162
1.59k
    free(found_blocks);
163
1.59k
  }
164
1.59k
  r = SC_SUCCESS;
165
166
1.59k
err:
167
1.59k
  return r;
168
1.59k
}
169
170
static int myeid_init(struct sc_card *card)
171
1.59k
{
172
1.59k
  unsigned long flags = 0, ext_flags = 0;
173
1.59k
  myeid_private_data_t *priv;
174
1.59k
  u8 appletInfo[20];
175
1.59k
  size_t appletInfoLen;
176
1.59k
  myeid_card_caps_t card_caps;
177
1.59k
  static struct sc_aid myeid_aid = { "\xA0\x00\x00\x00\x63\x50\x4B\x43\x53\x2D\x31\x35", 0x0C };
178
1.59k
  int rv = 0;
179
1.59k
  void *old_drv_data = card->drv_data;
180
181
1.59k
  LOG_FUNC_CALLED(card->ctx);
182
183
1.59k
  switch (card->type) {
184
172
  case SC_CARD_TYPE_MYEID_OSEID:
185
172
    card->name = oseid_card_name;
186
172
    break;
187
1.41k
  case SC_CARD_TYPE_MYEID_GENERIC:
188
1.41k
    card->name = myeid_card_name;
189
1.41k
    break;
190
0
  default:
191
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_CARD);
192
1.59k
  }
193
194
1.59k
  priv = calloc(1, sizeof(myeid_private_data_t));
195
196
1.59k
  if (!priv)
197
1.59k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
198
199
1.59k
  rv = myeid_load_options (card->ctx, priv);
200
1.59k
  LOG_TEST_GOTO_ERR(card->ctx, rv, "Unable to read options from opensc.conf");
201
202
1.59k
  priv->card_state = SC_FILE_STATUS_CREATION;
203
1.59k
  card->drv_data = priv;
204
205
  /* Ensure that the MyEID applet is selected. */
206
1.59k
  rv = iso7816_select_aid(card, myeid_aid.value, myeid_aid.len, NULL, NULL);
207
1.59k
  LOG_TEST_GOTO_ERR(card->ctx, rv, "Failed to select MyEID applet.");
208
209
  /* find out MyEID version */
210
211
1.57k
  appletInfoLen = 20;
212
213
1.57k
  if (0 > myeid_get_info(card, appletInfo, appletInfoLen))
214
1.57k
    LOG_TEST_GOTO_ERR(card->ctx, SC_ERROR_INVALID_CARD, "Failed to get MyEID applet information.");
215
216
741
  priv->change_counter = appletInfo[19] | appletInfo[18] << 8;
217
218
741
  memset(&card_caps, 0, sizeof(myeid_card_caps_t));
219
741
  card_caps.max_ecc_key_length = 256;
220
741
  card_caps.max_rsa_key_length = 2048;
221
222
741
  if (card->version.fw_major >= 40) {
223
      /* Since 4.0, we can query available algorithms and key sizes.
224
       * Since 3.5.0 RSA up to 2048 and ECC up to 256 are always supported, so we check only max ECC key length. */
225
633
      if (myeid_get_card_caps(card, &card_caps) != SC_SUCCESS) {
226
261
      sc_log(card->ctx, "Failed to get card capabilities. Using default max ECC key length 256.");
227
261
      }
228
633
  }
229
230
741
  flags = SC_ALGORITHM_RSA_RAW | SC_ALGORITHM_ONBOARD_KEY_GEN;
231
741
  if (priv->disable_hw_pkcs1_padding == 0)
232
741
    flags |= SC_ALGORITHM_RSA_PAD_PKCS1;
233
741
  flags |= SC_ALGORITHM_RSA_HASH_NONE;
234
235
741
  _sc_card_add_rsa_alg(card,  512, flags, 0);
236
741
  _sc_card_add_rsa_alg(card,  768, flags, 0);
237
741
  _sc_card_add_rsa_alg(card, 1024, flags, 0);
238
741
  _sc_card_add_rsa_alg(card, 1536, flags, 0);
239
741
  _sc_card_add_rsa_alg(card, 2048, flags, 0);
240
241
741
  if (card_caps.card_supported_features & MYEID_CARD_CAP_RSA) {
242
352
    if (card_caps.max_rsa_key_length >= 3072)
243
281
      _sc_card_add_rsa_alg(card, 3072, flags, 0);
244
352
    if (card_caps.max_rsa_key_length >= 4096)
245
277
      _sc_card_add_rsa_alg(card, 4096, flags, 0);
246
352
  }
247
248
  /* show ECC algorithms if the applet version of the inserted card supports them */
249
741
  if (card->version.fw_major >= 35) {
250
673
    int i;
251
252
673
    flags = SC_ALGORITHM_ECDSA_RAW | SC_ALGORITHM_ECDH_CDH_RAW | SC_ALGORITHM_ONBOARD_KEY_GEN;
253
673
    flags |= SC_ALGORITHM_ECDSA_HASH_NONE;
254
673
    ext_flags = SC_ALGORITHM_EXT_EC_NAMEDCURVE | SC_ALGORITHM_EXT_EC_UNCOMPRESES;
255
256
3.36k
    for (i=0; ec_curves[i].curve_name != NULL; i++) {
257
2.69k
      if (card_caps.max_ecc_key_length >= ec_curves[i].size)
258
2.02k
        _sc_card_add_ec_alg(card, ec_curves[i].size, flags, ext_flags, &ec_curves[i].curve_oid);
259
2.69k
    }
260
673
  }
261
262
  /* show supported symmetric algorithms */
263
741
  flags = 0;
264
741
  if (card_caps.card_supported_features & MYEID_CARD_CAP_3DES) {
265
348
    if (card_caps.max_des_key_length >= 64)
266
323
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_DES, 64, flags);
267
348
    if (card_caps.max_des_key_length >= 128)
268
315
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_3DES, 128, flags);
269
348
    if (card_caps.max_des_key_length >= 192)
270
304
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_3DES, 192, flags);
271
348
  }
272
741
  if (card_caps.card_supported_features & MYEID_CARD_CAP_AES) {
273
352
    if (card_caps.max_aes_key_length >= 128)
274
341
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_AES, 128, flags);
275
352
    if (card_caps.max_aes_key_length >= 256)
276
334
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_AES, 256, flags);
277
352
  }
278
279
  /* State that we have an RNG */
280
741
  card->caps |= SC_CARD_CAP_RNG | SC_CARD_CAP_ISO7816_PIN_INFO;
281
282
741
  if ((card->version.fw_major == 40 && card->version.fw_minor >= 10 )
283
735
    || card->version.fw_major >= 41)
284
622
    card->caps |= SC_CARD_CAP_WRAP_KEY | SC_CARD_CAP_UNWRAP_KEY
285
622
         | SC_CARD_CAP_ONCARD_SESSION_OBJECTS;
286
287
741
  if (card->version.fw_major >= 45)
288
614
    priv->cap_chaining = 1;
289
741
  if (card->version.fw_major >= 40)
290
633
    card->max_recv_size = 256;
291
108
  else
292
108
    card->max_recv_size = 255;
293
741
  card->max_send_size = 255;
294
295
741
  rv = SC_SUCCESS;
296
297
1.59k
err:
298
1.59k
  if (rv < 0) {
299
15
    free(priv);
300
15
    card->drv_data = old_drv_data;
301
15
  }
302
303
1.59k
  LOG_FUNC_RETURN(card->ctx, rv);
304
1.59k
}
305
306
static const struct sc_card_operations *iso_ops = NULL;
307
308
static int acl_to_byte(const struct sc_acl_entry *e)
309
5.62k
{
310
5.62k
  if (NULL == e)
311
11
    return 0x00;
312
5.61k
  switch (e->method) {
313
3.45k
  case SC_AC_NONE:
314
3.45k
    return 0x00;
315
1.84k
  case SC_AC_CHV:
316
1.86k
  case SC_AC_TERM:
317
1.89k
  case SC_AC_AUT:
318
1.89k
    if (e->key_ref == SC_AC_KEY_REF_NONE)
319
0
      return 0x00;
320
1.89k
    if (e->key_ref < 1 || e->key_ref > 14)
321
117
      return 0x00;
322
1.77k
    return e->key_ref;
323
101
  case SC_AC_NEVER:
324
101
    return 0x0F;
325
5.61k
  }
326
172
  return 0x00;
327
5.61k
}
328
329
static void add_acl_entry(struct sc_file *file, int op, u8 byte)
330
25.3k
{
331
25.3k
  unsigned int method, key_ref = SC_AC_KEY_REF_NONE;
332
333
25.3k
  switch (byte)
334
25.3k
  {
335
5.51k
  case 0:
336
5.51k
    method = SC_AC_NONE;
337
5.51k
    break;
338
1.25k
  case 15:
339
1.25k
    method = SC_AC_NEVER;
340
1.25k
    break;
341
18.6k
  default:
342
18.6k
    method = SC_AC_CHV;
343
18.6k
    key_ref = byte;
344
18.6k
    break;
345
25.3k
  }
346
25.3k
  sc_file_add_acl_entry(file, op, method, key_ref);
347
25.3k
}
348
349
static void parse_sec_attr(struct sc_file *file, const u8 *buf, size_t len)
350
12.1k
{
351
12.1k
  int i;
352
12.1k
  const int df_ops[4] =
353
12.1k
    { SC_AC_OP_CREATE, SC_AC_OP_CREATE, SC_AC_OP_DELETE, -1 };
354
12.1k
  const int ef_ops[4] =
355
12.1k
    { SC_AC_OP_READ, SC_AC_OP_UPDATE, SC_AC_OP_DELETE, -1 };
356
12.1k
  const int key_ops[4] =
357
12.1k
    { SC_AC_OP_CRYPTO, SC_AC_OP_UPDATE, SC_AC_OP_DELETE, SC_AC_OP_GENERATE };
358
359
12.1k
  const int *ops;
360
361
12.1k
  if (len < 2)
362
4.33k
    return;
363
364
7.80k
  switch (file->type) {
365
3.34k
  case SC_FILE_TYPE_WORKING_EF:
366
3.34k
    ops = ef_ops;
367
3.34k
    break;
368
183
  case SC_FILE_TYPE_INTERNAL_EF:
369
183
    ops = key_ops;
370
183
    break;
371
2.52k
  case SC_FILE_TYPE_DF:
372
2.52k
    ops = df_ops;
373
2.52k
    break;
374
1.76k
  default:
375
1.76k
    ops = key_ops;
376
1.76k
    break;
377
7.80k
  }
378
379
39.0k
  for (i = 0; i < 4; i++)
380
31.2k
  {
381
31.2k
    if (ops[i] == -1)
382
5.86k
      continue;
383
25.3k
    if ((i & 1) == 0)
384
15.6k
      add_acl_entry(file, ops[i], (u8)(buf[i / 2] >> 4));
385
9.75k
    else
386
9.75k
      add_acl_entry(file, ops[i], (u8)(buf[i / 2] & 0x0F));
387
25.3k
  }
388
7.80k
}
389
390
static int myeid_select_file(struct sc_card *card, const struct sc_path *in_path,
391
    struct sc_file **file)
392
125k
{
393
125k
  int r;
394
395
125k
  LOG_FUNC_CALLED(card->ctx);
396
125k
  r = iso_ops->select_file(card, in_path, file);
397
398
125k
  if (r == 0 && file != NULL && *file != NULL)
399
12.1k
    parse_sec_attr(*file, (*file)->sec_attr, (*file)->sec_attr_len);
400
401
125k
  LOG_FUNC_RETURN(card->ctx, r);
402
125k
}
403
404
static int myeid_logout(struct sc_card *card)
405
0
{
406
0
  sc_apdu_t apdu;
407
0
  int r;
408
409
0
  LOG_FUNC_CALLED(card->ctx);
410
411
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x2E, 0x00, 0x00 /*pin ref: 0x01-0x0E, 0=ALL*/);
412
0
  apdu.cla = 0;
413
414
0
  r = sc_transmit_apdu(card, &apdu);
415
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
416
417
0
  LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2));
418
0
}
419
420
static int myeid_list_files(struct sc_card *card, u8 *buf, size_t buflen)
421
0
{
422
0
  struct sc_apdu apdu;
423
0
  int r;
424
425
0
  LOG_FUNC_CALLED(card->ctx);
426
427
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xCA, 0x01, 0xA1);
428
0
  apdu.resp = buf;
429
0
  apdu.resplen = buflen;
430
0
  apdu.le = buflen > 256 ? 256 : buflen;
431
432
0
  r = sc_transmit_apdu(card, &apdu);
433
434
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
435
0
  if (apdu.resplen == 0)
436
0
    return sc_check_sw(card, apdu.sw1, apdu.sw2);
437
0
  return (int)apdu.resplen;
438
0
}
439
440
static int myeid_process_fci(struct sc_card *card, struct sc_file *file,
441
    const u8 *buf, size_t buflen)
442
11.6k
{
443
11.6k
  myeid_private_data_t *priv = (myeid_private_data_t *) card->drv_data;
444
11.6k
  size_t taglen = 0;
445
11.6k
  const u8 *tag = NULL;
446
11.6k
  int r;
447
448
11.6k
  LOG_FUNC_CALLED(card->ctx);
449
11.6k
  r = iso_ops->process_fci(card, file, buf, buflen);
450
11.6k
  if (r < 0)
451
11.6k
   LOG_FUNC_RETURN(card->ctx, r);
452
453
11.6k
  if(file->type == SC_FILE_EF_UNKNOWN)
454
4.80k
  {
455
4.80k
    tag = sc_asn1_find_tag(NULL, buf, buflen, 0x82, &taglen);
456
4.80k
    if (tag != NULL && taglen > 0 && *tag == 17)
457
127
    {
458
127
      file->type = SC_FILE_TYPE_INTERNAL_EF;
459
127
    }
460
4.80k
  }
461
11.6k
  if(file->sec_attr_len >= 3)
462
7.72k
  {
463
7.72k
    sc_log(card->ctx, "id (%X) sec_attr (%X %X %X)", file->id,
464
7.72k
      file->sec_attr[0],file->sec_attr[1],file->sec_attr[2]);
465
7.72k
  }
466
467
11.6k
  priv->card_state = file->status;
468
11.6k
  switch (file->status) {
469
5.32k
    case SC_FILE_STATUS_CREATION:
470
5.32k
      file->acl_inactive = 1;
471
5.32k
      sc_log(card->ctx, "File id (%X) status SC_FILE_STATUS_CREATION", file->id);
472
5.32k
      break;
473
233
    case SC_FILE_STATUS_ACTIVATED:
474
233
      sc_log(card->ctx, "File id (%X) status SC_FILE_STATUS_ACTIVATED", file->id);
475
233
      break;
476
6.04k
    default:
477
6.04k
      sc_log(card->ctx, "File id (%X) unusual status (0x%X)", file->id, file->status);
478
11.6k
  }
479
480
11.6k
  LOG_FUNC_RETURN(card->ctx, 0);
481
11.6k
}
482
483
static int encode_file_structure(sc_card_t *card, const sc_file_t *file,
484
    u8 *buf, size_t *outlen)
485
2.04k
{
486
2.04k
  const sc_acl_entry_t *read, *update, *delete, *generate;
487
2.04k
  size_t i;
488
489
2.04k
  LOG_FUNC_CALLED(card->ctx);
490
491
2.04k
  if (!buf || !outlen || *outlen < 45)
492
2.04k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
493
494
  /* PrivateKey
495
   * 0E0000019 6217 81020400 820111 83024B01 8603000000 85028000 8A0100 RESULT 6984
496
   *     6217 81020400 820111 83024B01 8603000000 85021000 8A0100 */
497
2.04k
  memset(buf, 0x0, *outlen);
498
499
2.04k
  buf[0] = 0x62;
500
2.04k
  buf[1] = 0x17;
501
  /* File size */
502
2.04k
  buf[2] = (SC_FILE_TYPE_WORKING_EF == file->type ? 0x80 : 0x81);
503
2.04k
  buf[3] = 0x02;
504
2.04k
  buf[4] = (file->size >> 8) & 0xFF;
505
2.04k
  buf[5] = file->size & 0xFF;
506
507
  /* File Description tag */
508
2.04k
  buf[6] = 0x82;
509
2.04k
  buf[7] = 0x01;
510
2.04k
  buf[8] = 0x01;
511
512
  /* File Identifier tag */
513
2.04k
  buf[9]  = 0x83;
514
2.04k
  buf[10] = 0x02;
515
2.04k
  buf[11] = (file->id >> 8) & 0xFF;
516
2.04k
  buf[12] = file->id & 0xFF;
517
518
  /* Security Attributes Tag */
519
2.04k
  buf[13] = 0x86;
520
2.04k
  buf[14] = 0x03;
521
2.04k
  buf[15] = 0xFF;
522
2.04k
  buf[16] = 0xFF;
523
2.04k
  buf[17] = 0xFF;
524
525
2.04k
  if (file->sec_attr_len == 3 && file->sec_attr)   {
526
168
    buf[15] = file->sec_attr[0];
527
168
    buf[16] = file->sec_attr[1];
528
168
    buf[17] = file->sec_attr[2];
529
530
168
    sc_log(card->ctx, "id (%X), sec_attr %X %X %X", file->id,
531
168
        file->sec_attr[0],file->sec_attr[1],file->sec_attr[2]);
532
168
  }
533
1.87k
  else   {
534
1.87k
    delete = sc_file_get_acl_entry(file, SC_AC_OP_DELETE);
535
536
1.87k
    sc_log(card->ctx, "id (%X), type (%X)", file->id, file->type);
537
538
1.87k
    switch (file->type) {
539
1.35k
    case SC_FILE_TYPE_WORKING_EF:
540
541
1.35k
      read = sc_file_get_acl_entry(file, SC_AC_OP_READ);
542
1.35k
      update = sc_file_get_acl_entry(file, SC_AC_OP_UPDATE);
543
544
1.35k
      buf[15] = (acl_to_byte(read) << 4) | acl_to_byte(update);
545
1.35k
      buf[16] = (acl_to_byte(delete)<< 4) | 0x0F;
546
1.35k
      break;
547
95
    case SC_FILE_TYPE_INTERNAL_EF:
548
549
95
      read = sc_file_get_acl_entry(file, SC_AC_OP_CRYPTO);
550
95
      update = sc_file_get_acl_entry(file, SC_AC_OP_UPDATE);
551
95
      generate = sc_file_get_acl_entry(file, SC_AC_OP_GENERATE);
552
553
95
      buf[15] = (acl_to_byte(read) << 4) | acl_to_byte(update);
554
95
      buf[16] = (acl_to_byte(delete)<< 4) | acl_to_byte(generate);
555
95
      break;
556
394
    case SC_FILE_TYPE_DF:
557
558
394
      update = sc_file_get_acl_entry(file, SC_AC_OP_CREATE);
559
560
394
      buf[15] = (acl_to_byte(update) << 4) | acl_to_byte(update);
561
394
      buf[16] = (acl_to_byte(delete) << 4) | 0x0F;
562
394
      break;
563
35
    default:
564
35
      break;
565
1.87k
    }
566
1.87k
  }
567
568
  /* Proprietary Information */
569
2.04k
  buf[18] = 0x85;
570
2.04k
  buf[19] = 0x02;
571
2.04k
  if (file->prop_attr_len == 2 && file->prop_attr != NULL)
572
458
      memcpy(&buf[20], file->prop_attr, 2);
573
1.58k
  else
574
1.58k
  {
575
1.58k
    buf[20] = 0x00;
576
1.58k
    buf[21] = 0x00;
577
1.58k
  }
578
579
  /* Life Cycle Status tag */
580
2.04k
  buf[22] = 0x8A;
581
2.04k
  buf[23] = 0x01;
582
2.04k
  buf[24] = 0x0; /* RFU */
583
584
2.04k
  switch (file->type)
585
2.04k
  {
586
1.42k
  case SC_FILE_TYPE_WORKING_EF:
587
1.42k
    break;
588
589
182
  case SC_FILE_TYPE_INTERNAL_EF:
590
182
    buf[8] = file->ef_structure; /* RSA or EC */
591
182
    break;
592
593
404
  case SC_FILE_TYPE_DF:
594
404
    buf[8] = 0x38;
595
404
    if(file->namelen > 0 && file->namelen <= 16)
596
150
    {
597
150
      buf[25] = 0x84;
598
150
      buf[26] = (u8)file->namelen;
599
600
1.54k
      for(i=0;i < file->namelen;i++)
601
1.39k
        buf[i + 27] = file->name[i];
602
603
150
      buf[1] = 27 + file->namelen;
604
150
    }
605
404
    break;
606
35
  default:
607
35
    sc_log(card->ctx, "Unknown file type\n");
608
35
    return SC_ERROR_INVALID_ARGUMENTS;
609
2.04k
  }
610
611
2.01k
  *outlen = buf[1]+2;
612
613
2.01k
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
614
2.01k
}
615
616
static int myeid_create_file(struct sc_card *card, struct sc_file *file)
617
2.04k
{
618
2.04k
  sc_apdu_t apdu;
619
2.04k
  u8 sbuf[45];
620
2.04k
  size_t buflen = sizeof sbuf;
621
2.04k
  int r;
622
623
2.04k
  LOG_FUNC_CALLED(card->ctx);
624
625
2.04k
  r = encode_file_structure(card, file, sbuf, &buflen);
626
2.04k
  if (r)
627
2.04k
    LOG_FUNC_RETURN(card->ctx, r);
628
629
2.01k
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x00, 0x00);
630
2.01k
  apdu.data = sbuf;
631
2.01k
  apdu.datalen = buflen;
632
2.01k
  apdu.lc = buflen;
633
634
2.01k
  r = sc_transmit_apdu(card, &apdu);
635
2.01k
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
636
2.00k
  if (apdu.sw1 == 0x6A && apdu.sw2 == 0x89)
637
2.00k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_FILE_ALREADY_EXISTS);
638
639
2.00k
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
640
2.00k
  LOG_FUNC_RETURN(card->ctx, r);
641
2.00k
}
642
643
static int myeid_delete_file(struct sc_card *card, const struct sc_path *path)
644
17
{
645
17
  int r;
646
17
  struct sc_apdu apdu;
647
648
17
  LOG_FUNC_CALLED(card->ctx);
649
17
  if (path->type != SC_PATH_TYPE_FILE_ID && path->len != 2)
650
0
  {
651
0
    sc_log(card->ctx, "File type has to be SC_PATH_TYPE_FILE_ID\n");
652
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
653
0
  }
654
17
  r = sc_select_file(card, path, NULL);
655
17
  LOG_TEST_RET(card->ctx, r, "Unable to select file to be deleted");
656
657
12
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0xE4, 0x00, 0x00);
658
12
  apdu.cla = 0xA0;
659
660
12
  r = sc_transmit_apdu(card, &apdu);
661
12
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
662
663
11
  LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2));
664
11
}
665
666
static int myeid_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data,
667
       int *tries_left)
668
637
{
669
637
  myeid_private_data_t *priv = (myeid_private_data_t *) card->drv_data;
670
671
637
  LOG_FUNC_CALLED(card->ctx);
672
673
637
  sc_log(card->ctx, "ref (%d), pin1 len(%zu), pin2 len (%zu)\n",
674
637
      data->pin_reference, data->pin1.len, data->pin2.len);
675
676
637
  if(data->pin1.len > 8 || data->pin2.len > 8)
677
637
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_PIN_LENGTH);
678
679
637
  data->pin1.pad_length = data->pin2.pad_length = 8;
680
637
  data->pin1.pad_char = data->pin2.pad_char = 0xFF;
681
682
637
  if (data->cmd == SC_PIN_CMD_VERIFY && priv->card_state == SC_FILE_STATUS_CREATION) {
683
11
    sc_log(card->ctx, "Card in creation state, no need to verify");
684
11
    return SC_SUCCESS;
685
11
  }
686
687
626
  LOG_FUNC_RETURN(card->ctx, iso_ops->pin_cmd(card, data, tries_left));
688
626
}
689
690
0
#define IS_SYMETRIC_CRYPT(x) ((x) == SC_SEC_OPERATION_ENCRYPT_SYM || (x) == SC_SEC_OPERATION_DECRYPT_SYM)
691
692
static int myeid_set_security_env_rsa(sc_card_t *card, const sc_security_env_t *env,
693
    int se_num)
694
0
{
695
0
  sc_apdu_t apdu;
696
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
697
0
  u8 *p;
698
0
  int r;
699
0
  size_t i, sz;
700
0
  sc_path_t *target_file;
701
702
0
  assert(card != NULL && env != NULL);
703
0
  LOG_FUNC_CALLED(card->ctx);
704
705
0
  if (env->flags & SC_SEC_ENV_KEY_REF_SYMMETRIC)
706
0
  {
707
0
    sc_log(card->ctx, "symmetric keyref not supported.\n");
708
0
    return SC_ERROR_NOT_SUPPORTED;
709
0
  }
710
0
  if (se_num > 0)
711
0
  {
712
0
    sc_log(card->ctx, "restore security environment not supported.\n");
713
0
    return SC_ERROR_NOT_SUPPORTED;
714
0
  }
715
716
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0, 0);
717
0
  switch (env->operation)
718
0
  {
719
0
  case SC_SEC_OPERATION_DECIPHER:
720
0
    apdu.p1 = 0x41;
721
0
    apdu.p2 = 0xB8;
722
0
    break;
723
0
  case SC_SEC_OPERATION_SIGN:
724
0
    apdu.p1 = 0x41;
725
0
    apdu.p2 = 0xB6;
726
0
    break;
727
0
  case SC_SEC_OPERATION_UNWRAP:
728
0
    apdu.p1 = 0x41;
729
0
    apdu.p2 = 0xB8;
730
0
    break;
731
0
  case SC_SEC_OPERATION_WRAP:
732
0
    apdu.p1 = 0x81;
733
0
    apdu.p2 = 0xB8;
734
0
    break;
735
0
  case SC_SEC_OPERATION_ENCRYPT_SYM:
736
0
    apdu.p1 = 0x81;
737
0
    apdu.p2 = 0xB8;
738
0
    break;
739
0
  case SC_SEC_OPERATION_DECRYPT_SYM:
740
0
    apdu.p1 = 0x41;
741
0
    apdu.p2 = 0xB8;
742
0
    break;
743
0
  default:
744
0
    return SC_ERROR_INVALID_ARGUMENTS;
745
0
  }
746
0
  apdu.le = 0;
747
0
  p = sbuf;
748
0
  if (env->flags & SC_SEC_ENV_ALG_REF_PRESENT)
749
0
  {
750
0
    *p++ = 0x80;  /* algorithm reference */
751
0
    *p++ = 0x01;
752
0
    *p++ = env->algorithm_ref & 0xFF;
753
0
  }
754
0
  if (env->flags & SC_SEC_ENV_FILE_REF_PRESENT)
755
0
  {
756
0
    *p++ = 0x81;
757
0
    *p++ = 2;
758
0
    memcpy(p, env->file_ref.value, 2);
759
0
    p += 2;
760
0
  }
761
  /* symmetric operations: we need to set the key reference */
762
0
  if (IS_SYMETRIC_CRYPT(env->operation)) {
763
0
    *p++ = 0x83;
764
0
    *p++ = 1;
765
0
    *p++ = 0;
766
0
  }
767
0
  if (env->flags & SC_SEC_ENV_KEY_REF_PRESENT && env->operation != SC_SEC_OPERATION_UNWRAP &&
768
0
      env->operation != SC_SEC_OPERATION_WRAP &&
769
0
      env->operation != SC_SEC_OPERATION_ENCRYPT_SYM &&
770
0
      env->operation != SC_SEC_OPERATION_DECRYPT_SYM) {
771
0
    *p++ = 0x84;
772
0
    *p++ = 1;
773
0
    *p++ = 0;
774
0
  }
775
0
  for (i = 0; i < SC_SEC_ENV_MAX_PARAMS; i++)
776
0
      if (env->params[i].param_type == SC_SEC_ENV_PARAM_TARGET_FILE) {
777
0
      target_file = (sc_path_t*) env->params[i].value;
778
0
      if (env->params[i].value_len < sizeof(sc_path_t) || target_file->len != 2) {
779
0
        sc_log(card->ctx, "wrong length of target file reference.\n");
780
0
        return SC_ERROR_WRONG_LENGTH;
781
0
      }
782
0
      *p++ = 0x83;
783
0
      *p++ = 2;
784
0
      memcpy(p, target_file->value, 2);
785
0
      p+= 2;
786
0
      break;
787
0
      }
788
789
0
  r = 0;
790
0
  if (env->operation == SC_SEC_OPERATION_UNWRAP || env->operation == SC_SEC_OPERATION_WRAP ||
791
0
      IS_SYMETRIC_CRYPT(env->operation)) {
792
    /* add IV if present */
793
0
    for (i = 0; i < SC_SEC_ENV_MAX_PARAMS; i++)
794
0
      if (env->params[i].param_type == SC_SEC_ENV_PARAM_IV) {
795
0
        r = 1;
796
0
        *p++ = 0x87;
797
0
        *p++ = (unsigned char) env->params[i].value_len;
798
0
        if (p + env->params[i].value_len >= sbuf + SC_MAX_APDU_BUFFER_SIZE) {
799
0
          sc_log(card->ctx, "IV too long.\n");
800
0
          return SC_ERROR_WRONG_LENGTH;
801
0
        }
802
0
        memcpy(p, env->params[i].value, env->params[i].value_len);
803
0
        p+=(unsigned char) env->params[i].value_len;
804
0
        break;
805
0
      }
806
0
  }
807
  /* for AES_ECB we need to reset the IV but we respect if the IV is already present */
808
0
  if (IS_SYMETRIC_CRYPT(env->operation) && env->algorithm == SC_ALGORITHM_AES &&
809
0
      env->algorithm_flags == SC_ALGORITHM_AES_ECB && r == 0) {
810
0
    *p++ = 0x87;
811
0
    *p++ = 16;
812
0
    memset(p, 0, 16);
813
0
    p += 16;
814
0
  }
815
816
0
  sz = p - sbuf;
817
0
  apdu.lc = sz;
818
0
  apdu.datalen = sz;
819
0
  apdu.data = sbuf;
820
0
  apdu.resplen = 0;
821
0
  r = (int)sz;
822
0
  if (apdu.datalen != 0)
823
0
  {
824
0
    r = sc_transmit_apdu(card, &apdu);
825
0
    if (r)
826
0
    {
827
0
      sc_log(card->ctx,
828
0
        "%s: APDU transmit failed", sc_strerror(r));
829
0
      goto err;
830
0
    }
831
0
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
832
0
    if (r)
833
0
    {
834
0
      sc_log(card->ctx,
835
0
        "%s: Card returned error", sc_strerror(r));
836
0
      goto err;
837
0
    }
838
0
  }
839
0
err:
840
0
  LOG_FUNC_RETURN(card->ctx, r);
841
0
}
842
843
static int myeid_set_security_env_ec(sc_card_t *card, const sc_security_env_t *env,
844
    int se_num)
845
0
{
846
0
  sc_apdu_t apdu;
847
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
848
0
  u8 *p;
849
0
  size_t sz;
850
0
  int r;
851
852
0
  assert(card != NULL && env != NULL);
853
0
  LOG_FUNC_CALLED(card->ctx);
854
855
0
  if (env->flags & SC_SEC_ENV_KEY_REF_SYMMETRIC)
856
0
  {
857
0
    sc_log(card->ctx, "symmetric keyref not supported.");
858
0
    return SC_ERROR_NOT_SUPPORTED;
859
0
  }
860
0
  if (se_num > 0)
861
0
  {
862
0
    sc_log(card->ctx, "restore security environment not supported.");
863
0
    return SC_ERROR_NOT_SUPPORTED;
864
0
  }
865
866
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0, 0);
867
0
  switch (env->operation)
868
0
  {
869
0
  case SC_SEC_OPERATION_DECIPHER:
870
0
    sc_log(card->ctx, "Decipher operation is not supported with EC keys.");
871
0
    return SC_ERROR_NOT_SUPPORTED;
872
0
    break;
873
0
  case SC_SEC_OPERATION_SIGN:
874
0
    apdu.p1 = 0x41;
875
0
    apdu.p2 = 0xB6;
876
0
    break;
877
0
  case SC_SEC_OPERATION_DERIVE:
878
0
    apdu.p1 = 0x41;
879
0
    apdu.p2 = 0xA4;
880
0
    break;
881
0
  default:
882
0
    return SC_ERROR_INVALID_ARGUMENTS;
883
0
  }
884
0
  apdu.le = 0;
885
0
  p = sbuf;
886
0
  if (env->flags & SC_SEC_ENV_ALG_REF_PRESENT)
887
0
  {
888
0
    *p++ = 0x80;  /* algorithm reference */
889
0
    *p++ = 0x01;
890
0
    *p++ = env->algorithm_ref & 0xFF;
891
0
  }
892
0
  if (env->flags & SC_SEC_ENV_FILE_REF_PRESENT)
893
0
  {
894
0
    *p++ = 0x81;
895
0
    *p++ = 0x02;
896
0
    memcpy(p, env->file_ref.value, 2);
897
0
    p += 2;
898
0
  }
899
0
  if (env->flags & SC_SEC_ENV_KEY_REF_PRESENT)
900
0
  {
901
0
    *p++ = 0x84;
902
0
    *p++ = 1;
903
0
    *p++ = 0;
904
0
  }
905
0
  sz = p - sbuf;
906
0
  apdu.lc = sz;
907
0
  apdu.datalen = sz;
908
0
  apdu.data = sbuf;
909
0
  apdu.resplen = 0;
910
0
  r = (int)sz;
911
0
  if (apdu.datalen != 0)
912
0
  {
913
0
    r = sc_transmit_apdu(card, &apdu);
914
0
    if (r)
915
0
    {
916
0
      sc_log(card->ctx,
917
0
        "%s: APDU transmit failed", sc_strerror(r));
918
0
      goto err;
919
0
    }
920
0
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
921
0
    if (r)
922
0
    {
923
0
      sc_log(card->ctx,
924
0
        "%s: Card returned error", sc_strerror(r));
925
0
      goto err;
926
0
    }
927
0
  }
928
0
err:
929
0
  LOG_FUNC_RETURN(card->ctx, r);
930
0
}
931
932
static int myeid_set_security_env(struct sc_card *card,
933
    const struct sc_security_env *env, int se_num)
934
0
{
935
0
  struct sc_context *ctx = card->ctx;
936
0
  myeid_private_data_t* priv;
937
938
0
  LOG_FUNC_CALLED(ctx);
939
940
0
  priv = (myeid_private_data_t*) card->drv_data;
941
  /* store security environment to differentiate between ECDH and RSA in decipher - Hannu*/
942
0
  priv->sec_env = env;
943
944
  /* for symmetric operation save algo and algo flags */
945
0
  priv->algorithm_flags = env->algorithm_flags;
946
0
  priv->algorithm = env->algorithm;
947
948
0
  if (env->flags & SC_SEC_ENV_ALG_PRESENT)
949
0
  {
950
0
    sc_security_env_t tmp;
951
952
0
    tmp = *env;
953
0
    tmp.flags &= ~SC_SEC_ENV_ALG_PRESENT;
954
0
    tmp.flags |= SC_SEC_ENV_ALG_REF_PRESENT;
955
956
0
    if (tmp.algorithm == SC_ALGORITHM_RSA)
957
0
    {
958
0
      if (tmp.operation == SC_SEC_OPERATION_UNWRAP || tmp.operation == SC_SEC_OPERATION_WRAP)
959
0
      {
960
0
          tmp.algorithm_ref = 0x0A;
961
0
      }
962
0
      else
963
0
      {
964
0
        tmp.algorithm_ref = 0x00;
965
        /* potential FIXME: return an error, if an unsupported
966
        * pad or hash was requested, although this shouldn't happen */
967
0
        if ((env->operation == SC_SEC_OPERATION_SIGN && env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01)
968
0
          || (env->operation == SC_SEC_OPERATION_DECIPHER && env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02))
969
0
          tmp.algorithm_ref = 0x02;
970
0
        if (tmp.algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1)
971
0
          tmp.algorithm_ref |= 0x10;
972
0
      }
973
974
0
      return myeid_set_security_env_rsa(card, &tmp, se_num);
975
0
    }
976
0
    else if (tmp.algorithm == SC_ALGORITHM_EC)
977
0
    {
978
0
      tmp.algorithm_ref = 0x04;
979
0
      tmp.algorithm_flags = 0;
980
0
      return myeid_set_security_env_ec(card, &tmp, se_num);
981
0
    }
982
0
    else if (tmp.algorithm == SC_ALGORITHM_AES)
983
0
    {
984
0
      if (tmp.operation == SC_SEC_OPERATION_UNWRAP || tmp.operation == SC_SEC_OPERATION_WRAP)
985
0
      {
986
0
        tmp.algorithm_ref = 0x0A;
987
0
      }
988
0
      else
989
0
      {
990
0
        tmp.algorithm_ref = 0x00;
991
0
      }
992
993
0
      if ((tmp.algorithm_flags & SC_ALGORITHM_AES_CBC_PAD) == SC_ALGORITHM_AES_CBC_PAD)
994
0
        tmp.algorithm_ref |= 0x80;   /* set PKCS#7 padding */
995
      /* Tag 0x80 algorithm_ref - value 0x80 or 0x8A is working only for UNWRAP/WRAP
996
       * AES is supported from version 4.0 but without pkcs#7 padding.
997
       * For SC_SEC_OPERATION_ENCRYPT_SYM and SC_SEC_OPERATION_DECRYPT_SYM we running
998
       * PKCS#7 in software, here we fix the algorithm_ref variable.
999
       */
1000
0
      if (IS_SYMETRIC_CRYPT(env->operation))
1001
0
        tmp.algorithm_ref &= ~0x80; /* do not handle padding in card */
1002
1003
      /* from this point, there's no difference to RSA SE */
1004
0
      return myeid_set_security_env_rsa(card, &tmp, se_num);
1005
0
    }
1006
0
    else
1007
0
    {
1008
1009
0
      sc_log(ctx, "Unsupported algorithm.");
1010
0
      return SC_ERROR_NOT_SUPPORTED;
1011
0
    }
1012
0
  }
1013
0
  return myeid_set_security_env_rsa(card, env, se_num);
1014
0
}
1015
1016
1017
static int
1018
myeid_convert_ec_signature(struct sc_context *ctx, size_t s_len, unsigned char *data, size_t datalen)
1019
0
{
1020
0
  unsigned char *buf;
1021
0
  size_t buflen;
1022
0
  int r;
1023
0
  size_t len_size = 1;
1024
0
  size_t sig_len = 0;
1025
1026
0
  assert(data && datalen && datalen > 3);
1027
1028
  /*
1029
   *  When validating the signature data, we have to consider that length of the signature
1030
   *  can be encoded in either one or two bytes depending on key size. With 521 bit keys
1031
   *  length of the structure takes two bytes.
1032
   */
1033
1034
0
  if (*data != 0x30)
1035
0
    return SC_ERROR_INVALID_DATA;
1036
1037
0
  if ((*(data + 1) & 0x80) == 0x80)
1038
0
    len_size += *(data + 1) & 0x7F;
1039
1040
0
  if (len_size == 1)
1041
0
      sig_len = *(data + 1);
1042
0
  else if (len_size == 2)
1043
0
      sig_len = *(data + 2);
1044
0
  else if (len_size == 3)
1045
0
  {
1046
0
      sig_len = *(data + 2) | (*data + 3) << 8;
1047
0
  }
1048
0
  else
1049
0
      return SC_ERROR_INVALID_DATA;
1050
1051
0
  if (*(data + 1 + len_size) != 0x02)   /* Verify that it is an INTEGER */
1052
1053
0
  if (sig_len != (datalen - len_size - 1)) /* validate size of the DER structure */
1054
0
      return SC_ERROR_INVALID_DATA;
1055
1056
  /* test&fail early */
1057
0
  buflen = BYTES4BITS(s_len) * 2;
1058
0
  if (buflen > datalen)
1059
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_DATA);
1060
1061
0
  buf = calloc(1, buflen);
1062
0
  if (!buf)
1063
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
1064
1065
0
  r = sc_asn1_sig_value_sequence_to_rs(ctx, data, datalen, buf, buflen);
1066
0
  if (r < 0) {
1067
0
    free(buf);
1068
0
    sc_log(ctx, "Failed to convert Sig-Value to the raw RS format");
1069
0
    return r;
1070
0
  }
1071
1072
0
  memmove(data, buf, buflen);
1073
0
  free(buf);
1074
0
  return (int)buflen;
1075
0
}
1076
/* MyEID cards before version 4.5 do not support RAW RSA signature for 2048 bit RSA keys.
1077
 * (Source: MyEID reference manual 2.1.4)
1078
 *
1079
 * This function uses decipher operation for calculating RAW 2048 bit signature. */
1080
static int
1081
myeid_compute_raw_2048_signature(struct sc_card *card, const u8 * data, size_t datalen,
1082
    u8 * out, size_t outlen)
1083
0
{
1084
0
  int r;
1085
0
  struct sc_context *ctx;
1086
0
  struct myeid_private_data *priv;
1087
0
  struct sc_apdu apdu;
1088
0
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
1089
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
1090
0
  sc_security_env_t env;
1091
1092
0
  ctx = card->ctx;
1093
0
  LOG_FUNC_CALLED(ctx);
1094
1095
0
  priv = (myeid_private_data_t *) card->drv_data;
1096
1097
/* security env change - use DECIPHER operation */
1098
0
  memcpy(&env, priv->sec_env, sizeof(sc_security_env_t));
1099
0
  env.flags |= SC_SEC_ENV_ALG_REF_PRESENT;
1100
0
  env.flags |= SC_SEC_ENV_FILE_REF_PRESENT;
1101
0
  env.flags |= SC_SEC_ENV_KEY_REF_PRESENT;
1102
0
  env.operation = SC_SEC_OPERATION_DECIPHER;
1103
0
  myeid_set_security_env_rsa(card, &env, 0);
1104
1105
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x2A, 0x80, 0x86);
1106
0
  apdu.resp = rbuf;
1107
0
  apdu.resplen = sizeof(rbuf);
1108
0
  apdu.le = 0;  /* there is no response to 1st part of data */
1109
1110
/* prepare 1st part of data */
1111
0
  sbuf[0] = 0x81;
1112
0
  memcpy(sbuf + 1, data, datalen / 2);
1113
0
  apdu.lc = datalen / 2 + 1;
1114
0
  apdu.datalen = apdu.lc;
1115
0
  apdu.data = sbuf;
1116
1117
0
  r = sc_transmit_apdu(card, &apdu);
1118
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1119
0
  if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
1120
/* prepare 2nd part of data */
1121
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x80, 0x86);
1122
0
    apdu.resp = rbuf;
1123
0
    apdu.resplen = sizeof(rbuf);
1124
0
    apdu.le = datalen;
1125
0
    sbuf[0] = 0x82;
1126
0
    memcpy(sbuf + 1, data + datalen / 2, datalen / 2);
1127
0
    apdu.lc = datalen / 2 + 1;
1128
0
    apdu.datalen = apdu.lc;
1129
0
    apdu.data = sbuf;
1130
1131
0
    r = sc_transmit_apdu(card, &apdu);
1132
0
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1133
1134
0
    if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
1135
0
      size_t len = apdu.resplen > outlen ? outlen : apdu.resplen;
1136
0
      memcpy(out, apdu.resp, len);
1137
0
      LOG_FUNC_RETURN(card->ctx, (int)len);
1138
0
    }
1139
0
  }
1140
0
  LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2));
1141
0
}
1142
1143
static int
1144
myeid_compute_signature(struct sc_card *card, const u8 * data, size_t datalen,
1145
    u8 * out, size_t outlen)
1146
0
{
1147
0
  struct sc_context *ctx;
1148
0
  struct sc_apdu apdu;
1149
0
  u8 rbuf[MYEID_MAX_EXT_APDU_BUFFER_SIZE];
1150
0
  u8 sbuf[MYEID_MAX_EXT_APDU_BUFFER_SIZE];
1151
0
  struct myeid_private_data* priv;
1152
0
  int r;
1153
0
  size_t field_length = 0;
1154
0
  size_t pad_chars = 0;
1155
1156
1157
0
  assert(card != NULL && data != NULL && out != NULL);
1158
0
  ctx = card->ctx;
1159
0
  LOG_FUNC_CALLED(ctx);
1160
1161
0
  priv = (myeid_private_data_t*) card->drv_data;
1162
0
  sc_log(ctx, "key type %lu, key length %lu", priv->sec_env->algorithm, priv->sec_env->algorithm_ref);
1163
1164
0
  if (priv->sec_env->algorithm == SC_ALGORITHM_EC ) {
1165
1166
0
      field_length = priv->sec_env->algorithm_ref;
1167
1168
      /* pad with zeros if needed */
1169
0
    if (datalen < BYTES4BITS(field_length)) {
1170
0
      pad_chars = BYTES4BITS(field_length) - datalen;
1171
1172
0
      memset(sbuf, 0, pad_chars);
1173
0
    }
1174
0
  }
1175
1176
0
  if ((datalen + pad_chars) > sizeof(sbuf))
1177
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
1178
1179
0
  if (priv->sec_env->algorithm == SC_ALGORITHM_RSA && datalen == 256 && !priv->cap_chaining)
1180
0
    return myeid_compute_raw_2048_signature(card, data, datalen, out, outlen);
1181
1182
  /* INS: 0x2A  PERFORM SECURITY OPERATION
1183
    * P1:  0x9E  Resp: Digital Signature
1184
    * P2:  0x9A  Cmd: Input for Digital Signature */
1185
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x9E, 0x9A);
1186
0
  apdu.flags |= SC_APDU_FLAGS_CHAINING;
1187
0
  apdu.resp = rbuf;
1188
0
  apdu.resplen = sizeof(rbuf);
1189
0
  apdu.le = 256;
1190
0
  memcpy(sbuf + pad_chars, data, datalen);
1191
0
  apdu.lc = datalen + pad_chars;
1192
0
  apdu.datalen = datalen + pad_chars;
1193
1194
0
  apdu.data = sbuf;
1195
0
  r = sc_transmit_apdu(card, &apdu);
1196
0
  LOG_TEST_RET(ctx, r, "APDU transmit failed");
1197
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1198
0
  LOG_TEST_RET(ctx, r, "compute_signature failed");
1199
1200
0
  if (priv->sec_env->algorithm == SC_ALGORITHM_EC) {
1201
0
    r = myeid_convert_ec_signature(ctx, priv->sec_env->algorithm_ref, apdu.resp, apdu.resplen);
1202
0
    LOG_TEST_RET(ctx, r, "compute_signature convert signature failed");
1203
0
    apdu.resplen = r;
1204
0
  }
1205
1206
0
  if (apdu.resplen > outlen)
1207
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_BUFFER_TOO_SMALL);
1208
1209
0
  memcpy(out, apdu.resp, apdu.resplen);
1210
0
  LOG_FUNC_RETURN(ctx, (int)apdu.resplen);
1211
0
}
1212
1213
1214
/* takes other party's public key as input, performs ECDH key derivation and returns the shared secret in [out]. */
1215
int myeid_ecdh_derive(struct sc_card *card, const u8* pubkey, size_t pubkey_len, u8* out, size_t outlen)
1216
0
{
1217
1218
  /* MyEID uses GENERAL AUTHENTICATE ISO command for ECDH */
1219
1220
0
  struct sc_apdu apdu;
1221
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
1222
0
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
1223
1224
0
  int r;
1225
0
  size_t ext_len_bytes;
1226
1227
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x86, 0x00, 0x00);
1228
1229
0
  apdu.resp = rbuf;
1230
0
  apdu.resplen = sizeof(rbuf);
1231
1232
  /* Fill in "Data objects in dynamic authentication template" (tag 0x7C) structure
1233
  *
1234
  * TODO: encode the structure using OpenSC's ASN1-functions.
1235
  *
1236
  *  Size of the structure depends on key length. With 521 bit keys two bytes are needed for defining length of a point.
1237
  */
1238
1239
0
  sbuf[0] = 0x7C;
1240
0
  ext_len_bytes = 0;
1241
1242
0
  if (pubkey_len > 127)
1243
0
  {
1244
0
    sbuf[1] = 0x81;
1245
0
    sbuf[2] = (u8) (pubkey_len + 3);
1246
0
    sbuf[3] = 0x85;
1247
0
    sbuf[4] = 0x81;
1248
0
    sbuf[5] = (u8) (pubkey_len);
1249
0
    ext_len_bytes = 2;
1250
0
  }
1251
0
  else
1252
0
  {
1253
0
    sbuf[1] = pubkey_len + 2;
1254
0
    sbuf[2] = 0x85;
1255
0
    sbuf[3] = pubkey_len;
1256
0
  }
1257
1258
0
  memcpy(&sbuf[4 + ext_len_bytes], pubkey, pubkey_len);
1259
1260
0
  apdu.lc = pubkey_len + 4 + ext_len_bytes;
1261
0
  apdu.le = pubkey_len / 2;
1262
0
  apdu.datalen = apdu.lc;
1263
0
  apdu.data = sbuf;
1264
1265
0
  r = sc_transmit_apdu(card, &apdu);
1266
1267
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed.");
1268
1269
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1270
0
  LOG_TEST_RET(card->ctx, r, "ECDH operation failed - GENERAL AUTHENTICATE returned error.");
1271
1272
0
  if (outlen < apdu.resplen)
1273
0
  {
1274
0
    r = SC_ERROR_BUFFER_TOO_SMALL;
1275
0
    LOG_TEST_RET(card->ctx, r, "Buffer too small to hold shared secret.");
1276
0
  }
1277
1278
0
  memcpy(out, rbuf, apdu.resplen);
1279
1280
0
  LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen);
1281
0
}
1282
1283
static int myeid_transmit_decipher_pi_split(struct sc_card *card, struct sc_apdu *apdu, u8 *sbuf)
1284
0
{
1285
  /* MyEID before 4.5.x does not support APDU chaining. The payload
1286
   * is split to two regular APDUs and Padding Indicator field is used to
1287
   * describe which slice it is. */
1288
0
  size_t crgram_len = apdu->lc - 1;
1289
0
  size_t crgram_half = crgram_len / 2;
1290
0
  size_t resplen = apdu->resplen;
1291
0
  unsigned char *resp = apdu->resp;
1292
0
  int r;
1293
1294
0
  LOG_FUNC_CALLED(card->ctx);
1295
1296
  /* Send 1st part, no response */
1297
0
  apdu->cse = SC_APDU_CASE_3_SHORT;
1298
0
  apdu->data = &sbuf[0];
1299
0
  apdu->datalen = apdu->lc = crgram_half + 1;
1300
0
  apdu->resp = 0;
1301
0
  apdu->resplen = 0;
1302
0
  apdu->le = 0;
1303
0
  sbuf[0] = 0x81;     /* Padding Indicator, 0x81 = First half */
1304
1305
0
  r = sc_transmit_apdu(card, apdu);
1306
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1307
0
  if (apdu->sw1 != 0x90 || apdu->sw2 != 0x00)
1308
0
    return 0;
1309
1310
  /* Send 2nd part, expect response */
1311
0
  apdu->cse = resplen ? SC_APDU_CASE_4_SHORT : SC_APDU_CASE_3_SHORT;
1312
0
  apdu->data = &sbuf[crgram_half];
1313
0
  apdu->datalen = apdu->lc = crgram_len - crgram_half + 1;
1314
0
  apdu->resp = resp;
1315
0
  apdu->resplen = resplen;
1316
0
  apdu->le = resplen ? MIN(card->max_recv_size, crgram_len) : 0;
1317
0
  sbuf[crgram_half] = 0x82; /* Padding Indicator, 0x82 = Second half */
1318
1319
0
  r = sc_transmit_apdu(card, apdu);
1320
0
  LOG_FUNC_RETURN(card->ctx, r);
1321
0
}
1322
1323
static int myeid_transmit_decipher(struct sc_card *card, u8 p1, u8 p2,
1324
    const u8 * crgram, size_t crgram_len, u8 * out, size_t outlen)
1325
0
{
1326
0
  myeid_private_data_t *priv = card->drv_data;
1327
0
  struct sc_apdu apdu;
1328
0
  u8 rbuf[SC_MAX_EXT_APDU_BUFFER_SIZE];
1329
0
  u8 sbuf[SC_MAX_EXT_APDU_BUFFER_SIZE];
1330
0
  int r;
1331
1332
0
  LOG_FUNC_CALLED(card->ctx);
1333
1334
  /* INS: 0x2A  PERFORM SECURITY OPERATION
1335
   * P1:  0x00  Resp: No response (unwrapping)
1336
   * P1:  0x80  Resp: Plain value
1337
   * P2:  0x84  Cmd: Cryptogram (no padding byte)
1338
   * P2:  0x86  Cmd: Padding indicator byte followed by cryptogram */
1339
0
  sc_format_apdu(card, &apdu, p1 ? SC_APDU_CASE_4_SHORT : SC_APDU_CASE_3_SHORT, 0x2A, p1, p2);
1340
0
  if (p2 == 0x86) {
1341
0
    if (crgram_len+1 > sizeof(sbuf))
1342
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
1343
0
    sbuf[0] = 0; /* Padding indicator: 0x00 = No further indication */
1344
0
    memcpy(sbuf + 1, crgram, crgram_len);
1345
0
    apdu.data = sbuf;
1346
0
    apdu.datalen = apdu.lc = crgram_len + 1;
1347
0
  } else {
1348
0
    apdu.data = crgram;
1349
0
    apdu.datalen = apdu.lc = crgram_len;
1350
0
  }
1351
0
  if (p1 != 0x00) {
1352
0
    apdu.resp = rbuf;
1353
0
    apdu.resplen = sizeof(rbuf);
1354
0
    apdu.le = MIN(card->max_recv_size, crgram_len);
1355
0
  }
1356
1357
  /* In MyEID 4.5.x, unwrapping with 2K RSA using APDU chaining doesn't work properly. Split the APDU in the old way in this case. */
1358
0
  if (p2 == 0x86 && crgram_len == 256 && priv && (!priv->cap_chaining || (card->version.fw_major == 45 && priv->sec_env != NULL && priv->sec_env->operation == SC_SEC_OPERATION_UNWRAP))) {
1359
0
    r = myeid_transmit_decipher_pi_split(card, &apdu, sbuf);
1360
0
  } else {
1361
0
    apdu.flags |= SC_APDU_FLAGS_CHAINING;
1362
0
    r = sc_transmit_apdu(card, &apdu);
1363
0
  }
1364
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1365
1366
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1367
0
  LOG_TEST_RET(card->ctx, r, "DECIPHER returned error");
1368
1369
0
  if (out && outlen) {
1370
0
    outlen = MIN(apdu.resplen, outlen);
1371
0
    memcpy(out, apdu.resp, outlen);
1372
0
  } else {
1373
0
    outlen = 0;
1374
0
  }
1375
0
  LOG_FUNC_RETURN(card->ctx, (int)outlen);
1376
0
}
1377
1378
static int myeid_decipher(struct sc_card *card, const u8 * crgram,
1379
    size_t crgram_len, u8 * out, size_t outlen)
1380
0
{
1381
0
  int r;
1382
0
  myeid_private_data_t* priv;
1383
1384
0
  LOG_FUNC_CALLED(card->ctx);
1385
1386
0
  assert(card != NULL && crgram != NULL && out != NULL);
1387
1388
0
  priv = (myeid_private_data_t*) card->drv_data;
1389
1390
0
  if (priv->sec_env && priv->sec_env->algorithm == SC_ALGORITHM_EC
1391
0
    && priv->sec_env->operation == SC_SEC_OPERATION_DERIVE
1392
0
    && priv->sec_env->algorithm_flags & SC_ALGORITHM_ECDH_CDH_RAW)
1393
0
  {
1394
0
    r = myeid_ecdh_derive(card, crgram, crgram_len, out, outlen);
1395
0
    priv->sec_env = NULL; /* clear after operation */
1396
0
    LOG_FUNC_RETURN(card->ctx, r);
1397
0
  }
1398
1399
0
  r = myeid_transmit_decipher(card, 0x80, 0x86, crgram, crgram_len, out, outlen);
1400
0
  LOG_FUNC_RETURN(card->ctx, r);
1401
0
}
1402
1403
1404
static int myeid_wrap_key(struct sc_card *card, u8 *out, size_t outlen)
1405
0
{
1406
0
  struct sc_context *ctx;
1407
0
  struct sc_apdu apdu;
1408
0
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
1409
0
  int r;
1410
1411
0
  assert(card != NULL);
1412
0
  ctx = card->ctx;
1413
0
  LOG_FUNC_CALLED(ctx);
1414
1415
  /* INS: 0x2A  PERFORM SECURITY OPERATION
1416
     P1:  0x84  Resp: Return a cryptogram
1417
   * P2:  0x00  The data field is absent */
1418
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0x2A, 0x84, 0x00);
1419
0
  apdu.resp = rbuf;
1420
0
  apdu.resplen = sizeof(rbuf);
1421
0
  apdu.le = sizeof(rbuf) <= 256 ? sizeof(rbuf) : 256;
1422
0
  apdu.lc = 0;
1423
1424
0
  r = sc_transmit_apdu(card, &apdu);
1425
0
  LOG_TEST_RET(ctx, r, "APDU transmit failed");
1426
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1427
0
  LOG_TEST_RET(ctx, r, "wrap key failed");
1428
1429
0
  if (apdu.resplen <= outlen && out != NULL)
1430
0
    memcpy(out, apdu.resp, apdu.resplen);
1431
1432
0
  LOG_FUNC_RETURN(ctx, (int)apdu.resplen);
1433
0
}
1434
1435
static int myeid_unwrap_key(struct sc_card *card, const u8 *crgram, size_t crgram_len)
1436
0
{
1437
0
  myeid_private_data_t* priv;
1438
0
  u8 p2 = 0x86; /* init P2 for asymmetric crypto by default.*/
1439
0
  int r;
1440
1441
0
  if (card == NULL || crgram == NULL)
1442
0
    return SC_ERROR_INVALID_ARGUMENTS;
1443
0
  priv = card->drv_data;
1444
1445
0
  LOG_FUNC_CALLED(card->ctx);
1446
1447
0
  if (crgram_len > MYEID_MAX_RSA_KEY_LEN / 8)
1448
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
1449
1450
0
  if (priv && priv->sec_env)
1451
0
  {
1452
0
    if (priv->sec_env->algorithm == SC_ALGORITHM_AES ||
1453
0
      priv->sec_env->algorithm == SC_ALGORITHM_3DES ||
1454
0
      priv->sec_env->algorithm == SC_ALGORITHM_DES)
1455
0
        p2 = 0x84;
1456
0
  }
1457
1458
0
  if (p2 == 0x84 && crgram_len > MYEID_MAX_APDU_DATA_LEN)
1459
0
    LOG_TEST_RET(card->ctx, SC_ERROR_WRONG_LENGTH, "Unwrapping symmetric data longer that 255 bytes is not supported\n");
1460
1461
  /* INS: 0x2A  PERFORM SECURITY OPERATION
1462
   * P1:  0x00  Do not expect response - the deciphered data will be placed into the target key EF.
1463
   * P2:  0x86  Cmd: Padding indicator byte followed by cryptogram
1464
   * P2:  0x84  Cmd: AES/3DES Cryptogram (plain value encoded in BER-TLV DO, but not including SM DOs) */
1465
0
  r = myeid_transmit_decipher(card, 0x00, p2, crgram, crgram_len, 0, 0);
1466
0
  LOG_FUNC_RETURN(card->ctx, r);
1467
0
}
1468
1469
1470
/* Write internal data, e.g. add default pin-records to pin */
1471
static int myeid_putdata(struct sc_card *card, struct sc_cardctl_myeid_data_obj* data_obj)
1472
1.11k
{
1473
1.11k
  int r;
1474
1.11k
  struct sc_apdu apdu;
1475
1476
1.11k
  LOG_FUNC_CALLED(card->ctx);
1477
1478
1.11k
  memset(&apdu, 0, sizeof(apdu));
1479
1.11k
  apdu.cse     = SC_APDU_CASE_3_SHORT;
1480
1.11k
  apdu.cla     = 0x00;
1481
1.11k
  apdu.ins     = 0xDA;
1482
1.11k
  apdu.p1      = data_obj->P1;
1483
1.11k
  apdu.p2      = data_obj->P2;
1484
1.11k
  apdu.lc      = data_obj->DataLen;
1485
1.11k
  apdu.datalen = data_obj->DataLen;
1486
1.11k
  apdu.data    = data_obj->Data;
1487
1488
1.11k
  r = sc_transmit_apdu(card, &apdu);
1489
1.11k
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1490
1491
1.11k
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1492
1.11k
  LOG_TEST_RET(card->ctx, r, "PUT_DATA returned error");
1493
1494
328
  LOG_FUNC_RETURN(card->ctx, r);
1495
328
}
1496
1497
/* Read internal data, e.g. get RSA public key */
1498
static int myeid_getdata(struct sc_card *card, struct sc_cardctl_myeid_data_obj* data_obj)
1499
69
{
1500
69
  int r;
1501
69
  struct sc_apdu apdu;
1502
1503
69
  LOG_FUNC_CALLED(card->ctx);
1504
1505
69
  memset(&apdu, 0, sizeof(apdu));
1506
69
  apdu.cse     = SC_APDU_CASE_2_SHORT;
1507
69
  apdu.cla     = 0x00;
1508
69
  apdu.ins     = 0xCA;    /* GET DATA */
1509
69
  apdu.p1      = data_obj->P1;
1510
69
  apdu.p2      = data_obj->P2;
1511
69
  apdu.lc      = 0;
1512
69
  apdu.datalen = 0;
1513
69
  apdu.data    = data_obj->Data;
1514
1515
69
  apdu.le      = card->max_recv_size;
1516
69
  apdu.resp    = data_obj->Data;
1517
69
  apdu.resplen = data_obj->DataLen;
1518
1519
69
  r = sc_transmit_apdu(card, &apdu);
1520
69
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1521
1522
66
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1523
66
  LOG_TEST_RET(card->ctx, r, "GET_DATA returned error");
1524
1525
59
  if (apdu.resplen > data_obj->DataLen)
1526
0
    r = SC_ERROR_WRONG_LENGTH;
1527
59
  else
1528
59
    data_obj->DataLen = apdu.resplen;
1529
1530
59
  LOG_FUNC_RETURN(card->ctx, r);
1531
59
}
1532
1533
static int myeid_loadkey(sc_card_t *card, unsigned mode, u8 *value, size_t value_len)
1534
71
{
1535
71
  myeid_private_data_t *priv = (myeid_private_data_t *) card->drv_data;
1536
71
  sc_apdu_t apdu;
1537
71
  u8 sbuf[MYEID_MAX_EXT_APDU_BUFFER_SIZE];
1538
71
  int r;
1539
1540
71
  LOG_FUNC_CALLED(card->ctx);
1541
71
  if (value_len == 0 || value == NULL)
1542
0
    return 0;
1543
1544
71
  if (mode == LOAD_KEY_MODULUS && value_len == 256 && !priv->cap_chaining)
1545
0
  {
1546
0
    mode = 0x88;
1547
0
    memset(&apdu, 0, sizeof(apdu));
1548
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xDA, 0x01, mode);
1549
1550
0
    apdu.cla     = 0x00;
1551
0
    apdu.data    = value;
1552
0
    apdu.datalen = 128;
1553
0
    apdu.lc      = 128;
1554
1555
0
    r = sc_transmit_apdu(card, &apdu);
1556
0
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1557
1558
0
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1559
0
    LOG_TEST_RET(card->ctx, r, "LOAD KEY returned error");
1560
1561
0
    mode = 0x89;
1562
0
    value += 128;
1563
0
    value_len -= 128;
1564
0
  }
1565
71
  else if ((mode & 0xff00) == 0 && mode != LOAD_KEY_PUBLIC_EXPONENT &&
1566
0
     value[0] != 0x00)
1567
0
  {
1568
    /* RSA components needing leading zero byte */
1569
0
    sbuf[0] = 0x0;
1570
0
    memcpy(&sbuf[1], value, value_len);
1571
0
    value = sbuf;
1572
0
    value_len ++;
1573
0
  }
1574
1575
71
  memset(&apdu, 0, sizeof(apdu));
1576
71
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xDA, 0x01, mode & 0xFF);
1577
71
  apdu.flags   = SC_APDU_FLAGS_CHAINING;
1578
71
  apdu.cla     = 0x00;
1579
71
  apdu.data    = value;
1580
71
  apdu.datalen = value_len;
1581
71
  apdu.lc      = value_len;
1582
1583
71
  r = sc_transmit_apdu(card, &apdu);
1584
71
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1585
1586
65
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1587
65
  LOG_FUNC_RETURN(card->ctx, r);
1588
65
}
1589
1590
/* Generate or store a key */
1591
static int myeid_generate_store_key(struct sc_card *card,
1592
  struct sc_cardctl_myeid_gen_store_key_info *data)
1593
174
{
1594
174
  struct  sc_apdu apdu;
1595
174
  u8  sbuf[SC_MAX_APDU_BUFFER_SIZE];
1596
174
  int r=0,len;
1597
1598
174
  LOG_FUNC_CALLED(card->ctx);
1599
  /* Setup key-generation parameters */
1600
174
  if (data->op_type == OP_TYPE_GENERATE)
1601
86
  {
1602
86
    len = 0;
1603
86
    memset(&apdu, 0, sizeof(apdu));
1604
1605
86
    if(data->key_type == SC_CARDCTL_MYEID_KEY_RSA)
1606
45
    {
1607
45
        sbuf[len++] = 0x30;
1608
45
        sbuf[len++] = 0x05;
1609
45
        sbuf[len++] = 0x81;
1610
45
        sbuf[len++] = data->pubexp_len;
1611
1612
45
        memcpy(sbuf + len, data->pubexp, data->pubexp_len);
1613
45
        len += data->pubexp_len;
1614
45
      sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x46, 0x00, 0x00);
1615
45
      apdu.data    = sbuf;
1616
45
    }
1617
41
    else if(data->key_type == SC_CARDCTL_MYEID_KEY_EC) {
1618
1619
41
      sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x46, 0x00, 0x00);
1620
1621
41
      apdu.data    = NULL;
1622
41
      apdu.resp  = sbuf;
1623
41
      apdu.resplen = 0x00;
1624
41
      apdu.le    = 0x00;
1625
41
    }
1626
1627
86
    apdu.cla     = 0x00;
1628
86
    apdu.datalen = len;
1629
86
    apdu.lc      = len;
1630
1631
86
    r = sc_transmit_apdu(card, &apdu);
1632
86
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1633
1634
83
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1635
83
    LOG_TEST_RET(card->ctx, r, "GENERATE_KEY returned error");
1636
83
  }
1637
88
  else
1638
88
  {
1639
88
    if(data->key_type == SC_CARDCTL_MYEID_KEY_RSA)
1640
0
    {
1641
0
      if((r=myeid_loadkey(card, LOAD_KEY_PRIME_P,
1642
0
        data->primep, data->primep_len)) >= 0 &&
1643
0
      (r=myeid_loadkey(card, LOAD_KEY_PRIME_Q,
1644
0
        data->primeq, data->primeq_len)) >= 0 &&
1645
0
      (r=myeid_loadkey(card, LOAD_KEY_DP1,
1646
0
        data->dp1, data->dp1_len)) >= 0 &&
1647
0
      (r=myeid_loadkey(card, LOAD_KEY_DQ1,
1648
0
        data->dq1, data->dq1_len)) >= 0 &&
1649
0
      (r=myeid_loadkey(card, LOAD_KEY_INVQ,
1650
0
        data->invq, data->invq_len)) >= 0 &&
1651
0
      (r=myeid_loadkey(card, LOAD_KEY_MODULUS,
1652
0
        data->mod, data->key_len_bits)) >= 0 &&
1653
0
      (r=myeid_loadkey(card, LOAD_KEY_PUBLIC_EXPONENT,
1654
0
        data->pubexp, data->pubexp_len)) >= 0)
1655
0
        LOG_FUNC_RETURN(card->ctx, r);
1656
0
    }
1657
88
    else if(data->key_type == SC_CARDCTL_MYEID_KEY_EC) {
1658
0
      if((r = myeid_loadkey(card, LOAD_KEY_EC_PRIVATE, data->d,
1659
0
          data->d_len)) >= 0 &&
1660
0
        (r = myeid_loadkey(card, LOAD_KEY_EC_PUBLIC, data->ecpublic_point,
1661
0
          data->ecpublic_point_len)) >= 0)
1662
0
      LOG_FUNC_RETURN(card->ctx, r);
1663
0
    }
1664
88
    else if(data->key_type == SC_CARDCTL_MYEID_KEY_AES ||
1665
71
      data->key_type == SC_CARDCTL_MYEID_KEY_DES) {
1666
71
      if((r = myeid_loadkey(card, LOAD_KEY_SYMMETRIC, data->d,
1667
71
          data->d_len)) >= 0)
1668
71
      LOG_FUNC_RETURN(card->ctx, r);
1669
71
    }
1670
88
  }
1671
1672
111
  LOG_FUNC_RETURN(card->ctx, r);
1673
111
}
1674
1675
static int myeid_activate_card(struct sc_card *card)
1676
1.23k
{
1677
1.23k
  int r;
1678
1.23k
  u8 sbuf[] ="\xA0\x00\x00\x00\x63\x50\x4B\x43\x53\x2D\x31\x35";
1679
1.23k
  sc_apdu_t apdu;
1680
1681
1.23k
  LOG_FUNC_CALLED(card->ctx);
1682
1.23k
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x44, 0x04, 0x00);
1683
1.23k
  apdu.cla     = 0x00;
1684
1.23k
  apdu.data    = sbuf;
1685
1.23k
  apdu.datalen = 0x0C;
1686
1.23k
  apdu.lc      = 0x0C;
1687
1688
1.23k
  r = sc_transmit_apdu(card, &apdu);
1689
1.23k
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1690
1691
1.23k
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1692
1.23k
  LOG_TEST_RET(card->ctx, r, "ACTIVATE_APPLET returned error");
1693
1694
46
  LOG_FUNC_RETURN(card->ctx, r);
1695
46
}
1696
1697
static int myeid_get_info(struct sc_card *card, u8 *rbuf, size_t buflen)
1698
69.2k
{
1699
69.2k
  sc_apdu_t apdu;
1700
69.2k
  int r;
1701
1702
69.2k
  LOG_FUNC_CALLED(card->ctx);
1703
1704
69.2k
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xca, 0x01, 0xA0);
1705
69.2k
  apdu.resp    = rbuf;
1706
69.2k
  apdu.resplen = buflen;
1707
69.2k
  apdu.le      = buflen;
1708
1709
69.2k
  r = sc_transmit_apdu(card, &apdu);
1710
69.2k
  LOG_TEST_RET(card->ctx, r,  "APDU transmit failed");
1711
1712
68.5k
  if (apdu.sw1 != 0x90 || apdu.sw2 != 0x00)
1713
63.7k
    return SC_ERROR_INTERNAL;
1714
1715
4.82k
  if (apdu.resplen != 20)
1716
3.45k
  {
1717
3.45k
    sc_log(card->ctx, "Unexpected response to GET DATA (applet info)");
1718
3.45k
    return SC_ERROR_INTERNAL;
1719
3.45k
  }
1720
1721
  /* store the applet version */
1722
1.37k
  card->version.fw_major = rbuf[5] * 10 + rbuf[6];
1723
1.37k
  card->version.fw_minor = rbuf[7];
1724
  /* add version to name */
1725
1.37k
  snprintf(card_name_buf, sizeof(card_name_buf),
1726
1.37k
      "%s %d.%d.%d", card->name, rbuf[5], rbuf[6], rbuf[7]);
1727
1.37k
  card->name = card_name_buf;
1728
1729
1.37k
  LOG_FUNC_RETURN(card->ctx, r);
1730
1.37k
}
1731
1732
static int myeid_get_serialnr(sc_card_t *card, sc_serial_number_t *serial)
1733
1.01k
{
1734
1.01k
  int r;
1735
1.01k
  u8  rbuf[256];
1736
1737
1.01k
  LOG_FUNC_CALLED(card->ctx);
1738
1739
  /* if number cached, get it
1740
  if(card->serialnr.value) {
1741
    memcpy(serial, &card->serialnr, sizeof(*serial));
1742
    LOG_FUNC_RETURN(card->ctx, r);
1743
  }*/
1744
1745
  /* get number from card */
1746
1.01k
  r = myeid_get_info(card, rbuf, sizeof(rbuf));
1747
1.01k
  LOG_TEST_RET(card->ctx, r,  "Get applet info failed");
1748
1749
  /* cache serial number */
1750
406
  memcpy(card->serialnr.value, &rbuf[8], 10);
1751
406
  card->serialnr.len = 10;
1752
1753
  /* copy and return serial number */
1754
406
  memcpy(serial, &card->serialnr, sizeof(*serial));
1755
1756
406
  LOG_FUNC_RETURN(card->ctx, r);
1757
406
}
1758
1759
static int
1760
myeid_get_change_counter(sc_card_t *card, size_t *change_counter)
1761
66.6k
{
1762
66.6k
  int r;
1763
66.6k
  u8 rbuf[256];
1764
1765
66.6k
  LOG_FUNC_CALLED(card->ctx);
1766
1767
  /* get change counter from card */
1768
66.6k
  r = myeid_get_info(card, rbuf, sizeof(rbuf));
1769
66.6k
  LOG_TEST_RET(card->ctx, r, "Get applet info failed");
1770
1771
226
  *change_counter = rbuf[18] * 256 + rbuf[19];
1772
1773
226
  LOG_FUNC_RETURN(card->ctx, r);
1774
226
}
1775
1776
/*
1777
 Get information of features that the card supports. MyEID 4.x cards are available on different
1778
 hardware and maximum key sizes cannot be determined simply from the version number anymore.
1779
 */
1780
static int myeid_get_card_caps(struct sc_card *card, myeid_card_caps_t* card_caps)
1781
633
{
1782
633
  sc_apdu_t apdu;
1783
633
  int r;
1784
633
  unsigned char rbuf[SC_MAX_APDU_BUFFER_SIZE];
1785
1786
633
  LOG_FUNC_CALLED(card->ctx);
1787
1788
633
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xca, 0x01, 0xAA);
1789
633
  apdu.resp    = rbuf;
1790
633
  apdu.resplen = sizeof(myeid_card_caps_t);
1791
633
  apdu.le      = sizeof(myeid_card_caps_t);
1792
1793
633
  r = sc_transmit_apdu(card, &apdu);
1794
633
  LOG_TEST_RET(card->ctx, r,  "APDU transmit failed");
1795
1796
628
  if (apdu.sw1 != 0x90 || apdu.sw2 != 0x00)
1797
163
    return SC_ERROR_INTERNAL;
1798
1799
465
  if (apdu.resplen < 11) {
1800
93
    sc_log(card->ctx, "Unexpected response to GET DATA (MyEIC card capabilities)");
1801
93
    return SC_ERROR_INTERNAL;
1802
93
  }
1803
1804
372
  card_caps->card_caps_ver = rbuf[0];
1805
  /* the card returns big endian values */
1806
372
  card_caps->card_supported_features = (unsigned short) rbuf[1] << 8 | rbuf[2];
1807
372
  card_caps->max_rsa_key_length = (unsigned short) rbuf[3] << 8 | rbuf[4];
1808
372
  card_caps->max_des_key_length = (unsigned short) rbuf[5] << 8 | rbuf[6];
1809
372
  card_caps->max_aes_key_length = (unsigned short) rbuf[7] << 8 | rbuf[8];
1810
372
  card_caps->max_ecc_key_length = (unsigned short) rbuf[9] << 8 | rbuf[10];
1811
1812
372
  LOG_FUNC_RETURN(card->ctx, r);
1813
372
}
1814
1815
static int myeid_card_ctl(struct sc_card *card, unsigned long cmd, void *ptr)
1816
73.9k
{
1817
73.9k
  int r = SC_ERROR_NOT_SUPPORTED;
1818
73.9k
  LOG_FUNC_CALLED(card->ctx);
1819
1820
73.9k
  switch(cmd) {
1821
1.11k
  case SC_CARDCTL_MYEID_PUTDATA:
1822
1.11k
    r = myeid_putdata(card,
1823
1.11k
      (struct sc_cardctl_myeid_data_obj*) ptr);
1824
1.11k
    break;
1825
69
  case SC_CARDCTL_MYEID_GETDATA:
1826
69
    r = myeid_getdata(card,
1827
69
      (struct sc_cardctl_myeid_data_obj*) ptr);
1828
69
    break;
1829
174
  case SC_CARDCTL_MYEID_GENERATE_STORE_KEY:
1830
174
    r = myeid_generate_store_key(card,
1831
174
      (struct sc_cardctl_myeid_gen_store_key_info *) ptr);
1832
174
    break;
1833
1.23k
  case SC_CARDCTL_MYEID_ACTIVATE_CARD:
1834
1.23k
    r = myeid_activate_card(card);
1835
1.23k
    break;
1836
1.01k
  case SC_CARDCTL_GET_SERIALNR:
1837
1.01k
    r = myeid_get_serialnr(card, (sc_serial_number_t *)ptr);
1838
1.01k
    break;
1839
66.6k
  case SC_CARDCTL_GET_CHANGE_COUNTER:
1840
66.6k
    r = myeid_get_change_counter(card, (size_t *)ptr);
1841
66.6k
    break;
1842
23
  case SC_CARDCTL_GET_DEFAULT_KEY:
1843
3.64k
  case SC_CARDCTL_LIFECYCLE_SET:
1844
3.64k
  case SC_CARDCTL_LIFECYCLE_GET:
1845
3.64k
    break;
1846
73.9k
  }
1847
73.9k
  LOG_FUNC_RETURN(card->ctx, r);
1848
73.9k
}
1849
1850
static int myeid_finish(sc_card_t * card)
1851
1.57k
{
1852
1.57k
  struct myeid_private_data *priv = (struct myeid_private_data *) card->drv_data;
1853
1.57k
  free(priv);
1854
1.57k
  return SC_SUCCESS;
1855
1.57k
}
1856
1857
static int
1858
myeid_enc_dec_sym(struct sc_card *card, const u8 *data, size_t datalen,
1859
    u8 *out, size_t *outlen, int decipher)
1860
0
{
1861
1862
0
  struct sc_context *ctx;
1863
1864
0
  struct sc_apdu apdu;
1865
0
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
1866
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
1867
0
  u8 *sdata;
1868
0
  int r, padding = 0, cbc = 0;
1869
1870
0
  size_t block_size;
1871
0
  size_t len, rest_len;
1872
0
  size_t return_len = 0;
1873
1874
0
  size_t max_apdu_datalen;
1875
0
  size_t apdu_datalen;
1876
1877
0
  assert(card != NULL);
1878
1879
0
  ctx = card->ctx;
1880
0
  LOG_FUNC_CALLED(ctx);
1881
1882
0
  myeid_private_data_t *priv;
1883
0
  priv = (myeid_private_data_t *)card->drv_data;
1884
1885
  /* How many cipher blocks will fit in the APDU. We do not use the APDU chaining
1886
   * mechanism from OpenSC, because we need the size of the APDU data block
1887
   * to match a multiple of the cipher block size */
1888
1889
0
  max_apdu_datalen = sc_get_max_send_size(card);
1890
0
  if (max_apdu_datalen > sc_get_max_recv_size(card))
1891
0
    max_apdu_datalen = sc_get_max_recv_size(card);
1892
1893
0
  if (max_apdu_datalen > SC_MAX_APDU_BUFFER_SIZE)
1894
0
    max_apdu_datalen = SC_MAX_APDU_BUFFER_SIZE;
1895
1896
0
  sc_log(ctx, "algorithm %lu algorithm_flags %lx", priv->algorithm, priv->algorithm_flags);
1897
1898
  /* for C_Encrypt/C_EncryptUpdate/C_EncryptFinalize/C_Decrypt/C_DecryptUpdate/C_DecryptFinalize
1899
   * the 'outlen' is always not NULL (src/pkcs11/framework-pkcs15.c).
1900
   * For C_EncryptInit and C_DecrytpInit the 'outlen' is set to NULL
1901
   */
1902
0
  if (outlen == NULL) {
1903
    /* C_EncryptInit/C_DecryptInit - clear buffers */
1904
0
    sc_log(ctx, "%s (symmetric key) initialized", decipher ? "C_DecryptInit" : "C_EncryptInit");
1905
0
    priv->sym_crypt_buffer_len = 0;
1906
0
    priv->sym_plain_buffer_len = 0;
1907
0
    return SC_SUCCESS;
1908
0
  }
1909
1910
0
  switch (priv->algorithm) {
1911
0
  case SC_ALGORITHM_AES:
1912
0
    block_size = 16;
1913
0
    if (priv->algorithm_flags & SC_ALGORITHM_AES_ECB) {
1914
0
      padding = 0;
1915
0
      cbc = 0;
1916
0
    } else if (priv->algorithm_flags & SC_ALGORITHM_AES_CBC) {
1917
0
      padding = 0;
1918
0
      cbc = 1;
1919
0
    } else if (priv->algorithm_flags & SC_ALGORITHM_AES_CBC_PAD) {
1920
0
      padding = 1;
1921
0
      cbc = 1;
1922
0
    }
1923
0
    break;
1924
0
  default:
1925
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
1926
0
  }
1927
1928
  /* MyEID: ECB APDU must match exact cipher block size in CBC
1929
   * mode up to 240 bytes can be handled in one APDU
1930
   * round max_apdu_datalen to multiple of block_size (CBC mode) */
1931
1932
0
  if (cbc)
1933
0
    max_apdu_datalen -= max_apdu_datalen % block_size;
1934
0
  else
1935
0
    max_apdu_datalen = block_size;
1936
1937
  /* Maybe we have more input data (from previous PSO operation). */
1938
0
  rest_len = priv->sym_crypt_buffer_len;
1939
1940
  /* no input data from application (this is C_EncryptFinalize/C_DecryptFinalize */
1941
0
  if (data == NULL) {
1942
0
    if (datalen != 0)
1943
0
      LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_LENGTH);
1944
0
    if (decipher) {
1945
      /* C_DecryptFinalize */
1946
      /* decrypted buffer size must match the block size */
1947
0
      if (priv->sym_plain_buffer_len != block_size)
1948
0
        LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_LENGTH);
1949
      /* do we have any encrypted data left? */
1950
0
      if (rest_len)
1951
0
        LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_LENGTH);
1952
1953
0
      return_len = block_size;
1954
0
      if (padding) {
1955
        /* check padding */
1956
0
        uint8_t i, pad_byte = *(priv->sym_plain_buffer + block_size - 1);
1957
1958
0
        sc_log(ctx, "Found padding byte %02x", pad_byte);
1959
0
        if (pad_byte == 0 || pad_byte > block_size)
1960
0
          LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_PADDING);
1961
0
        sdata = priv->sym_plain_buffer + block_size;
1962
0
        for (i = 0; i < pad_byte; i++)
1963
0
          if (*(--sdata) != pad_byte)
1964
0
            LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_PADDING);
1965
0
        return_len = block_size - pad_byte;
1966
0
      }
1967
      /* application can request buffer size or actual buffer size is too small */
1968
0
      if (out == NULL) {
1969
0
        *outlen = return_len;
1970
0
        LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1971
0
      }
1972
0
      if (return_len > *outlen)
1973
0
        LOG_FUNC_RETURN(ctx, SC_ERROR_BUFFER_TOO_SMALL);
1974
0
      *outlen = return_len;
1975
0
      memcpy(out, priv->sym_plain_buffer, return_len);
1976
0
      sc_log(ctx, "C_DecryptFinal %zu bytes", *outlen);
1977
0
      return SC_SUCCESS;
1978
0
    } else {
1979
      /* C_EncryptFinalize */
1980
0
      if (padding) {
1981
0
        uint8_t pad_byte = block_size - rest_len;
1982
0
        sc_log(ctx, "Generating padding, padding byte: %d", pad_byte);
1983
0
        sdata = priv->sym_crypt_buffer + rest_len;
1984
0
        memset(sdata, pad_byte, pad_byte);
1985
0
        rest_len = block_size;
1986
1987
0
      } else if (rest_len) {
1988
0
        LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_LENGTH);
1989
0
      }
1990
      /* fall through - encipher last block */
1991
0
    }
1992
0
  }
1993
  /* check output buffer size */
1994
0
  len = datalen + rest_len;
1995
1996
0
  sc_log(ctx, "datalen=%zu rest_len=%zu len=%zu outlen=%zu", datalen, rest_len, len, *outlen);
1997
  /* there is block_size bytes space that can be saved to next run */
1998
0
  len -= (len % block_size);
1999
2000
  /* application can request buffer size or actual buffer size is too small */
2001
0
  *outlen = len;
2002
0
  if (out == NULL)
2003
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
2004
  /* application buffer is too small */
2005
0
  if (*outlen < len)
2006
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_BUFFER_TOO_SMALL);
2007
2008
  /* main loop */
2009
0
  while (len >= block_size) {
2010
0
    if (!decipher)
2011
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x84, 0x80);
2012
0
    else
2013
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x80, 0x84);
2014
0
    apdu.cla = 0;
2015
2016
0
    if (len > max_apdu_datalen)
2017
0
      apdu_datalen = max_apdu_datalen;
2018
0
    else
2019
0
      apdu_datalen = len;
2020
2021
0
    if (cbc)
2022
0
      apdu.cla = 0x10;
2023
2024
0
    len -= apdu_datalen;
2025
0
    sdata = sbuf;
2026
2027
0
    apdu.le = apdu_datalen;
2028
0
    apdu.lc = apdu_datalen;
2029
0
    apdu.datalen = apdu_datalen;
2030
0
    apdu.data = sbuf;
2031
0
    apdu.resplen = sizeof(rbuf);
2032
0
    apdu.resp = rbuf;
2033
2034
    /* do we have any data from the previous step ? */
2035
0
    if (rest_len) {
2036
0
      memcpy(sbuf, priv->sym_crypt_buffer, rest_len);
2037
0
      sdata += rest_len;
2038
0
      apdu_datalen -= rest_len;
2039
0
      priv->sym_crypt_buffer_len = 0;
2040
0
      rest_len = 0;
2041
0
    }
2042
0
    if (data) {
2043
0
      memcpy(sdata, data, apdu_datalen);
2044
0
      data += apdu_datalen;
2045
0
      datalen -= apdu_datalen;
2046
0
    }
2047
0
    r = sc_transmit_apdu(card, &apdu);
2048
0
    LOG_TEST_RET(ctx, r, "APDU transmit failed");
2049
0
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
2050
0
    LOG_TEST_RET(ctx, r, "decrypt_sym/encrypt_sym failed");
2051
0
    if (apdu.resplen != apdu.datalen)
2052
0
      LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_LENGTH);
2053
0
    memcpy(out, apdu.resp, apdu.resplen);
2054
0
    out += apdu.resplen;
2055
0
    return_len += apdu.resplen;
2056
0
  }
2057
  /* last block is stored in buffer and is returned to application
2058
   * in next call to C_DecryptUpdate or C_DecryptFinal. This allow us
2059
   * to compute how many bytes is to be returned after padding removal.
2060
   * Whole handling of this is here, because "data" and "out" buffer
2061
   * can be in the same place.
2062
   */
2063
0
  if (decipher) {
2064
0
    uint8_t tmp_buf[16];
2065
0
    if (return_len >= block_size) {
2066
      /* save last block to temp buffer */
2067
0
      memcpy(tmp_buf, out - block_size, block_size);
2068
0
      if (priv->sym_plain_buffer_len) {
2069
        /* insert previous last block to output buffer */
2070
0
        sc_log(ctx, "inserting block from previous decrypt");
2071
0
        memmove(out - return_len + block_size, out - return_len, return_len - block_size);
2072
0
        memcpy(out - return_len, priv->sym_plain_buffer, block_size);
2073
0
      } else
2074
0
        return_len -= block_size;
2075
      /* save last (decrypted) block */
2076
0
      memcpy(priv->sym_plain_buffer, tmp_buf, block_size);
2077
0
      priv->sym_plain_buffer_len = block_size;
2078
2079
0
    } else
2080
0
      priv->sym_plain_buffer_len = 0;
2081
0
  }
2082
  /* save rest of data for next run */
2083
0
  priv->sym_crypt_buffer_len = datalen;
2084
0
  sc_log(ctx, "rest data len = %zu", datalen);
2085
0
  if (data)
2086
0
    memcpy(priv->sym_crypt_buffer, data, datalen);
2087
0
  sc_log(ctx, "return data len = %zu", return_len);
2088
0
  *outlen = return_len;
2089
0
  return SC_SUCCESS;
2090
0
}
2091
2092
static int
2093
myeid_encrypt_sym(struct sc_card *card, const u8 *data, size_t datalen, u8 *out, size_t *outlen)
2094
0
{
2095
0
  return myeid_enc_dec_sym(card, data, datalen, out, outlen, 0);
2096
0
}
2097
2098
static int
2099
myeid_decrypt_sym(struct sc_card *card, const u8 *data, size_t datalen, u8 *out, size_t *outlen)
2100
0
{
2101
0
  return myeid_enc_dec_sym(card, data, datalen, out, outlen, 1);
2102
0
}
2103
2104
static struct sc_card_driver * sc_get_driver(void)
2105
19.7k
{
2106
19.7k
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
2107
2108
19.7k
  if (iso_ops == NULL)
2109
1
    iso_ops = iso_drv->ops;
2110
2111
19.7k
  myeid_ops     = *iso_drv->ops;
2112
19.7k
  myeid_ops.match_card    = myeid_match_card;
2113
19.7k
  myeid_ops.init      = myeid_init;
2114
19.7k
  myeid_ops.finish    = myeid_finish;
2115
  /* no record oriented file services */
2116
19.7k
  myeid_ops.read_record   = NULL;
2117
19.7k
  myeid_ops.write_record    = NULL;
2118
19.7k
  myeid_ops.append_record   = NULL;
2119
19.7k
  myeid_ops.update_record   = NULL;
2120
19.7k
  myeid_ops.select_file   = myeid_select_file;
2121
19.7k
  myeid_ops.get_response    = iso_ops->get_response;
2122
19.7k
  myeid_ops.logout    = myeid_logout;
2123
19.7k
  myeid_ops.create_file   = myeid_create_file;
2124
19.7k
  myeid_ops.delete_file   = myeid_delete_file;
2125
19.7k
  myeid_ops.list_files    = myeid_list_files;
2126
19.7k
  myeid_ops.set_security_env  = myeid_set_security_env;
2127
19.7k
  myeid_ops.compute_signature = myeid_compute_signature;
2128
19.7k
  myeid_ops.decipher    = myeid_decipher;
2129
19.7k
  myeid_ops.process_fci   = myeid_process_fci;
2130
19.7k
  myeid_ops.card_ctl    = myeid_card_ctl;
2131
19.7k
  myeid_ops.pin_cmd   = myeid_pin_cmd;
2132
19.7k
  myeid_ops.wrap      = myeid_wrap_key;
2133
19.7k
  myeid_ops.unwrap    = myeid_unwrap_key;
2134
19.7k
  myeid_ops.encrypt_sym   = myeid_encrypt_sym;
2135
19.7k
  myeid_ops.decrypt_sym   = myeid_decrypt_sym;
2136
19.7k
  return &myeid_drv;
2137
19.7k
}
2138
2139
struct sc_card_driver * sc_get_myeid_driver(void)
2140
19.7k
{
2141
19.7k
  return sc_get_driver();
2142
19.7k
}
2143