1/* $OpenBSD: ct.h,v 1.7 2022/05/08 20:59:32 tb Exp $ */ 2/* 3 * Public API for Certificate Transparency (CT). 4 * Written by Rob Percival (robpercival@google.com) for the OpenSSL project. 5 */ 6/* ==================================================================== 7 * Copyright (c) 2016 The OpenSSL Project. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. All advertising materials mentioning features or use of this 22 * software must display the following acknowledgment: 23 * "This product includes software developed by the OpenSSL Project 24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25 * 26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27 * endorse or promote products derived from this software without 28 * prior written permission. For written permission, please contact 29 * licensing@OpenSSL.org. 30 * 31 * 5. Products derived from this software may not be called "OpenSSL" 32 * nor may "OpenSSL" appear in their names without prior written 33 * permission of the OpenSSL Project. 34 * 35 * 6. Redistributions of any form whatsoever must retain the following 36 * acknowledgment: 37 * "This product includes software developed by the OpenSSL Project 38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51 * OF THE POSSIBILITY OF SUCH DAMAGE. 52 * ==================================================================== 53 */ 54 55#ifndef HEADER_CT_H 56#define HEADER_CT_H 57 58#include <openssl/opensslconf.h> 59 60#ifndef OPENSSL_NO_CT 61#include <openssl/ossl_typ.h> 62#include <openssl/safestack.h> 63#include <openssl/x509.h> 64#ifdef __cplusplus 65extern "C" { 66#endif 67 68/* Minimum RSA key size, from RFC6962 */ 69#define SCT_MIN_RSA_BITS 2048 70 71/* All hashes are SHA256 in v1 of Certificate Transparency */ 72#define CT_V1_HASHLEN SHA256_DIGEST_LENGTH 73 74typedef enum { 75 CT_LOG_ENTRY_TYPE_NOT_SET = -1, 76 CT_LOG_ENTRY_TYPE_X509 = 0, 77 CT_LOG_ENTRY_TYPE_PRECERT = 1 78} ct_log_entry_type_t; 79 80typedef enum { 81 SCT_VERSION_NOT_SET = -1, 82 SCT_VERSION_V1 = 0 83} sct_version_t; 84 85typedef enum { 86 SCT_SOURCE_UNKNOWN, 87 SCT_SOURCE_TLS_EXTENSION, 88 SCT_SOURCE_X509V3_EXTENSION, 89 SCT_SOURCE_OCSP_STAPLED_RESPONSE 90} sct_source_t; 91 92typedef enum { 93 SCT_VALIDATION_STATUS_NOT_SET, 94 SCT_VALIDATION_STATUS_UNKNOWN_LOG, 95 SCT_VALIDATION_STATUS_VALID, 96 SCT_VALIDATION_STATUS_INVALID, 97 SCT_VALIDATION_STATUS_UNVERIFIED, 98 SCT_VALIDATION_STATUS_UNKNOWN_VERSION 99} sct_validation_status_t; 100 101DECLARE_STACK_OF(SCT) 102DECLARE_STACK_OF(CTLOG) 103 104/****************************************** 105 * CT policy evaluation context functions * 106 ******************************************/ 107 108/* 109 * Creates a new, empty policy evaluation context. 110 * The caller is responsible for calling CT_POLICY_EVAL_CTX_free when finished 111 * with the CT_POLICY_EVAL_CTX. 112 */ 113CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void); 114 115/* Deletes a policy evaluation context and anything it owns. */ 116void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx); 117 118/* Gets the peer certificate that the SCTs are for */ 119X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx); 120 121/* 122 * Sets the certificate associated with the received SCTs. 123 * Increments the reference count of cert. 124 * Returns 1 on success, 0 otherwise. 125 */ 126int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert); 127 128/* Gets the issuer of the aforementioned certificate */ 129X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx); 130 131/* 132 * Sets the issuer of the certificate associated with the received SCTs. 133 * Increments the reference count of issuer. 134 * Returns 1 on success, 0 otherwise. 135 */ 136int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer); 137 138/* Gets the CT logs that are trusted sources of SCTs */ 139const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx); 140 141/* Sets the log store that is in use. It must outlive the CT_POLICY_EVAL_CTX. */ 142void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx, 143 CTLOG_STORE *log_store); 144 145/* 146 * Gets the time, in milliseconds since the Unix epoch, that will be used as the 147 * current time when checking whether an SCT was issued in the future. 148 * Such SCTs will fail validation, as required by RFC6962. 149 */ 150uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx); 151 152/* 153 * Sets the time to evaluate SCTs against, in milliseconds since the Unix epoch. 154 * If an SCT's timestamp is after this time, it will be interpreted as having 155 * been issued in the future. RFC6962 states that "TLS clients MUST reject SCTs 156 * whose timestamp is in the future", so an SCT will not validate in this case. 157 */ 158void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms); 159 160/***************** 161 * SCT functions * 162 *****************/ 163 164/* 165 * Creates a new, blank SCT. 166 * The caller is responsible for calling SCT_free when finished with the SCT. 167 */ 168SCT *SCT_new(void); 169 170/* 171 * Creates a new SCT from some base64-encoded strings. 172 * The caller is responsible for calling SCT_free when finished with the SCT. 173 */ 174SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64, 175 ct_log_entry_type_t entry_type, uint64_t timestamp, 176 const char *extensions_base64, const char *signature_base64); 177 178/* 179 * Frees the SCT and the underlying data structures. 180 */ 181void SCT_free(SCT *sct); 182 183/* 184 * Free a stack of SCTs, and the underlying SCTs themselves. 185 * Intended to be compatible with X509V3_EXT_FREE. 186 */ 187void SCT_LIST_free(STACK_OF(SCT) *a); 188 189/* 190 * Returns the version of the SCT. 191 */ 192sct_version_t SCT_get_version(const SCT *sct); 193 194/* 195 * Set the version of an SCT. 196 * Returns 1 on success, 0 if the version is unrecognized. 197 */ 198int SCT_set_version(SCT *sct, sct_version_t version); 199 200/* 201 * Returns the log entry type of the SCT. 202 */ 203ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct); 204 205/* 206 * Set the log entry type of an SCT. 207 * Returns 1 on success, 0 otherwise. 208 */ 209int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type); 210 211/* 212 * Gets the ID of the log that an SCT came from. 213 * Ownership of the log ID remains with the SCT. 214 * Returns the length of the log ID. 215 */ 216size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id); 217 218/* 219 * Set the log ID of an SCT to point directly to the *log_id specified. 220 * The SCT takes ownership of the specified pointer. 221 * Returns 1 on success, 0 otherwise. 222 */ 223int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len); 224 225/* 226 * Set the log ID of an SCT. 227 * This makes a copy of the log_id. 228 * Returns 1 on success, 0 otherwise. 229 */ 230int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, 231 size_t log_id_len); 232 233/* 234 * Returns the timestamp for the SCT (epoch time in milliseconds). 235 */ 236uint64_t SCT_get_timestamp(const SCT *sct); 237 238/* 239 * Set the timestamp of an SCT (epoch time in milliseconds). 240 */ 241void SCT_set_timestamp(SCT *sct, uint64_t timestamp); 242 243/* 244 * Return the NID for the signature used by the SCT. 245 * For CT v1, this will be either NID_sha256WithRSAEncryption or 246 * NID_ecdsa_with_SHA256 (or NID_undef if incorrect/unset). 247 */ 248int SCT_get_signature_nid(const SCT *sct); 249 250/* 251 * Set the signature type of an SCT 252 * For CT v1, this should be either NID_sha256WithRSAEncryption or 253 * NID_ecdsa_with_SHA256. 254 * Returns 1 on success, 0 otherwise. 255 */ 256int SCT_set_signature_nid(SCT *sct, int nid); 257 258/* 259 * Set *ext to point to the extension data for the SCT. ext must not be NULL. 260 * The SCT retains ownership of this pointer. 261 * Returns length of the data pointed to. 262 */ 263size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext); 264 265/* 266 * Set the extensions of an SCT to point directly to the *ext specified. 267 * The SCT takes ownership of the specified pointer. 268 */ 269void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len); 270 271/* 272 * Set the extensions of an SCT. 273 * This takes a copy of the ext. 274 * Returns 1 on success, 0 otherwise. 275 */ 276int SCT_set1_extensions(SCT *sct, const unsigned char *ext, 277 size_t ext_len); 278 279/* 280 * Set *sig to point to the signature for the SCT. sig must not be NULL. 281 * The SCT retains ownership of this pointer. 282 * Returns length of the data pointed to. 283 */ 284size_t SCT_get0_signature(const SCT *sct, unsigned char **sig); 285 286/* 287 * Set the signature of an SCT to point directly to the *sig specified. 288 * The SCT takes ownership of the specified pointer. 289 */ 290void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len); 291 292/* 293 * Set the signature of an SCT to be a copy of the *sig specified. 294 * Returns 1 on success, 0 otherwise. 295 */ 296int SCT_set1_signature(SCT *sct, const unsigned char *sig, 297 size_t sig_len); 298 299/* 300 * The origin of this SCT, e.g. TLS extension, OCSP response, etc. 301 */ 302sct_source_t SCT_get_source(const SCT *sct); 303 304/* 305 * Set the origin of this SCT, e.g. TLS extension, OCSP response, etc. 306 * Returns 1 on success, 0 otherwise. 307 */ 308int SCT_set_source(SCT *sct, sct_source_t source); 309 310/* 311 * Returns a text string describing the validation status of |sct|. 312 */ 313const char *SCT_validation_status_string(const SCT *sct); 314 315/* 316 * Pretty-prints an |sct| to |out|. 317 * It will be indented by the number of spaces specified by |indent|. 318 * If |logs| is not NULL, it will be used to lookup the CT log that the SCT came 319 * from, so that the log name can be printed. 320 */ 321void SCT_print(const SCT *sct, BIO *out, int indent, const CTLOG_STORE *logs); 322 323/* 324 * Pretty-prints an |sct_list| to |out|. 325 * It will be indented by the number of spaces specified by |indent|. 326 * SCTs will be delimited by |separator|. 327 * If |logs| is not NULL, it will be used to lookup the CT log that each SCT 328 * came from, so that the log names can be printed. 329 */ 330void SCT_LIST_print(const STACK_OF(SCT) *sct_list, BIO *out, int indent, 331 const char *separator, const CTLOG_STORE *logs); 332 333/* 334 * Gets the last result of validating this SCT. 335 * If it has not been validated yet, returns SCT_VALIDATION_STATUS_NOT_SET. 336 */ 337sct_validation_status_t SCT_get_validation_status(const SCT *sct); 338 339/* 340 * Validates the given SCT with the provided context. 341 * Sets the "validation_status" field of the SCT. 342 * Returns 1 if the SCT is valid and the signature verifies. 343 * Returns 0 if the SCT is invalid or could not be verified. 344 * Returns -1 if an error occurs. 345 */ 346int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx); 347 348/* 349 * Validates the given list of SCTs with the provided context. 350 * Sets the "validation_status" field of each SCT. 351 * Returns 1 if there are no invalid SCTs and all signatures verify. 352 * Returns 0 if at least one SCT is invalid or could not be verified. 353 * Returns a negative integer if an error occurs. 354 */ 355int SCT_LIST_validate(const STACK_OF(SCT) *scts, 356 CT_POLICY_EVAL_CTX *ctx); 357 358 359/********************************* 360 * SCT parsing and serialisation * 361 *********************************/ 362 363/* 364 * Serialize (to TLS format) a stack of SCTs and return the length. 365 * "a" must not be NULL. 366 * If "pp" is NULL, just return the length of what would have been serialized. 367 * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer 368 * for data that caller is responsible for freeing (only if function returns 369 * successfully). 370 * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring 371 * that "*pp" is large enough to accept all of the serialized data. 372 * Returns < 0 on error, >= 0 indicating bytes written (or would have been) 373 * on success. 374 */ 375int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp); 376 377/* 378 * Convert TLS format SCT list to a stack of SCTs. 379 * If "a" or "*a" is NULL, a new stack will be created that the caller is 380 * responsible for freeing (by calling SCT_LIST_free). 381 * "**pp" and "*pp" must not be NULL. 382 * Upon success, "*pp" will point to after the last bytes read, and a stack 383 * will be returned. 384 * Upon failure, a NULL pointer will be returned, and the position of "*pp" is 385 * not defined. 386 */ 387STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, 388 size_t len); 389 390/* 391 * Serialize (to DER format) a stack of SCTs and return the length. 392 * "a" must not be NULL. 393 * If "pp" is NULL, just returns the length of what would have been serialized. 394 * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer 395 * for data that caller is responsible for freeing (only if function returns 396 * successfully). 397 * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring 398 * that "*pp" is large enough to accept all of the serialized data. 399 * Returns < 0 on error, >= 0 indicating bytes written (or would have been) 400 * on success. 401 */ 402int i2d_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp); 403 404/* 405 * Parses an SCT list in DER format and returns it. 406 * If "a" or "*a" is NULL, a new stack will be created that the caller is 407 * responsible for freeing (by calling SCT_LIST_free). 408 * "**pp" and "*pp" must not be NULL. 409 * Upon success, "*pp" will point to after the last bytes read, and a stack 410 * will be returned. 411 * Upon failure, a NULL pointer will be returned, and the position of "*pp" is 412 * not defined. 413 */ 414STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, 415 long len); 416 417/* 418 * Serialize (to TLS format) an |sct| and write it to |out|. 419 * If |out| is null, no SCT will be output but the length will still be returned. 420 * If |out| points to a null pointer, a string will be allocated to hold the 421 * TLS-format SCT. It is the responsibility of the caller to free it. 422 * If |out| points to an allocated string, the TLS-format SCT will be written 423 * to it. 424 * The length of the SCT in TLS format will be returned. 425 */ 426int i2o_SCT(const SCT *sct, unsigned char **out); 427 428/* 429 * Parses an SCT in TLS format and returns it. 430 * If |psct| is not null, it will end up pointing to the parsed SCT. If it 431 * already points to a non-null pointer, the pointer will be free'd. 432 * |in| should be a pointer to a string containing the TLS-format SCT. 433 * |in| will be advanced to the end of the SCT if parsing succeeds. 434 * |len| should be the length of the SCT in |in|. 435 * Returns NULL if an error occurs. 436 * If the SCT is an unsupported version, only the SCT's 'sct' and 'sct_len' 437 * fields will be populated (with |in| and |len| respectively). 438 */ 439SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len); 440 441/******************** 442 * CT log functions * 443 ********************/ 444 445/* 446 * Creates a new CT log instance with the given |public_key| and |name|. 447 * Takes ownership of |public_key| but copies |name|. 448 * Returns NULL if malloc fails or if |public_key| cannot be converted to DER. 449 * Should be deleted by the caller using CTLOG_free when no longer needed. 450 */ 451CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name); 452 453/* 454 * Creates a new CTLOG instance with the base64-encoded SubjectPublicKeyInfo DER 455 * in |pkey_base64|. The |name| is a string to help users identify this log. 456 * Returns 1 on success, 0 on failure. 457 * Should be deleted by the caller using CTLOG_free when no longer needed. 458 */ 459int CTLOG_new_from_base64(CTLOG **ct_log, const char *pkey_base64, 460 const char *name); 461 462/* 463 * Deletes a CT log instance and its fields. 464 */ 465void CTLOG_free(CTLOG *log); 466 467/* Gets the name of the CT log */ 468const char *CTLOG_get0_name(const CTLOG *log); 469/* Gets the ID of the CT log */ 470void CTLOG_get0_log_id(const CTLOG *log, const uint8_t **log_id, 471 size_t *log_id_len); 472/* Gets the public key of the CT log */ 473EVP_PKEY *CTLOG_get0_public_key(const CTLOG *log); 474 475/************************** 476 * CT log store functions * 477 **************************/ 478 479/* 480 * Creates a new CT log store. 481 * Should be deleted by the caller using CTLOG_STORE_free when no longer needed. 482 */ 483CTLOG_STORE *CTLOG_STORE_new(void); 484 485/* 486 * Deletes a CT log store and all of the CT log instances held within. 487 */ 488void CTLOG_STORE_free(CTLOG_STORE *store); 489 490/* 491 * Finds a CT log in the store based on its log ID. 492 * Returns the CT log, or NULL if no match is found. 493 */ 494const CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store, 495 const uint8_t *log_id, size_t log_id_len); 496 497/* 498 * Loads a CT log list into a |store| from a |file|. 499 * Returns 1 if loading is successful, or 0 otherwise. 500 */ 501int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file); 502 503/* 504 * Loads the default CT log list into a |store|. 505 * Returns 1 if loading is successful, or 0 otherwise. 506 */ 507int CTLOG_STORE_load_default_file(CTLOG_STORE *store); 508 509int ERR_load_CT_strings(void); 510 511/* 512 * CT function codes. 513 */ 514# define CT_F_CTLOG_NEW 117 515# define CT_F_CTLOG_NEW_FROM_BASE64 118 516# define CT_F_CTLOG_NEW_FROM_CONF 119 517# define CT_F_CTLOG_STORE_LOAD_CTX_NEW 122 518# define CT_F_CTLOG_STORE_LOAD_FILE 123 519# define CT_F_CTLOG_STORE_LOAD_LOG 130 520# define CT_F_CTLOG_STORE_NEW 131 521# define CT_F_CT_BASE64_DECODE 124 522# define CT_F_CT_POLICY_EVAL_CTX_NEW 133 523# define CT_F_CT_V1_LOG_ID_FROM_PKEY 125 524# define CT_F_I2O_SCT 107 525# define CT_F_I2O_SCT_LIST 108 526# define CT_F_I2O_SCT_SIGNATURE 109 527# define CT_F_O2I_SCT 110 528# define CT_F_O2I_SCT_LIST 111 529# define CT_F_O2I_SCT_SIGNATURE 112 530# define CT_F_SCT_CTX_NEW 126 531# define CT_F_SCT_CTX_VERIFY 128 532# define CT_F_SCT_NEW 100 533# define CT_F_SCT_NEW_FROM_BASE64 127 534# define CT_F_SCT_SET0_LOG_ID 101 535# define CT_F_SCT_SET1_EXTENSIONS 114 536# define CT_F_SCT_SET1_LOG_ID 115 537# define CT_F_SCT_SET1_SIGNATURE 116 538# define CT_F_SCT_SET_LOG_ENTRY_TYPE 102 539# define CT_F_SCT_SET_SIGNATURE_NID 103 540# define CT_F_SCT_SET_VERSION 104 541 542/* 543 * CT reason codes. 544 */ 545# define CT_R_BASE64_DECODE_ERROR 108 546# define CT_R_INVALID_LOG_ID_LENGTH 100 547# define CT_R_LOG_CONF_INVALID 109 548# define CT_R_LOG_CONF_INVALID_KEY 110 549# define CT_R_LOG_CONF_MISSING_DESCRIPTION 111 550# define CT_R_LOG_CONF_MISSING_KEY 112 551# define CT_R_LOG_KEY_INVALID 113 552# define CT_R_SCT_FUTURE_TIMESTAMP 116 553# define CT_R_SCT_INVALID 104 554# define CT_R_SCT_INVALID_SIGNATURE 107 555# define CT_R_SCT_LIST_INVALID 105 556# define CT_R_SCT_LOG_ID_MISMATCH 114 557# define CT_R_SCT_NOT_SET 106 558# define CT_R_SCT_UNSUPPORTED_VERSION 115 559# define CT_R_UNRECOGNIZED_SIGNATURE_NID 101 560# define CT_R_UNSUPPORTED_ENTRY_TYPE 102 561# define CT_R_UNSUPPORTED_VERSION 103 562 563#ifdef __cplusplus 564} 565#endif 566#endif 567#endif 568