| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.verifyTLogBody = verifyTLogBody; |
| 4 |
exports.verifyTLogInclusion = verifyTLogInclusion; |
| 5 |
/* |
| 6 |
Copyright 2023 The Sigstore Authors. |
| 7 |
|
| 8 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
you may not use this file except in compliance with the License. |
| 10 |
You may obtain a copy of the License at |
| 11 |
|
| 12 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
|
| 14 |
Unless required by applicable law or agreed to in writing, software |
| 15 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
See the License for the specific language governing permissions and |
| 18 |
limitations under the License. |
| 19 |
*/ |
| 20 |
const v2_1 = require("@sigstore/protobuf-specs/rekor/v2"); |
| 21 |
const error_1 = require("../error"); |
| 22 |
const dsse_1 = require("./dsse"); |
| 23 |
const hashedrekord_1 = require("./hashedrekord"); |
| 24 |
const intoto_1 = require("./intoto"); |
| 25 |
const checkpoint_1 = require("./checkpoint"); |
| 26 |
const merkle_1 = require("./merkle"); |
| 27 |
const set_1 = require("./set"); |
| 28 |
// Verifies that the given tlog entry matches the supplied signature content. |
| 29 |
function verifyTLogBody(entry, sigContent) { |
| 30 |
const { kind, version } = entry.kindVersion; |
| 31 |
const body = JSON.parse(entry.canonicalizedBody.toString('utf8')); |
| 32 |
// validate body |
| 33 |
if (kind !== body.kind || version !== body.apiVersion) { |
| 34 |
throw new error_1.VerificationError({ |
| 35 |
code: 'TLOG_BODY_ERROR', |
| 36 |
message: `kind/version mismatch - expected: ${kind}/${version}, received: ${body.kind}/${body.apiVersion}`, |
| 37 |
}); |
| 38 |
} |
| 39 |
switch (kind) { |
| 40 |
case 'dsse': |
| 41 |
// Rekor V1 and V2 use incompatible types so we need to branch here based on version |
| 42 |
if (version == dsse_1.DSSE_API_VERSION_V1) { |
| 43 |
return (0, dsse_1.verifyDSSETLogBody)(body, sigContent); |
| 44 |
} |
| 45 |
else { |
| 46 |
const entryRekorV2 = v2_1.Entry.fromJSON(body); |
| 47 |
return (0, dsse_1.verifyDSSETLogBodyV2)(entryRekorV2, sigContent); |
| 48 |
} |
| 49 |
case 'intoto': |
| 50 |
return (0, intoto_1.verifyIntotoTLogBody)(body, sigContent); |
| 51 |
case 'hashedrekord': |
| 52 |
// Rekor V1 and V2 use incompatible types so we need to branch here based on version |
| 53 |
if (version == hashedrekord_1.HASHEDREKORD_API_VERSION_V1) { |
| 54 |
return (0, hashedrekord_1.verifyHashedRekordTLogBody)(body, sigContent); |
| 55 |
} |
| 56 |
else { |
| 57 |
const entryRekorV2 = v2_1.Entry.fromJSON(body); |
| 58 |
return (0, hashedrekord_1.verifyHashedRekordTLogBodyV2)(entryRekorV2, sigContent); |
| 59 |
} |
| 60 |
/* istanbul ignore next */ |
| 61 |
default: |
| 62 |
throw new error_1.VerificationError({ |
| 63 |
code: 'TLOG_BODY_ERROR', |
| 64 |
message: `unsupported kind: ${kind}`, |
| 65 |
}); |
| 66 |
} |
| 67 |
} |
| 68 |
function verifyTLogInclusion(entry, tlogAuthorities) { |
| 69 |
let inclusionVerified = false; |
| 70 |
if (isTLogEntryWithInclusionPromise(entry)) { |
| 71 |
(0, set_1.verifyTLogSET)(entry, tlogAuthorities); |
| 72 |
inclusionVerified = true; |
| 73 |
} |
| 74 |
if (isTLogEntryWithInclusionProof(entry)) { |
| 75 |
const checkpoint = (0, checkpoint_1.verifyCheckpoint)(entry, tlogAuthorities); |
| 76 |
(0, merkle_1.verifyMerkleInclusion)(entry, checkpoint); |
| 77 |
inclusionVerified = true; |
| 78 |
} |
| 79 |
if (!inclusionVerified) { |
| 80 |
throw new error_1.VerificationError({ |
| 81 |
code: 'TLOG_MISSING_INCLUSION_ERROR', |
| 82 |
message: 'inclusion could not be verified', |
| 83 |
}); |
| 84 |
} |
| 85 |
return; |
| 86 |
} |
| 87 |
function isTLogEntryWithInclusionPromise(entry) { |
| 88 |
return entry.inclusionPromise !== undefined; |
| 89 |
} |
| 90 |
function isTLogEntryWithInclusionProof(entry) { |
| 91 |
return entry.inclusionProof !== undefined; |
| 92 |
} |