2
0

ct.h 16 KB

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