ct_sct_ctx.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 is disabled"
  11. #endif
  12. #include <stddef.h>
  13. #include <string.h>
  14. #include <openssl/err.h>
  15. #include <openssl/obj_mac.h>
  16. #include <openssl/x509.h>
  17. #include "ct_local.h"
  18. SCT_CTX *SCT_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
  19. {
  20. SCT_CTX *sctx = OPENSSL_zalloc(sizeof(*sctx));
  21. if (sctx == NULL)
  22. return NULL;
  23. sctx->libctx = libctx;
  24. if (propq != NULL) {
  25. sctx->propq = OPENSSL_strdup(propq);
  26. if (sctx->propq == NULL) {
  27. OPENSSL_free(sctx);
  28. return NULL;
  29. }
  30. }
  31. return sctx;
  32. }
  33. void SCT_CTX_free(SCT_CTX *sctx)
  34. {
  35. if (sctx == NULL)
  36. return;
  37. EVP_PKEY_free(sctx->pkey);
  38. OPENSSL_free(sctx->pkeyhash);
  39. OPENSSL_free(sctx->ihash);
  40. OPENSSL_free(sctx->certder);
  41. OPENSSL_free(sctx->preder);
  42. OPENSSL_free(sctx->propq);
  43. OPENSSL_free(sctx);
  44. }
  45. /*
  46. * Finds the index of the first extension with the given NID in cert.
  47. * If there is more than one extension with that NID, *is_duplicated is set to
  48. * 1, otherwise 0 (unless it is NULL).
  49. */
  50. static int ct_x509_get_ext(X509 *cert, int nid, int *is_duplicated)
  51. {
  52. int ret = X509_get_ext_by_NID(cert, nid, -1);
  53. if (is_duplicated != NULL)
  54. *is_duplicated = ret >= 0 && X509_get_ext_by_NID(cert, nid, ret) >= 0;
  55. return ret;
  56. }
  57. /*
  58. * Modifies a certificate by deleting extensions and copying the issuer and
  59. * AKID from the presigner certificate, if necessary.
  60. * Returns 1 on success, 0 otherwise.
  61. */
  62. __owur static int ct_x509_cert_fixup(X509 *cert, X509 *presigner)
  63. {
  64. int preidx, certidx;
  65. int pre_akid_ext_is_dup, cert_akid_ext_is_dup;
  66. if (presigner == NULL)
  67. return 1;
  68. preidx = ct_x509_get_ext(presigner, NID_authority_key_identifier,
  69. &pre_akid_ext_is_dup);
  70. certidx = ct_x509_get_ext(cert, NID_authority_key_identifier,
  71. &cert_akid_ext_is_dup);
  72. /* An error occurred whilst searching for the extension */
  73. if (preidx < -1 || certidx < -1)
  74. return 0;
  75. /* Invalid certificate if they contain duplicate extensions */
  76. if (pre_akid_ext_is_dup || cert_akid_ext_is_dup)
  77. return 0;
  78. /* AKID must be present in both certificate or absent in both */
  79. if (preidx >= 0 && certidx == -1)
  80. return 0;
  81. if (preidx == -1 && certidx >= 0)
  82. return 0;
  83. /* Copy issuer name */
  84. if (!X509_set_issuer_name(cert, X509_get_issuer_name(presigner)))
  85. return 0;
  86. if (preidx != -1) {
  87. /* Retrieve and copy AKID encoding */
  88. X509_EXTENSION *preext = X509_get_ext(presigner, preidx);
  89. X509_EXTENSION *certext = X509_get_ext(cert, certidx);
  90. ASN1_OCTET_STRING *preextdata;
  91. /* Should never happen */
  92. if (preext == NULL || certext == NULL)
  93. return 0;
  94. preextdata = X509_EXTENSION_get_data(preext);
  95. if (preextdata == NULL ||
  96. !X509_EXTENSION_set_data(certext, preextdata))
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner)
  102. {
  103. unsigned char *certder = NULL, *preder = NULL;
  104. X509 *pretmp = NULL;
  105. int certderlen = 0, prederlen = 0;
  106. int idx = -1;
  107. int poison_ext_is_dup, sct_ext_is_dup;
  108. int poison_idx = ct_x509_get_ext(cert, NID_ct_precert_poison, &poison_ext_is_dup);
  109. /* Duplicate poison extensions are present - error */
  110. if (poison_ext_is_dup)
  111. goto err;
  112. /* If *cert doesn't have a poison extension, it isn't a precert */
  113. if (poison_idx == -1) {
  114. /* cert isn't a precert, so we shouldn't have a presigner */
  115. if (presigner != NULL)
  116. goto err;
  117. certderlen = i2d_X509(cert, &certder);
  118. if (certderlen < 0)
  119. goto err;
  120. }
  121. /* See if cert has a precert SCTs extension */
  122. idx = ct_x509_get_ext(cert, NID_ct_precert_scts, &sct_ext_is_dup);
  123. /* Duplicate SCT extensions are present - error */
  124. if (sct_ext_is_dup)
  125. goto err;
  126. if (idx >= 0 && poison_idx >= 0) {
  127. /*
  128. * cert can't both contain SCTs (i.e. have an SCT extension) and be a
  129. * precert (i.e. have a poison extension).
  130. */
  131. goto err;
  132. }
  133. if (idx == -1) {
  134. idx = poison_idx;
  135. }
  136. /*
  137. * If either a poison or SCT extension is present, remove it before encoding
  138. * cert. This, along with ct_x509_cert_fixup(), gets a TBSCertificate (see
  139. * RFC5280) from cert, which is what the CT log signed when it produced the
  140. * SCT.
  141. */
  142. if (idx >= 0) {
  143. /* Take a copy of certificate so we don't modify passed version */
  144. pretmp = X509_dup(cert);
  145. if (pretmp == NULL)
  146. goto err;
  147. X509_EXTENSION_free(X509_delete_ext(pretmp, idx));
  148. if (!ct_x509_cert_fixup(pretmp, presigner))
  149. goto err;
  150. prederlen = i2d_re_X509_tbs(pretmp, &preder);
  151. if (prederlen <= 0)
  152. goto err;
  153. }
  154. X509_free(pretmp);
  155. OPENSSL_free(sctx->certder);
  156. sctx->certder = certder;
  157. sctx->certderlen = certderlen;
  158. OPENSSL_free(sctx->preder);
  159. sctx->preder = preder;
  160. sctx->prederlen = prederlen;
  161. return 1;
  162. err:
  163. OPENSSL_free(certder);
  164. OPENSSL_free(preder);
  165. X509_free(pretmp);
  166. return 0;
  167. }
  168. __owur static int ct_public_key_hash(SCT_CTX *sctx, X509_PUBKEY *pkey,
  169. unsigned char **hash, size_t *hash_len)
  170. {
  171. int ret = 0;
  172. unsigned char *md = NULL, *der = NULL;
  173. int der_len;
  174. unsigned int md_len;
  175. EVP_MD *sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq);
  176. if (sha256 == NULL)
  177. goto err;
  178. /* Reuse buffer if possible */
  179. if (*hash != NULL && *hash_len >= SHA256_DIGEST_LENGTH) {
  180. md = *hash;
  181. } else {
  182. md = OPENSSL_malloc(SHA256_DIGEST_LENGTH);
  183. if (md == NULL)
  184. goto err;
  185. }
  186. /* Calculate key hash */
  187. der_len = i2d_X509_PUBKEY(pkey, &der);
  188. if (der_len <= 0)
  189. goto err;
  190. if (!EVP_Digest(der, der_len, md, &md_len, sha256, NULL))
  191. goto err;
  192. if (md != *hash) {
  193. OPENSSL_free(*hash);
  194. *hash = md;
  195. *hash_len = SHA256_DIGEST_LENGTH;
  196. }
  197. md = NULL;
  198. ret = 1;
  199. err:
  200. EVP_MD_free(sha256);
  201. OPENSSL_free(md);
  202. OPENSSL_free(der);
  203. return ret;
  204. }
  205. int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer)
  206. {
  207. return SCT_CTX_set1_issuer_pubkey(sctx, X509_get_X509_PUBKEY(issuer));
  208. }
  209. int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
  210. {
  211. return ct_public_key_hash(sctx, pubkey, &sctx->ihash, &sctx->ihashlen);
  212. }
  213. int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
  214. {
  215. EVP_PKEY *pkey = X509_PUBKEY_get(pubkey);
  216. if (pkey == NULL)
  217. return 0;
  218. if (!ct_public_key_hash(sctx, pubkey, &sctx->pkeyhash, &sctx->pkeyhashlen)) {
  219. EVP_PKEY_free(pkey);
  220. return 0;
  221. }
  222. EVP_PKEY_free(sctx->pkey);
  223. sctx->pkey = pkey;
  224. return 1;
  225. }
  226. void SCT_CTX_set_time(SCT_CTX *sctx, uint64_t time_in_ms)
  227. {
  228. sctx->epoch_time_in_ms = time_in_ms;
  229. }