Coverage Report

Created: 2025-11-09 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/pkcs15-jpki.c
Line
Count
Source
1
/*
2
 * PKCS15 emulation layer for JPKI(Japanese Individual Number Cards).
3
 *
4
 * Copyright (C) 2016, HAMANO Tsukasa <hamano@osstech.co.jp>
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 <stdlib.h>
26
#include <string.h>
27
#include <stdio.h>
28
29
#include "common/compat_strlcpy.h"
30
#include "common/compat_strlcat.h"
31
32
#include "internal.h"
33
#include "pkcs15.h"
34
#include "jpki.h"
35
36
static int
37
sc_pkcs15emu_jpki_init(sc_pkcs15_card_t * p15card)
38
37
{
39
37
  sc_card_t *card = p15card->card;
40
37
  struct jpki_private_data *drvdata = JPKI_DRVDATA(card);
41
37
  int i, rc;
42
43
37
  LOG_FUNC_CALLED(p15card->card->ctx);
44
45
37
  set_string(&p15card->tokeninfo->label, "JPKI");
46
37
  set_string(&p15card->tokeninfo->manufacturer_id, "JPKI");
47
  /* set dummy until we found serial number */
48
37
  set_string(&p15card->tokeninfo->serial_number, "00000000");
49
50
  /* Select application directory */
51
37
  if (drvdata->selected != SELECT_JPKI_AP) {
52
37
    rc = jpki_select_ap(card);
53
37
    LOG_TEST_RET(card->ctx, rc, "select AP failed");
54
25
    drvdata->selected = SELECT_JPKI_AP;
55
25
  }
56
57
  /* add certificates */
58
125
  for (i = 0; i < 4; i++) {
59
100
    static const char *jpki_cert_names[4] = {
60
100
      "User Authentication Certificate",
61
100
      "Digital Signature Certificate",
62
100
      "User Authentication Certificate CA",
63
100
      "Digital Signature Certificate CA"
64
100
    };
65
100
    static char const *jpki_cert_paths[4] = {
66
100
      "000A",
67
100
      "0001",
68
100
      "000B",
69
100
      "0002"
70
100
    };
71
100
    static int jpki_cert_ids[4] = { 1, 2, 3, 4 };
72
100
    static int jpki_cert_flags[4] = {
73
100
      0,
74
100
      SC_PKCS15_CO_FLAG_PRIVATE,
75
100
      0,
76
100
      0,
77
100
    };
78
100
    static int jpki_cert_authority[4] = {0, 0, 1, 1};
79
100
    struct sc_pkcs15_cert_info cert_info;
80
100
    struct sc_pkcs15_object cert_obj;
81
100
    memset(&cert_info, 0, sizeof(cert_info));
82
100
    memset(&cert_obj, 0, sizeof(cert_obj));
83
84
100
    cert_info.id.value[0] = jpki_cert_ids[i];
85
100
    cert_info.id.len = 1;
86
100
    sc_format_path(jpki_cert_paths[i], &cert_info.path);
87
100
    cert_info.path.type = SC_PATH_TYPE_FILE_ID;
88
89
100
    strlcpy(cert_obj.label, jpki_cert_names[i], sizeof(cert_obj.label));
90
100
    cert_info.authority = jpki_cert_authority[i];
91
100
    cert_obj.flags = jpki_cert_flags[i];
92
100
    rc = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj, &cert_info);
93
100
    if (rc < 0) {
94
0
      sc_pkcs15_card_clear(p15card);
95
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
96
0
    }
97
98
100
  }
99
100
  /* add pins */
101
32
  for (i = 0; i < 2; i++) {
102
31
    static const char *jpki_pin_names[2] = {
103
31
      "User Authentication PIN",
104
31
      "Digital Signature PIN"
105
31
    };
106
31
    static const int jpki_pin_min[2] = { 4, 6 };
107
31
    static const int jpki_pin_max[2] = { 4, 16 };
108
31
    static const int jpki_pin_ref[2] = { 1, 2 };
109
31
    static const int jpki_pin_authid[2] = { 1, 2 };
110
31
    static const int jpki_pin_flags[2] = {
111
31
      SC_PKCS15_PIN_FLAG_INITIALIZED |
112
31
      SC_PKCS15_PIN_FLAG_LOCAL,
113
31
      SC_PKCS15_PIN_FLAG_INITIALIZED |
114
31
      SC_PKCS15_PIN_FLAG_LOCAL
115
31
    };
116
31
    static const int jpki_pin_max_tries[2] = {
117
31
      JPKI_AUTH_PIN_MAX_TRIES,
118
31
      JPKI_SIGN_PIN_MAX_TRIES
119
31
    };
120
121
31
    struct sc_pkcs15_auth_info pin_info;
122
31
    struct sc_pkcs15_object pin_obj;
123
31
    struct sc_pin_cmd_data pin_cmd_data;
124
31
    memset(&pin_info, 0, sizeof (pin_info));
125
31
    memset(&pin_obj, 0, sizeof (pin_obj));
126
31
    memset(&pin_cmd_data, 0, sizeof(pin_cmd_data));
127
128
31
    pin_info.auth_id.len = 1;
129
31
    pin_info.auth_id.value[0] = jpki_pin_authid[i];
130
31
    pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
131
31
    pin_info.attrs.pin.reference = jpki_pin_ref[i];
132
31
    pin_info.attrs.pin.flags = jpki_pin_flags[i];
133
31
    pin_info.attrs.pin.type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC;
134
31
    pin_info.attrs.pin.min_length = jpki_pin_min[i];
135
31
    pin_info.attrs.pin.stored_length = 0;
136
31
    pin_info.attrs.pin.max_length = jpki_pin_max[i];
137
31
    pin_info.attrs.pin.pad_char = '\0';
138
31
    pin_info.max_tries = jpki_pin_max_tries[i];
139
31
    pin_info.tries_left = -1;
140
31
    pin_info.logged_in = SC_PIN_STATE_UNKNOWN;
141
142
31
    pin_cmd_data.cmd = SC_PIN_CMD_GET_INFO;
143
31
    pin_cmd_data.pin_type = SC_AC_CHV;
144
31
    pin_cmd_data.pin_reference = jpki_pin_ref[i];
145
31
    rc = sc_pin_cmd(card, &pin_cmd_data, &pin_info.tries_left);
146
31
    LOG_TEST_RET(card->ctx, rc, "sc_pin_cmd failed");
147
7
    strlcpy(pin_obj.label, jpki_pin_names[i], sizeof(pin_obj.label));
148
7
    pin_obj.flags = jpki_pin_flags[i];
149
150
7
    rc = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
151
7
    if (rc < 0) {
152
0
      sc_pkcs15_card_clear(p15card);
153
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
154
0
    }
155
7
  }
