sl@0: /* ocsp_srv.c */ sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL sl@0: * project 2001. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* Utility functions related to sending OCSP responses and extracting sl@0: * relevant information from the request. sl@0: */ sl@0: sl@0: EXPORT_C int OCSP_request_onereq_count(OCSP_REQUEST *req) sl@0: { sl@0: return sk_OCSP_ONEREQ_num(req->tbsRequest->requestList); sl@0: } sl@0: sl@0: EXPORT_C OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i) sl@0: { sl@0: return sk_OCSP_ONEREQ_value(req->tbsRequest->requestList, i); sl@0: } sl@0: sl@0: EXPORT_C OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one) sl@0: { sl@0: return one->reqCert; sl@0: } sl@0: sl@0: EXPORT_C int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd, sl@0: ASN1_OCTET_STRING **pikeyHash, sl@0: ASN1_INTEGER **pserial, OCSP_CERTID *cid) sl@0: { sl@0: if (!cid) return 0; sl@0: if (pmd) *pmd = cid->hashAlgorithm->algorithm; sl@0: if(piNameHash) *piNameHash = cid->issuerNameHash; sl@0: if (pikeyHash) *pikeyHash = cid->issuerKeyHash; sl@0: if (pserial) *pserial = cid->serialNumber; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int OCSP_request_is_signed(OCSP_REQUEST *req) sl@0: { sl@0: if(req->optionalSignature) return 1; sl@0: return 0; sl@0: } sl@0: sl@0: /* Create an OCSP response and encode an optional basic response */ sl@0: EXPORT_C OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs) sl@0: { sl@0: OCSP_RESPONSE *rsp = NULL; sl@0: sl@0: if (!(rsp = OCSP_RESPONSE_new())) goto err; sl@0: if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status))) goto err; sl@0: if (!bs) return rsp; sl@0: if (!(rsp->responseBytes = OCSP_RESPBYTES_new())) goto err; sl@0: rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic); sl@0: if (!ASN1_item_pack(bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response)) sl@0: goto err; sl@0: return rsp; sl@0: err: sl@0: if (rsp) OCSP_RESPONSE_free(rsp); sl@0: return NULL; sl@0: } sl@0: sl@0: sl@0: EXPORT_C OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp, sl@0: OCSP_CERTID *cid, sl@0: int status, int reason, sl@0: ASN1_TIME *revtime, sl@0: ASN1_TIME *thisupd, ASN1_TIME *nextupd) sl@0: { sl@0: OCSP_SINGLERESP *single = NULL; sl@0: OCSP_CERTSTATUS *cs; sl@0: OCSP_REVOKEDINFO *ri; sl@0: sl@0: if(!rsp->tbsResponseData->responses && sl@0: !(rsp->tbsResponseData->responses = sk_OCSP_SINGLERESP_new_null())) sl@0: goto err; sl@0: sl@0: if (!(single = OCSP_SINGLERESP_new())) sl@0: goto err; sl@0: sl@0: sl@0: sl@0: if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate)) sl@0: goto err; sl@0: if (nextupd && sl@0: !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate)) sl@0: goto err; sl@0: sl@0: OCSP_CERTID_free(single->certId); sl@0: sl@0: if(!(single->certId = OCSP_CERTID_dup(cid))) sl@0: goto err; sl@0: sl@0: cs = single->certStatus; sl@0: switch(cs->type = status) sl@0: { sl@0: case V_OCSP_CERTSTATUS_REVOKED: sl@0: if (!revtime) sl@0: { sl@0: OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS,OCSP_R_NO_REVOKED_TIME); sl@0: goto err; sl@0: } sl@0: if (!(cs->value.revoked = ri = OCSP_REVOKEDINFO_new())) goto err; sl@0: if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime)) sl@0: goto err; sl@0: if (reason != OCSP_REVOKED_STATUS_NOSTATUS) sl@0: { sl@0: if (!(ri->revocationReason = ASN1_ENUMERATED_new())) sl@0: goto err; sl@0: if (!(ASN1_ENUMERATED_set(ri->revocationReason, sl@0: reason))) sl@0: goto err; sl@0: } sl@0: break; sl@0: sl@0: case V_OCSP_CERTSTATUS_GOOD: sl@0: cs->value.good = ASN1_NULL_new(); sl@0: break; sl@0: sl@0: case V_OCSP_CERTSTATUS_UNKNOWN: sl@0: cs->value.unknown = ASN1_NULL_new(); sl@0: break; sl@0: sl@0: default: sl@0: goto err; sl@0: sl@0: } sl@0: if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData->responses, single))) sl@0: goto err; sl@0: return single; sl@0: err: sl@0: OCSP_SINGLERESP_free(single); sl@0: return NULL; sl@0: } sl@0: sl@0: /* Add a certificate to an OCSP request */ sl@0: sl@0: EXPORT_C int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert) sl@0: { sl@0: if (!resp->certs && !(resp->certs = sk_X509_new_null())) sl@0: return 0; sl@0: sl@0: if(!sk_X509_push(resp->certs, cert)) return 0; sl@0: CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509); sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int OCSP_basic_sign(OCSP_BASICRESP *brsp, sl@0: X509 *signer, EVP_PKEY *key, const EVP_MD *dgst, sl@0: STACK_OF(X509) *certs, unsigned long flags) sl@0: { sl@0: int i; sl@0: OCSP_RESPID *rid; sl@0: sl@0: if (!X509_check_private_key(signer, key)) sl@0: { sl@0: OCSPerr(OCSP_F_OCSP_BASIC_SIGN, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); sl@0: goto err; sl@0: } sl@0: sl@0: if(!(flags & OCSP_NOCERTS)) sl@0: { sl@0: if(!OCSP_basic_add1_cert(brsp, signer)) sl@0: goto err; sl@0: for (i = 0; i < sk_X509_num(certs); i++) sl@0: { sl@0: X509 *tmpcert = sk_X509_value(certs, i); sl@0: if(!OCSP_basic_add1_cert(brsp, tmpcert)) sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: rid = brsp->tbsResponseData->responderId; sl@0: if (flags & OCSP_RESPID_KEY) sl@0: { sl@0: unsigned char md[SHA_DIGEST_LENGTH]; sl@0: X509_pubkey_digest(signer, EVP_sha1(), md, NULL); sl@0: if (!(rid->value.byKey = ASN1_OCTET_STRING_new())) sl@0: goto err; sl@0: if (!(ASN1_OCTET_STRING_set(rid->value.byKey, md, SHA_DIGEST_LENGTH))) sl@0: goto err; sl@0: rid->type = V_OCSP_RESPID_KEY; sl@0: } sl@0: else sl@0: { sl@0: if (!X509_NAME_set(&rid->value.byName, sl@0: X509_get_subject_name(signer))) sl@0: goto err; sl@0: rid->type = V_OCSP_RESPID_NAME; sl@0: } sl@0: sl@0: if (!(flags & OCSP_NOTIME) && sl@0: !X509_gmtime_adj(brsp->tbsResponseData->producedAt, 0)) sl@0: goto err; sl@0: sl@0: /* Right now, I think that not doing double hashing is the right sl@0: thing. -- Richard Levitte */ sl@0: sl@0: if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0)) goto err; sl@0: sl@0: return 1; sl@0: err: sl@0: return 0; sl@0: }