/src/opensc/src/libopensc/pkcs15-cedulauy.c
Line | Count | Source |
1 | | /* |
2 | | * PKCS#15 emulation for the Uruguayan national eID (cédula de identidad digital), |
3 | | * driven by card-cedulauy.c. |
4 | | * |
5 | | * The card's on-card EF(TokenInfo) encodes one supportedAlgorithms entry with an |
6 | | * empty SEQUENCE (30 00) where PKCS#15 expects NULL/OID, which the generic binder |
7 | | * rejects. Instead of parsing the on-card PKCS#15 structure, this emulator builds |
8 | | * a synthetic view from AGESIC's publicly documented file layout: |
9 | | * - IAS application AID A0 00 00 00 18 40 00 00 01 63 42 00 |
10 | | * - signing certificate EF B001 |
11 | | * - signing key reference 0x01 (RSA 2048) |
12 | | * - Global PIN reference 0x11 (VERIFY 00 20 00 11) |
13 | | * - identity data EFs 7001/7002/7004/700B under DF 7000 |
14 | | * References: AGESIC "Documentación técnica de la cédula de identidad con chip" |
15 | | * and the AGESIC reference code at https://github.com/eIDuy/apdu-services . |
16 | | * |
17 | | * Copyright (C) 2026 Carlos Andrés Planchón Prestes <carlosandresplanchonprestes@gmail.com> |
18 | | * |
19 | | * This library is free software; you can redistribute it and/or |
20 | | * modify it under the terms of the GNU Lesser General Public |
21 | | * License as published by the Free Software Foundation; either |
22 | | * version 2.1 of the License, or (at your option) any later version. |
23 | | * |
24 | | * This library is distributed in the hope that it will be useful, |
25 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
27 | | * Lesser General Public License for more details. |
28 | | * |
29 | | * You should have received a copy of the GNU Lesser General Public |
30 | | * License along with this library; if not, write to the Free Software |
31 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
32 | | */ |
33 | | |
34 | | #ifdef HAVE_CONFIG_H |
35 | | #include <config.h> |
36 | | #endif |
37 | | |
38 | | #include "card-cedulauy.h" |
39 | | #include "common/compat_strlcpy.h" |
40 | | #include "internal.h" |
41 | | #include "log.h" |
42 | | #include "pkcs15.h" |
43 | | #include <stdlib.h> |
44 | | #include <string.h> |
45 | | |
46 | 5 | #define CEDULAUY_CERT_FID "B001" /* signing certificate EF */ |
47 | 5 | #define CEDULAUY_KEY_REF 0x01 /* signing private key reference */ |
48 | 5 | #define CEDULAUY_PIN_REF 0x11 /* Global PIN reference */ |
49 | 20 | #define CEDULAUY_OBJ_ID 0x01 /* links cert <-> prkey */ |
50 | | |
51 | | /* Public identity data files under DF 7000 (AGESIC layout), all readable |
52 | | * without PIN. They are exposed as raw PKCS#15 data objects carrying the |
53 | | * BER-TLV content documented by AGESIC; callers parse the tags themselves. |
54 | | * EF 711D (the ICAO SOD) is not exposed: it is absent on earlier batches. */ |
55 | | static const struct { |
56 | | const char *path; /* absolute path; the MF is emulated by the driver */ |
57 | | const char *label; |
58 | | } cedulauy_data_files[] = { |
59 | | {CEDULAUY_DOC_NUMBER_PATH, "Numero de documento"}, |
60 | | {CEDULAUY_BIO_DATA_PATH, "Datos biograficos" }, |
61 | | {CEDULAUY_PHOTO_PATH, "Fotografia" }, |
62 | | {CEDULAUY_MRZ_PATH, "MRZ" }, |
63 | | }; |
64 | | |
65 | | static int |
66 | | sc_pkcs15emu_cedulauy_init(struct sc_pkcs15_card *p15card) |
67 | 5 | { |
68 | 5 | struct sc_context *ctx = p15card->card->ctx; |
69 | 5 | struct sc_aid aid; |
70 | 5 | int r; |
71 | 5 | size_t i; |
72 | | |
73 | 5 | struct sc_pkcs15_auth_info pin_info = {0}; |
74 | 5 | struct sc_pkcs15_object pin_obj = {0}; |
75 | 5 | struct sc_pkcs15_cert_info cert_info = {0}; |
76 | 5 | struct sc_pkcs15_object cert_obj = {0}; |
77 | 5 | struct sc_pkcs15_prkey_info prkey_info = {0}; |
78 | 5 | struct sc_pkcs15_object prkey_obj = {0}; |
79 | 5 | struct sc_pkcs15_cert *cert = NULL; |
80 | 5 | struct sc_app_info *appinfo; |
81 | | |
82 | 5 | LOG_FUNC_CALLED(ctx); |
83 | | |
84 | 5 | memcpy(aid.value, cedulauy_aid, sizeof cedulauy_aid); |
85 | 5 | aid.len = sizeof cedulauy_aid; |
86 | | |
87 | 5 | appinfo = calloc(1, sizeof(struct sc_app_info)); |
88 | 5 | if (appinfo == NULL) { |
89 | 0 | LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); |
90 | 0 | } |
91 | 5 | appinfo->aid = aid; |
92 | 5 | appinfo->ddo.aid = aid; |
93 | 5 | p15card->app = appinfo; |
94 | | |
95 | | /* Global PIN (reference 0x11, ASCII numeric, zero-padded to 12 bytes). */ |
96 | 5 | pin_info.auth_id.value[0] = CEDULAUY_OBJ_ID; |
97 | 5 | pin_info.auth_id.len = 1; |
98 | 5 | pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN; |
99 | 5 | pin_info.attrs.pin.reference = CEDULAUY_PIN_REF; |
100 | 5 | pin_info.attrs.pin.flags = SC_PKCS15_PIN_FLAG_INITIALIZED | SC_PKCS15_PIN_FLAG_NEEDS_PADDING; |
101 | 5 | pin_info.attrs.pin.type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC; |
102 | 5 | pin_info.attrs.pin.min_length = 4; |
103 | 5 | pin_info.attrs.pin.stored_length = 12; |
104 | 5 | pin_info.attrs.pin.max_length = 12; |
105 | 5 | pin_info.attrs.pin.pad_char = 0x00; |
106 | 5 | pin_info.tries_left = -1; |
107 | 5 | pin_info.max_tries = -1; |
108 | 5 | strlcpy(pin_obj.label, "PIN", sizeof pin_obj.label); |
109 | 5 | r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info); |
110 | 5 | LOG_TEST_RET(ctx, r, "Cannot add Global PIN object"); |
111 | | |
112 | | /* Signing certificate (EF B001 under the IAS application). */ |
113 | 5 | sc_format_path("i" CEDULAUY_CERT_FID, &cert_info.path); /* 'i' => select by file ID */ |
114 | 5 | cert_info.path.aid = aid; |
115 | 5 | cert_info.id.value[0] = CEDULAUY_OBJ_ID; |
116 | 5 | cert_info.id.len = 1; |
117 | 5 | strlcpy(cert_obj.label, "Certificado de Firma", sizeof cert_obj.label); |
118 | 5 | r = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj, &cert_info); |
119 | 5 | LOG_TEST_RET(ctx, r, "Cannot add signing certificate object"); |
120 | | |
121 | | /* Set the token label and serial number from the certificate, when |
122 | | * readable. The serial number also keys the file cache. */ |
123 | 5 | if (sc_pkcs15_read_certificate(p15card, &cert_info, 0, &cert) == SC_SUCCESS) { |
124 | 0 | static const struct sc_object_id cn_oid = { |
125 | 0 | {2, 5, 4, 3, -1} |
126 | 0 | }; |
127 | 0 | u8 *cn = NULL; |
128 | 0 | size_t cn_len = 0; |
129 | 0 | const u8 *serial = cert->serial; |
130 | 0 | size_t serial_len = cert->serial_len; |
131 | |
|
132 | 0 | sc_pkcs15_get_name_from_dn(ctx, cert->subject, cert->subject_len, |
133 | 0 | &cn_oid, &cn, &cn_len); |
134 | 0 | if (cn_len > 0) { |
135 | 0 | char *label = malloc(cn_len + 1); |
136 | 0 | if (label) { |
137 | 0 | memcpy(label, cn, cn_len); |
138 | 0 | label[cn_len] = '\0'; |
139 | 0 | free(p15card->tokeninfo->label); |
140 | 0 | p15card->tokeninfo->label = label; |
141 | 0 | } |
142 | 0 | } |
143 | 0 | free(cn); |
144 | | |
145 | | /* strip the ASN.1 INTEGER header, if present */ |
146 | 0 | if (serial_len > 2 && serial[0] == 0x02 && serial[1] == serial_len - 2) { |
147 | 0 | serial += 2; |
148 | 0 | serial_len -= 2; |
149 | 0 | } |
150 | 0 | if (serial_len > 0) { |
151 | 0 | char *sn = malloc(serial_len * 2 + 1); |
152 | 0 | if (sn) { |
153 | 0 | sc_bin_to_hex(serial, serial_len, sn, serial_len * 2 + 1, 0); |
154 | 0 | free(p15card->tokeninfo->serial_number); |
155 | 0 | p15card->tokeninfo->serial_number = sn; |
156 | 0 | } |
157 | 0 | } |
158 | |
|
159 | 0 | sc_pkcs15_free_certificate(cert); |
160 | 0 | } |
161 | | |
162 | | /* Signing private key (reference 0x01, RSA 2048, PIN-protected). */ |
163 | 5 | prkey_info.id.value[0] = CEDULAUY_OBJ_ID; |
164 | 5 | prkey_info.id.len = 1; |
165 | 5 | prkey_info.usage = SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_NONREPUDIATION; |
166 | 5 | prkey_info.native = 1; |
167 | 5 | prkey_info.key_reference = CEDULAUY_KEY_REF; |
168 | 5 | prkey_info.modulus_length = 2048; |
169 | 5 | prkey_obj.auth_id.value[0] = CEDULAUY_OBJ_ID; |
170 | 5 | prkey_obj.auth_id.len = 1; |
171 | 5 | prkey_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE; |
172 | 5 | strlcpy(prkey_obj.label, "Clave de Firma", sizeof prkey_obj.label); |
173 | 5 | r = sc_pkcs15emu_add_rsa_prkey(p15card, &prkey_obj, &prkey_info); |
174 | 5 | LOG_TEST_RET(ctx, r, "Cannot add signing private key object"); |
175 | | |
176 | | /* Public identity data objects (DF 7000), readable without PIN. */ |
177 | 25 | for (i = 0; i < sizeof cedulauy_data_files / sizeof cedulauy_data_files[0]; i++) { |
178 | 20 | struct sc_pkcs15_data_info dinfo = {0}; |
179 | 20 | struct sc_pkcs15_object dobj = {0}; |
180 | | |
181 | 20 | sc_format_path(cedulauy_data_files[i].path, &dinfo.path); |
182 | 20 | strlcpy(dinfo.app_label, cedulauy_data_files[i].label, sizeof dinfo.app_label); |
183 | 20 | strlcpy(dobj.label, cedulauy_data_files[i].label, sizeof dobj.label); |
184 | 20 | r = sc_pkcs15emu_add_data_object(p15card, &dobj, &dinfo); |
185 | 20 | LOG_TEST_RET(ctx, r, "Cannot add identity data object"); |
186 | 20 | } |
187 | | |
188 | 5 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
189 | 5 | } |
190 | | |
191 | | int |
192 | | sc_pkcs15emu_cedulauy_init_ex(struct sc_pkcs15_card *p15card, struct sc_aid *aid) |
193 | 8.43k | { |
194 | 8.43k | if (p15card->card->type != SC_CARD_TYPE_CEDULAUY && |
195 | 8.43k | p15card->card->type != SC_CARD_TYPE_CEDULAUY_CONTACTLESS) |
196 | 8.43k | return SC_ERROR_WRONG_CARD; |
197 | | |
198 | 5 | return sc_pkcs15emu_cedulauy_init(p15card); |
199 | 8.43k | } |