A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.4 KiB

  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. #include <string.h>
  10. #include <openssl/ct.h>
  11. #include <openssl/err.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/x509.h>
  14. #include "ct_locl.h"
  15. typedef enum sct_signature_type_t {
  16. SIGNATURE_TYPE_NOT_SET = -1,
  17. SIGNATURE_TYPE_CERT_TIMESTAMP,
  18. SIGNATURE_TYPE_TREE_HASH
  19. } SCT_SIGNATURE_TYPE;
  20. /*
  21. * Update encoding for SCT signature verification/generation to supplied
  22. * EVP_MD_CTX.
  23. */
  24. static int sct_ctx_update(EVP_MD_CTX *ctx, const SCT_CTX *sctx, const SCT *sct)
  25. {
  26. unsigned char tmpbuf[12];
  27. unsigned char *p, *der;
  28. size_t derlen;
  29. /*+
  30. * digitally-signed struct {
  31. * (1 byte) Version sct_version;
  32. * (1 byte) SignatureType signature_type = certificate_timestamp;
  33. * (8 bytes) uint64 timestamp;
  34. * (2 bytes) LogEntryType entry_type;
  35. * (? bytes) select(entry_type) {
  36. * case x509_entry: ASN.1Cert;
  37. * case precert_entry: PreCert;
  38. * } signed_entry;
  39. * (2 bytes + sct->ext_len) CtExtensions extensions;
  40. * }
  41. */
  42. if (sct->entry_type == CT_LOG_ENTRY_TYPE_NOT_SET)
  43. return 0;
  44. if (sct->entry_type == CT_LOG_ENTRY_TYPE_PRECERT && sctx->ihash == NULL)
  45. return 0;
  46. p = tmpbuf;
  47. *p++ = sct->version;
  48. *p++ = SIGNATURE_TYPE_CERT_TIMESTAMP;
  49. l2n8(sct->timestamp, p);
  50. s2n(sct->entry_type, p);
  51. if (!EVP_DigestUpdate(ctx, tmpbuf, p - tmpbuf))
  52. return 0;
  53. if (sct->entry_type == CT_LOG_ENTRY_TYPE_X509) {
  54. der = sctx->certder;
  55. derlen = sctx->certderlen;
  56. } else {
  57. if (!EVP_DigestUpdate(ctx, sctx->ihash, sctx->ihashlen))
  58. return 0;
  59. der = sctx->preder;
  60. derlen = sctx->prederlen;
  61. }
  62. /* If no encoding available, fatal error */
  63. if (der == NULL)
  64. return 0;
  65. /* Include length first */
  66. p = tmpbuf;
  67. l2n3(derlen, p);
  68. if (!EVP_DigestUpdate(ctx, tmpbuf, 3))
  69. return 0;
  70. if (!EVP_DigestUpdate(ctx, der, derlen))
  71. return 0;
  72. /* Add any extensions */
  73. p = tmpbuf;
  74. s2n(sct->ext_len, p);
  75. if (!EVP_DigestUpdate(ctx, tmpbuf, 2))
  76. return 0;
  77. if (sct->ext_len && !EVP_DigestUpdate(ctx, sct->ext, sct->ext_len))
  78. return 0;
  79. return 1;
  80. }
  81. int SCT_verify(const SCT_CTX *sctx, const SCT *sct)
  82. {
  83. EVP_MD_CTX *ctx = NULL;
  84. int ret = 0;
  85. if (!SCT_is_complete(sct) || sctx->pkey == NULL ||
  86. sct->entry_type == CT_LOG_ENTRY_TYPE_NOT_SET ||
  87. (sct->entry_type == CT_LOG_ENTRY_TYPE_PRECERT && sctx->ihash == NULL)) {
  88. CTerr(CT_F_SCT_VERIFY, CT_R_SCT_NOT_SET);
  89. return 0;
  90. }
  91. if (sct->version != SCT_VERSION_V1) {
  92. CTerr(CT_F_SCT_VERIFY, CT_R_SCT_UNSUPPORTED_VERSION);
  93. return 0;
  94. }
  95. if (sct->log_id_len != sctx->pkeyhashlen ||
  96. memcmp(sct->log_id, sctx->pkeyhash, sctx->pkeyhashlen) != 0) {
  97. CTerr(CT_F_SCT_VERIFY, CT_R_SCT_LOG_ID_MISMATCH);
  98. return 0;
  99. }
  100. ctx = EVP_MD_CTX_new();
  101. if (ctx == NULL)
  102. goto end;
  103. if (!EVP_DigestVerifyInit(ctx, NULL, EVP_sha256(), NULL, sctx->pkey))
  104. goto end;
  105. if (!sct_ctx_update(ctx, sctx, sct))
  106. goto end;
  107. /* Verify signature */
  108. ret = EVP_DigestVerifyFinal(ctx, sct->sig, sct->sig_len);
  109. /* If ret < 0 some other error: fall through without setting error */
  110. if (ret == 0)
  111. CTerr(CT_F_SCT_VERIFY, CT_R_SCT_INVALID_SIGNATURE);
  112. end:
  113. EVP_MD_CTX_free(ctx);
  114. return ret;
  115. }
  116. int SCT_verify_v1(SCT *sct, X509 *cert, X509 *preissuer,
  117. X509_PUBKEY *log_pubkey, X509 *issuer_cert)
  118. {
  119. int ret = 0;
  120. SCT_CTX *sctx = NULL;
  121. if (!SCT_is_complete(sct)) {
  122. CTerr(CT_F_SCT_VERIFY_V1, CT_R_SCT_NOT_SET);
  123. return 0;
  124. }
  125. if (sct->version != 0) {
  126. CTerr(CT_F_SCT_VERIFY_V1, CT_R_SCT_UNSUPPORTED_VERSION);
  127. return 0;
  128. }
  129. sctx = SCT_CTX_new();
  130. if (sctx == NULL)
  131. goto done;
  132. if (!SCT_CTX_set1_pubkey(sctx, log_pubkey))
  133. goto done;
  134. if (!SCT_CTX_set1_cert(sctx, cert, preissuer))
  135. goto done;
  136. if (sct->entry_type == CT_LOG_ENTRY_TYPE_PRECERT &&
  137. !SCT_CTX_set1_issuer(sctx, issuer_cert))
  138. goto done;
  139. ret = SCT_verify(sctx, sct);
  140. done:
  141. SCT_CTX_free(sctx);
  142. return ret;
  143. }