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-rutoken.c
Line
Count
Source
1
/*
2
 * card-rutoken.c: Support for Rutoken S cards
3
 *
4
 * Copyright (C) 2007  Pavel Mironchik <rutoken@rutoken.ru>
5
 * Copyright (C) 2007  Eugene Hermann <rutoken@rutoken.ru>
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
26
#include <sys/types.h>
27
#include <assert.h>
28
#include <ctype.h>
29
#include <string.h>
30
#include <stdlib.h>
31
#include <stdio.h>
32
33
#include "internal.h"
34
#include "opensc.h"
35
#include "pkcs15.h"
36
#include "asn1.h"
37
#include "cardctl.h"
38
39
struct auth_senv {
40
  unsigned int algorithm;
41
};
42
typedef struct auth_senv auth_senv_t;
43
44
struct helper_acl_to_sec_attr
45
{
46
  unsigned int ac_op;
47
  size_t sec_attr_pos;
48
};
49
typedef struct helper_acl_to_sec_attr helper_acl_to_sec_attr_t;
50
51
static const helper_acl_to_sec_attr_t arr_convert_attr_df [] = {
52
  { SC_AC_OP_CREATE, 0 },
53
  { SC_AC_OP_CREATE, 1 },
54
  { SC_AC_OP_DELETE, 6 }
55
};
56
57
static const helper_acl_to_sec_attr_t arr_convert_attr_ef [] = {
58
  { SC_AC_OP_READ, 0 },
59
  { SC_AC_OP_UPDATE, 1 },
60
  { SC_AC_OP_WRITE, 1 },
61
  { SC_AC_OP_DELETE, 6 }
62
};
63
64
static const sc_SecAttrV2_t default_sec_attr = {
65
  0x42,
66
  0, 1, 0, 0, 0, 0, 1,
67
  0, 0, 0, 0,
68
  2, 0, 0, 0,
69
  0, 0, 0, 0,
70
  0, 0, 0, 0,
71
  0, 0, 0, 0,
72
  0, 0, 0, 0,
73
  2, 0, 0, 0,
74
  0, 0, 0, 0 /* reserve */
