2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2018-09-11 12:22:14 +00:00
|
|
|
* Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2001-01-08 01:21:55 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2001-01-08 01:21:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2001-02-24 13:50:06 +00:00
|
|
|
#include <time.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2016-11-08 09:16:45 +00:00
|
|
|
#include <openssl/asn1.h>
|
2001-01-08 01:21:55 +00:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/x509v3.h>
|
|
|
|
#include <openssl/ocsp.h>
|
2015-03-05 13:41:11 +00:00
|
|
|
#include "ocsp_lcl.h"
|
2001-01-08 01:21:55 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Utility functions related to sending OCSP requests and extracting relevant
|
|
|
|
* information from the response.
|
2001-01-08 01:21:55 +00:00
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer:
|
|
|
|
* useful if we want to add extensions.
|
2001-01-08 01:21:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
OCSP_ONEREQ *one = NULL;
|
|
|
|
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((one = OCSP_ONEREQ_new()) == NULL)
|
2016-06-27 19:57:58 +00:00
|
|
|
return NULL;
|
2015-05-01 18:37:16 +00:00
|
|
|
OCSP_CERTID_free(one->reqCert);
|
2015-01-22 03:40:55 +00:00
|
|
|
one->reqCert = cid;
|
2016-07-05 13:59:29 +00:00
|
|
|
if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest.requestList, one)) {
|
|
|
|
one->reqCert = NULL; /* do not free on error */
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2016-07-05 13:59:29 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
return one;
|
|
|
|
err:
|
|
|
|
OCSP_ONEREQ_free(one);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-08 01:21:55 +00:00
|
|
|
|
|
|
|
/* Set requestorName from an X509_NAME structure */
|
|
|
|
|
|
|
|
int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
GENERAL_NAME *gen;
|
2015-05-01 18:37:16 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
gen = GENERAL_NAME_new();
|
|
|
|
if (gen == NULL)
|
|
|
|
return 0;
|
|
|
|
if (!X509_NAME_set(&gen->d.directoryName, nm)) {
|
|
|
|
GENERAL_NAME_free(gen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
gen->type = GEN_DIRNAME;
|
2015-10-06 22:04:08 +00:00
|
|
|
GENERAL_NAME_free(req->tbsRequest.requestorName);
|
|
|
|
req->tbsRequest.requestorName = gen;
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2001-01-08 01:21:55 +00:00
|
|
|
|
|
|
|
/* Add a certificate to an OCSP request */
|
|
|
|
|
|
|
|
int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
OCSP_SIGNATURE *sig;
|
2015-10-30 11:12:26 +00:00
|
|
|
if (req->optionalSignature == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
req->optionalSignature = OCSP_SIGNATURE_new();
|
|
|
|
sig = req->optionalSignature;
|
2015-10-30 11:12:26 +00:00
|
|
|
if (sig == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
2015-10-30 11:12:26 +00:00
|
|
|
if (cert == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
2015-05-06 17:43:59 +00:00
|
|
|
if (sig->certs == NULL
|
|
|
|
&& (sig->certs = sk_X509_new_null()) == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!sk_X509_push(sig->certs, cert))
|
|
|
|
return 0;
|
2015-08-31 19:29:57 +00:00
|
|
|
X509_up_ref(cert);
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-02-05 20:23:54 +00:00
|
|
|
* Sign an OCSP request set the requestorName to the subject name of an
|
2015-01-22 03:40:55 +00:00
|
|
|
* optional signers certificate and include one or more optional certificates
|
|
|
|
* in the request. Behaves like PKCS7_sign().
|
2001-01-08 01:21:55 +00:00
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
int OCSP_request_sign(OCSP_REQUEST *req,
|
|
|
|
X509 *signer,
|
|
|
|
EVP_PKEY *key,
|
|
|
|
const EVP_MD *dgst,
|
|
|
|
STACK_OF(X509) *certs, unsigned long flags)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
X509 *x;
|
|
|
|
|
|
|
|
if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
|
|
|
|
goto err;
|
|
|
|
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((req->optionalSignature = OCSP_SIGNATURE_new()) == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
if (key) {
|
|
|
|
if (!X509_check_private_key(signer, key)) {
|
|
|
|
OCSPerr(OCSP_F_OCSP_REQUEST_SIGN,
|
|
|
|
OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!OCSP_REQUEST_sign(req, key, dgst))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & OCSP_NOCERTS)) {
|
|
|
|
if (!OCSP_request_add1_cert(req, signer))
|
|
|
|
goto err;
|
|
|
|
for (i = 0; i < sk_X509_num(certs); i++) {
|
|
|
|
x = sk_X509_value(certs, i);
|
|
|
|
if (!OCSP_request_add1_cert(req, x))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
err:
|
|
|
|
OCSP_SIGNATURE_free(req->optionalSignature);
|
|
|
|
req->optionalSignature = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-01-08 01:21:55 +00:00
|
|
|
|
|
|
|
/* Get response status */
|
|
|
|
|
|
|
|
int OCSP_response_status(OCSP_RESPONSE *resp)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return ASN1_ENUMERATED_get(resp->responseStatus);
|
|
|
|
}
|
2001-01-08 01:21:55 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Extract basic response from OCSP_RESPONSE or NULL if no basic response
|
|
|
|
* present.
|
2001-01-08 01:21:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
OCSP_RESPBYTES *rb;
|
|
|
|
rb = resp->responseBytes;
|
|
|
|
if (!rb) {
|
|
|
|
OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NO_RESPONSE_DATA);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
|
|
|
|
OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NOT_BASIC_RESPONSE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
|
|
|
|
}
|
|
|
|
|
2016-08-12 20:37:55 +00:00
|
|
|
const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs)
|
2015-03-05 13:41:11 +00:00
|
|
|
{
|
|
|
|
return bs->signature;
|
|
|
|
}
|
|
|
|
|
2018-09-01 04:05:55 +00:00
|
|
|
const X509_ALGOR *OCSP_resp_get0_tbs_sigalg(const OCSP_BASICRESP *bs)
|
|
|
|
{
|
|
|
|
return &bs->signatureAlgorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
const OCSP_RESPDATA *OCSP_resp_get0_respdata(const OCSP_BASICRESP *bs)
|
|
|
|
{
|
|
|
|
return &bs->tbsResponseData;
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-02-05 20:23:54 +00:00
|
|
|
* Return number of OCSP_SINGLERESP responses present in a basic response.
|
2001-01-08 01:21:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int OCSP_resp_count(OCSP_BASICRESP *bs)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (!bs)
|
|
|
|
return -1;
|
2015-10-06 22:04:08 +00:00
|
|
|
return sk_OCSP_SINGLERESP_num(bs->tbsResponseData.responses);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-01-08 01:21:55 +00:00
|
|
|
|
|
|
|
/* Extract an OCSP_SINGLERESP response with a given index */
|
|
|
|
|
|
|
|
OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (!bs)
|
|
|
|
return NULL;
|
2015-10-06 22:04:08 +00:00
|
|
|
return sk_OCSP_SINGLERESP_value(bs->tbsResponseData.responses, idx);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-01-08 01:21:55 +00:00
|
|
|
|
2016-08-12 20:37:55 +00:00
|
|
|
const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP* bs)
|
2016-01-17 15:34:26 +00:00
|
|
|
{
|
|
|
|
return bs->tbsResponseData.producedAt;
|
|
|
|
}
|
|
|
|
|
2016-07-13 13:20:15 +00:00
|
|
|
const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs)
|
|
|
|
{
|
|
|
|
return bs->certs;
|
|
|
|
}
|
|
|
|
|
|
|
|
int OCSP_resp_get0_id(const OCSP_BASICRESP *bs,
|
|
|
|
const ASN1_OCTET_STRING **pid,
|
|
|
|
const X509_NAME **pname)
|
|
|
|
{
|
|
|
|
const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
|
2016-11-08 09:16:45 +00:00
|
|
|
|
2016-07-13 13:20:15 +00:00
|
|
|
if (rid->type == V_OCSP_RESPID_NAME) {
|
|
|
|
*pname = rid->value.byName;
|
|
|
|
*pid = NULL;
|
|
|
|
} else if (rid->type == V_OCSP_RESPID_KEY) {
|
|
|
|
*pid = rid->value.byKey;
|
|
|
|
*pname = NULL;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:16:45 +00:00
|
|
|
int OCSP_resp_get1_id(const OCSP_BASICRESP *bs,
|
|
|
|
ASN1_OCTET_STRING **pid,
|
|
|
|
X509_NAME **pname)
|
|
|
|
{
|
|
|
|
const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
|
|
|
|
|
|
|
|
if (rid->type == V_OCSP_RESPID_NAME) {
|
|
|
|
*pname = X509_NAME_dup(rid->value.byName);
|
|
|
|
*pid = NULL;
|
|
|
|
} else if (rid->type == V_OCSP_RESPID_KEY) {
|
|
|
|
*pid = ASN1_OCTET_STRING_dup(rid->value.byKey);
|
|
|
|
*pname = NULL;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-06-26 17:05:09 +00:00
|
|
|
if (*pname == NULL && *pid == NULL)
|
2016-11-08 09:16:45 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-01-08 01:21:55 +00:00
|
|
|
/* Look single response matching a given certificate ID */
|
|
|
|
|
|
|
|
int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
STACK_OF(OCSP_SINGLERESP) *sresp;
|
|
|
|
OCSP_SINGLERESP *single;
|
|
|
|
if (!bs)
|
|
|
|
return -1;
|
|
|
|
if (last < 0)
|
|
|
|
last = 0;
|
|
|
|
else
|
|
|
|
last++;
|
2015-10-06 22:04:08 +00:00
|
|
|
sresp = bs->tbsResponseData.responses;
|
2015-01-22 03:40:55 +00:00
|
|
|
for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
|
|
|
|
single = sk_OCSP_SINGLERESP_value(sresp, i);
|
|
|
|
if (!OCSP_id_cmp(id, single->certId))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract status information from an OCSP_SINGLERESP structure. Note: the
|
|
|
|
* revtime and reason values are only set if the certificate status is
|
|
|
|
* revoked. Returns numerical value of status.
|
2001-01-08 01:21:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_GENERALIZEDTIME **revtime,
|
|
|
|
ASN1_GENERALIZEDTIME **thisupd,
|
|
|
|
ASN1_GENERALIZEDTIME **nextupd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
OCSP_CERTSTATUS *cst;
|
|
|
|
if (!single)
|
|
|
|
return -1;
|
|
|
|
cst = single->certStatus;
|
|
|
|
ret = cst->type;
|
|
|
|
if (ret == V_OCSP_CERTSTATUS_REVOKED) {
|
|
|
|
OCSP_REVOKEDINFO *rev = cst->value.revoked;
|
|
|
|
if (revtime)
|
|
|
|
*revtime = rev->revocationTime;
|
|
|
|
if (reason) {
|
|
|
|
if (rev->revocationReason)
|
|
|
|
*reason = ASN1_ENUMERATED_get(rev->revocationReason);
|
|
|
|
else
|
|
|
|
*reason = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (thisupd)
|
|
|
|
*thisupd = single->thisUpdate;
|
|
|
|
if (nextupd)
|
|
|
|
*nextupd = single->nextUpdate;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function combines the previous ones: look up a certificate ID and if
|
|
|
|
* found extract status information. Return 0 is successful.
|
2001-01-08 01:21:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
|
2015-01-22 03:40:55 +00:00
|
|
|
int *reason,
|
|
|
|
ASN1_GENERALIZEDTIME **revtime,
|
|
|
|
ASN1_GENERALIZEDTIME **thisupd,
|
|
|
|
ASN1_GENERALIZEDTIME **nextupd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
OCSP_SINGLERESP *single;
|
|
|
|
i = OCSP_resp_find(bs, id, -1);
|
|
|
|
/* Maybe check for multiple responses and give an error? */
|
|
|
|
if (i < 0)
|
|
|
|
return 0;
|
|
|
|
single = OCSP_resp_get0(bs, i);
|
|
|
|
i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
|
|
|
|
if (status)
|
|
|
|
*status = i;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check validity of thisUpdate and nextUpdate fields. It is possible that
|
2016-08-05 17:56:58 +00:00
|
|
|
* the request will take a few seconds to process and/or the time won't be
|
2015-01-22 03:40:55 +00:00
|
|
|
* totally accurate. Therefore to avoid rejecting otherwise valid time we
|
|
|
|
* allow the times to be within 'nsec' of the current time. Also to avoid
|
|
|
|
* accepting very old responses without a nextUpdate field an optional maxage
|
2001-02-24 13:50:06 +00:00
|
|
|
* parameter specifies the maximum age the thisUpdate field can be.
|
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
|
|
|
|
ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
|
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
time_t t_now, t_tmp;
|
|
|
|
time(&t_now);
|
|
|
|
/* Check thisUpdate is valid and not more than nsec in the future */
|
|
|
|
if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
|
|
|
|
OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
t_tmp = t_now + nsec;
|
|
|
|
if (X509_cmp_time(thisupd, &t_tmp) > 0) {
|
|
|
|
OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_NOT_YET_VALID);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If maxsec specified check thisUpdate is not more than maxsec in
|
|
|
|
* the past
|
|
|
|
*/
|
|
|
|
if (maxsec >= 0) {
|
|
|
|
t_tmp = t_now - maxsec;
|
|
|
|
if (X509_cmp_time(thisupd, &t_tmp) < 0) {
|
|
|
|
OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_TOO_OLD);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nextupd)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Check nextUpdate is valid and not more than nsec in the past */
|
|
|
|
if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
|
|
|
|
OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
t_tmp = t_now - nsec;
|
|
|
|
if (X509_cmp_time(nextupd, &t_tmp) < 0) {
|
|
|
|
OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_EXPIRED);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Also don't allow nextUpdate to precede thisUpdate */
|
|
|
|
if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
|
|
|
|
OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
|
|
|
|
OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2016-01-17 15:34:26 +00:00
|
|
|
|
2016-08-12 20:37:55 +00:00
|
|
|
const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single)
|
2016-01-17 15:34:26 +00:00
|
|
|
{
|
|
|
|
return single->certId;
|
|
|
|
}
|