ct_vfy.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2016 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. #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_CTX_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_CTX_VERIFY, CT_R_SCT_NOT_SET);
  89. return 0;
  90. }
  91. if (sct->version != SCT_VERSION_V1) {
  92. CTerr(CT_F_SCT_CTX_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_CTX_VERIFY, CT_R_SCT_LOG_ID_MISMATCH);
  98. return 0;
  99. }
  100. if (sct->timestamp > sctx->epoch_time_in_ms) {
  101. CTerr(CT_F_SCT_CTX_VERIFY, CT_R_SCT_FUTURE_TIMESTAMP);
  102. return 0;
  103. }
  104. ctx = EVP_MD_CTX_new();
  105. if (ctx == NULL)
  106. goto end;
  107. if (!EVP_DigestVerifyInit(ctx, NULL, EVP_sha256(), NULL, sctx->pkey))
  108. goto end;
  109. if (!sct_ctx_update(ctx, sctx, sct))
  110. goto end;
  111. /* Verify signature */
  112. ret = EVP_DigestVerifyFinal(ctx, sct->sig, sct->sig_len);
  113. /* If ret < 0 some other error: fall through without setting error */
  114. if (ret == 0)
  115. CTerr(CT_F_SCT_CTX_VERIFY, CT_R_SCT_INVALID_SIGNATURE);
  116. end:
  117. EVP_MD_CTX_free(ctx);
  118. return ret;
  119. }