]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/auth/rsa2asn1.c
libaml: fix gc bug, need to amltake()/amldrop() temporary buffer
[plan9front.git] / sys / src / cmd / auth / rsa2asn1.c
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include <mp.h>
5 #include <libsec.h>
6 #include "rsa2any.h"
7
8 int privatekey = 0;
9
10 void
11 usage(void)
12 {
13         fprint(2, "usage: auth/rsa2asn1 [-a] [file]\n");
14         exits("usage");
15 }
16
17 void
18 main(int argc, char **argv)
19 {
20         uchar buf[16*1024];
21         RSApriv *k;
22         int n;
23
24         ARGBEGIN{
25         case 'a':
26                 privatekey = 1;
27                 break;
28         default:
29                 usage();
30         }ARGEND
31
32         if(argc > 1)
33                 usage();
34
35         if((k = getrsakey(argc, argv, privatekey, nil)) == nil)
36                 sysfatal("%r");
37         if(privatekey){
38                 if((n = asn1encodeRSApriv(k, buf, sizeof(buf))) < 0)
39                         sysfatal("asn1encodeRSApriv: %r");
40         }else{
41                 if((n = asn1encodeRSApub(&k->pub, buf, sizeof(buf))) < 0)
42                         sysfatal("asn1encodeRSApub: %r");
43         }
44         if(write(1, buf, n) != n)
45                 sysfatal("write: %r");
46         exits(nil);
47 }