Coverage Report

Created: 2026-12-03 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-epass2003.c
Line
Count
Source
1
/*
2
 * Support for ePass2003 smart cards
3
 *
4
 * Copyright (C) 2008, Weitao Sun <weitao@ftsafe.com>
5
 * Copyright (C) 2011, Xiaoshuo Wu <xiaoshuo@ftsafe.com>
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#ifdef HAVE_CONFIG_H
23
#include "config.h"
24
#endif
25
#ifdef ENABLE_SM    /* empty file without SM enabled */
26
#ifdef ENABLE_OPENSSL   /* empty file without openssl */
27
28
#include <ctype.h>
29
#include <stdlib.h>
30
#include <string.h>
31
32
#include <openssl/evp.h>
33
#include <openssl/sha.h>
34
35
#include "internal.h"
36
#include "asn1.h"
37
38
#include <ctype.h>
39
#include <stdlib.h>
40
#include <string.h>
41
42
#include <openssl/cmac.h>
43
#include <openssl/rand.h>
44
#include <openssl/sha.h>
45
46
#include "internal.h"
47
#include "asn1.h"
48
#include "cardctl.h"
49
50
/*
51
 * See https://github.com/OpenSC/OpenSC/issues/2572
52
 * 2012 ATR: note version 01:00:11
53
 *        3b:9f:95:81:31:fe:9f:00:66:46:53:05:01:00:11:71:df:00:00:03:90:00:80
54
 * 2022 ATRs: note version 23:00:25
55
 *   OpenSC-initialized ATR:
56
 *        3b 9f:95:81:31:fe:9f:00:66:46:53:05:23:00:25:71:df:00:00:03:90:00:96
57
 *   Feitian-initalized ATR:
58
 *        3b:9f:95:81:31:fe:9f:00:66:46:53:05:23:00:25:71:df:00:00:00:00:00:05
59
 */
60
61
static const struct sc_atr_table epass2003_atrs[] = {
62
  /* This is a FIPS certified card using SCP01 security messaging. */
63
  /* will match all the above */
64
  {"3B:9F:95:81:31:FE:9F:00:66:46:53:05:00:00:00:71:df:00:00:00:00:00:00",
65
   "FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:FF:FF:FF:FF:00:00:00:00",
66
   "FTCOS/ePass2003", SC_CARD_TYPE_ENTERSAFE_FTCOS_EPASS2003, 0, NULL },
67
  {NULL, NULL, NULL, 0, 0, NULL}
68
};
69
70
static struct sc_card_operations *iso_ops = NULL;
71
static struct sc_card_operations epass2003_ops;
72
73
static struct sc_card_driver epass2003_drv = {
74
  "epass2003",
75
  "epass2003",
76
  &epass2003_ops,
77
  NULL, 0, NULL
78
};
79
80
1.53k
#define KEY_TYPE_AES  0x01  /* FIPS mode */
81
557
#define KEY_TYPE_DES  0x02  /* Non-FIPS mode */
82
83
#define KEY_LEN_AES 16
84
#define KEY_LEN_DES 8
85
#define KEY_LEN_DES3  24
86
0
#define HASH_LEN  24
87
88
static unsigned char PIN_ID[2] = { ENTERSAFE_USER_PIN_ID, ENTERSAFE_SO_PIN_ID };
89
90
/*0x00:plain; 0x01:scp01 sm*/
91
794
#define SM_PLAIN        0x00
92
344
#define SM_SCP01        0x01
93
94
static unsigned char g_init_key_enc[16] = {
95
  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
96
  0x0D, 0x0E, 0x0F, 0x10
97
};
98
99
static unsigned char g_init_key_mac[16] = {
100
  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
101
  0x0D, 0x0E, 0x0F, 0x10
102
};
103
104
static unsigned char g_random[8];
105
106
typedef struct epass2003_exdata_st {
107
  unsigned char sm;   /* SM_PLAIN or SM_SCP01 */
108
  unsigned char smtype;   /* KEY_TYPE_AES or KEY_TYPE_DES */
109
  unsigned char sk_enc[16]; /* encrypt session key */
110
  unsigned char sk_mac[16]; /* mac session key */
111
  unsigned char icv_mac[16];  /* instruction counter vector(for sm) */
112
  unsigned char bFipsCertification; /* fips mode Alg */
113
  unsigned char currAlg;    /* current Alg */
114
  unsigned int  ecAlgFlags;   /* Ec Alg mechanism type*/
115
  /* Absolute path (starting with 3F00) of the DF that was selected last,
116
   * or cur_df_len == 0 if it is not known. Try to avoid selecting the same
117
   * DF twice to preserve the security status which would otherwise be
118
   * reset. */
119
  u8 cur_df[SC_MAX_PATH_SIZE];
120
  size_t cur_df_len;
121
  sc_file_t *cur_df_file; /* FCI of that DF */
122
} epass2003_exdata;
123
124
0
#define REVERSE_ORDER4(x) (       \
125
0
    ((unsigned long)x & 0xFF000000)>> 24  | \
126
0
    ((unsigned long)x & 0x00FF0000)>>  8  | \
127
0
    ((unsigned long)x & 0x0000FF00)<<  8  | \
128
0
    ((unsigned long)x & 0x000000FF)<< 24)
129
130
// clang-format off
131
static const struct sc_card_error epass2003_errors[] = {
132
  { 0x6200, SC_ERROR_CARD_CMD_FAILED, "Warning: no information given, non-volatile memory is unchanged" },
133
  { 0x6281, SC_ERROR_CORRUPTED_DATA,  "Part of returned data may be corrupted" },
134
  { 0x6282, SC_ERROR_FILE_END_REACHED,  "End of file/record reached before reading Le bytes" },
135
  { 0x6283, SC_ERROR_CARD_CMD_FAILED, "Selected file invalidated" },
136
  { 0x6284, SC_ERROR_CARD_CMD_FAILED, "FCI not formatted according to ISO 7816-4" },
137
138
  { 0x6300, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
139
  { 0x63C1, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed. One tries left"},
140
  { 0x63C2, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed. Two tries left"},
141
  { 0x63C3, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
142
  { 0x63C4, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
143
  { 0x63C5, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
144
  { 0x63C6, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
145
  { 0x63C7, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
146
  { 0x63C8, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
147
  { 0x63C9, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
148
  { 0x63CA, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
149
150
  { 0x6381, SC_ERROR_CARD_CMD_FAILED, "Warning: file filled up by last write" },
151
152
  { 0x6581, SC_ERROR_MEMORY_FAILURE,  "Memory failure" },
153
154
  { 0x6700, SC_ERROR_WRONG_LENGTH,  "Wrong length" },
155
156
  { 0x6800, SC_ERROR_NO_CARD_SUPPORT, "Functions in CLA not supported" },
157
  { 0x6881, SC_ERROR_NO_CARD_SUPPORT, "Logical channel not supported" },
158
  { 0x6882, SC_ERROR_NO_CARD_SUPPORT, "Secure messaging not supported" },
159
160
  { 0x6900, SC_ERROR_NOT_ALLOWED,   "Command not allowed" },
161
  { 0x6981, SC_ERROR_CARD_CMD_FAILED, "Command incompatible with file structure" },
162
  { 0x6982, SC_ERROR_SECURITY_STATUS_NOT_SATISFIED, "Security status not satisfied" },
163
  { 0x6983, SC_ERROR_AUTH_METHOD_BLOCKED, "Authentication method blocked" },
164
  { 0x6984, SC_ERROR_REF_DATA_NOT_USABLE, "Referenced data not usable" },
165
  { 0x6985, SC_ERROR_NOT_ALLOWED,   "Conditions of use not satisfied" },
166
  { 0x6986, SC_ERROR_NOT_ALLOWED,   "Command not allowed (no current EF)" },
167
  { 0x6987, SC_ERROR_INCORRECT_PARAMETERS,"Expected SM data objects missing" },
168
  { 0x6988, SC_ERROR_INCORRECT_SM_OBJECTS,"SM data objects incorrect" },
169
170
  { 0x6A00, SC_ERROR_INCORRECT_PARAMETERS,"Wrong parameter(s) P1-P2" },
171
  { 0x6A80, SC_ERROR_INCORRECT_PARAMETERS,"Incorrect parameters in the data field" },
172
  { 0x6A81, SC_ERROR_NO_CARD_SUPPORT, "Function not supported" },
173
  { 0x6A82, SC_ERROR_FILE_NOT_FOUND,  "File not found" },
174
  { 0x6A83, SC_ERROR_RECORD_NOT_FOUND,  "Record not found" },
175
  { 0x6A84, SC_ERROR_NOT_ENOUGH_MEMORY, "Not enough memory space in the file" },
176
  { 0x6A85, SC_ERROR_INCORRECT_PARAMETERS,"Lc inconsistent with TLV structure" },
177
  { 0x6A86, SC_ERROR_INCORRECT_PARAMETERS,"Incorrect parameters P1-P2" },
178
  { 0x6A87, SC_ERROR_INCORRECT_PARAMETERS,"Lc inconsistent with P1-P2" },
179
  { 0x6A88, SC_ERROR_DATA_OBJECT_NOT_FOUND,"Referenced data not found" },
180
  { 0x6A89, SC_ERROR_FILE_ALREADY_EXISTS,  "File already exists"},
181
  { 0x6A8A, SC_ERROR_FILE_ALREADY_EXISTS,  "DF name already exists"},
182
183
  { 0x6B00, SC_ERROR_INCORRECT_PARAMETERS,"Wrong parameter(s) P1-P2" },
184
  { 0x6D00, SC_ERROR_INS_NOT_SUPPORTED, "Instruction code not supported or invalid" },
185
  { 0x6E00, SC_ERROR_CLASS_NOT_SUPPORTED, "Class not supported" },
186
  { 0x6F00, SC_ERROR_CARD_CMD_FAILED, "No precise diagnosis" },
187
188
  { 0x9000,SC_SUCCESS,                       NULL }
189
};
190
// clang-format on
191
192
typedef struct sec_attr_to_acl_entries {
193
  unsigned int file_type;   /* file->type */
194
  unsigned int file_ef_structure; /* file->ef_structure */
195
  int index;      /* index in  epass2003 iversion of sec_attr */
196
  /* use the follow for sc_file_add_entry */
197
  int op;       /* SC_AC_OP_* */
198
} sec_attr_to_acl_entries_t;
199
200
// clang-format off
201
/* Known combinations of file type and methods. More can be added as needed */
202
static const sec_attr_to_acl_entries_t sec_attr_to_acl_entry[] = {
203
  {SC_FILE_TYPE_DF, 0, 0,       SC_AC_OP_LIST_FILES},
204
  {SC_FILE_TYPE_DF, 0, 1,       SC_AC_OP_CREATE},
205
  {SC_FILE_TYPE_DF, 0, 3,       SC_AC_OP_DELETE},
206
207
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_TRANSPARENT, 0,  SC_AC_OP_READ},
208
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_TRANSPARENT, 1,  SC_AC_OP_UPDATE},
209
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_TRANSPARENT, 3,  SC_AC_OP_DELETE},
210
211
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_TRANSPARENT, 0,  SC_AC_OP_READ},
212
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_TRANSPARENT, 1,  SC_AC_OP_UPDATE},
213
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_TRANSPARENT, 3,  SC_AC_OP_DELETE},
214
215
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_LINEAR_FIXED, 0,  SC_AC_OP_READ},
216
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_LINEAR_FIXED, 1,  SC_AC_OP_UPDATE},
217
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_LINEAR_FIXED, 2,  SC_AC_OP_WRITE},
218
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_LINEAR_FIXED, 3,  SC_AC_OP_DELETE},
219
220
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_LINEAR_VARIABLE, 0,  SC_AC_OP_READ},
221
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_LINEAR_VARIABLE, 1,  SC_AC_OP_UPDATE},
222
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_LINEAR_VARIABLE, 2,  SC_AC_OP_WRITE},
223
  {SC_FILE_TYPE_WORKING_EF, SC_FILE_EF_LINEAR_VARIABLE, 3,  SC_AC_OP_DELETE},
224
225
  {SC_FILE_TYPE_BSO, 0, 0,        SC_AC_OP_UPDATE},
226
  {SC_FILE_TYPE_BSO, 0, 3,        SC_AC_OP_DELETE},
227
228
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_RSA_CRT, 1,  SC_AC_OP_UPDATE},
229
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_EC_CRT,  1,  SC_AC_OP_UPDATE},
230
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_RSA_CRT, 2,  SC_AC_OP_CRYPTO},
231
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_EC_CRT,  2,  SC_AC_OP_CRYPTO},
232
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_RSA_CRT, 3,  SC_AC_OP_DELETE},
233
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_EC_CRT,  3,  SC_AC_OP_DELETE},
234
235
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_RSA_PUBLIC, 0, SC_AC_OP_READ},
236
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_EC_PUBLIC,  0, SC_AC_OP_READ},
237
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_RSA_PUBLIC, 1, SC_AC_OP_UPDATE},
238
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_EC_PUBLIC,  1, SC_AC_OP_UPDATE},
239
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_RSA_PUBLIC, 2, SC_AC_OP_CRYPTO},
240
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_EC_PUBLIC,  2, SC_AC_OP_CRYPTO},
241
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_RSA_PUBLIC, 3, SC_AC_OP_DELETE},
242
  {SC_FILE_TYPE_INTERNAL_EF, SC_CARDCTL_OBERTHUR_KEY_EC_PUBLIC,  3, SC_AC_OP_DELETE},
243
};
244
// clang-format on
245
246
static int epass2003_transmit_apdu(struct sc_card *card, struct sc_apdu *apdu);
247
static int epass2003_select_file(struct sc_card *card, const sc_path_t * in_path, sc_file_t ** file_out);
248
int epass2003_refresh(struct sc_card *card);
249
static int hash_data(struct sc_card *card, const unsigned char *data, size_t datalen, unsigned char *hash, unsigned int mechanismType);
250
251
static int
252
epass2003_check_sw(struct sc_card *card, unsigned int sw1, unsigned int sw2)
253
3.19k
{
254
3.19k
  const int err_count = sizeof(epass2003_errors)/sizeof(epass2003_errors[0]);
255
3.19k
  int i;
256
257
  /* Handle special cases here */
258
3.19k
  if (sw1 == 0x6C) {
259
14
    sc_log(card->ctx, "Wrong length; correct length is %d", sw2);
260
14
    return SC_ERROR_WRONG_LENGTH;
261
14
  }
262
263
149k
  for (i = 0; i < err_count; i++) {
264
149k
    if (epass2003_errors[i].SWs == ((sw1 << 8) | sw2)) {
265
3.02k
      sc_log(card->ctx, "%s", epass2003_errors[i].errorstr);
266
3.02k
      return epass2003_errors[i].errorno;
267
3.02k
    }
268
149k
  }
269
270
154
  sc_log(card->ctx, "Unknown SWs; SW1=%02X, SW2=%02X", sw1, sw2);
271
154
  return SC_ERROR_CARD_CMD_FAILED;
272
3.18k
}
273
274
static int
275
sc_transmit_apdu_t(sc_card_t *card, sc_apdu_t *apdu)
276
698
{
277
698
  size_t resplen = apdu->resplen;
278
698
  int r = sc_transmit_apdu(card, apdu);
279
698
  if ((0x69 == apdu->sw1 && 0x85 == apdu->sw2) || (0x69 == apdu->sw1 && 0x88 == apdu->sw2)) {
280
6
    epass2003_refresh(card);
281
    /* renew old resplen */
282
6
    apdu->resplen = resplen;
283
6
    r = sc_transmit_apdu(card, apdu);
284
6
  }
285
698
  return r;
286
698
}
287
288
static int
289
openssl_enc(const EVP_CIPHER * cipher, const unsigned char *key, const unsigned char *iv,
290
    const unsigned char *input, size_t length, unsigned char *output)
291
453
{
292
453
  int r = SC_ERROR_INTERNAL;
293
453
  EVP_CIPHER_CTX * ctx = NULL;
294
453
  int outl = 0;
295
453
  int outl_tmp = 0;
296
453
  unsigned char iv_tmp[EVP_MAX_IV_LENGTH] = {0};
297
298
453
  memcpy(iv_tmp, iv, EVP_MAX_IV_LENGTH);
299
453
  ctx = EVP_CIPHER_CTX_new();
300
453
  if (ctx == NULL)
301
0
    goto out;
302
303
453
  if (!EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv_tmp) || !EVP_CIPHER_CTX_set_padding(ctx, 0))
304
0
    goto out;
305
306
453
  if (!EVP_EncryptUpdate(ctx, output, &outl, input, (int)length))
307
0
    goto out;
308
309
453
  if (!EVP_EncryptFinal_ex(ctx, output + outl, &outl_tmp))
310
0
    goto out;
311
312
453
  r = SC_SUCCESS;
313
453
out:
314
453
  EVP_CIPHER_CTX_free(ctx);
315
453
  return r;
316
453
}
317
318
static int
319
openssl_dec(const EVP_CIPHER * cipher, const unsigned char *key, const unsigned char *iv,
320
    const unsigned char *input, size_t length, unsigned char *output)
321
187
{
322
187
  int r = SC_ERROR_INTERNAL;
323
187
  EVP_CIPHER_CTX * ctx = NULL;
324
187
  int outl = 0;
325
187
  int outl_tmp = 0;
326
187
  unsigned char iv_tmp[EVP_MAX_IV_LENGTH] = {0};
327
328
187
  memcpy(iv_tmp, iv, EVP_MAX_IV_LENGTH);
329
187
  ctx = EVP_CIPHER_CTX_new();
330
187
  if (ctx == NULL)
331
0
    goto out;
332
333
187
  if (!EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv_tmp) ||
334
187
      !EVP_CIPHER_CTX_set_padding(ctx, 0))
335
0
    goto out;
336
337
187
  if (!EVP_DecryptUpdate(ctx, output, &outl, input, (int)length))
338
0
    goto out;
339
340
187
  if (!EVP_DecryptFinal_ex(ctx, output + outl, &outl_tmp))
341
10
    goto out;
342
343
177
  r = SC_SUCCESS;
344
187
out:
345
187
  EVP_CIPHER_CTX_free(ctx);
346
187
  return r;
347
177
}
348
349
static int
350
aes128_encrypt_cmac_ft(struct sc_card *card, const unsigned char *key, int keysize,
351
  const unsigned char *input, size_t length, unsigned char *output,unsigned char *iv)
352
0
{
353
0
  unsigned char data1[32] = {0};
354
0
  unsigned char data2[32] = {0};
355
0
  unsigned char k1Bin[32] = {0};
356
0
  unsigned char k2Bin[32] = {0};
357
358
0
  unsigned char check = 0;
359
0
  BIGNUM *enc1,*lenc1;
360
0
  BIGNUM *enc2,*lenc2;
361
362
  // k1
363
0
  int offset = 0;
364
0
  int r = SC_ERROR_INTERNAL;
365
0
  unsigned char out[32] = {0};
366
0
  unsigned char iv0[EVP_MAX_IV_LENGTH] = {0};
367
0
  EVP_CIPHER *alg = sc_evp_cipher(card->ctx, "AES-128-ECB");
368
0
  r = openssl_enc(alg, key, iv0, data1, 16, out);
369
0
  if (r != SC_SUCCESS) {
370
0
    sc_log_openssl(card->ctx);
371
0
    sc_evp_cipher_free(alg);
372
0
    return r;
373
0
  }
374
375
0
  check = out[0];
376
0
  enc1 = BN_new();
377
0
  lenc1 = BN_new();
378
0
  BN_bin2bn(out,16,enc1);
379
0
  BN_lshift1(lenc1,enc1);
380
0
  BN_bn2bin(lenc1,k1Bin);
381
0
  if (check & 0x80) {
382
0
    offset = 1;
383
0
    k1Bin[15+offset] ^= 0x87;
384
0
  }
385
0
  BN_free(enc1);
386
0
  BN_free(lenc1);
387
388
  // k2
389
0
  enc2 = BN_new();
390
0
  lenc2 = BN_new();
391
0
  check = k1Bin[offset];
392
0
  BN_bin2bn(&k1Bin[offset],16,enc2);
393
394
0
  offset = 0;
395
0
  BN_lshift1(lenc2,enc2);
396
0
  BN_bn2bin(lenc2,k2Bin);
397
0
  if (check & 0x80) {
398
0
    offset = 1;
399
0
    k2Bin[15+offset] ^= 0x87;
400
0
  }
401
0
  BN_free(enc2);
402
0
  BN_free(lenc2);
403
  // padding
404
0
  if (length < 16) {
405
0
    memcpy(&data2[0],input,length);
406
0
    data2[length] = 0x80;
407
0
  }
408
409
  // k2 xor padded data
410
0
  for (int i = 0; i < 16; i++) {
411
0
    data2[i] = data2[i] ^ k2Bin[offset + i];
412
0
  }
413
0
  r = openssl_enc(alg, key, iv, data2, 16, output);
414
0
  sc_evp_cipher_free(alg);
415
0
  if (r != SC_SUCCESS)
416
0
    sc_log_openssl(card->ctx);
417
0
  return r;
418
0
}
419
420
static int
421
aes128_encrypt_cmac(struct sc_card *card, const unsigned char *key, int keysize,
422
  const unsigned char *input, size_t length, unsigned char *output)
