-
Notifications
You must be signed in to change notification settings - Fork 711
/
pkcs15-jpki.c
237 lines (209 loc) · 7.14 KB
/
pkcs15-jpki.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* PKCS15 emulation layer for JPKI(Japanese Individual Number Cards).
*
* Copyright (C) 2016, HAMANO Tsukasa <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "common/compat_strlcpy.h"
#include "common/compat_strlcat.h"
#include "internal.h"
#include "pkcs15.h"
#include "jpki.h"
static int
sc_pkcs15emu_jpki_init(sc_pkcs15_card_t * p15card)
{
sc_card_t *card = p15card->card;
struct jpki_private_data *drvdata = JPKI_DRVDATA(card);
int i, rc;
LOG_FUNC_CALLED(p15card->card->ctx);
set_string(&p15card->tokeninfo->label, "JPKI");
set_string(&p15card->tokeninfo->manufacturer_id, "JPKI");
/* set dummy until we found serial number */
set_string(&p15card->tokeninfo->serial_number, "00000000");
/* Select application directory */
if (drvdata->selected != SELECT_JPKI_AP) {
rc = jpki_select_ap(card);
LOG_TEST_RET(card->ctx, rc, "select AP failed");
drvdata->selected = SELECT_JPKI_AP;
}
/* add certificates */
for (i = 0; i < 4; i++) {
static const char *jpki_cert_names[4] = {
"User Authentication Certificate",
"Digital Signature Certificate",
"User Authentication Certificate CA",
"Digital Signature Certificate CA"
};
static char const *jpki_cert_paths[4] = {
"000A",
"0001",
"000B",
"0002"
};
static int jpki_cert_ids[4] = { 1, 2, 3, 4 };
static int jpki_cert_flags[4] = {
0,
SC_PKCS15_CO_FLAG_PRIVATE,
0,
0,
};
static int jpki_cert_authority[4] = {0, 0, 1, 1};
struct sc_pkcs15_cert_info cert_info;
struct sc_pkcs15_object cert_obj;
memset(&cert_info, 0, sizeof(cert_info));
memset(&cert_obj, 0, sizeof(cert_obj));
cert_info.id.value[0] = jpki_cert_ids[i];
cert_info.id.len = 1;
sc_format_path(jpki_cert_paths[i], &cert_info.path);
cert_info.path.type = SC_PATH_TYPE_FILE_ID;
strlcpy(cert_obj.label, jpki_cert_names[i], sizeof(cert_obj.label));
cert_info.authority = jpki_cert_authority[i];
cert_obj.flags = jpki_cert_flags[i];
rc = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj, &cert_info);
if (rc < 0) {
sc_pkcs15_card_clear(p15card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
}
}
/* add pins */
for (i = 0; i < 2; i++) {
static const char *jpki_pin_names[2] = {
"User Authentication PIN",
"Digital Signature PIN"
};
static const int jpki_pin_min[2] = { 4, 6 };
static const int jpki_pin_max[2] = { 4, 16 };
static const int jpki_pin_ref[2] = { 1, 2 };
static const int jpki_pin_authid[2] = { 1, 2 };
static const int jpki_pin_flags[2] = {
SC_PKCS15_PIN_FLAG_INITIALIZED |
SC_PKCS15_PIN_FLAG_LOCAL,
SC_PKCS15_PIN_FLAG_INITIALIZED |
SC_PKCS15_PIN_FLAG_LOCAL
};
static const int jpki_pin_max_tries[2] = {
JPKI_AUTH_PIN_MAX_TRIES,
JPKI_SIGN_PIN_MAX_TRIES
};
struct sc_pkcs15_auth_info pin_info;
struct sc_pkcs15_object pin_obj;
struct sc_pin_cmd_data pin_cmd_data;
memset(&pin_info, 0, sizeof (pin_info));
memset(&pin_obj, 0, sizeof (pin_obj));
memset(&pin_cmd_data, 0, sizeof(pin_cmd_data));
pin_info.auth_id.len = 1;
pin_info.auth_id.value[0] = jpki_pin_authid[i];
pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
pin_info.attrs.pin.reference = jpki_pin_ref[i];
pin_info.attrs.pin.flags = jpki_pin_flags[i];
pin_info.attrs.pin.type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC;
pin_info.attrs.pin.min_length = jpki_pin_min[i];
pin_info.attrs.pin.stored_length = 0;
pin_info.attrs.pin.max_length = jpki_pin_max[i];
pin_info.attrs.pin.pad_char = '\0';
pin_info.max_tries = jpki_pin_max_tries[i];
pin_info.tries_left = -1;
pin_info.logged_in = SC_PIN_STATE_UNKNOWN;
pin_cmd_data.cmd = SC_PIN_CMD_GET_INFO;
pin_cmd_data.pin_type = SC_AC_CHV;
pin_cmd_data.pin_reference = jpki_pin_ref[i];
rc = sc_pin_cmd(card, &pin_cmd_data, &pin_info.tries_left);
LOG_TEST_RET(card->ctx, rc, "sc_pin_cmd failed");
strlcpy(pin_obj.label, jpki_pin_names[i], sizeof(pin_obj.label));
pin_obj.flags = jpki_pin_flags[i];
rc = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
if (rc < 0) {
sc_pkcs15_card_clear(p15card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
}
}
/* add private keys */
for (i = 0; i < 2; i++) {
static int prkey_pin[2] = { 1, 2 };
static int prkey_usage[2] = {
SC_PKCS15_PRKEY_USAGE_SIGN,
SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_NONREPUDIATION
};
static const char *prkey_name[2] = {
"User Authentication Key",
"Digital Signature Key"
};
static int prkey_user_consent[2] = { 0, 1 };
struct sc_pkcs15_prkey_info prkey_info;
struct sc_pkcs15_object prkey_obj;
memset(&prkey_info, 0, sizeof (prkey_info));
memset(&prkey_obj, 0, sizeof (prkey_obj));
prkey_info.id.len = 1;
prkey_info.id.value[0] = prkey_pin[i];
prkey_info.usage = prkey_usage[i];
prkey_info.native = 1;
prkey_info.key_reference = i + 1;
prkey_info.modulus_length = 2048;
strlcpy(prkey_obj.label, prkey_name[i], sizeof (prkey_obj.label));
prkey_obj.auth_id.len = 1;
prkey_obj.auth_id.value[0] = prkey_pin[i];
prkey_obj.user_consent = prkey_user_consent[i];
prkey_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE;
rc = sc_pkcs15emu_add_rsa_prkey(p15card, &prkey_obj, &prkey_info);
if (rc < 0) {
sc_pkcs15_card_clear(p15card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
}
}
/* add public keys */
for (i = 0; i < 2; i++) {
static int pubkey_id[2] = { 1, 2 };
static const char *jpki_pubkey_names[2] = {
"User Authentication Public Key",
"Digital Signature Public Key"
};
struct sc_pkcs15_pubkey_info pubkey_info;
struct sc_pkcs15_object pubkey_obj;
static char const *jpki_pubkey_paths[2] = {
"000A",
"0001"
};
memset(&pubkey_info, 0, sizeof (pubkey_info));
memset(&pubkey_obj, 0, sizeof (pubkey_obj));
strlcpy(pubkey_obj.label, jpki_pubkey_names[i], sizeof (pubkey_obj.label));
pubkey_info.id.len = 1;
pubkey_info.id.value[0] = pubkey_id[i];
pubkey_info.native = 1;
pubkey_info.key_reference = i + 1;
sc_format_path(jpki_pubkey_paths[i], &pubkey_info.path);
pubkey_info.path.type = SC_PATH_TYPE_FILE_ID;
rc = sc_pkcs15emu_add_rsa_pubkey(p15card, &pubkey_obj, &pubkey_info);
if (rc < 0) {
sc_pkcs15_card_clear(p15card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
}
}
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
int
sc_pkcs15emu_jpki_init_ex(sc_pkcs15_card_t * p15card,
struct sc_aid *aid)
{
if (p15card->card->type != SC_CARD_TYPE_JPKI_BASE)
return SC_ERROR_WRONG_CARD;
return sc_pkcs15emu_jpki_init(p15card);
}