cproxy.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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();
  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. p->cfg.proxy_password, &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(Proxy_Socket p)
  144. {
  145. if (p->cfg.proxy_username[0] || p->cfg.proxy_password[0]) {
  146. char chapbuf[514];
  147. int ulen;
  148. chapbuf[0] = '\x01'; /* Version */
  149. chapbuf[1] = '\x02'; /* Number of attributes sent */
  150. chapbuf[2] = '\x11'; /* First attribute - algorithms list */
  151. chapbuf[3] = '\x01'; /* Only one CHAP algorithm */
  152. chapbuf[4] = '\x85'; /* ...and it's HMAC-MD5, the core one */
  153. chapbuf[5] = '\x02'; /* Second attribute - username */
  154. ulen = strlen(p->cfg.proxy_username);
  155. if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;
  156. chapbuf[6] = ulen;
  157. memcpy(chapbuf+7, p->cfg.proxy_username, ulen);
  158. sk_write(p->sub_socket, chapbuf, ulen + 7);
  159. p->chap_num_attributes = 0;
  160. p->chap_num_attributes_processed = 0;
  161. p->chap_current_attribute = -1;
  162. p->chap_current_datalen = 0;
  163. p->state = 8;
  164. } else
  165. plug_closing(p->plug, "Proxy error: Server chose "
  166. "CHAP authentication but we didn't offer it!",
  167. PROXY_ERROR_GENERAL, 0);
  168. return 1;
  169. }