Coverage Report

Created: 2026-12-03 06:15

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