Coverage Report

Created: 2026-08-31 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/pkcs15init/pkcs15-entersafe.c
Line
Count
Source
1
/*
2
 * This library is free software; you can redistribute it and/or
3
 * modify it under the terms of the GNU Lesser General Public
4
 * License as published by the Free Software Foundation; either
5
 * version 2.1 of the License, or (at your option) any later version.
6
 *
7
 * This library is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10
 * Lesser General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU Lesser General Public
13
 * License along with this library; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
 */
16
/* Initially written by Weitao Sun (weitao@ftsafe.com) 2008*/
17
/* Disable RSA:512bits by Shengchao Niu (shengchao@ftsafe.com) 2012 */
18
19
#include "config.h"
20
21
#include <sys/types.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <assert.h>
25
#include <stdarg.h>
26
27
#include "libopensc/log.h"
28
#include "libopensc/opensc.h"
29
#include "libopensc/cardctl.h"
30
#include "pkcs15-init.h"
31
#include "profile.h"
32
33
static u8 process_acl_entry(sc_file_t *in, unsigned int method, unsigned int in_def)
34
0
{
35
0
  u8 def = (u8)in_def;
36
0
  const sc_acl_entry_t *entry = sc_file_get_acl_entry(in, method);
37
0
  if (!entry)
38
0
  {
39
0
    return def;
40
0
  }
41
0
  else if (entry->method == SC_AC_CHV)
42
0
  {
43
0
    unsigned int key_ref = entry->key_ref;
44
0
    if (key_ref == SC_AC_KEY_REF_NONE)
45
0
      return def;
46
0
    else
47
0
      return ENTERSAFE_AC_ALWAYS&0x04;
48
0
  }
49
0
  else if (entry->method == SC_AC_SYMBOLIC)
50
0
  {
51
0
     return ENTERSAFE_AC_ALWAYS&0x04;
52
0
  }
53
0
  else if (entry->method == SC_AC_NEVER)
54
0
  {
55
0
    return ENTERSAFE_AC_NEVER;
56
0
  }
57
0
  else
58
0
  {
59
0
    return def;
60
0
  }
61
0
}
62
63
static int entersafe_erase_card(struct sc_profile *profile, sc_pkcs15_card_t *p15card)
64
107
{
65
107
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE);
66
67
107
  if (sc_select_file(p15card->card, sc_get_mf_path(), NULL) < 0)
68
100
    return SC_SUCCESS;
69
70
7
  return sc_card_ctl(p15card->card,SC_CARDCTL_ERASE_CARD,0);
71
107
}
72
73
static int entersafe_init_card(sc_profile_t *profile, sc_pkcs15_card_t *p15card)
74
90
{
75
90
  struct sc_card *card = p15card->card;
76
90
  int ret;
77
78
90
  {/* MF */
79
90
     sc_file_t *mf_file;
80
90
     sc_entersafe_create_data mf_data = {0};
81
82
90
     SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
83
84
90
     ret = sc_profile_get_file(profile, "MF", &mf_file);
85
90
     LOG_TEST_RET(card->ctx,ret,"Get MF info failed");
86
87
90
     mf_data.type = SC_ENTERSAFE_MF_DATA;
88
90
     mf_data.data.df.file_id[0]=0x3F;
89
90
     mf_data.data.df.file_id[1]=0x00;
90
90
     mf_data.data.df.file_count=0x04;
91
90
     mf_data.data.df.flag=0x11;
92
90
     mf_data.data.df.ikf_size[0]=(mf_file->size>>8)&0xFF;
93
90
     mf_data.data.df.ikf_size[1]=mf_file->size&0xFF;
94
90
     mf_data.data.df.create_ac=0x10;
95
90
     mf_data.data.df.append_ac=0xC0;
96
90
     mf_data.data.df.lock_ac=0x10;
97
90
     memcpy(mf_data.data.df.aid,mf_file->name,mf_file->namelen);
98
90
     sc_file_free(mf_file);
99
100
90
     ret = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_CREATE_FILE, &mf_data);
101
90
     LOG_TEST_RET(card->ctx,ret,"Create MF failed");
102
90
  }
