summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/sigstore/dist/sigstore.js
blob: cf8c90c309148e122f0d03852b7c0e79cbd53cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.verify = exports.attest = exports.sign = exports.DEFAULT_REKOR_URL = exports.DEFAULT_FULCIO_URL = exports.utils = void 0;
/*
Copyright 2023 The Sigstore Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const ca_1 = require("./ca");
const identity_1 = __importDefault(require("./identity"));
const sign_1 = require("./sign");
const tlog_1 = require("./tlog");
const tuf = __importStar(require("./tuf"));
const sigstore = __importStar(require("./types/sigstore"));
const util_1 = require("./util");
const verify_1 = require("./verify");
exports.utils = __importStar(require("./sigstore-utils"));
exports.DEFAULT_FULCIO_URL = 'https://fulcio.sigstore.dev';
exports.DEFAULT_REKOR_URL = 'https://rekor.sigstore.dev';
function createCAClient(options) {
    return new ca_1.CAClient({
        fulcioBaseURL: options.fulcioURL || exports.DEFAULT_FULCIO_URL,
    });
}
function createTLogClient(options) {
    return new tlog_1.TLogClient({
        rekorBaseURL: options.rekorURL || exports.DEFAULT_REKOR_URL,
    });
}
const tufCacheDir = util_1.appdata.appDataPath('sigstore-js');
async function sign(payload, options = {}) {
    const ca = createCAClient(options);
    const tlog = createTLogClient(options);
    const idps = configureIdentityProviders(options);
    const signer = new sign_1.Signer({
        ca,
        tlog,
        identityProviders: idps,
    });
    const bundle = await signer.signBlob(payload);
    return sigstore.Bundle.toJSON(bundle);
}
exports.sign = sign;
async function attest(payload, payloadType, options = {}) {
    const ca = createCAClient(options);
    const tlog = createTLogClient(options);
    const idps = configureIdentityProviders(options);
    const signer = new sign_1.Signer({
        ca,
        tlog,
        identityProviders: idps,
    });
    const bundle = await signer.signAttestation(payload, payloadType);
    return sigstore.Bundle.toJSON(bundle);
}
exports.attest = attest;
async function verify(bundle, payload, options = {}) {
    const trustedRoot = await tuf.getTrustedRoot(tufCacheDir, {
        mirrorURL: options.tufMirrorURL,
        rootPath: options.tufRootPath,
    });
    const verifier = new verify_1.Verifier(trustedRoot, options.keySelector);
    const deserializedBundle = sigstore.bundleFromJSON(bundle);
    const opts = collectArtifactVerificationOptions(options);
    return verifier.verify(deserializedBundle, opts, payload);
}
exports.verify = verify;
// Translates the IdenityProviderOptions into a list of Providers which
// should be queried to retrieve an identity token.
function configureIdentityProviders(options) {
    const idps = [];
    const token = options.identityToken;
    // If an explicit identity token is provided, use that. Setup a dummy
    // provider that just returns the token. Otherwise, setup the CI context
    // provider and (optionally) the OAuth provider.
    if (token) {
        idps.push({ getToken: () => Promise.resolve(token) });
    }
    else {
        idps.push(identity_1.default.ciContextProvider());
        if (options.oidcIssuer && options.oidcClientID) {
            idps.push(identity_1.default.oauthProvider({
                issuer: options.oidcIssuer,
                clientID: options.oidcClientID,
                clientSecret: options.oidcClientSecret,
                redirectURL: options.oidcRedirectURL,
            }));
        }
    }
    return idps;
}
// Assembles the AtifactVerificationOptions from the supplied VerifyOptions.
function collectArtifactVerificationOptions(options) {
    // The trusted signers are only used if the options contain a certificate
    // issuer
    let signers;
    if (options.certificateIssuer) {
        let san = undefined;
        if (options.certificateIdentityEmail) {
            san = {
                type: sigstore.SubjectAlternativeNameType.EMAIL,
                identity: {
                    $case: 'value',
                    value: options.certificateIdentityEmail,
                },
            };
        }
        else if (options.certificateIdentityURI) {
            san = {
                type: sigstore.SubjectAlternativeNameType.URI,
                identity: {
                    $case: 'value',
                    value: options.certificateIdentityURI,
                },
            };
        }
        const oids = Object.entries(options.certificateOIDs || {}).map(([oid, value]) => ({
            oid: { id: oid.split('.').map((s) => parseInt(s, 10)) },
            value: Buffer.from(value),
        }));
        signers = {
            $case: 'certificateIdentities',
            certificateIdentities: {
                identities: [
                    {
                        issuer: options.certificateIssuer,
                        san: san,
                        oids: oids,
                    },
                ],
            },
        };
    }
    // Construct the artifact verification options w/ defaults
    return {
        ctlogOptions: {
            disable: false,
            threshold: options.ctLogThreshold || 1,
            detachedSct: false,
        },
        tlogOptions: {
            disable: false,
            threshold: options.tlogThreshold || 1,
            performOnlineVerification: false,
        },
        signers,
    };
}