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 #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 ((certbio = BIO_new_file(certstr, "r")) == NULL) 32 return 0; 33 cert = PEM_read_bio_X509(certbio, NULL, NULL, NULL); 34 BIO_free(certbio); 35 if ((keybio = BIO_new_file(privkeystr, "r")) == NULL) 36 goto end; 37 key = PEM_read_bio_PrivateKey(keybio, NULL, NULL, NULL); 38 BIO_free(keybio); 39 if (cert == NULL || key == NULL) 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 OCSP_BASICRESP *make_dummy_resp(void) 51 { 52 const unsigned char namestr[] = "openssl.example.com"; 53 unsigned char keybytes[128] = {7}; 54 OCSP_BASICRESP *bs = OCSP_BASICRESP_new(); 55 OCSP_BASICRESP *bs_out = NULL; 56 OCSP_CERTID *cid = NULL; 57 ASN1_TIME *thisupd = ASN1_TIME_set(NULL, time(NULL)); 58 ASN1_TIME *nextupd = ASN1_TIME_set(NULL, time(NULL) + 200); 59 X509_NAME *name = X509_NAME_new(); 60 ASN1_BIT_STRING *key = ASN1_BIT_STRING_new(); 61 ASN1_INTEGER *serial = ASN1_INTEGER_new(); 62 63 if (!X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC, 64 namestr, -1, -1, 1) 65 || !ASN1_BIT_STRING_set(key, keybytes, sizeof(keybytes)) 66 || !ASN1_INTEGER_set_uint64(serial, (uint64_t)1)) 67 goto err; 68 cid = OCSP_cert_id_new(EVP_sha256(), name, key, serial); 69 if (bs == NULL 70 || thisupd == NULL 71 || nextupd == NULL 72 || cid == NULL 73 || !OCSP_basic_add1_status(bs, cid, 74 V_OCSP_CERTSTATUS_UNKNOWN, 75 0, NULL, thisupd, nextupd)) 76 goto err; 77 bs_out = bs; 78 bs = NULL; 79 err: 80 ASN1_TIME_free(thisupd); 81 ASN1_TIME_free(nextupd); 82 ASN1_BIT_STRING_free(key); 83 ASN1_INTEGER_free(serial); 84 OCSP_CERTID_free(cid); 85 OCSP_BASICRESP_free(bs); 86 X509_NAME_free(name); 87 return bs_out; 88 } 89 90 static int test_resp_signer(void) 91 { 92 OCSP_BASICRESP *bs = NULL; 93 X509 *signer = NULL, *tmp; 94 EVP_PKEY *key = NULL; 95 STACK_OF(X509) *extra_certs = NULL; 96 int ret = 0; 97 98 /* 99 * Test a response with no certs at all; get the signer from the 100 * extra certs given to OCSP_resp_get0_signer(). 101 */ 102 bs = make_dummy_resp(); 103 extra_certs = sk_X509_new_null(); 104 if (bs == NULL 105 || extra_certs == NULL 106 || !get_cert_and_key(&signer, &key) 107 || !sk_X509_push(extra_certs, signer) 108 || !OCSP_basic_sign(bs, signer, key, EVP_sha1(), 109 NULL, OCSP_NOCERTS)) 110 goto err; 111 if (!OCSP_resp_get0_signer(bs, &tmp, extra_certs) 112 || X509_cmp(tmp, signer) != 0) 113 goto err; 114 OCSP_BASICRESP_free(bs); 115 116 /* Do it again but include the signer cert */ 117 bs = make_dummy_resp(); 118 tmp = NULL; 119 if (bs == NULL 120 || !OCSP_basic_sign(bs, signer, key, EVP_sha1(), 121 NULL, 0)) 122 goto err; 123 if (!OCSP_resp_get0_signer(bs, &tmp, NULL) 124 || X509_cmp(tmp, signer) != 0) 125 goto err; 126 ret = 1; 127 err: 128 OCSP_BASICRESP_free(bs); 129 sk_X509_free(extra_certs); 130 X509_free(signer); 131 EVP_PKEY_free(key); 132 return ret; 133 } 134 #endif 135 136 int main(int argc, char *argv[]) 137 { 138 int testresult = 1; 139 BIO *err = NULL; 140 141 if (argc != 3) { 142 printf("Invalid argument count\n"); 143 return 1; 144 } 145 if ((certstr = argv[1]) == NULL 146 || (privkeystr = argv[2]) == NULL) 147 return 1; 148 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT); 149 150 CRYPTO_set_mem_debug(1); 151 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); 152 153 #ifndef OPENSSL_NO_OCSP 154 ADD_TEST(test_resp_signer); 155 #endif 156 testresult = run_tests(argv[0]); 157 158 #ifndef OPENSSL_NO_CRYPTO_MDEBUG 159 if (CRYPTO_mem_leaks(err) <= 0) 160 testresult = 1; 161 #endif 162 BIO_free(err); 163 164 if (!testresult) 165 printf("PASS\n"); 166 167 return testresult; 168 } 169