103
104
69
  {/* EF(DIR) */
105
69
     sc_file_t *dir_file;
106
69
     size_t fid,size;
107
69
     sc_entersafe_create_data ef_data;
108
69
     u8 *buff=0;
109
110
     /* get dir profile */
111
69
     ret = sc_profile_get_file(profile, "dir", &dir_file);
112
69
     LOG_TEST_RET(card->ctx,ret,"Get EF(DIR) info failed");
113
68
     fid=dir_file->id;
114
68
     size=dir_file->size;
115
68
     sc_file_free(dir_file);
116
117
68
     ef_data.type=SC_ENTERSAFE_EF_DATA;
118
68
     ef_data.data.ef.file_id[0]=(fid>>8)&0xFF;
119
68
     ef_data.data.ef.file_id[1]=fid&0xFF;
120
68
     ef_data.data.ef.size[0]=(size>>8)&0xFF;
121
68
     ef_data.data.ef.size[1]=size&0xFF;
122
68
     ef_data.data.ef.attr[0]=0x00;
123
68
     ef_data.data.ef.attr[1]=0x00;
124
68
     ef_data.data.ef.name=0x00;
125
68
     memset(ef_data.data.ef.ac,0x10,sizeof(ef_data.data.ef.ac));
126
68
     memset(ef_data.data.ef.sm,0x00,sizeof(ef_data.data.ef.sm));
127
128
68
     ret = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_CREATE_FILE, &ef_data);
129
68
     LOG_TEST_RET(card->ctx,ret,"Create EF(DIR) failed");
130
131
132
     /* fill file by 0 */
133
65
     if (size > MAX_FILE_SIZE)
134
65
       LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_DATA, "Initialize EF(DIR) failed with file size too large");
135
58
     buff = calloc(1,size);
136
58
     if(!buff)
137
0
        SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,SC_SUCCESS);
138
58
     memset(buff,0,size);
139
140
58
     ret = sc_update_binary(card,0,buff,size,0);
141
58
     free(buff);
142
58
     LOG_TEST_RET(card->ctx,ret,"Initialize EF(DIR) failed");
143
58
  }
144
145
15
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,SC_SUCCESS);
146
147
15
}
148
149
static int entersafe_create_dir(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
150
                sc_file_t *df)
151
15
{
152
15
  struct sc_card *card = p15card->card;
153
15
  int             ret;
154
155
15
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
156
157
15
  {/* df */
158
15
     sc_entersafe_create_data df_data;
159
160
15
     df_data.type = SC_ENTERSAFE_DF_DATA;
161
15
     df_data.data.df.file_id[0]=(df->id >> 8) & 0xFF;
162
15
     df_data.data.df.file_id[1]=df->id & 0xFF;
163
15
     df_data.data.df.file_count=0x30;
164
15
     df_data.data.df.flag=0x01;
165
15
     df_data.data.df.ikf_size[0]=(df->size>>8)&0xFF;
166
15
     df_data.data.df.ikf_size[1]=df->size&0xFF;
167
15
     df_data.data.df.create_ac=0x10;
168
15
     df_data.data.df.append_ac=0xC0;
169
15
     df_data.data.df.lock_ac=0x10;
170
15
     memcpy(df_data.data.df.aid,df->name,df->namelen);
171
172
15
     ret = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_CREATE_FILE, &df_data);
173
15
     LOG_TEST_RET(card->ctx,ret,"Create DF failed");
174
15
  }
175
176
4
  {/* GPKF */
177
4
     sc_file_t *gpkf_file;
178
4
     sc_entersafe_create_data ef_data;
179
180
     /* get p15_gpkf profile */
181
4
     ret = sc_profile_get_file(profile, "p15_gpkf", &gpkf_file);
182
4
     LOG_TEST_RET(card->ctx,ret,"Get GPKF info failed");
183
184
0
     ef_data.type=SC_ENTERSAFE_EF_DATA;
185
0
     ef_data.data.ef.file_id[0]=(gpkf_file->id>>8)&0xFF;
186
0
     ef_data.data.ef.file_id[1]=gpkf_file->id&0xFF;
187
0
     ef_data.data.ef.size[0]=(gpkf_file->size>>8)&0xFF;
188
0
     ef_data.data.ef.size[1]=gpkf_file->size&0xFF;
189
0
     ef_data.data.ef.attr[0]=0x15;
190
0
     ef_data.data.ef.attr[1]=0x80;
191
0
     ef_data.data.ef.name=0x00;
192
0
     memset(ef_data.data.ef.ac,0x10,sizeof(ef_data.data.ef.ac));
193
0
     memset(ef_data.data.ef.sm,0x00,sizeof(ef_data.data.ef.sm));
194
195
0
     sc_file_free(gpkf_file);
196
197
0
     ret = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_CREATE_FILE, &ef_data);
