pkcs8.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* pkcs8.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999-2004.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include "apps.h"
  62. #include <openssl/pem.h>
  63. #include <openssl/err.h>
  64. #include <openssl/evp.h>
  65. #include <openssl/pkcs12.h>
  66. #define PROG pkcs8_main
  67. int MAIN(int, char **);
  68. int MAIN(int argc, char **argv)
  69. {
  70. ENGINE *e = NULL;
  71. char **args, *infile = NULL, *outfile = NULL;
  72. char *passargin = NULL, *passargout = NULL;
  73. BIO *in = NULL, *out = NULL;
  74. int topk8 = 0;
  75. int pbe_nid = -1;
  76. const EVP_CIPHER *cipher = NULL;
  77. int iter = PKCS12_DEFAULT_ITER;
  78. int informat, outformat;
  79. int p8_broken = PKCS8_OK;
  80. int nocrypt = 0;
  81. X509_SIG *p8 = NULL;
  82. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  83. EVP_PKEY *pkey = NULL;
  84. char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL;
  85. int badarg = 0;
  86. int ret = 1;
  87. #ifndef OPENSSL_NO_ENGINE
  88. char *engine = NULL;
  89. #endif
  90. if (bio_err == NULL)
  91. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  92. if (!load_config(bio_err, NULL))
  93. goto end;
  94. informat = FORMAT_PEM;
  95. outformat = FORMAT_PEM;
  96. ERR_load_crypto_strings();
  97. OpenSSL_add_all_algorithms();
  98. args = argv + 1;
  99. while (!badarg && *args && *args[0] == '-') {
  100. if (!strcmp(*args, "-v2")) {
  101. if (args[1]) {
  102. args++;
  103. cipher = EVP_get_cipherbyname(*args);
  104. if (!cipher) {
  105. BIO_printf(bio_err, "Unknown cipher %s\n", *args);
  106. badarg = 1;
  107. }
  108. } else
  109. badarg = 1;
  110. } else if (!strcmp(*args, "-v1")) {
  111. if (args[1]) {
  112. args++;
  113. pbe_nid = OBJ_txt2nid(*args);
  114. if (pbe_nid == NID_undef) {
  115. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", *args);
  116. badarg = 1;
  117. }
  118. } else
  119. badarg = 1;
  120. } else if (!strcmp(*args, "-v2prf")) {
  121. if (args[1]) {
  122. args++;
  123. pbe_nid = OBJ_txt2nid(*args);
  124. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
  125. BIO_printf(bio_err, "Unknown PRF algorithm %s\n", *args);
  126. badarg = 1;
  127. }
  128. } else
  129. badarg = 1;
  130. } else if (!strcmp(*args, "-inform")) {
  131. if (args[1]) {
  132. args++;
  133. informat = str2fmt(*args);
  134. } else
  135. badarg = 1;
  136. } else if (!strcmp(*args, "-outform")) {
  137. if (args[1]) {
  138. args++;
  139. outformat = str2fmt(*args);
  140. } else
  141. badarg = 1;
  142. } else if (!strcmp(*args, "-topk8"))
  143. topk8 = 1;
  144. else if (!strcmp(*args, "-noiter"))
  145. iter = 1;
  146. else if (!strcmp(*args, "-nocrypt"))
  147. nocrypt = 1;
  148. else if (!strcmp(*args, "-nooct"))
  149. p8_broken = PKCS8_NO_OCTET;
  150. else if (!strcmp(*args, "-nsdb"))
  151. p8_broken = PKCS8_NS_DB;
  152. else if (!strcmp(*args, "-embed"))
  153. p8_broken = PKCS8_EMBEDDED_PARAM;
  154. else if (!strcmp(*args, "-passin")) {
  155. if (!args[1])
  156. goto bad;
  157. passargin = *(++args);
  158. } else if (!strcmp(*args, "-passout")) {
  159. if (!args[1])
  160. goto bad;
  161. passargout = *(++args);
  162. }
  163. #ifndef OPENSSL_NO_ENGINE
  164. else if (strcmp(*args, "-engine") == 0) {
  165. if (!args[1])
  166. goto bad;
  167. engine = *(++args);
  168. }
  169. #endif
  170. else if (!strcmp(*args, "-in")) {
  171. if (args[1]) {
  172. args++;
  173. infile = *args;
  174. } else
  175. badarg = 1;
  176. } else if (!strcmp(*args, "-out")) {
  177. if (args[1]) {
  178. args++;
  179. outfile = *args;
  180. } else
  181. badarg = 1;
  182. } else
  183. badarg = 1;
  184. args++;
  185. }
  186. if (badarg) {
  187. bad:
  188. BIO_printf(bio_err, "Usage pkcs8 [options]\n");
  189. BIO_printf(bio_err, "where options are\n");
  190. BIO_printf(bio_err, "-in file input file\n");
  191. BIO_printf(bio_err, "-inform X input format (DER or PEM)\n");
  192. BIO_printf(bio_err,
  193. "-passin arg input file pass phrase source\n");
  194. BIO_printf(bio_err, "-outform X output format (DER or PEM)\n");
  195. BIO_printf(bio_err, "-out file output file\n");
  196. BIO_printf(bio_err,
  197. "-passout arg output file pass phrase source\n");
  198. BIO_printf(bio_err, "-topk8 output PKCS8 file\n");
  199. BIO_printf(bio_err,
  200. "-nooct use (nonstandard) no octet format\n");
  201. BIO_printf(bio_err,
  202. "-embed use (nonstandard) embedded DSA parameters format\n");
  203. BIO_printf(bio_err,
  204. "-nsdb use (nonstandard) DSA Netscape DB format\n");
  205. BIO_printf(bio_err, "-noiter use 1 as iteration count\n");
  206. BIO_printf(bio_err,
  207. "-nocrypt use or expect unencrypted private key\n");
  208. BIO_printf(bio_err,
  209. "-v2 alg use PKCS#5 v2.0 and cipher \"alg\"\n");
  210. BIO_printf(bio_err,
  211. "-v1 obj use PKCS#5 v1.5 and cipher \"alg\"\n");
  212. #ifndef OPENSSL_NO_ENGINE
  213. BIO_printf(bio_err,
  214. " -engine e use engine e, possibly a hardware device.\n");
  215. #endif
  216. goto end;
  217. }
  218. #ifndef OPENSSL_NO_ENGINE
  219. e = setup_engine(bio_err, engine, 0);
  220. #endif
  221. if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
  222. BIO_printf(bio_err, "Error getting passwords\n");
  223. goto end;
  224. }
  225. if ((pbe_nid == -1) && !cipher)
  226. pbe_nid = NID_pbeWithMD5AndDES_CBC;
  227. if (infile) {
  228. if (!(in = BIO_new_file(infile, "rb"))) {
  229. BIO_printf(bio_err, "Can't open input file %s\n", infile);
  230. goto end;
  231. }
  232. } else
  233. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  234. if (outfile) {
  235. if (!(out = BIO_new_file(outfile, "wb"))) {
  236. BIO_printf(bio_err, "Can't open output file %s\n", outfile);
  237. goto end;
  238. }
  239. } else {
  240. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  241. #ifdef OPENSSL_SYS_VMS
  242. {
  243. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  244. out = BIO_push(tmpbio, out);
  245. }
  246. #endif
  247. }
  248. if (topk8) {
  249. pkey = load_key(bio_err, infile, informat, 1, passin, e, "key");
  250. if (!pkey)
  251. goto end;
  252. if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken))) {
  253. BIO_printf(bio_err, "Error converting key\n");
  254. ERR_print_errors(bio_err);
  255. goto end;
  256. }
  257. if (nocrypt) {
  258. if (outformat == FORMAT_PEM)
  259. PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
  260. else if (outformat == FORMAT_ASN1)
  261. i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
  262. else {
  263. BIO_printf(bio_err, "Bad format specified for key\n");
  264. goto end;
  265. }
  266. } else {
  267. if (passout)
  268. p8pass = passout;
  269. else {
  270. p8pass = pass;
  271. if (EVP_read_pw_string
  272. (pass, sizeof pass, "Enter Encryption Password:", 1))
  273. goto end;
  274. }
  275. app_RAND_load_file(NULL, bio_err, 0);
  276. if (!(p8 = PKCS8_encrypt(pbe_nid, cipher,
  277. p8pass, strlen(p8pass),
  278. NULL, 0, iter, p8inf))) {
  279. BIO_printf(bio_err, "Error encrypting key\n");
  280. ERR_print_errors(bio_err);
  281. goto end;
  282. }
  283. app_RAND_write_file(NULL, bio_err);
  284. if (outformat == FORMAT_PEM)
  285. PEM_write_bio_PKCS8(out, p8);
  286. else if (outformat == FORMAT_ASN1)
  287. i2d_PKCS8_bio(out, p8);
  288. else {
  289. BIO_printf(bio_err, "Bad format specified for key\n");
  290. goto end;
  291. }
  292. }
  293. ret = 0;
  294. goto end;
  295. }
  296. if (nocrypt) {
  297. if (informat == FORMAT_PEM)
  298. p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
  299. else if (informat == FORMAT_ASN1)
  300. p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
  301. else {
  302. BIO_printf(bio_err, "Bad format specified for key\n");
  303. goto end;
  304. }
  305. } else {
  306. if (informat == FORMAT_PEM)
  307. p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
  308. else if (informat == FORMAT_ASN1)
  309. p8 = d2i_PKCS8_bio(in, NULL);
  310. else {
  311. BIO_printf(bio_err, "Bad format specified for key\n");
  312. goto end;
  313. }
  314. if (!p8) {
  315. BIO_printf(bio_err, "Error reading key\n");
  316. ERR_print_errors(bio_err);
  317. goto end;
  318. }
  319. if (passin)
  320. p8pass = passin;
  321. else {
  322. p8pass = pass;
  323. EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0);
  324. }
  325. p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
  326. }
  327. if (!p8inf) {
  328. BIO_printf(bio_err, "Error decrypting key\n");
  329. ERR_print_errors(bio_err);
  330. goto end;
  331. }
  332. if (!(pkey = EVP_PKCS82PKEY(p8inf))) {
  333. BIO_printf(bio_err, "Error converting key\n");
  334. ERR_print_errors(bio_err);
  335. goto end;
  336. }
  337. if (p8inf->broken) {
  338. BIO_printf(bio_err, "Warning: broken key encoding: ");
  339. switch (p8inf->broken) {
  340. case PKCS8_NO_OCTET:
  341. BIO_printf(bio_err, "No Octet String in PrivateKey\n");
  342. break;
  343. case PKCS8_EMBEDDED_PARAM:
  344. BIO_printf(bio_err, "DSA parameters included in PrivateKey\n");
  345. break;
  346. case PKCS8_NS_DB:
  347. BIO_printf(bio_err, "DSA public key include in PrivateKey\n");
  348. break;
  349. case PKCS8_NEG_PRIVKEY:
  350. BIO_printf(bio_err, "DSA private key value is negative\n");
  351. break;
  352. default:
  353. BIO_printf(bio_err, "Unknown broken type\n");
  354. break;
  355. }
  356. }
  357. if (outformat == FORMAT_PEM)
  358. PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
  359. else if (outformat == FORMAT_ASN1)
  360. i2d_PrivateKey_bio(out, pkey);
  361. else {
  362. BIO_printf(bio_err, "Bad format specified for key\n");
  363. goto end;
  364. }
  365. ret = 0;
  366. end:
  367. X509_SIG_free(p8);
  368. PKCS8_PRIV_KEY_INFO_free(p8inf);
  369. EVP_PKEY_free(pkey);
  370. BIO_free_all(out);
  371. BIO_free(in);
  372. if (passin)
  373. OPENSSL_free(passin);
  374. if (passout)
  375. OPENSSL_free(passout);
  376. return ret;
  377. }