cproxy.c 5.4 KB

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