Commit bdd58d98 authored by Richard Levitte's avatar Richard Levitte
Browse files

Change the way apps open their input and output files



The different apps had the liberty to decide whether they would open their
input and output files in binary mode or not, which could be confusing if
two different apps were handling the same type of file in different ways.

The solution is to centralise the decision of low level file organisation,
and that the apps would use a selection of formats to state the intent of
the file.

Reviewed-by: default avatarTim Hudson <tjh@openssl.org>
parent d303b9d8
Loading
Loading
Loading
Loading
+7 −14
Original line number Diff line number Diff line
@@ -514,7 +514,7 @@ CONF *app_load_config(const char *filename)
    BIO *in;
    CONF *conf;

    in = bio_open_default(filename, "r");
    in = bio_open_default(filename, 'r', FORMAT_TEXT);
    if (in == NULL)
        return NULL;

@@ -527,7 +527,7 @@ CONF *app_load_config_quiet(const char *filename)
    BIO *in;
    CONF *conf;

    in = bio_open_default_quiet(filename, "r");
    in = bio_open_default_quiet(filename, 'r', FORMAT_TEXT);
    if (in == NULL)
        return NULL;

@@ -683,7 +683,7 @@ X509 *load_cert(const char *file, int format,
        unbuffer(stdin);
        cert = dup_bio_in();
    } else
        cert = bio_open_default(file, RB(format));
        cert = bio_open_default(file, 'r', format);
    if (cert == NULL)
        goto end;

@@ -718,7 +718,7 @@ X509_CRL *load_crl(const char *infile, int format)
        return x;
    }

    in = bio_open_default(infile, RB(format));
    in = bio_open_default(infile, 'r', format);
    if (in == NULL)
        goto end;
    if (format == FORMAT_ASN1)
