Debian Codegraph @

lib/hx509/ks_p11.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2004 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "hx_locl.h"cxx . . $ `<file>/lib/hx509/hx_locl.h`/
35 #ifdef HAVE_DLFCN_Hcxx . . $ `include/config.h:305:9`!
36 #include <dlfcn.h>
37 #endif
38
39 #ifdef HAVE_DLOPENcxx . . $ `include/config.h:308:9`!
40
41 #include "ref/pkcs11.h"cxx . . $ `<file>/lib/hx509/ref/pkcs11.h`/
42
43 struct p11_slotcxx . . $ p11_slot# {
44 int flagscxx . . $ p11_slot#flags.;
45 #define P11_SESSIONcxx . . $ `lib/hx509/ks_p11.c:45:9`! 1
46 #define P11_SESSION_IN_USEcxx . . $ `lib/hx509/ks_p11.c:46:9`! 2
47 #define P11_LOGIN_REQcxx . . $ `lib/hx509/ks_p11.c:47:9`! 4
48 #define P11_LOGIN_DONEcxx . . $ `lib/hx509/ks_p11.c:48:9`! 8
49 #define P11_TOKEN_PRESENTcxx . . $ `lib/hx509/ks_p11.c:49:9`! 16
50 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# sessioncxx . . $ p11_slot#session.;
51 CK_SLOT_IDcxx . . $ CK_SLOT_ID# idcxx . . $ p11_slot#id.;
52 CK_BBOOLcxx . . $ CK_BBOOL# tokencxx . . $ p11_slot#token.;
53 char *namecxx . . $ p11_slot#name.;
54 hx509_certscxx . . $ hx509_certs# certscxx . . $ p11_slot#certs.;
55 char *pincxx . . $ p11_slot#pin.;
56 structcxx . . $ p11_slot#$anonymous_type_0# {
57 CK_MECHANISM_TYPE_PTRcxx . . $ CK_MECHANISM_TYPE_PTR# listcxx . . $ p11_slot#$anonymous_type_0#list.;
58 CK_ULONGcxx . . $ CK_ULONG# numcxx . . $ p11_slot#$anonymous_type_0#num.;
59 CK_MECHANISM_INFO_PTRcxx . . $ CK_MECHANISM_INFO_PTR# *infoscxx . . $ p11_slot#$anonymous_type_0#infos.;
60 } mechscxx . . $ p11_slot#mechs.;
61 };
62
63 struct p11_modulecxx . . $ p11_module# {
64 void *dl_handlecxx . . $ p11_module#dl_handle.;
65 CK_FUNCTION_LIST_PTRcxx . . $ CK_FUNCTION_LIST_PTR# funcscxx . . $ p11_module#funcs.;
66 CK_ULONGcxx . . $ CK_ULONG# num_slotscxx . . $ p11_module#num_slots.;
67 unsigned int refcxx . . $ p11_module#ref.;
68 unsigned int selected_slotcxx . . $ p11_module#selected_slot.;
69 struct p11_slotcxx . . $ p11_slot# *slotcxx . . $ p11_module#slot.;
70 };
71
72 #define P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(module,f,args) (*(module)->funcs->C_##f)args
73
74 static int p11_get_sessioncxx . . $ p11_get_session(419055f853d4f27c).(hx509_contextcxx . . $ hx509_context#,
75 struct p11_modulecxx . . $ p11_module# *,
76 struct p11_slotcxx . . $ p11_slot# *,
77 hx509_lockcxx . . $ hx509_lock#,
78 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# *);
79 static int p11_put_sessioncxx . . $ p11_put_session(a588e30abc79b9c4).(struct p11_modulecxx . . $ p11_module# *,
80 struct p11_slotcxx . . $ p11_slot# *,
81 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE#);
82 static void p11_release_modulecxx . . $ p11_release_module(7ae84ea7a200f2a).(struct p11_modulecxx . . $ p11_module# *);
83
84 static int p11_list_keyscxx . . $ p11_list_keys(5bd82b7d8167d2b8).(hx509_contextcxx . . $ hx509_context#,
85 struct p11_modulecxx . . $ p11_module# *,
86 struct p11_slotcxx . . $ p11_slot# *,
87 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE#,
88 hx509_lockcxx . . $ hx509_lock#,
89 hx509_certscxx . . $ hx509_certs# *);
90
91 /*
92 *
93 */
94
95 struct p11_rsacxx . . $ p11_rsa# {
96 struct p11_modulecxx . . $ p11_module# *pcxx . . $ p11_rsa#p.;
97 struct p11_slotcxx . . $ p11_slot# *slotcxx . . $ p11_rsa#slot.;
98 CK_OBJECT_HANDLEcxx . . $ CK_OBJECT_HANDLE# private_keycxx . . $ p11_rsa#private_key.;
99 CK_OBJECT_HANDLEcxx . . $ CK_OBJECT_HANDLE# public_keycxx . . $ p11_rsa#public_key.;
100 };
101
102 static int
103 p11_rsa_public_encryptcxx . . $ p11_rsa_public_encrypt(847f1bc056753588).(int flen,
104 const unsigned char *from,
105 unsigned char *to,
106 RSAcxx . . $ `include/hcrypto/rsa.h:70:9`! *rsa,
107 int padding)
108 {
109 return -1;
110 }
111
112 static int
113 p11_rsa_public_decryptcxx . . $ p11_rsa_public_decrypt(847f1bc056753588).(int flen,
114 const unsigned char *from,
115 unsigned char *to,
116 RSAcxx . . $ `include/hcrypto/rsa.h:70:9`! *rsa,
117 int padding)
118 {
119 return -1;
120 }
121
122
123 static int
124 p11_rsa_private_encryptcxx . . $ p11_rsa_private_encrypt(847f1bc056753588).(int flen,
125 const unsigned char *from,
126 unsigned char *to,
127 RSAcxx . . $ `include/hcrypto/rsa.h:70:9`! *rsa,
128 int padding)
129 {
130 struct p11_rsacxx . . $ p11_rsa# *p11rsa = RSA_get_app_datacxx . . $ `include/hcrypto/rsa.h:55:9`!(rsa);
131 CK_OBJECT_HANDLEcxx . . $ CK_OBJECT_HANDLE# key = p11rsa->private_keycxx . . $ p11_rsa#private_key.;
132 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session;
133 CK_MECHANISMcxx . . $ CK_MECHANISM# mechanism;
134 CK_ULONGcxx . . $ CK_ULONG# ck_sigsize;
135 int ret;
136
137 if (padding != RSA_PKCS1_PADDINGcxx . . $ `include/hcrypto/rsa.h:132:9`!)
138 return -1;
139
140 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&mechanism, 0, sizeof(mechanism));
141 mechanism.mechanismcxx . . $ _CK_MECHANISM#mechanism. = CKM_RSA_PKCScxx . . $ `lib/hx509/ref/pkcs11.h:561:9`!;
142
143 ck_sigsize = RSA_sizecxx . . $ `include/hcrypto/rsa.h:57:9`!(rsa);
144
145 ret = p11_get_sessioncxx . . $ p11_get_session(419055f853d4f27c).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, p11rsa->pcxx . . $ p11_rsa#p., p11rsa->slotcxx . . $ p11_rsa#slot., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &session);
146 if (ret)
147 return -1;
148
149 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p11rsa->p, SignInit, (session, &mechanism, key));
150 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
151 p11_put_sessioncxx . . $ p11_put_session(a588e30abc79b9c4).(p11rsa->pcxx . . $ p11_rsa#p., p11rsa->slotcxx . . $ p11_rsa#slot., session);
152 return -1;
153 }
154
155 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p11rsa->p, Sign,
156 (session, (CK_BYTE *)(intptr_t)from, flen, to, &ck_sigsize));
157 p11_put_sessioncxx . . $ p11_put_session(a588e30abc79b9c4).(p11rsa->pcxx . . $ p11_rsa#p., p11rsa->slotcxx . . $ p11_rsa#slot., session);
158 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!)
159 return -1;
160
161 return ck_sigsize;
162 }
163
164 static int
165 p11_rsa_private_decryptcxx . . $ p11_rsa_private_decrypt(847f1bc056753588).(int flen, const unsigned char *from, unsigned char *to,
166 RSAcxx . . $ `include/hcrypto/rsa.h:70:9`! * rsa, int padding)
167 {
168 struct p11_rsacxx . . $ p11_rsa# *p11rsa = RSA_get_app_datacxx . . $ `include/hcrypto/rsa.h:55:9`!(rsa);
169 CK_OBJECT_HANDLEcxx . . $ CK_OBJECT_HANDLE# key = p11rsa->private_keycxx . . $ p11_rsa#private_key.;
170 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session;
171 CK_MECHANISMcxx . . $ CK_MECHANISM# mechanism;
172 CK_ULONGcxx . . $ CK_ULONG# ck_sigsize;
173 int ret;
174
175 if (padding != RSA_PKCS1_PADDINGcxx . . $ `include/hcrypto/rsa.h:132:9`!)
176 return -1;
177
178 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&mechanism, 0, sizeof(mechanism));
179 mechanism.mechanismcxx . . $ _CK_MECHANISM#mechanism. = CKM_RSA_PKCScxx . . $ `lib/hx509/ref/pkcs11.h:561:9`!;
180
181 ck_sigsize = RSA_sizecxx . . $ `include/hcrypto/rsa.h:57:9`!(rsa);
182
183 ret = p11_get_sessioncxx . . $ p11_get_session(419055f853d4f27c).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, p11rsa->pcxx . . $ p11_rsa#p., p11rsa->slotcxx . . $ p11_rsa#slot., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &session);
184 if (ret)
185 return -1;
186
187 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p11rsa->p, DecryptInit, (session, &mechanism, key));
188 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
189 p11_put_sessioncxx . . $ p11_put_session(a588e30abc79b9c4).(p11rsa->pcxx . . $ p11_rsa#p., p11rsa->slotcxx . . $ p11_rsa#slot., session);
190 return -1;
191 }
192
193 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p11rsa->p, Decrypt,
194 (session, (CK_BYTE *)(intptr_t)from, flen, to, &ck_sigsize));
195 p11_put_sessioncxx . . $ p11_put_session(a588e30abc79b9c4).(p11rsa->pcxx . . $ p11_rsa#p., p11rsa->slotcxx . . $ p11_rsa#slot., session);
196 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!)
197 return -1;
198
199 return ck_sigsize;
200 }
201
202 static int
203 p11_rsa_initcxx . . $ p11_rsa_init(4fb3dbf49f3c57cc).(RSAcxx . . $ `include/hcrypto/rsa.h:70:9`! *rsa)
204 {
205 return 1;
206 }
207
208 static int
209 p11_rsa_finishcxx . . $ p11_rsa_finish(4fb3dbf49f3c57cc).(RSAcxx . . $ `include/hcrypto/rsa.h:70:9`! *rsa)
210 {
211 struct p11_rsacxx . . $ p11_rsa# *p11rsa = RSA_get_app_datacxx . . $ `include/hcrypto/rsa.h:55:9`!(rsa);
212 p11_release_modulecxx . . $ p11_release_module(7ae84ea7a200f2a).(p11rsa->pcxx . . $ p11_rsa#p.);
213 freecxx . . $ free(5d22bdacc48458e8).(p11rsa);
214 return 1;
215 }
216
217 static const RSA_METHODcxx . . $ `include/hcrypto/rsa.h:71:9`! p11_rsa_pkcs1_methodcxx . . $ p11_rsa_pkcs1_method. = {
218 "hx509 PKCS11 PKCS#1 RSA",
219 p11_rsa_public_encryptcxx . . $ p11_rsa_public_encrypt(847f1bc056753588).,
220 p11_rsa_public_decryptcxx . . $ p11_rsa_public_decrypt(847f1bc056753588).,
221 p11_rsa_private_encryptcxx . . $ p11_rsa_private_encrypt(847f1bc056753588).,
222 p11_rsa_private_decryptcxx . . $ p11_rsa_private_decrypt(847f1bc056753588).,
223 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
224 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
225 p11_rsa_initcxx . . $ p11_rsa_init(4fb3dbf49f3c57cc).,
226 p11_rsa_finishcxx . . $ p11_rsa_finish(4fb3dbf49f3c57cc).,
227 0,
228 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
229 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
230 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
231 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!
232 };
233
234 /*
235 *
236 */
237
238 static int
239 p11_mech_infocxx . . $ p11_mech_info(2705319f80492647).(hx509_contextcxx . . $ hx509_context# context,
240 struct p11_modulecxx . . $ p11_module# *p,
241 struct p11_slotcxx . . $ p11_slot# *slot,
242 int num)
243 {
244 CK_ULONGcxx . . $ CK_ULONG# i;
245 int ret;
246
247 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetMechanismList, (slot->id, NULL_PTRcxx . . $ `lib/hx509/ref/pkcs11.h:1604:9`!, &i));
248 if (ret) {
249 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, HX509_PKCS11_NO_MECHcxx . . $ `./hx509_err.h:74:9`!,
250 "Failed to get mech list count for slot %d",
251 num);
252 return HX509_PKCS11_NO_MECHcxx . . $ `./hx509_err.h:74:9`!;
253 }
254 if (i == 0) {
255 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, HX509_PKCS11_NO_MECHcxx . . $ `./hx509_err.h:74:9`!,
256 "no mech supported for slot %d", num);
257 return HX509_PKCS11_NO_MECHcxx . . $ `./hx509_err.h:74:9`!;
258 }
259 slot->mechscxx . . $ p11_slot#mechs..listcxx . . $ p11_slot#$anonymous_type_0#list. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(i, sizeof(slot->mechscxx . . $ p11_slot#mechs..listcxx . . $ p11_slot#$anonymous_type_0#list.[0]));
260 if (slot->mechscxx . . $ p11_slot#mechs..listcxx . . $ p11_slot#$anonymous_type_0#list. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
261 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!,
262 "out of memory");
263 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
264 }
265 slot->mechscxx . . $ p11_slot#mechs..numcxx . . $ p11_slot#$anonymous_type_0#num. = i;
266 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetMechanismList, (slot->id, slot->mechs.list, &i));
267 if (ret) {
268 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, HX509_PKCS11_NO_MECHcxx . . $ `./hx509_err.h:74:9`!,
269 "Failed to get mech list for slot %d",
270 num);
271 return HX509_PKCS11_NO_MECHcxx . . $ `./hx509_err.h:74:9`!;
272 }
273 assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(i == slot->mechs.num);
274
275 slot->mechscxx . . $ p11_slot#mechs..infoscxx . . $ p11_slot#$anonymous_type_0#infos. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(i, sizeof(*slot->mechscxx . . $ p11_slot#mechs..infoscxx . . $ p11_slot#$anonymous_type_0#infos.));
276 if (slot->mechscxx . . $ p11_slot#mechs..listcxx . . $ p11_slot#$anonymous_type_0#list. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
277 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!,
278 "out of memory");
279 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
280 }
281
282 for (i = 0; i < slot->mechscxx . . $ p11_slot#mechs..numcxx . . $ p11_slot#$anonymous_type_0#num.; i++) {
283 slot->mechscxx . . $ p11_slot#mechs..infoscxx . . $ p11_slot#$anonymous_type_0#infos.[i] = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*(slot->mechscxx . . $ p11_slot#mechs..infoscxx . . $ p11_slot#$anonymous_type_0#infos.[0])));
284 if (slot->mechscxx . . $ p11_slot#mechs..infoscxx . . $ p11_slot#$anonymous_type_0#infos.[i] == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
285 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!,
286 "out of memory");
287 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
288 }
289 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetMechanismInfo, (slot->id, slot->mechs.list[i],
290 slot->mechs.infos[i]));
291 if (ret) {
292 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, HX509_PKCS11_NO_MECHcxx . . $ `./hx509_err.h:74:9`!,
293 "Failed to get mech info for slot %d",
294 num);
295 return HX509_PKCS11_NO_MECHcxx . . $ `./hx509_err.h:74:9`!;
296 }
297 }
298
299 return 0;
300 }
301
302 static int
303 p11_init_slotcxx . . $ p11_init_slot(8fb581dd3be8523).(hx509_contextcxx . . $ hx509_context# context,
304 struct p11_modulecxx . . $ p11_module# *p,
305 hx509_lockcxx . . $ hx509_lock# lock,
306 CK_SLOT_IDcxx . . $ CK_SLOT_ID# id,
307 int num,
308 struct p11_slotcxx . . $ p11_slot# *slot)
309 {
310 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session;
311 CK_SLOT_INFOcxx . . $ CK_SLOT_INFO# slot_info;
312 CK_TOKEN_INFOcxx . . $ CK_TOKEN_INFO# token_info;
313 size_t i;
314 int ret;
315
316 slot->certscxx . . $ p11_slot#certs. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
317 slot->idcxx . . $ p11_slot#id. = id;
318
319 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetSlotInfo, (slot->id, &slot_info));
320 if (ret) {
321 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, HX509_PKCS11_TOKEN_CONFUSEDcxx . . $ `./hx509_err.h:75:9`!,
322 "Failed to init PKCS11 slot %d",
323 num);
324 return HX509_PKCS11_TOKEN_CONFUSEDcxx . . $ `./hx509_err.h:75:9`!;
325 }
326
327 for (i = sizeof(slot_info.slotDescriptioncxx . . $ _CK_SLOT_INFO#slotDescription.) - 1; i > 0; i--) {
328 char c = slot_info.slotDescriptioncxx . . $ _CK_SLOT_INFO#slotDescription.[i];
329 if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\0')
330 continue;
331 i++;
332 break;
333 }
334
335 ret = asprintfcxx . . $ asprintf(70ad09b5e10823c4).(&slot->namecxx . . $ p11_slot#name., "%.*s", (int)i,
336 slot_info.slotDescriptioncxx . . $ _CK_SLOT_INFO#slotDescription.);
337 if (ret == -1)
338 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
339
340 if ((slot_info.flagscxx . . $ _CK_SLOT_INFO#flags. & CKF_TOKEN_PRESENTcxx . . $ `lib/hx509/ref/pkcs11.h:256:9`!) == 0)
341 return 0;
342
343 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetTokenInfo, (slot->id, &token_info));
344 if (ret) {
345 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, HX509_PKCS11_NO_TOKENcxx . . $ `./hx509_err.h:73:9`!,
346 "Failed to init PKCS11 slot %d "
347 "with error 0x%08x",
348 num, ret);
349 return HX509_PKCS11_NO_TOKENcxx . . $ `./hx509_err.h:73:9`!;
350 }
351 slot->flagscxx . . $ p11_slot#flags. |= P11_TOKEN_PRESENTcxx . . $ `lib/hx509/ks_p11.c:49:9`!;
352
353 if (token_info.flagscxx . . $ _CK_TOKEN_INFO#flags. & CKF_LOGIN_REQUIREDcxx . . $ `lib/hx509/ref/pkcs11.h:287:9`!)
354 slot->flagscxx . . $ p11_slot#flags. |= P11_LOGIN_REQcxx . . $ `lib/hx509/ks_p11.c:47:9`!;
355
356 ret = p11_get_sessioncxx . . $ p11_get_session(419055f853d4f27c).(context, p, slot, lock, &session);
357 if (ret)
358 return ret;
359
360 ret = p11_mech_infocxx . . $ p11_mech_info(2705319f80492647).(context, p, slot, num);
361 if (ret)
362 goto out;
363
364 ret = p11_list_keyscxx . . $ p11_list_keys(5bd82b7d8167d2b8).(context, p, slot, session, lock, &slot->certscxx . . $ p11_slot#certs.);
365 out:
366 p11_put_sessioncxx . . $ p11_put_session(a588e30abc79b9c4).(p, slot, session);
367
368 return ret;
369 }
370
371 static int
372 p11_get_sessioncxx . . $ p11_get_session(419055f853d4f27c).(hx509_contextcxx . . $ hx509_context# context,
373 struct p11_modulecxx . . $ p11_module# *p,
374 struct p11_slotcxx . . $ p11_slot# *slot,
375 hx509_lockcxx . . $ hx509_lock# lock,
376 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# *psession)
377 {
378 CK_RVcxx . . $ CK_RV# ret;
379
380 if (slot->flagscxx . . $ p11_slot#flags. & P11_SESSION_IN_USEcxx . . $ `lib/hx509/ks_p11.c:46:9`!)
381 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("slot already in session");
382
383 if (slot->flagscxx . . $ p11_slot#flags. & P11_SESSIONcxx . . $ `lib/hx509/ks_p11.c:45:9`!) {
384 slot->flagscxx . . $ p11_slot#flags. |= P11_SESSION_IN_USEcxx . . $ `lib/hx509/ks_p11.c:46:9`!;
385 *psession = slot->sessioncxx . . $ p11_slot#session.;
386 return 0;
387 }
388
389 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, OpenSession, (slot->id,
390 CKF_SERIAL_SESSIONcxx . . $ `lib/hx509/ref/pkcs11.h:339:9`!,
391 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
392 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
393 &slot->session));
394 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
395 if (context)
396 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, HX509_PKCS11_OPEN_SESSIONcxx . . $ `./hx509_err.h:76:9`!,
397 "Failed to OpenSession for slot id %d "
398 "with error: 0x%08x",
399 (int)slot->idcxx . . $ p11_slot#id., ret);
400 return HX509_PKCS11_OPEN_SESSIONcxx . . $ `./hx509_err.h:76:9`!;
401 }
402
403 slot->flagscxx . . $ p11_slot#flags. |= P11_SESSIONcxx . . $ `lib/hx509/ks_p11.c:45:9`!;
404
405 /*
406 * If we have have to login, and haven't tried before and have a
407 * prompter or known to work pin code.
408 *
409 * This code is very conversative and only uses the prompter in
410 * the hx509_lock, the reason is that it's bad to try many
411 * passwords on a pkcs11 token, it might lock up and have to be
412 * unlocked by a administrator.
413 *
414 * XXX try harder to not use pin several times on the same card.
415 */
416
417 if ( (slot->flagscxx . . $ p11_slot#flags. & P11_LOGIN_REQcxx . . $ `lib/hx509/ks_p11.c:47:9`!)
418 && (slot->flagscxx . . $ p11_slot#flags. & P11_LOGIN_DONEcxx . . $ `lib/hx509/ks_p11.c:48:9`!) == 0
419 && (lock || slot->pincxx . . $ p11_slot#pin.))
420 {
421 hx509_promptcxx . . $ hx509_prompt# prompt;
422 char pin[20];
423 char *str;
424
425 if (slot->pincxx . . $ p11_slot#pin. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
426
427 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&prompt, 0, sizeof(prompt));
428
429 ret = asprintfcxx . . $ asprintf(70ad09b5e10823c4).(&str, "PIN code for %s: ", slot->namecxx . . $ p11_slot#name.);
430 if (ret == -1 || str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
431 if (context)
432 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!, "out of memory");
433 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
434 }
435 prompt.promptcxx . . $ hx509_prompt#prompt. = str;
436 prompt.typecxx . . $ hx509_prompt#type. = HX509_PROMPT_TYPE_PASSWORDcxx . . $ HX509_PROMPT_TYPE_PASSWORD.;
437 prompt.replycxx . . $ hx509_prompt#reply..datacxx . . $ heim_base_data#data. = pin;
438 prompt.replycxx . . $ hx509_prompt#reply..lengthcxx . . $ heim_base_data#length. = sizeof(pin);
439
440 ret = hx509_lock_promptcxx . . $ hx509_lock_prompt(848f7215c6d31a01).(lock, &prompt);
441 if (ret) {
442 freecxx . . $ free(5d22bdacc48458e8).(str);
443 if (context)
444 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ret,
445 "Failed to get pin code for slot "
446 "id %d with error: %d",
447 (int)slot->idcxx . . $ p11_slot#id., ret);
448 return ret;
449 }
450 freecxx . . $ free(5d22bdacc48458e8).(str);
451 } else {
452 strlcpycxx . . $ `include/roken.h:146:9`!(pin, slot->pincxx . . $ p11_slot#pin., sizeof(pin));
453 }
454
455 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, Login, (slot->session, CKU_USERcxx . . $ `lib/hx509/ref/pkcs11.h:317:9`!,
456 (unsigned char*)pin, strlen(pin)));
457 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
458 if (context)
459 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, HX509_PKCS11_LOGINcxx . . $ `./hx509_err.h:77:9`!,
460 "Failed to login on slot id %d "
461 "with error: 0x%08x",
462 (int)slot->idcxx . . $ p11_slot#id., ret);
463 switch(ret) {
464 case CKR_PIN_LOCKEDcxx . . $ `lib/hx509/ref/pkcs11.h:1456:9`!:
465 return HX509_PKCS11_PIN_LOCKEDcxx . . $ `./hx509_err.h:80:9`!;
466 case CKR_PIN_EXPIREDcxx . . $ `lib/hx509/ref/pkcs11.h:1455:9`!:
467 return HX509_PKCS11_PIN_EXPIREDcxx . . $ `./hx509_err.h:82:9`!;
468 case CKR_PIN_INCORRECTcxx . . $ `lib/hx509/ref/pkcs11.h:1452:9`!:
469 return HX509_PKCS11_PIN_INCORRECTcxx . . $ `./hx509_err.h:79:9`!;
470 case CKR_USER_PIN_NOT_INITIALIZEDcxx . . $ `lib/hx509/ref/pkcs11.h:1477:9`!:
471 return HX509_PKCS11_PIN_NOT_INITIALIZEDcxx . . $ `./hx509_err.h:81:9`!;
472 default:
473 return HX509_PKCS11_LOGINcxx . . $ `./hx509_err.h:77:9`!;
474 }
475 } else
476 slot->flagscxx . . $ p11_slot#flags. |= P11_LOGIN_DONEcxx . . $ `lib/hx509/ks_p11.c:48:9`!;
477
478 if (slot->pincxx . . $ p11_slot#pin. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
479 slot->pincxx . . $ p11_slot#pin. = strdupcxx . . $ strdup(a8b8e32347182212).(pin);
480 if (slot->pincxx . . $ p11_slot#pin. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
481 if (context)
482 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!,
483 "out of memory");
484 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
485 }
486 }
487 } else
488 slot->flagscxx . . $ p11_slot#flags. |= P11_LOGIN_DONEcxx . . $ `lib/hx509/ks_p11.c:48:9`!;
489
490 slot->flagscxx . . $ p11_slot#flags. |= P11_SESSION_IN_USEcxx . . $ `lib/hx509/ks_p11.c:46:9`!;
491
492 *psession = slot->sessioncxx . . $ p11_slot#session.;
493
494 return 0;
495 }
496
497 static int
498 p11_put_sessioncxx . . $ p11_put_session(a588e30abc79b9c4).(struct p11_modulecxx . . $ p11_module# *p,
499 struct p11_slotcxx . . $ p11_slot# *slot,
500 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session)
501 {
502 if ((slot->flagscxx . . $ p11_slot#flags. & P11_SESSION_IN_USEcxx . . $ `lib/hx509/ks_p11.c:46:9`!) == 0)
503 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("slot not in session");
504 slot->flagscxx . . $ p11_slot#flags. &= ~P11_SESSION_IN_USEcxx . . $ `lib/hx509/ks_p11.c:46:9`!;
505
506 return 0;
507 }
508
509 static int
510 iterate_entriescxx . . $ iterate_entries(486d91f9ac7d065c).(hx509_contextcxx . . $ hx509_context# context,
511 struct p11_modulecxx . . $ p11_module# *p, struct p11_slotcxx . . $ p11_slot# *slot,
512 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session,
513 CK_ATTRIBUTEcxx . . $ CK_ATTRIBUTE# *search_data, int num_search_data,
514 CK_ATTRIBUTEcxx . . $ CK_ATTRIBUTE# *query, int num_query,
515 int (*func)(hx509_contextcxx . . $ hx509_context#,
516 struct p11_modulecxx . . $ p11_module# *, struct p11_slotcxx . . $ p11_slot# *,
517 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session,
518 CK_OBJECT_HANDLEcxx . . $ CK_OBJECT_HANDLE# object,
519 void *, CK_ATTRIBUTEcxx . . $ CK_ATTRIBUTE# *, int), void *ptr)
520 {
521 CK_OBJECT_HANDLEcxx . . $ CK_OBJECT_HANDLE# object;
522 CK_ULONGcxx . . $ CK_ULONG# object_count;
523 int ret, ret2, i;
524
525 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, FindObjectsInit, (session, search_data, num_search_data));
526 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
527 return -1;
528 }
529 while (1) {
530 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, FindObjects, (session, &object, 1, &object_count));
531 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
532 return -1;
533 }
534 if (object_count == 0)
535 break;
536
537 for (i = 0; i < num_query; i++)
538 query[i].pValuecxx . . $ _CK_ATTRIBUTE#pValue. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
539
540 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetAttributeValue,
541 (session, object, query, num_query));
542 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
543 return -1;
544 }
545 for (i = 0; i < num_query; i++) {
546 query[i].pValuecxx . . $ _CK_ATTRIBUTE#pValue. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(query[i].ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen.);
547 if (query[i].pValuecxx . . $ _CK_ATTRIBUTE#pValue. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
548 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
549 goto out;
550 }
551 }
552 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetAttributeValue,
553 (session, object, query, num_query));
554 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
555 ret = -1;
556 goto out;
557 }
558
559 ret = (*func)(context, p, slot, session, object, ptr, query, num_query);
560 if (ret)
561 goto out;
562
563 for (i = 0; i < num_query; i++) {
564 if (query[i].pValuecxx . . $ _CK_ATTRIBUTE#pValue.)
565 freecxx . . $ free(5d22bdacc48458e8).(query[i].pValuecxx . . $ _CK_ATTRIBUTE#pValue.);
566 query[i].pValuecxx . . $ _CK_ATTRIBUTE#pValue. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
567 }
568 }
569 out:
570
571 for (i = 0; i < num_query; i++) {
572 if (query[i].pValuecxx . . $ _CK_ATTRIBUTE#pValue.)
573 freecxx . . $ free(5d22bdacc48458e8).(query[i].pValuecxx . . $ _CK_ATTRIBUTE#pValue.);
574 query[i].pValuecxx . . $ _CK_ATTRIBUTE#pValue. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
575 }
576
577 ret2 = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, FindObjectsFinal, (session));
578 if (ret2 != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
579 return ret2;
580 }
581
582 return ret;
583 }
584
585 static BIGNUMcxx . . $ `include/hcrypto/bn.h:71:9`! *
586 getattr_bncxx . . $ getattr_bn(3d73419114093f7c).(struct p11_modulecxx . . $ p11_module# *p,
587 struct p11_slotcxx . . $ p11_slot# *slot,
588 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session,
589 CK_OBJECT_HANDLEcxx . . $ CK_OBJECT_HANDLE# object,
590 unsigned int type)
591 {
592 CK_ATTRIBUTEcxx . . $ CK_ATTRIBUTE# query;
593 BIGNUMcxx . . $ `include/hcrypto/bn.h:71:9`! *bn;
594 int ret;
595
596 query.typecxx . . $ _CK_ATTRIBUTE#type. = type;
597 query.pValuecxx . . $ _CK_ATTRIBUTE#pValue. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
598 query.ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen. = 0;
599
600 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetAttributeValue,
601 (session, object, &query, 1));
602 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!)
603 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
604
605 query.pValuecxx . . $ _CK_ATTRIBUTE#pValue. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(query.ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen.);
606
607 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetAttributeValue,
608 (session, object, &query, 1));
609 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
610 freecxx . . $ free(5d22bdacc48458e8).(query.pValuecxx . . $ _CK_ATTRIBUTE#pValue.);
611 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
612 }
613 bn = BN_bin2bncxx . . $ `include/hcrypto/bn.h:44:9`!(query.pValuecxx . . $ _CK_ATTRIBUTE#pValue., query.ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
614 freecxx . . $ free(5d22bdacc48458e8).(query.pValuecxx . . $ _CK_ATTRIBUTE#pValue.);
615
616 return bn;
617 }
618
619 static int
620 collect_private_keycxx . . $ collect_private_key(2315b1313187fd17).(hx509_contextcxx . . $ hx509_context# context,
621 struct p11_modulecxx . . $ p11_module# *p, struct p11_slotcxx . . $ p11_slot# *slot,
622 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session,
623 CK_OBJECT_HANDLEcxx . . $ CK_OBJECT_HANDLE# object,
624 void *ptr, CK_ATTRIBUTEcxx . . $ CK_ATTRIBUTE# *query, int num_query)
625 {
626 struct hx509_collectorcxx . . $ hx509_collector# *collector = ptr;
627 hx509_private_keycxx . . $ hx509_private_key# key;
628 heim_octet_stringcxx . . $ heim_octet_string# localKeyId;
629 int ret;
630 RSAcxx . . $ `include/hcrypto/rsa.h:70:9`! *rsa;
631 struct p11_rsacxx . . $ p11_rsa# *p11rsa;
632
633 localKeyId.datacxx . . $ heim_base_data#data. = query[0].pValuecxx . . $ _CK_ATTRIBUTE#pValue.;
634 localKeyId.lengthcxx . . $ heim_base_data#length. = query[0].ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen.;
635
636 ret = hx509_private_key_initcxx . . $ hx509_private_key_init(afee65427e6006c9).(&key, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
637 if (ret)
638 return ret;
639
640 rsa = RSA_newcxx . . $ `include/hcrypto/rsa.h:46:9`!();
641 if (rsa == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
642 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("out of memory");
643
644 /*
645 * The exponent and modulus should always be present according to
646 * the pkcs11 specification, but some smartcards leaves it out,
647 * let ignore any failure to fetch it.
648 */
649 rsa->ncxx . . $ hc_RSA#n. = getattr_bncxx . . $ getattr_bn(3d73419114093f7c).(p, slot, session, object, CKA_MODULUScxx . . $ `lib/hx509/ref/pkcs11.h:470:9`!);
650 rsa->ecxx . . $ hc_RSA#e. = getattr_bncxx . . $ getattr_bn(3d73419114093f7c).(p, slot, session, object, CKA_PUBLIC_EXPONENTcxx . . $ `lib/hx509/ref/pkcs11.h:472:9`!);
651
652 p11rsa = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*p11rsa));
653 if (p11rsa == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
654 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("out of memory");
655
656 p11rsa->pcxx . . $ p11_rsa#p. = p;
657 p11rsa->slotcxx . . $ p11_rsa#slot. = slot;
658 p11rsa->private_keycxx . . $ p11_rsa#private_key. = object;
659
660 if (p->refcxx . . $ p11_module#ref. == 0)
661 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("pkcs11 ref == 0 on alloc");
662 p->refcxx . . $ p11_module#ref.++;
663 if (p->refcxx . . $ p11_module#ref. == UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!)
664 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("pkcs11 ref == UINT_MAX on alloc");
665
666 RSA_set_methodcxx . . $ `include/hcrypto/rsa.h:52:9`!(rsa, &p11_rsa_pkcs1_methodcxx . . $ p11_rsa_pkcs1_method.);
667 ret = RSA_set_app_datacxx . . $ `include/hcrypto/rsa.h:54:9`!(rsa, p11rsa);
668 if (ret != 1)
669 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("RSA_set_app_data");
670
671 hx509_private_key_assign_rsacxx . . $ hx509_private_key_assign_rsa(c1beef5b71d95897).(key, rsa);
672
673 ret = _hx509_collector_private_key_addcxx . . $ _hx509_collector_private_key_add(9677aedc230352a7).(context,
674 collector,
675 hx509_signature_rsacxx . . $ hx509_signature_rsa(a750f1095571417d).(),
676 key,
677 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
678 &localKeyId);
679
680 if (ret) {
681 hx509_private_key_freecxx . . $ hx509_private_key_free(b34f8c13d56d521).(&key);
682 return ret;
683 }
684 return 0;
685 }
686
687 static void
688 p11_cert_releasecxx . . $ p11_cert_release(872e732a6a955ea).(hx509_certcxx . . $ hx509_cert# cert, void *ctx)
689 {
690 struct p11_modulecxx . . $ p11_module# *p = ctx;
691 p11_release_modulecxx . . $ p11_release_module(7ae84ea7a200f2a).(p);
692 }
693
694
695 static int
696 collect_certcxx . . $ collect_cert(2315b1313187fd17).(hx509_contextcxx . . $ hx509_context# context,
697 struct p11_modulecxx . . $ p11_module# *p, struct p11_slotcxx . . $ p11_slot# *slot,
698 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session,
699 CK_OBJECT_HANDLEcxx . . $ CK_OBJECT_HANDLE# object,
700 void *ptr, CK_ATTRIBUTEcxx . . $ CK_ATTRIBUTE# *query, int num_query)
701 {
702 struct hx509_collectorcxx . . $ hx509_collector# *collector = ptr;
703 heim_error_tcxx . . $ heim_error_t# error = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
704 hx509_certcxx . . $ hx509_cert# cert;
705 int ret;
706
707 if ((CK_LONGcxx . . $ CK_LONG#)query[0].ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen. == -1 ||
708 (CK_LONGcxx . . $ CK_LONG#)query[1].ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen. == -1)
709 {
710 return 0;
711 }
712
713 cert = hx509_cert_init_datacxx . . $ hx509_cert_init_data(5789adbc0562fc6e).(context, query[1].pValuecxx . . $ _CK_ATTRIBUTE#pValue.,
714 query[1].ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen., &error);
715 if (cert == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
716 ret = heim_error_get_codecxx . . $ heim_error_get_code(a12d5a0948a20701).(error);
717 heim_releasecxx . . $ heim_release(5d22bdacc48458e8).(error);
718 return ret;
719 }
720
721 if (p->refcxx . . $ p11_module#ref. == 0)
722 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("pkcs11 ref == 0 on alloc");
723 p->refcxx . . $ p11_module#ref.++;
724 if (p->refcxx . . $ p11_module#ref. == UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!)
725 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("pkcs11 ref to high");
726
727 _hx509_cert_set_releasecxx . . $ _hx509_cert_set_release(a0a0e7f5cd3a8b32).(cert, p11_cert_releasecxx . . $ p11_cert_release(872e732a6a955ea)., p);
728
729 {
730 heim_octet_stringcxx . . $ heim_octet_string# data;
731
732 data.datacxx . . $ heim_base_data#data. = query[0].pValuecxx . . $ _CK_ATTRIBUTE#pValue.;
733 data.lengthcxx . . $ heim_base_data#length. = query[0].ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen.;
734
735 _hx509_set_cert_attributecxx . . $ _hx509_set_cert_attribute(6548a3758e3d2132).(context,
736 cert,
737 &asn1_oid_id_pkcs_9_at_localKeyIdcxx . . $ asn1_oid_id_pkcs_9_at_localKeyId.,
738 &data);
739 }
740
741 if ((CK_LONGcxx . . $ CK_LONG#)query[2].ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen. != -1) {
742 char *str;
743
744 ret = asprintfcxx . . $ asprintf(70ad09b5e10823c4).(&str, "%.*s",
745 (int)query[2].ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen., (char *)query[2].pValuecxx . . $ _CK_ATTRIBUTE#pValue.);
746 if (ret != -1 && str) {
747 hx509_cert_set_friendly_namecxx . . $ hx509_cert_set_friendly_name(982adfa8214198fc).(cert, str);
748 freecxx . . $ free(5d22bdacc48458e8).(str);
749 }
750 }
751
752 ret = _hx509_collector_certs_addcxx . . $ _hx509_collector_certs_add(2713188675346810).(context, collector, cert);
753 hx509_cert_freecxx . . $ hx509_cert_free(a4bd42c4d5523b91).(cert);
754
755 return ret;
756 }
757
758
759 static int
760 p11_list_keyscxx . . $ p11_list_keys(5bd82b7d8167d2b8).(hx509_contextcxx . . $ hx509_context# context,
761 struct p11_modulecxx . . $ p11_module# *p,
762 struct p11_slotcxx . . $ p11_slot# *slot,
763 CK_SESSION_HANDLEcxx . . $ CK_SESSION_HANDLE# session,
764 hx509_lockcxx . . $ hx509_lock# lock,
765 hx509_certscxx . . $ hx509_certs# *certs)
766 {
767 struct hx509_collectorcxx . . $ hx509_collector# *collector;
768 CK_OBJECT_CLASScxx . . $ CK_OBJECT_CLASS# key_class;
769 CK_ATTRIBUTEcxx . . $ CK_ATTRIBUTE# search_data[] = {
770 {CKA_CLASScxx . . $ `lib/hx509/ref/pkcs11.h:434:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0},
771 };
772 CK_ATTRIBUTEcxx . . $ CK_ATTRIBUTE# query_data[3] = {
773 {CKA_IDcxx . . $ `lib/hx509/ref/pkcs11.h:457:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0},
774 {CKA_VALUEcxx . . $ `lib/hx509/ref/pkcs11.h:439:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0},
775 {CKA_LABELcxx . . $ `lib/hx509/ref/pkcs11.h:437:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0}
776 };
777 int ret;
778
779 search_data[0].pValuecxx . . $ _CK_ATTRIBUTE#pValue. = &key_class;
780 search_data[0].ulValueLencxx . . $ _CK_ATTRIBUTE#ulValueLen. = sizeof(key_class);
781
782 if (lock == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
783 lock = _hx509_empty_lockcxx . . $ _hx509_empty_lock.;
784
785 ret = _hx509_collector_alloccxx . . $ _hx509_collector_alloc(37ce0e3606632c99).(context, lock, &collector);
786 if (ret)
787 return ret;
788
789 key_class = CKO_PRIVATE_KEYcxx . . $ `lib/hx509/ref/pkcs11.h:350:9`!;
790 ret = iterate_entriescxx . . $ iterate_entries(486d91f9ac7d065c).(context, p, slot, session,
791 search_data, 1,
792 query_data, 1,
793 collect_private_keycxx . . $ collect_private_key(2315b1313187fd17)., collector);
794 if (ret)
795 goto out;
796
797 key_class = CKO_CERTIFICATEcxx . . $ `lib/hx509/ref/pkcs11.h:348:9`!;
798 ret = iterate_entriescxx . . $ iterate_entries(486d91f9ac7d065c).(context, p, slot, session,
799 search_data, 1,
800 query_data, 3,
801 collect_certcxx . . $ collect_cert(2315b1313187fd17)., collector);
802 if (ret)
803 goto out;
804
805 ret = _hx509_collector_collect_certscxx . . $ _hx509_collector_collect_certs(1d56695ac015f9c3).(context, collector, &slot->certscxx . . $ p11_slot#certs.);
806
807 out:
808 _hx509_collector_freecxx . . $ _hx509_collector_free(2b19c98744358106).(collector);
809
810 return ret;
811 }
812
813
814 static int
815 p11_initcxx . . $ p11_init(b8c8de8f8ec1b894).(hx509_contextcxx . . $ hx509_context# context,
816 hx509_certscxx . . $ hx509_certs# certs, void **data, int flags,
817 const char *residue, hx509_lockcxx . . $ hx509_lock# lock)
818 {
819 CK_C_GetFunctionListcxx . . $ CK_C_GetFunctionList# getFuncs;
820 struct p11_modulecxx . . $ p11_module# *p;
821 char *list, *str;
822 int ret;
823
824 *data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
825
826 list = strdupcxx . . $ strdup(a8b8e32347182212).(residue);
827 if (list == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
828 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
829
830 p = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*p));
831 if (p == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
832 freecxx . . $ free(5d22bdacc48458e8).(list);
833 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
834 }
835
836 p->refcxx . . $ p11_module#ref. = 1;
837 p->selected_slotcxx . . $ p11_module#selected_slot. = 0;
838
839 str = strchrcxx . . $ `<external>/13506128340496143423/string.h:264:11`!(list, ',');
840 if (str)
841 *str++ = '\0';
842 while (str) {
843 char *strnext;
844 strnext = strchrcxx . . $ `<external>/13506128340496143423/string.h:264:11`!(str, ',');
845 if (strnext)
846 *strnext++ = '\0';
847 if (strncasecmpcxx . . $ strncasecmp(fd278af2f295fa2e).(str, "slot=", 5) == 0)
848 p->selected_slotcxx . . $ p11_module#selected_slot. = atoicxx . . $ atoi(5c4306d8553b3696).(str + 5);
849 str = strnext;
850 }
851
852 p->dl_handlecxx . . $ p11_module#dl_handle. = dlopencxx . . $ dlopen(fb9ec0bb113f3da8).(list, RTLD_NOWcxx . . $ `<external>/10632388298615623587/dlfcn.h:25:9`!);
853 if (p->dl_handlecxx . . $ p11_module#dl_handle. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
854 ret = HX509_PKCS11_LOADcxx . . $ `./hx509_err.h:78:9`!;
855 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ret,
856 "Failed to open %s: %s", list, dlerrorcxx . . $ dlerror(41208ce6ca65dd58).());
857 goto out;
858 }
859
860 getFuncs = (CK_C_GetFunctionListcxx . . $ CK_C_GetFunctionList#) dlsymcxx . . $ dlsym(5c3a2d84d269ab4b).(p->dl_handlecxx . . $ p11_module#dl_handle., "C_GetFunctionList");
861 if (getFuncs == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
862 ret = HX509_PKCS11_LOADcxx . . $ `./hx509_err.h:78:9`!;
863 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ret,
864 "C_GetFunctionList missing in %s: %s",
865 list, dlerrorcxx . . $ dlerror(41208ce6ca65dd58).());
866 goto out;
867 }
868
869 ret = (*getFuncs)(&p->funcscxx . . $ p11_module#funcs.);
870 if (ret) {
871 ret = HX509_PKCS11_LOADcxx . . $ `./hx509_err.h:78:9`!;
872 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ret,
873 "C_GetFunctionList failed in %s", list);
874 goto out;
875 }
876
877 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, Initialize, (NULL_PTRcxx . . $ `lib/hx509/ref/pkcs11.h:1604:9`!));
878 if (ret != CKR_OKcxx . . $ `lib/hx509/ref/pkcs11.h:1412:9`!) {
879 ret = HX509_PKCS11_TOKEN_CONFUSEDcxx . . $ `./hx509_err.h:75:9`!;
880 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ret,
881 "Failed initialize the PKCS11 module");
882 goto out;
883 }
884
885 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetSlotList, (FALSEcxx . . $ `include/roken-common.h:94:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &p->num_slots));
886 if (ret) {
887 ret = HX509_PKCS11_TOKEN_CONFUSEDcxx . . $ `./hx509_err.h:75:9`!;
888 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ret,
889 "Failed to get number of PKCS11 slots");
890 goto out;
891 }
892
893 if (p->num_slotscxx . . $ p11_module#num_slots. == 0) {
894 ret = HX509_PKCS11_NO_SLOTcxx . . $ `./hx509_err.h:72:9`!;
895 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ret,
896 "Selected PKCS11 module have no slots");
897 goto out;
898 }
899
900
901 {
902 CK_SLOT_ID_PTRcxx . . $ CK_SLOT_ID_PTR# slot_ids;
903 int num_tokens = 0;
904 size_t i;
905
906 slot_ids = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(p->num_slotscxx . . $ p11_module#num_slots. * sizeof(*slot_ids));
907 if (slot_ids == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
908 hx509_clear_error_stringcxx . . $ hx509_clear_error_string(54867dbb71d6169d).(context);
909 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
910 goto out;
911 }
912
913 ret = P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, GetSlotList, (FALSEcxx . . $ `include/roken-common.h:94:9`!, slot_ids, &p->num_slots));
914 if (ret) {
915 freecxx . . $ free(5d22bdacc48458e8).(slot_ids);
916 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, HX509_PKCS11_TOKEN_CONFUSEDcxx . . $ `./hx509_err.h:75:9`!,
917 "Failed getting slot-list from "
918 "PKCS11 module");
919 ret = HX509_PKCS11_TOKEN_CONFUSEDcxx . . $ `./hx509_err.h:75:9`!;
920 goto out;
921 }
922
923 p->slotcxx . . $ p11_module#slot. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(p->num_slotscxx . . $ p11_module#num_slots., sizeof(p->slotcxx . . $ p11_module#slot.[0]));
924 if (p->slotcxx . . $ p11_module#slot. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
925 freecxx . . $ free(5d22bdacc48458e8).(slot_ids);
926 hx509_set_error_stringcxx . . $ hx509_set_error_string(bd6d501f93232e20).(context, 0, ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!,
927 "Failed to get memory for slot-list");
928 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
929 goto out;
930 }
931
932 for (i = 0; i < p->num_slotscxx . . $ p11_module#num_slots.; i++) {
933 if ((p->selected_slotcxx . . $ p11_module#selected_slot. != 0) && (slot_ids[i] != (p->selected_slotcxx . . $ p11_module#selected_slot. - 1)))
934 continue;
935 ret = p11_init_slotcxx . . $ p11_init_slot(8fb581dd3be8523).(context, p, lock, slot_ids[i], i, &p->slotcxx . . $ p11_module#slot.[i]);
936 if (!ret) {
937 if (p->slotcxx . . $ p11_module#slot.[i].flagscxx . . $ p11_slot#flags. & P11_TOKEN_PRESENTcxx . . $ `lib/hx509/ks_p11.c:49:9`!)
938 num_tokens++;
939 }
940 }
941 freecxx . . $ free(5d22bdacc48458e8).(slot_ids);
942 if (ret)
943 goto out;
944 if (num_tokens == 0) {
945 ret = HX509_PKCS11_NO_TOKENcxx . . $ `./hx509_err.h:73:9`!;
946 goto out;
947 }
948 }
949
950 freecxx . . $ free(5d22bdacc48458e8).(list);
951
952 *data = p;
953
954 return 0;
955 out:
956 if (list)
957 freecxx . . $ free(5d22bdacc48458e8).(list);
958 p11_release_modulecxx . . $ p11_release_module(7ae84ea7a200f2a).(p);
959 return ret;
960 }
961
962 static void
963 p11_release_modulecxx . . $ p11_release_module(7ae84ea7a200f2a).(struct p11_modulecxx . . $ p11_module# *p)
964 {
965 size_t i;
966
967 if (p->refcxx . . $ p11_module#ref. == 0)
968 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("pkcs11 ref to low");
969 if (--p->refcxx . . $ p11_module#ref. > 0)
970 return;
971
972 for (i = 0; i < p->num_slotscxx . . $ p11_module#num_slots.; i++) {
973 if (p->slotcxx . . $ p11_module#slot.[i].flagscxx . . $ p11_slot#flags. & P11_SESSION_IN_USEcxx . . $ `lib/hx509/ks_p11.c:46:9`!)
974 _hx509_abortcxx . . $ _hx509_abort(f6bc48f0426f26ee).("pkcs11 module release while session in use");
975 if (p->slotcxx . . $ p11_module#slot.[i].flagscxx . . $ p11_slot#flags. & P11_SESSIONcxx . . $ `lib/hx509/ks_p11.c:45:9`!) {
976 P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, CloseSession, (p->slot[i].session));
977 }
978
979 if (p->slotcxx . . $ p11_module#slot.[i].namecxx . . $ p11_slot#name.)
980 freecxx . . $ free(5d22bdacc48458e8).(p->slotcxx . . $ p11_module#slot.[i].namecxx . . $ p11_slot#name.);
981 if (p->slotcxx . . $ p11_module#slot.[i].pincxx . . $ p11_slot#pin.) {
982 memsetcxx . . $ memset(d1de1e4fb215f1c6).(p->slotcxx . . $ p11_module#slot.[i].pincxx . . $ p11_slot#pin., 0, strlencxx . . $ strlen(751346f8406fd082).(p->slotcxx . . $ p11_module#slot.[i].pincxx . . $ p11_slot#pin.));
983 freecxx . . $ free(5d22bdacc48458e8).(p->slotcxx . . $ p11_module#slot.[i].pincxx . . $ p11_slot#pin.);
984 }
985 if (p->slotcxx . . $ p11_module#slot.[i].mechscxx . . $ p11_slot#mechs..numcxx . . $ p11_slot#$anonymous_type_0#num.) {
986 freecxx . . $ free(5d22bdacc48458e8).(p->slotcxx . . $ p11_module#slot.[i].mechscxx . . $ p11_slot#mechs..listcxx . . $ p11_slot#$anonymous_type_0#list.);
987
988 if (p->slotcxx . . $ p11_module#slot.[i].mechscxx . . $ p11_slot#mechs..infoscxx . . $ p11_slot#$anonymous_type_0#infos.) {
989 size_t j;
990
991 for (j = 0 ; j < p->slotcxx . . $ p11_module#slot.[i].mechscxx . . $ p11_slot#mechs..numcxx . . $ p11_slot#$anonymous_type_0#num. ; j++)
992 freecxx . . $ free(5d22bdacc48458e8).(p->slotcxx . . $ p11_module#slot.[i].mechscxx . . $ p11_slot#mechs..infoscxx . . $ p11_slot#$anonymous_type_0#infos.[j]);
993 freecxx . . $ free(5d22bdacc48458e8).(p->slotcxx . . $ p11_module#slot.[i].mechscxx . . $ p11_slot#mechs..infoscxx . . $ p11_slot#$anonymous_type_0#infos.);
994 }
995 }
996 }
997 freecxx . . $ free(5d22bdacc48458e8).(p->slotcxx . . $ p11_module#slot.);
998
999 if (p->funcscxx . . $ p11_module#funcs.)
1000 P11FUNCcxx . . $ `lib/hx509/ks_p11.c:72:9`!(p, Finalize, (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!));
1001
1002 if (p->dl_handlecxx . . $ p11_module#dl_handle.)
1003 dlclosecxx . . $ dlclose(dedf1e9b1c614a07).(p->dl_handlecxx . . $ p11_module#dl_handle.);
1004
1005 memsetcxx . . $ memset(d1de1e4fb215f1c6).(p, 0, sizeof(*p));
1006 freecxx . . $ free(5d22bdacc48458e8).(p);
1007 }
1008
1009 static int
1010 p11_freecxx . . $ p11_free(18e6780d0262d322).(hx509_certscxx . . $ hx509_certs# certs, void *data)
1011 {
1012 struct p11_modulecxx . . $ p11_module# *p = data;
1013 size_t i;
1014
1015 for (i = 0; i < p->num_slotscxx . . $ p11_module#num_slots.; i++) {
1016 if (p->slotcxx . . $ p11_module#slot.[i].certscxx . . $ p11_slot#certs.)
1017 hx509_certs_freecxx . . $ hx509_certs_free(5dd004a9264d2921).(&p->slotcxx . . $ p11_module#slot.[i].certscxx . . $ p11_slot#certs.);
1018 }
1019 p11_release_modulecxx . . $ p11_release_module(7ae84ea7a200f2a).(p);
1020 return 0;
1021 }
1022
1023 struct p11_cursorcxx . . $ p11_cursor# {
1024 hx509_certscxx . . $ hx509_certs# certscxx . . $ p11_cursor#certs.;
1025 void *cursorcxx . . $ p11_cursor#cursor.;
1026 };
1027
1028 static int
1029 p11_iter_startcxx . . $ p11_iter_start(99af47b82533a8e0).(hx509_contextcxx . . $ hx509_context# context,
1030 hx509_certscxx . . $ hx509_certs# certs, void *data, void **cursor)
1031 {
1032 struct p11_modulecxx . . $ p11_module# *p = data;
1033 struct p11_cursorcxx . . $ p11_cursor# *c;
1034 int ret;
1035 size_t i;
1036
1037 c = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(*c));
1038 if (c == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
1039 hx509_clear_error_stringcxx . . $ hx509_clear_error_string(54867dbb71d6169d).(context);
1040 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
1041 }
1042 ret = hx509_certs_initcxx . . $ hx509_certs_init(69cd6dcc0188a099).(context, "MEMORY:pkcs11-iter", 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &c->certscxx . . $ p11_cursor#certs.);
1043 if (ret) {
1044 freecxx . . $ free(5d22bdacc48458e8).(c);
1045 return ret;
1046 }
1047
1048 for (i = 0 ; i < p->num_slotscxx . . $ p11_module#num_slots.; i++) {
1049 if (p->slotcxx . . $ p11_module#slot.[i].certscxx . . $ p11_slot#certs. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
1050 continue;
1051 ret = hx509_certs_mergecxx . . $ hx509_certs_merge(187a1bc5f6969f3).(context, c->certscxx . . $ p11_cursor#certs., p->slotcxx . . $ p11_module#slot.[i].certscxx . . $ p11_slot#certs.);
1052 if (ret) {
1053 hx509_certs_freecxx . . $ hx509_certs_free(5dd004a9264d2921).(&c->certscxx . . $ p11_cursor#certs.);
1054 freecxx . . $ free(5d22bdacc48458e8).(c);
1055 return ret;
1056 }
1057 }
1058
1059 ret = hx509_certs_start_seqcxx . . $ hx509_certs_start_seq(75da60395aa3d685).(context, c->certscxx . . $ p11_cursor#certs., &c->cursorcxx . . $ p11_cursor#cursor.);
1060 if (ret) {
1061 hx509_certs_freecxx . . $ hx509_certs_free(5dd004a9264d2921).(&c->certscxx . . $ p11_cursor#certs.);
1062 freecxx . . $ free(5d22bdacc48458e8).(c);
1063 return 0;
1064 }
1065 *cursor = c;
1066
1067 return 0;
1068 }
1069
1070 static int
1071 p11_itercxx . . $ p11_iter(cad85e8772a144a6).(hx509_contextcxx . . $ hx509_context# context,
1072 hx509_certscxx . . $ hx509_certs# certs, void *data, void *cursor, hx509_certcxx . . $ hx509_cert# *cert)
1073 {
1074 struct p11_cursorcxx . . $ p11_cursor# *c = cursor;
1075 return hx509_certs_next_certcxx . . $ hx509_certs_next_cert(753edddbbe48e15a).(context, c->certscxx . . $ p11_cursor#certs., c->cursorcxx . . $ p11_cursor#cursor., cert);
1076 }
1077
1078 static int
1079 p11_iter_endcxx . . $ p11_iter_end(c6690855a566acb7).(hx509_contextcxx . . $ hx509_context# context,
1080 hx509_certscxx . . $ hx509_certs# certs, void *data, void *cursor)
1081 {
1082 struct p11_cursorcxx . . $ p11_cursor# *c = cursor;
1083 int ret;
1084 ret = hx509_certs_end_seqcxx . . $ hx509_certs_end_seq(7ba52348774ec517).(context, c->certscxx . . $ p11_cursor#certs., c->cursorcxx . . $ p11_cursor#cursor.);
1085 hx509_certs_freecxx . . $ hx509_certs_free(5dd004a9264d2921).(&c->certscxx . . $ p11_cursor#certs.);
1086 freecxx . . $ free(5d22bdacc48458e8).(c);
1087 return ret;
1088 }
1089
1090 #define MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(x) { "unknown-flag-" #x, x }
1091 static struct unitscxx . . $ units# mechflagscxx . . $ mechflags.[] = {
1092 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x80000000),
1093 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x40000000),
1094 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x20000000),
1095 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x10000000),
1096 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x08000000),
1097 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x04000000),
1098 {"ec-compress", 0x2000000 },
1099 {"ec-uncompress", 0x1000000 },
1100 {"ec-namedcurve", 0x0800000 },
1101 {"ec-ecparameters", 0x0400000 },
1102 {"ec-f-2m", 0x0200000 },
1103 {"ec-f-p", 0x0100000 },
1104 {"derive", 0x0080000 },
1105 {"unwrap", 0x0040000 },
1106 {"wrap", 0x0020000 },
1107 {"genereate-key-pair", 0x0010000 },
1108 {"generate", 0x0008000 },
1109 {"verify-recover", 0x0004000 },
1110 {"verify", 0x0002000 },
1111 {"sign-recover", 0x0001000 },
1112 {"sign", 0x0000800 },
1113 {"digest", 0x0000400 },
1114 {"decrypt", 0x0000200 },
1115 {"encrypt", 0x0000100 },
1116 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x00080),
1117 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x00040),
1118 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x00020),
1119 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x00010),
1120 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x00008),
1121 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x00004),
1122 MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!(0x00002),
1123 {"hw", 0x0000001 },
1124 { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0x0000000 }
1125 };
1126 #undef MECHFLAGcxx . . $ `lib/hx509/ks_p11.c:1090:9`!
1127
1128 static int
1129 p11_printinfocxx . . $ p11_printinfo(bbf3b20f592158b8).(hx509_contextcxx . . $ hx509_context# context,
1130 hx509_certscxx . . $ hx509_certs# certs,
1131 void *data,
1132 int (*func)(void *, const char *),
1133 void *ctx)
1134 {
1135 struct p11_modulecxx . . $ p11_module# *p = data;
1136 size_t i, j;
1137
1138 _hx509_pi_printfcxx . . $ _hx509_pi_printf(b25473d2b135ce8b).(func, ctx, "pkcs11 driver with %d slot%s",
1139 p->num_slotscxx . . $ p11_module#num_slots., p->num_slotscxx . . $ p11_module#num_slots. > 1 ? "s" : "");
1140
1141 for (i = 0; i < p->num_slotscxx . . $ p11_module#num_slots.; i++) {
1142 struct p11_slotcxx . . $ p11_slot# *s = &p->slotcxx . . $ p11_module#slot.[i];
1143
1144 _hx509_pi_printfcxx . . $ _hx509_pi_printf(b25473d2b135ce8b).(func, ctx, "slot %d: id: %d name: %s flags: %08x",
1145 i, (int)s->idcxx . . $ p11_slot#id., s->namecxx . . $ p11_slot#name., s->flagscxx . . $ p11_slot#flags.);
1146
1147 _hx509_pi_printfcxx . . $ _hx509_pi_printf(b25473d2b135ce8b).(func, ctx, "number of supported mechanisms: %lu",
1148 (unsigned long)s->mechscxx . . $ p11_slot#mechs..numcxx . . $ p11_slot#$anonymous_type_0#num.);
1149 for (j = 0; j < s->mechscxx . . $ p11_slot#mechs..numcxx . . $ p11_slot#$anonymous_type_0#num.; j++) {
1150 const char *mechname = "unknown";
1151 char flags[256], unknownname[40];
1152 #define MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(s,n) case s: mechname = n; break
1153 switch(s->mechscxx . . $ p11_slot#mechs..listcxx . . $ p11_slot#$anonymous_type_0#list.[j]) {
1154 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_RSA_PKCS_KEY_PAIR_GENcxx . . $ `lib/hx509/ref/pkcs11.h:560:9`!, "rsa-pkcs-key-pair-gen");
1155 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_RSA_PKCScxx . . $ `lib/hx509/ref/pkcs11.h:561:9`!, "rsa-pkcs");
1156 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_RSA_X_509cxx . . $ `lib/hx509/ref/pkcs11.h:563:9`!, "rsa-x-509");
1157 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_MD5_RSA_PKCScxx . . $ `lib/hx509/ref/pkcs11.h:565:9`!, "md5-rsa-pkcs");
1158 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA1_RSA_PKCScxx . . $ `lib/hx509/ref/pkcs11.h:566:9`!, "sha1-rsa-pkcs");
1159 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA256_RSA_PKCScxx . . $ `lib/hx509/ref/pkcs11.h:588:9`!, "sha256-rsa-pkcs");
1160 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA384_RSA_PKCScxx . . $ `lib/hx509/ref/pkcs11.h:589:9`!, "sha384-rsa-pkcs");
1161 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA512_RSA_PKCScxx . . $ `lib/hx509/ref/pkcs11.h:590:9`!, "sha512-rsa-pkcs");
1162 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_RIPEMD160_RSA_PKCScxx . . $ `lib/hx509/ref/pkcs11.h:568:9`!, "ripemd160-rsa-pkcs");
1163 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_RSA_PKCS_OAEPcxx . . $ `lib/hx509/ref/pkcs11.h:569:9`!, "rsa-pkcs-oaep");
1164 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA512_HMACcxx . . $ `lib/hx509/ref/pkcs11.h:654:9`!, "sha512-hmac");
1165 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA512cxx . . $ `lib/hx509/ref/pkcs11.h:653:9`!, "sha512");
1166 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA384_HMACcxx . . $ `lib/hx509/ref/pkcs11.h:651:9`!, "sha384-hmac");
1167 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA384cxx . . $ `lib/hx509/ref/pkcs11.h:650:9`!, "sha384");
1168 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA256_HMACcxx . . $ `lib/hx509/ref/pkcs11.h:645:9`!, "sha256-hmac");
1169 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA256cxx . . $ `lib/hx509/ref/pkcs11.h:644:9`!, "sha256");
1170 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_SHA_1cxx . . $ `lib/hx509/ref/pkcs11.h:635:9`!, "sha1");
1171 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_MD5cxx . . $ `lib/hx509/ref/pkcs11.h:632:9`!, "md5");
1172 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_RIPEMD160cxx . . $ `lib/hx509/ref/pkcs11.h:641:9`!, "ripemd-160");
1173 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_DES_ECBcxx . . $ `lib/hx509/ref/pkcs11.h:605:9`!, "des-ecb");
1174 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_DES_CBCcxx . . $ `lib/hx509/ref/pkcs11.h:606:9`!, "des-cbc");
1175 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_AES_ECBcxx . . $ `lib/hx509/ref/pkcs11.h:815:9`!, "aes-ecb");
1176 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_AES_CBCcxx . . $ `lib/hx509/ref/pkcs11.h:816:9`!, "aes-cbc");
1177 MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!(CKM_DH_PKCS_PARAMETER_GENcxx . . $ `lib/hx509/ref/pkcs11.h:851:9`!, "dh-pkcs-parameter-gen");
1178 default:
1179 snprintfcxx . . $ snprintf(ea502d73e74c754c).(unknownname, sizeof(unknownname),
1180 "unknown-mech-%lu",
1181 (unsigned long)s->mechscxx . . $ p11_slot#mechs..listcxx . . $ p11_slot#$anonymous_type_0#list.[j]);
1182 mechname = unknownname;
1183 break;
1184 }
1185 #undef MECHNAMEcxx . . $ `lib/hx509/ks_p11.c:1152:9`!
1186 unparse_flagscxx . . $ unparse_flags(c42ab0696321da01).(s->mechscxx . . $ p11_slot#mechs..infoscxx . . $ p11_slot#$anonymous_type_0#infos.[j]->flagscxx . . $ _CK_MECHANISM_INFO#flags., mechflagscxx . . $ mechflags.,
1187 flags, sizeof(flags));
1188
1189 _hx509_pi_printfcxx . . $ _hx509_pi_printf(b25473d2b135ce8b).(func, ctx, " %s: %s", mechname, flags);
1190 }
1191 }
1192
1193 return 0;
1194 }
1195
1196 static struct hx509_keyset_opscxx . . $ hx509_keyset_ops# keyset_pkcs11cxx . . $ keyset_pkcs11. = {
1197 "PKCS11",
1198 0,
1199 p11_initcxx . . $ p11_init(b8c8de8f8ec1b894).,
1200 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
1201 p11_freecxx . . $ p11_free(18e6780d0262d322).,
1202 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
1203 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
1204 p11_iter_startcxx . . $ p11_iter_start(99af47b82533a8e0).,
1205 p11_itercxx . . $ p11_iter(cad85e8772a144a6).,
1206 p11_iter_endcxx . . $ p11_iter_end(c6690855a566acb7).,
1207 p11_printinfocxx . . $ p11_printinfo(bbf3b20f592158b8).,
1208 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
1209 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!
1210 };
1211
1212 #endif /* HAVE_DLOPEN */
1213
1214 void
1215 _hx509_ks_pkcs11_registercxx . . $ _hx509_ks_pkcs11_register(54867dbb71d6169d).(hx509_contextcxx . . $ hx509_context# context)
1216 {
1217 #ifdef HAVE_DLOPENcxx . . $ `include/config.h:308:9`!
1218 _hx509_ks_registercxx . . $ _hx509_ks_register(a9b934d8821b5346).(context, &keyset_pkcs11cxx . . $ keyset_pkcs11.);
1219 #endif
1220 }