asn_mime.c 30 KB

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