proxy_strategy.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/http/proxy.h>
  6. #include <aws/common/encoding.h>
  7. #include <aws/common/string.h>
  8. #include <aws/http/private/proxy_impl.h>
  9. #if defined(_MSC_VER)
  10. # pragma warning(push)
  11. # pragma warning(disable : 4221)
  12. #endif /* _MSC_VER */
  13. struct aws_http_proxy_negotiator *aws_http_proxy_negotiator_acquire(
  14. struct aws_http_proxy_negotiator *proxy_negotiator) {
  15. if (proxy_negotiator != NULL) {
  16. aws_ref_count_acquire(&proxy_negotiator->ref_count);
  17. }
  18. return proxy_negotiator;
  19. }
  20. void aws_http_proxy_negotiator_release(struct aws_http_proxy_negotiator *proxy_negotiator) {
  21. if (proxy_negotiator != NULL) {
  22. aws_ref_count_release(&proxy_negotiator->ref_count);
  23. }
  24. }
  25. struct aws_http_proxy_negotiator *aws_http_proxy_strategy_create_negotiator(
  26. struct aws_http_proxy_strategy *strategy,
  27. struct aws_allocator *allocator) {
  28. if (strategy == NULL || allocator == NULL) {
  29. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  30. return NULL;
  31. }
  32. return strategy->vtable->create_negotiator(strategy, allocator);
  33. }
  34. enum aws_http_proxy_negotiation_retry_directive aws_http_proxy_negotiator_get_retry_directive(
  35. struct aws_http_proxy_negotiator *proxy_negotiator) {
  36. if (proxy_negotiator != NULL) {
  37. if (proxy_negotiator->strategy_vtable.tunnelling_vtable->get_retry_directive != NULL) {
  38. return proxy_negotiator->strategy_vtable.tunnelling_vtable->get_retry_directive(proxy_negotiator);
  39. }
  40. }
  41. return AWS_HPNRD_STOP;
  42. }
  43. struct aws_http_proxy_strategy *aws_http_proxy_strategy_acquire(struct aws_http_proxy_strategy *proxy_strategy) {
  44. if (proxy_strategy != NULL) {
  45. aws_ref_count_acquire(&proxy_strategy->ref_count);
  46. }
  47. return proxy_strategy;
  48. }
  49. void aws_http_proxy_strategy_release(struct aws_http_proxy_strategy *proxy_strategy) {
  50. if (proxy_strategy != NULL) {
  51. aws_ref_count_release(&proxy_strategy->ref_count);
  52. }
  53. }
  54. /*****************************************************************************************************************/
  55. enum proxy_negotiator_connect_state {
  56. AWS_PNCS_READY,
  57. AWS_PNCS_IN_PROGRESS,
  58. AWS_PNCS_SUCCESS,
  59. AWS_PNCS_FAILURE,
  60. };
  61. /* Functions for basic auth strategy */
  62. struct aws_http_proxy_strategy_basic_auth {
  63. struct aws_allocator *allocator;
  64. struct aws_string *user_name;
  65. struct aws_string *password;
  66. struct aws_http_proxy_strategy strategy_base;
  67. };
  68. static void s_destroy_basic_auth_strategy(struct aws_http_proxy_strategy *proxy_strategy) {
  69. struct aws_http_proxy_strategy_basic_auth *basic_auth_strategy = proxy_strategy->impl;
  70. aws_string_destroy(basic_auth_strategy->user_name);
  71. aws_string_destroy(basic_auth_strategy->password);
  72. aws_mem_release(basic_auth_strategy->allocator, basic_auth_strategy);
  73. }
  74. struct aws_http_proxy_negotiator_basic_auth {
  75. struct aws_allocator *allocator;
  76. struct aws_http_proxy_strategy *strategy;
  77. enum proxy_negotiator_connect_state connect_state;
  78. struct aws_http_proxy_negotiator negotiator_base;
  79. };
  80. static void s_destroy_basic_auth_negotiator(struct aws_http_proxy_negotiator *proxy_negotiator) {
  81. struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator = proxy_negotiator->impl;
  82. aws_http_proxy_strategy_release(basic_auth_negotiator->strategy);
  83. aws_mem_release(basic_auth_negotiator->allocator, basic_auth_negotiator);
  84. }
  85. AWS_STATIC_STRING_FROM_LITERAL(s_proxy_authorization_header_name, "Proxy-Authorization");
  86. AWS_STATIC_STRING_FROM_LITERAL(s_proxy_authorization_header_basic_prefix, "Basic ");
  87. /*
  88. * Adds a proxy authentication header based on the basic authentication mode, rfc7617
  89. */
  90. static int s_add_basic_proxy_authentication_header(
  91. struct aws_allocator *allocator,
  92. struct aws_http_message *request,
  93. struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator) {
  94. struct aws_byte_buf base64_input_value;
  95. AWS_ZERO_STRUCT(base64_input_value);
  96. struct aws_byte_buf header_value;
  97. AWS_ZERO_STRUCT(header_value);
  98. int result = AWS_OP_ERR;
  99. struct aws_http_proxy_strategy_basic_auth *basic_auth_strategy = basic_auth_negotiator->strategy->impl;
  100. if (aws_byte_buf_init(
  101. &base64_input_value,
  102. allocator,
  103. basic_auth_strategy->user_name->len + basic_auth_strategy->password->len + 1)) {
  104. goto done;
  105. }
  106. /* First build a buffer with "username:password" in it */
  107. struct aws_byte_cursor username_cursor = aws_byte_cursor_from_string(basic_auth_strategy->user_name);
  108. if (aws_byte_buf_append(&base64_input_value, &username_cursor)) {
  109. goto done;
  110. }
  111. struct aws_byte_cursor colon_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(":");
  112. if (aws_byte_buf_append(&base64_input_value, &colon_cursor)) {
  113. goto done;
  114. }
  115. struct aws_byte_cursor password_cursor = aws_byte_cursor_from_string(basic_auth_strategy->password);
  116. if (aws_byte_buf_append(&base64_input_value, &password_cursor)) {
  117. goto done;
  118. }
  119. struct aws_byte_cursor base64_source_cursor =
  120. aws_byte_cursor_from_array(base64_input_value.buffer, base64_input_value.len);
  121. /* Figure out how much room we need in our final header value buffer */
  122. size_t required_size = 0;
  123. if (aws_base64_compute_encoded_len(base64_source_cursor.len, &required_size)) {
  124. goto done;
  125. }
  126. required_size += s_proxy_authorization_header_basic_prefix->len + 1;
  127. if (aws_byte_buf_init(&header_value, allocator, required_size)) {
  128. goto done;
  129. }
  130. /* Build the final header value by appending the authorization type and the base64 encoding string together */
  131. struct aws_byte_cursor basic_prefix = aws_byte_cursor_from_string(s_proxy_authorization_header_basic_prefix);
  132. if (aws_byte_buf_append_dynamic(&header_value, &basic_prefix)) {
  133. goto done;
  134. }
  135. if (aws_base64_encode(&base64_source_cursor, &header_value)) {
  136. goto done;
  137. }
  138. struct aws_http_header header = {
  139. .name = aws_byte_cursor_from_string(s_proxy_authorization_header_name),
  140. .value = aws_byte_cursor_from_array(header_value.buffer, header_value.len),
  141. };
  142. if (aws_http_message_add_header(request, header)) {
  143. goto done;
  144. }
  145. result = AWS_OP_SUCCESS;
  146. done:
  147. aws_byte_buf_clean_up(&header_value);
  148. aws_byte_buf_clean_up(&base64_input_value);
  149. return result;
  150. }
  151. int s_basic_auth_forward_add_header(
  152. struct aws_http_proxy_negotiator *proxy_negotiator,
  153. struct aws_http_message *message) {
  154. struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator = proxy_negotiator->impl;
  155. return s_add_basic_proxy_authentication_header(basic_auth_negotiator->allocator, message, basic_auth_negotiator);
  156. }
  157. void s_basic_auth_tunnel_add_header(
  158. struct aws_http_proxy_negotiator *proxy_negotiator,
  159. struct aws_http_message *message,
  160. aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback,
  161. aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback,
  162. void *internal_proxy_user_data) {
  163. struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator = proxy_negotiator->impl;
  164. if (basic_auth_negotiator->connect_state != AWS_PNCS_READY) {
  165. negotiation_termination_callback(message, AWS_ERROR_HTTP_PROXY_CONNECT_FAILED, internal_proxy_user_data);
  166. return;
  167. }
  168. basic_auth_negotiator->connect_state = AWS_PNCS_IN_PROGRESS;
  169. if (s_add_basic_proxy_authentication_header(basic_auth_negotiator->allocator, message, basic_auth_negotiator)) {
  170. negotiation_termination_callback(message, aws_last_error(), internal_proxy_user_data);
  171. return;
  172. }
  173. negotiation_http_request_forward_callback(message, internal_proxy_user_data);
  174. }
  175. static int s_basic_auth_on_connect_status(
  176. struct aws_http_proxy_negotiator *proxy_negotiator,
  177. enum aws_http_status_code status_code) {
  178. struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator = proxy_negotiator->impl;
  179. if (basic_auth_negotiator->connect_state == AWS_PNCS_IN_PROGRESS) {
  180. if (AWS_HTTP_STATUS_CODE_200_OK != status_code) {
  181. basic_auth_negotiator->connect_state = AWS_PNCS_FAILURE;
  182. } else {
  183. basic_auth_negotiator->connect_state = AWS_PNCS_SUCCESS;
  184. }
  185. }
  186. return AWS_OP_SUCCESS;
  187. }
  188. static struct aws_http_proxy_negotiator_forwarding_vtable s_basic_auth_proxy_negotiator_forwarding_vtable = {
  189. .forward_request_transform = s_basic_auth_forward_add_header,
  190. };
  191. static struct aws_http_proxy_negotiator_tunnelling_vtable s_basic_auth_proxy_negotiator_tunneling_vtable = {
  192. .on_status_callback = s_basic_auth_on_connect_status,
  193. .connect_request_transform = s_basic_auth_tunnel_add_header,
  194. };
  195. static struct aws_http_proxy_negotiator *s_create_basic_auth_negotiator(
  196. struct aws_http_proxy_strategy *proxy_strategy,
  197. struct aws_allocator *allocator) {
  198. if (proxy_strategy == NULL || allocator == NULL) {
  199. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  200. return NULL;
  201. }
  202. struct aws_http_proxy_negotiator_basic_auth *basic_auth_negotiator =
  203. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_basic_auth));
  204. if (basic_auth_negotiator == NULL) {
  205. return NULL;
  206. }
  207. basic_auth_negotiator->allocator = allocator;
  208. basic_auth_negotiator->connect_state = AWS_PNCS_READY;
  209. basic_auth_negotiator->negotiator_base.impl = basic_auth_negotiator;
  210. aws_ref_count_init(
  211. &basic_auth_negotiator->negotiator_base.ref_count,
  212. &basic_auth_negotiator->negotiator_base,
  213. (aws_simple_completion_callback *)s_destroy_basic_auth_negotiator);
  214. if (proxy_strategy->proxy_connection_type == AWS_HPCT_HTTP_FORWARD) {
  215. basic_auth_negotiator->negotiator_base.strategy_vtable.forwarding_vtable =
  216. &s_basic_auth_proxy_negotiator_forwarding_vtable;
  217. } else {
  218. basic_auth_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable =
  219. &s_basic_auth_proxy_negotiator_tunneling_vtable;
  220. }
  221. basic_auth_negotiator->strategy = aws_http_proxy_strategy_acquire(proxy_strategy);
  222. return &basic_auth_negotiator->negotiator_base;
  223. }
  224. static struct aws_http_proxy_strategy_vtable s_basic_auth_proxy_strategy_vtable = {
  225. .create_negotiator = s_create_basic_auth_negotiator,
  226. };
  227. struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_basic_auth(
  228. struct aws_allocator *allocator,
  229. struct aws_http_proxy_strategy_basic_auth_options *config) {
  230. if (config == NULL || allocator == NULL) {
  231. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  232. return NULL;
  233. }
  234. if (config->proxy_connection_type != AWS_HPCT_HTTP_FORWARD &&
  235. config->proxy_connection_type != AWS_HPCT_HTTP_TUNNEL) {
  236. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  237. return NULL;
  238. }
  239. struct aws_http_proxy_strategy_basic_auth *basic_auth_strategy =
  240. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_basic_auth));
  241. if (basic_auth_strategy == NULL) {
  242. return NULL;
  243. }
  244. basic_auth_strategy->strategy_base.impl = basic_auth_strategy;
  245. basic_auth_strategy->strategy_base.vtable = &s_basic_auth_proxy_strategy_vtable;
  246. basic_auth_strategy->allocator = allocator;
  247. basic_auth_strategy->strategy_base.proxy_connection_type = config->proxy_connection_type;
  248. aws_ref_count_init(
  249. &basic_auth_strategy->strategy_base.ref_count,
  250. &basic_auth_strategy->strategy_base,
  251. (aws_simple_completion_callback *)s_destroy_basic_auth_strategy);
  252. basic_auth_strategy->user_name = aws_string_new_from_cursor(allocator, &config->user_name);
  253. if (basic_auth_strategy->user_name == NULL) {
  254. goto on_error;
  255. }
  256. basic_auth_strategy->password = aws_string_new_from_cursor(allocator, &config->password);
  257. if (basic_auth_strategy->password == NULL) {
  258. goto on_error;
  259. }
  260. return &basic_auth_strategy->strategy_base;
  261. on_error:
  262. aws_http_proxy_strategy_release(&basic_auth_strategy->strategy_base);
  263. return NULL;
  264. }
  265. /*****************************************************************************************************************/
  266. struct aws_http_proxy_strategy_one_time_identity {
  267. struct aws_allocator *allocator;
  268. struct aws_http_proxy_strategy strategy_base;
  269. };
  270. struct aws_http_proxy_negotiator_one_time_identity {
  271. struct aws_allocator *allocator;
  272. enum proxy_negotiator_connect_state connect_state;
  273. struct aws_http_proxy_negotiator negotiator_base;
  274. };
  275. static void s_destroy_one_time_identity_negotiator(struct aws_http_proxy_negotiator *proxy_negotiator) {
  276. struct aws_http_proxy_negotiator_one_time_identity *identity_negotiator = proxy_negotiator->impl;
  277. aws_mem_release(identity_negotiator->allocator, identity_negotiator);
  278. }
  279. void s_one_time_identity_connect_transform(
  280. struct aws_http_proxy_negotiator *proxy_negotiator,
  281. struct aws_http_message *message,
  282. aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback,
  283. aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback,
  284. void *internal_proxy_user_data) {
  285. struct aws_http_proxy_negotiator_one_time_identity *one_time_identity_negotiator = proxy_negotiator->impl;
  286. if (one_time_identity_negotiator->connect_state != AWS_PNCS_READY) {
  287. negotiation_termination_callback(message, AWS_ERROR_HTTP_PROXY_CONNECT_FAILED, internal_proxy_user_data);
  288. return;
  289. }
  290. one_time_identity_negotiator->connect_state = AWS_PNCS_IN_PROGRESS;
  291. negotiation_http_request_forward_callback(message, internal_proxy_user_data);
  292. }
  293. static int s_one_time_identity_on_connect_status(
  294. struct aws_http_proxy_negotiator *proxy_negotiator,
  295. enum aws_http_status_code status_code) {
  296. struct aws_http_proxy_negotiator_one_time_identity *one_time_identity_negotiator = proxy_negotiator->impl;
  297. if (one_time_identity_negotiator->connect_state == AWS_PNCS_IN_PROGRESS) {
  298. if (AWS_HTTP_STATUS_CODE_200_OK != status_code) {
  299. one_time_identity_negotiator->connect_state = AWS_PNCS_FAILURE;
  300. } else {
  301. one_time_identity_negotiator->connect_state = AWS_PNCS_SUCCESS;
  302. }
  303. }
  304. return AWS_OP_SUCCESS;
  305. }
  306. static struct aws_http_proxy_negotiator_tunnelling_vtable s_one_time_identity_proxy_negotiator_tunneling_vtable = {
  307. .on_status_callback = s_one_time_identity_on_connect_status,
  308. .connect_request_transform = s_one_time_identity_connect_transform,
  309. };
  310. static struct aws_http_proxy_negotiator *s_create_one_time_identity_negotiator(
  311. struct aws_http_proxy_strategy *proxy_strategy,
  312. struct aws_allocator *allocator) {
  313. if (proxy_strategy == NULL || allocator == NULL) {
  314. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  315. return NULL;
  316. }
  317. struct aws_http_proxy_negotiator_one_time_identity *identity_negotiator =
  318. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_one_time_identity));
  319. if (identity_negotiator == NULL) {
  320. return NULL;
  321. }
  322. identity_negotiator->allocator = allocator;
  323. identity_negotiator->connect_state = AWS_PNCS_READY;
  324. identity_negotiator->negotiator_base.impl = identity_negotiator;
  325. aws_ref_count_init(
  326. &identity_negotiator->negotiator_base.ref_count,
  327. &identity_negotiator->negotiator_base,
  328. (aws_simple_completion_callback *)s_destroy_one_time_identity_negotiator);
  329. identity_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable =
  330. &s_one_time_identity_proxy_negotiator_tunneling_vtable;
  331. return &identity_negotiator->negotiator_base;
  332. }
  333. static struct aws_http_proxy_strategy_vtable s_one_time_identity_proxy_strategy_vtable = {
  334. .create_negotiator = s_create_one_time_identity_negotiator,
  335. };
  336. static void s_destroy_one_time_identity_strategy(struct aws_http_proxy_strategy *proxy_strategy) {
  337. struct aws_http_proxy_strategy_one_time_identity *identity_strategy = proxy_strategy->impl;
  338. aws_mem_release(identity_strategy->allocator, identity_strategy);
  339. }
  340. struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_one_time_identity(
  341. struct aws_allocator *allocator) {
  342. if (allocator == NULL) {
  343. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  344. return NULL;
  345. }
  346. struct aws_http_proxy_strategy_one_time_identity *identity_strategy =
  347. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_one_time_identity));
  348. if (identity_strategy == NULL) {
  349. return NULL;
  350. }
  351. identity_strategy->strategy_base.impl = identity_strategy;
  352. identity_strategy->strategy_base.vtable = &s_one_time_identity_proxy_strategy_vtable;
  353. identity_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL;
  354. identity_strategy->allocator = allocator;
  355. aws_ref_count_init(
  356. &identity_strategy->strategy_base.ref_count,
  357. &identity_strategy->strategy_base,
  358. (aws_simple_completion_callback *)s_destroy_one_time_identity_strategy);
  359. return &identity_strategy->strategy_base;
  360. }
  361. /******************************************************************************************************************/
  362. struct aws_http_proxy_strategy_forwarding_identity {
  363. struct aws_allocator *allocator;
  364. struct aws_http_proxy_strategy strategy_base;
  365. };
  366. struct aws_http_proxy_negotiator_forwarding_identity {
  367. struct aws_allocator *allocator;
  368. struct aws_http_proxy_negotiator negotiator_base;
  369. };
  370. static void s_destroy_forwarding_identity_negotiator(struct aws_http_proxy_negotiator *proxy_negotiator) {
  371. struct aws_http_proxy_negotiator_forwarding_identity *identity_negotiator = proxy_negotiator->impl;
  372. aws_mem_release(identity_negotiator->allocator, identity_negotiator);
  373. }
  374. int s_forwarding_identity_connect_transform(
  375. struct aws_http_proxy_negotiator *proxy_negotiator,
  376. struct aws_http_message *message) {
  377. (void)message;
  378. (void)proxy_negotiator;
  379. return AWS_OP_SUCCESS;
  380. }
  381. static struct aws_http_proxy_negotiator_forwarding_vtable s_forwarding_identity_proxy_negotiator_tunneling_vtable = {
  382. .forward_request_transform = s_forwarding_identity_connect_transform,
  383. };
  384. static struct aws_http_proxy_negotiator *s_create_forwarding_identity_negotiator(
  385. struct aws_http_proxy_strategy *proxy_strategy,
  386. struct aws_allocator *allocator) {
  387. if (proxy_strategy == NULL || allocator == NULL) {
  388. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  389. return NULL;
  390. }
  391. struct aws_http_proxy_negotiator_forwarding_identity *identity_negotiator =
  392. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_forwarding_identity));
  393. if (identity_negotiator == NULL) {
  394. return NULL;
  395. }
  396. identity_negotiator->allocator = allocator;
  397. identity_negotiator->negotiator_base.impl = identity_negotiator;
  398. aws_ref_count_init(
  399. &identity_negotiator->negotiator_base.ref_count,
  400. &identity_negotiator->negotiator_base,
  401. (aws_simple_completion_callback *)s_destroy_forwarding_identity_negotiator);
  402. identity_negotiator->negotiator_base.strategy_vtable.forwarding_vtable =
  403. &s_forwarding_identity_proxy_negotiator_tunneling_vtable;
  404. return &identity_negotiator->negotiator_base;
  405. }
  406. static struct aws_http_proxy_strategy_vtable s_forwarding_identity_strategy_vtable = {
  407. .create_negotiator = s_create_forwarding_identity_negotiator,
  408. };
  409. static void s_destroy_forwarding_identity_strategy(struct aws_http_proxy_strategy *proxy_strategy) {
  410. struct aws_http_proxy_strategy_forwarding_identity *identity_strategy = proxy_strategy->impl;
  411. aws_mem_release(identity_strategy->allocator, identity_strategy);
  412. }
  413. struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_forwarding_identity(struct aws_allocator *allocator) {
  414. if (allocator == NULL) {
  415. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  416. return NULL;
  417. }
  418. struct aws_http_proxy_strategy_forwarding_identity *identity_strategy =
  419. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_forwarding_identity));
  420. if (identity_strategy == NULL) {
  421. return NULL;
  422. }
  423. identity_strategy->strategy_base.impl = identity_strategy;
  424. identity_strategy->strategy_base.vtable = &s_forwarding_identity_strategy_vtable;
  425. identity_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_FORWARD;
  426. identity_strategy->allocator = allocator;
  427. aws_ref_count_init(
  428. &identity_strategy->strategy_base.ref_count,
  429. &identity_strategy->strategy_base,
  430. (aws_simple_completion_callback *)s_destroy_forwarding_identity_strategy);
  431. return &identity_strategy->strategy_base;
  432. }
  433. /******************************************************************************************************************/
  434. /* kerberos */
  435. AWS_STATIC_STRING_FROM_LITERAL(s_proxy_authorization_header_kerberos_prefix, "Negotiate ");
  436. struct aws_http_proxy_strategy_tunneling_kerberos {
  437. struct aws_allocator *allocator;
  438. aws_http_proxy_negotiation_get_token_sync_fn *get_token;
  439. void *get_token_user_data;
  440. struct aws_http_proxy_strategy strategy_base;
  441. };
  442. struct aws_http_proxy_negotiator_tunneling_kerberos {
  443. struct aws_allocator *allocator;
  444. struct aws_http_proxy_strategy *strategy;
  445. enum proxy_negotiator_connect_state connect_state;
  446. /*
  447. * ToDo: make adaptive and add any state needed here
  448. *
  449. * Likely things include response code (from the vanilla CONNECT) and the appropriate headers in
  450. * the response
  451. */
  452. struct aws_http_proxy_negotiator negotiator_base;
  453. };
  454. /*
  455. * Adds a proxy authentication header based on the user kerberos authentication token
  456. * This uses a token that is already base64 encoded
  457. */
  458. static int s_add_kerberos_proxy_usertoken_authentication_header(
  459. struct aws_allocator *allocator,
  460. struct aws_http_message *request,
  461. struct aws_byte_cursor user_token) {
  462. struct aws_byte_buf header_value;
  463. AWS_ZERO_STRUCT(header_value);
  464. int result = AWS_OP_ERR;
  465. if (aws_byte_buf_init(
  466. &header_value, allocator, s_proxy_authorization_header_kerberos_prefix->len + user_token.len)) {
  467. goto done;
  468. }
  469. /* First append proxy authorization header kerberos prefix */
  470. struct aws_byte_cursor auth_header_cursor =
  471. aws_byte_cursor_from_string(s_proxy_authorization_header_kerberos_prefix);
  472. if (aws_byte_buf_append(&header_value, &auth_header_cursor)) {
  473. goto done;
  474. }
  475. /* Append token to it */
  476. if (aws_byte_buf_append(&header_value, &user_token)) {
  477. goto done;
  478. }
  479. struct aws_http_header header = {
  480. .name = aws_byte_cursor_from_string(s_proxy_authorization_header_name),
  481. .value = aws_byte_cursor_from_array(header_value.buffer, header_value.len),
  482. };
  483. if (aws_http_message_add_header(request, header)) {
  484. goto done;
  485. }
  486. result = AWS_OP_SUCCESS;
  487. done:
  488. aws_byte_buf_clean_up(&header_value);
  489. return result;
  490. }
  491. static void s_kerberos_tunnel_transform_connect(
  492. struct aws_http_proxy_negotiator *proxy_negotiator,
  493. struct aws_http_message *message,
  494. aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback,
  495. aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback,
  496. void *internal_proxy_user_data) {
  497. struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl;
  498. struct aws_http_proxy_strategy_tunneling_kerberos *kerberos_strategy = kerberos_negotiator->strategy->impl;
  499. int result = AWS_OP_ERR;
  500. int error_code = AWS_ERROR_SUCCESS;
  501. struct aws_string *kerberos_token = NULL;
  502. if (kerberos_negotiator->connect_state == AWS_PNCS_FAILURE) {
  503. error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED;
  504. goto done;
  505. }
  506. if (kerberos_negotiator->connect_state != AWS_PNCS_READY) {
  507. error_code = AWS_ERROR_INVALID_STATE;
  508. goto done;
  509. }
  510. kerberos_negotiator->connect_state = AWS_PNCS_IN_PROGRESS;
  511. kerberos_token = kerberos_strategy->get_token(kerberos_strategy->get_token_user_data, &error_code);
  512. if (kerberos_token == NULL || error_code != AWS_ERROR_SUCCESS) {
  513. goto done;
  514. }
  515. /*transform the header with proxy authenticate:Negotiate and kerberos token*/
  516. if (s_add_kerberos_proxy_usertoken_authentication_header(
  517. kerberos_negotiator->allocator, message, aws_byte_cursor_from_string(kerberos_token))) {
  518. error_code = aws_last_error();
  519. goto done;
  520. }
  521. kerberos_negotiator->connect_state = AWS_PNCS_IN_PROGRESS;
  522. result = AWS_OP_SUCCESS;
  523. done:
  524. if (result != AWS_OP_SUCCESS) {
  525. if (error_code == AWS_ERROR_SUCCESS) {
  526. error_code = AWS_ERROR_UNKNOWN;
  527. }
  528. negotiation_termination_callback(message, error_code, internal_proxy_user_data);
  529. } else {
  530. negotiation_http_request_forward_callback(message, internal_proxy_user_data);
  531. }
  532. aws_string_destroy(kerberos_token);
  533. }
  534. static int s_kerberos_on_incoming_header_adaptive(
  535. struct aws_http_proxy_negotiator *proxy_negotiator,
  536. enum aws_http_header_block header_block,
  537. const struct aws_http_header *header_array,
  538. size_t num_headers) {
  539. struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl;
  540. (void)kerberos_negotiator;
  541. (void)header_block;
  542. (void)header_array;
  543. (void)num_headers;
  544. /* TODO: process vanilla CONNECT response headers here to improve usage/application */
  545. return AWS_OP_SUCCESS;
  546. }
  547. static int s_kerberos_on_connect_status(
  548. struct aws_http_proxy_negotiator *proxy_negotiator,
  549. enum aws_http_status_code status_code) {
  550. struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl;
  551. /* TODO: process status code of vanilla CONNECT request here to improve usage/application */
  552. if (kerberos_negotiator->connect_state == AWS_PNCS_IN_PROGRESS) {
  553. if (AWS_HTTP_STATUS_CODE_200_OK != status_code) {
  554. kerberos_negotiator->connect_state = AWS_PNCS_FAILURE;
  555. } else {
  556. kerberos_negotiator->connect_state = AWS_PNCS_SUCCESS;
  557. }
  558. }
  559. return AWS_OP_SUCCESS;
  560. }
  561. static int s_kerberos_on_incoming_body(
  562. struct aws_http_proxy_negotiator *proxy_negotiator,
  563. const struct aws_byte_cursor *data) {
  564. struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl;
  565. (void)kerberos_negotiator;
  566. (void)data;
  567. return AWS_OP_SUCCESS;
  568. }
  569. static struct aws_http_proxy_negotiator_tunnelling_vtable s_tunneling_kerberos_proxy_negotiator_tunneling_vtable = {
  570. .on_incoming_body_callback = s_kerberos_on_incoming_body,
  571. .on_incoming_headers_callback = s_kerberos_on_incoming_header_adaptive,
  572. .on_status_callback = s_kerberos_on_connect_status,
  573. .connect_request_transform = s_kerberos_tunnel_transform_connect,
  574. };
  575. static void s_destroy_tunneling_kerberos_negotiator(struct aws_http_proxy_negotiator *proxy_negotiator) {
  576. struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator = proxy_negotiator->impl;
  577. aws_http_proxy_strategy_release(kerberos_negotiator->strategy);
  578. aws_mem_release(kerberos_negotiator->allocator, kerberos_negotiator);
  579. }
  580. static struct aws_http_proxy_negotiator *s_create_tunneling_kerberos_negotiator(
  581. struct aws_http_proxy_strategy *proxy_strategy,
  582. struct aws_allocator *allocator) {
  583. if (proxy_strategy == NULL || allocator == NULL) {
  584. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  585. return NULL;
  586. }
  587. struct aws_http_proxy_negotiator_tunneling_kerberos *kerberos_negotiator =
  588. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_tunneling_kerberos));
  589. if (kerberos_negotiator == NULL) {
  590. return NULL;
  591. }
  592. kerberos_negotiator->allocator = allocator;
  593. kerberos_negotiator->negotiator_base.impl = kerberos_negotiator;
  594. aws_ref_count_init(
  595. &kerberos_negotiator->negotiator_base.ref_count,
  596. &kerberos_negotiator->negotiator_base,
  597. (aws_simple_completion_callback *)s_destroy_tunneling_kerberos_negotiator);
  598. kerberos_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable =
  599. &s_tunneling_kerberos_proxy_negotiator_tunneling_vtable;
  600. kerberos_negotiator->strategy = aws_http_proxy_strategy_acquire(proxy_strategy);
  601. return &kerberos_negotiator->negotiator_base;
  602. }
  603. static struct aws_http_proxy_strategy_vtable s_tunneling_kerberos_strategy_vtable = {
  604. .create_negotiator = s_create_tunneling_kerberos_negotiator,
  605. };
  606. static void s_destroy_tunneling_kerberos_strategy(struct aws_http_proxy_strategy *proxy_strategy) {
  607. struct aws_http_proxy_strategy_tunneling_kerberos *kerberos_strategy = proxy_strategy->impl;
  608. aws_mem_release(kerberos_strategy->allocator, kerberos_strategy);
  609. }
  610. struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_kerberos(
  611. struct aws_allocator *allocator,
  612. struct aws_http_proxy_strategy_tunneling_kerberos_options *config) {
  613. if (allocator == NULL || config == NULL || config->get_token == NULL) {
  614. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  615. return NULL;
  616. }
  617. struct aws_http_proxy_strategy_tunneling_kerberos *kerberos_strategy =
  618. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_tunneling_kerberos));
  619. if (kerberos_strategy == NULL) {
  620. return NULL;
  621. }
  622. kerberos_strategy->strategy_base.impl = kerberos_strategy;
  623. kerberos_strategy->strategy_base.vtable = &s_tunneling_kerberos_strategy_vtable;
  624. kerberos_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL;
  625. kerberos_strategy->allocator = allocator;
  626. aws_ref_count_init(
  627. &kerberos_strategy->strategy_base.ref_count,
  628. &kerberos_strategy->strategy_base,
  629. (aws_simple_completion_callback *)s_destroy_tunneling_kerberos_strategy);
  630. kerberos_strategy->get_token = config->get_token;
  631. kerberos_strategy->get_token_user_data = config->get_token_user_data;
  632. return &kerberos_strategy->strategy_base;
  633. }
  634. /******************************************************************************************************************/
  635. struct aws_http_proxy_strategy_tunneling_ntlm {
  636. struct aws_allocator *allocator;
  637. aws_http_proxy_negotiation_get_token_sync_fn *get_token;
  638. aws_http_proxy_negotiation_get_challenge_token_sync_fn *get_challenge_token;
  639. void *get_challenge_token_user_data;
  640. struct aws_http_proxy_strategy strategy_base;
  641. };
  642. struct aws_http_proxy_negotiator_tunneling_ntlm {
  643. struct aws_allocator *allocator;
  644. struct aws_http_proxy_strategy *strategy;
  645. enum proxy_negotiator_connect_state connect_state;
  646. struct aws_string *challenge_token;
  647. struct aws_http_proxy_negotiator negotiator_base;
  648. };
  649. AWS_STATIC_STRING_FROM_LITERAL(s_proxy_authorization_header_ntlm_prefix, "NTLM ");
  650. /*
  651. * Adds a proxy authentication header based on ntlm credential or response provided by user
  652. */
  653. static int s_add_ntlm_proxy_usertoken_authentication_header(
  654. struct aws_allocator *allocator,
  655. struct aws_http_message *request,
  656. struct aws_byte_cursor credential_response) {
  657. struct aws_byte_buf header_value;
  658. AWS_ZERO_STRUCT(header_value);
  659. int result = AWS_OP_ERR;
  660. if (aws_byte_buf_init(
  661. &header_value, allocator, s_proxy_authorization_header_ntlm_prefix->len + credential_response.len)) {
  662. goto done;
  663. }
  664. /* First append proxy authorization header prefix */
  665. struct aws_byte_cursor auth_header_cursor = aws_byte_cursor_from_string(s_proxy_authorization_header_ntlm_prefix);
  666. if (aws_byte_buf_append(&header_value, &auth_header_cursor)) {
  667. goto done;
  668. }
  669. /* Append the credential response to it; assumes already encoded properly (base64) */
  670. if (aws_byte_buf_append(&header_value, &credential_response)) {
  671. goto done;
  672. }
  673. struct aws_http_header header = {
  674. .name = aws_byte_cursor_from_string(s_proxy_authorization_header_name),
  675. .value = aws_byte_cursor_from_array(header_value.buffer, header_value.len),
  676. };
  677. if (aws_http_message_add_header(request, header)) {
  678. goto done;
  679. }
  680. result = AWS_OP_SUCCESS;
  681. done:
  682. aws_byte_buf_clean_up(&header_value);
  683. return result;
  684. }
  685. static void s_ntlm_tunnel_transform_connect(
  686. struct aws_http_proxy_negotiator *proxy_negotiator,
  687. struct aws_http_message *message,
  688. aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback,
  689. aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback,
  690. void *internal_proxy_user_data) {
  691. struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl;
  692. struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_strategy = ntlm_negotiator->strategy->impl;
  693. int result = AWS_OP_ERR;
  694. int error_code = AWS_ERROR_SUCCESS;
  695. struct aws_string *challenge_answer_token = NULL;
  696. struct aws_byte_cursor challenge_token;
  697. AWS_ZERO_STRUCT(challenge_token);
  698. if (ntlm_negotiator->connect_state == AWS_PNCS_FAILURE) {
  699. error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED;
  700. goto done;
  701. }
  702. if (ntlm_negotiator->connect_state != AWS_PNCS_READY) {
  703. error_code = AWS_ERROR_INVALID_STATE;
  704. goto done;
  705. }
  706. if (ntlm_negotiator->challenge_token == NULL) {
  707. error_code = AWS_ERROR_HTTP_PROXY_STRATEGY_NTLM_CHALLENGE_TOKEN_MISSING;
  708. goto done;
  709. }
  710. ntlm_negotiator->connect_state = AWS_PNCS_IN_PROGRESS;
  711. challenge_token = aws_byte_cursor_from_string(ntlm_negotiator->challenge_token);
  712. challenge_answer_token =
  713. ntlm_strategy->get_challenge_token(ntlm_strategy->get_challenge_token_user_data, &challenge_token, &error_code);
  714. if (challenge_answer_token == NULL || error_code != AWS_ERROR_SUCCESS) {
  715. goto done;
  716. }
  717. /*transform the header with proxy authenticate:Negotiate and kerberos token*/
  718. if (s_add_ntlm_proxy_usertoken_authentication_header(
  719. ntlm_negotiator->allocator, message, aws_byte_cursor_from_string(challenge_answer_token))) {
  720. error_code = aws_last_error();
  721. goto done;
  722. }
  723. ntlm_negotiator->connect_state = AWS_PNCS_IN_PROGRESS;
  724. result = AWS_OP_SUCCESS;
  725. done:
  726. if (result != AWS_OP_SUCCESS) {
  727. if (error_code == AWS_ERROR_SUCCESS) {
  728. error_code = AWS_ERROR_UNKNOWN;
  729. }
  730. negotiation_termination_callback(message, error_code, internal_proxy_user_data);
  731. } else {
  732. negotiation_http_request_forward_callback(message, internal_proxy_user_data);
  733. }
  734. aws_string_destroy(challenge_answer_token);
  735. }
  736. AWS_STATIC_STRING_FROM_LITERAL(s_ntlm_challenge_token_header, "Proxy-Authenticate");
  737. static int s_ntlm_on_incoming_header_adaptive(
  738. struct aws_http_proxy_negotiator *proxy_negotiator,
  739. enum aws_http_header_block header_block,
  740. const struct aws_http_header *header_array,
  741. size_t num_headers) {
  742. struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl;
  743. /*
  744. * only extract the challenge before we've started our own CONNECT attempt
  745. *
  746. * ToDo: we currently overwrite previous challenge tokens since it is unknown if multiple CONNECT requests
  747. * cause new challenges to be issued such that old challenges become invalid even if successfully computed
  748. */
  749. if (ntlm_negotiator->connect_state == AWS_PNCS_READY) {
  750. if (header_block == AWS_HTTP_HEADER_BLOCK_MAIN) {
  751. struct aws_byte_cursor proxy_authenticate_header_name =
  752. aws_byte_cursor_from_string(s_ntlm_challenge_token_header);
  753. for (size_t i = 0; i < num_headers; ++i) {
  754. struct aws_byte_cursor header_name_cursor = header_array[i].name;
  755. if (aws_byte_cursor_eq_ignore_case(&proxy_authenticate_header_name, &header_name_cursor)) {
  756. aws_string_destroy(ntlm_negotiator->challenge_token);
  757. struct aws_byte_cursor challenge_value_cursor = header_array[i].value;
  758. ntlm_negotiator->challenge_token =
  759. aws_string_new_from_cursor(ntlm_negotiator->allocator, &challenge_value_cursor);
  760. break;
  761. }
  762. }
  763. }
  764. }
  765. return AWS_OP_SUCCESS;
  766. }
  767. static int s_ntlm_on_connect_status(
  768. struct aws_http_proxy_negotiator *proxy_negotiator,
  769. enum aws_http_status_code status_code) {
  770. struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl;
  771. if (ntlm_negotiator->connect_state == AWS_PNCS_IN_PROGRESS) {
  772. if (AWS_HTTP_STATUS_CODE_200_OK != status_code) {
  773. ntlm_negotiator->connect_state = AWS_PNCS_FAILURE;
  774. } else {
  775. ntlm_negotiator->connect_state = AWS_PNCS_SUCCESS;
  776. }
  777. }
  778. return AWS_OP_SUCCESS;
  779. }
  780. static int s_ntlm_on_incoming_body(
  781. struct aws_http_proxy_negotiator *proxy_negotiator,
  782. const struct aws_byte_cursor *data) {
  783. struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl;
  784. (void)ntlm_negotiator;
  785. (void)data;
  786. return AWS_OP_SUCCESS;
  787. }
  788. static enum aws_http_proxy_negotiation_retry_directive s_ntlm_tunnel_get_retry_directive(
  789. struct aws_http_proxy_negotiator *proxy_negotiator) {
  790. (void)proxy_negotiator;
  791. return AWS_HPNRD_CURRENT_CONNECTION;
  792. }
  793. static struct aws_http_proxy_negotiator_tunnelling_vtable s_tunneling_ntlm_proxy_negotiator_tunneling_vtable = {
  794. .on_incoming_body_callback = s_ntlm_on_incoming_body,
  795. .on_incoming_headers_callback = s_ntlm_on_incoming_header_adaptive,
  796. .on_status_callback = s_ntlm_on_connect_status,
  797. .connect_request_transform = s_ntlm_tunnel_transform_connect,
  798. .get_retry_directive = s_ntlm_tunnel_get_retry_directive,
  799. };
  800. static void s_destroy_tunneling_ntlm_negotiator(struct aws_http_proxy_negotiator *proxy_negotiator) {
  801. struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator = proxy_negotiator->impl;
  802. aws_string_destroy(ntlm_negotiator->challenge_token);
  803. aws_http_proxy_strategy_release(ntlm_negotiator->strategy);
  804. aws_mem_release(ntlm_negotiator->allocator, ntlm_negotiator);
  805. }
  806. static struct aws_http_proxy_negotiator *s_create_tunneling_ntlm_negotiator(
  807. struct aws_http_proxy_strategy *proxy_strategy,
  808. struct aws_allocator *allocator) {
  809. if (proxy_strategy == NULL || allocator == NULL) {
  810. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  811. return NULL;
  812. }
  813. struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_negotiator =
  814. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_tunneling_ntlm));
  815. if (ntlm_negotiator == NULL) {
  816. return NULL;
  817. }
  818. ntlm_negotiator->allocator = allocator;
  819. ntlm_negotiator->negotiator_base.impl = ntlm_negotiator;
  820. aws_ref_count_init(
  821. &ntlm_negotiator->negotiator_base.ref_count,
  822. &ntlm_negotiator->negotiator_base,
  823. (aws_simple_completion_callback *)s_destroy_tunneling_ntlm_negotiator);
  824. ntlm_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable =
  825. &s_tunneling_ntlm_proxy_negotiator_tunneling_vtable;
  826. ntlm_negotiator->strategy = aws_http_proxy_strategy_acquire(proxy_strategy);
  827. return &ntlm_negotiator->negotiator_base;
  828. }
  829. static struct aws_http_proxy_strategy_vtable s_tunneling_ntlm_strategy_vtable = {
  830. .create_negotiator = s_create_tunneling_ntlm_negotiator,
  831. };
  832. static void s_destroy_tunneling_ntlm_strategy(struct aws_http_proxy_strategy *proxy_strategy) {
  833. struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_strategy = proxy_strategy->impl;
  834. aws_mem_release(ntlm_strategy->allocator, ntlm_strategy);
  835. }
  836. struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_ntlm(
  837. struct aws_allocator *allocator,
  838. struct aws_http_proxy_strategy_tunneling_ntlm_options *config) {
  839. if (allocator == NULL || config == NULL || config->get_challenge_token == NULL) {
  840. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  841. return NULL;
  842. }
  843. struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_strategy =
  844. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_tunneling_ntlm));
  845. if (ntlm_strategy == NULL) {
  846. return NULL;
  847. }
  848. ntlm_strategy->strategy_base.impl = ntlm_strategy;
  849. ntlm_strategy->strategy_base.vtable = &s_tunneling_ntlm_strategy_vtable;
  850. ntlm_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL;
  851. ntlm_strategy->allocator = allocator;
  852. aws_ref_count_init(
  853. &ntlm_strategy->strategy_base.ref_count,
  854. &ntlm_strategy->strategy_base,
  855. (aws_simple_completion_callback *)s_destroy_tunneling_ntlm_strategy);
  856. ntlm_strategy->get_challenge_token = config->get_challenge_token;
  857. ntlm_strategy->get_challenge_token_user_data = config->get_challenge_token_user_data;
  858. return &ntlm_strategy->strategy_base;
  859. }
  860. /******************************************************************************************************/
  861. static void s_ntlm_credential_tunnel_transform_connect(
  862. struct aws_http_proxy_negotiator *proxy_negotiator,
  863. struct aws_http_message *message,
  864. aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback,
  865. aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback,
  866. void *internal_proxy_user_data) {
  867. struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_credential_negotiator = proxy_negotiator->impl;
  868. struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_credential_strategy =
  869. ntlm_credential_negotiator->strategy->impl;
  870. int result = AWS_OP_ERR;
  871. int error_code = AWS_ERROR_SUCCESS;
  872. struct aws_string *token = NULL;
  873. if (ntlm_credential_negotiator->connect_state == AWS_PNCS_FAILURE) {
  874. error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED;
  875. goto done;
  876. }
  877. if (ntlm_credential_negotiator->connect_state != AWS_PNCS_READY) {
  878. error_code = AWS_ERROR_INVALID_STATE;
  879. goto done;
  880. }
  881. ntlm_credential_negotiator->connect_state = AWS_PNCS_IN_PROGRESS;
  882. token = ntlm_credential_strategy->get_token(ntlm_credential_strategy->get_challenge_token_user_data, &error_code);
  883. if (token == NULL || error_code != AWS_ERROR_SUCCESS) {
  884. goto done;
  885. }
  886. /*transform the header with proxy authenticate:Negotiate and kerberos token*/
  887. if (s_add_ntlm_proxy_usertoken_authentication_header(
  888. ntlm_credential_negotiator->allocator, message, aws_byte_cursor_from_string(token))) {
  889. error_code = aws_last_error();
  890. goto done;
  891. }
  892. ntlm_credential_negotiator->connect_state = AWS_PNCS_IN_PROGRESS;
  893. result = AWS_OP_SUCCESS;
  894. done:
  895. if (result != AWS_OP_SUCCESS) {
  896. if (error_code == AWS_ERROR_SUCCESS) {
  897. error_code = AWS_ERROR_UNKNOWN;
  898. }
  899. negotiation_termination_callback(message, error_code, internal_proxy_user_data);
  900. } else {
  901. negotiation_http_request_forward_callback(message, internal_proxy_user_data);
  902. }
  903. aws_string_destroy(token);
  904. }
  905. static struct aws_http_proxy_negotiator_tunnelling_vtable
  906. s_tunneling_ntlm_proxy_credential_negotiator_tunneling_vtable = {
  907. .on_incoming_body_callback = s_ntlm_on_incoming_body,
  908. .on_incoming_headers_callback = s_ntlm_on_incoming_header_adaptive,
  909. .on_status_callback = s_ntlm_on_connect_status,
  910. .connect_request_transform = s_ntlm_credential_tunnel_transform_connect,
  911. };
  912. static void s_destroy_tunneling_ntlm_credential_negotiator(struct aws_http_proxy_negotiator *proxy_negotiator) {
  913. struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_credential_negotiator = proxy_negotiator->impl;
  914. aws_string_destroy(ntlm_credential_negotiator->challenge_token);
  915. aws_http_proxy_strategy_release(ntlm_credential_negotiator->strategy);
  916. aws_mem_release(ntlm_credential_negotiator->allocator, ntlm_credential_negotiator);
  917. }
  918. static struct aws_http_proxy_negotiator *s_create_tunneling_ntlm_credential_negotiator(
  919. struct aws_http_proxy_strategy *proxy_strategy,
  920. struct aws_allocator *allocator) {
  921. if (proxy_strategy == NULL || allocator == NULL) {
  922. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  923. return NULL;
  924. }
  925. struct aws_http_proxy_negotiator_tunneling_ntlm *ntlm_credential_negotiator =
  926. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_tunneling_ntlm));
  927. if (ntlm_credential_negotiator == NULL) {
  928. return NULL;
  929. }
  930. ntlm_credential_negotiator->allocator = allocator;
  931. ntlm_credential_negotiator->negotiator_base.impl = ntlm_credential_negotiator;
  932. aws_ref_count_init(
  933. &ntlm_credential_negotiator->negotiator_base.ref_count,
  934. &ntlm_credential_negotiator->negotiator_base,
  935. (aws_simple_completion_callback *)s_destroy_tunneling_ntlm_credential_negotiator);
  936. ntlm_credential_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable =
  937. &s_tunneling_ntlm_proxy_credential_negotiator_tunneling_vtable;
  938. ntlm_credential_negotiator->strategy = aws_http_proxy_strategy_acquire(proxy_strategy);
  939. return &ntlm_credential_negotiator->negotiator_base;
  940. }
  941. static struct aws_http_proxy_strategy_vtable s_tunneling_ntlm_credential_strategy_vtable = {
  942. .create_negotiator = s_create_tunneling_ntlm_credential_negotiator,
  943. };
  944. static void s_destroy_tunneling_ntlm_credential_strategy(struct aws_http_proxy_strategy *proxy_strategy) {
  945. struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_credential_strategy = proxy_strategy->impl;
  946. aws_mem_release(ntlm_credential_strategy->allocator, ntlm_credential_strategy);
  947. }
  948. struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_ntlm_credential(
  949. struct aws_allocator *allocator,
  950. struct aws_http_proxy_strategy_tunneling_ntlm_options *config) {
  951. if (allocator == NULL || config == NULL || config->get_token == NULL) {
  952. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  953. return NULL;
  954. }
  955. struct aws_http_proxy_strategy_tunneling_ntlm *ntlm_credential_strategy =
  956. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_tunneling_ntlm));
  957. if (ntlm_credential_strategy == NULL) {
  958. return NULL;
  959. }
  960. ntlm_credential_strategy->strategy_base.impl = ntlm_credential_strategy;
  961. ntlm_credential_strategy->strategy_base.vtable = &s_tunneling_ntlm_credential_strategy_vtable;
  962. ntlm_credential_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL;
  963. ntlm_credential_strategy->allocator = allocator;
  964. aws_ref_count_init(
  965. &ntlm_credential_strategy->strategy_base.ref_count,
  966. &ntlm_credential_strategy->strategy_base,
  967. (aws_simple_completion_callback *)s_destroy_tunneling_ntlm_credential_strategy);
  968. ntlm_credential_strategy->get_token = config->get_token;
  969. ntlm_credential_strategy->get_challenge_token_user_data = config->get_challenge_token_user_data;
  970. return &ntlm_credential_strategy->strategy_base;
  971. }
  972. /******************************************************************************************************************/
  973. #define PROXY_STRATEGY_MAX_ADAPTIVE_STRATEGIES 4
  974. struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_adaptive(
  975. struct aws_allocator *allocator,
  976. struct aws_http_proxy_strategy_tunneling_adaptive_options *config) {
  977. if (allocator == NULL || config == NULL) {
  978. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  979. return NULL;
  980. }
  981. struct aws_http_proxy_strategy *strategies[PROXY_STRATEGY_MAX_ADAPTIVE_STRATEGIES];
  982. uint32_t strategy_count = 0;
  983. struct aws_http_proxy_strategy *identity_strategy = NULL;
  984. struct aws_http_proxy_strategy *kerberos_strategy = NULL;
  985. struct aws_http_proxy_strategy *ntlm_credential_strategy = NULL;
  986. struct aws_http_proxy_strategy *ntlm_strategy = NULL;
  987. struct aws_http_proxy_strategy *adaptive_sequence_strategy = NULL;
  988. identity_strategy = aws_http_proxy_strategy_new_tunneling_one_time_identity(allocator);
  989. if (identity_strategy == NULL) {
  990. goto done;
  991. }
  992. strategies[strategy_count++] = identity_strategy;
  993. if (config->kerberos_options != NULL) {
  994. kerberos_strategy = aws_http_proxy_strategy_new_tunneling_kerberos(allocator, config->kerberos_options);
  995. if (kerberos_strategy == NULL) {
  996. goto done;
  997. }
  998. strategies[strategy_count++] = kerberos_strategy;
  999. }
  1000. if (config->ntlm_options != NULL) {
  1001. ntlm_credential_strategy =
  1002. aws_http_proxy_strategy_new_tunneling_ntlm_credential(allocator, config->ntlm_options);
  1003. if (ntlm_credential_strategy == NULL) {
  1004. goto done;
  1005. }
  1006. strategies[strategy_count++] = ntlm_credential_strategy;
  1007. ntlm_strategy = aws_http_proxy_strategy_new_tunneling_ntlm(allocator, config->ntlm_options);
  1008. if (ntlm_strategy == NULL) {
  1009. goto done;
  1010. }
  1011. strategies[strategy_count++] = ntlm_strategy;
  1012. }
  1013. AWS_FATAL_ASSERT(strategy_count <= PROXY_STRATEGY_MAX_ADAPTIVE_STRATEGIES);
  1014. struct aws_http_proxy_strategy_tunneling_sequence_options sequence_config = {
  1015. .strategies = strategies,
  1016. .strategy_count = strategy_count,
  1017. };
  1018. adaptive_sequence_strategy = aws_http_proxy_strategy_new_tunneling_sequence(allocator, &sequence_config);
  1019. if (adaptive_sequence_strategy == NULL) {
  1020. goto done;
  1021. }
  1022. done:
  1023. aws_http_proxy_strategy_release(identity_strategy);
  1024. aws_http_proxy_strategy_release(kerberos_strategy);
  1025. aws_http_proxy_strategy_release(ntlm_credential_strategy);
  1026. aws_http_proxy_strategy_release(ntlm_strategy);
  1027. return adaptive_sequence_strategy;
  1028. }
  1029. /******************************************************************************************************************/
  1030. struct aws_http_proxy_strategy_tunneling_sequence {
  1031. struct aws_allocator *allocator;
  1032. struct aws_array_list strategies;
  1033. struct aws_http_proxy_strategy strategy_base;
  1034. };
  1035. struct aws_http_proxy_negotiator_tunneling_sequence {
  1036. struct aws_allocator *allocator;
  1037. struct aws_array_list negotiators;
  1038. size_t current_negotiator_transform_index;
  1039. void *original_internal_proxy_user_data;
  1040. aws_http_proxy_negotiation_terminate_fn *original_negotiation_termination_callback;
  1041. aws_http_proxy_negotiation_http_request_forward_fn *original_negotiation_http_request_forward_callback;
  1042. struct aws_http_proxy_negotiator negotiator_base;
  1043. };
  1044. static void s_sequence_tunnel_iteration_termination_callback(
  1045. struct aws_http_message *message,
  1046. int error_code,
  1047. void *user_data) {
  1048. struct aws_http_proxy_negotiator *proxy_negotiator = user_data;
  1049. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl;
  1050. AWS_LOGF_WARN(
  1051. AWS_LS_HTTP_PROXY_NEGOTIATION,
  1052. "(id=%p) Proxy negotiation step failed with error %d",
  1053. (void *)proxy_negotiator,
  1054. error_code);
  1055. int connection_error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED_RETRYABLE;
  1056. if (sequence_negotiator->current_negotiator_transform_index >=
  1057. aws_array_list_length(&sequence_negotiator->negotiators)) {
  1058. connection_error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED;
  1059. }
  1060. sequence_negotiator->original_negotiation_termination_callback(
  1061. message, connection_error_code, sequence_negotiator->original_internal_proxy_user_data);
  1062. }
  1063. static void s_sequence_tunnel_iteration_forward_callback(struct aws_http_message *message, void *user_data) {
  1064. struct aws_http_proxy_negotiator *proxy_negotiator = user_data;
  1065. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl;
  1066. sequence_negotiator->original_negotiation_http_request_forward_callback(
  1067. message, sequence_negotiator->original_internal_proxy_user_data);
  1068. }
  1069. static void s_sequence_tunnel_try_next_negotiator(
  1070. struct aws_http_proxy_negotiator *proxy_negotiator,
  1071. struct aws_http_message *message) {
  1072. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl;
  1073. size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators);
  1074. if (sequence_negotiator->current_negotiator_transform_index >= negotiator_count) {
  1075. goto on_error;
  1076. }
  1077. struct aws_http_proxy_negotiator *current_negotiator = NULL;
  1078. if (aws_array_list_get_at(
  1079. &sequence_negotiator->negotiators,
  1080. &current_negotiator,
  1081. sequence_negotiator->current_negotiator_transform_index++)) {
  1082. goto on_error;
  1083. }
  1084. current_negotiator->strategy_vtable.tunnelling_vtable->connect_request_transform(
  1085. current_negotiator,
  1086. message,
  1087. s_sequence_tunnel_iteration_termination_callback,
  1088. s_sequence_tunnel_iteration_forward_callback,
  1089. proxy_negotiator);
  1090. return;
  1091. on_error:
  1092. sequence_negotiator->original_negotiation_termination_callback(
  1093. message, AWS_ERROR_HTTP_PROXY_CONNECT_FAILED, sequence_negotiator->original_internal_proxy_user_data);
  1094. }
  1095. static void s_sequence_tunnel_transform_connect(
  1096. struct aws_http_proxy_negotiator *proxy_negotiator,
  1097. struct aws_http_message *message,
  1098. aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback,
  1099. aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback,
  1100. void *internal_proxy_user_data) {
  1101. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl;
  1102. sequence_negotiator->original_internal_proxy_user_data = internal_proxy_user_data;
  1103. sequence_negotiator->original_negotiation_termination_callback = negotiation_termination_callback;
  1104. sequence_negotiator->original_negotiation_http_request_forward_callback = negotiation_http_request_forward_callback;
  1105. s_sequence_tunnel_try_next_negotiator(proxy_negotiator, message);
  1106. }
  1107. static int s_sequence_on_incoming_headers(
  1108. struct aws_http_proxy_negotiator *proxy_negotiator,
  1109. enum aws_http_header_block header_block,
  1110. const struct aws_http_header *header_array,
  1111. size_t num_headers) {
  1112. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl;
  1113. size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators);
  1114. for (size_t i = 0; i < negotiator_count; ++i) {
  1115. struct aws_http_proxy_negotiator *negotiator = NULL;
  1116. if (aws_array_list_get_at(&sequence_negotiator->negotiators, &negotiator, i)) {
  1117. continue;
  1118. }
  1119. aws_http_proxy_negotiation_connect_on_incoming_headers_fn *on_incoming_headers =
  1120. negotiator->strategy_vtable.tunnelling_vtable->on_incoming_headers_callback;
  1121. if (on_incoming_headers != NULL) {
  1122. (*on_incoming_headers)(negotiator, header_block, header_array, num_headers);
  1123. }
  1124. }
  1125. return AWS_OP_SUCCESS;
  1126. }
  1127. static int s_sequence_on_connect_status(
  1128. struct aws_http_proxy_negotiator *proxy_negotiator,
  1129. enum aws_http_status_code status_code) {
  1130. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl;
  1131. size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators);
  1132. for (size_t i = 0; i < negotiator_count; ++i) {
  1133. struct aws_http_proxy_negotiator *negotiator = NULL;
  1134. if (aws_array_list_get_at(&sequence_negotiator->negotiators, &negotiator, i)) {
  1135. continue;
  1136. }
  1137. aws_http_proxy_negotiator_connect_status_fn *on_status =
  1138. negotiator->strategy_vtable.tunnelling_vtable->on_status_callback;
  1139. if (on_status != NULL) {
  1140. (*on_status)(negotiator, status_code);
  1141. }
  1142. }
  1143. return AWS_OP_SUCCESS;
  1144. }
  1145. static int s_sequence_on_incoming_body(
  1146. struct aws_http_proxy_negotiator *proxy_negotiator,
  1147. const struct aws_byte_cursor *data) {
  1148. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl;
  1149. size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators);
  1150. for (size_t i = 0; i < negotiator_count; ++i) {
  1151. struct aws_http_proxy_negotiator *negotiator = NULL;
  1152. if (aws_array_list_get_at(&sequence_negotiator->negotiators, &negotiator, i)) {
  1153. continue;
  1154. }
  1155. aws_http_proxy_negotiator_connect_on_incoming_body_fn *on_incoming_body =
  1156. negotiator->strategy_vtable.tunnelling_vtable->on_incoming_body_callback;
  1157. if (on_incoming_body != NULL) {
  1158. (*on_incoming_body)(negotiator, data);
  1159. }
  1160. }
  1161. return AWS_OP_SUCCESS;
  1162. }
  1163. static enum aws_http_proxy_negotiation_retry_directive s_sequence_get_retry_directive(
  1164. struct aws_http_proxy_negotiator *proxy_negotiator) {
  1165. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl;
  1166. if (sequence_negotiator->current_negotiator_transform_index <
  1167. aws_array_list_length(&sequence_negotiator->negotiators)) {
  1168. struct aws_http_proxy_negotiator *next_negotiator = NULL;
  1169. aws_array_list_get_at(
  1170. &sequence_negotiator->negotiators,
  1171. &next_negotiator,
  1172. sequence_negotiator->current_negotiator_transform_index);
  1173. enum aws_http_proxy_negotiation_retry_directive next_negotiator_directive =
  1174. aws_http_proxy_negotiator_get_retry_directive(next_negotiator);
  1175. if (next_negotiator_directive == AWS_HPNRD_CURRENT_CONNECTION) {
  1176. return AWS_HPNRD_CURRENT_CONNECTION;
  1177. } else {
  1178. return AWS_HPNRD_NEW_CONNECTION;
  1179. }
  1180. }
  1181. return AWS_HPNRD_STOP;
  1182. }
  1183. static struct aws_http_proxy_negotiator_tunnelling_vtable s_tunneling_sequence_proxy_negotiator_tunneling_vtable = {
  1184. .on_incoming_body_callback = s_sequence_on_incoming_body,
  1185. .on_incoming_headers_callback = s_sequence_on_incoming_headers,
  1186. .on_status_callback = s_sequence_on_connect_status,
  1187. .connect_request_transform = s_sequence_tunnel_transform_connect,
  1188. .get_retry_directive = s_sequence_get_retry_directive,
  1189. };
  1190. static void s_destroy_tunneling_sequence_negotiator(struct aws_http_proxy_negotiator *proxy_negotiator) {
  1191. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator = proxy_negotiator->impl;
  1192. size_t negotiator_count = aws_array_list_length(&sequence_negotiator->negotiators);
  1193. for (size_t i = 0; i < negotiator_count; ++i) {
  1194. struct aws_http_proxy_negotiator *negotiator = NULL;
  1195. if (aws_array_list_get_at(&sequence_negotiator->negotiators, &negotiator, i)) {
  1196. continue;
  1197. }
  1198. aws_http_proxy_negotiator_release(negotiator);
  1199. }
  1200. aws_array_list_clean_up(&sequence_negotiator->negotiators);
  1201. aws_mem_release(sequence_negotiator->allocator, sequence_negotiator);
  1202. }
  1203. static struct aws_http_proxy_negotiator *s_create_tunneling_sequence_negotiator(
  1204. struct aws_http_proxy_strategy *proxy_strategy,
  1205. struct aws_allocator *allocator) {
  1206. if (proxy_strategy == NULL || allocator == NULL) {
  1207. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  1208. return NULL;
  1209. }
  1210. struct aws_http_proxy_negotiator_tunneling_sequence *sequence_negotiator =
  1211. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_negotiator_tunneling_sequence));
  1212. if (sequence_negotiator == NULL) {
  1213. return NULL;
  1214. }
  1215. sequence_negotiator->allocator = allocator;
  1216. sequence_negotiator->negotiator_base.impl = sequence_negotiator;
  1217. aws_ref_count_init(
  1218. &sequence_negotiator->negotiator_base.ref_count,
  1219. &sequence_negotiator->negotiator_base,
  1220. (aws_simple_completion_callback *)s_destroy_tunneling_sequence_negotiator);
  1221. sequence_negotiator->negotiator_base.strategy_vtable.tunnelling_vtable =
  1222. &s_tunneling_sequence_proxy_negotiator_tunneling_vtable;
  1223. struct aws_http_proxy_strategy_tunneling_sequence *sequence_strategy = proxy_strategy->impl;
  1224. size_t strategy_count = aws_array_list_length(&sequence_strategy->strategies);
  1225. if (aws_array_list_init_dynamic(
  1226. &sequence_negotiator->negotiators, allocator, strategy_count, sizeof(struct aws_http_proxy_negotiator *))) {
  1227. goto on_error;
  1228. }
  1229. for (size_t i = 0; i < strategy_count; ++i) {
  1230. struct aws_http_proxy_strategy *strategy = NULL;
  1231. if (aws_array_list_get_at(&sequence_strategy->strategies, &strategy, i)) {
  1232. goto on_error;
  1233. }
  1234. struct aws_http_proxy_negotiator *negotiator = aws_http_proxy_strategy_create_negotiator(strategy, allocator);
  1235. if (negotiator == NULL) {
  1236. goto on_error;
  1237. }
  1238. if (aws_array_list_push_back(&sequence_negotiator->negotiators, &negotiator)) {
  1239. aws_http_proxy_negotiator_release(negotiator);
  1240. goto on_error;
  1241. }
  1242. }
  1243. return &sequence_negotiator->negotiator_base;
  1244. on_error:
  1245. aws_http_proxy_negotiator_release(&sequence_negotiator->negotiator_base);
  1246. return NULL;
  1247. }
  1248. static struct aws_http_proxy_strategy_vtable s_tunneling_sequence_strategy_vtable = {
  1249. .create_negotiator = s_create_tunneling_sequence_negotiator,
  1250. };
  1251. static void s_destroy_tunneling_sequence_strategy(struct aws_http_proxy_strategy *proxy_strategy) {
  1252. struct aws_http_proxy_strategy_tunneling_sequence *sequence_strategy = proxy_strategy->impl;
  1253. size_t strategy_count = aws_array_list_length(&sequence_strategy->strategies);
  1254. for (size_t i = 0; i < strategy_count; ++i) {
  1255. struct aws_http_proxy_strategy *strategy = NULL;
  1256. if (aws_array_list_get_at(&sequence_strategy->strategies, &strategy, i)) {
  1257. continue;
  1258. }
  1259. aws_http_proxy_strategy_release(strategy);
  1260. }
  1261. aws_array_list_clean_up(&sequence_strategy->strategies);
  1262. aws_mem_release(sequence_strategy->allocator, sequence_strategy);
  1263. }
  1264. struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_sequence(
  1265. struct aws_allocator *allocator,
  1266. struct aws_http_proxy_strategy_tunneling_sequence_options *config) {
  1267. if (allocator == NULL || config == NULL) {
  1268. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  1269. return NULL;
  1270. }
  1271. struct aws_http_proxy_strategy_tunneling_sequence *sequence_strategy =
  1272. aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_strategy_tunneling_sequence));
  1273. if (sequence_strategy == NULL) {
  1274. return NULL;
  1275. }
  1276. sequence_strategy->strategy_base.impl = sequence_strategy;
  1277. sequence_strategy->strategy_base.vtable = &s_tunneling_sequence_strategy_vtable;
  1278. sequence_strategy->strategy_base.proxy_connection_type = AWS_HPCT_HTTP_TUNNEL;
  1279. sequence_strategy->allocator = allocator;
  1280. aws_ref_count_init(
  1281. &sequence_strategy->strategy_base.ref_count,
  1282. &sequence_strategy->strategy_base,
  1283. (aws_simple_completion_callback *)s_destroy_tunneling_sequence_strategy);
  1284. if (aws_array_list_init_dynamic(
  1285. &sequence_strategy->strategies,
  1286. allocator,
  1287. config->strategy_count,
  1288. sizeof(struct aws_http_proxy_strategy *))) {
  1289. goto on_error;
  1290. }
  1291. for (size_t i = 0; i < config->strategy_count; ++i) {
  1292. struct aws_http_proxy_strategy *strategy = config->strategies[i];
  1293. if (aws_array_list_push_back(&sequence_strategy->strategies, &strategy)) {
  1294. goto on_error;
  1295. }
  1296. aws_http_proxy_strategy_acquire(strategy);
  1297. }
  1298. return &sequence_strategy->strategy_base;
  1299. on_error:
  1300. aws_http_proxy_strategy_release(&sequence_strategy->strategy_base);
  1301. return NULL;
  1302. }
  1303. #if defined(_MSC_VER)
  1304. # pragma warning(pop)
  1305. #endif /* _MSC_VER */