codekingpro/portable-devtools
114k
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.verifyIntotoTLogBody = verifyIntotoTLogBody;
4/*
5Copyright 2023 The Sigstore Authors.
6
7Licensed under the Apache License, Version 2.0 (the "License");
8you may not use this file except in compliance with the License.
9You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13Unless required by applicable law or agreed to in writing, software
14distributed under the License is distributed on an "AS IS" BASIS,
15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16See the License for the specific language governing permissions and
17limitations under the License.
18*/
19const error_1 = require("../error");
20// Compare the given intoto tlog entry to the given bundle
21function verifyIntotoTLogBody(tlogEntry, content) {
22 switch (tlogEntry.apiVersion) {
23 case '0.0.2':
24 return verifyIntoto002TLogBody(tlogEntry, content);
25 default:
26 throw new error_1.VerificationError({
27 code: 'TLOG_BODY_ERROR',
28 message: `unsupported intoto version: ${tlogEntry.apiVersion}`,
29 });
30 }
31}
32// Compare the given intoto v0.0.2 tlog entry to the given DSSE envelope.
33function verifyIntoto002TLogBody(tlogEntry, content) {
34 // Ensure the bundle's DSSE contains a single signature
35 if (tlogEntry.spec.content.envelope.signatures?.length !== 1) {
36 throw new error_1.VerificationError({
37 code: 'TLOG_BODY_ERROR',
38 message: 'signature count mismatch',
39 });
40 }
41 // Signature is double-base64-encoded in the tlog entry
42 const tlogSig = base64Decode(tlogEntry.spec.content.envelope.signatures[0].sig);
43 // Ensure that the signature in the bundle's DSSE matches tlog entry
44 if (!content.compareSignature(Buffer.from(tlogSig, 'base64'))) {
45 throw new error_1.VerificationError({
46 code: 'TLOG_BODY_ERROR',
47 message: 'tlog entry signature mismatch',
48 });
49 }
50 // Ensure the digest of the bundle's DSSE payload matches the digest in the
51 // tlog entry
52 const tlogHash = tlogEntry.spec.content.payloadHash?.value || '';
53 if (!content.compareDigest(Buffer.from(tlogHash, 'hex'))) {
54 throw new error_1.VerificationError({
55 code: 'TLOG_BODY_ERROR',
56 message: 'DSSE payload hash mismatch',
57 });
58 }
59}
60function base64Decode(str) {
61 return Buffer.from(str, 'base64').toString('utf-8');
62}
63 