check in keyconvert tool
[sfa.git] / keyconvert / keyconvertmain.c
1 #include <stdio.h>
2 #include "keyconvert.h"
3 #include "b64decode.h"
4
5 int main(int argc, char **argv)
6 {
7     FILE *fin, *fout;
8     char inbytes[16384], *inptr;
9     char decodedKey[16384];
10     int len;
11
12     b64decodeinit();
13
14     if (argc != 3) {
15         fprintf(stderr, "syntax: keyconvert <infile> <outfile>\n");
16         exit(1);
17     }
18
19     fin = fopen(argv[1], "rt");
20     if (fin == NULL) {
21         fprintf(stderr, "failed to open %s\n", argv[1]);
22         exit(1);
23     }
24
25     memset(inbytes, 0, sizeof(inbytes));
26     len = fread(inbytes, 1, sizeof(inbytes), fin);
27     fclose(fin);
28
29  //   fprintf(stdout, "read %d bytes from openssh file\n", len);
30
31     inptr = inbytes;
32
33     // skip leading space
34     while (isspace(*inptr)) inptr++;
35
36     // skip the ssh-rsa or ssh-dsa part
37     while (*inptr && !isspace(*inptr)) inptr++;
38
39     // skip spaces between ssh-rsa/ssh-dsa and key
40     while (isspace(*inptr)) inptr++;
41
42     // if there is any part after the key, terminate it
43     if (strchr(inptr, ' ') != NULL) {
44         *strchr(inptr, ' ') = '\0';
45     }
46
47     // at this point, inptr contains the b64 encoded openssh key
48
49     len = b64decode(inptr, decodedKey);
50
51 //    fprintf(stdout, "decoded openssh file length is %d\n", len);
52
53     fout = fopen(argv[2], "wt");
54     if (fout == NULL) {
55         fprintf(stderr, "failed to open output file %s\n", argv[2]);
56         exit(1);
57     }
58
59     openssh_binary_to_openssl(decodedKey, len, fout);
60
61     fclose(fout);
62
63     fprintf(stdout, "completed\n");
64     return 0;
65 }