198
0
     LOG_TEST_RET(card->ctx,ret,"Create GPKF failed");
199
0
  }
200
201
0
  {/* p15 efs */
202
0
     const char * create_efs[]={
203
0
        "PKCS15-ODF",
204
0
        "PKCS15-TokenInfo",
205
0
        "PKCS15-UnusedSpace",
206
0
        "PKCS15-AODF",
207
0
        "PKCS15-PrKDF",
208
0
        "PKCS15-PuKDF",
209
0
        "PKCS15-CDF",
210
0
        "PKCS15-DODF",
211
0
        NULL,
212
0
     };
213
0
     int i;
214
0
     sc_file_t *file=0;
215
0
     sc_entersafe_create_data tmp;
216
217
0
     for(i = 0; create_efs[i]; ++i)   {
218
0
        if (sc_profile_get_file(profile, create_efs[i], &file))   {
219
0
           sc_log(card->ctx,  "Inconsistent profile: cannot find %s", create_efs[i]);
220
0
           SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,SC_ERROR_INCONSISTENT_PROFILE);
221
0
        }
222
223
0
        tmp.type=SC_ENTERSAFE_EF_DATA;
224
0
        tmp.data.ef.file_id[0]=(file->id>>8)&0xFF;
225
0
        tmp.data.ef.file_id[1]=file->id&0xFF;
226
0
        tmp.data.ef.size[0]=(file->size>>8)&0xFF;
227
0
        tmp.data.ef.size[1]=file->size&0xFF;
228
0
        tmp.data.ef.attr[0]=0x00;
229
0
        tmp.data.ef.attr[1]=0x00;
230
0
        tmp.data.ef.name=0x00;
231
0
        memset(tmp.data.ef.ac,ENTERSAFE_AC_ALWAYS,sizeof(tmp.data.ef.ac));
232
0
        tmp.data.ef.ac[0]=process_acl_entry(file,SC_AC_OP_READ,ENTERSAFE_AC_ALWAYS); /* read */
233
0
        tmp.data.ef.ac[1]=process_acl_entry(file,SC_AC_OP_UPDATE,ENTERSAFE_AC_ALWAYS); /* update */
234
0
        memset(tmp.data.ef.sm,0x00,sizeof(tmp.data.ef.sm));
235
236
0
        sc_file_free(file);
237
238
0
        ret = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_CREATE_FILE, &tmp);
239
0
        LOG_TEST_RET(card->ctx,ret,"Create pkcs15 file failed");
240
0
     }
241
0
  }
242
243
0
  {/* Preinstall keys */
244
0
     ret = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_PREINSTALL_KEYS, 0);
245
0
     LOG_TEST_RET(card->ctx,ret,"Preinstall keys failed");
246
0
  }
247
248
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,ret);
249
0
}
250
251
static int entersafe_pin_reference(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
252
                   sc_pkcs15_auth_info_t *auth_info)
253
223
{
254
223
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE);
255
256
223
  if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
257
0
    return SC_ERROR_OBJECT_NOT_VALID;
258
259
223
  if (auth_info->attrs.pin.reference < ENTERSAFE_USER_PIN_ID)
260
198
     auth_info->attrs.pin.reference = ENTERSAFE_USER_PIN_ID;
261
223
  if (auth_info->attrs.pin.reference > ENTERSAFE_USER_PIN_ID)
262
25
     return SC_ERROR_TOO_MANY_OBJECTS;
263
264
198
  SC_FUNC_RETURN(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE,SC_SUCCESS);