@@ -772,7 +772,7 @@ EVP_PKEY *load_key(const char *file, int format, int maybe_stdin,
        unbuffer(stdin);
        key = dup_bio_in();
    } else
        key = bio_open_default(file, RB(format));
        key = bio_open_default(file, 'r', format);
    if (key == NULL)
        goto end;
    if (format == FORMAT_ASN1) {
@@ -808,13 +808,6 @@ EVP_PKEY *load_key(const char *file, int format, int maybe_stdin,
    return (pkey);
}

static const char *key_file_format(int format)
{
    if (format == FORMAT_PEM || format == FORMAT_PEMRSA)
        return "r";
    return "rb";
}

EVP_PKEY *load_pubkey(const char *file, int format, int maybe_stdin,
                      const char *pass, ENGINE *e, const char *key_descrip)
{
@@ -842,7 +835,7 @@ EVP_PKEY *load_pubkey(const char *file, int format, int maybe_stdin,
        unbuffer(stdin);
        key = dup_bio_in();
    } else
        key = bio_open_default(file, key_file_format(format));
        key = bio_open_default(file, 'r', format);
    if (key == NULL)
        goto end;
    if (format == FORMAT_ASN1) {
@@ -909,7 +902,7 @@ static int load_certs_crls(const char *file, int format,
        return 0;
    }

    bio = bio_open_default(file, "r");
    bio = bio_open_default(file, 'r', FORMAT_PEM);
    if (bio == NULL)
        return 0;

+16 −14
Original line number Diff line number Diff line
@@ -154,19 +154,14 @@ extern BIO *bio_out;
extern BIO *bio_err;
BIO *dup_bio_in(void);
BIO *dup_bio_out(void);
BIO *bio_open_owner(const char *filename, const char *mode, int private);
BIO *bio_open_default(const char *filename, const char *mode);
BIO *bio_open_default_quiet(const char *filename, const char *mode);
BIO *bio_open_owner(const char *filename, int format, int private);
BIO *bio_open_default(const char *filename, char mode, int format);
BIO *bio_open_default_quiet(const char *filename, char mode, int format);
CONF *app_load_config(const char *filename);
CONF *app_load_config_quiet(const char *filename);
int app_load_modules(const CONF *config);
void unbuffer(FILE *fp);

/* Often used in calls to bio_open_default. */
# define RB(xformat)  (((xformat) & B_FORMAT_TEXT) ? "rb" : "r")
# define WB(xformat)  (((xformat) & B_FORMAT_TEXT) ? "wb" : "w")
# define AB(xformat)  (((xformat) & B_FORMAT_TEXT) ? "ab" : "a")

/*
 * Common verification options.
 */
@@ -536,14 +531,21 @@ void print_cert_checks(BIO *bio, X509 *x,
void store_setup_crl_download(X509_STORE *st);

/* See OPT_FMT_xxx, above. */
/* On some platforms, it's important to distinguish between text and binary
 * files.  On some, there might even be specific file formats for different
 * contents.  The FORMAT_xxx macros are meant to express an intent with the
 * file being read or created.
 */
# define B_FORMAT_TEXT   0x8000
# define FORMAT_UNDEF    0
# define FORMAT_ASN1     1
# define FORMAT_TEXT    (2 | B_FORMAT_TEXT)
# define FORMAT_PEM     (3 | B_FORMAT_TEXT)
# define FORMAT_PKCS12   5
# define FORMAT_SMIME   (6 | B_FORMAT_TEXT)
# define FORMAT_ENGINE   7
# define FORMAT_TEXT    (1 | B_FORMAT_TEXT)     /* Generic text */
# define FORMAT_BINARY   2                      /* Generic binary */
# define FORMAT_BASE64  (3 | B_FORMAT_TEXT)     /* Base64 */
# define FORMAT_ASN1     4                      /* ASN.1/DER */
# define FORMAT_PEM     (5 | B_FORMAT_TEXT)
# define FORMAT_PKCS12   6
# define FORMAT_SMIME   (7 | B_FORMAT_TEXT)
# define FORMAT_ENGINE   8                      /* Not really a file format */
# define FORMAT_PEMRSA  (9 | B_FORMAT_TEXT)     /* PEM RSAPubicKey format */
# define FORMAT_ASN1RSA  10                     /* DER RSAPubicKey format */
# define FORMAT_MSBLOB   11                     /* MS Key blob format */
+3 −3
Original line number Diff line number Diff line
@@ -190,17 +190,17 @@ int asn1parse_main(int argc, char **argv)
        goto end;

    if (oidfile != NULL) {
      in = bio_open_default(oidfile, "r");
        in = bio_open_default(oidfile, 'r', FORMAT_TEXT);
        if (in == NULL)
            goto end;
        OBJ_create_objects(in);
        BIO_free(in);
    }

    if ((in = bio_open_default(infile, RB(informat))) == NULL)
    if ((in = bio_open_default(infile, 'r', informat)) == NULL)
        goto end;

    if (derfile && (derout = bio_open_default(derfile, "wb")) == NULL)
    if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL)
        goto end;

    if (strictpem) {
+2 −1
Original line number Diff line number Diff line
@@ -795,7 +795,8 @@ end_of_options:

    /*****************************************************************/
    if (req || gencrl) {
        Sout = bio_open_default(outfile, "w");
        /* FIXME: Is it really always text? */
        Sout = bio_open_default(outfile, 'w', FORMAT_TEXT);
        if (Sout == NULL)
            goto end;
    }
+6 −11
Original line number Diff line number Diff line
@@ -247,7 +247,6 @@ int cms_main(int argc, char **argv)
        NULL;
    char *to = NULL, *from = NULL, *subject = NULL, *prog;
    cms_key_param *key_first = NULL, *key_param = NULL;
    const char *inmode = "r", *outmode = "w";
    int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched =
        0;
    int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
@@ -689,18 +688,14 @@ int cms_main(int argc, char **argv)
    if (!(operation & SMIME_SIGNERS))
        flags &= ~CMS_DETACHED;

    if (operation & SMIME_OP) {
        outmode = WB(outformat);
    } else {
    if (!(operation & SMIME_OP)) {
        if (flags & CMS_BINARY)
            outmode = "wb";
            outformat = FORMAT_BINARY;
    }

    if (operation & SMIME_IP) {
        inmode = RB(informat);
    } else {
    if (!(operation & SMIME_IP)) {
        if (flags & CMS_BINARY)
            inmode = "rb";
            informat = FORMAT_BINARY;
    }

    if (operation == SMIME_ENCRYPT) {
@@ -770,7 +765,7 @@ int cms_main(int argc, char **argv)
            goto end;
    }

    in = bio_open_default(infile, inmode);
    in = bio_open_default(infile, 'r', informat);
    if (in == NULL)
        goto end;

@@ -834,7 +829,7 @@ int cms_main(int argc, char **argv)
        }
    }

    out = bio_open_default(outfile, outmode);
    out = bio_open_default(outfile, 'w', outformat);
    if (out == NULL)
        goto end;

Loading