blob: e771e01362d800fbfd13cd56d4d6a0186397c19a [file] [log] [blame]
Emilia Kasper1400f012016-03-30 22:37:05 +02001/*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
Rich Salz440e5d82016-05-17 14:20:24 -04004 * Licensed under the OpenSSL license (the "License"). You may not use
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
Emilia Kasper1400f012016-03-30 22:37:05 +02007 * https://www.openssl.org/source/license.html
Emilia Kasper1400f012016-03-30 22:37:05 +02008 */
9
10/* Regression tests for ASN.1 parsing bugs. */
11
12#include <stdio.h>
13#include <string.h>
14
15#include "testutil.h"
Emilia Kaspere364c3b2016-11-07 16:53:15 +010016#include "test_main_custom.h"
Emilia Kasper1400f012016-03-30 22:37:05 +020017
18#include <openssl/asn1.h>
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +010019#include <openssl/asn1t.h>
Emilia Kasper1400f012016-03-30 22:37:05 +020020#include <openssl/bio.h>
21#include <openssl/err.h>
22#include <openssl/x509.h>
23#include <openssl/x509v3.h>
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +010024#include "e_os.h"
Emilia Kasper1400f012016-03-30 22:37:05 +020025
26static const ASN1_ITEM *item_type;
27static const char *test_file;
28
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +010029typedef enum {
30 ASN1_UNKNOWN,
31 ASN1_OK,
32 ASN1_BIO,
33 ASN1_DECODE,
34 ASN1_ENCODE,
35 ASN1_COMPARE
36} expected_error_t;
37
38typedef struct {
39 const char *str;
40 expected_error_t code;
41} error_enum;
42
43static expected_error_t expected_error = ASN1_UNKNOWN;
44
Emilia Kasper308b8762016-11-03 17:15:41 +010045static int test_bad_asn1()
Emilia Kasper1400f012016-03-30 22:37:05 +020046{
47 BIO *bio = NULL;
48 ASN1_VALUE *value = NULL;
Emilia Kasperababe862016-04-05 14:29:06 +020049 int ret = 0;
Emilia Kasper1400f012016-03-30 22:37:05 +020050 unsigned char buf[2048];
51 const unsigned char *buf_ptr = buf;
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +010052 unsigned char *der = NULL;
53 int derlen;
Emilia Kasper1400f012016-03-30 22:37:05 +020054 int len;
55
56 if ((bio = BIO_new_file(test_file, "r")) == NULL)
Emilia Kasperababe862016-04-05 14:29:06 +020057 return 0;
Emilia Kasper1400f012016-03-30 22:37:05 +020058
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +010059 if (expected_error == ASN1_BIO) {
60 value = ASN1_item_d2i_bio(item_type, bio, NULL);
61 if (value == NULL)
62 ret = 1;
63 goto err;
64 }
65
Emilia Kasper1400f012016-03-30 22:37:05 +020066 /*
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +010067 * Unless we are testing it we don't use ASN1_item_d2i_bio because it
68 * performs sanity checks on the input and can reject it before the
69 * decoder is called.
Emilia Kasper1400f012016-03-30 22:37:05 +020070 */
71 len = BIO_read(bio, buf, sizeof buf);
72 if (len < 0)
73 goto err;
74
75 value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type);
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +010076 if (value == NULL) {
77 if (expected_error == ASN1_DECODE)
78 ret = 1;
Emilia Kasper1400f012016-03-30 22:37:05 +020079 goto err;
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +010080 }
Emilia Kasper1400f012016-03-30 22:37:05 +020081
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +010082 derlen = ASN1_item_i2d(value, &der, item_type);
83
84 if (der == NULL || derlen < 0) {
85 if (expected_error == ASN1_ENCODE)
86 ret = 1;
87 goto err;
88 }
89
90 if (derlen != len || memcmp(der, buf, derlen) != 0) {
91 if (expected_error == ASN1_COMPARE)
92 ret = 1;
93 goto err;
94 }
95
96 if (expected_error == ASN1_OK)
97 ret = 1;
Emilia Kasper1400f012016-03-30 22:37:05 +020098
99 err:
Dr. Stephen Henson53e409d2016-04-22 18:37:42 +0100100 /* Don't indicate success for memory allocation errors */
101 if (ret == 1 && ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE)
102 ret = 0;
Emilia Kasper1400f012016-03-30 22:37:05 +0200103 BIO_free(bio);
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +0100104 OPENSSL_free(der);
Emilia Kasper1400f012016-03-30 22:37:05 +0200105 ASN1_item_free(value, item_type);
106 return ret;
107}
108
Emilia Kasper1400f012016-03-30 22:37:05 +0200109/*
110 * Usage: d2i_test <type> <file>, e.g.
111 * d2i_test generalname bad_generalname.der
112 */
Emilia Kaspere364c3b2016-11-07 16:53:15 +0100113int test_main(int argc, char *argv[])
Emilia Kasper1400f012016-03-30 22:37:05 +0200114{
Emilia Kasper1400f012016-03-30 22:37:05 +0200115 const char *test_type_name;
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +0100116 const char *expected_error_string;
Emilia Kasper1400f012016-03-30 22:37:05 +0200117
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +0100118 size_t i;
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +0100119
120 static error_enum expected_errors[] = {
121 {"OK", ASN1_OK},
122 {"BIO", ASN1_BIO},
123 {"decode", ASN1_DECODE},
124 {"encode", ASN1_ENCODE},
125 {"compare", ASN1_COMPARE}
126 };
127
128 if (argc != 4) {
129 fprintf(stderr,
130 "Usage: d2i_test item_name expected_error file.der\n");
Emilia Kasper1400f012016-03-30 22:37:05 +0200131 return 1;
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +0100132 }
Emilia Kasper1400f012016-03-30 22:37:05 +0200133
134 test_type_name = argv[1];
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +0100135 expected_error_string = argv[2];
136 test_file = argv[3];
Emilia Kasper1400f012016-03-30 22:37:05 +0200137
Dr. Stephen Hensonadffae12016-09-28 00:24:58 +0100138 item_type = ASN1_ITEM_lookup(test_type_name);
139
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +0100140 if (item_type == NULL) {
141 fprintf(stderr, "Unknown type %s\n", test_type_name);
142 fprintf(stderr, "Supported types:\n");
Dr. Stephen Hensonadffae12016-09-28 00:24:58 +0100143 for (i = 0;; i++) {
144 const ASN1_ITEM *it = ASN1_ITEM_get(i);
145
146 if (it == NULL)
147 break;
Dr. Stephen Henson48c1e152016-04-20 21:37:57 +0100148 fprintf(stderr, "\t%s\n", it->sname);
149 }
150 return 1;
151 }
152
153 for (i = 0; i < OSSL_NELEM(expected_errors); i++) {
154 if (strcmp(expected_errors[i].str, expected_error_string) == 0) {
155 expected_error = expected_errors[i].code;
156 break;
157 }
158 }
159
160 if (expected_error == ASN1_UNKNOWN) {
161 fprintf(stderr, "Unknown expected error %s\n", expected_error_string);
Emilia Kasper1400f012016-03-30 22:37:05 +0200162 return 1;
163 }
164
165 ADD_TEST(test_bad_asn1);
166
Emilia Kaspere364c3b2016-11-07 16:53:15 +0100167 return run_tests(argv[0]);
Emilia Kasper1400f012016-03-30 22:37:05 +0200168}