1*f6e7d166Sjsing /* $OpenBSD: ct_local.h,v 1.8 2021/12/20 17:19:19 jsing Exp $ */ 2137efc4eSbeck /* 3f732855dStb * Written by Rob Percival (robpercival@google.com) for the OpenSSL project. 4f732855dStb */ 5f732855dStb /* ==================================================================== 6f732855dStb * Copyright (c) 2016 The OpenSSL Project. All rights reserved. 7137efc4eSbeck * 8f732855dStb * Redistribution and use in source and binary forms, with or without 9f732855dStb * modification, are permitted provided that the following conditions 10f732855dStb * are met: 11f732855dStb * 12f732855dStb * 1. Redistributions of source code must retain the above copyright 13f732855dStb * notice, this list of conditions and the following disclaimer. 14f732855dStb * 15f732855dStb * 2. Redistributions in binary form must reproduce the above copyright 16f732855dStb * notice, this list of conditions and the following disclaimer in 17f732855dStb * the documentation and/or other materials provided with the 18f732855dStb * distribution. 19f732855dStb * 20f732855dStb * 3. All advertising materials mentioning features or use of this 21f732855dStb * software must display the following acknowledgment: 22f732855dStb * "This product includes software developed by the OpenSSL Project 23f732855dStb * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24f732855dStb * 25f732855dStb * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26f732855dStb * endorse or promote products derived from this software without 27f732855dStb * prior written permission. For written permission, please contact 28f732855dStb * licensing@OpenSSL.org. 29f732855dStb * 30f732855dStb * 5. Products derived from this software may not be called "OpenSSL" 31f732855dStb * nor may "OpenSSL" appear in their names without prior written 32f732855dStb * permission of the OpenSSL Project. 33f732855dStb * 34f732855dStb * 6. Redistributions of any form whatsoever must retain the following 35f732855dStb * acknowledgment: 36f732855dStb * "This product includes software developed by the OpenSSL Project 37f732855dStb * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38f732855dStb * 39f732855dStb * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40f732855dStb * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41f732855dStb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42f732855dStb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43f732855dStb * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44f732855dStb * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45f732855dStb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46f732855dStb * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47f732855dStb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48f732855dStb * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49f732855dStb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50f732855dStb * OF THE POSSIBILITY OF SUCH DAMAGE. 51f732855dStb * ==================================================================== 52137efc4eSbeck */ 53137efc4eSbeck 54137efc4eSbeck #include <stddef.h> 55036987b0Sjsing 56137efc4eSbeck #include <openssl/ct.h> 57137efc4eSbeck #include <openssl/evp.h> 58*f6e7d166Sjsing #include <openssl/safestack.h> 59137efc4eSbeck #include <openssl/x509.h> 60137efc4eSbeck #include <openssl/x509v3.h> 61*f6e7d166Sjsing 62*f6e7d166Sjsing #include "bytestring.h" 63137efc4eSbeck 64036987b0Sjsing /* Number of bytes in an SCT v1 LogID - see RFC 6962 section 3.2. */ 65036987b0Sjsing #define CT_V1_LOG_ID_LEN 32 66036987b0Sjsing 67036987b0Sjsing /* Maximum size of an SCT - see RFC 6962 section 3.3. */ 68137efc4eSbeck #define MAX_SCT_SIZE 65535 69137efc4eSbeck #define MAX_SCT_LIST_SIZE MAX_SCT_SIZE 70137efc4eSbeck 71137efc4eSbeck /* 72036987b0Sjsing * Macros to write integers in network-byte order. 73137efc4eSbeck */ 74137efc4eSbeck 75137efc4eSbeck #define s2n(s,c) ((c[0]=(unsigned char)(((s)>> 8)&0xff), \ 76137efc4eSbeck c[1]=(unsigned char)(((s) )&0xff)),c+=2) 77137efc4eSbeck 78137efc4eSbeck #define l2n3(l,c) ((c[0]=(unsigned char)(((l)>>16)&0xff), \ 79137efc4eSbeck c[1]=(unsigned char)(((l)>> 8)&0xff), \ 80137efc4eSbeck c[2]=(unsigned char)(((l) )&0xff)),c+=3) 81137efc4eSbeck 82137efc4eSbeck #define l2n8(l,c) (*((c)++)=(unsigned char)(((l)>>56)&0xff), \ 83137efc4eSbeck *((c)++)=(unsigned char)(((l)>>48)&0xff), \ 84137efc4eSbeck *((c)++)=(unsigned char)(((l)>>40)&0xff), \ 85137efc4eSbeck *((c)++)=(unsigned char)(((l)>>32)&0xff), \ 86137efc4eSbeck *((c)++)=(unsigned char)(((l)>>24)&0xff), \ 87137efc4eSbeck *((c)++)=(unsigned char)(((l)>>16)&0xff), \ 88137efc4eSbeck *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ 89137efc4eSbeck *((c)++)=(unsigned char)(((l) )&0xff)) 90137efc4eSbeck 91137efc4eSbeck /* Signed Certificate Timestamp */ 92137efc4eSbeck struct sct_st { 93137efc4eSbeck sct_version_t version; 94137efc4eSbeck /* If version is not SCT_VERSION_V1, this contains the encoded SCT */ 95137efc4eSbeck unsigned char *sct; 96137efc4eSbeck size_t sct_len; 9732d9a10fSbeck /* 9832d9a10fSbeck * If version is SCT_VERSION_V1, fields below contain components of 9932d9a10fSbeck * the SCT 10032d9a10fSbeck */ 101137efc4eSbeck unsigned char *log_id; 102137efc4eSbeck size_t log_id_len; 103137efc4eSbeck /* 104137efc4eSbeck * Note, we cannot distinguish between an unset timestamp, and one 105137efc4eSbeck * that is set to 0. However since CT didn't exist in 1970, no real 106137efc4eSbeck * SCT should ever be set as such. 107137efc4eSbeck */ 108137efc4eSbeck uint64_t timestamp; 109137efc4eSbeck unsigned char *ext; 110137efc4eSbeck size_t ext_len; 111137efc4eSbeck unsigned char hash_alg; 112137efc4eSbeck unsigned char sig_alg; 113137efc4eSbeck unsigned char *sig; 114137efc4eSbeck size_t sig_len; 115137efc4eSbeck /* Log entry type */ 116137efc4eSbeck ct_log_entry_type_t entry_type; 117137efc4eSbeck /* Where this SCT was found, e.g. certificate, OCSP response, etc. */ 118137efc4eSbeck sct_source_t source; 119137efc4eSbeck /* The result of the last attempt to validate this SCT. */ 120137efc4eSbeck sct_validation_status_t validation_status; 121137efc4eSbeck }; 122137efc4eSbeck 123137efc4eSbeck /* Miscellaneous data that is useful when verifying an SCT */ 124137efc4eSbeck struct sct_ctx_st { 125137efc4eSbeck /* Public key */ 126137efc4eSbeck EVP_PKEY *pkey; 127137efc4eSbeck /* Hash of public key */ 128137efc4eSbeck unsigned char *pkeyhash; 129137efc4eSbeck size_t pkeyhashlen; 130137efc4eSbeck /* For pre-certificate: issuer public key hash */ 131137efc4eSbeck unsigned char *ihash; 132137efc4eSbeck size_t ihashlen; 133137efc4eSbeck /* certificate encoding */ 134137efc4eSbeck unsigned char *certder; 135137efc4eSbeck size_t certderlen; 136137efc4eSbeck /* pre-certificate encoding */ 137137efc4eSbeck unsigned char *preder; 138137efc4eSbeck size_t prederlen; 13932d9a10fSbeck /* 14032d9a10fSbeck * milliseconds since epoch (to check that the SCT isn't from the 14132d9a10fSbeck * future) 14232d9a10fSbeck */ 143137efc4eSbeck uint64_t epoch_time_in_ms; 144137efc4eSbeck }; 145137efc4eSbeck 146137efc4eSbeck /* Context when evaluating whether a Certificate Transparency policy is met */ 147137efc4eSbeck struct ct_policy_eval_ctx_st { 148137efc4eSbeck X509 *cert; 149137efc4eSbeck X509 *issuer; 150137efc4eSbeck CTLOG_STORE *log_store; 15132d9a10fSbeck /* 15232d9a10fSbeck * milliseconds since epoch (to check that the SCT isn't from the 15332d9a10fSbeck * future) 15432d9a10fSbeck */ 155137efc4eSbeck uint64_t epoch_time_in_ms; 156137efc4eSbeck }; 157137efc4eSbeck 158137efc4eSbeck /* 159137efc4eSbeck * Creates a new context for verifying an SCT. 160137efc4eSbeck */ 161137efc4eSbeck SCT_CTX *SCT_CTX_new(void); 162137efc4eSbeck /* 163137efc4eSbeck * Deletes an SCT verification context. 164137efc4eSbeck */ 165137efc4eSbeck void SCT_CTX_free(SCT_CTX *sctx); 166137efc4eSbeck 167137efc4eSbeck /* 168137efc4eSbeck * Sets the certificate that the SCT was created for. 169137efc4eSbeck * If *cert does not have a poison extension, presigner must be NULL. 170137efc4eSbeck * If *cert does not have a poison extension, it may have a single SCT 171137efc4eSbeck * (NID_ct_precert_scts) extension. 172137efc4eSbeck * If either *cert or *presigner have an AKID (NID_authority_key_identifier) 173137efc4eSbeck * extension, both must have one. 174137efc4eSbeck * Returns 1 on success, 0 on failure. 175137efc4eSbeck */ 17632d9a10fSbeck int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner); 177137efc4eSbeck 178137efc4eSbeck /* 179137efc4eSbeck * Sets the issuer of the certificate that the SCT was created for. 180137efc4eSbeck * This is just a convenience method to save extracting the public key and 181137efc4eSbeck * calling SCT_CTX_set1_issuer_pubkey(). 182137efc4eSbeck * Issuer must not be NULL. 183137efc4eSbeck * Returns 1 on success, 0 on failure. 184137efc4eSbeck */ 18532d9a10fSbeck int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer); 186137efc4eSbeck 187137efc4eSbeck /* 188137efc4eSbeck * Sets the public key of the issuer of the certificate that the SCT was created 189137efc4eSbeck * for. 190137efc4eSbeck * The public key must not be NULL. 191137efc4eSbeck * Returns 1 on success, 0 on failure. 192137efc4eSbeck */ 19332d9a10fSbeck int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey); 194137efc4eSbeck 195137efc4eSbeck /* 196137efc4eSbeck * Sets the public key of the CT log that the SCT is from. 197137efc4eSbeck * Returns 1 on success, 0 on failure. 198137efc4eSbeck */ 19932d9a10fSbeck int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey); 200137efc4eSbeck 201137efc4eSbeck /* 202137efc4eSbeck * Sets the time to evaluate the SCT against, in milliseconds since the Unix 203137efc4eSbeck * epoch. If the SCT's timestamp is after this time, it will be interpreted as 204137efc4eSbeck * having been issued in the future. RFC6962 states that "TLS clients MUST 205137efc4eSbeck * reject SCTs whose timestamp is in the future", so an SCT will not validate 206137efc4eSbeck * in this case. 207137efc4eSbeck */ 208137efc4eSbeck void SCT_CTX_set_time(SCT_CTX *sctx, uint64_t time_in_ms); 209137efc4eSbeck 210137efc4eSbeck /* 211137efc4eSbeck * Verifies an SCT with the given context. 212137efc4eSbeck * Returns 1 if the SCT verifies successfully; any other value indicates 213137efc4eSbeck * failure. See EVP_DigestVerifyFinal() for the meaning of those values. 214137efc4eSbeck */ 21532d9a10fSbeck int SCT_CTX_verify(const SCT_CTX *sctx, const SCT *sct); 216137efc4eSbeck 217137efc4eSbeck /* 218137efc4eSbeck * Does this SCT have the minimum fields populated to be usable? 219137efc4eSbeck * Returns 1 if so, 0 otherwise. 220137efc4eSbeck */ 22132d9a10fSbeck int SCT_is_complete(const SCT *sct); 222137efc4eSbeck 223137efc4eSbeck /* 224137efc4eSbeck * Does this SCT have the signature-related fields populated? 225137efc4eSbeck * Returns 1 if so, 0 otherwise. 226137efc4eSbeck * This checks that the signature and hash algorithms are set to supported 227137efc4eSbeck * values and that the signature field is set. 228137efc4eSbeck */ 22932d9a10fSbeck int SCT_signature_is_complete(const SCT *sct); 230137efc4eSbeck 231137efc4eSbeck /* 232137efc4eSbeck * TODO(RJPercival): Create an SCT_signature struct and make i2o_SCT_signature 233137efc4eSbeck * and o2i_SCT_signature conform to the i2d/d2i conventions. 234137efc4eSbeck */ 235137efc4eSbeck 236137efc4eSbeck /* 237137efc4eSbeck * Serialize (to TLS format) an |sct| signature and write it to |out|. 238137efc4eSbeck * If |out| is null, no signature will be output but the length will be returned. 239137efc4eSbeck * If |out| points to a null pointer, a string will be allocated to hold the 240137efc4eSbeck * TLS-format signature. It is the responsibility of the caller to free it. 241137efc4eSbeck * If |out| points to an allocated string, the signature will be written to it. 242137efc4eSbeck * The length of the signature in TLS format will be returned. 243137efc4eSbeck */ 24432d9a10fSbeck int i2o_SCT_signature(const SCT *sct, unsigned char **out); 245137efc4eSbeck 246137efc4eSbeck /* 247137efc4eSbeck * Parses an SCT signature in TLS format and populates the |sct| with it. 248137efc4eSbeck * |in| should be a pointer to a string containing the TLS-format signature. 249137efc4eSbeck * |in| will be advanced to the end of the signature if parsing succeeds. 250137efc4eSbeck * |len| should be the length of the signature in |in|. 251137efc4eSbeck * Returns the number of bytes parsed, or a negative integer if an error occurs. 252137efc4eSbeck * If an error occurs, the SCT's signature NID may be updated whilst the 253137efc4eSbeck * signature field itself remains unset. 254137efc4eSbeck */ 255*f6e7d166Sjsing int o2i_SCT_signature(SCT *sct, CBS *cbs); 256137efc4eSbeck 257137efc4eSbeck /* 258137efc4eSbeck * Handlers for Certificate Transparency X509v3/OCSP extensions 259137efc4eSbeck */ 260137efc4eSbeck extern const X509V3_EXT_METHOD v3_ct_scts[3]; 261