2
0

asn_mime.c 28 KB

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