Coverage Report

Created: 2026-07-10 06:04

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
0
#define LOAD_KEY_MODULUS    0x0080
36
0
#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
0
#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
0
#define MYEID_CARD_CAP_RSA    0x01
51
0
#define MYEID_CARD_CAP_3DES   0x02
52
0
#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
0
{
123
0
  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
0
  if (len >= 5) {
127
0
    if (!memcmp(&card->reader->atr_info.hist_bytes[len - 5], "MyEID", 5)) {
128
0
      sc_log(card->ctx, "Matched MyEID card");
129
0
      card->type = SC_CARD_TYPE_MYEID_GENERIC;
130
0
      return 1;
131
0
    }
132
    /* The software implementation of MyEID is identified by OsEID bytes */
133
0
    if (!memcmp(&card->reader->atr_info.hist_bytes[len - 5], "OsEID", 5)) {
134
0
      sc_log(card->ctx, "Matched OsEID card");
135
0
      card->type = SC_CARD_TYPE_MYEID_OSEID;
136
0
      return 1;
137
0
    }
138
0
  }
139
0
  return 0;
140
0
}
141
142
static int myeid_load_options(sc_context_t *ctx, myeid_private_data_t *priv)
143
0
{
144
0
  int r;
145
0
  size_t i, j;
146
0
  scconf_block **found_blocks, *block;
147
148
0
  if (!ctx || !priv) {
149
0
    r = SC_ERROR_INTERNAL;
150
0
    goto err;
151
0
  }
152
0
  priv->disable_hw_pkcs1_padding = 0;
153
0
  for (i = 0; ctx->conf_blocks[i]; i++) {
154
0
    found_blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i],
155
0
        "card_driver", "myeid");
156
0
    if (!found_blocks)
157
0
      continue;
158
0
    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
0
    free(found_blocks);
163
0
  }
164
0
  r = SC_SUCCESS;
165
166
0
err:
167
0
  return r;
168
0
}
169
170
static int myeid_init(struct sc_card *card)
171
0
{
172
0
  unsigned long flags = 0, ext_flags = 0;
173
0
  myeid_private_data_t *priv;
174
0
  u8 appletInfo[20];
175
0
  size_t appletInfoLen;
176
0
  myeid_card_caps_t card_caps;
177
0
  static struct sc_aid myeid_aid = { "\xA0\x00\x00\x00\x63\x50\x4B\x43\x53\x2D\x31\x35", 0x0C };
178
0
  int rv = 0;
179
0
  void *old_drv_data = card->drv_data;
180
181
0
  LOG_FUNC_CALLED(card->ctx);
182
183
0
  switch (card->type) {
184
0
  case SC_CARD_TYPE_MYEID_OSEID:
185
0
    card->name = oseid_card_name;
186
0
    break;
187
0
  case SC_CARD_TYPE_MYEID_GENERIC:
188
0
    card->name = myeid_card_name;
189
0
    break;
190
0
  default:
191
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_CARD);
192
0
  }
193
194
0
  priv = calloc(1, sizeof(myeid_private_data_t));
195
196
0
  if (!priv)
197
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
198
199
0
  rv = myeid_load_options (card->ctx, priv);
200
0
  LOG_TEST_GOTO_ERR(card->ctx, rv, "Unable to read options from opensc.conf");
201
202
0
  priv->card_state = SC_FILE_STATUS_CREATION;
203
0
  card->drv_data = priv;
204
205
  /* Ensure that the MyEID applet is selected. */
206
0
  rv = iso7816_select_aid(card, myeid_aid.value, myeid_aid.len, NULL, NULL);
207
0
  LOG_TEST_GOTO_ERR(card->ctx, rv, "Failed to select MyEID applet.");
208
209
  /* find out MyEID version */
210
211
0
  appletInfoLen = 20;
212
213
0
  if (0 > myeid_get_info(card, appletInfo, appletInfoLen))
214
0
    LOG_TEST_GOTO_ERR(card->ctx, SC_ERROR_INVALID_CARD, "Failed to get MyEID applet information.");
215
216
0
  priv->change_counter = appletInfo[19] | appletInfo[18] << 8;
217
218
0
  memset(&card_caps, 0, sizeof(myeid_card_caps_t));
219
0
  card_caps.max_ecc_key_length = 256;
220
0
  card_caps.max_rsa_key_length = 2048;
221
222
0
  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
0
      if (myeid_get_card_caps(card, &card_caps) != SC_SUCCESS) {
226
0
      sc_log(card->ctx, "Failed to get card capabilities. Using default max ECC key length 256.");
227
0
      }
228
0
  }
229
230
0
  flags = SC_ALGORITHM_RSA_RAW | SC_ALGORITHM_ONBOARD_KEY_GEN;
231
0
  if (priv->disable_hw_pkcs1_padding == 0)
232
0
    flags |= SC_ALGORITHM_RSA_PAD_PKCS1;