75
};
76
77
static const struct sc_card_operations *iso_ops = NULL;
78
static struct sc_card_operations rutoken_ops;
79
80
static struct sc_card_driver rutoken_drv = {
81
  "Rutoken driver",
82
  "rutoken",
83
  &rutoken_ops,
84
  NULL, 0, NULL
85
};
86
87
static const struct sc_atr_table rutoken_atrs[] = {
88
  { "3b:6f:00:ff:00:56:72:75:54:6f:6b:6e:73:30:20:00:00:90:00", NULL, NULL, SC_CARD_TYPE_RUTOKENS, 0, NULL }, /* Aktiv Rutoken S */
89
  { "3b:6f:00:ff:00:56:75:61:54:6f:6b:6e:73:30:20:00:00:90:00", NULL, NULL, SC_CARD_TYPE_RUTOKENS, 0, NULL }, /* Aktiv uaToken S */
90
  { NULL, NULL, NULL, 0, 0, NULL }
91
};
92
93
static int rutoken_finish(sc_card_t *card)
94
127
{
95
127
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
96
127
  if (!card->drv_data)
97
0
    return SC_ERROR_INTERNAL;
98
127
  free(card->drv_data);
99
127
  card->drv_data = NULL;
100
127
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
101
127
}
102
103
static int rutoken_match_card(sc_card_t *card)
104
9.75k
{
105
9.75k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
106
9.75k
  if (_sc_match_atr(card, rutoken_atrs, &card->type) >= 0)
107
127
  {
108
127
    sc_log(card->ctx,  "ATR recognized as Rutoken\n");
109
127
    LOG_FUNC_RETURN(card->ctx, 1);
110
127
  }
111
9.62k
  LOG_FUNC_RETURN(card->ctx, 0);
112
9.62k
}
113
114
static int token_init(sc_card_t *card, const char *card_name)
115
127
{
116
127
  LOG_FUNC_CALLED(card->ctx);
117
118
127
  card->name = card_name;
119
127
  card->caps |= SC_CARD_CAP_RNG;
120
127
  card->drv_data = calloc(1, sizeof(auth_senv_t));
121
127
  if (card->drv_data == NULL)
122
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_OUT_OF_MEMORY);
123
124
127
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
125
127
}
126
127
static int rutoken_init(sc_card_t *card)
128
127
{
129
127
  int ret;
130
131
127
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
132
  /* &rutoken_atrs[1] : { uaToken S ATR, NULL ATR } */
133
127
  if (_sc_match_atr(card, &rutoken_atrs[1], &card->type) >= 0)
134
1
    ret = token_init(card, "uaToken S card");
135
126
  else
136
126
    ret = token_init(card, "Rutoken S card");
137
138
127
  if (ret != SC_SUCCESS) {
139
0
    ret = SC_ERROR_INVALID_CARD;
140
0
  }
141
127
  LOG_FUNC_RETURN(card->ctx, ret);
142
127
}
143
144
static const struct sc_card_error rutoken_errors[] = {
145
146
  { 0x6300, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
147
  { 0x63C1, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed. One tries left"},
148
  { 0x63C2, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed. Two tries left"},
149
  { 0x63C3, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
150
  { 0x63C4, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
151
  { 0x63C5, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
152
  { 0x63C6, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
153
  { 0x63C7, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
154
  { 0x63C8, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
155
  { 0x63C9, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
156
  { 0x63CA, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
157
  { 0x63CB, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
158
  { 0x63CC, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
159
  { 0x63CD, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
160
  { 0x63CE, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
161
  { 0x63CF, SC_ERROR_PIN_CODE_INCORRECT,  "Authentication failed"},
162
163
  { 0x6400, SC_ERROR_CARD_CMD_FAILED,     "Aborting"},
164
165
  { 0x6500, SC_ERROR_MEMORY_FAILURE,      "Memory failure"},
166
  { 0x6581, SC_ERROR_MEMORY_FAILURE,      "Memory failure"},
167
168
  { 0x6700, SC_ERROR_WRONG_LENGTH,        "Lc or Le invalid"},
169
170
  { 0x6883, SC_ERROR_CARD_CMD_FAILED,     "The finishing command of a chain is expected"},
171
172
  { 0x6982, SC_ERROR_SECURITY_STATUS_NOT_SATISFIED, "Required access right not granted"},
173
  { 0x6983, SC_ERROR_AUTH_METHOD_BLOCKED, "DO blocked"},
174
  { 0x6985, SC_ERROR_CARD_CMD_FAILED,     "Command not allowed (unsuitable conditions)"},
175
  { 0x6986, SC_ERROR_INCORRECT_PARAMETERS,"No current EF selected"},
176
177
  { 0x6A80, SC_ERROR_INCORRECT_PARAMETERS,"Invalid parameters in data field"},
178
  { 0x6A81, SC_ERROR_NOT_SUPPORTED,       "Function/mode not supported"},
179
  { 0x6A82, SC_ERROR_FILE_NOT_FOUND,      "File (DO) not found"},
180
  { 0x6A84, SC_ERROR_CARD_CMD_FAILED,     "Not enough memory space in the token"},
181
  { 0x6A86, SC_ERROR_INCORRECT_PARAMETERS,"P1 or P2 invalid"},
182
  { 0x6A89, SC_ERROR_FILE_ALREADY_EXISTS, "File (DO) already exists"},
183
184
  { 0x6B00, SC_ERROR_INCORRECT_PARAMETERS,"Out of maximum file length"},
185
186
  { 0x6C00, SC_ERROR_WRONG_LENGTH,        "Le does not fit the data to be sent"},
187
188
  { 0x6D00, SC_ERROR_INS_NOT_SUPPORTED,   "Ins invalid (not supported)"},
189
190
  /* Own class of an error*/
191
  { 0x6F01, SC_ERROR_CARD_CMD_FAILED,     "Rutoken has the exchange protocol which is not supported by the USB-driver (newer, than in the driver)"},
192
  { 0x6F83, SC_ERROR_CARD_CMD_FAILED,     "Infringement of the exchange protocol with Rutoken is revealed"},
193
  { 0x6F84, SC_ERROR_CARD_CMD_FAILED,     "Rutoken is busy by processing of other command"},
194
  { 0x6F85, SC_ERROR_CARD_CMD_FAILED,     "In the current folder the maximum quantity of file system objects is already created"},
195
  { 0x6F86, SC_ERROR_CARD_CMD_FAILED,     "Invalid access right. Already login"},
196
197
  { 0x9000, SC_SUCCESS,                  NULL}
198
};
199
200
static int rutoken_check_sw(sc_card_t *card, unsigned int sw1, unsigned int sw2)
201
1.22k
{
202
1.22k
  size_t i;
203
204
45.1k
  for (i = 0; i < sizeof(rutoken_errors)/sizeof(rutoken_errors[0]); ++i) {
205
45.1k
    if (rutoken_errors[i].SWs == ((sw1 << 8) | sw2)) {
206
1.16k
      if ( rutoken_errors[i].errorstr )
207
515
        sc_log(card->ctx,  "%s\n", rutoken_errors[i].errorstr);
208
1.16k
      sc_log(card->ctx,  "sw1 = %x, sw2 = %x", sw1, sw2);
209
1.16k
      return rutoken_errors[i].errorno;
210
1.16k
    }
211
45.1k
  }
212
54
  sc_log(card->ctx,  "Unknown SWs; SW1=%02X, SW2=%02X\n", sw1, sw2);
213
54
  return SC_ERROR_CARD_CMD_FAILED;
214
1.22k
}
215
216
static void swap_pair(u8 *buf, size_t len)
217
468
{
218
468
  size_t i;
219
468
  u8 tmp;
220
221
1.17k
  for (i = 0; i + 1 < len; i += 2)
222
708
  {
223
708
    tmp = buf[i];
224
708
    buf[i] = buf[i + 1];
225
708
    buf[i + 1] = tmp;
226
708
  }
227
468
}
228
229
static void swap_four(u8 *buf, size_t len)
230
0
{
231
0
  size_t i;
232
0
  u8 tmp;
233
234
0
  for (i = 0; i + 3 < len; i += 4)
235
0
  {
236
0
    tmp = buf[i];
237
0
    buf[i] = buf[i + 3];
238
0
    buf[i + 3] = tmp;
239
0
    swap_pair(&buf[i + 1], 2);
240
0
  }
241
0
}
242
243
static int rutoken_list_files(sc_card_t *card, u8 *buf, size_t buflen)
244
0
{
245
0
  sc_apdu_t apdu;
246
0
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE], previd[2];
247
0
  const u8 *tag;
248
0
  size_t taglen, len = 0;
249
0
  int ret;
250
251
0
  if (!card || !card->ctx)
252
0
    return SC_ERROR_INTERNAL;
253
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
254
0
  if (!buf)
255
0
    return SC_ERROR_INTERNAL;
256
257
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xA4, 0, 0);
258
0
  for (;;)
259
0
  {
260
0
    apdu.resp = rbuf;
261
0
    apdu.resplen = sizeof(rbuf);
262
0
    apdu.le = 256;
263
0
    ret = sc_transmit_apdu(card, &apdu);
264
0
    LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
265
0
    if (apdu.sw1 == 0x6A  &&  apdu.sw2 == 0x82)
266
0
      break; /* Next file not found */
267
268
0
    ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
269
0
    LOG_TEST_RET(card->ctx, ret, "");
270
271
0
    if (apdu.resplen <= 2)
272
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_WRONG_LENGTH);
273
274
    /* save first file(dir) ID */
275
0
    tag = sc_asn1_find_tag(card->ctx, apdu.resp + 2, apdu.resplen - 2,
276
0
        0x83, &taglen);
277
0
    if (!tag || taglen != sizeof(previd))
278
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED);
279
0
    memcpy(previd, tag, sizeof(previd));
280
281
0
    if (len + sizeof(previd) <= buflen)
282
0
    {
283
0
      buf[len++] = previd[1];
284
0
      buf[len++] = previd[0];
285
0
    }
286
287
0
    tag = sc_asn1_find_tag(card->ctx, apdu.resp + 2, apdu.resplen - 2,
288
0
        0x82, &taglen);
289
0
    if (!tag || taglen != 2)
290
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED);
291
0
    if (tag[0] == 0x38)
292
0
    {
293
      /* Select parent DF of the current DF */
294
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xA4, 0x03, 0);
295
0
      apdu.resp = rbuf;
296
0
      apdu.resplen = sizeof(rbuf);
297
0
      apdu.le = 256;
298
0
      ret = sc_transmit_apdu(card, &apdu);
299
0
      LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
300
0
      ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
301
0
      LOG_TEST_RET(card->ctx, ret, "");
302
0
    }
303
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0, 0x02);
304
0
    apdu.lc = sizeof(previd);
305
0
    apdu.data = previd;
306
0
    apdu.datalen = sizeof(previd);
307
0
  }
308
0
  LOG_FUNC_RETURN(card->ctx, (int)len);
309
0
}
310
311
static void set_acl_from_sec_attr(sc_card_t *card, sc_file_t *file)
312
133
{
313
133
  if (file->sec_attr  &&  file->sec_attr_len == sizeof(sc_SecAttrV2_t))
314
133
  {
315
133
    sc_file_add_acl_entry(file, SC_AC_OP_SELECT,
316
133
        SC_AC_NONE, SC_AC_KEY_REF_NONE);
317
133
    if (file->sec_attr[0] & 0x40) /* if AccessMode.6 */
318
22
    {
319
22
      sc_log(card->ctx,  "SC_AC_OP_DELETE %i %i",
320
22
          (int)(*(int8_t*)&file->sec_attr[1 +6]),
321
22
          file->sec_attr[1+7 +6*4]);
322
22
      sc_file_add_acl_entry(file, SC_AC_OP_DELETE,
323
22
          (int)(*(int8_t*)&file->sec_attr[1 +6]),
324
22
          file->sec_attr[1+7 +6*4]);
325
22
    }
326
133
    if (file->sec_attr[0] & 0x01) /* if AccessMode.0 */
327
55
    {
328
55
      sc_log(card->ctx,  (file->type == SC_FILE_TYPE_DF) ?
329
55
          "SC_AC_OP_CREATE %i %i" : "SC_AC_OP_READ %i %i",
330
55
          (int)(*(int8_t*)&file->sec_attr[1 +0]),
331
55
          file->sec_attr[1+7 +0*4]);
332
55
      sc_file_add_acl_entry(file,
333
55
          (file->type == SC_FILE_TYPE_DF) ?
334
51
          SC_AC_OP_CREATE : SC_AC_OP_READ,
335
55
          (int)(*(int8_t*)&file->sec_attr[1 +0]),
336
55
          file->sec_attr[1+7 +0*4]);
337
55
    }
338
133
    if (file->type == SC_FILE_TYPE_DF)
339
4
    {
340
4
      sc_file_add_acl_entry(file, SC_AC_OP_LIST_FILES,
341
4
          SC_AC_NONE, SC_AC_KEY_REF_NONE);
342
4
    }
343
129
    else
344
129
      if (file->sec_attr[0] & 0x02) /* if AccessMode.1 */
345
25
      {
346
25
        sc_log(card->ctx,  "SC_AC_OP_UPDATE %i %i",
347
25
            (int)(*(int8_t*)&file->sec_attr[1 +1]),
348
25
            file->sec_attr[1+7 +1*4]);
349
25
        sc_file_add_acl_entry(file, SC_AC_OP_UPDATE,
350
25
            (int)(*(int8_t*)&file->sec_attr[1 +1]),
351
25
            file->sec_attr[1+7 +1*4]);
352
25
        sc_log(card->ctx,  "SC_AC_OP_WRITE %i %i",
353
25
            (int)(*(int8_t*)&file->sec_attr[1 +1]),
354
25
            file->sec_attr[1+7 +1*4]);
355
25
        sc_file_add_acl_entry(file, SC_AC_OP_WRITE,
356
25
            (int)(*(int8_t*)&file->sec_attr[1 +1]),
357
25
            file->sec_attr[1+7 +1*4]);
358
25
      }
359
133
  }
360
133
}
361
362
static int rutoken_select_file(sc_card_t *card,
363
      const sc_path_t *in_path, sc_file_t **file_out)
364
631
{
365
631
  sc_apdu_t apdu;
366
631
  u8 buf[SC_MAX_APDU_BUFFER_SIZE], pathbuf[SC_MAX_PATH_SIZE], *path = pathbuf;
367
631
  sc_file_t *file = NULL;
368
631
  size_t pathlen;
369
631
  int ret;
370
371
631
  if (!card || !card->ctx)
372
0
    return SC_ERROR_INTERNAL;
373
631
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
374
375
631
  if (in_path == NULL || sizeof(pathbuf) < in_path->len)
376
0
    return SC_ERROR_INTERNAL;
377
631
  memcpy(path, in_path->value, in_path->len);
378
631
  pathlen = in_path->len;
379
380
  /* p2 = 0; first record, return FCP */
381
631
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0, 0);
382
631
  switch (in_path->type)
383
631
  {
384
0
  case SC_PATH_TYPE_FILE_ID:
385
0
    if (pathlen != 2)
386
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
387
0
    break;
388
468
  case SC_PATH_TYPE_PATH:
389
468
    if (pathlen >= 2 && memcmp(path, "\x3F\x00", 2) == 0)
390
402
    {
391
402
      if (pathlen == 2)
392
11
        break; /* only 3F00 supplied */
393
391
      path += 2;
394
391
      pathlen -= 2;
395
391
    }
396
457
    apdu.p1 = 0x08;
397
457
    break;
398
163
  case SC_PATH_TYPE_DF_NAME:
399
163
  case SC_PATH_TYPE_FROM_CURRENT:
400
163
  case SC_PATH_TYPE_PARENT:
401
163
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
402
0
  default:
403
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
404
631
  }
405
468
  swap_pair(path, pathlen);
406
468
  apdu.lc = pathlen;
407
468
  apdu.data = path;
408
468
  apdu.datalen = pathlen;
409
410
468
  apdu.resp = buf;
411
468
  apdu.resplen = sizeof(buf);
412
468
  apdu.le = 256;
413
414
468
  ret = sc_transmit_apdu(card, &apdu);
415
468
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
416
452
  if (file_out == NULL)
417
169
  {
418
169
    if (apdu.sw1 == 0x61)
419
0
      SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, 0);
420
169
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, sc_check_sw(card, apdu.sw1, apdu.sw2));
421
169
  }
422
283
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
423
283
  LOG_TEST_RET(card->ctx, ret, "");
424
425
189
  if (apdu.resplen > 0 && apdu.resp[0] != 0x62) /* Tag 0x62 - FCP */
426
189
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED);
427
428
170
  file = sc_file_new();
429
170
  if (file == NULL)
430
170
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
431
170
  file->path = *in_path;
432
170
  if (card->ops->process_fci == NULL)
433
0
  {
434
0
    sc_file_free(file);
435
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
436
0
  }
437
170
  if (apdu.resplen > 1  &&  apdu.resplen >= (size_t)apdu.resp[1] + 2)
438
152
  {
439
152
    ret = card->ops->process_fci(card, file, apdu.resp+2, apdu.resp[1]);
440
152
  }
441
170
  if (file->sec_attr && file->sec_attr_len == sizeof(sc_SecAttrV2_t))
442
133
    set_acl_from_sec_attr(card, file);
443
37
  else
444
37
    ret = SC_ERROR_UNKNOWN_DATA_RECEIVED;
445
170
  if (ret != SC_SUCCESS)
446
37
    sc_file_free(file);
447
133
  else
448
133
  {
449
133
    *file_out = file;
450
133
  }
451
170
  LOG_FUNC_RETURN(card->ctx, ret);
452
170
}
453
454
static int rutoken_process_fci(struct sc_card *card, sc_file_t *file,
455
      const unsigned char *buf, size_t buflen)
456
152
{
457
152
  size_t taglen;
458
152
  int ret;
459
152
  const unsigned char *tag;
460
461
152
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
462
152
  ret = iso_ops->process_fci(card, file, buf, buflen);
463
152
  if (ret == SC_SUCCESS)
464
152
  {
465
    /* Rutoken S returns buffers in little-endian. */
466
    /* Set correct file id. */
467
152
    file->id = ((file->id & 0xFF) << 8) | ((file->id >> 8) & 0xFF);
468
152
    sc_log(card->ctx,  "  file identifier: 0x%04X", file->id);
469
    /* Determine file size. */
470
152
    tag = sc_asn1_find_tag(card->ctx, buf, buflen, 0x80, &taglen);
471
    /* Rutoken S always returns 2 bytes. */
472
152
    if (tag != NULL && taglen == 2)
473
13
    {
474
13
      file->size = (tag[1] << 8) | tag[0];
475
13
      sc_log(card->ctx, "  bytes in file: %zu", file->size);
476
13
    }
477
152
  }
478
152
  LOG_FUNC_RETURN(card->ctx, ret);
479
152
}
480
481
static int rutoken_construct_fci(sc_card_t *card, const sc_file_t *file,
482
      u8 *out, size_t *outlen)
483
0
{
484
0
  u8 buf[64], *p = out;
485
486
0
  if (!card || !card->ctx)
487
0
    return SC_ERROR_INTERNAL;
488
0
  LOG_FUNC_CALLED(card->ctx);
489
490
0
  if (!file || !out || !outlen)
491
0
    return SC_ERROR_INTERNAL;
492
0
  if (*outlen < (size_t)(p - out) + 2)
493
0
    return SC_ERROR_INTERNAL;
494
0
  *p++ = 0x62; /* FCP template */
495
0
  p++; /* for length */
496
497
  /* 0x80 - Number of data bytes in the file, excluding structural information */
498
0
  buf[1] = (file->size >> 8) & 0xFF;
499
0
  buf[0] = file->size & 0xFF;
500
0
  sc_asn1_put_tag(0x80, buf, 2, p, *outlen - (p - out), &p);
501
502
  /* 0x82 - File descriptor byte */
503
0
  if (file->type_attr_len)
504
0
  {
505
0
    if (sizeof(buf) < file->type_attr_len)
506
0
      return SC_ERROR_INTERNAL;
507
0
    memcpy(buf, file->type_attr, file->type_attr_len);
508
0
    sc_asn1_put_tag(0x82, buf, file->type_attr_len,
509
0
        p, *outlen - (p - out), &p);
510
0
  }
511
0
  else
512
0
  {
513
0
    switch (file->type)
514
0
    {
515
0
    case SC_FILE_TYPE_WORKING_EF:
516
0
      buf[0] = 0x01;
517
0
      break;
518
0
    case SC_FILE_TYPE_DF:
519
0
      buf[0] = 0x38;
520
0
      break;
521
0
    case SC_FILE_TYPE_INTERNAL_EF:
522
0
    default:
523
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
524
0
    }
525
0
    buf[1] = 0;
526
0
    sc_asn1_put_tag(0x82, buf, 2, p, *outlen - (p - out), &p);
527
0
  }
528
  /* 0x83 - File identifier */
529
0
  buf[1] = (file->id >> 8) & 0xFF;
530
0
  buf[0] = file->id & 0xFF;
531
0
  sc_asn1_put_tag(0x83, buf, 2, p, *outlen - (p - out), &p);
532
533
0
  if (file->prop_attr_len)
534
0
  {
535
0
    if (sizeof(buf) < file->prop_attr_len)
536
0
      return SC_ERROR_INTERNAL;
537
0
    memcpy(buf, file->prop_attr, file->prop_attr_len);
538
0
    sc_asn1_put_tag(0x85, buf, file->prop_attr_len,
539
0
        p, *outlen - (p - out), &p);
540
0
  }
541
0
  if (file->sec_attr_len)
542
0
  {
543
0
    if (sizeof(buf) < file->sec_attr_len)
544
0
      return SC_ERROR_INTERNAL;
545
0
    memcpy(buf, file->sec_attr, file->sec_attr_len);
546
0
    sc_asn1_put_tag(0x86, buf, file->sec_attr_len,
547
0
        p, *outlen - (p - out), &p);
548
0
  }
549
0
  out[1] = p - out - 2; /* length */
550
0
  *outlen = p - out;
551
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, 0);
552
0
}
553
554
static int set_sec_attr_from_acl(sc_card_t *card, sc_file_t *file)
555
0
{
556
0
  const helper_acl_to_sec_attr_t *conv_attr;
557
0
  size_t i, n_conv_attr;
558
0
  const sc_acl_entry_t *entry;
559
0
  sc_SecAttrV2_t attr = { 0 };
560
0
  int ret = SC_SUCCESS;
561
562
0
  LOG_FUNC_CALLED(card->ctx);
563
564
0
  if (file->type == SC_FILE_TYPE_DF)
565
0
  {
566
0
    conv_attr = arr_convert_attr_df;
567
0
    n_conv_attr = sizeof(arr_convert_attr_df)/sizeof(arr_convert_attr_df[0]);
568
0
  }
569
0
  else
570
0
  {
571
0
    conv_attr = arr_convert_attr_ef;
572
0
    n_conv_attr = sizeof(arr_convert_attr_ef)/sizeof(arr_convert_attr_ef[0]);
573
0
  }
574
0
  sc_log(card->ctx,  "file->type = %i", file->type);
575
576
0
  for (i = 0; i < n_conv_attr; ++i)
577
0
  {
578
0
    entry = sc_file_get_acl_entry(file, conv_attr[i].ac_op);
579
0
    if (entry  &&  (entry->method == SC_AC_CHV || entry->method == SC_AC_NONE
580
0
        || entry->method == SC_AC_NEVER)
581
0
    )
582
0
    {
583
      /* AccessMode.[conv_attr[i].sec_attr_pos] */
584
0
      attr[0] |= 1 << conv_attr[i].sec_attr_pos;
585
0
      sc_log(card->ctx, "AccessMode.%zu, attr[0]=0x%x", conv_attr[i].sec_attr_pos, attr[0]);
586
0
      attr[1 + conv_attr[i].sec_attr_pos] = (u8)entry->method;
587
0
      sc_log(card->ctx,  "method %u", (u8)entry->method);
588
0
      if (entry->method == SC_AC_CHV)
589
0
      {
590
0
        attr[1+7 + conv_attr[i].sec_attr_pos*4] = (u8)entry->key_ref;
591
0
        sc_log(card->ctx,  "key_ref %u", (u8)entry->key_ref);
592
0
      }
593
0
    }
594
0
    else
595
0
    {
596
0
      sc_log(card->ctx,  "ACL (%u) not set, set default sec_attr",
597
0
          conv_attr[i].ac_op);
598
0
      memcpy(attr, default_sec_attr, sizeof(attr));
599
0
      break;
600
0
    }
601
0
  }
602
0
  ret = sc_file_set_sec_attr(file, attr, sizeof(attr));
603
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
604
0
}
605
606
static int rutoken_create_file(sc_card_t *card, sc_file_t *file)
607
0
{
608
0
  int ret;
609
610
0
  if (!card || !card->ctx)
611
0
    return SC_ERROR_INTERNAL;
612
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
613
614
0
  if (!file)
615
0
    return SC_ERROR_INTERNAL;
616
0
  if (file->sec_attr_len == 0)
617
0
  {
618
0
    ret = set_sec_attr_from_acl(card, file);
619
0
    LOG_TEST_RET(card->ctx, ret, "Set sec_attr from ACL failed");
620
0
  }
621
0
  if (!iso_ops || !iso_ops->create_file)
622
0
    return SC_ERROR_INTERNAL;
623
0
  ret = iso_ops->create_file(card, file);
624
0
  LOG_FUNC_RETURN(card->ctx, ret);
625
0
}
626
627
static int rutoken_delete_file(sc_card_t *card, const sc_path_t *path)
628
0
{
629
0
  u8 sbuf[2];
630
0
  sc_apdu_t apdu;
631
632
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
633
0
  if (!path || path->type != SC_PATH_TYPE_FILE_ID || (path->len != 0 && path->len != 2))
634
0
  {
635
0
    sc_log(card->ctx,  "File type has to be SC_PATH_TYPE_FILE_ID\n");
636
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
637
0
  }
638
0
  if (path->len == sizeof(sbuf))
639
0
  {
640
0
    sbuf[1] = path->value[0];
641
0
    sbuf[0] = path->value[1];
642
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE4, 0x00, 0x00);
643
0
    apdu.lc = sizeof(sbuf);
644
0
    apdu.datalen = sizeof(sbuf);
645
0
    apdu.data = sbuf;
646
0
  }
647
0
  else /* No file ID given: means currently selected file */
648
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0xE4, 0x00, 0x00);
649
0
  LOG_TEST_RET(card->ctx, sc_transmit_apdu(card, &apdu), "APDU transmit failed");
650
0
  LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2));
