pem_info.c 9.9 KB

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