423
177
{
424
177
  size_t mactlen = 0;
425
177
  int r = SC_ERROR_INTERNAL;
426
#if OPENSSL_VERSION_NUMBER < 0x30000000L
427
  CMAC_CTX *ctx = CMAC_CTX_new();
428
  if (ctx == NULL) {
429
    return SC_ERROR_INTERNAL;
430
  }
431
432
  if (!CMAC_Init(ctx, key, keysize / 8, EVP_aes_128_cbc(), NULL)) {
433
    goto err;
434
  }
435
  if (!CMAC_Update(ctx, input, length)) {
436
    goto err;
437
  }
438
  if (!CMAC_Final(ctx, output, &mactlen)) {
439
    goto err;
440
  }
441
  r = SC_SUCCESS;
442
err:
443
  CMAC_CTX_free(ctx);
444
#else
445
177
  EVP_MAC *mac = EVP_MAC_fetch(card->ctx->ossl3ctx->libctx, "cmac", NULL);
446
177
  if (mac == NULL) {
447
0
    return r;
448
0
  }
449
450
177
  OSSL_PARAM params[2] = {0};
451
177
  params[0] = OSSL_PARAM_construct_utf8_string("cipher","aes-128-cbc", 0);
452
177
  params[1] = OSSL_PARAM_construct_end();
453
454
177
  EVP_MAC_CTX *ctx = EVP_MAC_CTX_new(mac);
455
177
  if (ctx == NULL) {
456
0
    EVP_MAC_CTX_free(ctx);
457
0
    sc_log_openssl(card->ctx);
458
0
    return r;
459
0
  }
460
177
  if (!EVP_MAC_init(ctx, (const unsigned char *)key, keysize / 8, params)) {
461
0
    sc_log_openssl(card->ctx);
462
0
    goto err;
463
0
  }
464
177
  if (!EVP_MAC_update(ctx, input, length)) {
465
0
    sc_log_openssl(card->ctx);
466
0
    goto err;
467
0
  }
468
177
  if (!EVP_MAC_final(ctx, output, &mactlen, 16)) {
469
0
    sc_log_openssl(card->ctx);
470
0
    goto err;
471
0
  }
472
177
  r = SC_SUCCESS;
473
177
err:
474
177
  EVP_MAC_CTX_free(ctx);
475
177
  EVP_MAC_free(mac);
476
177
#endif
477
177
  return r;
478
177
}
479
480
static int
481
aes128_encrypt_ecb(struct sc_card *card, const unsigned char *key, int keysize,
482
    const unsigned char *input, size_t length, unsigned char *output)
483
14
{
484
14
  unsigned char iv[EVP_MAX_IV_LENGTH] = {0};
485
14
  EVP_CIPHER *alg = sc_evp_cipher(card->ctx, "AES-128-ECB");
486
14
  int r;
487
14
  r = openssl_enc(alg, key, iv, input, length, output);
488
14
  sc_evp_cipher_free(alg);
489
14
  if (r != SC_SUCCESS)
490
0
    sc_log_openssl(card->ctx);
491
14
  return r;
492
14
}
493
494
495
static int
496
aes128_encrypt_cbc(struct sc_card *card, const unsigned char *key, int keysize, unsigned char iv[16],
497
    const unsigned char *input, size_t length, unsigned char *output)
498
62
{
499
62
  EVP_CIPHER *alg = sc_evp_cipher(card->ctx, "AES-128-CBC");
500
62
  int r;
501
62
  r = openssl_enc(alg, key, iv, input, length, output);
502
62
  sc_evp_cipher_free(alg);
503
62
  if (r != SC_SUCCESS)
504
0
    sc_log_openssl(card->ctx);
505
62
  return r;
506
62
}
507
508
509
static int
510
aes128_decrypt_cbc(struct sc_card *card, const unsigned char *key, int keysize, unsigned char iv[16],
511
    const unsigned char *input, size_t length, unsigned char *output)
512
17
{
513
17
  EVP_CIPHER *alg = sc_evp_cipher(card->ctx, "AES-128-CBC");
514
17
  int r;
515
17
  r = openssl_dec(alg, key, iv, input, length, output);
516
17
  sc_evp_cipher_free(alg);
517
17
  if (r != SC_SUCCESS)
518
10
    sc_log_openssl(card->ctx);
519
17
  return r;
520
17
}
521
522
523
static int
524
des3_encrypt_ecb(struct sc_card *card, const unsigned char *key, int keysize,
525
    const unsigned char *input, int length, unsigned char *output)
526
24
{
527
24
  unsigned char iv[EVP_MAX_IV_LENGTH] = {0};
528
24
  unsigned char bKey[24] = {0};
529
24
  EVP_CIPHER *alg = sc_evp_cipher(card->ctx, "DES-EDE3");
530
24
  int r;
531
532
24
  if (keysize == 16) {
533
24
    memcpy(&bKey[0], key, 16);
534
24
    memcpy(&bKey[16], key, 8);
535
24
  } else {
536
0
    memcpy(&bKey[0], key, 24);
537
0
  }
538
539
24
  r = openssl_enc(alg, bKey, iv, input, length, output);
540
24
  sc_evp_cipher_free(alg);
541
24
  if (r != SC_SUCCESS)
542
0
    sc_log_openssl(card->ctx);
543
24
  return r;
544
24
}
545
546
547
static int
548
des3_encrypt_cbc(struct sc_card *card, const unsigned char *key, int keysize, unsigned char iv[EVP_MAX_IV_LENGTH],
549
    const unsigned char *input, size_t length, unsigned char *output)
550
13
{
551
13
  unsigned char bKey[24] = {0};
552
13
  EVP_CIPHER *alg = sc_evp_cipher(card->ctx, "DES-EDE3-CBC");
553
13
  int r;
554
555
13
  if (keysize == 16) {
556
13
    memcpy(&bKey[0], key, 16);
557
13
    memcpy(&bKey[16], key, 8);
558
13
  } else {
559
0
    memcpy(&bKey[0], key, 24);
560
0
  }
561
562
13
  r = openssl_enc(EVP_des_ede3_cbc(), bKey, iv, input, length, output);
563
13
  sc_evp_cipher_free(alg);
564
13
  if (r != SC_SUCCESS)
565
0
    sc_log_openssl(card->ctx);
566
13
  return r;
567
13
}
568
569
570
static int
571
des3_decrypt_cbc(struct sc_card *card, const unsigned char *key, int keysize, unsigned char iv[EVP_MAX_IV_LENGTH],
572
    const unsigned char *input, size_t length, unsigned char *output)
573
0
{
574
0
  unsigned char bKey[24] = {0};
575
0
  EVP_CIPHER *alg = sc_evp_cipher(card->ctx, "DES-EDE3-CBC");
576
0
  int r;
577
578
0
  if (keysize == 16) {
579
0
    memcpy(&bKey[0], key, 16);
580
0
    memcpy(&bKey[16], key, 8);
581
0
  } else {
582
0
    memcpy(&bKey[0], key, 24);
583
0
  }
584
585
0
  r = openssl_dec(alg, bKey, iv, input, length, output);
586
0
  sc_evp_cipher_free(alg);
587
0
  if (r != SC_SUCCESS)
588
0
    sc_log_openssl(card->ctx);
589
0
  return r;
590
0
}
591
592
593
static int
594
des_encrypt_cbc(struct sc_card *card, const unsigned char *key, int keysize, unsigned char iv[EVP_MAX_IV_LENGTH],
595
    const unsigned char *input, size_t length, unsigned char *output)
596
340
{
597
340
  EVP_CIPHER *alg = sc_evp_cipher(card->ctx, "DES-CBC");
598
340
  int r;
599
600
340
  r = openssl_enc(alg, key, iv, input, length, output);
601
340
  sc_evp_cipher_free(alg);
602
340
  if (r != SC_SUCCESS)
603
0
    sc_log_openssl(card->ctx);
604
340
  return r;
605
340
}
606
607
608
static int
609
des_decrypt_cbc(struct sc_card *card, const unsigned char *key, int keysize, unsigned char iv[EVP_MAX_IV_LENGTH],
610
    const unsigned char *input, size_t length, unsigned char *output)
611
170
{
612
170
  EVP_CIPHER *alg = sc_evp_cipher(card->ctx, "DES-CBC");
613
170
  int r;
614
615
170
  r = openssl_dec(alg, key, iv, input, length, output);
616
170
  sc_evp_cipher_free(alg);
617
170
  if (r != SC_SUCCESS)
618
0
    sc_log_openssl(card->ctx);
619
170
  return r;
620
170
}
621
622
623
static int
624
openssl_dig(const EVP_MD * digest, const unsigned char *input, size_t length,
625
    unsigned char *output)
626
0
{
627
0
  int r = 0;
628
0
  EVP_MD_CTX *ctx = NULL;
629
0
  unsigned outl = 0;
630
631
0
  ctx = EVP_MD_CTX_create();
632
0
  if (ctx == NULL) {
633
0
    r = SC_ERROR_OUT_OF_MEMORY;
634
0
    goto err;
635
0
  }
636
637
0
  EVP_MD_CTX_init(ctx);
638
0
  if (!EVP_DigestInit_ex(ctx, digest, NULL) ||
639
0
      !EVP_DigestUpdate(ctx, input, length)) {
640
0
    r = SC_ERROR_INTERNAL;
641
0
    goto err;
642
0
  }
643
644
0
  if (!EVP_DigestFinal_ex(ctx, output, &outl)) {
645
0
    r = SC_ERROR_INTERNAL;
646
0
    goto err;
647
0
  }
648
0
  r = SC_SUCCESS;
649
0
err:
650
0
  if (ctx)
651
0
    EVP_MD_CTX_destroy(ctx);
652
653
0
  return r;
654
0
}
655
656
657
static int
658
sha1_digest(struct sc_card *card, const unsigned char *input, size_t length, unsigned char *output)
659
0
{
660
0
  EVP_MD *md = sc_evp_md(card->ctx, "SHA1");
661
0
  int r;
662
663
0
  r = openssl_dig(md, input, length, output);
664
0
  sc_evp_md_free(md);
665
0
  if (r != SC_SUCCESS)
666
0
    sc_log_openssl(card->ctx);
667
0
  return r;
668
0
}
669
670
static int
671
sha256_digest(struct sc_card *card, const unsigned char *input, size_t length, unsigned char *output)
672
0
{
673
0
  EVP_MD *md = sc_evp_md(card->ctx, "SHA256");
674
0
  int r;
675
676
0
  r = openssl_dig(md, input, length, output);
677
0
  sc_evp_md_free(md);
678
0
  if (r != SC_SUCCESS)
679
0
    sc_log_openssl(card->ctx);
680
0
  return r;
681
0
}
682
683
684
static int
685
gen_init_key(struct sc_card *card, unsigned char *key_enc, unsigned char *key_mac,
686
    unsigned char *result, unsigned char key_type)
687
375
{
688
375
  int r;
689
375
  struct sc_apdu apdu;
690
375
  unsigned char data[256] = {0};
691
375
  unsigned char tmp_sm;
692
375
  unsigned char isFips;
693
375
  unsigned long blocksize = 0;
694
375
  unsigned char cryptogram[256] = {0}; /* host cryptogram */
695
375
  unsigned char iv[16] = {0};
696
375
  epass2003_exdata *exdata = NULL;
697
698
375
  if (!card->drv_data)
699
0
    return SC_ERROR_INVALID_ARGUMENTS;
700
701
375
  exdata = (epass2003_exdata *)card->drv_data;
702
375
  isFips = exdata->bFipsCertification;
703
704
375
  LOG_FUNC_CALLED(card->ctx);
705
706
375
  if (1 != RAND_bytes(g_random, sizeof(g_random)))
707
0
    return SC_ERROR_INTERNAL;
708
709
375
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x50, 0x00, 0x00);
710
375
  apdu.cla = 0x80;
711
375
  apdu.lc = apdu.datalen = sizeof(g_random);
712
375
  apdu.data = g_random; /* host random */
713
375
  if (isFips)
714
78
    apdu.le = apdu.resplen = 29;
715
297
  else
716
297
    apdu.le = apdu.resplen = 28;
717
718
375
  apdu.resp = result; /* card random is result[12~19] */
719
720
375
  tmp_sm = exdata->sm;
721
375
  exdata->sm = SM_PLAIN;
722
375
  r = epass2003_transmit_apdu(card, &apdu);
723
375
  exdata->sm = tmp_sm;
724
375
  LOG_TEST_RET(card->ctx, r, "APDU gen_init_key failed");
725
726
371
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
727
371
  LOG_TEST_RET(card->ctx, r, "gen_init_key failed");
728
729
  /* Step 1 - Generate Derivation data */
730
33
  if (isFips) {
731
14
    memset(data, 0x00, 15);
732
14
    data[11] = 0x04;
733
14
    data[14] = 0x80;
734
14
    data[15] = 0x01;
735
14
    memcpy(&data[16], g_random, 8);
736
14
    memcpy(&data[24], &result[12 + 1], 8);
737
19
  } else {
738
19
    memcpy(data, &result[16], 4);
739
19
    memcpy(&data[4], g_random, 4);
740
19
    memcpy(&data[8], &result[12], 4);
741
19
    memcpy(&data[12], &g_random[4], 4);
742
19
  }
743
744
  /* Step 2,3 - Create S-ENC/S-MAC Session Key */
745
33
  if (KEY_TYPE_AES == key_type) {
746
21
    if (isFips) {
747
14
      r = aes128_encrypt_cmac(card, key_enc, 128, data, 32, exdata->sk_enc);
748
14
      LOG_TEST_RET(card->ctx, r, "aes128_encrypt_cmac enc failed");
749
14
      memset(&data[11], 0x06, 1);
750
14
      r = aes128_encrypt_cmac(card, key_mac, 128, data, 32, exdata->sk_mac);
751
14
      LOG_TEST_RET(card->ctx, r, "aes128_encrypt_cmac mac  failed");
752
14
    } else {
753
7
      r = aes128_encrypt_ecb(card, key_enc, 16, data, 16, exdata->sk_enc);
754
7
      LOG_TEST_RET(card->ctx, r, "aes128_encrypt_ecb enc  failed");
755
7
      r = aes128_encrypt_ecb(card, key_mac, 16, data, 16, exdata->sk_mac);
756
7
      LOG_TEST_RET(card->ctx, r, "aes128_encrypt_ecb mac  failed");
757
7
    }
758
21
  } else {
759
12
    r = des3_encrypt_ecb(card, key_enc, 16, data, 16, exdata->sk_enc);
760
12
    LOG_TEST_RET(card->ctx, r, "des3_encrypt_ecb failed");
761
12
    r = des3_encrypt_ecb(card, key_mac, 16, data, 16, exdata->sk_mac);
762
12
    LOG_TEST_RET(card->ctx, r, "des3_encrypt_ecb failed");
763
12
  }
764
765
33
  if (isFips) {
766
14
    data[11] = 0x00;
767
14
    data[14] = 0x40;
768
19
  } else {
769
19
    memcpy(data, g_random, 8);
770
19
    memcpy(&data[8], &result[12], 8);
771
19
    data[16] = 0x80;
772
19
    blocksize = (key_type == KEY_TYPE_AES ? 16 : 8);
773
19
    memset(&data[17], 0x00, blocksize - 1);
774
19
  }
775
776
  /* calculate host cryptogram */
777
33
  if (KEY_TYPE_AES == key_type) {
778
21
    if (isFips) {
779
14
      r = aes128_encrypt_cmac(card, exdata->sk_enc, 128, data, 32, cryptogram);
780
14
    } else {
781
7
      r = aes128_encrypt_cbc(card, exdata->sk_enc, 16, iv, data, 16 + blocksize, cryptogram);
782
7
    }
783
21
  } else {
784
12
    r = des3_encrypt_cbc(card, exdata->sk_enc, 16, iv, data, 16 + blocksize, cryptogram);
785
12
  }
786
787
33
  LOG_TEST_RET(card->ctx, r, "calculate host cryptogram failed");
788
789
  /* verify card cryptogram */
790
33
  if (isFips) {
791
14
    if (0 != memcmp(&cryptogram[0], &result[20+1], 8))
792
14
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_CARD_CMD_FAILED);
793
19
  } else {
794
19
    if (0 != memcmp(&cryptogram[16], &result[20], 8))
795
19
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_CARD_CMD_FAILED);
796
19
  }
797
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
798
0
}
799
800
801
static int
802
verify_init_key(struct sc_card *card, unsigned char *ran_key, unsigned char key_type)
803
0
{
804
0
  int r;
805
0
  struct sc_apdu apdu;
806
0
  unsigned long blocksize = (key_type == KEY_TYPE_AES ? 16 : 8);
807
0
  unsigned char data[256] = {0};
808
0
  unsigned char cryptogram[256] = {0}; /* host cryptogram */
809
0
  unsigned char iv[16] = {0};
810
0
  unsigned char mac[256] = {0};
811
0
  unsigned long i;
812
0
  unsigned char tmp_sm;
813
0
  unsigned char isFips;
814
0
  epass2003_exdata *exdata = NULL;
815
816
0
  if (!card->drv_data)
817
0
    return SC_ERROR_INVALID_ARGUMENTS;
818
0
  exdata = (epass2003_exdata *)card->drv_data;
819
0
  isFips = exdata->bFipsCertification;
820
821
0
  LOG_FUNC_CALLED(card->ctx);
822
823
0
  if (isFips) {
824
0
    memset(data,0x00,15);
825
0
    data[11] = 0x01;
826
0
    data[14] = 0x40;
827
0
    data[15] = 0x01;
828
0
    memcpy(&data[16], g_random, 8);
829
0
    memcpy(&data[24], ran_key, 8);
830
0
  } else {
831
0
    memcpy(data, ran_key, 8);
832
0
    memcpy(&data[8], g_random, 8);
833
0
    data[16] = 0x80;
834
0
    memset(&data[17], 0x00, blocksize - 1);
835
0
    memset(iv, 0, 16);
836
0
  }
837
838
  /* calculate host cryptogram */
839
0
  if (KEY_TYPE_AES == key_type) {
840
0
    if (isFips) {
841
0
      r = aes128_encrypt_cmac(card, exdata->sk_enc, 128, data, 32, cryptogram);
842
0
    } else {
843
0
      r = aes128_encrypt_cbc(card, exdata->sk_enc, 16, iv, data, 16 + blocksize,cryptogram);
844
0
    }
845
0
  } else {
846
0
    r = des3_encrypt_cbc(card, exdata->sk_enc, 16, iv, data, 16 + blocksize,cryptogram);
847
0
  }
848
849
0
  LOG_TEST_RET(card->ctx, r, "calculate host cryptogram  failed");
850
851
0
  memset(data, 0, sizeof(data));
852
0
  memcpy(data, "\x84\x82\x03\x00\x10", 5);
853
0
  if (isFips) {
854
0
    memcpy(&data[5], &cryptogram[0], 8);
855
0
  } else {
856
0
    memcpy(&data[5], &cryptogram[16], 8);
857
0
    memcpy(&data[13], "\x80\x00\x00", 3);
858
0
  }
859
860
  /* calculate mac icv */
861
0
  memset(iv, 0x00, 16);
862
0
  if (KEY_TYPE_AES == key_type) {
863
0
    if (isFips) {
864
0
      r = aes128_encrypt_cmac(card, exdata->sk_mac, 128, data, 13, mac);
865
0
    } else {
866
0
      r = aes128_encrypt_cbc(card, exdata->sk_mac, 16, iv, data, 16, mac);
867
0
    }
868
0
    i = 0;
869
0
  } else {
870
0
    r = des3_encrypt_cbc(card, exdata->sk_mac, 16, iv, data, 16, mac);
871
0
    i = 8;
872
0
  }
873
874
0
  LOG_TEST_RET(card->ctx, r, "calculate mac icv failed");
875
  /* save mac icv */
876
0
  memset(exdata->icv_mac, 0x00, 16);
877
0
  memcpy(exdata->icv_mac, &mac[i], 8);
878
879
  /* verify host cryptogram */
880
0
  if (isFips) {
881
0
    memcpy(data, &cryptogram[0], 8);
882
0
  } else {
883
0
    memcpy(data, &cryptogram[16], 8);
884
0
  }
885
0
  memcpy(&data[8], &mac[i], 8);
886
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x82, 0x03, 0x00);
887
0
  apdu.cla = 0x84;
888
0
  apdu.lc = apdu.datalen = 16;
889
0
  apdu.data = data;
890
0
  tmp_sm = exdata->sm;
891
0
  exdata->sm = SM_PLAIN;
892
0
  r = epass2003_transmit_apdu(card, &apdu);
893
0
  exdata->sm = tmp_sm;
894
0
  LOG_TEST_RET(card->ctx, r, "APDU verify_init_key failed");