651
0
}
652
653
static int rutoken_verify(sc_card_t *card, unsigned int type, int ref_qualifier,
654
      const u8 *data, size_t data_len, int *tries_left)
655
0
{
656
0
  sc_apdu_t apdu;
657
0
  int ret;
658
659
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
660
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0x00, ref_qualifier);
661
0
  ret = sc_transmit_apdu(card, &apdu);
662
0
  if (ret == SC_SUCCESS  &&  ((apdu.sw1 == 0x90 && apdu.sw2 == 0x00)
663
0
        ||  apdu.sw1 == 0x63)
664
0
  )
665
0
  {
666
    /* sw1 == 0x63  -  may be already login with other ref_qualifier
667
     * sw1 == 0x90 && sw2 == 0x00  -  already login with ref_qualifier
668
     */
669
    /* RESET ACCESS RIGHTS */
670
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x40, 0x00, 0x00);
671
0
    apdu.cla = 0x80;
672
0
    ret = sc_transmit_apdu(card, &apdu);
673
0
    LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
674
0
    ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
675
0
    LOG_TEST_RET(card->ctx, ret, "Reset access rights failed");
676
0
  }
677
678
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x20, 0x00, ref_qualifier);
679
0
  apdu.lc = data_len;
680
0
  apdu.datalen = data_len;
