pkcs8.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. char *engine = NULL;
  88. if (bio_err == NULL)
  89. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  90. if (!load_config(bio_err, NULL))
  91. goto end;
  92. informat = FORMAT_PEM;
  93. outformat = FORMAT_PEM;
  94. ERR_load_crypto_strings();
  95. OpenSSL_add_all_algorithms();
  96. args = argv + 1;
  97. while (!badarg && *args && *args[0] == '-') {
  98. if (!strcmp(*args, "-v2")) {
  99. if (args[1]) {
  100. args++;
  101. cipher = EVP_get_cipherbyname(*args);
  102. if (!cipher) {
  103. BIO_printf(bio_err, "Unknown cipher %s\n", *args);
  104. badarg = 1;
  105. }
  106. } else
  107. badarg = 1;
  108. } else if (!strcmp(*args, "-v1")) {
  109. if (args[1]) {
  110. args++;
  111. pbe_nid = OBJ_txt2nid(*args);
  112. if (pbe_nid == NID_undef) {
  113. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", *args);
  114. badarg = 1;
  115. }
  116. } else
  117. badarg = 1;
  118. } else if (!strcmp(*args, "-v2prf")) {
  119. if (args[1]) {
  120. args++;
  121. pbe_nid = OBJ_txt2nid(*args);
  122. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
  123. BIO_printf(bio_err, "Unknown PRF algorithm %s\n", *args);
  124. badarg = 1;
  125. }
  126. } else
  127. badarg = 1;
  128. } else if (!strcmp(*args, "-inform")) {
  129. if (args[1]) {
  130. args++;
  131. informat = str2fmt(*args);
  132. } else
  133. badarg = 1;
  134. } else if (!strcmp(*args, "-outform")) {
  135. if (args[1]) {
  136. args++;
  137. outformat = str2fmt(*args);
  138. } else
  139. badarg = 1;
  140. } else if (!strcmp(*args, "-topk8"))
  141. topk8 = 1;
  142. else if (!strcmp(*args, "-noiter"))
  143. iter = 1;
  144. else if (!strcmp(*args, "-nocrypt"))
  145. nocrypt = 1;
  146. else if (!strcmp(*args, "-nooct"))
  147. p8_broken = PKCS8_NO_OCTET;
  148. else if (!strcmp(*args, "-nsdb"))
  149. p8_broken = PKCS8_NS_DB;
  150. else if (!strcmp(*args, "-embed"))
  151. p8_broken = PKCS8_EMBEDDED_PARAM;
  152. else if (!strcmp(*args, "-passin")) {
  153. if (!args[1])
  154. goto bad;
  155. passargin = *(++args);
  156. } else if (!strcmp(*args, "-passout")) {
  157. if (!args[1])
  158. goto bad;
  159. passargout = *(++args);
  160. }
  161. #ifndef OPENSSL_NO_ENGINE
  162. else if (strcmp(*args, "-engine") == 0) {
  163. if (!args[1])
  164. goto bad;
  165. engine = *(++args);
  166. }
  167. #endif
  168. else if (!strcmp(*args, "-in")) {
  169. if (args[1]) {
  170. args++;
  171. infile = *args;
  172. } else
  173. badarg = 1;
  174. } else if (!strcmp(*args, "-out")) {
  175. if (args[1]) {
  176. args++;
  177. outfile = *args;
  178. } else
  179. badarg = 1;
  180. } else
  181. badarg = 1;
  182. args++;
  183. }
  184. if (badarg) {
  185. bad:
  186. BIO_printf(bio_err, "Usage pkcs8 [options]\n");
  187. BIO_printf(bio_err, "where options are\n");
  188. BIO_printf(bio_err, "-in file input file\n");
  189. BIO_printf(bio_err, "-inform X input format (DER or PEM)\n");
  190. BIO_printf(bio_err,
  191. "-passin arg input file pass phrase source\n");
  192. BIO_printf(bio_err, "-outform X output format (DER or PEM)\n");
  193. BIO_printf(bio_err, "-out file output file\n");
  194. BIO_printf(bio_err,
  195. "-passout arg output file pass phrase source\n");
  196. BIO_printf(bio_err, "-topk8 output PKCS8 file\n");
  197. BIO_printf(bio_err,
  198. "-nooct use (nonstandard) no octet format\n");
  199. BIO_printf(bio_err,
  200. "-embed use (nonstandard) embedded DSA parameters format\n");
  201. BIO_printf(bio_err,
  202. "-nsdb use (nonstandard) DSA Netscape DB format\n");
  203. BIO_printf(bio_err, "-noiter use 1 as iteration count\n");
  204. BIO_printf(bio_err,
  205. "-nocrypt use or expect unencrypted private key\n");
  206. BIO_printf(bio_err,
  207. "-v2 alg use PKCS#5 v2.0 and cipher \"alg\"\n");
  208. BIO_printf(bio_err,
  209. "-v1 obj use PKCS#5 v1.5 and cipher \"alg\"\n");
  210. #ifndef OPENSSL_NO_ENGINE
  211. BIO_printf(bio_err,
  212. " -engine e use engine e, possibly a hardware device.\n");
  213. #endif
  214. goto end;
  215. }
  216. e = setup_engine(bio_err, engine, 0);
  217. if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
  218. BIO_printf(bio_err, "Error getting passwords\n");
  219. goto end;
  220. }
  221. if ((pbe_nid == -1) && !cipher)
  222. pbe_nid = NID_pbeWithMD5AndDES_CBC;
  223. if (infile) {
  224. if (!(in = BIO_new_file(infile, "rb"))) {
  225. BIO_printf(bio_err, "Can't open input file %s\n", infile);
  226. goto end;
  227. }
  228. } else
  229. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  230. if (outfile) {
  231. if (!(out = BIO_new_file(outfile, "wb"))) {
  232. BIO_printf(bio_err, "Can't open output file %s\n", outfile);
  233. goto end;
  234. }
  235. } else {
  236. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  237. #ifdef OPENSSL_SYS_VMS
  238. {
  239. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  240. out = BIO_push(tmpbio, out);
  241. }
  242. #endif
  243. }
  244. if (topk8) {
  245. pkey = load_key(bio_err, infile, informat, 1, passin, e, "key");
  246. if (!pkey)
  247. goto end;
  248. if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken))) {
  249. BIO_printf(bio_err, "Error converting key\n");
  250. ERR_print_errors(bio_err);
  251. goto end;
  252. }
  253. if (nocrypt) {
  254. if (outformat == FORMAT_PEM)
  255. PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
  256. else if (outformat == FORMAT_ASN1)
  257. i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
  258. else {
  259. BIO_printf(bio_err, "Bad format specified for key\n");
  260. goto end;
  261. }
  262. } else {
  263. if (passout)
  264. p8pass = passout;
  265. else {
  266. p8pass = pass;
  267. if (EVP_read_pw_string
  268. (pass, sizeof pass, "Enter Encryption Password:", 1))
  269. goto end;
  270. }
  271. app_RAND_load_file(NULL, bio_err, 0);
  272. if (!(p8 = PKCS8_encrypt(pbe_nid, cipher,
  273. p8pass, strlen(p8pass),
  274. NULL, 0, iter, p8inf))) {
  275. BIO_printf(bio_err, "Error encrypting key\n");
  276. ERR_print_errors(bio_err);
  277. goto end;
  278. }
  279. app_RAND_write_file(NULL, bio_err);
  280. if (outformat == FORMAT_PEM)
  281. PEM_write_bio_PKCS8(out, p8);
  282. else if (outformat == FORMAT_ASN1)
  283. i2d_PKCS8_bio(out, p8);
  284. else {
  285. BIO_printf(bio_err, "Bad format specified for key\n");
  286. goto end;
  287. }
  288. }
  289. ret = 0;
  290. goto end;
  291. }
  292. if (nocrypt) {
  293. if (informat == FORMAT_PEM)
  294. p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
  295. else if (informat == FORMAT_ASN1)
  296. p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
  297. else {
  298. BIO_printf(bio_err, "Bad format specified for key\n");
  299. goto end;
  300. }
  301. } else {
  302. if (informat == FORMAT_PEM)
  303. p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
  304. else if (informat == FORMAT_ASN1)
  305. p8 = d2i_PKCS8_bio(in, NULL);
  306. else {
  307. BIO_printf(bio_err, "Bad format specified for key\n");
  308. goto end;
  309. }
  310. if (!p8) {
  311. BIO_printf(bio_err, "Error reading key\n");
  312. ERR_print_errors(bio_err);
  313. goto end;
  314. }
  315. if (passin)
  316. p8pass = passin;
  317. else {
  318. p8pass = pass;
  319. EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0);
  320. }
  321. p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
  322. }
  323. if (!p8inf) {
  324. BIO_printf(bio_err, "Error decrypting key\n");
  325. ERR_print_errors(bio_err);
  326. goto end;
  327. }
  328. if (!(pkey = EVP_PKCS82PKEY(p8inf))) {
  329. BIO_printf(bio_err, "Error converting key\n");
  330. ERR_print_errors(bio_err);
  331. goto end;
  332. }
  333. if (p8inf->broken) {
  334. BIO_printf(bio_err, "Warning: broken key encoding: ");
  335. switch (p8inf->broken) {
  336. case PKCS8_NO_OCTET:
  337. BIO_printf(bio_err, "No Octet String in PrivateKey\n");
  338. break;
  339. case PKCS8_EMBEDDED_PARAM:
  340. BIO_printf(bio_err, "DSA parameters included in PrivateKey\n");
  341. break;
  342. case PKCS8_NS_DB:
  343. BIO_printf(bio_err, "DSA public key include in PrivateKey\n");
  344. break;
  345. case PKCS8_NEG_PRIVKEY:
  346. BIO_printf(bio_err, "DSA private key value is negative\n");
  347. break;
  348. default:
  349. BIO_printf(bio_err, "Unknown broken type\n");
  350. break;
  351. }
  352. }
  353. if (outformat == FORMAT_PEM)
  354. PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
  355. else if (outformat == FORMAT_ASN1)
  356. i2d_PrivateKey_bio(out, pkey);
  357. else {
  358. BIO_printf(bio_err, "Bad format specified for key\n");
  359. goto end;
  360. }
  361. ret = 0;
  362. end:
  363. X509_SIG_free(p8);
  364. PKCS8_PRIV_KEY_INFO_free(p8inf);
  365. EVP_PKEY_free(pkey);
  366. release_engine(e);
  367. BIO_free_all(out);
  368. BIO_free(in);
  369. if (passin)
  370. OPENSSL_free(passin);
  371. if (passout)
  372. OPENSSL_free(passout);
  373. return ret;
  374. }