/src/openssl30/crypto/ct/ct_sct.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | #ifdef OPENSSL_NO_CT | 
| 11 |  | # error "CT disabled" | 
| 12 |  | #endif | 
| 13 |  |  | 
| 14 |  | #include <openssl/ct.h> | 
| 15 |  | #include <openssl/err.h> | 
| 16 |  | #include <openssl/evp.h> | 
| 17 |  | #include <openssl/tls1.h> | 
| 18 |  | #include <openssl/x509.h> | 
| 19 |  |  | 
| 20 |  | #include "ct_local.h" | 
| 21 |  |  | 
| 22 |  | SCT *SCT_new(void) | 
| 23 | 564k | { | 
| 24 | 564k |     SCT *sct = OPENSSL_zalloc(sizeof(*sct)); | 
| 25 |  |  | 
| 26 | 564k |     if (sct == NULL) { | 
| 27 | 0 |         ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE); | 
| 28 | 0 |         return NULL; | 
| 29 | 0 |     } | 
| 30 |  |  | 
| 31 | 564k |     sct->entry_type = CT_LOG_ENTRY_TYPE_NOT_SET; | 
| 32 | 564k |     sct->version = SCT_VERSION_NOT_SET; | 
| 33 | 564k |     return sct; | 
| 34 | 564k | } | 
| 35 |  |  | 
| 36 |  | void SCT_free(SCT *sct) | 
| 37 | 564k | { | 
| 38 | 564k |     if (sct == NULL) | 
| 39 | 0 |         return; | 
| 40 |  |  | 
| 41 | 564k |     OPENSSL_free(sct->log_id); | 
| 42 | 564k |     OPENSSL_free(sct->ext); | 
| 43 | 564k |     OPENSSL_free(sct->sig); | 
| 44 | 564k |     OPENSSL_free(sct->sct); | 
| 45 | 564k |     OPENSSL_free(sct); | 
| 46 | 564k | } | 
| 47 |  |  | 
| 48 |  | void SCT_LIST_free(STACK_OF(SCT) *a) | 
| 49 | 90.2k | { | 
| 50 | 90.2k |     sk_SCT_pop_free(a, SCT_free); | 
| 51 | 90.2k | } | 
| 52 |  |  | 
| 53 |  | int SCT_set_version(SCT *sct, sct_version_t version) | 
| 54 | 0 | { | 
| 55 | 0 |     if (version != SCT_VERSION_V1) { | 
| 56 | 0 |         ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_VERSION); | 
| 57 | 0 |         return 0; | 
| 58 | 0 |     } | 
| 59 | 0 |     sct->version = version; | 
| 60 | 0 |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 61 | 0 |     return 1; | 
| 62 | 0 | } | 
| 63 |  |  | 
| 64 |  | int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type) | 
| 65 | 63.9k | { | 
| 66 | 63.9k |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 67 |  |  | 
| 68 | 63.9k |     switch (entry_type) { | 
| 69 | 55.6k |     case CT_LOG_ENTRY_TYPE_X509: | 
| 70 | 63.9k |     case CT_LOG_ENTRY_TYPE_PRECERT: | 
| 71 | 63.9k |         sct->entry_type = entry_type; | 
| 72 | 63.9k |         return 1; | 
| 73 | 0 |     case CT_LOG_ENTRY_TYPE_NOT_SET: | 
| 74 | 0 |         break; | 
| 75 | 63.9k |     } | 
| 76 | 63.9k |     ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_ENTRY_TYPE); | 
| 77 | 0 |     return 0; | 
| 78 | 63.9k | } | 
| 79 |  |  | 
| 80 |  | int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len) | 
| 81 | 0 | { | 
| 82 | 0 |     if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) { | 
| 83 | 0 |         ERR_raise(ERR_LIB_CT, CT_R_INVALID_LOG_ID_LENGTH); | 
| 84 | 0 |         return 0; | 
| 85 | 0 |     } | 
| 86 |  |  | 
| 87 | 0 |     OPENSSL_free(sct->log_id); | 
| 88 | 0 |     sct->log_id = log_id; | 
| 89 | 0 |     sct->log_id_len = log_id_len; | 
| 90 | 0 |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 91 | 0 |     return 1; | 
| 92 | 0 | } | 
| 93 |  |  | 
| 94 |  | int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len) | 
| 95 | 0 | { | 
| 96 | 0 |     if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) { | 
| 97 | 0 |         ERR_raise(ERR_LIB_CT, CT_R_INVALID_LOG_ID_LENGTH); | 
| 98 | 0 |         return 0; | 
| 99 | 0 |     } | 
| 100 |  |  | 
| 101 | 0 |     OPENSSL_free(sct->log_id); | 
| 102 | 0 |     sct->log_id = NULL; | 
| 103 | 0 |     sct->log_id_len = 0; | 
| 104 | 0 |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 105 |  | 
 | 
