aboutsummaryrefslogtreecommitdiffstats
path: root/test/ocsptest.c
blob: 96dc07423b833dc86cbe8ad611bbe0fd722e3be5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Copyright 2026 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <time.h>
#include <openssl/bio.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#ifndef OPENSSL_NO_OCSP
#include <openssl/ocsp.h>
#endif

#include "testutil.h"

#ifndef OPENSSL_NO_OCSP

/*
 * Fixtures for the OCSP stapled-response verification path
 * (check_cert_ocsp_resp() in x509_vfy.c), reached from X509_verify_cert()
 * when X509_V_FLAG_OCSP_RESP_CHECK is set and responses are attached with
 * X509_STORE_CTX_set_ocsp_resp().
 *
 *   Root CA  (self-signed trust anchor)
 *       \-- leaf  (signed by the Root CA)
 *
 * The flat chain makes the root both the trust anchor and the authorized OCSP
 * responder for the leaf, and having the root key lets each test build its own
 * signed responses at run time. The certificates have a long validity because
 * OCSP_check_validity() compares against the wall clock and cannot be pinned
 * via X509_VERIFY_PARAM_set_time().
 *
 * The arrays below are generated by the test-tools ocsptest command.
 */

static const char *kOcspTestRoot[] = {
    "-----BEGIN CERTIFICATE-----\n",
    "MIID+DCCAuCgAwIBAgIBATANBgkqhkiG9w0BAQsFADCBmjELMAkGA1UEBhMCVVMx\n",
    "EzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDVNhbiBGcmFuY2lzY28xFTAT\n",
    "BgNVBAoMDEV4YW1wbGUgQ29ycDEeMBwGA1UECwwVQ2VydGlmaWNhdGUgQXV0aG9y\n",
    "aXR5MScwJQYDVQQDDB5FeGFtcGxlIENvcnAgT0NTUCBUZXN0IFJvb3QgQ0EwIBcN\n",
    "MjYwMTAxMDAwMDAwWhgPMjEyNjAxMDEwMDAwMDBaMIGaMQswCQYDVQQGEwJVUzET\n",
    "MBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNjbzEVMBMG\n",
    "A1UECgwMRXhhbXBsZSBDb3JwMR4wHAYDVQQLDBVDZXJ0aWZpY2F0ZSBBdXRob3Jp\n",
    "dHkxJzAlBgNVBAMMHkV4YW1wbGUgQ29ycCBPQ1NQIFRlc3QgUm9vdCBDQTCCASIw\n",
    "DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKuZRsVUiwQoVlwfXY0nVpvMQiJN\n",
    "Su5YfrxzKJ9sMfPI2rEg9d8kW2qsfnCB0isj+SK+CkcfVu5aXcUVUnp08VuKpsI9\n",
    "idkeC7bhXkQgBR1p1mBwChc+UUD6qEindJPhFxYur+7gCmpamSHYr8iYmjVe78l8\n",
    "vRHVuw8KMsiGegvPsuxIYWdY4mgKSCu3o6DeK4XehXU4Ll5j0fGX5eJgbaI6g8qE\n",
    "j71J6G8Y4Ur/WqzTp/GZueTY0wzU8k45HS5uWarGYB2PH8DfM2SRQBw5hYd2xIvS\n",
    "jgFYzCXeIZFnHVCMeB//VaUyYGIaw+Dreh+Wb5zBPh9FyJkrOXYyMeFZUScCAwEA\n",
    "AaNFMEMwEgYDVR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwHQYDVR0O\n",
    "BBYEFGSeLzT7Ur6wrLpFYVDHmNi/TQp5MA0GCSqGSIb3DQEBCwUAA4IBAQAMMukk\n",
    "I0VRfxY1vZngu3LdnBk5If6TbQ6lmWatT4q3BrAID3L9zG/qev6DlyruvnVqoNdU\n",
    "WcWXnzFSDInOKM64wU2oxdosMPWuAKKLHgDdCicikJtF+N0qJgOcyD+Gv9t0kwDB\n",
    "O094nZUgrkCafJiy5y7KtZvqOjcvfEK+oD7fXX/VURv8NMOxpuassPHnHGvHqwKu\n",
    "7jBZMjkOoZH4hJSo5EzZxRZHz6ay7Q8e+6C6PG8CIYXpIqukjVNq9jBb2yog67mW\n",
    "lHlfnEENCyQDSmnEXAVNpJS67jmcItoD1utnDINVzu87DY9WiLZB06Y1xSp1ie7s\n",
    "FIfS7bZ1cxVkt5wS\n",
    "-----END CERTIFICATE-----\n",
    NULL
};

