| 1 |
// Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
// Modifications Copyright Amazon.com, Inc. or its affiliates. |
| 4 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 5 |
|
| 6 |
#include <openssl/mem.h> |
| 7 |
#include <openssl/rand.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include "../internal.h" |
| 11 |
#include "internal.h" |
| 12 |
|
| 13 |
#define OCSP_DEFAULT_NONCE_LENGTH 16 |
| 14 |
|
| 15 |
int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *req, int nid, int lastpos) { |
| 16 |
return X509v3_get_ext_by_NID(req->tbsRequest->requestExtensions, nid, |
| 17 |
lastpos); |
| 18 |
} |
| 19 |
|
| 20 |
X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *req, int loc) { |
| 21 |
return X509v3_get_ext(req->tbsRequest->requestExtensions, loc); |
| 22 |
} |
| 23 |
|
| 24 |
int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *bs, X509_EXTENSION *ex, int loc) { |
| 25 |
return (X509v3_add_ext(&bs->tbsResponseData->responseExtensions, ex, loc) != |
| 26 |
NULL); |
| 27 |
} |
| 28 |
|
| 29 |
int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *bs, int nid, int lastpos) { |
| 30 |
return X509v3_get_ext_by_NID(bs->tbsResponseData->responseExtensions, nid, |
| 31 |
lastpos); |
| 32 |
} |
| 33 |
|
| 34 |
X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *bs, int loc) { |
| 35 |
return X509v3_get_ext(bs->tbsResponseData->responseExtensions, loc); |
| 36 |
} |
| 37 |
|
| 38 |
X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc) { |
| 39 |
return X509v3_delete_ext(x->tbsResponseData->responseExtensions, loc); |
| 40 |
} |
| 41 |
|
| 42 |
int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *sresp, X509_EXTENSION *ex, |
| 43 |
int loc) { |
| 44 |
GUARD_PTR(sresp); |
| 45 |
return (X509v3_add_ext(&sresp->singleExtensions, ex, loc) != NULL); |
| 46 |
} |
| 47 |
|
| 48 |
int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *sresp) { |
| 49 |
GUARD_PTR(sresp); |
| 50 |
return X509v3_get_ext_count(sresp->singleExtensions); |
| 51 |
} |
| 52 |
|
| 53 |
X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *sresp, int loc) { |
| 54 |
GUARD_PTR(sresp); |
| 55 |
return X509v3_get_ext(sresp->singleExtensions, loc); |
| 56 |
} |
| 57 |
|
| 58 |
static int ocsp_add_nonce(STACK_OF(X509_EXTENSION) **exts, unsigned char *val, |
| 59 |
int len) { |
| 60 |
unsigned char *tmpval; |
| 61 |
ASN1_OCTET_STRING os; |
| 62 |
int ret = 0; |
| 63 |
if (len <= 0) { |
| 64 |
len = OCSP_DEFAULT_NONCE_LENGTH; |
| 65 |
} |
| 66 |
// Create the OCTET STRING manually by writing out the header and |
| 67 |
// appending the content octets. This avoids an extra memory allocation |
| 68 |
// operation in some cases. Applications should *NOT* do this because it |
| 69 |
// relies on library internals. |
| 70 |
os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING); |
| 71 |
if (os.length < 0) { |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
os.data = OPENSSL_malloc(os.length); |
| 76 |
if (os.data == NULL) { |
| 77 |
goto err; |
| 78 |
} |
| 79 |
tmpval = os.data; |
| 80 |
ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL); |
| 81 |
if (val != NULL) { |
| 82 |
OPENSSL_memcpy(tmpval, val, len); |
| 83 |
} else { |
| 84 |
AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(tmpval, len)); |
| 85 |
} |
| 86 |
if (X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce, &os, 0, |
| 87 |
X509V3_ADD_REPLACE) <= 0) { |
| 88 |
goto err; |
| 89 |
} |
| 90 |
ret = 1; |
| 91 |
err: |
| 92 |
OPENSSL_free(os.data); |
| 93 |
return ret; |
| 94 |
} |
| 95 |
|
| 96 |
int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len) { |
| 97 |
if (req == NULL) { |
| 98 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
if (val != NULL && len <= 0) { |
| 102 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 103 |
return 0; |
| 104 |
} |
| 105 |
return ocsp_add_nonce(&req->tbsRequest->requestExtensions, val, len); |
| 106 |
} |
| 107 |
|
| 108 |
int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len) { |
| 109 |
if (resp == NULL) { |
| 110 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 111 |
return 0; |
| 112 |
} |
| 113 |
if (val != NULL && len <= 0) { |
| 114 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 115 |
return 0; |
| 116 |
} |
| 117 |
return ocsp_add_nonce(&resp->tbsResponseData->responseExtensions, val, len); |
| 118 |
} |
| 119 |
|
| 120 |
int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs) { |
| 121 |
if (req == NULL || bs == NULL) { |
| 122 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 123 |
return OCSP_NONCE_NOT_EQUAL; |
| 124 |
} |
| 125 |
// Since we are only interested in the presence or absence of |
| 126 |
// the nonce and comparing its value there is no need to use |
| 127 |
// the X509V3 routines: this way we can avoid them allocating an |
| 128 |
// ASN1_OCTET_STRING structure for the value which would be |
| 129 |
// freed immediately anyway. |
| 130 |
|
| 131 |
int req_idx, resp_idx; |
| 132 |
X509_EXTENSION *req_ext, *resp_ext; |
| 133 |
req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1); |
| 134 |
resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1); |
| 135 |
// Check that both are absent. |
| 136 |
if ((req_idx < 0) && (resp_idx < 0)) { |
| 137 |
return OCSP_NONCE_BOTH_ABSENT; |
| 138 |
} |
| 139 |
// Check in request only. |
| 140 |
if ((req_idx >= 0) && (resp_idx < 0)) { |
| 141 |
return OCSP_NONCE_REQUEST_ONLY; |
| 142 |
} |
| 143 |
// Check in response, but not request. |
| 144 |
if ((req_idx < 0) && (resp_idx >= 0)) { |
| 145 |
return OCSP_NONCE_RESPONSE_ONLY; |
| 146 |
} |
| 147 |
// Otherwise, there is a nonce in both the request and response, so retrieve |
| 148 |
// the extensions. |
| 149 |
req_ext = OCSP_REQUEST_get_ext(req, req_idx); |
| 150 |
resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx); |
| 151 |
if (ASN1_OCTET_STRING_cmp(X509_EXTENSION_get_data(req_ext), |
| 152 |
X509_EXTENSION_get_data(resp_ext))) { |
| 153 |
return OCSP_NONCE_NOT_EQUAL; |
| 154 |
} |
| 155 |
return OCSP_NONCE_EQUAL; |
| 156 |
} |
| 157 |
|
| 158 |
int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req) { |
| 159 |
GUARD_PTR(resp); |
| 160 |
GUARD_PTR(req); |
| 161 |
|
| 162 |
// Check for nonce in request. |
| 163 |
int req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1); |
| 164 |
// If no nonce, that's OK. We return 2 in this case. |
| 165 |
if (req_idx < 0) { |
| 166 |
return 2; |
| 167 |
} |
| 168 |
X509_EXTENSION *req_ext = OCSP_REQUEST_get_ext(req, req_idx); |
| 169 |
// Nonce found, but no entry at the index. |
| 170 |
// This shouldn't happen under normal circumstances. |
| 171 |
GUARD_PTR(req_ext); |
| 172 |
|
| 173 |
// Append the nonce. |
| 174 |
return OCSP_BASICRESP_add_ext(resp, req_ext, -1); |
| 175 |
} |