233
0
  flags |= SC_ALGORITHM_RSA_HASH_NONE;
234
235
0
  _sc_card_add_rsa_alg(card,  512, flags, 0);
236
0
  _sc_card_add_rsa_alg(card,  768, flags, 0);
237
0
  _sc_card_add_rsa_alg(card, 1024, flags, 0);
238
0
  _sc_card_add_rsa_alg(card, 1536, flags, 0);
239
0
  _sc_card_add_rsa_alg(card, 2048, flags, 0);
240
241
0
  if (card_caps.card_supported_features & MYEID_CARD_CAP_RSA) {
242
0
    if (card_caps.max_rsa_key_length >= 3072)
243
0
      _sc_card_add_rsa_alg(card, 3072, flags, 0);
244
0
    if (card_caps.max_rsa_key_length >= 4096)
245
0
      _sc_card_add_rsa_alg(card, 4096, flags, 0);
246
0
  }
247
248
  /* show ECC algorithms if the applet version of the inserted card supports them */
249
0
  if (card->version.fw_major >= 35) {
250
0
    int i;
251
252
0
    flags = SC_ALGORITHM_ECDSA_RAW | SC_ALGORITHM_ECDH_CDH_RAW | SC_ALGORITHM_ONBOARD_KEY_GEN;
253
0
    flags |= SC_ALGORITHM_ECDSA_HASH_NONE;
254
0
    ext_flags = SC_ALGORITHM_EXT_EC_NAMEDCURVE | SC_ALGORITHM_EXT_EC_UNCOMPRESES;
255
256
0
    for (i=0; ec_curves[i].curve_name != NULL; i++) {
257
0
      if (card_caps.max_ecc_key_length >= ec_curves[i].size)
258
0
        _sc_card_add_ec_alg(card, ec_curves[i].size, flags, ext_flags, &ec_curves[i].curve_oid);
259
0
    }
260
0
  }
261
262
  /* show supported symmetric algorithms */
263
0
  flags = 0;
264
0
  if (card_caps.card_supported_features & MYEID_CARD_CAP_3DES) {
265
0
    if (card_caps.max_des_key_length >= 64)
266
0
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_DES, 64, flags);
267
0
    if (card_caps.max_des_key_length >= 128)
268
0
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_3DES, 128, flags);
269
0
    if (card_caps.max_des_key_length >= 192)
270
0
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_3DES, 192, flags);
271
0
  }
272
0
  if (card_caps.card_supported_features & MYEID_CARD_CAP_AES) {
273
0
    if (card_caps.max_aes_key_length >= 128)
274
0
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_AES, 128, flags);
275
0
    if (card_caps.max_aes_key_length >= 256)
276
0
      _sc_card_add_symmetric_alg(card, SC_ALGORITHM_AES, 256, flags);
277
0
  }
278
279
  /* State that we have an RNG */
280
0
  card->caps |= SC_CARD_CAP_RNG | SC_CARD_CAP_ISO7816_PIN_INFO;
281
282
0
  if ((card->version.fw_major == 40 && card->version.fw_minor >= 10 )
283
0
    || card->version.fw_major >= 41)
284
0
    card->caps |= SC_CARD_CAP_WRAP_KEY | SC_CARD_CAP_UNWRAP_KEY
285
0
         | SC_CARD_CAP_ONCARD_SESSION_OBJECTS;
286
287
0
  if (card->version.fw_major >= 45)
288
0
    priv->cap_chaining = 1;
289
0
  if (card->version.fw_major >= 40)
290
0
    card->max_recv_size = 256;
291
0
  else
292
0
    card->max_recv_size = 255;
293
0
  card->max_send_size = 255;
294
295
0
  rv = SC_SUCCESS;
296
297
0
err:
298
0
  if (rv < 0) {
299
0
    free(priv);
300
0
    card->drv_data = old_drv_data;
301
0
  }
302
303
0
  LOG_FUNC_RETURN(card->ctx, rv);