895
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
896
0
  LOG_TEST_RET(card->ctx, r, "verify_init_key failed");
897
0
  return r;
898
0
}
899
900
901
static int
902
mutual_auth(struct sc_card *card, unsigned char *key_enc,
903
      unsigned char *key_mac)
904
375
{
905
375
  struct sc_context *ctx = card->ctx;
906
375
  int r;
907
375
  unsigned char result[256] = {0};
908
375
  unsigned char ran_key[8] = {0};
909
375
  epass2003_exdata *exdata = NULL;
910
911
375
  if (!card->drv_data)
912
0
    return SC_ERROR_INVALID_ARGUMENTS;
913
375
  exdata = (epass2003_exdata *)card->drv_data;
914
915
375
  LOG_FUNC_CALLED(ctx);
916
917
375
  r = gen_init_key(card, key_enc, key_mac, result, exdata->smtype);
918
375
  LOG_TEST_RET(ctx, r, "gen_init_key failed");
919
0
  if (exdata->bFipsCertification) {
920
0
    memcpy(ran_key, &result[12+1], 8);
921
0
  } else {
922
0
    memcpy(ran_key, &result[12], 8);
923
0
  }
924
925
0
  r = verify_init_key(card, ran_key, exdata->smtype);
926
0
  LOG_TEST_RET(ctx, r, "verify_init_key failed");
927
928
0
  LOG_FUNC_RETURN(ctx, r);
929
0
}
930
931
932
int
933
epass2003_refresh(struct sc_card *card)
934
456
{
935
456
  int r = SC_SUCCESS;
936
456
  epass2003_exdata *exdata = NULL;
937
938
456
  if (!card->drv_data)
939
0
    return SC_ERROR_INVALID_ARGUMENTS;
940
941
456
  exdata = (epass2003_exdata *)card->drv_data;
942
943
456
  if (exdata->sm) {
944
    /* Hold the reader lock so that no other process can establish its own session in the
945
     * middle of the handshake. */
946
375
    card->sm_ctx.sm_mode = 0;
947
375
    r = sc_lock(card);
948
375
    if (r == SC_SUCCESS) {
949
375
      r = mutual_auth(card, g_init_key_enc, g_init_key_mac);
950
375
      sc_unlock(card);
951
375
    }
952
375
    LOG_TEST_RET(card->ctx, r, "mutual_auth failed");
953
0
    card->sm_ctx.sm_mode = SM_MODE_TRANSMIT;
954
0
  }
955
956
81
  return r;
957
456
}
958
959
/* SM was stopped (e.g. after an SM error): re-establish it instead of sending
960
 * the next command in plain */
961
static int
962
epass2003_sm_restart(struct sc_card *card)
963
881
{
964
881
  epass2003_exdata *exdata = (epass2003_exdata *)card->drv_data;
965
966
881
  if (exdata && exdata->sm && card->sm_ctx.sm_mode == SM_MODE_NONE)
967
121
    return epass2003_refresh(card);
968
760
  return SC_SUCCESS;
969
881
}
970
971
/* Data(TLV)=0x87|L|0x01+Cipher */
972
static int
973
construct_data_tlv(struct sc_card *card, struct sc_apdu *apdu, unsigned char *apdu_buf,
974
    unsigned char *data_tlv, size_t * data_tlv_len, const unsigned char key_type)
975
24
{
976
24
  size_t block_size = (KEY_TYPE_AES == key_type ? 16 : 8);
977
24
  unsigned char pad[4096] = {0};
978
24
  size_t pad_len;
979
24
  size_t tlv_more;  /* increased tlv length */
980
24
  unsigned char iv[16] = {0};
981
24
  epass2003_exdata *exdata = NULL;
982
24
  int r = 0;
983
984
24
  if (!card->drv_data)
985
0
    return SC_ERROR_INVALID_ARGUMENTS;
986
987
24
  exdata = (epass2003_exdata *)card->drv_data;
988
989
  /* we encrypt the pad buffer and then write it to the apdu_buf at offset block_size + tlv_more */
990
24
  if (apdu->lc >= sizeof(pad) - block_size - 5)
991
24
    LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_DATA,
992
24
        "ePass2003 secure messaging APDU data too large");
993
994
  /* padding */
995
24
  apdu_buf[block_size] = 0x87;
996
24
  memcpy(pad, apdu->data, apdu->lc);
997
24
  pad[apdu->lc] = 0x80;
998
24
  if ((apdu->lc + 1) % block_size)
999
22
    pad_len = ((apdu->lc + 1) / block_size + 1) * block_size;
1000
2
  else
1001
2
    pad_len = apdu->lc + 1;
1002
1003
  /* encode Lc' */
1004
24
  if (pad_len > 0x7E) {
1005
    /* Lc' > 0x7E, use extended APDU */
1006
18
    apdu_buf[block_size + 1] = 0x82;
1007
18
    apdu_buf[block_size + 2] = (unsigned char)((pad_len + 1) / 0x100);
1008
18
    apdu_buf[block_size + 3] = (unsigned char)((pad_len + 1) % 0x100);
1009
18
    apdu_buf[block_size + 4] = 0x01;
1010
18
    tlv_more = 5;
1011
18
  } else {
1012
6
    apdu_buf[block_size + 1] = (unsigned char)pad_len + 1;
1013
6
    apdu_buf[block_size + 2] = 0x01;
1014
6
    tlv_more = 3;
1015
6
  }
1016
24
  memcpy(data_tlv, &apdu_buf[block_size], tlv_more);
1017
1018
  /* encrypt Data */
1019
24
  if (KEY_TYPE_AES == key_type) {
1020
23
    r = aes128_encrypt_cbc(card, exdata->sk_enc, 16, iv, pad, pad_len, apdu_buf + block_size + tlv_more);
1021
23
    LOG_TEST_RET(card->ctx, r, "aes128_encrypt_cbc failed");
1022
23
  } else {
1023
1
    r = des3_encrypt_cbc(card, exdata->sk_enc, 16, iv, pad, pad_len, apdu_buf + block_size + tlv_more);
1024
1
    LOG_TEST_RET(card->ctx, r, "des3_encrypt_cbc failed");
1025
1
  }
1026
1027
24
  memcpy(data_tlv + tlv_more, apdu_buf + block_size + tlv_more, pad_len);
1028
24
  *data_tlv_len = tlv_more + pad_len;
1029
24
  return 0;
1030
24
}
1031
1032
1033
/* Le(TLV)=0x97|L|Le */
1034
static int
1035
construct_le_tlv(struct sc_apdu *apdu, unsigned char *apdu_buf, size_t data_tlv_len,
1036
    unsigned char *le_tlv, size_t * le_tlv_len, const unsigned char key_type)
1037
300
{
1038
300
  size_t block_size = (KEY_TYPE_AES == key_type ? 16 : 8);
1039
1040
300
  *(apdu_buf + block_size + data_tlv_len) = 0x97;
1041
300
  if (apdu->le > 0x7F) {
1042
    /* Le' > 0x7E, use extended APDU */
1043
1
    *(apdu_buf + block_size + data_tlv_len + 1) = 2;
1044
1
    *(apdu_buf + block_size + data_tlv_len + 2) = (unsigned char)(apdu->le / 0x100);
1045
1
    *(apdu_buf + block_size + data_tlv_len + 3) = (unsigned char)(apdu->le % 0x100);
1046
1
    memcpy(le_tlv, apdu_buf + block_size + data_tlv_len, 4);
1047
1
    *le_tlv_len = 4;
1048
299
  } else {
1049
299
    *(apdu_buf + block_size + data_tlv_len + 1) = 1;
1050
299
    *(apdu_buf + block_size + data_tlv_len + 2) = (unsigned char)apdu->le;
1051
299
    memcpy(le_tlv, apdu_buf + block_size + data_tlv_len, 3);
1052
299
    *le_tlv_len = 3;
1053
299
  }
1054
300
  return 0;
1055
300
}
1056
1057
1058
/* MAC(TLV)=0x8e|0x08|MAC */
1059
static int
1060
construct_mac_tlv(struct sc_card *card, unsigned char *apdu_buf, size_t data_tlv_len, size_t le_tlv_len,
1061
    unsigned char *mac_tlv, size_t * mac_tlv_len, const unsigned char key_type)
1062
324
{
1063
324
  size_t block_size = (KEY_TYPE_AES == key_type ? 16 : 8);
1064
324
  unsigned char mac[4096] = {0};
1065
324
  size_t mac_len;
1066
324
  unsigned char icv[16] = {0};
1067
324
  int r ;
1068
324
  int i = (KEY_TYPE_AES == key_type ? 15 : 7);
1069
324
  epass2003_exdata *exdata = NULL;
1070
1071
324
  if (!card->drv_data)
1072
0
    return SC_ERROR_INVALID_ARGUMENTS;
1073
1074
324
  exdata = (epass2003_exdata *)card->drv_data;
1075
1076
324
  if (data_tlv_len + le_tlv_len + block_size + 1 > sizeof(mac)) {
1077
0
    return SC_ERROR_BUFFER_TOO_SMALL;
1078
0
  }
1079
1080
324
  if (0 == data_tlv_len && 0 == le_tlv_len) {
1081
0
    mac_len = block_size;
1082
324
  } else {
1083
    /* padding */
1084
324
    *(apdu_buf + block_size + data_tlv_len + le_tlv_len) = 0x80;
1085
324
    if ((data_tlv_len + le_tlv_len + 1) % block_size) {
1086
324
      mac_len = (((data_tlv_len + le_tlv_len + 1) / block_size) +
1087
324
          1) * block_size + block_size;
1088
324
    } else {
1089
0
      mac_len = data_tlv_len + le_tlv_len + 1 + block_size;
1090
0
    }
1091
324
    memset((apdu_buf + block_size + data_tlv_len + le_tlv_len + 1),
1092
324
           0, (mac_len - (data_tlv_len + le_tlv_len + 1)));
1093
324
  }
1094
1095
  /* increase icv */
1096
324
  for (; i >= 0; i--) {
1097
324
    if (exdata->icv_mac[i] == 0xff) {
1098
0
      exdata->icv_mac[i] = 0;
1099
324
    } else {
1100
324
      exdata->icv_mac[i]++;
1101
324
      break;
1102
324
    }
1103
324
  }
1104
1105
  /* calculate MAC */
1106
324
  memset(icv, 0, sizeof(icv));
1107
324
  memcpy(icv, exdata->icv_mac, 16);
1108
324
  if (KEY_TYPE_AES == key_type) {
1109
164
    if (exdata->bFipsCertification) {
1110
2.29k
      for (int i = 0; i < 16; i++) {
1111
2.16k
        apdu_buf[i] = apdu_buf[i] ^ icv[i];
1112
2.16k
      }
1113
135
      r = aes128_encrypt_cmac(card, exdata->sk_mac, 128, apdu_buf, data_tlv_len + le_tlv_len + block_size, mac);
1114
135
      LOG_TEST_RET(card->ctx, r, "aes128_encrypt_cmac failed");
1115
135
      memcpy(mac_tlv + 2, &mac[0 /*ulmacLen-16*/], 8);
1116
675
      for (int j = 0; j < 4; j++) {
1117
540
        apdu_buf[j] = apdu_buf[j] ^ icv[j];
1118
540
      }
1119
135
    } else {
1120
29
      r = aes128_encrypt_cbc(card, exdata->sk_mac, 16, icv, apdu_buf, mac_len, mac);
1121
29
      LOG_TEST_RET(card->ctx, r, "aes128_encrypt_cbc failed");
1122
29
      memcpy(mac_tlv + 2, &mac[mac_len - 16], 8);
1123
29
    }
1124
164
  } else {
1125
160
    unsigned char iv[EVP_MAX_IV_LENGTH] = {0};
1126
160
    unsigned char tmp[8] = {0};
1127
160
    r = des_encrypt_cbc(card, exdata->sk_mac, 8, icv, apdu_buf, mac_len, mac);
1128
160
    LOG_TEST_RET(card->ctx, r, "des_encrypt_cbc 1 failed");
1129
160
    r = des_decrypt_cbc(card, &exdata->sk_mac[8], 8, iv, &mac[mac_len - 8], 8, tmp);
1130
160
    LOG_TEST_RET(card->ctx, r, "des_decrypt_cbc failed");
1131
160
    memset(iv, 0x00, sizeof iv);
1132
160
    r = des_encrypt_cbc(card, exdata->sk_mac, 8, iv, tmp, 8, mac_tlv + 2);
1133
160
    LOG_TEST_RET(card->ctx, r, "des_encrypt_cbc 2 failed");
1134
160
  }
1135
1136
324
  *mac_tlv_len = 2 + 8;
1137
324
  return 0;
1138
324
}
1139
1140
/* MAC(TLV case 1) */
1141
static int
1142
construct_mac_tlv_case1(struct sc_card *card, unsigned char *apdu_buf, size_t data_tlv_len, size_t le_tlv_len,
1143
  unsigned char *mac_tlv, size_t * mac_tlv_len, const unsigned char key_type)
1144
0
{
1145
0
  int r;
1146
0
  size_t block_size = 4;
1147
0
  unsigned char mac[4096] = {0};
1148
0
  size_t mac_len;
1149
0
  int i = (KEY_TYPE_AES == key_type ? 15 : 7);
1150
0
  unsigned char icv[16] = {0};
1151
1152
0
  epass2003_exdata *exdata = NULL;
1153
1154
0
  if (!card->drv_data)
1155
0
    return SC_ERROR_INVALID_ARGUMENTS;
1156
1157
0
  exdata = (epass2003_exdata *)card->drv_data;
1158
1159
0
  if (data_tlv_len + le_tlv_len + block_size + 1 > sizeof(mac)) {
1160
0
    return SC_ERROR_BUFFER_TOO_SMALL;
1161
0
  }
1162
1163
0
  if (0 == data_tlv_len && 0 == le_tlv_len) {
1164
0
    mac_len = block_size;
1165
0
  } else {
1166
    /* padding */
1167
0
    *(apdu_buf + block_size + data_tlv_len + le_tlv_len) = 0x80;
1168
0
    if ((data_tlv_len + le_tlv_len + 1) % block_size) {
1169
0
      mac_len = (((data_tlv_len + le_tlv_len + 1) / block_size) + 1) * block_size + block_size;
1170
0
    } else {
1171
0
      mac_len = data_tlv_len + le_tlv_len + 1 + block_size;
1172
0
    }
1173
0
  }
1174
  /* increase icv */
1175
0
  for (; i >= 0; i--) {
1176
0
    if (exdata->icv_mac[i] == 0xff) {
1177
0
      exdata->icv_mac[i] = 0;
1178
0
    } else {
1179
0
      exdata->icv_mac[i]++;
1180
0
      break;
1181
0
    }
1182
0
  }
1183
1184
  /* calculate MAC */
1185
0
  memset(icv, 0, sizeof(icv));
1186
0
  memcpy(icv, exdata->icv_mac, 16);
1187
0
  if (KEY_TYPE_AES == key_type) {
1188
0
    if (exdata->bFipsCertification) {
1189
0
      r = aes128_encrypt_cmac_ft(card, exdata->sk_mac, 128, apdu_buf, data_tlv_len + le_tlv_len + block_size, mac, &icv[0]);
1190
0
      LOG_TEST_RET(card->ctx, r, "aes128_encrypt_cmac_ft failed");
1191
0
      memcpy(mac_tlv + 2, &mac[0 /*ulmacLen-16*/], 8);
1192
0
    } else {
1193
0
      if (mac_len < 16)
1194
0
        LOG_TEST_RET(card->ctx, SC_ERROR_INTERNAL, "incorrect mac length");
1195
0
      r = aes128_encrypt_cbc(card, exdata->sk_mac, 16, icv, apdu_buf, mac_len, mac);
1196
0
      LOG_TEST_RET(card->ctx, r, "aes128_encrypt_cbc failed");
1197
0
      memcpy(mac_tlv + 2, &mac[mac_len - 16], 8);
1198
0
    }
1199
0
  } else {
1200
0
    unsigned char iv[EVP_MAX_IV_LENGTH] = {0};
1201
0
    unsigned char tmp[8] = {0};
1202
0
    if (mac_len < 8)
1203
0
      LOG_TEST_RET(card->ctx, SC_ERROR_INTERNAL, "incorrect mac length");
1204
0
    r = des_encrypt_cbc(card, exdata->sk_mac, 8, icv, apdu_buf, mac_len, mac);
1205
0
    LOG_TEST_RET(card->ctx, r, "des_encrypt_cbc  failed");
1206
0
    r = des_decrypt_cbc(card, &exdata->sk_mac[8], 8, iv, &mac[mac_len - 8], 8, tmp);
1207
0
    LOG_TEST_RET(card->ctx, r, "des_decrypt_cbc failed");
1208
0
    memset(iv, 0x00, sizeof iv);
1209
0
    r = des_encrypt_cbc(card, exdata->sk_mac, 8, iv, tmp, 8, mac_tlv + 2);
1210
0
    LOG_TEST_RET(card->ctx, r, "des_encrypt_cbc failed");
1211
0
  }
1212
1213
0
  *mac_tlv_len = 2 + 8;
1214
0
  return 0;
1215
0
}
1216
1217
/* According to GlobalPlatform Card Specification's SCP01
1218
 * encode APDU from
1219
 * CLA INS P1 P2 [Lc] Data [Le]
1220
 * to
1221
 * CLA INS P1 P2 Lc' Data' [Le]
1222
 * where
1223
 * Data'=Data(TLV)+Le(TLV)+MAC(TLV) */
1224
static int
1225
encode_apdu(struct sc_card *card, struct sc_apdu *plain, struct sc_apdu *sm,
1226
    unsigned char *apdu_buf, size_t * apdu_buf_len)