265
198
}
266
267
static int entersafe_create_pin(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
268
                sc_file_t *df, sc_pkcs15_object_t *pin_obj,
269
                const unsigned char *pin, size_t pin_len,
270
                const unsigned char *puk, size_t puk_len)
271
106
{
272
106
  struct sc_card *card = p15card->card;
273
106
  int r;
274
106
  sc_pkcs15_auth_info_t *auth_info = (sc_pkcs15_auth_info_t *) pin_obj->data;
275
276
106
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
277
278
106
  if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
279
0
    return SC_ERROR_OBJECT_NOT_VALID;
280
281
106
  {/*pin*/
282
106
     sc_entersafe_wkey_data  data;
283
284
106
     if (!pin || !pin_len || pin_len > 16)
285
0
        return SC_ERROR_INVALID_ARGUMENTS;
286
287
106
     data.key_id = auth_info->attrs.pin.reference;
288
106
     data.usage=0x0B;
289
106
     data.key_data.symmetric.EC=0x33;
290
106
     data.key_data.symmetric.ver=0x00;
291
     /* pad pin with 0 */
292
106
     memset(data.key_data.symmetric.key_val, 0, sizeof(data.key_data.symmetric.key_val));
293
106
     memcpy(data.key_data.symmetric.key_val, pin, pin_len);
294
106
     data.key_data.symmetric.key_len=16;
295
296
106
     r = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_WRITE_KEY, &data);
297
106
     if (r < 0)
298
89
       SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
299
300
     /* Cache new PIN value. */
301
17
     sc_pkcs15_pincache_add(p15card, pin_obj, pin, pin_len);
302
17
  }
303
304
0
  {/*puk*/
305
17
     sc_entersafe_wkey_data  data;
306
307
17
     if (!puk || !puk_len || puk_len > 16)
308
0
        return SC_ERROR_INVALID_ARGUMENTS;
309
310
17
     data.key_id = auth_info->attrs.pin.reference+1;
311
17
     data.usage=0x0B;
312
17
     data.key_data.symmetric.EC=0x33;
313
17
     data.key_data.symmetric.ver=0x00;
314
     /* pad pin with 0 */
315
17
     memset(data.key_data.symmetric.key_val, 0, sizeof(data.key_data.symmetric.key_val));
316
17
     memcpy(data.key_data.symmetric.key_val, puk, puk_len);
317
17
     data.key_data.symmetric.key_len=16;
318
319
17
     r = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_WRITE_KEY, &data);
320
17
  }
321
322
323
17
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
324
17
}
325
326
static int entersafe_key_reference(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
327
                   sc_pkcs15_prkey_info_t *prkey)
328
107
{
329
107
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE);
330
107
  if (prkey->key_reference < ENTERSAFE_MIN_KEY_ID)
331
107
    prkey->key_reference = ENTERSAFE_MIN_KEY_ID;
332
107
  if (prkey->key_reference > ENTERSAFE_MAX_KEY_ID)
333
0
    return SC_ERROR_TOO_MANY_OBJECTS;
334
107
  SC_FUNC_RETURN(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE,SC_SUCCESS);
335
107
}
336
337
static int entersafe_create_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
338
                sc_pkcs15_object_t *obj)
339
107
{
340
107
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE);
341
107
  SC_FUNC_RETURN(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE,SC_SUCCESS);
342
107
}
343
344
static int entersafe_store_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
345
                 sc_pkcs15_object_t *obj, sc_pkcs15_prkey_t *key)
346
0
{
347
0
  sc_pkcs15_prkey_info_t *kinfo = (sc_pkcs15_prkey_info_t *) obj->data;
348
0
  sc_card_t *card = p15card->card;
349
0
  sc_entersafe_wkey_data data;
350
0
  sc_file_t              *tfile;
351
0
  const sc_acl_entry_t   *acl_entry;
352
0
  int r;
353
354
0
  struct sc_pkcs15_prkey_info *key_info = (struct sc_pkcs15_prkey_info *)obj->data;
355
0
  size_t keybits = key_info->modulus_length;
356
357
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
358
359
0
  if ( key->algorithm != SC_ALGORITHM_RSA )
360
0
  {
361
     /* ignore non-RSA keys */
362
0
     SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,SC_ERROR_INVALID_ARGUMENTS);
363
0
  }