304
0
}
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
0
{
310
0
  if (NULL == e)
311
0
    return 0x00;
312
0
  switch (e->method) {
313
0
  case SC_AC_NONE:
314
0
    return 0x00;
315
0
  case SC_AC_CHV:
316
0
  case SC_AC_TERM:
317
0
  case SC_AC_AUT:
318
0
    if (e->key_ref == SC_AC_KEY_REF_NONE)
319
0
      return 0x00;
320
0
    if (e->key_ref < 1 || e->key_ref > 14)
321
0
      return 0x00;
322
0
    return e->key_ref;
323
0
  case SC_AC_NEVER:
324
0
    return 0x0F;
325
0
  }
326
0
  return 0x00;
327
0
}
328
329
static void add_acl_entry(struct sc_file *file, int op, u8 byte)
330
0
{
331
0
  unsigned int method, key_ref = SC_AC_KEY_REF_NONE;
332
333
0
  switch (byte)
334
0
  {
335
0
  case 0:
336
0
    method = SC_AC_NONE;
337
0
    break;
338
0
  case 15:
339
0
    method = SC_AC_NEVER;
340
0
    break;
341
0
  default:
342
0
    method = SC_AC_CHV;
343
0
    key_ref = byte;
344
0
    break;
345
0
  }
346
0
  sc_file_add_acl_entry(file, op, method, key_ref);
347
0
}
348
349
static void parse_sec_attr(struct sc_file *file, const u8 *buf, size_t len)
350
0
{
351
0
  int i;
352
0
  const int df_ops[4] =
353
0
    { SC_AC_OP_CREATE, SC_AC_OP_CREATE, SC_AC_OP_DELETE, -1 };
354
0
  const int ef_ops[4] =
355
0
    { SC_AC_OP_READ, SC_AC_OP_UPDATE, SC_AC_OP_DELETE, -1 };
356
0
  const int key_ops[4] =
357
0
    { SC_AC_OP_CRYPTO, SC_AC_OP_UPDATE, SC_AC_OP_DELETE, SC_AC_OP_GENERATE };
358
359
0
  const int *ops;
360
361
0
  if (len < 2)
362
0
    return;
363
364
0
  switch (file->type) {
365
0
  case SC_FILE_TYPE_WORKING_EF:
366
0
    ops = ef_ops;
367
0
    break;
368
0
  case SC_FILE_TYPE_INTERNAL_EF:
369
0
    ops = key_ops;
370
0
    break;
371
0
  case SC_FILE_TYPE_DF:
372
0
    ops = df_ops;
373
0
    break;
374
0
  default:
375
0
    ops = key_ops;
376
0
    break;
377
0
  }
378
379
0
  for (i = 0; i < 4; i++)
380
0
  {
381
0
    if (ops[i] == -1)
382
0
      continue;
383
0
    if ((i & 1) == 0)
384
0
      add_acl_entry(file, ops[i], (u8)(buf[i / 2] >> 4));
385
0
    else
386
0
      add_acl_entry(file, ops[i], (u8)(buf[i / 2] & 0x0F));
387
0
  }
388
0
}
389
390
static int myeid_select_file(struct sc_card *card, const struct sc_path *in_path,
391
    struct sc_file **file)
392
0
{
393
0
  int r;
394
395
0
  LOG_FUNC_CALLED(card->ctx);
396
0
  r = iso_ops->select_file(card, in_path, file);
397
398
0
  if (r == 0 && file != NULL && *file != NULL)
399
0
    parse_sec_attr(*file, (*file)->sec_attr, (*file)->sec_attr_len);
400
401
0
  LOG_FUNC_RETURN(card->ctx, r);
402
0
}
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
0
{
443
0
  myeid_private_data_t *priv = (myeid_private_data_t *) card->drv_data;
444
0
  size_t taglen = 0;
445
0
  const u8 *tag = NULL;
446
0
  int r;
447
448
0
  LOG_FUNC_CALLED(card->ctx);
449
0
  r = iso_ops->process_fci(card, file, buf, buflen);
450
0
  if (r < 0)
451
0
   LOG_FUNC_RETURN(card->ctx, r);
452
453
0
  if(file->type == SC_FILE_EF_UNKNOWN)
454
0
  {
455
0
    tag = sc_asn1_find_tag(NULL, buf, buflen, 0x82, &taglen);
456
0
    if (tag != NULL && taglen > 0 && *tag == 17)
457
0
    {
458
0
      file->type = SC_FILE_TYPE_INTERNAL_EF;
459
0
    }
460
0
  }
461
0
  if(file->sec_attr_len >= 3)
462
0
  {
463
0
    sc_log(card->ctx, "id (%X) sec_attr (%X %X %X)", file->id,
464
0
      file->sec_attr[0],file->sec_attr[1],file->sec_attr[2]);
465
0
  }
466
467
0
  priv->card_state = file->status;
468
0
  switch (file->status) {
469
0
    case SC_FILE_STATUS_CREATION:
470
0
      file->acl_inactive = 1;
471
0
      sc_log(card->ctx, "File id (%X) status SC_FILE_STATUS_CREATION", file->id);
472
0
      break;
473
0
    case SC_FILE_STATUS_ACTIVATED:
474
0
      sc_log(card->ctx, "File id (%X) status SC_FILE_STATUS_ACTIVATED", file->id);
475
0
      break;
476
0
    default:
477
0
      sc_log(card->ctx, "File id (%X) unusual status (0x%X)", file->id, file->status);
478
0
  }
479
480
0
  LOG_FUNC_RETURN(card->ctx, 0);
481
0
}
482
483
static int encode_file_structure(sc_card_t *card, const sc_file_t *file,
484
    u8 *buf, size_t *outlen)
