Coverage Report

Created: 2026-07-10 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/pkcs15-skeid.c
Line
Count
Source
1
/*
2
 * PKCS15 emulation layer for Slovak eID card
3
 *
4
 * Copyright (C) 2022 Juraj Ĺ arinay <juraj@sarinay.com>
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
 * based on the PKCS15 emulation layer for EstEID card by Martin Paljak
21
 */
22
23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
#endif
26
27
#include <stdlib.h>
28
#include <string.h>
29
30
#include "common/compat_strlcpy.h"
31
32
#include "internal.h"
33
#include "log.h"
34
#include "pkcs15.h"
35
36
static const struct sc_aid skeid_aid_qes = {{0xE8, 0x28, 0xBD, 0x08, 0x0F, 0xA0, 0x00, 0x00, 0x08, 0x51, 0x00, 0x00, 0x11}, 13};
37
38
static int sc_pkcs15emu_skeid_init(sc_pkcs15_card_t * p15card)
39
26
{
40
26
  int r;
41
26
  int i;
42
26
  size_t sn_len;
43
26
  char *buf;
44
45
26
  set_string(&p15card->tokeninfo->label, "eID karta");
46
26
  set_string(&p15card->tokeninfo->manufacturer_id, "Atos Information Technology GmbH");
47
48
26
  sn_len = p15card->card->serialnr.len;
49
26
  if (sn_len > 0) {
50
26
    buf = malloc(2 * sn_len + 1);
51
26
    if (!buf) return SC_ERROR_OUT_OF_MEMORY;
52
26
    sc_bin_to_hex(p15card->card->serialnr.value, sn_len, buf,
53
26
      2 * sn_len + 1, 0);
54
26
    p15card->tokeninfo->serial_number = buf;
55
26
  }
56
57
26
  p15card->tokeninfo->flags = SC_PKCS15_TOKEN_PRN_GENERATION | SC_PKCS15_TOKEN_READONLY;
58
59
  /* add certificates */
60
26
  const char *skeid_cert_names[3] = {
61
26
    "Kvalifikovany certifikat pre elektronicky podpis",
62
26
    "Certifikat pre elektronicky podpis",
63
26
    "Sifrovaci certifikat"
64
26
  };
65
66
26
  const char *skeid_cert_paths[3] = {
67
26
    "3f0001030201",
68
26
    "3f0001030202",
69
26
    "3f0001030203"
70
26
  };
71
72
104
  for (i = 0; i < 3; i++) {
73
78
    struct sc_pkcs15_cert_info cert_info;
74
78
    struct sc_pkcs15_object cert_obj;
75
76
78
    memset(&cert_info, 0, sizeof(cert_info));
77
78
    memset(&cert_obj, 0, sizeof(cert_obj));
78
79
78
    cert_info.id.value[0] = i + 1;
80
78
    cert_info.id.len = 1;
81
82
78
    sc_format_path(skeid_cert_paths[i], &cert_info.path);
83
78
    strlcpy(cert_obj.label, skeid_cert_names[i], sizeof(cert_obj.label));
84
85
78
    r = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj, &cert_info);
86
87
78
    LOG_TEST_RET(p15card->card->ctx, r, "Error adding certificate.");
88
78
  }
89
90
  /* add pins */
91
26
  const char *skeid_pin_names[2] = {
92
26
    "BOK",
93
26
    "Podpisovy PIN"
94
26
  };
95
96
26
  const unsigned int skeid_pin_max_length[2] = {6, 10};
97
26
  const unsigned int skeid_pin_max_tries[2] = {5, 3};
98
26
  const int skeid_pin_ref[2] = {0x03, 0x87};
99
26
  const char *skeid_pin_paths[2] = {"3F00", "3F000101"};
100
101
26
  const unsigned int skeid_pin_flags[2] = {SC_PKCS15_PIN_FLAG_CASE_SENSITIVE | SC_PKCS15_PIN_FLAG_EXCHANGE_REF_DATA | SC_PKCS15_PIN_FLAG_INITIALIZED,
102
26
    SC_PKCS15_PIN_FLAG_CASE_SENSITIVE | SC_PKCS15_PIN_FLAG_LOCAL | SC_PKCS15_PIN_FLAG_EXCHANGE_REF_DATA | SC_PKCS15_PIN_FLAG_INITIALIZED};
