ocspapitest: properly check the return of memory-allocating functions
[openssl.git] / test / ocspapitest.c
1 /*
2  * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11
12 #include <openssl/opensslconf.h>
13 #include <openssl/crypto.h>
14 #include <openssl/ocsp.h>
15 #include <openssl/x509.h>
16 #include <openssl/asn1.h>
17 #include <openssl/pem.h>
18
19 #include "testutil.h"
20
21 static const char *certstr;
22 static const char *privkeystr;
23
24 #ifndef OPENSSL_NO_OCSP
25 static int get_cert_and_key(X509 **cert_out, EVP_PKEY **key_out)
26 {
27     BIO *certbio, *keybio;
28     X509 *cert = NULL;
29     EVP_PKEY *key = NULL;
30
31     if (!TEST_ptr(certbio = BIO_new_file(certstr, "r")))
32         return 0;
33     cert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
34     BIO_free(certbio);
35     if (!TEST_ptr(keybio = BIO_new_file(privkeystr, "r")))
36         goto end;
37     key = PEM_read_bio_PrivateKey(keybio, NULL, NULL, NULL);
38     BIO_free(keybio);
39     if (!TEST_ptr(cert) || !TEST_ptr(key))
40         goto end;
41     *cert_out = cert;
42     *key_out = key;
43     return 1;
44  end:
45     X509_free(cert);
46     EVP_PKEY_free(key);
47     return 0;
48 }
49
50 static int get_cert(X509 **cert_out)
51 {
52     BIO *certbio;
53     X509 *cert = NULL;
54
55     if (!TEST_ptr(certbio = BIO_new_file(certstr, "r")))
56         return 0;
57     cert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
58     BIO_free(certbio);
59     if (!TEST_ptr(cert))
60         goto end;
61     *cert_out = cert;
62     return 1;
63  end:
64     X509_free(cert);
65     return 0;
66 }
67
68 static OCSP_BASICRESP *make_dummy_resp(void)
69 {
70     const unsigned char namestr[] = "openssl.example.com";
71     unsigned char keybytes[128] = {7};
72     OCSP_BASICRESP *bs = OCSP_BASICRESP_new();
73     OCSP_BASICRESP *bs_out = NULL;
74     OCSP_CERTID *cid = NULL;
75     ASN1_TIME *thisupd = ASN1_TIME_set(NULL, time(NULL));
76     ASN1_TIME *nextupd = ASN1_TIME_set(NULL, time(NULL) + 200);
77     X509_NAME *name = X509_NAME_new();
78     ASN1_BIT_STRING *key = ASN1_BIT_STRING_new();
79     ASN1_INTEGER *serial = ASN1_INTEGER_new();
80
81     if (!TEST_ptr(name)
82         || !TEST_ptr(key)
83         || !TEST_ptr(serial)
84         || !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC,
85                                    namestr, -1, -1, 1)
86         || !ASN1_BIT_STRING_set(key, keybytes, sizeof(keybytes))
87         || !ASN1_INTEGER_set_uint64(serial, (uint64_t)1))
88         goto err;
89     cid = OCSP_cert_id_new(EVP_sha256(), name, key, serial);
90     if (!TEST_ptr(bs)
91         || !TEST_ptr(thisupd)
92         || !TEST_ptr(nextupd)
93         || !TEST_ptr(cid)
94         || !TEST_true(OCSP_basic_add1_status(bs, cid,
95                                              V_OCSP_CERTSTATUS_UNKNOWN,
96                                              0, NULL, thisupd, nextupd)))
97         goto err;
98     bs_out = bs;
99     bs = NULL;
100  err:
101     ASN1_TIME_free(thisupd);
102     ASN1_TIME_free(nextupd);
103     ASN1_BIT_STRING_free(key);
104     ASN1_INTEGER_free(serial);
105     OCSP_CERTID_free(cid);
106     OCSP_BASICRESP_free(bs);
107     X509_NAME_free(name);
108     return bs_out;
109 }
110
111 static int test_resp_signer(void)
112 {
113     OCSP_BASICRESP *bs = NULL;
114     X509 *signer = NULL, *tmp;
115     EVP_PKEY *key = NULL;
116     STACK_OF(X509) *extra_certs = NULL;
117     int ret = 0;
118
119     /*
120      * Test a response with no certs at all; get the signer from the
121      * extra certs given to OCSP_resp_get0_signer().
122      */
123     bs = make_dummy_resp();
124     extra_certs = sk_X509_new_null();
125     if (!TEST_ptr(bs)
126         || !TEST_ptr(extra_certs)
127         || !TEST_true(get_cert_and_key(&signer, &key))
128         || !TEST_true(sk_X509_push(extra_certs, signer))
129         || !TEST_true(OCSP_basic_sign(bs, signer, key, EVP_sha1(),
130                                       NULL, OCSP_NOCERTS)))
131         goto err;
132     if (!TEST_true(OCSP_resp_get0_signer(bs, &tmp, extra_certs))
133         || !TEST_int_eq(X509_cmp(tmp, signer), 0))
134         goto err;
135     OCSP_BASICRESP_free(bs);
136
137     /* Do it again but include the signer cert */
138     bs = make_dummy_resp();
139     tmp = NULL;
140     if (!TEST_ptr(bs)
141         || !TEST_true(OCSP_basic_sign(bs, signer, key, EVP_sha1(),
142                                       NULL, 0)))
143         goto err;
144     if (!TEST_true(OCSP_resp_get0_signer(bs, &tmp, NULL))
145         || !TEST_int_eq(X509_cmp(tmp, signer), 0))
146         goto err;
147     ret = 1;
148  err:
149     OCSP_BASICRESP_free(bs);
150     sk_X509_free(extra_certs);
151     X509_free(signer);
152     EVP_PKEY_free(key);
153     return ret;
154 }
155
156 static int test_access_description(int testcase)
157 {
158     ACCESS_DESCRIPTION *ad = ACCESS_DESCRIPTION_new();
159     int ret = 0;
160
161     if (!TEST_ptr(ad))
162         goto err;
163
164     switch (testcase) {
165     case 0:     /* no change */
166         break;
167     case 1:     /* check and release current location */
168         if (!TEST_ptr(ad->location))
169             goto err;
170         GENERAL_NAME_free(ad->location);
171         ad->location = NULL;
172         break;
173     case 2:     /* replace current location */
174         GENERAL_NAME_free(ad->location);
175         ad->location = GENERAL_NAME_new();
176         if (!TEST_ptr(ad->location))
177             goto err;
178         break;
179     }
180     ACCESS_DESCRIPTION_free(ad);
181     ret = 1;
182 err:
183     return ret;
184 }
185
186 static int test_ocsp_url_svcloc_new(void)
187 {
188     static const char *urls[] = {
189         "www.openssl.org",
190         "www.openssl.net",
191         NULL
192     };
193
194     X509 *issuer = NULL;
195     X509_EXTENSION * ext = NULL;
196     int ret = 0;
197
198     if (!TEST_true(get_cert(&issuer)))
199         goto err;
200
201     /*
202      * Test calling this ocsp method to catch any memory leak
203      */
204     ext = OCSP_url_svcloc_new(X509_get_issuer_name(issuer), urls);
205     if (!TEST_ptr(ext))
206         goto err;
207
208     X509_EXTENSION_free(ext);
209     ret = 1;
210 err:
211     X509_free(issuer);
212     return ret;
213 }
214
215 #endif /* OPENSSL_NO_OCSP */
216
217 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
218
219 int setup_tests(void)
220 {
221     if (!test_skip_common_options()) {
222         TEST_error("Error parsing test options\n");
223         return 0;
224     }
225
226     if (!TEST_ptr(certstr = test_get_argument(0))
227         || !TEST_ptr(privkeystr = test_get_argument(1)))
228         return 0;
229 #ifndef OPENSSL_NO_OCSP
230     ADD_TEST(test_resp_signer);
231     ADD_ALL_TESTS(test_access_description, 3);
232     ADD_TEST(test_ocsp_url_svcloc_new);
233 #endif
234     return 1;
235 }