681
0
  apdu.data = data;
682
0
  ret = sc_transmit_apdu(card, &apdu);
683
0
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
684
0
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
685
0
  if (ret == SC_ERROR_PIN_CODE_INCORRECT  &&  tries_left)
686
0
  {
687
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0x00, ref_qualifier);
688
0
    ret = sc_transmit_apdu(card, &apdu);
689
0
    LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
690
0
    ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
691
0
    if (ret == SC_ERROR_PIN_CODE_INCORRECT)
692
0
      *tries_left = (int)(apdu.sw2 & 0x0f);
693
0
  }
694
0
  LOG_FUNC_RETURN(card->ctx, ret);
695
0
}
696
697
static int rutoken_logout(sc_card_t *card)
698
0
{
699
0
  sc_apdu_t apdu;
700
0
  sc_path_t path;
701
0
  int ret;
702
703
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
704
0
  sc_format_path("3F00", &path);
705
0
  ret = rutoken_select_file(card, &path, NULL);
706
0
  LOG_TEST_RET(card->ctx, ret, "Select MF failed");
707
708
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x40, 0x00, 0x00);
709
0
  apdu.cla = 0x80;
710
0
  ret = sc_transmit_apdu(card, &apdu);
711
0
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
712
0
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
713
0
  LOG_FUNC_RETURN(card->ctx, ret);
