asn_mime.c 26 KB

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