Debian Codegraph @

src/pam/auth-method-x509/dirmngr.c

raw symbols SCIP
1 /* dirmngr.c - Poldi dirmngr access layer
2 * Copyright (C) 2002, 2003, 2005, 2007, 2008 Free Software Foundation, Inc.
3 *
4 * This file is part of Poldi.
5 *
6 * Poldi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Poldi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* See dirmngr.h for a description of the dirmngr access API
21 implemented by this file. */
22
23 #include <poldi.h>cxx . . $ `<file>/src/poldi.h`/
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <assert.h>
32 #include <ctype.h>
33
34 #include <gcrypt.h>
35 #include <ksba.h>
36
37 #include "assuan.h"cxx . . $ `<file>/src/assuan/assuan.h`/
38 #include "util/util.h"cxx . . $ `<file>/src/util/util.h`/
39 #include "util/membuf.h"cxx . . $ `<file>/src/util/membuf.h`/
40 #include "dirmngr.h"cxx . . $ `<file>/src/pam/auth-method-x509/dirmngr.h`/
41
42 #include <util/simplelog.h>cxx . . $ `<file>/src/util/simplelog.h`/
43
44
45
46 /* This is the a "dirmngr context". */
47 struct dirmngr_ctx_scxx . . $ dirmngr_ctx_s#
48 {
49 assuan_context_tcxx . . $ assuan_context_t# assuancxx . . $ dirmngr_ctx_s#assuan.; /* Assuan context for accessing
50 dirmngr. */
51 log_handle_tcxx . . $ log_handle_t# log_handlecxx . . $ dirmngr_ctx_s#log_handle.; /* Handle for logging messages. */
52 };
53
54 /* This structure is used for passing data to the "data callback"
55 during assuan transactions. */
56 struct lookup_parm_scxx . . $ lookup_parm_s# {
57 void (*cbcxx . . $ lookup_parm_s#cb.) (void *, ksba_cert_tcxx . . $ ksba_cert_t#);
58 void *cb_valuecxx . . $ lookup_parm_s#cb_value.;
59 membuf_tcxx . . $ membuf_t# datacxx . . $ lookup_parm_s#data.;
60 gpg_error_tcxx . . $ gpg_error_t# errcxx . . $ lookup_parm_s#err.;
61 dirmngr_ctx_tcxx . . $ dirmngr_ctx_t# ctxcxx . . $ lookup_parm_s#ctx.;
62 };
63
64
65
66 static struct dirmngr_ctx_scxx . . $ dirmngr_ctx_s# dirmngr_ctx_initcxx . . $ dirmngr_ctx_init.; /* For initialization
67 purpose. */
68
69 /* Connect to a running dirmngr through the local socket named by
70 SOCK, using LOG_HANDLE as logging handle and flags FLAGS. The new
71 context is stored in *CTX. Returns proper error code. */
72 gpg_error_tcxx . . $ gpg_error_t#
73 dirmngr_connectcxx . . $ dirmngr_connect(7038681cf8e299d2). (dirmngr_ctx_tcxx . . $ dirmngr_ctx_t# *ctx,
74 const char *sock,
75 unsigned int flags,
76 log_handle_tcxx . . $ log_handle_t# log_handle)
77 {
78 dirmngr_ctx_tcxx . . $ dirmngr_ctx_t# context;
79 gpg_error_tcxx . . $ gpg_error_t# err;
80
81 /* Allocate. */
82 context = xtrymalloccxx . . $ `src/poldi.h:35:9`! (sizeof (*context));
83 if (!context)
84 {
85 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
86 goto out;
87 }
88
89 /* Initialize with zeroes. */
90 *context = dirmngr_ctx_initcxx . . $ dirmngr_ctx_init.;
91
92 /* Connect to assuan server. */
93 err = assuan_socket_connectcxx . . $ `src/assuan/assuan.h:115:9`! (&context->assuancxx . . $ dirmngr_ctx_s#assuan., sock, -1);
94 if (err)
95 goto out;
96
97 /* Install logging handle in new context. */
98 context->log_handlecxx . . $ dirmngr_ctx_s#log_handle. = log_handle;
99
100 *ctx = context;
101
102 out:
103
104 if (err)
105 xfreecxx . . $ `src/poldi.h:39:9`! (context);
106
107 return err;
108 }
109
110 /* Close the dirmngr connection associated with CTX and release all
111 related resources. */
112 void
113 dirmngr_disconnectcxx . . $ dirmngr_disconnect(677575fc8d6c46df). (dirmngr_ctx_tcxx . . $ dirmngr_ctx_t# ctx)
114 {
115 if (ctx)
116 {
117 if (ctx->assuancxx . . $ dirmngr_ctx_s#assuan.)
118 assuan_disconnectcxx . . $ `src/assuan/assuan.h:117:9`! (ctx->assuancxx . . $ dirmngr_ctx_s#assuan.);
119 xfreecxx . . $ `src/poldi.h:39:9`! (ctx);
120 }
121 }
122
123
124
125
126 /* Communication structure for the certificate inquire callback. For
127 the assuan VALIDATE command. */
128 struct inq_cert_parm_scxx . . $ inq_cert_parm_s#
129 {
130 dirmngr_ctx_tcxx . . $ dirmngr_ctx_t# ctxcxx . . $ inq_cert_parm_s#ctx.; /* Dirmngr context of the caller. */
131 const unsigned char *certcxx . . $ inq_cert_parm_s#cert.; /* Raw certificate in question. */
132 size_t certlencxx . . $ inq_cert_parm_s#certlen.; /* Length of certificate in bytes. */
133 };
134
135 /* Callback for the inquire function to send back the
136 certificate. Sending of a certificate to Dirmngr is used for
137 validation purpose. */
138 static int
139 inq_certcxx . . $ inq_cert(f53b6be6549e6d95). (void *opaque, const char *line)
140 {
141 struct inq_cert_parm_scxx . . $ inq_cert_parm_s# *parm = opaque;
142 gpg_error_tcxx . . $ gpg_error_t# err;
143
144 if (!strncmpcxx . . $ strncmp(fd278af2f295fa2e). (line, "TARGETCERT", 10) && (line[10] == ' ' || !line[10]))
145 /* Send back the certificate we want to validate. */
146 err = assuan_send_datacxx . . $ `src/assuan/assuan.h:126:9`! (parm->ctxcxx . . $ inq_cert_parm_s#ctx.->assuancxx . . $ dirmngr_ctx_s#assuan., parm->certcxx . . $ inq_cert_parm_s#cert., parm->certlencxx . . $ inq_cert_parm_s#certlen.);
147 else if ((!strncmpcxx . . $ strncmp(fd278af2f295fa2e). (line, "SENDCERT", 8) && (line[8] == ' ' || !line[8]))
148 || (!strncmpcxx . . $ strncmp(fd278af2f295fa2e). (line, "SENDCERT_SKI", 12) && (line[12]==' ' || !line[12]))
149 || (!strncmpcxx . . $ strncmp(fd278af2f295fa2e). (line, "SENDISSUERCERT", 14) && (line[14] == ' ' || !line[14])))
150 {
151 /* We don't support this but dirmngr might ask for it. So
152 simply ignore it by sending back an empty value. */
153 log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (parm->ctxcxx . . $ inq_cert_parm_s#ctx.->log_handlecxx . . $ dirmngr_ctx_s#log_handle., "ignored inquiry from dirmngr: `%s'", line);
154 err = assuan_send_datacxx . . $ `src/assuan/assuan.h:126:9`! (parm->ctxcxx . . $ inq_cert_parm_s#ctx.->assuancxx . . $ dirmngr_ctx_s#assuan., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
155 if (err)
156 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (parm->ctxcxx . . $ inq_cert_parm_s#ctx.->log_handlecxx . . $ dirmngr_ctx_s#log_handle.,
157 "failed to send back empty value to dirmngr: %s",
158 gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
159 }
160 else
161 {
162 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (parm->ctxcxx . . $ inq_cert_parm_s#ctx.->log_handlecxx . . $ dirmngr_ctx_s#log_handle., "unsupported assuan inquiry `%s'", line);
163 err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_ASS_UNKNOWN_INQUIREcxx . . $ GPG_ERR_ASS_UNKNOWN_INQUIRE.);
164 /* Note that this error will let assuan_transact terminate
165 immediately instead of return the error to the caller. It is
166 not clear whether this is the desired behaviour - it may
167 change in future. */
168 }
169
170 return err;
171 }
172
173 /* Validate the certificate CERT through the dirmngr context
174 CTX. Returns zero in case the certificate is considered valid, an
175 appropriate error code otherwise. */
176 gpg_error_tcxx . . $ gpg_error_t#
177 dirmngr_validatecxx . . $ dirmngr_validate(c3a3f7162da2be4). (dirmngr_ctx_tcxx . . $ dirmngr_ctx_t# ctx, ksba_cert_tcxx . . $ ksba_cert_t# cert)
178 {
179 struct inq_cert_parm_scxx . . $ inq_cert_parm_s# parm;
180 const unsigned char *image;
181 size_t imagelen;
182 gpg_error_tcxx . . $ gpg_error_t# err;
183
184 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (ctx);
185 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (cert);
186
187 err = 0;
188
189 /* Retrieve pointer to the raw certificate data. */
190 image = ksba_cert_get_imagecxx . . $ ksba_cert_get_image(aec3f427fc78d780). (cert, &imagelen);
191 if (!image)
192 {
193 err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_INTERNALcxx . . $ GPG_ERR_INTERNAL.); /* FIXME: what error code? */
194 goto out;
195 }
196
197 /* Setup PARM structure. */
198 parm.ctxcxx . . $ inq_cert_parm_s#ctx. = ctx;
199 parm.certcxx . . $ inq_cert_parm_s#cert. = image;
200 parm.certlencxx . . $ inq_cert_parm_s#certlen. = imagelen;
201
202 /* Validate certificate. INQ_CERT is the callback that will send the
203 certificate in question to dirmngr. */
204 err = assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx->assuancxx . . $ dirmngr_ctx_s#assuan., "VALIDATE", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
205 inq_certcxx . . $ inq_cert(f53b6be6549e6d95)., &parm,
206 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
207 out:
208
209 return err;
210 }
211
212
213
214 /* Lookup helpers*/
215 static int
216 lookup_cbcxx . . $ lookup_cb(72e086fd9bc8337a). (void *opaque, const void *buffer, size_t length)
217 {
218 struct lookup_parm_scxx . . $ lookup_parm_s# *parm = opaque;
219
220 if (parm->errcxx . . $ lookup_parm_s#err.)
221 /* Already triggered an error => do nothing. */
222 return 0;
223
224 if (buffer)
225 {
226 /* Add more data into the buffer and return. */
227 put_membufcxx . . $ put_membuf(1ceb7b077846ab92). (&parm->datacxx . . $ lookup_parm_s#data., buffer, length);
228 return 0;
229 }
230 else
231 {
232 /* END encountered - process what we have. */
233
234 size_t len;
235 char *buf;
236 ksba_cert_tcxx . . $ ksba_cert_t# cert;
237 gpg_error_tcxx . . $ gpg_error_t# rc;
238
239 /* Retrieve pointer to accumulated data. */
240 buf = get_membufcxx . . $ get_membuf(5dfc76f6663417c1). (&parm->datacxx . . $ lookup_parm_s#data., &len);
241 if (!buf)
242 {
243 parm->errcxx . . $ lookup_parm_s#err. = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_ENOMEMcxx . . $ GPG_ERR_ENOMEM.);
244 return 0;
245 }
246
247 /* Create new certificate object from raw data. */
248 rc = ksba_cert_newcxx . . $ ksba_cert_new(a3a41e1420d5bfa5). (&cert);
249 if (rc)
250 {
251 parm->errcxx . . $ lookup_parm_s#err. = rc;
252 return 0;
253 }
254 rc = ksba_cert_init_from_memcxx . . $ ksba_cert_init_from_mem(7e6a930927ce1d45). (cert, buf, len);
255 if (rc)
256 {
257 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (parm->ctxcxx . . $ lookup_parm_s#ctx.->log_handlecxx . . $ dirmngr_ctx_s#log_handle.,
258 "failed to create new ksba certificate object: %s",
259 gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (rc));
260 /* FIXME: better error handling? -mo */
261 }
262 else
263 {
264 parm->cbcxx . . $ lookup_parm_s#cb. (parm->cb_valuecxx . . $ lookup_parm_s#cb_value., cert);
265 }
266
267 ksba_cert_releasecxx . . $ ksba_cert_release(e650ffcbc4dd6f60). (cert);
268 init_membufcxx . . $ init_membuf(917e2e0aeaea34de). (&parm->datacxx . . $ lookup_parm_s#data., 4096); /* FIXME: what is this for?? -mo */
269 }
270
271 return 0;
272 }
273
274 /* FIXME: simplify -mo */
275 static void
276 lookup_url_cbcxx . . $ lookup_url_cb(c76cc09c3f16abf7). (void *opaque, ksba_cert_tcxx . . $ ksba_cert_t# cert)
277 {
278 ksba_cert_tcxx . . $ ksba_cert_t# *cert_cp = opaque;
279
280 if (*cert_cp)
281 /* Does already contain a cert. */
282 return;
283
284 ksba_cert_refcxx . . $ ksba_cert_ref(e650ffcbc4dd6f60). (cert);
285 *cert_cp = cert;
286 }
287
288 /* Retrieve the certificate stored under the url URL through the
289 dirmngr context CTX and store it in *CERTIFICATE. Returns proper
290 error code. */
291 gpg_error_tcxx . . $ gpg_error_t#
292 dirmngr_lookup_urlcxx . . $ dirmngr_lookup_url(ed84806f3812eb2c). (dirmngr_ctx_tcxx . . $ dirmngr_ctx_t# ctx,
293 const char *url, ksba_cert_tcxx . . $ ksba_cert_t# *certificate)
294 {
295 gpg_error_tcxx . . $ gpg_error_t# err;
296 char line[ASSUAN_LINELENGTHcxx . . $ `src/assuan/assuan.h:376:9`!];
297 struct lookup_parm_scxx . . $ lookup_parm_s# parm;
298 ksba_cert_tcxx . . $ ksba_cert_t# cert;
299
300 cert = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
301 err = 0;
302
303 /* Prepare command. */
304
305 snprintfcxx . . $ snprintf(ea502d73e74c754c). (line, DIMcxx . . $ `src/util/util.h:39:9`!(line)-1, "LOOKUP --url %s", url);
306 line[DIMcxx . . $ `src/util/util.h:39:9`!(line)-1] = 0;
307
308 parm.cbcxx . . $ lookup_parm_s#cb. = lookup_url_cbcxx . . $ lookup_url_cb(c76cc09c3f16abf7).;
309 parm.cb_valuecxx . . $ lookup_parm_s#cb_value. = &cert;
310 parm.errcxx . . $ lookup_parm_s#err. = 0;
311 init_membufcxx . . $ init_membuf(917e2e0aeaea34de). (&parm.datacxx . . $ lookup_parm_s#data., 4096);
312 parm.ctxcxx . . $ lookup_parm_s#ctx. = ctx;
313
314 /* Execute command. */
315
316 err = assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx->assuancxx . . $ dirmngr_ctx_s#assuan., line, lookup_cbcxx . . $ lookup_cb(72e086fd9bc8337a)., &parm,
317 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
318 if (err)
319 goto out;
320 if (parm.errcxx . . $ lookup_parm_s#err.)
321 {
322 err = parm.errcxx . . $ lookup_parm_s#err.;
323 goto out;
324 }
325 if (!cert)
326 {
327 err = GPG_ERR_GENERALcxx . . $ GPG_ERR_GENERAL.; /* FIXME: better error code? -mo */
328 goto out;
329 }
330
331 out:
332
333 xfreecxx . . $ `src/poldi.h:39:9`! (get_membuf (&parm.data, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!));
334
335 if (err)
336 {
337 if (cert)
338 {
339 ksba_cert_releasecxx . . $ ksba_cert_release(e650ffcbc4dd6f60). (cert);
340 cert = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
341 }
342 }
343 else
344 *certificate = cert;
345
346 return err;
347 }
348
349 /* END */