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