| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include "../asn1/internal.h" |
| 5 |
#include "internal.h" |
| 6 |
|
| 7 |
OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid) { |
| 8 |
OCSP_ONEREQ *one = OCSP_ONEREQ_new(); |
| 9 |
if (one == NULL) { |
| 10 |
return NULL; |
| 11 |
} |
| 12 |
// Reassign |OCSP_CERTID| allocated by OCSP_ONEREQ_new(). |
| 13 |
OCSP_CERTID_free(one->reqCert); |
| 14 |
one->reqCert = cid; |
| 15 |
if (req != NULL && !sk_OCSP_ONEREQ_push(req->tbsRequest->requestList, one)) { |
| 16 |
one->reqCert = NULL; // do not free on error |
| 17 |
OCSP_ONEREQ_free(one); |
| 18 |
return NULL; |
| 19 |
} |
| 20 |
return one; |
| 21 |
} |
| 22 |
|
| 23 |
int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm) { |
| 24 |
GENERAL_NAME *gen = GENERAL_NAME_new(); |
| 25 |
if (gen == NULL) { |
| 26 |
return 0; |
| 27 |
} |
| 28 |
if (!X509_NAME_set(&gen->d.directoryName, nm)) { |
| 29 |
GENERAL_NAME_free(gen); |
| 30 |
return 0; |
| 31 |
} |
| 32 |
gen->type = GEN_DIRNAME; |
| 33 |
GENERAL_NAME_free(req->tbsRequest->requestorName); |
| 34 |
req->tbsRequest->requestorName = gen; |
| 35 |
return 1; |
| 36 |
} |
| 37 |
|
| 38 |
int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert) { |
| 39 |
if (req->optionalSignature == NULL) { |
| 40 |
req->optionalSignature = OCSP_SIGNATURE_new(); |
| 41 |
} |
| 42 |
OCSP_SIGNATURE *sig = req->optionalSignature; |
| 43 |
if (sig == NULL) { |
| 44 |
return 0; |
| 45 |
} |
| 46 |
if (cert == NULL) { |
| 47 |
return 1; |
| 48 |
} |
| 49 |
|
| 50 |
if (sig->certs == NULL && (sig->certs = sk_X509_new_null()) == NULL) { |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
if (!sk_X509_push(sig->certs, cert)) { |
| 55 |
return 0; |
| 56 |
} |
| 57 |
// sk_X509_push takes ownership. |
| 58 |
X509_up_ref(cert); |
| 59 |
return 1; |
| 60 |
} |
| 61 |
|
| 62 |
int OCSP_request_sign(OCSP_REQUEST *req, X509 *signer, EVP_PKEY *key, |
| 63 |
const EVP_MD *dgst, STACK_OF(X509) *certs, |
| 64 |
unsigned long flags) { |
| 65 |
if (req->optionalSignature != NULL) { |
| 66 |
// OpenSSL lets an |OCSP_REQUEST| be signed twice, regardless of whether |
| 67 |
// an |optionalSignature| exists. Signing an OCSP Request twice creates |
| 68 |
// a dangling |OCSP_SIGNATURE| pointer with no clear way of recovering |
| 69 |
// it. We disallow this behavior and only allow the |OCSP_REQUEST| to be |
| 70 |
// signed once. There's no indication or use case detailed in the RFC |
| 71 |
// that the OCSP request can or should be signed twice. |
| 72 |
// https://datatracker.ietf.org/doc/html/rfc6960#section-4.1.2 |
| 73 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_OCSP_REQUEST_DUPLICATE_SIGNATURE); |
| 74 |
goto err; |
| 75 |
} |
| 76 |
|
| 77 |
if (!OCSP_request_set1_name(req, X509_get_subject_name(signer))) { |
| 78 |
goto err; |
| 79 |
} |
| 80 |
req->optionalSignature = OCSP_SIGNATURE_new(); |
| 81 |
if (req->optionalSignature == NULL) { |
| 82 |
goto err; |
| 83 |
} |
| 84 |
if (key != NULL) { |
| 85 |
if (!X509_check_private_key(signer, key)) { |
| 86 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); |
| 87 |
goto err; |
| 88 |
} |
| 89 |
const EVP_MD *init_dgst = OCSP_get_default_digest(dgst, key); |
| 90 |
if (init_dgst == NULL) { |
| 91 |
OPENSSL_PUT_ERROR(OCSP, EVP_R_NO_DEFAULT_DIGEST); |
| 92 |
goto err; |
| 93 |
} |
| 94 |
|
| 95 |
if (!ASN1_item_sign(ASN1_ITEM_rptr(OCSP_REQINFO), |
| 96 |
req->optionalSignature->signatureAlgorithm, NULL, |
| 97 |
req->optionalSignature->signature, req->tbsRequest, key, |
| 98 |
init_dgst)) { |
| 99 |
goto err; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if (!IS_OCSP_FLAG_SET(flags, OCSP_NOCERTS)) { |
| 104 |
if (!OCSP_request_add1_cert(req, signer)) { |
| 105 |
goto err; |
| 106 |
} |
| 107 |
for (size_t i = 0; i < sk_X509_num(certs); i++) { |
| 108 |
if (!OCSP_request_add1_cert(req, sk_X509_value(certs, i))) { |
| 109 |
goto err; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return 1; |
| 115 |
err: |
| 116 |
OCSP_SIGNATURE_free(req->optionalSignature); |
| 117 |
req->optionalSignature = NULL; |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
int OCSP_response_status(OCSP_RESPONSE *resp) { |
| 122 |
if (resp == NULL) { |
| 123 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
return ASN1_ENUMERATED_get(resp->responseStatus); |
| 127 |
} |
| 128 |
|
| 129 |
OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp) { |
| 130 |
if (resp == NULL) { |
| 131 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
OCSP_RESPBYTES *rb = resp->responseBytes; |
| 136 |
if (rb == NULL) { |
| 137 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_NO_RESPONSE_DATA); |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) { |
| 141 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_NOT_BASIC_RESPONSE); |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP)); |
| 145 |
} |
| 146 |
|
| 147 |
OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, size_t idx) { |
| 148 |
if (bs == NULL) { |
| 149 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
if (bs->tbsResponseData == NULL) { |
| 153 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_NO_RESPONSE_DATA); |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
return sk_OCSP_SINGLERESP_value(bs->tbsResponseData->responses, idx); |
| 157 |
} |
| 158 |
|
| 159 |
int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last) { |
| 160 |
if (bs == NULL || id == NULL) { |
| 161 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
if (bs->tbsResponseData == NULL) { |
| 165 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_NO_RESPONSE_DATA); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
// |OCSP_SINGLERESP| stack is in |OCSP_BASICRESP| responseData, we look for |
| 170 |
// |OCSP_CERTID| in here. |
| 171 |
STACK_OF(OCSP_SINGLERESP) *sresp = bs->tbsResponseData->responses; |
| 172 |
OCSP_SINGLERESP *single; |
| 173 |
|
| 174 |
if (last < 0) { |
| 175 |
last = 0; |
| 176 |
} else { |
| 177 |
last++; |
| 178 |
} |
| 179 |
for (size_t i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) { |
| 180 |
single = sk_OCSP_SINGLERESP_value(sresp, i); |
| 181 |
if (!OCSP_id_cmp(id, single->certId)) { |
| 182 |
return i; |
| 183 |
} |
| 184 |
} |
| 185 |
return -1; |
| 186 |
} |
| 187 |
|
| 188 |
int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason, |
| 189 |
ASN1_GENERALIZEDTIME **revtime, |
| 190 |
ASN1_GENERALIZEDTIME **thisupd, |
| 191 |
ASN1_GENERALIZEDTIME **nextupd) { |
| 192 |
if (single == NULL) { |
| 193 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
OCSP_CERTSTATUS *cst = single->certStatus; |
| 197 |
if (cst == NULL) { |
| 198 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 199 |
return -1; |
| 200 |
} |
| 201 |
int status = cst->type; |
| 202 |
|
| 203 |
// If certificate status is revoked, we look up certificate revocation time |
| 204 |
// and reason. |
| 205 |
if (status == V_OCSP_CERTSTATUS_REVOKED) { |
| 206 |
OCSP_REVOKEDINFO *rev = cst->value.revoked; |
| 207 |
if (rev != NULL) { |
| 208 |
if (revtime != NULL) { |
| 209 |
*revtime = rev->revocationTime; |
| 210 |
} |
| 211 |
if (reason != NULL) { |
| 212 |
if (rev->revocationReason) { |
| 213 |
*reason = ASN1_ENUMERATED_get(rev->revocationReason); |
| 214 |
} else { |
| 215 |
*reason = -1; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
// Send back when certificate was last updated and when is the next update |
| 221 |
// time. |
| 222 |
if (thisupd != NULL) { |
| 223 |
*thisupd = single->thisUpdate; |
| 224 |
} |
| 225 |
if (nextupd != NULL) { |
| 226 |
*nextupd = single->nextUpdate; |
| 227 |
} |
| 228 |
return status; |
| 229 |
} |
| 230 |
|
| 231 |
int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status, |
| 232 |
int *reason, ASN1_GENERALIZEDTIME **revtime, |
| 233 |
ASN1_GENERALIZEDTIME **thisupd, |
| 234 |
ASN1_GENERALIZEDTIME **nextupd) { |
| 235 |
if (bs == NULL || id == NULL) { |
| 236 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 237 |
return -1; |
| 238 |
} |
| 239 |
|
| 240 |
// we look for index of equivalent |OCSP_CERTID| of issuer certificate in |
| 241 |
// |OCSP_BASICRESP| |
| 242 |
int single_idx = OCSP_resp_find(bs, id, -1); |
| 243 |
if (single_idx < 0) { |
| 244 |
return 0; |
| 245 |
} |
| 246 |
OCSP_SINGLERESP *single = OCSP_resp_get0(bs, single_idx); |
| 247 |
|
| 248 |
// Extract the update time and revocation status of certificate sent back |
| 249 |
// from OCSP responder |
| 250 |
int single_status = |
| 251 |
OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd); |
| 252 |
if (status != NULL) { |
| 253 |
*status = single_status; |
| 254 |
} |
| 255 |
return 1; |
| 256 |
} |
| 257 |
|
| 258 |
int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisUpdate, |
| 259 |
ASN1_GENERALIZEDTIME *nextUpdate, |
| 260 |
long drift_num_seconds, long max_age_seconds) { |
| 261 |
int ret = 1; |
| 262 |
int64_t t_tmp; |
| 263 |
int64_t t_now = time(NULL); |
| 264 |
|
| 265 |
// Check |thisUpdate| is valid and not more than |drift_num_seconds| in the |
| 266 |
// future. |
| 267 |
if (!ASN1_GENERALIZEDTIME_check(thisUpdate)) { |
| 268 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_ERROR_IN_THISUPDATE_FIELD); |
| 269 |
ret = 0; |
| 270 |
} else { |
| 271 |
t_tmp = t_now + drift_num_seconds; |
| 272 |
if (X509_cmp_time_posix(thisUpdate, t_tmp) > 0) { |
| 273 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_STATUS_NOT_YET_VALID); |
| 274 |
ret = 0; |
| 275 |
} |
| 276 |
|
| 277 |
// If |max_num_seconds| is specified, check that |thisUpdate| is not more |
| 278 |
// than |max_num_seconds| in the past. |
| 279 |
if (max_age_seconds >= 0) { |
| 280 |
t_tmp = t_now - max_age_seconds; |
| 281 |
if (X509_cmp_time_posix(thisUpdate, t_tmp) < 0) { |
| 282 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_STATUS_TOO_OLD); |
| 283 |
ret = 0; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
// If |nextUpdate| field is empty, we have validated everything we can at |
| 289 |
// this point. |
| 290 |
if (nextUpdate == NULL) { |
| 291 |
return ret; |
| 292 |
} |
| 293 |
|
| 294 |
// Check |nextUpdate| is valid and not more than |drift_num_seconds| in the |
| 295 |
// past. |
| 296 |
if (!ASN1_GENERALIZEDTIME_check(nextUpdate)) { |
| 297 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD); |
| 298 |
ret = 0; |
| 299 |
} else { |
| 300 |
t_tmp = t_now - drift_num_seconds; |
| 301 |
if (X509_cmp_time_posix(nextUpdate, t_tmp) < 0) { |
| 302 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_STATUS_EXPIRED); |
| 303 |
ret = 0; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
// Also don't allow |nextUpdate| to precede |thisUpdate|. |
| 308 |
if (ASN1_STRING_cmp(nextUpdate, thisUpdate) < 0) { |
| 309 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE); |
| 310 |
ret = 0; |
| 311 |
} |
| 312 |
|
| 313 |
return ret; |
| 314 |
} |
| 315 |
|
| 316 |
int OCSP_resp_count(OCSP_BASICRESP *bs) { |
| 317 |
if (bs == NULL) { |
| 318 |
return -1; |
| 319 |
} |
| 320 |
return (int)sk_OCSP_SINGLERESP_num(bs->tbsResponseData->responses); |
| 321 |
} |
| 322 |
|
| 323 |
const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single) { |
| 324 |
return single->certId; |
| 325 |
} |