comparison dropbearconvert.c @ 4:fe6bca95afa7

Makefile.in contains updated files required
author Matt Johnston <matt@ucc.asn.au>
date Tue, 01 Jun 2004 02:46:09 +0000
parents
children 223b0f5f8dce
comparison
equal deleted inserted replaced
-1:000000000000 4:fe6bca95afa7
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 "\n"
52 "The inputfile and output file can be '-' to specify\n"
53 "standard input or standard output.\n", progname);
54 }
55
56 #if defined(DBMULTI_CONVERT) || !defined(DROPBEAR_MULTI)
57 #if defined(DBMULTI_CONVERT) && defined(DROPBEAR_MULTI)
58 int dropbearconvert_main(int argc, char ** argv) {
59 #else
60 int main(int argc, char ** argv) {
61 #endif
62
63 int intype, outtype;
64 const char* infile;
65 const char* outfile;
66
67 /* get the commandline options */
68 if (argc != 5) {
69 fprintf(stderr, "All arguments must be specified\n");
70 goto usage;
71 }
72
73 /* input type */
74 if (argv[1][0] == 'd') {
75 intype = KEYFILE_DROPBEAR;
76 } else if (argv[1][0] == 'o') {
77 intype = KEYFILE_OPENSSH;
78 } else {
79 fprintf(stderr, "Invalid input key type\n");
80 goto usage;
81 }
82
83 /* output type */
84 if (argv[2][0] == 'd') {
85 outtype = KEYFILE_DROPBEAR;
86 } else if (argv[2][0] == 'o') {
87 outtype = KEYFILE_OPENSSH;
88 } else {
89 fprintf(stderr, "Invalid output key type\n");
90 goto usage;
91 }
92
93 /* we don't want output readable by others */
94 umask(077);
95
96 infile = argv[3];
97 outfile = argv[4];
98
99 return do_convert(intype, infile, outtype, outfile);
100
101 usage:
102 printhelp(argv[0]);
103 return 1;
104 }
105 #endif
106
107 static int do_convert(int intype, const char* infile, int outtype,
108 const char* outfile) {
109
110 sign_key * key = NULL;
111 char * keytype = NULL;
112 int ret = 1;
113
114 key = import_read(infile, NULL, intype);
115 if (!key) {
116 fprintf(stderr, "Error reading key from '%s'\n",
117 infile);
118 goto out;
119 }
120
121 #ifdef DROPBEAR_RSA
122 if (key->rsakey != NULL) {
123 keytype = "RSA";
124 }
125 #endif
126 #ifdef DROPBEAR_DSS
127 if (key->dsskey != NULL) {
128 keytype = "DSS";
129 }
130 #endif
131
132 fprintf(stderr, "Key is a %s key\n", keytype);
133
134 if (import_write(outfile, key, NULL, outtype) != 1) {
135 fprintf(stderr, "Error writing key to '%s'\n", outfile);
136 } else {
137 fprintf(stderr, "Wrote key to '%s'\n", outfile);
138 ret = 0;
139 }
140
141 out:
142 if (key) {
143 sign_key_free(key);
144 }
145 return ret;
146 }