485
0
{
486
0
  const sc_acl_entry_t *read, *update, *delete, *generate;
487
0
  size_t i;
488
489
0
  LOG_FUNC_CALLED(card->ctx);
490
491
0
  if (!buf || !outlen || *outlen < 45)
492
0
    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
0
  memset(buf, 0x0, *outlen);
498
499
0
  buf[0] = 0x62;
500
0
  buf[1] = 0x17;
501
  /* File size */
502
0
  buf[2] = (SC_FILE_TYPE_WORKING_EF == file->type ? 0x80 : 0x81);
503
0
  buf[3] = 0x02;
504
0
  buf[4] = (file->size >> 8) & 0xFF;
505
0
  buf[5] = file->size & 0xFF;
506
507
  /* File Description tag */
508
0
  buf[6] = 0x82;
509
0
  buf[7] = 0x01;
510
0
  buf[8] = 0x01;
511
512
  /* File Identifier tag */
513
0
  buf[9]  = 0x83;
514
0
  buf[10] = 0x02;
515
0
  buf[11] = (file->id >> 8) & 0xFF;
516
0
  buf[12] = file->id & 0xFF;
517
518
  /* Security Attributes Tag */
519
0
  buf[13] = 0x86;
520
0
  buf[14] = 0x03;
521
0
  buf[15] = 0xFF;
522
0
  buf[16] = 0xFF;
523
0
  buf[17] = 0xFF;
524
525
0
  if (file->sec_attr_len == 3 && file->sec_attr)   {
526
0
    buf[15] = file->sec_attr[0];
527
0
    buf[16] = file->sec_attr[1];
528
0
    buf[17] = file->sec_attr[2];
529
530
0
    sc_log(card->ctx, "id (%X), sec_attr %X %X %X", file->id,
531
0
        file->sec_attr[0],file->sec_attr[1],file->sec_attr[2]);
532
0
  }
533
0
  else   {
534
0
    delete = sc_file_get_acl_entry(file, SC_AC_OP_DELETE);
535
536
0
    sc_log(card->ctx, "id (%X), type (%X)", file->id, file->type);
537
538
0
    switch (file->type) {
539
0
    case SC_FILE_TYPE_WORKING_EF:
540
541
0
      read = sc_file_get_acl_entry(file, SC_AC_OP_READ);
542
0
      update = sc_file_get_acl_entry(file, SC_AC_OP_UPDATE);
543
544
0
      buf[15] = (acl_to_byte(read) << 4) | acl_to_byte(update);
545
0
      buf[16] = (acl_to_byte(delete)<< 4) | 0x0F;
546
0
      break;
547
0
    case SC_FILE_TYPE_INTERNAL_EF:
548
549
0
      read = sc_file_get_acl_entry(file, SC_AC_OP_CRYPTO);
550
0
      update = sc_file_get_acl_entry(file, SC_AC_OP_UPDATE);
551
0
      generate = sc_file_get_acl_entry(file, SC_AC_OP_GENERATE);
552
553
0
      buf[15] = (acl_to_byte(read) << 4) | acl_to_byte(update);
554
0
      buf[16] = (acl_to_byte(delete)<< 4) | acl_to_byte(generate);
555
0
      break;
556
0
    case SC_FILE_TYPE_DF:
557
558
0
      update = sc_file_get_acl_entry(file, SC_AC_OP_CREATE);
559
560
0
      buf[15] = (acl_to_byte(update) << 4) | acl_to_byte(update);
561
0
      buf[16] = (acl_to_byte(delete) << 4) | 0x0F;
562
0
      break;
563
0
    default:
564
0
      break;
565
0
    }
566
0
  }
567
568
  /* Proprietary Information */
569
0
  buf[18] = 0x85;
570
0
  buf[19] = 0x02;
571
0
  if (file->prop_attr_len == 2 && file->prop_attr != NULL)
572
0
      memcpy(&buf[20], file->prop_attr, 2);
573
0
  else
574
0
  {
575
0
    buf[20] = 0x00;
576
0
    buf[21] = 0x00;
577
0
  }
578
579
  /* Life Cycle Status tag */
580
0
  buf[22] = 0x8A;
581
0
  buf[23] = 0x01;
582
0
  buf[24] = 0x0; /* RFU */
583
584
0
  switch (file->type)
585
0
  {
586
0
  case SC_FILE_TYPE_WORKING_EF:
587
0
    break;
588
589
0
  case SC_FILE_TYPE_INTERNAL_EF:
590
0
    buf[8] = file->ef_structure; /* RSA or EC */
591
0
    break;
592
593
0
  case SC_FILE_TYPE_DF:
594
0
    buf[8] = 0x38;
595
0
    if(file->namelen > 0 && file->namelen <= 16)
596
0
    {
597
0
      buf[25] = 0x84;
598
0
      buf[26] = (u8)file->namelen;
599
600
0
      for(i=0;i < file->namelen;i++)
601
0
        buf[i + 27] = file->name[i];
602
603
0
      buf[1] = 27 + file->namelen;
604
0
    }
605
0
    break;
606
0
  default:
607
0
    sc_log(card->ctx, "Unknown file type\n");
608
0
    return SC_ERROR_INVALID_ARGUMENTS;
609
0
  }
610
611
0
  *outlen = buf[1]+2;
612
613
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
614
0
}
615
616
static int myeid_create_file(struct sc_card *card, struct sc_file *file)
617
0
{
618
0
  sc_apdu_t apdu;
619
0
  u8 sbuf[45];
620
0
  size_t buflen = sizeof sbuf;
621
0
  int r;
622
623
0
  LOG_FUNC_CALLED(card->ctx);
624
625
0
  r = encode_file_structure(card, file, sbuf, &buflen);
626
0
  if (r)
627
0
    LOG_FUNC_RETURN(card->ctx, r);
628
629
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x00, 0x00);
630
0
  apdu.data = sbuf;