| 106 | 0 |     if (log_id != NULL && log_id_len > 0) { | 
| 107 | 0 |         sct->log_id = OPENSSL_memdup(log_id, log_id_len); | 
| 108 | 0 |         if (sct->log_id == NULL) { | 
| 109 | 0 |             ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE); | 
| 110 | 0 |             return 0; | 
| 111 | 0 |         } | 
| 112 | 0 |         sct->log_id_len = log_id_len; | 
| 113 | 0 |     } | 
| 114 | 0 |     return 1; | 
| 115 | 0 | } | 
| 116 |  |  | 
| 117 |  |  | 
| 118 |  | void SCT_set_timestamp(SCT *sct, uint64_t timestamp) | 
| 119 | 0 | { | 
| 120 | 0 |     sct->timestamp = timestamp; | 
| 121 | 0 |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 122 | 0 | } | 
| 123 |  |  | 
| 124 |  | int SCT_set_signature_nid(SCT *sct, int nid) | 
| 125 | 0 | { | 
| 126 | 0 |     switch (nid) { | 
| 127 | 0 |     case NID_sha256WithRSAEncryption: | 
| 128 | 0 |         sct->hash_alg = TLSEXT_hash_sha256; | 
| 129 | 0 |         sct->sig_alg = TLSEXT_signature_rsa; | 
| 130 | 0 |         sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 131 | 0 |         return 1; | 
| 132 | 0 |     case NID_ecdsa_with_SHA256: | 
| 133 | 0 |         sct->hash_alg = TLSEXT_hash_sha256; | 
| 134 | 0 |         sct->sig_alg = TLSEXT_signature_ecdsa; | 
| 135 | 0 |         sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 136 | 0 |         return 1; | 
| 137 | 0 |     default: | 
| 138 | 0 |         ERR_raise(ERR_LIB_CT, CT_R_UNRECOGNIZED_SIGNATURE_NID); | 
| 139 | 0 |         return 0; | 
| 140 | 0 |     } | 
| 141 | 0 | } | 
| 142 |  |  | 
| 143 |  | void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len) | 
| 144 | 0 | { | 
| 145 | 0 |     OPENSSL_free(sct->ext); | 
| 146 | 0 |     sct->ext = ext; | 
| 147 | 0 |     sct->ext_len = ext_len; | 
| 148 | 0 |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 149 | 0 | } | 
| 150 |  |  | 
| 151 |  | int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len) | 
| 152 | 0 | { | 
| 153 | 0 |     OPENSSL_free(sct->ext); | 
| 154 | 0 |     sct->ext = NULL; | 
| 155 | 0 |     sct->ext_len = 0; | 
| 156 | 0 |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 157 |  | 
 | 
| 158 | 0 |     if (ext != NULL && ext_len > 0) { | 
| 159 | 0 |         sct->ext = OPENSSL_memdup(ext, ext_len); | 
| 160 | 0 |         if (sct->ext == NULL) { | 
| 161 | 0 |             ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE); | 
| 162 | 0 |             return 0; | 
| 163 | 0 |         } | 
| 164 | 0 |         sct->ext_len = ext_len; | 
| 165 | 0 |     } | 
| 166 | 0 |     return 1; | 
| 167 | 0 | } | 
| 168 |  |  | 
| 169 |  | void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len) | 
| 170 | 0 | { | 
| 171 | 0 |     OPENSSL_free(sct->sig); | 
| 172 | 0 |     sct->sig = sig; | 
| 173 | 0 |     sct->sig_len = sig_len; | 
| 174 | 0 |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 175 | 0 | } | 
| 176 |  |  | 
| 177 |  | int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len) | 
| 178 | 49.8k | { | 
| 179 | 49.8k |     OPENSSL_free(sct->sig); | 
| 180 | 49.8k |     sct->sig = NULL; | 
| 181 | 49.8k |     sct->sig_len = 0; | 
| 182 | 49.8k |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 183 |  |  | 
| 184 | 49.8k |     if (sig != NULL && sig_len > 0) { | 
| 185 | 22.5k |         sct->sig = OPENSSL_memdup(sig, sig_len); | 
| 186 | 22.5k |         if (sct->sig == NULL) { | 
| 187 | 0 |             ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE); | 
| 188 | 0 |             return 0; | 
| 189 | 0 |         } | 
| 190 | 22.5k |         sct->sig_len = sig_len; | 
| 191 | 22.5k |     } | 
| 192 | 49.8k |     return 1; | 
| 193 | 49.8k | } | 
| 194 |  |  | 
| 195 |  | sct_version_t SCT_get_version(const SCT *sct) | 
| 196 | 0 | { | 
| 197 | 0 |     return sct->version; | 
| 198 | 0 | } | 
| 199 |  |  | 
| 200 |  | ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct) | 
| 201 | 0 | { | 
| 202 | 0 |     return sct->entry_type; | 
| 203 | 0 | } | 
| 204 |  |  | 
| 205 |  | size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id) | 
| 206 | 0 | { | 
| 207 | 0 |     *log_id = sct->log_id; | 
| 208 | 0 |     return sct->log_id_len; | 
| 209 | 0 | } | 
| 210 |  |  | 
| 211 |  | uint64_t SCT_get_timestamp(const SCT *sct) | 
| 212 | 0 | { | 
| 213 | 0 |     return sct->timestamp; | 
| 214 | 0 | } | 
| 215 |  |  | 
| 216 |  | int SCT_get_signature_nid(const SCT *sct) | 
| 217 | 119k | { | 
| 218 | 119k |     if (sct->version == SCT_VERSION_V1) { | 
| 219 | 119k |         if (sct->hash_alg == TLSEXT_hash_sha256) { | 
| 220 | 117k |             switch (sct->sig_alg) { | 
| 221 | 29.8k |             case TLSEXT_signature_ecdsa: | 
| 222 | 29.8k |                 return NID_ecdsa_with_SHA256; | 
| 223 | 86.4k |             case TLSEXT_signature_rsa: | 
| 224 | 86.4k |                 return NID_sha256WithRSAEncryption; | 
| 225 | 1.24k |             default: | 
| 226 | 1.24k |                 return NID_undef; | 
| 227 | 117k |             } | 
| 228 | 117k |         } | 
| 229 | 119k |     } | 
| 230 | 2.28k |     return NID_undef; | 
| 231 | 119k | } | 
| 232 |  |  | 
| 233 |  | size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext) | 
| 234 | 0 | { | 
| 235 | 0 |     *ext = sct->ext; | 
| 236 | 0 |     return sct->ext_len; | 
| 237 | 0 | } | 
| 238 |  |  | 
| 239 |  | size_t SCT_get0_signature(const SCT *sct, unsigned char **sig) | 
| 240 | 0 | { | 
| 241 | 0 |     *sig = sct->sig; | 
| 242 | 0 |     return sct->sig_len; | 
| 243 | 0 | } | 
| 244 |  |  | 
| 245 |  | int SCT_is_complete(const SCT *sct) | 
| 246 | 586k | { | 
| 247 | 586k |     switch (sct->version) { | 
| 248 | 0 |     case SCT_VERSION_NOT_SET: | 
| 249 | 0 |         return 0; | 
| 250 | 12.8k |     case SCT_VERSION_V1: | 
| 251 | 12.8k |         return sct->log_id != NULL && SCT_signature_is_complete(sct); | 
| 252 | 574k |     default: | 
| 253 | 574k |         return sct->sct != NULL; /* Just need cached encoding */ | 
| 254 | 586k |     } | 
| 255 | 586k | } | 
| 256 |  |  | 
| 257 |  | int SCT_signature_is_complete(const SCT *sct) | 
| 258 | 18.6k | { | 
| 259 | 18.6k |     return SCT_get_signature_nid(sct) != NID_undef && | 
| 260 | 18.6k |         sct->sig != NULL && sct->sig_len > 0; | 
| 261 | 18.6k | } | 
| 262 |  |  | 
| 263 |  | sct_source_t SCT_get_source(const SCT *sct) | 
| 264 | 0 | { | 
| 265 | 0 |     return sct->source; | 
| 266 | 0 | } | 
| 267 |  |  | 
| 268 |  | int SCT_set_source(SCT *sct, sct_source_t source) | 
| 269 | 63.9k | { | 
| 270 | 63.9k |     sct->source = source; | 
| 271 | 63.9k |     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; | 
| 272 | 63.9k |     switch (source) { | 
| 273 | 0 |     case SCT_SOURCE_TLS_EXTENSION: | 
| 274 | 55.6k |     case SCT_SOURCE_OCSP_STAPLED_RESPONSE: | 
| 275 | 55.6k |         return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509); | 
| 276 | 8.34k |     case SCT_SOURCE_X509V3_EXTENSION: | 
| 277 | 8.34k |         return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT); | 
| 278 | 0 |     case SCT_SOURCE_UNKNOWN: | 
| 279 | 0 |         break; | 
| 280 | 63.9k |     } | 
| 281 |  |     /* if we aren't sure, leave the log entry type alone */ | 
| 282 | 0 |     return 1; | 
| 283 | 63.9k | } | 
| 284 |  |  | 
| 285 |  | sct_validation_status_t SCT_get_validation_status(const SCT *sct) | 
| 286 | 0 | { | 
| 287 | 0 |     return sct->validation_status; | 
| 288 | 0 | } | 
| 289 |  |  | 
| 290 |  | int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx) | 
| 291 | 0 | { | 
| 292 | 0 |     int is_sct_valid = -1; | 
| 293 | 0 |     SCT_CTX *sctx = NULL; | 
| 294 | 0 |     X509_PUBKEY *pub = NULL, *log_pkey = NULL; | 
| 295 | 0 |     const CTLOG *log; | 
| 296 |  |  | 
| 297 |  |     /* | 
| 298 |  |      * With an unrecognized SCT version we don't know what such an SCT means, | 
| 299 |  |      * let alone validate one.  So we return validation failure (0). | 
| 300 |  |      */ | 
| 301 | 0 |     if (sct->version != SCT_VERSION_V1) { | 
| 302 | 0 |         sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION; | 
| 303 | 0 |         return 0; | 
| 304 | 0 |     } | 
| 305 |  |  | 
| 306 | 0 |     log = CTLOG_STORE_get0_log_by_id(ctx->log_store, | 
| 307 | 0 |                                      sct->log_id, sct->log_id_len); | 
| 308 |  |  | 
| 309 |  |     /* Similarly, an SCT from an unknown log also cannot be validated. */ | 
| 310 | 0 |     if (log == NULL) { | 
| 311 | 0 |         sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG; | 
| 312 | 0 |         return 0; | 
| 313 | 0 |     } | 
| 314 |  |  | 
| 315 | 0 |     sctx = SCT_CTX_new(ctx->libctx, ctx->propq); | 
| 316 | 0 |     if (sctx == NULL) | 
| 317 | 0 |         goto err; | 
| 318 |  |  | 
| 319 | 0 |     if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(log)) != 1) | 
| 320 | 0 |         goto err; | 
| 321 | 0 |     if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1) | 
| 322 | 0 |         goto err; | 
| 323 |  |  | 
| 324 | 0 |     if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) { | 
| 325 | 0 |         EVP_PKEY *issuer_pkey; | 
| 326 |  | 
 | 
| 327 | 0 |         if (ctx->issuer == NULL) { | 
| 328 | 0 |             sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED; | 
| 329 | 0 |             goto end; | 
| 330 | 0 |         } | 
| 331 |  |  | 
| 332 | 0 |         issuer_pkey = X509_get0_pubkey(ctx->issuer); | 
| 333 |  | 
 | 
| 334 | 0 |         if (X509_PUBKEY_set(&pub, issuer_pkey) != 1) | 
| 335 | 0 |             goto err; | 
| 336 | 0 |         if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1) | 
| 337 | 0 |             goto err; | 
| 338 | 0 |     } | 
| 339 |  |  | 
| 340 | 0 |     SCT_CTX_set_time(sctx, ctx->epoch_time_in_ms); | 
| 341 |  |  | 
| 342 |  |     /* | 
| 343 |  |      * XXX: Potential for optimization.  This repeats some idempotent heavy | 
| 344 |  |      * lifting on the certificate for each candidate SCT, and appears to not | 
| 345 |  |      * use any information in the SCT itself, only the certificate is | 
| 346 |  |      * processed.  So it may make more sense to do this just once, perhaps | 
| 347 |  |      * associated with the shared (by all SCTs) policy eval ctx. | 
| 348 |  |      * | 
| 349 |  |      * XXX: Failure here is global (SCT independent) and represents either an | 
| 350 |  |      * issue with the certificate (e.g. duplicate extensions) or an out of | 
| 351 |  |      * memory condition.  When the certificate is incompatible with CT, we just | 
| 352 |  |      * mark the SCTs invalid, rather than report a failure to determine the | 
| 353 |  |      * validation status.  That way, callbacks that want to do "soft" SCT | 
| 354 |  |      * processing will not abort handshakes with false positive internal | 
| 355 |  |      * errors.  Since the function does not distinguish between certificate | 
| 356 |  |      * issues (peer's fault) and internal problems (out fault) the safe thing | 
| 357 |  |      * to do is to report a validation failure and let the callback or | 
| 358 |  |      * application decide what to do. | 
| 359 |  |      */ | 
| 360 | 0 |     if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1) | 
| 361 | 0 |         sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED; | 
| 362 | 0 |     else | 
| 363 | 0 |         sct->validation_status = SCT_CTX_verify(sctx, sct) == 1 ? | 
| 364 | 0 |             SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID; | 
| 365 |  | 
 | 