714
0
}
715
716
static int rutoken_change_reference_data(sc_card_t *card, unsigned int type,
717
      int ref_qualifier, const u8 *old, size_t oldlen,
718
      const u8 *newref, size_t newlen, int *tries_left)
719
0
{
720
0
  sc_apdu_t apdu;
721
0
  int ret;
722
723
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
724
0
  if (old && oldlen)
725
0
  {
726
0
    ret = rutoken_verify(card, type, ref_qualifier, old, oldlen, tries_left);
727
0
    LOG_TEST_RET(card->ctx, ret, "Invalid 'old' pass");
728
0
  }
729
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x24, 0x01, ref_qualifier);
730
0
  apdu.lc = newlen;
731
0
  apdu.datalen = newlen;
732
0
  apdu.data = newref;
733
0
  ret = sc_transmit_apdu(card, &apdu);
734
0
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
735
0
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
736
0
  LOG_FUNC_RETURN(card->ctx, ret);
737
0
}
738
739
static int rutoken_reset_retry_counter(sc_card_t *card, unsigned int type,
740
      int ref_qualifier, const u8 *puk, size_t puklen,
741
      const u8 *newref, size_t newlen)
742
0
{
743
#ifdef FORCE_VERIFY_RUTOKEN
744
  int left;
745
#endif
746
0
  sc_apdu_t apdu;
747
0
  int ret;
748
749
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
750
#ifdef FORCE_VERIFY_RUTOKEN
751
  if (puk && puklen)
752
  {
753
    ret = rutoken_verify(card, type, ref_qualifier, puk, puklen, &left);
754
    sc_log(card->ctx,  "Tries left: %i\n", left);
755
    LOG_TEST_RET(card->ctx, ret, "Invalid 'puk' pass");
756
  }
757
#endif
758
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x2c, 0x03, ref_qualifier);
759
0
  ret = sc_transmit_apdu(card, &apdu);
760
0
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
761
0
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
762
0
  LOG_FUNC_RETURN(card->ctx, ret);
763
0
}
764
765
static int rutoken_restore_security_env(sc_card_t *card, int se_num)
766
0
{
767
0
  sc_apdu_t apdu;
768
0
  int ret;
769
770
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
771
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x22, 3, se_num);
772
0
  ret = sc_transmit_apdu(card, &apdu);
773
0
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
774
0
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
775
0
  LOG_FUNC_RETURN(card->ctx, ret);
776
0
}
777
778
static int rutoken_set_security_env(sc_card_t *card,
779
      const sc_security_env_t *env,
780
      int se_num)
781
0
{
782
0
  sc_apdu_t apdu;
783
0
  auth_senv_t *senv;
784
0
  u8 data[3] = { 0x83, 0x01 };
785
0
  int ret;
786
787
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
788
0
  if (!env)
789
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
790
0
  senv = (auth_senv_t*)card->drv_data;
791
0
  if (!senv)
792
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
793
0
  if (env->algorithm != SC_ALGORITHM_GOST)
794
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
795
796
0
  senv->algorithm = SC_ALGORITHM_GOST;
797
0
  if (env->key_ref_len != 1)
798
0
  {
799
0
    sc_log(card->ctx,  "No or invalid key reference\n");
800
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
801
0
  }
802
0
  data[2] = env->key_ref[0];
803
  /*  select component  */
804
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 1, 0);
805
0
  apdu.lc = apdu.datalen = sizeof(data);
806
0
  apdu.data = data;
807
0
  switch (env->operation)
808
0
  {
809
0
    case SC_SEC_OPERATION_AUTHENTICATE:
810
0
      apdu.p2 = 0xA4;
811
0
      break;
812
0
    case SC_SEC_OPERATION_DECIPHER:
813
0
      apdu.p2 = 0xB8;
814
0
      break;
815
0
    case SC_SEC_OPERATION_SIGN:
816
0
      apdu.p2 = 0xAA;
817
0
      break;
818
0
    default:
819
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
820
0
  }
821
  /*  set SE  */
822
0
  ret = sc_transmit_apdu(card, &apdu);
823
0
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
824
0
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
825
0
  LOG_FUNC_RETURN(card->ctx, ret);
826
0
}
827
828
static void rutoken_set_do_hdr(u8 *data, size_t *data_len, sc_DOHdrV2_t *hdr)
829
0
{
830
0
  u8 buf[64], *p = data;
831
832
0
  if (!hdr || !data || !data_len)
833
0
    return;
834
835
  /* 0x80 - Number of data bytes in the file, excluding structural information */
836
0
  buf[1] = (hdr->wDOBodyLen >> 8) & 0xFF;
837
0
  buf[0] = hdr->wDOBodyLen & 0xFF;
838
0
  sc_asn1_put_tag(0x80, buf, 2, p, *data_len - (p - data), &p);
839
840
  /* 0x83 - Type and ID */
841
0
  buf[0] = hdr->OTID.byObjectType;
842
0
  buf[1] = hdr->OTID.byObjectID;
843
0
  sc_asn1_put_tag(0x83, buf, 2, p, *data_len - (p - data), &p);
844
845
  /* 0x85 - Options, Flags and Max count of try */
846
0
  buf[0] = hdr->OP.byObjectOptions;
847
0
  buf[1] = hdr->OP.byObjectFlags;
848
0
  buf[2] = hdr->OP.byObjectTry;
849
0
  sc_asn1_put_tag(0x85, buf, 3, p, *data_len - (p - data), &p);
850
851
0
  static_assert(sizeof(buf) >= sizeof(hdr->SA_V2), "internal error");
852
0
  memcpy(buf, hdr->SA_V2, sizeof(hdr->SA_V2));
853
0
  sc_asn1_put_tag(0x86, buf, sizeof(hdr->SA_V2), p, *data_len - (p - data), &p);
854
855
0
  if (*data_len < (size_t)(p - data))
856
0
    return;
857
0
  *data_len = p - data;
858
0
}
859
860
static int rutoken_key_gen(sc_card_t *card, sc_DOHdrV2_t *pHdr)
861
0
{
862
0
  u8 data[SC_MAX_APDU_BUFFER_SIZE];
863
0
  size_t data_len = sizeof(data);
864
0
  sc_apdu_t apdu;
865
0
  int ret;
866
867
0
  LOG_FUNC_CALLED(card->ctx);
868
0
  if (
869
0
       (pHdr->wDOBodyLen != SC_RUTOKEN_DEF_LEN_DO_GOST) ||
870
0
       (pHdr->OTID.byObjectType != SC_RUTOKEN_TYPE_KEY) ||
871
0
       (pHdr->OP.byObjectFlags & SC_RUTOKEN_FLAGS_COMPACT_DO) ||
872
0
       (pHdr->OP.byObjectFlags & SC_RUTOKEN_FLAGS_FULL_OPEN_DO) ||
873
0
       (pHdr->OTID.byObjectID < SC_RUTOKEN_DO_ALL_MIN_ID) ||
874
0
       (pHdr->OTID.byObjectID > SC_RUTOKEN_DO_NOCHV_MAX_ID_V2)
875
0
  )
876
0
  {
877
0
    ret = SC_ERROR_INVALID_ARGUMENTS;
878
0
  }
879
0
  else
880
0
  {
881
0
    pHdr->OP.byObjectTry = 0;
882
0
    rutoken_set_do_hdr(data, &data_len, pHdr);
883
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xda, 0x01, 0x65);
884
0
    apdu.data = data;
885
0
    apdu.datalen = apdu.lc = data_len;
886
0
    ret = sc_transmit_apdu(card, &apdu);
887
0
    LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
888
0
    ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
889
0
  }