631
0
  apdu.datalen = buflen;
632
0
  apdu.lc = buflen;
633
634
0
  r = sc_transmit_apdu(card, &apdu);
635
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
636
0
  if (apdu.sw1 == 0x6A && apdu.sw2 == 0x89)
637
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_FILE_ALREADY_EXISTS);
638
639
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
640
0
  LOG_FUNC_RETURN(card->ctx, r);
641
0
}
642
643
static int myeid_delete_file(struct sc_card *card, const struct sc_path *path)
644
0
{
645
0
  int r;
646
0
  struct sc_apdu apdu;
647
648
0
  LOG_FUNC_CALLED(card->ctx);
649
0
  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
0
  r = sc_select_file(card, path, NULL);
655
0
  LOG_TEST_RET(card->ctx, r, "Unable to select file to be deleted");
656
657
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0xE4, 0x00, 0x00);
658
0
  apdu.cla = 0xA0;
659
660
0
  r = sc_transmit_apdu(card, &apdu);
661
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
662
663
0
  LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2));
664
0
}
665
666
static int myeid_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data)
667
0
{
668
0
  myeid_private_data_t *priv = (myeid_private_data_t *) card->drv_data;
669
670
0
  LOG_FUNC_CALLED(card->ctx);
671
672
0
  sc_log(card->ctx, "ref (%d), pin1 len(%zu), pin2 len (%zu)\n",
673
0
      data->pin_reference, data->pin1.len, data->pin2.len);
674
675
0
  if(data->pin1.len > 8 || data->pin2.len > 8)
676
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_PIN_LENGTH);
677
678
0
  data->pin1.pad_length = data->pin2.pad_length = 8;
679
0
  data->pin1.pad_char = data->pin2.pad_char = 0xFF;
680
681
0
  if (data->cmd == SC_PIN_CMD_VERIFY && priv->card_state == SC_FILE_STATUS_CREATION) {
682
0
    sc_log(card->ctx, "Card in creation state, no need to verify");
683
0
    return SC_SUCCESS;
684
0
  }
685
686
0
  LOG_FUNC_RETURN(card->ctx, iso_ops->pin_cmd(card, data));
687
0
}
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
0
{
1477
0
  int r;
1478
0
  struct sc_apdu apdu;
1479
1480
0
  LOG_FUNC_CALLED(card->ctx);
1481
1482
0
  memset(&apdu, 0, sizeof(apdu));
1483
0
  apdu.cse     = SC_APDU_CASE_3_SHORT;
1484
0
  apdu.cla     = 0x00;
1485
0
  apdu.ins     = 0xDA;
1486
0
  apdu.p1      = data_obj->P1;
1487
0
  apdu.p2      = data_obj->P2;
1488
0
  apdu.lc      = data_obj->DataLen;
1489
0
  apdu.datalen = data_obj->DataLen;
1490
0
  apdu.data    = data_obj->Data;
1491
1492
0
  r = sc_transmit_apdu(card, &apdu);
1493
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1494
1495
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1496
0
  LOG_TEST_RET(card->ctx, r, "PUT_DATA returned error");
1497
1498
0
  LOG_FUNC_RETURN(card->ctx, r);
1499
0
}
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
0
{
1504
0
  int r;
1505
0
  struct sc_apdu apdu;
1506
1507
0
  LOG_FUNC_CALLED(card->ctx);
1508
1509
0
  memset(&apdu, 0, sizeof(apdu));
1510
0
  apdu.cse     = SC_APDU_CASE_2_SHORT;
1511
0
  apdu.cla     = 0x00;
1512
0
  apdu.ins     = 0xCA;    /* GET DATA */
1513
0
  apdu.p1      = data_obj->P1;
1514
0
  apdu.p2      = data_obj->P2;
1515
0
  apdu.lc      = 0;
1516
0
  apdu.datalen = 0;
1517
0
  apdu.data    = data_obj->Data;
1518
1519
0
  apdu.le      = card->max_recv_size;
1520
0
  apdu.resp    = data_obj->Data;
1521
0
  apdu.resplen = data_obj->DataLen;
1522
1523
0
  r = sc_transmit_apdu(card, &apdu);
1524
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1525
1526
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1527
0
  LOG_TEST_RET(card->ctx, r, "GET_DATA returned error");
1528
1529
0
  if (apdu.resplen > data_obj->DataLen)
1530
0
    r = SC_ERROR_WRONG_LENGTH;
1531
0
  else
1532
0
    data_obj->DataLen = apdu.resplen;
1533
1534
0
  LOG_FUNC_RETURN(card->ctx, r);
1535
0
}
1536
1537
static int myeid_loadkey(sc_card_t *card, unsigned mode, u8 *value, size_t value_len)
1538
0
{
1539
0
  myeid_private_data_t *priv = (myeid_private_data_t *) card->drv_data;
1540
0
  sc_apdu_t apdu;
1541
0
  u8 sbuf[MYEID_MAX_EXT_APDU_BUFFER_SIZE];
1542
0
  int r;
1543
1544
0
  LOG_FUNC_CALLED(card->ctx);
1545
0
  if (value_len == 0 || value == NULL)
1546
0
    return 0;
1547
1548
0
  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
0
  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
0
  memset(&apdu, 0, sizeof(apdu));
1580
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xDA, 0x01, mode & 0xFF);
1581
0
  apdu.flags   = SC_APDU_FLAGS_CHAINING;
