codekingpro/portable-devtools
114k
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.DEFAULT_TIMEOUT = exports.DEFAULT_RETRY = void 0;
4exports.createBundleBuilder = createBundleBuilder;
5exports.createKeyFinder = createKeyFinder;
6exports.createVerificationPolicy = createVerificationPolicy;
7/*
8Copyright 2023 The Sigstore Authors.
9
10Licensed under the Apache License, Version 2.0 (the "License");
11you may not use this file except in compliance with the License.
12You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16Unless required by applicable law or agreed to in writing, software
17distributed under the License is distributed on an "AS IS" BASIS,
18WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19See the License for the specific language governing permissions and
20limitations under the License.
21*/
22const core_1 = require("@sigstore/core");
23const sign_1 = require("@sigstore/sign");
24const verify_1 = require("@sigstore/verify");
25exports.DEFAULT_RETRY = { retries: 2 };
26exports.DEFAULT_TIMEOUT = 5000;
27function createBundleBuilder(bundleType, options) {
28 const bundlerOptions = {
29 signer: initSigner(options),
30 witnesses: initWitnesses(options),
31 };
32 switch (bundleType) {
33 case 'messageSignature':
34 return new sign_1.MessageSignatureBundleBuilder(bundlerOptions);
35 case 'dsseEnvelope':
36 return new sign_1.DSSEBundleBuilder({
37 ...bundlerOptions,
38 certificateChain: options.legacyCompatibility,
39 });
40 }
41}
42// Translates the public KeySelector type into the KeyFinderFunc type needed by
43// the verifier.
44function createKeyFinder(keySelector) {
45 return (hint) => {
46 const key = keySelector(hint);
47 if (!key) {
48 throw new verify_1.VerificationError({
49 code: 'PUBLIC_KEY_ERROR',
50 message: `key not found: ${hint}`,
51 });
52 }
53 return {
54 publicKey: core_1.crypto.createPublicKey(key),
55 validFor: () => true,
56 };
57 };
58}
59function createVerificationPolicy(options) {
60 const policy = {};
61 const san = options.certificateIdentityEmail || options.certificateIdentityURI;
62 if (san) {
63 policy.subjectAlternativeName = san;
64 }
65 if (options.certificateIssuer) {
66 policy.extensions = { issuer: options.certificateIssuer };
67 }
68 return policy;
69}
70// Instantiate the FulcioSigner based on the supplied options.
71function initSigner(options) {
72 return new sign_1.FulcioSigner({
73 fulcioBaseURL: options.fulcioURL,
74 identityProvider: options.identityProvider || initIdentityProvider(options),
75 retry: options.retry ?? exports.DEFAULT_RETRY,
76 timeout: options.timeout ?? exports.DEFAULT_TIMEOUT,
77 });
78}
79// Instantiate an identity provider based on the supplied options. If an
80// explicit identity token is provided, use that. Otherwise, use the CI
81// context provider.
82function initIdentityProvider(options) {
83 const token = options.identityToken;
84 if (token) {
85 /* istanbul ignore next */
86 return { getToken: () => Promise.resolve(token) };
87 }
88 else {
89 return new sign_1.CIContextProvider('sigstore');
90 }
91}
92// Instantiate a collection of witnesses based on the supplied options.
93function initWitnesses(options) {
94 const witnesses = [];
95 if (isRekorEnabled(options)) {
96 witnesses.push(new sign_1.RekorWitness({
97 rekorBaseURL: options.rekorURL,
98 entryType: options.legacyCompatibility ? 'intoto' : 'dsse',
99 fetchOnConflict: false,
100 retry: options.retry ?? exports.DEFAULT_RETRY,
101 timeout: options.timeout ?? exports.DEFAULT_TIMEOUT,
102 }));
103 }
104 if (isTSAEnabled(options)) {
105 witnesses.push(new sign_1.TSAWitness({
106 tsaBaseURL: options.tsaServerURL,
107 retry: options.retry ?? exports.DEFAULT_RETRY,
108 timeout: options.timeout ?? exports.DEFAULT_TIMEOUT,
109 }));
110 }
111 return witnesses;
112}
113// Type assertion to ensure that Rekor is enabled
114function isRekorEnabled(options) {
115 return options.tlogUpload !== false;
116}
117// Type assertion to ensure that TSA is enabled
118function isTSAEnabled(options) {
119 return options.tsaServerURL !== undefined;
120}
121 