dropbear 2016.73
[tomato.git] / release / src / router / dropbear / dropbearconvert.c
blob37ee7acae84608b7a131030828e4807618184356
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:
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
23 * SOFTWARE. */
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 #include "crypto_desc.h"
32 #include "dbrandom.h"
35 static int do_convert(int intype, const char* infile, int outtype,
36 const char* outfile);
38 static void printhelp(char * progname);
40 static void printhelp(char * progname) {
42 fprintf(stderr, "Usage: %s <inputtype> <outputtype> <inputfile> <outputfile>\n\n"
43 "CAUTION: This program is for convenience only, and is not secure if used on\n"
44 "untrusted input files, ie it could allow arbitrary code execution.\n"
45 "All parameters must be specified in order.\n"
46 "\n"
47 "The input and output types are one of:\n"
48 "openssh\n"
49 "dropbear\n"
50 "\n"
51 "Example:\n"
52 "dropbearconvert openssh dropbear /etc/ssh/ssh_host_rsa_key /etc/dropbear_rsa_host_key\n",
53 progname);
56 #if defined(DBMULTI_dropbearconvert) || !defined(DROPBEAR_MULTI)
57 #if defined(DBMULTI_dropbearconvert) && defined(DROPBEAR_MULTI)
58 int dropbearconvert_main(int argc, char ** argv) {
59 #else
60 int main(int argc, char ** argv) {
61 #endif
63 int intype, outtype;
64 const char* infile;
65 const char* outfile;
67 crypto_init();
68 seedrandom();
70 #ifdef DEBUG_TRACE
71 /* It's hard for it to get in the way _too_ much */
72 debug_trace = 1;
73 #endif
75 /* get the commandline options */
76 if (argc != 5) {
77 fprintf(stderr, "All arguments must be specified\n");
78 goto usage;
81 /* input type */
82 if (argv[1][0] == 'd') {
83 intype = KEYFILE_DROPBEAR;
84 } else if (argv[1][0] == 'o') {
85 intype = KEYFILE_OPENSSH;
86 } else {
87 fprintf(stderr, "Invalid input key type\n");
88 goto usage;
91 /* output type */
92 if (argv[2][0] == 'd') {
93 outtype = KEYFILE_DROPBEAR;
94 } else if (argv[2][0] == 'o') {
95 outtype = KEYFILE_OPENSSH;
96 } else {
97 fprintf(stderr, "Invalid output key type\n");
98 goto usage;
101 /* we don't want output readable by others */
102 umask(077);
104 infile = argv[3];
105 outfile = argv[4];
107 return do_convert(intype, infile, outtype, outfile);
109 usage:
110 printhelp(argv[0]);
111 return 1;
113 #endif
115 static int do_convert(int intype, const char* infile, int outtype,
116 const char* outfile) {
118 sign_key * key = NULL;
119 const char * keytype = NULL;
120 int ret = 1;
122 key = import_read(infile, NULL, intype);
123 if (!key) {
124 fprintf(stderr, "Error reading key from '%s'\n",
125 infile);
126 goto out;
129 keytype = signkey_name_from_type(key->type, NULL);
131 fprintf(stderr, "Key is a %s key\n", keytype);
133 if (import_write(outfile, key, NULL, outtype) != 1) {
134 fprintf(stderr, "Error writing key to '%s'\n", outfile);
135 } else {
136 fprintf(stderr, "Wrote key to '%s'\n", outfile);
137 ret = 0;
140 out:
141 if (key) {
142 sign_key_free(key);
144 return ret;