cproxy.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Routines to do cryptographic interaction with proxies in PuTTY.
  3. * This is in a separate module from proxy.c, so that it can be
  4. * conveniently removed in PuTTYtel by replacing this module with
  5. * the stub version nocproxy.c.
  6. */
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include "putty.h"
  11. #include "ssh.h" /* For MD5 support */
  12. #include "network.h"
  13. #include "proxy.h"
  14. #include "marshal.h"
  15. static void hmacmd5_chap(const unsigned char *challenge, int challen,
  16. const char *passwd, unsigned char *response)
  17. {
  18. struct hmacmd5_context *hmacmd5_ctx;
  19. int pwlen;
  20. hmacmd5_ctx = hmacmd5_make_context();
  21. pwlen = strlen(passwd);
  22. if (pwlen>64) {
  23. unsigned char md5buf[16];
  24. MD5Simple(passwd, pwlen, md5buf);
  25. hmacmd5_key(hmacmd5_ctx, md5buf, 16);
  26. } else {
  27. hmacmd5_key(hmacmd5_ctx, passwd, pwlen);
  28. }
  29. hmacmd5_do_hmac(hmacmd5_ctx, challenge, challen, response);
  30. hmacmd5_free_context(hmacmd5_ctx);
  31. }
  32. void proxy_socks5_offerencryptedauth(BinarySink *bs)
  33. {
  34. put_byte(bs, 0x03); /* CHAP */
  35. }
  36. int proxy_socks5_handlechap (ProxySocket *p)
  37. {
  38. /* CHAP authentication reply format:
  39. * version number (1 bytes) = 1
  40. * number of commands (1 byte)
  41. *
  42. * For each command:
  43. * command identifier (1 byte)
  44. * data length (1 byte)
  45. */
  46. unsigned char data[260];
  47. unsigned char outbuf[20];
  48. while(p->chap_num_attributes == 0 ||
  49. p->chap_num_attributes_processed < p->chap_num_attributes) {
  50. if (p->chap_num_attributes == 0 ||
  51. p->chap_current_attribute == -1) {
  52. /* CHAP normally reads in two bytes, either at the
  53. * beginning or for each attribute/value pair. But if
  54. * we're waiting for the value's data, we might not want
  55. * to read 2 bytes.
  56. */
  57. if (bufchain_size(&p->pending_input_data) < 2)
  58. return 1; /* not got anything yet */
  59. /* get the response */
  60. bufchain_fetch(&p->pending_input_data, data, 2);
  61. bufchain_consume(&p->pending_input_data, 2);
  62. }
  63. if (p->chap_num_attributes == 0) {
  64. /* If there are no attributes, this is our first msg
  65. * with the server, where we negotiate version and
  66. * number of attributes
  67. */
  68. if (data[0] != 0x01) {
  69. plug_closing(p->plug, "Proxy error: SOCKS proxy wants"
  70. " a different CHAP version",
  71. PROXY_ERROR_GENERAL, 0);
  72. return 1;
  73. }
  74. if (data[1] == 0x00) {
  75. plug_closing(p->plug, "Proxy error: SOCKS proxy won't"
  76. " negotiate CHAP with us",
  77. PROXY_ERROR_GENERAL, 0);
  78. return 1;
  79. }
  80. p->chap_num_attributes = data[1];
  81. } else {
  82. if (p->chap_current_attribute == -1) {
  83. /* We have to read in each attribute/value pair -
  84. * those we don't understand can be ignored, but
  85. * there are a few we'll need to handle.
  86. */
  87. p->chap_current_attribute = data[0];
  88. p->chap_current_datalen = data[1];
  89. }
  90. if (bufchain_size(&p->pending_input_data) <
  91. p->chap_current_datalen)
  92. return 1; /* not got everything yet */
  93. /* get the response */
  94. bufchain_fetch(&p->pending_input_data, data,
  95. p->chap_current_datalen);
  96. bufchain_consume(&p->pending_input_data,
  97. p->chap_current_datalen);
  98. switch (p->chap_current_attribute) {
  99. case 0x00:
  100. /* Successful authentication */
  101. if (data[0] == 0x00)
  102. p->state = 2;
  103. else {
  104. plug_closing(p->plug, "Proxy error: SOCKS proxy"
  105. " refused CHAP authentication",
  106. PROXY_ERROR_GENERAL, 0);
  107. return 1;
  108. }
  109. break;
  110. case 0x03:
  111. outbuf[0] = 0x01; /* Version */
  112. outbuf[1] = 0x01; /* One attribute */
  113. outbuf[2] = 0x04; /* Response */
  114. outbuf[3] = 0x10; /* Length */
  115. hmacmd5_chap(data, p->chap_current_datalen,
  116. conf_get_str(p->conf, CONF_proxy_password),
  117. &outbuf[4]);
  118. sk_write(p->sub_socket, outbuf, 20);
  119. break;
  120. case 0x11:
  121. /* Chose a protocol */
  122. if (data[0] != 0x85) {
  123. plug_closing(p->plug, "Proxy error: Server chose "
  124. "CHAP of other than HMAC-MD5 but we "
  125. "didn't offer it!",
  126. PROXY_ERROR_GENERAL, 0);
  127. return 1;
  128. }
  129. break;
  130. }
  131. p->chap_current_attribute = -1;
  132. p->chap_num_attributes_processed++;
  133. }
  134. if (p->state == 8 &&
  135. p->chap_num_attributes_processed >= p->chap_num_attributes) {
  136. p->chap_num_attributes = 0;
  137. p->chap_num_attributes_processed = 0;
  138. p->chap_current_datalen = 0;
  139. }
  140. }
  141. return 0;
  142. }
  143. int proxy_socks5_selectchap(ProxySocket *p)
  144. {
  145. char *username = conf_get_str(p->conf, CONF_proxy_username);
  146. char *password = conf_get_str(p->conf, CONF_proxy_password);
  147. if (username[0] || password[0]) {
  148. char chapbuf[514];
  149. int ulen;
  150. chapbuf[0] = '\x01'; /* Version */
  151. chapbuf[1] = '\x02'; /* Number of attributes sent */
  152. chapbuf[2] = '\x11'; /* First attribute - algorithms list */
  153. chapbuf[3] = '\x01'; /* Only one CHAP algorithm */
  154. chapbuf[4] = '\x85'; /* ...and it's HMAC-MD5, the core one */
  155. chapbuf[5] = '\x02'; /* Second attribute - username */
  156. ulen = strlen(username);
  157. if (ulen > 255) ulen = 255;
  158. if (ulen < 1) ulen = 1;
  159. chapbuf[6] = ulen;
  160. memcpy(chapbuf+7, username, ulen);
  161. sk_write(p->sub_socket, chapbuf, ulen + 7);
  162. p->chap_num_attributes = 0;
  163. p->chap_num_attributes_processed = 0;
  164. p->chap_current_attribute = -1;
  165. p->chap_current_datalen = 0;
  166. p->state = 8;
  167. } else
  168. plug_closing(p->plug, "Proxy error: Server chose "
  169. "CHAP authentication but we didn't offer it!",
  170. PROXY_ERROR_GENERAL, 0);
  171. return 1;
  172. }