sshconnect2.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  1. /* $OpenBSD: sshconnect2.c,v 1.336 2020/11/13 07:30:44 djm Exp $ */
  2. /*
  3. * Copyright (c) 2000 Markus Friedl. All rights reserved.
  4. * Copyright (c) 2008 Damien Miller. 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. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "includes.h"
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <sys/wait.h>
  30. #include <sys/stat.h>
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <limits.h>
  34. #include <netdb.h>
  35. #include <pwd.h>
  36. #include <signal.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdarg.h>
  40. #include <unistd.h>
  41. #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
  42. #include <vis.h>
  43. #endif
  44. #include "openbsd-compat/sys-queue.h"
  45. #include "xmalloc.h"
  46. #include "ssh.h"
  47. #include "ssh2.h"
  48. #include "sshbuf.h"
  49. #include "packet.h"
  50. #include "compat.h"
  51. #include "cipher.h"
  52. #include "sshkey.h"
  53. #include "kex.h"
  54. #include "myproposal.h"
  55. #include "sshconnect.h"
  56. #include "authfile.h"
  57. #include "dh.h"
  58. #include "authfd.h"
  59. #include "log.h"
  60. #include "misc.h"
  61. #include "readconf.h"
  62. #include "match.h"
  63. #include "dispatch.h"
  64. #include "canohost.h"
  65. #include "msg.h"
  66. #include "pathnames.h"
  67. #include "uidswap.h"
  68. #include "hostfile.h"
  69. #include "ssherr.h"
  70. #include "utf8.h"
  71. #include "ssh-sk.h"
  72. #include "sk-api.h"
  73. #ifdef GSSAPI
  74. #include "ssh-gss.h"
  75. #endif
  76. /* import */
  77. extern char *client_version_string;
  78. extern char *server_version_string;
  79. extern Options options;
  80. /*
  81. * tty_flag is set in ssh.c. Use this in ssh_userauth2:
  82. * if it is set, then prevent the switch to the null cipher.
  83. */
  84. extern int tty_flag;
  85. /*
  86. * SSH2 key exchange
  87. */
  88. static char *xxx_host;
  89. static struct sockaddr *xxx_hostaddr;
  90. static const struct ssh_conn_info *xxx_conn_info;
  91. static int
  92. verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
  93. {
  94. if (verify_host_key(xxx_host, xxx_hostaddr, hostkey,
  95. xxx_conn_info) == -1)
  96. fatal("Host key verification failed.");
  97. return 0;
  98. }
  99. /* Returns the first item from a comma-separated algorithm list */
  100. static char *
  101. first_alg(const char *algs)
  102. {
  103. char *ret, *cp;
  104. ret = xstrdup(algs);
  105. if ((cp = strchr(ret, ',')) != NULL)
  106. *cp = '\0';
  107. return ret;
  108. }
  109. static char *
  110. order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port,
  111. const struct ssh_conn_info *cinfo)
  112. {
  113. char *oavail = NULL, *avail = NULL, *first = NULL, *last = NULL;
  114. char *alg = NULL, *hostname = NULL, *ret = NULL, *best = NULL;
  115. size_t maxlen;
  116. struct hostkeys *hostkeys = NULL;
  117. int ktype;
  118. u_int i;
  119. /* Find all hostkeys for this hostname */
  120. get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
  121. hostkeys = init_hostkeys();
  122. for (i = 0; i < options.num_user_hostfiles; i++)
  123. load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
  124. for (i = 0; i < options.num_system_hostfiles; i++)
  125. load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
  126. /*
  127. * If a plain public key exists that matches the type of the best
  128. * preference HostkeyAlgorithms, then use the whole list as is.
  129. * Note that we ignore whether the best preference algorithm is a
  130. * certificate type, as sshconnect.c will downgrade certs to
  131. * plain keys if necessary.
  132. */
  133. best = first_alg(options.hostkeyalgorithms);
  134. if (lookup_key_in_hostkeys_by_type(hostkeys,
  135. sshkey_type_plain(sshkey_type_from_name(best)),
  136. sshkey_ecdsa_nid_from_name(best), NULL)) {
  137. debug3("%s: have matching best-preference key type %s, "
  138. "using HostkeyAlgorithms verbatim", __func__, best);
  139. ret = xstrdup(options.hostkeyalgorithms);
  140. goto out;
  141. }
  142. /*
  143. * Otherwise, prefer the host key algorithms that match known keys
  144. * while keeping the ordering of HostkeyAlgorithms as much as possible.
  145. */
  146. oavail = avail = xstrdup(options.hostkeyalgorithms);
  147. maxlen = strlen(avail) + 1;
  148. first = xmalloc(maxlen);
  149. last = xmalloc(maxlen);
  150. *first = *last = '\0';
  151. #define ALG_APPEND(to, from) \
  152. do { \
  153. if (*to != '\0') \
  154. strlcat(to, ",", maxlen); \
  155. strlcat(to, from, maxlen); \
  156. } while (0)
  157. while ((alg = strsep(&avail, ",")) && *alg != '\0') {
  158. if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
  159. fatal("%s: unknown alg %s", __func__, alg);
  160. /*
  161. * If we have a @cert-authority marker in known_hosts then
  162. * prefer all certificate algorithms.
  163. */
  164. if (sshkey_type_is_cert(ktype) &&
  165. lookup_marker_in_hostkeys(hostkeys, MRK_CA)) {
  166. ALG_APPEND(first, alg);
  167. continue;
  168. }
  169. /* If the key appears in known_hosts then prefer it */
  170. if (lookup_key_in_hostkeys_by_type(hostkeys,
  171. sshkey_type_plain(ktype),
  172. sshkey_ecdsa_nid_from_name(alg), NULL)) {
  173. ALG_APPEND(first, alg);
  174. continue;
  175. }
  176. /* Otherwise, put it last */
  177. ALG_APPEND(last, alg);
  178. }
  179. #undef ALG_APPEND
  180. xasprintf(&ret, "%s%s%s", first,
  181. (*first == '\0' || *last == '\0') ? "" : ",", last);
  182. if (*first != '\0')
  183. debug3("%s: prefer hostkeyalgs: %s", __func__, first);
  184. out:
  185. free(best);
  186. free(first);
  187. free(last);
  188. free(hostname);
  189. free(oavail);
  190. free_hostkeys(hostkeys);
  191. return ret;
  192. }
  193. static char *myproposal[PROPOSAL_MAX];
  194. static const char *myproposal_default[PROPOSAL_MAX] = { KEX_CLIENT };
  195. void
  196. ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port,
  197. const struct ssh_conn_info *cinfo)
  198. {
  199. char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
  200. char *s, *all_key;
  201. int r, use_known_hosts_order = 0;
  202. memcpy(&myproposal, &myproposal_default, sizeof(myproposal));
  203. memcpy(&myproposal, &myproposal_default, sizeof(myproposal));
  204. xxx_host = host;
  205. xxx_hostaddr = hostaddr;
  206. xxx_conn_info = cinfo;
  207. /*
  208. * If the user has not specified HostkeyAlgorithms, or has only
  209. * appended or removed algorithms from that list then prefer algorithms
  210. * that are in the list that are supported by known_hosts keys.
  211. */
  212. if (options.hostkeyalgorithms == NULL ||
  213. options.hostkeyalgorithms[0] == '-' ||
  214. options.hostkeyalgorithms[0] == '+')
  215. use_known_hosts_order = 1;
  216. /* Expand or fill in HostkeyAlgorithms */
  217. all_key = sshkey_alg_list(0, 0, 1, ',');
  218. if (kex_assemble_names(&options.hostkeyalgorithms,
  219. kex_default_pk_alg(), all_key) != 0)
  220. fatal("%s: kex_assemble_namelist", __func__);
  221. free(all_key);
  222. if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
  223. fatal("%s: kex_names_cat", __func__);
  224. myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(ssh, s);
  225. myproposal[PROPOSAL_ENC_ALGS_CTOS] =
  226. compat_cipher_proposal(ssh, options.ciphers);
  227. myproposal[PROPOSAL_ENC_ALGS_STOC] =
  228. compat_cipher_proposal(ssh, options.ciphers);
  229. myproposal[PROPOSAL_COMP_ALGS_CTOS] =
  230. myproposal[PROPOSAL_COMP_ALGS_STOC] =
  231. (char *)compression_alg_list(options.compression);
  232. myproposal[PROPOSAL_MAC_ALGS_CTOS] =
  233. myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
  234. if (use_known_hosts_order) {
  235. /* Query known_hosts and prefer algorithms that appear there */
  236. myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
  237. compat_pkalg_proposal(ssh,
  238. order_hostkeyalgs(host, hostaddr, port, cinfo));
  239. } else {
  240. /* Use specified HostkeyAlgorithms exactly */
  241. myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
  242. compat_pkalg_proposal(ssh, options.hostkeyalgorithms);
  243. }
  244. if (options.rekey_limit || options.rekey_interval)
  245. ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
  246. options.rekey_interval);
  247. /* start key exchange */
  248. if ((r = kex_setup(ssh, myproposal)) != 0)
  249. fatal("kex_setup: %s", ssh_err(r));
  250. #ifdef WITH_OPENSSL
  251. ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
  252. ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
  253. ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
  254. ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
  255. ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
  256. ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
  257. ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
  258. # ifdef OPENSSL_HAS_ECC
  259. ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
  260. # endif
  261. #endif
  262. ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
  263. ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client;
  264. ssh->kex->verify_host_key=&verify_host_key_callback;
  265. ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
  266. /* remove ext-info from the KEX proposals for rekeying */
  267. myproposal[PROPOSAL_KEX_ALGS] =
  268. compat_kex_proposal(ssh, options.kex_algorithms);
  269. if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
  270. fatal("kex_prop2buf: %s", ssh_err(r));
  271. #ifdef DEBUG_KEXDH
  272. /* send 1st encrypted/maced/compressed message */
  273. if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
  274. (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
  275. (r = sshpkt_send(ssh)) != 0 ||
  276. (r = ssh_packet_write_wait(ssh)) != 0)
  277. fatal("%s: %s", __func__, ssh_err(r));
  278. #endif
  279. }
  280. /*
  281. * Authenticate user
  282. */
  283. typedef struct cauthctxt Authctxt;
  284. typedef struct cauthmethod Authmethod;
  285. typedef struct identity Identity;
  286. typedef struct idlist Idlist;
  287. struct identity {
  288. TAILQ_ENTRY(identity) next;
  289. int agent_fd; /* >=0 if agent supports key */
  290. struct sshkey *key; /* public/private key */
  291. char *filename; /* comment for agent-only keys */
  292. int tried;
  293. int isprivate; /* key points to the private key */
  294. int userprovided;
  295. };
  296. TAILQ_HEAD(idlist, identity);
  297. struct cauthctxt {
  298. const char *server_user;
  299. const char *local_user;
  300. const char *host;
  301. const char *service;
  302. struct cauthmethod *method;
  303. sig_atomic_t success;
  304. char *authlist;
  305. #ifdef GSSAPI
  306. /* gssapi */
  307. gss_OID_set gss_supported_mechs;
  308. u_int mech_tried;
  309. #endif
  310. /* pubkey */
  311. struct idlist keys;
  312. int agent_fd;
  313. /* hostbased */
  314. Sensitive *sensitive;
  315. char *oktypes, *ktypes;
  316. const char *active_ktype;
  317. /* kbd-interactive */
  318. int info_req_seen;
  319. int attempt_kbdint;
  320. /* password */
  321. int attempt_passwd;
  322. /* generic */
  323. void *methoddata;
  324. };
  325. struct cauthmethod {
  326. char *name; /* string to compare against server's list */
  327. int (*userauth)(struct ssh *ssh);
  328. void (*cleanup)(struct ssh *ssh);
  329. int *enabled; /* flag in option struct that enables method */
  330. int *batch_flag; /* flag in option struct that disables method */
  331. };
  332. static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
  333. static int input_userauth_ext_info(int, u_int32_t, struct ssh *);
  334. static int input_userauth_success(int, u_int32_t, struct ssh *);
  335. static int input_userauth_failure(int, u_int32_t, struct ssh *);
  336. static int input_userauth_banner(int, u_int32_t, struct ssh *);
  337. static int input_userauth_error(int, u_int32_t, struct ssh *);
  338. static int input_userauth_info_req(int, u_int32_t, struct ssh *);
  339. static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
  340. static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
  341. static int userauth_none(struct ssh *);
  342. static int userauth_pubkey(struct ssh *);
  343. static int userauth_passwd(struct ssh *);
  344. static int userauth_kbdint(struct ssh *);
  345. static int userauth_hostbased(struct ssh *);
  346. #ifdef GSSAPI
  347. static int userauth_gssapi(struct ssh *);
  348. static void userauth_gssapi_cleanup(struct ssh *);
  349. static int input_gssapi_response(int type, u_int32_t, struct ssh *);
  350. static int input_gssapi_token(int type, u_int32_t, struct ssh *);
  351. static int input_gssapi_error(int, u_int32_t, struct ssh *);
  352. static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
  353. #endif
  354. void userauth(struct ssh *, char *);
  355. static void pubkey_cleanup(struct ssh *);
  356. static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
  357. static void pubkey_prepare(Authctxt *);
  358. static void pubkey_reset(Authctxt *);
  359. static struct sshkey *load_identity_file(Identity *);
  360. static Authmethod *authmethod_get(char *authlist);
  361. static Authmethod *authmethod_lookup(const char *name);
  362. static char *authmethods_get(void);
  363. Authmethod authmethods[] = {
  364. #ifdef GSSAPI
  365. {"gssapi-with-mic",
  366. userauth_gssapi,
  367. userauth_gssapi_cleanup,
  368. &options.gss_authentication,
  369. NULL},
  370. #endif
  371. {"hostbased",
  372. userauth_hostbased,
  373. NULL,
  374. &options.hostbased_authentication,
  375. NULL},
  376. {"publickey",
  377. userauth_pubkey,
  378. NULL,
  379. &options.pubkey_authentication,
  380. NULL},
  381. {"keyboard-interactive",
  382. userauth_kbdint,
  383. NULL,
  384. &options.kbd_interactive_authentication,
  385. &options.batch_mode},
  386. {"password",
  387. userauth_passwd,
  388. NULL,
  389. &options.password_authentication,
  390. &options.batch_mode},
  391. {"none",
  392. userauth_none,
  393. NULL,
  394. NULL,
  395. NULL},
  396. {NULL, NULL, NULL, NULL, NULL}
  397. };
  398. void
  399. ssh_userauth2(struct ssh *ssh, const char *local_user,
  400. const char *server_user, char *host, Sensitive *sensitive)
  401. {
  402. Authctxt authctxt;
  403. int r;
  404. if (options.challenge_response_authentication)
  405. options.kbd_interactive_authentication = 1;
  406. if (options.preferred_authentications == NULL)
  407. options.preferred_authentications = authmethods_get();
  408. /* setup authentication context */
  409. memset(&authctxt, 0, sizeof(authctxt));
  410. authctxt.server_user = server_user;
  411. authctxt.local_user = local_user;
  412. authctxt.host = host;
  413. authctxt.service = "ssh-connection"; /* service name */
  414. authctxt.success = 0;
  415. authctxt.method = authmethod_lookup("none");
  416. authctxt.authlist = NULL;
  417. authctxt.methoddata = NULL;
  418. authctxt.sensitive = sensitive;
  419. authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
  420. authctxt.info_req_seen = 0;
  421. authctxt.attempt_kbdint = 0;
  422. authctxt.attempt_passwd = 0;
  423. #if GSSAPI
  424. authctxt.gss_supported_mechs = NULL;
  425. authctxt.mech_tried = 0;
  426. #endif
  427. authctxt.agent_fd = -1;
  428. pubkey_prepare(&authctxt);
  429. if (authctxt.method == NULL) {
  430. fatal("%s: internal error: cannot send userauth none request",
  431. __func__);
  432. }
  433. if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
  434. (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
  435. (r = sshpkt_send(ssh)) != 0)
  436. fatal("%s: %s", __func__, ssh_err(r));
  437. ssh->authctxt = &authctxt;
  438. ssh_dispatch_init(ssh, &input_userauth_error);
  439. ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
  440. ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
  441. ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
  442. pubkey_cleanup(ssh);
  443. ssh->authctxt = NULL;
  444. ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
  445. if (!authctxt.success)
  446. fatal("Authentication failed.");
  447. /*
  448. * If the user wants to use the none cipher and/or none mac, do it post authentication
  449. * and only if the right conditions are met -- both of the NONE commands
  450. * must be true and there must be no tty allocated.
  451. */
  452. if (options.none_switch == 1 && options.none_enabled == 1) {
  453. if (!tty_flag) { /* no null on tty sessions */
  454. debug("Requesting none rekeying...");
  455. memcpy(&myproposal, &myproposal_default, sizeof(myproposal));
  456. myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
  457. myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
  458. fprintf(stderr, "WARNING: ENABLED NONE CIPHER!!!\n");
  459. /* NONEMAC can only be used in context of the NONE CIPHER */
  460. if (options.nonemac_enabled == 1) {
  461. myproposal[PROPOSAL_MAC_ALGS_STOC] = "none";
  462. myproposal[PROPOSAL_MAC_ALGS_CTOS] = "none";
  463. fprintf(stderr, "WARNING: ENABLED NONE MAC\n");
  464. }
  465. kex_prop2buf(ssh->kex->my, myproposal);
  466. packet_request_rekeying();
  467. } else {
  468. /* requested NONE cipher when in a tty */
  469. debug("Cannot switch to NONE cipher with tty allocated");
  470. fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
  471. }
  472. }
  473. #ifdef WITH_OPENSSL
  474. if (options.disable_multithreaded == 0) {
  475. /* if we are using aes-ctr there can be issues in either a fork or sandbox
  476. * so the initial aes-ctr is defined to point to the original single process
  477. * evp. After authentication we'll be past the fork and the sandboxed privsep
  478. * so we repoint the define to the multithreaded evp. To start the threads we
  479. * then force a rekey
  480. */
  481. const void *cc = ssh_packet_get_send_context(ssh);
  482. /* only do this for the ctr cipher. otherwise gcm mode breaks. Don't know why though */
  483. if (strstr(cipher_ctx_name(cc), "ctr")) {
  484. debug("Single to Multithread CTR cipher swap - client request");
  485. cipher_reset_multithreaded();
  486. packet_request_rekeying();
  487. }
  488. }
  489. #endif
  490. debug("Authentication succeeded (%s).", authctxt.method->name);
  491. }
  492. /* ARGSUSED */
  493. static int
  494. input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
  495. {
  496. int r;
  497. if (ssh_packet_remaining(ssh) > 0) {
  498. char *reply;
  499. if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
  500. goto out;
  501. debug2("service_accept: %s", reply);
  502. free(reply);
  503. } else {
  504. debug2("buggy server: service_accept w/o service");
  505. }
  506. if ((r = sshpkt_get_end(ssh)) != 0)
  507. goto out;
  508. debug("SSH2_MSG_SERVICE_ACCEPT received");
  509. /* initial userauth request */
  510. userauth_none(ssh);
  511. ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
  512. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
  513. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
  514. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
  515. r = 0;
  516. out:
  517. return r;
  518. }
  519. /* ARGSUSED */
  520. static int
  521. input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
  522. {
  523. return kex_input_ext_info(type, seqnr, ssh);
  524. }
  525. void
  526. userauth(struct ssh *ssh, char *authlist)
  527. {
  528. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  529. if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
  530. authctxt->method->cleanup(ssh);
  531. free(authctxt->methoddata);
  532. authctxt->methoddata = NULL;
  533. if (authlist == NULL) {
  534. authlist = authctxt->authlist;
  535. } else {
  536. free(authctxt->authlist);
  537. authctxt->authlist = authlist;
  538. }
  539. for (;;) {
  540. Authmethod *method = authmethod_get(authlist);
  541. if (method == NULL)
  542. fatal("%s@%s: Permission denied (%s).",
  543. authctxt->server_user, authctxt->host, authlist);
  544. authctxt->method = method;
  545. /* reset the per method handler */
  546. ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
  547. SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
  548. /* and try new method */
  549. if (method->userauth(ssh) != 0) {
  550. debug2("we sent a %s packet, wait for reply", method->name);
  551. break;
  552. } else {
  553. debug2("we did not send a packet, disable method");
  554. method->enabled = NULL;
  555. }
  556. }
  557. }
  558. /* ARGSUSED */
  559. static int
  560. input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
  561. {
  562. fatal("%s: bad message during authentication: type %d", __func__, type);
  563. return 0;
  564. }
  565. /* ARGSUSED */
  566. static int
  567. input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
  568. {
  569. char *msg = NULL;
  570. size_t len;
  571. int r;
  572. debug3("%s", __func__);
  573. if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
  574. (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
  575. goto out;
  576. if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
  577. fmprintf(stderr, "%s", msg);
  578. r = 0;
  579. out:
  580. free(msg);
  581. return r;
  582. }
  583. /* ARGSUSED */
  584. static int
  585. input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
  586. {
  587. Authctxt *authctxt = ssh->authctxt;
  588. if (authctxt == NULL)
  589. fatal("%s: no authentication context", __func__);
  590. free(authctxt->authlist);
  591. authctxt->authlist = NULL;
  592. if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
  593. authctxt->method->cleanup(ssh);
  594. free(authctxt->methoddata);
  595. authctxt->methoddata = NULL;
  596. authctxt->success = 1; /* break out */
  597. return 0;
  598. }
  599. #if 0
  600. static int
  601. input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
  602. {
  603. Authctxt *authctxt = ssh->authctxt;
  604. if (authctxt == NULL)
  605. fatal("%s: no authentication context", __func__);
  606. fatal("Unexpected authentication success during %s.",
  607. authctxt->method->name);
  608. return 0;
  609. }
  610. #endif
  611. /* ARGSUSED */
  612. static int
  613. input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
  614. {
  615. Authctxt *authctxt = ssh->authctxt;
  616. char *authlist = NULL;
  617. u_char partial;
  618. if (authctxt == NULL)
  619. fatal("input_userauth_failure: no authentication context");
  620. if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 ||
  621. sshpkt_get_u8(ssh, &partial) != 0 ||
  622. sshpkt_get_end(ssh) != 0)
  623. goto out;
  624. if (partial != 0) {
  625. verbose("Authenticated with partial success.");
  626. /* reset state */
  627. pubkey_reset(authctxt);
  628. }
  629. debug("Authentications that can continue: %s", authlist);
  630. userauth(ssh, authlist);
  631. authlist = NULL;
  632. out:
  633. free(authlist);
  634. return 0;
  635. }
  636. /*
  637. * Format an identity for logging including filename, key type, fingerprint
  638. * and location (agent, etc.). Caller must free.
  639. */
  640. static char *
  641. format_identity(Identity *id)
  642. {
  643. char *fp = NULL, *ret = NULL;
  644. const char *note = "";
  645. if (id->key != NULL) {
  646. fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
  647. SSH_FP_DEFAULT);
  648. }
  649. if (id->key) {
  650. if ((id->key->flags & SSHKEY_FLAG_EXT) != 0)
  651. note = " token";
  652. else if (sshkey_is_sk(id->key))
  653. note = " authenticator";
  654. }
  655. xasprintf(&ret, "%s %s%s%s%s%s%s",
  656. id->filename,
  657. id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
  658. fp ? fp : "",
  659. id->userprovided ? " explicit" : "", note,
  660. id->agent_fd != -1 ? " agent" : "");
  661. free(fp);
  662. return ret;
  663. }
  664. /* ARGSUSED */
  665. static int
  666. input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
  667. {
  668. Authctxt *authctxt = ssh->authctxt;
  669. struct sshkey *key = NULL;
  670. Identity *id = NULL;
  671. int pktype, found = 0, sent = 0;
  672. size_t blen;
  673. char *pkalg = NULL, *fp = NULL, *ident = NULL;
  674. u_char *pkblob = NULL;
  675. int r;
  676. if (authctxt == NULL)
  677. fatal("input_userauth_pk_ok: no authentication context");
  678. if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
  679. (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
  680. (r = sshpkt_get_end(ssh)) != 0)
  681. goto done;
  682. if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
  683. debug("%s: server sent unknown pkalg %s", __func__, pkalg);
  684. goto done;
  685. }
  686. if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
  687. debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
  688. goto done;
  689. }
  690. if (key->type != pktype) {
  691. error("input_userauth_pk_ok: type mismatch "
  692. "for decoded key (received %d, expected %d)",
  693. key->type, pktype);
  694. goto done;
  695. }
  696. /*
  697. * search keys in the reverse order, because last candidate has been
  698. * moved to the end of the queue. this also avoids confusion by
  699. * duplicate keys
  700. */
  701. TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
  702. if (sshkey_equal(key, id->key)) {
  703. found = 1;
  704. break;
  705. }
  706. }
  707. if (!found || id == NULL) {
  708. fp = sshkey_fingerprint(key, options.fingerprint_hash,
  709. SSH_FP_DEFAULT);
  710. error("%s: server replied with unknown key: %s %s", __func__,
  711. sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
  712. goto done;
  713. }
  714. ident = format_identity(id);
  715. debug("Server accepts key: %s", ident);
  716. sent = sign_and_send_pubkey(ssh, id);
  717. r = 0;
  718. done:
  719. sshkey_free(key);
  720. free(ident);
  721. free(fp);
  722. free(pkalg);
  723. free(pkblob);
  724. /* try another method if we did not send a packet */
  725. if (r == 0 && sent == 0)
  726. userauth(ssh, NULL);
  727. return r;
  728. }
  729. #ifdef GSSAPI
  730. static int
  731. userauth_gssapi(struct ssh *ssh)
  732. {
  733. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  734. Gssctxt *gssctxt = NULL;
  735. OM_uint32 min;
  736. int r, ok = 0;
  737. gss_OID mech = NULL;
  738. /* Try one GSSAPI method at a time, rather than sending them all at
  739. * once. */
  740. if (authctxt->gss_supported_mechs == NULL)
  741. gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
  742. /* Check to see whether the mechanism is usable before we offer it */
  743. while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
  744. !ok) {
  745. mech = &authctxt->gss_supported_mechs->
  746. elements[authctxt->mech_tried];
  747. /* My DER encoding requires length<128 */
  748. if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
  749. mech, authctxt->host)) {
  750. ok = 1; /* Mechanism works */
  751. } else {
  752. authctxt->mech_tried++;
  753. }
  754. }
  755. if (!ok || mech == NULL)
  756. return 0;
  757. authctxt->methoddata=(void *)gssctxt;
  758. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  759. (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
  760. (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
  761. (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
  762. (r = sshpkt_put_u32(ssh, 1)) != 0 ||
  763. (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
  764. (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
  765. (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
  766. (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
  767. (r = sshpkt_send(ssh)) != 0)
  768. fatal("%s: %s", __func__, ssh_err(r));
  769. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
  770. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
  771. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
  772. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
  773. authctxt->mech_tried++; /* Move along to next candidate */
  774. return 1;
  775. }
  776. static void
  777. userauth_gssapi_cleanup(struct ssh *ssh)
  778. {
  779. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  780. Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
  781. ssh_gssapi_delete_ctx(&gssctxt);
  782. authctxt->methoddata = NULL;
  783. free(authctxt->gss_supported_mechs);
  784. authctxt->gss_supported_mechs = NULL;
  785. }
  786. static OM_uint32
  787. process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
  788. {
  789. Authctxt *authctxt = ssh->authctxt;
  790. Gssctxt *gssctxt = authctxt->methoddata;
  791. gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
  792. gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
  793. gss_buffer_desc gssbuf;
  794. OM_uint32 status, ms, flags;
  795. int r;
  796. status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
  797. recv_tok, &send_tok, &flags);
  798. if (send_tok.length > 0) {
  799. u_char type = GSS_ERROR(status) ?
  800. SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
  801. SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
  802. if ((r = sshpkt_start(ssh, type)) != 0 ||
  803. (r = sshpkt_put_string(ssh, send_tok.value,
  804. send_tok.length)) != 0 ||
  805. (r = sshpkt_send(ssh)) != 0)
  806. fatal("%s: %s", __func__, ssh_err(r));
  807. gss_release_buffer(&ms, &send_tok);
  808. }
  809. if (status == GSS_S_COMPLETE) {
  810. /* send either complete or MIC, depending on mechanism */
  811. if (!(flags & GSS_C_INTEG_FLAG)) {
  812. if ((r = sshpkt_start(ssh,
  813. SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
  814. (r = sshpkt_send(ssh)) != 0)
  815. fatal("%s: %s", __func__, ssh_err(r));
  816. } else {
  817. struct sshbuf *b;
  818. if ((b = sshbuf_new()) == NULL)
  819. fatal("%s: sshbuf_new failed", __func__);
  820. ssh_gssapi_buildmic(b, authctxt->server_user,
  821. authctxt->service, "gssapi-with-mic",
  822. ssh->kex->session_id);
  823. if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
  824. fatal("%s: sshbuf_mutable_ptr failed", __func__);
  825. gssbuf.length = sshbuf_len(b);
  826. status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
  827. if (!GSS_ERROR(status)) {
  828. if ((r = sshpkt_start(ssh,
  829. SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
  830. (r = sshpkt_put_string(ssh, mic.value,
  831. mic.length)) != 0 ||
  832. (r = sshpkt_send(ssh)) != 0)
  833. fatal("%s: %s", __func__, ssh_err(r));
  834. }
  835. sshbuf_free(b);
  836. gss_release_buffer(&ms, &mic);
  837. }
  838. }
  839. return status;
  840. }
  841. /* ARGSUSED */
  842. static int
  843. input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
  844. {
  845. Authctxt *authctxt = ssh->authctxt;
  846. Gssctxt *gssctxt;
  847. size_t oidlen;
  848. u_char *oidv = NULL;
  849. int r;
  850. if (authctxt == NULL)
  851. fatal("input_gssapi_response: no authentication context");
  852. gssctxt = authctxt->methoddata;
  853. /* Setup our OID */
  854. if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
  855. goto done;
  856. if (oidlen <= 2 ||
  857. oidv[0] != SSH_GSS_OIDTYPE ||
  858. oidv[1] != oidlen - 2) {
  859. debug("Badly encoded mechanism OID received");
  860. userauth(ssh, NULL);
  861. goto ok;
  862. }
  863. if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
  864. fatal("Server returned different OID than expected");
  865. if ((r = sshpkt_get_end(ssh)) != 0)
  866. goto done;
  867. if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
  868. /* Start again with next method on list */
  869. debug("Trying to start again");
  870. userauth(ssh, NULL);
  871. goto ok;
  872. }
  873. ok:
  874. r = 0;
  875. done:
  876. free(oidv);
  877. return r;
  878. }
  879. /* ARGSUSED */
  880. static int
  881. input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
  882. {
  883. Authctxt *authctxt = ssh->authctxt;
  884. gss_buffer_desc recv_tok;
  885. u_char *p = NULL;
  886. size_t len;
  887. OM_uint32 status;
  888. int r;
  889. if (authctxt == NULL)
  890. fatal("input_gssapi_response: no authentication context");
  891. if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
  892. (r = sshpkt_get_end(ssh)) != 0)
  893. goto out;
  894. recv_tok.value = p;
  895. recv_tok.length = len;
  896. status = process_gssapi_token(ssh, &recv_tok);
  897. /* Start again with the next method in the list */
  898. if (GSS_ERROR(status)) {
  899. userauth(ssh, NULL);
  900. /* ok */
  901. }
  902. r = 0;
  903. out:
  904. free(p);
  905. return r;
  906. }
  907. /* ARGSUSED */
  908. static int
  909. input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
  910. {
  911. Authctxt *authctxt = ssh->authctxt;
  912. Gssctxt *gssctxt;
  913. gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
  914. gss_buffer_desc recv_tok;
  915. OM_uint32 ms;
  916. u_char *p = NULL;
  917. size_t len;
  918. int r;
  919. if (authctxt == NULL)
  920. fatal("input_gssapi_response: no authentication context");
  921. gssctxt = authctxt->methoddata;
  922. if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
  923. (r = sshpkt_get_end(ssh)) != 0) {
  924. free(p);
  925. return r;
  926. }
  927. /* Stick it into GSSAPI and see what it says */
  928. recv_tok.value = p;
  929. recv_tok.length = len;
  930. (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
  931. &recv_tok, &send_tok, NULL);
  932. free(p);
  933. gss_release_buffer(&ms, &send_tok);
  934. /* Server will be returning a failed packet after this one */
  935. return 0;
  936. }
  937. /* ARGSUSED */
  938. static int
  939. input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
  940. {
  941. char *msg = NULL;
  942. char *lang = NULL;
  943. int r;
  944. if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
  945. (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
  946. (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
  947. (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
  948. goto out;
  949. r = sshpkt_get_end(ssh);
  950. debug("Server GSSAPI Error:\n%s", msg);
  951. out:
  952. free(msg);
  953. free(lang);
  954. return r;
  955. }
  956. #endif /* GSSAPI */
  957. static int
  958. userauth_none(struct ssh *ssh)
  959. {
  960. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  961. int r;
  962. /* initial userauth request */
  963. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  964. (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
  965. (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
  966. (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
  967. (r = sshpkt_send(ssh)) != 0)
  968. fatal("%s: %s", __func__, ssh_err(r));
  969. return 1;
  970. }
  971. static int
  972. userauth_passwd(struct ssh *ssh)
  973. {
  974. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  975. char *password, *prompt = NULL;
  976. const char *host = options.host_key_alias ? options.host_key_alias :
  977. authctxt->host;
  978. int r;
  979. if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
  980. return 0;
  981. if (authctxt->attempt_passwd != 1)
  982. error("Permission denied, please try again.");
  983. xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
  984. password = read_passphrase(prompt, 0);
  985. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  986. (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
  987. (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
  988. (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
  989. (r = sshpkt_put_u8(ssh, 0)) != 0 ||
  990. (r = sshpkt_put_cstring(ssh, password)) != 0 ||
  991. (r = sshpkt_add_padding(ssh, 64)) != 0 ||
  992. (r = sshpkt_send(ssh)) != 0)
  993. fatal("%s: %s", __func__, ssh_err(r));
  994. free(prompt);
  995. if (password != NULL)
  996. freezero(password, strlen(password));
  997. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
  998. &input_userauth_passwd_changereq);
  999. return 1;
  1000. }
  1001. /*
  1002. * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
  1003. */
  1004. /* ARGSUSED */
  1005. static int
  1006. input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
  1007. {
  1008. Authctxt *authctxt = ssh->authctxt;
  1009. char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
  1010. char prompt[256];
  1011. const char *host;
  1012. int r;
  1013. debug2("input_userauth_passwd_changereq");
  1014. if (authctxt == NULL)
  1015. fatal("input_userauth_passwd_changereq: "
  1016. "no authentication context");
  1017. host = options.host_key_alias ? options.host_key_alias : authctxt->host;
  1018. if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
  1019. (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
  1020. goto out;
  1021. if (strlen(info) > 0)
  1022. logit("%s", info);
  1023. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  1024. (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
  1025. (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
  1026. (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
  1027. (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
  1028. goto out;
  1029. snprintf(prompt, sizeof(prompt),
  1030. "Enter %.30s@%.128s's old password: ",
  1031. authctxt->server_user, host);
  1032. password = read_passphrase(prompt, 0);
  1033. if ((r = sshpkt_put_cstring(ssh, password)) != 0)
  1034. goto out;
  1035. freezero(password, strlen(password));
  1036. password = NULL;
  1037. while (password == NULL) {
  1038. snprintf(prompt, sizeof(prompt),
  1039. "Enter %.30s@%.128s's new password: ",
  1040. authctxt->server_user, host);
  1041. password = read_passphrase(prompt, RP_ALLOW_EOF);
  1042. if (password == NULL) {
  1043. /* bail out */
  1044. r = 0;
  1045. goto out;
  1046. }
  1047. snprintf(prompt, sizeof(prompt),
  1048. "Retype %.30s@%.128s's new password: ",
  1049. authctxt->server_user, host);
  1050. retype = read_passphrase(prompt, 0);
  1051. if (strcmp(password, retype) != 0) {
  1052. freezero(password, strlen(password));
  1053. logit("Mismatch; try again, EOF to quit.");
  1054. password = NULL;
  1055. }
  1056. freezero(retype, strlen(retype));
  1057. }
  1058. if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
  1059. (r = sshpkt_add_padding(ssh, 64)) != 0 ||
  1060. (r = sshpkt_send(ssh)) != 0)
  1061. goto out;
  1062. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
  1063. &input_userauth_passwd_changereq);
  1064. r = 0;
  1065. out:
  1066. if (password)
  1067. freezero(password, strlen(password));
  1068. free(info);
  1069. free(lang);
  1070. return r;
  1071. }
  1072. /*
  1073. * Select an algorithm for publickey signatures.
  1074. * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
  1075. *
  1076. * Call with ssh==NULL to ignore server-sig-algs extension list and
  1077. * only attempt with the key's base signature type.
  1078. */
  1079. static char *
  1080. key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
  1081. {
  1082. char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
  1083. /*
  1084. * The signature algorithm will only differ from the key algorithm
  1085. * for RSA keys/certs and when the server advertises support for
  1086. * newer (SHA2) algorithms.
  1087. */
  1088. if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
  1089. (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
  1090. (key->type == KEY_RSA_CERT && (ssh->compat & SSH_BUG_SIGTYPE))) {
  1091. /* Filter base key signature alg against our configuration */
  1092. return match_list(sshkey_ssh_name(key),
  1093. options.pubkey_accepted_algos, NULL);
  1094. }
  1095. /*
  1096. * For RSA keys/certs, since these might have a different sig type:
  1097. * find the first entry in PubkeyAcceptedAlgorithms of the right type
  1098. * that also appears in the supported signature algorithms list from
  1099. * the server.
  1100. */
  1101. oallowed = allowed = xstrdup(options.pubkey_accepted_algos);
  1102. while ((cp = strsep(&allowed, ",")) != NULL) {
  1103. if (sshkey_type_from_name(cp) != key->type)
  1104. continue;
  1105. tmp = match_list(sshkey_sigalg_by_name(cp),
  1106. ssh->kex->server_sig_algs, NULL);
  1107. if (tmp != NULL)
  1108. alg = xstrdup(cp);
  1109. free(tmp);
  1110. if (alg != NULL)
  1111. break;
  1112. }
  1113. free(oallowed);
  1114. return alg;
  1115. }
  1116. static int
  1117. identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
  1118. const u_char *data, size_t datalen, u_int compat, const char *alg)
  1119. {
  1120. struct sshkey *sign_key = NULL, *prv = NULL;
  1121. int retried = 0, r = SSH_ERR_INTERNAL_ERROR;
  1122. struct notifier_ctx *notifier = NULL;
  1123. char *fp = NULL, *pin = NULL, *prompt = NULL;
  1124. *sigp = NULL;
  1125. *lenp = 0;
  1126. /* The agent supports this key. */
  1127. if (id->key != NULL && id->agent_fd != -1) {
  1128. return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
  1129. data, datalen, alg, compat);
  1130. }
  1131. /*
  1132. * We have already loaded the private key or the private key is
  1133. * stored in external hardware.
  1134. */
  1135. if (id->key != NULL &&
  1136. (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
  1137. sign_key = id->key;
  1138. } else {
  1139. /* Load the private key from the file. */
  1140. if ((prv = load_identity_file(id)) == NULL)
  1141. return SSH_ERR_KEY_NOT_FOUND;
  1142. if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
  1143. error("%s: private key %s contents do not match public",
  1144. __func__, id->filename);
  1145. r = SSH_ERR_KEY_NOT_FOUND;
  1146. goto out;
  1147. }
  1148. sign_key = prv;
  1149. if (sshkey_is_sk(sign_key)) {
  1150. if ((sign_key->sk_flags &
  1151. SSH_SK_USER_VERIFICATION_REQD)) {
  1152. retry_pin:
  1153. xasprintf(&prompt, "Enter PIN for %s key %s: ",
  1154. sshkey_type(sign_key), id->filename);
  1155. pin = read_passphrase(prompt, 0);
  1156. }
  1157. if ((sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
  1158. /* XXX should batch mode just skip these? */
  1159. if ((fp = sshkey_fingerprint(sign_key,
  1160. options.fingerprint_hash,
  1161. SSH_FP_DEFAULT)) == NULL)
  1162. fatal("%s: fingerprint", __func__);
  1163. notifier = notify_start(options.batch_mode,
  1164. "Confirm user presence for key %s %s",
  1165. sshkey_type(sign_key), fp);
  1166. free(fp);
  1167. }
  1168. }
  1169. }
  1170. if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen,
  1171. alg, options.sk_provider, pin, compat)) != 0) {
  1172. debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
  1173. if (pin == NULL && !retried && sshkey_is_sk(sign_key) &&
  1174. r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
  1175. //notify_complete(notifier, NULL);
  1176. notifier = NULL;
  1177. retried = 1;
  1178. goto retry_pin;
  1179. }
  1180. goto out;
  1181. }
  1182. /*
  1183. * PKCS#11 tokens may not support all signature algorithms,
  1184. * so check what we get back.
  1185. */
  1186. if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) {
  1187. debug("%s: sshkey_check_sigtype: %s", __func__, ssh_err(r));
  1188. goto out;
  1189. }
  1190. /* success */
  1191. r = 0;
  1192. out:
  1193. free(prompt);
  1194. if (pin != NULL)
  1195. freezero(pin, strlen(pin));
  1196. //notify_complete(notifier, r == 0 ? "User presence confirmed" : NULL);
  1197. sshkey_free(prv);
  1198. return r;
  1199. }
  1200. static int
  1201. id_filename_matches(Identity *id, Identity *private_id)
  1202. {
  1203. const char *suffixes[] = { ".pub", "-cert.pub", NULL };
  1204. size_t len = strlen(id->filename), plen = strlen(private_id->filename);
  1205. size_t i, slen;
  1206. if (strcmp(id->filename, private_id->filename) == 0)
  1207. return 1;
  1208. for (i = 0; suffixes[i]; i++) {
  1209. slen = strlen(suffixes[i]);
  1210. if (len > slen && plen == len - slen &&
  1211. strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
  1212. memcmp(id->filename, private_id->filename, plen) == 0)
  1213. return 1;
  1214. }
  1215. return 0;
  1216. }
  1217. static int
  1218. sign_and_send_pubkey(struct ssh *ssh, Identity *id)
  1219. {
  1220. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  1221. struct sshbuf *b = NULL;
  1222. Identity *private_id, *sign_id = NULL;
  1223. u_char *signature = NULL;
  1224. size_t slen = 0, skip = 0;
  1225. int r, fallback_sigtype, sent = 0;
  1226. char *alg = NULL, *fp = NULL;
  1227. const char *loc = "";
  1228. if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
  1229. SSH_FP_DEFAULT)) == NULL)
  1230. return 0;
  1231. debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
  1232. /*
  1233. * If the key is an certificate, try to find a matching private key
  1234. * and use it to complete the signature.
  1235. * If no such private key exists, fall back to trying the certificate
  1236. * key itself in case it has a private half already loaded.
  1237. * This will try to set sign_id to the private key that will perform
  1238. * the signature.
  1239. */
  1240. if (sshkey_is_cert(id->key)) {
  1241. TAILQ_FOREACH(private_id, &authctxt->keys, next) {
  1242. if (sshkey_equal_public(id->key, private_id->key) &&
  1243. id->key->type != private_id->key->type) {
  1244. sign_id = private_id;
  1245. break;
  1246. }
  1247. }
  1248. /*
  1249. * Exact key matches are preferred, but also allow
  1250. * filename matches for non-PKCS#11/agent keys that
  1251. * didn't load public keys. This supports the case
  1252. * of keeping just a private key file and public
  1253. * certificate on disk.
  1254. */
  1255. if (sign_id == NULL &&
  1256. !id->isprivate && id->agent_fd == -1 &&
  1257. (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
  1258. TAILQ_FOREACH(private_id, &authctxt->keys, next) {
  1259. if (private_id->key == NULL &&
  1260. id_filename_matches(id, private_id)) {
  1261. sign_id = private_id;
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. if (sign_id != NULL) {
  1267. debug2("%s: using private key \"%s\"%s for "
  1268. "certificate", __func__, id->filename,
  1269. id->agent_fd != -1 ? " from agent" : "");
  1270. } else {
  1271. debug("%s: no separate private key for certificate "
  1272. "\"%s\"", __func__, id->filename);
  1273. }
  1274. }
  1275. /*
  1276. * If the above didn't select another identity to do the signing
  1277. * then default to the one we started with.
  1278. */
  1279. if (sign_id == NULL)
  1280. sign_id = id;
  1281. /* assemble and sign data */
  1282. for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
  1283. free(alg);
  1284. slen = 0;
  1285. signature = NULL;
  1286. if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
  1287. id->key)) == NULL) {
  1288. error("%s: no mutual signature supported", __func__);
  1289. goto out;
  1290. }
  1291. debug3("%s: signing using %s %s", __func__, alg, fp);
  1292. sshbuf_free(b);
  1293. if ((b = sshbuf_new()) == NULL)
  1294. fatal("%s: sshbuf_new failed", __func__);
  1295. if (ssh->compat & SSH_OLD_SESSIONID) {
  1296. if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
  1297. fatal(r, "sshbuf_putb");
  1298. } else {
  1299. if ((r = sshbuf_put_stringb(b,
  1300. ssh->kex->session_id)) != 0)
  1301. fatal(r, "sshbuf_put_stringb");
  1302. }
  1303. skip = sshbuf_len(b);
  1304. if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  1305. (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
  1306. (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
  1307. (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
  1308. (r = sshbuf_put_u8(b, 1)) != 0 ||
  1309. (r = sshbuf_put_cstring(b, alg)) != 0 ||
  1310. (r = sshkey_puts(id->key, b)) != 0) {
  1311. fatal("%s: assemble signed data: %s",
  1312. __func__, ssh_err(r));
  1313. }
  1314. /* generate signature */
  1315. r = identity_sign(sign_id, &signature, &slen,
  1316. sshbuf_ptr(b), sshbuf_len(b), ssh->compat, alg);
  1317. if (r == 0)
  1318. break;
  1319. else if (r == SSH_ERR_KEY_NOT_FOUND)
  1320. goto out; /* soft failure */
  1321. else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
  1322. !fallback_sigtype) {
  1323. if (sign_id->agent_fd != -1)
  1324. loc = "agent ";
  1325. else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
  1326. loc = "token ";
  1327. logit("%skey %s %s returned incorrect signature type",
  1328. loc, sshkey_type(id->key), fp);
  1329. continue;
  1330. }
  1331. error("%s: signing failed for %s \"%s\"%s: %s", __func__,
  1332. sshkey_type(sign_id->key), sign_id->filename,
  1333. id->agent_fd != -1 ? " from agent" : "", ssh_err(r));
  1334. goto out;
  1335. }
  1336. if (slen == 0 || signature == NULL) /* shouldn't happen */
  1337. fatal("%s: no signature", __func__);
  1338. /* append signature */
  1339. if ((r = sshbuf_put_string(b, signature, slen)) != 0)
  1340. fatal("%s: append signature: %s", __func__, ssh_err(r));
  1341. #ifdef DEBUG_PK
  1342. sshbuf_dump(b, stderr);
  1343. #endif
  1344. /* skip session id and packet type */
  1345. if ((r = sshbuf_consume(b, skip + 1)) != 0)
  1346. fatal("%s: consume: %s", __func__, ssh_err(r));
  1347. /* put remaining data from buffer into packet */
  1348. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  1349. (r = sshpkt_putb(ssh, b)) != 0 ||
  1350. (r = sshpkt_send(ssh)) != 0)
  1351. fatal("%s: enqueue request: %s", __func__, ssh_err(r));
  1352. /* success */
  1353. sent = 1;
  1354. out:
  1355. free(fp);
  1356. free(alg);
  1357. sshbuf_free(b);
  1358. freezero(signature, slen);
  1359. return sent;
  1360. }
  1361. static int
  1362. send_pubkey_test(struct ssh *ssh, Identity *id)
  1363. {
  1364. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  1365. u_char *blob = NULL;
  1366. char *alg = NULL;
  1367. size_t bloblen;
  1368. u_int have_sig = 0;
  1369. int sent = 0, r;
  1370. if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
  1371. debug("%s: no mutual signature algorithm", __func__);
  1372. goto out;
  1373. }
  1374. if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
  1375. /* we cannot handle this key */
  1376. debug3("%s: cannot handle key", __func__);
  1377. goto out;
  1378. }
  1379. /* register callback for USERAUTH_PK_OK message */
  1380. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
  1381. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  1382. (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
  1383. (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
  1384. (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
  1385. (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
  1386. (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
  1387. (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
  1388. (r = sshpkt_send(ssh)) != 0)
  1389. fatal("%s: %s", __func__, ssh_err(r));
  1390. sent = 1;
  1391. out:
  1392. free(alg);
  1393. free(blob);
  1394. return sent;
  1395. }
  1396. static struct sshkey *
  1397. load_identity_file(Identity *id)
  1398. {
  1399. struct sshkey *private = NULL;
  1400. char prompt[300], *passphrase, *comment;
  1401. int r, quit = 0, i;
  1402. struct stat st;
  1403. if (stat(id->filename, &st) == -1) {
  1404. do_log2(id->userprovided ?
  1405. SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_DEBUG3,
  1406. "no such identity: %s: %s", id->filename, strerror(errno));
  1407. return NULL;
  1408. }
  1409. snprintf(prompt, sizeof prompt,
  1410. "Enter passphrase for key '%.100s': ", id->filename);
  1411. for (i = 0; i <= options.number_of_password_prompts; i++) {
  1412. if (i == 0)
  1413. passphrase = "";
  1414. else {
  1415. passphrase = read_passphrase(prompt, 0);
  1416. if (*passphrase == '\0') {
  1417. debug2("no passphrase given, try next key");
  1418. free(passphrase);
  1419. break;
  1420. }
  1421. }
  1422. switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
  1423. passphrase, &private, &comment))) {
  1424. case 0:
  1425. break;
  1426. case SSH_ERR_KEY_WRONG_PASSPHRASE:
  1427. if (options.batch_mode) {
  1428. quit = 1;
  1429. break;
  1430. }
  1431. if (i != 0)
  1432. debug2("bad passphrase given, try again...");
  1433. break;
  1434. case SSH_ERR_SYSTEM_ERROR:
  1435. if (errno == ENOENT) {
  1436. debug2("Load key \"%s\": %s",
  1437. id->filename, ssh_err(r));
  1438. quit = 1;
  1439. break;
  1440. }
  1441. /* FALLTHROUGH */
  1442. default:
  1443. error("Load key \"%s\": %s", id->filename, ssh_err(r));
  1444. quit = 1;
  1445. break;
  1446. }
  1447. if (private != NULL && sshkey_is_sk(private) &&
  1448. options.sk_provider == NULL) {
  1449. debug("key \"%s\" is an authenticator-hosted key, "
  1450. "but no provider specified", id->filename);
  1451. sshkey_free(private);
  1452. private = NULL;
  1453. quit = 1;
  1454. }
  1455. if (!quit && private != NULL && id->agent_fd == -1 &&
  1456. !(id->key && id->isprivate))
  1457. maybe_add_key_to_agent(id->filename, private, comment,
  1458. passphrase);
  1459. if (i > 0)
  1460. freezero(passphrase, strlen(passphrase));
  1461. free(comment);
  1462. if (private != NULL || quit)
  1463. break;
  1464. }
  1465. return private;
  1466. }
  1467. static int
  1468. key_type_allowed_by_config(struct sshkey *key)
  1469. {
  1470. if (match_pattern_list(sshkey_ssh_name(key),
  1471. options.pubkey_accepted_algos, 0) == 1)
  1472. return 1;
  1473. /* RSA keys/certs might be allowed by alternate signature types */
  1474. switch (key->type) {
  1475. case KEY_RSA:
  1476. if (match_pattern_list("rsa-sha2-512",
  1477. options.pubkey_accepted_algos, 0) == 1)
  1478. return 1;
  1479. if (match_pattern_list("rsa-sha2-256",
  1480. options.pubkey_accepted_algos, 0) == 1)
  1481. return 1;
  1482. break;
  1483. case KEY_RSA_CERT:
  1484. if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
  1485. options.pubkey_accepted_algos, 0) == 1)
  1486. return 1;
  1487. if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
  1488. options.pubkey_accepted_algos, 0) == 1)
  1489. return 1;
  1490. break;
  1491. }
  1492. return 0;
  1493. }
  1494. /*
  1495. * try keys in the following order:
  1496. * 1. certificates listed in the config file
  1497. * 2. other input certificates
  1498. * 3. agent keys that are found in the config file
  1499. * 4. other agent keys
  1500. * 5. keys that are only listed in the config file
  1501. */
  1502. static void
  1503. pubkey_prepare(Authctxt *authctxt)
  1504. {
  1505. struct identity *id, *id2, *tmp;
  1506. struct idlist agent, files, *preferred;
  1507. struct sshkey *key;
  1508. int agent_fd = -1, i, r, found;
  1509. size_t j;
  1510. struct ssh_identitylist *idlist;
  1511. char *ident;
  1512. TAILQ_INIT(&agent); /* keys from the agent */
  1513. TAILQ_INIT(&files); /* keys from the config file */
  1514. preferred = &authctxt->keys;
  1515. TAILQ_INIT(preferred); /* preferred order of keys */
  1516. /* list of keys stored in the filesystem and PKCS#11 */
  1517. for (i = 0; i < options.num_identity_files; i++) {
  1518. key = options.identity_keys[i];
  1519. if (key && key->cert &&
  1520. key->cert->type != SSH2_CERT_TYPE_USER) {
  1521. debug("%s: ignoring certificate %s: not a user "
  1522. "certificate", __func__,
  1523. options.identity_files[i]);
  1524. continue;
  1525. }
  1526. if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
  1527. debug("%s: ignoring authenticator-hosted key %s as no "
  1528. "SecurityKeyProvider has been specified",
  1529. __func__, options.identity_files[i]);
  1530. continue;
  1531. }
  1532. options.identity_keys[i] = NULL;
  1533. id = xcalloc(1, sizeof(*id));
  1534. id->agent_fd = -1;
  1535. id->key = key;
  1536. id->filename = xstrdup(options.identity_files[i]);
  1537. id->userprovided = options.identity_file_userprovided[i];
  1538. TAILQ_INSERT_TAIL(&files, id, next);
  1539. }
  1540. /* list of certificates specified by user */
  1541. for (i = 0; i < options.num_certificate_files; i++) {
  1542. key = options.certificates[i];
  1543. if (!sshkey_is_cert(key) || key->cert == NULL ||
  1544. key->cert->type != SSH2_CERT_TYPE_USER) {
  1545. debug("%s: ignoring certificate %s: not a user "
  1546. "certificate", __func__,
  1547. options.identity_files[i]);
  1548. continue;
  1549. }
  1550. if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
  1551. debug("%s: ignoring authenticator-hosted key "
  1552. "certificate %s as no "
  1553. "SecurityKeyProvider has been specified",
  1554. __func__, options.identity_files[i]);
  1555. continue;
  1556. }
  1557. id = xcalloc(1, sizeof(*id));
  1558. id->agent_fd = -1;
  1559. id->key = key;
  1560. id->filename = xstrdup(options.certificate_files[i]);
  1561. id->userprovided = options.certificate_file_userprovided[i];
  1562. TAILQ_INSERT_TAIL(preferred, id, next);
  1563. }
  1564. /* list of keys supported by the agent */
  1565. if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
  1566. if (r != SSH_ERR_AGENT_NOT_PRESENT)
  1567. debug("%s: ssh_get_authentication_socket: %s",
  1568. __func__, ssh_err(r));
  1569. } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
  1570. if (r != SSH_ERR_AGENT_NO_IDENTITIES)
  1571. debug("%s: ssh_fetch_identitylist: %s",
  1572. __func__, ssh_err(r));
  1573. close(agent_fd);
  1574. } else {
  1575. for (j = 0; j < idlist->nkeys; j++) {
  1576. found = 0;
  1577. TAILQ_FOREACH(id, &files, next) {
  1578. /*
  1579. * agent keys from the config file are
  1580. * preferred
  1581. */
  1582. if (sshkey_equal(idlist->keys[j], id->key)) {
  1583. TAILQ_REMOVE(&files, id, next);
  1584. TAILQ_INSERT_TAIL(preferred, id, next);
  1585. id->agent_fd = agent_fd;
  1586. found = 1;
  1587. break;
  1588. }
  1589. }
  1590. if (!found && !options.identities_only) {
  1591. id = xcalloc(1, sizeof(*id));
  1592. /* XXX "steals" key/comment from idlist */
  1593. id->key = idlist->keys[j];
  1594. id->filename = idlist->comments[j];
  1595. idlist->keys[j] = NULL;
  1596. idlist->comments[j] = NULL;
  1597. id->agent_fd = agent_fd;
  1598. TAILQ_INSERT_TAIL(&agent, id, next);
  1599. }
  1600. }
  1601. ssh_free_identitylist(idlist);
  1602. /* append remaining agent keys */
  1603. TAILQ_CONCAT(preferred, &agent, next);
  1604. authctxt->agent_fd = agent_fd;
  1605. }
  1606. /* Prefer PKCS11 keys that are explicitly listed */
  1607. TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
  1608. if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
  1609. continue;
  1610. found = 0;
  1611. TAILQ_FOREACH(id2, &files, next) {
  1612. if (id2->key == NULL ||
  1613. (id2->key->flags & SSHKEY_FLAG_EXT) != 0)
  1614. continue;
  1615. if (sshkey_equal(id->key, id2->key)) {
  1616. TAILQ_REMOVE(&files, id, next);
  1617. TAILQ_INSERT_TAIL(preferred, id, next);
  1618. found = 1;
  1619. break;
  1620. }
  1621. }
  1622. /* If IdentitiesOnly set and key not found then don't use it */
  1623. if (!found && options.identities_only) {
  1624. TAILQ_REMOVE(&files, id, next);
  1625. freezero(id, sizeof(*id));
  1626. }
  1627. }
  1628. /* append remaining keys from the config file */
  1629. TAILQ_CONCAT(preferred, &files, next);
  1630. /* finally, filter by PubkeyAcceptedAlgorithms */
  1631. TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
  1632. if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
  1633. debug("Skipping %s key %s - "
  1634. "corresponding algo not in PubkeyAcceptedAlgorithms",
  1635. sshkey_ssh_name(id->key), id->filename);
  1636. TAILQ_REMOVE(preferred, id, next);
  1637. sshkey_free(id->key);
  1638. free(id->filename);
  1639. memset(id, 0, sizeof(*id));
  1640. continue;
  1641. }
  1642. }
  1643. /* List the keys we plan on using */
  1644. TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
  1645. ident = format_identity(id);
  1646. debug("Will attempt key: %s", ident);
  1647. free(ident);
  1648. }
  1649. debug2("%s: done", __func__);
  1650. }
  1651. static void
  1652. pubkey_cleanup(struct ssh *ssh)
  1653. {
  1654. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  1655. Identity *id;
  1656. if (authctxt->agent_fd != -1) {
  1657. ssh_close_authentication_socket(authctxt->agent_fd);
  1658. authctxt->agent_fd = -1;
  1659. }
  1660. for (id = TAILQ_FIRST(&authctxt->keys); id;
  1661. id = TAILQ_FIRST(&authctxt->keys)) {
  1662. TAILQ_REMOVE(&authctxt->keys, id, next);
  1663. sshkey_free(id->key);
  1664. free(id->filename);
  1665. free(id);
  1666. }
  1667. }
  1668. static void
  1669. pubkey_reset(Authctxt *authctxt)
  1670. {
  1671. Identity *id;
  1672. TAILQ_FOREACH(id, &authctxt->keys, next)
  1673. id->tried = 0;
  1674. }
  1675. static int
  1676. try_identity(struct ssh *ssh, Identity *id)
  1677. {
  1678. if (!id->key)
  1679. return (0);
  1680. if (sshkey_type_plain(id->key->type) == KEY_RSA &&
  1681. (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
  1682. debug("Skipped %s key %s for RSA/MD5 server",
  1683. sshkey_type(id->key), id->filename);
  1684. return (0);
  1685. }
  1686. return 1;
  1687. }
  1688. static int
  1689. userauth_pubkey(struct ssh *ssh)
  1690. {
  1691. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  1692. Identity *id;
  1693. int sent = 0;
  1694. char *ident;
  1695. while ((id = TAILQ_FIRST(&authctxt->keys))) {
  1696. if (id->tried++)
  1697. return (0);
  1698. /* move key to the end of the queue */
  1699. TAILQ_REMOVE(&authctxt->keys, id, next);
  1700. TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
  1701. /*
  1702. * send a test message if we have the public key. for
  1703. * encrypted keys we cannot do this and have to load the
  1704. * private key instead
  1705. */
  1706. if (id->key != NULL) {
  1707. if (try_identity(ssh, id)) {
  1708. ident = format_identity(id);
  1709. debug("Offering public key: %s", ident);
  1710. free(ident);
  1711. sent = send_pubkey_test(ssh, id);
  1712. }
  1713. } else {
  1714. debug("Trying private key: %s", id->filename);
  1715. id->key = load_identity_file(id);
  1716. if (id->key != NULL) {
  1717. if (try_identity(ssh, id)) {
  1718. id->isprivate = 1;
  1719. sent = sign_and_send_pubkey(ssh, id);
  1720. }
  1721. sshkey_free(id->key);
  1722. id->key = NULL;
  1723. id->isprivate = 0;
  1724. }
  1725. }
  1726. if (sent)
  1727. return (sent);
  1728. }
  1729. return (0);
  1730. }
  1731. /*
  1732. * Send userauth request message specifying keyboard-interactive method.
  1733. */
  1734. static int
  1735. userauth_kbdint(struct ssh *ssh)
  1736. {
  1737. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  1738. int r;
  1739. if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
  1740. return 0;
  1741. /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
  1742. if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
  1743. debug3("userauth_kbdint: disable: no info_req_seen");
  1744. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
  1745. return 0;
  1746. }
  1747. debug2("userauth_kbdint");
  1748. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  1749. (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
  1750. (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
  1751. (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
  1752. (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
  1753. (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
  1754. options.kbd_interactive_devices : "")) != 0 ||
  1755. (r = sshpkt_send(ssh)) != 0)
  1756. fatal("%s: %s", __func__, ssh_err(r));
  1757. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
  1758. return 1;
  1759. }
  1760. /*
  1761. * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
  1762. */
  1763. static int
  1764. input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
  1765. {
  1766. Authctxt *authctxt = ssh->authctxt;
  1767. char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
  1768. char *display_prompt = NULL, *response = NULL;
  1769. u_char echo = 0;
  1770. u_int num_prompts, i;
  1771. int r;
  1772. debug2("entering");
  1773. if (authctxt == NULL)
  1774. fatal("no authentication context");
  1775. authctxt->info_req_seen = 1;
  1776. if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
  1777. (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
  1778. (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
  1779. goto out;
  1780. if (strlen(name) > 0)
  1781. logit("%s", name);
  1782. if (strlen(inst) > 0)
  1783. logit("%s", inst);
  1784. if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
  1785. goto out;
  1786. /*
  1787. * Begin to build info response packet based on prompts requested.
  1788. * We commit to providing the correct number of responses, so if
  1789. * further on we run into a problem that prevents this, we have to
  1790. * be sure and clean this up and send a correct error response.
  1791. */
  1792. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
  1793. (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
  1794. goto out;
  1795. debug2("num_prompts %d", num_prompts);
  1796. for (i = 0; i < num_prompts; i++) {
  1797. if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
  1798. (r = sshpkt_get_u8(ssh, &echo)) != 0)
  1799. goto out;
  1800. if (asmprintf(&display_prompt, INT_MAX, NULL, "(%s@%s) %s",
  1801. authctxt->server_user, options.host_key_alias ?
  1802. options.host_key_alias : authctxt->host, prompt) == -1)
  1803. fatal("asmprintf failed");
  1804. response = read_passphrase(display_prompt, echo ? RP_ECHO : 0);
  1805. if ((r = sshpkt_put_cstring(ssh, response)) != 0)
  1806. goto out;
  1807. freezero(response, strlen(response));
  1808. free(prompt);
  1809. free(display_prompt);
  1810. display_prompt = response = prompt = NULL;
  1811. }
  1812. /* done with parsing incoming message. */
  1813. if ((r = sshpkt_get_end(ssh)) != 0 ||
  1814. (r = sshpkt_add_padding(ssh, 64)) != 0)
  1815. goto out;
  1816. r = sshpkt_send(ssh);
  1817. out:
  1818. if (response)
  1819. freezero(response, strlen(response));
  1820. free(prompt);
  1821. free(display_prompt);
  1822. free(name);
  1823. free(inst);
  1824. free(lang);
  1825. return r;
  1826. }
  1827. static int
  1828. ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
  1829. const u_char *data, size_t datalen)
  1830. {
  1831. struct sshbuf *b;
  1832. struct stat st;
  1833. pid_t pid;
  1834. int r, to[2], from[2], status;
  1835. int sock = ssh_packet_get_connection_in(ssh);
  1836. u_char rversion = 0, version = 2;
  1837. void (*osigchld)(int);
  1838. *sigp = NULL;
  1839. *lenp = 0;
  1840. if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
  1841. error("%s: not installed: %s", __func__, strerror(errno));
  1842. return -1;
  1843. }
  1844. if (fflush(stdout) != 0) {
  1845. error("%s: fflush: %s", __func__, strerror(errno));
  1846. return -1;
  1847. }
  1848. if (pipe(to) == -1) {
  1849. error("%s: pipe: %s", __func__, strerror(errno));
  1850. return -1;
  1851. }
  1852. if (pipe(from) == -1) {
  1853. error("%s: pipe: %s", __func__, strerror(errno));
  1854. return -1;
  1855. }
  1856. if ((pid = fork()) == -1) {
  1857. error("%s: fork: %s", __func__, strerror(errno));
  1858. return -1;
  1859. }
  1860. osigchld = ssh_signal(SIGCHLD, SIG_DFL);
  1861. if (pid == 0) {
  1862. close(from[0]);
  1863. if (dup2(from[1], STDOUT_FILENO) == -1)
  1864. fatal("%s: dup2: %s", __func__, strerror(errno));
  1865. close(to[1]);
  1866. if (dup2(to[0], STDIN_FILENO) == -1)
  1867. fatal("%s: dup2: %s", __func__, strerror(errno));
  1868. close(from[1]);
  1869. close(to[0]);
  1870. if (dup2(sock, STDERR_FILENO + 1) == -1)
  1871. fatal("%s: dup2: %s", __func__, strerror(errno));
  1872. sock = STDERR_FILENO + 1;
  1873. fcntl(sock, F_SETFD, 0); /* keep the socket on exec */
  1874. closefrom(sock + 1);
  1875. debug3("%s: [child] pid=%ld, exec %s",
  1876. __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
  1877. execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
  1878. fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
  1879. strerror(errno));
  1880. }
  1881. close(from[1]);
  1882. close(to[0]);
  1883. sock = STDERR_FILENO + 1;
  1884. if ((b = sshbuf_new()) == NULL)
  1885. fatal("%s: sshbuf_new failed", __func__);
  1886. /* send # of sock, data to be signed */
  1887. if ((r = sshbuf_put_u32(b, sock)) != 0 ||
  1888. (r = sshbuf_put_string(b, data, datalen)) != 0)
  1889. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1890. if (ssh_msg_send(to[1], version, b) == -1)
  1891. fatal("%s: couldn't send request", __func__);
  1892. sshbuf_reset(b);
  1893. r = ssh_msg_recv(from[0], b);
  1894. close(from[0]);
  1895. close(to[1]);
  1896. if (r < 0) {
  1897. error("%s: no reply", __func__);
  1898. goto fail;
  1899. }
  1900. errno = 0;
  1901. while (waitpid(pid, &status, 0) == -1) {
  1902. if (errno != EINTR) {
  1903. error("%s: waitpid %ld: %s",
  1904. __func__, (long)pid, strerror(errno));
  1905. goto fail;
  1906. }
  1907. }
  1908. if (!WIFEXITED(status)) {
  1909. error("%s: exited abnormally", __func__);
  1910. goto fail;
  1911. }
  1912. if (WEXITSTATUS(status) != 0) {
  1913. error("%s: exited with status %d",
  1914. __func__, WEXITSTATUS(status));
  1915. goto fail;
  1916. }
  1917. if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
  1918. error("%s: buffer error: %s", __func__, ssh_err(r));
  1919. goto fail;
  1920. }
  1921. if (rversion != version) {
  1922. error("%s: bad version", __func__);
  1923. goto fail;
  1924. }
  1925. if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
  1926. error("%s: buffer error: %s", __func__, ssh_err(r));
  1927. fail:
  1928. ssh_signal(SIGCHLD, osigchld);
  1929. sshbuf_free(b);
  1930. return -1;
  1931. }
  1932. ssh_signal(SIGCHLD, osigchld);
  1933. sshbuf_free(b);
  1934. return 0;
  1935. }
  1936. static int
  1937. userauth_hostbased(struct ssh *ssh)
  1938. {
  1939. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  1940. struct sshkey *private = NULL;
  1941. struct sshbuf *b = NULL;
  1942. u_char *sig = NULL, *keyblob = NULL;
  1943. char *fp = NULL, *chost = NULL, *lname = NULL;
  1944. size_t siglen = 0, keylen = 0;
  1945. int i, r, success = 0;
  1946. if (authctxt->ktypes == NULL) {
  1947. authctxt->oktypes = xstrdup(options.hostbased_accepted_algos);
  1948. authctxt->ktypes = authctxt->oktypes;
  1949. }
  1950. /*
  1951. * Work through each listed type pattern in HostbasedAcceptedAlgorithms,
  1952. * trying each hostkey that matches the type in turn.
  1953. */
  1954. for (;;) {
  1955. if (authctxt->active_ktype == NULL)
  1956. authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
  1957. if (authctxt->active_ktype == NULL ||
  1958. *authctxt->active_ktype == '\0')
  1959. break;
  1960. debug3("%s: trying key type %s", __func__,
  1961. authctxt->active_ktype);
  1962. /* check for a useful key */
  1963. private = NULL;
  1964. for (i = 0; i < authctxt->sensitive->nkeys; i++) {
  1965. if (authctxt->sensitive->keys[i] == NULL ||
  1966. authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
  1967. continue;
  1968. if (match_pattern_list(
  1969. sshkey_ssh_name(authctxt->sensitive->keys[i]),
  1970. authctxt->active_ktype, 0) != 1)
  1971. continue;
  1972. /* we take and free the key */
  1973. private = authctxt->sensitive->keys[i];
  1974. authctxt->sensitive->keys[i] = NULL;
  1975. break;
  1976. }
  1977. /* Found one */
  1978. if (private != NULL)
  1979. break;
  1980. /* No more keys of this type; advance */
  1981. authctxt->active_ktype = NULL;
  1982. }
  1983. if (private == NULL) {
  1984. free(authctxt->oktypes);
  1985. authctxt->oktypes = authctxt->ktypes = NULL;
  1986. authctxt->active_ktype = NULL;
  1987. debug("No more client hostkeys for hostbased authentication.");
  1988. goto out;
  1989. }
  1990. if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
  1991. SSH_FP_DEFAULT)) == NULL) {
  1992. error("%s: sshkey_fingerprint failed", __func__);
  1993. goto out;
  1994. }
  1995. debug("%s: trying hostkey %s %s",
  1996. __func__, sshkey_ssh_name(private), fp);
  1997. /* figure out a name for the client host */
  1998. lname = get_local_name(ssh_packet_get_connection_in(ssh));
  1999. if (lname == NULL) {
  2000. error("%s: cannot get local ipaddr/name", __func__);
  2001. goto out;
  2002. }
  2003. /* XXX sshbuf_put_stringf? */
  2004. xasprintf(&chost, "%s.", lname);
  2005. debug2("%s: chost %s", __func__, chost);
  2006. /* construct data */
  2007. if ((b = sshbuf_new()) == NULL) {
  2008. error("%s: sshbuf_new failed", __func__);
  2009. goto out;
  2010. }
  2011. if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
  2012. error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
  2013. goto out;
  2014. }
  2015. if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
  2016. (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  2017. (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
  2018. (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
  2019. (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
  2020. (r = sshbuf_put_cstring(b, authctxt->active_ktype)) != 0 ||
  2021. (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
  2022. (r = sshbuf_put_cstring(b, chost)) != 0 ||
  2023. (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
  2024. error("%s: buffer error: %s", __func__, ssh_err(r));
  2025. goto out;
  2026. }
  2027. #ifdef DEBUG_PK
  2028. sshbuf_dump(b, stderr);
  2029. #endif
  2030. if ((r = ssh_keysign(ssh, private, &sig, &siglen,
  2031. sshbuf_ptr(b), sshbuf_len(b))) != 0) {
  2032. error("sign using hostkey %s %s failed",
  2033. sshkey_ssh_name(private), fp);
  2034. goto out;
  2035. }
  2036. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  2037. (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
  2038. (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
  2039. (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
  2040. (r = sshpkt_put_cstring(ssh, authctxt->active_ktype)) != 0 ||
  2041. (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
  2042. (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
  2043. (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
  2044. (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
  2045. (r = sshpkt_send(ssh)) != 0) {
  2046. error("%s: packet error: %s", __func__, ssh_err(r));
  2047. goto out;
  2048. }
  2049. success = 1;
  2050. out:
  2051. if (sig != NULL)
  2052. freezero(sig, siglen);
  2053. free(keyblob);
  2054. free(lname);
  2055. free(fp);
  2056. free(chost);
  2057. sshkey_free(private);
  2058. sshbuf_free(b);
  2059. return success;
  2060. }
  2061. /* find auth method */
  2062. /*
  2063. * given auth method name, if configurable options permit this method fill
  2064. * in auth_ident field and return true, otherwise return false.
  2065. */
  2066. static int
  2067. authmethod_is_enabled(Authmethod *method)
  2068. {
  2069. if (method == NULL)
  2070. return 0;
  2071. /* return false if options indicate this method is disabled */
  2072. if (method->enabled == NULL || *method->enabled == 0)
  2073. return 0;
  2074. /* return false if batch mode is enabled but method needs interactive mode */
  2075. if (method->batch_flag != NULL && *method->batch_flag != 0)
  2076. return 0;
  2077. return 1;
  2078. }
  2079. static Authmethod *
  2080. authmethod_lookup(const char *name)
  2081. {
  2082. Authmethod *method = NULL;
  2083. if (name != NULL)
  2084. for (method = authmethods; method->name != NULL; method++)
  2085. if (strcmp(name, method->name) == 0)
  2086. return method;
  2087. debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
  2088. return NULL;
  2089. }
  2090. /* XXX internal state */
  2091. static Authmethod *current = NULL;
  2092. static char *supported = NULL;
  2093. static char *preferred = NULL;
  2094. /*
  2095. * Given the authentication method list sent by the server, return the
  2096. * next method we should try. If the server initially sends a nil list,
  2097. * use a built-in default list.
  2098. */
  2099. static Authmethod *
  2100. authmethod_get(char *authlist)
  2101. {
  2102. char *name = NULL;
  2103. u_int next;
  2104. /* Use a suitable default if we're passed a nil list. */
  2105. if (authlist == NULL || strlen(authlist) == 0)
  2106. authlist = options.preferred_authentications;
  2107. if (supported == NULL || strcmp(authlist, supported) != 0) {
  2108. debug3("start over, passed a different list %s", authlist);
  2109. free(supported);
  2110. supported = xstrdup(authlist);
  2111. preferred = options.preferred_authentications;
  2112. debug3("preferred %s", preferred);
  2113. current = NULL;
  2114. } else if (current != NULL && authmethod_is_enabled(current))
  2115. return current;
  2116. for (;;) {
  2117. if ((name = match_list(preferred, supported, &next)) == NULL) {
  2118. debug("No more authentication methods to try.");
  2119. current = NULL;
  2120. return NULL;
  2121. }
  2122. preferred += next;
  2123. debug3("authmethod_lookup %s", name);
  2124. debug3("remaining preferred: %s", preferred);
  2125. if ((current = authmethod_lookup(name)) != NULL &&
  2126. authmethod_is_enabled(current)) {
  2127. debug3("authmethod_is_enabled %s", name);
  2128. debug("Next authentication method: %s", name);
  2129. free(name);
  2130. return current;
  2131. }
  2132. free(name);
  2133. }
  2134. }
  2135. static char *
  2136. authmethods_get(void)
  2137. {
  2138. Authmethod *method = NULL;
  2139. struct sshbuf *b;
  2140. char *list;
  2141. int r;
  2142. if ((b = sshbuf_new()) == NULL)
  2143. fatal("%s: sshbuf_new failed", __func__);
  2144. for (method = authmethods; method->name != NULL; method++) {
  2145. if (authmethod_is_enabled(method)) {
  2146. if ((r = sshbuf_putf(b, "%s%s",
  2147. sshbuf_len(b) ? "," : "", method->name)) != 0)
  2148. fatal("%s: buffer error: %s",
  2149. __func__, ssh_err(r));
  2150. }
  2151. }
  2152. if ((list = sshbuf_dup_string(b)) == NULL)
  2153. fatal("%s: sshbuf_dup_string failed", __func__);
  2154. sshbuf_free(b);
  2155. return list;
  2156. }