/src/opensc/src/libopensc/card-srbeid.c
Line | Count | Source |
1 | | /* |
2 | | * card-srbeid.c: Driver for Serbian cards using the CardEdge PKI applet. |
3 | | * |
4 | | * Serbian eID, health insurance, and Chamber of Commerce cards use the |
5 | | * same CardEdge PKCS#15 applet. Cards are matched by ATR whitelist, |
6 | | * with a PKCS#15 AID select as a confirmation step. |
7 | | * |
8 | | * Copyright (C) 2026 LibreSCRS contributors |
9 | | * |
10 | | * This library is free software; you can redistribute it and/or |
11 | | * modify it under the terms of the GNU Lesser General Public |
12 | | * License as published by the Free Software Foundation; either |
13 | | * version 2.1 of the License, or (at your option) any later version. |
14 | | * |
15 | | * This library is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | | * Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General Public |
21 | | * License along with this library; if not, write to the Free Software |
22 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | | */ |
24 | | |
25 | | #ifdef HAVE_CONFIG_H |
26 | | #include "config.h" |
27 | | #endif |
28 | | |
29 | | #include <string.h> |
30 | | |
31 | | #include "card-srbeid.h" |
32 | | #include "internal.h" |
33 | | #include "log.h" |
34 | | |
35 | | /* MSE SET algorithm byte (tag 0x80). */ |
36 | 20 | #define CE_MSE_ALG_RSA_PKCS1 0x02u /* card adds PKCS#1 padding */ |
37 | 18 | #define CE_MSE_ALG_RSA_RAW 0x00u /* raw RSA; OpenSC pads */ |
38 | | |
39 | | static struct sc_card_operations srbeid_ops; |
40 | | static const struct sc_card_operations *iso_ops; |
41 | | |
42 | | static struct sc_card_driver srbeid_drv = { |
43 | | "Serbian CardEdge driver", |
44 | | "srbeid", |
45 | | &srbeid_ops, |
46 | | NULL, 0, NULL}; |
47 | | |
48 | | /* Full ATR list provided by NetSeT (the CardEdge PKI applet vendor). Covers |
49 | | * citizen eID, foreigner eID, PKS Chamber of Commerce, and RFZO health |
50 | | * insurance cards across their various underlying card platforms. */ |
51 | | static const struct sc_atr_table srbeid_atrs[] = { |
52 | | {"3B:FF:94:00:00:81:31:80:43:80:31:80:65:B0:85:02:01:F3:12:0F:FF:82:90:00:79", NULL, "Serbian eID (Gemalto Multiapp 80K)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
53 | | {"3B:F8:13:00:00:81:31:FE:45:4A:43:4F:50:76:32:34:31:B7", NULL, "Serbian eID (NXP JCOP v2.4.1 80K)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
54 | | {"3B:FA:13:00:00:81:31:FE:45:4A:43:4F:50:32:31:56:32:33:31:91", NULL, "Serbian eID (NXP JCOP21 v2.3.1)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
55 | | {"3B:F4:13:00:00:81:31:FE:45:52:46:5A:4F:ED", NULL, "Serbian health card (NXP JCOP21 v2.4.1)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
56 | | {"3B:7A:96:00:00:80:65:A2:01:01:02:3D:72:D6:43", NULL, "Serbian eID (Gemalto Multiapp 80K IDCore10)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
57 | | {"3B:F9:96:00:00:80:31:FE:45:53:43:45:37:20:47:43:4E:33:5E", NULL, "Serbian eID (SmartCafe Expert v7.0)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
58 | | {"3B:FD:94:00:00:81:31:80:43:80:31:80:65:B1:F3:01:07:0F:83:01:90:00:17", NULL, "Serbian eID (Gemalto Dual-Interface 144K Contact)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
59 | | {"3B:89:80:01:53:43:45:2E:37:20:20:20:20:44", NULL, "Serbian eID (SmartCafe Expert v7.0 Contactless)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
60 | | {"3B:F9:96:00:00:80:31:FE:45:53:43:45:37:20:20:00:20:20:07", NULL, "Serbian eID (SmartCafe Expert v7.1)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
61 | | {"3B:DE:97:00:80:31:FE:45:53:43:45:20:38:2E:30:2D:43:31:56:30:0D:0A:2E", NULL, "Serbian PKS card (SmartCafe Expert v8.0 B)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
62 | | {"3B:9E:96:80:31:FE:45:53:43:45:20:38:2E:30:2D:43:31:56:30:0D:0A:6F", NULL, "Serbian eID (SmartCafe Expert v8.0)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
63 | | {"3B:9E:96:80:31:FE:45:53:43:45:20:38:2E:30:2D:43:32:56:30:0D:0A:6C", NULL, "Serbian eID (SmartCafe Expert v8.0 C2)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
64 | | {"3B:D5:18:FF:81:91:FE:1F:C3:80:73:C8:21:10:0A", NULL, "Serbian eID (NXP JCOP4 P71)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
65 | | {"3B:DE:97:00:80:31:FE:45:53:43:45:20:38:2E:30:2D:43:32:56:30:0D:0A:2D", NULL, "Serbian PKS card (SmartCafe Expert v8.0 C2 B)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
66 | | {"3B:9E:97:80:31:FE:45:53:43:45:20:38:2E:30:2D:43:31:56:30:0D:0A:6E", NULL, "Serbian health card (SmartCafe Expert v8.0 S)", SC_CARD_TYPE_SRBEID_BASE, 0, NULL}, |
67 | | {NULL, NULL, NULL, 0, 0, NULL} |
68 | | }; |
69 | | |
70 | | static int |
71 | | srbeid_match_card(sc_card_t *card) |
72 | 7.42k | { |
73 | 7.42k | int i = _sc_match_atr(card, srbeid_atrs, &card->type); |
74 | 7.42k | if (i >= 0 && iso7816_select_aid(card, AID_PKCS15, AID_PKCS15_LEN, NULL, NULL) == SC_SUCCESS) { |
75 | 286 | card->name = srbeid_atrs[i].name; |
76 | 286 | return 1; |
77 | 286 | } |
78 | 7.14k | return 0; |
79 | 7.42k | } |
80 | | |
81 | | static int |
82 | | srbeid_init(sc_card_t *card) |
83 | 286 | { |
84 | 286 | LOG_FUNC_CALLED(card->ctx); |
85 | | |
86 | 286 | card->caps |= SC_CARD_CAP_ISO7816_PIN_INFO; |
87 | | |
88 | | /* Keep the card's own PKCS#1 signing, and add raw RSA on top: that gives |
89 | | * CKM_RSA_X_509 and makes 2048-bit decrypt work (OpenSC does the padding). */ |
90 | 286 | _sc_card_add_rsa_alg(card, 2048, |
91 | 286 | SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01 | SC_ALGORITHM_RSA_RAW | |
92 | 286 | SC_ALGORITHM_RSA_HASH_NONE, |
93 | 286 | 0); |
94 | | |
95 | 286 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
96 | 286 | } |
97 | | |
98 | | /* |
99 | | * select_file — handle CardEdge's proprietary 10-byte FCI response. |
100 | | * |
101 | | * CardEdge FCI layout (10 bytes, big-endian): |
102 | | * [FID_H FID_L Size_H Size_L ACL*6] |
103 | | * |
104 | | * iso7816_select_file() would try to parse this as ISO 7816-4 TLV (tag 0x6F) |
105 | | * and fail with SC_ERROR_UNKNOWN_DATA_RECEIVED. |
106 | | * |
107 | | * DF_NAME (AID) selection is delegated to the ISO layer. |
108 | | */ |
109 | | static int |
110 | | srbeid_select_file(sc_card_t *card, const sc_path_t *in_path, |
111 | | sc_file_t **file_out) |
112 | 2.17k | { |
113 | 2.17k | sc_apdu_t apdu; |
114 | 2.17k | u8 fci[16]; |
115 | 2.17k | sc_file_t *file; |
116 | 2.17k | int r; |
117 | | |
118 | 2.17k | if (in_path->type == SC_PATH_TYPE_DF_NAME) |
119 | 602 | return iso_ops->select_file(card, in_path, file_out); |
120 | | |
121 | | /* AID-only path (path.len==0, path.aid.len>0): PKCS#15 layer wants |
122 | | * to select the applet before a PIN or key operation. */ |
123 | 1.57k | if (in_path->len == 0 && in_path->aid.len > 0) |
124 | 43 | return iso7816_select_aid(card, in_path->aid.value, |
125 | 43 | in_path->aid.len, NULL, NULL); |
126 | | |
127 | 1.53k | if (in_path->len != 2) |
128 | 1.53k | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
129 | | |
130 | 675 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0x00, 0x00); |
131 | 675 | apdu.data = in_path->value; |
132 | 675 | apdu.datalen = 2; |
133 | 675 | apdu.lc = 2; |
134 | 675 | apdu.resp = fci; |
135 | 675 | apdu.resplen = sizeof(fci); |
136 | 675 | apdu.le = 10; |
137 | | |
138 | 675 | r = sc_transmit_apdu(card, &apdu); |
139 | 675 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
140 | 659 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
141 | 659 | LOG_TEST_RET(card->ctx, r, "SELECT FILE failed"); |
142 | | |
143 | 444 | if (apdu.resplen < 4) |
144 | 444 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED); |
145 | | |
146 | 422 | if (file_out) { |
147 | 377 | file = sc_file_new(); |
148 | 377 | if (!file) |
149 | 377 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
150 | | |
151 | 377 | file->id = ((unsigned)in_path->value[0] << 8) | in_path->value[1]; |
152 | 377 | file->path = *in_path; |
153 | 377 | file->size = ((size_t)fci[2] << 8) | (size_t)fci[3]; |
154 | 377 | file->type = SC_FILE_TYPE_WORKING_EF; |
155 | 377 | *file_out = file; |
156 | 377 | } |
157 | | |
158 | 422 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
159 | 422 | } |
160 | | |
161 | | /* |
162 | | * set_security_env — send MSE SET to the card. |
163 | | * |
164 | | * The PKCS#15 layer selects the PKI applet via the AID attached to |
165 | | * key_info.path before calling this function (see select_key_file() |
166 | | * in pkcs15-sec.c). |
167 | | * |
168 | | * OpenSC populates env->key_ref[0] from key_info.key_reference (low byte). |
169 | | * The high byte is always 0x60 (CE_KEYS_BASE_FID >> 8) for all CardEdge |
170 | | * key FIDs, so the full 2-byte FID is reconstructed here. |
171 | | * |
172 | | * MSE SET template P2: 0xB6 for signing, 0xB8 for deciphering. |
173 | | */ |
174 | | static int |
175 | | srbeid_set_security_env(sc_card_t *card, |
176 | | const struct sc_security_env *env, int se_num) |
177 | 19 | { |
178 | 19 | sc_apdu_t apdu; |
179 | 19 | u8 mse_data[7]; |
180 | 19 | unsigned key_ref; |
181 | 19 | u8 p2; |
182 | 19 | int r; |
183 | | |
184 | 19 | LOG_FUNC_CALLED(card->ctx); |
185 | 19 | (void)se_num; |
186 | | |
187 | | /* Extract key FID. */ |
188 | 19 | if ((env->flags & SC_SEC_ENV_FILE_REF_PRESENT) && env->file_ref.len >= 2) { |
189 | 0 | key_ref = ((unsigned)env->file_ref.value[0] << 8) | (unsigned)env->file_ref.value[1]; |
190 | 19 | } else if ((env->flags & SC_SEC_ENV_KEY_REF_PRESENT) && env->key_ref_len >= 1) { |
191 | 19 | key_ref = CE_KEYS_BASE_FID | (unsigned)env->key_ref[0]; |
192 | 19 | } else { |
193 | 0 | sc_log(card->ctx, "srbeid: set_security_env: no key reference"); |
194 | 0 | return SC_ERROR_INCORRECT_PARAMETERS; |
195 | 0 | } |
196 | | |
197 | | /* Determine MSE SET template from operation type. */ |
198 | 19 | switch (env->operation) { |
199 | 8 | case SC_SEC_OPERATION_SIGN: |
200 | 8 | p2 = 0xB6; |
201 | 8 | break; |
202 | 11 | case SC_SEC_OPERATION_DECIPHER: |
203 | 11 | p2 = 0xB8; |
204 | 11 | break; |
205 | 0 | default: |
206 | 0 | return SC_ERROR_NOT_SUPPORTED; |
207 | 19 | } |
208 | | |
209 | | /* MSE SET: tag 0x80 = algorithm, tag 0x84 = key ref (2 bytes BE). |
210 | | * The algorithm byte tells the card to pad (PKCS#1) or not (raw RSA). */ |
211 | 19 | mse_data[0] = 0x80; |
212 | 19 | mse_data[1] = 0x01; |
213 | 19 | mse_data[2] = (env->algorithm_flags & SC_ALGORITHM_RSA_RAW) |
214 | 19 | ? CE_MSE_ALG_RSA_RAW |
215 | 19 | : CE_MSE_ALG_RSA_PKCS1; |
216 | 19 | mse_data[3] = 0x84; |
217 | 19 | mse_data[4] = 0x02; |
218 | 19 | mse_data[5] = (u8)((key_ref >> 8) & 0xFF); |
219 | 19 | mse_data[6] = (u8)(key_ref & 0xFF); |
220 | | |
221 | 19 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x41, p2); |
222 | 19 | apdu.data = mse_data; |
223 | 19 | apdu.datalen = sizeof(mse_data); |
224 | 19 | apdu.lc = sizeof(mse_data); |
225 | | |
226 | 19 | r = sc_transmit_apdu(card, &apdu); |
227 | 19 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
228 | 18 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
229 | 18 | LOG_TEST_RET(card->ctx, r, "MSE SET failed"); |
230 | | |
231 | 11 | sc_log(card->ctx, "srbeid: set_security_env: key_ref=0x%04x p2=0x%02x algo=0x%02x", |
232 | 11 | key_ref, p2, mse_data[2]); |
233 | 11 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
234 | 11 | } |
235 | | |
236 | | /* |
237 | | * One PSO (INS=0x2A, given P1). A 256-byte block exceeds the short-APDU Lc, so |
238 | | * its first byte is carried in P2; a smaller DigestInfo is sent as-is. |
239 | | */ |
240 | | static int |
241 | | srbeid_pso(sc_card_t *card, u8 p1, const u8 *data, size_t datalen, u8 *out, |
242 | | size_t outlen) |
243 | 13 | { |
244 | 13 | sc_apdu_t apdu; |
245 | 13 | u8 resp[256]; |
246 | 13 | u8 p2 = 0x00; |
247 | 13 | int r; |
248 | | |
249 | 13 | LOG_FUNC_CALLED(card->ctx); |
250 | | |
251 | 13 | if (datalen == 0 || datalen > sizeof(resp)) |
252 | 13 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
253 | | |
254 | 12 | if (datalen > SC_MAX_APDU_DATA_SIZE) { |
255 | 2 | p2 = data[0]; |
256 | 2 | ++data; |
257 | 2 | --datalen; |
258 | 2 | } |
259 | | |
260 | 12 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, p1, p2); |
261 | 12 | apdu.data = data; |
262 | 12 | apdu.datalen = apdu.lc = datalen; |
263 | 12 | apdu.resp = resp; |
264 | 12 | apdu.resplen = sizeof(resp); |
265 | 12 | apdu.le = sizeof(resp); |
266 | | |
267 | 12 | r = sc_transmit_apdu(card, &apdu); |
268 | 12 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
269 | 8 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
270 | 8 | LOG_TEST_RET(card->ctx, r, "PSO failed"); |
271 | | |
272 | 4 | if (apdu.resplen > outlen) |
273 | 4 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_BUFFER_TOO_SMALL); |
274 | 4 | memcpy(out, resp, apdu.resplen); |
275 | 4 | LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen); |
276 | 4 | } |
277 | | |
278 | | /* Signing: PSO COMPUTE SIGNATURE (00 2A 9E). */ |
279 | | static int |
280 | | srbeid_compute_signature(sc_card_t *card, const u8 *data, size_t datalen, |
281 | | u8 *out, size_t outlen) |
282 | 2 | { |
283 | 2 | return srbeid_pso(card, 0x9E, data, datalen, out, outlen); |
284 | 2 | } |
285 | | |
286 | | /* |
287 | | * Decryption: PSO DECIPHER (00 2A 80) so the card is told the operation. The |
288 | | * applet reads P2 as an ISO template and rejects a cryptogram starting with |
289 | | * 0x86, so fall back to COMPUTE SIGNATURE (00 2A 9E), which is the same RSA |
290 | | * operation on this card. |
291 | | */ |
292 | | static int |
293 | | srbeid_decipher(sc_card_t *card, const u8 *crgram, size_t crgram_len, u8 *out, |
294 | | size_t outlen) |
295 | 9 | { |
296 | 9 | int r = srbeid_pso(card, 0x80, crgram, crgram_len, out, outlen); |
297 | | |
298 | 9 | if (r == SC_ERROR_INCORRECT_PARAMETERS) |
299 | 2 | r = srbeid_pso(card, 0x9E, crgram, crgram_len, out, outlen); |
300 | 9 | return r; |
301 | 9 | } |
302 | | |
303 | | struct sc_card_driver * |
304 | | sc_get_srbeid_driver(void) |
305 | 14.3k | { |
306 | | /* Save ISO ops for delegation, then override what we handle. */ |
307 | 14.3k | iso_ops = sc_get_iso7816_driver()->ops; |
308 | 14.3k | srbeid_ops = *iso_ops; |
309 | 14.3k | srbeid_ops.match_card = srbeid_match_card; |
310 | 14.3k | srbeid_ops.init = srbeid_init; |
311 | 14.3k | srbeid_ops.select_file = srbeid_select_file; |
312 | 14.3k | srbeid_ops.set_security_env = srbeid_set_security_env; |
313 | 14.3k | srbeid_ops.compute_signature = srbeid_compute_signature; |
314 | 14.3k | srbeid_ops.decipher = srbeid_decipher; |
315 | | |
316 | 14.3k | return &srbeid_drv; |
317 | 14.3k | } |