x509aux.c 4.7 KB
Newer Older
1
/*
2
 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed under the OpenSSL licenses, (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.openssl.org/source/license.html
 * or in the file LICENSE in the source distribution.
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/conf.h>
#include <openssl/err.h>
19 20
#include "e_os.h"
#include "testutil.h"
21

22 23 24 25 26

/* List of files, from argv */
static char **files;

static int test_certs(int num)
27
{
P
Pauli 已提交
28
    int c;
29 30 31 32 33 34 35
    char *name = 0;
    char *header = 0;
    unsigned char *data = 0;
    long len;
    typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
    typedef int (*i2d_X509_t)(X509 *, unsigned char **);
    int err = 0;
36 37 38 39
    BIO *fp = BIO_new_file(files[num], "r");

    if (!TEST_ptr(fp))
        return 0;
40

P
Pauli 已提交
41 42 43
    for (c = 0; !err && PEM_read_bio(fp, &name, &header, &data, &len); ++c) {
        const int trusted = (strcmp(name, PEM_STRING_X509_TRUSTED) == 0);

44 45 46
        d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509;
        i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509;
        X509 *cert = NULL;
47
        const unsigned char *p = data;
48 49 50 51
        unsigned char *buf = NULL;
        unsigned char *bufp;
        long enclen;

52
        if (!trusted
53
            && strcmp(name, PEM_STRING_X509) != 0
54 55
            && strcmp(name, PEM_STRING_X509_OLD) != 0) {
            TEST_error("unexpected PEM object: %s", name);
56
            err = 1;
57
            goto next;
58 59 60 61
        }
        cert = d2i(NULL, &p, len);

        if (cert == NULL || (p - data) != len) {
62
            TEST_error("error parsing input %s", name);
63 64 65 66 67 68 69
            err = 1;
            goto next;
        }

        /* Test traditional 2-pass encoding into caller allocated buffer */
        enclen = i2d(cert, NULL);
        if (len != enclen) {
70 71
            TEST_error("encoded length %ld of %s != input length %ld",
                       enclen, name, len);
72 73 74 75
            err = 1;
            goto next;
        }
        if ((buf = bufp = OPENSSL_malloc(len)) == NULL) {
76
            TEST_perror("malloc");
77 78 79 80 81
            err = 1;
            goto next;
        }
        enclen = i2d(cert, &bufp);
        if (len != enclen) {
82 83
            TEST_error("encoded length %ld of %s != input length %ld",
                       enclen, name, len);
84 85 86 87 88
            err = 1;
            goto next;
        }
        enclen = (long) (bufp - buf);
        if (enclen != len) {
89
            TEST_error("unexpected buffer position after encoding %s", name);
90 91 92 93
            err = 1;
            goto next;
        }
        if (memcmp(buf, data, len) != 0) {
94
            TEST_error("encoded content of %s does not match input", name);
95 96 97 98 99 100 101 102 103
            err = 1;
            goto next;
        }
        OPENSSL_free(buf);
        buf = NULL;

        /* Test 1-pass encoding into library allocated buffer */
        enclen = i2d(cert, &buf);
        if (len != enclen) {
104 105
            TEST_error("encoded length %ld of %s != input length %ld",
                       enclen, name, len);
106 107 108 109
            err = 1;
            goto next;
        }
        if (memcmp(buf, data, len) != 0) {
110
            TEST_error("encoded content of %s does not match input", name);
111 112 113 114 115 116 117 118 119 120 121 122
            err = 1;
            goto next;
        }

        if (trusted) {
            /* Encode just the cert and compare with initial encoding */
            OPENSSL_free(buf);
            buf = NULL;

            /* Test 1-pass encoding into library allocated buffer */
            enclen = i2d(cert, &buf);
            if (enclen > len) {
123 124
                TEST_error("encoded length %ld of %s > input length %ld",
                           enclen, name, len);
125 126 127 128
                err = 1;
                goto next;
            }
            if (memcmp(buf, data, enclen) != 0) {
129
                TEST_error("encoded cert content does not match input");
130 131 132 133 134
                err = 1;
                goto next;
            }
        }

135 136 137
        /*
         * If any of these were null, PEM_read() would have failed.
         */
138 139 140
    next:
        X509_free(cert);
        OPENSSL_free(buf);
141 142 143
        OPENSSL_free(name);
        OPENSSL_free(header);
        OPENSSL_free(data);
144
    }
145
    BIO_free(fp);
146 147 148

    if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
        /* Reached end of PEM file */
P
Pauli 已提交
149
        if (c > 0) {
150 151 152 153 154 155 156 157 158
            ERR_clear_error();
            return 1;
        }
    }

    /* Some other PEM read error */
    return 0;
}

159
int test_main(int argc, char *argv[])
160
{
161
    if (argc < 2) {
162
        TEST_error("usage: %s certfile...", argv[0]);
163
        return 0;
164
    }
165 166 167 168

    files = &argv[1];
    ADD_ALL_TESTS(test_certs, argc - 1);
    return run_tests(argv[0]);
169
}