2
0

asn_mime.c 29 KB

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