364
365
  /* Disable RSA:512bits */
366
0
  if ( ( keybits < 1024 ) ||
367
0
     ( keybits > 2048 ) ||
368
0
     ( keybits % 0x20 ) )
369
0
  {
370
0
    sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Unsupported key size %zu", keybits);
371
0
    return SC_ERROR_INVALID_ARGUMENTS;
372
0
  }
373
374
0
  r = sc_profile_get_file(profile, "PKCS15-AODF", &tfile);
375
0
  if (r < 0)
376
0
     return r;
377
0
  acl_entry = sc_file_get_acl_entry(tfile, SC_AC_OP_UPDATE);
378
0
  if (acl_entry->method  != SC_AC_NONE) {
379
0
     r = sc_pkcs15init_authenticate(profile, p15card, tfile, SC_AC_OP_UPDATE);
380
0
     if(r<0)
381
0
        r = SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
382
0
  }
383
0
  sc_file_free(tfile);
384
0
  LOG_TEST_RET(card->ctx, r, "can't verify pin");
385
386
0
  data.key_id = (u8) kinfo->key_reference;
387
0
  data.usage=0x22;
388
0
  data.key_data.rsa=&key->u.rsa;
389
0
  return sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_WRITE_KEY, &data);
390
0
}
391
392
static int entersafe_generate_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
393
                  sc_pkcs15_object_t *obj, sc_pkcs15_pubkey_t *pubkey)
394
107
{
395
107
  int r;
396
107
  sc_entersafe_gen_key_data gendat;
397
107
  sc_pkcs15_prkey_info_t *kinfo = (sc_pkcs15_prkey_info_t *) obj->data;
398
107
  sc_card_t *card = p15card->card;
399
107
  sc_file_t              *tfile;
400
107
  const sc_acl_entry_t   *acl_entry;
401
402
107
  struct sc_pkcs15_prkey_info *key_info = (struct sc_pkcs15_prkey_info *)obj->data;
403
107
  size_t keybits = key_info->modulus_length;
404
405
107
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
406
407
107
  if ( obj->type != SC_PKCS15_TYPE_PRKEY_RSA )
408
0
  {
409
0
    return SC_ERROR_NOT_SUPPORTED;
410
0
  }
411
412
  /* Disable RSA:512bits */
413
107
  if ( ( keybits < 1024 ) ||
414
107
     ( keybits > 2048 ) ||
415
107
     ( keybits % 0x20 ) )
416
0
  {
417
0
    sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Unsupported key size %zu", keybits);
418
0
    return SC_ERROR_INVALID_ARGUMENTS;
419
0
  }
420
421
107
  r = sc_profile_get_file(profile, "PKCS15-AODF", &tfile);
422
107
  if (r < 0)
423
66
     return r;
424
41
  acl_entry = sc_file_get_acl_entry(tfile, SC_AC_OP_UPDATE);
425
41
  if (acl_entry->method  != SC_AC_NONE) {
426
2
     r = sc_pkcs15init_authenticate(profile, p15card, tfile, SC_AC_OP_UPDATE);
427
2
     if(r<0)
428
1
        r = SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
429
2
  }
430
41
  sc_file_free(tfile);
431
41
  LOG_TEST_RET(card->ctx, r, "can't verify pin");
432
433
  /* generate key pair */
434
40
  gendat.key_id     = (u8) kinfo->key_reference;
435
40
  gendat.key_length = (size_t) kinfo->modulus_length;
436
40
  gendat.modulus    = NULL;
437
40
  r = sc_card_ctl(card, SC_CARDCTL_ENTERSAFE_GENERATE_KEY, &gendat);
438
40
  LOG_TEST_RET(card->ctx, r, "EnterSafe generate RSA key pair failed");
439
440
  /* get the modulus via READ PUBLIC KEY */
441
1
  if (pubkey) {
442
1
    u8 *buf;
443
1
    struct sc_pkcs15_pubkey_rsa *rsa = &pubkey->u.rsa;
444
    /* set the modulus */
445
1
    rsa->modulus.data = gendat.modulus;
446
1
    rsa->modulus.len  = kinfo->modulus_length >> 3;
447
    /* set the exponent (always 0x10001) */
448
1
    buf = malloc(3);
449
1
    if (!buf)
450
0
      return SC_ERROR_OUT_OF_MEMORY;
451
1
    buf[0] = 0x01;
452
1
    buf[1] = 0x00;
453
1
    buf[2] = 0x01;
454
1
    rsa->exponent.data = buf;
455
1
    rsa->exponent.len  = 3;
456
457
1
    pubkey->algorithm = SC_ALGORITHM_RSA;
458
1
  } else
459
    /* free public key */
460
0
    free(gendat.modulus);
461
462
1
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,SC_SUCCESS);
463
1
}
464
465
466
static int entersafe_sanity_check(sc_profile_t *profile, sc_pkcs15_card_t *p15card)
467
107
{
468
107
  struct sc_context *ctx = p15card->card->ctx;
469
107
  struct sc_pkcs15_auth_info profile_auth = {0};
470
107
  struct sc_pkcs15_object *objs[32];
471
107
  int rv, nn, ii, update_df = 0;
472
473
107
  SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
474
475
107
  sc_log(ctx,  "Check and if needed update PinFlags");
476
107
  rv = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, objs, 32);
