Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 1 | /* |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 2 | * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved. |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the OpenSSL licenses, (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * https://www.openssl.org/source/license.html |
| 8 | * or in the file LICENSE in the source distribution. |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <errno.h> |
| 14 | |
| 15 | #include <openssl/x509.h> |
| 16 | #include <openssl/pem.h> |
| 17 | #include <openssl/conf.h> |
| 18 | #include <openssl/err.h> |
Rich Salz | 176db6d | 2017-08-22 08:35:43 -0400 | [diff] [blame] | 19 | #include "internal/nelem.h" |
Rich Salz | e2a29ad | 2017-04-11 16:16:33 -0400 | [diff] [blame] | 20 | #include "testutil.h" |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 21 | |
Rich Salz | f1e793c | 2017-04-12 09:27:33 -0400 | [diff] [blame] | 22 | static int test_certs(int num) |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 23 | { |
Pauli | 4f58c6b | 2017-06-20 07:58:51 +1000 | [diff] [blame] | 24 | int c; |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 25 | char *name = 0; |
| 26 | char *header = 0; |
| 27 | unsigned char *data = 0; |
| 28 | long len; |
| 29 | typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long); |
| 30 | typedef int (*i2d_X509_t)(X509 *, unsigned char **); |
| 31 | int err = 0; |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 32 | BIO *fp = BIO_new_file(test_get_argument(num), "r"); |
Rich Salz | f1e793c | 2017-04-12 09:27:33 -0400 | [diff] [blame] | 33 | |
| 34 | if (!TEST_ptr(fp)) |
| 35 | return 0; |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 36 | |
Pauli | 4f58c6b | 2017-06-20 07:58:51 +1000 | [diff] [blame] | 37 | for (c = 0; !err && PEM_read_bio(fp, &name, &header, &data, &len); ++c) { |
| 38 | const int trusted = (strcmp(name, PEM_STRING_X509_TRUSTED) == 0); |
| 39 | |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 40 | d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509; |
| 41 | i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509; |
| 42 | X509 *cert = NULL; |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 43 | const unsigned char *p = data; |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 44 | unsigned char *buf = NULL; |
| 45 | unsigned char *bufp; |
| 46 | long enclen; |
| 47 | |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 48 | if (!trusted |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 49 | && strcmp(name, PEM_STRING_X509) != 0 |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 50 | && strcmp(name, PEM_STRING_X509_OLD) != 0) { |
| 51 | TEST_error("unexpected PEM object: %s", name); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 52 | err = 1; |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 53 | goto next; |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 54 | } |
| 55 | cert = d2i(NULL, &p, len); |
| 56 | |
| 57 | if (cert == NULL || (p - data) != len) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 58 | TEST_error("error parsing input %s", name); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 59 | err = 1; |
| 60 | goto next; |
| 61 | } |
| 62 | |
| 63 | /* Test traditional 2-pass encoding into caller allocated buffer */ |
| 64 | enclen = i2d(cert, NULL); |
| 65 | if (len != enclen) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 66 | TEST_error("encoded length %ld of %s != input length %ld", |
| 67 | enclen, name, len); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 68 | err = 1; |
| 69 | goto next; |
| 70 | } |
| 71 | if ((buf = bufp = OPENSSL_malloc(len)) == NULL) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 72 | TEST_perror("malloc"); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 73 | err = 1; |
| 74 | goto next; |
| 75 | } |
| 76 | enclen = i2d(cert, &bufp); |
| 77 | if (len != enclen) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 78 | TEST_error("encoded length %ld of %s != input length %ld", |
| 79 | enclen, name, len); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 80 | err = 1; |
| 81 | goto next; |
| 82 | } |
| 83 | enclen = (long) (bufp - buf); |
| 84 | if (enclen != len) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 85 | TEST_error("unexpected buffer position after encoding %s", name); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 86 | err = 1; |
| 87 | goto next; |
| 88 | } |
| 89 | if (memcmp(buf, data, len) != 0) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 90 | TEST_error("encoded content of %s does not match input", name); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 91 | err = 1; |
| 92 | goto next; |
| 93 | } |
| 94 | OPENSSL_free(buf); |
| 95 | buf = NULL; |
| 96 | |
| 97 | /* Test 1-pass encoding into library allocated buffer */ |
| 98 | enclen = i2d(cert, &buf); |
| 99 | if (len != enclen) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 100 | TEST_error("encoded length %ld of %s != input length %ld", |
| 101 | enclen, name, len); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 102 | err = 1; |
| 103 | goto next; |
| 104 | } |
| 105 | if (memcmp(buf, data, len) != 0) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 106 | TEST_error("encoded content of %s does not match input", name); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 107 | err = 1; |
| 108 | goto next; |
| 109 | } |
| 110 | |
| 111 | if (trusted) { |
| 112 | /* Encode just the cert and compare with initial encoding */ |
| 113 | OPENSSL_free(buf); |
| 114 | buf = NULL; |
| 115 | |
| 116 | /* Test 1-pass encoding into library allocated buffer */ |
| 117 | enclen = i2d(cert, &buf); |
| 118 | if (enclen > len) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 119 | TEST_error("encoded length %ld of %s > input length %ld", |
| 120 | enclen, name, len); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 121 | err = 1; |
| 122 | goto next; |
| 123 | } |
| 124 | if (memcmp(buf, data, enclen) != 0) { |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 125 | TEST_error("encoded cert content does not match input"); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 126 | err = 1; |
| 127 | goto next; |
| 128 | } |
| 129 | } |
| 130 | |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 131 | /* |
| 132 | * If any of these were null, PEM_read() would have failed. |
| 133 | */ |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 134 | next: |
| 135 | X509_free(cert); |
| 136 | OPENSSL_free(buf); |
Pauli | 8fe3127 | 2017-06-19 11:21:22 +1000 | [diff] [blame] | 137 | OPENSSL_free(name); |
| 138 | OPENSSL_free(header); |
| 139 | OPENSSL_free(data); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 140 | } |
Rich Salz | f1e793c | 2017-04-12 09:27:33 -0400 | [diff] [blame] | 141 | BIO_free(fp); |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 142 | |
| 143 | if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) { |
| 144 | /* Reached end of PEM file */ |
Pauli | 4f58c6b | 2017-06-20 07:58:51 +1000 | [diff] [blame] | 145 | if (c > 0) { |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 146 | ERR_clear_error(); |
| 147 | return 1; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* Some other PEM read error */ |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 155 | int setup_tests(void) |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 156 | { |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 157 | size_t n = test_get_argument_count(); |
| 158 | |
| 159 | if (n == 0) { |
| 160 | TEST_error("usage: %s certfile...", test_get_program_name()); |
Rich Salz | f1e793c | 2017-04-12 09:27:33 -0400 | [diff] [blame] | 161 | return 0; |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 162 | } |
Rich Salz | f1e793c | 2017-04-12 09:27:33 -0400 | [diff] [blame] | 163 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 164 | ADD_ALL_TESTS(test_certs, n); |
| 165 | return 1; |
Viktor Dukhovni | fde2257 | 2016-05-02 14:46:51 -0400 | [diff] [blame] | 166 | } |