http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / userapps / opensource / sshd / dropbearconvert.c
1 /*
2  * Dropbear - a SSH2 server
3  * 
4  * Copyright (c) 2002,2003 Matt Johnston
5  * All rights reserved.
6  * 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  * 
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  * 
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE. */
24
25 /* This program converts to/from Dropbear and OpenSSH private-key formats */
26 #include "includes.h"
27 #include "signkey.h"
28 #include "buffer.h"
29 #include "dbutil.h"
30 #include "keyimport.h"
31
32
33 static int do_convert(int intype, const char* infile, int outtype,
34                 const char* outfile);
35
36 static void printhelp(char * progname);
37
38 static void printhelp(char * progname) {
39
40         fprintf(stderr, "Usage: %s <inputtype> <outputtype> <inputfile> <outputfile>\n\n"
41                                         "CAUTION: This program is for convenience only, and is not secure if used on\n"
42                                         "untrusted input files, ie it could allow arbitrary code execution.\n"
43                                         "All parameters must be specified in order.\n"
44                                         "\n"
45                                         "The input and output types are one of:\n"
46                                         "openssh\n"
47                                         "dropbear\n"
48                                         "\n"
49                                         "Example:\n"
50                                         "dropbearconvert openssh dropbear /etc/ssh/ssh_host_rsa_key /etc/dropbear_rsa_host_key\n",
51                                         progname);
52 }
53
54 #if defined(DBMULTI_dropbearconvert) || !defined(DROPBEAR_MULTI)
55 #if defined(DBMULTI_dropbearconvert) && defined(DROPBEAR_MULTI)
56 int dropbearconvert_main(int argc, char ** argv) {
57 #else 
58 int main(int argc, char ** argv) {
59 #endif
60
61         int intype, outtype;
62         const char* infile;
63         const char* outfile;
64
65 #ifdef DEBUG_TRACE
66         /* It's hard for it to get in the way _too_ much */
67         debug_trace = 1;
68 #endif
69
70         /* get the commandline options */
71         if (argc != 5) {
72                 fprintf(stderr, "All arguments must be specified\n");
73                 goto usage;
74         }
75
76         /* input type */
77         if (argv[1][0] == 'd') {
78                 intype = KEYFILE_DROPBEAR;
79         } else if (argv[1][0] == 'o') {
80                 intype = KEYFILE_OPENSSH;
81         } else {
82                 fprintf(stderr, "Invalid input key type\n");
83                 goto usage;
84         }
85
86         /* output type */
87         if (argv[2][0] == 'd') {
88                 outtype = KEYFILE_DROPBEAR;
89         } else if (argv[2][0] == 'o') {
90                 outtype = KEYFILE_OPENSSH;
91         } else {
92                 fprintf(stderr, "Invalid output key type\n");
93                 goto usage;
94         }
95
96         /* we don't want output readable by others */
97         umask(077);
98
99         infile = argv[3];
100         outfile = argv[4];
101
102         return do_convert(intype, infile, outtype, outfile);
103
104 usage:
105         printhelp(argv[0]);
106         return 1;
107 }
108 #endif
109
110 static int do_convert(int intype, const char* infile, int outtype,
111                 const char* outfile) {
112
113         sign_key * key = NULL;
114         char * keytype = NULL;
115         int ret = 1;
116
117         key = import_read(infile, NULL, intype);
118         if (!key) {
119                 fprintf(stderr, "Error reading key from '%s'\n",
120                                 infile);
121                 goto out;
122         }
123
124 #ifdef DROPBEAR_RSA
125         if (key->rsakey != NULL) {
126                 keytype = "RSA";
127         }
128 #endif
129 #ifdef DROPBEAR_DSS
130         if (key->dsskey != NULL) {
131                 keytype = "DSS";
132         }
133 #endif
134
135         fprintf(stderr, "Key is a %s key\n", keytype);
136
137         if (import_write(outfile, key, NULL, outtype) != 1) {
138                 fprintf(stderr, "Error writing key to '%s'\n", outfile);
139         } else {
140                 fprintf(stderr, "Wrote key to '%s'\n", outfile);
141                 ret = 0;
142         }
143
144 out:
145         if (key) {
146                 sign_key_free(key);
147         }
148         return ret;
149 }