sct.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.verifySCTs = verifySCTs;
  4. /*
  5. Copyright 2023 The Sigstore Authors.
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. const core_1 = require("@sigstore/core");
  17. const error_1 = require("../error");
  18. const trust_1 = require("../trust");
  19. function verifySCTs(cert, issuer, ctlogs) {
  20. let extSCT;
  21. // Verifying the SCT requires that we remove the SCT extension and
  22. // re-encode the TBS structure to DER -- this value is part of the data
  23. // over which the signature is calculated. Since this is a destructive action
  24. // we create a copy of the certificate so we can remove the SCT extension
  25. // without affecting the original certificate.
  26. const clone = cert.clone();
  27. // Intentionally not using the findExtension method here because we want to
  28. // remove the the SCT extension from the certificate before calculating the
  29. // PreCert structure
  30. for (let i = 0; i < clone.extensions.length; i++) {
  31. const ext = clone.extensions[i];
  32. if (ext.subs[0].toOID() === core_1.EXTENSION_OID_SCT) {
  33. extSCT = new core_1.X509SCTExtension(ext);
  34. // Remove the extension from the certificate
  35. clone.extensions.splice(i, 1);
  36. break;
  37. }
  38. }
  39. // No SCT extension found to verify
  40. if (!extSCT) {
  41. return [];
  42. }
  43. // Found an SCT extension but it has no SCTs
  44. /* istanbul ignore if -- too difficult to fabricate test case for this */
  45. if (extSCT.signedCertificateTimestamps.length === 0) {
  46. return [];
  47. }
  48. // Construct the PreCert structure
  49. // https://www.rfc-editor.org/rfc/rfc6962#section-3.2
  50. const preCert = new core_1.ByteStream();
  51. // Calculate hash of the issuer's public key
  52. const issuerId = core_1.crypto.digest('sha256', issuer.publicKey);
  53. preCert.appendView(issuerId);
  54. // Re-encodes the certificate to DER after removing the SCT extension
  55. const tbs = clone.tbsCertificate.toDER();
  56. preCert.appendUint24(tbs.length);
  57. preCert.appendView(tbs);
  58. // Calculate and return the verification results for each SCT
  59. return extSCT.signedCertificateTimestamps.map((sct) => {
  60. // Find the ctlog instance that corresponds to the SCT's logID
  61. const validCTLogs = (0, trust_1.filterTLogAuthorities)(ctlogs, {
  62. logID: sct.logID,
  63. targetDate: sct.datetime,
  64. });
  65. // See if the SCT is valid for any of the CT logs
  66. const verified = validCTLogs.some((log) => sct.verify(preCert.buffer, log.publicKey));
  67. if (!verified) {
  68. throw new error_1.VerificationError({
  69. code: 'CERTIFICATE_ERROR',
  70. message: 'SCT verification failed',
  71. });
  72. }
  73. return sct.logID;
  74. });
  75. }