static const char *kOcspTestRootKey[] = {
    "-----BEGIN RSA PRIVATE KEY-----\n",
    "MIIEowIBAAKC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9s67JT53l02RIVTog/VRHbKsOnj+d9oXglGcxALT\n",
    "qG02lKocNHzrPUXEw/EIMETKR6WvATZCzhM9mpsCgYB9RoKfb27A4Cgun6husS+T\n",
    "o3IuUR1KzSvkux+BIRQjcF8fwh3gzb3u9wcn7satJBNeAjvmaW2U47H7AxAwfMPr\n",
    "jQXo0oIBc29LJkVrkJaNFCQOFQQcRHJLFUZdPT8xASngHKMgNvAxkW+1rIz+ixRb\n",
    "Q6CgK9oPOkmRjtd7thFBaQKBgQCMbt0QBhRWe0D0tCbHqFaTWJBVPYt60oF9hQFo\n",
    "VHZiu6EVHQMx8ihimT6hKdc6ps+nm4YHtXez6v07BWxOyW8DXDlx2XyfOexOk7W2\n",
    "pbcXsr6eW4XJbipgjX0A+pPgkhJsRt26tfi3QVhH0i4XFx7c7mB1Dp9JVE7jqzIh\n",
    "B7Y28QKBgCg93v7zP5GFmOa6zCZreIIfi1dy50otxfqYewcXy4Egt90cE67vY55u\n",
    "3B44ySrLkKDuzX2Inrwp5Vao8b9pDQ/AisRzt617eO1H5JtRF3lzZJukYC6j4urU\n",
    "24kB2IhZCB6/3pwGiS21ma9E6OF9tO5YaGGYo02ZqkoflN+XgoeZ\n",
    "-----END RSA PRIVATE KEY-----\n",
    NULL
};

static const char *kOcspTestLeaf[] = {
    "-----BEGIN CERTIFICATE-----\n",
    "MIIEOzCCAyOgAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgZoxCzAJBgNVBAYTAlVT\n",
    "MRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1TYW4gRnJhbmNpc2NvMRUw\n",
    "EwYDVQQKDAxFeGFtcGxlIENvcnAxHjAcBgNVBAsMFUNlcnRpZmljYXRlIEF1dGhv\n",
    "cml0eTEnMCUGA1UEAwweRXhhbXBsZSBDb3JwIE9DU1AgVGVzdCBSb290IENBMCAX\n",
    "DTI2MDEwMTAwMDAwMFoYDzIxMjYwMTAxMDAwMDAwWjCBiDELMAkGA1UEBhMC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",
    "z9pN8YoCiXh/I/IxVIWunk7Dla+Gr6BDJ762iQMMzPQ5D3cRAe9BDRgGiN0lPeIa\n",
    "LMtqDeZZ+dR0KZAr3yZcfyci8SNXwCBbjsoVjmuUj6dlN12pRKxxtBSa23KT6/Dy\n",
    "ijXZ+PtyIJz8FVBxO0RoECLimwbUPHfNjAWjuJkugA==\n",
    "-----END CERTIFICATE-----\n",
    NULL
};

