Coverage Report

Created: 2026-12-03 06:14

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