Coverage Report

Created: 2026-01-17 06:12

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