1582
0
  apdu.cla     = 0x00;
1583
0
  apdu.data    = value;
1584
0
  apdu.datalen = value_len;
1585
0
  apdu.lc      = value_len;
1586
1587
0
  r = sc_transmit_apdu(card, &apdu);
1588
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1589
1590
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1591
0
  LOG_FUNC_RETURN(card->ctx, r);
1592
0
}
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
0
{
1598
0
  struct  sc_apdu apdu;
1599
0
  u8  sbuf[SC_MAX_APDU_BUFFER_SIZE];
1600
0
  int r=0,len;
1601
1602
0
  LOG_FUNC_CALLED(card->ctx);
1603
  /* Setup key-generation parameters */
1604
0
  if (data->op_type == OP_TYPE_GENERATE)
1605
0
  {
1606
0
    len = 0;
1607
0
    memset(&apdu, 0, sizeof(apdu));
1608
1609
0
    if(data->key_type == SC_CARDCTL_MYEID_KEY_RSA)
1610
0
    {
1611
0
        sbuf[len++] = 0x30;
1612
0
        sbuf[len++] = 0x05;
1613
0
        sbuf[len++] = 0x81;
1614
0
        sbuf[len++] = data->pubexp_len;
1615
1616
0
        memcpy(sbuf + len, data->pubexp, data->pubexp_len);
1617
0
        len += data->pubexp_len;
1618
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x46, 0x00, 0x00);
1619
0
      apdu.data    = sbuf;
1620
0
    }
1621
0
    else if(data->key_type == SC_CARDCTL_MYEID_KEY_EC) {
1622
1623
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x46, 0x00, 0x00);
1624
1625
0
      apdu.data    = NULL;
1626
0
      apdu.resp  = sbuf;
1627
0
      apdu.resplen = 0x00;
1628
0
      apdu.le    = 0x00;
1629
0
    }
1630
1631
0
    apdu.cla     = 0x00;
1632
0
    apdu.datalen = len;
1633
0
    apdu.lc      = len;
1634
1635
0
    r = sc_transmit_apdu(card, &apdu);
1636
0
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1637
1638
0
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1639
0
    LOG_TEST_RET(card->ctx, r, "GENERATE_KEY returned error");
1640
0
  }
1641
0
  else
1642
0
  {
1643
0
    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
0
    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
0
    else if(data->key_type == SC_CARDCTL_MYEID_KEY_AES ||
1669
0
      data->key_type == SC_CARDCTL_MYEID_KEY_DES) {
1670
0
      if((r = myeid_loadkey(card, LOAD_KEY_SYMMETRIC, data->d,
1671
0
          data->d_len)) >= 0)
1672
0
      LOG_FUNC_RETURN(card->ctx, r);
1673
0
    }
1674
0
  }
1675
1676
0
  LOG_FUNC_RETURN(card->ctx, r);
1677
0
}
1678
1679
static int myeid_activate_card(struct sc_card *card)
1680
0
{
1681
0
  int r;
1682
0
  u8 sbuf[] ="\xA0\x00\x00\x00\x63\x50\x4B\x43\x53\x2D\x31\x35";
1683
0
  sc_apdu_t apdu;
1684
1685
0
  LOG_FUNC_CALLED(card->ctx);
1686
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x44, 0x04, 0x00);
1687
0
  apdu.cla     = 0x00;
