plan9port

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

asn12rsa.c (1206B)


      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/asn12rsa [-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 	RSApriv *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 = asn1toRSApriv(buf, tot);
     59 	if(key == nil)
     60 		sysfatal("couldn't parse asn1 key");
     61 
     62 	s = smprint("key proto=rsa %s%ssize=%d ek=%lB !dk=%lB n=%lB !p=%lB !q=%lB !kp=%lB !kq=%lB !c2=%lB\n",
     63 		tag ? tag : "", tag ? " " : "",
     64 		mpsignif(key->pub.n), key->pub.ek,
     65 		key->dk, key->pub.n, key->p, key->q,
     66 		key->kp, key->kq, key->c2);
     67 	if(s == nil)
     68 		sysfatal("smprint: %r");
     69 	write(1, s, strlen(s));
     70 	exits(0);
     71 }