1 /* $OpenBSD: ocsp_srv.c,v 1.6 2014/07/11 08:44:49 jsing Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 2001. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * openssl-core@openssl.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #include <stdio.h> 60 61 #include <openssl/err.h> 62 #include <openssl/objects.h> 63 #include <openssl/ocsp.h> 64 #include <openssl/pem.h> 65 #include <openssl/rand.h> 66 #include <openssl/x509.h> 67 #include <openssl/x509v3.h> 68 69 /* Utility functions related to sending OCSP responses and extracting 70 * relevant information from the request. 71 */ 72 73 int 74 OCSP_request_onereq_count(OCSP_REQUEST *req) 75 { 76 return sk_OCSP_ONEREQ_num(req->tbsRequest->requestList); 77 } 78 79 OCSP_ONEREQ * 80 OCSP_request_onereq_get0(OCSP_REQUEST *req, int i) 81 { 82 return sk_OCSP_ONEREQ_value(req->tbsRequest->requestList, i); 83 } 84 85 OCSP_CERTID * 86 OCSP_onereq_get0_id(OCSP_ONEREQ *one) 87 { 88 return one->reqCert; 89 } 90 91 int 92 OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd, 93 ASN1_OCTET_STRING **pikeyHash, ASN1_INTEGER **pserial, OCSP_CERTID *cid) 94 { 95 if (!cid) 96 return 0; 97 if (pmd) 98 *pmd = cid->hashAlgorithm->algorithm; 99 if (piNameHash) 100 *piNameHash = cid->issuerNameHash; 101 if (pikeyHash) 102 *pikeyHash = cid->issuerKeyHash; 103 if (pserial) 104 *pserial = cid->serialNumber; 105 return 1; 106 } 107 108 int 109 OCSP_request_is_signed(OCSP_REQUEST *req) 110 { 111 if (req->optionalSignature) 112 return 1; 113 return 0; 114 } 115 116 /* Create an OCSP response and encode an optional basic response */ 117 OCSP_RESPONSE * 118 OCSP_response_create(int status, OCSP_BASICRESP *bs) 119 { 120 OCSP_RESPONSE *rsp = NULL; 121 122 if (!(rsp = OCSP_RESPONSE_new())) 123 goto err; 124 if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status))) 125 goto err; 126 if (!bs) 127 return rsp; 128 if (!(rsp->responseBytes = OCSP_RESPBYTES_new())) 129 goto err; 130 rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic); 131 if (!ASN1_item_pack(bs, ASN1_ITEM_rptr(OCSP_BASICRESP), 132 &rsp->responseBytes->response)) 133 goto err; 134 return rsp; 135 136 err: 137 if (rsp) 138 OCSP_RESPONSE_free(rsp); 139 return NULL; 140 } 141 142 OCSP_SINGLERESP * 143 OCSP_basic_add1_status(OCSP_BASICRESP *rsp, OCSP_CERTID *cid, int status, 144 int reason, ASN1_TIME *revtime, ASN1_TIME *thisupd, ASN1_TIME *nextupd) 145 { 146 OCSP_SINGLERESP *single = NULL; 147 OCSP_CERTSTATUS *cs; 148 OCSP_REVOKEDINFO *ri; 149 150 if (!rsp->tbsResponseData->responses && 151 !(rsp->tbsResponseData->responses = sk_OCSP_SINGLERESP_new_null())) 152 goto err; 153 154 if (!(single = OCSP_SINGLERESP_new())) 155 goto err; 156 157 if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate)) 158 goto err; 159 if (nextupd && 160 !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate)) 161 goto err; 162 163 OCSP_CERTID_free(single->certId); 164 165 if (!(single->certId = OCSP_CERTID_dup(cid))) 166 goto err; 167 168 cs = single->certStatus; 169 switch (cs->type = status) { 170 case V_OCSP_CERTSTATUS_REVOKED: 171 if (!revtime) { 172 OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, 173 OCSP_R_NO_REVOKED_TIME); 174 goto err; 175 } 176 if (!(cs->value.revoked = ri = OCSP_REVOKEDINFO_new())) 177 goto err; 178 if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime)) 179 goto err; 180 if (reason != OCSP_REVOKED_STATUS_NOSTATUS) { 181 if (!(ri->revocationReason = ASN1_ENUMERATED_new())) 182 goto err; 183 if (!(ASN1_ENUMERATED_set(ri->revocationReason, 184 reason))) 185 goto err; 186 } 187 break; 188 189 case V_OCSP_CERTSTATUS_GOOD: 190 cs->value.good = ASN1_NULL_new(); 191 break; 192 193 case V_OCSP_CERTSTATUS_UNKNOWN: 194 cs->value.unknown = ASN1_NULL_new(); 195 break; 196 197 default: 198 goto err; 199 } 200 if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData->responses, single))) 201 goto err; 202 return single; 203 204 err: 205 OCSP_SINGLERESP_free(single); 206 return NULL; 207 } 208 209 /* Add a certificate to an OCSP request */ 210 int 211 OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert) 212 { 213 if (!resp->certs && !(resp->certs = sk_X509_new_null())) 214 return 0; 215 216 if (!sk_X509_push(resp->certs, cert)) 217 return 0; 218 CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509); 219 return 1; 220 } 221 222 int 223 OCSP_basic_sign(OCSP_BASICRESP *brsp, X509 *signer, EVP_PKEY *key, 224 const EVP_MD *dgst, STACK_OF(X509) *certs, unsigned long flags) 225 { 226 int i; 227 OCSP_RESPID *rid; 228 229 if (!X509_check_private_key(signer, key)) { 230 OCSPerr(OCSP_F_OCSP_BASIC_SIGN, 231 OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); 232 goto err; 233 } 234 235 if (!(flags & OCSP_NOCERTS)) { 236 if (!OCSP_basic_add1_cert(brsp, signer)) 237 goto err; 238 for (i = 0; i < sk_X509_num(certs); i++) { 239 X509 *tmpcert = sk_X509_value(certs, i); 240 if (!OCSP_basic_add1_cert(brsp, tmpcert)) 241 goto err; 242 } 243 } 244 245 rid = brsp->tbsResponseData->responderId; 246 if (flags & OCSP_RESPID_KEY) { 247 unsigned char md[SHA_DIGEST_LENGTH]; 248 249 X509_pubkey_digest(signer, EVP_sha1(), md, NULL); 250 if (!(rid->value.byKey = ASN1_OCTET_STRING_new())) 251 goto err; 252 if (!(ASN1_OCTET_STRING_set(rid->value.byKey, md, 253 SHA_DIGEST_LENGTH))) 254 goto err; 255 rid->type = V_OCSP_RESPID_KEY; 256 } else { 257 if (!X509_NAME_set(&rid->value.byName, 258 X509_get_subject_name(signer))) 259 goto err; 260 rid->type = V_OCSP_RESPID_NAME; 261 } 262 263 if (!(flags & OCSP_NOTIME) && 264 !X509_gmtime_adj(brsp->tbsResponseData->producedAt, 0)) 265 goto err; 266 267 /* Right now, I think that not doing double hashing is the right 268 thing. -- Richard Levitte */ 269 270 if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0)) 271 goto err; 272 273 return 1; 274 275 err: 276 return 0; 277 } 278