asn_mime.c 28 KB

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