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.
 
 
 
 
 
 

362 regels
12 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 = BIO_new_fp(fp, BIO_NOCLOSE);
  75. if (b == NULL) {
  76. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  77. return 0;
  78. }
  79. STACK_OF(X509_INFO) *ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
  80. BIO_free(b);
  81. return ret;
  82. }
  83. #endif
  84. enum parse_result_t {
  85. parse_ok,
  86. parse_error,
  87. parse_new_entry,
  88. };
  89. static enum parse_result_t parse_x509(X509_INFO *info, const uint8_t *data,
  90. size_t len, int key_type)
  91. {
  92. if (info->x509 != NULL) {
  93. return parse_new_entry;
  94. }
  95. info->x509 = d2i_X509(NULL, &data, len);
  96. return info->x509 != NULL ? parse_ok : parse_error;
  97. }
  98. static enum parse_result_t parse_x509_aux(X509_INFO *info, const uint8_t *data,
  99. size_t len, int key_type)
  100. {
  101. if (info->x509 != NULL) {
  102. return parse_new_entry;
  103. }
  104. info->x509 = d2i_X509_AUX(NULL, &data, len);
  105. return info->x509 != NULL ? parse_ok : parse_error;
  106. }
  107. static enum parse_result_t parse_crl(X509_INFO *info, const uint8_t *data,
  108. size_t len, int key_type)
  109. {
  110. if (info->crl != NULL) {
  111. return parse_new_entry;
  112. }
  113. info->crl = d2i_X509_CRL(NULL, &data, len);
  114. return info->crl != NULL ? parse_ok : parse_error;
  115. }
  116. static enum parse_result_t parse_key(X509_INFO *info, const uint8_t *data,
  117. size_t len, int key_type)
  118. {
  119. if (info->x_pkey != NULL) {
  120. return parse_new_entry;
  121. }
  122. info->x_pkey = X509_PKEY_new();
  123. if (info->x_pkey == NULL) {
  124. return parse_error;
  125. }
  126. info->x_pkey->dec_pkey = d2i_PrivateKey(key_type, NULL, &data, len);
  127. return info->x_pkey->dec_pkey != NULL ? parse_ok : parse_error;
  128. }
  129. STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
  130. pem_password_cb *cb, void *u)
  131. {
  132. X509_INFO *info = NULL;
  133. char *name = NULL, *header = NULL;
  134. unsigned char *data = NULL;
  135. long len;
  136. int ok = 0;
  137. STACK_OF(X509_INFO) *ret = NULL;
  138. if (sk == NULL) {
  139. ret = sk_X509_INFO_new_null();
  140. if (ret == NULL) {
  141. OPENSSL_PUT_ERROR(PEM, ERR_R_MALLOC_FAILURE);
  142. return NULL;
  143. }
  144. } else {
  145. ret = sk;
  146. }
  147. size_t orig_num = sk_X509_INFO_num(ret);
  148. info = X509_INFO_new();
  149. if (info == NULL) {
  150. goto err;
  151. }
  152. for (;;) {
  153. if (!PEM_read_bio(bp, &name, &header, &data, &len)) {
  154. uint32_t error = ERR_peek_last_error();
  155. if (ERR_GET_LIB(error) == ERR_LIB_PEM &&
  156. ERR_GET_REASON(error) == PEM_R_NO_START_LINE) {
  157. ERR_clear_error();
  158. break;
  159. }
  160. goto err;
  161. }
  162. enum parse_result_t (*parse_function)(X509_INFO *, const uint8_t *,
  163. size_t, int) = NULL;
  164. int key_type = EVP_PKEY_NONE;
  165. if (strcmp(name, PEM_STRING_X509) == 0 ||
  166. strcmp(name, PEM_STRING_X509_OLD) == 0) {
  167. parse_function = parse_x509;
  168. } else if (strcmp(name, PEM_STRING_X509_TRUSTED) == 0) {
  169. parse_function = parse_x509_aux;
  170. } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {
  171. parse_function = parse_crl;
  172. } else if (strcmp(name, PEM_STRING_RSA) == 0) {
  173. parse_function = parse_key;
  174. key_type = EVP_PKEY_RSA;
  175. } else if (strcmp(name, PEM_STRING_DSA) == 0) {
  176. parse_function = parse_key;
  177. key_type = EVP_PKEY_DSA;
  178. } else if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {
  179. parse_function = parse_key;
  180. key_type = EVP_PKEY_EC;
  181. }
  182. /* If a private key has a header, assume it is encrypted. */
  183. if (key_type != EVP_PKEY_NONE && strlen(header) > 10) {
  184. if (info->x_pkey != NULL) {
  185. if (!sk_X509_INFO_push(ret, info)) {
  186. goto err;
  187. }
  188. info = X509_INFO_new();
  189. if (info == NULL) {
  190. goto err;
  191. }
  192. }
  193. /* Historically, raw entries pushed an empty key. */
  194. info->x_pkey = X509_PKEY_new();
  195. if (info->x_pkey == NULL ||
  196. !PEM_get_EVP_CIPHER_INFO(header, &info->enc_cipher)) {
  197. goto err;
  198. }
  199. info->enc_data = (char *)data;
  200. info->enc_len = (int)len;
  201. data = NULL;
  202. } else if (parse_function != NULL) {
  203. EVP_CIPHER_INFO cipher;
  204. if (!PEM_get_EVP_CIPHER_INFO(header, &cipher) ||
  205. !PEM_do_header(&cipher, data, &len, cb, u)) {
  206. goto err;
  207. }
  208. enum parse_result_t result =
  209. parse_function(info, data, len, key_type);
  210. if (result == parse_new_entry) {
  211. if (!sk_X509_INFO_push(ret, info)) {
  212. goto err;
  213. }
  214. info = X509_INFO_new();
  215. if (info == NULL) {
  216. goto err;
  217. }
  218. result = parse_function(info, data, len, key_type);
  219. }
  220. if (result != parse_ok) {
  221. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  222. goto err;
  223. }
  224. }
  225. OPENSSL_free(name);
  226. OPENSSL_free(header);
  227. OPENSSL_free(data);
  228. name = NULL;
  229. header = NULL;
  230. data = NULL;
  231. }
  232. /* Push the last entry on the stack if not empty. */
  233. if (info->x509 != NULL || info->crl != NULL ||
  234. info->x_pkey != NULL || info->enc_data != NULL) {
  235. if (!sk_X509_INFO_push(ret, info)) {
  236. goto err;
  237. }
  238. info = NULL;
  239. }
  240. ok = 1;
  241. err:
  242. X509_INFO_free(info);
  243. if (!ok) {
  244. while (sk_X509_INFO_num(ret) > orig_num) {
  245. X509_INFO_free(sk_X509_INFO_pop(ret));
  246. }
  247. if (ret != sk) {
  248. sk_X509_INFO_free(ret);
  249. }
  250. ret = NULL;
  251. }
  252. OPENSSL_free(name);
  253. OPENSSL_free(header);
  254. OPENSSL_free(data);
  255. return ret;
  256. }
  257. /* A TJH addition */
  258. int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
  259. unsigned char *kstr, int klen,
  260. pem_password_cb *cb, void *u)
  261. {
  262. int i, ret = 0;
  263. unsigned char *data = NULL;
  264. const char *objstr = NULL;
  265. char buf[PEM_BUFSIZE];
  266. unsigned char *iv = NULL;
  267. unsigned iv_len = 0;
  268. if (enc != NULL) {
  269. iv_len = EVP_CIPHER_iv_length(enc);
  270. objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
  271. if (objstr == NULL) {
  272. OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_CIPHER);
  273. goto err;
  274. }
  275. }
  276. /*
  277. * now for the fun part ... if we have a private key then we have to be
  278. * able to handle a not-yet-decrypted key being written out correctly ...
  279. * if it is decrypted or it is non-encrypted then we use the base code
  280. */
  281. if (xi->x_pkey != NULL) {
  282. if ((xi->enc_data != NULL) && (xi->enc_len > 0)) {
  283. if (enc == NULL) {
  284. OPENSSL_PUT_ERROR(PEM, PEM_R_CIPHER_IS_NULL);
  285. goto err;
  286. }
  287. /* copy from weirdo names into more normal things */
  288. iv = xi->enc_cipher.iv;
  289. data = (unsigned char *)xi->enc_data;
  290. i = xi->enc_len;
  291. /*
  292. * we take the encryption data from the internal stuff rather
  293. * than what the user has passed us ... as we have to match
  294. * exactly for some strange reason
  295. */
  296. objstr = OBJ_nid2sn(EVP_CIPHER_nid(xi->enc_cipher.cipher));
  297. if (objstr == NULL) {
  298. OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_CIPHER);
  299. goto err;
  300. }
  301. /* create the right magic header stuff */
  302. assert(strlen(objstr) + 23 + 2 * iv_len + 13 <= sizeof buf);
  303. buf[0] = '\0';
  304. PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
  305. PEM_dek_info(buf, objstr, iv_len, (char *)iv);
  306. /* use the normal code to write things out */
  307. i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
  308. if (i <= 0)
  309. goto err;
  310. } else {
  311. /* Add DSA/DH */
  312. /* normal optionally encrypted stuff */
  313. if (PEM_write_bio_RSAPrivateKey(bp,
  314. xi->x_pkey->dec_pkey->pkey.rsa,
  315. enc, kstr, klen, cb, u) <= 0)
  316. goto err;
  317. }
  318. }
  319. /* if we have a certificate then write it out now */
  320. if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0))
  321. goto err;
  322. /*
  323. * we are ignoring anything else that is loaded into the X509_INFO
  324. * structure for the moment ... as I don't need it so I'm not coding it
  325. * here and Eric can do it when this makes it into the base library --tjh
  326. */
  327. ret = 1;
  328. err:
  329. OPENSSL_cleanse(buf, PEM_BUFSIZE);
  330. return ret;
  331. }