pem_pkey.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* We need to use some deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include <openssl/buffer.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/pkcs12.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/engine.h>
  19. #include <openssl/dh.h>
  20. #include <openssl/decoder.h>
  21. #include <openssl/ui.h>
  22. #include "internal/cryptlib.h"
  23. #include "internal/passphrase.h"
  24. #include "crypto/asn1.h"
  25. #include "crypto/x509.h"
  26. #include "crypto/evp.h"
  27. #include "pem_local.h"
  28. int ossl_pem_check_suffix(const char *pem_str, const char *suffix);
  29. static EVP_PKEY *pem_read_bio_key_decoder(BIO *bp, EVP_PKEY **x,
  30. pem_password_cb *cb, void *u,
  31. OSSL_LIB_CTX *libctx,
  32. const char *propq,
  33. int selection)
  34. {
  35. EVP_PKEY *pkey = NULL;
  36. OSSL_DECODER_CTX *dctx = NULL;
  37. int pos, newpos;
  38. if ((pos = BIO_tell(bp)) < 0)
  39. /* We can depend on BIO_tell() thanks to the BIO_f_readbuffer() */
  40. return NULL;
  41. dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, NULL,
  42. selection, libctx, propq);
  43. if (dctx == NULL)
  44. return NULL;
  45. if (cb == NULL)
  46. cb = PEM_def_callback;
  47. if (!OSSL_DECODER_CTX_set_pem_password_cb(dctx, cb, u))
  48. goto err;
  49. ERR_set_mark();
  50. while (!OSSL_DECODER_from_bio(dctx, bp) || pkey == NULL)
  51. if (BIO_eof(bp) != 0 || (newpos = BIO_tell(bp)) < 0 || newpos <= pos) {
  52. ERR_clear_last_mark();
  53. goto err;
  54. } else {
  55. if (ERR_GET_REASON(ERR_peek_error()) == ERR_R_UNSUPPORTED) {
  56. /* unsupported PEM data, try again */
  57. ERR_pop_to_mark();
  58. ERR_set_mark();
  59. } else {
  60. /* other error, bail out */
  61. ERR_clear_last_mark();
  62. goto err;
  63. }
  64. pos = newpos;
  65. }
  66. ERR_pop_to_mark();
  67. /* if we were asked for private key, the public key is optional */
  68. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  69. selection = selection & ~OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  70. if (!evp_keymgmt_util_has(pkey, selection)) {
  71. EVP_PKEY_free(pkey);
  72. pkey = NULL;
  73. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
  74. goto err;
  75. }
  76. if (x != NULL) {
  77. EVP_PKEY_free(*x);
  78. *x = pkey;
  79. }
  80. err:
  81. OSSL_DECODER_CTX_free(dctx);
  82. return pkey;
  83. }
  84. static EVP_PKEY *pem_read_bio_key_legacy(BIO *bp, EVP_PKEY **x,
  85. pem_password_cb *cb, void *u,
  86. OSSL_LIB_CTX *libctx,
  87. const char *propq,
  88. int selection)
  89. {
  90. char *nm = NULL;
  91. const unsigned char *p = NULL;
  92. unsigned char *data = NULL;
  93. long len;
  94. int slen;
  95. EVP_PKEY *ret = NULL;
  96. ERR_set_mark(); /* not interested in PEM read errors */
  97. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  98. if (!PEM_bytes_read_bio_secmem(&data, &len, &nm,
  99. PEM_STRING_EVP_PKEY,
  100. bp, cb, u)) {
  101. ERR_pop_to_mark();
  102. return NULL;
  103. }
  104. } else {
  105. const char *pem_string = PEM_STRING_PARAMETERS;
  106. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  107. pem_string = PEM_STRING_PUBLIC;
  108. if (!PEM_bytes_read_bio(&data, &len, &nm,
  109. pem_string,
  110. bp, cb, u)) {
  111. ERR_pop_to_mark();
  112. return NULL;
  113. }
  114. }
  115. ERR_clear_last_mark();
  116. p = data;
  117. if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
  118. PKCS8_PRIV_KEY_INFO *p8inf;
  119. if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len)) == NULL)
  120. goto p8err;
  121. ret = evp_pkcs82pkey_legacy(p8inf, libctx, propq);
  122. if (x != NULL) {
  123. EVP_PKEY_free(*x);
  124. *x = ret;
  125. }
  126. PKCS8_PRIV_KEY_INFO_free(p8inf);
  127. } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
  128. PKCS8_PRIV_KEY_INFO *p8inf;
  129. X509_SIG *p8;
  130. int klen;
  131. char psbuf[PEM_BUFSIZE];
  132. if ((p8 = d2i_X509_SIG(NULL, &p, len)) == NULL)
  133. goto p8err;
  134. if (cb != NULL)
  135. klen = cb(psbuf, PEM_BUFSIZE, 0, u);
  136. else
  137. klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
  138. if (klen < 0) {
  139. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
  140. X509_SIG_free(p8);
  141. goto err;
  142. }
  143. p8inf = PKCS8_decrypt(p8, psbuf, klen);
  144. X509_SIG_free(p8);
  145. OPENSSL_cleanse(psbuf, klen);
  146. if (p8inf == NULL)
  147. goto p8err;
  148. ret = evp_pkcs82pkey_legacy(p8inf, libctx, propq);
  149. if (x != NULL) {
  150. EVP_PKEY_free(*x);
  151. *x = ret;
  152. }
  153. PKCS8_PRIV_KEY_INFO_free(p8inf);
  154. } else if ((slen = ossl_pem_check_suffix(nm, "PRIVATE KEY")) > 0) {
  155. const EVP_PKEY_ASN1_METHOD *ameth;
  156. ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
  157. if (ameth == NULL || ameth->old_priv_decode == NULL)
  158. goto p8err;
  159. ret = ossl_d2i_PrivateKey_legacy(ameth->pkey_id, x, &p, len, libctx,
  160. propq);
  161. } else if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0
  162. && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  163. /* Trying legacy PUBKEY decoding only if we do not want private key. */
  164. ret = ossl_d2i_PUBKEY_legacy(x, &p, len);
  165. } else if ((selection & EVP_PKEY_KEYPAIR) == 0
  166. && (slen = ossl_pem_check_suffix(nm, "PARAMETERS")) > 0) {
  167. /* Trying legacy params decoding only if we do not want a key. */
  168. ret = EVP_PKEY_new();
  169. if (ret == NULL)
  170. goto err;
  171. if (!EVP_PKEY_set_type_str(ret, nm, slen)
  172. || !ret->ameth->param_decode
  173. || !ret->ameth->param_decode(ret, &p, len)) {
  174. EVP_PKEY_free(ret);
  175. ret = NULL;
  176. goto err;
  177. }
  178. if (x) {
  179. EVP_PKEY_free(*x);
  180. *x = ret;
  181. }
  182. }
  183. p8err:
  184. if (ret == NULL && ERR_peek_last_error() == 0)
  185. /* ensure some error is reported but do not hide the real one */
  186. ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB);
  187. err:
  188. OPENSSL_secure_free(nm);
  189. OPENSSL_secure_clear_free(data, len);
  190. return ret;
  191. }
  192. static EVP_PKEY *pem_read_bio_key(BIO *bp, EVP_PKEY **x,
  193. pem_password_cb *cb, void *u,
  194. OSSL_LIB_CTX *libctx,
  195. const char *propq,
  196. int selection)
  197. {
  198. EVP_PKEY *ret = NULL;
  199. BIO *new_bio = NULL;
  200. int pos;
  201. struct ossl_passphrase_data_st pwdata = { 0 };
  202. if ((pos = BIO_tell(bp)) < 0) {
  203. new_bio = BIO_new(BIO_f_readbuffer());
  204. if (new_bio == NULL)
  205. return NULL;
  206. bp = BIO_push(new_bio, bp);
  207. pos = BIO_tell(bp);
  208. }
  209. if (cb == NULL)
  210. cb = PEM_def_callback;
  211. if (!ossl_pw_set_pem_password_cb(&pwdata, cb, u)
  212. || !ossl_pw_enable_passphrase_caching(&pwdata))
  213. goto err;
  214. ERR_set_mark();
  215. ret = pem_read_bio_key_decoder(bp, x, ossl_pw_pem_password, &pwdata,
  216. libctx, propq, selection);
  217. if (ret == NULL
  218. && (BIO_seek(bp, pos) < 0
  219. || (ret = pem_read_bio_key_legacy(bp, x,
  220. ossl_pw_pem_password, &pwdata,
  221. libctx, propq,
  222. selection)) == NULL))
  223. ERR_clear_last_mark();
  224. else
  225. ERR_pop_to_mark();
  226. err:
  227. ossl_pw_clear_passphrase_data(&pwdata);
  228. if (new_bio != NULL) {
  229. BIO_pop(new_bio);
  230. BIO_free(new_bio);
  231. }
  232. return ret;
  233. }
  234. EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
  235. pem_password_cb *cb, void *u,
  236. OSSL_LIB_CTX *libctx, const char *propq)
  237. {
  238. return pem_read_bio_key(bp, x, cb, u, libctx, propq,
  239. EVP_PKEY_PUBLIC_KEY);
  240. }
  241. EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  242. void *u)
  243. {
  244. return PEM_read_bio_PUBKEY_ex(bp, x, cb, u, NULL, NULL);
  245. }
  246. #ifndef OPENSSL_NO_STDIO
  247. EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
  248. pem_password_cb *cb, void *u,
  249. OSSL_LIB_CTX *libctx, const char *propq)
  250. {
  251. BIO *b;
  252. EVP_PKEY *ret;
  253. if ((b = BIO_new(BIO_s_file())) == NULL) {
  254. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  255. return 0;
  256. }
  257. BIO_set_fp(b, fp, BIO_NOCLOSE);
  258. ret = PEM_read_bio_PUBKEY_ex(b, x, cb, u, libctx, propq);
  259. BIO_free(b);
  260. return ret;
  261. }
  262. EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
  263. {
  264. return PEM_read_PUBKEY_ex(fp, x, cb, u, NULL, NULL);
  265. }
  266. #endif
  267. EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
  268. pem_password_cb *cb, void *u,
  269. OSSL_LIB_CTX *libctx, const char *propq)
  270. {
  271. return pem_read_bio_key(bp, x, cb, u, libctx, propq,
  272. /* we also want the public key, if available */
  273. EVP_PKEY_KEYPAIR);
  274. }
  275. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  276. void *u)
  277. {
  278. return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL);
  279. }
  280. PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
  281. {
  282. IMPLEMENT_PEM_provided_write_body_vars(pkey, PrivateKey, propq);
  283. IMPLEMENT_PEM_provided_write_body_pass();
  284. IMPLEMENT_PEM_provided_write_body_main(pkey, bio);
  285. legacy:
  286. if (x != NULL && (x->ameth == NULL || x->ameth->priv_encode != NULL))
  287. return PEM_write_bio_PKCS8PrivateKey(out, x, enc,
  288. (const char *)kstr, klen, cb, u);
  289. return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
  290. }
  291. PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
  292. {
  293. return PEM_write_bio_PrivateKey_ex(out, x, enc, kstr, klen, cb, u,
  294. NULL, NULL);
  295. }
  296. /*
  297. * Note: there is no way to tell a provided pkey encoder to use "traditional"
  298. * encoding. Therefore, if the pkey is provided, we try to take a copy
  299. */
  300. int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
  301. const EVP_CIPHER *enc,
  302. const unsigned char *kstr, int klen,
  303. pem_password_cb *cb, void *u)
  304. {
  305. char pem_str[80];
  306. EVP_PKEY *copy = NULL;
  307. int ret;
  308. if (x == NULL)
  309. return 0;
  310. if (evp_pkey_is_assigned(x)
  311. && evp_pkey_is_provided(x)
  312. && evp_pkey_copy_downgraded(&copy, x))
  313. x = copy;
  314. if (x->ameth == NULL || x->ameth->old_priv_encode == NULL) {
  315. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  316. EVP_PKEY_free(copy);
  317. return 0;
  318. }
  319. BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
  320. ret = PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
  321. pem_str, bp, x, enc, kstr, klen, cb, u);
  322. EVP_PKEY_free(copy);
  323. return ret;
  324. }
  325. static int no_password_cb(char *buf, int num, int rwflag, void *userdata)
  326. {
  327. return -1;
  328. }
  329. EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
  330. OSSL_LIB_CTX *libctx, const char *propq)
  331. {
  332. /*
  333. * PEM_read_bio_Parameters(_ex) should never ask for a password. Any attempt
  334. * to get a password just fails.
  335. */
  336. return pem_read_bio_key(bp, x, no_password_cb, NULL, libctx, propq,
  337. EVP_PKEY_KEY_PARAMETERS);
  338. }
  339. EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
  340. {
  341. return PEM_read_bio_Parameters_ex(bp, x, NULL, NULL);
  342. }
  343. PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
  344. {
  345. char pem_str[80];
  346. IMPLEMENT_PEM_provided_write_body_vars(pkey, Parameters, NULL);
  347. IMPLEMENT_PEM_provided_write_body_main(pkey, bio);
  348. legacy:
  349. if (!x->ameth || !x->ameth->param_encode)
  350. return 0;
  351. BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
  352. return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
  353. pem_str, out, x, NULL, NULL, 0, 0, NULL);
  354. }
  355. #ifndef OPENSSL_NO_STDIO
  356. EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  357. void *u, OSSL_LIB_CTX *libctx,
  358. const char *propq)
  359. {
  360. BIO *b;
  361. EVP_PKEY *ret;
  362. if ((b = BIO_new(BIO_s_file())) == NULL) {
  363. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  364. return 0;
  365. }
  366. BIO_set_fp(b, fp, BIO_NOCLOSE);
  367. ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq);
  368. BIO_free(b);
  369. return ret;
  370. }
  371. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  372. void *u)
  373. {
  374. return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL);
  375. }
  376. PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, FILE, write)
  377. {
  378. BIO *b;
  379. int ret;
  380. if ((b = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
  381. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  382. return 0;
  383. }
  384. ret = PEM_write_bio_PrivateKey_ex(b, x, enc, kstr, klen, cb, u,
  385. libctx, propq);
  386. BIO_free(b);
  387. return ret;
  388. }
  389. PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, FILE, write)
  390. {
  391. return PEM_write_PrivateKey_ex(out, x, enc, kstr, klen, cb, u, NULL, NULL);
  392. }
  393. #endif