sspi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2015 Intel Corporation.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #include <config.h>
  18. #include "openconnect-internal.h"
  19. #include <errno.h>
  20. #include <string.h>
  21. static int sspi_setup(struct openconnect_info *vpninfo, struct http_auth_state *auth_state, const char *service, int proxy)
  22. {
  23. SECURITY_STATUS status;
  24. struct oc_text_buf *buf = buf_alloc();
  25. buf_append_utf16le(buf, service);
  26. buf_append_utf16le(buf, "/");
  27. buf_append_utf16le(buf, proxy ? vpninfo->proxy : vpninfo->hostname);
  28. if (buf_error(buf))
  29. return buf_free(buf);
  30. auth_state->sspi_target_name = (wchar_t *)buf->data;
  31. buf->data = NULL;
  32. buf_free(buf);
  33. status = AcquireCredentialsHandleW(NULL, (SEC_WCHAR *)L"Negotiate",
  34. SECPKG_CRED_OUTBOUND, NULL, NULL,
  35. NULL, NULL, &auth_state->sspi_cred,
  36. NULL);
  37. if (status != SEC_E_OK) {
  38. vpn_progress(vpninfo, PRG_ERR,
  39. _("AcquireCredentialsHandle() failed: %lx\n"), status);
  40. free(auth_state->sspi_target_name);
  41. auth_state->sspi_target_name = NULL;
  42. return -EIO;
  43. }
  44. return 0;
  45. }
  46. int gssapi_authorization(struct openconnect_info *vpninfo, int proxy,
  47. struct http_auth_state *auth_state, struct oc_text_buf *hdrbuf)
  48. {
  49. SECURITY_STATUS status;
  50. SecBufferDesc input_desc, output_desc;
  51. SecBuffer in_token, out_token;
  52. ULONG ret_flags;
  53. int first = 1;
  54. if (auth_state->state == AUTH_AVAILABLE && sspi_setup(vpninfo, auth_state, "HTTP", proxy)) {
  55. auth_state->state = AUTH_FAILED;
  56. return -EIO;
  57. }
  58. if (auth_state->challenge && *auth_state->challenge) {
  59. int token_len = -EINVAL;
  60. input_desc.cBuffers = 1;
  61. input_desc.pBuffers = &in_token;
  62. input_desc.ulVersion = SECBUFFER_VERSION;
  63. in_token.BufferType = SECBUFFER_TOKEN;
  64. in_token.pvBuffer = openconnect_base64_decode(&token_len,
  65. auth_state->challenge);
  66. if (!in_token.pvBuffer)
  67. return token_len;
  68. in_token.cbBuffer = token_len;
  69. first = 0;
  70. } else if (auth_state->state > AUTH_AVAILABLE) {
  71. /* This indicates failure. We were trying, but got an empty
  72. 'Proxy-Authorization: Negotiate' header back from the server
  73. implying that we should start again... */
  74. goto fail_gssapi;
  75. }
  76. auth_state->state = AUTH_IN_PROGRESS;
  77. output_desc.cBuffers = 1;
  78. output_desc.pBuffers = &out_token;
  79. output_desc.ulVersion = SECBUFFER_VERSION;
  80. out_token.BufferType = SECBUFFER_TOKEN;
  81. out_token.cbBuffer = 0;
  82. out_token.pvBuffer = NULL;
  83. status = InitializeSecurityContextW(&auth_state->sspi_cred,
  84. first ? NULL : &auth_state->sspi_ctx,
  85. auth_state->sspi_target_name,
  86. ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_CONFIDENTIALITY | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONNECTION,
  87. 0, SECURITY_NETWORK_DREP,
  88. first ? NULL : &input_desc,
  89. 0, &auth_state->sspi_ctx,
  90. &output_desc, &ret_flags, NULL);
  91. if (status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
  92. vpn_progress(vpninfo, PRG_ERR,
  93. _("InitializeSecurityContext() failed: %lx\n"), status);
  94. fail_gssapi:
  95. cleanup_gssapi_auth(vpninfo, auth_state);
  96. auth_state->state = AUTH_FAILED;
  97. /* -EAGAIN to first a reconnect if we had been trying. Else -EIO */
  98. return first ? -EIO : -EAGAIN;
  99. }
  100. buf_append(hdrbuf, "%sAuthorization: Negotiate ", proxy ? "Proxy-" : "");
  101. buf_append_base64(hdrbuf, out_token.pvBuffer, out_token.cbBuffer, 0);
  102. buf_append(hdrbuf, "\r\n");
  103. FreeContextBuffer(out_token.pvBuffer);
  104. return 0;
  105. }
  106. void cleanup_gssapi_auth(struct openconnect_info *vpninfo,
  107. struct http_auth_state *auth_state)
  108. {
  109. if (auth_state->state >= AUTH_IN_PROGRESS) {
  110. free(auth_state->sspi_target_name);
  111. auth_state->sspi_target_name = NULL;
  112. FreeCredentialsHandle(&auth_state->sspi_cred);
  113. DeleteSecurityContext(&auth_state->sspi_ctx);
  114. }
  115. }
  116. int socks_gssapi_auth(struct openconnect_info *vpninfo)
  117. {
  118. SECURITY_STATUS status;
  119. SecBufferDesc input_desc, output_desc;
  120. SecBuffer in_token, out_token;
  121. ULONG ret_flags;
  122. unsigned char *pktbuf;
  123. int first = 1;
  124. int i;
  125. int ret = -EIO;
  126. struct http_auth_state *auth_state = &vpninfo->proxy_auth[AUTH_TYPE_GSSAPI];
  127. if (sspi_setup(vpninfo, auth_state, "rcmd", 1))
  128. return -EIO;
  129. vpninfo->proxy_auth[AUTH_TYPE_GSSAPI].state = AUTH_IN_PROGRESS;
  130. pktbuf = malloc(65538);
  131. if (!pktbuf)
  132. return -ENOMEM;
  133. input_desc.cBuffers = 1;
  134. input_desc.pBuffers = &in_token;
  135. input_desc.ulVersion = SECBUFFER_VERSION;
  136. in_token.BufferType = SECBUFFER_TOKEN;
  137. output_desc.cBuffers = 1;
  138. output_desc.pBuffers = &out_token;
  139. output_desc.ulVersion = SECBUFFER_VERSION;
  140. out_token.BufferType = SECBUFFER_TOKEN;
  141. out_token.cbBuffer = 0;
  142. out_token.pvBuffer = NULL;
  143. while (1) {
  144. status = InitializeSecurityContextW(&auth_state->sspi_cred,
  145. first ? NULL : &auth_state->sspi_ctx,
  146. auth_state->sspi_target_name,
  147. ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_CONFIDENTIALITY | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONNECTION,
  148. 0, SECURITY_NETWORK_DREP,
  149. first ? NULL : &input_desc,
  150. 0, &auth_state->sspi_ctx,
  151. &output_desc, &ret_flags, NULL);
  152. if (status == SEC_E_OK) {
  153. /* If we still have a token to send, send it. */
  154. if (!out_token.cbBuffer) {
  155. vpn_progress(vpninfo, PRG_DEBUG,
  156. _("GSSAPI authentication completed\n"));
  157. ret = 0;
  158. break;
  159. }
  160. } else if (status != SEC_I_CONTINUE_NEEDED) {
  161. vpn_progress(vpninfo, PRG_ERR,
  162. _("InitializeSecurityContext() failed: %lx\n"), status);
  163. break;
  164. }
  165. if (out_token.cbBuffer > 65535) {
  166. vpn_progress(vpninfo, PRG_ERR,
  167. _("SSPI token too large (%ld bytes)\n"),
  168. out_token.cbBuffer);
  169. break;
  170. }
  171. pktbuf[0] = 1; /* ver */
  172. pktbuf[1] = 1; /* mtyp */
  173. store_be16(pktbuf + 2, out_token.cbBuffer);
  174. memcpy(pktbuf + 4, out_token.pvBuffer, out_token.cbBuffer);
  175. FreeContextBuffer(out_token.pvBuffer);
  176. vpn_progress(vpninfo, PRG_TRACE,
  177. _("Sending SSPI token of %lu bytes\n"), out_token.cbBuffer + 4);
  178. i = vpninfo->ssl_write(vpninfo, (void *)pktbuf, out_token.cbBuffer + 4);
  179. if (i < 0) {
  180. vpn_progress(vpninfo, PRG_ERR,
  181. _("Failed to send SSPI authentication token to proxy: %s\n"),
  182. strerror(-i));
  183. break;
  184. }
  185. i = vpninfo->ssl_read(vpninfo, (void *)pktbuf, 4);
  186. if (i < 0) {
  187. vpn_progress(vpninfo, PRG_ERR,
  188. _("Failed to receive SSPI authentication token from proxy: %s\n"),
  189. strerror(-i));
  190. break;
  191. }
  192. if (pktbuf[1] == 0xff) {
  193. vpn_progress(vpninfo, PRG_ERR,
  194. _("SOCKS server reported SSPI context failure\n"));
  195. break;
  196. } else if (pktbuf[1] != 1) {
  197. vpn_progress(vpninfo, PRG_ERR,
  198. _("Unknown SSPI status response (0x%02x) from SOCKS server\n"),
  199. pktbuf[1]);
  200. break;
  201. }
  202. in_token.cbBuffer = load_be16(pktbuf + 2);
  203. in_token.pvBuffer = pktbuf;
  204. first = 0;
  205. if (!in_token.cbBuffer) {
  206. vpn_progress(vpninfo, PRG_DEBUG,
  207. _("GSSAPI authentication completed\n"));
  208. ret = 0;
  209. break;
  210. }
  211. i = vpninfo->ssl_read(vpninfo, (void *)pktbuf, in_token.cbBuffer);
  212. if (i < 0) {
  213. vpn_progress(vpninfo, PRG_ERR,
  214. _("Failed to receive SSPI authentication token from proxy: %s\n"),
  215. strerror(-i));
  216. break;
  217. }
  218. vpn_progress(vpninfo, PRG_TRACE, _("Got SSPI token of %lu bytes: %02x %02x %02x %02x\n"),
  219. in_token.cbBuffer, pktbuf[0], pktbuf[1], pktbuf[2], pktbuf[3]);
  220. }
  221. if (!ret) {
  222. SecPkgContext_Sizes sizes;
  223. SecBufferDesc enc_desc;
  224. SecBuffer enc_bufs[3];
  225. int len;
  226. ret = -EIO;
  227. status = QueryContextAttributes(&auth_state->sspi_ctx, SECPKG_ATTR_SIZES, &sizes);
  228. if (status != SEC_E_OK) {
  229. vpn_progress(vpninfo, PRG_ERR,
  230. _("QueryContextAttributes() failed: %lx\n"), status);
  231. goto err;
  232. }
  233. enc_desc.cBuffers = 3;
  234. enc_desc.pBuffers = enc_bufs;
  235. enc_desc.ulVersion = SECBUFFER_VERSION;
  236. enc_bufs[0].BufferType = SECBUFFER_TOKEN;
  237. enc_bufs[0].cbBuffer = sizes.cbSecurityTrailer;
  238. enc_bufs[0].pvBuffer = malloc(sizes.cbSecurityTrailer);
  239. if (!enc_bufs[0].pvBuffer) {
  240. ret = -ENOMEM;
  241. goto err;
  242. }
  243. memset(enc_bufs[0].pvBuffer, 0, enc_bufs[0].cbBuffer);
  244. enc_bufs[1].BufferType = SECBUFFER_DATA;
  245. enc_bufs[1].pvBuffer = pktbuf;
  246. enc_bufs[1].cbBuffer = 1;
  247. /* All this just to sign this single byte... */
  248. pktbuf[0] = 0;
  249. enc_bufs[2].BufferType = SECBUFFER_PADDING;
  250. enc_bufs[2].cbBuffer = sizes.cbBlockSize;
  251. enc_bufs[2].pvBuffer = malloc(sizes.cbBlockSize);
  252. if (!enc_bufs[2].pvBuffer) {
  253. free(enc_bufs[0].pvBuffer);
  254. ret = -ENOMEM;
  255. goto err;
  256. }
  257. status = EncryptMessage(&auth_state->sspi_ctx, SECQOP_WRAP_NO_ENCRYPT, &enc_desc, 0);
  258. if (status != SEC_E_OK) {
  259. vpn_progress(vpninfo, PRG_ERR,
  260. _("EncryptMessage() failed: %lx\n"), status);
  261. free(enc_bufs[0].pvBuffer);
  262. free(enc_bufs[2].pvBuffer);
  263. goto err;
  264. }
  265. len = enc_bufs[0].cbBuffer + enc_bufs[1].cbBuffer + enc_bufs[2].cbBuffer;
  266. /* Check each one to avoid the (utterly theoretical) overflow when calculated
  267. into an 'int' type. */
  268. if (enc_bufs[1].cbBuffer != 1 || enc_bufs[0].cbBuffer > 65535 ||
  269. enc_bufs[2].cbBuffer > 65535 || len > 65535) {
  270. vpn_progress(vpninfo, PRG_ERR,
  271. _("EncryptMessage() result too large (%lu + %lu + %lu)\n"),
  272. enc_bufs[0].cbBuffer, enc_bufs[1].cbBuffer, enc_bufs[2].cbBuffer);
  273. free(enc_bufs[0].pvBuffer);
  274. free(enc_bufs[2].pvBuffer);
  275. goto err;
  276. }
  277. /* Our single byte of payload was *supposed* to be unencrypted but
  278. Windows doesn't always manage to do as it's told... */
  279. pktbuf[4 + enc_bufs[0].cbBuffer] = pktbuf[0];
  280. pktbuf[0] = 1;
  281. pktbuf[1] = 2;
  282. store_be16(pktbuf + 2, len);
  283. if (enc_bufs[0].cbBuffer)
  284. memcpy(pktbuf + 4, enc_bufs[0].pvBuffer, enc_bufs[0].cbBuffer);
  285. if (enc_bufs[2].cbBuffer)
  286. memcpy(pktbuf + 5 + enc_bufs[0].cbBuffer, enc_bufs[2].pvBuffer, enc_bufs[2].cbBuffer);
  287. free(enc_bufs[0].pvBuffer);
  288. free(enc_bufs[2].pvBuffer);
  289. vpn_progress(vpninfo, PRG_TRACE,
  290. _("Sending SSPI protection negotiation of %u bytes\n"), len + 4);
  291. i = vpninfo->ssl_write(vpninfo, (void *)pktbuf, len + 4);
  292. if (i < 0) {
  293. vpn_progress(vpninfo, PRG_ERR,
  294. _("Failed to send SSPI protection response to proxy: %s\n"),
  295. strerror(-i));
  296. goto err;
  297. }
  298. i = vpninfo->ssl_read(vpninfo, (void *)pktbuf, 4);
  299. if (i < 0) {
  300. vpn_progress(vpninfo, PRG_ERR,
  301. _("Failed to receive SSPI protection response from proxy: %s\n"),
  302. strerror(-i));
  303. goto err;
  304. }
  305. len = load_be16(pktbuf + 2);
  306. i = vpninfo->ssl_read(vpninfo, (void *)pktbuf, len);
  307. if (i < 0) {
  308. vpn_progress(vpninfo, PRG_ERR,
  309. _("Failed to receive SSPI protection response from proxy: %s\n"),
  310. strerror(-i));
  311. goto err;
  312. }
  313. vpn_progress(vpninfo, PRG_TRACE,
  314. _("Got SSPI protection response of %d bytes: %02x %02x %02x %02x\n"),
  315. len, pktbuf[0], pktbuf[1], pktbuf[2], pktbuf[3]);
  316. enc_desc.cBuffers = 2;
  317. enc_bufs[0].BufferType = SECBUFFER_STREAM;
  318. enc_bufs[0].cbBuffer = len;
  319. enc_bufs[0].pvBuffer = pktbuf;
  320. enc_bufs[1].BufferType = SECBUFFER_DATA;
  321. enc_bufs[1].cbBuffer = 0;
  322. enc_bufs[1].pvBuffer = NULL;
  323. status = DecryptMessage(&auth_state->sspi_ctx, &enc_desc, 0, NULL);
  324. if (status != SEC_E_OK) {
  325. vpn_progress(vpninfo, PRG_ERR,
  326. _("DecryptMessage failed: %lx\n"), status);
  327. goto err;
  328. }
  329. if (enc_bufs[1].cbBuffer != 1) {
  330. vpn_progress(vpninfo, PRG_ERR,
  331. _("Invalid SSPI protection response from proxy (%lu bytes)\n"),
  332. enc_bufs[1].cbBuffer);
  333. FreeContextBuffer(enc_bufs[1].pvBuffer);
  334. goto err;
  335. }
  336. i = *(char *)enc_bufs[1].pvBuffer;
  337. if (i == 1) {
  338. vpn_progress(vpninfo, PRG_ERR,
  339. _("SOCKS proxy demands message integrity, which is not supported\n"));
  340. goto err;
  341. } else if (i == 2) {
  342. vpn_progress(vpninfo, PRG_ERR,
  343. _("SOCKS proxy demands message confidentiality, which is not supported\n"));
  344. goto err;
  345. } else if (i) {
  346. vpn_progress(vpninfo, PRG_ERR,
  347. _("SOCKS proxy demands protection unknown type 0x%02x\n"),
  348. (unsigned char)i);
  349. goto err;
  350. }
  351. ret = 0;
  352. }
  353. err:
  354. cleanup_gssapi_auth(vpninfo, &vpninfo->proxy_auth[AUTH_TYPE_GSSAPI]);
  355. vpninfo->proxy_auth[AUTH_TYPE_GSSAPI].state = AUTH_UNSEEN;
  356. free(pktbuf);
  357. return ret;
  358. }