/* Load the fixed PKI. Any of the out params may be NULL to skip it. */
static int load_pki(X509 **root, EVP_PKEY **root_key, X509 **leaf)
{
    if (root != NULL && !TEST_ptr(*root = X509_from_strings(kOcspTestRoot)))
        return 0;
    if (root_key != NULL
        && !TEST_ptr(*root_key = PKEY_from_strings(kOcspTestRootKey)))
        return 0;
    if (leaf != NULL && !TEST_ptr(*leaf = X509_from_strings(kOcspTestLeaf)))
        return 0;
    return 1;
}

/*
 * Build a signed OCSP response for |cert| (issued by |issuer|). |resp_status|
 * is the outer response status, |cert_status| the single-response certificate
 * status. thisUpdate and nextUpdate are the current time offset by
 * |this_off_sec| and |next_off_sec| seconds, so callers can also produce
 * expired or not-yet-valid responses. The response is signed by
 * |signer|/|signer_key|. Returns a response the caller must free, or NULL.
 */
static OCSP_RESPONSE *make_ocsp_response(X509 *cert, X509 *issuer,
    int resp_status, int cert_status, int this_off_sec, int next_off_sec,
    X509 *signer, EVP_PKEY *signer_key)
{
    OCSP_RESPONSE *resp = NULL;
    OCSP_BASICRESP *bs = NULL;
    OCSP_CERTID *cid = NULL;
    ASN1_TIME *thisupd = NULL, *nextupd = NULL, *revtime = NULL;

    if (cert_status == V_OCSP_CERTSTATUS_REVOKED
        && !TEST_ptr(revtime = X509_gmtime_adj(NULL, 0)))
        goto end;

    if (!TEST_ptr(bs = OCSP_BASICRESP_new())
        || !TEST_ptr(thisupd = X509_time_adj_ex(NULL, 0, this_off_sec, NULL))
        || !TEST_ptr(nextupd = X509_time_adj_ex(NULL, 0, next_off_sec, NULL))
        || !TEST_ptr(cid = OCSP_cert_to_id(EVP_sha256(), cert, issuer))
        || !TEST_ptr(OCSP_basic_add1_status(bs, cid, cert_status, 0, revtime,
            thisupd, nextupd))
        || !TEST_true(OCSP_basic_sign(bs, signer, signer_key, EVP_sha256(),
            NULL, OCSP_NOCERTS)))
        goto end;

    resp = OCSP_response_create(resp_status, bs);

end:
    ASN1_TIME_free(revtime);
    ASN1_TIME_free(thisupd);
    ASN1_TIME_free(nextupd);
    OCSP_CERTID_free(cid);
    OCSP_BASICRESP_free(bs);
    return resp;
}

/* Wrap |resp| into a stack, taking ownership on success. */
static STACK_OF(OCSP_RESPONSE) *make_ocsp_resp_stack(OCSP_RESPONSE *resp)
{
    STACK_OF(OCSP_RESPONSE) *sk = sk_OCSP_RESPONSE_new_null();

    if (!TEST_ptr(sk))
        return NULL;
    if (!TEST_true(sk_OCSP_RESPONSE_push(sk, resp))) {
        sk_OCSP_RESPONSE_free(sk);
        return NULL;
    }
    return sk;
}

/*
 * Verify |leaf| against |root| with the stapled |resps| and |flags|, taking
 * ownership of |resps|. Returns X509_V_OK or an X509_V_ERR_xxx code. The
 * X509_verify_cert() call is wrapped for malloc-failure injection.
 */
