Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

405 řádky
11 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, pem_password_cb *cb, void *u)
  72. {
  73. BIO *b;
  74. STACK_OF(X509_INFO) *ret;
  75. if ((b=BIO_new(BIO_s_file())) == NULL)
  76. {
  77. OPENSSL_PUT_ERROR(PEM, PEM_X509_INFO_read, 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, pem_password_cb *cb, void *u)
  87. {
  88. X509_INFO *xi=NULL;
  89. char *name=NULL,*header=NULL;
  90. void *pp;
  91. unsigned char *data=NULL;
  92. const unsigned char *p;
  93. long len,error=0;
  94. int ok=0;
  95. STACK_OF(X509_INFO) *ret=NULL;
  96. unsigned int i,raw,ptype;
  97. d2i_of_void *d2i = 0;
  98. if (sk == NULL)
  99. {
  100. if ((ret=sk_X509_INFO_new_null()) == NULL)
  101. {
  102. OPENSSL_PUT_ERROR(PEM, PEM_X509_INFO_read_bio, ERR_R_MALLOC_FAILURE);
  103. goto err;
  104. }
  105. }
  106. else
  107. ret=sk;
  108. if ((xi=X509_INFO_new()) == NULL) goto err;
  109. for (;;)
  110. {
  111. raw=0;
  112. ptype = 0;
  113. i=PEM_read_bio(bp,&name,&header,&data,&len);
  114. if (i == 0)
  115. {
  116. error=ERR_GET_REASON(ERR_peek_last_error());
  117. if (error == PEM_R_NO_START_LINE)
  118. {
  119. ERR_clear_error();
  120. break;
  121. }
  122. goto err;
  123. }
  124. start:
  125. if ( (strcmp(name,PEM_STRING_X509) == 0) ||
  126. (strcmp(name,PEM_STRING_X509_OLD) == 0))
  127. {
  128. d2i=(D2I_OF(void))d2i_X509;
  129. if (xi->x509 != NULL)
  130. {
  131. if (!sk_X509_INFO_push(ret,xi)) goto err;
  132. if ((xi=X509_INFO_new()) == NULL) goto err;
  133. goto start;
  134. }
  135. pp=&(xi->x509);
  136. }
  137. else if ((strcmp(name,PEM_STRING_X509_TRUSTED) == 0))
  138. {
  139. d2i=(D2I_OF(void))d2i_X509_AUX;
  140. if (xi->x509 != NULL)
  141. {
  142. if (!sk_X509_INFO_push(ret,xi)) goto err;
  143. if ((xi=X509_INFO_new()) == NULL) goto err;
  144. goto start;
  145. }
  146. pp=&(xi->x509);
  147. }
  148. else if (strcmp(name,PEM_STRING_X509_CRL) == 0)
  149. {
  150. d2i=(D2I_OF(void))d2i_X509_CRL;
  151. if (xi->crl != NULL)
  152. {
  153. if (!sk_X509_INFO_push(ret,xi)) goto err;
  154. if ((xi=X509_INFO_new()) == NULL) goto err;
  155. goto start;
  156. }
  157. pp=&(xi->crl);
  158. }
  159. else
  160. if (strcmp(name,PEM_STRING_RSA) == 0)
  161. {
  162. d2i=(D2I_OF(void))d2i_RSAPrivateKey;
  163. if (xi->x_pkey != NULL)
  164. {
  165. if (!sk_X509_INFO_push(ret,xi)) goto err;
  166. if ((xi=X509_INFO_new()) == NULL) goto err;
  167. goto start;
  168. }
  169. xi->enc_data=NULL;
  170. xi->enc_len=0;
  171. xi->x_pkey=X509_PKEY_new();
  172. ptype=EVP_PKEY_RSA;
  173. pp=&xi->x_pkey->dec_pkey;
  174. if ((int)strlen(header) > 10) /* assume encrypted */
  175. raw=1;
  176. }
  177. else
  178. #ifndef OPENSSL_NO_DSA
  179. if (strcmp(name,PEM_STRING_DSA) == 0)
  180. {
  181. d2i=(D2I_OF(void))d2i_DSAPrivateKey;
  182. if (xi->x_pkey != NULL)
  183. {
  184. if (!sk_X509_INFO_push(ret,xi)) goto err;
  185. if ((xi=X509_INFO_new()) == NULL) goto err;
  186. goto start;
  187. }
  188. xi->enc_data=NULL;
  189. xi->enc_len=0;
  190. xi->x_pkey=X509_PKEY_new();
  191. ptype = EVP_PKEY_DSA;
  192. pp=&xi->x_pkey->dec_pkey;
  193. if ((int)strlen(header) > 10) /* assume encrypted */
  194. raw=1;
  195. }
  196. else
  197. #endif
  198. if (strcmp(name,PEM_STRING_ECPRIVATEKEY) == 0)
  199. {
  200. d2i=(D2I_OF(void))d2i_ECPrivateKey;
  201. if (xi->x_pkey != NULL)
  202. {
  203. if (!sk_X509_INFO_push(ret,xi)) goto err;
  204. if ((xi=X509_INFO_new()) == NULL) goto err;
  205. goto start;
  206. }
  207. xi->enc_data=NULL;
  208. xi->enc_len=0;
  209. xi->x_pkey=X509_PKEY_new();
  210. ptype = EVP_PKEY_EC;
  211. pp=&xi->x_pkey->dec_pkey;
  212. if ((int)strlen(header) > 10) /* assume encrypted */
  213. raw=1;
  214. }
  215. else
  216. {
  217. d2i=NULL;
  218. pp=NULL;
  219. }
  220. if (d2i != NULL)
  221. {
  222. if (!raw)
  223. {
  224. EVP_CIPHER_INFO cipher;
  225. if (!PEM_get_EVP_CIPHER_INFO(header,&cipher))
  226. goto err;
  227. if (!PEM_do_header(&cipher,data,&len,cb,u))
  228. goto err;
  229. p=data;
  230. if (ptype)
  231. {
  232. if (!d2i_PrivateKey(ptype, pp, &p, len))
  233. {
  234. OPENSSL_PUT_ERROR(PEM, PEM_X509_INFO_read_bio, ERR_R_ASN1_LIB);
  235. goto err;
  236. }
  237. }
  238. else if (d2i(pp,&p,len) == NULL)
  239. {
  240. OPENSSL_PUT_ERROR(PEM, PEM_X509_INFO_read_bio, ERR_R_ASN1_LIB);
  241. goto err;
  242. }
  243. }
  244. else
  245. { /* encrypted RSA data */
  246. if (!PEM_get_EVP_CIPHER_INFO(header,
  247. &xi->enc_cipher)) goto err;
  248. xi->enc_data=(char *)data;
  249. xi->enc_len=(int)len;
  250. data=NULL;
  251. }
  252. }
  253. else {
  254. /* unknown */
  255. }
  256. if (name != NULL) OPENSSL_free(name);
  257. if (header != NULL) OPENSSL_free(header);
  258. if (data != NULL) OPENSSL_free(data);
  259. name=NULL;
  260. header=NULL;
  261. data=NULL;
  262. }
  263. /* if the last one hasn't been pushed yet and there is anything
  264. * in it then add it to the stack ...
  265. */
  266. if ((xi->x509 != NULL) || (xi->crl != NULL) ||
  267. (xi->x_pkey != NULL) || (xi->enc_data != NULL))
  268. {
  269. if (!sk_X509_INFO_push(ret,xi)) goto err;
  270. xi=NULL;
  271. }
  272. ok=1;
  273. err:
  274. if (xi != NULL) X509_INFO_free(xi);
  275. if (!ok)
  276. {
  277. for (i=0; ((int)i)<sk_X509_INFO_num(ret); i++)
  278. {
  279. xi=sk_X509_INFO_value(ret,i);
  280. X509_INFO_free(xi);
  281. }
  282. if (ret != sk) sk_X509_INFO_free(ret);
  283. ret=NULL;
  284. }
  285. if (name != NULL) OPENSSL_free(name);
  286. if (header != NULL) OPENSSL_free(header);
  287. if (data != NULL) OPENSSL_free(data);
  288. return(ret);
  289. }
  290. /* A TJH addition */
  291. int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
  292. unsigned char *kstr, int klen, pem_password_cb *cb, void *u)
  293. {
  294. EVP_CIPHER_CTX ctx;
  295. int i,ret=0;
  296. unsigned char *data=NULL;
  297. const char *objstr=NULL;
  298. char buf[PEM_BUFSIZE];
  299. unsigned char *iv=NULL;
  300. unsigned iv_len = 0;
  301. if (enc != NULL)
  302. {
  303. iv_len = EVP_CIPHER_iv_length(enc);
  304. objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc));
  305. if (objstr == NULL)
  306. {
  307. OPENSSL_PUT_ERROR(PEM, PEM_X509_INFO_write_bio, PEM_R_UNSUPPORTED_CIPHER);
  308. goto err;
  309. }
  310. }
  311. /* now for the fun part ... if we have a private key then
  312. * we have to be able to handle a not-yet-decrypted key
  313. * being written out correctly ... if it is decrypted or
  314. * it is non-encrypted then we use the base code
  315. */
  316. if (xi->x_pkey!=NULL)
  317. {
  318. if ( (xi->enc_data!=NULL) && (xi->enc_len>0) )
  319. {
  320. if (enc == NULL)
  321. {
  322. OPENSSL_PUT_ERROR(PEM, PEM_X509_INFO_write_bio, PEM_R_CIPHER_IS_NULL);
  323. goto err;
  324. }
  325. /* copy from weirdo names into more normal things */
  326. iv=xi->enc_cipher.iv;
  327. data=(unsigned char *)xi->enc_data;
  328. i=xi->enc_len;
  329. /* we take the encryption data from the
  330. * internal stuff rather than what the
  331. * user has passed us ... as we have to
  332. * match exactly for some strange reason
  333. */
  334. objstr=OBJ_nid2sn(
  335. EVP_CIPHER_nid(xi->enc_cipher.cipher));
  336. if (objstr == NULL)
  337. {
  338. OPENSSL_PUT_ERROR(PEM, PEM_X509_INFO_write_bio, PEM_R_UNSUPPORTED_CIPHER);
  339. goto err;
  340. }
  341. /* create the right magic header stuff */
  342. assert(strlen(objstr)+23+2*iv_len+13 <= sizeof buf);
  343. buf[0]='\0';
  344. PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
  345. PEM_dek_info(buf,objstr,iv_len,(char *)iv);
  346. /* use the normal code to write things out */
  347. i=PEM_write_bio(bp,PEM_STRING_RSA,buf,data,i);
  348. if (i <= 0) goto err;
  349. }
  350. else
  351. {
  352. /* Add DSA/DH */
  353. /* normal optionally encrypted stuff */
  354. if (PEM_write_bio_RSAPrivateKey(bp,
  355. xi->x_pkey->dec_pkey->pkey.rsa,
  356. enc,kstr,klen,cb,u)<=0)
  357. goto err;
  358. }
  359. }
  360. /* if we have a certificate then write it out now */
  361. if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp,xi->x509) <= 0))
  362. goto err;
  363. /* we are ignoring anything else that is loaded into the X509_INFO
  364. * structure for the moment ... as I don't need it so I'm not
  365. * coding it here and Eric can do it when this makes it into the
  366. * base library --tjh
  367. */
  368. ret=1;
  369. err:
  370. OPENSSL_cleanse((char *)&ctx,sizeof(ctx));
  371. OPENSSL_cleanse(buf,PEM_BUFSIZE);
  372. return(ret);
  373. }