Debian Codegraph @

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

raw symbols SCIP
1 /* auth-x509.c - x509 authentication backend for Poldi.
2 * Copyright (C) 2007, 2008 g10 Code GmbH
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 #include <poldi.h>
21
22 #include <stdlib.h>
23 #include <stdio.h> /* FIXME, so far only required for
24 old ksba.h. */
25 #include <ksba.h>
26
27 #define PAM_SM_AUTH
28 #include <security/pam_modules.h>
29
30 #include "scd/scd.h"
31 #include "dirmngr.h"
32 #include "conv.h"
33 #include "util/util.h"
34 #include "util/support.h"
35 #include "auth-support/ctx.h"
36 #include "auth-support/getpin-cb.h"
37 #include "auth-methods.h"
38 #include "util/defs.h"
39 #include "util/simplelog.h"
40 #include "util/simpleparse.h"
41
42
43
44 struct x509_ctx_s
45 {
46 char *x509_domain;
47 char *dirmngr_socket;
48 };
49
50 typedef struct x509_ctx_s *x509_ctx_t;
51
52 /* Initialize this authentication methods; create a method specific
53 cookie, which is stored in *OPAQUE. Returns proper error code. */
54 static gpg_error_t
55 auth_method_x509_init (void **opaque)
56 {
57 x509_ctx_t cookie;
58 gpg_error_t err;
59
60 cookie = xtrymalloc (sizeof (*cookie));
61 if (!cookie)
62 err = gpg_error_from_errno (errno);
63 else
64 {
65 cookie->x509_domain = NULL;
66 cookie->dirmngr_socket = NULL;
67 err = 0;
68 }
69
70 *opaque = cookie;
71
72 return err;
73 }
74
75 /* Release any resources associated with this authentication
76 method. Takes care of releasing the cookie. */
77 static void
78 auth_method_x509_deinit (void *opaque)
79 {
80 x509_ctx_t cookie = opaque;
81
82 if (cookie)
83 {
84 xfree (cookie->x509_domain);
85 xfree (cookie->dirmngr_socket);
86 xfree (opaque);
87 }
88 }
89
90 /* IDs for supported options. */
91 enum opt_ids
92 {
93 opt_none,
94 opt_dirmngr_socket,
95 opt_x509_domain
96 };
97
98 /* Option specifications. */
99 static simpleparse_opt_spec_t x509_opt_specs[] =
100 {
101 { opt_dirmngr_socket, "dirmngr-socket",
102 0, SIMPLEPARSE_ARG_REQUIRED, 0, N_("Specify local socket for dirmngr access") },
103 { opt_x509_domain, "x509-domain",
104 0, SIMPLEPARSE_ARG_REQUIRED, 0, N_("Specify X509 domain for this host") },
105 { 0 }
106 };
107
108 /* Callback for simpleparse, implements x509-specific options. */
109 static gpg_error_t
110 auth_method_x509_parsecb (void *opaque, simpleparse_opt_spec_t spec, const char *arg)
111 {
112 struct auth_method_parse_cookie *cookie = opaque;
113 x509_ctx_t x509_ctx = cookie->method_ctx;
114 poldi_ctx_t ctx = cookie->poldi_ctx;
115 gpg_err_code_t err = GPG_ERR_NO_ERROR;
116
117 if (!strcmp (spec.long_opt, "x509-domain"))
118 {
119 x509_ctx->x509_domain = xtrystrdup (arg);
120 if (!x509_ctx->x509_domain)
121 {
122 log_msg_error (ctx->loghandle,
123 "failed to duplicate %s (length: %i): %s",
124 "x509-domain option string",
125 strlen (arg), strerror (errno));
126 err = gpg_error_from_syserror ();
127 }
128 }
129 else if (!strcmp (spec.long_opt, "dirmngr-socket"))
130 {
131 x509_ctx->dirmngr_socket = xtrystrdup (arg);
132 if (!x509_ctx->dirmngr_socket)
133 {
134 log_msg_error (ctx->loghandle,
135 "failed to duplicate %s (length: %i): %s",
136 "dirmngr-socket option string",
137 strlen (arg), strerror (errno));
138 err = gpg_error_from_syserror ();
139 }
140 }
141
142 return gpg_error (err);
143 }
144
145
146
147 /* This functions extracts the raw public key from the certificate
148 CERT und returns it as a newly allocated S-Expressions in
149 *PUBLIC_KEY. Returns error code. */
150 static gpg_error_t
151 extract_public_key_from_cert (poldi_ctx_t ctx, ksba_cert_t cert, gcry_sexp_t *public_key)
152 {
153 gcry_sexp_t pubkey;
154 gpg_error_t err;
155 size_t sexp_len;
156 ksba_sexp_t ksba_sexp;
157
158 pubkey = NULL;
159 ksba_sexp = NULL;
160 err = 0;
161
162 ksba_sexp = ksba_cert_get_public_key (cert);
163 sexp_len = gcry_sexp_canon_len (ksba_sexp, 0, NULL, NULL);
164 if (!sexp_len)
165 {
166 log_msg_error (ctx->loghandle,
167 "libksba did not return a proper S-Exp");
168 err = GPG_ERR_BUG;
169 goto out;
170 }
171
172 err = gcry_sexp_sscan (&pubkey, NULL, (char *) ksba_sexp, sexp_len);
173 if (err)
174 {
175 log_msg_error (ctx->loghandle,
176 "failed to convert KSBA s-expression into s-expression object: %s",
177 gpg_strerror (err));
178 goto out;
179 }
180
181 *public_key = pubkey;
182
183 out:
184
185 ksba_free (ksba_sexp);
186
187 return err;
188 }
189
190 /* This functions checks if RESPONSE/RESPONSE_N contains a valid
191 signature for the data CHALLENGE/CHALLENGE_N, created by the
192 private key belonging to the certificate CERT. Returns zero if the
193 signature verification succeeded, an error code otherwise. */
194 static gpg_error_t
195 verify_challenge_sig (poldi_ctx_t ctx, ksba_cert_t cert,
196 void *challenge, size_t challenge_n,
197 void *response, size_t response_n)
198 {
199 gcry_sexp_t pubkey;
200 gpg_error_t err;
201
202 pubkey = NULL;
203
204 err = extract_public_key_from_cert (ctx, cert, &pubkey);
205 if (err)
206 goto out;
207
208 err = challenge_verify (pubkey, challenge, challenge_n,
209 response, response_n);
210
211 out:
212
213 gcry_sexp_release (pubkey);
214
215 return err;
216 }
217
218 /* Extract the certificate contained in the file FILENAME, store it in
219 *CERTIFICATE. Return proper error code. */
220 static gpg_error_t
221 lookup_cert_from_file (const char *filename, ksba_cert_t *certificate)
222 {
223 gpg_error_t err;
224 ksba_cert_t cert;
225 void *data;
226 size_t datalen;
227
228 cert = NULL;
229 data = NULL;
230 err = 0;
231
232 err = ksba_cert_new (&cert);
233 if (err)
234 goto out;
235
236 err = file_to_binstring (filename, &data, &datalen);
237 if (err)
238 goto out;
239
240 err = ksba_cert_init_from_mem (cert, data, datalen);
241 if (err)
242 goto out;
243
244 *certificate = cert;
245
246 out:
247
248 if (err)
249 ksba_cert_release (cert);
250 free (data);
251
252 return err;
253 }
254
255 /* Return an allocated string with the email address extracted from a
256 DN */
257 static char *
258 email_kludge (const char *name)
259 {
260 const char *p, *string;
261 unsigned char *buf;
262 int n;
263
264 string = name;
265 for (;;)
266 {
267 p = strstr (string, "1.2.840.113549.1.9.1=#");
268 if (!p)
269 return NULL;
270 if (p == name || (p > string+1 && p[-1] == ',' && p[-2] != '\\'))
271 {
272 name = p + 22;
273 break;
274 }
275 string = p + 22;
276 }
277
278
279 /* This looks pretty much like an email address in the subject's DN
280 we use this to add an additional user ID entry. This way,
281 openSSL generated keys get a nicer and usable listing */
282 for (n=0, p=name; hexdigitp (p) && hexdigitp (p+1); p +=2, n++)
283 ;
284 if (!n)
285 return NULL;
286 buf = xtrymalloc (n+3);
287 if (!buf)
288 return NULL; /* oops, out of core */
289 *buf = '<';
290 for (n=1, p=name; hexdigitp (p); p +=2, n++)
291 buf[n] = xtoi_2 (p);
292 buf[n++] = '>';
293 buf[n] = 0;
294 return (char*)buf;
295 }
296
297 /* Returns true if the mail address (of the form "<user@domain>")
298 contained in SUBJECT has X509_DOMAIN as it's domain part. */
299 static int
300 email_address_match (const char *subject, const char *x509_domain)
301 {
302 size_t subject_len = strlen (subject);
303 size_t x509_domain_len = strlen (x509_domain);
304
305 /* FIXME: some more sanity checks necessary? */
306
307 if ((x509_domain_len < subject_len - 3)
308 && (subject[0] == '<')
309 && (subject[subject_len - 1 - x509_domain_len - 1] == '@')
310 && (! strncmp (subject + subject_len - x509_domain_len - 1, x509_domain, x509_domain_len))
311 && (subject[subject_len - 1] == '>'))
312 return 1;
313 else
314 return 0;
315 }
316
317 /* Extracts the mailbox name from the e-mail address ADDRESS and store
318 it in *ACCOUNT. ADDRESS must be a string of the form "<x@y>", in
319 this case *ACCOUNT will be the newly allocated string "x". Returns
320 proper error code. */
321 static gpg_error_t
322 email_address_extract_account (const char *address, char **account)
323 {
324 gpg_error_t err;
325 char *name;
326 size_t name_len;
327 char *p;
328
329 err = 0;
330 name = NULL;
331
332 p = strchr (address, '@');
333 name_len = p - (address + 1);
334 name = xtrymalloc (name_len + 1);
335 if (!name)
336 {
337 err = gpg_error_from_errno (errno);
338 goto out;
339 }
340
341 strncpy (name, address + 1, name_len);
342 name[name_len] = 0;
343 *account = name;
344
345 out:
346
347 return err;
348 }
349
350 /* This function takes the X509 certificate CERT, iterates through the
351 e-mail addresses contained in CERT and returns the mailbox name of
352 the first address with the domain part being X509_DOMAIN. Returns
353 proper error code. */
354 static gpg_error_t
355 extract_username_from_cert (poldi_ctx_t ctx, ksba_cert_t cert,
356 const char *x509_domain, char **username)
357 {
358 gpg_error_t err;
359 unsigned int idx;
360 char *subject;
361 int found;
362 char *account;
363
364 err = 0;
365 found = 0;
366 account = NULL;
367
368 /* Iterate over subject items contained in certificate. */
369 for (idx = 0; (subject = ksba_cert_get_subject (cert, idx)); idx++)
370 {
371 if (!idx)
372 {
373 /* Kludge for the DN (idx == 0). */
374 char *kludge_uid = email_kludge (subject);
375 if (kludge_uid)
376 {
377 if (email_address_match (kludge_uid, x509_domain))
378 {
379 found = 1;
380 err = email_address_extract_account (kludge_uid, &account);
381 }
382 xfree (kludge_uid);
383 }
384 }
385 else
386 {
387 if (email_address_match (subject, x509_domain))
388 {
389 found = 1;
390 err = email_address_extract_account (subject, &account);
391 }
392 }
393
394 ksba_free (subject);
395
396 if (found)
397 break;
398 }
399
400 if (found)
401 {
402 if (!err)
403 *username = account;
404 }
405 else
406 {
407 log_msg_error (ctx->loghandle,
408 "failed to extract username from certificate");
409 err = gpg_error (GPG_ERR_UNSUPPORTED_CERT);
410 }
411
412 return err;
413 }
414
415 /* Lookup the certificate identified by URL (supported schemes are
416 "ldap://" and "file://") through the dirmngr connection identified
417 by DIRMNGR and store the certificate in *CERTIFICATE. CTX is the
418 Poldi context to use. Returns proper error code. */
419 static gpg_error_t
420 lookup_cert (poldi_ctx_t ctx, dirmngr_ctx_t dirmngr, const char *url,
421 ksba_cert_t *certificate)
422 {
423 ksba_cert_t cert;
424 gpg_error_t err;
425
426 cert = NULL;
427 err = 0;
428
429 if (!url)
430 {
431 err = GPG_ERR_INV_CARD;
432 goto out;
433 }
434
435 if (strncmp (url, "ldap://", 7) == 0)
436 err = dirmngr_lookup_url (dirmngr, url, &cert);
437 else if (strncmp (ctx->cardinfo.pubkey_url, "file://", 7) == 0)
438 err = lookup_cert_from_file (ctx->cardinfo.pubkey_url + 7, &cert);
439 else
440 {
441 log_msg_error (ctx->loghandle,
442 "invalid certificate URL on card");
443 err = GPG_ERR_INV_CARD;
444 }
445 if (err)
446 goto out;
447
448 *certificate = cert;
449
450 out:
451
452 if (err)
453 ksba_cert_release (cert);
454
455 return err;
456 }
457
458
459
460 /* Entry point for the x509 authentication method. Returns TRUE (1) if
461 authentication succeeded and FALSE (0) otherwise. */
462 static int
463 auth_method_x509_auth_do (poldi_ctx_t ctx, x509_ctx_t cookie,
464 const char *username_desired,
465 char **username_authenticated)
466 {
467 unsigned char *challenge;
468 unsigned char *response;
469 size_t challenge_n;
470 size_t response_n;
471 gpg_error_t err;
472 char *card_username;
473 ksba_cert_t cert;
474 dirmngr_ctx_t dirmngr;
475
476 dirmngr = NULL;
477 challenge = NULL;
478 response = NULL;
479 card_username = NULL;
480 cert = NULL;
481 err = 0;
482
483 /*** Sanity checks. ***/
484
485 if (! (cookie->x509_domain && cookie->dirmngr_socket))
486 {
487 err = gpg_error (GPG_ERR_CONFIGURATION);
488 log_msg_error (ctx->loghandle,
489 "x509 authentication method not properly configured");
490 goto out;
491 }
492
493 /*** Connect to Dirmngr. ***/
494
495 err = dirmngr_connect (&dirmngr, cookie->dirmngr_socket, 0, ctx->loghandle);
496 if (err)
497 goto out;
498
499 // /*** Receive card info. ***/
500
501 if (ctx->debug)
502 log_msg_debug (ctx->loghandle,
503 "public key url is '%s'", ctx->cardinfo.pubkey_url);
504
505 /*** Fetch certificate. ***/
506
507 err = lookup_cert (ctx, dirmngr, ctx->cardinfo.pubkey_url, &cert);
508 if (err)
509 {
510 log_msg_error (ctx->loghandle,
511 "failed to look up certificate `%s': %s",
512 ctx->cardinfo.pubkey_url, gpg_strerror (err));
513 goto out;
514 }
515
516 /*** Valide cert. ***/
517
518 /* FIXME: implement mechanism which allows for specifying the
519 issuer? -mo */
520
521 err = dirmngr_validate (dirmngr, cert);
522 if (err)
523 goto out;
524
525 /*** Check username. ***/
526
527 err = extract_username_from_cert (ctx, cert, cookie->x509_domain, &card_username);
528 if (err)
529 goto out;
530
531
532 if (username_desired)
533 {
534 /* Application wants us to authenticate the user as
535 PAM_USERNAME. */
536 if (strcmp (username_desired, card_username) != 0)
537 {
538 /* Current card's cert is not setup for authentication as
539 PAM_USERNAME. */
540 err = GPG_ERR_INV_USER_ID; /* FIXME, I guess we need a
541 better err code. -mo */
542 goto out;
543 }
544 }
545
546 /*** Generate challenge. ***/
547
548 err = challenge_generate (&challenge, &challenge_n);
549 if (err)
550 {
551 log_msg_error (ctx->loghandle, "failed to generate challenge: %s",
552 gpg_strerror (err));
553 goto out;
554 }
555
556 /*** Let card sign the challenge. ***/
557 err = scd_pksign (ctx->scd, "OPENPGP.3",
558 challenge, challenge_n,
559 &response, &response_n);
560 if (err)
561 {
562 log_msg_error (ctx->loghandle,
563 "failed to retrieve challenge signature from card: %s",
564 gpg_strerror (err));
565 goto out;
566 }
567
568 /*** Verify challenge signature against certificate. ***/
569
570 err = verify_challenge_sig (ctx, cert,
571 challenge, challenge_n,
572 response, response_n);
573 if (err)
574 {
575 log_msg_error (ctx->loghandle, "failed to verify challenge signature");
576 goto out;
577 }
578
579 /* Auth succeeded. */
580
581 if (!username_desired)
582 *username_authenticated = card_username;
583
584 out:
585
586 /* Release resources. */
587 dirmngr_disconnect (dirmngr);
588 ksba_cert_release (cert);
589
590 if (err)
591 xfree (card_username);
592
593 /* Log result. */
594 if (err)
595 log_msg_error (ctx->loghandle, "failure: %s", gpg_strerror (err));
596 else if (ctx->debug)
597 log_msg_debug (ctx->loghandle, "success");
598
599 return !err;
600 }
601
602 /* Try to authenticate a user. The user's identity on the system is
603 figured out during the authentication process. COOKIE is the
604 cookie for this authentication method. CTX is the Poldi context
605 object. On successful authentication, the newly allocated username
606 as which the user has been authenticated is stored in *USERNAME.
607 Returns TRUE on success, FALSE on failure. */
608 static int
609 auth_method_x509_auth (poldi_ctx_t ctx, void *cookie, char **username)
610 {
611 return auth_method_x509_auth_do (ctx, cookie, NULL, username);
612 }
613
614 /* Try to authenticate a user as USERNAME. COOKIE is the cookie for
615 this authentication method. CTX is the Poldi context object.
616 Returns TRUE on success, FALSE on failure. */
617 static int
618 auth_method_x509_auth_as (poldi_ctx_t ctx, void *cookie, const char *username)
619 {
620 return auth_method_x509_auth_do (ctx, cookie, username, NULL);
621 }
622
623
624
625 struct auth_method_s auth_method_x509 =
626 {
627 auth_method_x509_init,
628 auth_method_x509_deinit,
629 auth_method_x509_auth,
630 auth_method_x509_auth_as,
631 x509_opt_specs,
632 auth_method_x509_parsecb,
633 POLDI_CONF_DIRECTORY "/" "poldi-x509.conf"
634 };