xref: /plan9/sys/src/cmd/ssh2/rsa2ssh2.c (revision 63afb9a5d3f910047231762bcce0ee49fed3d07c)
1 #include <u.h>
2 #include <libc.h>
3 #include <mp.h>
4 #include <fcall.h>
5 #include <thread.h>
6 #include <9p.h>
7 #include <auth.h>
8 #include <authsrv.h>
9 #include <libsec.h>
10 #include "netssh.h"
11 
12 Cipher *cryptos[1];
13 int debug;
14 
15 void
usage(void)16 usage(void)
17 {
18 	fprint(2, "usage: %s [file]\n", argv0);
19 	exits("usage");
20 }
21 
22 void
main(int argc,char * argv[])23 main(int argc, char *argv[])
24 {
25 	Packet *p;
26 	char *ep, *np, *user;
27 	mpint *e, *n;
28 	int fd, m;
29 	char key[Maxrpcbuf], encpub[Maxrpcbuf];
30 
31 	ARGBEGIN {
32 	default:
33 		usage();
34 	} ARGEND
35 	if (argc > 1)
36 		usage();
37 
38 	user = getenv("user");
39 	if (argc == 0)
40 		fd = 0;
41 	else {
42 		fd = open(argv[0], OREAD);
43 		if (fd < 0)
44 			usage();
45 	}
46 	m = read(fd, key, Maxrpcbuf - 1);
47 	close(fd);
48 	key[m >= 0? m: 0] = 0;
49 
50 	ep = strstr(key, " ek=");
51 	np = strstr(key, " n=");
52 	if (ep == nil || np == nil)
53 		sysfatal("bad key file\n");
54 	e = strtomp(ep+4, nil, 16, nil);
55 	n = strtomp(np+3, nil, 16, nil);
56 	p = new_packet(nil);
57 	add_string(p, "ssh-rsa");
58 	add_mp(p, e);
59 	add_mp(p, n);
60 	if ((m = enc64(encpub, Maxrpcbuf, p->payload, p->rlength-1)) < 0)
61 		sysfatal("base-64 encoding failed\n");
62 	print("ssh-rsa ");
63 	write(1, encpub, m);
64 	if (user)
65 		print(" %s\n", user);
66 }
67