asn_mime.c 30 KB

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