1688
0
  apdu.data    = sbuf;
1689
0
  apdu.datalen = 0x0C;
1690
0
  apdu.lc      = 0x0C;
1691
1692
0
  r = sc_transmit_apdu(card, &apdu);
1693
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1694
1695
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1696
0
  LOG_TEST_RET(card->ctx, r, "ACTIVATE_APPLET returned error");
1697
1698
0
  LOG_FUNC_RETURN(card->ctx, r);
1699
0
}
1700
1701
static int myeid_get_info(struct sc_card *card, u8 *rbuf, size_t buflen)
1702
0
{
1703
0
  sc_apdu_t apdu;
1704
0
  int r;
1705
1706
0
  LOG_FUNC_CALLED(card->ctx);
1707
1708
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xca, 0x01, 0xA0);
1709
0
  apdu.resp    = rbuf;
1710
0
  apdu.resplen = buflen;
1711
0
  apdu.le      = buflen;
1712
1713
0
  r = sc_transmit_apdu(card, &apdu);
1714
0
  LOG_TEST_RET(card->ctx, r,  "APDU transmit failed");
1715
1716
0
  if (apdu.sw1 != 0x90 || apdu.sw2 != 0x00)
1717
0
    return SC_ERROR_INTERNAL;
1718
1719
0
  if (apdu.resplen != 20)
1720
0
  {
1721
0
    sc_log(card->ctx, "Unexpected response to GET DATA (applet info)");
1722
0
    return SC_ERROR_INTERNAL;
1723
0
  }
1724
1725
  /* store the applet version */
1726
0
  card->version.fw_major = rbuf[5] * 10 + rbuf[6];
1727
0
  card->version.fw_minor = rbuf[7];
1728
  /* add version to name */
1729
0
  snprintf(card_name_buf, sizeof(card_name_buf),
1730
0
      "%s %d.%d.%d", card->name, rbuf[5], rbuf[6], rbuf[7]);
1731
0
  card->name = card_name_buf;
1732
1733
0
  LOG_FUNC_RETURN(card->ctx, r);
1734
0
}
1735
1736
static int myeid_get_serialnr(sc_card_t *card, sc_serial_number_t *serial)
1737
0
{
1738
0
  int r;
1739
0
  u8  rbuf[256];
1740
1741
0
  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
0
  r = myeid_get_info(card, rbuf, sizeof(rbuf));
1751
0
  LOG_TEST_RET(card->ctx, r,  "Get applet info failed");
1752
1753
  /* cache serial number */
1754
0
  memcpy(card->serialnr.value, &rbuf[8], 10);
1755
0
  card->serialnr.len = 10;
1756
1757
  /* copy and return serial number */
1758
0
  memcpy(serial, &card->serialnr, sizeof(*serial));
1759
1760
0
  LOG_FUNC_RETURN(card->ctx, r);
1761
0
}
1762
1763
static int
1764
myeid_get_change_counter(sc_card_t *card, size_t *change_counter)
1765
0
{
1766
0
  int r;
1767
0
  u8 rbuf[256];
1768
1769
0
  LOG_FUNC_CALLED(card->ctx);
1770
1771
  /* get change counter from card */
1772
0
  r = myeid_get_info(card, rbuf, sizeof(rbuf));
1773
0
  LOG_TEST_RET(card->ctx, r, "Get applet info failed");
1774
1775
0
  *change_counter = rbuf[18] * 256 + rbuf[19];
1776
1777
0
  LOG_FUNC_RETURN(card->ctx, r);
1778
0
}
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
0
{
1786
0
  sc_apdu_t apdu;
1787
0
  int r;
1788
0
  unsigned char rbuf[SC_MAX_APDU_BUFFER_SIZE];
1789
1790
0
  LOG_FUNC_CALLED(card->ctx);
1791
1792
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xca, 0x01, 0xAA);
1793
0
  apdu.resp    = rbuf;
1794
0
  apdu.resplen = sizeof(myeid_card_caps_t);
1795
0
  apdu.le      = sizeof(myeid_card_caps_t);
1796
1797
0
  r = sc_transmit_apdu(card, &apdu);
1798
0
  LOG_TEST_RET(card->ctx, r,  "APDU transmit failed");
1799
1800
0
  if (apdu.sw1 != 0x90 || apdu.sw2 != 0x00)
1801
0
    return SC_ERROR_INTERNAL;
1802
1803
0
  if (apdu.resplen < 11) {
1804
0
    sc_log(card->ctx, "Unexpected response to GET DATA (MyEIC card capabilities)");
1805
0
    return SC_ERROR_INTERNAL;
1806
0
  }
1807
1808
0
  card_caps->card_caps_ver = rbuf[0];
1809
  /* the card returns big endian values */
1810
0
  card_caps->card_supported_features = (unsigned short) rbuf[1] << 8 | rbuf[2];