890
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
891
0
}
892
893
static int rutoken_create_do(sc_card_t *card, sc_DO_V2_t * pDO)
894
0
{
895
0
  u8 data[SC_MAX_APDU_BUFFER_SIZE];
896
0
  size_t data_len = sizeof(data);
897
0
  sc_apdu_t apdu;
898
0
  int ret;
899
900
0
  LOG_FUNC_CALLED(card->ctx);
901
0
  if (
902
0
       ((pDO->HDR.OTID.byObjectType & SC_RUTOKEN_TYPE_CHV) &&
903
0
        (pDO->HDR.OTID.byObjectID != SC_RUTOKEN_DEF_ID_GCHV_USER) &&
904
0
        (pDO->HDR.OTID.byObjectID != SC_RUTOKEN_DEF_ID_GCHV_ADMIN)) ||
905
0
       ((pDO->HDR.OTID.byObjectType == SC_RUTOKEN_ALLTYPE_GOST) &&
906
0
        (pDO->HDR.wDOBodyLen != SC_RUTOKEN_DEF_LEN_DO_GOST)) ||
907
0
       ((pDO->HDR.OTID.byObjectType == SC_RUTOKEN_ALLTYPE_SE) &&
908
0
        (pDO->HDR.wDOBodyLen != SC_RUTOKEN_DEF_LEN_DO_SE)) ||
909
0
       (pDO->HDR.OTID.byObjectID < SC_RUTOKEN_DO_ALL_MIN_ID) ||
910
0
       (pDO->HDR.OTID.byObjectID > SC_RUTOKEN_DO_NOCHV_MAX_ID_V2) ||
911
0
       ((pDO->HDR.OP.byObjectFlags & SC_RUTOKEN_FLAGS_COMPACT_DO) &&
912
0
        (pDO->HDR.wDOBodyLen > SC_RUTOKEN_COMPACT_DO_MAX_LEN)) ||
913
0
       (pDO->HDR.wDOBodyLen > SC_RUTOKEN_DO_PART_BODY_LEN)
914
0
     )
915
0
  {
916
0
    ret = SC_ERROR_INVALID_ARGUMENTS;
917
0
  }
918
0
  else
919
0
  {
920
0
    rutoken_set_do_hdr(data, &data_len, &pDO->HDR);
921
0
    if (sizeof(data) < data_len + pDO->HDR.wDOBodyLen + 2)
922
0
      return SC_ERROR_INTERNAL;
923
0
    ret = sc_asn1_put_tag(0xA5, pDO->abyDOBody, pDO->HDR.wDOBodyLen,
924
0
        data + data_len, sizeof(data) - data_len, NULL);
925
0
    if (ret == SC_SUCCESS)
926
0
      data_len += pDO->HDR.wDOBodyLen + 2;
927
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xda, 0x01, 0x62);
928
0
    apdu.data = data;
929
0
    apdu.datalen = apdu.lc = data_len;
930
0
    ret = sc_transmit_apdu(card, &apdu);
931
0
    LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
932
0
    ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
933
0
  }
934
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
935
0
}
936
937
static int rutoken_get_do_info(sc_card_t *card, sc_DO_INFO_t * pInfo)
938
0
{
939
0
  u8 data[1];
940
0
  sc_apdu_t apdu;
941
0
  int ret;
942
943
0
  LOG_FUNC_CALLED(card->ctx);
944
0
  if ((pInfo->SelType != select_first) &&
945
0
      ((pInfo->DoId < SC_RUTOKEN_DO_ALL_MIN_ID) ||
946
0
       (pInfo->DoId > SC_RUTOKEN_DO_NOCHV_MAX_ID_V2)))
947
0
  {
948
0
    ret = SC_ERROR_INVALID_ARGUMENTS;
949
0
  }
950
0
  else
951
0
  {
952
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x30, 0x00, 0x00);
953
0
    apdu.cla = 0x80;
954
0
    apdu.resp = pInfo->pDoData;
955
0
    apdu.resplen = sizeof(pInfo->pDoData);
956
0
    apdu.le = 255;
957
0
    memset(apdu.resp, 0, apdu.resplen);
958
0
    switch(pInfo->SelType)
959
0
    {
960
0
    case select_first:
961
0
      apdu.cse = SC_APDU_CASE_2_SHORT;
962
0
      break;
963
0
    case select_next:
964
0
      apdu.p2 = 0x02;
965
      /* fall through */
966
0
    case select_by_id:
967
0
      data[0] = pInfo->DoId;
968
0
      apdu.data = data;
969
0
      apdu.datalen = sizeof(data);
970
0
      apdu.lc = sizeof(data);
971
0
      break;
972
0
    default:
973
0
      SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
974
0
      break;
975
0
    }
976
0
    ret = sc_transmit_apdu(card, &apdu);
977
0
    LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
978
0
    ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
979
0
  }
980
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
981
0
}
982
983
static int rutoken_delete_do(sc_card_t *card, u8 *pId)
984
0
{
985
0
  u8 data[1];
986
0
  sc_apdu_t apdu;
987
0
  int ret;
988
989
0
  LOG_FUNC_CALLED(card->ctx);
990
0
  if ((*pId < SC_RUTOKEN_DO_ALL_MIN_ID) ||
991
0
      (*pId > SC_RUTOKEN_DO_NOCHV_MAX_ID_V2))
992
0
  {
993
0
    ret = SC_ERROR_INVALID_ARGUMENTS;
994
0
  }
995
0
  else
996
0
  {
997
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xda, 0x01, 0x64);
998
0
    data[0] = *pId;
999
0
    apdu.data = data;
1000
0
    apdu.datalen = sizeof(data);
1001
0
    apdu.lc = sizeof(data);
1002
0
    ret = sc_transmit_apdu(card, &apdu);
1003
0
    LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
1004
0
    ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
1005
0
  }
