2011-03-30 12:46:40 +00:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
#include <mp.h>
|
|
|
|
#include <libsec.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
2012-11-15 18:32:53 +00:00
|
|
|
fprint(2, "usage: tlsclient [-c lib/tls/clientcert] [-t /sys/lib/tls/xxx] [-x /sys/lib/tls/xxx.exclude] dialstring\n");
|
2011-03-30 12:46:40 +00:00
|
|
|
exits("usage");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xfer(int from, int to)
|
|
|
|
{
|
|
|
|
char buf[12*1024];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
while((n = read(from, buf, sizeof buf)) > 0)
|
|
|
|
if(write(to, buf, n) < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-15 18:32:53 +00:00
|
|
|
static int
|
|
|
|
reporter(char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprint(2, "%s: tls reports ", argv0);
|
|
|
|
vfprint(2, fmt, ap);
|
|
|
|
fprint(2, "\n");
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-30 12:46:40 +00:00
|
|
|
void
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2013-09-14 17:19:08 +00:00
|
|
|
int fd, debug;
|
2011-03-30 12:46:40 +00:00
|
|
|
uchar digest[20];
|
2012-11-15 18:32:53 +00:00
|
|
|
TLSconn *conn;
|
|
|
|
char *addr, *file, *filex, *ccert;
|
2011-03-30 12:46:40 +00:00
|
|
|
Thumbprint *thumb;
|
|
|
|
|
|
|
|
file = nil;
|
|
|
|
filex = nil;
|
|
|
|
thumb = nil;
|
2012-11-15 18:32:53 +00:00
|
|
|
ccert=nil;
|
|
|
|
debug=0;
|
2011-03-30 12:46:40 +00:00
|
|
|
ARGBEGIN{
|
|
|
|
case 't':
|
|
|
|
file = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
filex = EARGF(usage());
|
|
|
|
break;
|
2012-11-15 18:32:53 +00:00
|
|
|
case 'D':
|
|
|
|
debug++;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
ccert = EARGF(usage());
|
|
|
|
break;
|
2011-03-30 12:46:40 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND
|
|
|
|
|
|
|
|
if(argc != 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if(filex && !file)
|
|
|
|
sysfatal("specifying -x without -t is useless");
|
|
|
|
if(file){
|
|
|
|
thumb = initThumbprints(file, filex);
|
|
|
|
if(thumb == nil)
|
|
|
|
sysfatal("initThumbprints: %r");
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = argv[0];
|
2013-09-14 17:19:08 +00:00
|
|
|
if((fd = dial(addr, 0, 0, 0)) < 0)
|
2011-03-30 12:46:40 +00:00
|
|
|
sysfatal("dial %s: %r", addr);
|
|
|
|
|
2012-11-15 18:32:53 +00:00
|
|
|
conn = (TLSconn*)mallocz(sizeof *conn, 1);
|
|
|
|
if(ccert)
|
|
|
|
conn->cert = readcert(ccert, &conn->certlen);
|
|
|
|
if(debug)
|
|
|
|
conn->trace = reporter;
|
2013-09-14 17:19:08 +00:00
|
|
|
fd = tlsClient(fd, conn);
|
2011-03-30 12:46:40 +00:00
|
|
|
if(fd < 0)
|
|
|
|
sysfatal("tlsclient: %r");
|
|
|
|
if(thumb){
|
2012-11-15 18:32:53 +00:00
|
|
|
if(conn->cert==nil || conn->certlen<=0)
|
2011-03-30 12:46:40 +00:00
|
|
|
sysfatal("server did not provide TLS certificate");
|
2012-11-15 18:32:53 +00:00
|
|
|
sha1(conn->cert, conn->certlen, digest, nil);
|
2011-03-30 12:46:40 +00:00
|
|
|
if(!okThumbprint(digest, thumb)){
|
|
|
|
fmtinstall('H', encodefmt);
|
|
|
|
sysfatal("server certificate %.*H not recognized", SHA1dlen, digest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rfork(RFNOTEG);
|
|
|
|
switch(fork()){
|
|
|
|
case -1:
|
|
|
|
fprint(2, "%s: fork: %r\n", argv0);
|
|
|
|
exits("dial");
|
|
|
|
case 0:
|
|
|
|
xfer(0, fd);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
xfer(fd, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
postnote(PNGROUP, getpid(), "die yankee pig dog");
|
|
|
|
exits(0);
|
|
|
|
}
|