| 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> |
| 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" |
| 38 | #include "util/util.h" |
| 39 | #include "util/membuf.h" |
| 40 | #include "dirmngr.h" |
| 41 | |
| 42 | #include <util/simplelog.h> |
| 43 | |
| 44 | |
| 45 | |
| 46 | /* This is the a "dirmngr context". */ |
| 47 | struct dirmngr_ctx_s |
| 48 | { |
| 49 | assuan_context_t assuan; /* Assuan context for accessing |
| 50 | dirmngr. */ |
| 51 | log_handle_t 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_s { |
| 57 | void (*cb) (void *, ksba_cert_t); |
| 58 | void *cb_value; |
| 59 | membuf_t data; |
| 60 | gpg_error_t err; |
| 61 | dirmngr_ctx_t ctx; |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | |
| 66 | static struct dirmngr_ctx_s 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_t |
| 73 | dirmngr_connect (dirmngr_ctx_t *ctx, |
| 74 | const char *sock, |
| 75 | unsigned int flags, |
| 76 | log_handle_t log_handle) |
| 77 | { |
| 78 | dirmngr_ctx_t context; |
| 79 | gpg_error_t err; |
| 80 | |
| 81 | /* Allocate. */ |
| 82 | context = xtrymalloc (sizeof (*context)); |
| 83 | if (!context) |
| 84 | { |
| 85 | err = gpg_error_from_errno (errno); |
| 86 | goto out; |
| 87 | } |
| 88 | |
| 89 | /* Initialize with zeroes. */ |
| 90 | *context = dirmngr_ctx_init; |
| 91 | |
| 92 | /* Connect to assuan server. */ |
| 93 | err = assuan_socket_connect (&context->assuan, sock, -1); |
| 94 | if (err) |
| 95 | goto out; |
| 96 | |
| 97 | /* Install logging handle in new context. */ |
| 98 | context->log_handle = log_handle; |
| 99 | |
| 100 | *ctx = context; |
| 101 | |
| 102 | out: |
| 103 | |
| 104 | if (err) |
| 105 | xfree (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_disconnect (dirmngr_ctx_t ctx) |
| 114 | { |
| 115 | if (ctx) |
| 116 | { |
| 117 | if (ctx->assuan) |
| 118 | assuan_disconnect (ctx->assuan); |
| 119 | xfree (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_s |
| 129 | { |
| 130 | dirmngr_ctx_t ctx; /* Dirmngr context of the caller. */ |
| 131 | const unsigned char *cert; /* Raw certificate in question. */ |
| 132 | size_t 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_cert (void *opaque, const char *line) |
| 140 | { |
| 141 | struct inq_cert_parm_s *parm = opaque; |
| 142 | gpg_error_t err; |
| 143 | |
| 144 | if (!strncmp (line, "TARGETCERT", 10) && (line[10] == ' ' || !line[10])) |
| 145 | /* Send back the certificate we want to validate. */ |
| 146 | err = assuan_send_data (parm->ctx->assuan, parm->cert, parm->certlen); |
| 147 | else if ((!strncmp (line, "SENDCERT", 8) && (line[8] == ' ' || !line[8])) |
| 148 | || (!strncmp (line, "SENDCERT_SKI", 12) && (line[12]==' ' || !line[12])) |
| 149 | || (!strncmp (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_debug (parm->ctx->log_handle, "ignored inquiry from dirmngr: `%s'", line); |
| 154 | err = assuan_send_data (parm->ctx->assuan, NULL, 0); |
| 155 | if (err) |
| 156 | log_msg_error (parm->ctx->log_handle, |
| 157 | "failed to send back empty value to dirmngr: %s", |
| 158 | gpg_strerror (err)); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | log_msg_error (parm->ctx->log_handle, "unsupported assuan inquiry `%s'", line); |
| 163 | err = gpg_error (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_t |
| 177 | dirmngr_validate (dirmngr_ctx_t ctx, ksba_cert_t cert) |
| 178 | { |
| 179 | struct inq_cert_parm_s parm; |
| 180 | const unsigned char *image; |
| 181 | size_t imagelen; |
| 182 | gpg_error_t err; |
| 183 | |
| 184 | assert (ctx); |
| 185 | assert (cert); |
| 186 | |
| 187 | err = 0; |
| 188 | |
| 189 | /* Retrieve pointer to the raw certificate data. */ |
| 190 | image = ksba_cert_get_image (cert, &imagelen); |
| 191 | if (!image) |
| 192 | { |
| 193 | err = gpg_error (GPG_ERR_INTERNAL); /* FIXME: what error code? */ |
| 194 | goto out; |
| 195 | } |
| 196 | |
| 197 | /* Setup PARM structure. */ |
| 198 | parm.ctx = ctx; |
| 199 | parm.cert = image; |
| 200 | parm.certlen = imagelen; |
| 201 | |
| 202 | /* Validate certificate. INQ_CERT is the callback that will send the |
| 203 | certificate in question to dirmngr. */ |
| 204 | err = assuan_transact (ctx->assuan, "VALIDATE", NULL, NULL, |
| 205 | inq_cert, &parm, |
| 206 | NULL, NULL); |
| 207 | out: |
| 208 | |
| 209 | return err; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | |
| 214 | /* Lookup helpers*/ |
| 215 | static int |
| 216 | lookup_cb (void *opaque, const void *buffer, size_t length) |
| 217 | { |
| 218 | struct lookup_parm_s *parm = opaque; |
| 219 | |
| 220 | if (parm->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_membuf (&parm->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_t cert; |
| 237 | gpg_error_t rc; |
| 238 | |
| 239 | /* Retrieve pointer to accumulated data. */ |
| 240 | buf = get_membuf (&parm->data, &len); |
| 241 | if (!buf) |
| 242 | { |
| 243 | parm->err = gpg_error (GPG_ERR_ENOMEM); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | /* Create new certificate object from raw data. */ |
| 248 | rc = ksba_cert_new (&cert); |
| 249 | if (rc) |
| 250 | { |
| 251 | parm->err = rc; |
| 252 | return 0; |
| 253 | } |
| 254 | rc = ksba_cert_init_from_mem (cert, buf, len); |
| 255 | if (rc) |
| 256 | { |
| 257 | log_msg_error (parm->ctx->log_handle, |
| 258 | "failed to create new ksba certificate object: %s", |
| 259 | gpg_strerror (rc)); |
| 260 | /* FIXME: better error handling? -mo */ |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | parm->cb (parm->cb_value, cert); |
| 265 | } |
| 266 | |
| 267 | ksba_cert_release (cert); |
| 268 | init_membuf (&parm->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_cb (void *opaque, ksba_cert_t cert) |
| 277 | { |
| 278 | ksba_cert_t *cert_cp = opaque; |
| 279 | |
| 280 | if (*cert_cp) |
| 281 | /* Does already contain a cert. */ |
| 282 | return; |
| 283 | |
| 284 | ksba_cert_ref (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_t |
| 292 | dirmngr_lookup_url (dirmngr_ctx_t ctx, |
| 293 | const char *url, ksba_cert_t *certificate) |
| 294 | { |
| 295 | gpg_error_t err; |
| 296 | char line[ASSUAN_LINELENGTH]; |
| 297 | struct lookup_parm_s parm; |
| 298 | ksba_cert_t cert; |
| 299 | |
| 300 | cert = NULL; |
| 301 | err = 0; |
| 302 | |
| 303 | /* Prepare command. */ |
| 304 | |
| 305 | snprintf (line, DIM(line)-1, "LOOKUP --url %s", url); |
| 306 | line[DIM(line)-1] = 0; |
| 307 | |
| 308 | parm.cb = lookup_url_cb; |
| 309 | parm.cb_value = &cert; |
| 310 | parm.err = 0; |
| 311 | init_membuf (&parm.data, 4096); |
| 312 | parm.ctx = ctx; |
| 313 | |
| 314 | /* Execute command. */ |
| 315 | |
| 316 | err = assuan_transact (ctx->assuan, line, lookup_cb, &parm, |
| 317 | NULL, NULL, NULL, NULL); |
| 318 | if (err) |
| 319 | goto out; |
| 320 | if (parm.err) |
| 321 | { |
| 322 | err = parm.err; |
| 323 | goto out; |
| 324 | } |
| 325 | if (!cert) |
| 326 | { |
| 327 | err = GPG_ERR_GENERAL; /* FIXME: better error code? -mo */ |
| 328 | goto out; |
| 329 | } |
| 330 | |
| 331 | out: |
| 332 | |
| 333 | xfree (get_membuf (&parm.data, NULL)); |
| 334 | |
| 335 | if (err) |
| 336 | { |
| 337 | if (cert) |
| 338 | { |
| 339 | ksba_cert_release (cert); |
| 340 | cert = NULL; |
| 341 | } |
| 342 | } |
| 343 | else |
| 344 | *certificate = cert; |
| 345 | |
| 346 | return err; |
| 347 | } |
| 348 | |
| 349 | /* END */ |