1227
324
{
1228
324
  size_t block_size = 0;
1229
324
  unsigned char dataTLV[4096] = {0};
1230
324
  size_t data_tlv_len = 0;
1231
324
  unsigned char le_tlv[256] = {0};
1232
324
  size_t le_tlv_len = 0;
1233
324
  size_t mac_tlv_len = 10;
1234
324
  size_t tmp_lc = 0;
1235
324
  size_t tmp_le = 0;
1236
324
  size_t expected_total_len;
1237
324
  unsigned char mac_tlv[256] = {0};
1238
324
  epass2003_exdata *exdata = NULL;
1239
1240
324
  mac_tlv[0] = 0x8E;
1241
324
  mac_tlv[1] = 8;
1242
  /* size_t plain_le = 0; */
1243
324
  if (!card->drv_data)
1244
0
    return SC_ERROR_INVALID_ARGUMENTS;
1245
324
  exdata = (epass2003_exdata*)card->drv_data;
1246
324
  block_size = (KEY_TYPE_DES == exdata->smtype ? 16 : 8);
1247
1248
324
  sm->cse = SC_APDU_CASE_4_SHORT;
1249
324
  apdu_buf[0] = (unsigned char)plain->cla;
1250
324
  apdu_buf[1] = (unsigned char)plain->ins;
1251
324
  apdu_buf[2] = (unsigned char)plain->p1;
1252
324
  apdu_buf[3] = (unsigned char)plain->p2;
1253
  /* plain_le = plain->le; */
1254
  /* padding */
1255
324
  if (exdata->bFipsCertification && plain->lc == 0 && apdu_buf[1] == 0x82 && apdu_buf[2] == 0x01) {
1256
0
    apdu_buf[4] = 0x00;
1257
324
  } else {
1258
324
    apdu_buf[4] = 0x80;
1259
324
  }
1260
324
  memset(&apdu_buf[5], 0x00, block_size - 5);
1261
1262
  /* Data -> Data' */
1263
324
  if (plain->lc != 0)
1264
24
    if (0 != construct_data_tlv(card, plain, apdu_buf, dataTLV, &data_tlv_len, exdata->smtype))
1265
0
      return -1;
1266
1267
324
  if (plain->le != 0 || (plain->le == 0 && plain->resplen != 0))
1268
300
    if (0 != construct_le_tlv(plain, apdu_buf, data_tlv_len, le_tlv,
1269
300
             &le_tlv_len, exdata->smtype))
1270
0
      return -1;
1271
1272
324
  if (exdata->bFipsCertification && plain->lc == 0 && apdu_buf[1] == 0x82 && apdu_buf[2] == 0x01) {
1273
0
    if (0 != construct_mac_tlv_case1(card, apdu_buf, data_tlv_len, le_tlv_len, mac_tlv, &mac_tlv_len, exdata->smtype))
1274
0
      return -1;
1275
324
  } else {
1276
324
    if (0 != construct_mac_tlv(card, apdu_buf, data_tlv_len, le_tlv_len, mac_tlv, &mac_tlv_len, exdata->smtype))
1277
0
      return -1;
1278
324
  }
1279
1280
324
  memset(apdu_buf + 4, 0, *apdu_buf_len - 4);
1281
324
  sm->lc = sm->datalen = data_tlv_len + le_tlv_len + mac_tlv_len;
1282
324
  if (sm->lc > 0xFF) {
1283
0
    sm->cse = SC_APDU_CASE_4_EXT;
1284
0
    apdu_buf[4] = (unsigned char)((sm->lc) / 0x10000);
1285
0
    apdu_buf[5] = (unsigned char)(((sm->lc) / 0x100) % 0x100);
1286
0
    apdu_buf[6] = (unsigned char)((sm->lc) % 0x100);
1287
0
    tmp_lc = 3;
1288
324
  } else {
1289
324
    apdu_buf[4] = (unsigned char)sm->lc;
1290
324
    tmp_lc = 1;
1291
324
  }
1292
1293
  /* 2 is for Le extension in the worst case */
1294
324
  expected_total_len = 4 + tmp_lc + data_tlv_len + le_tlv_len + mac_tlv_len + 2;
1295
1296
324
  if (expected_total_len > *apdu_buf_len) {
1297
0
      return SC_ERROR_BUFFER_TOO_SMALL;
1298
0
  }
1299
1300
324
  memcpy(apdu_buf + 4 + tmp_lc, dataTLV, data_tlv_len);
1301
324
  memcpy(apdu_buf + 4 + tmp_lc + data_tlv_len, le_tlv, le_tlv_len);
1302
324
  memcpy(apdu_buf + 4 + tmp_lc + data_tlv_len + le_tlv_len, mac_tlv, mac_tlv_len);
1303
324
  memcpy((unsigned char *)sm->data, apdu_buf + 4 + tmp_lc, sm->datalen);
1304
324
  *apdu_buf_len = 0;
1305
1306
324
  if (4 == le_tlv_len) {
1307
1
    sm->cse = SC_APDU_CASE_4_EXT;
1308
1
    *(apdu_buf + 4 + tmp_lc + sm->lc) = (unsigned char)(plain->le / 0x100);
1309
1
    *(apdu_buf + 4 + tmp_lc + sm->lc + 1) = (unsigned char)(plain->le % 0x100);
1310
1
    tmp_le = 2;
1311
323
  } else if (3 == le_tlv_len) {
1312
299
    *(apdu_buf + 4 + tmp_lc + sm->lc) = (unsigned char)plain->le;
1313
299
    tmp_le = 1;
1314
299
  }
1315
1316
324
  *apdu_buf_len += 4 + tmp_lc + data_tlv_len + le_tlv_len + mac_tlv_len + tmp_le;
1317
  /* sm->le = calc_le(plain_le); */
1318
324
  return 0;
1319
324
}
1320
1321
1322
static int
1323
epass2003_sm_wrap_apdu(struct sc_card *card, struct sc_apdu *plain, struct sc_apdu *sm)
1324
1.32k
{
1325
1.32k
  unsigned char buf[4096] = {0}; /* APDU buffer */
1326
1.32k
  size_t buf_len = sizeof(buf);
1327
1.32k
  epass2003_exdata *exdata = NULL;
1328
1329
1.32k
  if (!card->drv_data)
1330
0
    return SC_ERROR_INVALID_ARGUMENTS;
1331
1332
1.32k
  exdata = (epass2003_exdata *)card->drv_data;
1333
1334
1.32k
  LOG_FUNC_CALLED(card->ctx);
1335
1336
1.32k
  if (exdata->sm)
1337
324
    plain->cla |= 0x0C;
1338
1339
1.32k
  sm->cse = plain->cse;
1340
1.32k
  sm->cla = plain->cla;
1341
1.32k
  sm->ins = plain->ins;
1342
1.32k
  sm->p1 = plain->p1;
1343
1.32k
  sm->p2 = plain->p2;
1344
1.32k
  sm->lc = plain->lc;
1345
1.32k
  sm->le = plain->le;
1346
1.32k
  sm->control = plain->control;
1347
1.32k
  sm->flags = plain->flags;
1348
1349
1.32k
  switch (sm->cla & 0x0C) {
1350
1.00k
  case 0x00:
1351
1.00k
  case 0x04:
1352
1.00k
    sm->datalen = plain->datalen;
1353
1.00k
    memcpy((void *)sm->data, plain->data, plain->datalen);
1354
1.00k
    sm->resplen = plain->resplen;
1355
1.00k
    memcpy(sm->resp, plain->resp, plain->resplen);
1356
1.00k
    break;
1357
324
  case 0x0C:
1358
324
    memset(buf, 0, sizeof(buf));
1359
324
    if (0 != encode_apdu(card, plain, sm, buf, &buf_len))
1360
0
      return SC_ERROR_CARD_CMD_FAILED;
1361
324
    break;
1362
324
  default:
1363
0
    return SC_ERROR_INCORRECT_PARAMETERS;
1364
1.32k
  }
1365
1366
1.32k
  return SC_SUCCESS;
1367
1.32k
}
1368
1369
static int
1370
epass2003_sm_read_tag(struct sc_card *card, const u8 **buf, size_t buflen,
1371
    unsigned int *cla_out, unsigned int *tag_out, size_t *taglen)
1372
370
{
1373
370
  const u8 *p;
1374
1375
370
  if (!buf || !*buf)
1376
0
    return sc_asn1_read_tag(buf, buflen, cla_out, tag_out, taglen);
1377
1378
370
  p = *buf;
1379
370
  if (buflen >= 3 && p[0] == 0x87 && (p[1] & 0x80) && p[2] == 0x01 && p[1] != 0x82) {
1380
    /*
1381
     * Workaround for EPASS2003 firmware bug:
1382
     * Instead of (correct) TLV 87 81 81 [01 .. ..], incorrect TLV 87 81 [01 .. ..]
1383
     * is returned (0x81 is encoded as a short-form length).
1384
     */
1385
17
    size_t len = p[1];
1386
17
    if (buflen - 2 < len) {
1387
4
      *buf = NULL;
1388
4
      return SC_ERROR_ASN1_END_OF_CONTENTS;
1389
4
    }
1390
13
    sc_log(card->ctx, "Workaround for wrong BER-TLV in SM response");
1391
13
    *cla_out = (p[0] & SC_ASN1_TAG_CLASS) | (p[0] & SC_ASN1_TAG_CONSTRUCTED);
1392
13
    *tag_out = p[0] & SC_ASN1_TAG_PRIMITIVE;
1393
13
    *taglen = len;
1394
13
    *buf = p + 2;
1395
13
    return SC_SUCCESS;
1396
17
  }
1397
1398
353
  return sc_asn1_read_tag(buf, buflen, cla_out, tag_out, taglen);
1399
370
}
1400
1401
1402
static int
1403
epass2003_check_response_mac_and_sw(struct sc_card *card, struct sc_apdu *sm)
1404
324
{
1405
324
  unsigned char iv[16];
1406
324
  unsigned char *data = NULL, *mac = NULL;
1407
324
  size_t blocksize, mac_len;
1408
324
  int ret = -1;
1409
324
  unsigned int cla = 0, tag = 0;
1410
324
  size_t taglen = 0;
1411
324
  const u8 *p = sm->resp;
1412
324
  const u8 *tag_start;
1413
324
  const u8 *mac_val;
1414
324
  size_t left = sm->resplen;
1415
324
  epass2003_exdata *exdata;
1416
324
  unsigned char *in = sm->resp;
1417
324
  size_t inlen = sm->resplen;
1418
1419
  /* card/ctx/drv_data is already checked by caller */
1420
324
  exdata = (epass2003_exdata *)card->drv_data;
1421
1422
  /* The SM must contain at least TLV encoded SW and MAC fields. */
1423
324
  if (inlen < 14)
1424
115
    return ret;
1425
1426
  /* Optional DO 0x87 */
1427
209
  tag_start = p;
1428
209
  if (SC_SUCCESS != epass2003_sm_read_tag(card, &p, left, &cla, &tag, &taglen) || p == NULL)
1429
17
    return ret;
1430
192
  left -= (p - tag_start);
1431
192
  if (taglen > left)
1432
0
    return ret;
1433
1434
192
  if ((cla | tag) == 0x87) {
1435
34
    p += taglen;
1436
34
    left -= taglen;
1437
1438
    /* Read next tag (must be DO 0x99) */
1439
34
    tag_start = p;
1440
34
    if (SC_SUCCESS != epass2003_sm_read_tag(card, &p, left, &cla, &tag, &taglen) || p == NULL)
1441
10
      return ret;
1442
24
    left -= (p - tag_start);
1443
24
    if (taglen > left)
1444
0
      return ret;
1445
24
  }
1446
1447
  /* compare BER-TLV encoded SW (TAG 0x99) and raw SW */
1448
182
  if ((cla | tag) != 0x99 || taglen != 2)
1449
37
    return ret;
1450
145
  if (sm->sw1 != p[0] || sm->sw2 != p[1])
1451
17
    return ret;
1452
1453
128
  p += taglen;
1454
128
  left -= taglen;
1455
1456
  /* no documentation/real hardware to test, the response is accepted without MAC check */
1457
128
  if (exdata->bFipsCertification) {
1458
90
    sc_log(card->ctx, "Warning, MAC is not checked");
1459
90
    return 0;
1460
90
  }
1461
1462
  /* Next tag must be DO 0x8E */
1463
38
  tag_start = p;
1464
38
  if (SC_SUCCESS != epass2003_sm_read_tag(card, &p, left, &cla, &tag, &taglen) || p == NULL)
1465
3
    return ret;
1466
35
  left -= (p - tag_start);
1467
35
  if (taglen > left)
1468
0
    return ret;
1469
1470
35
  if ((cla | tag) != 0x8e || taglen != 8)
1471
22
    return ret;
1472
13
  mac_val = p;
1473
1474
13
  if (KEY_TYPE_AES == exdata->smtype)
1475
3
    blocksize = 16;
1476
10
  else
1477
10
    blocksize = 8;
1478
1479
13
  mac_len = tag_start - in;
1480
13
  if (NULL == (data = calloc(1, mac_len + blocksize)))
1481
0
    goto end;
1482
13
  if (NULL == (mac = malloc(mac_len + blocksize)))
1483
0
    goto end;
1484
1485
  /* copy response to buffer and append padding */
1486
13
  memcpy(data, in, mac_len);
1487
13
  data[mac_len++] = 0x80;
1488
1489
13
  if (mac_len % blocksize)
1490
13
    mac_len += (blocksize - (mac_len % blocksize));
1491
1492
  /* calculate MAC */
1493
13
  memcpy(iv, exdata->icv_mac, blocksize);
1494
1495
13
  if (KEY_TYPE_AES == exdata->smtype) {
1496
3
    if (aes128_encrypt_cbc(card, exdata->sk_mac, 16, iv, data, mac_len, mac))
1497
0
      goto end;
1498
10
  } else {
1499
10
    uint8_t tmp[8];
1500
10
    uint8_t iv0[EVP_MAX_IV_LENGTH];
1501
10
    if (des_encrypt_cbc(card, exdata->sk_mac, 8, iv, data, mac_len, mac))
1502
0
      goto end;
1503
10
    memset(iv0, 0, EVP_MAX_IV_LENGTH);
1504
10
    if (des_decrypt_cbc(card, &exdata->sk_mac[8], 8, iv0, &mac[mac_len - 8], 8, tmp))
1505
0
      goto end;
1506
10
    memset(iv0, 0, EVP_MAX_IV_LENGTH);
1507
10
    if (des_encrypt_cbc(card, exdata->sk_mac, 8, iv0, tmp, 8, &mac[mac_len - 8]))
1508
0
      goto end;
1509
10
  }
1510
  /* compare MAC */
1511
13
  if (!memcmp(mac_val, mac + mac_len - blocksize, 8))
1512
4
    ret = 0;
1513
13
end:
1514
13
  if (data)
1515
13
    free(data);
1516
13
  if (mac)
1517
13
    free(mac);
1518
13
  return ret;
1519
13
}
1520
1521
/* According to GlobalPlatform Card Specification's SCP01
1522
 * decrypt APDU response from
1523
 * ResponseData' SW1 SW2
1524
 * to
1525
 * ResponseData SW1 SW2
1526
 * where
1527
 * ResponseData'=Data(TLV)+SW12(TLV)+MAC(TLV)
1528
 * where
1529
 * Data(TLV)=0x87|L|Cipher
1530
 * SW12(TLV)=0x99|0x02|SW1+SW2
1531
 * MAC(TLV)=0x8e|0x08|MAC */
1532
static int
1533
decrypt_response(struct sc_card *card, unsigned char *in, size_t inlen, unsigned char *out, size_t * out_len)
1534
89
{
1535
89
  unsigned int cla = 0, tag = 0;
1536
89
  const unsigned char *p = in;
1537
89
  size_t cipher_len;
1538
89
  unsigned char iv[16] = {0};
1539
89
  unsigned char plaintext[4096] = {0};
1540
89
  epass2003_exdata *exdata = NULL;
1541
1542
89
  if (!card->drv_data)
1543
0
    return SC_ERROR_INVALID_ARGUMENTS;
1544
1545
89
  exdata = (epass2003_exdata *)card->drv_data;
1546
1547
89
  if (SC_SUCCESS != epass2003_sm_read_tag(card, &p, inlen, &cla, &tag, &cipher_len) || p == NULL) {
1548
0
    return -1;
1549
0
  }
1550
89
  if ((cla | tag) == 0x99)
1551
71
    return 0;
1552
18
  if ((cla | tag) != 0x87)
1553
0
    return -1;
1554
1555
18
  if (cipher_len < 2 || cipher_len > sizeof plaintext)
1556
1
    return -1;
1557
1558
  /* Skip the 0x01 separator between tag and ciphertext */
1559
17
  p++;
1560
  /* decrypt */
1561
17
  if (KEY_TYPE_AES == exdata->smtype)
1562
17
    aes128_decrypt_cbc(card, exdata->sk_enc, 16, iv, p, cipher_len - 1, plaintext);
1563
0
  else
1564
0
    des3_decrypt_cbc(card, exdata->sk_enc, 16, iv, p, cipher_len - 1, plaintext);
1565
1566
  /* unpadding */
1567
659
  while (0x80 != plaintext[cipher_len - 2] && (cipher_len > 2))
1568
642
    cipher_len--;
1569
1570
17
  if (2 == cipher_len || *out_len < cipher_len - 2)
1571
6
    return -1;
1572
1573
11
  memcpy(out, plaintext, cipher_len - 2);
1574
11
  *out_len = cipher_len - 2;
1575
11
  return 0;
1576
17
}
1577
1578
1579
static int
1580
epass2003_sm_unwrap_apdu(struct sc_card *card, struct sc_apdu *sm, struct sc_apdu *plain)
1581
1.32k
{
1582
1.32k
  int r;
1583
1.32k
  size_t len = 0;
1584
1.32k
  epass2003_exdata *exdata = NULL;
1585
1586
1.32k
  if (!card->drv_data)
1587
0
    return SC_ERROR_INVALID_ARGUMENTS;
1588
1589
1.32k
  exdata = (epass2003_exdata *)card->drv_data;
1590
1591
1.32k
  LOG_FUNC_CALLED(card->ctx);
1592
1593
  /* verify MAC, and check if SW1,2 match SW1,2 encapsulated in SM */
1594
1.32k
  if (exdata->sm) {
1595
324
    if (epass2003_check_response_mac_and_sw(card, sm)) {
1596
230
      sc_log(card->ctx, "MAC or SW incorrect");
1597
      /* The card answers 6988 in plain when our session keys are no longer valid,
1598
       * e.g. because another process has established its own session. Let
1599
       * sc_sm_single_transmit() re-establish secure messaging and retry. */
1600
230
      if (sc_check_sw(card, sm->sw1, sm->sw2) == SC_ERROR_INCORRECT_SM_OBJECTS)
1601
6
        return SC_ERROR_INCORRECT_SM_OBJECTS;
1602
224
      return SC_ERROR_CARD_CMD_FAILED;
1603
230
    }
1604
324
  }
1605
1.09k
  r = sc_check_sw(card, sm->sw1, sm->sw2);
1606
1.09k
  if (r == SC_SUCCESS) {
1607
961
    if (exdata->sm) {
1608
89
      len = plain->resplen;
1609
89
      if (0 != decrypt_response(card, sm->resp, sm->resplen, plain->resp, &len))
1610
7
        return SC_ERROR_CARD_CMD_FAILED;
1611
872
    } else {
1612
872
      if (sm->resplen > 0) {
1613
670
        if (!plain->resp || plain->resplen < sm->resplen)
1614
0
          return SC_ERROR_BUFFER_TOO_SMALL;
1615
670
        memcpy(plain->resp, sm->resp, sm->resplen);
1616
670
      }
1617
872
      len = sm->resplen;
1618
872
    }
1619
961
  }
1620
1621
1.08k
  plain->resplen = len;
1622
1.08k
  plain->sw1 = sm->sw1;
1623
1.08k
  plain->sw2 = sm->sw2;
1624
1625
1.08k
  sc_log(card->ctx, "unwrapped APDU: resplen %zu, SW %02X%02X", plain->resplen, plain->sw1, plain->sw2);
1626
1.08k
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
1627
1.08k
}
1628
1629
1630
static int
1631
epass2003_sm_free_wrapped_apdu(struct sc_card *card,
1632
    struct sc_apdu *plain, struct sc_apdu **sm_apdu)
1633
1.32k
{
1634
1.32k
  struct sc_context *ctx = card->ctx;
1635
1.32k
  int rv = SC_SUCCESS;
1636
1637
1.32k
  LOG_FUNC_CALLED(ctx);
1638
1.32k
  if (!sm_apdu)
1639
1.32k
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
1640
1.32k
  if (!(*sm_apdu))
1641
1.32k
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1642
1643
1644
1.32k
  if (plain)
1645
1.32k
    rv = epass2003_sm_unwrap_apdu(card, *sm_apdu, plain);
1646
1647
1.32k
  if ((*sm_apdu)->data) {
1648
1.32k
    unsigned char * p = (unsigned char *)((*sm_apdu)->data);
1649
1.32k
    free(p);
1650
1.32k
  }
1651
1.32k
  if ((*sm_apdu)->resp) {
1652
1.32k
    free((*sm_apdu)->resp);
1653
1.32k
  }
1654
1655
1.32k
  free(*sm_apdu);
1656
1.32k
  *sm_apdu = NULL;
1657
1658
1.32k
  LOG_FUNC_RETURN(ctx, rv);
1659
1.32k
}
1660
1661
1662
static int
1663
epass2003_sm_get_wrapped_apdu(struct sc_card *card,
1664
    struct sc_apdu *plain, struct sc_apdu **sm_apdu)
1665
1.32k
{
1666
1.32k
  struct sc_context *ctx = card->ctx;
1667
1.32k
  struct sc_apdu *apdu = NULL;
1668
1.32k
  int rv;
1669
1670
1.32k
  LOG_FUNC_CALLED(ctx);
1671
1.32k
  if (!plain || !sm_apdu)
1672
1.32k
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
1673
1674
  /* A command rejected with 6988 has not been executed and can be sent again in a new
1675
   * session, except EXTERNAL AUTHENTICATE: its cryptogram is computed over the preceding
1676
   * GET CHALLENGE, which re-establishing secure messaging does not preserve. */
1677
1.32k
  if (plain->ins != 0x82)
1678
1.32k
    plain->flags |= SC_APDU_FLAGS_RETRY_WITH_SM_OPEN;
1679
1680
1.32k
  *sm_apdu = NULL;
1681
  //construct new SM apdu from original apdu
1682
1.32k
  apdu = calloc(1, sizeof(struct sc_apdu));
1683
1.32k
  if (!apdu) {
1684
0
    rv = SC_ERROR_OUT_OF_MEMORY;
1685
0
    goto err;
1686
0
  }
1687
1.32k
  apdu->data = calloc (1, SC_MAX_EXT_APDU_BUFFER_SIZE);
1688
1.32k
  if (!apdu->data) {
1689
0
    rv = SC_ERROR_OUT_OF_MEMORY;
1690
0
    goto err;
1691
0
  }
1692
1.32k
  apdu->resp = calloc (1, SC_MAX_EXT_APDU_BUFFER_SIZE);
1693
1.32k
  if (!apdu->resp) {
1694
0
    rv = SC_ERROR_OUT_OF_MEMORY;
1695
0
    goto err;
1696
0
  }
1697
1.32k
  apdu->datalen = SC_MAX_EXT_APDU_BUFFER_SIZE;
1698
1.32k
  apdu->resplen = SC_MAX_EXT_APDU_BUFFER_SIZE;
1699
1700
1.32k
  rv = epass2003_sm_wrap_apdu(card, plain, apdu);
1701
1.32k
  if (rv) {
1702
0
    rv = epass2003_sm_free_wrapped_apdu(card, NULL, &apdu);
1703
0
    if (rv < 0)
1704
0
      goto err;
1705
0
  }
1706
1707
1.32k
  *sm_apdu = apdu;
1708
1.32k
  apdu = NULL;
1709
1.32k
err:
1710
1.32k
  if (apdu) {
1711
0
    free((unsigned char *) apdu->data);
1712
0
    free(apdu->resp);
1713
0
    free(apdu);
1714
0
    apdu = NULL;
1715
0
  }
1716
1.32k
  LOG_FUNC_RETURN(ctx, rv);
1717
1.32k
}
1718
1719
1720
static int
1721
epass2003_transmit_apdu(struct sc_card *card, struct sc_apdu *apdu)
1722
375
{
1723
375
  int r;
1724
1725
375
  LOG_FUNC_CALLED(card->ctx);
1726
1727
375
  r = sc_transmit_apdu_t(card, apdu);
1728
375
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1729
1730
371
  return r;
1731
375
}
1732
1733
1734
static int
1735
get_data(struct sc_card *card, unsigned char type, unsigned char *data, size_t datalen)
1736
344
{
1737
344
  int r;
1738
344
  struct sc_apdu apdu;
1739
344
  unsigned char resp[SC_MAX_APDU_BUFFER_SIZE] = {0};
1740
344
  size_t resplen = SC_MAX_APDU_BUFFER_SIZE;
1741
344
  epass2003_exdata *exdata = NULL;
1742
1743
344
  if (!card->drv_data)
1744
0
    return SC_ERROR_INVALID_ARGUMENTS;
1745
1746
344
  exdata = (epass2003_exdata *)card->drv_data;
1747
1748
344
  LOG_FUNC_CALLED(card->ctx);
1749
1750
344
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xca, 0x01, type);
1751
344
  apdu.resp = resp;
