Debian Codegraph @

node_modules/@sigstore/verify/dist/verifier.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Verifier = void 0;
4 /*
5 Copyright 2023 The Sigstore Authors.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 */
19 const util_1 = require("util");
20 const error_1 = require("./error");
21 const key_1 = require("./key");
22 const policy_1 = require("./policy");
23 const timestamp_1 = require("./timestamp");
24 const tlog_1 = require("./tlog");
25 class Verifier {
26 trustMaterial;
27 options;
28 constructor(trustMaterial, options = {}) {
29 this.trustMaterial = trustMaterial;
30 this.options = {
31 ctlogThreshold: options.ctlogThreshold ?? 1,
32 tlogThreshold: options.tlogThreshold ?? 1,
33 timestampThreshold: options.timestampThreshold ?? options.tsaThreshold ?? 1,
34 tsaThreshold: 0,
35 };
36 }
37 verify(entity, policy) {
38 const timestamps = this.verifyTimestamps(entity);
39 const signer = this.verifySigningKey(entity, timestamps);
40 this.verifyTLogs(entity);
41 this.verifySignature(entity, signer);
42 if (policy) {
43 this.verifyPolicy(policy, signer.identity || {});
44 }
45 return signer;
46 }
47 // Checks that all of the timestamps in the entity are valid and returns them
48 verifyTimestamps(entity) {
49 const timestamps = [];
50 for (const timestamp of entity.timestamps) {
51 switch (timestamp.$case) {
52 case 'timestamp-authority':
53 timestamps.push((0, timestamp_1.getTSATimestamp)(timestamp.timestamp, entity.signature.signature, this.trustMaterial.timestampAuthorities));
54 break;
55 case 'transparency-log': {
56 const result = (0, timestamp_1.getTLogTimestamp)(timestamp.tlogEntry);
57 /* istanbul ignore else */
58 if (result) {
59 timestamps.push(result);
60 }
61 break;
62 }
63 }
64 }
65 // Check for duplicate timestamps
66 if (containsDupes(timestamps)) {
67 throw new error_1.VerificationError({
68 code: 'TIMESTAMP_ERROR',
69 message: 'duplicate timestamp',
70 });
71 }
72 if (timestamps.length < this.options.timestampThreshold) {
73 throw new error_1.VerificationError({
74 code: 'TIMESTAMP_ERROR',
75 message: `expected ${this.options.timestampThreshold} timestamps, got ${timestamps.length}`,
76 });
77 }
78 return timestamps.map((t) => t.timestamp);
79 }
80 // Checks that the signing key is valid for all of the the supplied timestamps
81 // and returns the signer.
82 verifySigningKey({ key }, timestamps) {
83 switch (key.$case) {
84 case 'public-key': {
85 return (0, key_1.verifyPublicKey)(key.hint, timestamps, this.trustMaterial);
86 }
87 case 'certificate': {
88 const result = (0, key_1.verifyCertificate)(key.certificate, timestamps, this.trustMaterial);
89 /* istanbul ignore next - no fixture */
90 if (containsDupes(result.scts)) {
91 throw new error_1.VerificationError({
92 code: 'CERTIFICATE_ERROR',
93 message: 'duplicate SCT',
94 });
95 }
96 if (result.scts.length < this.options.ctlogThreshold) {
97 throw new error_1.VerificationError({
98 code: 'CERTIFICATE_ERROR',
99 message: `expected ${this.options.ctlogThreshold} SCTs, got ${result.scts.length}`,
100 });
101 }
102 return result.signer;
103 }
104 }
105 }
106 // Checks that the tlog entries are valid for the supplied content
107 verifyTLogs({ signature: content, tlogEntries }) {
108 let tlogCount = 0;
109 tlogEntries.forEach((entry) => {
110 tlogCount++;
111 (0, tlog_1.verifyTLogInclusion)(entry, this.trustMaterial.tlogs);
112 (0, tlog_1.verifyTLogBody)(entry, content);
113 });
114 if (tlogCount < this.options.tlogThreshold) {
115 throw new error_1.VerificationError({
116 code: 'TLOG_ERROR',
117 message: `expected ${this.options.tlogThreshold} tlog entries, got ${tlogCount}`,
118 });
119 }
120 }
121 // Checks that the signature is valid for the supplied content
122 verifySignature(entity, signer) {
123 if (!entity.signature.verifySignature(signer.key)) {
124 throw new error_1.VerificationError({
125 code: 'SIGNATURE_ERROR',
126 message: 'signature verification failed',
127 });
128 }
129 }
130 verifyPolicy(policy, identity) {
131 // Check the subject alternative name of the signer matches the policy
132 /* istanbul ignore else */
133 if (policy.subjectAlternativeName) {
134 (0, policy_1.verifySubjectAlternativeName)(policy.subjectAlternativeName, identity.subjectAlternativeName);
135 }
136 // Check that the extensions of the signer match the policy
137 /* istanbul ignore else */
138 if (policy.extensions) {
139 (0, policy_1.verifyExtensions)(policy.extensions, identity.extensions);
140 }
141 // Check that the OIDs of the signer match the policy
142 /* istanbul ignore if */
143 if (policy.oids) {
144 (0, policy_1.verifyOIDs)(policy.oids, identity.oids);
145 }
146 }
147 }
148 exports.Verifier = Verifier;
149 // Checks for duplicate items in the array. Objects are compared using
150 // deep equality.
151 function containsDupes(arr) {
152 for (let i = 0; i < arr.length; i++) {
153 for (let j = i + 1; j < arr.length; j++) {
154 if ((0, util_1.isDeepStrictEqual)(arr[i], arr[j])) {
155 return true;
156 }
157 }
158 }
159 return false;
160 }