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