1752
344
  apdu.le = 0;
1753
344
  apdu.resplen = resplen;
1754
344
  if (0x86 == type) {
1755
    /* No SM temporarily */
1756
344
    unsigned char tmp_sm = exdata->sm;
1757
344
    exdata->sm = SM_PLAIN;
1758
344
    r = sc_transmit_apdu(card, &apdu);
1759
344
    exdata->sm = tmp_sm;
1760
344
  } else {
1761
0
    r = sc_transmit_apdu_t(card, &apdu);
1762
0
  }
1763
344
  LOG_TEST_RET(card->ctx, r, "APDU get_data failed");
1764
343
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1765
343
  LOG_TEST_RET(card->ctx, r, "get_data failed");
1766
1767
323
  memcpy(data, resp, datalen);
1768
323
  return r;
1769
343
}
1770
1771
/* card driver functions */
1772
1773
static int epass2003_match_card(struct sc_card *card)
1774
7.66k
{
1775
7.66k
  int r;
1776
1777
7.66k
  LOG_FUNC_CALLED(card->ctx);
1778
7.66k
  r = _sc_match_atr(card, epass2003_atrs, &card->type);
1779
7.66k
  if (r < 0)
1780
7.32k
    return 0;
1781
1782
344
  return 1;
1783
7.66k
}
1784
1785
1786
static int
1787
epass2003_init(struct sc_card *card)
1788
344
{
1789
344
  unsigned int flags;
1790
344
  unsigned int ext_flags;
1791
344
  unsigned char data[SC_MAX_APDU_BUFFER_SIZE] = {0};
1792
344
  size_t datalen = SC_MAX_APDU_BUFFER_SIZE;
1793
344
  epass2003_exdata *exdata = NULL;
1794
344
  void *old_drv_data = card->drv_data;
1795
1796
344
  LOG_FUNC_CALLED(card->ctx);
1797
1798
344
  card->name = "epass2003";
1799
344
  card->cla = 0x00;
1800
344
  exdata = (epass2003_exdata *)calloc(1, sizeof(epass2003_exdata));
1801
344
  if (!exdata)
1802
0
    return SC_ERROR_OUT_OF_MEMORY;
1803
1804
344
  card->drv_data = exdata;
1805
1806
344
  exdata->sm = SM_SCP01;
1807
1808
  /* decide FIPS/Non-FIPS mode */
1809
344
  if (SC_SUCCESS != get_data(card, 0x86, data, datalen)) {
1810
21
    free(exdata);
1811
21
    card->drv_data = old_drv_data;
1812
21
    return SC_ERROR_INVALID_CARD;
1813
21
  }
1814
1815
323
  if (memcmp(&data[32], "\x87\x01\x01", 3) == 0 && memcmp(&data[0], "\x80\x01\x01", 3) == 0) {
1816
63
    exdata->bFipsCertification = 0x01;
1817
260
  } else {
1818
260
    exdata->bFipsCertification = 0x00;
1819
260
  }
1820
1821
323
  if (0x01 == data[2])
1822
90
    exdata->smtype = KEY_TYPE_AES;
1823
233
  else
1824
233
    exdata->smtype = KEY_TYPE_DES;
1825
1826
323
  if (0x84 == data[14]) {
1827
82
    if (0x00 == data[16]) {
1828
75
      exdata->sm = SM_PLAIN;
1829
75
    }
1830
82
  }
1831
1832
  /* mutual authentication */
1833
323
  card->max_recv_size = 0xD8;
1834
323
  card->max_send_size = 0xE8;
1835
1836
323
  card->sm_ctx.ops.open = epass2003_refresh;
1837
323
  card->sm_ctx.ops.get_sm_apdu = epass2003_sm_get_wrapped_apdu;
1838
323
  card->sm_ctx.ops.free_sm_apdu = epass2003_sm_free_wrapped_apdu;
1839
1840
  /* FIXME (VT): rather then set/unset 'g_sm', better to implement filter for APDUs to be wrapped */
1841
323
  epass2003_refresh(card);
1842
1843
323
  card->sm_ctx.sm_mode = SM_MODE_TRANSMIT;
1844
1845
323
  flags = SC_ALGORITHM_ONBOARD_KEY_GEN | SC_ALGORITHM_RSA_RAW | SC_ALGORITHM_RSA_HASH_NONE;
1846
1847
323
  _sc_card_add_rsa_alg(card, 512, flags, 0);
1848
323
  _sc_card_add_rsa_alg(card, 768, flags, 0);
1849
323
  _sc_card_add_rsa_alg(card, 1024, flags, 0);
1850
323
  _sc_card_add_rsa_alg(card, 2048, flags, 0);
1851
1852
  //set EC Alg Flags
1853
323
  flags = SC_ALGORITHM_ONBOARD_KEY_GEN|SC_ALGORITHM_ECDSA_HASH_SHA1|SC_ALGORITHM_ECDSA_HASH_SHA256|SC_ALGORITHM_ECDSA_HASH_NONE|SC_ALGORITHM_ECDSA_RAW;
1854
323
  ext_flags = 0;
1855
323
  _sc_card_add_ec_alg(card, 256, flags, ext_flags, NULL);
1856
1857
323
  card->caps = SC_CARD_CAP_RNG | SC_CARD_CAP_APDU_EXT;
1858
1859
323
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
1860
323
}
1861
1862
static int
1863
epass2003_finish(sc_card_t *card)
1864
323
{
1865
323
  epass2003_exdata *exdata = (epass2003_exdata *)card->drv_data;
1866
1867
323
  if (exdata) {
1868
323
    sc_file_free(exdata->cur_df_file);
1869
323
    free(exdata);
1870
323
  }
1871
323
  return SC_SUCCESS;
1872
323
}
1873
1874
/* COS implement SFI as lower 5 bits of FID, and not allow same SFI at the
1875
 * same DF, so use hook functions to increase/decrease FID by 0x20 */
1876
static int
1877
epass2003_hook_path(struct sc_path *path, int inc)
1878
0
{
1879
0
  u8 fid_h = 0;
1880
0
  u8 fid_l = 0;
1881
1882
0
  if (!path || path->len < 2)
1883
0
    return -1;
1884
0
  fid_h = path->value[path->len - 2];
1885
0
  fid_l = path->value[path->len - 1];
1886
1887
0
  switch (fid_h) {
1888
0
  case 0x29:
1889
0
  case 0x30:
1890
0
  case 0x31:
1891
0
  case 0x32:
1892
0
  case 0x33:
1893
0
  case 0x34:
1894
0
    if (inc)
1895
0
      fid_l = fid_l * FID_STEP;
1896
0
    else
1897
0
      fid_l = fid_l / FID_STEP;
1898
0
    path->value[path->len - 1] = fid_l;
1899
0
    return 1;
1900
0
  default:
1901
0
    break;
1902
0
  }
1903
0
  return 0;
1904
0
}
1905
1906
1907
static int
1908
epass2003_hook_file(struct sc_file *file, int inc)
1909
0
{
1910
0
  int fidl = file->id & 0xff;
1911
0
  int fidh = file->id & 0xff00;
1912
0
  int rv = 0;
1913
1914
0
  rv = epass2003_hook_path(&file->path, inc);
1915
1916
0
  if (rv > 0) {
1917
0
    if (inc)
1918
0
      file->id = fidh + fidl * FID_STEP;
1919
0
    else
1920
0
      file->id = fidh + fidl / FID_STEP;
1921
0
  }
1922
0
  if (rv < 0)
1923
0
    return rv;
1924
0
  return SC_SUCCESS;
1925
0
}
1926
1927
1928
static int
1929
epass2003_select_fid_(struct sc_card *card, sc_path_t * in_path, sc_file_t ** file_out)
1930
0
{
1931
0
  struct sc_apdu apdu;
1932
0
  u8 buf[SC_MAX_APDU_BUFFER_SIZE] = {0};
1933
0
  u8 pathbuf[SC_MAX_PATH_SIZE], *path = pathbuf;
1934
0
  int r;
1935
0
  size_t pathlen;
1936
0
  sc_file_t *file = NULL;
1937
1938
0
  if (!in_path || in_path->len > sizeof(pathbuf))
1939
0
    return SC_ERROR_INVALID_ARGUMENTS;
1940
1941
0
  r = epass2003_hook_path(in_path, 1);
1942
0
  LOG_TEST_RET(card->ctx, r, "Can not hook path");
1943
1944
0
  memcpy(path, in_path->value, in_path->len);
1945
0
  pathlen = in_path->len;
1946
1947
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0x00, 0x00);
1948
1949
0
  switch (in_path->type) {
1950
0
  case SC_PATH_TYPE_FILE_ID:
1951
0
    apdu.p1 = 0;
1952
0
    if (pathlen != 2)
1953
0
      return SC_ERROR_INVALID_ARGUMENTS;
1954
0
    break;
1955
0
  default:
1956
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
1957
0
  }
1958
0
  apdu.p2 = 0;    /* first record, return FCI */
1959
0
  apdu.lc = pathlen;
1960
0
  apdu.data = path;
1961
0
  apdu.datalen = pathlen;
1962
1963
0
  if (file_out != NULL) {
1964
0
    apdu.resp = buf;
1965
0
    apdu.resplen = sizeof(buf);
1966
0
    apdu.le = 0;
1967
0
  } else {
1968
0
    apdu.cse = (apdu.lc == 0) ? SC_APDU_CASE_1 : SC_APDU_CASE_3_SHORT;
1969
0
  }
1970
1971
0
  if (path[0] == 0x29) { /* TODO:0x29 accords with FID prefix in profile  */
1972
    /* Not allowed to select private key file, so fake fci. */
1973
    /* 62 16 82 02 11 00 83 02 29 00 85 02 08 00 86 08 FF 90 90 90 FF FF FF FF */
1974
0
    apdu.resplen = 0x18;
1975
0
    memcpy(apdu.resp,
1976
0
           "\x6f\x16\x82\x02\x11\x00\x83\x02\x29\x00\x85\x02\x08\x00\x86\x08\xff\x90\x90\x90\xff\xff\xff\xff",
1977
0
           apdu.resplen);
1978
0
    apdu.resp[9] = path[1];
1979
0
    apdu.sw1 = 0x90;
1980
0
    apdu.sw2 = 0x00;
1981
0
  }
1982
0
  else {
1983
0
    r = sc_transmit_apdu_t(card, &apdu);
1984
0
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1985
0
  }
1986
1987
0
  if (file_out == NULL) {
1988
0
    if (apdu.sw1 == 0x61)
1989
0
      LOG_FUNC_RETURN(card->ctx, 0);
1990
0
    LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2));
1991
0
  }
1992
1993
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1994
0
  if (r)
1995
0
    LOG_FUNC_RETURN(card->ctx, r);
1996
0
  if (apdu.resplen < 2)
1997
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED);
1998
1999
0
  switch (apdu.resp[0]) {
2000
0
  case 0x6F:
2001
0
    file = sc_file_new();
2002
0
    if (file == NULL)
2003
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
2004
0
    file->path = *in_path;
2005
0
    if (card->ops->process_fci == NULL) {
2006
0
      sc_file_free(file);
2007
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
2008
0
    }
2009
2010
0
    if ((size_t) apdu.resp[1] + 2 <= apdu.resplen)
2011
0
      card->ops->process_fci(card, file, apdu.resp + 2, apdu.resp[1]);
2012
0
    epass2003_hook_file(file, 0);
2013
0
    *file_out = file;
2014
0
    break;
2015
0
  case 0x00:    /* proprietary coding */
2016
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED);
2017
0
    break;
2018
0
  default:
2019
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED);
2020
0
  }
2021
0
  return 0;
2022
0
}
2023
2024
static void
2025
epass2003_forget_cur_df(struct sc_card *card)
2026
0
{
2027
0
  epass2003_exdata *exdata = (epass2003_exdata *)card->drv_data;
2028
2029
0
  if (exdata) {
2030
0
    exdata->cur_df_len = 0;
2031
0
    sc_file_free(exdata->cur_df_file);
2032
0
    exdata->cur_df_file = NULL;
2033
0
  }
2034
0
}
2035
2036
static void
2037
epass2003_set_cur_df(struct sc_card *card, const u8 *path, size_t len,
2038
    const sc_file_t *file)
2039
0
{
2040
0
  epass2003_exdata *exdata = (epass2003_exdata *)card->drv_data;
2041
0
  sc_file_t *df = NULL;
2042
2043
0
  if (!exdata)
2044
0
    return;
2045
0
  if (len > sizeof(exdata->cur_df)) {
2046
0
    epass2003_forget_cur_df(card);
2047
0
    return;
2048
0
  }
2049
0
  sc_file_dup(&df, file);
2050
0
  if (!df) {
2051
0
    epass2003_forget_cur_df(card);
2052
0
    return;
2053
0
  }
2054
0
  sc_file_free(exdata->cur_df_file);
2055
0
  exdata->cur_df_file = df;
2056
0
  memcpy(exdata->cur_df, path, len);
2057
0
  exdata->cur_df_len = len;
2058
0
}
2059
2060
static int
2061
epass2003_select_fid(struct sc_card *card, unsigned int id_hi, unsigned int id_lo,
2062
    sc_file_t ** file_out)
2063
0
{
2064
0
  int r;
2065
0
  sc_file_t *file = NULL;
2066
0
  sc_path_t path;
2067
2068
0
  memset(&path, 0, sizeof(path));
2069
0
  path.type = SC_PATH_TYPE_FILE_ID;
2070
0
  path.value[0] = id_hi;
2071
0
  path.value[1] = id_lo;
2072
0
  path.len = 2;
2073
2074
0
  r = epass2003_select_fid_(card, &path, &file);
2075
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
2076
2077
0
  if (file_out) {
2078
0
    *file_out = file;
2079
0
  } else {
2080
0
    sc_file_free(file);
2081
0
  }
2082
2083
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
2084
0
}
2085
2086
2087
static int
2088
epass2003_select_aid(struct sc_card *card, const sc_path_t * in_path, sc_file_t ** file_out)
2089
0
{
2090
0
  int r = 0;
2091
2092
0
  r = iso_ops->select_file(card, in_path, file_out);
2093
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
2094
2095
0
  if (file_out && *file_out) {
2096
0
    sc_file_t *file = *file_out;
2097
0
    if (in_path->len > sizeof(file->name))
2098
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
2099
2100
0
    file->type = SC_FILE_TYPE_DF;
2101
0
    file->ef_structure = SC_FILE_EF_UNKNOWN;
2102
0
    file->path.len = 0;
2103
0
    file->size = 0;
2104
    /* AID */
2105
0
    memcpy(file->name, in_path->value, in_path->len);
2106
0
    file->namelen = in_path->len;
2107
0
    file->id = 0x0000;
2108
0
  }
2109
2110
0
  LOG_FUNC_RETURN(card->ctx, r);
2111
0
}
2112
2113
2114
static int
2115
epass2003_select_path(struct sc_card *card, const u8 pathbuf[16], const size_t len,
2116
    sc_file_t ** file_out)
2117
0
{
2118
0
  u8 n_pathbuf[SC_MAX_PATH_SIZE];
2119
0
  const u8 *path = pathbuf;
2120
0
  size_t pathlen = len;
2121
0
  size_t i, start = 0;
2122
0
  int r;
2123
0
  sc_file_t *file = NULL;
2124
0
  epass2003_exdata *exdata;
2125
2126
0
  if (pathlen % 2 != 0 || pathlen > 6 || pathlen <= 0)
2127
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
2128
2129
  /* if pathlen == 6 then the first FID must be MF (== 3F00) */
2130
0
  if (pathlen == 6 && (path[0] != 0x3f || path[1] != 0x00))
2131
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
2132
2133
  /* unify path (the first FID should be MF) */
2134
0
  if (path[0] != 0x3f || path[1] != 0x00) {
2135
0
    n_pathbuf[0] = 0x3f;
2136
0
    n_pathbuf[1] = 0x00;
2137
0
    memcpy(n_pathbuf + 2, path, pathlen);
2138
0
    path = n_pathbuf;
2139
0
    pathlen += 2;
2140
0
  }
2141
2142
0
  exdata = (epass2003_exdata *)card->drv_data;
2143
0
  if (exdata && exdata->cur_df_len > 0 && exdata->cur_df_len <= pathlen &&
2144
0
      memcmp(path, exdata->cur_df, exdata->cur_df_len) == 0) {
2145
0
    if (exdata->cur_df_len == pathlen) {
2146
      /* already in the requested DF */
2147
0
      if (file_out) {
2148
0
        sc_file_dup(file_out, exdata->cur_df_file);
2149
0
        if (!*file_out)
2150
0
          LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
2151
0
      }
2152
0
      LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
2153
0
    }
2154
    /* inside the current DF: select only what follows it */
2155
0
    start = exdata->cur_df_len;
2156
0
  } else {
2157
0
    epass2003_forget_cur_df(card);
2158
0
  }
2159
2160
0
  for (i = start; i < pathlen - 2; i += 2) {
2161
0
    r = epass2003_select_fid(card, path[i], path[i + 1], &file);
2162
0
    LOG_TEST_RET(card->ctx, r, "SELECT FILE (DF-ID) failed");
2163
    /* a FID that is followed by further FIDs is a DF */
2164
0
    epass2003_set_cur_df(card, path, i + 2, file);
2165
0
    sc_file_free(file);
2166
0
    file = NULL;
2167
0
  }
2168
2169
0
  r = epass2003_select_fid(card, path[pathlen - 2], path[pathlen - 1], &file);
2170
  /* selecting an EF or a failed select leaves the current DF unchanged */
2171
0
  if (r == SC_SUCCESS && file->type == SC_FILE_TYPE_DF)
2172
0
    epass2003_set_cur_df(card, path, pathlen, file);
2173
2174
0
  if (file_out)
2175
0
    *file_out = file;
2176
0
  else
2177
0
    sc_file_free(file);
2178
0
  LOG_FUNC_RETURN(card->ctx, r);
2179
0
}
2180
2181
/* A file identifier (P1 = 00) can select the MF, the current DF, its parent or
2182
 * a file below either of them (ISO 7816-4). Track the current DF where the
2183
 * result tells which DF it is, otherwise forget it. This COS looks for an EF
2184
 * only in the current DF, so selecting an EF keeps it. */
2185
static int
2186
epass2003_select_file_id(struct sc_card *card, u8 id_hi, u8 id_lo,
2187
    sc_file_t **file_out)
2188
0
{
2189
0
  epass2003_exdata *exdata = (epass2003_exdata *)card->drv_data;
2190
0
  sc_file_t *file = NULL;
2191
0
  u8 df[SC_MAX_PATH_SIZE];
2192
0
  size_t len;
2193
0
  int r;
2194
2195
0
  r = epass2003_select_fid(card, id_hi, id_lo, &file);
2196
  /* a failed select leaves the current DF unchanged */
2197
0
  LOG_TEST_RET(card->ctx, r, "SELECT FILE failed");
2198
2199
0
  if (exdata && file->type == SC_FILE_TYPE_DF) {
2200
0
    len = exdata->cur_df_len;
2201
0
    memcpy(df, exdata->cur_df, len);
2202
0
    if (id_hi == 0x3f && id_lo == 0x00) {
2203
0
      df[0] = 0x3f;
2204
0
      df[1] = 0x00;
2205
0
      epass2003_set_cur_df(card, df, 2, file);
2206
0
    } else if (len == 2) {
2207
      /* the MF has no parent, so this DF is below it */
2208
0
      df[len] = id_hi;
2209
0
      df[len + 1] = id_lo;
2210
0
      epass2003_set_cur_df(card, df, len + 2, file);
2211
0
    } else if (len >= 4 && df[len - 2] == id_hi && df[len - 1] == id_lo) {
2212
      /* the current DF itself */
2213
0
      epass2003_set_cur_df(card, df, len, file);
2214
0
    } else if (len >= 6 && df[len - 4] == id_hi && df[len - 3] == id_lo) {
2215
      /* the parent DF */
2216
0
      epass2003_set_cur_df(card, df, len - 2, file);
2217
0
    } else {
2218
      /* below the current DF or below its parent */
2219
0
      epass2003_forget_cur_df(card);
2220
0
    }
2221
0
  }
2222
2223
0
  if (file_out)
2224
0
    *file_out = file;
2225
0
  else
2226
0
    sc_file_free(file);
2227
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
2228
0
}
2229
2230
static int
2231
epass2003_select_file(struct sc_card *card, const sc_path_t * in_path,
2232
    sc_file_t ** file_out)
