Debian Codegraph @

node_modules/@sigstore/sign/dist/signer/fulcio/index.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.FulcioSigner = exports.DEFAULT_FULCIO_URL = void 0;
4 /*
5 Copyright 2023 The Sigstore Authors.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 */
19 const error_1 = require("../../error");
20 const util_1 = require("../../util");
21 const ca_1 = require("./ca");
22 const ephemeral_1 = require("./ephemeral");
23 exports.DEFAULT_FULCIO_URL = 'https://fulcio.sigstore.dev';
24 // Signer implementation which can be used to decorate another signer
25 // with a Fulcio-issued signing certificate for the signer's public key.
26 // Must be instantiated with an identity provider which can provide a JWT
27 // which represents the identity to be bound to the signing certificate.
28 class FulcioSigner {
29 ca;
30 identityProvider;
31 keyHolder;
32 constructor(options) {
33 this.ca = new ca_1.CAClient({
34 ...options,
35 fulcioBaseURL: options.fulcioBaseURL || /* istanbul ignore next */ exports.DEFAULT_FULCIO_URL,
36 });
37 this.identityProvider = options.identityProvider;
38 this.keyHolder = options.keyHolder || new ephemeral_1.EphemeralSigner();
39 }
40 async sign(data) {
41 // Retrieve identity token from the supplied identity provider
42 const identityToken = await this.getIdentityToken();
43 // Extract challenge claim from OIDC token
44 let subject;
45 try {
46 subject = util_1.oidc.extractJWTSubject(identityToken);
47 }
48 catch (err) {
49 throw new error_1.InternalError({
50 code: 'IDENTITY_TOKEN_PARSE_ERROR',
51 message: `invalid identity token: ${identityToken}`,
52 cause: err,
53 });
54 }
55 // Construct challenge value by signing the subject claim
56 const challenge = await this.keyHolder.sign(Buffer.from(subject));
57 if (challenge.key.$case !== 'publicKey') {
58 throw new error_1.InternalError({
59 code: 'CA_CREATE_SIGNING_CERTIFICATE_ERROR',
60 message: 'unexpected format for signing key',
61 });
62 }
63 // Create signing certificate
64 const certificates = await this.ca.createSigningCertificate(identityToken, challenge.key.publicKey, challenge.signature);
65 // Generate artifact signature
66 const signature = await this.keyHolder.sign(data);
67 // Specifically returning only the first certificate in the chain
68 // as the key.
69 return {
70 signature: signature.signature,
71 key: {
72 $case: 'x509Certificate',
73 certificate: certificates[0],
74 },
75 };
76 }
77 async getIdentityToken() {
78 try {
79 return await this.identityProvider.getToken();
80 }
81 catch (err) {
82 throw new error_1.InternalError({
83 code: 'IDENTITY_TOKEN_READ_ERROR',
84 message: 'error retrieving identity token',
85 cause: err,
86 });
87 }
88 }
89 }
90 exports.FulcioSigner = FulcioSigner;