Consistent formatting for sizeof(foo)
[openssl.git] / test / ocspapitest.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * 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
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 static int get_cert_and_key(X509 **cert_out, EVP_PKEY **key_out)
25 {
26     BIO *certbio, *keybio;
27     X509 *cert = NULL;
28     EVP_PKEY *key = NULL;
29
30     if (!TEST_ptr(certbio = BIO_new_file(certstr, "r")))
31         return 0;
32     cert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
33     BIO_free(certbio);
34     if (!TEST_ptr(keybio = BIO_new_file(privkeystr, "r")))
35         goto end;
36     key = PEM_read_bio_PrivateKey(keybio, NULL, NULL, NULL);
37     BIO_free(keybio);
38     if (!TEST_ptr(cert) || !TEST_ptr(key))
39         goto end;
40     *cert_out = cert;
41     *key_out = key;
42     return 1;
43  end:
44     X509_free(cert);
45     EVP_PKEY_free(key);
46     return 0;
47 }
48
49 static OCSP_BASICRESP *make_dummy_resp(void)
50 {
51     const unsigned char namestr[] = "openssl.example.com";
52     unsigned char keybytes[128] = {7};
53     OCSP_BASICRESP *bs = OCSP_BASICRESP_new();
54     OCSP_CERTID *cid;
55     ASN1_TIME *thisupd = ASN1_TIME_set(NULL, time(NULL));
56     ASN1_TIME *nextupd = ASN1_TIME_set(NULL, time(NULL) + 200);
57     X509_NAME *name = X509_NAME_new();
58     ASN1_BIT_STRING *key = ASN1_BIT_STRING_new();
59     ASN1_INTEGER *serial = ASN1_INTEGER_new();
60
61     if (!X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC,
62                                    namestr, -1, -1, 1)
63         || !ASN1_BIT_STRING_set(key, keybytes, sizeof(keybytes)
64         || !ASN1_INTEGER_set_uint64(serial, (uint64_t)1)))
65         return NULL;
66     cid = OCSP_cert_id_new(EVP_sha256(), name, key, serial);
67     if (!TEST_ptr(bs)
68         || !TEST_ptr(thisupd)
69         || !TEST_ptr(nextupd)
70         || !TEST_ptr(cid)
71         || !TEST_true(OCSP_basic_add1_status(bs, cid,
72                                              V_OCSP_CERTSTATUS_UNKNOWN,
73                                              0, NULL, thisupd, nextupd)))
74         return NULL;
75     ASN1_TIME_free(thisupd);
76     ASN1_TIME_free(nextupd);
77     ASN1_BIT_STRING_free(key);
78     ASN1_INTEGER_free(serial);
79     OCSP_CERTID_free(cid);
80     X509_NAME_free(name);
81     return bs;
82 }
83
84 #ifndef OPENSSL_NO_OCSP
85 static int test_resp_signer(void)
86 {
87     OCSP_BASICRESP *bs;
88     X509 *signer = NULL, *tmp;
89     EVP_PKEY *key = NULL;
90     STACK_OF(X509) *extra_certs;
91
92     /*
93      * Test a response with no certs at all; get the signer from the
94      * extra certs given to OCSP_resp_get0_signer().
95      */
96     bs = make_dummy_resp();
97     extra_certs = sk_X509_new_null();
98     if (!TEST_ptr(bs)
99         || !TEST_ptr(extra_certs)
100         || !TEST_true(get_cert_and_key(&signer, &key))
101         || !TEST_true(sk_X509_push(extra_certs, signer))
102         || !TEST_true(OCSP_basic_sign(bs, signer, key, EVP_sha1(),
103                                       NULL, OCSP_NOCERTS)))
104         return 0;
105     if (!TEST_true(OCSP_resp_get0_signer(bs, &tmp, extra_certs))
106         || !TEST_int_eq(X509_cmp(tmp, signer), 0))
107         return 0;
108     OCSP_BASICRESP_free(bs);
109
110     /* Do it again but include the signer cert */
111     bs = make_dummy_resp();
112     tmp = NULL;
113     if (!TEST_ptr(bs)
114         || !TEST_true(OCSP_basic_sign(bs, signer, key, EVP_sha1(),
115                                       NULL, 0)))
116         return 0;
117     if (!TEST_true(OCSP_resp_get0_signer(bs, &tmp, NULL))
118         || !TEST_int_eq(X509_cmp(tmp, signer), 0))
119         return 0;
120     OCSP_BASICRESP_free(bs);
121     sk_X509_free(extra_certs);
122     X509_free(signer);
123     EVP_PKEY_free(key);
124     return 1;
125 }
126 #endif
127
128 int setup_tests(void)
129 {
130     if (!TEST_ptr(certstr = test_get_argument(0))
131         || !TEST_ptr(privkeystr = test_get_argument(1)))
132         return 0;
133 #ifndef OPENSSL_NO_OCSP
134     ADD_TEST(test_resp_signer);
135 #endif
136     return 1;
137 }