| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.HASHEDREKORD_API_VERSION_V1 = void 0; |
| 4 |
exports.verifyHashedRekordTLogBody = verifyHashedRekordTLogBody; |
| 5 |
exports.verifyHashedRekordTLogBodyV2 = verifyHashedRekordTLogBodyV2; |
| 6 |
/* |
| 7 |
Copyright 2025 The Sigstore Authors. |
| 8 |
|
| 9 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 |
you may not use this file except in compliance with the License. |
| 11 |
You may obtain a copy of the License at |
| 12 |
|
| 13 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
|
| 15 |
Unless required by applicable law or agreed to in writing, software |
| 16 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
See the License for the specific language governing permissions and |
| 19 |
limitations under the License. |
| 20 |
*/ |
| 21 |
const error_1 = require("../error"); |
| 22 |
exports.HASHEDREKORD_API_VERSION_V1 = '0.0.1'; |
| 23 |
// Compare the given hashedrekord tlog entry to the given bundle |
| 24 |
function verifyHashedRekordTLogBody(tlogEntry, content) { |
| 25 |
switch (tlogEntry.apiVersion) { |
| 26 |
case exports.HASHEDREKORD_API_VERSION_V1: |
| 27 |
return verifyHashedrekord001TLogBody(tlogEntry, content); |
| 28 |
default: |
| 29 |
throw new error_1.VerificationError({ |
| 30 |
code: 'TLOG_BODY_ERROR', |
| 31 |
message: `unsupported hashedrekord version: ${tlogEntry.apiVersion}`, |
| 32 |
}); |
| 33 |
} |
| 34 |
} |
| 35 |
// Compare the given hashedrekor tlog entry to the given bundle. This function is |
| 36 |
// specifically for Rekor V2 entries. |
| 37 |
function verifyHashedRekordTLogBodyV2(tlogEntry, content) { |
| 38 |
const spec = tlogEntry.spec?.spec; |
| 39 |
if (!spec) { |
| 40 |
throw new error_1.VerificationError({ |
| 41 |
code: 'TLOG_BODY_ERROR', |
| 42 |
message: `missing dsse spec`, |
| 43 |
}); |
| 44 |
} |
| 45 |
switch (spec.$case) { |
| 46 |
case 'hashedRekordV002': |
| 47 |
return verifyHashedrekord002TLogBody(spec.hashedRekordV002, content); |
| 48 |
default: |
| 49 |
throw new error_1.VerificationError({ |
| 50 |
code: 'TLOG_BODY_ERROR', |
| 51 |
message: `unsupported version: ${spec.$case}`, |
| 52 |
}); |
| 53 |
} |
| 54 |
} |
| 55 |
// Compare the given hashedrekord v0.0.1 tlog entry to the given message |
| 56 |
// signature |
| 57 |
function verifyHashedrekord001TLogBody(tlogEntry, content) { |
| 58 |
// Ensure that the bundles message signature matches the tlog entry |
| 59 |
const tlogSig = tlogEntry.spec.signature.content || ''; |
| 60 |
if (!content.compareSignature(Buffer.from(tlogSig, 'base64'))) { |
| 61 |
throw new error_1.VerificationError({ |
| 62 |
code: 'TLOG_BODY_ERROR', |
| 63 |
message: 'signature mismatch', |
| 64 |
}); |
| 65 |
} |
| 66 |
// Ensure that the bundle's message digest matches the tlog entry |
| 67 |
const tlogDigest = tlogEntry.spec.data.hash?.value || ''; |
| 68 |
if (!content.compareDigest(Buffer.from(tlogDigest, 'hex'))) { |
| 69 |
throw new error_1.VerificationError({ |
| 70 |
code: 'TLOG_BODY_ERROR', |
| 71 |
message: 'digest mismatch', |
| 72 |
}); |
| 73 |
} |
| 74 |
} |
| 75 |
// Compare the given hashedrekord v0.0.2 tlog entry to the given message |
| 76 |
// signature |
| 77 |
function verifyHashedrekord002TLogBody(spec, content) { |
| 78 |
// Ensure that the bundles message signature matches the tlog entry |
| 79 |
const tlogSig = spec.signature?.content || Buffer.from(''); |
| 80 |
if (!content.compareSignature(tlogSig)) { |
| 81 |
throw new error_1.VerificationError({ |
| 82 |
code: 'TLOG_BODY_ERROR', |
| 83 |
message: 'signature mismatch', |
| 84 |
}); |
| 85 |
} |
| 86 |
// Ensure that the bundle's message digest matches the tlog entry |
| 87 |
const tlogHash = spec.data?.digest || Buffer.from(''); |
| 88 |
if (!content.compareDigest(tlogHash)) { |
| 89 |
throw new error_1.VerificationError({ |
| 90 |
code: 'TLOG_BODY_ERROR', |
| 91 |
message: 'digest mismatch', |
| 92 |
}); |
| 93 |
} |
| 94 |
} |