verifier.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Verifier = void 0;
  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 util_1 = require("util");
  17. const error_1 = require("./error");
  18. const key_1 = require("./key");
  19. const policy_1 = require("./policy");
  20. const timestamp_1 = require("./timestamp");
  21. const tlog_1 = require("./tlog");
  22. class Verifier {
  23. constructor(trustMaterial, options = {}) {
  24. this.trustMaterial = trustMaterial;
  25. this.options = {
  26. ctlogThreshold: options.ctlogThreshold ?? 1,
  27. tlogThreshold: options.tlogThreshold ?? 1,
  28. tsaThreshold: options.tsaThreshold ?? 0,
  29. };
  30. }
  31. verify(entity, policy) {
  32. const timestamps = this.verifyTimestamps(entity);
  33. const signer = this.verifySigningKey(entity, timestamps);
  34. this.verifyTLogs(entity);
  35. this.verifySignature(entity, signer);
  36. if (policy) {
  37. this.verifyPolicy(policy, signer.identity || {});
  38. }
  39. return signer;
  40. }
  41. // Checks that all of the timestamps in the entity are valid and returns them
  42. verifyTimestamps(entity) {
  43. let tlogCount = 0;
  44. let tsaCount = 0;
  45. const timestamps = entity.timestamps.map((timestamp) => {
  46. switch (timestamp.$case) {
  47. case 'timestamp-authority':
  48. tsaCount++;
  49. return (0, timestamp_1.verifyTSATimestamp)(timestamp.timestamp, entity.signature.signature, this.trustMaterial.timestampAuthorities);
  50. case 'transparency-log':
  51. tlogCount++;
  52. return (0, timestamp_1.verifyTLogTimestamp)(timestamp.tlogEntry, this.trustMaterial.tlogs);
  53. }
  54. });
  55. // Check for duplicate timestamps
  56. if (containsDupes(timestamps)) {
  57. throw new error_1.VerificationError({
  58. code: 'TIMESTAMP_ERROR',
  59. message: 'duplicate timestamp',
  60. });
  61. }
  62. if (tlogCount < this.options.tlogThreshold) {
  63. throw new error_1.VerificationError({
  64. code: 'TIMESTAMP_ERROR',
  65. message: `expected ${this.options.tlogThreshold} tlog timestamps, got ${tlogCount}`,
  66. });
  67. }
  68. if (tsaCount < this.options.tsaThreshold) {
  69. throw new error_1.VerificationError({
  70. code: 'TIMESTAMP_ERROR',
  71. message: `expected ${this.options.tsaThreshold} tsa timestamps, got ${tsaCount}`,
  72. });
  73. }
  74. return timestamps.map((t) => t.timestamp);
  75. }
  76. // Checks that the signing key is valid for all of the the supplied timestamps
  77. // and returns the signer.
  78. verifySigningKey({ key }, timestamps) {
  79. switch (key.$case) {
  80. case 'public-key': {
  81. return (0, key_1.verifyPublicKey)(key.hint, timestamps, this.trustMaterial);
  82. }
  83. case 'certificate': {
  84. const result = (0, key_1.verifyCertificate)(key.certificate, timestamps, this.trustMaterial);
  85. /* istanbul ignore next - no fixture */
  86. if (containsDupes(result.scts)) {
  87. throw new error_1.VerificationError({
  88. code: 'CERTIFICATE_ERROR',
  89. message: 'duplicate SCT',
  90. });
  91. }
  92. if (result.scts.length < this.options.ctlogThreshold) {
  93. throw new error_1.VerificationError({
  94. code: 'CERTIFICATE_ERROR',
  95. message: `expected ${this.options.ctlogThreshold} SCTs, got ${result.scts.length}`,
  96. });
  97. }
  98. return result.signer;
  99. }
  100. }
  101. }
  102. // Checks that the tlog entries are valid for the supplied content
  103. verifyTLogs({ signature: content, tlogEntries }) {
  104. tlogEntries.forEach((entry) => (0, tlog_1.verifyTLogBody)(entry, content));
  105. }
  106. // Checks that the signature is valid for the supplied content
  107. verifySignature(entity, signer) {
  108. if (!entity.signature.verifySignature(signer.key)) {
  109. throw new error_1.VerificationError({
  110. code: 'SIGNATURE_ERROR',
  111. message: 'signature verification failed',
  112. });
  113. }
  114. }
  115. verifyPolicy(policy, identity) {
  116. // Check the subject alternative name of the signer matches the policy
  117. if (policy.subjectAlternativeName) {
  118. (0, policy_1.verifySubjectAlternativeName)(policy.subjectAlternativeName, identity.subjectAlternativeName);
  119. }
  120. // Check that the extensions of the signer match the policy
  121. if (policy.extensions) {
  122. (0, policy_1.verifyExtensions)(policy.extensions, identity.extensions);
  123. }
  124. }
  125. }
  126. exports.Verifier = Verifier;
  127. // Checks for duplicate items in the array. Objects are compared using
  128. // deep equality.
  129. function containsDupes(arr) {
  130. for (let i = 0; i < arr.length; i++) {
  131. for (let j = i + 1; j < arr.length; j++) {
  132. if ((0, util_1.isDeepStrictEqual)(arr[i], arr[j])) {
  133. return true;
  134. }
  135. }
  136. }
  137. return false;
  138. }