Debian Codegraph @

node_modules/@sigstore/sign/dist/witness/tlog/entry.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.toProposedEntry = toProposedEntry;
4 exports.toCreateEntryRequest = toCreateEntryRequest;
5 /*
6 Copyright 2025 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 bundle_1 = require("@sigstore/bundle");
21 const protobuf_specs_1 = require("@sigstore/protobuf-specs");
22 const util_1 = require("../../util");
23 const SHA256_ALGORITHM = 'sha256';
24 function toProposedEntry(content, publicKey,
25 // TODO: Remove this parameter once have completely switched to 'dsse' entries
26 entryType = 'dsse') {
27 switch (content.$case) {
28 case 'dsseEnvelope':
29 // TODO: Remove this conditional once have completely ditched "intoto" entries
30 if (entryType === 'intoto') {
31 return toProposedIntotoEntry(content.dsseEnvelope, publicKey);
32 }
33 return toProposedDSSEEntry(content.dsseEnvelope, publicKey);
34 case 'messageSignature':
35 return toProposedHashedRekordEntry(content.messageSignature, publicKey);
36 }
37 }
38 // Returns a properly formatted Rekor "hashedrekord" entry for the given digest
39 // and signature
40 function toProposedHashedRekordEntry(messageSignature, publicKey) {
41 const hexDigest = messageSignature.messageDigest.digest.toString('hex');
42 const b64Signature = messageSignature.signature.toString('base64');
43 const b64Key = util_1.encoding.base64Encode(publicKey);
44 return {
45 apiVersion: '0.0.1',
46 kind: 'hashedrekord',
47 spec: {
48 data: {
49 hash: {
50 algorithm: SHA256_ALGORITHM,
51 value: hexDigest,
52 },
53 },
54 signature: {
55 content: b64Signature,
56 publicKey: {
57 content: b64Key,
58 },
59 },
60 },
61 };
62 }
63 // Returns a properly formatted Rekor "dsse" entry for the given DSSE envelope
64 // and signature
65 function toProposedDSSEEntry(envelope, publicKey) {
66 const envelopeJSON = JSON.stringify((0, bundle_1.envelopeToJSON)(envelope));
67 const encodedKey = util_1.encoding.base64Encode(publicKey);
68 return {
69 apiVersion: '0.0.1',
70 kind: 'dsse',
71 spec: {
72 proposedContent: {
73 envelope: envelopeJSON,
74 verifiers: [encodedKey],
75 },
76 },
77 };
78 }
79 // Returns a properly formatted Rekor "intoto" entry for the given DSSE
80 // envelope and signature
81 function toProposedIntotoEntry(envelope, publicKey) {
82 // Calculate the value for the payloadHash field in the Rekor entry
83 const payloadHash = util_1.crypto
84 .digest(SHA256_ALGORITHM, envelope.payload)
85 .toString('hex');
86 // Calculate the value for the hash field in the Rekor entry
87 const envelopeHash = calculateDSSEHash(envelope, publicKey);
88 // Collect values for re-creating the DSSE envelope.
89 // Double-encode payload and signature cause that's what Rekor expects
90 const payload = util_1.encoding.base64Encode(envelope.payload.toString('base64'));
91 const sig = util_1.encoding.base64Encode(envelope.signatures[0].sig.toString('base64'));
92 const keyid = envelope.signatures[0].keyid;
93 const encodedKey = util_1.encoding.base64Encode(publicKey);
94 // Create the envelope portion of the entry. Note the inclusion of the
95 // publicKey in the signature struct is not a standard part of a DSSE
96 // envelope, but is required by Rekor.
97 const dsse = {
98 payloadType: envelope.payloadType,
99 payload: payload,
100 signatures: [{ sig, publicKey: encodedKey }],
101 };
102 // If the keyid is an empty string, Rekor seems to remove it altogether. We
103 // need to do the same here so that we can properly recreate the entry for
104 // verification.
105 if (keyid.length > 0) {
106 dsse.signatures[0].keyid = keyid;
107 }
108 return {
109 apiVersion: '0.0.2',
110 kind: 'intoto',
111 spec: {
112 content: {
113 envelope: dsse,
114 hash: { algorithm: SHA256_ALGORITHM, value: envelopeHash },
115 payloadHash: { algorithm: SHA256_ALGORITHM, value: payloadHash },
116 },
117 },
118 };
119 }
120 // Calculates the hash of a DSSE envelope for inclusion in a Rekor entry.
121 // There is no standard way to do this, so the scheme we're using as as
122 // follows:
123 // * payload is base64 encoded
124 // * signature is base64 encoded (only the first signature is used)
125 // * keyid is included ONLY if it is NOT an empty string
126 // * The resulting JSON is canonicalized and hashed to a hex string
127 function calculateDSSEHash(envelope, publicKey) {
128 const dsse = {
129 payloadType: envelope.payloadType,
130 payload: envelope.payload.toString('base64'),
131 signatures: [
132 { sig: envelope.signatures[0].sig.toString('base64'), publicKey },
133 ],
134 };
135 // If the keyid is an empty string, Rekor seems to remove it altogether.
136 if (envelope.signatures[0].keyid.length > 0) {
137 dsse.signatures[0].keyid = envelope.signatures[0].keyid;
138 }
139 return util_1.crypto
140 .digest(SHA256_ALGORITHM, util_1.json.canonicalize(dsse))
141 .toString('hex');
142 }
143 function toCreateEntryRequest(content, publicKey) {
144 switch (content.$case) {
145 case 'dsseEnvelope':
146 return toCreateEntryRequestDSSE(content.dsseEnvelope, publicKey);
147 case 'messageSignature':
148 return toCreateEntryRequestMessageSignature(content.messageSignature, publicKey);
149 }
150 }
151 function toCreateEntryRequestDSSE(envelope, publicKey) {
152 return {
153 spec: {
154 $case: 'dsseRequestV002',
155 dsseRequestV002: {
156 envelope: envelope,
157 verifiers: [
158 {
159 // TODO: We need to add support of passing the key details in the
160 // signature bundle. For now we're hardcoding the key details here.
161 keyDetails: protobuf_specs_1.PublicKeyDetails.PKIX_ECDSA_P256_SHA_256,
162 verifier: {
163 $case: 'x509Certificate',
164 x509Certificate: {
165 rawBytes: util_1.pem.toDER(publicKey),
166 },
167 },
168 },
169 ],
170 },
171 },
172 };
173 }
174 function toCreateEntryRequestMessageSignature(messageSignature, publicKey) {
175 return {
176 spec: {
177 $case: 'hashedRekordRequestV002',
178 hashedRekordRequestV002: {
179 digest: messageSignature.messageDigest.digest,
180 signature: {
181 content: messageSignature.signature,
182 verifier: {
183 // TODO: We need to add support of passing the key details in the
184 // signature bundle. For now we're hardcoding the key details here.
185 keyDetails: protobuf_specs_1.PublicKeyDetails.PKIX_ECDSA_P256_SHA_256,
186 verifier: {
187 $case: 'x509Certificate',
188 x509Certificate: {
189 rawBytes: util_1.pem.toDER(publicKey),
190 },
191 },
192 },
193 },
194 },
195 },
196 };
197 }