1006
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
1007
0
}
1008
1009
/*  Both direction GOST cipher  */
1010
1011
static int rutoken_cipher_p(sc_card_t *card, const u8 * crgram, size_t crgram_len,
1012
      u8 * out, size_t outlen, int p1, int p2, int isIV)
1013
0
{
1014
0
  u8 buf[248]; /* 248 (cipher_chunk) <= SC_MAX_APDU_BUFFER_SIZE  */
1015
0
  size_t len, outlen_tail = outlen;
1016
0
  int ret;
1017
0
  sc_apdu_t apdu;
1018
1019
0
  LOG_FUNC_CALLED(card->ctx);
1020
0
  sc_log(card->ctx, ": crgram_len %zu; outlen %zu", crgram_len, outlen);
1021
1022
0
  if (!out)
1023
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
1024
0
  if (crgram_len < 16 || ((crgram_len) % 8))
1025
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_WRONG_LENGTH);
1026
1027
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, p1, p2);
1028
0
  do
1029
0
  {
1030
0
    len = (crgram_len > sizeof(buf)) ? sizeof(buf) : crgram_len;
1031
0
    apdu.lc = len;
1032
0
    apdu.datalen = len;
1033
0
    apdu.data = crgram;
1034
0
    crgram += len;
1035
0
    crgram_len -= len;
1036
1037
0
    apdu.cla = (crgram_len == 0) ? 0x00 : 0x10;
1038
0
    apdu.le = len;
1039
0
    apdu.resplen = len;
1040
0
    apdu.resp = buf;
1041
1042
0
    ret = sc_transmit_apdu(card, &apdu);
1043
0
    if (ret < 0)
1044
0
      break;
1045
0
    ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
1046
0
    if (ret == SC_SUCCESS)
1047
0
    {
1048
0
      if (isIV)
1049
0
      {
1050
0
        if (apdu.resplen < 8) {
1051
0
          ret = SC_ERROR_WRONG_LENGTH;
1052
0
          break;
1053
0
        }
1054
0
        apdu.resp += 8;
1055
0
        apdu.resplen -= 8;
1056
0
        isIV = 0;
1057
0
      }
1058
0
      if (apdu.resplen > outlen_tail)
1059
0
        ret = SC_ERROR_BUFFER_TOO_SMALL;
1060
0
      else
1061
0
      {
1062
0
        memcpy(out, apdu.resp, apdu.resplen);
1063
0
        out += apdu.resplen;
1064
0
        outlen_tail -= apdu.resplen;
1065
0
      }
1066
0
    }
1067
0
  } while (ret == SC_SUCCESS  &&  crgram_len != 0);
1068
0
  sc_mem_clear(buf, sizeof(buf));
1069
0
  sc_log(card->ctx, "len out cipher %zu\n", outlen - outlen_tail);
1070
0
  if (ret == SC_SUCCESS)
1071
0
    ret = (outlen_tail == 0) ? (int)outlen : SC_ERROR_WRONG_LENGTH;
1072
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
1073
0
}
1074
1075
/*  Launcher for cipher  */
1076
1077
static int rutoken_cipher_gost(sc_card_t *card,
1078
      struct sc_rutoken_decipherinfo *ptr, char is_encipher)
1079
0
{
1080
0
  int ret;
1081
1082
0
  if (is_encipher)
1083
0
    ret = rutoken_cipher_p(card, ptr->inbuf, ptr->inlen,
1084
0
        ptr->outbuf, ptr->outlen, 0x86, 0x80, 0);
1085
0
  else
1086
0
    ret = rutoken_cipher_p(card, ptr->inbuf, ptr->inlen,
1087
0
        ptr->outbuf, ptr->outlen, 0x80, 0x86, 1);
1088
0
  if (ret > 0)
1089
0
  {
1090
0
    if ((size_t)ret == ptr->outlen)
1091
0
      ret = SC_SUCCESS;
1092
0
    else
1093
0
      ret = SC_ERROR_INTERNAL; /* SC_ERROR_DECRYPT_FAILED; */
1094
0
  }
1095
0
  return ret;
1096
1097
0
}
1098
1099
static int rutoken_compute_mac_gost(sc_card_t *card,
1100
      const u8 *in, size_t ilen,
1101
      u8 *out, size_t olen)
1102
0
{
1103
0
  const size_t signing_chunk = 248;
1104
0
  size_t len;
1105
0
  int ret;
1106
0
  sc_apdu_t apdu;
1107
1108
0
  LOG_FUNC_CALLED(card->ctx);
1109
0
  if (!in || !out || olen != 4 || ilen == 0)
1110
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
1111
0
  do
1112
0
  {
1113
0
    sc_format_apdu(card, &apdu,
1114
0
        ilen > signing_chunk ?
1115
0
        SC_APDU_CASE_3_SHORT : SC_APDU_CASE_4_SHORT,
1116
0
        0x2A, 0x90, 0x80);
1117
0
    len = (ilen > signing_chunk) ? signing_chunk : ilen;
1118
0
    apdu.lc = len;
1119
0
    apdu.datalen = len;
1120
0
    apdu.data = in;
1121
0
    in += len;
1122
0
    ilen -= len;
1123
0
    if (ilen == 0)
1124
0
    {
1125
0
      apdu.cla = 0x00;
1126
0
      apdu.le = olen;
1127
0
      apdu.resplen = olen;
1128
0
      apdu.resp = out;
1129
0
    }
1130
0
    else
1131
0
      apdu.cla = 0x10;
1132
0
    ret = sc_transmit_apdu(card, &apdu);
1133
0
    LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
1134
0
    ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
1135
0
  } while (ret == SC_SUCCESS  &&  ilen != 0);
1136
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
1137
0
}
1138
1139
static int rutoken_compute_signature(struct sc_card *card,
1140
      const u8 * data, size_t datalen,
1141
      u8 * out, size_t outlen)
1142
0
{
1143
0
  int ret;
1144
0
  auth_senv_t *senv = (auth_senv_t *)card->drv_data;
1145
1146
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1147
0
  if (!senv)
1148
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
1149
1150
0
  if (senv->algorithm == SC_ALGORITHM_GOST)
1151
0
    ret = rutoken_compute_mac_gost(card, data, datalen, out, outlen);
1152
0
  else
1153
0
    ret = SC_ERROR_NOT_SUPPORTED;
1154
0
  LOG_FUNC_RETURN(card->ctx, ret);
1155
0
}
1156
1157
static int rutoken_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
1158
102
{
1159
102
  unsigned char rbuf[32];
1160
102
  size_t out_len;
1161
102
  int r;
1162
1163
102
  LOG_FUNC_CALLED(card->ctx);
1164
1165
102
  r = iso_ops->get_challenge(card, rbuf, sizeof rbuf);
1166
102
  LOG_TEST_RET(card->ctx, r, "GET CHALLENGE cmd failed");
1167
1168
90
  if (len < (size_t) r) {
1169
1
    out_len = len;
1170
89
  } else {
1171
89
    out_len = (size_t) r;
1172
89
  }
1173
90
  memcpy(rnd, rbuf, out_len);
1174
1175
90
  LOG_FUNC_RETURN(card->ctx, (int)out_len);
1176
90
}
1177
1178
static int rutoken_get_serial(sc_card_t *card, sc_serial_number_t *serial)
1179
0
{
1180
0
  sc_apdu_t apdu;
1181
0
  int ret;
1182
1183
0
  LOG_FUNC_CALLED(card->ctx);
1184
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xCA, 0x01, 0x81);
1185
0
  apdu.resp = serial->value;
1186
0
  apdu.resplen = sizeof(serial->value);
1187
0
  apdu.le = 4;
1188
0
  ret = sc_transmit_apdu(card, &apdu);