2233
0
{
2234
0
  int r;
2235
2236
0
  LOG_FUNC_CALLED(card->ctx);
2237
2238
0
  r = epass2003_sm_restart(card);
2239
0
  LOG_TEST_RET(card->ctx, r, "Can not re-establish SM");
2240
2241
0
  switch (in_path->type) {
2242
0
  case SC_PATH_TYPE_FILE_ID:
2243
0
    if (in_path->len != 2)
2244
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
2245
0
    return epass2003_select_file_id(card, in_path->value[0], in_path->value[1], file_out);
2246
0
  case SC_PATH_TYPE_DF_NAME:
2247
0
    r = epass2003_select_aid(card, in_path, file_out);
2248
    /* the path of a DF selected by its name is not known */
2249
0
    if (r == SC_SUCCESS)
2250
0
      epass2003_forget_cur_df(card);
2251
0
    return r;
2252
0
  case SC_PATH_TYPE_PATH:
2253
0
    return epass2003_select_path(card, in_path->value, in_path->len, file_out);
2254
0
  default:
2255
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
2256
0
  }
2257
0
}
2258
2259
static int
2260
epass2003_card_reader_lock_obtained(struct sc_card *card, int was_reset)
2261
1.23k
{
2262
  /* after a reset the current DF is either not set or set to an unknown
2263
   * value by a different application */
2264
1.23k
  if (was_reset > 0)
2265
0
    epass2003_forget_cur_df(card);
2266
1.23k
  return SC_SUCCESS;
2267
1.23k
}
2268
2269
static int
2270
epass2003_set_security_env(struct sc_card *card, const sc_security_env_t * env, int se_num)
2271
0
{
2272
0
  struct sc_apdu apdu;
2273
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE] = {0};
2274
0
  u8 *p;
2275
0
  unsigned short fid = 0;
2276
0
  int r, locked = 0;
2277
0
  epass2003_exdata *exdata = NULL;
2278
2279
0
  if (!card->drv_data)
2280
0
    return SC_ERROR_INVALID_ARGUMENTS;
2281
2282
0
  exdata = (epass2003_exdata *)card->drv_data;
2283
2284
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x41, 0);
2285
2286
0
  p = sbuf;
2287
0
  *p++ = 0x80;    /* algorithm reference */
2288
0
  *p++ = 0x01;
2289
0
  *p++ = 0x84;
2290
2291
0
  *p++ = 0x81;
2292
0
  *p++ = 0x02;
2293
2294
0
  fid = 0x2900;
2295
0
  fid += (unsigned short)(0x20 * (env->key_ref[0] & 0xff));
2296
0
  *p++ = fid >> 8;
2297
0
  *p++ = fid & 0xff;
2298
0
  r = (int)(p - sbuf);
2299
0
  apdu.lc = r;
2300
0
  apdu.datalen = r;
2301
0
  apdu.data = sbuf;
2302
2303
0
  if (env->algorithm == SC_ALGORITHM_EC) {
2304
0
    apdu.p2 = 0xB6;
2305
0
    exdata->currAlg = SC_ALGORITHM_EC;
2306
0
    if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA1) {
2307
0
      sbuf[2] = 0x91;
2308
0
      exdata->ecAlgFlags = SC_ALGORITHM_ECDSA_HASH_SHA1;
2309
0
    } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA256) {
2310
0
      sbuf[2] = 0x92;
2311
0
      exdata->ecAlgFlags = SC_ALGORITHM_ECDSA_HASH_SHA256;
2312
0
    } else {
2313
0
      sbuf[2] = 0x92;
2314
0
      exdata->ecAlgFlags = SC_ALGORITHM_ECDSA_HASH_NONE;
2315
0
    }
2316
0
  } else if (env->algorithm == SC_ALGORITHM_RSA) {
2317
0
    exdata->currAlg = SC_ALGORITHM_RSA;
2318
0
    apdu.p2 = 0xB8;
2319
0
    sc_log(card->ctx, "setenv RSA Algorithm alg_flags = %0lx\n", env->algorithm_flags);
2320
0
  } else {
2321
0
    sc_log(card->ctx, "%0lx Alg Not Supported!", env->algorithm);
2322
0
  }
2323
2324
0
  if (se_num > 0) {
2325
0
    r = sc_lock(card);
2326
0
    LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
2327
0
    locked = 1;
2328
0
  }
2329
0
  if (apdu.datalen != 0) {
2330
0
    r = sc_transmit_apdu_t(card, &apdu);
2331
0
    if (r) {
2332
0
      sc_log(card->ctx, "%s: APDU transmit failed", sc_strerror(r));
2333
0
      goto err;
2334
0
    }
2335
2336
0
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
2337
0
    if (r) {
2338
0
      sc_log(card->ctx, "%s: Card returned error", sc_strerror(r));
2339
0
      goto err;
2340
0
    }
2341
0
  }
2342
0
  if (se_num <= 0)
2343
0
    return 0;
2344
2345
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0xF2, se_num);
2346
0
  r = sc_transmit_apdu_t(card, &apdu);
2347
0
  sc_unlock(card);
2348
2349
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
2350
0
  return sc_check_sw(card, apdu.sw1, apdu.sw2);
2351
2352
0
err:
2353
0
  if (locked)
2354
0
    sc_unlock(card);
2355
0
  return r;
2356
0
}
2357
2358
2359
static int
2360
epass2003_restore_security_env(struct sc_card *card, int se_num)
2361
0
{
2362
0
  LOG_FUNC_CALLED(card->ctx);
2363
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
2364
0
}
2365
2366
2367
static int epass2003_decipher(struct sc_card *card, const u8 * data, size_t datalen,
2368
    u8 * out, size_t outlen)
2369
0
{
2370
0
  int r;
2371
0
  struct sc_apdu apdu;
2372
0
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE] = {0};
2373
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE] = {0};
2374
0
  epass2003_exdata *exdata = NULL;
2375
2376
0
  LOG_FUNC_CALLED(card->ctx);
2377
2378
0
  if (!card->drv_data)
2379
0
    return SC_ERROR_INVALID_ARGUMENTS;
2380
2381
0
  exdata = (epass2003_exdata *)card->drv_data;
2382
2383
0
  if (exdata->currAlg == SC_ALGORITHM_EC) {
2384
0
    if (exdata->ecAlgFlags & SC_ALGORITHM_ECDSA_HASH_SHA1) {
2385
0
      r = hash_data(card, data, datalen, sbuf, SC_ALGORITHM_ECDSA_HASH_SHA1);
2386
0
      LOG_TEST_RET(card->ctx, r, "hash_data failed");
2387
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_3, 0x2A, 0x9E, 0x9A);
2388
0
      apdu.data = sbuf;
2389
0
      apdu.lc = 0x14;
2390
0
      apdu.datalen = 0x14;
2391
0
    } else if (exdata->ecAlgFlags & SC_ALGORITHM_ECDSA_HASH_SHA256) {
2392
0
      r = hash_data(card, data, datalen, sbuf, SC_ALGORITHM_ECDSA_HASH_SHA256);
2393
0
      LOG_TEST_RET(card->ctx, r, "hash_data failed");
2394
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_3, 0x2A, 0x9E, 0x9A);
2395
0
      apdu.data = sbuf;
2396
0
      apdu.lc = 0x20;
2397
0
      apdu.datalen = 0x20;
2398
0
    } else if (exdata->ecAlgFlags & SC_ALGORITHM_ECDSA_HASH_NONE) {
2399
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_3,0x2A, 0x9E, 0x9A);
2400
0
      apdu.data = data;
2401
0
      apdu.lc = datalen;
2402
0
      apdu.datalen = datalen;
2403
0
    } else {
2404
0
      return SC_ERROR_NOT_SUPPORTED;
2405
0
    }
2406
0
    apdu.resp = rbuf;
2407
0
    apdu.resplen = sizeof(rbuf);
2408
0
    apdu.le = 0;
2409
2410
0
    r = sc_transmit_apdu_t(card, &apdu);
2411
0
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
2412
0
    if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
2413
0
      size_t len = apdu.resplen > outlen ? outlen : apdu.resplen;
2414
0
      memcpy(out, apdu.resp, len);
2415
0
      LOG_FUNC_RETURN(card->ctx, (int)len);
2416
0
    }
2417
0
    LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2));
2418
0
  } else if (exdata->currAlg == SC_ALGORITHM_RSA) {
2419
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_4_EXT, 0x2A, 0x80, 0x86);
2420
0
    apdu.resp = rbuf;
2421
0
    apdu.resplen = sizeof(rbuf);
2422
0
    apdu.le = 0;
2423
2424
0
    if (datalen > sizeof(sbuf)) {
2425
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
2426
0
    }
2427
2428
0
    memcpy(sbuf, data, datalen);
2429
0
    apdu.data = sbuf;
2430
0
    apdu.lc = datalen;
2431
0
    apdu.datalen = datalen;
2432
0
  } else {
2433
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_4_EXT, 0x2A, 0x80, 0x86);
2434
0
    apdu.resp = rbuf;
2435
0
    apdu.resplen = sizeof(rbuf);
2436
0
    apdu.le = 256;
2437
2438
0
    if (datalen > sizeof(sbuf)) {
2439
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
2440
0
    }
2441
2442
0
    memcpy(sbuf, data, datalen);
2443
0
    apdu.data = sbuf;
2444
0
    apdu.lc = datalen;
2445
0
    apdu.datalen = datalen;
2446
0
  }
2447
2448
0
  r = sc_transmit_apdu_t(card, &apdu);
2449
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
2450
2451
0
  if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
2452
0
    size_t len = apdu.resplen > outlen ? outlen : apdu.resplen;
2453
0
    memcpy(out, apdu.resp, len);
2454
0
    LOG_FUNC_RETURN(card->ctx, (int)len);
2455
0
  }
2456
2457
0
  LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2));
2458
0
}
2459
2460
2461
static int
2462
acl_to_ac_byte(struct sc_card *card, const struct sc_acl_entry *e)
2463
0
{
2464
0
  if (e == NULL)
2465
0
    return SC_ERROR_OBJECT_NOT_FOUND;
2466
2467
0
  switch (e->method) {
2468
0
  case SC_AC_NONE:
2469
0
    LOG_FUNC_RETURN(card->ctx, EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_EVERYONE);
2470
0
  case SC_AC_NEVER:
2471
0
    LOG_FUNC_RETURN(card->ctx, EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_NOONE);
2472
0
  default:
2473
0
    LOG_FUNC_RETURN(card->ctx, EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_USER);
2474
0
  }
2475
2476
0
  LOG_FUNC_RETURN(card->ctx, SC_ERROR_INCORRECT_PARAMETERS);
2477
0
}
2478
2479
/* Use epass2003 sec_attr to add acl entries */
2480
int
2481
sec_attr_to_entry(struct sc_card *card, sc_file_t *file, int index)
2482
0
{
2483
0
  int i;
2484
0
  int found = 0;
2485
2486
0
  unsigned int method;
2487
0
  unsigned long  keyref;
2488
2489
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
2490
2491
0
  switch (file->sec_attr[index]) {
2492
0
  case (EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_EVERYONE):
2493
0
    method = SC_AC_NONE;
2494
0
    keyref = SC_AC_KEY_REF_NONE;
2495
0
    break;
2496
0
  case (EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_USER):
2497
0
    method = SC_AC_CHV;
2498
0
    keyref = 1;
2499
0
    break;
2500
0
  default:
2501
0
    sc_log(card->ctx, "Unknown value 0x%2.2x in file->sec_attr[%d]", file->sec_attr[index], index);
2502
0
    method = SC_AC_NEVER;
2503
0
    keyref = SC_AC_KEY_REF_NONE;
2504
0
    break;
2505
0
  }
2506
2507
0
  for (i = 0; i < (int)(sizeof(sec_attr_to_acl_entry) / sizeof(sec_attr_to_acl_entries_t)); i++) {
2508
0
    const sec_attr_to_acl_entries_t *e = &sec_attr_to_acl_entry[i];
2509
2510
0
    if (index == e->index && file->type == e->file_type
2511
0
        && file->ef_structure == e->file_ef_structure) {
2512
        /* may add multiple entries */
2513
0
      sc_file_add_acl_entry(file, e->op, method, keyref);
2514
0
      found++;
2515
0
    }
2516
0
  }
2517
0
  if (found != 1) {
2518
0
    sc_log(card->ctx,"found %d entries ", found);
2519
0
  }
2520
2521
0
  return 0;
2522
0
}
2523
2524
static int
2525
epass2003_process_fci(struct sc_card *card, sc_file_t * file, const u8 * buf, size_t buflen)
2526
0
{
2527
0
  sc_context_t *ctx = card->ctx;
2528
0
  size_t taglen, len = buflen;
2529
0
  const u8 *tag = NULL, *p = buf;
2530
2531
0
  sc_log(ctx, "processing FCI bytes");
2532
0
  tag = sc_asn1_find_tag(ctx, p, len, 0x83, &taglen);
2533
0
  if (tag != NULL && taglen == 2) {
2534
0
    file->id = (tag[0] << 8) | tag[1];
2535
0
    sc_log(ctx, "  file identifier: 0x%02X%02X", tag[0], tag[1]);
2536
0
  }
2537
2538
0
  tag = sc_asn1_find_tag(ctx, p, len, 0x80, &taglen);
2539
0
  if (tag != NULL && taglen > 0 && taglen < 3) {
2540
0
    file->size = tag[0];
2541
0
    if (taglen == 2)
2542
0
      file->size = (file->size << 8) + tag[1];
2543
0
    sc_log(ctx, "  bytes in file: %zu", file->size);
2544
0
  }
2545
2546
0
  if (tag == NULL) {
2547
0
    tag = sc_asn1_find_tag(ctx, p, len, 0x81, &taglen);
2548
0
    if (tag != NULL && taglen >= 2) {
2549
0
      int bytes = (tag[0] << 8) + tag[1];
2550
2551
0
      sc_log(ctx, "  bytes in file: %d", bytes);
2552
0
      file->size = bytes;
2553
0
    }
2554
0
  }
2555
2556
0
  tag = sc_asn1_find_tag(ctx, p, len, 0x82, &taglen);
2557
0
  if (tag != NULL) {
2558
0
    if (taglen > 0) {
2559
0
      unsigned char byte = tag[0];
2560
0
      const char *type;
2561
2562
0
      if (byte == 0x38) {
2563
0
        type = "DF";
2564
0
        file->type = SC_FILE_TYPE_DF;
2565
0
      } else if (0x01 <= byte && byte <= 0x07) {
2566
0
        type = "working EF";
2567
0
        file->type = SC_FILE_TYPE_WORKING_EF;
2568
0
        switch (byte) {
2569
0
        case 0x01:
2570
0
          file->ef_structure = SC_FILE_EF_TRANSPARENT;
2571
0
          break;
2572
0
        case 0x02:
2573
0
          file->ef_structure = SC_FILE_EF_LINEAR_FIXED;
2574
0
          break;
2575
0
        case 0x04:
2576
0
          file->ef_structure = SC_FILE_EF_LINEAR_FIXED;
2577
0
          break;
2578
0
        default:
2579
0
          break;
2580
0
        }
2581
2582
0
      } else if (0x10 == byte) {
2583
0
        type = "BSO";
2584
0
        file->type = SC_FILE_TYPE_BSO;
2585
0
      } else if (0x11 <= byte) {
2586
0
        type = "internal EF";
2587
0
        file->type = SC_FILE_TYPE_INTERNAL_EF;
2588
0
        switch (byte) {
2589
0
        case 0x11:
2590
0
          break;
2591
0
        case 0x12:
2592
0
          break;
2593
0
        default:
2594
0
          break;
2595
0
        }
2596
0
      } else {
2597
0
        type = "unknown";
2598
0
        file->type = SC_FILE_TYPE_INTERNAL_EF;
2599
0
      }
2600
0
      sc_log(ctx, "type %s, EF structure %d", type, byte);
2601
0
    }
2602
0
  }
2603
2604
0
  tag = sc_asn1_find_tag(ctx, p, len, 0x84, &taglen);
2605
0
  if (tag != NULL && taglen > 0 && taglen <= 16) {
2606
0
    memcpy(file->name, tag, taglen);
2607
0
    file->namelen = taglen;
2608
2609
0
    sc_log_hex(ctx, "File name", file->name, file->namelen);
2610
0
    if (!file->type)
2611
0
      file->type = SC_FILE_TYPE_DF;
2612
0
  }
2613
2614
0
  tag = sc_asn1_find_tag(ctx, p, len, 0x85, &taglen);
2615
0
  if (tag != NULL && taglen)
2616
0
    sc_file_set_prop_attr(file, tag, taglen);
2617
0
  else
2618
0
    file->prop_attr_len = 0;
2619
2620
0
  tag = sc_asn1_find_tag(ctx, p, len, 0xA5, &taglen);
2621
0
  if (tag != NULL && taglen)
2622
0
    sc_file_set_prop_attr(file, tag, taglen);
2623
2624
0
  tag = sc_asn1_find_tag(ctx, p, len, 0x86, &taglen);
2625
0
  if (tag != NULL && taglen) {
2626
0
    unsigned int i;
2627
0
    sc_file_set_sec_attr(file, tag, taglen);
2628
0
    for (i = 0; i< taglen; i++)
2629
0
      if (tag[i] != 0xff) /* skip unused entries */
2630
0
        sec_attr_to_entry(card, file, i);
2631
0
  }
2632
2633
0
  tag = sc_asn1_find_tag(ctx, p, len, 0x8A, &taglen);
2634
0
  if (tag != NULL && taglen == 1) {
2635
0
    if (tag[0] == 0x01)
2636
0
      file->status = SC_FILE_STATUS_CREATION;
2637
0
    else if (tag[0] == 0x07 || tag[0] == 0x05)
2638
0
      file->status = SC_FILE_STATUS_ACTIVATED;
2639
0
    else if (tag[0] == 0x06 || tag[0] == 0x04)
2640
0
      file->status = SC_FILE_STATUS_INVALIDATED;
2641
0
  }
2642
0
  file->magic = SC_FILE_MAGIC;
2643
2644
0
  return 0;
2645
0
}
2646
2647
2648
static int
2649
epass2003_construct_fci(struct sc_card *card, const sc_file_t * file,
2650
    u8 * out, size_t * outlen)
