]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/d2i_test.c
Fix invalid expression syntax
[thirdparty/openssl.git] / test / d2i_test.c
CommitLineData
1400f013 1/*
33388b44 2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
1400f013 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
440e5d80
RS
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
1400f013 7 * https://www.openssl.org/source/license.html
1400f013
EK
8 */
9
10/* Regression tests for ASN.1 parsing bugs. */
11
12#include <stdio.h>
13#include <string.h>
14
15#include "testutil.h"
16
17#include <openssl/asn1.h>
48c1e15c 18#include <openssl/asn1t.h>
1400f013
EK
19#include <openssl/bio.h>
20#include <openssl/err.h>
21#include <openssl/x509.h>
22#include <openssl/x509v3.h>
176db6dc 23#include "internal/nelem.h"
1400f013
EK
24
25static const ASN1_ITEM *item_type;
26static const char *test_file;
27
48c1e15c
DSH
28typedef enum {
29 ASN1_UNKNOWN,
30 ASN1_OK,
31 ASN1_BIO,
32 ASN1_DECODE,
33 ASN1_ENCODE,
34 ASN1_COMPARE
35} expected_error_t;
36
37typedef struct {
38 const char *str;
39 expected_error_t code;
40} error_enum;
41
42static expected_error_t expected_error = ASN1_UNKNOWN;
43
31a80694 44static int test_bad_asn1(void)
1400f013
EK
45{
46 BIO *bio = NULL;
47 ASN1_VALUE *value = NULL;
ababe86b 48 int ret = 0;
1400f013
EK
49 unsigned char buf[2048];
50 const unsigned char *buf_ptr = buf;
48c1e15c
DSH
51 unsigned char *der = NULL;
52 int derlen;
1400f013
EK
53 int len;
54
f5864227
P
55 bio = BIO_new_file(test_file, "r");
56 if (!TEST_ptr(bio))
ababe86b 57 return 0;
1400f013 58
48c1e15c 59 if (expected_error == ASN1_BIO) {
f5864227 60 if (TEST_ptr_null(ASN1_item_d2i_bio(item_type, bio, NULL)))
48c1e15c
DSH
61 ret = 1;
62 goto err;
63 }
64
1400f013 65 /*
48c1e15c
DSH
66 * Unless we are testing it we don't use ASN1_item_d2i_bio because it
67 * performs sanity checks on the input and can reject it before the
68 * decoder is called.
1400f013 69 */
cbe29648 70 len = BIO_read(bio, buf, sizeof(buf));
f5864227 71 if (!TEST_int_ge(len, 0))
1400f013
EK
72 goto err;
73
74 value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type);
48c1e15c 75 if (value == NULL) {
f5864227 76 if (TEST_int_eq(expected_error, ASN1_DECODE))
48c1e15c 77 ret = 1;
1400f013 78 goto err;
48c1e15c
DSH
79 }
80
81 derlen = ASN1_item_i2d(value, &der, item_type);
1400f013 82
48c1e15c 83 if (der == NULL || derlen < 0) {
f5864227 84 if (TEST_int_eq(expected_error, ASN1_ENCODE))
48c1e15c
DSH
85 ret = 1;
86 goto err;
87 }
88
89 if (derlen != len || memcmp(der, buf, derlen) != 0) {
f5864227 90 if (TEST_int_eq(expected_error, ASN1_COMPARE))
48c1e15c
DSH
91 ret = 1;
92 goto err;
93 }
94
f5864227 95 if (TEST_int_eq(expected_error, ASN1_OK))
48c1e15c 96 ret = 1;
1400f013
EK
97
98 err:
53e409db 99 /* Don't indicate success for memory allocation errors */
f5864227
P
100 if (ret == 1
101 && !TEST_false(ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE))
53e409db 102 ret = 0;
1400f013 103 BIO_free(bio);
48c1e15c 104 OPENSSL_free(der);
1400f013
EK
105 ASN1_item_free(value, item_type);
106 return ret;
107}
108
a43ce58f
SL
109OPT_TEST_DECLARE_USAGE("item_name expected_error test_file.der\n")
110
1400f013 111/*
ad887416 112 * Usage: d2i_test <name> <type> <file>, e.g.
1400f013
EK
113 * d2i_test generalname bad_generalname.der
114 */
ad887416 115int setup_tests(void)
1400f013 116{
1400f013 117 const char *test_type_name;
48c1e15c
DSH
118 const char *expected_error_string;
119
120 size_t i;
48c1e15c
DSH
121
122 static error_enum expected_errors[] = {
123 {"OK", ASN1_OK},
124 {"BIO", ASN1_BIO},
125 {"decode", ASN1_DECODE},
126 {"encode", ASN1_ENCODE},
127 {"compare", ASN1_COMPARE}
128 };
129
8d242823
MC
130 if (!test_skip_common_options()) {
131 TEST_error("Error parsing test options\n");
132 return 0;
133 }
134
ad887416
P
135 if (!TEST_ptr(test_type_name = test_get_argument(0))
136 || !TEST_ptr(expected_error_string = test_get_argument(1))
a43ce58f 137 || !TEST_ptr(test_file = test_get_argument(2)))
ad887416 138 return 0;
1400f013 139
adffae15
DSH
140 item_type = ASN1_ITEM_lookup(test_type_name);
141
48c1e15c 142 if (item_type == NULL) {
8fe3127c
P
143 TEST_error("Unknown type %s", test_type_name);
144 TEST_note("Supported types:");
adffae15
DSH
145 for (i = 0;; i++) {
146 const ASN1_ITEM *it = ASN1_ITEM_get(i);
147
148 if (it == NULL)
149 break;
8fe3127c 150 TEST_note("\t%s", it->sname);
48c1e15c 151 }
ad887416 152 return 0;
48c1e15c
DSH
153 }
154
155 for (i = 0; i < OSSL_NELEM(expected_errors); i++) {
156 if (strcmp(expected_errors[i].str, expected_error_string) == 0) {
157 expected_error = expected_errors[i].code;
158 break;
159 }
160 }
161
162 if (expected_error == ASN1_UNKNOWN) {
f5864227 163 TEST_error("Unknown expected error %s\n", expected_error_string);
ad887416 164 return 0;
1400f013
EK
165 }
166
167 ADD_TEST(test_bad_asn1);
ad887416 168 return 1;
1400f013 169}