2 * Dropbear - a SSH2 server
4 * Copyright (c) 2002,2003 Matt Johnston
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 /* This program converts to/from Dropbear and OpenSSH private-key formats */
30 #include "keyimport.h"
33 static int do_convert(int intype
, const char* infile
, int outtype
,
36 static void printhelp(char * progname
);
38 static void printhelp(char * progname
) {
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"
45 "The input and output types are one of:\n"
50 "dropbearconvert openssh dropbear /etc/ssh/ssh_host_rsa_key /etc/dropbear_rsa_host_key\n",
54 #if defined(DBMULTI_dropbearconvert) || !defined(DROPBEAR_MULTI)
55 #if defined(DBMULTI_dropbearconvert) && defined(DROPBEAR_MULTI)
56 int dropbearconvert_main(int argc
, char ** argv
) {
58 int main(int argc
, char ** argv
) {
66 /* It's hard for it to get in the way _too_ much */
70 /* get the commandline options */
72 fprintf(stderr
, "All arguments must be specified\n");
77 if (argv
[1][0] == 'd') {
78 intype
= KEYFILE_DROPBEAR
;
79 } else if (argv
[1][0] == 'o') {
80 intype
= KEYFILE_OPENSSH
;
82 fprintf(stderr
, "Invalid input key type\n");
87 if (argv
[2][0] == 'd') {
88 outtype
= KEYFILE_DROPBEAR
;
89 } else if (argv
[2][0] == 'o') {
90 outtype
= KEYFILE_OPENSSH
;
92 fprintf(stderr
, "Invalid output key type\n");
96 /* we don't want output readable by others */
102 return do_convert(intype
, infile
, outtype
, outfile
);
110 static int do_convert(int intype
, const char* infile
, int outtype
,
111 const char* outfile
) {
113 sign_key
* key
= NULL
;
114 char * keytype
= NULL
;
117 key
= import_read(infile
, NULL
, intype
);
119 fprintf(stderr
, "Error reading key from '%s'\n",
125 if (key
->rsakey
!= NULL
) {
130 if (key
->dsskey
!= NULL
) {
135 fprintf(stderr
, "Key is a %s key\n", keytype
);
137 if (import_write(outfile
, key
, NULL
, outtype
) != 1) {
138 fprintf(stderr
, "Error writing key to '%s'\n", outfile
);
140 fprintf(stderr
, "Wrote key to '%s'\n", outfile
);