]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/auth/asn12rsa.c
show line numbers in dtracy type errors
[plan9front.git] / sys / src / cmd / auth / asn12rsa.c
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         RSApub *pub;
24
25         fmtinstall('B', mpfmt);
26
27         tag = nil;
28         ARGBEGIN{
29         case 't':
30                 tag = EARGF(usage());
31                 break;
32         default:
33                 usage();
34         }ARGEND
35
36         if(argc != 0 && argc != 1)
37                 usage();
38
39         if(argc == 1)
40                 file = argv[0];
41         else
42                 file = "#d/0";
43
44         if((fd = open(file, OREAD)) < 0)
45                 sysfatal("open %s: %r", file);
46         buf = nil;
47         tot = 0;
48         for(;;){
49                 buf = realloc(buf, tot+8192);
50                 if(buf == nil)
51                         sysfatal("realloc: %r");
52                 if((n = read(fd, buf+tot, 8192)) < 0)
53                         sysfatal("read: %r");
54                 if(n == 0)
55                         break;
56                 tot += n;
57         }
58         key = asn1toRSApriv(buf, tot);
59         if(key != nil){
60                 s = smprint("key proto=rsa %s%ssize=%d ek=%B !dk=%B n=%B !p=%B !q=%B !kp=%B !kq=%B !c2=%B\n",
61                         tag ? tag : "", tag ? " " : "",
62                         mpsignif(key->pub.n), key->pub.ek,
63                         key->dk, key->pub.n, key->p, key->q,
64                         key->kp, key->kq, key->c2);
65         } else {
66                 pub = asn1toRSApub(buf, tot);
67                 if(pub == nil)
68                         sysfatal("couldn't parse asn1 key");
69                 s = smprint("key proto=rsa %s%ssize=%d ek=%B n=%B\n",
70                         tag ? tag : "", tag ? " " : "",
71                         mpsignif(pub->n), pub->ek, pub->n);
72         }
73         if(s == nil)
74                 sysfatal("smprint: %r");
75         write(1, s, strlen(s));
76         exits(0);
77 }