You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

382 lines
13 KiB

  1. /* crypto/pem/pem_info.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <openssl/pem.h>
  59. #include <assert.h>
  60. #include <stdio.h>
  61. #include <string.h>
  62. #include <openssl/buf.h>
  63. #include <openssl/dsa.h>
  64. #include <openssl/err.h>
  65. #include <openssl/evp.h>
  66. #include <openssl/mem.h>
  67. #include <openssl/obj.h>
  68. #include <openssl/rsa.h>
  69. #include <openssl/x509.h>
  70. #ifndef OPENSSL_NO_FP_API
  71. STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
  72. pem_password_cb *cb, void *u)
  73. {
  74. BIO *b;
  75. STACK_OF(X509_INFO) *ret;
  76. if ((b = BIO_new(BIO_s_file())) == NULL) {
  77. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  78. return (0);
  79. }
  80. BIO_set_fp(b, fp, BIO_NOCLOSE);
  81. ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
  82. BIO_free(b);
  83. return (ret);
  84. }
  85. #endif
  86. STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
  87. pem_password_cb *cb, void *u)
  88. {
  89. X509_INFO *xi = NULL;
  90. char *name = NULL, *header = NULL;
  91. void *pp;
  92. unsigned char *data = NULL;
  93. const unsigned char *p;
  94. long len, error = 0;
  95. int ok = 0;
  96. STACK_OF(X509_INFO) *ret = NULL;
  97. unsigned int i, raw, ptype;
  98. d2i_of_void *d2i = 0;
  99. if (sk == NULL) {
  100. if ((ret = sk_X509_INFO_new_null()) == NULL) {
  101. OPENSSL_PUT_ERROR(PEM, ERR_R_MALLOC_FAILURE);
  102. goto err;
  103. }
  104. } else
  105. ret = sk;
  106. if ((xi = X509_INFO_new()) == NULL)
  107. goto err;
  108. for (;;) {
  109. raw = 0;
  110. ptype = 0;
  111. i = PEM_read_bio(bp, &name, &header, &data, &len);
  112. if (i == 0) {
  113. error = ERR_GET_REASON(ERR_peek_last_error());
  114. if (error == PEM_R_NO_START_LINE) {
  115. ERR_clear_error();
  116. break;
  117. }
  118. goto err;
  119. }
  120. start:
  121. if ((strcmp(name, PEM_STRING_X509) == 0) ||
  122. (strcmp(name, PEM_STRING_X509_OLD) == 0)) {
  123. d2i = (D2I_OF(void)) d2i_X509;
  124. if (xi->x509 != NULL) {
  125. if (!sk_X509_INFO_push(ret, xi))
  126. goto err;
  127. if ((xi = X509_INFO_new()) == NULL)
  128. goto err;
  129. goto start;
  130. }
  131. pp = &(xi->x509);
  132. } else if ((strcmp(name, PEM_STRING_X509_TRUSTED) == 0)) {
  133. d2i = (D2I_OF(void)) d2i_X509_AUX;
  134. if (xi->x509 != NULL) {
  135. if (!sk_X509_INFO_push(ret, xi))
  136. goto err;
  137. if ((xi = X509_INFO_new()) == NULL)
  138. goto err;
  139. goto start;
  140. }
  141. pp = &(xi->x509);
  142. } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {
  143. d2i = (D2I_OF(void)) d2i_X509_CRL;
  144. if (xi->crl != NULL) {
  145. if (!sk_X509_INFO_push(ret, xi))
  146. goto err;
  147. if ((xi = X509_INFO_new()) == NULL)
  148. goto err;
  149. goto start;
  150. }
  151. pp = &(xi->crl);
  152. } else if (strcmp(name, PEM_STRING_RSA) == 0) {
  153. d2i = (D2I_OF(void)) d2i_RSAPrivateKey;
  154. if (xi->x_pkey != NULL) {
  155. if (!sk_X509_INFO_push(ret, xi))
  156. goto err;
  157. if ((xi = X509_INFO_new()) == NULL)
  158. goto err;
  159. goto start;
  160. }
  161. xi->enc_data = NULL;
  162. xi->enc_len = 0;
  163. xi->x_pkey = X509_PKEY_new();
  164. ptype = EVP_PKEY_RSA;
  165. pp = &xi->x_pkey->dec_pkey;
  166. if ((int)strlen(header) > 10) /* assume encrypted */
  167. raw = 1;
  168. } else
  169. #ifndef OPENSSL_NO_DSA
  170. if (strcmp(name, PEM_STRING_DSA) == 0) {
  171. d2i = (D2I_OF(void)) d2i_DSAPrivateKey;
  172. if (xi->x_pkey != NULL) {
  173. if (!sk_X509_INFO_push(ret, xi))
  174. goto err;
  175. if ((xi = X509_INFO_new()) == NULL)
  176. goto err;
  177. goto start;
  178. }
  179. xi->enc_data = NULL;
  180. xi->enc_len = 0;
  181. xi->x_pkey = X509_PKEY_new();
  182. ptype = EVP_PKEY_DSA;
  183. pp = &xi->x_pkey->dec_pkey;
  184. if ((int)strlen(header) > 10) /* assume encrypted */
  185. raw = 1;
  186. } else
  187. #endif
  188. if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {
  189. d2i = (D2I_OF(void)) d2i_ECPrivateKey;
  190. if (xi->x_pkey != NULL) {
  191. if (!sk_X509_INFO_push(ret, xi))
  192. goto err;
  193. if ((xi = X509_INFO_new()) == NULL)
  194. goto err;
  195. goto start;
  196. }
  197. xi->enc_data = NULL;
  198. xi->enc_len = 0;
  199. xi->x_pkey = X509_PKEY_new();
  200. ptype = EVP_PKEY_EC;
  201. pp = &xi->x_pkey->dec_pkey;
  202. if ((int)strlen(header) > 10) /* assume encrypted */
  203. raw = 1;
  204. } else {
  205. d2i = NULL;
  206. pp = NULL;
  207. }
  208. if (d2i != NULL) {
  209. if (!raw) {
  210. EVP_CIPHER_INFO cipher;
  211. if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
  212. goto err;
  213. if (!PEM_do_header(&cipher, data, &len, cb, u))
  214. goto err;
  215. p = data;
  216. if (ptype) {
  217. if (!d2i_PrivateKey(ptype, pp, &p, len)) {
  218. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  219. goto err;
  220. }
  221. } else if (d2i(pp, &p, len) == NULL) {
  222. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  223. goto err;
  224. }
  225. } else { /* encrypted RSA data */
  226. if (!PEM_get_EVP_CIPHER_INFO(header, &xi->enc_cipher))
  227. goto err;
  228. xi->enc_data = (char *)data;
  229. xi->enc_len = (int)len;
  230. data = NULL;
  231. }
  232. } else {
  233. /* unknown */
  234. }
  235. if (name != NULL)
  236. OPENSSL_free(name);
  237. if (header != NULL)
  238. OPENSSL_free(header);
  239. if (data != NULL)
  240. OPENSSL_free(data);
  241. name = NULL;
  242. header = NULL;
  243. data = NULL;
  244. }
  245. /*
  246. * if the last one hasn't been pushed yet and there is anything in it
  247. * then add it to the stack ...
  248. */
  249. if ((xi->x509 != NULL) || (xi->crl != NULL) ||
  250. (xi->x_pkey != NULL) || (xi->enc_data != NULL)) {
  251. if (!sk_X509_INFO_push(ret, xi))
  252. goto err;
  253. xi = NULL;
  254. }
  255. ok = 1;
  256. err:
  257. if (xi != NULL)
  258. X509_INFO_free(xi);
  259. if (!ok) {
  260. for (i = 0; i < sk_X509_INFO_num(ret); i++) {
  261. xi = sk_X509_INFO_value(ret, i);
  262. X509_INFO_free(xi);
  263. }
  264. if (ret != sk)
  265. sk_X509_INFO_free(ret);
  266. ret = NULL;
  267. }
  268. if (name != NULL)
  269. OPENSSL_free(name);
  270. if (header != NULL)
  271. OPENSSL_free(header);
  272. if (data != NULL)
  273. OPENSSL_free(data);
  274. return (ret);
  275. }
  276. /* A TJH addition */
  277. int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
  278. unsigned char *kstr, int klen,
  279. pem_password_cb *cb, void *u)
  280. {
  281. EVP_CIPHER_CTX ctx;
  282. int i, ret = 0;
  283. unsigned char *data = NULL;
  284. const char *objstr = NULL;
  285. char buf[PEM_BUFSIZE];
  286. unsigned char *iv = NULL;
  287. unsigned iv_len = 0;
  288. if (enc != NULL) {
  289. iv_len = EVP_CIPHER_iv_length(enc);
  290. objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
  291. if (objstr == NULL) {
  292. OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_CIPHER);
  293. goto err;
  294. }
  295. }
  296. /*
  297. * now for the fun part ... if we have a private key then we have to be
  298. * able to handle a not-yet-decrypted key being written out correctly ...
  299. * if it is decrypted or it is non-encrypted then we use the base code
  300. */
  301. if (xi->x_pkey != NULL) {
  302. if ((xi->enc_data != NULL) && (xi->enc_len > 0)) {
  303. if (enc == NULL) {
  304. OPENSSL_PUT_ERROR(PEM, PEM_R_CIPHER_IS_NULL);
  305. goto err;
  306. }
  307. /* copy from weirdo names into more normal things */
  308. iv = xi->enc_cipher.iv;
  309. data = (unsigned char *)xi->enc_data;
  310. i = xi->enc_len;
  311. /*
  312. * we take the encryption data from the internal stuff rather
  313. * than what the user has passed us ... as we have to match
  314. * exactly for some strange reason
  315. */
  316. objstr = OBJ_nid2sn(EVP_CIPHER_nid(xi->enc_cipher.cipher));
  317. if (objstr == NULL) {
  318. OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_CIPHER);
  319. goto err;
  320. }
  321. /* create the right magic header stuff */
  322. assert(strlen(objstr) + 23 + 2 * iv_len + 13 <= sizeof buf);
  323. buf[0] = '\0';
  324. PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
  325. PEM_dek_info(buf, objstr, iv_len, (char *)iv);
  326. /* use the normal code to write things out */
  327. i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
  328. if (i <= 0)
  329. goto err;
  330. } else {
  331. /* Add DSA/DH */
  332. /* normal optionally encrypted stuff */
  333. if (PEM_write_bio_RSAPrivateKey(bp,
  334. xi->x_pkey->dec_pkey->pkey.rsa,
  335. enc, kstr, klen, cb, u) <= 0)
  336. goto err;
  337. }
  338. }
  339. /* if we have a certificate then write it out now */
  340. if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0))
  341. goto err;
  342. /*
  343. * we are ignoring anything else that is loaded into the X509_INFO
  344. * structure for the moment ... as I don't need it so I'm not coding it
  345. * here and Eric can do it when this makes it into the base library --tjh
  346. */
  347. ret = 1;
  348. err:
  349. OPENSSL_cleanse((char *)&ctx, sizeof(ctx));
  350. OPENSSL_cleanse(buf, PEM_BUFSIZE);
  351. return (ret);
  352. }