ct_sct.c 11 KB

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