asn_mime.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /* asn_mime.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2008 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. */
  54. #include <stdio.h>
  55. #include <ctype.h>
  56. #include "cryptlib.h"
  57. #include <openssl/rand.h>
  58. #include <openssl/x509.h>
  59. #include <openssl/asn1.h>
  60. #include <openssl/asn1t.h>
  61. #include "asn1_locl.h"
  62. /* Generalised MIME like utilities for streaming ASN1. Although many
  63. * have a PKCS7/CMS like flavour others are more general purpose.
  64. */
  65. /* MIME format structures
  66. * Note that all are translated to lower case apart from
  67. * parameter values. Quotes are stripped off
  68. */
  69. typedef struct {
  70. char *param_name; /* Param name e.g. "micalg" */
  71. char *param_value; /* Param value e.g. "sha1" */
  72. } MIME_PARAM;
  73. DECLARE_STACK_OF(MIME_PARAM)
  74. IMPLEMENT_STACK_OF(MIME_PARAM)
  75. typedef struct {
  76. char *name; /* Name of line e.g. "content-type" */
  77. char *value; /* Value of line e.g. "text/plain" */
  78. STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
  79. } MIME_HEADER;
  80. DECLARE_STACK_OF(MIME_HEADER)
  81. IMPLEMENT_STACK_OF(MIME_HEADER)
  82. static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
  83. const ASN1_ITEM *it);
  84. static char * strip_ends(char *name);
  85. static char * strip_start(char *name);
  86. static char * strip_end(char *name);
  87. static MIME_HEADER *mime_hdr_new(char *name, char *value);
  88. static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
  89. static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
  90. static int mime_hdr_cmp(const MIME_HEADER * const *a,
  91. const MIME_HEADER * const *b);
  92. static int mime_param_cmp(const MIME_PARAM * const *a,
  93. const MIME_PARAM * const *b);
  94. static void mime_param_free(MIME_PARAM *param);
  95. static int mime_bound_check(char *line, int linelen, char *bound, int blen);
  96. static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
  97. static int strip_eol(char *linebuf, int *plen);
  98. static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name);
  99. static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name);
  100. static void mime_hdr_free(MIME_HEADER *hdr);
  101. #define MAX_SMLEN 1024
  102. #define mime_debug(x) /* x */
  103. /* Output an ASN1 structure in BER format streaming if necessary */
  104. int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
  105. const ASN1_ITEM *it)
  106. {
  107. /* If streaming create stream BIO and copy all content through it */
  108. if (flags & SMIME_STREAM)
  109. {
  110. BIO *bio, *tbio;
  111. bio = BIO_new_NDEF(out, val, it);
  112. if (!bio)
  113. {
  114. ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM,ERR_R_MALLOC_FAILURE);
  115. return 0;
  116. }
  117. SMIME_crlf_copy(in, bio, flags);
  118. (void)BIO_flush(bio);
  119. /* Free up successive BIOs until we hit the old output BIO */
  120. do
  121. {
  122. tbio = BIO_pop(bio);
  123. BIO_free(bio);
  124. bio = tbio;
  125. } while (bio != out);
  126. }
  127. /* else just write out ASN1 structure which will have all content
  128. * stored internally
  129. */
  130. else
  131. ASN1_item_i2d_bio(it, out, val);
  132. return 1;
  133. }
  134. /* Base 64 read and write of ASN1 structure */
  135. static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
  136. const ASN1_ITEM *it)
  137. {
  138. BIO *b64;
  139. int r;
  140. b64 = BIO_new(BIO_f_base64());
  141. if(!b64)
  142. {
  143. ASN1err(ASN1_F_B64_WRITE_ASN1,ERR_R_MALLOC_FAILURE);
  144. return 0;
  145. }
  146. /* prepend the b64 BIO so all data is base64 encoded.
  147. */
  148. out = BIO_push(b64, out);
  149. r = i2d_ASN1_bio_stream(out, val, in, flags, it);
  150. (void)BIO_flush(out);
  151. BIO_pop(out);
  152. BIO_free(b64);
  153. return r;
  154. }
  155. /* Streaming ASN1 PEM write */
  156. int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
  157. const char *hdr,
  158. const ASN1_ITEM *it)
  159. {
  160. int r;
  161. BIO_printf(out, "-----BEGIN %s-----\n", hdr);
  162. r = B64_write_ASN1(out, val, in, flags, it);
  163. BIO_printf(out, "-----END %s-----\n", hdr);
  164. return r;
  165. }
  166. static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
  167. {
  168. BIO *b64;
  169. ASN1_VALUE *val;
  170. if(!(b64 = BIO_new(BIO_f_base64()))) {
  171. ASN1err(ASN1_F_B64_READ_ASN1,ERR_R_MALLOC_FAILURE);
  172. return 0;
  173. }
  174. bio = BIO_push(b64, bio);
  175. val = ASN1_item_d2i_bio(it, bio, NULL);
  176. if(!val)
  177. ASN1err(ASN1_F_B64_READ_ASN1,ASN1_R_DECODE_ERROR);
  178. (void)BIO_flush(bio);
  179. bio = BIO_pop(bio);
  180. BIO_free(b64);
  181. return val;
  182. }
  183. /* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
  184. static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
  185. {
  186. const EVP_MD *md;
  187. int i, have_unknown = 0, write_comma, ret = 0, md_nid;
  188. have_unknown = 0;
  189. write_comma = 0;
  190. for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++)
  191. {
  192. if (write_comma)
  193. BIO_write(out, ",", 1);
  194. write_comma = 1;
  195. md_nid = OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs, i)->algorithm);
  196. md = EVP_get_digestbynid(md_nid);
  197. if (md && md->md_ctrl)
  198. {
  199. int rv;
  200. char *micstr;
  201. rv = md->md_ctrl(NULL, EVP_MD_CTRL_MICALG, 0, &micstr);
  202. if (rv > 0)
  203. {
  204. BIO_puts(out, micstr);
  205. OPENSSL_free(micstr);
  206. continue;
  207. }
  208. if (rv != -2)
  209. goto err;
  210. }
  211. switch(md_nid)
  212. {
  213. case NID_sha1:
  214. BIO_puts(out, "sha1");
  215. break;
  216. case NID_md5:
  217. BIO_puts(out, "md5");
  218. break;
  219. case NID_sha256:
  220. BIO_puts(out, "sha-256");
  221. break;
  222. case NID_sha384:
  223. BIO_puts(out, "sha-384");
  224. break;
  225. case NID_sha512:
  226. BIO_puts(out, "sha-512");
  227. break;
  228. case NID_id_GostR3411_94:
  229. BIO_puts(out, "gostr3411-94");
  230. goto err;
  231. break;
  232. default:
  233. if (have_unknown)
  234. write_comma = 0;
  235. else
  236. {
  237. BIO_puts(out, "unknown");
  238. have_unknown = 1;
  239. }
  240. break;
  241. }
  242. }
  243. ret = 1;
  244. err:
  245. return ret;
  246. }
  247. /* SMIME sender */
  248. int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
  249. int ctype_nid, int econt_nid,
  250. STACK_OF(X509_ALGOR) *mdalgs,
  251. const ASN1_ITEM *it)
  252. {
  253. char bound[33], c;
  254. int i;
  255. const char *mime_prefix, *mime_eol, *cname = "smime.p7m";
  256. const char *msg_type=NULL;
  257. if (flags & SMIME_OLDMIME)
  258. mime_prefix = "application/x-pkcs7-";
  259. else
  260. mime_prefix = "application/pkcs7-";
  261. if (flags & SMIME_CRLFEOL)
  262. mime_eol = "\r\n";
  263. else
  264. mime_eol = "\n";
  265. if((flags & SMIME_DETACHED) && data) {
  266. /* We want multipart/signed */
  267. /* Generate a random boundary */
  268. RAND_pseudo_bytes((unsigned char *)bound, 32);
  269. for(i = 0; i < 32; i++) {
  270. c = bound[i] & 0xf;
  271. if(c < 10) c += '0';
  272. else c += 'A' - 10;
  273. bound[i] = c;
  274. }
  275. bound[32] = 0;
  276. BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
  277. BIO_printf(bio, "Content-Type: multipart/signed;");
  278. BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
  279. BIO_puts(bio, " micalg=\"");
  280. asn1_write_micalg(bio, mdalgs);
  281. BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
  282. bound, mime_eol, mime_eol);
  283. BIO_printf(bio, "This is an S/MIME signed message%s%s",
  284. mime_eol, mime_eol);
  285. /* Now write out the first part */
  286. BIO_printf(bio, "------%s%s", bound, mime_eol);
  287. if (!asn1_output_data(bio, data, val, flags, it))
  288. return 0;
  289. BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
  290. /* Headers for signature */
  291. BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
  292. BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
  293. BIO_printf(bio, "Content-Transfer-Encoding: base64%s",
  294. mime_eol);
  295. BIO_printf(bio, "Content-Disposition: attachment;");
  296. BIO_printf(bio, " filename=\"smime.p7s\"%s%s",
  297. mime_eol, mime_eol);
  298. B64_write_ASN1(bio, val, NULL, 0, it);
  299. BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound,
  300. mime_eol, mime_eol);
  301. return 1;
  302. }
  303. /* Determine smime-type header */
  304. if (ctype_nid == NID_pkcs7_enveloped)
  305. msg_type = "enveloped-data";
  306. else if (ctype_nid == NID_pkcs7_signed)
  307. {
  308. if (econt_nid == NID_id_smime_ct_receipt)
  309. msg_type = "signed-receipt";
  310. else if (sk_X509_ALGOR_num(mdalgs) >= 0)
  311. msg_type = "signed-data";
  312. else
  313. msg_type = "certs-only";
  314. }
  315. else if (ctype_nid == NID_id_smime_ct_compressedData)
  316. {
  317. msg_type = "compressed-data";
  318. cname = "smime.p7z";
  319. }
  320. /* MIME headers */
  321. BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
  322. BIO_printf(bio, "Content-Disposition: attachment;");
  323. BIO_printf(bio, " filename=\"%s\"%s", cname, mime_eol);
  324. BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
  325. if (msg_type)
  326. BIO_printf(bio, " smime-type=%s;", msg_type);
  327. BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol);
  328. BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
  329. mime_eol, mime_eol);
  330. if (!B64_write_ASN1(bio, val, data, flags, it))
  331. return 0;
  332. BIO_printf(bio, "%s", mime_eol);
  333. return 1;
  334. }
  335. /* Handle output of ASN1 data */
  336. static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
  337. const ASN1_ITEM *it)
  338. {
  339. BIO *tmpbio;
  340. const ASN1_AUX *aux = it->funcs;
  341. ASN1_STREAM_ARG sarg;
  342. if (!(flags & SMIME_DETACHED))
  343. {
  344. SMIME_crlf_copy(data, out, flags);
  345. return 1;
  346. }
  347. if (!aux || !aux->asn1_cb)
  348. {
  349. ASN1err(ASN1_F_ASN1_OUTPUT_DATA,
  350. ASN1_R_STREAMING_NOT_SUPPORTED);
  351. return 0;
  352. }
  353. sarg.out = out;
  354. sarg.ndef_bio = NULL;
  355. sarg.boundary = NULL;
  356. /* Let ASN1 code prepend any needed BIOs */
  357. if (aux->asn1_cb(ASN1_OP_DETACHED_PRE, &val, it, &sarg) <= 0)
  358. return 0;
  359. /* Copy data across, passing through filter BIOs for processing */
  360. SMIME_crlf_copy(data, sarg.ndef_bio, flags);
  361. /* Finalize structure */
  362. if (aux->asn1_cb(ASN1_OP_DETACHED_POST, &val, it, &sarg) <= 0)
  363. return 0;
  364. /* Now remove any digests prepended to the BIO */
  365. while (sarg.ndef_bio != out)
  366. {
  367. tmpbio = BIO_pop(sarg.ndef_bio);
  368. BIO_free(sarg.ndef_bio);
  369. sarg.ndef_bio = tmpbio;
  370. }
  371. return 1;
  372. }
  373. /* SMIME reader: handle multipart/signed and opaque signing.
  374. * in multipart case the content is placed in a memory BIO
  375. * pointed to by "bcont". In opaque this is set to NULL
  376. */
  377. ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
  378. {
  379. BIO *asnin;
  380. STACK_OF(MIME_HEADER) *headers = NULL;
  381. STACK_OF(BIO) *parts = NULL;
  382. MIME_HEADER *hdr;
  383. MIME_PARAM *prm;
  384. ASN1_VALUE *val;
  385. int ret;
  386. if(bcont) *bcont = NULL;
  387. if (!(headers = mime_parse_hdr(bio))) {
  388. ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_PARSE_ERROR);
  389. return NULL;
  390. }
  391. if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
  392. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  393. ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE);
  394. return NULL;
  395. }
  396. /* Handle multipart/signed */
  397. if(!strcmp(hdr->value, "multipart/signed")) {
  398. /* Split into two parts */
  399. prm = mime_param_find(hdr, "boundary");
  400. if(!prm || !prm->param_value) {
  401. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  402. ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY);
  403. return NULL;
  404. }
  405. ret = multi_split(bio, prm->param_value, &parts);
  406. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  407. if(!ret || (sk_BIO_num(parts) != 2) ) {
  408. ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE);
  409. sk_BIO_pop_free(parts, BIO_vfree);
  410. return NULL;
  411. }
  412. /* Parse the signature piece */
  413. asnin = sk_BIO_value(parts, 1);
  414. if (!(headers = mime_parse_hdr(asnin))) {
  415. ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_SIG_PARSE_ERROR);
  416. sk_BIO_pop_free(parts, BIO_vfree);
  417. return NULL;
  418. }
  419. /* Get content type */
  420. if(!(hdr = mime_hdr_find(headers, "content-type")) ||
  421. !hdr->value) {
  422. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  423. ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE);
  424. return NULL;
  425. }
  426. if(strcmp(hdr->value, "application/x-pkcs7-signature") &&
  427. strcmp(hdr->value, "application/pkcs7-signature")) {
  428. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  429. ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_SIG_INVALID_MIME_TYPE);
  430. ERR_add_error_data(2, "type: ", hdr->value);
  431. sk_BIO_pop_free(parts, BIO_vfree);
  432. return NULL;
  433. }
  434. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  435. /* Read in ASN1 */
  436. if(!(val = b64_read_asn1(asnin, it))) {
  437. ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_ASN1_SIG_PARSE_ERROR);
  438. sk_BIO_pop_free(parts, BIO_vfree);
  439. return NULL;
  440. }
  441. if(bcont) {
  442. *bcont = sk_BIO_value(parts, 0);
  443. BIO_free(asnin);
  444. sk_BIO_free(parts);
  445. } else sk_BIO_pop_free(parts, BIO_vfree);
  446. return val;
  447. }
  448. /* OK, if not multipart/signed try opaque signature */
  449. if (strcmp (hdr->value, "application/x-pkcs7-mime") &&
  450. strcmp (hdr->value, "application/pkcs7-mime")) {
  451. ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_INVALID_MIME_TYPE);
  452. ERR_add_error_data(2, "type: ", hdr->value);
  453. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  454. return NULL;
  455. }
  456. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  457. if(!(val = b64_read_asn1(bio, it))) {
  458. ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR);
  459. return NULL;
  460. }
  461. return val;
  462. }
  463. /* Copy text from one BIO to another making the output CRLF at EOL */
  464. int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
  465. {
  466. BIO *bf;
  467. char eol;
  468. int len;
  469. char linebuf[MAX_SMLEN];
  470. /* Buffer output so we don't write one line at a time. This is
  471. * useful when streaming as we don't end up with one OCTET STRING
  472. * per line.
  473. */
  474. bf = BIO_new(BIO_f_buffer());
  475. if (!bf)
  476. return 0;
  477. out = BIO_push(bf, out);
  478. if(flags & SMIME_BINARY)
  479. {
  480. while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
  481. BIO_write(out, linebuf, len);
  482. }
  483. else
  484. {
  485. if(flags & SMIME_TEXT)
  486. BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
  487. while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0)
  488. {
  489. eol = strip_eol(linebuf, &len);
  490. if (len)
  491. BIO_write(out, linebuf, len);
  492. if(eol) BIO_write(out, "\r\n", 2);
  493. }
  494. }
  495. (void)BIO_flush(out);
  496. BIO_pop(out);
  497. BIO_free(bf);
  498. return 1;
  499. }
  500. /* Strip off headers if they are text/plain */
  501. int SMIME_text(BIO *in, BIO *out)
  502. {
  503. char iobuf[4096];
  504. int len;
  505. STACK_OF(MIME_HEADER) *headers;
  506. MIME_HEADER *hdr;
  507. if (!(headers = mime_parse_hdr(in))) {
  508. ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_PARSE_ERROR);
  509. return 0;
  510. }
  511. if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
  512. ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_NO_CONTENT_TYPE);
  513. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  514. return 0;
  515. }
  516. if (strcmp (hdr->value, "text/plain")) {
  517. ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_INVALID_MIME_TYPE);
  518. ERR_add_error_data(2, "type: ", hdr->value);
  519. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  520. return 0;
  521. }
  522. sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
  523. while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
  524. BIO_write(out, iobuf, len);
  525. if (len < 0)
  526. return 0;
  527. return 1;
  528. }
  529. /* Split a multipart/XXX message body into component parts: result is
  530. * canonical parts in a STACK of bios
  531. */
  532. static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
  533. {
  534. char linebuf[MAX_SMLEN];
  535. int len, blen;
  536. int eol = 0, next_eol = 0;
  537. BIO *bpart = NULL;
  538. STACK_OF(BIO) *parts;
  539. char state, part, first;
  540. blen = strlen(bound);
  541. part = 0;
  542. state = 0;
  543. first = 1;
  544. parts = sk_BIO_new_null();
  545. *ret = parts;
  546. while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
  547. state = mime_bound_check(linebuf, len, bound, blen);
  548. if(state == 1) {
  549. first = 1;
  550. part++;
  551. } else if(state == 2) {
  552. sk_BIO_push(parts, bpart);
  553. return 1;
  554. } else if(part) {
  555. /* Strip CR+LF from linebuf */
  556. next_eol = strip_eol(linebuf, &len);
  557. if(first) {
  558. first = 0;
  559. if(bpart) sk_BIO_push(parts, bpart);
  560. bpart = BIO_new(BIO_s_mem());
  561. BIO_set_mem_eof_return(bpart, 0);
  562. } else if (eol)
  563. BIO_write(bpart, "\r\n", 2);
  564. eol = next_eol;
  565. if (len)
  566. BIO_write(bpart, linebuf, len);
  567. }
  568. }
  569. return 0;
  570. }
  571. /* This is the big one: parse MIME header lines up to message body */
  572. #define MIME_INVALID 0
  573. #define MIME_START 1
  574. #define MIME_TYPE 2
  575. #define MIME_NAME 3
  576. #define MIME_VALUE 4
  577. #define MIME_QUOTE 5
  578. #define MIME_COMMENT 6
  579. static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
  580. {
  581. char *p, *q, c;
  582. char *ntmp;
  583. char linebuf[MAX_SMLEN];
  584. MIME_HEADER *mhdr = NULL;
  585. STACK_OF(MIME_HEADER) *headers;
  586. int len, state, save_state = 0;
  587. headers = sk_MIME_HEADER_new(mime_hdr_cmp);
  588. while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
  589. /* If whitespace at line start then continuation line */
  590. if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME;
  591. else state = MIME_START;
  592. ntmp = NULL;
  593. /* Go through all characters */
  594. for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
  595. /* State machine to handle MIME headers
  596. * if this looks horrible that's because it *is*
  597. */
  598. switch(state) {
  599. case MIME_START:
  600. if(c == ':') {
  601. state = MIME_TYPE;
  602. *p = 0;
  603. ntmp = strip_ends(q);
  604. q = p + 1;
  605. }
  606. break;
  607. case MIME_TYPE:
  608. if(c == ';') {
  609. mime_debug("Found End Value\n");
  610. *p = 0;
  611. mhdr = mime_hdr_new(ntmp, strip_ends(q));
  612. sk_MIME_HEADER_push(headers, mhdr);
  613. ntmp = NULL;
  614. q = p + 1;
  615. state = MIME_NAME;
  616. } else if(c == '(') {
  617. save_state = state;
  618. state = MIME_COMMENT;
  619. }
  620. break;
  621. case MIME_COMMENT:
  622. if(c == ')') {
  623. state = save_state;
  624. }
  625. break;
  626. case MIME_NAME:
  627. if(c == '=') {
  628. state = MIME_VALUE;
  629. *p = 0;
  630. ntmp = strip_ends(q);
  631. q = p + 1;
  632. }
  633. break ;
  634. case MIME_VALUE:
  635. if(c == ';') {
  636. state = MIME_NAME;
  637. *p = 0;
  638. mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
  639. ntmp = NULL;
  640. q = p + 1;
  641. } else if (c == '"') {
  642. mime_debug("Found Quote\n");
  643. state = MIME_QUOTE;
  644. } else if(c == '(') {
  645. save_state = state;
  646. state = MIME_COMMENT;
  647. }
  648. break;
  649. case MIME_QUOTE:
  650. if(c == '"') {
  651. mime_debug("Found Match Quote\n");
  652. state = MIME_VALUE;
  653. }
  654. break;
  655. }
  656. }
  657. if(state == MIME_TYPE) {
  658. mhdr = mime_hdr_new(ntmp, strip_ends(q));
  659. sk_MIME_HEADER_push(headers, mhdr);
  660. } else if(state == MIME_VALUE)
  661. mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
  662. if(p == linebuf) break; /* Blank line means end of headers */
  663. }
  664. return headers;
  665. }
  666. static char *strip_ends(char *name)
  667. {
  668. return strip_end(strip_start(name));
  669. }
  670. /* Strip a parameter of whitespace from start of param */
  671. static char *strip_start(char *name)
  672. {
  673. char *p, c;
  674. /* Look for first non white space or quote */
  675. for(p = name; (c = *p) ;p++) {
  676. if(c == '"') {
  677. /* Next char is start of string if non null */
  678. if(p[1]) return p + 1;
  679. /* Else null string */
  680. return NULL;
  681. }
  682. if(!isspace((unsigned char)c)) return p;
  683. }
  684. return NULL;
  685. }
  686. /* As above but strip from end of string : maybe should handle brackets? */
  687. static char *strip_end(char *name)
  688. {
  689. char *p, c;
  690. if(!name) return NULL;
  691. /* Look for first non white space or quote */
  692. for(p = name + strlen(name) - 1; p >= name ;p--) {
  693. c = *p;
  694. if(c == '"') {
  695. if(p - 1 == name) return NULL;
  696. *p = 0;
  697. return name;
  698. }
  699. if(isspace((unsigned char)c)) *p = 0;
  700. else return name;
  701. }
  702. return NULL;
  703. }
  704. static MIME_HEADER *mime_hdr_new(char *name, char *value)
  705. {
  706. MIME_HEADER *mhdr;
  707. char *tmpname, *tmpval, *p;
  708. int c;
  709. if(name) {
  710. if(!(tmpname = BUF_strdup(name))) return NULL;
  711. for(p = tmpname ; *p; p++) {
  712. c = *p;
  713. if(isupper(c)) {
  714. c = tolower(c);
  715. *p = c;
  716. }
  717. }
  718. } else tmpname = NULL;
  719. if(value) {
  720. if(!(tmpval = BUF_strdup(value))) return NULL;
  721. for(p = tmpval ; *p; p++) {
  722. c = *p;
  723. if(isupper(c)) {
  724. c = tolower(c);
  725. *p = c;
  726. }
  727. }
  728. } else tmpval = NULL;
  729. mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER));
  730. if(!mhdr) return NULL;
  731. mhdr->name = tmpname;
  732. mhdr->value = tmpval;
  733. if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL;
  734. return mhdr;
  735. }
  736. static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
  737. {
  738. char *tmpname, *tmpval, *p;
  739. int c;
  740. MIME_PARAM *mparam;
  741. if(name) {
  742. tmpname = BUF_strdup(name);
  743. if(!tmpname) return 0;
  744. for(p = tmpname ; *p; p++) {
  745. c = *p;
  746. if(isupper(c)) {
  747. c = tolower(c);
  748. *p = c;
  749. }
  750. }
  751. } else tmpname = NULL;
  752. if(value) {
  753. tmpval = BUF_strdup(value);
  754. if(!tmpval) return 0;
  755. } else tmpval = NULL;
  756. /* Parameter values are case sensitive so leave as is */
  757. mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM));
  758. if(!mparam) return 0;
  759. mparam->param_name = tmpname;
  760. mparam->param_value = tmpval;
  761. sk_MIME_PARAM_push(mhdr->params, mparam);
  762. return 1;
  763. }
  764. static int mime_hdr_cmp(const MIME_HEADER * const *a,
  765. const MIME_HEADER * const *b)
  766. {
  767. return(strcmp((*a)->name, (*b)->name));
  768. }
  769. static int mime_param_cmp(const MIME_PARAM * const *a,
  770. const MIME_PARAM * const *b)
  771. {
  772. return(strcmp((*a)->param_name, (*b)->param_name));
  773. }
  774. /* Find a header with a given name (if possible) */
  775. static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
  776. {
  777. MIME_HEADER htmp;
  778. int idx;
  779. htmp.name = name;
  780. idx = sk_MIME_HEADER_find(hdrs, &htmp);
  781. if(idx < 0) return NULL;
  782. return sk_MIME_HEADER_value(hdrs, idx);
  783. }
  784. static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name)
  785. {
  786. MIME_PARAM param;
  787. int idx;
  788. param.param_name = name;
  789. idx = sk_MIME_PARAM_find(hdr->params, &param);
  790. if(idx < 0) return NULL;
  791. return sk_MIME_PARAM_value(hdr->params, idx);
  792. }
  793. static void mime_hdr_free(MIME_HEADER *hdr)
  794. {
  795. if(hdr->name) OPENSSL_free(hdr->name);
  796. if(hdr->value) OPENSSL_free(hdr->value);
  797. if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
  798. OPENSSL_free(hdr);
  799. }
  800. static void mime_param_free(MIME_PARAM *param)
  801. {
  802. if(param->param_name) OPENSSL_free(param->param_name);
  803. if(param->param_value) OPENSSL_free(param->param_value);
  804. OPENSSL_free(param);
  805. }
  806. /* Check for a multipart boundary. Returns:
  807. * 0 : no boundary
  808. * 1 : part boundary
  809. * 2 : final boundary
  810. */
  811. static int mime_bound_check(char *line, int linelen, char *bound, int blen)
  812. {
  813. if(linelen == -1) linelen = strlen(line);
  814. if(blen == -1) blen = strlen(bound);
  815. /* Quickly eliminate if line length too short */
  816. if(blen + 2 > linelen) return 0;
  817. /* Check for part boundary */
  818. if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
  819. if(!strncmp(line + blen + 2, "--", 2)) return 2;
  820. else return 1;
  821. }
  822. return 0;
  823. }
  824. static int strip_eol(char *linebuf, int *plen)
  825. {
  826. int len = *plen;
  827. char *p, c;
  828. int is_eol = 0;
  829. p = linebuf + len - 1;
  830. for (p = linebuf + len - 1; len > 0; len--, p--)
  831. {
  832. c = *p;
  833. if (c == '\n')
  834. is_eol = 1;
  835. else if (c != '\r')
  836. break;
  837. }
  838. *plen = len;
  839. return is_eol;
  840. }