Coverage Report

Created: 2025-07-18 06:10

/src/opensc/src/libopensc/pkcs15-dtrust.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * PKCS15 emulation layer for D-Trust card.
3
 *
4
 * Copyright (C) 2024, Mario Haustein <mario.haustein@hrz.tu-chemnitz.de>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include "config.h"
23
#endif
24
25
#include "internal.h"
26
#include "pkcs15.h"
27
28
static int
29
_dtrust_parse_df(struct sc_pkcs15_card *p15card, struct sc_pkcs15_df *df)
30
0
{
31
0
  struct sc_context *ctx = p15card->card->ctx;
32
0
  struct sc_pkcs15_object *pkobjs[32];
33
0
  struct sc_pkcs15_prkey_info *prkey_info;
34
0
  int rv, i, count;
35
36
0
  LOG_FUNC_CALLED(ctx);
37
38
0
  if (!df)
39
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
40
41
0
  if (df->enumerated)
42
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
43
44
0
  rv = sc_pkcs15_parse_df(p15card, df);
45
0
  LOG_TEST_RET(ctx, rv, "DF parse error");
46
47
0
  if (df->type != SC_PKCS15_PRKDF)
48
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
49
50
0
  switch (p15card->card->type) {
51
  /* Cards with EC keys, don't encode the curve size in the
52
   * private key directory file. We need to set the field_length
53
   * element after parsing the private key directory file. */
54
0
  case SC_CARD_TYPE_DTRUST_V4_1_MULTI:
55
0
  case SC_CARD_TYPE_DTRUST_V4_1_M100:
56
0
  case SC_CARD_TYPE_DTRUST_V4_4_MULTI:
57
0
    rv = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_PRKEY, pkobjs, sizeof(pkobjs) / sizeof(pkobjs[0]));
58
0
    LOG_TEST_RET(ctx, rv, "Cannot get PRKEY objects list");
59
60
0
    count = rv;
61
0
    for (i = 0; i < count; i++) {
62
0
      prkey_info = (struct sc_pkcs15_prkey_info *)pkobjs[i]->data;
63
0
      prkey_info->field_length = 256;
64
0
    }
65
0
    break;
66
0
  }
67
68
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
69
0
}
70
71
static int
72
dtrust_pkcs15emu_detect_card(sc_pkcs15_card_t *p15card)
73
6.55k
{
74
6.55k
  if (p15card->card->type < SC_CARD_TYPE_DTRUST_V4_1_STD)
75
6.55k
    return SC_ERROR_WRONG_CARD;
76
77
0
  if (p15card->card->type > SC_CARD_TYPE_DTRUST_V4_4_MULTI)
78
0
    return SC_ERROR_WRONG_CARD;
79
80
0
  return SC_SUCCESS;
81
0
}
82
83
static int
84
sc_pkcs15emu_dtrust_init(struct sc_pkcs15_card *p15card, struct sc_aid *aid)
85
0
{
86
0
  struct sc_context *ctx = p15card->card->ctx;
87
0
  int rv;
88
89
0
  LOG_FUNC_CALLED(ctx);
90
91
0
  rv = sc_pkcs15_bind_internal(p15card, aid);
92
93
0
  p15card->ops.parse_df = _dtrust_parse_df;
94
95
0
  LOG_FUNC_RETURN(ctx, rv);
96
0
}
97
98
int
99
sc_pkcs15emu_dtrust_init_ex(struct sc_pkcs15_card *p15card, struct sc_aid *aid)
100
6.55k
{
101
6.55k
  if (dtrust_pkcs15emu_detect_card(p15card))
102
6.55k
    return SC_ERROR_WRONG_CARD;
103
104
0
  return sc_pkcs15emu_dtrust_init(p15card, aid);
105
6.55k
}