| 366 | 0 | end: | 
| 367 | 0 |     is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID; | 
| 368 | 0 | err: | 
| 369 | 0 |     X509_PUBKEY_free(pub); | 
| 370 | 0 |     X509_PUBKEY_free(log_pkey); | 
| 371 | 0 |     SCT_CTX_free(sctx); | 
| 372 |  | 
 | 
| 373 | 0 |     return is_sct_valid; | 
| 374 | 0 | } | 
| 375 |  |  | 
| 376 |  | int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx) | 
| 377 | 0 | { | 
| 378 | 0 |     int are_scts_valid = 1; | 
| 379 | 0 |     int sct_count = scts != NULL ? sk_SCT_num(scts) : 0; | 
| 380 | 0 |     int i; | 
| 381 |  | 
 | 
| 382 | 0 |     for (i = 0; i < sct_count; ++i) { | 
| 383 | 0 |         int is_sct_valid = -1; | 
| 384 | 0 |         SCT *sct = sk_SCT_value(scts, i); | 
| 385 |  | 
 | 
| 386 | 0 |         if (sct == NULL) | 
| 387 | 0 |             continue; | 
| 388 |  |  | 
| 389 | 0 |         is_sct_valid = SCT_validate(sct, ctx); | 
| 390 | 0 |         if (is_sct_valid < 0) | 
| 391 | 0 |             return is_sct_valid; | 
| 392 | 0 |         are_scts_valid &= is_sct_valid; | 
| 393 | 0 |     } | 
| 394 |  |  | 
| 395 | 0 |     return are_scts_valid; | 
| 396 | 0 | } |