2651
0
{
2652
0
  u8 *p = out;
2653
0
  u8 buf[64];
2654
0
  unsigned char ops[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2655
0
  int rv;
2656
0
  unsigned ii;
2657
2658
0
  if (*outlen < 2)
2659
0
    return SC_ERROR_BUFFER_TOO_SMALL;
2660
2661
0
  *p++ = 0x62;
2662
0
  p++;
2663
0
  if (file->type == SC_FILE_TYPE_WORKING_EF) {
2664
0
    if (file->ef_structure == SC_FILE_EF_TRANSPARENT) {
2665
0
      buf[0] = (file->size >> 8) & 0xFF;
2666
0
      buf[1] = file->size & 0xFF;
2667
0
      sc_asn1_put_tag(0x80, buf, 2, p, *outlen - (p - out), &p);
2668
0
    }
2669
0
  }
2670
0
  if (file->type == SC_FILE_TYPE_DF) {
2671
0
    buf[0] = 0x38;
2672
0
    buf[1] = 0x00;
2673
0
    sc_asn1_put_tag(0x82, buf, 2, p, *outlen - (p - out), &p);
2674
0
  }
2675
0
  else if (file->type == SC_FILE_TYPE_WORKING_EF) {
2676
0
    buf[0] = file->ef_structure & 7;
2677
0
    if (file->ef_structure == SC_FILE_EF_TRANSPARENT) {
2678
0
      buf[1] = 0x00;
2679
0
      sc_asn1_put_tag(0x82, buf, 2, p, *outlen - (p - out), &p);
2680
0
    } else if (file->ef_structure == SC_FILE_EF_LINEAR_FIXED ||
2681
0
        file->ef_structure == SC_FILE_EF_LINEAR_VARIABLE) {
2682
0
      buf[1] = 0x00;
2683
0
      buf[2] = 0x00;
2684
0
      buf[3] = 0x40;  /* record length */
2685
0
      buf[4] = 0x00;  /* record count */
2686
0
      sc_asn1_put_tag(0x82, buf, 5, p, *outlen - (p - out), &p);
2687
0
    } else {
2688
0
      return SC_ERROR_NOT_SUPPORTED;
2689
0
    }
2690
2691
0
  }
2692
0
  else if (file->type == SC_FILE_TYPE_INTERNAL_EF) {
2693
0
    if (file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_RSA_CRT ||
2694
0
        file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_EC_CRT) {
2695
0
      buf[0] = 0x11;
2696
0
      buf[1] = 0x00;
2697
0
    } else if (file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_RSA_PUBLIC ||
2698
0
        file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_EC_PUBLIC) {
2699
0
      buf[0] = 0x12;
2700
0
      buf[1] = 0x00;
2701
0
    } else {
2702
0
      return SC_ERROR_NOT_SUPPORTED;
2703
0
    }
2704
0
    sc_asn1_put_tag(0x82, buf, 2, p, *outlen - (p - out), &p);
2705
0
  } else if (file->type == SC_FILE_TYPE_BSO) {
2706
0
    buf[0] = 0x10;
2707
0
    buf[1] = 0x00;
2708
0
    sc_asn1_put_tag(0x82, buf, 2, p, *outlen - (p - out), &p);
2709
0
  }
2710
2711
0
  buf[0] = (file->id >> 8) & 0xFF;
2712
0
  buf[1] = file->id & 0xFF;
2713
0
  sc_asn1_put_tag(0x83, buf, 2, p, *outlen - (p - out), &p);
2714
0
  if (file->type == SC_FILE_TYPE_DF) {
2715
0
    if (file->namelen != 0) {
2716
0
      sc_asn1_put_tag(0x84, file->name, file->namelen, p, *outlen - (p - out), &p);
2717
0
    } else {
2718
0
      return SC_ERROR_INVALID_ARGUMENTS;
2719
0
    }
2720
0
  }
2721
0
  if (file->type == SC_FILE_TYPE_DF) {
2722
0
    unsigned char data[2] = {0x00, 0x7F};
2723
    /* 127 files at most */
2724
0
    sc_asn1_put_tag(0x85, data, sizeof(data), p, *outlen - (p - out), &p);
2725
0
  } else if (file->type == SC_FILE_TYPE_BSO) {
2726
0
    buf[0] = file->size & 0xff;
2727
0
    sc_asn1_put_tag(0x85, buf, 1, p, *outlen - (p - out), &p);
2728
0
  } else if (file->type == SC_FILE_TYPE_INTERNAL_EF) {
2729
0
    if (file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_RSA_CRT ||
2730
0
        file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_RSA_PUBLIC ||
2731
0
        file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_EC_CRT ||
2732
0
        file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_EC_PUBLIC) {
2733
0
      buf[0] = (file->size >> 8) & 0xFF;
2734
0
      buf[1] = file->size & 0xFF;
2735
0
      sc_asn1_put_tag(0x85, buf, 2, p, *outlen - (p - out), &p);
2736
0
    }
2737
0
  }
2738
0
  if (file->sec_attr_len) {
2739
0
    if (file->sec_attr_len > sizeof(buf)) {
2740
0
      return SC_ERROR_INVALID_DATA;
2741
0
    }
2742
0
    memcpy(buf, file->sec_attr, file->sec_attr_len);
2743
0
    sc_asn1_put_tag(0x86, buf, file->sec_attr_len, p, *outlen - (p - out), &p);
2744
2745
0
  } else {
2746
0
    sc_log(card->ctx, "SC_FILE_ACL");
2747
0
    if (file->type == SC_FILE_TYPE_DF) {
2748
0
      ops[0] = SC_AC_OP_LIST_FILES;
2749
0
      ops[1] = SC_AC_OP_CREATE;
2750
0
      ops[3] = SC_AC_OP_DELETE;
2751
0
    } else if (file->type == SC_FILE_TYPE_WORKING_EF) {
2752
0
      if (file->ef_structure == SC_FILE_EF_TRANSPARENT) {
2753
0
        ops[0] = SC_AC_OP_READ;
2754
0
        ops[1] = SC_AC_OP_UPDATE;
2755
0
        ops[3] = SC_AC_OP_DELETE;
2756
0
      } else if (file->ef_structure == SC_FILE_EF_LINEAR_FIXED ||
2757
0
          file->ef_structure == SC_FILE_EF_LINEAR_VARIABLE) {
2758
0
        ops[0] = SC_AC_OP_READ;
2759
0
        ops[1] = SC_AC_OP_UPDATE;
2760
0
        ops[2] = SC_AC_OP_WRITE;
2761
0
        ops[3] = SC_AC_OP_DELETE;
2762
0
      } else {
2763
0
        return SC_ERROR_NOT_SUPPORTED;
2764
0
      }
2765
0
    } else if (file->type == SC_FILE_TYPE_BSO) {
2766
0
      ops[0] = SC_AC_OP_UPDATE;
2767
0
      ops[3] = SC_AC_OP_DELETE;
2768
0
    } else if (file->type == SC_FILE_TYPE_INTERNAL_EF) {
2769
0
      if (file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_RSA_CRT ||
2770
0
          file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_EC_CRT) {
2771
0
        ops[1] = SC_AC_OP_UPDATE;
2772
0
        ops[2] = SC_AC_OP_CRYPTO;
2773
0
        ops[3] = SC_AC_OP_DELETE;
2774
0
      } else if (file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_RSA_PUBLIC ||
2775
0
          file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_EC_PUBLIC) {
2776
0
        ops[0] = SC_AC_OP_READ;
2777
0
        ops[1] = SC_AC_OP_UPDATE;
2778
0
        ops[2] = SC_AC_OP_CRYPTO;
2779
0
        ops[3] = SC_AC_OP_DELETE;
2780
0
      }
2781
0
    } else {
2782
0
      return SC_ERROR_NOT_SUPPORTED;
2783
0
    }
2784
2785
0
    for (ii = 0; ii < sizeof(ops); ii++) {
2786
0
      const struct sc_acl_entry *entry;
2787
2788
0
      buf[ii] = 0xFF;
2789
0
      if (ops[ii] == 0xFF)
2790
0
        continue;
2791
0
      entry = sc_file_get_acl_entry(file, ops[ii]);
2792
2793
0
      rv = acl_to_ac_byte(card, entry);
2794
0
      LOG_TEST_RET(card->ctx, rv, "Invalid ACL");
2795
2796
0
      buf[ii] = rv;
2797
0
    }
2798
0
    sc_asn1_put_tag(0x86, buf, sizeof(ops), p, *outlen - (p - out), &p);
2799
0
    if (file->size == 256) {
2800
0
      out[4]= 0x13;
2801
0
    }
2802
0
  }
2803
2804
  /* VT ??? */
2805
0
  if (file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_RSA_PUBLIC ||
2806
0
      file->ef_structure == SC_CARDCTL_OBERTHUR_KEY_EC_PUBLIC) {
2807
0
    unsigned char data[2] = {0x00, 0x66};
2808
0
    sc_asn1_put_tag(0x87, data, sizeof(data), p, *outlen - (p - out), &p);
2809
0
    if (file->size == 256) {
2810
0
      out[4] = 0x14;
2811
0
    }
2812
0
  }
2813
2814
0
  out[1] = p - out - 2;
2815
2816
0
  *outlen = p - out;
2817
0
  return 0;
2818
0
}
2819
2820
2821
static int
2822
epass2003_create_file(struct sc_card *card, sc_file_t * file)
2823
0
{
2824
0
  int r;
2825
0
  size_t len;
2826
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE] = {0};
2827
0
  struct sc_apdu apdu;
2828
2829
0
  len = SC_MAX_APDU_BUFFER_SIZE;
2830
2831
  /* creating an EF leaves the current DF unchanged */
2832
0
  if (file->type == SC_FILE_TYPE_DF)
2833
0
    epass2003_forget_cur_df(card);
2834
0
  epass2003_hook_file(file, 1);
2835
2836
0
  if (card->ops->construct_fci == NULL)
2837
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
2838
2839
0
  r = epass2003_construct_fci(card, file, sbuf, &len);
2840
0
  LOG_TEST_RET(card->ctx, r, "construct_fci() failed");
2841
2842
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x00, 0x00);
2843
0
  apdu.lc = len;
2844
0
  apdu.datalen = len;
2845
0
  apdu.data = sbuf;
2846
2847
0
  r = sc_transmit_apdu_t(card, &apdu);
2848
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
2849
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
2850
0
  LOG_TEST_RET(card->ctx, r, "APDU sw1/2 wrong");
2851
2852
0
  epass2003_hook_file(file, 0);
2853
0
  return r;
2854
0
}
2855
2856
2857
static int
2858
epass2003_delete_file(struct sc_card *card, const sc_path_t * path)
2859
0
{
2860
0
  int r;
2861
0
  u8 sbuf[2];
2862
0
  struct sc_apdu apdu;
2863
0
  sc_file_t *file = NULL;
2864
2865
0
  LOG_FUNC_CALLED(card->ctx);
2866
2867
0
  r = sc_select_file(card, path, &file);
2868
0
  LOG_TEST_RET(card->ctx, r, "Can not select file");
2869
  /* deleting an EF leaves the current DF unchanged */
2870
0
  if (!file || file->type == SC_FILE_TYPE_DF)
2871
0
    epass2003_forget_cur_df(card);
2872
0
  sc_file_free(file);
2873
0
  r = epass2003_hook_path((struct sc_path *)path, 1);
2874
0
  LOG_TEST_RET(card->ctx, r, "Can not hook path");
2875
2876
0
  sbuf[0] = path->value[path->len - 2];
2877
0
  sbuf[1] = path->value[path->len - 1];
2878
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE4, 0x00, 0x00);
2879
0
  apdu.lc = 2;
2880
0
  apdu.datalen = 2;
2881
0
  apdu.data = sbuf;
2882
2883
0
  r = sc_transmit_apdu_t(card, &apdu);
2884
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
2885
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
2886
0
  LOG_TEST_RET(card->ctx, r, "Delete file failed");
2887
2888
0
  LOG_FUNC_RETURN(card->ctx, r);
2889
0
}
2890
2891
static int
2892
epass2003_list_files(struct sc_card *card, unsigned char *buf, size_t buflen)
2893
323
{
2894
323
  struct sc_apdu apdu;
2895
323
  unsigned char rbuf[SC_MAX_APDU_BUFFER_SIZE] = {0};
2896
323
  int r;
2897
2898
323
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
2899
323
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x34, 0x00, 0x00);
2900
323
  apdu.cla = 0x80;
2901
323
  apdu.le = 0;
2902
323
  apdu.resplen = sizeof(rbuf);
2903
323
  apdu.resp = rbuf;
2904
2905
323
  r = sc_transmit_apdu_t(card, &apdu);
2906
323
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
2907
2908
105
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
2909
105
  LOG_TEST_RET(card->ctx, r, "Card returned error");
2910
2911
60
  if (apdu.resplen == 0x100 && rbuf[0] == 0 && rbuf[1] == 0)
2912
60
    LOG_FUNC_RETURN(card->ctx, 0);
2913
2914
59
  buflen = buflen < apdu.resplen ? buflen : apdu.resplen;
2915
59
  memcpy(buf, rbuf, buflen);
2916
2917
59
  LOG_FUNC_RETURN(card->ctx, (int)buflen);
2918
59
}
2919
2920
2921
static int
2922
internal_write_rsa_key_factor(struct sc_card *card, unsigned short fid, u8 factor,
2923
    sc_pkcs15_bignum_t data)
2924
0
{
2925
0
  int r;
2926
0
  struct sc_apdu apdu;
2927
0
  u8 sbuff[SC_MAX_EXT_APDU_BUFFER_SIZE] = {0};
2928
2929
0
  LOG_FUNC_CALLED(card->ctx);
2930
2931
0
  sbuff[0] = ((fid & 0xff00) >> 8);
2932
0
  sbuff[1] = (fid & 0x00ff);
2933
0
  memcpy(&sbuff[2], data.data, data.len);
2934
//  sc_mem_reverse(&sbuff[2], data.len);
2935
2936
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3, 0xe7, factor, 0x00);
2937
0
  apdu.cla = 0x80;
2938
0
  apdu.lc = apdu.datalen = 2 + data.len;
2939
0
  apdu.data = sbuff;
2940
2941
0
  r = sc_transmit_apdu_t(card, &apdu);
2942
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
2943
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
2944
0
  LOG_TEST_RET(card->ctx, r, "Write rsa key factor failed");
2945
2946
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
2947
0
}
2948
2949
2950
static int
2951
internal_write_rsa_key(struct sc_card *card, unsigned short fid, struct sc_pkcs15_prkey_rsa *rsa)
2952
0
{
2953
0
  int r;
2954
2955
0
  LOG_FUNC_CALLED(card->ctx);
2956
2957
0
  r = internal_write_rsa_key_factor(card, fid, 0x02, rsa->modulus);
2958
0
  LOG_TEST_RET(card->ctx, r, "write n failed");
2959
0
  r = internal_write_rsa_key_factor(card, fid, 0x03, rsa->d);
2960
0
  LOG_TEST_RET(card->ctx, r, "write d failed");
2961
2962
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
2963
0
}
2964
2965
2966
static int
2967
hash_data(struct sc_card *card, const unsigned char *data, size_t datalen, unsigned char *hash, unsigned int mechanismType)
2968
0
{
2969
2970
0
  if ((NULL == data) || (NULL == hash))
2971
0
    return SC_ERROR_INVALID_ARGUMENTS;
2972
2973
0
  if (mechanismType & SC_ALGORITHM_ECDSA_HASH_SHA1) {
2974
0
    unsigned char data_hash[24] = {0};
2975
0
    size_t len = 0;
2976
2977
0
    sha1_digest(card, data, datalen, data_hash);
2978
0
    len = REVERSE_ORDER4(datalen);
2979
0
    memcpy(&data_hash[20], &len, 4);
2980
0
    memcpy(hash, data_hash, 24);
2981
0
  } else if (mechanismType & SC_ALGORITHM_ECDSA_HASH_SHA256) {
2982
0
    unsigned char data_hash[36] = {0};
2983
0
    size_t len = 0;
2984
2985
0
    sha256_digest(card, data, datalen, data_hash);
2986
0
    len = REVERSE_ORDER4(datalen);
2987
0
    memcpy(&data_hash[32], &len, 4);
2988
0
    memcpy(hash, data_hash, 36);
2989
0
  } else {
2990
0
    return SC_ERROR_NOT_SUPPORTED;
2991
0
  }
2992
2993
0
  return SC_SUCCESS;
2994
0
}
2995
2996
2997
static int
2998
install_secret_key(struct sc_card *card, unsigned char ktype, unsigned char kid,
2999
    unsigned char useac, unsigned char modifyac, unsigned char EC,
3000
    unsigned char *data, unsigned long dataLen)
3001
0
{
3002
0
  int r;
3003
0
  struct sc_apdu apdu;
3004
0
  unsigned char isapp = 0x00; /* appendable */
3005
0
  unsigned char tmp_data[256] = {0};
3006
3007
0
  tmp_data[0] = ktype;
3008
0
  tmp_data[1] = kid;
3009
0
  tmp_data[2] = useac;
3010
0
  tmp_data[3] = modifyac;
3011
0
  tmp_data[8] = 0xFF;
3012
3013
0
  if (0x04 == ktype || 0x06 == ktype) {
3014
0
    tmp_data[4] = EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_SO;
3015
0
    tmp_data[5] = EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_SO;
3016
0
    tmp_data[7] = (kid == PIN_ID[0] ? EPASS2003_AC_USER : EPASS2003_AC_SO);
3017
0
    tmp_data[9] = (EC << 4) | EC;
3018
0
  }
3019
3020
0
  memcpy(&tmp_data[10], data, dataLen);
3021
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xe3, isapp, 0x00);
3022
0
  apdu.cla = 0x80;
3023
0
  apdu.lc = apdu.datalen = 10 + dataLen;
3024
0
  apdu.data = tmp_data;
3025
3026
0
  r = sc_transmit_apdu_t(card, &apdu);
3027
0
  LOG_TEST_RET(card->ctx, r, "APDU install_secret_key failed");
3028
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
3029
0
  LOG_TEST_RET(card->ctx, r, "install_secret_key failed");
3030
3031
0
  return r;
3032
0
}
3033
3034
3035
static int
3036
internal_install_pre(struct sc_card *card)
3037
0
{
3038
0
  int r;
3039
  /* init key for enc */
3040
0
  r = install_secret_key(card, 0x01, 0x00,
3041
0
             EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_EVERYONE,
3042
0
             EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_EVERYONE,
3043
0
             0, g_init_key_enc, 16);
3044
0
  LOG_TEST_RET(card->ctx, r, "Install init key failed");
3045
3046
  /* init key for mac */
3047
0
  r = install_secret_key(card, 0x02, 0x00,
3048
0
             EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_EVERYONE,
3049
0
             EPASS2003_AC_MAC_NOLESS | EPASS2003_AC_EVERYONE,
3050
0
             0, g_init_key_mac, 16);
3051
0
  LOG_TEST_RET(card->ctx, r, "Install init key failed");
3052
3053
0
  return r;
3054
0
}
3055
3056
3057
/* use external auth secret as pin */
3058
static int
3059
internal_install_pin(struct sc_card *card, sc_epass2003_wkey_data * pin)
3060
0
{
3061
0
  int r;
3062
0
  unsigned char hash[HASH_LEN] = {0};
3063
3064
0
  r = hash_data(card, pin->key_data.es_secret.key_val, pin->key_data.es_secret.key_len, hash, SC_ALGORITHM_ECDSA_HASH_SHA1);
3065
0
  LOG_TEST_RET(card->ctx, r, "hash data failed");
3066
3067
0
  r = install_secret_key(card, 0x04, pin->key_data.es_secret.kid,
3068
0
             pin->key_data.es_secret.ac[0],
3069
0
             pin->key_data.es_secret.ac[1],
3070
0
             pin->key_data.es_secret.EC, hash, HASH_LEN);
3071
0
  LOG_TEST_RET(card->ctx, r, "Install failed");
3072
3073
0
  return r;
3074
0
}
3075
3076
3077
static int
3078
epass2003_write_key(struct sc_card *card, sc_epass2003_wkey_data * data)
3079
0
{
3080
0
  LOG_FUNC_CALLED(card->ctx);
3081
3082
0
  if (data->type & SC_EPASS2003_KEY) {
3083
0
    if (data->type == SC_EPASS2003_KEY_RSA)
3084
0
      return internal_write_rsa_key(card, data->key_data.es_key.fid,
3085
0
                  data->key_data.es_key.rsa);
3086
0
    else
3087
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
3088
0
  } else if (data->type & SC_EPASS2003_SECRET) {
3089
0
    if (data->type == SC_EPASS2003_SECRET_PRE)
3090
0
      return internal_install_pre(card);
3091
0
    else if (data->type == SC_EPASS2003_SECRET_PIN)
3092
0
      return internal_install_pin(card, data);
3093
0
    else
3094
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
3095
0
  } else {
3096
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
3097
0
  }
3098
3099
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
3100
0
}
3101
3102
3103
static int
3104
epass2003_gen_key(struct sc_card *card, sc_epass2003_gen_key_data * data)
3105
0
{
3106
0
  int r;
3107
0
  size_t len = data->key_length;
3108
0
  struct sc_apdu apdu;
3109
0
  u8 rbuf[SC_MAX_EXT_APDU_BUFFER_SIZE] = {0};
3110
0
  u8 sbuf[SC_MAX_EXT_APDU_BUFFER_SIZE] = {0};
3111
3112
0
  LOG_FUNC_CALLED(card->ctx);
3113
3114
0
  if (len == 256) {
3115
0
    sbuf[0] = 0x02;
3116
0
  } else {
3117
0
    sbuf[0] = 0x01;
3118
0
  }
3119
0
  sbuf[1] = (u8) ((len >> 8) & 0xff);
3120
0
  sbuf[2] = (u8) (len & 0xff);
3121
0
  sbuf[3] = (u8) ((data->prkey_id >> 8) & 0xFF);
3122
0
  sbuf[4] = (u8) ((data->prkey_id) & 0xFF);
3123
0
  sbuf[5] = (u8) ((data->pukey_id >> 8) & 0xFF);
3124
0
  sbuf[6] = (u8) ((data->pukey_id) & 0xFF);
3125
3126
  /* generate key */
3127
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x46, 0x00, 0x00);
3128
0
  apdu.lc = apdu.datalen = 7;
3129
0
  apdu.data = sbuf;
3130
3131
0
  r = sc_transmit_apdu_t(card, &apdu);
3132
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
3133
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
3134
0
  LOG_TEST_RET(card->ctx, r, "generate key pair failed");
3135
3136
  /* read public key */
3137
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xb4, 0x02, 0x00);
3138
0
  if (len == 256) {
3139
0
    apdu.p1 = 0x00;
3140
0
  }
3141
3142
0
  apdu.cla = 0x80;
3143
0
  apdu.lc = apdu.datalen = 2;
3144
0
  apdu.data = &sbuf[5];
