| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <openssl/mem.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "internal.h" |
| 8 |
|
| 9 |
OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, const X509 *subject, |
| 10 |
const X509 *issuer) { |
| 11 |
if (issuer == NULL) { |
| 12 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 13 |
return NULL; |
| 14 |
} |
| 15 |
|
| 16 |
const X509_NAME *iname; |
| 17 |
const ASN1_INTEGER *serial; |
| 18 |
ASN1_BIT_STRING *ikey; |
| 19 |
|
| 20 |
if (dgst == NULL) { |
| 21 |
dgst = EVP_sha1(); |
| 22 |
} |
| 23 |
if (subject != NULL) { |
| 24 |
iname = X509_get_issuer_name(subject); |
| 25 |
serial = X509_get0_serialNumber(subject); |
| 26 |
} else { |
| 27 |
iname = X509_get_subject_name(issuer); |
| 28 |
serial = NULL; |
| 29 |
} |
| 30 |
ikey = X509_get0_pubkey_bitstr(issuer); |
| 31 |
return OCSP_cert_id_new(dgst, iname, ikey, serial); |
| 32 |
} |
| 33 |
|
| 34 |
OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst, const X509_NAME *issuerName, |
| 35 |
const ASN1_BIT_STRING *issuerKey, |
| 36 |
const ASN1_INTEGER *serialNumber) { |
| 37 |
if (dgst == NULL || issuerName == NULL || issuerKey == NULL) { |
| 38 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 39 |
return NULL; |
| 40 |
} |
| 41 |
|
| 42 |
int nid; |
| 43 |
unsigned int i; |
| 44 |
X509_ALGOR *alg; |
| 45 |
unsigned char md[EVP_MAX_MD_SIZE]; |
| 46 |
OCSP_CERTID *cid = OCSP_CERTID_new(); |
| 47 |
if (cid == NULL) { |
| 48 |
return NULL; |
| 49 |
} |
| 50 |
|
| 51 |
alg = cid->hashAlgorithm; |
| 52 |
ASN1_OBJECT_free(alg->algorithm); |
| 53 |
nid = EVP_MD_type(dgst); |
| 54 |
if (nid == NID_undef) { |
| 55 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_UNKNOWN_NID); |
| 56 |
goto err; |
| 57 |
} |
| 58 |
alg->algorithm = OBJ_nid2obj(nid); |
| 59 |
if (alg->algorithm == NULL) { |
| 60 |
goto err; |
| 61 |
} |
| 62 |
alg->parameter = ASN1_TYPE_new(); |
| 63 |
if (alg->parameter == NULL) { |
| 64 |
goto err; |
| 65 |
} |
| 66 |
alg->parameter->type = V_ASN1_NULL; |
| 67 |
|
| 68 |
if (!X509_NAME_digest(issuerName, dgst, md, &i)) { |
| 69 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_DIGEST_ERR); |
| 70 |
goto err; |
| 71 |
} |
| 72 |
if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i))) { |
| 73 |
goto err; |
| 74 |
} |
| 75 |
// Calculate the issuerKey hash, excluding tag and length |
| 76 |
if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL)) { |
| 77 |
goto err; |
| 78 |
} |
| 79 |
if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) { |
| 80 |
goto err; |
| 81 |
} |
| 82 |
// Only copy |serialNumber| if available. This may be empty. |
| 83 |
if (serialNumber != NULL) { |
| 84 |
if (ASN1_STRING_copy(cid->serialNumber, serialNumber) == 0) { |
| 85 |
goto err; |
| 86 |
} |
| 87 |
} |
| 88 |
return cid; |
| 89 |
|
| 90 |
err: |
| 91 |
OCSP_CERTID_free(cid); |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
|
| 95 |
int OCSP_id_issuer_cmp(const OCSP_CERTID *a, const OCSP_CERTID *b) { |
| 96 |
if (a == NULL || b == NULL) { |
| 97 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
|
| 101 |
if (a->hashAlgorithm == NULL || b->hashAlgorithm == NULL) { |
| 102 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
int ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm); |
| 107 |
if (ret != 0) { |
| 108 |
return ret; |
| 109 |
} |
| 110 |
ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash); |
| 111 |
if (ret != 0) { |
| 112 |
return ret; |
| 113 |
} |
| 114 |
ret = ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash); |
| 115 |
return ret; |
| 116 |
} |
| 117 |
|
| 118 |
int OCSP_id_cmp(const OCSP_CERTID *a, const OCSP_CERTID *b) { |
| 119 |
if (a == NULL || b == NULL) { |
| 120 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
// Compare OCSP issuer name and key |
| 125 |
int ret = OCSP_id_issuer_cmp(a, b); |
| 126 |
if (ret != 0) { |
| 127 |
return ret; |
| 128 |
} |
| 129 |
// Compare certificate serialNumber |
| 130 |
ret = ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber); |
| 131 |
return ret; |
| 132 |
} |
| 133 |
|
| 134 |
int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath, |
| 135 |
int *pssl) { |
| 136 |
if (url == NULL || phost == NULL || pport == NULL || ppath == NULL || |
| 137 |
pssl == NULL) { |
| 138 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER); |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
char *parser, *buffer; |
| 143 |
char *host = NULL; |
| 144 |
char *port = NULL; |
| 145 |
|
| 146 |
*phost = NULL; |
| 147 |
*pport = NULL; |
| 148 |
*ppath = NULL; |
| 149 |
|
| 150 |
// Duplicate into the buffer since the contents are going to be changed. |
| 151 |
buffer = OPENSSL_strdup(url); |
| 152 |
if (buffer == NULL) { |
| 153 |
goto mem_err; |
| 154 |
} |
| 155 |
|
| 156 |
// Check for initial colon |
| 157 |
parser = strchr(buffer, ':'); |
| 158 |
if (parser == NULL) { |
| 159 |
goto parse_err; |
| 160 |
} |
| 161 |
*(parser++) = '\0'; |
| 162 |
|
| 163 |
// Set default ports for http and https. If a port is specified later, this |
| 164 |
// will be overwritten. |pssl| will be set to true, if https is being used. |
| 165 |
if (strncmp(buffer, "https", 5) == 0) { |
| 166 |
*pssl = 1; |
| 167 |
port = (char *)"443"; |
| 168 |
} else if (strncmp(buffer, "http", 4) == 0) { |
| 169 |
*pssl = 0; |
| 170 |
port = (char *)"80"; |
| 171 |
} else { |
| 172 |
goto parse_err; |
| 173 |
} |
| 174 |
|
| 175 |
// Check for double slash. |
| 176 |
if ((parser[0] != '/') || (parser[1] != '/')) { |
| 177 |
goto parse_err; |
| 178 |
} |
| 179 |
parser += 2; |
| 180 |
host = parser; |
| 181 |
|
| 182 |
// Check for trailing part of path. |
| 183 |
parser = strchr(parser, '/'); |
| 184 |
if (parser == NULL) { |
| 185 |
// Default is "/" if there is no trailing path in the URL. |
| 186 |
*ppath = OPENSSL_strdup("/"); |
| 187 |
} else { |
| 188 |
*ppath = OPENSSL_strdup(parser); |
| 189 |
// Set start of path to 0 so hostname is valid. |
| 190 |
*parser = '\0'; |
| 191 |
} |
| 192 |
if (*ppath == NULL) { |
| 193 |
goto mem_err; |
| 194 |
} |
| 195 |
|
| 196 |
parser = host; |
| 197 |
// Checks if the host is an ipv6 host address. |
| 198 |
if (host[0] == '[') { |
| 199 |
host++; |
| 200 |
parser = strchr(host, ']'); |
| 201 |
if (parser == NULL) { |
| 202 |
goto parse_err; |
| 203 |
} |
| 204 |
*parser = '\0'; |
| 205 |
parser++; |
| 206 |
} |
| 207 |
|
| 208 |
// Look for optional ':' for port number. |
| 209 |
if ((parser = strchr(parser, ':'))) { |
| 210 |
*parser = 0; |
| 211 |
port = parser + 1; |
| 212 |
} |
| 213 |
|
| 214 |
*pport = OPENSSL_strdup(port); |
| 215 |
if (*pport == NULL) { |
| 216 |
goto mem_err; |
| 217 |
} |
| 218 |
|
| 219 |
*phost = OPENSSL_strdup(host); |
| 220 |
if (*phost == NULL) { |
| 221 |
goto mem_err; |
| 222 |
} |
| 223 |
OPENSSL_free(buffer); |
| 224 |
return 1; |
| 225 |
|
| 226 |
mem_err: |
| 227 |
OPENSSL_PUT_ERROR(OCSP, ERR_R_MALLOC_FAILURE); |
| 228 |
goto err; |
| 229 |
parse_err: |
| 230 |
OPENSSL_PUT_ERROR(OCSP, OCSP_R_ERROR_PARSING_URL); |
| 231 |
err: |
| 232 |
OPENSSL_free(buffer); |
| 233 |
OPENSSL_free(*ppath); |
| 234 |
*ppath = NULL; |
| 235 |
OPENSSL_free(*pport); |
| 236 |
*pport = NULL; |
| 237 |
OPENSSL_free(*phost); |
| 238 |
*phost = NULL; |
| 239 |
return 0; |
| 240 |
} |