A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

958 lines
27 KiB

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