static int verify_ocsp(X509 *leaf, X509 *root, STACK_OF(OCSP_RESPONSE) *resps,
    unsigned long flags)
{
    X509_STORE *store = X509_STORE_new();
    X509_STORE_CTX *ctx = X509_STORE_CTX_new();
    X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
    int status = X509_V_ERR_UNSPECIFIED;

    if (!TEST_ptr(store) || !TEST_ptr(ctx) || !TEST_ptr(param))
        goto end;

    if (!TEST_true(X509_STORE_add_cert(store, root))
        || !TEST_true(X509_STORE_CTX_init(ctx, store, leaf, NULL)))
        goto end;

    X509_STORE_CTX_set_ocsp_resp(ctx, resps);

    X509_VERIFY_PARAM_set_depth(param, 16);
    if (flags != 0)
        X509_VERIFY_PARAM_set_flags(param, flags);
    X509_STORE_CTX_set0_param(ctx, param);
    param = NULL;

    ERR_clear_error();
    MFAIL_start();
    status = X509_verify_cert(ctx) == 1 ? X509_V_OK
                                        : X509_STORE_CTX_get_error(ctx);
    MFAIL_end();

end:
    X509_VERIFY_PARAM_free(param);
    X509_STORE_CTX_free(ctx);
    X509_STORE_free(store);
    sk_OCSP_RESPONSE_pop_free(resps, OCSP_RESPONSE_free);
    return status;
}

/*
 * Build a single response for the leaf and verify it, expecting |expected|.
 * Every failure inside check_cert_ocsp_resp() surfaces as
 * X509_V_ERR_OCSP_VERIFY_FAILED at the X509_verify_cert() level.
 */
static int run_ocsp_verify(int resp_status, int cert_status, int this_off_sec,
    int next_off_sec, int expected)
{
    X509 *root = NULL, *leaf = NULL;
    EVP_PKEY *root_key = NULL;
    OCSP_RESPONSE *resp = NULL;
    STACK_OF(OCSP_RESPONSE) *resps = NULL;
    int testresult = 0;

    if (!load_pki(&root, &root_key, &leaf))
        goto end;

    if (!TEST_ptr(resp = make_ocsp_response(leaf, root, resp_status, cert_status,
                      this_off_sec, next_off_sec, root, root_key))
        || !TEST_ptr(resps = make_ocsp_resp_stack(resp)))
        goto end;
    resp = NULL; /* owned by resps */

    testresult = TEST_int_eq(verify_ocsp(leaf, root, resps,
                                 X509_V_FLAG_OCSP_RESP_CHECK),
        expected);
    resps = NULL; /* freed by verify_ocsp */

end:
    sk_OCSP_RESPONSE_pop_free(resps, OCSP_RESPONSE_free);
    OCSP_RESPONSE_free(resp);
    EVP_PKEY_free(root_key);
    X509_free(leaf);
    X509_free(root);
    return testresult;
}

/*
 * A good response that expired a moment ago is still accepted, because stapled
 * responses are honoured for up to five minutes past nextUpdate.
 */
static int test_ocsp_resp_good(void)
{
    return run_ocsp_verify(OCSP_RESPONSE_STATUS_SUCCESSFUL,
        V_OCSP_CERTSTATUS_GOOD, -10 * 60, -2 * 60, X509_V_OK);
}

/* An outer response status other than successful is rejected. */
static int test_ocsp_resp_not_successful(void)
{
    return run_ocsp_verify(OCSP_RESPONSE_STATUS_TRYLATER,
        V_OCSP_CERTSTATUS_GOOD, 0, 24 * 60 * 60, X509_V_ERR_OCSP_VERIFY_FAILED);
}

/* A response more than five minutes past nextUpdate is rejected as expired. */
static int test_ocsp_resp_expired(void)
{
    return run_ocsp_verify(OCSP_RESPONSE_STATUS_SUCCESSFUL,
        V_OCSP_CERTSTATUS_GOOD, -20 * 60, -10 * 60, X509_V_ERR_OCSP_VERIFY_FAILED);
}

