plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

asn12dsa.c (1147B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <bio.h>
      4 #include <mp.h>
      5 #include <libsec.h>
      6 
      7 void
      8 usage(void)
      9 {
     10 	fprint(2, "auth/asn12dsa [-t tag] [file]\n");
     11 	exits("usage");
     12 }
     13 
     14 void
     15 main(int argc, char **argv)
     16 {
     17 	char *s;
     18 	uchar *buf;
     19 	int fd;
     20 	long n, tot;
     21 	char *tag, *file;
     22 	DSApriv *key;
     23 
     24 	fmtinstall('B', mpfmt);
     25 
     26 	tag = nil;
     27 	ARGBEGIN{
     28 	case 't':
     29 		tag = EARGF(usage());
     30 		break;
     31 	default:
     32 		usage();
     33 	}ARGEND
     34 
     35 	if(argc != 0 && argc != 1)
     36 		usage();
     37 
     38 	if(argc == 1)
     39 		file = argv[0];
     40 	else
     41 		file = "/dev/stdin";
     42 
     43 	if((fd = open(file, OREAD)) < 0)
     44 		sysfatal("open %s: %r", file);
     45 	buf = nil;
     46 	tot = 0;
     47 	for(;;){
     48 		buf = realloc(buf, tot+8192);
     49 		if(buf == nil)
     50 			sysfatal("realloc: %r");
     51 		if((n = read(fd, buf+tot, 8192)) < 0)
     52 			sysfatal("read: %r");
     53 		if(n == 0)
     54 			break;
     55 		tot += n;
     56 	}
     57 
     58 	key = asn1toDSApriv(buf, tot);
     59 	if(key == nil)
     60 		sysfatal("couldn't parse asn1 key");
     61 
     62 	s = smprint("key proto=dsa %s%sp=%lB q=%lB alpha=%lB key=%lB !secret=%lB\n",
     63 		tag ? tag : "", tag ? " " : "",
     64 		key->pub.p, key->pub.q, key->pub.alpha, key->pub.key,
     65 		key->secret);
     66 	if(s == nil)
     67 		sysfatal("smprint: %r");
     68 	write(1, s, strlen(s));
     69 	exits(0);
     70 }