1189
0
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
1190
0
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
1191
0
  serial->len = apdu.resplen;
1192
0
  swap_four(serial->value, serial->len);
1193
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
1194
0
}
1195
1196
static int rutoken_get_info(sc_card_t *card, void *buff)
1197
0
{
1198
0
  sc_apdu_t apdu;
1199
0
  u8 rbuf[8];
1200
0
  int ret;
1201
1202
0
  LOG_FUNC_CALLED(card->ctx);
1203
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xCA, 0x01, 0x89);
1204
0
  apdu.resp = rbuf;
1205
0
  apdu.resplen = sizeof(rbuf);
1206
0
  apdu.le = sizeof(rbuf);
1207
0
  ret = sc_transmit_apdu(card, &apdu);
1208
0
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
1209
0
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
1210
0
  if (ret == SC_SUCCESS)
1211
0
    memcpy(buff, apdu.resp, apdu.resplen);
1212
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
1213
0
}
1214
1215
static int rutoken_format(sc_card_t *card, int apdu_ins)
1216
0
{
1217
0
  int ret;
1218
0
  sc_apdu_t apdu;
1219
1220
0
  LOG_FUNC_CALLED(card->ctx);
1221
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, apdu_ins, 0x00, 0x00);
1222
0
  apdu.cla = 0x80;
1223
0
  ret = sc_transmit_apdu(card, &apdu);
1224
0
  LOG_TEST_RET(card->ctx, ret, "APDU transmit failed");
1225
0
  ret = sc_check_sw(card, apdu.sw1, apdu.sw2);
1226
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, ret);
1227
0
}
1228
1229
static int rutoken_card_ctl(sc_card_t *card, unsigned long cmd, void *ptr)
1230
31
{
1231
31
  int ret = (ptr != NULL
1232
      /*|| cmd == SC_CARDCTL_ERASE_CARD */
1233
0
      || cmd == SC_CARDCTL_RUTOKEN_FORMAT_INIT
1234
0
      || cmd == SC_CARDCTL_RUTOKEN_FORMAT_END
1235
31
    ) ? SC_SUCCESS : SC_ERROR_INVALID_ARGUMENTS;
1236
1237
31
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1238
1239
31
  if (ret == SC_SUCCESS)
1240
31
  {
1241
31
    switch (cmd)
1242
31
    {
1243
0
    case SC_CARDCTL_RUTOKEN_CREATE_DO:
1244
0
      ret = rutoken_create_do(card, ptr);
1245
0
      break;
1246
0
    case SC_CARDCTL_RUTOKEN_GENERATE_KEY_DO:
1247
0
      ret = rutoken_key_gen(card, ptr);
1248
0
      break;
1249
0
    case SC_CARDCTL_RUTOKEN_DELETE_DO:
1250
0
      ret = rutoken_delete_do(card, ptr);
1251
0
      break;
1252
0
    case SC_CARDCTL_RUTOKEN_GET_DO_INFO:
1253
0
      ret = rutoken_get_do_info(card, ptr);
1254
0
      break;
1255
0
    case SC_CARDCTL_GET_SERIALNR:
1256
0
      ret = rutoken_get_serial(card, ptr);
1257
0
      break;
1258
0
    case SC_CARDCTL_RUTOKEN_CHANGE_DO:
1259
0
      ret = SC_ERROR_NOT_SUPPORTED;
1260
0
      break;
1261
0
    case SC_CARDCTL_RUTOKEN_GET_INFO:
1262
0
      ret = rutoken_get_info(card, ptr);
1263
0
      break;
1264
0
    case SC_CARDCTL_RUTOKEN_GOST_ENCIPHER:
1265
0
      ret = rutoken_cipher_gost(card, ptr, 1);
1266
0
      break;
1267
0
    case SC_CARDCTL_RUTOKEN_GOST_DECIPHER:
1268
0
      ret = rutoken_cipher_gost(card, ptr, 0);
1269
0
      break;
1270
    /* case SC_CARDCTL_ERASE_CARD: */
1271
0
    case SC_CARDCTL_RUTOKEN_FORMAT_INIT:
1272
      /* ret = rutoken_format(card, 0x7a); *//*  APDU: INIT RUTOKEN */
1273
0
      ret = rutoken_format(card, 0x8a); /* APDU: NEW INIT RUTOKEN */
1274
0
      break;
1275
0
    case SC_CARDCTL_RUTOKEN_FORMAT_END:
1276
0
      ret = rutoken_format(card, 0x7b); /* APDU: FORMAT END */
1277
0
      break;
1278
31
    default:
1279
31
      sc_log(card->ctx,  "cmd = %lu", cmd);
1280
31
      ret = SC_ERROR_NOT_SUPPORTED;
1281
31
      break;
1282
31
    }
1283
31
  }
1284
31
  LOG_FUNC_RETURN(card->ctx, ret);
1285
31
}
1286
1287
static struct sc_card_driver* get_rutoken_driver(void)
1288
15.8k
{
1289
15.8k
  if (iso_ops == NULL)
1290
1
    iso_ops = sc_get_iso7816_driver()->ops;
1291
15.8k
  rutoken_ops = *iso_ops;
1292
1293
15.8k
  rutoken_ops.match_card = rutoken_match_card;
1294
15.8k
  rutoken_ops.init = rutoken_init;
1295
15.8k
  rutoken_ops.finish = rutoken_finish;
1296
  /* read_binary */
1297
15.8k
  rutoken_ops.write_binary = NULL;
1298
  /* update_binary */
1299
15.8k
  rutoken_ops.read_record = NULL;
1300
15.8k
  rutoken_ops.write_record = NULL;
1301
15.8k
  rutoken_ops.append_record = NULL;
1302
15.8k
  rutoken_ops.update_record = NULL;
1303
15.8k
  rutoken_ops.select_file = rutoken_select_file;
1304
15.8k
  rutoken_ops.get_response = NULL;
1305
15.8k
  rutoken_ops.get_challenge = rutoken_get_challenge;
1306
15.8k
  rutoken_ops.verify = rutoken_verify;
1307
15.8k
  rutoken_ops.logout = rutoken_logout;
1308
15.8k
  rutoken_ops.restore_security_env = rutoken_restore_security_env;
1309
15.8k
  rutoken_ops.set_security_env = rutoken_set_security_env;
1310
15.8k
  rutoken_ops.decipher = NULL;
1311
15.8k
  rutoken_ops.compute_signature = rutoken_compute_signature;
1312
15.8k
  rutoken_ops.change_reference_data = rutoken_change_reference_data;
1313
15.8k
  rutoken_ops.reset_retry_counter = rutoken_reset_retry_counter;
1314
15.8k
  rutoken_ops.create_file = rutoken_create_file;
1315
15.8k
  rutoken_ops.delete_file = rutoken_delete_file;
1316
15.8k
  rutoken_ops.list_files = rutoken_list_files;
1317
15.8k
  rutoken_ops.check_sw = rutoken_check_sw;
1318
15.8k
  rutoken_ops.card_ctl = rutoken_card_ctl;
1319
15.8k
  rutoken_ops.process_fci = rutoken_process_fci;
1320
15.8k
  rutoken_ops.construct_fci = rutoken_construct_fci;
1321
15.8k
  rutoken_ops.pin_cmd = NULL;
1322
1323
15.8k
  return &rutoken_drv;
1324
15.8k
}
1325
1326
struct sc_card_driver * sc_get_rutoken_driver(void)
1327
15.8k
{
1328
15.8k
  return get_rutoken_driver();
1329
15.8k
}
1330