/* No response for the leaf's depth: sk_OCSP_RESPONSE_num() <= error_depth. */
static int test_ocsp_resp_none(void)
{
    X509 *root = NULL, *leaf = NULL;
    STACK_OF(OCSP_RESPONSE) *resps = NULL;
    int testresult = 0;

    if (!load_pki(&root, NULL, &leaf)
        || !TEST_ptr(resps = sk_OCSP_RESPONSE_new_null()))
        goto end;

    testresult = TEST_int_eq(verify_ocsp(leaf, root, resps,
                                 X509_V_FLAG_OCSP_RESP_CHECK),
        X509_V_ERR_OCSP_VERIFY_FAILED);
    resps = NULL; /* freed by verify_ocsp */

end:
    sk_OCSP_RESPONSE_pop_free(resps, OCSP_RESPONSE_free);
    X509_free(leaf);
    X509_free(root);
    return testresult;
}

/*
 * A well-formed response carrying a single response for a different certificate:
 * no CertID matches the leaf, so no status is found for it.
 */
static int test_ocsp_resp_wrong_cert(void)
{
    X509 *root = NULL, *leaf = NULL;
    EVP_PKEY *root_key = NULL;
    OCSP_RESPONSE *resp = NULL;
    STACK_OF(OCSP_RESPONSE) *resps = NULL;
    int testresult = 0;

    if (!load_pki(&root, &root_key, &leaf))
        goto end;

    /* the response is about the root, not the leaf being verified */
    if (!TEST_ptr(resp = make_ocsp_response(root, root,
                      OCSP_RESPONSE_STATUS_SUCCESSFUL, V_OCSP_CERTSTATUS_GOOD, 0,
                      24 * 60 * 60, root, root_key))
        || !TEST_ptr(resps = make_ocsp_resp_stack(resp)))
        goto end;
    resp = NULL; /* owned by resps */

    testresult = TEST_int_eq(verify_ocsp(leaf, root, resps,
                                 X509_V_FLAG_OCSP_RESP_CHECK),
        X509_V_ERR_OCSP_VERIFY_FAILED);
    resps = NULL; /* freed by verify_ocsp */

end:
    sk_OCSP_RESPONSE_pop_free(resps, OCSP_RESPONSE_free);
    OCSP_RESPONSE_free(resp);
    EVP_PKEY_free(root_key);
    X509_free(leaf);
    X509_free(root);
    return testresult;
}

/* Exercise the success path under mfail. */
static int test_ocsp_resp_mfail(void)
{
    X509 *root = NULL, *leaf = NULL;
    EVP_PKEY *root_key = NULL;
    OCSP_RESPONSE *resp = NULL;
    STACK_OF(OCSP_RESPONSE) *resps = NULL;
    int testresult = 0;

    if (!load_pki(&root, &root_key, &leaf))
        goto end;

    if (!TEST_ptr(resp = make_ocsp_response(leaf, root,
                      OCSP_RESPONSE_STATUS_SUCCESSFUL, V_OCSP_CERTSTATUS_GOOD, 0,
                      24 * 60 * 60, root, root_key))
        || !TEST_ptr(resps = make_ocsp_resp_stack(resp)))
        goto end;
    resp = NULL; /* owned by resps */

    testresult = verify_ocsp(leaf, root, resps, X509_V_FLAG_OCSP_RESP_CHECK)
        == X509_V_OK;
    resps = NULL; /* freed by verify_ocsp */

end:
    sk_OCSP_RESPONSE_pop_free(resps, OCSP_RESPONSE_free);
    OCSP_RESPONSE_free(resp);
    EVP_PKEY_free(root_key);
    X509_free(leaf);
    X509_free(root);
    return testresult;
}

#endif /* OPENSSL_NO_OCSP */

int setup_tests(void)
{
#ifndef OPENSSL_NO_OCSP
    ADD_TEST(test_ocsp_resp_good);
    ADD_TEST(test_ocsp_resp_not_successful);
    ADD_TEST(test_ocsp_resp_expired);
    ADD_TEST(test_ocsp_resp_none);
    ADD_TEST(test_ocsp_resp_wrong_cert);
    ADD_MFAIL_NO_CHECK_TEST(test_ocsp_resp_mfail);
#endif
    return 1;
}