| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.verifyIntotoTLogBody = verifyIntotoTLogBody; |
| 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 error_1 = require("../error"); |
| 20 |
// Compare the given intoto tlog entry to the given bundle |
| 21 |
function 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. |
| 33 |
function 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 |
} |
| 60 |
function base64Decode(str) { |
| 61 |
return Buffer.from(str, 'base64').toString('utf-8'); |
| 62 |
} |