cproxy.c 6.0 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. const bool socks5_chap_available = true;
  16. const bool http_digest_available = true;
  17. strbuf *chap_response(ptrlen challenge, ptrlen password)
  18. {
  19. strbuf *sb = strbuf_new_nm();
  20. const ssh2_macalg *alg = &ssh_hmac_md5;
  21. enable_dit(); /* just in case main() forgot */
  22. mac_simple(alg, password, challenge, strbuf_append(sb, alg->len));
  23. return sb;
  24. }
  25. static void BinarySink_put_hex_data(BinarySink *bs, const void *vptr,
  26. size_t len)
  27. {
  28. const unsigned char *p = (const unsigned char *)vptr;
  29. const char *hexdigits = "0123456789abcdef";
  30. while (len-- > 0) {
  31. unsigned c = *p++;
  32. put_byte(bs, hexdigits[0xF & (c >> 4)]);
  33. put_byte(bs, hexdigits[0xF & (c )]);
  34. }
  35. }
  36. #define put_hex_data(bs, p, len) \
  37. BinarySink_put_hex_data(BinarySink_UPCAST(bs), p, len)
  38. const char *const httphashnames[] = {
  39. #define DECL_ARRAY(id, str, alg, bits, accepted) str,
  40. HTTP_DIGEST_HASHES(DECL_ARRAY)
  41. #undef DECL_ARRAY
  42. };
  43. const bool httphashaccepted[] = {
  44. #define DECL_ARRAY(id, str, alg, bits, accepted) accepted,
  45. HTTP_DIGEST_HASHES(DECL_ARRAY)
  46. #undef DECL_ARRAY
  47. };
  48. static const ssh_hashalg *const httphashalgs[] = {
  49. #define DECL_ARRAY(id, str, alg, bits, accepted) alg,
  50. HTTP_DIGEST_HASHES(DECL_ARRAY)
  51. #undef DECL_ARRAY
  52. };
  53. static const size_t httphashlengths[] = {
  54. #define DECL_ARRAY(id, str, alg, bits, accepted) bits/8,
  55. HTTP_DIGEST_HASHES(DECL_ARRAY)
  56. #undef DECL_ARRAY
  57. };
  58. void http_digest_response(BinarySink *bs, ptrlen username, ptrlen password,
  59. ptrlen realm, ptrlen method, ptrlen uri, ptrlen qop,
  60. ptrlen nonce, ptrlen opaque, uint32_t nonce_count,
  61. HttpDigestHash hash, bool hash_username)
  62. {
  63. unsigned char a1hash[MAX_HASH_LEN];
  64. unsigned char a2hash[MAX_HASH_LEN];
  65. unsigned char rsphash[MAX_HASH_LEN];
  66. const ssh_hashalg *alg = httphashalgs[hash];
  67. size_t hashlen = httphashlengths[hash];
  68. enable_dit(); /* just in case main() forgot */
  69. unsigned char ncbuf[4];
  70. PUT_32BIT_MSB_FIRST(ncbuf, nonce_count);
  71. unsigned char client_nonce_raw[33];
  72. random_read(client_nonce_raw, lenof(client_nonce_raw));
  73. char client_nonce_base64[lenof(client_nonce_raw) / 3 * 4];
  74. for (unsigned i = 0; i < lenof(client_nonce_raw)/3; i++)
  75. base64_encode_atom(client_nonce_raw + 3*i, 3,
  76. client_nonce_base64 + 4*i);
  77. /*
  78. * RFC 7616 section 3.4.2: the hash "A1" is a hash of
  79. * username:realm:password (in the absence of hash names like
  80. * "MD5-sess" which as far as I know don't sensibly apply to
  81. * proxies and HTTP CONNECT).
  82. */
  83. ssh_hash *h = ssh_hash_new(alg);
  84. put_datapl(h, username);
  85. put_byte(h, ':');
  86. put_datapl(h, realm);
  87. put_byte(h, ':');
  88. put_datapl(h, password);
  89. ssh_hash_digest_nondestructive(h, a1hash);
  90. /*
  91. * RFC 7616 section 3.4.3: the hash "A2" is a hash of method:uri
  92. * (in the absence of more interesting quality-of-protection
  93. * schemes than plain "auth" - e.g. "auth-int" hashes the entire
  94. * document as well - which again I don't think make sense in the
  95. * context of proxies and CONNECT).
  96. */
  97. ssh_hash_reset(h);
  98. put_datapl(h, method);
  99. put_byte(h, ':');
  100. put_datapl(h, uri);
  101. ssh_hash_digest_nondestructive(h, a2hash);
  102. /*
  103. * RFC 7616 section 3.4.1: the overall output hash in the
  104. * "response" parameter of the authorization header is a hash of
  105. * A1:nonce:nonce-count:client-nonce:qop:A2, where A1 and A2 are
  106. * the hashes computed above.
  107. */
  108. ssh_hash_reset(h);
  109. put_hex_data(h, a1hash, hashlen);
  110. put_byte(h, ':');
  111. put_datapl(h, nonce);
  112. put_byte(h, ':');
  113. put_hex_data(h, ncbuf, 4);
  114. put_byte(h, ':');
  115. put_data(h, client_nonce_base64, lenof(client_nonce_base64));
  116. put_byte(h, ':');
  117. put_datapl(h, qop);
  118. put_byte(h, ':');
  119. put_hex_data(h, a2hash, hashlen);
  120. ssh_hash_final(h, rsphash);
  121. /*
  122. * Now construct the output header (everything after the initial
  123. * "Proxy-Authorization: Digest ") and write it to the provided
  124. * BinarySink.
  125. */
  126. put_datalit(bs, "username=\"");
  127. if (hash_username) {
  128. /*
  129. * RFC 7616 section 3.4.4: if we're hashing the username, we
  130. * actually hash username:realm (like a truncated version of
  131. * A1 above).
  132. */
  133. ssh_hash *h = ssh_hash_new(alg);
  134. put_datapl(h, username);
  135. put_byte(h, ':');
  136. put_datapl(h, realm);
  137. ssh_hash_final(h, a1hash);
  138. put_hex_data(bs, a1hash, hashlen);
  139. } else {
  140. put_datapl(bs, username);
  141. }
  142. put_datalit(bs, "\", realm=\"");
  143. put_datapl(bs, realm);
  144. put_datalit(bs, "\", uri=\"");
  145. put_datapl(bs, uri);
  146. put_datalit(bs, "\", algorithm=");
  147. put_dataz(bs, httphashnames[hash]);
  148. put_datalit(bs, ", nonce=\"");
  149. put_datapl(bs, nonce);
  150. put_datalit(bs, "\", nc=");
  151. put_hex_data(bs, ncbuf, 4);
  152. put_datalit(bs, ", cnonce=\"");
  153. put_data(bs, client_nonce_base64, lenof(client_nonce_base64));
  154. put_datalit(bs, "\", qop=");
  155. put_datapl(bs, qop);
  156. put_datalit(bs, ", response=\"");
  157. put_hex_data(bs, rsphash, hashlen);
  158. put_datalit(bs, "\"");
  159. if (opaque.ptr) {
  160. put_datalit(bs, ", opaque=\"");
  161. put_datapl(bs, opaque);
  162. put_datalit(bs, "\"");
  163. }
  164. if (hash_username) {
  165. put_datalit(bs, ", userhash=true");
  166. }
  167. smemclr(a1hash, lenof(a1hash));
  168. smemclr(a2hash, lenof(a2hash));
  169. smemclr(rsphash, lenof(rsphash));
  170. smemclr(client_nonce_raw, lenof(client_nonce_raw));
  171. smemclr(client_nonce_base64, lenof(client_nonce_base64));
  172. }