1218822Sdim/* asn_mime.c */
2130561Sobrien/*
3218822Sdim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4218822Sdim * project.
5218822Sdim */
6130561Sobrien/* ====================================================================
7218822Sdim * Copyright (c) 1999-2018 The OpenSSL Project.  All rights reserved.
8130561Sobrien *
9218822Sdim * Redistribution and use in source and binary forms, with or without
10218822Sdim * modification, are permitted provided that the following conditions
11218822Sdim * are met:
12218822Sdim *
13218822Sdim * 1. Redistributions of source code must retain the above copyright
14218822Sdim *    notice, this list of conditions and the following disclaimer.
15218822Sdim *
16218822Sdim * 2. Redistributions in binary form must reproduce the above copyright
17218822Sdim *    notice, this list of conditions and the following disclaimer in
18218822Sdim *    the documentation and/or other materials provided with the
19218822Sdim *    distribution.
20218822Sdim *
21218822Sdim * 3. All advertising materials mentioning features or use of this
22218822Sdim *    software must display the following acknowledgment:
23218822Sdim *    "This product includes software developed by the OpenSSL Project
24218822Sdim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25218822Sdim *
26218822Sdim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27218822Sdim *    endorse or promote products derived from this software without
28218822Sdim *    prior written permission. For written permission, please contact
29218822Sdim *    licensing@OpenSSL.org.
30218822Sdim *
31218822Sdim * 5. Products derived from this software may not be called "OpenSSL"
32218822Sdim *    nor may "OpenSSL" appear in their names without prior written
33218822Sdim *    permission of the OpenSSL Project.
34218822Sdim *
35218822Sdim * 6. Redistributions of any form whatsoever must retain the following
36218822Sdim *    acknowledgment:
37218822Sdim *    "This product includes software developed by the OpenSSL Project
38218822Sdim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39218822Sdim *
40218822Sdim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41218822Sdim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42218822Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43218822Sdim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44218822Sdim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45218822Sdim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46218822Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47218822Sdim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48218822Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49218822Sdim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50218822Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51218822Sdim * OF THE POSSIBILITY OF SUCH DAMAGE.
52218822Sdim * ====================================================================
53218822Sdim *
54218822Sdim */
55218822Sdim
56218822Sdim#include <stdio.h>
57218822Sdim#include <ctype.h>
58218822Sdim#include "cryptlib.h"
59218822Sdim#include <openssl/rand.h>
60218822Sdim#include <openssl/x509.h>
61218822Sdim#include <openssl/asn1.h>
62218822Sdim#include <openssl/asn1t.h>
63218822Sdim#include "asn1_locl.h"
64218822Sdim
65218822Sdim/*
66218822Sdim * Generalised MIME like utilities for streaming ASN1. Although many have a
67218822Sdim * PKCS7/CMS like flavour others are more general purpose.
68218822Sdim */
69218822Sdim
70218822Sdim/*
71218822Sdim * MIME format structures Note that all are translated to lower case apart
72218822Sdim * from parameter values. Quotes are stripped off
73218822Sdim */
74218822Sdim
75218822Sdimtypedef struct {
76218822Sdim    char *param_name;           /* Param name e.g. "micalg" */
77218822Sdim    char *param_value;          /* Param value e.g. "sha1" */
78218822Sdim} MIME_PARAM;
79218822Sdim
80218822SdimDECLARE_STACK_OF(MIME_PARAM)
81218822SdimIMPLEMENT_STACK_OF(MIME_PARAM)
82218822Sdim
83218822Sdimtypedef struct {
84218822Sdim    char *name;                 /* Name of line e.g. "content-type" */
85218822Sdim    char *value;                /* Value of line e.g. "text/plain" */
86218822Sdim    STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
87218822Sdim} MIME_HEADER;
88218822Sdim
89218822SdimDECLARE_STACK_OF(MIME_HEADER)
90218822SdimIMPLEMENT_STACK_OF(MIME_HEADER)
91218822Sdim
92218822Sdimstatic int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
93218822Sdim                            const ASN1_ITEM *it);
94218822Sdimstatic char *strip_ends(char *name);
95218822Sdimstatic char *strip_start(char *name);
96218822Sdimstatic char *strip_end(char *name);
97218822Sdimstatic MIME_HEADER *mime_hdr_new(char *name, char *value);
98218822Sdimstatic int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
99218822Sdimstatic STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
100218822Sdimstatic int mime_hdr_cmp(const MIME_HEADER *const *a,
101218822Sdim                        const MIME_HEADER *const *b);
102218822Sdimstatic int mime_param_cmp(const MIME_PARAM *const *a,
103218822Sdim                          const MIME_PARAM *const *b);
104218822Sdimstatic void mime_param_free(MIME_PARAM *param);
105218822Sdimstatic int mime_bound_check(char *line, int linelen, char *bound, int blen);
106218822Sdimstatic int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
107218822Sdimstatic int strip_eol(char *linebuf, int *plen);
108218822Sdimstatic MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name);
109218822Sdimstatic MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name);
110218822Sdimstatic void mime_hdr_free(MIME_HEADER *hdr);
111218822Sdim
112218822Sdim#define MAX_SMLEN 1024
113218822Sdim#define mime_debug(x)           /* x */
114218822Sdim
115218822Sdim/* Output an ASN1 structure in BER format streaming if necessary */
116218822Sdim
117218822Sdimint i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
118218822Sdim                        const ASN1_ITEM *it)
119218822Sdim{
120218822Sdim    /* If streaming create stream BIO and copy all content through it */
121218822Sdim    if (flags & SMIME_STREAM) {
122218822Sdim        BIO *bio, *tbio;
123218822Sdim        bio = BIO_new_NDEF(out, val, it);
124218822Sdim        if (!bio) {
125218822Sdim            ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM, ERR_R_MALLOC_FAILURE);
126218822Sdim            return 0;
127218822Sdim        }
128218822Sdim        SMIME_crlf_copy(in, bio, flags);
129218822Sdim        (void)BIO_flush(bio);
130218822Sdim        /* Free up successive BIOs until we hit the old output BIO */
131218822Sdim        do {
132218822Sdim            tbio = BIO_pop(bio);
133218822Sdim            BIO_free(bio);
134218822Sdim            bio = tbio;
135218822Sdim        } while (bio != out);
136218822Sdim    }
137218822Sdim    /*
138218822Sdim     * else just write out ASN1 structure which will have all content stored
139218822Sdim     * internally
140218822Sdim     */
141218822Sdim    else
142218822Sdim        ASN1_item_i2d_bio(it, out, val);
143218822Sdim    return 1;
144218822Sdim}
145218822Sdim
146218822Sdim/* Base 64 read and write of ASN1 structure */
147218822Sdim
148218822Sdimstatic int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
149218822Sdim                          const ASN1_ITEM *it)
150218822Sdim{
151218822Sdim    BIO *b64;
152218822Sdim    int r;
153218822Sdim    b64 = BIO_new(BIO_f_base64());
154218822Sdim    if (!b64) {
155218822Sdim        ASN1err(ASN1_F_B64_WRITE_ASN1, ERR_R_MALLOC_FAILURE);
156218822Sdim        return 0;
157218822Sdim    }
158218822Sdim    /*
159218822Sdim     * prepend the b64 BIO so all data is base64 encoded.
160218822Sdim     */
161218822Sdim    out = BIO_push(b64, out);
162218822Sdim    r = i2d_ASN1_bio_stream(out, val, in, flags, it);
163218822Sdim    (void)BIO_flush(out);
164218822Sdim    BIO_pop(out);
165218822Sdim    BIO_free(b64);
166218822Sdim    return r;
167218822Sdim}
168218822Sdim
169218822Sdim/* Streaming ASN1 PEM write */
170218822Sdim
171218822Sdimint PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
172218822Sdim                              const char *hdr, const ASN1_ITEM *it)
173218822Sdim{
174218822Sdim    int r;
175218822Sdim    BIO_printf(out, "-----BEGIN %s-----\n", hdr);
176218822Sdim    r = B64_write_ASN1(out, val, in, flags, it);
177218822Sdim    BIO_printf(out, "-----END %s-----\n", hdr);
178218822Sdim    return r;
179218822Sdim}
180218822Sdim
181218822Sdimstatic ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
182218822Sdim{
183218822Sdim    BIO *b64;
184218822Sdim    ASN1_VALUE *val;
185218822Sdim    if (!(b64 = BIO_new(BIO_f_base64()))) {
186218822Sdim        ASN1err(ASN1_F_B64_READ_ASN1, ERR_R_MALLOC_FAILURE);
187218822Sdim        return 0;
188218822Sdim    }
189218822Sdim    bio = BIO_push(b64, bio);
190218822Sdim    val = ASN1_item_d2i_bio(it, bio, NULL);
191218822Sdim    if (!val)
192218822Sdim        ASN1err(ASN1_F_B64_READ_ASN1, ASN1_R_DECODE_ERROR);
193218822Sdim    (void)BIO_flush(bio);
194218822Sdim    bio = BIO_pop(bio);
195218822Sdim    BIO_free(b64);
196218822Sdim    return val;
197218822Sdim}
198218822Sdim
199218822Sdim/* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
200218822Sdim
201218822Sdimstatic int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
202218822Sdim{
203218822Sdim    const EVP_MD *md;
204218822Sdim    int i, have_unknown = 0, write_comma, ret = 0, md_nid;
205218822Sdim    have_unknown = 0;
206218822Sdim    write_comma = 0;
207218822Sdim    for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++) {
208218822Sdim        if (write_comma)
209218822Sdim            BIO_write(out, ",", 1);
210218822Sdim        write_comma = 1;
211218822Sdim        md_nid = OBJ_obj2nid(sk_X509_ALGOR_value(mdalgs, i)->algorithm);
212218822Sdim        md = EVP_get_digestbynid(md_nid);
213218822Sdim        if (md && md->md_ctrl) {
214218822Sdim            int rv;
215218822Sdim            char *micstr;
216218822Sdim            rv = md->md_ctrl(NULL, EVP_MD_CTRL_MICALG, 0, &micstr);
217218822Sdim            if (rv > 0) {
218218822Sdim                BIO_puts(out, micstr);
219218822Sdim                OPENSSL_free(micstr);
220218822Sdim                continue;
221218822Sdim            }
222218822Sdim            if (rv != -2)
223218822Sdim                goto err;
224218822Sdim        }
225218822Sdim        switch (md_nid) {
226218822Sdim        case NID_sha1:
227218822Sdim            BIO_puts(out, "sha1");
228218822Sdim            break;
229218822Sdim
230218822Sdim        case NID_md5:
231218822Sdim            BIO_puts(out, "md5");
232218822Sdim            break;
233218822Sdim
234218822Sdim        case NID_sha256:
235218822Sdim            BIO_puts(out, "sha-256");
236218822Sdim            break;
237218822Sdim
238218822Sdim        case NID_sha384:
239218822Sdim            BIO_puts(out, "sha-384");
240218822Sdim            break;
241218822Sdim
242218822Sdim        case NID_sha512:
243218822Sdim            BIO_puts(out, "sha-512");
244218822Sdim            break;
245218822Sdim
246218822Sdim        case NID_id_GostR3411_94:
247218822Sdim            BIO_puts(out, "gostr3411-94");
248218822Sdim            goto err;
249218822Sdim            break;
250218822Sdim
251218822Sdim        default:
252218822Sdim            if (have_unknown)
253218822Sdim                write_comma = 0;
254218822Sdim            else {
255218822Sdim                BIO_puts(out, "unknown");
256218822Sdim                have_unknown = 1;
257218822Sdim            }
258218822Sdim            break;
259218822Sdim
260218822Sdim        }
261218822Sdim    }
262218822Sdim
263218822Sdim    ret = 1;
264218822Sdim err:
265218822Sdim
266218822Sdim    return ret;
267218822Sdim
268218822Sdim}
269218822Sdim
270218822Sdim/* SMIME sender */
271218822Sdim
272218822Sdimint SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
273218822Sdim                     int ctype_nid, int econt_nid,
274218822Sdim                     STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it)
275218822Sdim{
276218822Sdim    char bound[33], c;
277218822Sdim    int i;
278218822Sdim    const char *mime_prefix, *mime_eol, *cname = "smime.p7m";
279218822Sdim    const char *msg_type = NULL;
280218822Sdim    if (flags & SMIME_OLDMIME)
281218822Sdim        mime_prefix = "application/x-pkcs7-";
282218822Sdim    else
283218822Sdim        mime_prefix = "application/pkcs7-";
284218822Sdim
285218822Sdim    if (flags & SMIME_CRLFEOL)
286218822Sdim        mime_eol = "\r\n";
287218822Sdim    else
288218822Sdim        mime_eol = "\n";
289218822Sdim    if ((flags & SMIME_DETACHED) && data) {
290218822Sdim        /* We want multipart/signed */
291218822Sdim        /* Generate a random boundary */
292218822Sdim        if (RAND_bytes((unsigned char *)bound, 32) <= 0)
293218822Sdim            return 0;
294218822Sdim        for (i = 0; i < 32; i++) {
295218822Sdim            c = bound[i] & 0xf;
296218822Sdim            if (c < 10)
297218822Sdim                c += '0';
298218822Sdim            else
299218822Sdim                c += 'A' - 10;
300218822Sdim            bound[i] = c;
301218822Sdim        }
302218822Sdim        bound[32] = 0;
303218822Sdim        BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
304218822Sdim        BIO_printf(bio, "Content-Type: multipart/signed;");
305218822Sdim        BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix);
306218822Sdim        BIO_puts(bio, " micalg=\"");
307218822Sdim        asn1_write_micalg(bio, mdalgs);
308218822Sdim        BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
309218822Sdim                   bound, mime_eol, mime_eol);
310218822Sdim        BIO_printf(bio, "This is an S/MIME signed message%s%s",
311218822Sdim                   mime_eol, mime_eol);
312218822Sdim        /* Now write out the first part */
313218822Sdim        BIO_printf(bio, "------%s%s", bound, mime_eol);
314218822Sdim        if (!asn1_output_data(bio, data, val, flags, it))
315218822Sdim            return 0;
316218822Sdim        BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol);
317218822Sdim
318218822Sdim        /* Headers for signature */
319218822Sdim
320218822Sdim        BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
321218822Sdim        BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
322218822Sdim        BIO_printf(bio, "Content-Transfer-Encoding: base64%s", mime_eol);
323218822Sdim        BIO_printf(bio, "Content-Disposition: attachment;");
324218822Sdim        BIO_printf(bio, " filename=\"smime.p7s\"%s%s", mime_eol, mime_eol);
325218822Sdim        B64_write_ASN1(bio, val, NULL, 0, it);
326218822Sdim        BIO_printf(bio, "%s------%s--%s%s", mime_eol, bound,
327218822Sdim                   mime_eol, mime_eol);
328218822Sdim        return 1;
329218822Sdim    }
330218822Sdim
331218822Sdim    /* Determine smime-type header */
332218822Sdim
333218822Sdim    if (ctype_nid == NID_pkcs7_enveloped)
334218822Sdim        msg_type = "enveloped-data";
335218822Sdim    else if (ctype_nid == NID_pkcs7_signed) {
336218822Sdim        if (econt_nid == NID_id_smime_ct_receipt)
337218822Sdim            msg_type = "signed-receipt";
338218822Sdim        else if (sk_X509_ALGOR_num(mdalgs) >= 0)
339218822Sdim            msg_type = "signed-data";
340218822Sdim        else
341218822Sdim            msg_type = "certs-only";
342218822Sdim    } else if (ctype_nid == NID_id_smime_ct_compressedData) {
343218822Sdim        msg_type = "compressed-data";
344218822Sdim        cname = "smime.p7z";
345218822Sdim    }
346218822Sdim    /* MIME headers */
347218822Sdim    BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
348218822Sdim    BIO_printf(bio, "Content-Disposition: attachment;");
349218822Sdim    BIO_printf(bio, " filename=\"%s\"%s", cname, mime_eol);
350218822Sdim    BIO_printf(bio, "Content-Type: %smime;", mime_prefix);
351218822Sdim    if (msg_type)
352218822Sdim        BIO_printf(bio, " smime-type=%s;", msg_type);
353218822Sdim    BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol);
354218822Sdim    BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
355218822Sdim               mime_eol, mime_eol);
356218822Sdim    if (!B64_write_ASN1(bio, val, data, flags, it))
357218822Sdim        return 0;
358218822Sdim    BIO_printf(bio, "%s", mime_eol);
359218822Sdim    return 1;
360218822Sdim}
361218822Sdim
362218822Sdim/* Handle output of ASN1 data */
363218822Sdim
364218822Sdimstatic int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
365218822Sdim                            const ASN1_ITEM *it)
366218822Sdim{
367218822Sdim    BIO *tmpbio;
368218822Sdim    const ASN1_AUX *aux = it->funcs;
369218822Sdim    ASN1_STREAM_ARG sarg;
370218822Sdim    int rv = 1;
371218822Sdim
372218822Sdim    /*
373218822Sdim     * If data is not deteched or resigning then the output BIO is already
374218822Sdim     * set up to finalise when it is written through.
375218822Sdim     */
376218822Sdim    if (!(flags & SMIME_DETACHED) || (flags & PKCS7_REUSE_DIGEST)) {
377218822Sdim        SMIME_crlf_copy(data, out, flags);
378218822Sdim        return 1;
379218822Sdim    }
380218822Sdim
381218822Sdim    if (!aux || !aux->asn1_cb) {
382218822Sdim        ASN1err(ASN1_F_ASN1_OUTPUT_DATA, ASN1_R_STREAMING_NOT_SUPPORTED);
383218822Sdim        return 0;
384218822Sdim    }
385218822Sdim
386218822Sdim    sarg.out = out;
387218822Sdim    sarg.ndef_bio = NULL;
388218822Sdim    sarg.boundary = NULL;
389218822Sdim
390218822Sdim    /* Let ASN1 code prepend any needed BIOs */
391218822Sdim
392218822Sdim    if (aux->asn1_cb(ASN1_OP_DETACHED_PRE, &val, it, &sarg) <= 0)
393218822Sdim        return 0;
394218822Sdim
395218822Sdim    /* Copy data across, passing through filter BIOs for processing */
396218822Sdim    SMIME_crlf_copy(data, sarg.ndef_bio, flags);
397218822Sdim
398218822Sdim    /* Finalize structure */
399218822Sdim    if (aux->asn1_cb(ASN1_OP_DETACHED_POST, &val, it, &sarg) <= 0)
400218822Sdim        rv = 0;
401218822Sdim
402218822Sdim    /* Now remove any digests prepended to the BIO */
403218822Sdim
404218822Sdim    while (sarg.ndef_bio != out) {
405218822Sdim        tmpbio = BIO_pop(sarg.ndef_bio);
406218822Sdim        BIO_free(sarg.ndef_bio);
407218822Sdim        sarg.ndef_bio = tmpbio;
408218822Sdim    }
409218822Sdim
410218822Sdim    return rv;
411218822Sdim
412218822Sdim}
413218822Sdim
414218822Sdim/*
415218822Sdim * SMIME reader: handle multipart/signed and opaque signing. in multipart
416218822Sdim * case the content is placed in a memory BIO pointed to by "bcont". In
417218822Sdim * opaque this is set to NULL
418218822Sdim */
419218822Sdim
420218822SdimASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
421218822Sdim{
422218822Sdim    BIO *asnin;
423218822Sdim    STACK_OF(MIME_HEADER) *headers = NULL;
424218822Sdim    STACK_OF(BIO) *parts = NULL;
425218822Sdim    MIME_HEADER *hdr;
426218822Sdim    MIME_PARAM *prm;
427218822Sdim    ASN1_VALUE *val;
428218822Sdim    int ret;
429218822Sdim
430218822Sdim    if (bcont)
431218822Sdim        *bcont = NULL;
432218822Sdim
433218822Sdim    if (!(headers = mime_parse_hdr(bio))) {
434218822Sdim        ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_MIME_PARSE_ERROR);
435218822Sdim        return NULL;
436218822Sdim    }
437218822Sdim
438218822Sdim    if (!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
439218822Sdim        sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
440218822Sdim        ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE);
441218822Sdim        return NULL;
442218822Sdim    }
443218822Sdim
444218822Sdim    /* Handle multipart/signed */
445218822Sdim
446218822Sdim    if (!strcmp(hdr->value, "multipart/signed")) {
447218822Sdim        /* Split into two parts */
448218822Sdim        prm = mime_param_find(hdr, "boundary");
449218822Sdim        if (!prm || !prm->param_value) {
450218822Sdim            sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
451218822Sdim            ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY);
452218822Sdim            return NULL;
453218822Sdim        }
454218822Sdim        ret = multi_split(bio, prm->param_value, &parts);
455218822Sdim        sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
456218822Sdim        if (!ret || (sk_BIO_num(parts) != 2)) {
457218822Sdim            ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE);
458218822Sdim            sk_BIO_pop_free(parts, BIO_vfree);
459218822Sdim            return NULL;
460218822Sdim        }
461218822Sdim
462218822Sdim        /* Parse the signature piece */
463218822Sdim        asnin = sk_BIO_value(parts, 1);
464218822Sdim
465218822Sdim        if (!(headers = mime_parse_hdr(asnin))) {
466218822Sdim            ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_MIME_SIG_PARSE_ERROR);
467218822Sdim            sk_BIO_pop_free(parts, BIO_vfree);
468218822Sdim            return NULL;
469218822Sdim        }
470218822Sdim
471218822Sdim        /* Get content type */
472218822Sdim
473218822Sdim        if (!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
474218822Sdim            sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
475218822Sdim            ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE);
476218822Sdim            sk_BIO_pop_free(parts, BIO_vfree);
477218822Sdim            return NULL;
478218822Sdim        }
479218822Sdim
480218822Sdim        if (strcmp(hdr->value, "application/x-pkcs7-signature") &&
481218822Sdim            strcmp(hdr->value, "application/pkcs7-signature")) {
482218822Sdim            ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_SIG_INVALID_MIME_TYPE);
483218822Sdim            ERR_add_error_data(2, "type: ", hdr->value);
484218822Sdim            sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
485218822Sdim            sk_BIO_pop_free(parts, BIO_vfree);
486218822Sdim            return NULL;
487218822Sdim        }
488218822Sdim        sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
489218822Sdim        /* Read in ASN1 */
490218822Sdim        if (!(val = b64_read_asn1(asnin, it))) {
491218822Sdim            ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_SIG_PARSE_ERROR);
492218822Sdim            sk_BIO_pop_free(parts, BIO_vfree);
493218822Sdim            return NULL;
494218822Sdim        }
495218822Sdim
496218822Sdim        if (bcont) {
497218822Sdim            *bcont = sk_BIO_value(parts, 0);
498218822Sdim            BIO_free(asnin);
499218822Sdim            sk_BIO_free(parts);
500218822Sdim        } else
501218822Sdim            sk_BIO_pop_free(parts, BIO_vfree);
502218822Sdim        return val;
503218822Sdim    }
504218822Sdim
505218822Sdim    /* OK, if not multipart/signed try opaque signature */
506218822Sdim
507218822Sdim    if (strcmp(hdr->value, "application/x-pkcs7-mime") &&
508218822Sdim        strcmp(hdr->value, "application/pkcs7-mime")) {
509218822Sdim        ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_INVALID_MIME_TYPE);
510218822Sdim        ERR_add_error_data(2, "type: ", hdr->value);
511218822Sdim        sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
512218822Sdim        return NULL;
513218822Sdim    }
514218822Sdim
515218822Sdim    sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
516218822Sdim
517218822Sdim    if (!(val = b64_read_asn1(bio, it))) {
518218822Sdim        ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR);
519218822Sdim        return NULL;
520218822Sdim    }
521218822Sdim    return val;
522218822Sdim
523218822Sdim}
524218822Sdim
525218822Sdim/* Copy text from one BIO to another making the output CRLF at EOL */
526218822Sdimint SMIME_crlf_copy(BIO *in, BIO *out, int flags)
527218822Sdim{
528218822Sdim    BIO *bf;
529218822Sdim    char eol;
530218822Sdim    int len;
531218822Sdim    char linebuf[MAX_SMLEN];
532218822Sdim    /*
533218822Sdim     * Buffer output so we don't write one line at a time. This is useful
534218822Sdim     * when streaming as we don't end up with one OCTET STRING per line.
535218822Sdim     */
536218822Sdim    bf = BIO_new(BIO_f_buffer());
537218822Sdim    if (!bf)
538218822Sdim        return 0;
539218822Sdim    out = BIO_push(bf, out);
540218822Sdim    if (flags & SMIME_BINARY) {
541218822Sdim        while ((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
542218822Sdim            BIO_write(out, linebuf, len);
543218822Sdim    } else {
544218822Sdim        if (flags & SMIME_TEXT)
545218822Sdim            BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
546218822Sdim        while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) {
547218822Sdim            eol = strip_eol(linebuf, &len);
548218822Sdim            if (len)
549218822Sdim                BIO_write(out, linebuf, len);
550218822Sdim            if (eol)
551218822Sdim                BIO_write(out, "\r\n", 2);
552218822Sdim        }
553218822Sdim    }
554218822Sdim    (void)BIO_flush(out);
555218822Sdim    BIO_pop(out);
556218822Sdim    BIO_free(bf);
557218822Sdim    return 1;
558218822Sdim}
559218822Sdim
560218822Sdim/* Strip off headers if they are text/plain */
561218822Sdimint SMIME_text(BIO *in, BIO *out)
562218822Sdim{
563218822Sdim    char iobuf[4096];
564218822Sdim    int len;
565218822Sdim    STACK_OF(MIME_HEADER) *headers;
566218822Sdim    MIME_HEADER *hdr;
567218822Sdim
568218822Sdim    if (!(headers = mime_parse_hdr(in))) {
569218822Sdim        ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_PARSE_ERROR);
570218822Sdim        return 0;
571218822Sdim    }
572218822Sdim    if (!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
573218822Sdim        ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_NO_CONTENT_TYPE);
574218822Sdim        sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
575218822Sdim        return 0;
576218822Sdim    }
577218822Sdim    if (strcmp(hdr->value, "text/plain")) {
578218822Sdim        ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_INVALID_MIME_TYPE);
579218822Sdim        ERR_add_error_data(2, "type: ", hdr->value);
580218822Sdim        sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
581218822Sdim        return 0;
582218822Sdim    }
583218822Sdim    sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
584218822Sdim    while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
585218822Sdim        BIO_write(out, iobuf, len);
586218822Sdim    if (len < 0)
587218822Sdim        return 0;
588218822Sdim    return 1;
589218822Sdim}
590218822Sdim
591218822Sdim/*
592218822Sdim * Split a multipart/XXX message body into component parts: result is
593218822Sdim * canonical parts in a STACK of bios
594218822Sdim */
595218822Sdim
596218822Sdimstatic int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
597218822Sdim{
598218822Sdim    char linebuf[MAX_SMLEN];
599218822Sdim    int len, blen;
600218822Sdim    int eol = 0, next_eol = 0;
601218822Sdim    BIO *bpart = NULL;
602218822Sdim    STACK_OF(BIO) *parts;
603218822Sdim    char state, part, first;
604218822Sdim
605218822Sdim    blen = strlen(bound);
606218822Sdim    part = 0;
607218822Sdim    state = 0;
608218822Sdim    first = 1;
609218822Sdim    parts = sk_BIO_new_null();
610218822Sdim    *ret = parts;
611218822Sdim    while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
612218822Sdim        state = mime_bound_check(linebuf, len, bound, blen);
613218822Sdim        if (state == 1) {
614218822Sdim            first = 1;
615218822Sdim            part++;
616218822Sdim        } else if (state == 2) {
617218822Sdim            sk_BIO_push(parts, bpart);
618218822Sdim            return 1;
619218822Sdim        } else if (part) {
620218822Sdim            /* Strip CR+LF from linebuf */
621218822Sdim            next_eol = strip_eol(linebuf, &len);
622218822Sdim            if (first) {
623218822Sdim                first = 0;
624218822Sdim                if (bpart)
625218822Sdim                    sk_BIO_push(parts, bpart);
626218822Sdim                bpart = BIO_new(BIO_s_mem());
627218822Sdim                if (bpart == NULL)
628218822Sdim                    return 1;
629218822Sdim                BIO_set_mem_eof_return(bpart, 0);
630218822Sdim            } else if (eol)
631218822Sdim                BIO_write(bpart, "\r\n", 2);
632218822Sdim            eol = next_eol;
633218822Sdim            if (len)
634218822Sdim                BIO_write(bpart, linebuf, len);
635218822Sdim        }
636218822Sdim    }
637218822Sdim    return 0;
638218822Sdim}
639218822Sdim
640218822Sdim/* This is the big one: parse MIME header lines up to message body */
641218822Sdim
642218822Sdim#define MIME_INVALID    0
643218822Sdim#define MIME_START      1
644218822Sdim#define MIME_TYPE       2
645218822Sdim#define MIME_NAME       3
646218822Sdim#define MIME_VALUE      4
647218822Sdim#define MIME_QUOTE      5
648218822Sdim#define MIME_COMMENT    6
649218822Sdim
650218822Sdimstatic STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
651218822Sdim{
652218822Sdim    char *p, *q, c;
653218822Sdim    char *ntmp;
654218822Sdim    char linebuf[MAX_SMLEN];
655218822Sdim    MIME_HEADER *mhdr = NULL;
656218822Sdim    STACK_OF(MIME_HEADER) *headers;
657218822Sdim    int len, state, save_state = 0;
658218822Sdim
659218822Sdim    headers = sk_MIME_HEADER_new(mime_hdr_cmp);
660218822Sdim    if (!headers)
661218822Sdim        return NULL;
662218822Sdim    while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
663218822Sdim        /* If whitespace at line start then continuation line */
664218822Sdim        if (mhdr && isspace((unsigned char)linebuf[0]))
665218822Sdim            state = MIME_NAME;
666218822Sdim        else
667218822Sdim            state = MIME_START;
668218822Sdim        ntmp = NULL;
669218822Sdim        /* Go through all characters */
670218822Sdim        for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
671218822Sdim             p++) {
672218822Sdim
673218822Sdim            /*
674218822Sdim             * State machine to handle MIME headers if this looks horrible
675218822Sdim             * that's because it *is*
676218822Sdim             */
677218822Sdim
678218822Sdim            switch (state) {
679218822Sdim            case MIME_START:
680218822Sdim                if (c == ':') {
681218822Sdim                    state = MIME_TYPE;
682218822Sdim                    *p = 0;
683218822Sdim                    ntmp = strip_ends(q);
684218822Sdim                    q = p + 1;
685218822Sdim                }
686218822Sdim                break;
687218822Sdim
688218822Sdim            case MIME_TYPE:
689218822Sdim                if (c == ';') {
690218822Sdim                    mime_debug("Found End Value\n");
691218822Sdim                    *p = 0;
692218822Sdim                    mhdr = mime_hdr_new(ntmp, strip_ends(q));
693218822Sdim                    sk_MIME_HEADER_push(headers, mhdr);
694218822Sdim                    ntmp = NULL;
695218822Sdim                    q = p + 1;
696218822Sdim                    state = MIME_NAME;
697218822Sdim                } else if (c == '(') {
698218822Sdim                    save_state = state;
699218822Sdim                    state = MIME_COMMENT;
700218822Sdim                }
701218822Sdim                break;
702218822Sdim
703218822Sdim            case MIME_COMMENT:
704218822Sdim                if (c == ')') {
705218822Sdim                    state = save_state;
706218822Sdim                }
707218822Sdim                break;
708218822Sdim
709218822Sdim            case MIME_NAME:
710218822Sdim                if (c == '=') {
711218822Sdim                    state = MIME_VALUE;
712218822Sdim                    *p = 0;
713218822Sdim                    ntmp = strip_ends(q);
714218822Sdim                    q = p + 1;
715218822Sdim                }
716218822Sdim                break;
717218822Sdim
718218822Sdim            case MIME_VALUE:
719218822Sdim                if (c == ';') {
720218822Sdim                    state = MIME_NAME;
721218822Sdim                    *p = 0;
722218822Sdim                    mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
723218822Sdim                    ntmp = NULL;
724218822Sdim                    q = p + 1;
725218822Sdim                } else if (c == '"') {
726218822Sdim                    mime_debug("Found Quote\n");
727218822Sdim                    state = MIME_QUOTE;
728218822Sdim                } else if (c == '(') {
729218822Sdim                    save_state = state;
730218822Sdim                    state = MIME_COMMENT;
731218822Sdim                }
732218822Sdim                break;
733218822Sdim
734218822Sdim            case MIME_QUOTE:
735218822Sdim                if (c == '"') {
736218822Sdim                    mime_debug("Found Match Quote\n");
737218822Sdim                    state = MIME_VALUE;
738218822Sdim                }
739218822Sdim                break;
740218822Sdim            }
741218822Sdim        }
742218822Sdim
743218822Sdim        if (state == MIME_TYPE) {
744218822Sdim            mhdr = mime_hdr_new(ntmp, strip_ends(q));
745218822Sdim            sk_MIME_HEADER_push(headers, mhdr);
746218822Sdim        } else if (state == MIME_VALUE)
747218822Sdim            mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
748218822Sdim        if (p == linebuf)
749218822Sdim            break;              /* Blank line means end of headers */
750218822Sdim    }
751218822Sdim
752218822Sdim    return headers;
753218822Sdim
754218822Sdim}
755218822Sdim
756218822Sdimstatic char *strip_ends(char *name)
757218822Sdim{
758218822Sdim    return strip_end(strip_start(name));
759218822Sdim}
760218822Sdim
761218822Sdim/* Strip a parameter of whitespace from start of param */
762218822Sdimstatic char *strip_start(char *name)
763218822Sdim{
764218822Sdim    char *p, c;
765218822Sdim    /* Look for first non white space or quote */
766218822Sdim    for (p = name; (c = *p); p++) {
767218822Sdim        if (c == '"') {
768218822Sdim            /* Next char is start of string if non null */
769218822Sdim            if (p[1])
770218822Sdim                return p + 1;
771218822Sdim            /* Else null string */
772218822Sdim            return NULL;
773218822Sdim        }
774218822Sdim        if (!isspace((unsigned char)c))
775218822Sdim            return p;
776218822Sdim    }
777218822Sdim    return NULL;
778218822Sdim}
779218822Sdim
780218822Sdim/* As above but strip from end of string : maybe should handle brackets? */
781218822Sdimstatic char *strip_end(char *name)
782218822Sdim{
783218822Sdim    char *p, c;
784218822Sdim    if (!name)
785218822Sdim        return NULL;
786218822Sdim    /* Look for first non white space or quote */
787218822Sdim    for (p = name + strlen(name) - 1; p >= name; p--) {
788218822Sdim        c = *p;
789218822Sdim        if (c == '"') {
790218822Sdim            if (p - 1 == name)
791218822Sdim                return NULL;
792218822Sdim            *p = 0;
793218822Sdim            return name;
794218822Sdim        }
795218822Sdim        if (isspace((unsigned char)c))
796218822Sdim            *p = 0;
797218822Sdim        else
798218822Sdim            return name;
799218822Sdim    }
800218822Sdim    return NULL;
801218822Sdim}
802218822Sdim
803218822Sdimstatic MIME_HEADER *mime_hdr_new(char *name, char *value)
804218822Sdim{
805218822Sdim    MIME_HEADER *mhdr;
806218822Sdim    char *tmpname, *tmpval, *p;
807218822Sdim    int c;
808218822Sdim    if (name) {
809218822Sdim        if (!(tmpname = BUF_strdup(name)))
810218822Sdim            return NULL;
811218822Sdim        for (p = tmpname; *p; p++) {
812218822Sdim            c = (unsigned char)*p;
813218822Sdim            if (isupper(c)) {
814218822Sdim                c = tolower(c);
815218822Sdim                *p = c;
816218822Sdim            }
817218822Sdim        }
818218822Sdim    } else
819218822Sdim        tmpname = NULL;
820218822Sdim    if (value) {
821218822Sdim        if (!(tmpval = BUF_strdup(value)))
822218822Sdim            return NULL;
823218822Sdim        for (p = tmpval; *p; p++) {
824218822Sdim            c = (unsigned char)*p;
825218822Sdim            if (isupper(c)) {
826218822Sdim                c = tolower(c);
827218822Sdim                *p = c;
828218822Sdim            }
829218822Sdim        }
830218822Sdim    } else
831218822Sdim        tmpval = NULL;
832218822Sdim    mhdr = (MIME_HEADER *)OPENSSL_malloc(sizeof(MIME_HEADER));
833218822Sdim    if (!mhdr)
834218822Sdim        return NULL;
835218822Sdim    mhdr->name = tmpname;
836218822Sdim    mhdr->value = tmpval;
837218822Sdim    if (!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp)))
838218822Sdim        return NULL;
839218822Sdim    return mhdr;
840218822Sdim}
841218822Sdim
842218822Sdimstatic int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
843218822Sdim{
844218822Sdim    char *tmpname, *tmpval, *p;
845218822Sdim    int c;
846218822Sdim    MIME_PARAM *mparam;
847218822Sdim    if (name) {
848218822Sdim        tmpname = BUF_strdup(name);
849218822Sdim        if (!tmpname)
850218822Sdim            return 0;
851218822Sdim        for (p = tmpname; *p; p++) {
852218822Sdim            c = (unsigned char)*p;
853218822Sdim            if (isupper(c)) {
854218822Sdim                c = tolower(c);
855218822Sdim                *p = c;
856218822Sdim            }
857218822Sdim        }
858218822Sdim    } else
859218822Sdim        tmpname = NULL;
860218822Sdim    if (value) {
861218822Sdim        tmpval = BUF_strdup(value);
862218822Sdim        if (!tmpval)
863218822Sdim            return 0;
864218822Sdim    } else
865218822Sdim        tmpval = NULL;
866218822Sdim    /* Parameter values are case sensitive so leave as is */
867218822Sdim    mparam = (MIME_PARAM *)OPENSSL_malloc(sizeof(MIME_PARAM));
868218822Sdim    if (!mparam)
869218822Sdim        return 0;
870218822Sdim    mparam->param_name = tmpname;
871218822Sdim    mparam->param_value = tmpval;
872218822Sdim    sk_MIME_PARAM_push(mhdr->params, mparam);
873218822Sdim    return 1;
874218822Sdim}
875218822Sdim
876218822Sdimstatic int mime_hdr_cmp(const MIME_HEADER *const *a,
877218822Sdim                        const MIME_HEADER *const *b)
878218822Sdim{
879218822Sdim    if (!(*a)->name || !(*b)->name)
880218822Sdim        return ! !(*a)->name - ! !(*b)->name;
881218822Sdim
882218822Sdim    return (strcmp((*a)->name, (*b)->name));
883218822Sdim}
884218822Sdim
885218822Sdimstatic int mime_param_cmp(const MIME_PARAM *const *a,
886218822Sdim                          const MIME_PARAM *const *b)
887218822Sdim{
888218822Sdim    if (!(*a)->param_name || !(*b)->param_name)
889218822Sdim        return ! !(*a)->param_name - ! !(*b)->param_name;
890218822Sdim    return (strcmp((*a)->param_name, (*b)->param_name));
891218822Sdim}
892218822Sdim
893218822Sdim/* Find a header with a given name (if possible) */
894218822Sdim
895218822Sdimstatic MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
896218822Sdim{
897218822Sdim    MIME_HEADER htmp;
898218822Sdim    int idx;
899218822Sdim    htmp.name = name;
900218822Sdim    idx = sk_MIME_HEADER_find(hdrs, &htmp);
901218822Sdim    if (idx < 0)
902218822Sdim        return NULL;
903218822Sdim    return sk_MIME_HEADER_value(hdrs, idx);
904218822Sdim}
905218822Sdim
906218822Sdimstatic MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name)
907218822Sdim{
908218822Sdim    MIME_PARAM param;
909218822Sdim    int idx;
910218822Sdim    param.param_name = name;
911218822Sdim    idx = sk_MIME_PARAM_find(hdr->params, &param);
912218822Sdim    if (idx < 0)
913218822Sdim        return NULL;
914218822Sdim    return sk_MIME_PARAM_value(hdr->params, idx);
915218822Sdim}
916218822Sdim
917218822Sdimstatic void mime_hdr_free(MIME_HEADER *hdr)
918218822Sdim{
919218822Sdim    if (hdr->name)
920218822Sdim        OPENSSL_free(hdr->name);
921218822Sdim    if (hdr->value)
922218822Sdim        OPENSSL_free(hdr->value);
923218822Sdim    if (hdr->params)
924218822Sdim        sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
925218822Sdim    OPENSSL_free(hdr);
926218822Sdim}
927218822Sdim
928218822Sdimstatic void mime_param_free(MIME_PARAM *param)
929218822Sdim{
930218822Sdim    if (param->param_name)
931218822Sdim        OPENSSL_free(param->param_name);
932218822Sdim    if (param->param_value)
933218822Sdim        OPENSSL_free(param->param_value);
934218822Sdim    OPENSSL_free(param);
935218822Sdim}
936218822Sdim
937218822Sdim/*-
938218822Sdim * Check for a multipart boundary. Returns:
939218822Sdim * 0 : no boundary
940218822Sdim * 1 : part boundary
941218822Sdim * 2 : final boundary
942218822Sdim */
943218822Sdimstatic int mime_bound_check(char *line, int linelen, char *bound, int blen)
944218822Sdim{
945218822Sdim    if (linelen == -1)
946218822Sdim        linelen = strlen(line);
947218822Sdim    if (blen == -1)
948218822Sdim        blen = strlen(bound);
949218822Sdim    /* Quickly eliminate if line length too short */
950218822Sdim    if (blen + 2 > linelen)
951218822Sdim        return 0;
952218822Sdim    /* Check for part boundary */
953218822Sdim    if (!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
954218822Sdim        if (!strncmp(line + blen + 2, "--", 2))
955218822Sdim            return 2;
956218822Sdim        else
957218822Sdim            return 1;
958218822Sdim    }
959218822Sdim    return 0;
960218822Sdim}
961218822Sdim
962218822Sdimstatic int strip_eol(char *linebuf, int *plen)
963218822Sdim{
964218822Sdim    int len = *plen;
965218822Sdim    char *p, c;
966218822Sdim    int is_eol = 0;
967218822Sdim    p = linebuf + len - 1;
968218822Sdim    for (p = linebuf + len - 1; len > 0; len--, p--) {
969218822Sdim        c = *p;
970218822Sdim        if (c == '\n')
971218822Sdim            is_eol = 1;
972218822Sdim        else if (c != '\r')
973218822Sdim            break;
974218822Sdim    }
975218822Sdim    *plen = len;
976218822Sdim    return is_eol;
977218822Sdim}
978218822Sdim