Coverage Report

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