3145
0
  apdu.resp = rbuf;
3146
0
  apdu.resplen = sizeof(rbuf);
3147
0
  apdu.le = 0x00;
3148
3149
0
  r = sc_transmit_apdu_t(card, &apdu);
3150
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
3151
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
3152
0
  LOG_TEST_RET(card->ctx, r, "get pukey failed");
3153
3154
0
  if (len < apdu.resplen)
3155
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
3156
3157
0
  if (256 == len) { /* ECC 256 bit */
3158
0
    size_t xCoordinateLen = rbuf[1];
3159
0
    size_t yCoordinateLen;
3160
0
    unsigned char *tmp;
3161
3162
0
    if (2 + xCoordinateLen + 1 > apdu.resplen) {
3163
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_DATA);
3164
0
    }
3165
0
    yCoordinateLen = rbuf[2 + xCoordinateLen + 1];
3166
0
    if (2 + xCoordinateLen + 2 + yCoordinateLen > apdu.resplen) {
3167
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_DATA);
3168
0
    }
3169
0
    data->modulus_len = xCoordinateLen + yCoordinateLen;
3170
0
    tmp = (u8 *)malloc(data->modulus_len);
3171
0
    if (!tmp) {
3172
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
3173
0
    }
3174
3175
0
    if (0x58 == rbuf[0]) {
3176
0
      memcpy(tmp, &rbuf[2], xCoordinateLen);
3177
0
    } else {
3178
0
      free(tmp);
3179
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OBJECT_NOT_VALID);
3180
0
    }
3181
0
    if (0x59 == rbuf[2 + xCoordinateLen]) {
3182
0
      memcpy(tmp + xCoordinateLen, &rbuf[2+xCoordinateLen+2], yCoordinateLen);
3183
0
    } else {
3184
0
      free(tmp);
3185
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OBJECT_NOT_VALID);
3186
0
    }
3187
3188
0
    data->modulus = tmp;
3189
0
  } else {
3190
0
    data->modulus = (u8 *) malloc(len);
3191
0
    if (!data->modulus) {
3192
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
3193
0
    } else {
3194
0
      memcpy(data->modulus, rbuf, len);
3195
0
    }
3196
0
  }
3197
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
3198
0
}
3199
3200
3201
static int
3202
epass2003_erase_card(struct sc_card *card)
3203
0
{
3204
0
  static const unsigned char install_magic_pin[26] = {
3205
    /* compare install_secret_key */
3206
0
      0x06, 0x01, 0x10, 0x16, 0x16, 0x16, 0x00, 0x0f, 0xff, 0x66,
3207
0
      0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
3208
0
      0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
3209
0
  };
3210
0
  static const unsigned char magic_pin[16] = {
3211
0
      0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
3212
0
      0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
3213
0
  };
3214
0
  static const unsigned char mf_path[2] = { 0x3f, 0x00 };
3215
0
  sc_apdu_t apdu;
3216
0
  int r;
3217
3218
0
  LOG_FUNC_CALLED(card->ctx);
3219
0
  epass2003_forget_cur_df(card);
3220
3221
  /* install magic pin */
3222
0
  sc_format_apdu(card, &apdu, 0x03, 0xe3, 0x00, 0x00);
3223
0
  apdu.cla = 0x80;
3224
0
  apdu.data = install_magic_pin;
3225
0
  apdu.datalen = apdu.lc = sizeof(install_magic_pin);
3226
0
  r = sc_transmit_apdu(card, &apdu);
3227
0
  LOG_TEST_RET(card->ctx, r, "APDU install magic pin failed");
3228
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
3229
0
  LOG_TEST_RET(card->ctx, r, "install magic pin failed");
3230
3231
  /* verify magic pin */
3232
0
  sc_format_apdu(card, &apdu, 0x03, 0x20, 0x00, 0x01);
3233
0
  apdu.cla = 0;
3234
0
  apdu.data = magic_pin;
3235
0
  apdu.datalen = apdu.lc = sizeof(magic_pin);
3236
0
  r = sc_transmit_apdu(card, &apdu);
3237
0
  LOG_TEST_RET(card->ctx, r, "APDU verify magic pin failed");
3238
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
3239
0
  LOG_TEST_RET(card->ctx, r, "verify magic pin failed");
3240
3241
  /* delete MF */
3242
0
  sc_format_apdu(card, &apdu, 0x03, 0xe4, 0x00, 0x00);
3243
0
  apdu.cla = 0;
3244
0
  apdu.data = mf_path;
3245
0
  apdu.datalen = apdu.lc = sizeof(mf_path);
3246
0
  r = sc_transmit_apdu(card, &apdu);
3247
0
  LOG_TEST_RET(card->ctx, r, "APDU delete MF failed");
3248
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
3249
0
  LOG_TEST_RET(card->ctx, r, "delete MF failed");
3250
3251
0
  LOG_FUNC_RETURN(card->ctx, r);
3252
0
}
3253
3254
3255
static int
3256
epass2003_get_serialnr(struct sc_card *card, sc_serial_number_t * serial)
3257
0
{
3258
0
  u8 rbuf[8];
3259
0
  size_t rbuf_len = sizeof(rbuf);
3260
3261
0
  LOG_FUNC_CALLED(card->ctx);
3262
3263
0
  if (SC_SUCCESS != get_data(card, 0x80, rbuf, rbuf_len))
3264
0
    return SC_ERROR_CARD_CMD_FAILED;
3265
3266
0
  card->serialnr.len = serial->len = 8;
3267
0
  memcpy(card->serialnr.value, rbuf, 8);
3268
0
  memcpy(serial->value, rbuf, 8);
3269
3270
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
3271
0
}
3272
3273
3274
static int
3275
epass2003_card_ctl(struct sc_card *card, unsigned long cmd, void *ptr)
3276
0
{
3277
0
  LOG_FUNC_CALLED(card->ctx);
3278
3279
0
  sc_log(card->ctx, "cmd is %0lx", cmd);
3280
0
  switch (cmd) {
3281
0
  case SC_CARDCTL_ENTERSAFE_WRITE_KEY:
3282
0
    return epass2003_write_key(card, (sc_epass2003_wkey_data *) ptr);
3283
0
  case SC_CARDCTL_ENTERSAFE_GENERATE_KEY:
3284
0
    return epass2003_gen_key(card, (sc_epass2003_gen_key_data *) ptr);
3285
0
  case SC_CARDCTL_ERASE_CARD:
3286
0
    return epass2003_erase_card(card);
3287
0
  case SC_CARDCTL_GET_SERIALNR:
3288
0
    return epass2003_get_serialnr(card, (sc_serial_number_t *) ptr);
3289
0
  default:
3290
0
    return SC_ERROR_NOT_SUPPORTED;
3291
0
  }
3292
0
}
3293
3294
3295
static void
3296
internal_sanitize_pin_info(struct sc_pin_cmd_pin *pin, unsigned int num)
3297
0
{
3298
0
  pin->encoding = SC_PIN_ENCODING_ASCII;
3299
0
  pin->min_length = 4;
3300
0
  pin->max_length = 16;
3301
0
  pin->pad_length = 16;
3302
0
  pin->offset = 5 + num * 16;
3303
0
  pin->pad_char = 0x00;
3304
0
}
3305
3306
3307
static int
3308
get_external_key_maxtries(struct sc_card *card, unsigned char *maxtries)
3309
0
{
3310
0
  unsigned char maxcounter[2] = {0};
3311
0
  static const sc_path_t file_path = {
3312
0
    {0x3f, 0x00, 0x50, 0x15, 0x9f, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 6,
3313
0
    0,
3314
0
    0,
3315
0
    SC_PATH_TYPE_PATH,
3316
0
    {{0}, 0}
3317
0
  };
3318
0
  int ret;
3319
3320
0
  ret = sc_select_file(card, &file_path, NULL);
3321
0
  LOG_TEST_RET(card->ctx, ret, "select max counter file failed");
3322
3323
0
  ret = sc_read_binary(card, 0, maxcounter, 2, 0);
3324
0
  LOG_TEST_RET(card->ctx, ret, "read max counter file failed");
3325
3326
0
  *maxtries = maxcounter[0];
3327
0
  return SC_SUCCESS;
3328
0
}
3329
3330
3331
static int
3332
get_external_key_retries(struct sc_card *card, unsigned char kid, unsigned char *retries)
3333
0
{
3334
0
  int r;
3335
0
  struct sc_apdu apdu;
3336
0
  unsigned char random[16] = {0};
3337
3338
0
  r = sc_get_challenge(card, random, 8);
3339
0
  LOG_TEST_RET(card->ctx, r, "get challenge get_external_key_retries failed");
3340
3341
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x82, 0x01, 0x80 | kid);
3342
0
  apdu.resp = NULL;
3343
0
  apdu.resplen = 0;
3344
3345
0
  r = sc_transmit_apdu_t(card, &apdu);
3346
0
  LOG_TEST_RET(card->ctx, r, "APDU get_external_key_retries failed");
3347
3348
0
  if (retries && ((0x63 == (apdu.sw1 & 0xff)) && (0xC0 == (apdu.sw2 & 0xf0)))) {
3349
0
    *retries = (apdu.sw2 & 0x0f);
3350
0
    r = SC_SUCCESS;
3351
0
  } else {
3352
0
    LOG_TEST_RET(card->ctx, r, "get_external_key_retries failed");
3353
0
    r = SC_ERROR_CARD_CMD_FAILED;
3354
0
  }
3355
3356
0
  return r;
3357
0
}
3358
3359
static int
3360
epass2003_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
3361
881
{
3362
881
  u8 rbuf[16];
3363
881
  size_t out_len;
3364
881
  int r;
3365
3366
881
  LOG_FUNC_CALLED(card->ctx);
3367
3368
881
  if (!rnd || len == 0)
3369
881
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
3370
3371
881
  r = epass2003_sm_restart(card);
3372
881
  LOG_TEST_RET(card->ctx, r, "Can not re-establish SM");
3373
3374
760
  r = iso_ops->get_challenge(card, rbuf, sizeof rbuf);
3375
760
  LOG_TEST_RET(card->ctx, r, "GET CHALLENGE cmd failed");
3376
3377
697
  if (len < (size_t) r) {
3378
2
    out_len = len;
3379
695
  } else {
3380
695
    out_len = (size_t) r;
3381
695
  }
3382
697
  memcpy(rnd, rbuf, out_len);
3383
3384
697
  LOG_FUNC_RETURN(card->ctx, (int) out_len);
3385
697
}
3386
3387
3388
static int
3389
external_key_auth(struct sc_card *card, unsigned char kid,
3390
    unsigned char *data, size_t datalen)
3391
0
{
3392
0
  int r;
3393
0
  struct sc_apdu apdu;
3394
0
  unsigned char random[16] = {0};
3395
0
  unsigned char tmp_data[16] = {0};
3396
0
  unsigned char hash[HASH_LEN] = {0};
3397
0
  unsigned char iv[16] = {0};
3398
3399
0
  r = sc_get_challenge(card, random, 8);
3400
0
  LOG_TEST_RET(card->ctx, r, "get challenge external_key_auth failed");
3401
3402
0
  r = hash_data(card, data, datalen, hash, SC_ALGORITHM_ECDSA_HASH_SHA1);
3403
0
  LOG_TEST_RET(card->ctx, r, "hash data failed");
3404
3405
0
  r = des3_encrypt_cbc(card, hash, HASH_LEN, iv, random, 8, tmp_data);
3406
0
  LOG_TEST_RET(card->ctx, r, "encryption failed");
3407
3408
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x82, 0x01, 0x80 | kid);
3409
0
  apdu.lc = apdu.datalen = 8;
3410
0
  apdu.data = tmp_data;
3411
3412
0
  r = sc_transmit_apdu_t(card, &apdu);
3413
0
  LOG_TEST_RET(card->ctx, r, "APDU external_key_auth failed");
3414
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
3415
0
  LOG_TEST_RET(card->ctx, r, "external_key_auth failed");
3416
3417
0
  return r;
3418
0
}
3419
3420
3421
static int
3422
update_secret_key(struct sc_card *card, unsigned char ktype, unsigned char kid,
3423
    const unsigned char *data, unsigned long datalen)
3424
0
{
3425
0
  int r;
3426
0
  struct sc_apdu apdu;
3427
0
  unsigned char hash[HASH_LEN] = {0};
3428
0
  unsigned char tmp_data[256] = {0};
3429
0
  unsigned char maxtries = 0;
3430
3431
0
  r = hash_data(card, data, datalen, hash, SC_ALGORITHM_ECDSA_HASH_SHA1);
3432
0
  LOG_TEST_RET(card->ctx, r, "hash data failed");
3433
3434
0
  r = get_external_key_maxtries(card, &maxtries);
3435
0
  LOG_TEST_RET(card->ctx, r, "get max counter failed");
3436
3437
0
  tmp_data[0] = (maxtries << 4) | maxtries;
3438
0
  memcpy(&tmp_data[1], hash, HASH_LEN);
3439
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xe5, ktype, kid);
3440
0
  apdu.cla = 0x80;
3441
0
  apdu.lc = apdu.datalen = 1 + HASH_LEN;
3442
0
  apdu.data = tmp_data;
3443
3444
0
  r = sc_transmit_apdu_t(card, &apdu);
3445
0
  LOG_TEST_RET(card->ctx, r, "APDU update_secret_key failed");
3446
0
  r = sc_check_sw(card, apdu.sw1, apdu.sw2);
3447
0
  LOG_TEST_RET(card->ctx, r, "update_secret_key failed");
3448
3449
0
  return r;
3450
0
}
3451
3452
/* use external auth secret as pin */
3453
static int
3454
epass2003_pin_cmd(struct sc_card *card, struct sc_pin_cmd_data *data)
3455
0
{
3456
0
  int r;
3457
0
  u8 kid;
3458
0
  u8 retries = 0;
3459
0
  u8 pin_low = 3;
3460
0
  unsigned char maxtries = 0;
3461
3462
0
  LOG_FUNC_CALLED(card->ctx);
3463
3464
0
  if (!data)
3465
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
3466
3467
0
  internal_sanitize_pin_info(&data->pin1, 0);
3468
0
  internal_sanitize_pin_info(&data->pin2, 1);
3469
0
  data->pin1.tries_left = -1;
3470
0
  data->flags |= SC_PIN_CMD_NEED_PADDING;
3471
0
  kid = data->pin_reference;
3472
3473
  /* get pin retries */
3474
0
  if (data->cmd == SC_PIN_CMD_GET_INFO) {
3475
3476
0
    r = get_external_key_retries(card, 0x80 | kid, &retries);
3477
0
    if (r == SC_SUCCESS) {
3478
0
      data->pin1.tries_left = retries;
3479
3480
0
      r = get_external_key_maxtries(card, &maxtries);
3481
0
      LOG_TEST_RET(card->ctx, r, "get max counter failed");
3482
3483
0
      data->pin1.max_tries = maxtries;
3484
0
      data->pin1.logged_in = SC_PIN_STATE_UNKNOWN;
3485
0
    }
3486
0
    LOG_FUNC_RETURN(card->ctx, r);
3487
0
  }
3488
3489
0
  if (NULL == (unsigned char *)data->pin1.data || 0 == data->pin1.len)
3490
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_PIN_CODE_INCORRECT);
3491
3492
0
  if (data->cmd == SC_PIN_CMD_UNBLOCK) {
3493
0
    r = external_key_auth(card, (kid + 1), (unsigned char *)data->pin1.data,
3494
0
        data->pin1.len);
3495
0
    if (r == SC_ERROR_PIN_CODE_INCORRECT &&
3496
0
        get_external_key_retries(card, 0x80 | (kid + 1), &retries) == SC_SUCCESS)
3497
0
      data->pin1.tries_left = retries;
3498
0
    LOG_TEST_RET(card->ctx, r, "verify unblock pin failed");
3499
3500
0
    if (data->pin2.data && data->pin2.len > 0) {
3501
0
      r = update_secret_key(card, 0x04, kid, data->pin2.data,
3502
0
          (unsigned long)data->pin2.len);
3503
0
      LOG_TEST_RET(card->ctx, r, "change pin failed");
3504
0
    }
3505
0
  } else if (data->cmd == SC_PIN_CMD_CHANGE) {
3506
0
    if (NULL == data->pin2.data || 0 == data->pin2.len)
3507
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
3508
3509
0
    r = external_key_auth(card, kid, (unsigned char *)data->pin1.data,
3510
0
        data->pin1.len);
3511
0
    if (r == SC_ERROR_PIN_CODE_INCORRECT &&
3512
0
        get_external_key_retries(card, 0x80 | kid, &retries) == SC_SUCCESS)
3513
0
      data->pin1.tries_left = retries;
3514
0
    LOG_TEST_RET(card->ctx, r, "verify pin failed");
3515
3516
0
    r = update_secret_key(card, 0x04, kid, data->pin2.data,
3517
0
        (unsigned long)data->pin2.len);
3518
0
    LOG_TEST_RET(card->ctx, r, "change pin failed");
3519
0
  } else {
3520
0
    r = external_key_auth(card, kid, (unsigned char *)data->pin1.data,
3521
0
        data->pin1.len);
3522
0
    if (r == SC_ERROR_PIN_CODE_INCORRECT &&
3523
0
        get_external_key_retries(card, 0x80 | kid, &retries) == SC_SUCCESS)
3524
0
      data->pin1.tries_left = retries;
3525
0
    LOG_TEST_RET(card->ctx, r, "verify pin failed");
3526
3527
0
    r = get_external_key_retries(card, 0x80 | kid, &retries);
3528
0
    if (retries < pin_low)
3529
0
      sc_log(card->ctx, "Verification failed (remaining tries: %d)", retries);
3530
3531
0
    LOG_TEST_RET(card->ctx, r, "verify pin failed");
3532
0
  }
3533
3534
0
  if (r == SC_SUCCESS) {
3535
0
    data->pin1.logged_in = SC_PIN_STATE_LOGGED_IN;
3536
0
  }
3537
0
  return r;
3538
0
}
3539
3540
static int
3541
epass2003_logout(struct sc_card *card)
3542
0
{
3543
0
  epass2003_exdata *exdata = NULL;
3544
3545
0
  if (!card->drv_data)
3546
0
    return SC_ERROR_INVALID_ARGUMENTS;
3547
3548
0
  exdata = (epass2003_exdata *)card->drv_data;
3549
0
  if (exdata->sm) {
3550
0
    sc_sm_stop(card);
3551
0
    return epass2003_refresh(card);
3552
0
  }
3553
3554
0
  return SC_ERROR_NOT_SUPPORTED;
3555
0
}
3556
3557
static struct sc_card_driver *sc_get_driver(void)
3558
9.82k
{
3559
9.82k
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
3560
3561
9.82k
  if (iso_ops == NULL)
3562
1
    iso_ops = iso_drv->ops;
3563
3564
9.82k
  epass2003_ops = *iso_ops;
3565
3566
9.82k
  epass2003_ops.match_card = epass2003_match_card;
3567
9.82k
  epass2003_ops.init = epass2003_init;
3568
9.82k
  epass2003_ops.finish = epass2003_finish;
3569
9.82k
  epass2003_ops.write_binary = NULL;
3570
9.82k
  epass2003_ops.write_record = NULL;
3571
9.82k
  epass2003_ops.select_file = epass2003_select_file;
3572
9.82k
  epass2003_ops.get_response = NULL;
3573
9.82k
  epass2003_ops.restore_security_env = epass2003_restore_security_env;
3574
9.82k
  epass2003_ops.set_security_env = epass2003_set_security_env;
3575
9.82k
  epass2003_ops.decipher = epass2003_decipher;
3576
9.82k
  epass2003_ops.compute_signature = epass2003_decipher;
3577
9.82k
  epass2003_ops.create_file = epass2003_create_file;
3578
9.82k
  epass2003_ops.delete_file = epass2003_delete_file;
3579
9.82k
  epass2003_ops.list_files = epass2003_list_files;
3580
9.82k
  epass2003_ops.card_ctl = epass2003_card_ctl;
3581
9.82k
  epass2003_ops.process_fci = epass2003_process_fci;
3582
9.82k
  epass2003_ops.construct_fci = epass2003_construct_fci;
3583
9.82k
  epass2003_ops.pin_cmd = epass2003_pin_cmd;
3584
9.82k
  epass2003_ops.check_sw = epass2003_check_sw;
3585
9.82k
  epass2003_ops.get_challenge = epass2003_get_challenge;
3586
9.82k
  epass2003_ops.logout = epass2003_logout;
3587
9.82k
  epass2003_ops.card_reader_lock_obtained = epass2003_card_reader_lock_obtained;
3588
9.82k
  return &epass2003_drv;
3589
9.82k
}
3590
3591
struct sc_card_driver *sc_get_epass2003_driver(void)
3592
9.82k
{
3593
9.82k
  return sc_get_driver();
3594
9.82k
}
3595
#endif  /* #ifdef ENABLE_OPENSSL */
3596
#endif  /* #ifdef ENABLE_SM */