x11_make_greeting.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Construct an X11 greeting packet, including making up the right
  3. * authorisation data.
  4. */
  5. #include "putty.h"
  6. #include "ssh.h"
  7. void *x11_make_greeting(int endian, int protomajor, int protominor,
  8. int auth_proto, const void *auth_data, int auth_len,
  9. const char *peer_addr, int peer_port,
  10. int *outlen)
  11. {
  12. unsigned char *greeting;
  13. unsigned char realauthdata[64];
  14. const char *authname;
  15. const unsigned char *authdata;
  16. int authnamelen, authnamelen_pad;
  17. int authdatalen, authdatalen_pad;
  18. int greeting_len;
  19. authname = x11_authnames[auth_proto];
  20. authnamelen = strlen(authname);
  21. authnamelen_pad = (authnamelen + 3) & ~3;
  22. if (auth_proto == X11_MIT) {
  23. authdata = auth_data;
  24. authdatalen = auth_len;
  25. } else if (auth_proto == X11_XDM && auth_len == 16) {
  26. time_t t;
  27. unsigned long peer_ip = 0;
  28. x11_parse_ip(peer_addr, &peer_ip);
  29. authdata = realauthdata;
  30. authdatalen = 24;
  31. memset(realauthdata, 0, authdatalen);
  32. memcpy(realauthdata, auth_data, 8);
  33. PUT_32BIT_MSB_FIRST(realauthdata+8, peer_ip);
  34. PUT_16BIT_MSB_FIRST(realauthdata+12, peer_port);
  35. t = time(NULL);
  36. PUT_32BIT_MSB_FIRST(realauthdata+14, t);
  37. des_encrypt_xdmauth((char *)auth_data + 9, realauthdata, authdatalen);
  38. } else {
  39. authdata = realauthdata;
  40. authdatalen = 0;
  41. }
  42. authdatalen_pad = (authdatalen + 3) & ~3;
  43. greeting_len = 12 + authnamelen_pad + authdatalen_pad;
  44. greeting = snewn(greeting_len, unsigned char);
  45. memset(greeting, 0, greeting_len);
  46. greeting[0] = endian;
  47. PUT_16BIT_X11(endian, greeting+2, protomajor);
  48. PUT_16BIT_X11(endian, greeting+4, protominor);
  49. PUT_16BIT_X11(endian, greeting+6, authnamelen);
  50. PUT_16BIT_X11(endian, greeting+8, authdatalen);
  51. memcpy(greeting+12, authname, authnamelen);
  52. memcpy(greeting+12+authnamelen_pad, authdata, authdatalen);
  53. smemclr(realauthdata, sizeof(realauthdata));
  54. *outlen = greeting_len;
  55. return greeting;
  56. }