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