dropbearconvert.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /* This program converts to/from Dropbear and OpenSSH private-key formats */
  25. #include "includes.h"
  26. #include "signkey.h"
  27. #include "buffer.h"
  28. #include "dbutil.h"
  29. #include "keyimport.h"
  30. #include "crypto_desc.h"
  31. #include "dbrandom.h"
  32. static int do_convert(int intype, const char* infile, int outtype,
  33. const char* outfile);
  34. static void printhelp(char * progname);
  35. static void printhelp(char * progname) {
  36. fprintf(stderr, "Usage: %s <inputtype> <outputtype> <inputfile> <outputfile>\n\n"
  37. "CAUTION: This program is for convenience only, and is not secure if used on\n"
  38. "untrusted input files, ie it could allow arbitrary code execution.\n"
  39. "All parameters must be specified in order.\n"
  40. "\n"
  41. "The input and output types are one of:\n"
  42. "openssh\n"
  43. "dropbear\n"
  44. "\n"
  45. "Example:\n"
  46. "dropbearconvert openssh dropbear /etc/ssh/ssh_host_rsa_key /etc/dropbear_rsa_host_key\n",
  47. progname);
  48. }
  49. #if defined(DBMULTI_dropbearconvert) || !DROPBEAR_MULTI
  50. #if defined(DBMULTI_dropbearconvert) && DROPBEAR_MULTI
  51. int dropbearconvert_main(int argc, char ** argv) {
  52. #else
  53. int main(int argc, char ** argv) {
  54. #endif
  55. int intype, outtype;
  56. const char* infile;
  57. const char* outfile;
  58. crypto_init();
  59. seedrandom();
  60. #if DEBUG_TRACE
  61. /* It's hard for it to get in the way _too_ much */
  62. debug_trace = DROPBEAR_VERBOSE_LEVEL;
  63. #endif
  64. /* get the commandline options */
  65. if (argc != 5) {
  66. fprintf(stderr, "All arguments must be specified\n");
  67. goto usage;
  68. }
  69. /* input type */
  70. if (argv[1][0] == 'd') {
  71. intype = KEYFILE_DROPBEAR;
  72. } else if (argv[1][0] == 'o') {
  73. intype = KEYFILE_OPENSSH;
  74. } else {
  75. fprintf(stderr, "Invalid input key type\n");
  76. goto usage;
  77. }
  78. /* output type */
  79. if (argv[2][0] == 'd') {
  80. outtype = KEYFILE_DROPBEAR;
  81. } else if (argv[2][0] == 'o') {
  82. outtype = KEYFILE_OPENSSH;
  83. } else {
  84. fprintf(stderr, "Invalid output key type\n");
  85. goto usage;
  86. }
  87. /* we don't want output readable by others */
  88. umask(077);
  89. infile = argv[3];
  90. outfile = argv[4];
  91. return do_convert(intype, infile, outtype, outfile);
  92. usage:
  93. printhelp(argv[0]);
  94. return 1;
  95. }
  96. #endif
  97. static int do_convert(int intype, const char* infile, int outtype,
  98. const char* outfile) {
  99. sign_key * key = NULL;
  100. const char * keytype = NULL;
  101. int ret = 1;
  102. key = import_read(infile, NULL, intype);
  103. if (!key) {
  104. fprintf(stderr, "Error reading key from '%s'\n",
  105. infile);
  106. goto out;
  107. }
  108. keytype = signkey_name_from_type(key->type, NULL);
  109. fprintf(stderr, "Key is a %s key\n", keytype);
  110. if (import_write(outfile, key, NULL, outtype) != 1) {
  111. fprintf(stderr, "Error writing key to '%s'\n", outfile);
  112. } else {
  113. fprintf(stderr, "Wrote key to '%s'\n", outfile);
  114. ret = 0;
  115. }
  116. out:
  117. if (key) {
  118. sign_key_free(key);
  119. }
  120. return ret;
  121. }