103
104
78
  for (i = 0; i < 2; i++) {
105
52
    struct sc_pkcs15_auth_info pin_info;
106
52
    struct sc_pkcs15_object pin_obj;
107
108
52
    memset(&pin_info, 0, sizeof(pin_info));
109
52
    memset(&pin_obj, 0, sizeof(pin_obj));
110
111
52
    pin_info.auth_id.len = 1;
112
52
    pin_info.auth_id.value[0] = i + 1;
113
52
    pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
114
52
    pin_info.attrs.pin.reference = skeid_pin_ref[i];
115
52
    pin_info.attrs.pin.flags = skeid_pin_flags[i];
116
52
    pin_info.attrs.pin.type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC;
117
52
    pin_info.attrs.pin.min_length = 6;
118
52
    pin_info.attrs.pin.max_length = skeid_pin_max_length[i];
119
52
    pin_info.max_tries = skeid_pin_max_tries[i];
120
121
52
    strlcpy(pin_obj.label, skeid_pin_names[i], sizeof(pin_obj.label));
122
52
    pin_obj.flags = skeid_pin_flags[i];
123
124
52
    sc_format_path(skeid_pin_paths[i], &pin_info.path);
125
126
52
    r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
127
128
52
    LOG_TEST_RET(p15card->card->ctx, r, "Error adding PIN.");
129
52
  }
130
131
  /* add private keys */
132
26
  const u8 skeid_prkey_pin[3] = {2, 1, 1};
133
134
  /* store seIdentifier rather than keyReference */
135
26
  const int skeid_prkey_ref[3] = {0x01, 0x34, 0x44};
136
26
  const int skeid_prkey_usage[3] =
137
26
    { SC_PKCS15_PRKEY_USAGE_NONREPUDIATION | SC_PKCS15_PRKEY_USAGE_SIGN,
138
26
      SC_PKCS15_PRKEY_USAGE_SIGN,
139
26
      SC_PKCS15_PRKEY_USAGE_DECRYPT
140
26
    };
141
142
26
  const char *skeid_prkey_paths[3] = {"3F000101", "3F000102", "3F000102"};
143
144
26
  const char *skeid_prkey_name[3] = {
145
26
    "Podpisovy kluc (KEP)",
146
26
    "Podpisovy kluc",
147
26
    "Sifrovaci kluc",
148
26
  };
149
150
104
  for (i = 0; i < 3; i++) {
151
78
    struct sc_pkcs15_prkey_info prkey_info;
152
78
    struct sc_pkcs15_object prkey_obj;
153
154
78
    memset(&prkey_info, 0, sizeof(prkey_info));
155
78
    memset(&prkey_obj, 0, sizeof(prkey_obj));
156
157
78
    prkey_info.id.len = 1;
158
78
    prkey_info.id.value[0] = i + 1;
159
78
    prkey_info.native = 1;
160
78
    prkey_info.key_reference = skeid_prkey_ref[i];
161
78
    prkey_info.modulus_length = 3072;
162
78
    sc_format_path(skeid_prkey_paths[i], &prkey_info.path);
163
164
78
    prkey_info.usage = skeid_prkey_usage[i];
165
166
78
    strlcpy(prkey_obj.label, skeid_prkey_name[i], sizeof(prkey_obj.label));
167
78
    prkey_obj.auth_id.len = 1;
168
78
    prkey_obj.auth_id.value[0] = skeid_prkey_pin[i];
169
78
    if (i == 0) prkey_obj.user_consent = 1;
170
171
78
    prkey_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE;
172
173
78
    r = sc_pkcs15emu_add_rsa_prkey(p15card, &prkey_obj, &prkey_info);
174
175
78
    LOG_TEST_RET(p15card->card->ctx, r, "Error adding private key.");
176
78
  }
177
26
  LOG_FUNC_RETURN(p15card->card->ctx, SC_SUCCESS);
178
26
}
179
180
int sc_pkcs15emu_skeid_init_ex(sc_pkcs15_card_t *p15card, struct sc_aid *aid)
181
46.4k
{
182
46.4k
  int r = SC_ERROR_WRONG_CARD;
183
184
46.4k
  if (p15card->card->type == SC_CARD_TYPE_SKEID_V3
185
30
    && (aid == NULL || (aid->len == skeid_aid_qes.len && !memcmp(aid->value, &skeid_aid_qes.value, skeid_aid_qes.len))))
186
26
    r = sc_pkcs15emu_skeid_init(p15card);
187
188
46.4k
  return r;
189
46.4k
}