| 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>cxx . . $ `<file>/src/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_AUTHcxx . . $ `src/pam/auth-method-x509/auth-x509.c:27:9`! |
| 28 | #include <security/pam_modules.h> |
| 29 | |
| 30 | #include "scd/scd.h"cxx . . $ `<file>/src/scd/scd.h`/ |
| 31 | #include "dirmngr.h"cxx . . $ `<file>/src/pam/auth-method-x509/dirmngr.h`/ |
| 32 | #include "conv.h"cxx . . $ `<file>/src/pam/auth-support/conv.h`/ |
| 33 | #include "util/util.h"cxx . . $ `<file>/src/util/util.h`/ |
| 34 | #include "util/support.h"cxx . . $ `<file>/src/util/support.h`/ |
| 35 | #include "auth-support/ctx.h"cxx . . $ `<file>/src/pam/auth-support/ctx.h`/ |
| 36 | #include "auth-support/getpin-cb.h"cxx . . $ `<file>/src/pam/auth-support/getpin-cb.h`/ |
| 37 | #include "auth-methods.h"cxx . . $ `<file>/src/pam/auth-methods.h`/ |
| 38 | #include "util/defs.h"cxx . . $ `<file>/src/util/defs.h`/ |
| 39 | #include "util/simplelog.h"cxx . . $ `<file>/src/util/simplelog.h`/ |
| 40 | #include "util/simpleparse.h"cxx . . $ `<file>/src/util/simpleparse.h`/ |
| 41 | |
| 42 | |
| 43 | |
| 44 | struct x509_ctx_scxx . . $ x509_ctx_s# |
| 45 | { |
| 46 | char *x509_domaincxx . . $ x509_ctx_s#x509_domain.; |
| 47 | char *dirmngr_socketcxx . . $ x509_ctx_s#dirmngr_socket.; |
| 48 | }; |
| 49 | |
| 50 | typedef struct x509_ctx_scxx . . $ x509_ctx_s# *x509_ctx_tcxx . . $ 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_tcxx . . $ gpg_error_t# |
| 55 | auth_method_x509_initcxx . . $ auth_method_x509_init(178d98ccaffed2ab). (void **opaque) |
| 56 | { |
| 57 | x509_ctx_tcxx . . $ x509_ctx_t# cookie; |
| 58 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 59 | |
| 60 | cookie = xtrymalloccxx . . $ `src/poldi.h:35:9`! (sizeof (*cookie)); |
| 61 | if (!cookie) |
| 62 | err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 63 | else |
| 64 | { |
| 65 | cookie->x509_domaincxx . . $ x509_ctx_s#x509_domain. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 66 | cookie->dirmngr_socketcxx . . $ x509_ctx_s#dirmngr_socket. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 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_deinitcxx . . $ auth_method_x509_deinit(5d22bdacc48458e8). (void *opaque) |
| 79 | { |
| 80 | x509_ctx_tcxx . . $ x509_ctx_t# cookie = opaque; |
| 81 | |
| 82 | if (cookie) |
| 83 | { |
| 84 | xfreecxx . . $ `src/poldi.h:39:9`! (cookie->x509_domain); |
| 85 | xfreecxx . . $ `src/poldi.h:39:9`! (cookie->dirmngr_socket); |
| 86 | xfreecxx . . $ `src/poldi.h:39:9`! (opaque); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /* IDs for supported options. */ |
| 91 | enum opt_idscxx . . $ opt_ids# |
| 92 | { |
| 93 | opt_nonecxx . . $ opt_none., |
| 94 | opt_dirmngr_socketcxx . . $ opt_dirmngr_socket., |
| 95 | opt_x509_domaincxx . . $ opt_x509_domain. |
| 96 | }; |
| 97 | |
| 98 | /* Option specifications. */ |
| 99 | static simpleparse_opt_spec_tcxx . . $ simpleparse_opt_spec_t# x509_opt_specscxx . . $ x509_opt_specs.[] = |
| 100 | { |
| 101 | { opt_dirmngr_socketcxx . . $ opt_dirmngr_socket., "dirmngr-socket", |
| 102 | 0, SIMPLEPARSE_ARG_REQUIREDcxx . . $ SIMPLEPARSE_ARG_REQUIRED., 0, N_cxx . . $ `src/poldi.h:47:9`!("Specify local socket for dirmngr access") }, |
| 103 | { opt_x509_domaincxx . . $ opt_x509_domain., "x509-domain", |
| 104 | 0, SIMPLEPARSE_ARG_REQUIREDcxx . . $ SIMPLEPARSE_ARG_REQUIRED., 0, N_cxx . . $ `src/poldi.h:47:9`!("Specify X509 domain for this host") }, |
| 105 | { 0 } |
| 106 | }; |
| 107 | |
| 108 | /* Callback for simpleparse, implements x509-specific options. */ |
| 109 | static gpg_error_tcxx . . $ gpg_error_t# |
| 110 | auth_method_x509_parsecbcxx . . $ auth_method_x509_parsecb(c7ea03b5806a970). (void *opaque, simpleparse_opt_spec_tcxx . . $ simpleparse_opt_spec_t# spec, const char *arg) |
| 111 | { |
| 112 | struct auth_method_parse_cookiecxx . . $ auth_method_parse_cookie# *cookie = opaque; |
| 113 | x509_ctx_tcxx . . $ x509_ctx_t# x509_ctx = cookie->method_ctxcxx . . $ auth_method_parse_cookie#method_ctx.; |
| 114 | poldi_ctx_tcxx . . $ poldi_ctx_t# ctx = cookie->poldi_ctxcxx . . $ auth_method_parse_cookie#poldi_ctx.; |
| 115 | gpg_err_code_tcxx . . $ gpg_err_code_t# err = GPG_ERR_NO_ERRORcxx . . $ GPG_ERR_NO_ERROR.; |
| 116 | |
| 117 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88). (spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., "x509-domain")) |
| 118 | { |
| 119 | x509_ctx->x509_domaincxx . . $ x509_ctx_s#x509_domain. = xtrystrdupcxx . . $ `src/poldi.h:37:9`! (arg); |
| 120 | if (!x509_ctx->x509_domaincxx . . $ x509_ctx_s#x509_domain.) |
| 121 | { |
| 122 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 123 | "failed to duplicate %s (length: %i): %s", |
| 124 | "x509-domain option string", |
| 125 | strlencxx . . $ strlen(751346f8406fd082). (arg), strerrorcxx . . $ strerror(ff224f080444ee45). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 126 | err = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). (); |
| 127 | } |
| 128 | } |
| 129 | else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88). (spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., "dirmngr-socket")) |
| 130 | { |
| 131 | x509_ctx->dirmngr_socketcxx . . $ x509_ctx_s#dirmngr_socket. = xtrystrdupcxx . . $ `src/poldi.h:37:9`! (arg); |
| 132 | if (!x509_ctx->dirmngr_socketcxx . . $ x509_ctx_s#dirmngr_socket.) |
| 133 | { |
| 134 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 135 | "failed to duplicate %s (length: %i): %s", |
| 136 | "dirmngr-socket option string", |
| 137 | strlencxx . . $ strlen(751346f8406fd082). (arg), strerrorcxx . . $ strerror(ff224f080444ee45). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 138 | err = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). (); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (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_tcxx . . $ gpg_error_t# |
| 151 | extract_public_key_from_certcxx . . $ extract_public_key_from_cert(24211e2deda51946). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, ksba_cert_tcxx . . $ ksba_cert_t# cert, gcry_sexp_tcxx . . $ gcry_sexp_t# *public_key) |
| 152 | { |
| 153 | gcry_sexp_tcxx . . $ gcry_sexp_t# pubkey; |
| 154 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 155 | size_t sexp_len; |
| 156 | ksba_sexp_tcxx . . $ ksba_sexp_t# ksba_sexp; |
| 157 | |
| 158 | pubkey = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 159 | ksba_sexp = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 160 | err = 0; |
| 161 | |
| 162 | ksba_sexp = ksba_cert_get_public_keycxx . . $ ksba_cert_get_public_key(aab002fd58b25978). (cert); |
| 163 | sexp_len = gcry_sexp_canon_lencxx . . $ gcry_sexp_canon_len(de1a7274e1c1e7fe). (ksba_sexp, 0, 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`!); |
| 164 | if (!sexp_len) |
| 165 | { |
| 166 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 167 | "libksba did not return a proper S-Exp"); |
| 168 | err = GPG_ERR_BUGcxx . . $ GPG_ERR_BUG.; |
| 169 | goto out; |
| 170 | } |
| 171 | |
| 172 | err = gcry_sexp_sscancxx . . $ gcry_sexp_sscan(98bdf0951f34d12f). (&pubkey, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, (char *) ksba_sexp, sexp_len); |
| 173 | if (err) |
| 174 | { |
| 175 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 176 | "failed to convert KSBA s-expression into s-expression object: %s", |
| 177 | gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 178 | goto out; |
| 179 | } |
| 180 | |
| 181 | *public_key = pubkey; |
| 182 | |
| 183 | out: |
| 184 | |
| 185 | ksba_freecxx . . $ ksba_free(5d22bdacc48458e8). (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_tcxx . . $ gpg_error_t# |
| 195 | verify_challenge_sigcxx . . $ verify_challenge_sig(f6287335c06905fd). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, ksba_cert_tcxx . . $ ksba_cert_t# cert, |
| 196 | void *challenge, size_t challenge_n, |
| 197 | void *response, size_t response_n) |
| 198 | { |
| 199 | gcry_sexp_tcxx . . $ gcry_sexp_t# pubkey; |
| 200 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 201 | |
| 202 | pubkey = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 203 | |
| 204 | err = extract_public_key_from_certcxx . . $ extract_public_key_from_cert(24211e2deda51946). (ctx, cert, &pubkey); |
| 205 | if (err) |
| 206 | goto out; |
| 207 | |
| 208 | err = challenge_verifycxx . . $ challenge_verify(ffed3e639787afb3). (pubkey, challenge, challenge_n, |
| 209 | response, response_n); |
| 210 | |
| 211 | out: |
| 212 | |
| 213 | gcry_sexp_releasecxx . . $ gcry_sexp_release(c5bb25d0f29ec38). (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_tcxx . . $ gpg_error_t# |
| 221 | lookup_cert_from_filecxx . . $ lookup_cert_from_file(6464f99eeacc900c). (const char *filename, ksba_cert_tcxx . . $ ksba_cert_t# *certificate) |
| 222 | { |
| 223 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 224 | ksba_cert_tcxx . . $ ksba_cert_t# cert; |
| 225 | void *data; |
| 226 | size_t datalen; |
| 227 | |
| 228 | cert = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 229 | data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 230 | err = 0; |
| 231 | |
| 232 | err = ksba_cert_newcxx . . $ ksba_cert_new(a3a41e1420d5bfa5). (&cert); |
| 233 | if (err) |
| 234 | goto out; |
| 235 | |
| 236 | err = file_to_binstringcxx . . $ file_to_binstring(e0db3a0091505537). (filename, &data, &datalen); |
| 237 | if (err) |
| 238 | goto out; |
| 239 | |
| 240 | err = ksba_cert_init_from_memcxx . . $ ksba_cert_init_from_mem(7e6a930927ce1d45). (cert, data, datalen); |
| 241 | if (err) |
| 242 | goto out; |
| 243 | |
| 244 | *certificate = cert; |
| 245 | |
| 246 | out: |
| 247 | |
| 248 | if (err) |
| 249 | ksba_cert_releasecxx . . $ ksba_cert_release(e650ffcbc4dd6f60). (cert); |
| 250 | freecxx . . $ free(5d22bdacc48458e8). (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_kludgecxx . . $ email_kludge(a8b8e32347182212). (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 = strstrcxx . . $ strstr(a713fe7b9a57cf96). (string, "1.2.840.113549.1.9.1=#"); |
| 268 | if (!p) |
| 269 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 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; hexdigitpcxx . . $ `src/util/util.h:48:9`! (p) && hexdigitpcxx . . $ `src/util/util.h:48:9`! (p+1); p +=2, n++) |
| 283 | ; |
| 284 | if (!n) |
| 285 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 286 | buf = xtrymalloccxx . . $ `src/poldi.h:35:9`! (n+3); |
| 287 | if (!buf) |
| 288 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; /* oops, out of core */ |
| 289 | *buf = '<'; |
| 290 | for (n=1, p=name; hexdigitpcxx . . $ `src/util/util.h:48:9`! (p); p +=2, n++) |
| 291 | buf[n] = xtoi_2cxx . . $ `src/util/util.h:61:9`! (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_matchcxx . . $ email_address_match(5ae29d726ae57f88). (const char *subject, const char *x509_domain) |
| 301 | { |
| 302 | size_t subject_len = strlencxx . . $ strlen(751346f8406fd082). (subject); |
| 303 | size_t x509_domain_len = strlencxx . . $ strlen(751346f8406fd082). (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 | && (! strncmpcxx . . $ strncmp(fd278af2f295fa2e). (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_tcxx . . $ gpg_error_t# |
| 322 | email_address_extract_accountcxx . . $ email_address_extract_account(a9c69b9d9ab9e4b8). (const char *address, char **account) |
| 323 | { |
| 324 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 325 | char *name; |
| 326 | size_t name_len; |
| 327 | char *p; |
| 328 | |
| 329 | err = 0; |
| 330 | name = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 331 | |
| 332 | p = strchrcxx . . $ strchr(2050848944e00d54). (address, '@'); |
| 333 | name_len = p - (address + 1); |
| 334 | name = xtrymalloccxx . . $ `src/poldi.h:35:9`! (name_len + 1); |
| 335 | if (!name) |
| 336 | { |
| 337 | err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 338 | goto out; |
| 339 | } |
| 340 | |
| 341 | strncpycxx . . $ strncpy(255158a415b673b0). (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_tcxx . . $ gpg_error_t# |
| 355 | extract_username_from_certcxx . . $ extract_username_from_cert(9c6bacfda9032e6a). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, ksba_cert_tcxx . . $ ksba_cert_t# cert, |
| 356 | const char *x509_domain, char **username) |
| 357 | { |
| 358 | gpg_error_tcxx . . $ 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 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 367 | |
| 368 | /* Iterate over subject items contained in certificate. */ |
| 369 | for (idx = 0; (subject = ksba_cert_get_subjectcxx . . $ ksba_cert_get_subject(82144ebb144bdef9). (cert, idx)); idx++) |
| 370 | { |
| 371 | if (!idx) |
| 372 | { |
| 373 | /* Kludge for the DN (idx == 0). */ |
| 374 | char *kludge_uid = email_kludgecxx . . $ email_kludge(a8b8e32347182212). (subject); |
| 375 | if (kludge_uid) |
| 376 | { |
| 377 | if (email_address_matchcxx . . $ email_address_match(5ae29d726ae57f88). (kludge_uid, x509_domain)) |
| 378 | { |
| 379 | found = 1; |
| 380 | err = email_address_extract_accountcxx . . $ email_address_extract_account(a9c69b9d9ab9e4b8). (kludge_uid, &account); |
| 381 | } |
| 382 | xfreecxx . . $ `src/poldi.h:39:9`! (kludge_uid); |
| 383 | } |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | if (email_address_matchcxx . . $ email_address_match(5ae29d726ae57f88). (subject, x509_domain)) |
| 388 | { |
| 389 | found = 1; |
| 390 | err = email_address_extract_accountcxx . . $ email_address_extract_account(a9c69b9d9ab9e4b8). (subject, &account); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | ksba_freecxx . . $ ksba_free(5d22bdacc48458e8). (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_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 408 | "failed to extract username from certificate"); |
| 409 | err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_UNSUPPORTED_CERTcxx . . $ 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_tcxx . . $ gpg_error_t# |
| 420 | lookup_certcxx . . $ lookup_cert(2e6d16096fc4c1b). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, dirmngr_ctx_tcxx . . $ dirmngr_ctx_t# dirmngr, const char *url, |
| 421 | ksba_cert_tcxx . . $ ksba_cert_t# *certificate) |
| 422 | { |
| 423 | ksba_cert_tcxx . . $ ksba_cert_t# cert; |
| 424 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 425 | |
| 426 | cert = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 427 | err = 0; |
| 428 | |
| 429 | if (!url) |
| 430 | { |
| 431 | err = GPG_ERR_INV_CARDcxx . . $ GPG_ERR_INV_CARD.; |
| 432 | goto out; |
| 433 | } |
| 434 | |
| 435 | if (strncmpcxx . . $ strncmp(fd278af2f295fa2e). (url, "ldap://", 7) == 0) |
| 436 | err = dirmngr_lookup_urlcxx . . $ dirmngr_lookup_url(ed84806f3812eb2c). (dirmngr, url, &cert); |
| 437 | else if (strncmpcxx . . $ strncmp(fd278af2f295fa2e). (ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..pubkey_urlcxx . . $ scd_cardinfo#pubkey_url., "file://", 7) == 0) |
| 438 | err = lookup_cert_from_filecxx . . $ lookup_cert_from_file(6464f99eeacc900c). (ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..pubkey_urlcxx . . $ scd_cardinfo#pubkey_url. + 7, &cert); |
| 439 | else |
| 440 | { |
| 441 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 442 | "invalid certificate URL on card"); |
| 443 | err = GPG_ERR_INV_CARDcxx . . $ 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_releasecxx . . $ ksba_cert_release(e650ffcbc4dd6f60). (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_docxx . . $ auth_method_x509_auth_do(d734b837ce0ca1cd). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, x509_ctx_tcxx . . $ 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_tcxx . . $ gpg_error_t# err; |
| 472 | char *card_username; |
| 473 | ksba_cert_tcxx . . $ ksba_cert_t# cert; |
| 474 | dirmngr_ctx_tcxx . . $ dirmngr_ctx_t# dirmngr; |
| 475 | |
| 476 | dirmngr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 477 | challenge = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 478 | response = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 479 | card_username = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 480 | cert = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 481 | err = 0; |
| 482 | |
| 483 | /*** Sanity checks. ***/ |
| 484 | |
| 485 | if (! (cookie->x509_domaincxx . . $ x509_ctx_s#x509_domain. && cookie->dirmngr_socketcxx . . $ x509_ctx_s#dirmngr_socket.)) |
| 486 | { |
| 487 | err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_CONFIGURATIONcxx . . $ GPG_ERR_CONFIGURATION.); |
| 488 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 489 | "x509 authentication method not properly configured"); |
| 490 | goto out; |
| 491 | } |
| 492 | |
| 493 | /*** Connect to Dirmngr. ***/ |
| 494 | |
| 495 | err = dirmngr_connectcxx . . $ dirmngr_connect(7038681cf8e299d2). (&dirmngr, cookie->dirmngr_socketcxx . . $ x509_ctx_s#dirmngr_socket., 0, ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.); |
| 496 | if (err) |
| 497 | goto out; |
| 498 | |
| 499 | // /*** Receive card info. ***/ |
| 500 | |
| 501 | if (ctx->debugcxx . . $ poldi_ctx_s#debug.) |
| 502 | log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 503 | "public key url is '%s'", ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..pubkey_urlcxx . . $ scd_cardinfo#pubkey_url.); |
| 504 | |
| 505 | /*** Fetch certificate. ***/ |
| 506 | |
| 507 | err = lookup_certcxx . . $ lookup_cert(2e6d16096fc4c1b). (ctx, dirmngr, ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..pubkey_urlcxx . . $ scd_cardinfo#pubkey_url., &cert); |
| 508 | if (err) |
| 509 | { |
| 510 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 511 | "failed to look up certificate `%s': %s", |
| 512 | ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..pubkey_urlcxx . . $ scd_cardinfo#pubkey_url., gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (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_validatecxx . . $ dirmngr_validate(c3a3f7162da2be4). (dirmngr, cert); |
| 522 | if (err) |
| 523 | goto out; |
| 524 | |
| 525 | /*** Check username. ***/ |
| 526 | |
| 527 | err = extract_username_from_certcxx . . $ extract_username_from_cert(9c6bacfda9032e6a). (ctx, cert, cookie->x509_domaincxx . . $ x509_ctx_s#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 (strcmpcxx . . $ strcmp(5ae29d726ae57f88). (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_IDcxx . . $ 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_generatecxx . . $ challenge_generate(fee6e208587138e9). (&challenge, &challenge_n); |
| 549 | if (err) |
| 550 | { |
| 551 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., "failed to generate challenge: %s", |
| 552 | gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 553 | goto out; |
| 554 | } |
| 555 | |
| 556 | /*** Let card sign the challenge. ***/ |
| 557 | err = scd_pksigncxx . . $ scd_pksign(def977db0e0fd549). (ctx->scdcxx . . $ poldi_ctx_s#scd., "OPENPGP.3", |
| 558 | challenge, challenge_n, |
| 559 | &response, &response_n); |
| 560 | if (err) |
| 561 | { |
| 562 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 563 | "failed to retrieve challenge signature from card: %s", |
| 564 | gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 565 | goto out; |
| 566 | } |
| 567 | |
| 568 | /*** Verify challenge signature against certificate. ***/ |
| 569 | |
| 570 | err = verify_challenge_sigcxx . . $ verify_challenge_sig(f6287335c06905fd). (ctx, cert, |
| 571 | challenge, challenge_n, |
| 572 | response, response_n); |
| 573 | if (err) |
| 574 | { |
| 575 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#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_disconnectcxx . . $ dirmngr_disconnect(677575fc8d6c46df). (dirmngr); |
| 588 | ksba_cert_releasecxx . . $ ksba_cert_release(e650ffcbc4dd6f60). (cert); |
| 589 | |
| 590 | if (err) |
| 591 | xfreecxx . . $ `src/poldi.h:39:9`! (card_username); |
| 592 | |
| 593 | /* Log result. */ |
| 594 | if (err) |
| 595 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., "failure: %s", gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 596 | else if (ctx->debugcxx . . $ poldi_ctx_s#debug.) |
| 597 | log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#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_authcxx . . $ auth_method_x509_auth(611d44073efe9c24). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, void *cookie, char **username) |
| 610 | { |
| 611 | return auth_method_x509_auth_docxx . . $ auth_method_x509_auth_do(d734b837ce0ca1cd). (ctx, cookie, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 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_ascxx . . $ auth_method_x509_auth_as(29cbdd2ecba470e0). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, void *cookie, const char *username) |
| 619 | { |
| 620 | return auth_method_x509_auth_docxx . . $ auth_method_x509_auth_do(d734b837ce0ca1cd). (ctx, cookie, username, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 621 | } |
| 622 | |
| 623 | |
| 624 | |
| 625 | struct auth_method_scxx . . $ auth_method_s# auth_method_x509cxx . . $ auth_method_x509. = |
| 626 | { |
| 627 | auth_method_x509_initcxx . . $ auth_method_x509_init(178d98ccaffed2ab)., |
| 628 | auth_method_x509_deinitcxx . . $ auth_method_x509_deinit(5d22bdacc48458e8)., |
| 629 | auth_method_x509_authcxx . . $ auth_method_x509_auth(611d44073efe9c24)., |
| 630 | auth_method_x509_auth_ascxx . . $ auth_method_x509_auth_as(29cbdd2ecba470e0)., |
| 631 | x509_opt_specscxx . . $ x509_opt_specs., |
| 632 | auth_method_x509_parsecbcxx . . $ auth_method_x509_parsecb(c7ea03b5806a970)., |
| 633 | POLDI_CONF_DIRECTORYcxx . . $ `src/util/defs.h:26:9`! "/" "poldi-x509.conf" |
| 634 | }; |