ct.h.in 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * {- join("\n * ", @autowarntext) -}
  3. *
  4. * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. {-
  12. use OpenSSL::stackhash qw(generate_stack_macros);
  13. -}
  14. #ifndef OPENSSL_CT_H
  15. # define OPENSSL_CT_H
  16. # pragma once
  17. # include <openssl/macros.h>
  18. # ifndef OPENSSL_NO_DEPRECATED_3_0
  19. # define HEADER_CT_H
  20. # endif
  21. # include <openssl/opensslconf.h>
  22. # ifndef OPENSSL_NO_CT
  23. # include <openssl/types.h>
  24. # include <openssl/safestack.h>
  25. # include <openssl/x509.h>
  26. # include <openssl/cterr.h>
  27. # ifdef __cplusplus
  28. extern "C" {
  29. # endif
  30. /* Minimum RSA key size, from RFC6962 */
  31. # define SCT_MIN_RSA_BITS 2048
  32. /* All hashes are SHA256 in v1 of Certificate Transparency */
  33. # define CT_V1_HASHLEN SHA256_DIGEST_LENGTH
  34. {-
  35. generate_stack_macros("SCT")
  36. .generate_stack_macros("CTLOG");
  37. -}
  38. typedef enum {
  39. CT_LOG_ENTRY_TYPE_NOT_SET = -1,
  40. CT_LOG_ENTRY_TYPE_X509 = 0,
  41. CT_LOG_ENTRY_TYPE_PRECERT = 1
  42. } ct_log_entry_type_t;
  43. typedef enum {
  44. SCT_VERSION_NOT_SET = -1,
  45. SCT_VERSION_V1 = 0
  46. } sct_version_t;
  47. typedef enum {
  48. SCT_SOURCE_UNKNOWN,
  49. SCT_SOURCE_TLS_EXTENSION,
  50. SCT_SOURCE_X509V3_EXTENSION,
  51. SCT_SOURCE_OCSP_STAPLED_RESPONSE
  52. } sct_source_t;
  53. typedef enum {
  54. SCT_VALIDATION_STATUS_NOT_SET,
  55. SCT_VALIDATION_STATUS_UNKNOWN_LOG,
  56. SCT_VALIDATION_STATUS_VALID,
  57. SCT_VALIDATION_STATUS_INVALID,
  58. SCT_VALIDATION_STATUS_UNVERIFIED,
  59. SCT_VALIDATION_STATUS_UNKNOWN_VERSION
  60. } sct_validation_status_t;
  61. /******************************************
  62. * CT policy evaluation context functions *
  63. ******************************************/
  64. /*
  65. * Creates a new, empty policy evaluation context associated with the given
  66. * library context and property query string.
  67. * The caller is responsible for calling CT_POLICY_EVAL_CTX_free when finished
  68. * with the CT_POLICY_EVAL_CTX.
  69. */
  70. CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new_ex(OSSL_LIB_CTX *libctx,
  71. const char *propq);
  72. /*
  73. * The same as CT_POLICY_EVAL_CTX_new_ex() but the default library
  74. * context and property query string is used.
  75. */
  76. CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void);
  77. /* Deletes a policy evaluation context and anything it owns. */
  78. void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx);
  79. /* Gets the peer certificate that the SCTs are for */
  80. X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx);
  81. /*
  82. * Sets the certificate associated with the received SCTs.
  83. * Increments the reference count of cert.
  84. * Returns 1 on success, 0 otherwise.
  85. */
  86. int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert);
  87. /* Gets the issuer of the aforementioned certificate */
  88. X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx);
  89. /*
  90. * Sets the issuer of the certificate associated with the received SCTs.
  91. * Increments the reference count of issuer.
  92. * Returns 1 on success, 0 otherwise.
  93. */
  94. int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer);
  95. /* Gets the CT logs that are trusted sources of SCTs */
  96. const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx);
  97. /* Sets the log store that is in use. It must outlive the CT_POLICY_EVAL_CTX. */
  98. void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx,
  99. CTLOG_STORE *log_store);
  100. /*
  101. * Gets the time, in milliseconds since the Unix epoch, that will be used as the
  102. * current time when checking whether an SCT was issued in the future.
  103. * Such SCTs will fail validation, as required by RFC6962.
  104. */
  105. uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx);
  106. /*
  107. * Sets the time to evaluate SCTs against, in milliseconds since the Unix epoch.
  108. * If an SCT's timestamp is after this time, it will be interpreted as having
  109. * been issued in the future. RFC6962 states that "TLS clients MUST reject SCTs
  110. * whose timestamp is in the future", so an SCT will not validate in this case.
  111. */
  112. void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms);
  113. /*****************
  114. * SCT functions *
  115. *****************/
  116. /*
  117. * Creates a new, blank SCT.
  118. * The caller is responsible for calling SCT_free when finished with the SCT.
  119. */
  120. SCT *SCT_new(void);
  121. /*
  122. * Creates a new SCT from some base64-encoded strings.
  123. * The caller is responsible for calling SCT_free when finished with the SCT.
  124. */
  125. SCT *SCT_new_from_base64(unsigned char version,
  126. const char *logid_base64,
  127. ct_log_entry_type_t entry_type,
  128. uint64_t timestamp,
  129. const char *extensions_base64,
  130. const char *signature_base64);
  131. /*
  132. * Frees the SCT and the underlying data structures.
  133. */
  134. void SCT_free(SCT *sct);
  135. /*
  136. * Free a stack of SCTs, and the underlying SCTs themselves.
  137. * Intended to be compatible with X509V3_EXT_FREE.
  138. */
  139. void SCT_LIST_free(STACK_OF(SCT) *a);
  140. /*
  141. * Returns the version of the SCT.
  142. */
  143. sct_version_t SCT_get_version(const SCT *sct);
  144. /*
  145. * Set the version of an SCT.
  146. * Returns 1 on success, 0 if the version is unrecognized.
  147. */
  148. __owur int SCT_set_version(SCT *sct, sct_version_t version);
  149. /*
  150. * Returns the log entry type of the SCT.
  151. */
  152. ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct);
  153. /*
  154. * Set the log entry type of an SCT.
  155. * Returns 1 on success, 0 otherwise.
  156. */
  157. __owur int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type);
  158. /*
  159. * Gets the ID of the log that an SCT came from.
  160. * Ownership of the log ID remains with the SCT.
  161. * Returns the length of the log ID.
  162. */
  163. size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id);
  164. /*
  165. * Set the log ID of an SCT to point directly to the *log_id specified.
  166. * The SCT takes ownership of the specified pointer.
  167. * Returns 1 on success, 0 otherwise.
  168. */
  169. __owur int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len);
  170. /*
  171. * Set the log ID of an SCT.
  172. * This makes a copy of the log_id.
  173. * Returns 1 on success, 0 otherwise.
  174. */
  175. __owur int SCT_set1_log_id(SCT *sct, const unsigned char *log_id,
  176. size_t log_id_len);
  177. /*
  178. * Returns the timestamp for the SCT (epoch time in milliseconds).
  179. */
  180. uint64_t SCT_get_timestamp(const SCT *sct);
  181. /*
  182. * Set the timestamp of an SCT (epoch time in milliseconds).
  183. */
  184. void SCT_set_timestamp(SCT *sct, uint64_t timestamp);
  185. /*
  186. * Return the NID for the signature used by the SCT.
  187. * For CT v1, this will be either NID_sha256WithRSAEncryption or
  188. * NID_ecdsa_with_SHA256 (or NID_undef if incorrect/unset).
  189. */
  190. int SCT_get_signature_nid(const SCT *sct);
  191. /*
  192. * Set the signature type of an SCT
  193. * For CT v1, this should be either NID_sha256WithRSAEncryption or
  194. * NID_ecdsa_with_SHA256.
  195. * Returns 1 on success, 0 otherwise.
  196. */
  197. __owur int SCT_set_signature_nid(SCT *sct, int nid);
  198. /*
  199. * Set *ext to point to the extension data for the SCT. ext must not be NULL.
  200. * The SCT retains ownership of this pointer.
  201. * Returns length of the data pointed to.
  202. */
  203. size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext);
  204. /*
  205. * Set the extensions of an SCT to point directly to the *ext specified.
  206. * The SCT takes ownership of the specified pointer.
  207. */
  208. void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len);
  209. /*
  210. * Set the extensions of an SCT.
  211. * This takes a copy of the ext.
  212. * Returns 1 on success, 0 otherwise.
  213. */
  214. __owur int SCT_set1_extensions(SCT *sct, const unsigned char *ext,
  215. size_t ext_len);
  216. /*
  217. * Set *sig to point to the signature for the SCT. sig must not be NULL.
  218. * The SCT retains ownership of this pointer.
  219. * Returns length of the data pointed to.
  220. */
  221. size_t SCT_get0_signature(const SCT *sct, unsigned char **sig);
  222. /*
  223. * Set the signature of an SCT to point directly to the *sig specified.
  224. * The SCT takes ownership of the specified pointer.
  225. */
  226. void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len);
  227. /*
  228. * Set the signature of an SCT to be a copy of the *sig specified.
  229. * Returns 1 on success, 0 otherwise.
  230. */
  231. __owur int SCT_set1_signature(SCT *sct, const unsigned char *sig,
  232. size_t sig_len);
  233. /*
  234. * The origin of this SCT, e.g. TLS extension, OCSP response, etc.
  235. */
  236. sct_source_t SCT_get_source(const SCT *sct);
  237. /*
  238. * Set the origin of this SCT, e.g. TLS extension, OCSP response, etc.
  239. * Returns 1 on success, 0 otherwise.
  240. */
  241. __owur int SCT_set_source(SCT *sct, sct_source_t source);
  242. /*
  243. * Returns a text string describing the validation status of |sct|.
  244. */
  245. const char *SCT_validation_status_string(const SCT *sct);
  246. /*
  247. * Pretty-prints an |sct| to |out|.
  248. * It will be indented by the number of spaces specified by |indent|.
  249. * If |logs| is not NULL, it will be used to lookup the CT log that the SCT came
  250. * from, so that the log name can be printed.
  251. */
  252. void SCT_print(const SCT *sct, BIO *out, int indent, const CTLOG_STORE *logs);
  253. /*
  254. * Pretty-prints an |sct_list| to |out|.
  255. * It will be indented by the number of spaces specified by |indent|.
  256. * SCTs will be delimited by |separator|.
  257. * If |logs| is not NULL, it will be used to lookup the CT log that each SCT
  258. * came from, so that the log names can be printed.
  259. */
  260. void SCT_LIST_print(const STACK_OF(SCT) *sct_list, BIO *out, int indent,
  261. const char *separator, const CTLOG_STORE *logs);
  262. /*
  263. * Gets the last result of validating this SCT.
  264. * If it has not been validated yet, returns SCT_VALIDATION_STATUS_NOT_SET.
  265. */
  266. sct_validation_status_t SCT_get_validation_status(const SCT *sct);
  267. /*
  268. * Validates the given SCT with the provided context.
  269. * Sets the "validation_status" field of the SCT.
  270. * Returns 1 if the SCT is valid and the signature verifies.
  271. * Returns 0 if the SCT is invalid or could not be verified.
  272. * Returns -1 if an error occurs.
  273. */
  274. __owur int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx);
  275. /*
  276. * Validates the given list of SCTs with the provided context.
  277. * Sets the "validation_status" field of each SCT.
  278. * Returns 1 if there are no invalid SCTs and all signatures verify.
  279. * Returns 0 if at least one SCT is invalid or could not be verified.
  280. * Returns a negative integer if an error occurs.
  281. */
  282. __owur int SCT_LIST_validate(const STACK_OF(SCT) *scts,
  283. CT_POLICY_EVAL_CTX *ctx);
  284. /*********************************
  285. * SCT parsing and serialization *
  286. *********************************/
  287. /*
  288. * Serialize (to TLS format) a stack of SCTs and return the length.
  289. * "a" must not be NULL.
  290. * If "pp" is NULL, just return the length of what would have been serialized.
  291. * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer
  292. * for data that caller is responsible for freeing (only if function returns
  293. * successfully).
  294. * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring
  295. * that "*pp" is large enough to accept all of the serialized data.
  296. * Returns < 0 on error, >= 0 indicating bytes written (or would have been)
  297. * on success.
  298. */
  299. __owur int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp);
  300. /*
  301. * Convert TLS format SCT list to a stack of SCTs.
  302. * If "a" or "*a" is NULL, a new stack will be created that the caller is
  303. * responsible for freeing (by calling SCT_LIST_free).
  304. * "**pp" and "*pp" must not be NULL.
  305. * Upon success, "*pp" will point to after the last bytes read, and a stack
  306. * will be returned.
  307. * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
  308. * not defined.
  309. */
  310. STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
  311. size_t len);
  312. /*
  313. * Serialize (to DER format) a stack of SCTs and return the length.
  314. * "a" must not be NULL.
  315. * If "pp" is NULL, just returns the length of what would have been serialized.
  316. * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer
  317. * for data that caller is responsible for freeing (only if function returns
  318. * successfully).
  319. * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring
  320. * that "*pp" is large enough to accept all of the serialized data.
  321. * Returns < 0 on error, >= 0 indicating bytes written (or would have been)
  322. * on success.
  323. */
  324. __owur int i2d_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp);
  325. /*
  326. * Parses an SCT list in DER format and returns it.
  327. * If "a" or "*a" is NULL, a new stack will be created that the caller is
  328. * responsible for freeing (by calling SCT_LIST_free).
  329. * "**pp" and "*pp" must not be NULL.
  330. * Upon success, "*pp" will point to after the last bytes read, and a stack
  331. * will be returned.
  332. * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
  333. * not defined.
  334. */
  335. STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
  336. long len);
  337. /*
  338. * Serialize (to TLS format) an |sct| and write it to |out|.
  339. * If |out| is null, no SCT will be output but the length will still be returned.
  340. * If |out| points to a null pointer, a string will be allocated to hold the
  341. * TLS-format SCT. It is the responsibility of the caller to free it.
  342. * If |out| points to an allocated string, the TLS-format SCT will be written
  343. * to it.
  344. * The length of the SCT in TLS format will be returned.
  345. */
  346. __owur int i2o_SCT(const SCT *sct, unsigned char **out);
  347. /*
  348. * Parses an SCT in TLS format and returns it.
  349. * If |psct| is not null, it will end up pointing to the parsed SCT. If it
  350. * already points to a non-null pointer, the pointer will be free'd.
  351. * |in| should be a pointer to a string containing the TLS-format SCT.
  352. * |in| will be advanced to the end of the SCT if parsing succeeds.
  353. * |len| should be the length of the SCT in |in|.
  354. * Returns NULL if an error occurs.
  355. * If the SCT is an unsupported version, only the SCT's 'sct' and 'sct_len'
  356. * fields will be populated (with |in| and |len| respectively).
  357. */
  358. SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len);
  359. /********************
  360. * CT log functions *
  361. ********************/
  362. /*
  363. * Creates a new CT log instance with the given |public_key| and |name| and
  364. * associates it with the give library context |libctx| and property query
  365. * string |propq|.
  366. * Takes ownership of |public_key| but copies |name|.
  367. * Returns NULL if malloc fails or if |public_key| cannot be converted to DER.
  368. * Should be deleted by the caller using CTLOG_free when no longer needed.
  369. */
  370. CTLOG *CTLOG_new_ex(EVP_PKEY *public_key, const char *name, OSSL_LIB_CTX *libctx,
  371. const char *propq);
  372. /*
  373. * The same as CTLOG_new_ex except that the default library context and
  374. * property query string are used.
  375. */
  376. CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name);
  377. /*
  378. * Creates a new CTLOG instance with the base64-encoded SubjectPublicKeyInfo DER
  379. * in |pkey_base64| and associated with the given library context |libctx| and
  380. * property query string |propq|. The |name| is a string to help users identify
  381. * this log.
  382. * Returns 1 on success, 0 on failure.
  383. * Should be deleted by the caller using CTLOG_free when no longer needed.
  384. */
  385. int CTLOG_new_from_base64_ex(CTLOG **ct_log, const char *pkey_base64,
  386. const char *name, OSSL_LIB_CTX *libctx,
  387. const char *propq);
  388. /*
  389. * The same as CTLOG_new_from_base64_ex() except that the default
  390. * library context and property query string are used.
  391. * Returns 1 on success, 0 on failure.
  392. */
  393. int CTLOG_new_from_base64(CTLOG ** ct_log,
  394. const char *pkey_base64, const char *name);
  395. /*
  396. * Deletes a CT log instance and its fields.
  397. */
  398. void CTLOG_free(CTLOG *log);
  399. /* Gets the name of the CT log */
  400. const char *CTLOG_get0_name(const CTLOG *log);
  401. /* Gets the ID of the CT log */
  402. void CTLOG_get0_log_id(const CTLOG *log, const uint8_t **log_id,
  403. size_t *log_id_len);
  404. /* Gets the public key of the CT log */
  405. EVP_PKEY *CTLOG_get0_public_key(const CTLOG *log);
  406. /**************************
  407. * CT log store functions *
  408. **************************/
  409. /*
  410. * Creates a new CT log store and associates it with the given libctx and
  411. * property query string.
  412. * Should be deleted by the caller using CTLOG_STORE_free when no longer needed.
  413. */
  414. CTLOG_STORE *CTLOG_STORE_new_ex(OSSL_LIB_CTX *libctx, const char *propq);
  415. /*
  416. * Same as CTLOG_STORE_new_ex except that the default libctx and
  417. * property query string are used.
  418. * Should be deleted by the caller using CTLOG_STORE_free when no longer needed.
  419. */
  420. CTLOG_STORE *CTLOG_STORE_new(void);
  421. /*
  422. * Deletes a CT log store and all of the CT log instances held within.
  423. */
  424. void CTLOG_STORE_free(CTLOG_STORE *store);
  425. /*
  426. * Finds a CT log in the store based on its log ID.
  427. * Returns the CT log, or NULL if no match is found.
  428. */
  429. const CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store,
  430. const uint8_t *log_id,
  431. size_t log_id_len);
  432. /*
  433. * Loads a CT log list into a |store| from a |file|.
  434. * Returns 1 if loading is successful, or 0 otherwise.
  435. */
  436. __owur int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file);
  437. /*
  438. * Loads the default CT log list into a |store|.
  439. * Returns 1 if loading is successful, or 0 otherwise.
  440. */
  441. __owur int CTLOG_STORE_load_default_file(CTLOG_STORE *store);
  442. # ifdef __cplusplus
  443. }
  444. # endif
  445. # endif
  446. #endif