cproxy.c 6.3 KB

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