tlsclient.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <mp.h>
  12. #include <libsec.h>
  13. void
  14. usage(void)
  15. {
  16. fprint(2, "usage: tlsclient [-t /sys/lib/tls/xxx] [-x /sys/lib/tls/xxx.exclude] dialstring\n");
  17. exits("usage");
  18. }
  19. void
  20. xfer(int from, int to)
  21. {
  22. char buf[12*1024];
  23. int n;
  24. while((n = read(from, buf, sizeof buf)) > 0)
  25. if(write(to, buf, n) < 0)
  26. break;
  27. }
  28. void
  29. main(int argc, char **argv)
  30. {
  31. int fd, netfd;
  32. unsigned char digest[20];
  33. TLSconn conn;
  34. char *addr, *file, *filex;
  35. Thumbprint *thumb;
  36. file = nil;
  37. filex = nil;
  38. thumb = nil;
  39. ARGBEGIN{
  40. case 't':
  41. file = EARGF(usage());
  42. break;
  43. case 'x':
  44. filex = EARGF(usage());
  45. break;
  46. default:
  47. usage();
  48. }ARGEND
  49. if(argc != 1)
  50. usage();
  51. if(filex && !file)
  52. sysfatal("specifying -x without -t is useless");
  53. if(file){
  54. thumb = initThumbprints(file, filex);
  55. if(thumb == nil)
  56. sysfatal("initThumbprints: %r");
  57. }
  58. addr = argv[0];
  59. if((netfd = dial(addr, 0, 0, 0)) < 0)
  60. sysfatal("dial %s: %r", addr);
  61. memset(&conn, 0, sizeof conn);
  62. fd = tlsClient(netfd, &conn);
  63. if(fd < 0)
  64. sysfatal("tlsclient: %r");
  65. if(thumb){
  66. if(conn.cert==nil || conn.certlen<=0)
  67. sysfatal("server did not provide TLS certificate");
  68. sha1(conn.cert, conn.certlen, digest, nil);
  69. if(!okThumbprint(digest, thumb)){
  70. fmtinstall('H', encodefmt);
  71. sysfatal("server certificate %.*H not recognized", SHA1dlen, digest);
  72. }
  73. }
  74. free(conn.cert);
  75. close(netfd);
  76. rfork(RFNOTEG);
  77. switch(fork()){
  78. case -1:
  79. fprint(2, "%s: fork: %r\n", argv0);
  80. exits("dial");
  81. case 0:
  82. xfer(0, fd);
  83. break;
  84. default:
  85. xfer(fd, 1);
  86. break;
  87. }
  88. postnote(PNGROUP, getpid(), "die yankee pig dog");
  89. exits(0);
  90. }