| 1 |
// Copyright 2001-2017 The OpenSSL Project Authors. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Modifications Copyright Amazon.com, Inc. or its affiliates. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 6 |
|
| 7 |
#include "../internal.h" |
| 8 |
#include "internal.h" |
| 9 |
|
| 10 |
int OCSP_request_onereq_count(OCSP_REQUEST *req) { |
| 11 |
GUARD_PTR(req); |
| 12 |
GUARD_PTR(req->tbsRequest); |
| 13 |
return (int)sk_OCSP_ONEREQ_num(req->tbsRequest->requestList); |
| 14 |
} |
| 15 |
|
| 16 |
OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i) { |
| 17 |
GUARD_PTR(req); |
| 18 |
GUARD_PTR(req->tbsRequest); |
| 19 |
return sk_OCSP_ONEREQ_value(req->tbsRequest->requestList, i); |
| 20 |
} |
| 21 |
|
| 22 |
int OCSP_id_get0_info(ASN1_OCTET_STRING **nameHash, ASN1_OBJECT **algor, |
| 23 |
ASN1_OCTET_STRING **keyHash, ASN1_INTEGER **serial, |
| 24 |
OCSP_CERTID *cid) { |
| 25 |
if (cid == NULL) { |
| 26 |
return 0; |
| 27 |
} |
| 28 |
if (algor != NULL) { |
| 29 |
*algor = cid->hashAlgorithm->algorithm; |
| 30 |
} |
| 31 |
if (nameHash != NULL) { |
| 32 |
*nameHash = cid->issuerNameHash; |
| 33 |
} |
| 34 |
if (keyHash != NULL) { |
| 35 |
*keyHash = cid->issuerKeyHash; |
| 36 |
} |
| 37 |
if (serial != NULL) { |
| 38 |
*serial = cid->serialNumber; |
| 39 |
} |
| 40 |
return 1; |
| 41 |
} |
| 42 |
|
| 43 |
int OCSP_request_is_signed(OCSP_REQUEST *req) { |
| 44 |
GUARD_PTR(req); |
| 45 |
if (req->optionalSignature != NULL) { |
| 46 |
return 1; |
| 47 |
} |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one) { |
| 52 |
GUARD_PTR(one); |
| 53 |
return one->reqCert; |
| 54 |
} |
| 55 |
|
| 56 |
int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert) { |
| 57 |
if (resp->certs == NULL && (resp->certs = sk_X509_new_null()) == NULL) { |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
if (!sk_X509_push(resp->certs, cert)) { |
| 62 |
return 0; |
| 63 |
} |
| 64 |
X509_up_ref(cert); |
| 65 |
return 1; |
| 66 |
} |
| 67 |
|
| 68 |
static int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert) { |
| 69 |
GUARD_PTR(respid); |
| 70 |
GUARD_PTR(cert); |
| 71 |
if (!X509_NAME_set(&respid->value.byName, X509_get_subject_name(cert))) { |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
respid->type = V_OCSP_RESPID_NAME; |
| 76 |
return 1; |
| 77 |
} |
| 78 |
|
| 79 |
static int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert) { |
| 80 |
GUARD_PTR(respid); |
| 81 |
GUARD_PTR(cert); |
| 82 |
|
| 83 |
// RFC2560 requires SHA1. |
| 84 |
unsigned char digest[SHA_DIGEST_LENGTH]; |
| 85 |
if (!X509_pubkey_digest(cert, EVP_sha1(), digest, NULL)) { |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
ASN1_OCTET_STRING *byKey = ASN1_OCTET_STRING_new(); |
| 90 |
if (byKey == NULL) { |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
if (!ASN1_OCTET_STRING_set(byKey, digest, SHA_DIGEST_LENGTH)) { |
| 95 |
ASN1_OCTET_STRING_free(byKey); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
respid->type = V_OCSP_RESPID_KEY; |
| 99 |
respid->value.byKey = byKey; |
| 100 |
|
| 101 |
return 1; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
// OCSP_basic_sign_ctx does the actual signing operation for |OCSP_basic_sign|, |
| 106 |
// but with an initialized |EVP_MD_CTX|. |
| 107 |
static int OCSP_basic_sign_ctx(OCSP_BASICRESP *resp, X509 *signer, |
| 108 |
EVP_MD_CTX *ctx, STACK_OF(X509) *certs, |
| 109 |
unsigned long flags) { |
| 110 |
GUARD_PTR(resp); |
| 111 |
GUARD_PTR(signer); |
| 112 |
|
| 113 |
if (ctx == NULL || ctx->pctx == NULL) { |
| 114 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_NO_SIGNER_KEY); |
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pctx); |
| 119 |
if (pkey == NULL || !X509_check_private_key(signer, pkey)) { |
| 120 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
// Add relevant certificates to the response. This is optional according to |
| 125 |
// the RFC. |
| 126 |
if (!IS_OCSP_FLAG_SET(flags, OCSP_NOCERTS)) { |
| 127 |
if (!OCSP_basic_add1_cert(resp, signer)) { |
| 128 |
return 0; |
| 129 |
} |
| 130 |
for (size_t i = 0; i < sk_X509_num(certs); i++) { |
| 131 |
X509 *tmpcert = sk_X509_value(certs, i); |
| 132 |
if (!OCSP_basic_add1_cert(resp, tmpcert)) { |
| 133 |
return 0; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// Set |responderId| of response. |
| 139 |
OCSP_RESPID *rid = resp->tbsResponseData->responderId; |
| 140 |
if (IS_OCSP_FLAG_SET(flags, OCSP_RESPID_KEY)) { |
| 141 |
if (!OCSP_RESPID_set_by_key(rid, signer)) { |
| 142 |
return 0; |
| 143 |
} |
| 144 |
} else if (!OCSP_RESPID_set_by_name(rid, signer)) { |
| 145 |
// The OCSP responder is identified by name by default. |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
// Set |producedAt| time field of response. Although this can be |
| 150 |
// excluded with |OCSP_NOTIME|, a definitive response should include |
| 151 |
// the generation time. |
| 152 |
if (!IS_OCSP_FLAG_SET(flags, OCSP_NOTIME)) { |
| 153 |
if (!ASN1_GENERALIZEDTIME_set(resp->tbsResponseData->producedAt, |
| 154 |
time(NULL))) { |
| 155 |
return 0; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// Do the actual signing. This is mandatory according to the RFC. |
| 160 |
if (!ASN1_item_sign_ctx(ASN1_ITEM_rptr(OCSP_RESPDATA), |
| 161 |
resp->signatureAlgorithm, NULL, resp->signature, |
| 162 |
resp->tbsResponseData, ctx)) { |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
return 1; |
| 167 |
} |
| 168 |
|
| 169 |
const EVP_MD *OCSP_get_default_digest(const EVP_MD *dgst, EVP_PKEY *signer) { |
| 170 |
if (dgst != NULL) { |
| 171 |
return dgst; |
| 172 |
} |
| 173 |
int pkey_nid = EVP_PKEY_id(signer); |
| 174 |
if (pkey_nid == EVP_PKEY_EC || pkey_nid == EVP_PKEY_RSA || |
| 175 |
pkey_nid == EVP_PKEY_DSA) { |
| 176 |
return EVP_sha256(); |
| 177 |
} |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
int OCSP_basic_sign(OCSP_BASICRESP *resp, X509 *signer, EVP_PKEY *key, |
| 182 |
const EVP_MD *dgst, STACK_OF(X509) *certs, |
| 183 |
unsigned long flags) { |
| 184 |
GUARD_PTR(resp); |
| 185 |
GUARD_PTR(signer); |
| 186 |
GUARD_PTR(key); |
| 187 |
|
| 188 |
const EVP_MD *init_dgst = OCSP_get_default_digest(dgst, key); |
| 189 |
if (init_dgst == NULL) { |
| 190 |
OPENSSL_PUT_ERROR(OCSP, EVP_R_NO_DEFAULT_DIGEST); |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
| 195 |
if (ctx == NULL) { |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
if (!EVP_DigestSignInit(ctx, NULL, init_dgst, NULL, key)) { |
| 200 |
EVP_MD_CTX_free(ctx); |
| 201 |
return 0; |
| 202 |
} |
| 203 |
int ret = OCSP_basic_sign_ctx(resp, signer, ctx, certs, flags); |
| 204 |
EVP_MD_CTX_free(ctx); |
| 205 |
return ret; |
| 206 |
} |
| 207 |
|
| 208 |
OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *resp, OCSP_CERTID *cid, |
| 209 |
int status, int revoked_reason, |
| 210 |
ASN1_TIME *revoked_time, |
| 211 |
ASN1_TIME *this_update, |
| 212 |
ASN1_TIME *next_update) { |
| 213 |
if (resp == NULL || resp->tbsResponseData == NULL || cid == NULL || |
| 214 |
this_update == NULL) { |
| 215 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
|
| 219 |
// Ambiguous status values are not allowed. |
| 220 |
if (status < V_OCSP_CERTSTATUS_GOOD || status > V_OCSP_CERTSTATUS_UNKNOWN) { |
| 221 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_UNKNOWN_FIELD_VALUE); |
| 222 |
return NULL; |
| 223 |
} |
| 224 |
|
| 225 |
OCSP_SINGLERESP *single = OCSP_SINGLERESP_new(); |
| 226 |
if (single == NULL) { |
| 227 |
goto err; |
| 228 |
} |
| 229 |
|
| 230 |
// Init |resp->tbsResponseData->responses| if NULL. |
| 231 |
if (resp->tbsResponseData->responses == NULL) { |
| 232 |
resp->tbsResponseData->responses = sk_OCSP_SINGLERESP_new_null(); |
| 233 |
if (resp->tbsResponseData->responses == NULL) { |
| 234 |
goto err; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if (!ASN1_TIME_to_generalizedtime(this_update, &single->thisUpdate)) { |
| 239 |
goto err; |
| 240 |
} |
| 241 |
if (next_update != NULL) { |
| 242 |
// |next_update| is allowed to be NULL. Only set |single->nextUpdate| if |
| 243 |
// |next_update| is non-NULL. |
| 244 |
if (!ASN1_TIME_to_generalizedtime(next_update, &single->nextUpdate)) { |
| 245 |
goto err; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Reset |single->certId|. |
| 250 |
OCSP_CERTID_free(single->certId); |
| 251 |
single->certId = OCSP_CERTID_dup(cid); |
| 252 |
if (single->certId == NULL) { |
| 253 |
goto err; |
| 254 |
} |
| 255 |
|
| 256 |
single->certStatus->type = status; |
| 257 |
switch (single->certStatus->type) { |
| 258 |
case V_OCSP_CERTSTATUS_REVOKED: |
| 259 |
if (revoked_time == NULL) { |
| 260 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_NO_REVOKED_TIME); |
| 261 |
goto err; |
| 262 |
} |
| 263 |
|
| 264 |
single->certStatus->value.revoked = OCSP_REVOKEDINFO_new(); |
| 265 |
if (single->certStatus->value.revoked == NULL) { |
| 266 |
goto err; |
| 267 |
} |
| 268 |
|
| 269 |
// Start assigning values to |info| once initialized successfully. |
| 270 |
OCSP_REVOKEDINFO *info = single->certStatus->value.revoked; |
| 271 |
if (!ASN1_TIME_to_generalizedtime(revoked_time, &info->revocationTime)) { |
| 272 |
goto err; |
| 273 |
} |
| 274 |
// https://www.rfc-editor.org/rfc/rfc5280#section-5.3.1 specifies the only |
| 275 |
// valid reason codes are 0-10. Value 7 is not used. |
| 276 |
if (revoked_reason < OCSP_REVOKED_STATUS_UNSPECIFIED || |
| 277 |
revoked_reason > OCSP_REVOKED_STATUS_AACOMPROMISE || |
| 278 |
revoked_reason == OCSP_UNASSIGNED_REVOKED_STATUS) { |
| 279 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_UNKNOWN_FIELD_VALUE); |
| 280 |
goto err; |
| 281 |
} |
| 282 |
info->revocationReason = ASN1_ENUMERATED_new(); |
| 283 |
if (info->revocationReason == NULL || |
| 284 |
!ASN1_ENUMERATED_set(info->revocationReason, revoked_reason)) { |
| 285 |
goto err; |
| 286 |
} |
| 287 |
|
| 288 |
break; |
| 289 |
|
| 290 |
case V_OCSP_CERTSTATUS_GOOD: |
| 291 |
single->certStatus->value.good = ASN1_NULL_new(); |
| 292 |
if (single->certStatus->value.good == NULL) { |
| 293 |
goto err; |
| 294 |
} |
| 295 |
break; |
| 296 |
|
| 297 |
case V_OCSP_CERTSTATUS_UNKNOWN: |
| 298 |
single->certStatus->value.unknown = ASN1_NULL_new(); |
| 299 |
if (single->certStatus->value.unknown == NULL) { |
| 300 |
goto err; |
| 301 |
} |
| 302 |
break; |
| 303 |
|
| 304 |
default: |
| 305 |
goto err; |
| 306 |
} |
| 307 |
|
| 308 |
// Finally add the |OCSP_SINGLERESP| we were working with to |resp|. |
| 309 |
if (!sk_OCSP_SINGLERESP_push(resp->tbsResponseData->responses, single)) { |
| 310 |
goto err; |
| 311 |
} |
| 312 |
return single; |
| 313 |
|
| 314 |
err: |
| 315 |
OCSP_SINGLERESP_free(single); |
| 316 |
return NULL; |
| 317 |
} |
| 318 |
|
| 319 |
OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs) { |
| 320 |
if (status < OCSP_RESPONSE_STATUS_SUCCESSFUL || |
| 321 |
status > OCSP_RESPONSE_STATUS_UNAUTHORIZED || |
| 322 |
// 4 is not a valid response status code. |
| 323 |
status == OCSP_UNASSIGNED_RESPONSE_STATUS) { |
| 324 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_UNKNOWN_FIELD_VALUE); |
| 325 |
return NULL; |
| 326 |
} |
| 327 |
|
| 328 |
OCSP_RESPONSE *rsp = OCSP_RESPONSE_new(); |
| 329 |
if (rsp == NULL) { |
| 330 |
goto err; |
| 331 |
} |
| 332 |
if (!ASN1_ENUMERATED_set(rsp->responseStatus, status)) { |
| 333 |
goto err; |
| 334 |
} |
| 335 |
if (bs == NULL) { |
| 336 |
// |bs| is allowed to be NULL. |
| 337 |
return rsp; |
| 338 |
} |
| 339 |
|
| 340 |
rsp->responseBytes = OCSP_RESPBYTES_new(); |
| 341 |
if (rsp->responseBytes == NULL) { |
| 342 |
goto err; |
| 343 |
} |
| 344 |
|
| 345 |
// responseType will be id-pkix-ocsp-basic according to RFC6960. |
| 346 |
// |
| 347 |
// https://datatracker.ietf.org/doc/html/rfc6960#section-4.2.1 |
| 348 |
rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic); |
| 349 |
if (!ASN1_item_pack(bs, ASN1_ITEM_rptr(OCSP_BASICRESP), |
| 350 |
&rsp->responseBytes->response)) { |
| 351 |
goto err; |
| 352 |
} |
| 353 |
return rsp; |
| 354 |
|
| 355 |
err: |
| 356 |
OCSP_RESPONSE_free(rsp); |
| 357 |
return NULL; |
| 358 |
} |