477
107
  LOG_TEST_RET(ctx, rv, "Failed to get PINs");
478
107
  nn = rv;
479
480
107
  sc_profile_get_pin_info(profile, SC_PKCS15INIT_USER_PIN, &profile_auth);
481
107
  LOG_TEST_RET(ctx, rv, "Failed to get PIN info");
482
483
107
  for (ii=0; ii<nn; ii++) {
484
0
    struct sc_pkcs15_auth_info *ainfo = (struct sc_pkcs15_auth_info *) objs[ii]->data;
485
0
    struct sc_pkcs15_pin_attributes *pin_attrs = &ainfo->attrs.pin;
486
487
0
    if (ainfo->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
488
0
      continue;
489
490
0
    if (pin_attrs->reference == profile_auth.attrs.pin.reference
491
0
        && pin_attrs->flags != profile_auth.attrs.pin.flags)   {
492
0
      sc_log(ctx,  "Set flags of '%s'(flags:%X,ref:%i,id:%s) to %X", objs[ii]->label,
493
0
          pin_attrs->flags, pin_attrs->reference, sc_pkcs15_print_id(&ainfo->auth_id),
494
0
          profile_auth.attrs.pin.flags);
495
0
      pin_attrs->flags = profile_auth.attrs.pin.flags;
496
0
      update_df = 1;
497
0
    }
498
0
  }
499
107
  if (update_df)   {
500
0
    struct sc_pkcs15_df *df = p15card->df_list;
501
502
0
    while (df != NULL && df->type != SC_PKCS15_AODF)
503
0
      df = df->next;
504
0
    if (!df)
505
0
      LOG_TEST_RET(ctx, SC_ERROR_OBJECT_NOT_FOUND, "Cannot find AODF");
506
0
    rv = sc_pkcs15init_update_any_df(p15card, profile, df, 0);
507
0
    LOG_TEST_RET(ctx, rv, "Update AODF error");
508
0
  }
509
510
107
  SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_VERBOSE, rv);
511
107
}
512
513
static struct sc_pkcs15init_operations sc_pkcs15init_entersafe_operations = {
514
  entersafe_erase_card,
515
  entersafe_init_card,
516
  entersafe_create_dir,
517
  NULL,       /* create_domain */
518
  entersafe_pin_reference,
519
  entersafe_create_pin,
520
  entersafe_key_reference,
521
  entersafe_create_key,
522
  entersafe_store_key,
523
  entersafe_generate_key,
524
  NULL, NULL,     /* encode private/public key */
525
  NULL,         /* finalize */
526
  NULL,         /* delete_object */
527
  NULL, NULL, NULL, NULL, NULL,   /* pkcs15init emulation */
528
  entersafe_sanity_check,
529
};
530
531
struct sc_pkcs15init_operations *sc_pkcs15init_get_entersafe_ops(void)
532
293
{
533
293
  return &sc_pkcs15init_entersafe_operations;
534
293
}