156
157
  /* add private keys */
158
3
  for (i = 0; i < 2; i++) {
159
2
    static int prkey_pin[2] = { 1, 2 };
160
2
    static int prkey_usage[2] = {
161
2
      SC_PKCS15_PRKEY_USAGE_SIGN,
162
2
      SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_NONREPUDIATION
163
2
    };
164
2
    static const char *prkey_name[2] = {
165
2
      "User Authentication Key",
166
2
      "Digital Signature Key"
167
2
    };
168
2
    static int prkey_user_consent[2] = { 0, 1 };
169
2
    struct sc_pkcs15_prkey_info prkey_info;
170
2
    struct sc_pkcs15_object prkey_obj;
171
172
2
    memset(&prkey_info, 0, sizeof (prkey_info));
173
2
    memset(&prkey_obj, 0, sizeof (prkey_obj));
174
175
2
    prkey_info.id.len = 1;
176
2
    prkey_info.id.value[0] = prkey_pin[i];
177
2
    prkey_info.usage = prkey_usage[i];
178
2
    prkey_info.native = 1;
179
2
    prkey_info.key_reference = i + 1;
180
2
    prkey_info.modulus_length = 2048;
181
182
2
    strlcpy(prkey_obj.label, prkey_name[i], sizeof (prkey_obj.label));
183
2
    prkey_obj.auth_id.len = 1;
184
2
    prkey_obj.auth_id.value[0] = prkey_pin[i];
185
2
    prkey_obj.user_consent = prkey_user_consent[i];
186
2
    prkey_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE;
187
188
2
    rc = sc_pkcs15emu_add_rsa_prkey(p15card, &prkey_obj, &prkey_info);
189
2
    if (rc < 0) {
190
0
      sc_pkcs15_card_clear(p15card);
191
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
192
0
    }
193
2
  }
194
195
  /* add public keys */
196
3
  for (i = 0; i < 2; i++) {
197
2
    static int pubkey_id[2] = { 1, 2 };
198
2
    static const char *jpki_pubkey_names[2] = {
199
2
      "User Authentication Public Key",
200
2
      "Digital Signature Public Key"
201
2
    };
202
2
    static int jpki_pubkey_flags[2] = {
203
2
        0,
204
2
        SC_PKCS15_CO_FLAG_PRIVATE};
205
2
    static int jpki_pubkey_auth_id[2] = {0, 2};
206
2
    struct sc_pkcs15_pubkey_info pubkey_info;
207
2
    struct sc_pkcs15_object pubkey_obj;
208
2
    static char const *jpki_pubkey_paths[2] = {
209
2
      "000A",
210
2
      "0001"
211
2
    };
212
213
2
    memset(&pubkey_info, 0, sizeof (pubkey_info));
214
2
    memset(&pubkey_obj, 0, sizeof (pubkey_obj));
215
216
2
    strlcpy(pubkey_obj.label, jpki_pubkey_names[i], sizeof (pubkey_obj.label));
217
2
    pubkey_info.id.len = 1;
218
2
    pubkey_info.id.value[0] = pubkey_id[i];
219
2
    pubkey_info.native = 1;
220
2
    pubkey_info.key_reference = i + 1;
221
222
2
    sc_format_path(jpki_pubkey_paths[i], &pubkey_info.path);
223
2
    pubkey_info.path.type = SC_PATH_TYPE_FILE_ID;
224
2
    pubkey_obj.flags = jpki_pubkey_flags[i];
225
2
    pubkey_obj.auth_id.len = 1;
226
2
    pubkey_obj.auth_id.value[0] = jpki_pubkey_auth_id[i];
227
228
2
    rc = sc_pkcs15emu_add_rsa_pubkey(p15card, &pubkey_obj, &pubkey_info);
229
2
    if (rc < 0) {
230
0
      sc_pkcs15_card_clear(p15card);
231
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
232
0
    }
233
2
  }
234
1
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
235
1
}
236
237
int
238
sc_pkcs15emu_jpki_init_ex(sc_pkcs15_card_t * p15card,
239
        struct sc_aid *aid)
240
7.73k
{
241
7.73k
  if (p15card->card->type != SC_CARD_TYPE_JPKI_BASE)
242
7.69k
    return SC_ERROR_WRONG_CARD;
243
37
  return sc_pkcs15emu_jpki_init(p15card);
244
7.73k
}