1811
0
  card_caps->max_rsa_key_length = (unsigned short) rbuf[3] << 8 | rbuf[4];
1812
0
  card_caps->max_des_key_length = (unsigned short) rbuf[5] << 8 | rbuf[6];
1813
0
  card_caps->max_aes_key_length = (unsigned short) rbuf[7] << 8 | rbuf[8];
1814
0
  card_caps->max_ecc_key_length = (unsigned short) rbuf[9] << 8 | rbuf[10];
1815
1816
0
  LOG_FUNC_RETURN(card->ctx, r);
1817
0
}
1818
1819
static int myeid_card_ctl(struct sc_card *card, unsigned long cmd, void *ptr)
1820
0
{
1821
0
  int r = SC_ERROR_NOT_SUPPORTED;
1822
0
  LOG_FUNC_CALLED(card->ctx);
1823
1824
0
  switch(cmd) {
1825
0
  case SC_CARDCTL_MYEID_PUTDATA:
1826
0
    r = myeid_putdata(card,
1827
0
      (struct sc_cardctl_myeid_data_obj*) ptr);
1828
0
    break;
1829
0
  case SC_CARDCTL_MYEID_GETDATA:
1830
0
    r = myeid_getdata(card,
1831
0
      (struct sc_cardctl_myeid_data_obj*) ptr);
1832
0
    break;
1833
0
  case SC_CARDCTL_MYEID_GENERATE_STORE_KEY:
1834
0
    r = myeid_generate_store_key(card,
1835
0
      (struct sc_cardctl_myeid_gen_store_key_info *) ptr);
1836
0
    break;
1837
0
  case SC_CARDCTL_MYEID_ACTIVATE_CARD:
1838
0
    r = myeid_activate_card(card);
1839
0
    break;
1840
0
  case SC_CARDCTL_GET_SERIALNR:
1841
0
    r = myeid_get_serialnr(card, (sc_serial_number_t *)ptr);
1842
0
    break;
1843
0
  case SC_CARDCTL_GET_CHANGE_COUNTER:
1844
0
    r = myeid_get_change_counter(card, (size_t *)ptr);
1845
0
    break;
1846
0
  case SC_CARDCTL_GET_DEFAULT_KEY:
1847
0
  case SC_CARDCTL_LIFECYCLE_SET:
1848
0
  case SC_CARDCTL_LIFECYCLE_GET:
1849
0
    break;
1850
0
  }
1851
0
  LOG_FUNC_RETURN(card->ctx, r);
1852
0
}
1853
1854
static int myeid_finish(sc_card_t * card)
1855
0
{
1856
0
  struct myeid_private_data *priv = (struct myeid_private_data *) card->drv_data;
1857
0
  free(priv);
1858
0
  return SC_SUCCESS;
1859
0
}
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
0
{
2111
0
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
2112
2113
0
  if (iso_ops == NULL)
2114
0
    iso_ops = iso_drv->ops;
2115
2116
0
  myeid_ops     = *iso_drv->ops;
2117
0
  myeid_ops.match_card    = myeid_match_card;
2118
0
  myeid_ops.init      = myeid_init;
2119
0
  myeid_ops.finish    = myeid_finish;
2120
  /* no record oriented file services */
2121
0
  myeid_ops.read_record   = NULL;
2122
0
  myeid_ops.write_record    = NULL;
2123
0
  myeid_ops.append_record   = NULL;
2124
0
  myeid_ops.update_record   = NULL;
2125
0
  myeid_ops.select_file   = myeid_select_file;
2126
0
  myeid_ops.get_response    = iso_ops->get_response;
2127
0
  myeid_ops.logout    = myeid_logout;
2128
0
  myeid_ops.create_file   = myeid_create_file;
2129
0
  myeid_ops.delete_file   = myeid_delete_file;
2130
0
  myeid_ops.list_files    = myeid_list_files;
2131
0
  myeid_ops.set_security_env  = myeid_set_security_env;
2132
0
  myeid_ops.compute_signature = myeid_compute_signature;
2133
0
  myeid_ops.decipher    = myeid_decipher;
2134
0
  myeid_ops.process_fci   = myeid_process_fci;
2135
0
  myeid_ops.card_ctl    = myeid_card_ctl;
2136
0
  myeid_ops.pin_cmd   = myeid_pin_cmd;
2137
0
  myeid_ops.wrap      = myeid_wrap_key;
2138
0
  myeid_ops.unwrap    = myeid_unwrap_key;
2139
0
  myeid_ops.encrypt_sym   = myeid_encrypt_sym;
2140
0
  myeid_ops.decrypt_sym   = myeid_decrypt_sym;
2141
0
  return &myeid_drv;
2142
0
}
2143
2144
struct sc_card_driver * sc_get_myeid_driver(void)
2145
0
{
2146
0
  return sc_get_driver();
2147
0
}
2148