pem_info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright 1995-2020 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. /*
  10. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/buffer.h>
  17. #include <openssl/objects.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/x509.h>
  20. #include <openssl/pem.h>
  21. #include <openssl/rsa.h>
  22. #include <openssl/dsa.h>
  23. DEFINE_STACK_OF(X509_INFO)
  24. #ifndef OPENSSL_NO_STDIO
  25. STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
  26. pem_password_cb *cb, void *u)
  27. {
  28. BIO *b;
  29. STACK_OF(X509_INFO) *ret;
  30. if ((b = BIO_new(BIO_s_file())) == NULL) {
  31. PEMerr(PEM_F_PEM_X509_INFO_READ, ERR_R_BUF_LIB);
  32. return 0;
  33. }
  34. BIO_set_fp(b, fp, BIO_NOCLOSE);
  35. ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
  36. BIO_free(b);
  37. return ret;
  38. }
  39. #endif
  40. STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
  41. pem_password_cb *cb, void *u)
  42. {
  43. X509_INFO *xi = NULL;
  44. char *name = NULL, *header = NULL;
  45. void *pp;
  46. unsigned char *data = NULL;
  47. const unsigned char *p;
  48. long len, error = 0;
  49. int ok = 0;
  50. STACK_OF(X509_INFO) *ret = NULL;
  51. unsigned int i, raw, ptype;
  52. d2i_of_void *d2i = 0;
  53. if (sk == NULL) {
  54. if ((ret = sk_X509_INFO_new_null()) == NULL) {
  55. PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_MALLOC_FAILURE);
  56. goto err;
  57. }
  58. } else
  59. ret = sk;
  60. if ((xi = X509_INFO_new()) == NULL)
  61. goto err;
  62. for (;;) {
  63. raw = 0;
  64. ptype = 0;
  65. i = PEM_read_bio(bp, &name, &header, &data, &len);
  66. if (i == 0) {
  67. error = ERR_GET_REASON(ERR_peek_last_error());
  68. if (error == PEM_R_NO_START_LINE) {
  69. ERR_clear_error();
  70. break;
  71. }
  72. goto err;
  73. }
  74. start:
  75. if ((strcmp(name, PEM_STRING_X509) == 0) ||
  76. (strcmp(name, PEM_STRING_X509_OLD) == 0)) {
  77. d2i = (D2I_OF(void)) d2i_X509;
  78. if (xi->x509 != NULL) {
  79. if (!sk_X509_INFO_push(ret, xi))
  80. goto err;
  81. if ((xi = X509_INFO_new()) == NULL)
  82. goto err;
  83. goto start;
  84. }
  85. pp = &(xi->x509);
  86. } else if ((strcmp(name, PEM_STRING_X509_TRUSTED) == 0)) {
  87. d2i = (D2I_OF(void)) d2i_X509_AUX;
  88. if (xi->x509 != NULL) {
  89. if (!sk_X509_INFO_push(ret, xi))
  90. goto err;
  91. if ((xi = X509_INFO_new()) == NULL)
  92. goto err;
  93. goto start;
  94. }
  95. pp = &(xi->x509);
  96. } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {
  97. d2i = (D2I_OF(void)) d2i_X509_CRL;
  98. if (xi->crl != NULL) {
  99. if (!sk_X509_INFO_push(ret, xi))
  100. goto err;
  101. if ((xi = X509_INFO_new()) == NULL)
  102. goto err;
  103. goto start;
  104. }
  105. pp = &(xi->crl);
  106. } else
  107. #ifndef OPENSSL_NO_RSA
  108. if (strcmp(name, PEM_STRING_RSA) == 0) {
  109. d2i = (D2I_OF(void)) d2i_RSAPrivateKey;
  110. if (xi->x_pkey != NULL) {
  111. if (!sk_X509_INFO_push(ret, xi))
  112. goto err;
  113. if ((xi = X509_INFO_new()) == NULL)
  114. goto err;
  115. goto start;
  116. }
  117. xi->enc_data = NULL;
  118. xi->enc_len = 0;
  119. xi->x_pkey = X509_PKEY_new();
  120. if (xi->x_pkey == NULL)
  121. goto err;
  122. ptype = EVP_PKEY_RSA;
  123. pp = &xi->x_pkey->dec_pkey;
  124. if ((int)strlen(header) > 10) /* assume encrypted */
  125. raw = 1;
  126. } else
  127. #endif
  128. #ifndef OPENSSL_NO_DSA
  129. if (strcmp(name, PEM_STRING_DSA) == 0) {
  130. d2i = (D2I_OF(void)) d2i_DSAPrivateKey;
  131. if (xi->x_pkey != NULL) {
  132. if (!sk_X509_INFO_push(ret, xi))
  133. goto err;
  134. if ((xi = X509_INFO_new()) == NULL)
  135. goto err;
  136. goto start;
  137. }
  138. xi->enc_data = NULL;
  139. xi->enc_len = 0;
  140. xi->x_pkey = X509_PKEY_new();
  141. if (xi->x_pkey == NULL)
  142. goto err;
  143. ptype = EVP_PKEY_DSA;
  144. pp = &xi->x_pkey->dec_pkey;
  145. if ((int)strlen(header) > 10) /* assume encrypted */
  146. raw = 1;
  147. } else
  148. #endif
  149. #ifndef OPENSSL_NO_EC
  150. if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {
  151. d2i = (D2I_OF(void)) d2i_ECPrivateKey;
  152. if (xi->x_pkey != NULL) {
  153. if (!sk_X509_INFO_push(ret, xi))
  154. goto err;
  155. if ((xi = X509_INFO_new()) == NULL)
  156. goto err;
  157. goto start;
  158. }
  159. xi->enc_data = NULL;
  160. xi->enc_len = 0;
  161. xi->x_pkey = X509_PKEY_new();
  162. if (xi->x_pkey == NULL)
  163. goto err;
  164. ptype = EVP_PKEY_EC;
  165. pp = &xi->x_pkey->dec_pkey;
  166. if ((int)strlen(header) > 10) /* assume encrypted */
  167. raw = 1;
  168. } else
  169. #endif
  170. {
  171. d2i = NULL;
  172. pp = NULL;
  173. }
  174. if (d2i != NULL) {
  175. if (!raw) {
  176. EVP_CIPHER_INFO cipher;
  177. if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
  178. goto err;
  179. if (!PEM_do_header(&cipher, data, &len, cb, u))
  180. goto err;
  181. p = data;
  182. if (ptype) {
  183. if (!d2i_PrivateKey(ptype, pp, &p, len)) {
  184. PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);
  185. goto err;
  186. }
  187. } else if (d2i(pp, &p, len) == NULL) {
  188. PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);
  189. goto err;
  190. }
  191. } else { /* encrypted RSA data */
  192. if (!PEM_get_EVP_CIPHER_INFO(header, &xi->enc_cipher))
  193. goto err;
  194. xi->enc_data = (char *)data;
  195. xi->enc_len = (int)len;
  196. data = NULL;
  197. }
  198. } else {
  199. /* unknown */
  200. }
  201. OPENSSL_free(name);
  202. name = NULL;
  203. OPENSSL_free(header);
  204. header = NULL;
  205. OPENSSL_free(data);
  206. data = NULL;
  207. }
  208. /*
  209. * if the last one hasn't been pushed yet and there is anything in it
  210. * then add it to the stack ...
  211. */
  212. if ((xi->x509 != NULL) || (xi->crl != NULL) ||
  213. (xi->x_pkey != NULL) || (xi->enc_data != NULL)) {
  214. if (!sk_X509_INFO_push(ret, xi))
  215. goto err;
  216. xi = NULL;
  217. }
  218. ok = 1;
  219. err:
  220. X509_INFO_free(xi);
  221. if (!ok) {
  222. for (i = 0; ((int)i) < sk_X509_INFO_num(ret); i++) {
  223. xi = sk_X509_INFO_value(ret, i);
  224. X509_INFO_free(xi);
  225. }
  226. if (ret != sk)
  227. sk_X509_INFO_free(ret);
  228. ret = NULL;
  229. }
  230. OPENSSL_free(name);
  231. OPENSSL_free(header);
  232. OPENSSL_free(data);
  233. return ret;
  234. }
  235. /* A TJH addition */
  236. int PEM_X509_INFO_write_bio(BIO *bp, const X509_INFO *xi, EVP_CIPHER *enc,
  237. const unsigned char *kstr, int klen,
  238. pem_password_cb *cb, void *u)
  239. {
  240. int i, ret = 0;
  241. unsigned char *data = NULL;
  242. const char *objstr = NULL;
  243. char buf[PEM_BUFSIZE];
  244. const unsigned char *iv = NULL;
  245. if (enc != NULL) {
  246. objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
  247. if (objstr == NULL
  248. /*
  249. * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
  250. * fits into buf
  251. */
  252. || (strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13)
  253. > sizeof(buf)) {
  254. PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, PEM_R_UNSUPPORTED_CIPHER);
  255. goto err;
  256. }
  257. }
  258. /*
  259. * now for the fun part ... if we have a private key then we have to be
  260. * able to handle a not-yet-decrypted key being written out correctly ...
  261. * if it is decrypted or it is non-encrypted then we use the base code
  262. */
  263. if (xi->x_pkey != NULL) {
  264. if ((xi->enc_data != NULL) && (xi->enc_len > 0)) {
  265. if (enc == NULL) {
  266. PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, PEM_R_CIPHER_IS_NULL);
  267. goto err;
  268. }
  269. /* copy from weirdo names into more normal things */
  270. iv = xi->enc_cipher.iv;
  271. data = (unsigned char *)xi->enc_data;
  272. i = xi->enc_len;
  273. /*
  274. * we take the encryption data from the internal stuff rather
  275. * than what the user has passed us ... as we have to match
  276. * exactly for some strange reason
  277. */
  278. objstr = OBJ_nid2sn(EVP_CIPHER_nid(xi->enc_cipher.cipher));
  279. if (objstr == NULL) {
  280. PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,
  281. PEM_R_UNSUPPORTED_CIPHER);
  282. goto err;
  283. }
  284. /* Create the right magic header stuff */
  285. buf[0] = '\0';
  286. PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
  287. PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc),
  288. (const char *)iv);
  289. /* use the normal code to write things out */
  290. i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
  291. if (i <= 0)
  292. goto err;
  293. } else {
  294. /* Add DSA/DH */
  295. #ifndef OPENSSL_NO_RSA
  296. /* normal optionally encrypted stuff */
  297. if (PEM_write_bio_RSAPrivateKey(bp,
  298. EVP_PKEY_get0_RSA(xi->x_pkey->dec_pkey),
  299. enc, kstr, klen, cb, u) <= 0)
  300. goto err;
  301. #endif
  302. }
  303. }
  304. /* if we have a certificate then write it out now */
  305. if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0))
  306. goto err;
  307. /*
  308. * we are ignoring anything else that is loaded into the X509_INFO
  309. * structure for the moment ... as I don't need it so I'm not coding it
  310. * here and Eric can do it when this makes it into the base library --tjh
  311. */
  312. ret = 1;
  313. err:
  314. OPENSSL_cleanse(buf, PEM_BUFSIZE);
  315. return ret;
  316. }